4 * Copyright 1993 Robert J. Amstadt
5 * Copyright 1995 Martin von Loewis
6 * Copyright 1995, 1996, 1997 Alexandre Julliard
7 * Copyright 1997 Eric Youngdale
8 * Copyright 1999 Ulrich Weigand
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include "wine/port.h"
41 static char ParseBuffer[512];
42 static char TokenBuffer[512];
43 static char *ParseNext = ParseBuffer;
44 static FILE *input_file;
46 static const char * const TypeNames[TYPE_NBTYPES] =
48 "variable", /* TYPE_VARIABLE */
49 "pascal", /* TYPE_PASCAL */
50 "equate", /* TYPE_ABS */
51 "stub", /* TYPE_STUB */
52 "stdcall", /* TYPE_STDCALL */
53 "cdecl", /* TYPE_CDECL */
54 "varargs", /* TYPE_VARARGS */
55 "extern" /* TYPE_EXTERN */
58 static const char * const FlagNames[] =
60 "norelay", /* FLAG_NORELAY */
61 "noname", /* FLAG_NONAME */
62 "ret16", /* FLAG_RET16 */
63 "ret64", /* FLAG_RET64 */
64 "i386", /* FLAG_I386 */
65 "register", /* FLAG_REGISTER */
66 "interrupt", /* FLAG_INTERRUPT */
67 "private", /* FLAG_PRIVATE */
71 static int IsNumberString(const char *s)
73 while (*s) if (!isdigit(*s++)) return 0;
77 inline static int is_token_separator( char ch )
79 return (ch == '(' || ch == ')' || ch == '-');
82 /* get the next line from the input file, or return 0 if at eof */
83 static int get_next_line(void)
85 ParseNext = ParseBuffer;
87 return (fgets(ParseBuffer, sizeof(ParseBuffer), input_file) != NULL);
90 static const char * GetToken( int allow_eol )
93 char *token = TokenBuffer;
97 /* remove initial white space */
99 while (isspace(*p)) p++;
101 if (*p == '\\' && p[1] == '\n') /* line continuation */
103 if (!get_next_line())
105 if (!allow_eol) error( "Unexpected end of file\n" );
112 if ((*p == '\0') || (*p == '#'))
114 if (!allow_eol) error( "Declaration not terminated properly\n" );
121 if (is_token_separator(*p))
123 /* a separator is always a complete token */
126 else while (*p != '\0' && !is_token_separator(*p) && !isspace(*p))
129 if (*p) *token++ = *p++;
137 /*******************************************************************
140 * Parse a variable definition.
142 static int ParseVariable( ORDDEF *odp )
147 int value_array_size;
150 if (SpecType == SPEC_WIN32)
152 error( "'variable' not supported in Win32, use 'extern' instead\n" );
156 if (!(token = GetToken(0))) return 0;
159 error( "Expected '(' got '%s'\n", token );
164 value_array_size = 25;
165 value_array = xmalloc(sizeof(*value_array) * value_array_size);
169 if (!(token = GetToken(0)))
177 value_array[n_values++] = strtol(token, &endptr, 0);
178 if (n_values == value_array_size)
180 value_array_size += 25;
181 value_array = xrealloc(value_array,
182 sizeof(*value_array) * value_array_size);
185 if (endptr == NULL || *endptr != '\0')
187 error( "Expected number value, got '%s'\n", token );
193 odp->u.var.n_values = n_values;
194 odp->u.var.values = xrealloc(value_array, sizeof(*value_array) * n_values);
199 /*******************************************************************
200 * ParseExportFunction
202 * Parse a function definition.
204 static int ParseExportFunction( ORDDEF *odp )
212 if (odp->type == TYPE_STDCALL)
214 error( "'stdcall' not supported for Win16\n" );
219 if (odp->type == TYPE_PASCAL)
221 error( "'pascal' not supported for Win32\n" );
224 if (odp->flags & FLAG_INTERRUPT)
226 error( "'interrupt' not supported for Win32\n" );
234 if (!(token = GetToken(0))) return 0;
237 error( "Expected '(' got '%s'\n", token );
241 for (i = 0; i < sizeof(odp->u.func.arg_types); i++)
243 if (!(token = GetToken(0))) return 0;
247 if (!strcmp(token, "word"))
248 odp->u.func.arg_types[i] = 'w';
249 else if (!strcmp(token, "s_word"))
250 odp->u.func.arg_types[i] = 's';
251 else if (!strcmp(token, "long") || !strcmp(token, "segptr"))
252 odp->u.func.arg_types[i] = 'l';
253 else if (!strcmp(token, "ptr"))
254 odp->u.func.arg_types[i] = 'p';
255 else if (!strcmp(token, "str"))
256 odp->u.func.arg_types[i] = 't';
257 else if (!strcmp(token, "wstr"))
258 odp->u.func.arg_types[i] = 'W';
259 else if (!strcmp(token, "segstr"))
260 odp->u.func.arg_types[i] = 'T';
261 else if (!strcmp(token, "double"))
263 odp->u.func.arg_types[i++] = 'l';
264 if (i < sizeof(odp->u.func.arg_types)) odp->u.func.arg_types[i] = 'l';
268 error( "Unknown argument type '%s'\n", token );
272 if (SpecType == SPEC_WIN32)
274 if (strcmp(token, "long") &&
275 strcmp(token, "ptr") &&
276 strcmp(token, "str") &&
277 strcmp(token, "wstr") &&
278 strcmp(token, "double"))
280 error( "Type '%s' not supported for Win32\n", token );
285 if ((*token != ')') || (i >= sizeof(odp->u.func.arg_types)))
287 error( "Too many arguments\n" );
291 odp->u.func.arg_types[i] = '\0';
292 if (odp->type == TYPE_VARARGS)
293 odp->flags |= FLAG_NORELAY; /* no relay debug possible for varags entry point */
295 if (!(token = GetToken(1)))
297 if (!strcmp( odp->name, "@" ))
299 error( "Missing handler name for anonymous function\n" );
302 odp->link_name = xstrdup( odp->name );
306 odp->link_name = xstrdup( token );
307 if (strchr( odp->link_name, '.' ))
309 if (SpecType == SPEC_WIN16)
311 error( "Forwarded functions not supported for Win16\n" );
314 odp->flags |= FLAG_FORWARD;
321 /*******************************************************************
324 * Parse an 'equate' definition.
326 static int ParseEquate( ORDDEF *odp )
332 if (SpecType == SPEC_WIN32)
334 error( "'equate' not supported for Win32\n" );
337 if (!(token = GetToken(0))) return 0;
338 value = strtol(token, &endptr, 0);
339 if (endptr == NULL || *endptr != '\0')
341 error( "Expected number value, got '%s'\n", token );
344 odp->u.abs.value = value;
349 /*******************************************************************
352 * Parse a 'stub' definition.
354 static int ParseStub( ORDDEF *odp )
356 odp->u.func.arg_types[0] = '\0';
357 odp->link_name = xstrdup("");
362 /*******************************************************************
365 * Parse an 'extern' definition.
367 static int ParseExtern( ORDDEF *odp )
371 if (SpecType == SPEC_WIN16)
373 error( "'extern' not supported for Win16, use 'variable' instead\n" );
376 if (!(token = GetToken(1)))
378 if (!strcmp( odp->name, "@" ))
380 error( "Missing handler name for anonymous extern\n" );
383 odp->link_name = xstrdup( odp->name );
387 odp->link_name = xstrdup( token );
388 if (strchr( odp->link_name, '.' )) odp->flags |= FLAG_FORWARD;
394 /*******************************************************************
397 * Parse the optional flags for an entry point
399 static const char *ParseFlags( ORDDEF *odp )
406 if (!(token = GetToken(0))) break;
407 for (i = 0; FlagNames[i]; i++)
408 if (!strcmp( FlagNames[i], token )) break;
411 error( "Unknown flag '%s'\n", token );
414 odp->flags |= 1 << i;
416 } while (token && *token == '-');
421 /*******************************************************************
424 * Fix an exported function name by removing a possible @xx suffix
426 static void fix_export_name( char *name )
428 char *p, *end = strrchr( name, '@' );
429 if (!end || !end[1] || end == name) return;
430 /* make sure all the rest is digits */
431 for (p = end + 1; *p; p++) if (!isdigit(*p)) return;
435 /*******************************************************************
438 * Parse an ordinal definition.
440 static int ParseOrdinal(int ordinal)
444 ORDDEF *odp = xmalloc( sizeof(*odp) );
445 memset( odp, 0, sizeof(*odp) );
446 EntryPoints[nb_entry_points++] = odp;
448 if (!(token = GetToken(0))) goto error;
450 for (odp->type = 0; odp->type < TYPE_NBTYPES; odp->type++)
451 if (TypeNames[odp->type] && !strcmp( token, TypeNames[odp->type] ))
454 if (odp->type >= TYPE_NBTYPES)
456 /* special case for backwards compatibility */
457 if (!strcmp( token, "pascal16" ))
459 odp->type = TYPE_PASCAL;
460 odp->flags |= FLAG_RET16;
464 error( "Expected type after ordinal, found '%s' instead\n", token );
469 if (!(token = GetToken(0))) goto error;
470 if (*token == '-' && !(token = ParseFlags( odp ))) goto error;
472 odp->name = xstrdup( token );
473 fix_export_name( odp->name );
474 odp->lineno = current_line;
475 odp->ordinal = ordinal;
480 if (!ParseVariable( odp )) goto error;
486 if (!ParseExportFunction( odp )) goto error;
489 if (!ParseEquate( odp )) goto error;
492 if (!ParseStub( odp )) goto error;
495 if (!ParseExtern( odp )) goto error;
502 if (odp->flags & FLAG_I386)
504 /* ignore this entry point on non-Intel archs */
505 EntryPoints[--nb_entry_points] = NULL;
515 error( "Ordinal 0 is not valid\n" );
518 if (ordinal >= MAX_ORDINALS)
520 error( "Ordinal number %d too large\n", ordinal );
523 if (ordinal > Limit) Limit = ordinal;
524 if (ordinal < Base) Base = ordinal;
525 odp->ordinal = ordinal;
526 if (Ordinals[ordinal])
528 error( "Duplicate ordinal %d\n", ordinal );
531 Ordinals[ordinal] = odp;
534 if (!strcmp( odp->name, "@" ) || odp->flags & FLAG_NONAME)
538 error( "Nameless function needs an explicit ordinal number\n" );
541 if (SpecType != SPEC_WIN32)
543 error( "Nameless functions not supported for Win16\n" );
546 if (!strcmp( odp->name, "@" )) free( odp->name );
547 else odp->export_name = odp->name;
550 else Names[nb_names++] = odp;
554 EntryPoints[--nb_entry_points] = NULL;
561 static int name_compare( const void *name1, const void *name2 )
563 ORDDEF *odp1 = *(ORDDEF **)name1;
564 ORDDEF *odp2 = *(ORDDEF **)name2;
565 return strcmp( odp1->name, odp2->name );
568 /*******************************************************************
571 * Sort the name array and catch duplicates.
573 static void sort_names(void)
577 if (!nb_names) return;
579 /* sort the list of names */
580 qsort( Names, nb_names, sizeof(Names[0]), name_compare );
582 /* check for duplicate names */
583 for (i = 0; i < nb_names - 1; i++)
585 if (!strcmp( Names[i]->name, Names[i+1]->name ))
587 current_line = max( Names[i]->lineno, Names[i+1]->lineno );
588 error( "'%s' redefined\n%s:%d: First defined here\n",
589 Names[i]->name, input_file_name,
590 min( Names[i]->lineno, Names[i+1]->lineno ) );
596 /*******************************************************************
601 int ParseTopLevel( FILE *file )
608 while (get_next_line())
610 if (!(token = GetToken(1))) continue;
611 if (strcmp(token, "@") == 0)
613 if (SpecType != SPEC_WIN32)
615 error( "'@' ordinals not supported for Win16\n" );
618 if (!ParseOrdinal( -1 )) continue;
620 else if (IsNumberString(token))
622 if (!ParseOrdinal( atoi(token) )) continue;
626 error( "Expected ordinal declaration, got '%s'\n", token );
629 if ((token = GetToken(1))) error( "Syntax error near '%s'\n", token );
632 current_line = 0; /* no longer parsing the input file */
638 /*******************************************************************
641 static void add_debug_channel( const char *name )
645 for (i = 0; i < nb_debug_channels; i++)
646 if (!strcmp( debug_channels[i], name )) return;
648 debug_channels = xrealloc( debug_channels, (nb_debug_channels + 1) * sizeof(*debug_channels));
649 debug_channels[nb_debug_channels++] = xstrdup(name);
653 /*******************************************************************
654 * parse_debug_channels
656 * Parse a source file and extract the debug channel definitions.
658 int parse_debug_channels( const char *srcdir, const char *filename )
663 file = open_input_file( srcdir, filename );
664 while (fgets( ParseBuffer, sizeof(ParseBuffer), file ))
666 char *channel, *end, *p = ParseBuffer;
668 p = ParseBuffer + strlen(ParseBuffer) - 1;
669 if (!eol_seen) /* continuation line */
671 eol_seen = (*p == '\n');
674 if ((eol_seen = (*p == '\n'))) *p = 0;
677 while (isspace(*p)) p++;
678 if (!memcmp( p, "WINE_DECLARE_DEBUG_CHANNEL", 26 ) ||
679 !memcmp( p, "WINE_DEFAULT_DEBUG_CHANNEL", 26 ))
682 while (isspace(*p)) p++;
685 error( "invalid debug channel specification '%s'\n", ParseBuffer );
689 while (isspace(*p)) p++;
692 error( "invalid debug channel specification '%s'\n", ParseBuffer );
696 while (isalnum(*p) || *p == '_') p++;
698 while (isspace(*p)) p++;
701 error( "invalid debug channel specification '%s'\n", ParseBuffer );
705 add_debug_channel( channel );
710 close_input_file( file );