Removed support for the 'forward' ordinal type.
[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 <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include "winbase.h"
35 #include "build.h"
36
37 int current_line = 0;
38
39 static char ParseBuffer[512];
40 static char TokenBuffer[512];
41 static char *ParseNext = ParseBuffer;
42 static FILE *input_file;
43
44 static const char * const TypeNames[TYPE_NBTYPES] =
45 {
46     "variable",     /* TYPE_VARIABLE */
47     "pascal16",     /* TYPE_PASCAL_16 */
48     "pascal",       /* TYPE_PASCAL */
49     "equate",       /* TYPE_ABS */
50     "stub",         /* TYPE_STUB */
51     "stdcall",      /* TYPE_STDCALL */
52     "cdecl",        /* TYPE_CDECL */
53     "varargs",      /* TYPE_VARARGS */
54     "extern"        /* TYPE_EXTERN */
55 };
56
57 static const char * const FlagNames[] =
58 {
59     "norelay",     /* FLAG_NORELAY */
60     "noname",      /* FLAG_NONAME */
61     "ret64",       /* FLAG_RET64 */
62     "i386",        /* FLAG_I386 */
63     "register",    /* FLAG_REGISTER */
64     "interrupt",   /* FLAG_INTERRUPT */
65     NULL
66 };
67
68 static int IsNumberString(const char *s)
69 {
70     while (*s) if (!isdigit(*s++)) return 0;
71     return 1;
72 }
73
74 inline static int is_token_separator( char ch )
75 {
76     return (ch == '(' || ch == ')' || ch == '-');
77 }
78
79 static const char * GetTokenInLine(void)
80 {
81     char *p = ParseNext;
82     char *token = TokenBuffer;
83
84     /*
85      * Remove initial white space.
86      */
87     while (isspace(*p)) p++;
88
89     if ((*p == '\0') || (*p == '#')) return NULL;
90
91     /*
92      * Find end of token.
93      */
94     if (is_token_separator(*p))
95     {
96         /* a separator is always a complete token */
97         *token++ = *p++;
98     }
99     else while (*p != '\0' && !is_token_separator(*p) && !isspace(*p))
100     {
101         if (*p == '\\') p++;
102         if (*p) *token++ = *p++;
103     }
104     *token = '\0';
105     ParseNext = p;
106     return TokenBuffer;
107 }
108
109 static const char * GetToken( int allow_eof )
110 {
111     const char *token;
112
113     while ((token = GetTokenInLine()) == NULL)
114     {
115         ParseNext = ParseBuffer;
116         current_line++;
117         if (fgets(ParseBuffer, sizeof(ParseBuffer), input_file) == NULL)
118         {
119             if (!allow_eof) fatal_error( "Unexpected end of file\n" );
120             return NULL;
121         }
122     }
123     return token;
124 }
125
126
127 /*******************************************************************
128  *         ParseVariable
129  *
130  * Parse a variable definition.
131  */
132 static void ParseVariable( ORDDEF *odp )
133 {
134     char *endptr;
135     int *value_array;
136     int n_values;
137     int value_array_size;
138     const char *token;
139
140     if (SpecType == SPEC_WIN32)
141         fatal_error( "'variable' not supported in Win32, use 'extern' instead\n" );
142
143     token = GetToken(0);
144     if (*token != '(') fatal_error( "Expected '(' got '%s'\n", token );
145
146     n_values = 0;
147     value_array_size = 25;
148     value_array = xmalloc(sizeof(*value_array) * value_array_size);
149
150     for (;;)
151     {
152         token = GetToken(0);
153         if (*token == ')')
154             break;
155
156         value_array[n_values++] = strtol(token, &endptr, 0);
157         if (n_values == value_array_size)
158         {
159             value_array_size += 25;
160             value_array = xrealloc(value_array,
161                                    sizeof(*value_array) * value_array_size);
162         }
163
164         if (endptr == NULL || *endptr != '\0')
165             fatal_error( "Expected number value, got '%s'\n", token );
166     }
167
168     odp->u.var.n_values = n_values;
169     odp->u.var.values = xrealloc(value_array, sizeof(*value_array) * n_values);
170 }
171
172
173 /*******************************************************************
174  *         ParseExportFunction
175  *
176  * Parse a function definition.
177  */
178 static void ParseExportFunction( ORDDEF *odp )
179 {
180     const char *token;
181     unsigned int i;
182
183     switch(SpecType)
184     {
185     case SPEC_WIN16:
186         if (odp->type == TYPE_STDCALL)
187             fatal_error( "'stdcall' not supported for Win16\n" );
188         if (odp->type == TYPE_VARARGS)
189             fatal_error( "'varargs' not supported for Win16\n" );
190         break;
191     case SPEC_WIN32:
192         if ((odp->type == TYPE_PASCAL) || (odp->type == TYPE_PASCAL_16))
193             fatal_error( "'pascal' not supported for Win32\n" );
194         if (odp->flags & FLAG_INTERRUPT)
195             fatal_error( "'interrupt' not supported for Win32\n" );
196         break;
197     default:
198         break;
199     }
200
201     token = GetToken(0);
202     if (*token != '(') fatal_error( "Expected '(' got '%s'\n", token );
203
204     for (i = 0; i < sizeof(odp->u.func.arg_types); i++)
205     {
206         token = GetToken(0);
207         if (*token == ')')
208             break;
209
210         if (!strcmp(token, "word"))
211             odp->u.func.arg_types[i] = 'w';
212         else if (!strcmp(token, "s_word"))
213             odp->u.func.arg_types[i] = 's';
214         else if (!strcmp(token, "long") || !strcmp(token, "segptr"))
215             odp->u.func.arg_types[i] = 'l';
216         else if (!strcmp(token, "ptr"))
217             odp->u.func.arg_types[i] = 'p';
218         else if (!strcmp(token, "str"))
219             odp->u.func.arg_types[i] = 't';
220         else if (!strcmp(token, "wstr"))
221             odp->u.func.arg_types[i] = 'W';
222         else if (!strcmp(token, "segstr"))
223             odp->u.func.arg_types[i] = 'T';
224         else if (!strcmp(token, "double"))
225         {
226             odp->u.func.arg_types[i++] = 'l';
227             if (i < sizeof(odp->u.func.arg_types)) odp->u.func.arg_types[i] = 'l';
228         }
229         else fatal_error( "Unknown variable type '%s'\n", token );
230
231         if (SpecType == SPEC_WIN32)
232         {
233             if (strcmp(token, "long") &&
234                 strcmp(token, "ptr") &&
235                 strcmp(token, "str") &&
236                 strcmp(token, "wstr") &&
237                 strcmp(token, "double"))
238             {
239                 fatal_error( "Type '%s' not supported for Win32\n", token );
240             }
241         }
242     }
243     if ((*token != ')') || (i >= sizeof(odp->u.func.arg_types)))
244         fatal_error( "Too many arguments\n" );
245
246     odp->u.func.arg_types[i] = '\0';
247     if (odp->type == TYPE_VARARGS)
248         odp->flags |= FLAG_NORELAY;  /* no relay debug possible for varags entry point */
249     odp->link_name = xstrdup( GetToken(0) );
250     if (strchr( odp->link_name, '.' ))
251     {
252         if (SpecType == SPEC_WIN16) fatal_error( "Forwarded functions not supported for Win16\n" );
253         odp->flags |= FLAG_FORWARD;
254     }
255 }
256
257
258 /*******************************************************************
259  *         ParseEquate
260  *
261  * Parse an 'equate' definition.
262  */
263 static void ParseEquate( ORDDEF *odp )
264 {
265     char *endptr;
266
267     const char *token = GetToken(0);
268     int value = strtol(token, &endptr, 0);
269     if (endptr == NULL || *endptr != '\0')
270         fatal_error( "Expected number value, got '%s'\n", token );
271     if (SpecType == SPEC_WIN32)
272         fatal_error( "'equate' not supported for Win32\n" );
273     odp->u.abs.value = value;
274 }
275
276
277 /*******************************************************************
278  *         ParseStub
279  *
280  * Parse a 'stub' definition.
281  */
282 static void ParseStub( ORDDEF *odp )
283 {
284     odp->u.func.arg_types[0] = '\0';
285     odp->link_name = xstrdup("");
286 }
287
288
289 /*******************************************************************
290  *         ParseExtern
291  *
292  * Parse an 'extern' definition.
293  */
294 static void ParseExtern( ORDDEF *odp )
295 {
296     if (SpecType == SPEC_WIN16)
297         fatal_error( "'extern' not supported for Win16, use 'variable' instead\n" );
298     odp->link_name = xstrdup( GetToken(0) );
299     if (strchr( odp->link_name, '.' )) odp->flags |= FLAG_FORWARD;
300 }
301
302
303 /*******************************************************************
304  *         ParseFlags
305  *
306  * Parse the optional flags for an entry point
307  */
308 static const char *ParseFlags( ORDDEF *odp )
309 {
310     unsigned int i;
311     const char *token;
312
313     do
314     {
315         token = GetToken(0);
316         for (i = 0; FlagNames[i]; i++)
317             if (!strcmp( FlagNames[i], token )) break;
318         if (!FlagNames[i]) fatal_error( "Unknown flag '%s'\n", token );
319         odp->flags |= 1 << i;
320         token = GetToken(0);
321     } while (*token == '-');
322
323     return token;
324 }
325
326 /*******************************************************************
327  *         fix_export_name
328  *
329  * Fix an exported function name by removing a possible @xx suffix
330  */
331 static void fix_export_name( char *name )
332 {
333     char *p, *end = strrchr( name, '@' );
334     if (!end || !end[1] || end == name) return;
335     /* make sure all the rest is digits */
336     for (p = end + 1; *p; p++) if (!isdigit(*p)) return;
337     *end = 0;
338 }
339
340 /*******************************************************************
341  *         ParseOrdinal
342  *
343  * Parse an ordinal definition.
344  */
345 static void ParseOrdinal(int ordinal)
346 {
347     const char *token;
348
349     ORDDEF *odp = xmalloc( sizeof(*odp) );
350     memset( odp, 0, sizeof(*odp) );
351     EntryPoints[nb_entry_points++] = odp;
352
353     token = GetToken(0);
354
355     for (odp->type = 0; odp->type < TYPE_NBTYPES; odp->type++)
356         if (TypeNames[odp->type] && !strcmp( token, TypeNames[odp->type] ))
357             break;
358
359     if (odp->type >= TYPE_NBTYPES)
360         fatal_error( "Expected type after ordinal, found '%s' instead\n", token );
361
362     token = GetToken(0);
363     if (*token == '-') token = ParseFlags( odp );
364
365     odp->name = xstrdup( token );
366     fix_export_name( odp->name );
367     odp->lineno = current_line;
368     odp->ordinal = ordinal;
369
370     switch(odp->type)
371     {
372     case TYPE_VARIABLE:
373         ParseVariable( odp );
374         break;
375     case TYPE_PASCAL_16:
376     case TYPE_PASCAL:
377     case TYPE_STDCALL:
378     case TYPE_VARARGS:
379     case TYPE_CDECL:
380         ParseExportFunction( odp );
381         break;
382     case TYPE_ABS:
383         ParseEquate( odp );
384         break;
385     case TYPE_STUB:
386         ParseStub( odp );
387         break;
388     case TYPE_EXTERN:
389         ParseExtern( odp );
390         break;
391     default:
392         assert( 0 );
393     }
394
395 #ifndef __i386__
396     if (odp->flags & FLAG_I386)
397     {
398         /* ignore this entry point on non-Intel archs */
399         EntryPoints[--nb_entry_points] = NULL;
400         free( odp );
401         return;
402     }
403 #endif
404
405     if (ordinal != -1)
406     {
407         if (!ordinal) fatal_error( "Ordinal 0 is not valid\n" );
408         if (ordinal >= MAX_ORDINALS) fatal_error( "Ordinal number %d too large\n", ordinal );
409         if (ordinal > Limit) Limit = ordinal;
410         if (ordinal < Base) Base = ordinal;
411         odp->ordinal = ordinal;
412         Ordinals[ordinal] = odp;
413     }
414
415     if (!strcmp( odp->name, "@" ) || odp->flags & FLAG_NONAME)
416     {
417         if (ordinal == -1)
418             fatal_error( "Nameless function needs an explicit ordinal number\n" );
419         if (SpecType != SPEC_WIN32)
420             fatal_error( "Nameless functions not supported for Win16\n" );
421         if (!strcmp( odp->name, "@" )) free( odp->name );
422         else odp->export_name = odp->name;
423         odp->name = NULL;
424     }
425     else Names[nb_names++] = odp;
426 }
427
428
429 static int name_compare( const void *name1, const void *name2 )
430 {
431     ORDDEF *odp1 = *(ORDDEF **)name1;
432     ORDDEF *odp2 = *(ORDDEF **)name2;
433     return strcmp( odp1->name, odp2->name );
434 }
435
436 /*******************************************************************
437  *         sort_names
438  *
439  * Sort the name array and catch duplicates.
440  */
441 static void sort_names(void)
442 {
443     int i;
444
445     if (!nb_names) return;
446
447     /* sort the list of names */
448     qsort( Names, nb_names, sizeof(Names[0]), name_compare );
449
450     /* check for duplicate names */
451     for (i = 0; i < nb_names - 1; i++)
452     {
453         if (!strcmp( Names[i]->name, Names[i+1]->name ))
454         {
455             current_line = max( Names[i]->lineno, Names[i+1]->lineno );
456             fatal_error( "'%s' redefined\n%s:%d: First defined here\n",
457                          Names[i]->name, input_file_name,
458                          min( Names[i]->lineno, Names[i+1]->lineno ) );
459         }
460     }
461 }
462
463
464 /*******************************************************************
465  *         ParseTopLevel
466  *
467  * Parse a spec file.
468  */
469 void ParseTopLevel( FILE *file )
470 {
471     const char *token;
472
473     input_file = file;
474     current_line = 0;
475
476     while ((token = GetToken(1)) != NULL)
477     {
478         if (strcmp(token, "@") == 0)
479         {
480             if (SpecType != SPEC_WIN32)
481                 fatal_error( "'@' ordinals not supported for Win16\n" );
482             ParseOrdinal( -1 );
483         }
484         else if (IsNumberString(token))
485         {
486             ParseOrdinal( atoi(token) );
487         }
488         else
489             fatal_error( "Expected ordinal declaration\n" );
490     }
491
492     current_line = 0;  /* no longer parsing the input file */
493     sort_names();
494 }
495
496
497 /*******************************************************************
498  *         add_debug_channel
499  */
500 static void add_debug_channel( const char *name )
501 {
502     int i;
503
504     for (i = 0; i < nb_debug_channels; i++)
505         if (!strcmp( debug_channels[i], name )) return;
506
507     debug_channels = xrealloc( debug_channels, (nb_debug_channels + 1) * sizeof(*debug_channels));
508     debug_channels[nb_debug_channels++] = xstrdup(name);
509 }
510
511
512 /*******************************************************************
513  *         parse_debug_channels
514  *
515  * Parse a source file and extract the debug channel definitions.
516  */
517 void parse_debug_channels( const char *srcdir, const char *filename )
518 {
519     FILE *file;
520     int eol_seen = 1;
521
522     file = open_input_file( srcdir, filename );
523     while (fgets( ParseBuffer, sizeof(ParseBuffer), file ))
524     {
525         char *channel, *end, *p = ParseBuffer;
526
527         p = ParseBuffer + strlen(ParseBuffer) - 1;
528         if (!eol_seen)  /* continuation line */
529         {
530             eol_seen = (*p == '\n');
531             continue;
532         }
533         if ((eol_seen = (*p == '\n'))) *p = 0;
534
535         p = ParseBuffer;
536         while (isspace(*p)) p++;
537         if (!memcmp( p, "WINE_DECLARE_DEBUG_CHANNEL", 26 ) ||
538             !memcmp( p, "WINE_DEFAULT_DEBUG_CHANNEL", 26 ))
539         {
540             p += 26;
541             while (isspace(*p)) p++;
542             if (*p != '(')
543                 fatal_error( "invalid debug channel specification '%s'\n", ParseBuffer );
544             p++;
545             while (isspace(*p)) p++;
546             if (!isalpha(*p))
547                 fatal_error( "invalid debug channel specification '%s'\n", ParseBuffer );
548             channel = p;
549             while (isalnum(*p) || *p == '_') p++;
550             end = p;
551             while (isspace(*p)) p++;
552             if (*p != ')')
553                 fatal_error( "invalid debug channel specification '%s'\n", ParseBuffer );
554             *end = 0;
555             add_debug_channel( channel );
556         }
557         current_line++;
558     }
559     close_input_file( file );
560 }