wnaspi32: Make winaspi.dll into a stand-alone 16-bit module.
[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, 2004 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24
25 #include "config.h"
26 #include "wine/port.h"
27
28 #include <assert.h>
29 #include <ctype.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 #include "windef.h"
36 #include "winbase.h"
37 #include "build.h"
38
39 int current_line = 0;
40
41 static char ParseBuffer[512];
42 static char TokenBuffer[512];
43 static char *ParseNext = ParseBuffer;
44 static FILE *input_file;
45
46 static const char *separator_chars;
47 static const char *comment_chars;
48
49 /* valid characters in ordinal names */
50 static const char valid_ordname_chars[] = "/$:-_@?abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
51
52 static const char * const TypeNames[TYPE_NBTYPES] =
53 {
54     "variable",     /* TYPE_VARIABLE */
55     "pascal",       /* TYPE_PASCAL */
56     "equate",       /* TYPE_ABS */
57     "stub",         /* TYPE_STUB */
58     "stdcall",      /* TYPE_STDCALL */
59     "cdecl",        /* TYPE_CDECL */
60     "varargs",      /* TYPE_VARARGS */
61     "extern"        /* TYPE_EXTERN */
62 };
63
64 static const char * const FlagNames[] =
65 {
66     "norelay",     /* FLAG_NORELAY */
67     "noname",      /* FLAG_NONAME */
68     "ret16",       /* FLAG_RET16 */
69     "ret64",       /* FLAG_RET64 */
70     "register",    /* FLAG_REGISTER */
71     "private",     /* FLAG_PRIVATE */
72     "ordinal",     /* FLAG_ORDINAL */
73     NULL
74 };
75
76 static int IsNumberString(const char *s)
77 {
78     while (*s) if (!isdigit(*s++)) return 0;
79     return 1;
80 }
81
82 static inline int is_token_separator( char ch )
83 {
84     return strchr( separator_chars, ch ) != NULL;
85 }
86
87 static inline int is_token_comment( char ch )
88 {
89     return strchr( comment_chars, ch ) != NULL;
90 }
91
92 /* get the next line from the input file, or return 0 if at eof */
93 static int get_next_line(void)
94 {
95     ParseNext = ParseBuffer;
96     current_line++;
97     return (fgets(ParseBuffer, sizeof(ParseBuffer), input_file) != NULL);
98 }
99
100 static const char * GetToken( int allow_eol )
101 {
102     char *p = ParseNext;
103     char *token = TokenBuffer;
104
105     for (;;)
106     {
107         /* remove initial white space */
108         p = ParseNext;
109         while (isspace(*p)) p++;
110
111         if (*p == '\\' && p[1] == '\n')  /* line continuation */
112         {
113             if (!get_next_line())
114             {
115                 if (!allow_eol) error( "Unexpected end of file\n" );
116                 return NULL;
117             }
118         }
119         else break;
120     }
121
122     if ((*p == '\0') || is_token_comment(*p))
123     {
124         if (!allow_eol) error( "Declaration not terminated properly\n" );
125         return NULL;
126     }
127
128     /*
129      * Find end of token.
130      */
131     if (is_token_separator(*p))
132     {
133         /* a separator is always a complete token */
134         *token++ = *p++;
135     }
136     else while (*p != '\0' && !is_token_separator(*p) && !isspace(*p))
137     {
138         if (*p == '\\') p++;
139         if (*p) *token++ = *p++;
140     }
141     *token = '\0';
142     ParseNext = p;
143     return TokenBuffer;
144 }
145
146
147 static ORDDEF *add_entry_point( DLLSPEC *spec )
148 {
149     ORDDEF *ret;
150
151     if (spec->nb_entry_points == spec->alloc_entry_points)
152     {
153         spec->alloc_entry_points += 128;
154         spec->entry_points = xrealloc( spec->entry_points,
155                                        spec->alloc_entry_points * sizeof(*spec->entry_points) );
156     }
157     ret = &spec->entry_points[spec->nb_entry_points++];
158     memset( ret, 0, sizeof(*ret) );
159     return ret;
160 }
161
162 /*******************************************************************
163  *         parse_spec_variable
164  *
165  * Parse a variable definition in a .spec file.
166  */
167 static int parse_spec_variable( ORDDEF *odp, DLLSPEC *spec )
168 {
169     char *endptr;
170     int *value_array;
171     int n_values;
172     int value_array_size;
173     const char *token;
174
175     if (spec->type == SPEC_WIN32)
176     {
177         error( "'variable' not supported in Win32, use 'extern' instead\n" );
178         return 0;
179     }
180
181     if (!(token = GetToken(0))) return 0;
182     if (*token != '(')
183     {
184         error( "Expected '(' got '%s'\n", token );
185         return 0;
186     }
187
188     n_values = 0;
189     value_array_size = 25;
190     value_array = xmalloc(sizeof(*value_array) * value_array_size);
191
192     for (;;)
193     {
194         if (!(token = GetToken(0)))
195         {
196             free( value_array );
197             return 0;
198         }
199         if (*token == ')')
200             break;
201
202         value_array[n_values++] = strtol(token, &endptr, 0);
203         if (n_values == value_array_size)
204         {
205             value_array_size += 25;
206             value_array = xrealloc(value_array,
207                                    sizeof(*value_array) * value_array_size);
208         }
209
210         if (endptr == NULL || *endptr != '\0')
211         {
212             error( "Expected number value, got '%s'\n", token );
213             free( value_array );
214             return 0;
215         }
216     }
217
218     odp->u.var.n_values = n_values;
219     odp->u.var.values = xrealloc(value_array, sizeof(*value_array) * n_values);
220     return 1;
221 }
222
223
224 /*******************************************************************
225  *         parse_spec_export
226  *
227  * Parse an exported function definition in a .spec file.
228  */
229 static int parse_spec_export( ORDDEF *odp, DLLSPEC *spec )
230 {
231     const char *token;
232     unsigned int i;
233
234     switch(spec->type)
235     {
236     case SPEC_WIN16:
237         if (odp->type == TYPE_STDCALL)
238         {
239             error( "'stdcall' not supported for Win16\n" );
240             return 0;
241         }
242         break;
243     case SPEC_WIN32:
244         if (odp->type == TYPE_PASCAL)
245         {
246             error( "'pascal' not supported for Win32\n" );
247             return 0;
248         }
249         break;
250     default:
251         break;
252     }
253
254     if (!(token = GetToken(0))) return 0;
255     if (*token != '(')
256     {
257         error( "Expected '(' got '%s'\n", token );
258         return 0;
259     }
260
261     for (i = 0; i < sizeof(odp->u.func.arg_types); i++)
262     {
263         if (!(token = GetToken(0))) return 0;
264         if (*token == ')')
265             break;
266
267         if (!strcmp(token, "word"))
268             odp->u.func.arg_types[i] = 'w';
269         else if (!strcmp(token, "s_word"))
270             odp->u.func.arg_types[i] = 's';
271         else if (!strcmp(token, "long") || !strcmp(token, "segptr"))
272             odp->u.func.arg_types[i] = 'l';
273         else if (!strcmp(token, "ptr"))
274             odp->u.func.arg_types[i] = 'p';
275         else if (!strcmp(token, "str"))
276             odp->u.func.arg_types[i] = 't';
277         else if (!strcmp(token, "wstr"))
278             odp->u.func.arg_types[i] = 'W';
279         else if (!strcmp(token, "segstr"))
280             odp->u.func.arg_types[i] = 'T';
281         else if (!strcmp(token, "double"))
282         {
283             odp->u.func.arg_types[i++] = 'l';
284             if (get_ptr_size() == 4 && i < sizeof(odp->u.func.arg_types))
285                 odp->u.func.arg_types[i] = 'l';
286         }
287         else
288         {
289             error( "Unknown argument type '%s'\n", token );
290             return 0;
291         }
292
293         if (spec->type == SPEC_WIN32)
294         {
295             if (strcmp(token, "long") &&
296                 strcmp(token, "ptr") &&
297                 strcmp(token, "str") &&
298                 strcmp(token, "wstr") &&
299                 strcmp(token, "double"))
300             {
301                 error( "Type '%s' not supported for Win32\n", token );
302                 return 0;
303             }
304         }
305     }
306     if ((*token != ')') || (i >= sizeof(odp->u.func.arg_types)))
307     {
308         error( "Too many arguments\n" );
309         return 0;
310     }
311
312     odp->u.func.arg_types[i] = '\0';
313     if (odp->type == TYPE_VARARGS)
314         odp->flags |= FLAG_NORELAY;  /* no relay debug possible for varags entry point */
315
316     if (!(token = GetToken(1)))
317     {
318         if (!strcmp( odp->name, "@" ))
319         {
320             error( "Missing handler name for anonymous function\n" );
321             return 0;
322         }
323         odp->link_name = xstrdup( odp->name );
324     }
325     else
326     {
327         odp->link_name = xstrdup( token );
328         if (strchr( odp->link_name, '.' ))
329         {
330             if (spec->type == SPEC_WIN16)
331             {
332                 error( "Forwarded functions not supported for Win16\n" );
333                 return 0;
334             }
335             odp->flags |= FLAG_FORWARD;
336         }
337     }
338     return 1;
339 }
340
341
342 /*******************************************************************
343  *         parse_spec_equate
344  *
345  * Parse an 'equate' definition in a .spec file.
346  */
347 static int parse_spec_equate( ORDDEF *odp, DLLSPEC *spec )
348 {
349     char *endptr;
350     int value;
351     const char *token;
352
353     if (spec->type == SPEC_WIN32)
354     {
355         error( "'equate' not supported for Win32\n" );
356         return 0;
357     }
358     if (!(token = GetToken(0))) return 0;
359     value = strtol(token, &endptr, 0);
360     if (endptr == NULL || *endptr != '\0')
361     {
362         error( "Expected number value, got '%s'\n", token );
363         return 0;
364     }
365     if (value < -0x8000 || value > 0xffff)
366     {
367         error( "Value %d for absolute symbol doesn't fit in 16 bits\n", value );
368         value = 0;
369     }
370     odp->u.abs.value = value;
371     return 1;
372 }
373
374
375 /*******************************************************************
376  *         parse_spec_stub
377  *
378  * Parse a 'stub' definition in a .spec file
379  */
380 static int parse_spec_stub( ORDDEF *odp, DLLSPEC *spec )
381 {
382     odp->u.func.arg_types[0] = '\0';
383     odp->link_name = xstrdup("");
384     odp->flags |= FLAG_CPU(CPU_x86) | FLAG_CPU(CPU_x86_64); /* don't bother generating stubs for Winelib */
385     return 1;
386 }
387
388
389 /*******************************************************************
390  *         parse_spec_extern
391  *
392  * Parse an 'extern' definition in a .spec file.
393  */
394 static int parse_spec_extern( ORDDEF *odp, DLLSPEC *spec )
395 {
396     const char *token;
397
398     if (spec->type == SPEC_WIN16)
399     {
400         error( "'extern' not supported for Win16, use 'variable' instead\n" );
401         return 0;
402     }
403     if (!(token = GetToken(1)))
404     {
405         if (!strcmp( odp->name, "@" ))
406         {
407             error( "Missing handler name for anonymous extern\n" );
408             return 0;
409         }
410         odp->link_name = xstrdup( odp->name );
411     }
412     else
413     {
414         odp->link_name = xstrdup( token );
415         if (strchr( odp->link_name, '.' )) odp->flags |= FLAG_FORWARD;
416     }
417     return 1;
418 }
419
420
421 /*******************************************************************
422  *         parse_spec_flags
423  *
424  * Parse the optional flags for an entry point in a .spec file.
425  */
426 static const char *parse_spec_flags( ORDDEF *odp )
427 {
428     unsigned int i;
429     const char *token;
430
431     do
432     {
433         if (!(token = GetToken(0))) break;
434         if (!strncmp( token, "arch=", 5))
435         {
436             char *args = xstrdup( token + 5 );
437             char *cpu_name = strtok( args, "," );
438             while (cpu_name)
439             {
440                 enum target_cpu cpu = get_cpu_from_name( cpu_name );
441                 if (cpu == -1)
442                 {
443                     error( "Unknown architecture '%s'\n", cpu_name );
444                     return NULL;
445                 }
446                 odp->flags |= FLAG_CPU( cpu );
447                 cpu_name = strtok( NULL, "," );
448             }
449             free( args );
450         }
451         else if (!strcmp( token, "i386" ))  /* backwards compatibility */
452         {
453             odp->flags |= FLAG_CPU(CPU_x86);
454         }
455         else
456         {
457             for (i = 0; FlagNames[i]; i++)
458                 if (!strcmp( FlagNames[i], token )) break;
459             if (!FlagNames[i])
460             {
461                 error( "Unknown flag '%s'\n", token );
462                 return NULL;
463             }
464             odp->flags |= 1 << i;
465         }
466         token = GetToken(0);
467     } while (token && *token == '-');
468
469     return token;
470 }
471
472
473 /*******************************************************************
474  *         parse_spec_ordinal
475  *
476  * Parse an ordinal definition in a .spec file.
477  */
478 static int parse_spec_ordinal( int ordinal, DLLSPEC *spec )
479 {
480     const char *token;
481     size_t len;
482     ORDDEF *odp = add_entry_point( spec );
483
484     if (!(token = GetToken(0))) goto error;
485
486     for (odp->type = 0; odp->type < TYPE_NBTYPES; odp->type++)
487         if (TypeNames[odp->type] && !strcmp( token, TypeNames[odp->type] ))
488             break;
489
490     if (odp->type >= TYPE_NBTYPES)
491     {
492         error( "Expected type after ordinal, found '%s' instead\n", token );
493         goto error;
494     }
495
496     if (!(token = GetToken(0))) goto error;
497     if (*token == '-' && !(token = parse_spec_flags( odp ))) goto error;
498
499     odp->name = xstrdup( token );
500     odp->lineno = current_line;
501     odp->ordinal = ordinal;
502
503     len = strspn( odp->name, valid_ordname_chars );
504     if (len < strlen( odp->name ))
505     {
506         error( "Character '%c' is not allowed in exported name '%s'\n", odp->name[len], odp->name );
507         goto error;
508     }
509
510     switch(odp->type)
511     {
512     case TYPE_VARIABLE:
513         if (!parse_spec_variable( odp, spec )) goto error;
514         break;
515     case TYPE_PASCAL:
516     case TYPE_STDCALL:
517     case TYPE_VARARGS:
518     case TYPE_CDECL:
519         if (!parse_spec_export( odp, spec )) goto error;
520         break;
521     case TYPE_ABS:
522         if (!parse_spec_equate( odp, spec )) goto error;
523         break;
524     case TYPE_STUB:
525         if (!parse_spec_stub( odp, spec )) goto error;
526         break;
527     case TYPE_EXTERN:
528         if (!parse_spec_extern( odp, spec )) goto error;
529         break;
530     default:
531         assert( 0 );
532     }
533
534     if ((odp->flags & FLAG_CPU_MASK) && !(odp->flags & FLAG_CPU(target_cpu)))
535     {
536         /* ignore this entry point */
537         spec->nb_entry_points--;
538         return 1;
539     }
540
541     if (ordinal != -1)
542     {
543         if (!ordinal)
544         {
545             error( "Ordinal 0 is not valid\n" );
546             goto error;
547         }
548         if (ordinal >= MAX_ORDINALS)
549         {
550             error( "Ordinal number %d too large\n", ordinal );
551             goto error;
552         }
553         if (ordinal > spec->limit) spec->limit = ordinal;
554         if (ordinal < spec->base) spec->base = ordinal;
555         odp->ordinal = ordinal;
556     }
557
558     if (odp->type == TYPE_STDCALL && !(odp->flags & FLAG_PRIVATE))
559     {
560         if (!strcmp( odp->name, "DllRegisterServer" ) ||
561             !strcmp( odp->name, "DllUnregisterServer" ) ||
562             !strcmp( odp->name, "DllGetClassObject" ) ||
563             !strcmp( odp->name, "DllGetVersion" ) ||
564             !strcmp( odp->name, "DllInstall" ) ||
565             !strcmp( odp->name, "DllCanUnloadNow" ))
566         {
567             warning( "Function %s should be marked private\n", odp->name );
568             if (strcmp( odp->name, odp->link_name ))
569                 warning( "Function %s should not use a different internal name (%s)\n",
570                          odp->name, odp->link_name );
571         }
572     }
573
574     if (!strcmp( odp->name, "@" ) || odp->flags & (FLAG_NONAME | FLAG_ORDINAL))
575     {
576         if (ordinal == -1)
577         {
578             if (!strcmp( odp->name, "@" ))
579                 error( "Nameless function needs an explicit ordinal number\n" );
580             else
581                 error( "Function imported by ordinal needs an explicit ordinal number\n" );
582             goto error;
583         }
584         if (spec->type != SPEC_WIN32)
585         {
586             error( "Nameless functions not supported for Win16\n" );
587             goto error;
588         }
589         if (!strcmp( odp->name, "@" ))
590         {
591             free( odp->name );
592             odp->name = NULL;
593         }
594         else if (!(odp->flags & FLAG_ORDINAL))  /* -ordinal only affects the import library */
595         {
596             odp->export_name = odp->name;
597             odp->name = NULL;
598         }
599     }
600     return 1;
601
602 error:
603     spec->nb_entry_points--;
604     free( odp->name );
605     return 0;
606 }
607
608
609 static int name_compare( const void *ptr1, const void *ptr2 )
610 {
611     const ORDDEF *odp1 = *(const ORDDEF * const *)ptr1;
612     const ORDDEF *odp2 = *(const ORDDEF * const *)ptr2;
613     const char *name1 = odp1->name ? odp1->name : odp1->export_name;
614     const char *name2 = odp2->name ? odp2->name : odp2->export_name;
615     return strcmp( name1, name2 );
616 }
617
618 /*******************************************************************
619  *         assign_names
620  *
621  * Build the name array and catch duplicates.
622  */
623 static void assign_names( DLLSPEC *spec )
624 {
625     int i, j, nb_exp_names = 0;
626     ORDDEF **all_names;
627
628     spec->nb_names = 0;
629     for (i = 0; i < spec->nb_entry_points; i++)
630         if (spec->entry_points[i].name) spec->nb_names++;
631         else if (spec->entry_points[i].export_name) nb_exp_names++;
632
633     if (!spec->nb_names && !nb_exp_names) return;
634
635     /* check for duplicates */
636
637     all_names = xmalloc( (spec->nb_names + nb_exp_names) * sizeof(all_names[0]) );
638     for (i = j = 0; i < spec->nb_entry_points; i++)
639         if (spec->entry_points[i].name || spec->entry_points[i].export_name)
640             all_names[j++] = &spec->entry_points[i];
641
642     qsort( all_names, j, sizeof(all_names[0]), name_compare );
643
644     for (i = 0; i < j - 1; i++)
645     {
646         const char *name1 = all_names[i]->name ? all_names[i]->name : all_names[i]->export_name;
647         const char *name2 = all_names[i+1]->name ? all_names[i+1]->name : all_names[i+1]->export_name;
648         if (!strcmp( name1, name2 ))
649         {
650             current_line = max( all_names[i]->lineno, all_names[i+1]->lineno );
651             error( "'%s' redefined\n%s:%d: First defined here\n",
652                    name1, input_file_name,
653                    min( all_names[i]->lineno, all_names[i+1]->lineno ) );
654         }
655     }
656     free( all_names );
657
658     if (spec->nb_names)
659     {
660         spec->names = xmalloc( spec->nb_names * sizeof(spec->names[0]) );
661         for (i = j = 0; i < spec->nb_entry_points; i++)
662             if (spec->entry_points[i].name) spec->names[j++] = &spec->entry_points[i];
663
664         /* sort the list of names */
665         qsort( spec->names, spec->nb_names, sizeof(spec->names[0]), name_compare );
666     }
667 }
668
669 /*******************************************************************
670  *         assign_ordinals
671  *
672  * Build the ordinal array.
673  */
674 static void assign_ordinals( DLLSPEC *spec )
675 {
676     int i, count, ordinal;
677
678     /* start assigning from base, or from 1 if no ordinal defined yet */
679
680     spec->base = MAX_ORDINALS;
681     spec->limit = 0;
682     for (i = 0; i < spec->nb_entry_points; i++)
683     {
684         ordinal = spec->entry_points[i].ordinal;
685         if (ordinal == -1) continue;
686         if (ordinal > spec->limit) spec->limit = ordinal;
687         if (ordinal < spec->base) spec->base = ordinal;
688     }
689     if (spec->base == MAX_ORDINALS) spec->base = 1;
690     if (spec->limit < spec->base) spec->limit = spec->base;
691
692     count = max( spec->limit + 1, spec->base + spec->nb_entry_points );
693     spec->ordinals = xmalloc( count * sizeof(spec->ordinals[0]) );
694     memset( spec->ordinals, 0, count * sizeof(spec->ordinals[0]) );
695
696     /* fill in all explicitly specified ordinals */
697     for (i = 0; i < spec->nb_entry_points; i++)
698     {
699         ordinal = spec->entry_points[i].ordinal;
700         if (ordinal == -1) continue;
701         if (spec->ordinals[ordinal])
702         {
703             current_line = max( spec->entry_points[i].lineno, spec->ordinals[ordinal]->lineno );
704             error( "ordinal %d redefined\n%s:%d: First defined here\n",
705                    ordinal, input_file_name,
706                    min( spec->entry_points[i].lineno, spec->ordinals[ordinal]->lineno ) );
707         }
708         else spec->ordinals[ordinal] = &spec->entry_points[i];
709     }
710
711     /* now assign ordinals to the rest */
712     for (i = 0, ordinal = spec->base; i < spec->nb_entry_points; i++)
713     {
714         if (spec->entry_points[i].ordinal != -1) continue;
715         while (spec->ordinals[ordinal]) ordinal++;
716         if (ordinal >= MAX_ORDINALS)
717         {
718             current_line = spec->entry_points[i].lineno;
719             fatal_error( "Too many functions defined (max %d)\n", MAX_ORDINALS );
720         }
721         spec->entry_points[i].ordinal = ordinal;
722         spec->ordinals[ordinal] = &spec->entry_points[i];
723     }
724     if (ordinal > spec->limit) spec->limit = ordinal;
725 }
726
727
728 /*******************************************************************
729  *         add_16bit_exports
730  *
731  * Add the necessary exports to the 32-bit counterpart of a 16-bit module.
732  */
733 void add_16bit_exports( DLLSPEC *spec32, DLLSPEC *spec16 )
734 {
735     ORDDEF *odp;
736
737     /* add an export for the NE module */
738
739     odp = add_entry_point( spec32 );
740     odp->type = TYPE_EXTERN;
741     odp->name = xstrdup( "__wine_spec_dos_header" );
742     odp->lineno = 0;
743     odp->ordinal = 1;
744     odp->link_name = xstrdup( ".L__wine_spec_dos_header" );
745
746     if (spec16->main_module)
747     {
748         odp = add_entry_point( spec32 );
749         odp->type = TYPE_EXTERN;
750         odp->name = xstrdup( "__wine_spec_main_module" );
751         odp->lineno = 0;
752         odp->ordinal = 2;
753         odp->link_name = xstrdup( ".L__wine_spec_main_module" );
754     }
755
756     assign_names( spec32 );
757     assign_ordinals( spec32 );
758 }
759
760
761 /*******************************************************************
762  *         parse_spec_file
763  *
764  * Parse a .spec file.
765  */
766 int parse_spec_file( FILE *file, DLLSPEC *spec )
767 {
768     const char *token;
769
770     input_file = file;
771     current_line = 0;
772
773     comment_chars = "#;";
774     separator_chars = "()-";
775
776     while (get_next_line())
777     {
778         if (!(token = GetToken(1))) continue;
779         if (strcmp(token, "@") == 0)
780         {
781             if (spec->type != SPEC_WIN32)
782             {
783                 error( "'@' ordinals not supported for Win16\n" );
784                 continue;
785             }
786             if (!parse_spec_ordinal( -1, spec )) continue;
787         }
788         else if (IsNumberString(token))
789         {
790             if (!parse_spec_ordinal( atoi(token), spec )) continue;
791         }
792         else
793         {
794             error( "Expected ordinal declaration, got '%s'\n", token );
795             continue;
796         }
797         if ((token = GetToken(1))) error( "Syntax error near '%s'\n", token );
798     }
799
800     current_line = 0;  /* no longer parsing the input file */
801     assign_names( spec );
802     assign_ordinals( spec );
803     return !nb_errors;
804 }
805
806
807 /*******************************************************************
808  *         parse_def_library
809  *
810  * Parse a LIBRARY declaration in a .def file.
811  */
812 static int parse_def_library( DLLSPEC *spec )
813 {
814     const char *token = GetToken(1);
815
816     if (!token) return 1;
817     if (strcmp( token, "BASE" ))
818     {
819         free( spec->file_name );
820         spec->file_name = xstrdup( token );
821         if (!(token = GetToken(1))) return 1;
822     }
823     if (strcmp( token, "BASE" ))
824     {
825         error( "Expected library name or BASE= declaration, got '%s'\n", token );
826         return 0;
827     }
828     if (!(token = GetToken(0))) return 0;
829     if (strcmp( token, "=" ))
830     {
831         error( "Expected '=' after BASE, got '%s'\n", token );
832         return 0;
833     }
834     if (!(token = GetToken(0))) return 0;
835     /* FIXME: do something with base address */
836
837     return 1;
838 }
839
840
841 /*******************************************************************
842  *         parse_def_stack_heap_size
843  *
844  * Parse a STACKSIZE or HEAPSIZE declaration in a .def file.
845  */
846 static int parse_def_stack_heap_size( int is_stack, DLLSPEC *spec )
847 {
848     const char *token = GetToken(0);
849     char *end;
850     unsigned long size;
851
852     if (!token) return 0;
853     size = strtoul( token, &end, 0 );
854     if (*end)
855     {
856         error( "Invalid number '%s'\n", token );
857         return 0;
858     }
859     if (is_stack) spec->stack_size = size / 1024;
860     else spec->heap_size = size / 1024;
861     if (!(token = GetToken(1))) return 1;
862     if (strcmp( token, "," ))
863     {
864         error( "Expected ',' after size, got '%s'\n", token );
865         return 0;
866     }
867     if (!(token = GetToken(0))) return 0;
868     /* FIXME: do something with reserve size */
869     return 1;
870 }
871
872
873 /*******************************************************************
874  *         parse_def_export
875  *
876  * Parse an export declaration in a .def file.
877  */
878 static int parse_def_export( char *name, DLLSPEC *spec )
879 {
880     int i, args;
881     const char *token = GetToken(1);
882     ORDDEF *odp = add_entry_point( spec );
883
884     odp->lineno = current_line;
885     odp->ordinal = -1;
886     odp->name = name;
887     args = remove_stdcall_decoration( odp->name );
888     if (args == -1) odp->type = TYPE_CDECL;
889     else
890     {
891         odp->type = TYPE_STDCALL;
892         args /= get_ptr_size();
893         if (args >= sizeof(odp->u.func.arg_types))
894         {
895             error( "Too many arguments in stdcall function '%s'\n", odp->name );
896             return 0;
897         }
898         for (i = 0; i < args; i++) odp->u.func.arg_types[i] = 'l';
899     }
900
901     /* check for optional internal name */
902
903     if (token && !strcmp( token, "=" ))
904     {
905         if (!(token = GetToken(0))) goto error;
906         odp->link_name = xstrdup( token );
907         remove_stdcall_decoration( odp->link_name );
908         token = GetToken(1);
909     }
910     else
911     {
912       odp->link_name = xstrdup( name );
913     }
914
915     /* check for optional ordinal */
916
917     if (token && token[0] == '@')
918     {
919         int ordinal;
920
921         if (!IsNumberString( token+1 ))
922         {
923             error( "Expected number after '@', got '%s'\n", token+1 );
924             goto error;
925         }
926         ordinal = atoi( token+1 );
927         if (!ordinal)
928         {
929             error( "Ordinal 0 is not valid\n" );
930             goto error;
931         }
932         if (ordinal >= MAX_ORDINALS)
933         {
934             error( "Ordinal number %d too large\n", ordinal );
935             goto error;
936         }
937         odp->ordinal = ordinal;
938         token = GetToken(1);
939     }
940
941     /* check for other optional keywords */
942
943     if (token && !strcmp( token, "NONAME" ))
944     {
945         if (odp->ordinal == -1)
946         {
947             error( "NONAME requires an ordinal\n" );
948             goto error;
949         }
950         odp->export_name = odp->name;
951         odp->name = NULL;
952         odp->flags |= FLAG_NONAME;
953         token = GetToken(1);
954     }
955     if (token && !strcmp( token, "PRIVATE" ))
956     {
957         odp->flags |= FLAG_PRIVATE;
958         token = GetToken(1);
959     }
960     if (token && !strcmp( token, "DATA" ))
961     {
962         odp->type = TYPE_EXTERN;
963         token = GetToken(1);
964     }
965     if (token)
966     {
967         error( "Garbage text '%s' found at end of export declaration\n", token );
968         goto error;
969     }
970     return 1;
971
972 error:
973     spec->nb_entry_points--;
974     free( odp->name );
975     return 0;
976 }
977
978
979 /*******************************************************************
980  *         parse_def_file
981  *
982  * Parse a .def file.
983  */
984 int parse_def_file( FILE *file, DLLSPEC *spec )
985 {
986     const char *token;
987     int in_exports = 0;
988
989     input_file = file;
990     current_line = 0;
991
992     comment_chars = ";";
993     separator_chars = ",=";
994
995     while (get_next_line())
996     {
997         if (!(token = GetToken(1))) continue;
998
999         if (!strcmp( token, "LIBRARY" ) || !strcmp( token, "NAME" ))
1000         {
1001             if (!parse_def_library( spec )) continue;
1002             goto end_of_line;
1003         }
1004         else if (!strcmp( token, "STACKSIZE" ))
1005         {
1006             if (!parse_def_stack_heap_size( 1, spec )) continue;
1007             goto end_of_line;
1008         }
1009         else if (!strcmp( token, "HEAPSIZE" ))
1010         {
1011             if (!parse_def_stack_heap_size( 0, spec )) continue;
1012             goto end_of_line;
1013         }
1014         else if (!strcmp( token, "EXPORTS" ))
1015         {
1016             in_exports = 1;
1017             if (!(token = GetToken(1))) continue;
1018         }
1019         else if (!strcmp( token, "IMPORTS" ))
1020         {
1021             in_exports = 0;
1022             if (!(token = GetToken(1))) continue;
1023         }
1024         else if (!strcmp( token, "SECTIONS" ))
1025         {
1026             in_exports = 0;
1027             if (!(token = GetToken(1))) continue;
1028         }
1029
1030         if (!in_exports) continue;  /* ignore this line */
1031         if (!parse_def_export( xstrdup(token), spec )) continue;
1032
1033     end_of_line:
1034         if ((token = GetToken(1))) error( "Syntax error near '%s'\n", token );
1035     }
1036
1037     current_line = 0;  /* no longer parsing the input file */
1038     assign_names( spec );
1039     assign_ordinals( spec );
1040     return !nb_errors;
1041 }