dinput: [joystick_linux] Copy and modify default data format.
[wine/dibdrv.git] / tools / makedep.c
blob1d070b2218799833fdba12cef88aa00e34386c1b
1 /*
2 * Generate include file dependencies
4 * Copyright 1996 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #define NO_LIBWINE_PORT
23 #include "wine/port.h"
25 #include <assert.h>
26 #include <ctype.h>
27 #include <errno.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <stdarg.h>
31 #include <string.h>
32 #ifdef HAVE_UNISTD_H
33 # include <unistd.h>
34 #endif
35 #include "wine/list.h"
37 /* Max first-level includes per file */
38 #define MAX_INCLUDES 200
40 typedef struct _INCL_FILE
42 struct list entry;
43 char *name;
44 char *filename;
45 char *sourcename; /* source file name for generated headers */
46 struct _INCL_FILE *included_by; /* file that included this one */
47 int included_line; /* line where this file was included */
48 int system; /* is it a system include (#include <name>) */
49 struct _INCL_FILE *owner;
50 struct _INCL_FILE *files[MAX_INCLUDES];
51 } INCL_FILE;
53 static struct list sources = LIST_INIT(sources);
54 static struct list includes = LIST_INIT(includes);
56 typedef struct _INCL_PATH
58 struct list entry;
59 const char *name;
60 } INCL_PATH;
62 static struct list paths = LIST_INIT(paths);
64 static const char *src_dir;
65 static const char *top_src_dir;
66 static const char *top_obj_dir;
67 static const char *OutputFileName = "Makefile";
68 static const char *Separator = "### Dependencies";
69 static const char *ProgramName;
70 static int input_line;
72 static const char Usage[] =
73 "Usage: %s [options] [files]\n"
74 "Options:\n"
75 " -Idir Search for include files in directory 'dir'\n"
76 " -Cdir Search for source files in directory 'dir'\n"
77 " -Sdir Set the top source directory\n"
78 " -Sdir Set the top object directory\n"
79 " -fxxx Store output in file 'xxx' (default: Makefile)\n"
80 " -sxxx Use 'xxx' as separator (default: \"### Dependencies\")\n";
83 /*******************************************************************
84 * fatal_error
86 static void fatal_error( const char *msg, ... )
88 va_list valist;
89 va_start( valist, msg );
90 vfprintf( stderr, msg, valist );
91 va_end( valist );
92 exit(1);
96 /*******************************************************************
97 * xmalloc
99 static void *xmalloc( size_t size )
101 void *res;
102 if (!(res = malloc (size ? size : 1)))
103 fatal_error( "%s: Virtual memory exhausted.\n", ProgramName );
104 return res;
108 /*******************************************************************
109 * xrealloc
111 static void *xrealloc (void *ptr, size_t size)
113 void *res;
114 assert( size );
115 if (!(res = realloc( ptr, size )))
116 fatal_error( "%s: Virtual memory exhausted.\n", ProgramName );
117 return res;
120 /*******************************************************************
121 * xstrdup
123 static char *xstrdup( const char *str )
125 char *res = strdup( str );
126 if (!res) fatal_error( "%s: Virtual memory exhausted.\n", ProgramName );
127 return res;
131 /*******************************************************************
132 * strmake
134 static char *strmake( const char* fmt, ... )
136 int n;
137 size_t size = 100;
138 va_list ap;
140 for (;;)
142 char *p = xmalloc (size);
143 va_start(ap, fmt);
144 n = vsnprintf (p, size, fmt, ap);
145 va_end(ap);
146 if (n == -1) size *= 2;
147 else if ((size_t)n >= size) size = n + 1;
148 else return p;
149 free(p);
154 /*******************************************************************
155 * strendswith
157 static int strendswith( const char* str, const char* end )
159 int l = strlen(str);
160 int m = strlen(end);
162 return l >= m && strcmp(str + l - m, end) == 0;
165 /*******************************************************************
166 * get_extension
168 static char *get_extension( char *filename )
170 char *ext = strrchr( filename, '.' );
171 if (ext && strchr( ext, '/' )) ext = NULL;
172 return ext;
176 /*******************************************************************
177 * get_line
179 static char *get_line( FILE *file )
181 static char *buffer;
182 static unsigned int size;
184 if (!size)
186 size = 1024;
187 buffer = xmalloc( size );
189 if (!fgets( buffer, size, file )) return NULL;
190 input_line++;
192 for (;;)
194 char *p = buffer + strlen(buffer);
195 /* if line is larger than buffer, resize buffer */
196 while (p == buffer + size - 1 && p[-1] != '\n')
198 buffer = xrealloc( buffer, size * 2 );
199 fgets( buffer + size - 1, size + 1, file );
200 p = buffer + strlen(buffer);
201 size *= 2;
203 if (p > buffer && p[-1] == '\n')
205 *(--p) = 0;
206 if (p > buffer && p[-1] == '\r') *(--p) = 0;
207 if (p > buffer && p[-1] == '\\')
209 *(--p) = 0;
210 /* line ends in backslash, read continuation line */
211 fgets( p, size - (p - buffer), file );
212 input_line++;
213 continue;
216 return buffer;
220 /*******************************************************************
221 * add_include_path
223 * Add a directory to the include path.
225 static void add_include_path( const char *name )
227 INCL_PATH *path = xmalloc( sizeof(*path) );
228 list_add_tail( &paths, &path->entry );
229 path->name = name;
232 /*******************************************************************
233 * find_src_file
235 static INCL_FILE *find_src_file( const char *name )
237 INCL_FILE *file;
239 LIST_FOR_EACH_ENTRY( file, &sources, INCL_FILE, entry )
240 if (!strcmp( name, file->name )) return file;
241 return NULL;
244 /*******************************************************************
245 * add_src_file
247 * Add a source file to the list.
249 static INCL_FILE *add_src_file( const char *name )
251 INCL_FILE *file;
253 if (find_src_file( name )) return NULL; /* we already have it */
254 file = xmalloc( sizeof(*file) );
255 memset( file, 0, sizeof(*file) );
256 file->name = xstrdup(name);
257 list_add_tail( &sources, &file->entry );
258 return file;
262 /*******************************************************************
263 * add_include
265 * Add an include file if it doesn't already exists.
267 static INCL_FILE *add_include( INCL_FILE *pFile, const char *name, int line, int system )
269 INCL_FILE *include;
270 char *ext;
271 int pos;
273 for (pos = 0; pos < MAX_INCLUDES; pos++) if (!pFile->files[pos]) break;
274 if (pos >= MAX_INCLUDES)
275 fatal_error( "%s: %s: too many included files, please fix MAX_INCLUDES\n",
276 ProgramName, pFile->name );
278 /* enforce some rules for the Wine tree */
280 if (!memcmp( name, "../", 3 ))
281 fatal_error( "%s:%d: #include directive with relative path not allowed\n",
282 pFile->filename, line );
284 if (!strcmp( name, "config.h" ))
286 if ((ext = strrchr( pFile->filename, '.' )) && !strcmp( ext, ".h" ))
287 fatal_error( "%s:%d: config.h must not be included by a header file\n",
288 pFile->filename, line );
289 if (pos)
290 fatal_error( "%s:%d: config.h must be included before anything else\n",
291 pFile->filename, line );
293 else if (!strcmp( name, "wine/port.h" ))
295 if ((ext = strrchr( pFile->filename, '.' )) && !strcmp( ext, ".h" ))
296 fatal_error( "%s:%d: wine/port.h must not be included by a header file\n",
297 pFile->filename, line );
298 if (!pos) fatal_error( "%s:%d: config.h must be included before wine/port.h\n",
299 pFile->filename, line );
300 if (pos > 1)
301 fatal_error( "%s:%d: wine/port.h must be included before everything except config.h\n",
302 pFile->filename, line );
303 if (strcmp( pFile->files[0]->name, "config.h" ))
304 fatal_error( "%s:%d: config.h must be included before wine/port.h\n",
305 pFile->filename, line );
308 LIST_FOR_EACH_ENTRY( include, &includes, INCL_FILE, entry )
309 if (!strcmp( name, include->name )) goto found;
311 include = xmalloc( sizeof(INCL_FILE) );
312 memset( include, 0, sizeof(INCL_FILE) );
313 include->name = xstrdup(name);
314 include->included_by = pFile;
315 include->included_line = line;
316 include->system = system;
317 list_add_tail( &includes, &include->entry );
318 found:
319 pFile->files[pos] = include;
320 return include;
324 /*******************************************************************
325 * open_src_file
327 static FILE *open_src_file( INCL_FILE *pFile )
329 FILE *file;
331 /* first try name as is */
332 if ((file = fopen( pFile->name, "r" )))
334 pFile->filename = xstrdup( pFile->name );
335 return file;
337 /* now try in source dir */
338 if (src_dir)
340 pFile->filename = strmake( "%s/%s", src_dir, pFile->name );
341 file = fopen( pFile->filename, "r" );
343 if (!file)
345 perror( pFile->name );
346 exit(1);
348 return file;
352 /*******************************************************************
353 * open_include_file
355 static FILE *open_include_file( INCL_FILE *pFile )
357 FILE *file = NULL;
358 char *filename, *p;
359 INCL_PATH *path;
361 errno = ENOENT;
363 /* check for generated bison header */
365 if (strendswith( pFile->name, ".tab.h" ))
367 if (src_dir)
368 filename = strmake( "%s/%.*s.y", src_dir, strlen(pFile->name) - 6, pFile->name );
369 else
370 filename = strmake( "%.*s.y", strlen(pFile->name) - 6, pFile->name );
372 if ((file = fopen( filename, "r" )))
374 pFile->sourcename = filename;
375 pFile->filename = xstrdup( pFile->name );
376 /* don't bother to parse it */
377 fclose( file );
378 return NULL;
380 free( filename );
383 /* check for generated message resource */
385 if (strendswith( pFile->name, ".mc.rc" ))
387 if (src_dir)
388 filename = strmake( "%s/%s", src_dir, pFile->name );
389 else
390 filename = xstrdup( pFile->name );
392 filename[strlen(filename) - 3] = 0;
394 if ((file = fopen( filename, "r" )))
396 pFile->sourcename = filename;
397 pFile->filename = xstrdup( pFile->name );
398 /* don't bother to parse it */
399 fclose( file );
400 return NULL;
402 free( filename );
405 /* check for corresponding idl file in source dir */
407 if (strendswith( pFile->name, ".h" ))
409 if (src_dir)
410 filename = strmake( "%s/%.*s.idl", src_dir, strlen(pFile->name) - 2, pFile->name );
411 else
412 filename = strmake( "%.*s.idl", strlen(pFile->name) - 2, pFile->name );
414 if ((file = fopen( filename, "r" )))
416 pFile->sourcename = filename;
417 pFile->filename = xstrdup( pFile->name );
418 return file;
420 free( filename );
423 /* first try name as is */
424 if ((file = fopen( pFile->name, "r" )))
426 pFile->filename = xstrdup( pFile->name );
427 return file;
430 /* now try in source dir */
431 if (src_dir)
433 filename = strmake( "%s/%s", src_dir, pFile->name );
434 if ((file = fopen( filename, "r" ))) goto found;
435 free( filename );
438 /* check for corresponding idl file in global includes */
440 if (strendswith( pFile->name, ".h" ))
442 if (top_src_dir)
443 filename = strmake( "%s/include/%.*s.idl",
444 top_src_dir, strlen(pFile->name) - 2, pFile->name );
445 else if (top_obj_dir)
446 filename = strmake( "%s/include/%.*s.idl",
447 top_obj_dir, strlen(pFile->name) - 2, pFile->name );
448 else
449 filename = NULL;
451 if (filename && (file = fopen( filename, "r" )))
453 pFile->sourcename = filename;
454 pFile->filename = strmake( "%s/include/%s", top_obj_dir, pFile->name );
455 return file;
457 free( filename );
460 /* now try in global includes */
461 if (top_obj_dir)
463 filename = strmake( "%s/include/%s", top_obj_dir, pFile->name );
464 if ((file = fopen( filename, "r" ))) goto found;
465 free( filename );
467 if (top_src_dir)
469 filename = strmake( "%s/include/%s", top_src_dir, pFile->name );
470 if ((file = fopen( filename, "r" ))) goto found;
471 free( filename );
474 /* now search in include paths */
475 LIST_FOR_EACH_ENTRY( path, &paths, INCL_PATH, entry )
477 filename = strmake( "%s/%s", path->name, pFile->name );
478 if ((file = fopen( filename, "r" ))) goto found;
479 free( filename );
481 if (pFile->system) return NULL; /* ignore system files we cannot find */
483 /* try in src file directory */
484 if ((p = strrchr(pFile->included_by->filename, '/')))
486 int l = p - pFile->included_by->filename + 1;
487 filename = xmalloc(l + strlen(pFile->name) + 1);
488 memcpy( filename, pFile->included_by->filename, l );
489 strcpy( filename + l, pFile->name );
490 if ((file = fopen( filename, "r" ))) goto found;
491 free( filename );
494 perror( pFile->name );
495 while (pFile->included_by)
497 const char *parent = pFile->included_by->sourcename;
498 if (!parent) parent = pFile->included_by->name;
499 fprintf( stderr, " %s was first included from %s:%d\n",
500 pFile->name, parent, pFile->included_line );
501 pFile = pFile->included_by;
503 exit(1);
505 found:
506 pFile->filename = filename;
507 return file;
511 /*******************************************************************
512 * parse_idl_file
514 * If for_h_file is non-zero, it means we are not interested in the idl file
515 * itself, but only in the contents of the .h file that will be generated from it.
517 static void parse_idl_file( INCL_FILE *pFile, FILE *file, int for_h_file )
519 char *buffer, *include;
521 if (for_h_file)
523 /* generated .h file always includes these */
524 add_include( pFile, "rpc.h", 0, 1 );
525 add_include( pFile, "rpcndr.h", 0, 1 );
528 input_line = 0;
529 while ((buffer = get_line( file )))
531 char quote;
532 char *p = buffer;
533 while (*p && isspace(*p)) p++;
535 if (!strncmp( p, "import", 6 ))
537 p += 6;
538 while (*p && isspace(*p)) p++;
539 if (*p != '"') continue;
540 include = ++p;
541 while (*p && (*p != '"')) p++;
542 if (!*p) fatal_error( "%s:%d: Malformed import directive\n", pFile->filename, input_line );
543 *p = 0;
544 if (for_h_file && strendswith( include, ".idl" )) strcpy( p - 4, ".h" );
545 add_include( pFile, include, input_line, 0 );
546 continue;
549 if (for_h_file) /* only check for #include inside cpp_quote */
551 if (strncmp( p, "cpp_quote", 9 )) continue;
552 p += 9;
553 while (*p && isspace(*p)) p++;
554 if (*p++ != '(') continue;
555 while (*p && isspace(*p)) p++;
556 if (*p++ != '"') continue;
557 if (*p++ != '#') continue;
558 while (*p && isspace(*p)) p++;
559 if (strncmp( p, "include", 7 )) continue;
560 p += 7;
561 while (*p && isspace(*p)) p++;
562 if (*p == '\\' && p[1] == '"')
564 p += 2;
565 quote = '"';
567 else
569 if (*p++ != '<' ) continue;
570 quote = '>';
572 include = p;
573 while (*p && (*p != quote)) p++;
574 if (!*p || (quote == '"' && p[-1] != '\\'))
575 fatal_error( "%s:%d: Malformed #include directive inside cpp_quote\n",
576 pFile->filename, input_line );
577 if (quote == '"') p--; /* remove backslash */
578 *p = 0;
579 add_include( pFile, include, input_line, (quote == '>') );
580 continue;
583 /* check for normal #include */
584 if (*p++ != '#') continue;
585 while (*p && isspace(*p)) p++;
586 if (strncmp( p, "include", 7 )) continue;
587 p += 7;
588 while (*p && isspace(*p)) p++;
589 if (*p != '\"' && *p != '<' ) continue;
590 quote = *p++;
591 if (quote == '<') quote = '>';
592 include = p;
593 while (*p && (*p != quote)) p++;
594 if (!*p) fatal_error( "%s:%d: Malformed #include directive\n", pFile->filename, input_line );
595 *p = 0;
596 add_include( pFile, include, input_line, (quote == '>') );
600 /*******************************************************************
601 * parse_c_file
603 static void parse_c_file( INCL_FILE *pFile, FILE *file )
605 char *buffer, *include;
607 input_line = 0;
608 while ((buffer = get_line( file )))
610 char quote;
611 char *p = buffer;
612 while (*p && isspace(*p)) p++;
613 if (*p++ != '#') continue;
614 while (*p && isspace(*p)) p++;
615 if (strncmp( p, "include", 7 )) continue;
616 p += 7;
617 while (*p && isspace(*p)) p++;
618 if (*p != '\"' && *p != '<' ) continue;
619 quote = *p++;
620 if (quote == '<') quote = '>';
621 include = p;
622 while (*p && (*p != quote)) p++;
623 if (!*p) fatal_error( "%s:%d: Malformed #include directive\n",
624 pFile->filename, input_line );
625 *p = 0;
626 add_include( pFile, include, input_line, (quote == '>') );
631 /*******************************************************************
632 * parse_rc_file
634 static void parse_rc_file( INCL_FILE *pFile, FILE *file )
636 char *buffer, *include;
638 input_line = 0;
639 while ((buffer = get_line( file )))
641 char quote;
642 char *p = buffer;
643 while (*p && isspace(*p)) p++;
645 if (p[0] == '/' && p[1] == '*') /* check for magic makedep comment */
647 p += 2;
648 while (*p && isspace(*p)) p++;
649 if (strncmp( p, "@makedep:", 9 )) continue;
650 p += 9;
651 while (*p && isspace(*p)) p++;
652 quote = '"';
653 if (*p == quote)
655 include = ++p;
656 while (*p && *p != quote) p++;
658 else
660 include = p;
661 while (*p && !isspace(*p) && *p != '*') p++;
663 if (!*p)
664 fatal_error( "%s:%d: Malformed makedep comment\n", pFile->filename, input_line );
665 *p = 0;
667 else /* check for #include */
669 if (*p++ != '#') continue;
670 while (*p && isspace(*p)) p++;
671 if (strncmp( p, "include", 7 )) continue;
672 p += 7;
673 while (*p && isspace(*p)) p++;
674 if (*p != '\"' && *p != '<' ) continue;
675 quote = *p++;
676 if (quote == '<') quote = '>';
677 include = p;
678 while (*p && (*p != quote)) p++;
679 if (!*p) fatal_error( "%s:%d: Malformed #include directive\n",
680 pFile->filename, input_line );
681 *p = 0;
683 add_include( pFile, include, input_line, (quote == '>') );
688 /*******************************************************************
689 * parse_generated_idl
691 static void parse_generated_idl( INCL_FILE *source )
693 char *header, *basename;
695 basename = xstrdup( source->name );
696 basename[strlen(basename) - 4] = 0;
697 header = strmake( "%s.h", basename );
698 source->filename = xstrdup( source->name );
700 if (strendswith( source->name, "_c.c" ))
702 add_include( source, header, 0, 0 );
704 else if (strendswith( source->name, "_i.c" ))
706 add_include( source, "rpc.h", 0, 1 );
707 add_include( source, "rpcndr.h", 0, 1 );
708 add_include( source, "guiddef.h", 0, 1 );
710 else if (strendswith( source->name, "_p.c" ))
712 add_include( source, "rpcproxy.h", 0, 1 );
713 add_include( source, header, 0, 0 );
715 else if (strendswith( source->name, "_s.c" ))
717 add_include( source, header, 0, 0 );
720 free( header );
721 free( basename );
724 /*******************************************************************
725 * parse_file
727 static void parse_file( INCL_FILE *pFile, int src )
729 FILE *file;
731 /* special case for source files generated from idl */
732 if (strendswith( pFile->name, "_c.c" ) ||
733 strendswith( pFile->name, "_i.c" ) ||
734 strendswith( pFile->name, "_p.c" ) ||
735 strendswith( pFile->name, "_s.c" ))
737 parse_generated_idl( pFile );
738 return;
741 file = src ? open_src_file( pFile ) : open_include_file( pFile );
742 if (!file) return;
744 if (pFile->sourcename && strendswith( pFile->sourcename, ".idl" ))
745 parse_idl_file( pFile, file, 1 );
746 else if (strendswith( pFile->filename, ".idl" ))
747 parse_idl_file( pFile, file, 0 );
748 else if (strendswith( pFile->filename, ".c" ) ||
749 strendswith( pFile->filename, ".h" ) ||
750 strendswith( pFile->filename, ".l" ) ||
751 strendswith( pFile->filename, ".y" ))
752 parse_c_file( pFile, file );
753 else if (strendswith( pFile->filename, ".rc" ))
754 parse_rc_file( pFile, file );
755 fclose(file);
759 /*******************************************************************
760 * output_include
762 static void output_include( FILE *file, INCL_FILE *pFile,
763 INCL_FILE *owner, int *column )
765 int i;
767 if (pFile->owner == owner) return;
768 if (!pFile->filename) return;
769 pFile->owner = owner;
770 if (*column + strlen(pFile->filename) + 1 > 70)
772 fprintf( file, " \\\n" );
773 *column = 0;
775 fprintf( file, " %s", pFile->filename );
776 *column += strlen(pFile->filename) + 1;
777 for (i = 0; i < MAX_INCLUDES; i++)
778 if (pFile->files[i]) output_include( file, pFile->files[i],
779 owner, column );
783 /*******************************************************************
784 * output_src
786 static void output_src( FILE *file, INCL_FILE *pFile, int *column )
788 char *obj = xstrdup( pFile->name );
789 char *ext = get_extension( obj );
790 if (ext)
792 *ext++ = 0;
793 if (!strcmp( ext, "y" )) /* yacc file */
795 *column += fprintf( file, "%s.tab.o: %s.tab.c", obj, obj );
797 else if (!strcmp( ext, "l" )) /* lex file */
799 *column += fprintf( file, "%s.yy.o: %s.yy.c", obj, obj );
801 else if (!strcmp( ext, "rc" )) /* resource file */
803 *column += fprintf( file, "%s.res: %s", obj, pFile->filename );
805 else if (!strcmp( ext, "mc" )) /* message file */
807 *column += fprintf( file, "%s.mc.rc: %s", obj, pFile->filename );
809 else if (!strcmp( ext, "idl" )) /* IDL file */
811 char *name;
813 *column += fprintf( file, "%s.h", obj );
815 name = strmake( "%s_c.c", obj );
816 if (find_src_file( name )) *column += fprintf( file, " %s", name );
817 free( name );
818 name = strmake( "%s_i.c", obj );
819 if (find_src_file( name )) *column += fprintf( file, " %s", name );
820 free( name );
821 name = strmake( "%s_p.c", obj );
822 if (find_src_file( name )) *column += fprintf( file, " %s", name );
823 free( name );
824 name = strmake( "%s_s.c", obj );
825 if (find_src_file( name )) *column += fprintf( file, " %s", name );
826 free( name );
828 *column += fprintf( file, ": %s", pFile->filename );
830 else
832 *column += fprintf( file, "%s.o: %s", obj, pFile->filename );
835 free( obj );
839 /*******************************************************************
840 * output_dependencies
842 static void output_dependencies(void)
844 INCL_FILE *pFile;
845 int i, column;
846 FILE *file = NULL;
847 char *buffer;
849 if (Separator && ((file = fopen( OutputFileName, "r+" ))))
851 while ((buffer = get_line( file )))
853 if (strncmp( buffer, Separator, strlen(Separator) )) continue;
854 ftruncate( fileno(file), ftell(file) );
855 fseek( file, 0L, SEEK_END );
856 break;
859 if (!file)
861 if (!(file = fopen( OutputFileName, Separator ? "a" : "w" )))
863 perror( OutputFileName );
864 exit(1);
867 LIST_FOR_EACH_ENTRY( pFile, &sources, INCL_FILE, entry )
869 column = 0;
870 output_src( file, pFile, &column );
871 for (i = 0; i < MAX_INCLUDES; i++)
872 if (pFile->files[i]) output_include( file, pFile->files[i],
873 pFile, &column );
874 fprintf( file, "\n" );
876 fclose(file);
880 /*******************************************************************
881 * parse_option
883 static void parse_option( const char *opt )
885 switch(opt[1])
887 case 'I':
888 if (opt[2]) add_include_path( opt + 2 );
889 break;
890 case 'C':
891 src_dir = opt + 2;
892 break;
893 case 'S':
894 top_src_dir = opt + 2;
895 break;
896 case 'T':
897 top_obj_dir = opt + 2;
898 break;
899 case 'f':
900 if (opt[2]) OutputFileName = opt + 2;
901 break;
902 case 's':
903 if (opt[2]) Separator = opt + 2;
904 else Separator = NULL;
905 break;
906 default:
907 fprintf( stderr, "Unknown option '%s'\n", opt );
908 fprintf( stderr, Usage, ProgramName );
909 exit(1);
914 /*******************************************************************
915 * main
917 int main( int argc, char *argv[] )
919 INCL_FILE *pFile;
920 INCL_PATH *path, *next;
921 int i, j;
923 ProgramName = argv[0];
925 i = 1;
926 while (i < argc)
928 if (argv[i][0] == '-')
930 parse_option( argv[i] );
931 for (j = i; j < argc; j++) argv[j] = argv[j+1];
932 argc--;
934 else i++;
937 /* ignore redundant source paths */
938 if (src_dir && !strcmp( src_dir, "." )) src_dir = NULL;
939 if (top_src_dir && top_obj_dir && !strcmp( top_src_dir, top_obj_dir )) top_src_dir = NULL;
941 /* get rid of absolute paths that don't point into the source dir */
942 LIST_FOR_EACH_ENTRY_SAFE( path, next, &paths, INCL_PATH, entry )
944 if (path->name[0] != '/') continue;
945 if (top_src_dir)
947 if (!strncmp( path->name, top_src_dir, strlen(top_src_dir) )) continue;
948 if (path->name[strlen(top_src_dir)] == '/') continue;
950 list_remove( &path->entry );
951 free( path );
954 for (i = 1; i < argc; i++)
956 if ((pFile = add_src_file( argv[i] ))) parse_file( pFile, 1 );
958 LIST_FOR_EACH_ENTRY( pFile, &includes, INCL_FILE, entry ) parse_file( pFile, 0 );
959 output_dependencies();
960 return 0;