regex-match.c 3.06 KB
Newer Older
1
/*
Per Cederqvist's avatar
Per Cederqvist committed
2
 * $Id: regex-match.c,v 1.6 1993/10/13 00:37:27 ceder Exp $
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 * Copyright (C) 1992  Lysator Academic Computer Association.
 *
 * This file is part of the LysKOM server.
 * 
 * LysKOM is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 1, or (at your option) 
 * any later version.
 * 
 * LysKOM is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with LysKOM; see the file COPYING.  If not, write to
 * Lysator, c/o ISY, Linkoping University, S-581 83 Linkoping, SWEDEN,
 * or the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, 
 * MA 02139, USA.
 *
 * Please mail bug reports to bug-lyskom@lysator.liu.se. 
 */

/*
 * Regexp matching
 */

Per Cederqvist's avatar
Per Cederqvist committed
30
static char *rcsid = "$Id: regex-match.c,v 1.6 1993/10/13 00:37:27 ceder Exp $";
31
32
#include "rcs.h"
USE(rcsid);
33

Per Cederqvist's avatar
Per Cederqvist committed
34
35
#include <stdio.h>
#include <setjmp.h>
Per Cederqvist's avatar
Per Cederqvist committed
36
#include <sys/types.h>
37
38

#include "s-string.h"
Per Cederqvist's avatar
Per Cederqvist committed
39
40
41
42
43
#include "misc-types.h"
#include "kom-types.h"
#include "services.h"
#include "regex.h"
#include "server/smalloc.h"
Per Cederqvist's avatar
Per Cederqvist committed
44
#include "cache.h"
Per Cederqvist's avatar
Per Cederqvist committed
45
#include "kom-errno.h"
Per Cederqvist's avatar
Per Cederqvist committed
46
47
#include "com.h"
#include "connections.h"
Per Cederqvist's avatar
Per Cederqvist committed
48
49
#include "manipulate.h"
#include "log.h"
50
51
52
53

static Success
lookup_regexp (const String  regexp,
	       Conf_no_list  *result,
Per Cederqvist's avatar
Per Cederqvist committed
54
	       Bool	     want_persons)
55
56
{
    struct re_pattern_buffer pat_buf;
Per Cederqvist's avatar
Per Cederqvist committed
57
58
    Conf_no conf_no;
    String name = EMPTY_STRING;
Per Cederqvist's avatar
Per Cederqvist committed
59
    const char *errmsg;
60
61
62
63
64
65

    /* +++ Unnecessary to allocate this much if only one conference matches. */
    result->conf_nos = tmp_alloc (cached_no_of_existing_conferences()
				  * sizeof(Conf_no));
    result->no_of_confs = 0;

Per Cederqvist's avatar
Per Cederqvist committed
66
67
    re_syntax_options = RE_SYNTAX_GREP;

68
    pat_buf.translate = swedish_collate_tab;
Per Cederqvist's avatar
Per Cederqvist committed
69
70
    pat_buf.translate = NULL;
    pat_buf.fastmap = 0;
71
72
73
    pat_buf.allocated = 0;
    pat_buf.buffer = 0;

Per Cederqvist's avatar
Per Cederqvist committed
74
75
76
    if ((errmsg =
	 re_compile_pattern(regexp.string, s_strlen(regexp), &pat_buf))
	!= NULL)
77
    {
Per Cederqvist's avatar
Per Cederqvist committed
78
	regfree(&pat_buf);
79
80
81
82
83
84
	kom_errno = KOM_REGEX_ERROR;
	return FAILURE;
    }

    for (conf_no = 0; (conf_no = traverse_conference(conf_no)) != 0;)
    {
Per Cederqvist's avatar
Per Cederqvist committed
85
86
	if (cached_get_conf_type(conf_no).letter_box == want_persons
	    &&  fast_access_perm (conf_no, ACTPERS, ACT_P) > none )
87
	{
Per Cederqvist's avatar
Per Cederqvist committed
88
89
	    name = cached_get_name(conf_no);
	    switch ( re_search (&pat_buf, name.string, s_strlen(name), 0,
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
				s_strlen(name), NULL) )
	    {
	    case -1:
		break;
	    case -2:
		log("Internal error in regex.");
		break;
	    default:
		/* It matched. (Ignore where it matched). */
		result->conf_nos[result->no_of_confs++] = conf_no;
		break;
	    }
	}
    }

Per Cederqvist's avatar
Per Cederqvist committed
105
106
107
    regfree(&pat_buf);

    return OK;
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
}

Success
re_lookup_person (const String  regexp,
		  Conf_no_list  *result)
{
    return lookup_regexp(regexp, result, TRUE);
}


Success
re_lookup_conf (const String  regexp,
		Conf_no_list  *result)
{
    return lookup_regexp(regexp, result, FALSE);
}