parser.c 10.8 KB
Newer Older
Linus Tolke Y's avatar
Linus Tolke Y committed
1
/*
2
 * $Id: parser.c,v 0.13 1998/07/08 13:41:33 ceder Exp $
3
 * Copyright (C) 1990, 1991, 1993, 1994, 1995  Lysator Academic Computer Association.
Linus Tolke Y's avatar
Linus Tolke Y committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * 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. 
 */
Per Cederqvist's avatar
.  
Per Cederqvist committed
25 26 27 28
/*
 *  client/parser.c  --  Routines to parse commands
 *
 *
29
 *  Copyright (C) 1990, 1991, 1993, 1994, 1995	Lysator Computer Club,
Per Cederqvist's avatar
.  
Per Cederqvist committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
 *			Linkoping University,  Sweden
 *
 *  Everyone is granted permission to copy, modify and redistribute
 *  this code, provided the people they give it to can.
 *
 *
 *  Author:	Thomas Bellman
 *		Lysator Computer Club
 *		Linkoping University
 *		Sweden
 *
 *  email:	Bellman@Lysator.LiU.SE
 *
 *
 *  Any opinions expressed in this code are the author's PERSONAL opinions,
 *  and does NOT, repeat NOT, represent any official standpoint of Lysator,
 *  even if so stated.
 */


50 51
static const char *
rcsid = "$Id: parser.c,v 0.13 1998/07/08 13:41:33 ceder Exp $";
Per Cederqvist's avatar
Per Cederqvist committed
52 53 54
#include "rcs.h"
USE(rcsid);

Per Cederqvist's avatar
Per Cederqvist committed
55 56 57
#ifdef HAVE_STDDEF_H
#  include <stddef.h>
#endif
Per Cederqvist's avatar
Per Cederqvist committed
58
#include <sys/types.h>
Per Cederqvist's avatar
.  
Per Cederqvist committed
59

Per Cederqvist's avatar
Per Cederqvist committed
60 61
#include "s-string.h"
#include "s-collat-tabs.h"
Per Cederqvist's avatar
.  
Per Cederqvist committed
62 63 64 65 66 67 68
#include "parser.h"

#define EXPORT			/* To emphasize export of objects */
#define PRIVATE		static


#if defined (SERVER)
Per Cederqvist's avatar
Per Cederqvist committed
69
#	include "server/smalloc.h"
Per Cederqvist's avatar
.  
Per Cederqvist committed
70 71 72 73
#	define	MALLOC	smalloc
#	define	REALLOC	srealloc
#	define	FREE	sfree
#elif defined (CLIENT)
Per Cederqvist's avatar
Per Cederqvist committed
74
#	include	"zmalloc.h"
Per Cederqvist's avatar
.  
Per Cederqvist committed
75 76 77 78
#	define	MALLOC	zmalloc
#	define	REALLOC	zrealloc
#	define	FREE	zfree
#else
Per Cederqvist's avatar
Per Cederqvist committed
79
#	include	<stdlib.h>
Per Cederqvist's avatar
.  
Per Cederqvist committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
#	define  MALLOC  malloc
#	define  REALLOC realloc
#	define  FREE	free
#endif



/*
 *  Remove paranthesized "expressions" from the string STR.
 *  E g if STR is "Foo (Bar (vfslck)) Gazonk", then it is reduced
 *  to "Foo _ Gazonk", where '_' is the character in SEPARATOR.
 *  Superflous close paranthesis are disregarded.
 */
EXPORT  void
remove_parenthesis (String	* str,
		    char	  blanker)

{
    String_size		  i;	/* Index in loop */
    int			  nesting_depth;
    

    /* Match parantheses.  Remove text inside parantheses. */
    nesting_depth = 0;
    for ( i = 0 ;  i < s_strlen(*str) ;  i++ )
    {
	if (str->string[i] == '(')
	    nesting_depth++;

	if (str->string[i] == ')')
	{
	    nesting_depth--;
	    str->string[i] = blanker;		/* Don't forget that... */
	    if (nesting_depth < 0)
		nesting_depth = 0;
	}

	if (nesting_depth > 0)
	    str->string[i] = blanker;
    }

    return;
}




/*
 *  Convert a String to a list of tokens (words).  This list is
 *  easier to parse than a string (since the string would have to
 *  be tokenized first anyway).  The last entry is EMPTY_STRING.
 *  Returns NULL if any error occured (couldn't allocate memory).
Per Cederqvist's avatar
Per Cederqvist committed
132 133 134
 *  The result from this function should be freed when it is no longer
 *  used by a FREE(Parse_token *).  Note that String (Parse_token *)[X].word
 *  points into source, and should thus not be freed. 
Per Cederqvist's avatar
.  
Per Cederqvist committed
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
 */
