1 /* dircolors - output commands to set the LS_COLOR environment variable
2 Copyright (C) 1994, 1995, 1997, 1998 H. Peter Anvin
3 Copyright (C) 96, 97, 1998 Free Software Foundation, Inc.
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)
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
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 #include <sys/types.h>
30 #include "long-options.h"
34 #include "dircolors.h"
36 #define obstack_chunk_alloc malloc
37 #define obstack_chunk_free free
49 #define APPEND_CHAR(C) obstack_1grow (&lsc_obstack, C)
50 #define APPEND_TWO_CHAR_STRING(S) \
58 /* Accumulate in this obstack the value for the LS_COLORS environment
60 static struct obstack lsc_obstack
;
62 /* Nonzero if the input file was the standard input. */
63 static int have_read_stdin
;
65 /* FIXME: associate with ls_codes? */
66 static const char *const slack_codes
[] =
68 "NORMAL", "NORM", "FILE", "DIR", "LNK", "LINK",
69 "SYMLINK", "ORPHAN", "MISSING", "FIFO", "PIPE", "SOCK", "BLK", "BLOCK",
70 "CHR", "CHAR", "EXEC", "LEFT", "LEFTCODE", "RIGHT", "RIGHTCODE", "END",
74 static const char *const ls_codes
[] =
76 "no", "no", "fi", "di", "ln", "ln", "ln", "or", "mi", "pi", "pi",
77 "so", "bd", "bd", "cd", "cd", "ex", "lc", "lc", "rc", "rc", "ec", "ec"
80 static struct option
const long_options
[] =
82 {"bourne-shell", no_argument
, NULL
, 'b'},
83 {"sh", no_argument
, NULL
, 'b'},
84 {"csh", no_argument
, NULL
, 'c'},
85 {"c-shell", no_argument
, NULL
, 'c'},
86 {"help", no_argument
, NULL
, 'h'},
87 {"print-database", no_argument
, NULL
, 'p'},
88 {"version", no_argument
, NULL
, 'v'},
98 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
102 printf (_("Usage: %s [OPTION]... [FILE]\n"), program_name
);
104 Output commands to set the LS_COLORS environment variable.\n\
106 Determine format of output:\n\
107 -b, --sh, --bourne-shell output Bourne shell code to set LS_COLORS\n\
108 -c, --csh, --c-shell output C shell code to set LS_COLORS\n\
109 -p, --print-database output defaults\n\
110 --help display this help and exit\n\
111 --version output version information and exit\n\
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 puts (_("\nReport bugs to <fileutils-bugs@gnu.org>."));
121 exit (status
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
);
125 xstrndup (const char *s
, size_t n
)
127 char *new = strndup (s
, n
);
129 error (EXIT_FAILURE
, 0, _("virtual memory exhausted"));
133 /* If the SHELL environment variable is set to `csh' or `tcsh,'
134 assume C shell. Else Bourne shell. */
136 static enum Shell_syntax
137 guess_shell_syntax (void)
141 shell
= getenv ("SHELL");
142 if (shell
== NULL
|| *shell
== '\0')
143 return SHELL_SYNTAX_UNKNOWN
;
145 shell
= base_name (shell
);
147 if (STREQ (shell
, "csh") || STREQ (shell
, "tcsh"))
148 return SHELL_SYNTAX_C
;
150 return SHELL_SYNTAX_BOURNE
;
154 parse_line (unsigned char const *line
, char **keyword
, char **arg
)
156 unsigned char const *p
;
157 unsigned char const *keyword_start
;
158 unsigned char const *arg_start
;
163 for (p
= line
; ISSPACE (*p
); ++p
)
166 /* Ignore blank lines and shell-style comments. */
167 if (*p
== '\0' || *p
== '#')
172 while (!ISSPACE (*p
) && *p
!= '\0')
177 *keyword
= xstrndup ((const char *) keyword_start
, p
- keyword_start
);
185 while (ISSPACE (*p
));
187 if (*p
== '\0' || *p
== '#')
192 while (*p
!= '\0' && *p
!= '#')
195 for (--p
; ISSPACE (*p
); --p
)
201 *arg
= xstrndup ((const char *) arg_start
, p
- arg_start
);
204 /* FIXME: Write a string to standard out, while watching for "dangerous"
205 sequences like unescaped : and = characters. */
208 append_quoted (const char *str
)
210 int need_backslash
= 1;
218 need_backslash
= !need_backslash
;
237 /* Read the file open on FP (with name FILENAME). First, look for a
238 `TERM name' directive where name matches the current terminal type.
239 Once found, translate and accumulate the associated directives onto
240 the global obstack LSC_OBSTACK. Give a diagnostic and return nonzero
241 upon failure (unrecognized keyword is the only way to fail here).
242 Return zero otherwise. */
245 dc_parse_stream (FILE *fp
, const char *filename
)
247 size_t line_number
= 0;
249 size_t line_chars_allocated
= 0;
254 /* State for the parser. */
255 enum states
{ ST_TERMNO
, ST_TERMYES
, ST_TERMSURE
, ST_GLOBAL
};
259 /* Get terminal type */
260 term
= getenv ("TERM");
261 if (term
== NULL
|| *term
== '\0')
274 line_length
= getline (&line
, &line_chars_allocated
, fp
);
275 if (line_length
<= 0)
284 line
= (char *) (G_line
[line_number
- 1]);
285 line_length
= G_line_length
[line_number
- 1];
286 if (line_number
> G_N_LINES
)
290 parse_line ((unsigned char *) line
, &keywd
, &arg
);
296 if (strcasecmp (keywd
, "TERM") == 0)
298 if (STREQ (arg
, term
))
300 else if (state
!= ST_TERMSURE
)
305 if (state
== ST_TERMSURE
)
306 state
= ST_TERMYES
; /* Another TERM can cancel */
308 if (state
!= ST_TERMNO
)
313 append_quoted (keywd
);
318 else if (keywd
[0] == '*')
320 append_quoted (keywd
);
325 else if (strcasecmp (keywd
, "OPTIONS") == 0
326 || strcasecmp (keywd
, "COLOR") == 0
327 || strcasecmp (keywd
, "EIGHTBIT") == 0)
335 for (i
= 0; slack_codes
[i
] != NULL
; ++i
)
336 if (strcasecmp (keywd
, slack_codes
[i
]) == 0)
339 if (slack_codes
[i
] != NULL
)
341 APPEND_TWO_CHAR_STRING (ls_codes
[i
]);
358 if (unrecognized
&& (state
== ST_TERMSURE
|| state
== ST_TERMYES
))
360 error (0, 0, _("%s:%lu: unrecognized keyword `%s'"),
361 filename
, (long unsigned) line_number
, keywd
);
374 dc_parse_file (const char *filename
)
379 if (STREQ (filename
, "-"))
386 /* OPENOPTS is a macro. It varies with the system.
387 Some systems distinguish between internal and
388 external text representations. */
390 fp
= fopen (filename
, "r");
393 error (0, errno
, "%s", filename
);
398 err
= dc_parse_stream (fp
, filename
);
400 if (fp
!= stdin
&& fclose (fp
) == EOF
)
402 error (0, errno
, "%s", filename
);
410 main (int argc
, char **argv
)
414 enum Shell_syntax syntax
= SHELL_SYNTAX_UNKNOWN
;
415 int print_database
= 0;
417 program_name
= argv
[0];
418 setlocale (LC_ALL
, "");
419 bindtextdomain (PACKAGE
, LOCALEDIR
);
420 textdomain (PACKAGE
);
422 parse_long_options (argc
, argv
, "dircolors", GNU_PACKAGE
, VERSION
, usage
);
424 while ((optc
= getopt_long (argc
, argv
, "bcp", long_options
, NULL
)) != -1)
427 case 'b': /* Bourne shell syntax. */
428 syntax
= SHELL_SYNTAX_BOURNE
;
431 case 'c': /* C shell syntax. */
432 syntax
= SHELL_SYNTAX_C
;
446 /* It doesn't make sense to use --print with either of
447 --bourne or --c-shell. */
448 if (print_database
&& syntax
!= SHELL_SYNTAX_UNKNOWN
)
451 _("the options to output dircolors' internal database and\n\
452 to select a shell syntax are mutually exclusive"));
456 if (print_database
&& argc
> 0)
459 _("no FILE arguments may be used with the option to output\n\
460 dircolors' internal database"));
464 if (!print_database
&& argc
> 1)
466 error (0, 0, _("too many arguments"));
473 for (i
= 0; i
< G_N_LINES
; i
++)
475 fwrite (G_line
[i
], 1, G_line_length
[i
], stdout
);
476 fputc ('\n', stdout
);
481 /* If shell syntax was not explicitly specified, try to guess it. */
482 if (syntax
== SHELL_SYNTAX_UNKNOWN
)
484 syntax
= guess_shell_syntax ();
485 if (syntax
== SHELL_SYNTAX_UNKNOWN
)
487 error (EXIT_FAILURE
, 0,
488 _("no SHELL environment variable, and no shell type option given"));
492 obstack_init (&lsc_obstack
);
494 err
= dc_parse_stream (NULL
, NULL
);
496 err
= dc_parse_file (argv
[0]);
500 size_t len
= obstack_object_size (&lsc_obstack
);
501 char *s
= obstack_finish (&lsc_obstack
);
505 if (syntax
== SHELL_SYNTAX_BOURNE
)
507 prefix
= "LS_COLORS='";
508 suffix
= "';\nexport LS_COLORS\n";
512 prefix
= "setenv LS_COLORS '";
515 fputs (prefix
, stdout
);
516 fwrite (s
, 1, len
, stdout
);
517 fputs (suffix
, stdout
);
523 if (have_read_stdin
&& fclose (stdin
) == EOF
)
524 error (EXIT_FAILURE
, errno
, _("standard input"));
526 exit (err
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
);