Fixed header dependencies to be fully compatible with the Windows
[wine/multimedia.git] / tools / winebuild / parser.c
blob10db504d7f6835a39e7d6a043f9e1a8557d038b9
1 /*
2 * Spec file parser
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
25 #include "config.h"
26 #include "wine/port.h"
28 #include <assert.h>
29 #include <ctype.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
35 #include "windef.h"
36 #include "winbase.h"
37 #include "build.h"
39 int current_line = 0;
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 */
68 NULL
71 static int IsNumberString(const char *s)
73 while (*s) if (!isdigit(*s++)) return 0;
74 return 1;
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;
86 current_line++;
87 return (fgets(ParseBuffer, sizeof(ParseBuffer), input_file) != NULL);
90 static const char * GetToken( int allow_eol )
92 char *p = ParseNext;
93 char *token = TokenBuffer;
95 for (;;)
97 /* remove initial white space */
98 p = ParseNext;
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" );
106 return NULL;
109 else break;
112 if ((*p == '\0') || (*p == '#'))
114 if (!allow_eol) error( "Declaration not terminated properly\n" );
115 return NULL;
119 * Find end of token.
121 if (is_token_separator(*p))
123 /* a separator is always a complete token */
124 *token++ = *p++;
126 else while (*p != '\0' && !is_token_separator(*p) && !isspace(*p))
128 if (*p == '\\') p++;
129 if (*p) *token++ = *p++;
131 *token = '\0';
132 ParseNext = p;
133 return TokenBuffer;
137 /*******************************************************************
138 * ParseVariable
140 * Parse a variable definition.
142 static int ParseVariable( ORDDEF *odp )
144 char *endptr;
145 int *value_array;
146 int n_values;
147 int value_array_size;
148 const char *token;
150 if (SpecType == SPEC_WIN32)
152 error( "'variable' not supported in Win32, use 'extern' instead\n" );
153 return 0;
156 if (!(token = GetToken(0))) return 0;
157 if (*token != '(')
159 error( "Expected '(' got '%s'\n", token );
160 return 0;
163 n_values = 0;
164 value_array_size = 25;
165 value_array = xmalloc(sizeof(*value_array) * value_array_size);
167 for (;;)
169 if (!(token = GetToken(0)))
171 free( value_array );
172 return 0;
174 if (*token == ')')
175 break;
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 );
188 free( value_array );
189 return 0;
193 odp->u.var.n_values = n_values;
194 odp->u.var.values = xrealloc(value_array, sizeof(*value_array) * n_values);
195 return 1;
199 /*******************************************************************
200 * ParseExportFunction
202 * Parse a function definition.
204 static int ParseExportFunction( ORDDEF *odp )
206 const char *token;
207 unsigned int i;
209 switch(SpecType)
211 case SPEC_WIN16:
212 if (odp->type == TYPE_STDCALL)
214 error( "'stdcall' not supported for Win16\n" );
215 return 0;
217 break;
218 case SPEC_WIN32:
219 if (odp->type == TYPE_PASCAL)
221 error( "'pascal' not supported for Win32\n" );
222 return 0;
224 if (odp->flags & FLAG_INTERRUPT)
226 error( "'interrupt' not supported for Win32\n" );
227 return 0;
229 break;
230 default:
231 break;
234 if (!(token = GetToken(0))) return 0;
235 if (*token != '(')
237 error( "Expected '(' got '%s'\n", token );
238 return 0;
241 for (i = 0; i < sizeof(odp->u.func.arg_types); i++)
243 if (!(token = GetToken(0))) return 0;
244 if (*token == ')')
245 break;
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';
266 else
268 error( "Unknown argument type '%s'\n", token );
269 return 0;
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 );
281 return 0;
285 if ((*token != ')') || (i >= sizeof(odp->u.func.arg_types)))
287 error( "Too many arguments\n" );
288 return 0;
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" );
300 return 0;
302 odp->link_name = xstrdup( odp->name );
304 else
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" );
312 return 0;
314 odp->flags |= FLAG_FORWARD;
317 return 1;
321 /*******************************************************************
322 * ParseEquate
324 * Parse an 'equate' definition.
326 static int ParseEquate( ORDDEF *odp )
328 char *endptr;
329 int value;
330 const char *token;
332 if (SpecType == SPEC_WIN32)
334 error( "'equate' not supported for Win32\n" );
335 return 0;
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 );
342 return 0;
344 odp->u.abs.value = value;
345 return 1;
349 /*******************************************************************
350 * ParseStub
352 * Parse a 'stub' definition.
354 static int ParseStub( ORDDEF *odp )
356 odp->u.func.arg_types[0] = '\0';
357 odp->link_name = xstrdup("");
358 return 1;
362 /*******************************************************************
363 * ParseExtern
365 * Parse an 'extern' definition.
367 static int ParseExtern( ORDDEF *odp )
369 const char *token;
371 if (SpecType == SPEC_WIN16)
373 error( "'extern' not supported for Win16, use 'variable' instead\n" );
374 return 0;
376 if (!(token = GetToken(1)))
378 if (!strcmp( odp->name, "@" ))
380 error( "Missing handler name for anonymous extern\n" );
381 return 0;
383 odp->link_name = xstrdup( odp->name );
385 else
387 odp->link_name = xstrdup( token );
388 if (strchr( odp->link_name, '.' )) odp->flags |= FLAG_FORWARD;
390 return 1;
394 /*******************************************************************
395 * ParseFlags
397 * Parse the optional flags for an entry point
399 static const char *ParseFlags( ORDDEF *odp )
401 unsigned int i;
402 const char *token;
406 if (!(token = GetToken(0))) break;
407 for (i = 0; FlagNames[i]; i++)
408 if (!strcmp( FlagNames[i], token )) break;
409 if (!FlagNames[i])
411 error( "Unknown flag '%s'\n", token );
412 return NULL;
414 odp->flags |= 1 << i;
415 token = GetToken(0);
416 } while (token && *token == '-');
418 return token;
421 /*******************************************************************
422 * fix_export_name
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;
432 *end = 0;
435 /*******************************************************************
436 * ParseOrdinal
438 * Parse an ordinal definition.
440 static int ParseOrdinal(int ordinal)
442 const char *token;
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] ))
452 break;
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;
462 else
464 error( "Expected type after ordinal, found '%s' instead\n", token );
465 goto error;
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;
477 switch(odp->type)
479 case TYPE_VARIABLE:
480 if (!ParseVariable( odp )) goto error;
481 break;
482 case TYPE_PASCAL:
483 case TYPE_STDCALL:
484 case TYPE_VARARGS:
485 case TYPE_CDECL:
486 if (!ParseExportFunction( odp )) goto error;
487 break;
488 case TYPE_ABS:
489 if (!ParseEquate( odp )) goto error;
490 break;
491 case TYPE_STUB:
492 if (!ParseStub( odp )) goto error;
493 break;
494 case TYPE_EXTERN:
495 if (!ParseExtern( odp )) goto error;
496 break;
497 default:
498 assert( 0 );
501 #ifndef __i386__
502 if (odp->flags & FLAG_I386)
504 /* ignore this entry point on non-Intel archs */
505 EntryPoints[--nb_entry_points] = NULL;
506 free( odp );
507 return 1;
509 #endif
511 if (ordinal != -1)
513 if (!ordinal)
515 error( "Ordinal 0 is not valid\n" );
516 goto error;
518 if (ordinal >= MAX_ORDINALS)
520 error( "Ordinal number %d too large\n", ordinal );
521 goto error;
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 );
529 goto error;
531 Ordinals[ordinal] = odp;
534 if (!strcmp( odp->name, "@" ) || odp->flags & FLAG_NONAME)
536 if (ordinal == -1)
538 error( "Nameless function needs an explicit ordinal number\n" );
539 goto error;
541 if (SpecType != SPEC_WIN32)
543 error( "Nameless functions not supported for Win16\n" );
544 goto error;
546 if (!strcmp( odp->name, "@" )) free( odp->name );
547 else odp->export_name = odp->name;
548 odp->name = NULL;
550 else Names[nb_names++] = odp;
551 return 1;
553 error:
554 EntryPoints[--nb_entry_points] = NULL;
555 free( odp->name );
556 free( odp );
557 return 0;
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 /*******************************************************************
569 * sort_names
571 * Sort the name array and catch duplicates.
573 static void sort_names(void)
575 int i;
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 /*******************************************************************
597 * ParseTopLevel
599 * Parse a spec file.
601 int ParseTopLevel( FILE *file )
603 const char *token;
605 input_file = file;
606 current_line = 0;
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" );
616 continue;
618 if (!ParseOrdinal( -1 )) continue;
620 else if (IsNumberString(token))
622 if (!ParseOrdinal( atoi(token) )) continue;
624 else
626 error( "Expected ordinal declaration, got '%s'\n", token );
627 continue;
629 if ((token = GetToken(1))) error( "Syntax error near '%s'\n", token );
632 current_line = 0; /* no longer parsing the input file */
633 sort_names();
634 return !nb_errors;
638 /*******************************************************************
639 * add_debug_channel
641 static void add_debug_channel( const char *name )
643 int i;
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 )
660 FILE *file;
661 int eol_seen = 1;
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');
672 continue;
674 if ((eol_seen = (*p == '\n'))) *p = 0;
676 p = ParseBuffer;
677 while (isspace(*p)) p++;
678 if (!memcmp( p, "WINE_DECLARE_DEBUG_CHANNEL", 26 ) ||
679 !memcmp( p, "WINE_DEFAULT_DEBUG_CHANNEL", 26 ))
681 p += 26;
682 while (isspace(*p)) p++;
683 if (*p != '(')
685 error( "invalid debug channel specification '%s'\n", ParseBuffer );
686 goto next;
688 p++;
689 while (isspace(*p)) p++;
690 if (!isalpha(*p))
692 error( "invalid debug channel specification '%s'\n", ParseBuffer );
693 goto next;
695 channel = p;
696 while (isalnum(*p) || *p == '_') p++;
697 end = p;
698 while (isspace(*p)) p++;
699 if (*p != ')')
701 error( "invalid debug channel specification '%s'\n", ParseBuffer );
702 goto next;
704 *end = 0;
705 add_debug_channel( channel );
707 next:
708 current_line++;
710 close_input_file( file );
711 return !nb_errors;