EXPORT  Parse_token *
tokenize (const String	  source,
	  const String	  separators)

{
    Parse_token		* tokens		= NULL;
    Parse_token		* temp_list;		/* Temporary */
    String		  work_string		= EMPTY_STRING;
    String		  a_token;
    String		  a_temp_token;		/* More temporaries... */
    int			  no_of_tokens;
    int			  list_size;
    String_size		  pos_in_string;
    const int		  chunk_size		= 10;



    /* Copy string to working area */
    if (s_strcpy(&work_string, source) == FAILURE)
    {
	/* Couldn't allocate space for temporary string. */
	return  NULL;
    }

    remove_parenthesis (&work_string, separators.string[0]);

    no_of_tokens = 0;
    list_size = 0;
    pos_in_string = 0;

Per Cederqvist's avatar
Per Cederqvist committed
166 167
    while (a_token = s_strtok (work_string, &pos_in_string, separators),
	   s_empty(a_token) == FALSE)
Per Cederqvist's avatar
.  
Per Cederqvist committed
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
    {
	/* Make the token point into the original source string
	 * instead of the working string			*/
	a_temp_token = s_fsubstr (source,
				  pos_in_string - s_strlen (a_token),
				  pos_in_string - 1);
#if 0
// *	/* Get a real copy of the word */
// *	a_temp_token = EMPTY_STRING;
// *	if (s_strcpy(&a_temp_token, a_token) == FAILURE)
// *	{
// *	    /* Grumble... */
// *	    free_tokens (tokens);
// *	    s_clear(&work_string);
// *	    return  NULL;
// *	}
#endif

	/* Is the allocated list large enough? */
	if (no_of_tokens++ >= list_size)
	{
	    /* No, allocate more */
	    temp_list = REALLOC (tokens, (list_size += chunk_size)
					  * sizeof(Parse_token));
	    if (temp_list == NULL)
	    {
		/* Sigh. Out of memory. */
		free_tokens (tokens);
		s_clear (&work_string);
		return  NULL;
	    }
	    else
	    {
		/* OK, we got what we asked for */
		tokens = temp_list;
	    }
	}

	/* Insert the new token in the list */
	tokens [no_of_tokens-1].word = a_temp_token;
	tokens [no_of_tokens-1].start_in_string =
	    pos_in_string - s_strlen(a_temp_token);
    }

    s_clear (&work_string);

    /* Is there room in the list for the 'stop' element? */
    if (list_size <= no_of_tokens)
    {
	/* No, get some more memory */
	temp_list = REALLOC (tokens, (++list_size) * sizeof(Parse_token));
	if (temp_list == NULL)
	{
	    /* Sigh. Out of memory. */
	    free_tokens (tokens);
	    return  NULL;
	}
	else
	{
	    /* OK, we got what we asked for */
	    tokens = temp_list;
	}
    }

    /* OK, insert the 'stop' element. */
    tokens [no_of_tokens].word = EMPTY_STRING;
    tokens [no_of_tokens].start_in_string = END_OF_STRING;

    return  tokens;
}




/*
 *  Count the number of tokens (words) in TOKEN_LIST.  Used to
 *  set the NUMBER_OF_WORDS field in a 'Matching_info' object.
 */
extern  int
count_words (const Parse_token   * token_list)

{
    int		no_of_words;

    no_of_words = 0;
    while (! s_empty(token_list++ -> word))
	no_of_words++;

    return  no_of_words;
}




/*
 *  Free the list of tokens (// and the strings they are pointing to //).
 *  Free:ing NULL is a no-op.
 */
EXPORT  void
free_tokens (Parse_token   * token_list)

{
    if (token_list != NULL)
    {
	FREE (token_list);
    }
}




/*
 *  Returns the number of the first word of SOURCE that does
 *  not match PATTERN.  A word "foo" in SOURCE matches "foobar"
 *  in PATTERN, but not vice versa.
 */
EXPORT  int
match (Parse_token	* source,
       Parse_token	* pattern,
       char		  collat_tab [ COLLAT_TAB_SIZE ] )

{
    int		word_no;

    word_no = 0;
    while (   (! s_streq (pattern[word_no].word, EMPTY_STRING))
	   && (! s_streq (source[word_no].word, EMPTY_STRING))
	   && (s_usr_strhead (source[word_no].word,
			      pattern[word_no].word,
Per Cederqvist's avatar
Per Cederqvist committed
297
			      collat_tab) == TRUE))
Per Cederqvist's avatar
.  
Per Cederqvist committed
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
    {
	word_no++;
    }

    return  word_no;
}




