Fix the case of product and company names.
[wine] / tools / winebuild / parser.c
1 /*
2  * Spec file parser
3  *
4  * Copyright 1993 Robert J. Amstadt
5  * Copyright 1995 Martin von Loewis
6  * Copyright 1995, 1996, 1997 Alexandre Julliard
7  * Copyright 1997 Eric Youngdale
8  * Copyright 1999 Ulrich Weigand
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23  */
24
25 #include "config.h"
26 #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 * const TypeNames[TYPE_NBTYPES] =
47 {
48     "variable",     /* TYPE_VARIABLE */
49     "pascal",       /* TYPE_PASCAL */
50     "equate",       /* TYPE_ABS */
51     "stub",         /* TYPE_STUB */
52     "stdcall",      /* TYPE_STDCALL */
53     "cdecl",        /* TYPE_CDECL */
54     "varargs",      /* TYPE_VARARGS */
55     "extern"        /* TYPE_EXTERN */
56 };
57
58 static const char * const FlagNames[] =
59 {
60     "norelay",     /* FLAG_NORELAY */
61     "noname",      /* FLAG_NONAME */
62     "ret16",       /* FLAG_RET16 */
63     "ret64",       /* FLAG_RET64 */
64     "i386",        /* FLAG_I386 */
65     "register",    /* FLAG_REGISTER */
66     "interrupt",   /* FLAG_INTERRUPT */
67     "private",     /* FLAG_PRIVATE */
68     NULL
69 };
70
71 static int IsNumberString(const char *s)
72 {
73     while (*s) if (!isdigit(*s++)) return 0;
74     return 1;
75 }
76
77 inline static int is_token_separator( char ch )
78 {
79     return (ch == '(' || ch == ')' || ch == '-');
80 }
81
82 /* get the next line from the input file, or return 0 if at eof */
83 static int get_next_line(void)
84 {
85     ParseNext = ParseBuffer;
86     current_line++;
87     return (fgets(ParseBuffer, sizeof(ParseBuffer), input_file) != NULL);
88 }
89
90 static const char * GetToken( int allow_eol )
91 {
92     char *p = ParseNext;
93     char *token = TokenBuffer;
94
95     for (;;)
96     {
97         /* remove initial white space */
98         p = ParseNext;
99         while (isspace(*p)) p++;
100
101         if (*p == '\\' && p[1] == '\n')  /* line continuation */
102         {
103             if (!get_next_line())
104             {
105                 if (!allow_eol) error( "Unexpected end of file\n" );
106                 return NULL;
107             }
108         }
109         else break;
110     }
111
112     if ((*p == '\0') || (*p == '#'))
113     {
114         if (!allow_eol) error( "Declaration not terminated properly\n" );
115         return NULL;
116     }
117
118     /*
119      * Find end of token.
120      */
121     if (is_token_separator(*p))
122     {
123         /* a separator is always a complete token */
124         *token++ = *p++;
125     }
126     else while (*p != '\0' && !is_token_separator(*p) && !isspace(*p))
127     {
128         if (*p == '\\') p++;
129         if (*p) *token++ = *p++;
130     }
131     *token = '\0';
132     ParseNext = p;
133     return TokenBuffer;
134 }
135
136
137 /*******************************************************************
138  *         ParseVariable
139  *
140  * Parse a variable definition.
141  */
142 static int ParseVariable( ORDDEF *odp )
143 {
144     char *endptr;
145     int *value_array;
146     int n_values;
147     int value_array_size;
148     const char *token;
149
150     if (SpecType == SPEC_WIN32)
151     {
152         error( "'variable' not supported in Win32, use 'extern' instead\n" );
153         return 0;
154     }
155
156     if (!(token = GetToken(0))) return 0;
157     if (*token != '(')
158     {
159         error( "Expected '(' got '%s'\n", token );
160         return 0;
161     }
162
163     n_values = 0;
164     value_array_size = 25;
165     value_array = xmalloc(sizeof(*value_array) * value_array_size);
166
167     for (;;)
168     {
169         if (!(token = GetToken(0)))
170         {
171             free( value_array );
172             return 0;
173         }
174         if (*token == ')')
175             break;
176
177         value_array[n_values++] = strtol(token, &endptr, 0);
178         if (n_values == value_array_size)
179         {
180             value_array_size += 25;
181             value_array = xrealloc(value_array,
182                                    sizeof(*value_array) * value_array_size);
183         }
184
185         if (endptr == NULL || *endptr != '\0')
186         {
187             error( "Expected number value, got '%s'\n", token );
188             free( value_array );
189             return 0;
190         }
191     }
192
193     odp->u.var.n_values = n_values;
194     odp->u.var.values = xrealloc(value_array, sizeof(*value_array) * n_values);
195     return 1;
196 }
197
198
199 /*******************************************************************
200  *         ParseExportFunction
201  *
202  * Parse a function definition.
203  */
204 static int ParseExportFunction( ORDDEF *odp )
205 {
206     const char *token;
207     unsigned int i;
208
209     switch(SpecType)
210     {
211     case SPEC_WIN16:
212         if (odp->type == TYPE_STDCALL)
213         {
214             error( "'stdcall' not supported for Win16\n" );
215             return 0;
216         }
217         break;
218     case SPEC_WIN32:
219         if (odp->type == TYPE_PASCAL)
220         {
221             error( "'pascal' not supported for Win32\n" );
222             return 0;
223         }
224         if (odp->flags & FLAG_INTERRUPT)
225         {
226             error( "'interrupt' not supported for Win32\n" );
227             return 0;
228         }
229         break;
230     default:
231         break;
232     }
233
234     if (!(token = GetToken(0))) return 0;
235     if (*token != '(')
236     {
237         error( "Expected '(' got '%s'\n", token );
238         return 0;
239     }
240
241     for (i = 0; i < sizeof(odp->u.func.arg_types); i++)
242     {
243         if (!(token = GetToken(0))) return 0;
244         if (*token == ')')
245             break;
246
247         if (!strcmp(token, "word"))
248             odp->u.func.arg_types[i] = 'w';
249         else if (!strcmp(token, "s_word"))
250             odp->u.func.arg_types[i] = 's';
251         else if (!strcmp(token, "long") || !strcmp(token, "segptr"))
252             odp->u.func.arg_types[i] = 'l';
253         else if (!strcmp(token, "ptr"))
254             odp->u.func.arg_types[i] = 'p';
255         else if (!strcmp(token, "str"))
256             odp->u.func.arg_types[i] = 't';
257         else if (!strcmp(token, "wstr"))
258             odp->u.func.arg_types[i] = 'W';
259         else if (!strcmp(token, "segstr"))
260             odp->u.func.arg_types[i] = 'T';
261         else if (!strcmp(token, "double"))
262         {
263             odp->u.func.arg_types[i++] = 'l';
264             if (i < sizeof(odp->u.func.arg_types)) odp->u.func.arg_types[i] = 'l';
265         }
266         else
267         {
268             error( "Unknown argument type '%s'\n", token );
269             return 0;
270         }
271
272         if (SpecType == SPEC_WIN32)
273         {
274             if (strcmp(token, "long") &&
275                 strcmp(token, "ptr") &&
276                 strcmp(token, "str") &&
277                 strcmp(token, "wstr") &&
278                 strcmp(token, "double"))
279             {
280                 error( "Type '%s' not supported for Win32\n", token );
281                 return 0;
282             }
283         }
284     }
285     if ((*token != ')') || (i >= sizeof(odp->u.func.arg_types)))
286     {
287         error( "Too many arguments\n" );
288         return 0;
289     }
290
291     odp->u.func.arg_types[i] = '\0';
292     if (odp->type == TYPE_VARARGS)
293         odp->flags |= FLAG_NORELAY;  /* no relay debug possible for varags entry point */
294
295     if (!(token = GetToken(1)))
296     {
297         if (!strcmp( odp->name, "@" ))
298         {
299             error( "Missing handler name for anonymous function\n" );
300             return 0;
301         }
302         odp->link_name = xstrdup( odp->name );
303     }
304     else
305     {
306         odp->link_name = xstrdup( token );
307         if (strchr( odp->link_name, '.' ))
308         {
309             if (SpecType == SPEC_WIN16)
310             {
311                 error( "Forwarded functions not supported for Win16\n" );
312                 return 0;
313             }
314             odp->flags |= FLAG_FORWARD;
315         }
316     }
317     return 1;
318 }
319
320
321 /*******************************************************************
322  *         ParseEquate
323  *
324  * Parse an 'equate' definition.
325  */
326 static int ParseEquate( ORDDEF *odp )
327 {
328     char *endptr;
329     int value;
330     const char *token;
331
332     if (SpecType == SPEC_WIN32)
333     {
334         error( "'equate' not supported for Win32\n" );
335         return 0;
336     }
337     if (!(token = GetToken(0))) return 0;
338     value = strtol(token, &endptr, 0);
339     if (endptr == NULL || *endptr != '\0')
340     {
341         error( "Expected number value, got '%s'\n", token );
342         return 0;
343     }
344     odp->u.abs.value = value;
345     return 1;
346 }
347
348
349 /*******************************************************************
350  *         ParseStub
351  *
352  * Parse a 'stub' definition.
353  */
354 static int ParseStub( ORDDEF *odp )
355 {
356     odp->u.func.arg_types[0] = '\0';
357     odp->link_name = xstrdup("");
358     return 1;
359 }
360
361
362 /*******************************************************************
363  *         ParseExtern
364  *
365  * Parse an 'extern' definition.
366  */
367 static int ParseExtern( ORDDEF *odp )
368 {
369     const char *token;
370
371     if (SpecType == SPEC_WIN16)
372     {
373         error( "'extern' not supported for Win16, use 'variable' instead\n" );
374         return 0;
375     }
376     if (!(token = GetToken(1)))
377     {
378         if (!strcmp( odp->name, "@" ))
379         {
380             error( "Missing handler name for anonymous extern\n" );
381             return 0;
382         }
383         odp->link_name = xstrdup( odp->name );
384     }
385     else
386     {
387         odp->link_name = xstrdup( token );
388         if (strchr( odp->link_name, '.' )) odp->flags |= FLAG_FORWARD;
389     }
390     return 1;
391 }
392
393
394 /*******************************************************************
395  *         ParseFlags
396  *
397  * Parse the optional flags for an entry point
398  */
399 static const char *ParseFlags( ORDDEF *odp )
400 {
401     unsigned int i;
402     const char *token;
403
404     do
405     {
406         if (!(token = GetToken(0))) break;
407         for (i = 0; FlagNames[i]; i++)
408             if (!strcmp( FlagNames[i], token )) break;
409         if (!FlagNames[i])
410         {
411             error( "Unknown flag '%s'\n", token );
412             return NULL;
413         }
414         odp->flags |= 1 << i;
415         token = GetToken(0);
416     } while (token && *token == '-');
417
418     return token;
419 }
420
421 /*******************************************************************
422  *         fix_export_name
423  *
424  * Fix an exported function name by removing a possible @xx suffix
425  */
426 static void fix_export_name( char *name )
427 {
428     char *p, *end = strrchr( name, '@' );
429     if (!end || !end[1] || end == name) return;
430     /* make sure all the rest is digits */
431     for (p = end + 1; *p; p++) if (!isdigit(*p)) return;
432     *end = 0;
433 }
434
435 /*******************************************************************
436  *         ParseOrdinal
437  *
438  * Parse an ordinal definition.
439  */
440 static int ParseOrdinal(int ordinal)
441 {
442     const char *token;
443
444     ORDDEF *odp = xmalloc( sizeof(*odp) );
445     memset( odp, 0, sizeof(*odp) );
446     EntryPoints[nb_entry_points++] = odp;
447
448     if (!(token = GetToken(0))) goto error;
449
450     for (odp->type = 0; odp->type < TYPE_NBTYPES; odp->type++)
451         if (TypeNames[odp->type] && !strcmp( token, TypeNames[odp->type] ))
452             break;
453
454     if (odp->type >= TYPE_NBTYPES)
455     {
456         /* special case for backwards compatibility */
457         if (!strcmp( token, "pascal16" ))
458         {
459             odp->type = TYPE_PASCAL;
460             odp->flags |= FLAG_RET16;
461         }
462         else
463         {
464             error( "Expected type after ordinal, found '%s' instead\n", token );
465             goto error;
466         }
467     }
468
469     if (!(token = GetToken(0))) goto error;
470     if (*token == '-' && !(token = ParseFlags( odp ))) goto error;
471
472     odp->name = xstrdup( token );
473     fix_export_name( odp->name );
474     odp->lineno = current_line;
475     odp->ordinal = ordinal;
476
477     switch(odp->type)
478     {
479     case TYPE_VARIABLE:
480         if (!ParseVariable( odp )) goto error;
481         break;
482     case TYPE_PASCAL:
483     case TYPE_STDCALL:
484     case TYPE_VARARGS:
485     case TYPE_CDECL:
486         if (!ParseExportFunction( odp )) goto error;
487         break;
488     case TYPE_ABS:
489         if (!ParseEquate( odp )) goto error;
490         break;
491     case TYPE_STUB:
492         if (!ParseStub( odp )) goto error;
493         break;
494     case TYPE_EXTERN:
495         if (!ParseExtern( odp )) goto error;
496         break;
497     default:
498         assert( 0 );
499     }
500
501 #ifndef __i386__
502     if (odp->flags & FLAG_I386)
503     {
504         /* ignore this entry point on non-Intel archs */
505         EntryPoints[--nb_entry_points] = NULL;
506         free( odp );
507         return 1;
508     }
509 #endif
510
511     if (ordinal != -1)
512     {
513         if (!ordinal)
514         {
515             error( "Ordinal 0 is not valid\n" );
516             goto error;
517         }
518         if (ordinal >= MAX_ORDINALS)
519         {
520             error( "Ordinal number %d too large\n", ordinal );
521             goto error;
522         }
523         if (ordinal > Limit) Limit = ordinal;
524         if (ordinal < Base) Base = ordinal;
525         odp->ordinal = ordinal;
526         if (Ordinals[ordinal])
527         {
528             error( "Duplicate ordinal %d\n", ordinal );
529             goto error;
530         }
531         Ordinals[ordinal] = odp;
532     }
533
534     if (!strcmp( odp->name, "@" ) || odp->flags & FLAG_NONAME)
535     {
536         if (ordinal == -1)
537         {
538             error( "Nameless function needs an explicit ordinal number\n" );
539             goto error;
540         }
541         if (SpecType != SPEC_WIN32)
542         {
543             error( "Nameless functions not supported for Win16\n" );
544             goto error;
545         }
546         if (!strcmp( odp->name, "@" )) free( odp->name );
547         else odp->export_name = odp->name;
548         odp->name = NULL;
549     }
550     else Names[nb_names++] = odp;
551     return 1;
552
553 error:
554     EntryPoints[--nb_entry_points] = NULL;
555     free( odp->name );
556     free( odp );
557     return 0;
558 }
559
560
561 static int name_compare( const void *name1, const void *name2 )
562 {
563     ORDDEF *odp1 = *(ORDDEF **)name1;
564     ORDDEF *odp2 = *(ORDDEF **)name2;
565     return strcmp( odp1->name, odp2->name );
566 }
567
568 /*******************************************************************
569  *         sort_names
570  *
571  * Sort the name array and catch duplicates.
572  */
573 static void sort_names(void)
574 {
575     int i;
576
577     if (!nb_names) return;
578
579     /* sort the list of names */
580     qsort( Names, nb_names, sizeof(Names[0]), name_compare );
581
582     /* check for duplicate names */
583     for (i = 0; i < nb_names - 1; i++)
584     {
585         if (!strcmp( Names[i]->name, Names[i+1]->name ))
586         {
587             current_line = max( Names[i]->lineno, Names[i+1]->lineno );
588             error( "'%s' redefined\n%s:%d: First defined here\n",
589                    Names[i]->name, input_file_name,
590                    min( Names[i]->lineno, Names[i+1]->lineno ) );
591         }
592     }
593 }
594
595
596 /*******************************************************************
597  *         ParseTopLevel
598  *
599  * Parse a spec file.
600  */
601 int ParseTopLevel( FILE *file )
602 {
603     const char *token;
604
605     input_file = file;
606     current_line = 0;
607
608     while (get_next_line())
609     {
610         if (!(token = GetToken(1))) continue;
611         if (strcmp(token, "@") == 0)
612         {
613             if (SpecType != SPEC_WIN32)
614             {
615                 error( "'@' ordinals not supported for Win16\n" );
616                 continue;
617             }
618             if (!ParseOrdinal( -1 )) continue;
619         }
620         else if (IsNumberString(token))
621         {
622             if (!ParseOrdinal( atoi(token) )) continue;
623         }
624         else
625         {
626             error( "Expected ordinal declaration, got '%s'\n", token );
627             continue;
628         }
629         if ((token = GetToken(1))) error( "Syntax error near '%s'\n", token );
630     }
631
632     current_line = 0;  /* no longer parsing the input file */
633     sort_names();
634     return !nb_errors;
635 }
636
637
638 /*******************************************************************
639  *         add_debug_channel
640  */
641 static void add_debug_channel( const char *name )
642 {
643     int i;
644
645     for (i = 0; i < nb_debug_channels; i++)
646         if (!strcmp( debug_channels[i], name )) return;
647
648     debug_channels = xrealloc( debug_channels, (nb_debug_channels + 1) * sizeof(*debug_channels));
649     debug_channels[nb_debug_channels++] = xstrdup(name);
650 }
651
652
653 /*******************************************************************
654  *         parse_debug_channels
655  *
656  * Parse a source file and extract the debug channel definitions.
657  */
658 int parse_debug_channels( const char *srcdir, const char *filename )
659 {
660     FILE *file;
661     int eol_seen = 1;
662
663     file = open_input_file( srcdir, filename );
664     while (fgets( ParseBuffer, sizeof(ParseBuffer), file ))
665     {
666         char *channel, *end, *p = ParseBuffer;
667
668         p = ParseBuffer + strlen(ParseBuffer) - 1;
669         if (!eol_seen)  /* continuation line */
670         {
671             eol_seen = (*p == '\n');
672             continue;
673         }
674         if ((eol_seen = (*p == '\n'))) *p = 0;
675
676         p = ParseBuffer;
677         while (isspace(*p)) p++;
678         if (!memcmp( p, "WINE_DECLARE_DEBUG_CHANNEL", 26 ) ||
679             !memcmp( p, "WINE_DEFAULT_DEBUG_CHANNEL", 26 ))
680         {
681             p += 26;
682             while (isspace(*p)) p++;
683             if (*p != '(')
684             {
685                 error( "invalid debug channel specification '%s'\n", ParseBuffer );
686                 goto next;
687             }
688             p++;
689             while (isspace(*p)) p++;
690             if (!isalpha(*p))
691             {
692                 error( "invalid debug channel specification '%s'\n", ParseBuffer );
693                 goto next;
694             }
695             channel = p;
696             while (isalnum(*p) || *p == '_') p++;
697             end = p;
698             while (isspace(*p)) p++;
699             if (*p != ')')
700             {
701                 error( "invalid debug channel specification '%s'\n", ParseBuffer );
702                 goto next;
703             }
704             *end = 0;
705             add_debug_channel( channel );
706         }
707     next:
708         current_line++;
709     }
710     close_input_file( file );
711     return !nb_errors;
712 }