Always load the 32-bit dll containing a given 16-bit builtin.
[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_STDCALL) && !i)
268         odp->type = TYPE_CDECL; /* stdcall is the same as cdecl for 0 args */
269     if (odp->type == TYPE_VARARGS)
270         odp->flags |= FLAG_NORELAY;  /* no relay debug possible for varags entry point */
271     odp->link_name = xstrdup( GetToken(0) );
272 }
273
274
275 /*******************************************************************
276  *         ParseEquate
277  *
278  * Parse an 'equate' definition.
279  */
280 static void ParseEquate( ORDDEF *odp )
281 {
282     char *endptr;
283
284     const char *token = GetToken(0);
285     int value = strtol(token, &endptr, 0);
286     if (endptr == NULL || *endptr != '\0')
287         fatal_error( "Expected number value, got '%s'\n", token );
288     if (SpecType == SPEC_WIN32)
289         fatal_error( "'equate' not supported for Win32\n" );
290     odp->u.abs.value = value;
291 }
292
293
294 /*******************************************************************
295  *         ParseStub
296  *
297  * Parse a 'stub' definition.
298  */
299 static void ParseStub( ORDDEF *odp )
300 {
301     odp->u.func.arg_types[0] = '\0';
302     odp->link_name = xstrdup("");
303 }
304
305
306 /*******************************************************************
307  *         ParseInterrupt
308  *
309  * Parse an 'interrupt' definition.
310  */
311 static void ParseInterrupt( ORDDEF *odp )
312 {
313     const char *token;
314
315     if (SpecType == SPEC_WIN32)
316         fatal_error( "'interrupt' not supported for Win32\n" );
317
318     token = GetToken(0);
319     if (*token != '(') fatal_error( "Expected '(' got '%s'\n", token );
320
321     token = GetToken(0);
322     if (*token != ')') fatal_error( "Expected ')' got '%s'\n", token );
323
324     odp->u.func.arg_types[0] = '\0';
325     odp->link_name = xstrdup( GetToken(0) );
326 }
327
328
329 /*******************************************************************
330  *         ParseExtern
331  *
332  * Parse an 'extern' definition.
333  */
334 static void ParseExtern( ORDDEF *odp )
335 {
336     if (SpecType == SPEC_WIN16) fatal_error( "'extern' not supported for Win16\n" );
337     odp->link_name = xstrdup( GetToken(0) );
338     /* 'extern' definitions are not available for implicit import */
339     odp->flags |= FLAG_NOIMPORT;
340 }
341
342
343 /*******************************************************************
344  *         ParseForward
345  *
346  * Parse a 'forward' definition.
347  */
348 static void ParseForward( ORDDEF *odp )
349 {
350     if (SpecType == SPEC_WIN16) fatal_error( "'forward' not supported for Win16\n" );
351     odp->link_name = xstrdup( GetToken(0) );
352 }
353
354
355 /*******************************************************************
356  *         ParseFlags
357  *
358  * Parse the optional flags for an entry point
359  */
360 static const char *ParseFlags( ORDDEF *odp )
361 {
362     unsigned int i;
363     const char *token;
364
365     do
366     {
367         token = GetToken(0);
368         for (i = 0; FlagNames[i]; i++)
369             if (!strcmp( FlagNames[i], token )) break;
370         if (!FlagNames[i]) fatal_error( "Unknown flag '%s'\n", token );
371         odp->flags |= 1 << i;
372         token = GetToken(0);
373     } while (*token == '-');
374
375     return token;
376 }
377
378 /*******************************************************************
379  *         fix_export_name
380  *
381  * Fix an exported function name by removing a possible @xx suffix
382  */
383 static void fix_export_name( char *name )
384 {
385     char *p, *end = strrchr( name, '@' );
386     if (!end || !end[1] || end == name) return;
387     /* make sure all the rest is digits */
388     for (p = end + 1; *p; p++) if (!isdigit(*p)) return;
389     *end = 0;
390 }
391
392 /*******************************************************************
393  *         ParseOrdinal
394  *
395  * Parse an ordinal definition.
396  */
397 static void ParseOrdinal(int ordinal)
398 {
399     const char *token;
400
401     ORDDEF *odp = xmalloc( sizeof(*odp) );
402     memset( odp, 0, sizeof(*odp) );
403     EntryPoints[nb_entry_points++] = odp;
404
405     token = GetToken(0);
406
407     for (odp->type = 0; odp->type < TYPE_NBTYPES; odp->type++)
408         if (TypeNames[odp->type] && !strcmp( token, TypeNames[odp->type] ))
409             break;
410
411     if (odp->type >= TYPE_NBTYPES)
412         fatal_error( "Expected type after ordinal, found '%s' instead\n", token );
413
414     token = GetToken(0);
415     if (*token == '-') token = ParseFlags( odp );
416
417     odp->name = xstrdup( token );
418     fix_export_name( odp->name );
419     odp->lineno = current_line;
420     odp->ordinal = ordinal;
421
422     switch(odp->type)
423     {
424     case TYPE_VARIABLE:
425         ParseVariable( odp );
426         break;
427     case TYPE_REGISTER:
428     case TYPE_PASCAL_16:
429     case TYPE_PASCAL:
430     case TYPE_STDCALL:
431     case TYPE_VARARGS:
432     case TYPE_CDECL:
433         ParseExportFunction( odp );
434         break;
435     case TYPE_INTERRUPT:
436         ParseInterrupt( odp );
437         break;
438     case TYPE_ABS:
439         ParseEquate( odp );
440         break;
441     case TYPE_STUB:
442         ParseStub( odp );
443         break;
444     case TYPE_EXTERN:
445         ParseExtern( odp );
446         break;
447     case TYPE_FORWARD:
448         ParseForward( odp );
449         break;
450     default:
451         assert( 0 );
452     }
453
454 #ifndef __i386__
455     if (odp->flags & FLAG_I386)
456     {
457         /* ignore this entry point on non-Intel archs */
458         EntryPoints[--nb_entry_points] = NULL;
459         free( odp );
460         return;
461     }
462 #endif
463
464     if (ordinal != -1)
465     {
466         if (ordinal >= MAX_ORDINALS) fatal_error( "Ordinal number %d too large\n", ordinal );
467         if (ordinal > Limit) Limit = ordinal;
468         if (ordinal < Base) Base = ordinal;
469         odp->ordinal = ordinal;
470         Ordinals[ordinal] = odp;
471     }
472
473     if (!strcmp( odp->name, "@" ))
474     {
475         if (ordinal == -1)
476             fatal_error( "Nameless function needs an explicit ordinal number\n" );
477         if (SpecType != SPEC_WIN32)
478             fatal_error( "Nameless functions not supported for Win16\n" );
479         odp->name[0] = 0;
480     }
481     else Names[nb_names++] = odp;
482 }
483
484
485 static int name_compare( const void *name1, const void *name2 )
486 {
487     ORDDEF *odp1 = *(ORDDEF **)name1;
488     ORDDEF *odp2 = *(ORDDEF **)name2;
489     return strcmp( odp1->name, odp2->name );
490 }
491
492 /*******************************************************************
493  *         sort_names
494  *
495  * Sort the name array and catch duplicates.
496  */
497 static void sort_names(void)
498 {
499     int i;
500
501     if (!nb_names) return;
502
503     /* sort the list of names */
504     qsort( Names, nb_names, sizeof(Names[0]), name_compare );
505
506     /* check for duplicate names */
507     for (i = 0; i < nb_names - 1; i++)
508     {
509         if (!strcmp( Names[i]->name, Names[i+1]->name ))
510         {
511             current_line = max( Names[i]->lineno, Names[i+1]->lineno );
512             fatal_error( "'%s' redefined (previous definition at line %d)\n",
513                          Names[i]->name, min( Names[i]->lineno, Names[i+1]->lineno ) );
514         }
515     }
516 }
517
518
519 /*******************************************************************
520  *         ParseTopLevel
521  *
522  * Parse a spec file.
523  */
524 SPEC_TYPE ParseTopLevel( FILE *file )
525 {
526     const char *token;
527
528     input_file = file;
529     current_line = 1;
530     while ((token = GetToken(1)) != NULL)
531     {
532         if (strcmp(token, "name") == 0)
533         {
534             strcpy(DLLName, GetToken(0));
535         }
536         else if (strcmp(token, "file") == 0)
537         {
538             strcpy(DLLFileName, GetToken(0));
539             strupper(DLLFileName);
540         }
541         else if (strcmp(token, "type") == 0)
542         {
543             token = GetToken(0);
544             if (!strcmp(token, "win16" )) SpecType = SPEC_WIN16;
545             else if (!strcmp(token, "win32" )) SpecType = SPEC_WIN32;
546             else fatal_error( "Type must be 'win16' or 'win32'\n" );
547         }
548         else if (strcmp(token, "mode") == 0)
549         {
550             token = GetToken(0);
551             if (!strcmp(token, "dll" )) SpecMode = SPEC_MODE_DLL;
552             else if (!strcmp(token, "guiexe" )) SpecMode = SPEC_MODE_GUIEXE;
553             else if (!strcmp(token, "cuiexe" )) SpecMode = SPEC_MODE_CUIEXE;
554             else if (!strcmp(token, "guiexe_unicode" )) SpecMode = SPEC_MODE_GUIEXE_UNICODE;
555             else if (!strcmp(token, "cuiexe_unicode" )) SpecMode = SPEC_MODE_CUIEXE_UNICODE;
556             else fatal_error( "Mode must be 'dll', 'guiexe', 'cuiexe', 'guiexe_unicode' or 'cuiexe_unicode'\n" );
557         }
558         else if (strcmp(token, "heap") == 0)
559         {
560             token = GetToken(0);
561             if (!IsNumberString(token)) fatal_error( "Expected number after heap\n" );
562             DLLHeapSize = atoi(token);
563         }
564         else if (strcmp(token, "init") == 0)
565         {
566             if (SpecType == SPEC_WIN16)
567                 fatal_error( "init cannot be used for Win16 spec files\n" );
568             init_func = xstrdup( GetToken(0) );
569         }
570         else if (strcmp(token, "import") == 0)
571         {
572             const char* name;
573             int delay = 0;
574
575             if (SpecType != SPEC_WIN32)
576                 fatal_error( "Imports not supported for Win16\n" );
577             name = GetToken(0);
578             if (*name == '-')
579             {
580                 name = GetToken(0);
581                 if (!strcmp(name, "delay"))
582                 {
583
584                     name = GetToken(0);
585 #ifndef __PPC__
586                     delay = 1;
587 #else
588                     warning( "The 'delay' option is not yet supported on the PPC. 'delay' will be ignored.\n");
589 #endif /* __PPC__ */
590                 }
591                 else fatal_error( "Unknown option '%s' for import directive\n", name );
592             }
593             add_import_dll( name, delay );
594         }
595         else if (strcmp(token, "rsrc") == 0)
596         {
597             if (SpecType != SPEC_WIN16) load_res32_file( GetToken(0) );
598             else load_res16_file( GetToken(0) );
599         }
600         else if (strcmp(token, "owner") == 0)
601         {
602             if (SpecType != SPEC_WIN16)
603                 fatal_error( "Owner only supported for Win16 spec files\n" );
604             strcpy( owner_name, GetToken(0) );
605         }
606         else if (strcmp(token, "debug_channels") == 0)
607         {
608             if (SpecType != SPEC_WIN32)
609                 fatal_error( "debug channels only supported for Win32 spec files\n" );
610             ParseDebug();
611         }
612         else if (strcmp(token, "ignore") == 0)
613         {
614             if (SpecType != SPEC_WIN32)
615                 fatal_error( "'ignore' only supported for Win32 spec files\n" );
616             ParseIgnore();
617         }
618         else if (strcmp(token, "@") == 0)
619         {
620             if (SpecType != SPEC_WIN32)
621                 fatal_error( "'@' ordinals not supported for Win16\n" );
622             ParseOrdinal( -1 );
623         }
624         else if (IsNumberString(token))
625         {
626             ParseOrdinal( atoi(token) );
627         }
628         else
629             fatal_error( "Expected name, id, length or ordinal\n" );
630     }
631
632     if (!DLLFileName[0])
633     {
634         if (SpecMode == SPEC_MODE_DLL)
635             sprintf( DLLFileName, "%s.dll", DLLName );
636         else
637             sprintf( DLLFileName, "%s.exe", DLLName );
638     }
639
640     if (SpecType == SPEC_INVALID) fatal_error( "Missing 'type' declaration\n" );
641     if (SpecType == SPEC_WIN16 && !owner_name[0])
642         fatal_error( "'owner' not specified for Win16 dll\n" );
643
644     current_line = 0;  /* no longer parsing the input file */
645     sort_names();
646     return SpecType;
647 }