Debian Codegraph @

options.c

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 2 — not the installed (fully patched) source.
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 (" Compiled: %s, %s\n", __DATE__, __TIME__);
928 printf (" Addresses: <%s>, %s\n", AUTHOR_EMAIL, PROGRAM_URL);
929 printFeatureList ();
930 }
931
932 static void processHelpOption (
933 const char *const option __unused__,
934 const char *const parameter __unused__)
935 {
936 printProgramIdentification ();
937 putchar ('\n');
938 printInvocationDescription ();
939 putchar ('\n');
940 printOptionDescriptions (LongOptionDescription);
941 exit (0);
942 }
943
944 static void processLanguageForceOption (
945 const char *const option, const char *const parameter)
946 {
947 langType language;
948 if (strcasecmp (parameter, "auto") == 0)
949 language = LANG_AUTO;
950 else
951 language = getNamedLanguage (parameter);
952
953 if (strcmp (option, "lang") == 0 || strcmp (option, "language") == 0)
954 error (WARNING,
955 "\"--%s\" option is obsolete; use \"--language-force\" instead",
956 option);
957 if (language == LANG_IGNORE)
958 error (FATAL, "Unknown language \"%s\" in \"%s\" option", parameter, option);
959 else
960 Option.language = language;
961 }
962 static char* skipPastMap (char* p)
963 {
964 while (*p != EXTENSION_SEPARATOR &&
965 *p != PATTERN_START && *p != ',' && *p != '\0')
966 ++p;
967 return p;
968 }
969
970 /* Parses the mapping beginning at `map', adds it to the language map, and
971 * returns first character past the map.
972 */
973 static char* addLanguageMap (const langType language, char* map)
974 {
975 char* p = NULL;
976 const char first = *map;
977 if (first == EXTENSION_SEPARATOR) /* extension map */
978 {
979 ++map;
980 p = skipPastMap (map);
981 if (*p == '\0')
982 {
983 verbose (" .%s", map);
984 addLanguageExtensionMap (language, map);
985 p = map + strlen (map);
986 }
987 else
988 {
989 const char separator = *p;
990 *p = '\0';
991 verbose (" .%s", map);
992 addLanguageExtensionMap (language, map);
993 *p = separator;
994 }
995 }
996 else if (first == PATTERN_START) /* pattern map */
997 {
998 ++map;
999 for (p = map ; *p != PATTERN_STOP && *p != '\0' ; ++p)
1000 {
1001 if (*p == '\\' && *(p + 1) == PATTERN_STOP)
1002 ++p;
1003 }
1004 if (*p == '\0')
1005 error (FATAL, "Unterminated file name pattern for %s language",
1006 getLanguageName (language));
1007 else
1008 {
1009 *p++ = '\0';
1010 verbose (" (%s)", map);
1011 addLanguagePatternMap (language, map);
1012 }
1013 }
1014 else
1015 error (FATAL, "Badly formed language map for %s language",
1016 getLanguageName (language));
1017 return p;
1018 }
1019
1020 static char* processLanguageMap (char* map)
1021 {
1022 char* const separator = strchr (map, ':');
1023 char* result = NULL;
1024 if (separator != NULL)
1025 {
1026 langType language;
1027 char *list = separator + 1;
1028 boolean clear = FALSE;
1029 *separator = '\0';
1030 language = getNamedLanguage (map);
1031 if (language != LANG_IGNORE)
1032 {
1033 const char *const deflt = "default";
1034 char* p;
1035 if (*list == '+')
1036 ++list;
1037 else
1038 clear = TRUE;
1039 for (p = list ; *p != ',' && *p != '\0' ; ++p) /*no-op*/ ;
1040 if ((size_t) (p - list) == strlen (deflt) &&
1041 strncasecmp (list, deflt, p - list) == 0)
1042 {
1043 verbose (" Restoring default %s language map: ", getLanguageName (language));
1044 installLanguageMapDefault (language);
1045 list = p;
1046 }
1047 else
1048 {
1049 if (clear)
1050 {
1051 verbose (" Setting %s language map:", getLanguageName (language));
1052 clearLanguageMap (language);
1053 }
1054 else
1055 verbose (" Adding to %s language map:", getLanguageName (language));
1056 while (list != NULL && *list != '\0' && *list != ',')
1057 list = addLanguageMap (language, list);
1058 verbose ("\n");
1059 }
1060 if (list != NULL && *list == ',')
1061 ++list;
1062 result = list;
1063 }
1064 }
1065 return result;
1066 }
1067
1068 static void processLanguageMapOption (
1069 const char *const option, const char *const parameter)
1070 {
1071 char *const maps = eStrdup (parameter);
1072 char *map = maps;
1073
1074 if (strcmp (parameter, "default") == 0)
1075 {
1076 verbose (" Restoring default language maps:\n");
1077 installLanguageMapDefaults ();
1078 }
1079 else while (map != NULL && *map != '\0')
1080 {
1081 char* const next = processLanguageMap (map);
1082 if (next == NULL)
1083 error (WARNING, "Unknown language \"%s\" in \"%s\" option", parameter, option);
1084 map = next;
1085 }
1086 eFree (maps);
1087 }
1088
1089 static void processLanguagesOption (
1090 const char *const option, const char *const parameter)
1091 {
1092 char *const langs = eStrdup (parameter);
1093 enum { Add, Remove, Replace } mode = Replace;
1094 boolean first = TRUE;
1095 char *lang = langs;
1096 const char* prefix = "";
1097 verbose (" Enabled languages: ");
1098 while (lang != NULL)
1099 {
1100 char *const end = strchr (lang, ',');
1101 if (lang [0] == '+')
1102 {
1103 ++lang;
1104 mode = Add;
1105 prefix = "+ ";
1106 }
1107 else if (lang [0] == '-')
1108 {
1109 ++lang;
1110 mode = Remove;
1111 prefix = "- ";
1112 }
1113 if (mode == Replace)
1114 enableLanguages (FALSE);
1115 if (end != NULL)
1116 *end = '\0';
1117 if (lang [0] != '\0')
1118 {
1119 if (strcmp (lang, "all") == 0)
1120 enableLanguages ((boolean) (mode != Remove));
1121 else
1122 {
1123 const langType language = getNamedLanguage (lang);
1124 if (language == LANG_IGNORE)
1125 error (WARNING, "Unknown language \"%s\" in \"%s\" option", lang, option);
1126 else
1127 enableLanguage (language, (boolean) (mode != Remove));
1128 }
1129 verbose ("%s%s%s", (first ? "" : ", "), prefix, lang);
1130 prefix = "";
1131 first = FALSE;
1132 if (mode == Replace)
1133 mode = Add;
1134 }
1135 lang = (end != NULL ? end + 1 : NULL);
1136 }
1137 verbose ("\n");
1138 eFree (langs);
1139 }
1140
1141 static void processLicenseOption (
1142 const char *const option __unused__,
1143 const char *const parameter __unused__)
1144 {
1145 printProgramIdentification ();
1146 puts ("");
1147 puts (License1);
1148 puts (License2);
1149 exit (0);
1150 }
1151
1152 static void processListKindsOption (
1153 const char *const option, const char *const parameter)
1154 {
1155 if (parameter [0] == '\0' || strcasecmp (parameter, "all") == 0)
1156 printLanguageKinds (LANG_AUTO);
1157 else
1158 {
1159 langType language = getNamedLanguage (parameter);
1160 if (language == LANG_IGNORE)
1161 error (FATAL, "Unknown language \"%s\" in \"%s\" option", parameter, option);
1162 else
1163 printLanguageKinds (language);
1164 }
1165 exit (0);
1166 }
1167
1168 static void processListMapsOption (
1169 const char *const __unused__ option,
1170 const char *const __unused__ parameter)
1171 {
1172 if (parameter [0] == '\0' || strcasecmp (parameter, "all") == 0)
1173 printLanguageMaps (LANG_AUTO);
1174 else
1175 {
1176 langType language = getNamedLanguage (parameter);
1177 if (language == LANG_IGNORE)
1178 error (FATAL, "Unknown language \"%s\" in \"%s\" option", parameter, option);
1179 else
1180 printLanguageMaps (language);
1181 }
1182 exit (0);
1183 }
1184
1185 static void processListLanguagesOption (
1186 const char *const option __unused__,
1187 const char *const parameter __unused__)
1188 {
1189 printLanguageList ();
1190 exit (0);
1191 }
1192
1193 static void processOptionFile (
1194 const char *const option, const char *const parameter)
1195 {
1196 if (parameter [0] == '\0')
1197 error (WARNING, "no option file supplied for \"%s\"", option);
1198 else if (! parseFileOptions (parameter))
1199 error (FATAL | PERROR, "cannot open option file \"%s\"", parameter);
1200 }
1201
1202 static void processSortOption (
1203 const char *const option, const char *const parameter)
1204 {
1205 if (isFalse (parameter))
1206 Option.sorted = SO_UNSORTED;
1207 else if (isTrue (parameter))
1208 Option.sorted = SO_SORTED;
1209 else if (strcasecmp (parameter, "f") == 0 ||
1210 strcasecmp (parameter, "fold") == 0 ||
1211 strcasecmp (parameter, "foldcase") == 0)
1212 Option.sorted = SO_FOLDSORTED;
1213 else
1214 error (FATAL, "Invalid value for \"%s\" option", option);
1215 }
1216
1217 static void installHeaderListDefaults (void)
1218 {
1219 Option.headerExt = stringListNewFromArgv (HeaderExtensions);
1220 if (Option.verbose)
1221 {
1222 printf (" Setting default header extensions: ");
1223 stringListPrint (Option.headerExt);
1224 putchar ('\n');
1225 }
1226 }
1227
1228 static void processHeaderListOption (const int option, const char *parameter)
1229 {
1230 /* Check to make sure that the user did not enter "ctags -h *.c"
1231 * by testing to see if the list is a filename that exists.
1232 */
1233 if (doesFileExist (parameter))
1234 error (FATAL, "-%c: Invalid list", option);
1235 if (strcmp (parameter, "default") == 0)
1236 installHeaderListDefaults ();
1237 else
1238 {
1239 boolean clear = TRUE;
1240
1241 if (parameter [0] == '+')
1242 {
1243 ++parameter;
1244 clear = FALSE;
1245 }
1246 if (Option.headerExt == NULL)
1247 Option.headerExt = stringListNew ();
1248 verbose (" Header Extensions:\n");
1249 addExtensionList (Option.headerExt, parameter, clear);
1250 }
1251 }
1252
1253 /*
1254 * Token ignore processing
1255 */
1256
1257 /* Determines whether or not "name" should be ignored, per the ignore list.
1258 */
1259 extern boolean isIgnoreToken (
1260 const char *const name, boolean *const pIgnoreParens,
1261 const char **const replacement)
1262 {
1263 boolean result = FALSE;
1264
1265 if (Option.ignore != NULL)
1266 {
1267 const size_t nameLen = strlen (name);
1268 unsigned int i;
1269
1270 if (pIgnoreParens != NULL)
1271 *pIgnoreParens = FALSE;
1272
1273 for (i = 0 ; i < stringListCount (Option.ignore) ; ++i)
1274 {
1275 vString *token = stringListItem (Option.ignore, i);
1276
1277 if (strncmp (vStringValue (token), name, nameLen) == 0)
1278 {
1279 const size_t tokenLen = vStringLength (token);
1280
1281 if (nameLen == tokenLen)
1282 {
1283 result = TRUE;
1284 break;
1285 }
1286 else if (tokenLen == nameLen + 1 &&
1287 vStringChar (token, tokenLen - 1) == '+')
1288 {
1289 result = TRUE;
1290 if (pIgnoreParens != NULL)
1291 *pIgnoreParens = TRUE;
1292 break;
1293 }
1294 else if (vStringChar (token, nameLen) == '=')
1295 {
1296 if (replacement != NULL)
1297 *replacement = vStringValue (token) + nameLen + 1;
1298 break;
1299 }
1300 }
1301 }
1302 }
1303 return result;
1304 }
1305
1306 static void saveIgnoreToken (vString *const ignoreToken)
1307 {
1308 if (Option.ignore == NULL)
1309 Option.ignore = stringListNew ();
1310 stringListAdd (Option.ignore, ignoreToken);
1311 verbose (" ignore token: %s\n", vStringValue (ignoreToken));
1312 }
1313
1314 static void readIgnoreList (const char *const list)
1315 {
1316 char* newList = stringCopy (list);
1317 const char *token = strtok (newList, IGNORE_SEPARATORS);
1318
1319 while (token != NULL)
1320 {
1321 vString *const entry = vStringNewInit (token);
1322
1323 saveIgnoreToken (entry);
1324 token = strtok (NULL, IGNORE_SEPARATORS);
1325 }
1326 eFree (newList);
1327 }
1328
1329 static void addIgnoreListFromFile (const char *const fileName)
1330 {
1331 stringList* tokens = stringListNewFromFile (fileName);
1332 if (tokens == NULL)
1333 error (FATAL | PERROR, "cannot open \"%s\"", fileName);
1334 if (Option.ignore == NULL)
1335 Option.ignore = tokens;
1336 else
1337 stringListCombine (Option.ignore, tokens);
1338 }
1339
1340 static void processIgnoreOption (const char *const list)
1341 {
1342 if (strchr ("@./\\", list [0]) != NULL)
1343 {
1344 const char* fileName = (*list == '@') ? list + 1 : list;
1345 addIgnoreListFromFile (fileName);
1346 }
1347 #if defined (MSDOS) || defined (WIN32) || defined (OS2)
1348 else if (isalpha (list [0]) && list [1] == ':')
1349 addIgnoreListFromFile (list);
1350 #endif
1351 else if (strcmp (list, "-") == 0)
1352 {
1353 freeList (&Option.ignore);
1354 verbose (" clearing list\n");
1355 }
1356 else
1357 readIgnoreList (list);
1358 }
1359
1360 static void processVersionOption (
1361 const char *const option __unused__,
1362 const char *const parameter __unused__)
1363 {
1364 printProgramIdentification ();
1365 exit (0);
1366 }
1367
1368 /*
1369 * Option tables
1370 */
1371
1372 static parametricOption ParametricOptions [] = {
1373 { "etags-include", processEtagsInclude, FALSE },
1374 { "exclude", processExcludeOption, FALSE },
1375 { "excmd", processExcmdOption, FALSE },
1376 { "extra", processExtraTagsOption, FALSE },
1377 { "fields", processFieldsOption, FALSE },
1378 { "filter-terminator", processFilterTerminatorOption, TRUE },
1379 { "format", processFormatOption, TRUE },
1380 { "help", processHelpOption, TRUE },
1381 { "lang", processLanguageForceOption, FALSE },
1382 { "language", processLanguageForceOption, FALSE },
1383 { "language-force", processLanguageForceOption, FALSE },
1384 { "languages", processLanguagesOption, FALSE },
1385 { "langdef", processLanguageDefineOption, FALSE },
1386 { "langmap", processLanguageMapOption, FALSE },
1387 { "license", processLicenseOption, TRUE },
1388 { "list-kinds", processListKindsOption, TRUE },
1389 { "list-maps", processListMapsOption, TRUE },
1390 { "list-languages", processListLanguagesOption, TRUE },
1391 { "options", processOptionFile, FALSE },
1392 { "sort", processSortOption, TRUE },
1393 { "version", processVersionOption, TRUE },
1394 };
1395
1396 static booleanOption BooleanOptions [] = {
1397 { "append", &Option.append, TRUE },
1398 { "file-scope", &Option.include.fileScope, FALSE },
1399 { "file-tags", &Option.include.fileNames, FALSE },
1400 { "filter", &Option.filter, TRUE },
1401 { "if0", &Option.if0, FALSE },
1402 { "kind-long", &Option.kindLong, TRUE },
1403 { "line-directives",&Option.lineDirectives, FALSE },
1404 { "links", &Option.followLinks, FALSE },
1405 #ifdef RECURSE_SUPPORTED
1406 { "recurse", &Option.recurse, FALSE },
1407 #endif
1408 { "tag-relative", &Option.tagRelative, TRUE },
1409 { "totals", &Option.printTotals, TRUE },
1410 { "verbose", &Option.verbose, FALSE },
1411 };
1412
1413 /*
1414 * Generic option parsing
1415 */
1416
1417 static void checkOptionOrder (const char* const option)
1418 {
1419 if (NonOptionEncountered)
1420 error (FATAL, "-%s option may not follow a file name", option);
1421 }
1422
1423 static boolean processParametricOption (
1424 const char *const option, const char *const parameter)
1425 {
1426 const int count = sizeof (ParametricOptions) / sizeof (parametricOption);
1427 boolean found = FALSE;
1428 int i;
1429
1430 for (i = 0 ; i < count && ! found ; ++i)
1431 {
1432 parametricOption* const entry = &ParametricOptions [i];
1433 if (strcmp (option, entry->name) == 0)
1434 {
1435 found = TRUE;
1436 if (entry->initOnly)
1437 checkOptionOrder (option);
1438 (entry->handler) (option, parameter);
1439 }
1440 }
1441 return found;
1442 }
1443
1444 static boolean getBooleanOption (
1445 const char *const option, const char *const parameter)
1446 {
1447 boolean selection = TRUE;
1448
1449 if (parameter [0] == '\0')
1450 selection = TRUE;
1451 else if (isFalse (parameter))
1452 selection = FALSE;
1453 else if (isTrue (parameter))
1454 selection = TRUE;
1455 else
1456 error (FATAL, "Invalid value for \"%s\" option", option);
1457
1458 return selection;
1459 }
1460
1461 static boolean processBooleanOption (
1462 const char *const option, const char *const parameter)
1463 {
1464 const int count = sizeof (BooleanOptions) / sizeof (booleanOption);
1465 boolean found = FALSE;
1466 int i;
1467
1468 for (i = 0 ; i < count && ! found ; ++i)
1469 {
1470 booleanOption* const entry = &BooleanOptions [i];
1471 if (strcmp (option, entry->name) == 0)
1472 {
1473 found = TRUE;
1474 if (entry->initOnly)
1475 checkOptionOrder (option);
1476 *entry->pValue = getBooleanOption (option, parameter);
1477 }
1478 }
1479 return found;
1480 }
1481
1482 static void processLongOption (
1483 const char *const option, const char *const parameter)
1484 {
1485 Assert (parameter != NULL);
1486 if (parameter == NULL && parameter [0] == '\0')
1487 verbose (" Option: --%s\n", option);
1488 else
1489 verbose (" Option: --%s=%s\n", option, parameter);
1490
1491 if (processBooleanOption (option, parameter))
1492 ;
1493 else if (processParametricOption (option, parameter))
1494 ;
1495 else if (processKindOption (option, parameter))
1496 ;
1497 else if (processRegexOption (option, parameter))
1498 ;
1499 #ifndef RECURSE_SUPPORTED
1500 else if (strcmp (option, "recurse") == 0)
1501 error (WARNING, "%s option not supported on this host", option);
1502 #endif
1503 else
1504 error (FATAL, "Unknown option: --%s", option);
1505 }
1506
1507 static void processShortOption (
1508 const char *const option, const char *const parameter)
1509 {
1510 if (parameter == NULL || parameter [0] == '\0')
1511 verbose (" Option: -%s\n", option);
1512 else
1513 verbose (" Option: -%s %s\n", option, parameter);
1514
1515 if (isCompoundOption (*option) && (parameter == NULL || parameter [0] == '\0'))
1516 error (FATAL, "Missing parameter for \"%s\" option", option);
1517 else switch (*option)
1518 {
1519 case '?':
1520 processHelpOption ("?", NULL);
1521 exit (0);
1522 break;
1523 case 'a':
1524 checkOptionOrder (option);
1525 Option.append = TRUE;
1526 break;
1527 #ifdef DEBUG
1528 case 'b':
1529 if (atol (parameter) < 0)
1530 error (FATAL, "-%s: Invalid line number", option);
1531 Option.breakLine = atol (parameter);
1532 break;
1533 case 'D':
1534 Option.debugLevel = strtol (parameter, NULL, 0);
1535 if (debug (DEBUG_STATUS))
1536 Option.verbose = TRUE;
1537 break;
1538 #endif
1539 case 'B':
1540 Option.backward = TRUE;
1541 break;
1542 case 'e':
1543 checkOptionOrder (option);
1544 setEtagsMode ();
1545 break;
1546 case 'f':
1547 case 'o':
1548 checkOptionOrder (option);
1549 if (Option.tagFileName != NULL)
1550 {
1551 error (WARNING,
1552 "-%s option specified more than once, last value used",
1553 option);
1554 freeString (&Option.tagFileName);
1555 }
1556 else if (parameter [0] == '-' && parameter [1] != '\0')
1557 error (FATAL, "output file name may not begin with a '-'");
1558 Option.tagFileName = stringCopy (parameter);
1559 break;
1560 case 'F':
1561 Option.backward = FALSE;
1562 break;
1563 case 'h':
1564 processHeaderListOption (*option, parameter);
1565 break;
1566 case 'I':
1567 processIgnoreOption (parameter);
1568 break;
1569 case 'L':
1570 if (Option.fileList != NULL)
1571 {
1572 error (WARNING,
1573 "-%s option specified more than once, last value used",
1574 option);
1575 freeString (&Option.fileList);
1576 }
1577 Option.fileList = stringCopy (parameter);
1578 break;
1579 case 'n':
1580 Option.locate = EX_LINENUM;
1581 break;
1582 case 'N':
1583 Option.locate = EX_PATTERN;
1584 break;
1585 case 'R':
1586 #ifdef RECURSE_SUPPORTED
1587 Option.recurse = TRUE;
1588 #else
1589 error (WARNING, "-%s option not supported on this host", option);
1590 #endif
1591 break;
1592 case 'u':
1593 checkOptionOrder (option);
1594 Option.sorted = SO_UNSORTED;
1595 break;
1596 case 'V':
1597 Option.verbose = TRUE;
1598 break;
1599 case 'w':
1600 /* silently ignored */
1601 break;
1602 case 'x':
1603 checkOptionOrder (option);
1604 Option.xref = TRUE;
1605 break;
1606 default:
1607 error (FATAL, "Unknown option: -%s", option);
1608 break;
1609 }
1610 }
1611
1612 extern void parseOption (cookedArgs* const args)
1613 {
1614 Assert (! cArgOff (args));
1615 if (args->isOption)
1616 {
1617 if (args->longOption)
1618 processLongOption (args->item, args->parameter);
1619 else
1620 {
1621 const char *parameter = args->parameter;
1622 while (*parameter == ' ')
1623 ++parameter;
1624 processShortOption (args->item, parameter);
1625 }
1626 cArgForth (args);
1627 }
1628 }
1629
1630 extern void parseOptions (cookedArgs* const args)
1631 {
1632 NonOptionEncountered = FALSE;
1633 while (! cArgOff (args) && cArgIsOption (args))
1634 parseOption (args);
1635 if (! cArgOff (args) && ! cArgIsOption (args))
1636 NonOptionEncountered = TRUE;
1637 }
1638
1639 static const char *CheckFile;
1640 static boolean checkSameFile (const char *const fileName)
1641 {
1642 return isSameFile (CheckFile, fileName);
1643 }
1644
1645 static boolean parseFileOptions (const char* const fileName)
1646 {
1647 boolean fileFound = FALSE;
1648 const char* const format = "Considering option file %s: %s\n";
1649 CheckFile = fileName;
1650 if (stringListHasTest (OptionFiles, checkSameFile))
1651 verbose (format, fileName, "already considered");
1652 else
1653 {
1654 FILE* const fp = fopen (fileName, "r");
1655 if (fp == NULL)
1656 verbose (format, fileName, "not found");
1657 else
1658 {
1659 cookedArgs* const args = cArgNewFromLineFile (fp);
1660 vString* file = vStringNewInit (fileName);
1661 stringListAdd (OptionFiles, file);
1662 verbose (format, fileName, "reading...");
1663 parseOptions (args);
1664 if (NonOptionEncountered)
1665 error (WARNING, "Ignoring non-option in %s\n", fileName);
1666 cArgDelete (args);
1667 fclose (fp);
1668 fileFound = TRUE;
1669 }
1670 }
1671 return fileFound;
1672 }
1673
1674 /* Actions to be taken before reading any other options */
1675 extern void previewFirstOption (cookedArgs* const args)
1676 {
1677 while (cArgIsOption (args))
1678 {
1679 if (strcmp (args->item, "V") == 0 || strcmp (args->item, "verbose") == 0)
1680 parseOption (args);
1681 else if (strcmp (args->item, "options") == 0 &&
1682 strcmp (args->parameter, "NONE") == 0)
1683 {
1684 fprintf (stderr, "No options will be read from files or environment\n");
1685 SkipConfiguration = TRUE;
1686 cArgForth (args);
1687 }
1688 else
1689 break;
1690 }
1691 }
1692
1693 static void parseConfigurationFileOptionsInDirectoryWithLeafname (const char* directory, const char* leafname)
1694 {
1695 vString* const pathname = combinePathAndFile (directory, leafname);
1696 parseFileOptions (vStringValue (pathname));
1697 vStringDelete (pathname);
1698 }
1699
1700 static void parseConfigurationFileOptionsInDirectory (const char* directory)
1701 {
1702 parseConfigurationFileOptionsInDirectoryWithLeafname (directory, ".ctags");
1703 #ifdef MSDOS_STYLE_PATH
1704 parseConfigurationFileOptionsInDirectoryWithLeafname (directory, "ctags.cnf");
1705 #endif
1706 }
1707
1708 static void parseConfigurationFileOptions (void)
1709 {
1710 /* We parse .ctags on all systems, and additionally ctags.cnf on DOS. */
1711 const char* const home = getenv ("HOME");
1712 #ifdef CUSTOM_CONFIGURATION_FILE
1713 parseFileOptions (CUSTOM_CONFIGURATION_FILE);
1714 #endif
1715 #ifdef MSDOS_STYLE_PATH
1716 parseFileOptions ("/ctags.cnf");
1717 #endif
1718 parseFileOptions ("/etc/ctags.conf");
1719 parseFileOptions ("/usr/local/etc/ctags.conf");
1720 if (home != NULL)
1721 {
1722 parseConfigurationFileOptionsInDirectory (home);
1723 }
1724 else
1725 {
1726 #ifdef MSDOS_STYLE_PATH
1727 /*
1728 * Windows users don't usually set HOME.
1729 * The OS sets HOMEDRIVE and HOMEPATH for them.
1730 */
1731 const char* homeDrive = getenv ("HOMEDRIVE");
1732 const char* homePath = getenv ("HOMEPATH");
1733 if (homeDrive != NULL && homePath != NULL)
1734 {
1735 vString* const windowsHome = vStringNew ();
1736 vStringCatS (windowsHome, homeDrive);
1737 vStringCatS (windowsHome, homePath);
1738 parseConfigurationFileOptionsInDirectory (vStringValue (windowsHome));
1739 vStringDelete (windowsHome);
1740 }
1741 #endif
1742 }
1743 parseConfigurationFileOptionsInDirectory (".");
1744 }
1745
1746 static void parseEnvironmentOptions (void)
1747 {
1748 const char *envOptions = NULL;
1749 const char* var = NULL;
1750
1751 if (Option.etags)
1752 {
1753 var = ETAGS_ENVIRONMENT;
1754 envOptions = getenv (var);
1755 }
1756 if (envOptions == NULL)
1757 {
1758 var = CTAGS_ENVIRONMENT;
1759 envOptions = getenv (var);
1760 }
1761 if (envOptions != NULL && envOptions [0] != '\0')
1762 {
1763 cookedArgs* const args = cArgNewFromString (envOptions);
1764 verbose ("Reading options from $CTAGS\n");
1765 parseOptions (args);
1766 cArgDelete (args);
1767 if (NonOptionEncountered)
1768 error (WARNING, "Ignoring non-option in %s variable", var);
1769 }
1770 }
1771
1772 extern void readOptionConfiguration (void)
1773 {
1774 if (! SkipConfiguration)
1775 {
1776 parseConfigurationFileOptions ();
1777 parseEnvironmentOptions ();
1778 }
1779 }
1780
1781 /*
1782 * Option initialization
1783 */
1784
1785 extern void initOptions (void)
1786 {
1787 OptionFiles = stringListNew ();
1788 verbose ("Setting option defaults\n");
1789 installHeaderListDefaults ();
1790 verbose (" Installing default language mappings:\n");
1791 installLanguageMapDefaults ();
1792
1793 /* always excluded by default */
1794 verbose (" Installing default exclude patterns:\n");
1795 processExcludeOption (NULL, "{arch}");
1796 processExcludeOption (NULL, ".arch-ids");
1797 processExcludeOption (NULL, ".arch-inventory");
1798 processExcludeOption (NULL, "autom4te.cache");
1799 processExcludeOption (NULL, "BitKeeper");
1800 processExcludeOption (NULL, ".bzr");
1801 processExcludeOption (NULL, ".bzrignore");
1802 processExcludeOption (NULL, "CVS");
1803 processExcludeOption (NULL, ".cvsignore");
1804 processExcludeOption (NULL, "_darcs");
1805 processExcludeOption (NULL, ".deps");
1806 processExcludeOption (NULL, "EIFGEN");
1807 processExcludeOption (NULL, ".git");
1808 processExcludeOption (NULL, ".hg");
1809 processExcludeOption (NULL, "PENDING");
1810 processExcludeOption (NULL, "RCS");
1811 processExcludeOption (NULL, "RESYNC");
1812 processExcludeOption (NULL, "SCCS");
1813 processExcludeOption (NULL, ".svn");
1814 }
1815
1816 extern void freeOptionResources (void)
1817 {
1818 freeString (&Option.tagFileName);
1819 freeString (&Option.fileList);
1820 freeString (&Option.filterTerminator);
1821
1822 freeList (&Excluded);
1823 freeList (&Option.ignore);
1824 freeList (&Option.headerExt);
1825 freeList (&Option.etagsInclude);
1826 freeList (&OptionFiles);
1827 }
1828
1829 /* vi:set tabstop=4 shiftwidth=4: */