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