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
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.
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.
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
26 #include "wine/port.h"
41 static char ParseBuffer[512];
42 static char TokenBuffer[512];
43 static char *ParseNext = ParseBuffer;
44 static FILE *input_file;
46 static const char *separator_chars;
47 static const char *comment_chars;
49 static const char * const TypeNames[TYPE_NBTYPES] =
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 */
61 static const char * const FlagNames[] =
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 "interrupt", /* FLAG_INTERRUPT */
70 "private", /* FLAG_PRIVATE */
74 static int IsNumberString(const char *s)
76 while (*s) if (!isdigit(*s++)) return 0;
80 inline static int is_token_separator( char ch )
82 return strchr( separator_chars, ch ) != NULL;
85 inline static int is_token_comment( char ch )
87 return strchr( comment_chars, ch ) != NULL;
90 /* get the next line from the input file, or return 0 if at eof */
91 static int get_next_line(void)
93 ParseNext = ParseBuffer;
95 return (fgets(ParseBuffer, sizeof(ParseBuffer), input_file) != NULL);
98 static const char * GetToken( int allow_eol )
101 char *token = TokenBuffer;
105 /* remove initial white space */
107 while (isspace(*p)) p++;
109 if (*p == '\\' && p[1] == '\n') /* line continuation */
111 if (!get_next_line())
113 if (!allow_eol) error( "Unexpected end of file\n" );
120 if ((*p == '\0') || is_token_comment(*p))
122 if (!allow_eol) error( "Declaration not terminated properly\n" );
129 if (is_token_separator(*p))
131 /* a separator is always a complete token */
134 else while (*p != '\0' && !is_token_separator(*p) && !isspace(*p))
137 if (*p) *token++ = *p++;
145 static ORDDEF *add_entry_point( DLLSPEC *spec )
147 if (spec->nb_entry_points == spec->alloc_entry_points)
149 spec->alloc_entry_points += 128;
150 spec->entry_points = xrealloc( spec->entry_points,
151 spec->alloc_entry_points * sizeof(*spec->entry_points) );
153 return &spec->entry_points[spec->nb_entry_points++];
156 /*******************************************************************
157 * parse_spec_variable
159 * Parse a variable definition in a .spec file.
161 static int parse_spec_variable( ORDDEF *odp, DLLSPEC *spec )
166 int value_array_size;
169 if (spec->type == SPEC_WIN32)
171 error( "'variable' not supported in Win32, use 'extern' instead\n" );
175 if (!(token = GetToken(0))) return 0;
178 error( "Expected '(' got '%s'\n", token );
183 value_array_size = 25;
184 value_array = xmalloc(sizeof(*value_array) * value_array_size);
188 if (!(token = GetToken(0)))
196 value_array[n_values++] = strtol(token, &endptr, 0);
197 if (n_values == value_array_size)
199 value_array_size += 25;
200 value_array = xrealloc(value_array,
201 sizeof(*value_array) * value_array_size);
204 if (endptr == NULL || *endptr != '\0')
206 error( "Expected number value, got '%s'\n", token );
212 odp->u.var.n_values = n_values;
213 odp->u.var.values = xrealloc(value_array, sizeof(*value_array) * n_values);
218 /*******************************************************************
221 * Parse an exported function definition in a .spec file.
223 static int parse_spec_export( ORDDEF *odp, DLLSPEC *spec )
231 if (odp->type == TYPE_STDCALL)
233 error( "'stdcall' not supported for Win16\n" );
238 if (odp->type == TYPE_PASCAL)
240 error( "'pascal' not supported for Win32\n" );
243 if (odp->flags & FLAG_INTERRUPT)
245 error( "'interrupt' not supported for Win32\n" );
253 if (!(token = GetToken(0))) return 0;
256 error( "Expected '(' got '%s'\n", token );
260 for (i = 0; i < sizeof(odp->u.func.arg_types); i++)
262 if (!(token = GetToken(0))) return 0;
266 if (!strcmp(token, "word"))
267 odp->u.func.arg_types[i] = 'w';
268 else if (!strcmp(token, "s_word"))
269 odp->u.func.arg_types[i] = 's';
270 else if (!strcmp(token, "long") || !strcmp(token, "segptr"))
271 odp->u.func.arg_types[i] = 'l';
272 else if (!strcmp(token, "ptr"))
273 odp->u.func.arg_types[i] = 'p';
274 else if (!strcmp(token, "str"))
275 odp->u.func.arg_types[i] = 't';
276 else if (!strcmp(token, "wstr"))
277 odp->u.func.arg_types[i] = 'W';
278 else if (!strcmp(token, "segstr"))
279 odp->u.func.arg_types[i] = 'T';
280 else if (!strcmp(token, "double"))
282 odp->u.func.arg_types[i++] = 'l';
283 if (i < sizeof(odp->u.func.arg_types)) odp->u.func.arg_types[i] = 'l';
287 error( "Unknown argument type '%s'\n", token );
291 if (spec->type == SPEC_WIN32)
293 if (strcmp(token, "long") &&
294 strcmp(token, "ptr") &&
295 strcmp(token, "str") &&
296 strcmp(token, "wstr") &&
297 strcmp(token, "double"))
299 error( "Type '%s' not supported for Win32\n", token );
304 if ((*token != ')') || (i >= sizeof(odp->u.func.arg_types)))
306 error( "Too many arguments\n" );
310 odp->u.func.arg_types[i] = '\0';
311 if (odp->type == TYPE_VARARGS)
312 odp->flags |= FLAG_NORELAY; /* no relay debug possible for varags entry point */
314 if (!(token = GetToken(1)))
316 if (!strcmp( odp->name, "@" ))
318 error( "Missing handler name for anonymous function\n" );
321 odp->link_name = xstrdup( odp->name );
325 odp->link_name = xstrdup( token );
326 if (strchr( odp->link_name, '.' ))
328 if (spec->type == SPEC_WIN16)
330 error( "Forwarded functions not supported for Win16\n" );
333 odp->flags |= FLAG_FORWARD;
340 /*******************************************************************
343 * Parse an 'equate' definition in a .spec file.
345 static int parse_spec_equate( ORDDEF *odp, DLLSPEC *spec )
351 if (spec->type == SPEC_WIN32)
353 error( "'equate' not supported for Win32\n" );
356 if (!(token = GetToken(0))) return 0;
357 value = strtol(token, &endptr, 0);
358 if (endptr == NULL || *endptr != '\0')
360 error( "Expected number value, got '%s'\n", token );
363 odp->u.abs.value = value;
368 /*******************************************************************
371 * Parse a 'stub' definition in a .spec file
373 static int parse_spec_stub( ORDDEF *odp, DLLSPEC *spec )
375 odp->u.func.arg_types[0] = '\0';
376 odp->link_name = xstrdup("");
381 /*******************************************************************
384 * Parse an 'extern' definition in a .spec file.
386 static int parse_spec_extern( ORDDEF *odp, DLLSPEC *spec )
390 if (spec->type == SPEC_WIN16)
392 error( "'extern' not supported for Win16, use 'variable' instead\n" );
395 if (!(token = GetToken(1)))
397 if (!strcmp( odp->name, "@" ))
399 error( "Missing handler name for anonymous extern\n" );
402 odp->link_name = xstrdup( odp->name );
406 odp->link_name = xstrdup( token );
407 if (strchr( odp->link_name, '.' )) odp->flags |= FLAG_FORWARD;
413 /*******************************************************************
416 * Parse the optional flags for an entry point in a .spec file.
418 static const char *parse_spec_flags( ORDDEF *odp )
425 if (!(token = GetToken(0))) break;
426 for (i = 0; FlagNames[i]; i++)
427 if (!strcmp( FlagNames[i], token )) break;
430 error( "Unknown flag '%s'\n", token );
433 odp->flags |= 1 << i;
435 } while (token && *token == '-');
441 /*******************************************************************
444 * Parse an ordinal definition in a .spec file.
446 static int parse_spec_ordinal( int ordinal, DLLSPEC *spec )
450 ORDDEF *odp = add_entry_point( spec );
451 memset( odp, 0, sizeof(*odp) );
453 if (!(token = GetToken(0))) goto error;
455 for (odp->type = 0; odp->type < TYPE_NBTYPES; odp->type++)
456 if (TypeNames[odp->type] && !strcmp( token, TypeNames[odp->type] ))
459 if (odp->type >= TYPE_NBTYPES)
461 error( "Expected type after ordinal, found '%s' instead\n", token );
465 if (!(token = GetToken(0))) goto error;
466 if (*token == '-' && !(token = parse_spec_flags( odp ))) goto error;
468 odp->name = xstrdup( token );
469 remove_stdcall_decoration( odp->name );
470 odp->lineno = current_line;
471 odp->ordinal = ordinal;
476 if (!parse_spec_variable( odp, spec )) goto error;
482 if (!parse_spec_export( odp, spec )) goto error;
485 if (!parse_spec_equate( odp, spec )) goto error;
488 if (!parse_spec_stub( odp, spec )) goto error;
491 if (!parse_spec_extern( odp, spec )) goto error;
498 if (odp->flags & FLAG_I386)
500 /* ignore this entry point on non-Intel archs */
501 spec->nb_entry_points--;
510 error( "Ordinal 0 is not valid\n" );
513 if (ordinal >= MAX_ORDINALS)
515 error( "Ordinal number %d too large\n", ordinal );
518 if (ordinal > spec->limit) spec->limit = ordinal;
519 if (ordinal < spec->base) spec->base = ordinal;
520 odp->ordinal = ordinal;
523 if (!strcmp( odp->name, "@" ) || odp->flags & FLAG_NONAME)
527 error( "Nameless function needs an explicit ordinal number\n" );
530 if (spec->type != SPEC_WIN32)
532 error( "Nameless functions not supported for Win16\n" );
535 if (!strcmp( odp->name, "@" )) free( odp->name );
536 else odp->export_name = odp->name;
542 spec->nb_entry_points--;
548 static int name_compare( const void *name1, const void *name2 )
550 ORDDEF *odp1 = *(ORDDEF **)name1;
551 ORDDEF *odp2 = *(ORDDEF **)name2;
552 return strcmp( odp1->name, odp2->name );
555 /*******************************************************************
558 * Build the name array and catch duplicates.
560 static void assign_names( DLLSPEC *spec )
565 for (i = 0; i < spec->nb_entry_points; i++)
566 if (spec->entry_points[i].name) spec->nb_names++;
567 if (!spec->nb_names) return;
569 spec->names = xmalloc( spec->nb_names * sizeof(spec->names[0]) );
570 for (i = j = 0; i < spec->nb_entry_points; i++)
571 if (spec->entry_points[i].name) spec->names[j++] = &spec->entry_points[i];
573 /* sort the list of names */
574 qsort( spec->names, spec->nb_names, sizeof(spec->names[0]), name_compare );
576 /* check for duplicate names */
577 for (i = 0; i < spec->nb_names - 1; i++)
579 if (!strcmp( spec->names[i]->name, spec->names[i+1]->name ))
581 current_line = max( spec->names[i]->lineno, spec->names[i+1]->lineno );
582 error( "'%s' redefined\n%s:%d: First defined here\n",
583 spec->names[i]->name, input_file_name,
584 min( spec->names[i]->lineno, spec->names[i+1]->lineno ) );
589 /*******************************************************************
592 * Build the ordinal array.
594 static void assign_ordinals( DLLSPEC *spec )
596 int i, count, ordinal;
598 /* start assigning from base, or from 1 if no ordinal defined yet */
599 if (spec->base == MAX_ORDINALS) spec->base = 1;
600 if (spec->limit < spec->base) spec->limit = spec->base;
602 count = max( spec->limit + 1, spec->base + spec->nb_entry_points );
603 spec->ordinals = xmalloc( count * sizeof(spec->ordinals[0]) );
604 memset( spec->ordinals, 0, count * sizeof(spec->ordinals[0]) );
606 /* fill in all explicitly specified ordinals */
607 for (i = 0; i < spec->nb_entry_points; i++)
609 ordinal = spec->entry_points[i].ordinal;
610 if (ordinal == -1) continue;
611 if (spec->ordinals[ordinal])
613 current_line = max( spec->entry_points[i].lineno, spec->ordinals[ordinal]->lineno );
614 error( "ordinal %d redefined\n%s:%d: First defined here\n",
615 ordinal, input_file_name,
616 min( spec->entry_points[i].lineno, spec->ordinals[ordinal]->lineno ) );
618 else spec->ordinals[ordinal] = &spec->entry_points[i];
621 /* now assign ordinals to the rest */
622 for (i = 0, ordinal = spec->base; i < spec->nb_names; i++)
624 if (spec->names[i]->ordinal != -1) continue; /* already has an ordinal */
625 while (spec->ordinals[ordinal]) ordinal++;
626 if (ordinal >= MAX_ORDINALS)
628 current_line = spec->names[i]->lineno;
629 fatal_error( "Too many functions defined (max %d)\n", MAX_ORDINALS );
631 spec->names[i]->ordinal = ordinal;
632 spec->ordinals[ordinal] = spec->names[i];
634 if (ordinal > spec->limit) spec->limit = ordinal;
638 /*******************************************************************
641 * Parse a .spec file.
643 int parse_spec_file( FILE *file, DLLSPEC *spec )
650 comment_chars = "#;";
651 separator_chars = "()-";
653 while (get_next_line())
655 if (!(token = GetToken(1))) continue;
656 if (strcmp(token, "@") == 0)
658 if (spec->type != SPEC_WIN32)
660 error( "'@' ordinals not supported for Win16\n" );
663 if (!parse_spec_ordinal( -1, spec )) continue;
665 else if (IsNumberString(token))
667 if (!parse_spec_ordinal( atoi(token), spec )) continue;
671 error( "Expected ordinal declaration, got '%s'\n", token );
674 if ((token = GetToken(1))) error( "Syntax error near '%s'\n", token );
677 current_line = 0; /* no longer parsing the input file */
678 assign_names( spec );
679 assign_ordinals( spec );
684 /*******************************************************************
687 * Parse a LIBRARY declaration in a .def file.
689 static int parse_def_library( DLLSPEC *spec )
691 const char *token = GetToken(1);
693 if (!token) return 1;
694 if (strcmp( token, "BASE" ))
696 free( spec->file_name );
697 spec->file_name = xstrdup( token );
698 if (!(token = GetToken(1))) return 1;
700 if (strcmp( token, "BASE" ))
702 error( "Expected library name or BASE= declaration, got '%s'\n", token );
705 if (!(token = GetToken(0))) return 0;
706 if (strcmp( token, "=" ))
708 error( "Expected '=' after BASE, got '%s'\n", token );
711 if (!(token = GetToken(0))) return 0;
712 /* FIXME: do something with base address */
718 /*******************************************************************
719 * parse_def_stack_heap_size
721 * Parse a STACKSIZE or HEAPSIZE declaration in a .def file.
723 static int parse_def_stack_heap_size( int is_stack, DLLSPEC *spec )
725 const char *token = GetToken(0);
729 if (!token) return 0;
730 size = strtoul( token, &end, 0 );
733 error( "Invalid number '%s'\n", token );
736 if (is_stack) spec->stack_size = size / 1024;
737 else spec->heap_size = size / 1024;
738 if (!(token = GetToken(1))) return 1;
739 if (strcmp( token, "," ))
741 error( "Expected ',' after size, got '%s'\n", token );
744 if (!(token = GetToken(0))) return 0;
745 /* FIXME: do something with reserve size */
750 /*******************************************************************
753 * Parse an export declaration in a .def file.
755 static int parse_def_export( char *name, DLLSPEC *spec )
758 const char *token = GetToken(1);
760 ORDDEF *odp = add_entry_point( spec );
761 memset( odp, 0, sizeof(*odp) );
763 odp->lineno = current_line;
766 args = remove_stdcall_decoration( odp->name );
767 if (args == -1) odp->type = TYPE_CDECL;
770 odp->type = TYPE_STDCALL;
772 if (args >= sizeof(odp->u.func.arg_types))
774 error( "Too many arguments in stdcall function '%s'\n", odp->name );
777 for (i = 0; i < args; i++) odp->u.func.arg_types[i] = 'l';
780 /* check for optional internal name */
782 if (token && !strcmp( token, "=" ))
784 if (!(token = GetToken(0))) goto error;
785 odp->link_name = xstrdup( token );
786 remove_stdcall_decoration( odp->link_name );
790 /* check for optional ordinal */
792 if (token && token[0] == '@')
796 if (!IsNumberString( token+1 ))
798 error( "Expected number after '@', got '%s'\n", token+1 );
801 ordinal = atoi( token+1 );
804 error( "Ordinal 0 is not valid\n" );
807 if (ordinal >= MAX_ORDINALS)
809 error( "Ordinal number %d too large\n", ordinal );
812 if (ordinal > spec->limit) spec->limit = ordinal;
813 if (ordinal < spec->base) spec->base = ordinal;
814 odp->ordinal = ordinal;
818 /* check for other optional keywords */
820 if (token && !strcmp( token, "NONAME" ))
822 if (odp->ordinal == -1)
824 error( "NONAME requires an ordinal\n" );
827 odp->export_name = odp->name;
829 odp->flags |= FLAG_NONAME;
832 if (token && !strcmp( token, "PRIVATE" ))
834 odp->flags |= FLAG_PRIVATE;
837 if (token && !strcmp( token, "DATA" ))
839 odp->type = TYPE_EXTERN;
844 error( "Garbage text '%s' found at end of export declaration\n", token );
850 spec->nb_entry_points--;
856 /*******************************************************************
861 int parse_def_file( FILE *file, DLLSPEC *spec )
870 separator_chars = ",=";
872 while (get_next_line())
874 if (!(token = GetToken(1))) continue;
876 if (!strcmp( token, "LIBRARY" ) || !strcmp( token, "NAME" ))
878 if (!parse_def_library( spec )) continue;
881 else if (!strcmp( token, "STACKSIZE" ))
883 if (!parse_def_stack_heap_size( 1, spec )) continue;
886 else if (!strcmp( token, "HEAPSIZE" ))
888 if (!parse_def_stack_heap_size( 0, spec )) continue;
891 else if (!strcmp( token, "EXPORTS" ))
894 if (!(token = GetToken(1))) continue;
896 else if (!strcmp( token, "IMPORTS" ))
899 if (!(token = GetToken(1))) continue;
901 else if (!strcmp( token, "SECTIONS" ))
904 if (!(token = GetToken(1))) continue;
907 if (!in_exports) continue; /* ignore this line */
908 if (!parse_def_export( xstrdup(token), spec )) continue;
911 if ((token = GetToken(1))) error( "Syntax error near '%s'\n", token );
914 current_line = 0; /* no longer parsing the input file */
915 assign_names( spec );
916 assign_ordinals( spec );
921 /*******************************************************************
924 static void add_debug_channel( const char *name )
928 for (i = 0; i < nb_debug_channels; i++)
929 if (!strcmp( debug_channels[i], name )) return;
931 debug_channels = xrealloc( debug_channels, (nb_debug_channels + 1) * sizeof(*debug_channels));
932 debug_channels[nb_debug_channels++] = xstrdup(name);
936 /*******************************************************************
937 * parse_debug_channels
939 * Parse a source file and extract the debug channel definitions.
941 int parse_debug_channels( const char *srcdir, const char *filename )
946 file = open_input_file( srcdir, filename );
947 while (fgets( ParseBuffer, sizeof(ParseBuffer), file ))
949 char *channel, *end, *p = ParseBuffer;
951 p = ParseBuffer + strlen(ParseBuffer) - 1;
952 if (!eol_seen) /* continuation line */
954 eol_seen = (*p == '\n');
957 if ((eol_seen = (*p == '\n'))) *p = 0;
960 while (isspace(*p)) p++;
961 if (!memcmp( p, "WINE_DECLARE_DEBUG_CHANNEL", 26 ) ||
962 !memcmp( p, "WINE_DEFAULT_DEBUG_CHANNEL", 26 ))
965 while (isspace(*p)) p++;
968 error( "invalid debug channel specification '%s'\n", ParseBuffer );
972 while (isspace(*p)) p++;
975 error( "invalid debug channel specification '%s'\n", ParseBuffer );
979 while (isalnum(*p) || *p == '_') p++;
981 while (isspace(*p)) p++;
984 error( "invalid debug channel specification '%s'\n", ParseBuffer );
988 add_debug_channel( channel );
993 close_input_file( file );