LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / soltools / mkdepend / parse.c
bloba5c8273a27dd7f95461a544c9c786dd2b302eb08
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* $XConsortium: parse.c,v 1.30 94/04/17 20:10:38 gildea Exp $ */
3 /*
5 Copyright (c) 1993, 1994 X Consortium
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
14 The above copyright notice and this permission notice shall be included in
15 all copies or substantial portions of the Software.
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 Except as contained in this notice, the name of the X Consortium shall not be
25 used in advertising or otherwise to promote the sale, use or other dealings
26 in this Software without prior written authorization from the X Consortium.
30 #include <ctype.h>
32 #include "def.h"
33 static char *hash_lookup( char *symbol, struct symhash *symbols );
34 static int gobble( struct filepointer *filep, struct inclist *file,
35 struct inclist *file_red, struct symhash *symbols );
36 static int deftype ( char *line, struct filepointer *filep, struct inclist *file,
37 int parse_it, struct symhash *symbols);
38 static int zero_value(char const *exp, struct symhash *symbols);
40 extern struct symhash *maininclist;
42 int find_includes(struct filepointer *filep, struct inclist *file, struct inclist *file_red, int recursion, boolean failOK, struct IncludesCollection* incCollection, struct symhash *symbols)
44 char *line;
45 int type;
47 while ((line = get_line(filep))) {
48 type = deftype(line, filep, file, TRUE, symbols);
49 switch(type) {
50 case IF:
51 doif:
52 type = find_includes(filep, file,
53 file_red, recursion+1, failOK, incCollection, symbols);
54 while ((type == ELIF) || (type == ELIFFALSE) ||
55 (type == ELIFGUESSFALSE))
56 type = gobble(filep, file, file_red, symbols);
57 break;
58 case IFFALSE:
59 doiffalse:
60 type = gobble(filep, file, file_red, symbols);
61 if (type == ELIF)
62 goto doif;
63 else if ((type == ELIFFALSE) || (type == ELIFGUESSFALSE))
64 goto doiffalse;
65 break;
66 case ELIFFALSE:
67 case ELIFGUESSFALSE:
68 case ELIF:
69 if (!recursion)
70 gobble(filep, file, file_red, symbols);
71 if (recursion)
72 return type;
73 define(line, &symbols);
74 break;
75 case ERROR:
76 warning("%s: %d: %s\n", file_red->i_file,
77 filep->f_line, line);
78 break;
80 case -1:
81 warning("%s", file_red->i_file);
82 if (file_red != file)
83 warning1(" (reading %s)", file->i_file);
84 warning1(", line %d: unknown directive == \"%s\"\n",
85 filep->f_line, line);
86 break;
87 case -2:
88 warning("%s", file_red->i_file);
89 if (file_red != file)
90 warning1(" (reading %s)", file->i_file);
91 warning1(", line %d: incomplete include == \"%s\"\n",
92 filep->f_line, line);
93 break;
96 // coverity[leaked_storage] - on purpose
97 return -1;
100 int gobble(struct filepointer *filep,
101 struct inclist *file,
102 struct inclist *file_red,
103 struct symhash *symbols)
105 char *line;
106 int type;
108 while ((line = get_line(filep))) {
109 type = deftype(line, filep, file, FALSE, symbols);
110 switch(type) {
111 case IF:
112 case IFFALSE:
113 type = gobble(filep, file, file_red, symbols);
114 while ((type == ELIF) || (type == ELIFFALSE) ||
115 (type == ELIFGUESSFALSE))
116 type = gobble(filep, file, file_red, symbols);
117 break;
118 case ERROR:
119 break;
120 case ELIF:
121 case ELIFFALSE:
122 case ELIFGUESSFALSE:
123 return type;
124 case -1:
125 warning("%s, line %d: unknown directive == \"%s\"\n",
126 file_red->i_file, filep->f_line, line);
127 break;
130 return -1;
134 * Decide what type of # directive this line is.
136 int deftype (char *line, struct filepointer *filep, struct inclist * file,
137 int parse_it, struct symhash *symbols)
139 char *p;
140 char *directive, savechar;
141 int ret;
142 (void)file; // used in DEBUG mode
143 (void)filep;
145 * Parse the directive...
147 directive=line+1;
148 while (*directive == ' ' || *directive == '\t')
149 directive++;
151 p = directive;
152 while (*p >= 'a' && *p <= 'z')
153 p++;
154 savechar = *p;
155 *p = '\0';
156 ret = match(directive, directives);
157 *p = savechar;
159 /* If we don't recognize this compiler directive or we happen to just
160 * be gobbling up text while waiting for an #endif or #elif or #else
161 * in the case of an #elif we must check the zero_value and return an
162 * ELIF or an ELIFFALSE.
165 if (ret == ELIF && !parse_it)
167 while (*p == ' ' || *p == '\t')
168 p++;
170 * parse an expression.
172 debug(0,("%s, line %d: #elif %s ",
173 file->i_file, filep->f_line, p));
174 ret = zero_value(p, symbols);
175 if (ret != IF)
177 debug(0,("false...\n"));
178 if (ret == IFFALSE)
179 return ELIFFALSE;
180 else
181 return ELIFGUESSFALSE;
183 else
185 debug(0,("true...\n"));
186 return ELIF;
190 if (ret < 0 || ! parse_it)
191 return ret;
194 * now decide how to parse the directive, and do it.
196 while (*p == ' ' || *p == '\t')
197 p++;
198 switch (ret) {
199 case IF:
201 * parse an expression.
203 ret = zero_value(p, symbols);
204 debug(0,("%s, line %d: %s #if %s\n",
205 file->i_file, filep->f_line, ret?"false":"true", p));
206 break;
207 case ELIF:
208 case ERROR:
209 debug(0,("%s, line %d: #%s\n",
210 file->i_file, filep->f_line, directives[ret]));
212 * nothing to do.
214 break;
216 return ret;
220 * HACK! - so that we do not have to introduce 'symbols' in each cppsetup.c
221 * function... It's safe, functions from cppsetup.c don't return here.
223 static struct symhash *global_symbols = NULL;
225 char * isdefined( char *symbol )
227 return hash_lookup( symbol, global_symbols );
231 * Return type based on if the #if expression evaluates to 0
233 int zero_value(char const *exp, struct symhash *symbols)
235 global_symbols = symbols; /* HACK! see above */
236 if (cppsetup(exp))
237 return IFFALSE;
238 else
239 return IF;
242 void define( char *def, struct symhash **symbols )
244 char *val;
246 /* Separate symbol name and its value */
247 val = def;
248 while (isalnum((unsigned char)*val) || *val == '_')
249 val++;
250 if (*val)
251 *val++ = '\0';
252 while (*val == ' ' || *val == '\t')
253 val++;
255 if (!*val)
256 val = "1";
257 hash_define( def, val, symbols );
260 static int hash( char *str )
262 /* Hash (Kernighan and Ritchie) */
263 unsigned int hashval = 0;
265 for ( ; *str; str++ )
267 hashval = ( hashval * SYMHASHSEED ) + ( *str );
270 return hashval & ( SYMHASHMEMBERS - 1 );
273 struct symhash *hash_copy( struct symhash *symbols )
275 int i;
276 struct symhash *newsym;
277 if ( !symbols )
278 return NULL;
280 newsym = (struct symhash *) malloc( sizeof( struct symhash ) );
282 for ( i = 0; i < SYMHASHMEMBERS; ++i )
284 if ( !symbols->s_pairs[ i ] )
285 newsym->s_pairs[ i ] = NULL;
286 else
288 struct pair *it = symbols->s_pairs[ i ];
289 struct pair *nw = newsym->s_pairs[ i ] = (struct pair*) malloc( sizeof( struct pair ) );
290 nw->p_name = it->p_name;
291 nw->p_value = it->p_value;
292 nw->p_next = NULL;
294 while ( it->p_next )
296 nw->p_next = (struct pair*) malloc( sizeof( struct pair ) );
297 it = it->p_next;
298 nw = nw->p_next;
299 nw->p_name = it->p_name;
300 nw->p_value = it->p_value;
301 nw->p_next = NULL;
305 return newsym;
308 void hash_free( struct symhash *symbols )
310 int i;
312 if ( !symbols )
313 return;
315 for ( i = 0; i < SYMHASHMEMBERS; ++i )
317 struct pair *it = symbols->s_pairs[ i ];
318 struct pair *next;
319 while ( it )
321 next = it->p_next;
322 free( it );
323 it = next;
326 free( symbols->s_pairs );
329 void hash_define( char *name, char const *val, struct symhash **symbols )
331 int hashval;
332 struct pair *it;
334 if ( !symbols )
335 return;
337 /* Make space if it's needed */
338 if ( *symbols == NULL )
340 int i;
342 *symbols = (struct symhash *) malloc( sizeof( struct symhash ) );
343 if ( *symbols == NULL )
344 fatalerr( "malloc()/realloc() failure in insert_defn()\n" );
346 for ( i = 0; i < SYMHASHMEMBERS; ++i )
347 (*symbols)->s_pairs[i] = NULL;
350 hashval = hash( name );
351 it = (*symbols)->s_pairs[ hashval ];
353 /* Replace/insert the symbol */
354 if ( it == NULL )
356 it = (*symbols)->s_pairs[ hashval ] = (struct pair*) malloc( sizeof( struct pair ) );
357 it->p_name = copy( name );
358 it->p_value = copy( val );
359 it->p_next = NULL;
361 else if ( strcmp( it->p_name, name ) == 0 )
363 it->p_value = copy( val );
365 else
367 while ( it->p_next && ( strcmp( it->p_next->p_name, name ) != 0 ) )
369 it = it->p_next;
371 if ( it->p_next )
372 it->p_next->p_name = copy( name );
373 else
375 it->p_next = (struct pair*) malloc( sizeof( struct pair ) );
376 it->p_next->p_name = copy( name );
377 it->p_next->p_value = copy( val );
378 it->p_next->p_next = NULL;
383 char *hash_lookup( char *symbol, struct symhash *symbols )
385 struct pair *it;
387 if ( !symbols )
388 return NULL;
390 it = symbols->s_pairs[ hash( symbol ) ];
392 while ( it && ( strcmp( it->p_name, symbol ) != 0 ) )
394 it = it->p_next;
396 if ( it )
397 return it->p_value;
399 return NULL;
402 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */