Added an unknown VxD error code.
[wine] / tools / winebuild / parser.c
1 /*
2  * Spec file parser
3  *
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
9  */
10
11 #include <assert.h>
12 #include <ctype.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <unistd.h>
17
18 #include "config.h"
19 #include "winbase.h"
20 #include "build.h"
21
22 int current_line = 0;
23
24 static SPEC_TYPE SpecType = SPEC_INVALID;
25
26 static char ParseBuffer[512];
27 static char TokenBuffer[512];
28 static char *ParseNext = ParseBuffer;
29 static FILE *input_file;
30
31 static const char * const TypeNames[TYPE_NBTYPES] =
32 {
33     "variable",     /* TYPE_VARIABLE */
34     "pascal16",     /* TYPE_PASCAL_16 */
35     "pascal",       /* TYPE_PASCAL */
36     "equate",       /* TYPE_ABS */
37     "register",     /* TYPE_REGISTER */
38     "interrupt",    /* TYPE_INTERRUPT */
39     "stub",         /* TYPE_STUB */
40     "stdcall",      /* TYPE_STDCALL */
41     "cdecl",        /* TYPE_CDECL */
42     "varargs",      /* TYPE_VARARGS */
43     "extern",       /* TYPE_EXTERN */
44     "forward"       /* TYPE_FORWARD */
45 };
46
47 static const char * const FlagNames[] =
48 {
49     "noimport",    /* FLAG_NOIMPORT */
50     "norelay",     /* FLAG_NORELAY */
51     "ret64",       /* FLAG_RET64 */
52     "i386",        /* FLAG_I386 */
53     NULL
54 };
55
56 static int IsNumberString(const char *s)
57 {
58     while (*s) if (!isdigit(*s++)) return 0;
59     return 1;
60 }
61
62 inline static int is_token_separator( char ch )
63 {
64     return (ch == '(' || ch == ')' || ch == '-');
65 }
66
67 static const char * GetTokenInLine(void)
68 {
69     char *p = ParseNext;
70     char *token = TokenBuffer;
71
72     /*
73      * Remove initial white space.
74      */
75     while (isspace(*p)) p++;
76
77     if ((*p == '\0') || (*p == '#')) return NULL;
78
79     /*
80      * Find end of token.
81      */
82     if (is_token_separator(*p))
83     {
84         /* a separator is always a complete token */
85         *token++ = *p++;
86     }
87     else while (*p != '\0' && !is_token_separator(*p) && !isspace(*p))
88     {
89         if (*p == '\\') p++;
90         if (*p) *token++ = *p++;
91     }
92     *token = '\0';
93     ParseNext = p;
94     return TokenBuffer;
95 }
96
97 static const char * GetToken( int allow_eof )
98 {
99     const char *token;
100
101     while ((token = GetTokenInLine()) == NULL)
102     {
103         ParseNext = ParseBuffer;
104         current_line++;
105         if (fgets(ParseBuffer, sizeof(ParseBuffer), input_file) == NULL)
106         {
107             if (!allow_eof) fatal_error( "Unexpected end of file\n" );
108             return NULL;
109         }
110     }
111     return token;
112 }
113
114
115 /*******************************************************************
116  *         ParseDebug
117  *
118  * Parse a debug channel definition.
119  */
120 static void ParseDebug(void)
121 {
122     const char *token = GetToken(0);
123     if (*token != '(') fatal_error( "Expected '(' got '%s'\n", token );
124     for (;;)
125     {
126         token = GetToken(0);
127         if (*token == ')') break;
128         debug_channels = xrealloc( debug_channels,
129                                    (nb_debug_channels + 1) * sizeof(*debug_channels));
130         debug_channels[nb_debug_channels++] = xstrdup(token);
131     }
132 }
133
134
135 /*******************************************************************
136  *         ParseIgnore
137  *
138  * Parse an 'ignore' definition.
139  */
140 static void ParseIgnore(void)
141 {
142     const char *token = GetToken(0);
143     if (*token != '(') fatal_error( "Expected '(' got '%s'\n", token );
144     for (;;)
145     {
146         token = GetToken(0);
147         if (*token == ')') break;
148         add_ignore_symbol( token );
149     }
150 }
151
152
153 /*******************************************************************
154  *         ParseVariable
155  *
156  * Parse a variable definition.
157  */
158 static void ParseVariable( ORDDEF *odp )
159 {
160     char *endptr;
161     int *value_array;
162     int n_values;
163     int value_array_size;
164
165     const char *token = GetToken(0);
166     if (*token != '(') fatal_error( "Expected '(' got '%s'\n", token );
167
168     n_values = 0;
169     value_array_size = 25;
170     value_array = xmalloc(sizeof(*value_array) * value_array_size);
171     
172     for (;;)
173     {
174         token = GetToken(0);
175         if (*token == ')')
176             break;
177
178         value_array[n_values++] = strtol(token, &endptr, 0);
179         if (n_values == value_array_size)
180         {
181             value_array_size += 25;
182             value_array = xrealloc(value_array, 
183                                    sizeof(*value_array) * value_array_size);
184         }
185         
186         if (endptr == NULL || *endptr != '\0')
187             fatal_error( "Expected number value, got '%s'\n", token );
188     }
189
190     odp->u.var.n_values = n_values;
191     odp->u.var.values = xrealloc(value_array, sizeof(*value_array) * n_values);
192 }
193
194
195 /*******************************************************************
196  *         ParseExportFunction
197  *
198  * Parse a function definition.
199  */
200 static void ParseExportFunction( ORDDEF *odp )
201 {
202     const char *token;
203     unsigned int i;
204
205     switch(SpecType)
206     {
207     case SPEC_WIN16:
208         if (odp->type == TYPE_STDCALL)
209             fatal_error( "'stdcall' not supported for Win16\n" );
210         if (odp->type == TYPE_VARARGS)
211             fatal_error( "'varargs' not supported for Win16\n" );
212         break;
213     case SPEC_WIN32:
214         if ((odp->type == TYPE_PASCAL) || (odp->type == TYPE_PASCAL_16))
215             fatal_error( "'pascal' not supported for Win32\n" );
216         break;
217     default:
218         break;
219     }
220
221     token = GetToken(0);
222     if (*token != '(') fatal_error( "Expected '(' got '%s'\n", token );
223
224     for (i = 0; i < sizeof(odp->u.func.arg_types); i++)
225     {
226         token = GetToken(0);
227         if (*token == ')')
228             break;
229
230         if (!strcmp(token, "word"))
231             odp->u.func.arg_types[i] = 'w';
232         else if (!strcmp(token, "s_word"))
233             odp->u.func.arg_types[i] = 's';
234         else if (!strcmp(token, "long") || !strcmp(token, "segptr"))
235             odp->u.func.arg_types[i] = 'l';
236         else if (!strcmp(token, "ptr"))
237             odp->u.func.arg_types[i] = 'p';
238         else if (!strcmp(token, "str"))
239             odp->u.func.arg_types[i] = 't';
240         else if (!strcmp(token, "wstr"))
241             odp->u.func.arg_types[i] = 'W';
242         else if (!strcmp(token, "segstr"))
243             odp->u.func.arg_types[i] = 'T';
244         else if (!strcmp(token, "double"))
245         {
246             odp->u.func.arg_types[i++] = 'l';
247             if (i < sizeof(odp->u.func.arg_types)) odp->u.func.arg_types[i] = 'l';
248         }
249         else fatal_error( "Unknown variable type '%s'\n", token );
250
251         if (SpecType == SPEC_WIN32)
252         {
253             if (strcmp(token, "long") &&
254                 strcmp(token, "ptr") &&
255                 strcmp(token, "str") &&
256                 strcmp(token, "wstr") &&
257                 strcmp(token, "double"))
258             {
259                 fatal_error( "Type '%s' not supported for Win32\n", token );
260             }
261         }
262     }
263     if ((*token != ')') || (i >= sizeof(odp->u.func.arg_types)))
264         fatal_error( "Too many arguments\n" );
265
266     odp->u.func.arg_types[i] = '\0';
267     if (odp->type == TYPE_VARARGS)
268         odp->flags |= FLAG_NORELAY;  /* no relay debug possible for varags entry point */
269     odp->link_name = xstrdup( GetToken(0) );
270 }
271
272
273 /*******************************************************************
274  *         ParseEquate
275  *
276  * Parse an 'equate' definition.
277  */
278 static void ParseEquate( ORDDEF *odp )
279 {
280     char *endptr;
281
282     const char *token = GetToken(0);
283     int value = strtol(token, &endptr, 0);
284     if (endptr == NULL || *endptr != '\0')
285         fatal_error( "Expected number value, got '%s'\n", token );
286     if (SpecType == SPEC_WIN32)
287         fatal_error( "'equate' not supported for Win32\n" );
288     odp->u.abs.value = value;
289 }
290
291
292 /*******************************************************************
293  *         ParseStub
294  *
295  * Parse a 'stub' definition.
296  */
297 static void ParseStub( ORDDEF *odp )
298 {
299     odp->u.func.arg_types[0] = '\0';
300     odp->link_name = xstrdup("");
301 }
302
303
304 /*******************************************************************
305  *         ParseInterrupt
306  *
307  * Parse an 'interrupt' definition.
308  */
309 static void ParseInterrupt( ORDDEF *odp )
310 {
311     const char *token;
312
313     if (SpecType == SPEC_WIN32)
314         fatal_error( "'interrupt' not supported for Win32\n" );
315
316     token = GetToken(0);
317     if (*token != '(') fatal_error( "Expected '(' got '%s'\n", token );
318
319     token = GetToken(0);
320     if (*token != ')') fatal_error( "Expected ')' got '%s'\n", token );
321
322     odp->u.func.arg_types[0] = '\0';
323     odp->link_name = xstrdup( GetToken(0) );
324 }
325
326
327 /*******************************************************************
328  *         ParseExtern
329  *
330  * Parse an 'extern' definition.
331  */
332 static void ParseExtern( ORDDEF *odp )
333 {
334     if (SpecType == SPEC_WIN16) fatal_error( "'extern' not supported for Win16\n" );
335     odp->link_name = xstrdup( GetToken(0) );
336     /* 'extern' definitions are not available for implicit import */
337     odp->flags |= FLAG_NOIMPORT;
338 }
339
340
341 /*******************************************************************
342  *         ParseForward
343  *
344  * Parse a 'forward' definition.
345  */
346 static void ParseForward( ORDDEF *odp )
347 {
348     if (SpecType == SPEC_WIN16) fatal_error( "'forward' not supported for Win16\n" );
349     odp->link_name = xstrdup( GetToken(0) );
350 }
351
352
353 /*******************************************************************
354  *         ParseFlags
355  *
356  * Parse the optional flags for an entry point
357  */
358 static const char *ParseFlags( ORDDEF *odp )
359 {
360     unsigned int i;
361     const char *token;
362
363     do
364     {
365         token = GetToken(0);
366         for (i = 0; FlagNames[i]; i++)
367             if (!strcmp( FlagNames[i], token )) break;
368         if (!FlagNames[i]) fatal_error( "Unknown flag '%s'\n", token );
369         odp->flags |= 1 << i;
370         token = GetToken(0);
371     } while (*token == '-');
372
373     return token;
374 }
375
376 /*******************************************************************
377  *         fix_export_name
378  *
379  * Fix an exported function name by removing a possible @xx suffix
380  */
381 static void fix_export_name( char *name )
382 {
383     char *p, *end = strrchr( name, '@' );
384     if (!end || !end[1] || end == name) return;
385     /* make sure all the rest is digits */
386     for (p = end + 1; *p; p++) if (!isdigit(*p)) return;
387     *end = 0;
388 }
389
390 /*******************************************************************
391  *         ParseOrdinal
392  *
393  * Parse an ordinal definition.
394  */
395 static void ParseOrdinal(int ordinal)
396 {
397     const char *token;
398
399     ORDDEF *odp = xmalloc( sizeof(*odp) );
400     memset( odp, 0, sizeof(*odp) );
401     EntryPoints[nb_entry_points++] = odp;
402
403     token = GetToken(0);
404
405     for (odp->type = 0; odp->type < TYPE_NBTYPES; odp->type++)
406         if (TypeNames[odp->type] && !strcmp( token, TypeNames[odp->type] ))
407             break;
408
409     if (odp->type >= TYPE_NBTYPES)
410         fatal_error( "Expected type after ordinal, found '%s' instead\n", token );
411
412     token = GetToken(0);
413     if (*token == '-') token = ParseFlags( odp );
414
415     odp->name = xstrdup( token );
416     fix_export_name( odp->name );
417     odp->lineno = current_line;
418     odp->ordinal = ordinal;
419
420     switch(odp->type)
421     {
422     case TYPE_VARIABLE:
423         ParseVariable( odp );
424         break;
425     case TYPE_REGISTER:
426     case TYPE_PASCAL_16:
427     case TYPE_PASCAL:
428     case TYPE_STDCALL:
429     case TYPE_VARARGS:
430     case TYPE_CDECL:
431         ParseExportFunction( odp );
432         break;
433     case TYPE_INTERRUPT:
434         ParseInterrupt( odp );
435         break;
436     case TYPE_ABS:
437         ParseEquate( odp );
438         break;
439     case TYPE_STUB:
440         ParseStub( odp );
441         break;
442     case TYPE_EXTERN:
443         ParseExtern( odp );
444         break;
445     case TYPE_FORWARD:
446         ParseForward( odp );
447         break;
448     default:
449         assert( 0 );
450     }
451
452 #ifndef __i386__
453     if (odp->flags & FLAG_I386)
454     {
455         /* ignore this entry point on non-Intel archs */
456         EntryPoints[--nb_entry_points] = NULL;
457         free( odp );
458         return;
459     }
460 #endif
461
462     if (ordinal != -1)
463     {
464         if (ordinal >= MAX_ORDINALS) fatal_error( "Ordinal number %d too large\n", ordinal );
465         if (ordinal > Limit) Limit = ordinal;
466         if (ordinal < Base) Base = ordinal;
467         odp->ordinal = ordinal;
468         Ordinals[ordinal] = odp;
469     }
470
471     if (!strcmp( odp->name, "@" ))
472     {
473         if (ordinal == -1)
474             fatal_error( "Nameless function needs an explicit ordinal number\n" );
475         if (SpecType != SPEC_WIN32)
476             fatal_error( "Nameless functions not supported for Win16\n" );
477         odp->name[0] = 0;
478     }
479     else Names[nb_names++] = odp;
480 }
481
482
483 static int name_compare( const void *name1, const void *name2 )
484 {
485     ORDDEF *odp1 = *(ORDDEF **)name1;
486     ORDDEF *odp2 = *(ORDDEF **)name2;
487     return strcmp( odp1->name, odp2->name );
488 }
489
490 /*******************************************************************
491  *         sort_names
492  *
493  * Sort the name array and catch duplicates.
494  */
495 static void sort_names(void)
496 {
497     int i;
498
499     if (!nb_names) return;
500
501     /* sort the list of names */
502     qsort( Names, nb_names, sizeof(Names[0]), name_compare );
503
504     /* check for duplicate names */
505     for (i = 0; i < nb_names - 1; i++)
506     {
507         if (!strcmp( Names[i]->name, Names[i+1]->name ))
508         {
509             current_line = max( Names[i]->lineno, Names[i+1]->lineno );
510             fatal_error( "'%s' redefined (previous definition at line %d)\n",
511                          Names[i]->name, min( Names[i]->lineno, Names[i+1]->lineno ) );
512         }
513     }
514 }
515
516
517 /*******************************************************************
518  *         ParseTopLevel
519  *
520  * Parse a spec file.
521  */
522 SPEC_TYPE ParseTopLevel( FILE *file )
523 {
524     const char *token;
525
526     input_file = file;
527     current_line = 1;
528     while ((token = GetToken(1)) != NULL)
529     {
530         if (strcmp(token, "name") == 0)
531         {
532             strcpy(DLLName, GetToken(0));
533         }
534         else if (strcmp(token, "file") == 0)
535         {
536             strcpy(DLLFileName, GetToken(0));
537             strupper(DLLFileName);
538         }
539         else if (strcmp(token, "type") == 0)
540         {
541             token = GetToken(0);
542             if (!strcmp(token, "win16" )) SpecType = SPEC_WIN16;
543             else if (!strcmp(token, "win32" )) SpecType = SPEC_WIN32;
544             else fatal_error( "Type must be 'win16' or 'win32'\n" );
545         }
546         else if (strcmp(token, "mode") == 0)
547         {
548             token = GetToken(0);
549             if (!strcmp(token, "dll" )) SpecMode = SPEC_MODE_DLL;
550             else if (!strcmp(token, "guiexe" )) SpecMode = SPEC_MODE_GUIEXE;
551             else if (!strcmp(token, "cuiexe" )) SpecMode = SPEC_MODE_CUIEXE;
552             else if (!strcmp(token, "guiexe_unicode" )) SpecMode = SPEC_MODE_GUIEXE_UNICODE;
553             else if (!strcmp(token, "cuiexe_unicode" )) SpecMode = SPEC_MODE_CUIEXE_UNICODE;
554             else fatal_error( "Mode must be 'dll', 'guiexe', 'cuiexe', 'guiexe_unicode' or 'cuiexe_unicode'\n" );
555         }
556         else if (strcmp(token, "heap") == 0)
557         {
558             token = GetToken(0);
559             if (!IsNumberString(token)) fatal_error( "Expected number after heap\n" );
560             DLLHeapSize = atoi(token);
561         }
562         else if (strcmp(token, "init") == 0)
563         {
564             if (SpecType == SPEC_WIN16)
565                 fatal_error( "init cannot be used for Win16 spec files\n" );
566             init_func = xstrdup( GetToken(0) );
567         }
568         else if (strcmp(token, "import") == 0)
569         {
570             const char* name;
571             int delay = 0;
572
573             if (SpecType != SPEC_WIN32)
574                 fatal_error( "Imports not supported for Win16\n" );
575             name = GetToken(0);
576             if (*name == '-')
577             {
578                 name = GetToken(0);
579                 if (!strcmp(name, "delay"))
580                 {
581
582                     name = GetToken(0);
583 #ifndef __PPC__
584                     delay = 1;
585 #else
586                     warning( "The 'delay' option is not yet supported on the PPC. 'delay' will be ignored.\n");
587 #endif /* __PPC__ */
588                 }
589                 else fatal_error( "Unknown option '%s' for import directive\n", name );
590             }
591             add_import_dll( name, delay );
592         }
593         else if (strcmp(token, "rsrc") == 0)
594         {
595             if (SpecType != SPEC_WIN16) load_res32_file( GetToken(0) );
596             else load_res16_file( GetToken(0) );
597         }
598         else if (strcmp(token, "owner") == 0)
599         {
600             if (SpecType != SPEC_WIN16)
601                 fatal_error( "Owner only supported for Win16 spec files\n" );
602             strcpy( owner_name, GetToken(0) );
603         }
604         else if (strcmp(token, "debug_channels") == 0)
605         {
606             if (SpecType != SPEC_WIN32)
607                 fatal_error( "debug channels only supported for Win32 spec files\n" );
608             ParseDebug();
609         }
610         else if (strcmp(token, "ignore") == 0)
611         {
612             if (SpecType != SPEC_WIN32)
613                 fatal_error( "'ignore' only supported for Win32 spec files\n" );
614             ParseIgnore();
615         }
616         else if (strcmp(token, "@") == 0)
617         {
618             if (SpecType != SPEC_WIN32)
619                 fatal_error( "'@' ordinals not supported for Win16\n" );
620             ParseOrdinal( -1 );
621         }
622         else if (IsNumberString(token))
623         {
624             ParseOrdinal( atoi(token) );
625         }
626         else
627             fatal_error( "Expected name, id, length or ordinal\n" );
628     }
629
630     if (!DLLFileName[0])
631     {
632         if (SpecMode == SPEC_MODE_DLL)
633             sprintf( DLLFileName, "%s.dll", DLLName );
634         else
635             sprintf( DLLFileName, "%s.exe", DLLName );
636     }
637
638     if (SpecType == SPEC_INVALID) fatal_error( "Missing 'type' declaration\n" );
639     if (SpecType == SPEC_WIN16 && !owner_name[0])
640         fatal_error( "'owner' not specified for Win16 dll\n" );
641
642     current_line = 0;  /* no longer parsing the input file */
643     sort_names();
644     return SpecType;
645 }