--- global~.c 2013-07-05 17:36:57 +1000 +++ global.c 2013-07-11 00:11:43 +1000 @@ -59,6 +59,7 @@ static void help(void); static void setcom(int); int decide_tag_by_context(const char *, const char *, int); +int prepend_args(const char *, int *, char ***); int main(int, char **); int completion_tags(const char *, const char *, const char *, int); void completion(const char *, const char *, const char *, int); @@ -150,10 +151,11 @@ static struct option const long_options[] = { {"absolute", no_argument, NULL, 'a'}, {"completion", no_argument, NULL, 'c'}, - {"color", no_argument, NULL, 'C'}, - {"colour", no_argument, NULL, 'C'}, + {"color", optional_argument, NULL, 'C'}, + {"colour", optional_argument, NULL, 'C'}, {"definition", no_argument, NULL, 'd'}, {"regexp", required_argument, NULL, 'e'}, + {"ext-regexp", no_argument, NULL, 'E'}, {"file", no_argument, NULL, 'f'}, {"local", no_argument, NULL, 'l'}, {"file-list", required_argument, NULL, 'L'}, @@ -162,6 +164,7 @@ {"basic-regexp", no_argument, NULL, 'G'}, {"ignore-case", no_argument, NULL, 'i'}, {"idutils", no_argument, NULL, 'I'}, + {"match-case", no_argument, NULL, 'm'}, {"other", no_argument, NULL, 'o'}, {"only-other", no_argument, NULL, 'O'}, {"print-dbpath", no_argument, NULL, 'p'}, @@ -170,6 +173,7 @@ {"reference", no_argument, NULL, 'r'}, {"rootdir", no_argument, NULL, 'r'}, {"symbol", no_argument, NULL, 's'}, + {"project", no_argument, NULL, 'S'}, {"tags", no_argument, NULL, 't'}, {"through", no_argument, NULL, 'T'}, {"update", no_argument, NULL, 'u'}, @@ -335,6 +339,56 @@ } return db; } +/** + * prepend_args: insert arguments from a string into an array + * + * @param[in] var string containing arguments to be inserted + * @param argc current and new array size + * @param argv current and new array arguments + * @return number of arguments prepended + * + * @note preserves first element and assumes NULL exists after last. + * @note backslash is used to escape the next character. + */ +int +prepend_args(const char *var, int *argc, char ***argv) +{ + int cnt = 0; + + if (var && *var) { + char *buf = check_strdup(var); + char *s = buf; + char *d = buf; + char **a, **args, **arg = *argv; + int i; + for (;;) { + while (*s && isspace(*s)) + ++s; + if (!*s) + break; + ++cnt; + do { + *d = *s++; + if (*d == '\\' && *s) + *d = *s++; + ++d; + } while (*s && !isspace(*s)); + *d++ = '\0'; + } + a = args = check_malloc((*argc + cnt + 1) * sizeof(char*)); + *a++ = *arg++; + s = buf; + for (i = 0; i < cnt; ++i) { + *a++ = s; + s = strchr(s, '\0') + 1; + } + memcpy(a, arg, *argc * sizeof(char*)); + *argc += cnt; + *argv = args; + } + + return cnt; +} int main(int argc, char **argv) { @@ -343,8 +397,9 @@ int optchar; int option_index = 0; + prepend_args(getenv("GLOBAL_OPTIONS"), &argc, &argv); logging_arguments(argc, argv); - while ((optchar = getopt_long(argc, argv, "acCde:ifgGIlL:noOpPqrstTuvVx", long_options, &option_index)) != EOF) { + while ((optchar = getopt_long(argc, argv, "acCde:EifgGIlL:mnoOpPqrsStTuvVx", long_options, &option_index)) != EOF) { switch (optchar) { case 0: break; @@ -356,7 +411,17 @@ setcom(optchar); break; case 'C': - Cflag++; + if (optarg) { + if (!strcmp(optarg, "never")) + Cflag = 0; + else if (!strcmp(optarg, "always")) + Cflag = 1; + else if (!strcmp(optarg, "auto")) + Cflag = 2; + else + die_with_code(2, "unknown color type for the --color option."); + } else + Cflag = 2; break; case 'd': dflag++; @@ -364,6 +429,9 @@ case 'e': av = optarg; break; + case 'E': + Gflag = 0; + break; case 'f': fflag++; xflag++; @@ -389,6 +457,9 @@ case 'L': file_list = optarg; break; + case 'm': + iflag = 0; + break; case 'n': nflag++; if (optarg) { @@ -424,6 +495,9 @@ case 's': sflag++; break; + case 'S': + Tflag = 0; + break; case 't': tflag++; break; @@ -532,10 +606,6 @@ warning("cannot encode '/' and '.' in the path. Ignored."); set_encode_chars((unsigned char *)encode_chars); } - if (getenv("GTAGSTHROUGH")) - Tflag++; - if (getenv("GTAGSCOLOR")) - Cflag++; if (qflag) vflag = 0; if (show_help) @@ -547,6 +617,8 @@ else if (!(getenv("ANSICON") || LoadLibrary("ANSI32.dll"))) Cflag = 0; #endif + if (Cflag == 2 && !isatty(1)) + Cflag = 0; if (Cflag) highlight_init(); } --- manual~.in 2013-07-05 17:36:57 +1000 +++ manual.in 2013-07-11 00:37:58 +1000 @@ -25,12 +25,12 @@ @HEADER GLOBAL,1,May 2012,GNU Project @NAME global - print locations of the specified object. @SYNOPSIS - @name{global} [-aCdGilnqrstTvx] [-e] @arg{pattern} - @name{global} -c[diIoOPrsT] @arg{prefix} + @name{global} [-aCdEGilmnqrsStTvx] [-e] @arg{pattern} + @name{global} -c[diImoOPrsST] @arg{prefix} @name{global} -f[aCdlnqrstvx] [-L file-list] @arg{files} - @name{global} -g[aCGilnoOqtvVx] [-L file-list] [-e] @arg{pattern} address@hidden - @name{global} -I[aCilnqtvx] [-e] @arg{pattern} - @name{global} -P[aCGilnoOqtvVx] [-e] @arg{pattern} + @name{global} -g[aCEGilmnoOqtvVx] [-L file-list] [-e] @arg{pattern} address@hidden + @name{global} -I[aCilmnqtvx] [-e] @arg{pattern} + @name{global} -P[aCEGilmnoOqtvVx] [-e] @arg{pattern} @name{global} -p[qrv] @name{global} -u[qv] @DESCRIPTION @@ -91,10 +91,11 @@ @begin_itemize @address@hidden, @option{--absolute}} Print absolute path name. By default, print relative path name. - @address@hidden, @option{--color}, @option{--colour}} - Use color to highlight the pattern within the line. The default - color is bold red text on current background; the environment - variable @var{GREP_COLORS} or @var{GREP_COLOR} defines it. + @address@hidden, @option{--color}, @option{--colour}} @arg{when} + Use color to highlight the pattern within the line; @arg{when} may be one + of: @arg{never}, @arg{always} or @arg{auto} (default). The default color is bold red + text on current background; the environment variable @var{GREP_COLORS} or + @var{GREP_COLOR} defines it. @address@hidden, @option{--definition}} Print locations of object definitions. @address@hidden @arg{context}} @@ -105,6 +106,9 @@ editors and IDEs. @address@hidden, @option{--regexp} @arg{pattern}} Use @arg{pattern} as the pattern; useful to protect patterns beginning with @file{-}. + @address@hidden, @option{--ext-regexp}} + Interpret @arg{pattern} as an extended regular expression. + This is provided to override @option{-G} in @var{GLOBAL_OPTIONS}. @address@hidden @arg{chars}} Convert path characters in @arg{chars} into a '%' symbol, followed by the two-digit hexadecimal representation of the character. @@ -121,6 +125,9 @@ @address@hidden Execute literal search instead of regular expression search. This option is only valid when the @option{-g} command is specified. + @address@hidden, @option{--match-case}} + Consider case distinctions in the pattern. + This is provided to override @option{-i} in @var{GLOBAL_OPTIONS}. @address@hidden @arg{part}}} Specify the matched part of path name. This option is valid only with the @option{-c} command with the @option{-P} option. @@ -153,6 +160,9 @@ The @option{--result=ctags} and @option{--result=ctags-x} options are equivalent to the @option{-t} and @option{-x} options respectively. The @option{--result} option is given more priority than the @option{-t} and @option{-x} options. + @address@hidden, @option{--project}} + Consider only the project's tag files. + This is provided to override @option{-T} in @var{GLOBAL_OPTIONS}. @address@hidden @arg{file}} Update tag files using @xref{gtags,1} with @option{--single-update} option. It is considered that @arg{file} was added or updated, @@ -242,12 +252,12 @@ @address@hidden If this variable is set, @file{$MAKEOBJDIRPREFIX} is used as the prefix of BSD-style objdir. The default is @file{/usr/obj}. - @address@hidden - If this variable is set, the @option{-T} option is specified. @address@hidden If this variable is set, the @option{--encode-path=" "} option is specified. - @address@hidden - If this variable is set, the @option{-C} option is specified. + @address@hidden + Use this variable to always add options to the beginning of the + command line (like 'global $GLOBAL_OPTIONS ...'). + Backslash is used to escape the next character. @address@hidden The color to use for @option{-C}; @var{GREP_COLORS} has precedence. @address@hidden