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