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