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