.
[coreutils.git] / src / dircolors.c
blob5d18f098cd402193bf9b39348f7c3aeb3def1cbd
1 /* dircolors - output commands to set the LS_COLOR environment variable
2 Copyright (C) 1996-2003 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 2, or (at your option)
8 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, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19 #if HAVE_CONFIG_H
20 # include <config.h>
21 #endif
23 #include <sys/types.h>
24 #include <getopt.h>
25 #include <stdio.h>
27 #include "system.h"
28 #include "dircolors.h"
29 #include "dirname.h"
30 #include "error.h"
31 #include "getline.h"
32 #include "obstack.h"
33 #include "quote.h"
34 #include "xstrndup.h"
36 /* The official name of this program (e.g., no `g' prefix). */
37 #define PROGRAM_NAME "dircolors"
39 #define AUTHORS "H. Peter Anvin"
41 #define obstack_chunk_alloc malloc
42 #define obstack_chunk_free free
44 enum Shell_syntax
46 SHELL_SYNTAX_BOURNE,
47 SHELL_SYNTAX_C,
48 SHELL_SYNTAX_UNKNOWN
51 #define APPEND_CHAR(C) obstack_1grow (&lsc_obstack, C)
52 #define APPEND_TWO_CHAR_STRING(S) \
53 do \
54 { \
55 APPEND_CHAR (S[0]); \
56 APPEND_CHAR (S[1]); \
57 } \
58 while (0)
60 /* Accumulate in this obstack the value for the LS_COLORS environment
61 variable. */
62 static struct obstack lsc_obstack;
64 /* Nonzero if the input file was the standard input. */
65 static int have_read_stdin;
67 /* FIXME: associate with ls_codes? */
68 static const char *const slack_codes[] =
70 "NORMAL", "NORM", "FILE", "DIR", "LNK", "LINK",
71 "SYMLINK", "ORPHAN", "MISSING", "FIFO", "PIPE", "SOCK", "BLK", "BLOCK",
72 "CHR", "CHAR", "DOOR", "EXEC", "LEFT", "LEFTCODE", "RIGHT", "RIGHTCODE",
73 "END", "ENDCODE", NULL
76 static const char *const ls_codes[] =
78 "no", "no", "fi", "di", "ln", "ln", "ln", "or", "mi", "pi", "pi",
79 "so", "bd", "bd", "cd", "cd", "do", "ex", "lc", "lc", "rc", "rc", "ec", "ec"
82 static struct option const long_options[] =
84 {"bourne-shell", no_argument, NULL, 'b'},
85 {"sh", no_argument, NULL, 'b'},
86 {"csh", no_argument, NULL, 'c'},
87 {"c-shell", no_argument, NULL, 'c'},
88 {"print-database", no_argument, NULL, 'p'},
89 {GETOPT_HELP_OPTION_DECL},
90 {GETOPT_VERSION_OPTION_DECL},
91 {NULL, 0, NULL, 0}
94 char *program_name;
96 void
97 usage (int status)
99 if (status != 0)
100 fprintf (stderr, _("Try `%s --help' for more information.\n"),
101 program_name);
102 else
104 printf (_("Usage: %s [OPTION]... [FILE]\n"), program_name);
105 fputs (_("\
106 Output commands to set the LS_COLORS environment variable.\n\
108 Determine format of output:\n\
109 -b, --sh, --bourne-shell output Bourne shell code to set LS_COLORS\n\
110 -c, --csh, --c-shell output C shell code to set LS_COLORS\n\
111 -p, --print-database output defaults\n\
112 "), stdout);
113 fputs (HELP_OPTION_DESCRIPTION, stdout);
114 fputs (VERSION_OPTION_DESCRIPTION, stdout);
115 fputs (_("\
117 If FILE is specified, read it to determine which colors to use for which\n\
118 file types and extensions. Otherwise, a precompiled database is used.\n\
119 For details on the format of these files, run `dircolors --print-database'.\n\
120 "), stdout);
121 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
124 exit (status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
127 /* If the SHELL environment variable is set to `csh' or `tcsh,'
128 assume C shell. Else Bourne shell. */
130 static enum Shell_syntax
131 guess_shell_syntax (void)
133 char *shell;
135 shell = getenv ("SHELL");
136 if (shell == NULL || *shell == '\0')
137 return SHELL_SYNTAX_UNKNOWN;
139 shell = base_name (shell);
141 if (STREQ (shell, "csh") || STREQ (shell, "tcsh"))
142 return SHELL_SYNTAX_C;
144 return SHELL_SYNTAX_BOURNE;
147 static void
148 parse_line (unsigned char const *line, char **keyword, char **arg)
150 unsigned char const *p;
151 unsigned char const *keyword_start;
152 unsigned char const *arg_start;
154 *keyword = NULL;
155 *arg = NULL;
157 for (p = line; ISSPACE (*p); ++p)
160 /* Ignore blank lines and shell-style comments. */
161 if (*p == '\0' || *p == '#')
162 return;
164 keyword_start = p;
166 while (!ISSPACE (*p) && *p != '\0')
168 ++p;
171 *keyword = xstrndup ((const char *) keyword_start, p - keyword_start);
172 if (*p == '\0')
173 return;
177 ++p;
179 while (ISSPACE (*p));
181 if (*p == '\0' || *p == '#')
182 return;
184 arg_start = p;
186 while (*p != '\0' && *p != '#')
187 ++p;
189 for (--p; ISSPACE (*p); --p)
191 /* empty */
193 ++p;
195 *arg = xstrndup ((const char *) arg_start, p - arg_start);
198 /* FIXME: Write a string to standard out, while watching for "dangerous"
199 sequences like unescaped : and = characters. */
201 static void
202 append_quoted (const char *str)
204 int need_backslash = 1;
206 while (*str != '\0')
208 switch (*str)
210 case '\\':
211 case '^':
212 need_backslash = !need_backslash;
213 break;
215 case ':':
216 case '=':
217 if (need_backslash)
218 APPEND_CHAR ('\\');
219 /* Fall through */
221 default:
222 need_backslash = 1;
223 break;
226 APPEND_CHAR (*str);
227 ++str;
231 /* Read the file open on FP (with name FILENAME). First, look for a
232 `TERM name' directive where name matches the current terminal type.
233 Once found, translate and accumulate the associated directives onto
234 the global obstack LSC_OBSTACK. Give a diagnostic and return nonzero
235 upon failure (unrecognized keyword is the only way to fail here).
236 Return zero otherwise. */
238 static int
239 dc_parse_stream (FILE *fp, const char *filename)
241 size_t line_number = 0;
242 char *line = NULL;
243 size_t line_chars_allocated = 0;
244 int state;
245 char *term;
246 int err = 0;
248 /* State for the parser. */
249 enum states { ST_TERMNO, ST_TERMYES, ST_TERMSURE, ST_GLOBAL };
251 state = ST_GLOBAL;
253 /* Get terminal type */
254 term = getenv ("TERM");
255 if (term == NULL || *term == '\0')
256 term = "none";
258 while (1)
260 int line_length;
261 char *keywd, *arg;
262 int unrecognized;
264 ++line_number;
266 if (fp)
268 line_length = getline (&line, &line_chars_allocated, fp);
269 if (line_length <= 0)
271 if (line)
272 free (line);
273 break;
276 else
278 line = (char *) (G_line[line_number - 1]);
279 line_length = G_line_length[line_number - 1];
280 if (line_number > G_N_LINES)
281 break;
284 parse_line ((unsigned char *) line, &keywd, &arg);
286 if (keywd == NULL)
287 continue;
289 if (arg == NULL)
291 error (0, 0, _("%s:%lu: invalid line; missing second token"),
292 filename, (long unsigned) line_number);
293 err = 1;
294 free (keywd);
295 continue;
298 unrecognized = 0;
299 if (strcasecmp (keywd, "TERM") == 0)
301 if (STREQ (arg, term))
302 state = ST_TERMSURE;
303 else if (state != ST_TERMSURE)
304 state = ST_TERMNO;
306 else
308 if (state == ST_TERMSURE)
309 state = ST_TERMYES; /* Another TERM can cancel */
311 if (state != ST_TERMNO)
313 if (keywd[0] == '.')
315 APPEND_CHAR ('*');
316 append_quoted (keywd);
317 APPEND_CHAR ('=');
318 append_quoted (arg);
319 APPEND_CHAR (':');
321 else if (keywd[0] == '*')
323 append_quoted (keywd);
324 APPEND_CHAR ('=');
325 append_quoted (arg);
326 APPEND_CHAR (':');
328 else if (strcasecmp (keywd, "OPTIONS") == 0
329 || strcasecmp (keywd, "COLOR") == 0
330 || strcasecmp (keywd, "EIGHTBIT") == 0)
332 /* Ignore. */
334 else
336 int i;
338 for (i = 0; slack_codes[i] != NULL; ++i)
339 if (strcasecmp (keywd, slack_codes[i]) == 0)
340 break;
342 if (slack_codes[i] != NULL)
344 APPEND_TWO_CHAR_STRING (ls_codes[i]);
345 APPEND_CHAR ('=');
346 append_quoted (arg);
347 APPEND_CHAR (':');
349 else
351 unrecognized = 1;
355 else
357 unrecognized = 1;
361 if (unrecognized && (state == ST_TERMSURE || state == ST_TERMYES))
363 error (0, 0, _("%s:%lu: unrecognized keyword %s"),
364 (filename ? quote (filename) : _("<internal>")),
365 (long unsigned) line_number, keywd);
366 err = 1;
369 free (keywd);
370 if (arg)
371 free (arg);
374 return err;
377 static int
378 dc_parse_file (const char *filename)
380 FILE *fp;
381 int err;
383 if (STREQ (filename, "-"))
385 have_read_stdin = 1;
386 fp = stdin;
388 else
390 /* OPENOPTS is a macro. It varies with the system.
391 Some systems distinguish between internal and
392 external text representations. */
394 fp = fopen (filename, "r");
395 if (fp == NULL)
397 error (0, errno, "%s", quote (filename));
398 return 1;
402 err = dc_parse_stream (fp, filename);
404 if (fp != stdin && fclose (fp) == EOF)
406 error (0, errno, "%s", quote (filename));
407 return 1;
410 return err;
414 main (int argc, char **argv)
416 int err = 0;
417 int optc;
418 enum Shell_syntax syntax = SHELL_SYNTAX_UNKNOWN;
419 int print_database = 0;
421 initialize_main (&argc, &argv);
422 program_name = argv[0];
423 setlocale (LC_ALL, "");
424 bindtextdomain (PACKAGE, LOCALEDIR);
425 textdomain (PACKAGE);
427 atexit (close_stdout);
429 while ((optc = getopt_long (argc, argv, "bcp", long_options, NULL)) != -1)
430 switch (optc)
432 case 'b': /* Bourne shell syntax. */
433 syntax = SHELL_SYNTAX_BOURNE;
434 break;
436 case 'c': /* C shell syntax. */
437 syntax = SHELL_SYNTAX_C;
438 break;
440 case 'p':
441 print_database = 1;
442 break;
444 case_GETOPT_HELP_CHAR;
446 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
448 default:
449 usage (EXIT_FAILURE);
452 argc -= optind;
453 argv += optind;
455 /* It doesn't make sense to use --print with either of
456 --bourne or --c-shell. */
457 if (print_database && syntax != SHELL_SYNTAX_UNKNOWN)
459 error (0, 0,
460 _("the options to output dircolors' internal database and\n\
461 to select a shell syntax are mutually exclusive"));
462 usage (EXIT_FAILURE);
465 if (print_database && argc > 0)
467 error (0, 0,
468 _("no FILE arguments may be used with the option to output\n\
469 dircolors' internal database"));
470 usage (EXIT_FAILURE);
473 if (!print_database && argc > 1)
475 error (0, 0, _("too many arguments"));
476 usage (EXIT_FAILURE);
479 if (print_database)
481 int i;
482 for (i = 0; i < G_N_LINES; i++)
484 fwrite (G_line[i], 1, G_line_length[i], stdout);
485 fputc ('\n', stdout);
488 else
490 /* If shell syntax was not explicitly specified, try to guess it. */
491 if (syntax == SHELL_SYNTAX_UNKNOWN)
493 syntax = guess_shell_syntax ();
494 if (syntax == SHELL_SYNTAX_UNKNOWN)
496 error (EXIT_FAILURE, 0,
497 _("no SHELL environment variable, and no shell type option given"));
501 obstack_init (&lsc_obstack);
502 if (argc == 0)
503 err = dc_parse_stream (NULL, NULL);
504 else
505 err = dc_parse_file (argv[0]);
507 if (!err)
509 size_t len = obstack_object_size (&lsc_obstack);
510 char *s = obstack_finish (&lsc_obstack);
511 const char *prefix;
512 const char *suffix;
514 if (syntax == SHELL_SYNTAX_BOURNE)
516 prefix = "LS_COLORS='";
517 suffix = "';\nexport LS_COLORS\n";
519 else
521 prefix = "setenv LS_COLORS '";
522 suffix = "'\n";
524 fputs (prefix, stdout);
525 fwrite (s, 1, len, stdout);
526 fputs (suffix, stdout);
531 if (have_read_stdin && fclose (stdin) == EOF)
532 error (EXIT_FAILURE, errno, _("standard input"));
534 exit (err == 0 ? EXIT_SUCCESS : EXIT_FAILURE);