/*
 *  Searches for a matching string in the table 'match_table'.
 *  Some weird pattern matching is done.
 *  parse().no_of_matches is -1 if an error occured (out of
 *  memory).
 *
 *  What?  You want a description of how it matches?  Forget it!   BUG!
 *  Try for yourself, and you'll find out!
 */

EXPORT  Parse_info
parse (String		  source_string,
       Matching_info	* match_table,
       Bool		  allow_trailing_words,
       Bool		  number_of_words_must_match,
       String		  separators,
       char		  collat_tab [ COLLAT_TAB_SIZE ] )

{
    Parse_info		  answer;
    int			* temp_indexes;
    int			  index;
    int			  size_of_index_list;
    Parse_token		* source_words;
    int			  no_of_source_words;
    int			  first_non_matching;
    int			  best_match;
    int			  highest_priority;

    const int		  chunk_size	= 20;

    

    source_words = tokenize(source_string, separators);
    if (source_words == NULL)
    {
	answer.no_of_matches = -1;
	return  answer;
    }

    no_of_source_words = count_words(source_words);

    /* Check if SOURCE_STRING was empty of words */
    if (no_of_source_words == 0)
    {
Per Cederqvist's avatar
Per Cederqvist committed
353
	FREE (source_words);
Per Cederqvist's avatar
.  
Per Cederqvist committed
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
	answer.indexes = MALLOC (1 * sizeof(int));
	if (answer.indexes == NULL)
	{
	    /* Gahh! Someone eats memory! */
	    answer.no_of_matches = -1;
	    return  answer;
	}
	answer.indexes[0] = -1;
	answer.no_of_matches = 1;
	return  answer;
    }


    answer.no_of_matches = 0;
    answer.indexes = NULL;
    size_of_index_list = 0;
    index = -1;
    best_match = 1;			/* At least one word */
    highest_priority = 1;
    while (! s_empty (match_table[++index].name))
    {
	first_non_matching = match (source_words, match_table[index].tokens,
				    collat_tab);

	if (   (    ! allow_trailing_words
		&&  first_non_matching < no_of_source_words)
Per Cederqvist's avatar
Per Cederqvist committed
380
	    || (    number_of_words_must_match == TRUE
Per Cederqvist's avatar
.  
Per Cederqvist committed
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
		&&  first_non_matching != count_words (match_table[index].tokens)))
	{
	    continue;			/* Try next entry in table */
	}

	if (first_non_matching < best_match)
	    continue;			/* Try next entry in table */

	if (    first_non_matching == best_match
	    &&  highest_priority > match_table[index].priority)
	    continue;

	/*  If we reach this far, then we have a match that should be
	 *  inserted in the table.  But if it is a better match than any
	 *  before, then we clear the table first.			*/
	if (    first_non_matching > best_match
	    ||  match_table[index].priority > highest_priority)
	{
	    highest_priority = match_table[index].priority;
	    best_match = first_non_matching;
	    answer.no_of_matches = 0;
	}

	/* Insert the match in the table */

	/* Increase the size if necessary */
	if (answer.no_of_matches >= size_of_index_list)
	{
	    temp_indexes = REALLOC (answer.indexes,
				    (size_of_index_list += chunk_size)
				    * sizeof(answer.indexes));
	    if (temp_indexes == NULL)
	    {
		/* Grumble!  Out of memory. */
Per Cederqvist's avatar
Per Cederqvist committed
415
		FREE (source_words);
Per Cederqvist's avatar
.  
Per Cederqvist committed
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
		FREE (answer.indexes);
		answer.no_of_matches = -1;
		return  answer;
	    }

	    answer.indexes = temp_indexes;
	}

	highest_priority = match_table [index].priority;
	answer.indexes[answer.no_of_matches] = index;
	/*  Find out where the arguments start.
	 *  This value should not be used if more than one match is found.
	 */
	/* Special hack needed if no parameters */
	if (s_empty (source_words [first_non_matching].word))
	    answer.arguments = EMPTY_STRING;
	else
	    answer.arguments =
		s_fsubstr(source_string,
			  source_words[first_non_matching].
			  start_in_string,
			  END_OF_STRING);
	answer.no_of_matches++;
    }

    /* All matches found by now */

    /* Strip trailing blanks from the argument */
    if (answer.no_of_matches == 1)
	answer.arguments = s_strip_trailing (answer.arguments, separators);

Per Cederqvist's avatar
Per Cederqvist committed
447
    FREE (source_words);
Per Cederqvist's avatar
.  
Per Cederqvist committed
448 449 450
    return  answer;

}   /* END: parse() */