1 /* dircolors - output commands to set the LS_COLOR environment variable
2 Copyright (C) 1996-2008 Free Software Foundation, Inc.
3 Copyright (C) 1994, 1995, 1997, 1998, 1999, 2000 H. Peter Anvin
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include <sys/types.h>
25 #include "dircolors.h"
26 #include "c-strcase.h"
32 /* The official name of this program (e.g., no `g' prefix). */
33 #define PROGRAM_NAME "dircolors"
35 #define AUTHORS proper_name ("H. Peter Anvin")
37 #define obstack_chunk_alloc malloc
38 #define obstack_chunk_free free
47 #define APPEND_CHAR(C) obstack_1grow (&lsc_obstack, C)
48 #define APPEND_TWO_CHAR_STRING(S) \
56 /* Accumulate in this obstack the value for the LS_COLORS environment
58 static struct obstack lsc_obstack;
60 static const char *const slack_codes[] =
62 "NORMAL", "NORM", "FILE", "RESET", "DIR", "LNK", "LINK",
63 "SYMLINK", "ORPHAN", "MISSING", "FIFO", "PIPE", "SOCK", "BLK", "BLOCK",
64 "CHR", "CHAR", "DOOR", "EXEC", "LEFT", "LEFTCODE", "RIGHT", "RIGHTCODE",
65 "END", "ENDCODE", "SUID", "SETUID", "SGID", "SETGID", "STICKY",
66 "OTHER_WRITABLE", "OWR", "STICKY_OTHER_WRITABLE", "OWT", NULL
69 static const char *const ls_codes[] =
71 "no", "no", "fi", "rs", "di", "ln", "ln", "ln", "or", "mi", "pi", "pi",
72 "so", "bd", "bd", "cd", "cd", "do", "ex", "lc", "lc", "rc", "rc", "ec", "ec",
73 "su", "su", "sg", "sg", "st", "ow", "ow", "tw", "tw", NULL
75 #define array_len(Array) (sizeof (Array) / sizeof *(Array))
76 verify (array_len (slack_codes) == array_len (ls_codes));
78 static struct option const long_options[] =
80 {"bourne-shell", no_argument, NULL, 'b'},
81 {"sh", no_argument, NULL, 'b'},
82 {"csh", no_argument, NULL, 'c'},
83 {"c-shell", no_argument, NULL, 'c'},
84 {"print-database", no_argument, NULL, 'p'},
85 {GETOPT_HELP_OPTION_DECL},
86 {GETOPT_VERSION_OPTION_DECL},
95 if (status != EXIT_SUCCESS)
96 fprintf (stderr, _("Try `%s --help' for more information.\n"),
100 printf (_("Usage: %s [OPTION]... [FILE]\n"), program_name);
102 Output commands to set the LS_COLORS environment variable.\n\
104 Determine format of output:\n\
105 -b, --sh, --bourne-shell output Bourne shell code to set LS_COLORS\n\
106 -c, --csh, --c-shell output C shell code to set LS_COLORS\n\
107 -p, --print-database output defaults\n\
109 fputs (HELP_OPTION_DESCRIPTION, stdout);
110 fputs (VERSION_OPTION_DESCRIPTION, stdout);
113 If FILE is specified, read it to determine which colors to use for which\n\
114 file types and extensions. Otherwise, a precompiled database is used.\n\
115 For details on the format of these files, run `dircolors --print-database'.\n\
117 emit_bug_reporting_address ();
123 /* If the SHELL environment variable is set to `csh' or `tcsh,'
124 assume C shell. Else Bourne shell. */
126 static enum Shell_syntax
127 guess_shell_syntax (void)
131 shell = getenv ("SHELL");
132 if (shell == NULL || *shell == '\0')
133 return SHELL_SYNTAX_UNKNOWN;
135 shell = last_component (shell);
137 if (STREQ (shell, "csh") || STREQ (shell, "tcsh"))
138 return SHELL_SYNTAX_C;
140 return SHELL_SYNTAX_BOURNE;
144 parse_line (char const *line, char **keyword, char **arg)
147 char const *keyword_start;
148 char const *arg_start;
153 for (p = line; isspace (to_uchar (*p)); ++p)
156 /* Ignore blank lines and shell-style comments. */
157 if (*p == '\0' || *p == '#')
162 while (!isspace (to_uchar (*p)) && *p != '\0')
167 *keyword = xstrndup (keyword_start, p - keyword_start);
175 while (isspace (to_uchar (*p)));
177 if (*p == '\0' || *p == '#')
182 while (*p != '\0' && *p != '#')
185 for (--p; isspace (to_uchar (*p)); --p)
189 *arg = xstrndup (arg_start, p - arg_start);
192 /* FIXME: Write a string to standard out, while watching for "dangerous"
193 sequences like unescaped : and = characters. */
196 append_quoted (const char *str)
198 bool need_backslash = true;
208 need_backslash = true;
213 need_backslash = !need_backslash;
223 need_backslash = true;
232 /* Read the file open on FP (with name FILENAME). First, look for a
233 `TERM name' directive where name matches the current terminal type.
234 Once found, translate and accumulate the associated directives onto
235 the global obstack LSC_OBSTACK. Give a diagnostic
236 upon failure (unrecognized keyword is the only way to fail here).
237 Return true if successful. */
240 dc_parse_stream (FILE *fp, const char *filename)
242 size_t line_number = 0;
243 char const *next_G_line = G_line;
244 char *input_line = NULL;
245 size_t input_line_size = 0;
250 /* State for the parser. */
251 enum { ST_TERMNO, ST_TERMYES, ST_TERMSURE, ST_GLOBAL } state = ST_GLOBAL;
253 /* Get terminal type */
254 term = getenv ("TERM");
255 if (term == NULL || *term == '\0')
267 if (getline (&input_line, &input_line_size, fp) <= 0)
276 if (next_G_line == G_line + sizeof G_line)
279 next_G_line += strlen (next_G_line) + 1;
282 parse_line (line, &keywd, &arg);
289 error (0, 0, _("%s:%lu: invalid line; missing second token"),
290 filename, (unsigned long int) line_number);
296 unrecognized = false;
297 if (c_strcasecmp (keywd, "TERM") == 0)
299 if (STREQ (arg, term))
301 else if (state != ST_TERMSURE)
306 if (state == ST_TERMSURE)
307 state = ST_TERMYES; /* Another TERM can cancel */
309 if (state != ST_TERMNO)
314 append_quoted (keywd);
319 else if (keywd[0] == '*')
321 append_quoted (keywd);
326 else if (c_strcasecmp (keywd, "OPTIONS") == 0
327 || c_strcasecmp (keywd, "COLOR") == 0
328 || c_strcasecmp (keywd, "EIGHTBIT") == 0)
336 for (i = 0; slack_codes[i] != NULL; ++i)
337 if (c_strcasecmp (keywd, slack_codes[i]) == 0)
340 if (slack_codes[i] != NULL)
342 APPEND_TWO_CHAR_STRING (ls_codes[i]);
359 if (unrecognized && (state == ST_TERMSURE || state == ST_TERMYES))
361 error (0, 0, _("%s:%lu: unrecognized keyword %s"),
362 (filename ? quote (filename) : _("<internal>")),
363 (unsigned long int) line_number, keywd);
375 dc_parse_file (const char *filename)
379 if (! STREQ (filename, "-") && freopen (filename, "r", stdin) == NULL)
381 error (0, errno, "%s", filename);
385 ok = dc_parse_stream (stdin, filename);
387 if (fclose (stdin) != 0)
389 error (0, errno, "%s", quote (filename));
397 main (int argc, char **argv)
401 enum Shell_syntax syntax = SHELL_SYNTAX_UNKNOWN;
402 bool print_database = false;
404 initialize_main (&argc, &argv);
405 program_name = argv[0];
406 setlocale (LC_ALL, "");
407 bindtextdomain (PACKAGE, LOCALEDIR);
408 textdomain (PACKAGE);
410 atexit (close_stdout);
412 while ((optc = getopt_long (argc, argv, "bcp", long_options, NULL)) != -1)
415 case 'b': /* Bourne shell syntax. */
416 syntax = SHELL_SYNTAX_BOURNE;
419 case 'c': /* C shell syntax. */
420 syntax = SHELL_SYNTAX_C;
424 print_database = true;
427 case_GETOPT_HELP_CHAR;
429 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
432 usage (EXIT_FAILURE);
438 /* It doesn't make sense to use --print with either of
439 --bourne or --c-shell. */
440 if (print_database && syntax != SHELL_SYNTAX_UNKNOWN)
443 _("the options to output dircolors' internal database and\n\
444 to select a shell syntax are mutually exclusive"));
445 usage (EXIT_FAILURE);
448 if (!print_database < argc)
450 error (0, 0, _("extra operand %s"), quote (argv[!print_database]));
452 fprintf (stderr, "%s\n",
453 _("File operands cannot be combined with "
454 "--print-database (-p)."));
455 usage (EXIT_FAILURE);
460 char const *p = G_line;
461 while (p < G_line + sizeof G_line)
469 /* If shell syntax was not explicitly specified, try to guess it. */
470 if (syntax == SHELL_SYNTAX_UNKNOWN)
472 syntax = guess_shell_syntax ();
473 if (syntax == SHELL_SYNTAX_UNKNOWN)
475 error (EXIT_FAILURE, 0,
476 _("no SHELL environment variable, and no shell type option given"));
480 obstack_init (&lsc_obstack);
482 ok = dc_parse_stream (NULL, NULL);
484 ok = dc_parse_file (argv[0]);
488 size_t len = obstack_object_size (&lsc_obstack);
489 char *s = obstack_finish (&lsc_obstack);
493 if (syntax == SHELL_SYNTAX_BOURNE)
495 prefix = "LS_COLORS='";
496 suffix = "';\nexport LS_COLORS\n";
500 prefix = "setenv LS_COLORS '";
503 fputs (prefix, stdout);
504 fwrite (s, 1, len, stdout);
505 fputs (suffix, stdout);
509 exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);