Debian Codegraph @

options.c

raw ◀ ▶
2 Debian patches touch this file
1 /*
2 * $Id: options.c 576 2007-06-30 04:16:23Z elliotth $
3 *
4 * Copyright (c) 1996-2003, Darren Hiebert
5 *
6 * This source code is released for free distribution under the terms of the
7 * GNU General Public License.
8 *
9 * This module contains functions to process command line options.
10 */
11
12 /*
13 * INCLUDE FILES
14 */
15 #include "general.h" /* must always come first */
16
17 #include <stdlib.h>
18 #include <string.h>
19 #include <stdio.h>
20 #include <ctype.h> /* to declare isspace () */
21
22 #include "ctags.h"
23 #include "debug.h"
24 #include "main.h"
25 #define OPTION_WRITE
26 #include "options.h"
27 #include "parse.h"
28 #include "routines.h"
29
30 /*
31 * MACROS
32 */
33 #define INVOCATION "Usage: %s [options] [file(s)]\n"
34
35 #define CTAGS_ENVIRONMENT "CTAGS"
36 #define ETAGS_ENVIRONMENT "ETAGS"
37
38 #define CTAGS_FILE "tags"
39 #define ETAGS_FILE "TAGS"
40
41 #ifndef ETAGS
42 # define ETAGS "etags" /* name which causes default use of to -e */
43 #endif
44
45 /* The following separators are permitted for list options.
46 */
47 #define EXTENSION_SEPARATOR '.'
48 #define PATTERN_START '('
49 #define PATTERN_STOP ')'
50 #define IGNORE_SEPARATORS ", \t\n"
51
52 #ifndef DEFAULT_FILE_FORMAT
53 # define DEFAULT_FILE_FORMAT 2
54 #endif
55
56 #if defined (HAVE_OPENDIR) || defined (HAVE_FINDFIRST) || defined (HAVE__FINDFIRST) || defined (AMIGA)
57 # define RECURSE_SUPPORTED
58 #endif
59
60 #define isCompoundOption(c) (boolean) (strchr ("fohiILpDb", (c)) != NULL)
61
62 /*
63 * Data declarations
64 */
65
66 enum eOptionLimits {
67 MaxHeaderExtensions = 100, /* maximum number of extensions in -h option */
68 MaxSupportedTagFormat = 2
69 };
70
71 typedef struct sOptionDescription {
72 int usedByEtags;
73 const char *description;
74 } optionDescription;
75
76 typedef void (*parametricOptionHandler) (const char *const option, const char *const parameter);
77
78 typedef const struct {
79 const char* name; /* name of option as specified by user */
80 parametricOptionHandler handler; /* routine to handle option */
81 boolean initOnly; /* option must be specified before any files */
82 } parametricOption;
83
84 typedef const struct {
85 const char* name; /* name of option as specified by user */
86 boolean* pValue; /* pointer to option value */
87 boolean initOnly; /* option must be specified before any files */
88 } booleanOption;
89
90 /*
91 * DATA DEFINITIONS
92 */
93
94 static boolean NonOptionEncountered;
95 static stringList *OptionFiles;
96 static stringList* Excluded;
97 static boolean FilesRequired = TRUE;
98 static boolean SkipConfiguration;
99
100 static const char *const HeaderExtensions [] = {
101 "h", "H", "hh", "hpp", "hxx", "h++", "inc", "def", NULL
102 };
103
104 optionValues Option = {
105 {
106 FALSE, /* --extra=f */
107 FALSE, /* --extra=q */
108 TRUE, /* --file-scope */
109 },
110 {
111 FALSE, /* -fields=a */
112 TRUE, /* -fields=f */
113 FALSE, /* -fields=m */
114 FALSE, /* -fields=i */
115 TRUE, /* -fields=k */
116 FALSE, /* -fields=z */
117 FALSE, /* -fields=K */
118 FALSE, /* -fields=l */
119 FALSE, /* -fields=n */
120 TRUE, /* -fields=s */
121 FALSE, /* -fields=S */
122 TRUE /* -fields=t */
123 },
124 NULL, /* -I */
125 FALSE, /* -a */
126 FALSE, /* -B */
127 FALSE, /* -e */
128 #ifdef MACROS_USE_PATTERNS
129 EX_PATTERN, /* -n, --excmd */
130 #else
131 EX_MIX, /* -n, --excmd */
132 #endif
133 FALSE, /* -R */
134 SO_SORTED, /* -u, --sort */
135 FALSE, /* -V */
136 FALSE, /* -x */
137 NULL, /* -L */
138 NULL, /* -o */
139 NULL, /* -h */
140 NULL, /* --etags-include */
141 DEFAULT_FILE_FORMAT,/* --format */
142 FALSE, /* --if0 */
143 FALSE, /* --kind-long */
144 LANG_AUTO, /* --lang */
145 TRUE, /* --links */
146 FALSE, /* --filter */
147 NULL, /* --filter-terminator */
148 FALSE, /* --tag-relative */
149 FALSE, /* --totals */
150 FALSE, /* --line-directives */
151 #ifdef DEBUG
152 0, 0 /* -D, -b */
153 #endif
154 };
155
156 /*
157 - Locally used only
158 */
159
160 static optionDescription LongOptionDescription [] = {
161 {1," -a Append the tags to an existing tag file."},
162 #ifdef DEBUG
163 {1," -b <line>"},
164 {1," Set break line."},
165 #endif
166 {0," -B Use backward searching patterns (?...?)."},
167 #ifdef DEBUG
168 {1," -D <level>"},
169 {1," Set debug level."},
170 #endif
171 {0," -e Output tag file for use with Emacs."},
172 {1," -f <name>"},
173 {1," Write tags to specified file. Value of \"-\" writes tags to stdout"},
174 {1," [\"tags\"; or \"TAGS\" when -e supplied]."},
175 {0," -F Use forward searching patterns (/.../) (default)."},
176 {1," -h <list>"},
177 {1," Specify list of file extensions to be treated as include files."},
178 {1," [\".h.H.hh.hpp.hxx.h++\"]."},
179 {1," -I <list|@file>"},
180 {1," A list of tokens to be specially handled is read from either the"},
181 {1," command line or the specified file."},
182 {1," -L <file>"},
183 {1," A list of source file names are read from the specified file."},
184 {1," If specified as \"-\", then standard input is read."},
185 {0," -n Equivalent to --excmd=number."},
186 {0," -N Equivalent to --excmd=pattern."},
187 {1," -o Alternative for -f."},
188 #ifdef RECURSE_SUPPORTED
189 {1," -R Equivalent to --recurse."},
190 #else
191 {1," -R Not supported on this platform."},
192 #endif
193 {0," -u Equivalent to --sort=no."},
194 {1," -V Equivalent to --verbose."},
195 {1," -x Print a tabular cross reference file to standard output."},
196 {1," --append=[yes|no]"},
197 {1," Should tags should be appended to existing tag file [no]?"},
198 {1," --etags-include=file"},
199 {1," Include reference to 'file' in Emacs-style tag file (requires -e)."},
200 {1," --exclude=pattern"},
201 {1," Exclude files and directories matching 'pattern'."},
202 {0," --excmd=number|pattern|mix"},
203 #ifdef MACROS_USE_PATTERNS
204 {0," Uses the specified type of EX command to locate tags [pattern]."},
205 #else
206 {0," Uses the specified type of EX command to locate tags [mix]."},
207 #endif
208 {1," --extra=[+|-]flags"},
209 {1," Include extra tag entries for selected information (flags: \"fq\")."},
210 {1," --fields=[+|-]flags"},
211 {1," Include selected extension fields (flags: \"afmikKlnsStz\") [fks]."},
212 {1," --file-scope=[yes|no]"},
213 {1," Should tags scoped only for a single file (e.g. \"static\" tags"},
214 {1," be included in the output [yes]?"},
215 {1," --filter=[yes|no]"},
216 {1," Behave as a filter, reading file names from standard input and"},
217 {1," writing tags to standard output [no]."},
218 {1," --filter-terminator=string"},
219 {1," Specify string to print to stdout following the tags for each file"},
220 {1," parsed when --filter is enabled."},
221 {0," --format=level"},
222 #if DEFAULT_FILE_FORMAT == 1
223 {0," Force output of specified tag file format [1]."},
224 #else
225 {0," Force output of specified tag file format [2]."},
226 #endif
227 {1," --help"},
228 {1," Print this option summary."},
229 {1," --if0=[yes|no]"},
230 {1," Should C code within #if 0 conditional branches be parsed [no]?"},
231 {1," --<LANG>-kinds=[+|-]kinds"},
232 {1," Enable/disable tag kinds for language <LANG>."},
233 {1," --langdef=name"},
234 {1," Define a new language to be parsed with regular expressions."},
235 {1," --langmap=map(s)"},
236 {1," Override default mapping of language to source file extension."},
237 {1," --language-force=language"},
238 {1," Force all files to be interpreted using specified language."},
239 {1," --languages=[+|-]list"},
240 {1," Restrict files scanned for tags to those mapped to langauges"},
241 {1," specified in the comma-separated 'list'. The list can contain any"},
242 {1," built-in or user-defined language [all]."},
243 {1," --license"},
244 {1," Print details of software license."},
245 {0," --line-directives=[yes|no]"},
246 {0," Should #line directives be processed [no]?"},
247 {1," --links=[yes|no]"},
248 {1," Indicate whether symbolic links should be followed [yes]."},
249 {1," --list-kinds=[language|all]"},
250 {1," Output a list of all tag kinds for specified language or all."},
251 {1," --list-languages"},
252 {1," Output list of supported languages."},
253 {1," --list-maps=[language|all]"},
254 {1," Output list of language mappings."},
255 {1," --options=file"},
256 {1," Specify file from which command line options should be read."},
257 {1," --recurse=[yes|no]"},
258 #ifdef RECURSE_SUPPORTED
259 {1," Recurse into directories supplied on command line [no]."},
260 #else
261 {1," Not supported on this platform."},
262 #endif
263 #ifdef HAVE_REGEX
264 {1," --regex-<LANG>=/line_pattern/name_pattern/[flags]"},
265 {1," Define regular expression for locating tags in specific language."},
266 #endif
267 {0," --sort=[yes|no|foldcase]"},
268 {0," Should tags be sorted (optionally ignoring case) [yes]?."},
269 {0," --tag-relative=[yes|no]"},
270 {0," Should paths be relative to location of tag file [no; yes when -e]?"},
271 {1," --totals=[yes|no]"},
272 {1," Print statistics about source and tag files [no]."},
273 {1," --verbose=[yes|no]"},
274 {1," Enable verbose messages describing actions on each source file."},
275 {1," --version"},
276 {1," Print version identifier to standard output."},
277 {1, NULL}
278 };
279
280 static const char* const License1 =
281 "This program is free software; you can redistribute it and/or\n"
282 "modify it under the terms of the GNU General Public License\n"
283 "as published by the Free Software Foundation; either version 2\n"
284 "of the License, or (at your option) any later version.\n"
285 "\n";
286 static const char* const License2 =
287 "This program is distributed in the hope that it will be useful,\n"
288 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
289 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
290 "GNU General Public License for more details.\n"
291 "\n"
292 "You should have received a copy of the GNU General Public License\n"
293 "along with this program; if not, write to the Free Software\n"
294 "Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\n";
295
296 /* Contains a set of strings describing the set of "features" compiled into
297 * the code.
298 */
299 static const char *const Features [] = {
300 #ifdef WIN32
301 "win32",
302 #endif
303 #ifdef DJGPP
304 "msdos_32",
305 #else
306 # ifdef MSDOS
307 "msdos_16",
308 # endif
309 #endif
310 #ifdef OS2
311 "os2",
312 #endif
313 #ifdef AMIGA
314 "amiga",
315 #endif
316 #ifdef VMS
317 "vms",
318 #endif
319 #ifdef HAVE_FNMATCH
320 "wildcards",
321 #endif
322 #ifdef HAVE_REGEX
323 "regex",
324 #endif
325 #ifndef EXTERNAL_SORT
326 "internal-sort",
327 #endif
328 #ifdef CUSTOM_CONFIGURATION_FILE
329 "custom-conf",
330 #endif
331 #if (defined (MSDOS) || defined (WIN32) || defined (OS2)) && defined (UNIX_PATH_SEPARATOR)
332 "unix-path-separator",
333 #endif
334 #ifdef DEBUG
335 "debug",
336 #endif
337 NULL
338 };
339
340 /*
341 * FUNCTION PROTOTYPES
342 */
343 static boolean parseFileOptions (const char *const fileName);
344
345 /*
346 * FUNCTION DEFINITIONS
347 */
348
349 extern void verbose (const char *const format, ...)
350 {
351 if (Option.verbose)
352 {
353 va_list ap;
354 va_start (ap, format);
355 vprintf (format, ap);
356 va_end (ap);
357 }
358 }
359
360 static char *stringCopy (const char *const string)
361 {
362 char* result = NULL;
363 if (string != NULL)
364 result = eStrdup (string);
365 return result;
366 }
367
368 static void freeString (char **const pString)
369 {
370 if (*pString != NULL)
371 {
372 eFree (*pString);
373 *pString = NULL;
374 }
375 }
376
377 extern void freeList (stringList** const pList)
378 {
379 if (*pList != NULL)
380 {
381 stringListDelete (*pList);
382 *pList = NULL;
383 }
384 }
385
386 extern void setDefaultTagFileName (void)
387 {
388 if (Option.tagFileName != NULL)
389 ; /* accept given name */
390 else if (Option.etags)
391 Option.tagFileName = stringCopy (ETAGS_FILE);
392 else
393 Option.tagFileName = stringCopy (CTAGS_FILE);
394 }
395
396 extern boolean filesRequired (void)
397 {
398 boolean result = FilesRequired;
399 if (Option.recurse)
400 result = FALSE;
401 return result;
402 }
403
404 extern void checkOptions (void)
405 {
406 const char* notice;
407 if (Option.xref)
408 {
409 notice = "xref output";
410 if (Option.include.fileNames)
411 {
412 error (WARNING, "%s disables file name tags", notice);
413 Option.include.fileNames = FALSE;
414 }
415 }
416 if (Option.append)
417 {
418 notice = "append mode is not compatible with";
419 if (isDestinationStdout ())
420 error (FATAL, "%s tags to stdout", notice);
421 }
422 if (Option.filter)
423 {
424 notice = "filter mode";
425 if (Option.printTotals)
426 {
427 error (WARNING, "%s disables totals", notice);
428 Option.printTotals = FALSE;
429 }
430 if (Option.tagFileName != NULL)
431 error (WARNING, "%s ignores output tag file name", notice);
432 }
433 }
434
435 static void setEtagsMode (void)
436 {
437 Option.etags = TRUE;
438 Option.sorted = SO_UNSORTED;
439 Option.lineDirectives = FALSE;
440 Option.tagRelative = TRUE;
441 }
442
443 extern void testEtagsInvocation (void)
444 {
445 char* const execName = eStrdup (getExecutableName ());
446 char* const etags = eStrdup (ETAGS);
447 #ifdef CASE_INSENSITIVE_FILENAMES
448 toLowerString (execName);
449 toLowerString (etags);
450 #endif
451 if (strstr (execName, etags) != NULL)
452 {
453 verbose ("Running in etags mode\n");
454 setEtagsMode ();
455 }
456 eFree (execName);
457 eFree (etags);
458 }
459
460 /*
461 * Cooked argument parsing
462 */
463
464 static void parseShortOption (cookedArgs *const args)
465 {
466 args->simple [0] = *args->shortOptions++;
467 args->simple [1] = '\0';
468 args->item = args->simple;
469 if (! isCompoundOption (*args->simple))
470 args->parameter = "";
471 else if (*args->shortOptions == '\0')
472 {
473 argForth (args->args);
474 if (argOff (args->args))
475 args->parameter = NULL;
476 else
477 args->parameter = argItem (args->args);
478 args->shortOptions = NULL;
479 }
480 else
481 {
482 args->parameter = args->shortOptions;
483 args->shortOptions = NULL;
484 }
485 }
486
487 static void parseLongOption (cookedArgs *const args, const char *item)
488 {
489 const char* const equal = strchr (item, '=');
490 if (equal == NULL)
491 {
492 args->item = eStrdup (item); /* FIXME: memory leak. */
493 args->parameter = "";
494 }
495 else
496 {
497 const size_t length = equal - item;
498 args->item = xMalloc (length + 1, char); /* FIXME: memory leak. */
499 strncpy (args->item, item, length);
500 args->item [length] = '\0';
501 args->parameter = equal + 1;
502 }
503 Assert (args->item != NULL);
504 Assert (args->parameter != NULL);
505 }
506
507 static void cArgRead (cookedArgs *const current)
508 {
509 char* item;
510
511 Assert (current != NULL);
512 if (! argOff (current->args))
513 {
514 item = argItem (current->args);
515 current->shortOptions = NULL;
516 Assert (item != NULL);
517 if (strncmp (item, "--", (size_t) 2) == 0)
518 {
519 current->isOption = TRUE;
520 current->longOption = TRUE;
521 parseLongOption (current, item + 2);
522 Assert (current->item != NULL);
523 Assert (current->parameter != NULL);
524 }
525 else if (*item == '-')
526 {
527 current->isOption = TRUE;
528 current->longOption = FALSE;
529 current->shortOptions = item + 1;
530 parseShortOption (current);
531 }
532 else
533 {
534 current->isOption = FALSE;
535 current->longOption = FALSE;
536 current->item = item;
537 current->parameter = NULL;
538 }
539 }
540 }
541
542 extern cookedArgs* cArgNewFromString (const char* string)
543 {
544 cookedArgs* const result = xMalloc (1, cookedArgs);
545 memset (result, 0, sizeof (cookedArgs));
546 result->args = argNewFromString (string);
547 cArgRead (result);
548 return result;
549 }
550
551 extern cookedArgs* cArgNewFromArgv (char* const* const argv)
552 {
553 cookedArgs* const result = xMalloc (1, cookedArgs);
554 memset (result, 0, sizeof (cookedArgs));
555 result->args = argNewFromArgv (argv);
556 cArgRead (result);
557 return result;
558 }
559
560 extern cookedArgs* cArgNewFromFile (FILE* const fp)
561 {
562 cookedArgs* const result = xMalloc (1, cookedArgs);
563 memset (result, 0, sizeof (cookedArgs));
564 result->args = argNewFromFile (fp);
565 cArgRead (result);
566 return result;
567 }
568
569 extern cookedArgs* cArgNewFromLineFile (FILE* const fp)
570 {
571 cookedArgs* const result = xMalloc (1, cookedArgs);
572 memset (result, 0, sizeof (cookedArgs));
573 result->args = argNewFromLineFile (fp);
574 cArgRead (result);
575 return result;
576 }
577
578 extern void cArgDelete (cookedArgs* const current)
579 {
580 Assert (current != NULL);
581 argDelete (current->args);
582 memset (current, 0, sizeof (cookedArgs));
583 eFree (current);
584 }
585
586 static boolean cArgOptionPending (cookedArgs* const current)
587 {
588 boolean result = FALSE;
589 if (current->shortOptions != NULL)
590 if (*current->shortOptions != '\0')
591 result = TRUE;
592 return result;
593 }
594
595 extern boolean cArgOff (cookedArgs* const current)
596 {
597 Assert (current != NULL);
598 return (boolean) (argOff (current->args) && ! cArgOptionPending (current));
599 }
600
601 extern boolean cArgIsOption (cookedArgs* const current)
602 {
603 Assert (current != NULL);
604 return current->isOption;
605 }
606
607 extern const char* cArgItem (cookedArgs* const current)
608 {
609 Assert (current != NULL);
610 return current->item;
611 }
612
613 extern void cArgForth (cookedArgs* const current)
614 {
615 Assert (current != NULL);
616 Assert (! cArgOff (current));
617 if (cArgOptionPending (current))
618 parseShortOption (current);
619 else
620 {
621 Assert (! argOff (current->args));
622 argForth (current->args);
623 if (! argOff (current->args))
624 cArgRead (current);
625 else
626 {
627 current->isOption = FALSE;
628 current->longOption = FALSE;
629 current->shortOptions = NULL;
630 current->item = NULL;
631 current->parameter = NULL;
632 }
633 }
634 }
635
636 /*
637 * File extension and language mapping
638 */
639
640 static void addExtensionList (
641 stringList *const slist, const char *const elist, const boolean clear)
642 {
643 char *const extensionList = eStrdup (elist);
644 const char *extension = NULL;
645 boolean first = TRUE;
646
647 if (clear)
648 {
649 verbose (" clearing\n");
650 stringListClear (slist);
651 }
652 verbose (" adding: ");
653 if (elist != NULL && *elist != '\0')
654 {
655 extension = extensionList;
656 if (elist [0] == EXTENSION_SEPARATOR)
657 ++extension;
658 }
659 while (extension != NULL)
660 {
661 char *separator = strchr (extension, EXTENSION_SEPARATOR);
662 if (separator != NULL)
663 *separator = '\0';
664 verbose ("%s%s", first ? "" : ", ",
665 *extension == '\0' ? "(NONE)" : extension);
666 stringListAdd (slist, vStringNewInit (extension));
667 first = FALSE;
668 if (separator == NULL)
669 extension = NULL;
670 else
671 extension = separator + 1;
672 }
673 if (Option.verbose)
674 {
675 printf ("\n now: ");
676 stringListPrint (slist);
677 putchar ('\n');
678 }
679 eFree (extensionList);
680 }
681
682 static boolean isFalse (const char *parameter)
683 {
684 return (boolean) (
685 strcasecmp (parameter, "0" ) == 0 ||
686 strcasecmp (parameter, "n" ) == 0 ||
687 strcasecmp (parameter, "no" ) == 0 ||
688 strcasecmp (parameter, "off") == 0);
689 }
690
691 static boolean isTrue (const char *parameter)
692 {
693 return (boolean) (
694 strcasecmp (parameter, "1" ) == 0 ||
695 strcasecmp (parameter, "y" ) == 0 ||
696 strcasecmp (parameter, "yes") == 0 ||
697 strcasecmp (parameter, "on" ) == 0);
698 }
699
700 /* Determines whether the specified file name is considered to be a header
701 * file for the purposes of determining whether enclosed tags are global or
702 * static.
703 */
704 extern boolean isIncludeFile (const char *const fileName)
705 {
706 boolean result = FALSE;
707 const char *const extension = fileExtension (fileName);
708 if (Option.headerExt != NULL)
709 result = stringListExtensionMatched (Option.headerExt, extension);
710 return result;
711 }
712
713 /*
714 * Specific option processing
715 */
716
717 static void processEtagsInclude (
718 const char *const option, const char *const parameter)
719 {
720 if (! Option.etags)
721 error (FATAL, "Etags must be enabled to use \"%s\" option", option);
722 else
723 {
724 vString *const file = vStringNewInit (parameter);
725 if (Option.etagsInclude == NULL)
726 Option.etagsInclude = stringListNew ();
727 stringListAdd (Option.etagsInclude, file);
728 FilesRequired = FALSE;
729 }
730 }
731
732 static void processExcludeOption (
•733 const char *const option __unused, const char *const parameter)
734 {
735 const char *const fileName = parameter + 1;
736 if (parameter [0] == '\0')
737 freeList (&Excluded);
738 else if (parameter [0] == '@')
739 {
740 stringList* const sl = stringListNewFromFile (fileName);
741 if (sl == NULL)
742 error (FATAL | PERROR, "cannot open \"%s\"", fileName);
743 if (Excluded == NULL)
744 Excluded = sl;
745 else
746 stringListCombine (Excluded, sl);
747 verbose (" adding exclude patterns from %s\n", fileName);
748 }
749 else
750 {
751 vString *const item = vStringNewInit (parameter);
752 if (Excluded == NULL)
753 Excluded = stringListNew ();
754 stringListAdd (Excluded, item);
755 verbose (" adding exclude pattern: %s\n", parameter);
756 }
757 }
758
759 extern boolean isExcludedFile (const char* const name)
760 {
761 const char* base = baseFilename (name);
762 boolean result = FALSE;
763 if (Excluded != NULL)
764 {
765 result = stringListFileMatched (Excluded, base);
766 if (! result && name != base)
767 result = stringListFileMatched (Excluded, name);
768 }
769 #ifdef AMIGA
770 /* not a good solution, but the only one which works often */
771 if (! result)
772 result = (boolean) (strcmp (name, TagFile.name) == 0);
773 #endif
774 return result;
775 }
776
777 static void processExcmdOption (
778 const char *const option, const char *const parameter)
779 {
780 switch (*parameter)
781 {
782 case 'm': Option.locate = EX_MIX; break;
783 case 'n': Option.locate = EX_LINENUM; break;
784 case 'p': Option.locate = EX_PATTERN; break;
785 default:
786 error (FATAL, "Invalid value for \"%s\" option", option);
787 break;
788 }
789 }
790
791 static void processExtraTagsOption (
792 const char *const option, const char *const parameter)
793 {
794 struct sInclude *const inc = &Option.include;
795 const char *p = parameter;
796 boolean mode = TRUE;
797 int c;
798
799 if (*p != '+' && *p != '-')
800 {
801 inc->fileNames = FALSE;
802 inc->qualifiedTags = FALSE;
803 #if 0
804 inc->fileScope = FALSE;
805 #endif
806 }
807 while ((c = *p++) != '\0') switch (c)
808 {
809 case '+': mode = TRUE; break;
810 case '-': mode = FALSE; break;
811
812 case 'f': inc->fileNames = mode; break;
813 case 'q': inc->qualifiedTags = mode; break;
814 #if 0
815 case 'F': inc->fileScope = mode; break;
816 #endif
817
818 default: error(WARNING, "Unsupported parameter '%c' for \"%s\" option",
819 c, option);
820 break;
821 }
822 }
823
824 static void processFieldsOption (
825 const char *const option, const char *const parameter)
826 {
827 struct sExtFields *field = &Option.extensionFields;
828 const char *p = parameter;
829 boolean mode = TRUE;
830 int c;
831
832 if (*p != '+' && *p != '-')
833 {
834 field->access = FALSE;
835 field->fileScope = FALSE;
836 field->implementation = FALSE;
837 field->inheritance = FALSE;
838 field->kind = FALSE;
839 field->kindKey = FALSE;
840 field->kindLong = FALSE;
841 field->language = FALSE;
842 field->scope = FALSE;
843 field->typeRef = FALSE;
844 }
845 while ((c = *p++) != '\0') switch (c)
846 {
847 case '+': mode = TRUE; break;
848 case '-': mode = FALSE; break;
849
850 case 'a': field->access = mode; break;
851 case 'f': field->fileScope = mode; break;
852 case 'm': field->implementation = mode; break;
853 case 'i': field->inheritance = mode; break;
854 case 'k': field->kind = mode; break;
855 case 'K': field->kindLong = mode; break;
856 case 'l': field->language = mode; break;
857 case 'n': field->lineNumber = mode; break;
858 case 's': field->scope = mode; break;
859 case 'S': field->signature = mode; break;
860 case 'z': field->kindKey = mode; break;
861 case 't': field->typeRef = mode; break;
862
863 default: error(WARNING, "Unsupported parameter '%c' for \"%s\" option",
864 c, option);
865 break;
866 }
867 }
868
869 static void processFilterTerminatorOption (
•870 const char *const option __unused, const char *const parameter)
871 {
872 freeString (&Option.filterTerminator);
873 Option.filterTerminator = stringCopy (parameter);
874 }
875
876 static void processFormatOption (
877 const char *const option, const char *const parameter)
878 {
879 unsigned int format;
880
881 if (sscanf (parameter, "%u", &format) < 1)
882 error (FATAL, "Invalid value for \"%s\" option",option);
883 else if (format <= (unsigned int) MaxSupportedTagFormat)
884 Option.tagFileFormat = format;
885 else
886 error (FATAL, "Unsupported value for \"%s\" option", option);
887 }
888
889 static void printInvocationDescription (void)
890 {
891 printf (INVOCATION, getExecutableName ());
892 }
893
894 static void printOptionDescriptions (const optionDescription *const optDesc)
895 {
896 int i;
897 for (i = 0 ; optDesc [i].description != NULL ; ++i)
898 {
899 if (! Option.etags || optDesc [i].usedByEtags)
900 puts (optDesc [i].description);
901 }
902 }
903
904 static void printFeatureList (void)
905 {
906 int i;
907
908 for (i = 0 ; Features [i] != NULL ; ++i)
909 {
910 if (i == 0)
911 printf (" Optional compiled features: ");
912 printf ("%s+%s", (i>0 ? ", " : ""), Features [i]);
913 #ifdef CUSTOM_CONFIGURATION_FILE
914 if (strcmp (Features [i], "custom-conf") == 0)
915 printf ("=%s", CUSTOM_CONFIGURATION_FILE);
916 #endif
917 }
918 if (i > 0)
919 putchar ('\n');
920 }
921
922 static void printProgramIdentification (void)
923 {
924 printf ("%s %s, %s %s\n",
925 PROGRAM_NAME, PROGRAM_VERSION,
926 PROGRAM_COPYRIGHT, AUTHOR_NAME);
927 printf (" Addresses: <%s>, %s\n", AUTHOR_EMAIL, PROGRAM_URL);
928 printFeatureList ();
929 }
930
931 static void processHelpOption (
•932 const char *const option __unused,
933 const char *const parameter __unused)
934 {
935 printProgramIdentification ();
936 putchar ('\n');
937 printInvocationDescription ();
938 putchar ('\n');
939 printOptionDescriptions (LongOptionDescription);
940 exit (0);
941 }
942
943 static void processLanguageForceOption (
944 const char *const option, const char *const parameter)
945 {
946 langType language;
947 if (strcasecmp (parameter, "auto") == 0)
948 language = LANG_AUTO;
949 else
950 language = getNamedLanguage (parameter);
951
952 if (strcmp (option, "lang") == 0 || strcmp (option, "language") == 0)
953 error (WARNING,
954 "\"--%s\" option is obsolete; use \"--language-force\" instead",
955 option);
956 if (language == LANG_IGNORE)
957 error (FATAL, "Unknown language \"%s\" in \"%s\" option", parameter, option);
958 else
959 Option.language = language;
960 }
961 static char* skipPastMap (char* p)
962 {
963 while (*p != EXTENSION_SEPARATOR &&
964 *p != PATTERN_START && *p != ',' && *p != '\0')
965 ++p;
966 return p;
967 }
968
969 /* Parses the mapping beginning at `map', adds it to the language map, and
970 * returns first character past the map.
971 */
972 static char* addLanguageMap (const langType language, char* map)
973 {
974 char* p = NULL;
975 const char first = *map;
976 if (first == EXTENSION_SEPARATOR) /* extension map */
977 {
978 ++map;
979 p = skipPastMap (map);
980 if (*p == '\0')
981 {
982 verbose (" .%s", map);
983 addLanguageExtensionMap (language, map);
984 p = map + strlen (map);
985 }
986 else
987 {
988 const char separator = *p;
989 *p = '\0';
990 verbose (" .%s", map);
991 addLanguageExtensionMap (language, map);
992 *p = separator;
993 }
994 }
995 else if (first == PATTERN_START) /* pattern map */
996 {
997 ++map;
998 for (p = map ; *p != PATTERN_STOP && *p != '\0' ; ++p)
999 {
1000 if (*p == '\\' && *(p + 1) == PATTERN_STOP)
1001 ++p;
1002 }
1003 if (*p == '\0')
1004 error (FATAL, "Unterminated file name pattern for %s language",
1005 getLanguageName (language));
1006 else
1007 {
1008 *p++ = '\0';
1009 verbose (" (%s)", map);
1010 addLanguagePatternMap (language, map);
1011 }
1012 }
1013 else
1014 error (FATAL, "Badly formed language map for %s language",
1015 getLanguageName (language));
1016 return p;
1017 }
1018
1019 static char* processLanguageMap (char* map)
1020 {
1021 char* const separator = strchr (map, ':');
1022 char* result = NULL;
1023 if (separator != NULL)
1024 {
1025 langType language;
1026 char *list = separator + 1;
1027 boolean clear = FALSE;
1028 *separator = '\0';
1029 language = getNamedLanguage (map);
1030 if (language != LANG_IGNORE)
1031 {
1032 const char *const deflt = "default";
1033 char* p;
1034 if (*list == '+')
1035 ++list;
1036 else
1037 clear = TRUE;
1038 for (p = list ; *p != ',' && *p != '\0' ; ++p) /*no-op*/ ;
1039 if ((size_t) (p - list) == strlen (deflt) &&
1040 strncasecmp (list, deflt, p - list) == 0)
1041 {
1042 verbose (" Restoring default %s language map: ", getLanguageName (language));
1043 installLanguageMapDefault (language);
1044 list = p;
1045 }
1046 else
1047 {
1048 if (clear)
1049 {
1050 verbose (" Setting %s language map:", getLanguageName (language));
1051 clearLanguageMap (language);
1052 }
1053 else
1054 verbose (" Adding to %s language map:", getLanguageName (language));
1055 while (list != NULL && *list != '\0' && *list != ',')
1056 list = addLanguageMap (language, list);
1057 verbose ("\n");
1058 }
1059 if (list != NULL && *list == ',')
1060 ++list;
1061 result = list;
1062 }
1063 }
1064 return result;
1065 }
1066
1067 static void processLanguageMapOption (
1068 const char *const option, const char *const parameter)
1069 {
1070 char *const maps = eStrdup (parameter);
1071 char *map = maps;
1072
1073 if (strcmp (parameter, "default") == 0)
1074 {
1075 verbose (" Restoring default language maps:\n");
1076 installLanguageMapDefaults ();
1077 }
1078 else while (map != NULL && *map != '\0')
1079 {
1080 char* const next = processLanguageMap (map);
1081 if (next == NULL)
1082 error (WARNING, "Unknown language \"%s\" in \"%s\" option", parameter, option);
1083 map = next;
1084 }
1085 eFree (maps);
1086 }
1087
1088 static void processLanguagesOption (
1089 const char *const option, const char *const parameter)
1090 {
1091 char *const langs = eStrdup (parameter);
1092 enum { Add, Remove, Replace } mode = Replace;
1093 boolean first = TRUE;
1094 char *lang = langs;
1095 const char* prefix = "";
1096 verbose (" Enabled languages: ");
1097 while (lang != NULL)
1098 {
1099 char *const end = strchr (lang, ',');
1100 if (lang [0] == '+')
1101 {
1102 ++lang;
1103 mode = Add;
1104 prefix = "+ ";
1105 }
1106 else if (lang [0] == '-')
1107 {
1108 ++lang;
1109 mode = Remove;
1110 prefix = "- ";
1111 }
1112 if (mode == Replace)
1113 enableLanguages (FALSE);
1114 if (end != NULL)
1115 *end = '\0';
1116 if (lang [0] != '\0')
1117 {
1118 if (strcmp (lang, "all") == 0)
1119 enableLanguages ((boolean) (mode != Remove));
1120 else
1121 {
1122 const langType language = getNamedLanguage (lang);
1123 if (language == LANG_IGNORE)
1124 error (WARNING, "Unknown language \"%s\" in \"%s\" option", lang, option);
1125 else
1126 enableLanguage (language, (boolean) (mode != Remove));
1127 }
1128 verbose ("%s%s%s", (first ? "" : ", "), prefix, lang);
1129 prefix = "";
1130 first = FALSE;
1131 if (mode == Replace)
1132 mode = Add;
1133 }
1134 lang = (end != NULL ? end + 1 : NULL);
1135 }
1136 verbose ("\n");
1137 eFree (langs);
1138 }
1139
1140 static void processLicenseOption (
•1141 const char *const option __unused,
1142 const char *const parameter __unused)
1143 {
1144 printProgramIdentification ();
1145 puts ("");
1146 puts (License1);
1147 puts (License2);
1148 exit (0);
1149 }
1150
1151 static void processListKindsOption (
1152 const char *const option, const char *const parameter)
1153 {
1154 if (parameter [0] == '\0' || strcasecmp (parameter, "all") == 0)
1155 printLanguageKinds (LANG_AUTO);
1156 else
1157 {
1158 langType language = getNamedLanguage (parameter);
1159 if (language == LANG_IGNORE)
1160 error (FATAL, "Unknown language \"%s\" in \"%s\" option", parameter, option);
1161 else
1162 printLanguageKinds (language);
1163 }
1164 exit (0);
1165 }
1166
1167 static void processListMapsOption (
•1168 const char *const __unused option,
1169 const char *const __unused parameter)
1170 {
1171 if (parameter [0] == '\0' || strcasecmp (parameter, "all") == 0)
1172 printLanguageMaps (LANG_AUTO);
1173 else
1174 {
1175 langType language = getNamedLanguage (parameter);
1176 if (language == LANG_IGNORE)
1177 error (FATAL, "Unknown language \"%s\" in \"%s\" option", parameter, option);
1178 else
1179 printLanguageMaps (language);
1180 }
1181 exit (0);
1182 }
1183
1184 static void processListLanguagesOption (
•1185 const char *const option __unused,
1186 const char *const parameter __unused)
1187 {
1188 printLanguageList ();
1189 exit (0);
1190 }
1191
1192 static void processOptionFile (
1193 const char *const option, const char *const parameter)
1194 {
1195 if (parameter [0] == '\0')
1196 error (WARNING, "no option file supplied for \"%s\"", option);
1197 else if (! parseFileOptions (parameter))
1198 error (FATAL | PERROR, "cannot open option file \"%s\"", parameter);
1199 }
1200
1201 static void processSortOption (
1202 const char *const option, const char *const parameter)
1203 {
1204 if (isFalse (parameter))
1205 Option.sorted = SO_UNSORTED;
1206 else if (isTrue (parameter))
1207 Option.sorted = SO_SORTED;
1208 else if (strcasecmp (parameter, "f") == 0 ||
1209 strcasecmp (parameter, "fold") == 0 ||
1210 strcasecmp (parameter, "foldcase") == 0)
1211 Option.sorted = SO_FOLDSORTED;
1212 else
1213 error (FATAL, "Invalid value for \"%s\" option", option);
1214 }
1215
1216 static void installHeaderListDefaults (void)
1217 {
1218 Option.headerExt = stringListNewFromArgv (HeaderExtensions);
1219 if (Option.verbose)
1220 {
1221 printf (" Setting default header extensions: ");
1222 stringListPrint (Option.headerExt);
1223 putchar ('\n');
1224 }
1225 }
1226
1227 static void processHeaderListOption (const int option, const char *parameter)
1228 {
1229 /* Check to make sure that the user did not enter "ctags -h *.c"
1230 * by testing to see if the list is a filename that exists.
1231 */
1232 if (doesFileExist (parameter))
1233 error (FATAL, "-%c: Invalid list", option);
1234 if (strcmp (parameter, "default") == 0)
1235 installHeaderListDefaults ();
1236 else
1237 {
1238 boolean clear = TRUE;
1239
1240 if (parameter [0] == '+')
1241 {
1242 ++parameter;
1243 clear = FALSE;
1244 }
1245 if (Option.headerExt == NULL)
1246 Option.headerExt = stringListNew ();
1247 verbose (" Header Extensions:\n");
1248 addExtensionList (Option.headerExt, parameter, clear);
1249 }
1250 }
1251
1252 /*
1253 * Token ignore processing
1254 */
1255
1256 /* Determines whether or not "name" should be ignored, per the ignore list.
1257 */
1258 extern boolean isIgnoreToken (
1259 const char *const name, boolean *const pIgnoreParens,
1260 const char **const replacement)
1261 {
1262 boolean result = FALSE;
1263
1264 if (Option.ignore != NULL)
1265 {
1266 const size_t nameLen = strlen (name);
1267 unsigned int i;
1268
1269 if (pIgnoreParens != NULL)
1270 *pIgnoreParens = FALSE;
1271
1272 for (i = 0 ; i < stringListCount (Option.ignore) ; ++i)
1273 {
1274 vString *token = stringListItem (Option.ignore, i);
1275
1276 if (strncmp (vStringValue (token), name, nameLen) == 0)
1277 {
1278 const size_t tokenLen = vStringLength (token);
1279
1280 if (nameLen == tokenLen)
1281 {
1282 result = TRUE;
1283 break;
1284 }
1285 else if (tokenLen == nameLen + 1 &&
1286 vStringChar (token, tokenLen - 1) == '+')
1287 {
1288 result = TRUE;
1289 if (pIgnoreParens != NULL)
1290 *pIgnoreParens = TRUE;
1291 break;
1292 }
1293 else if (vStringChar (token, nameLen) == '=')
1294 {
1295 if (replacement != NULL)
1296 *replacement = vStringValue (token) + nameLen + 1;
1297 break;
1298 }
1299 }
1300 }
1301 }
1302 return result;
1303 }
1304
1305 static void saveIgnoreToken (vString *const ignoreToken)
1306 {
1307 if (Option.ignore == NULL)
1308 Option.ignore = stringListNew ();
1309 stringListAdd (Option.ignore, ignoreToken);
1310 verbose (" ignore token: %s\n", vStringValue (ignoreToken));
1311 }
1312
1313 static void readIgnoreList (const char *const list)
1314 {
1315 char* newList = stringCopy (list);
1316 const char *token = strtok (newList, IGNORE_SEPARATORS);
1317
1318 while (token != NULL)
1319 {
1320 vString *const entry = vStringNewInit (token);
1321
1322 saveIgnoreToken (entry);
1323 token = strtok (NULL, IGNORE_SEPARATORS);
1324 }
1325 eFree (newList);
1326 }
1327
1328 static void addIgnoreListFromFile (const char *const fileName)
1329 {
1330 stringList* tokens = stringListNewFromFile (fileName);
1331 if (tokens == NULL)
1332 error (FATAL | PERROR, "cannot open \"%s\"", fileName);
1333 if (Option.ignore == NULL)
1334 Option.ignore = tokens;
1335 else
1336 stringListCombine (Option.ignore, tokens);
1337 }
1338
1339 static void processIgnoreOption (const char *const list)
1340 {
1341 if (strchr ("@./\\", list [0]) != NULL)
1342 {
1343 const char* fileName = (*list == '@') ? list + 1 : list;
1344 addIgnoreListFromFile (fileName);
1345 }
1346 #if defined (MSDOS) || defined (WIN32) || defined (OS2)
1347 else if (isalpha (list [0]) && list [1] == ':')
1348 addIgnoreListFromFile (list);
1349 #endif
1350 else if (strcmp (list, "-") == 0)
1351 {
1352 freeList (&Option.ignore);
1353 verbose (" clearing list\n");
1354 }
1355 else
1356 readIgnoreList (list);
1357 }
1358
1359 static void processVersionOption (
•1360 const char *const option __unused,
1361 const char *const parameter __unused)
1362 {
1363 printProgramIdentification ();
1364 exit (0);
1365 }
1366
1367 /*
1368 * Option tables
1369 */
1370
1371 static parametricOption ParametricOptions [] = {
1372 { "etags-include", processEtagsInclude, FALSE },
1373 { "exclude", processExcludeOption, FALSE },
1374 { "excmd", processExcmdOption, FALSE },
1375 { "extra", processExtraTagsOption, FALSE },
1376 { "fields", processFieldsOption, FALSE },
1377 { "filter-terminator", processFilterTerminatorOption, TRUE },
1378 { "format", processFormatOption, TRUE },
1379 { "help", processHelpOption, TRUE },
1380 { "lang", processLanguageForceOption, FALSE },
1381 { "language", processLanguageForceOption, FALSE },
1382 { "language-force", processLanguageForceOption, FALSE },
1383 { "languages", processLanguagesOption, FALSE },
1384 { "langdef", processLanguageDefineOption, FALSE },
1385 { "langmap", processLanguageMapOption, FALSE },
1386 { "license", processLicenseOption, TRUE },
1387 { "list-kinds", processListKindsOption, TRUE },
1388 { "list-maps", processListMapsOption, TRUE },
1389 { "list-languages", processListLanguagesOption, TRUE },
1390 { "options", processOptionFile, FALSE },
1391 { "sort", processSortOption, TRUE },
1392 { "version", processVersionOption, TRUE },
1393 };
1394
1395 static booleanOption BooleanOptions [] = {
1396 { "append", &Option.append, TRUE },
1397 { "file-scope", &Option.include.fileScope, FALSE },
1398 { "file-tags", &Option.include.fileNames, FALSE },
1399 { "filter", &Option.filter, TRUE },
1400 { "if0", &Option.if0, FALSE },
1401 { "kind-long", &Option.kindLong, TRUE },
1402 { "line-directives",&Option.lineDirectives, FALSE },
1403 { "links", &Option.followLinks, FALSE },
1404 #ifdef RECURSE_SUPPORTED
1405 { "recurse", &Option.recurse, FALSE },
1406 #endif
1407 { "tag-relative", &Option.tagRelative, TRUE },
1408 { "totals", &Option.printTotals, TRUE },
1409 { "verbose", &Option.verbose, FALSE },
1410 };
1411
1412 /*
1413 * Generic option parsing
1414 */
1415
1416 static void checkOptionOrder (const char* const option)
1417 {
1418 if (NonOptionEncountered)
1419 error (FATAL, "-%s option may not follow a file name", option);
1420 }
1421
1422 static boolean processParametricOption (
1423 const char *const option, const char *const parameter)
1424 {
1425 const int count = sizeof (ParametricOptions) / sizeof (parametricOption);
1426 boolean found = FALSE;
1427 int i;
1428
1429 for (i = 0 ; i < count && ! found ; ++i)
1430 {
1431 parametricOption* const entry = &ParametricOptions [i];
1432 if (strcmp (option, entry->name) == 0)
1433 {
1434 found = TRUE;
1435 if (entry->initOnly)
1436 checkOptionOrder (option);
1437 (entry->handler) (option, parameter);
1438 }
1439 }
1440 return found;
1441 }
1442
1443 static boolean getBooleanOption (
1444 const char *const option, const char *const parameter)
1445 {
1446 boolean selection = TRUE;
1447
1448 if (parameter [0] == '\0')
1449 selection = TRUE;
1450 else if (isFalse (parameter))
1451 selection = FALSE;
1452 else if (isTrue (parameter))
1453 selection = TRUE;
1454 else
1455 error (FATAL, "Invalid value for \"%s\" option", option);
1456
1457 return selection;
1458 }
1459
1460 static boolean processBooleanOption (
1461 const char *const option, const char *const parameter)
1462 {
1463 const int count = sizeof (BooleanOptions) / sizeof (booleanOption);
1464 boolean found = FALSE;
1465 int i;
1466
1467 for (i = 0 ; i < count && ! found ; ++i)
1468 {
1469 booleanOption* const entry = &BooleanOptions [i];
1470 if (strcmp (option, entry->name) == 0)
1471 {
1472 found = TRUE;
1473 if (entry->initOnly)
1474 checkOptionOrder (option);
1475 *entry->pValue = getBooleanOption (option, parameter);
1476 }
1477 }
1478 return found;
1479 }
1480
1481 static void processLongOption (
1482 const char *const option, const char *const parameter)
1483 {
1484 Assert (parameter != NULL);
1485 if (parameter == NULL && parameter [0] == '\0')
1486 verbose (" Option: --%s\n", option);
1487 else
1488 verbose (" Option: --%s=%s\n", option, parameter);
1489
1490 if (processBooleanOption (option, parameter))
1491 ;
1492 else if (processParametricOption (option, parameter))
1493 ;
1494 else if (processKindOption (option, parameter))
1495 ;
1496 else if (processRegexOption (option, parameter))
1497 ;
1498 #ifndef RECURSE_SUPPORTED
1499 else if (strcmp (option, "recurse") == 0)
1500 error (WARNING, "%s option not supported on this host", option);
1501 #endif
1502 else
1503 error (FATAL, "Unknown option: --%s", option);
1504 }
1505
1506 static void processShortOption (
1507 const char *const option, const char *const parameter)
1508 {
1509 if (parameter == NULL || parameter [0] == '\0')
1510 verbose (" Option: -%s\n", option);
1511 else
1512 verbose (" Option: -%s %s\n", option, parameter);
1513
1514 if (isCompoundOption (*option) && (parameter == NULL || parameter [0] == '\0'))
1515 error (FATAL, "Missing parameter for \"%s\" option", option);
1516 else switch (*option)
1517 {
1518 case '?':
1519 processHelpOption ("?", NULL);
1520 exit (0);
1521 break;
1522 case 'a':
1523 checkOptionOrder (option);
1524 Option.append = TRUE;
1525 break;
1526 #ifdef DEBUG
1527 case 'b':
1528 if (atol (parameter) < 0)
1529 error (FATAL, "-%s: Invalid line number", option);
1530 Option.breakLine = atol (parameter);
1531 break;
1532 case 'D':
1533 Option.debugLevel = strtol (parameter, NULL, 0);
1534 if (debug (DEBUG_STATUS))
1535 Option.verbose = TRUE;
1536 break;
1537 #endif
1538 case 'B':
1539 Option.backward = TRUE;
1540 break;
1541 case 'e':
1542 checkOptionOrder (option);
1543 setEtagsMode ();
1544 break;
1545 case 'f':
1546 case 'o':
1547 checkOptionOrder (option);
1548 if (Option.tagFileName != NULL)
1549 {
1550 error (WARNING,
1551 "-%s option specified more than once, last value used",
1552 option);
1553 freeString (&Option.tagFileName);
1554 }
1555 else if (parameter [0] == '-' && parameter [1] != '\0')
1556 error (FATAL, "output file name may not begin with a '-'");
1557 Option.tagFileName = stringCopy (parameter);
1558 break;
1559 case 'F':
1560 Option.backward = FALSE;
1561 break;
1562 case 'h':
1563 processHeaderListOption (*option, parameter);
1564 break;
1565 case 'I':
1566 processIgnoreOption (parameter);
1567 break;
1568 case 'L':
1569 if (Option.fileList != NULL)
1570 {
1571 error (WARNING,
1572 "-%s option specified more than once, last value used",
1573 option);
1574 freeString (&Option.fileList);
1575 }
1576 Option.fileList = stringCopy (parameter);
1577 break;
1578 case 'n':
1579 Option.locate = EX_LINENUM;
1580 break;
1581 case 'N':
1582 Option.locate = EX_PATTERN;
1583 break;
1584 case 'R':
1585 #ifdef RECURSE_SUPPORTED
1586 Option.recurse = TRUE;
1587 #else
1588 error (WARNING, "-%s option not supported on this host", option);
1589 #endif
1590 break;
1591 case 'u':
1592 checkOptionOrder (option);
1593 Option.sorted = SO_UNSORTED;
1594 break;
1595 case 'V':
1596 Option.verbose = TRUE;
1597 break;
1598 case 'w':
1599 /* silently ignored */
1600 break;
1601 case 'x':
1602 checkOptionOrder (option);
1603 Option.xref = TRUE;
1604 break;
1605 default:
1606 error (FATAL, "Unknown option: -%s", option);
1607 break;
1608 }
1609 }
1610
1611 extern void parseOption (cookedArgs* const args)
1612 {
1613 Assert (! cArgOff (args));
1614 if (args->isOption)
1615 {
1616 if (args->longOption)
1617 processLongOption (args->item, args->parameter);
1618 else
1619 {
1620 const char *parameter = args->parameter;
1621 while (*parameter == ' ')
1622 ++parameter;
1623 processShortOption (args->item, parameter);
1624 }
1625 cArgForth (args);
1626 }
1627 }
1628
1629 extern void parseOptions (cookedArgs* const args)
1630 {
1631 NonOptionEncountered = FALSE;
1632 while (! cArgOff (args) && cArgIsOption (args))
1633 parseOption (args);
1634 if (! cArgOff (args) && ! cArgIsOption (args))
1635 NonOptionEncountered = TRUE;
1636 }
1637
1638 static const char *CheckFile;
1639 static boolean checkSameFile (const char *const fileName)
1640 {
1641 return isSameFile (CheckFile, fileName);
1642 }
1643
1644 static boolean parseFileOptions (const char* const fileName)
1645 {
1646 boolean fileFound = FALSE;
1647 const char* const format = "Considering option file %s: %s\n";
1648 CheckFile = fileName;
1649 if (stringListHasTest (OptionFiles, checkSameFile))
1650 verbose (format, fileName, "already considered");
1651 else
1652 {
1653 FILE* const fp = fopen (fileName, "r");
1654 if (fp == NULL)
1655 verbose (format, fileName, "not found");
1656 else
1657 {
1658 cookedArgs* const args = cArgNewFromLineFile (fp);
1659 vString* file = vStringNewInit (fileName);
1660 stringListAdd (OptionFiles, file);
1661 verbose (format, fileName, "reading...");
1662 parseOptions (args);
1663 if (NonOptionEncountered)
1664 error (WARNING, "Ignoring non-option in %s\n", fileName);
1665 cArgDelete (args);
1666 fclose (fp);
1667 fileFound = TRUE;
1668 }
1669 }
1670 return fileFound;
1671 }
1672
1673 /* Actions to be taken before reading any other options */
1674 extern void previewFirstOption (cookedArgs* const args)
1675 {
1676 while (cArgIsOption (args))
1677 {
1678 if (strcmp (args->item, "V") == 0 || strcmp (args->item, "verbose") == 0)
1679 parseOption (args);
1680 else if (strcmp (args->item, "options") == 0 &&
1681 strcmp (args->parameter, "NONE") == 0)
1682 {
1683 fprintf (stderr, "No options will be read from files or environment\n");
1684 SkipConfiguration = TRUE;
1685 cArgForth (args);
1686 }
1687 else
1688 break;
1689 }
1690 }
1691
1692 static void parseConfigurationFileOptionsInDirectoryWithLeafname (const char* directory, const char* leafname)
1693 {
1694 vString* const pathname = combinePathAndFile (directory, leafname);
1695 parseFileOptions (vStringValue (pathname));
1696 vStringDelete (pathname);
1697 }
1698
1699 static void parseConfigurationFileOptionsInDirectory (const char* directory)
1700 {
1701 parseConfigurationFileOptionsInDirectoryWithLeafname (directory, ".ctags");
1702 #ifdef MSDOS_STYLE_PATH
1703 parseConfigurationFileOptionsInDirectoryWithLeafname (directory, "ctags.cnf");
1704 #endif
1705 }
1706
1707 static void parseConfigurationFileOptions (void)
1708 {
1709 /* We parse .ctags on all systems, and additionally ctags.cnf on DOS. */
1710 const char* const home = getenv ("HOME");
1711 #ifdef CUSTOM_CONFIGURATION_FILE
1712 parseFileOptions (CUSTOM_CONFIGURATION_FILE);
1713 #endif
1714 #ifdef MSDOS_STYLE_PATH
1715 parseFileOptions ("/ctags.cnf");
1716 #endif
1717 parseFileOptions ("/etc/ctags.conf");
1718 parseFileOptions ("/usr/local/etc/ctags.conf");
1719 if (home != NULL)
1720 {
1721 parseConfigurationFileOptionsInDirectory (home);
1722 }
1723 else
1724 {
1725 #ifdef MSDOS_STYLE_PATH
1726 /*
1727 * Windows users don't usually set HOME.
1728 * The OS sets HOMEDRIVE and HOMEPATH for them.
1729 */
1730 const char* homeDrive = getenv ("HOMEDRIVE");
1731 const char* homePath = getenv ("HOMEPATH");
1732 if (homeDrive != NULL && homePath != NULL)
1733 {
1734 vString* const windowsHome = vStringNew ();
1735 vStringCatS (windowsHome, homeDrive);
1736 vStringCatS (windowsHome, homePath);
1737 parseConfigurationFileOptionsInDirectory (vStringValue (windowsHome));
1738 vStringDelete (windowsHome);
1739 }
1740 #endif
1741 }
1742 parseConfigurationFileOptionsInDirectory (".");
1743 }
1744
1745 static void parseEnvironmentOptions (void)
1746 {
1747 const char *envOptions = NULL;
1748 const char* var = NULL;
1749
1750 if (Option.etags)
1751 {
1752 var = ETAGS_ENVIRONMENT;
1753 envOptions = getenv (var);
1754 }
1755 if (envOptions == NULL)
1756 {
1757 var = CTAGS_ENVIRONMENT;
1758 envOptions = getenv (var);
1759 }
1760 if (envOptions != NULL && envOptions [0] != '\0')
1761 {
1762 cookedArgs* const args = cArgNewFromString (envOptions);
1763 verbose ("Reading options from $CTAGS\n");
1764 parseOptions (args);
1765 cArgDelete (args);
1766 if (NonOptionEncountered)
1767 error (WARNING, "Ignoring non-option in %s variable", var);
1768 }
1769 }
1770
1771 extern void readOptionConfiguration (void)
1772 {
1773 if (! SkipConfiguration)
1774 {
1775 parseConfigurationFileOptions ();
1776 parseEnvironmentOptions ();
1777 }
1778 }
1779
1780 /*
1781 * Option initialization
1782 */
1783
1784 extern void initOptions (void)
1785 {
1786 OptionFiles = stringListNew ();
1787 verbose ("Setting option defaults\n");
1788 installHeaderListDefaults ();
1789 verbose (" Installing default language mappings:\n");
1790 installLanguageMapDefaults ();
1791
1792 /* always excluded by default */
1793 verbose (" Installing default exclude patterns:\n");
1794 processExcludeOption (NULL, "{arch}");
1795 processExcludeOption (NULL, ".arch-ids");
1796 processExcludeOption (NULL, ".arch-inventory");
1797 processExcludeOption (NULL, "autom4te.cache");
1798 processExcludeOption (NULL, "BitKeeper");
1799 processExcludeOption (NULL, ".bzr");
1800 processExcludeOption (NULL, ".bzrignore");
1801 processExcludeOption (NULL, "CVS");
1802 processExcludeOption (NULL, ".cvsignore");
1803 processExcludeOption (NULL, "_darcs");
1804 processExcludeOption (NULL, ".deps");
1805 processExcludeOption (NULL, "EIFGEN");
1806 processExcludeOption (NULL, ".git");
1807 processExcludeOption (NULL, ".hg");
1808 processExcludeOption (NULL, "PENDING");
1809 processExcludeOption (NULL, "RCS");
1810 processExcludeOption (NULL, "RESYNC");
1811 processExcludeOption (NULL, "SCCS");
1812 processExcludeOption (NULL, ".svn");
1813 }
1814
1815 extern void freeOptionResources (void)
1816 {
1817 freeString (&Option.tagFileName);
1818 freeString (&Option.fileList);
1819 freeString (&Option.filterTerminator);
1820
1821 freeList (&Excluded);
1822 freeList (&Option.ignore);
1823 freeList (&Option.headerExt);
1824 freeList (&Option.etagsInclude);
1825 freeList (&OptionFiles);
1826 }
1827
1828 /* vi:set tabstop=4 shiftwidth=4: */