1 /* dircolors - output commands to set the LS_COLOR environment variable
2 Copyright (C) 1996-2005 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)
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
23 #include <sys/types.h>
28 #include "dircolors.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
51 #define APPEND_CHAR(C) obstack_1grow (&lsc_obstack, C)
52 #define APPEND_TWO_CHAR_STRING(S) \
60 /* Accumulate in this obstack the value for the LS_COLORS environment
62 static struct obstack lsc_obstack
;
64 /* FIXME: associate with ls_codes? */
65 static const char *const slack_codes
[] =
67 "NORMAL", "NORM", "FILE", "DIR", "LNK", "LINK",
68 "SYMLINK", "ORPHAN", "MISSING", "FIFO", "PIPE", "SOCK", "BLK", "BLOCK",
69 "CHR", "CHAR", "DOOR", "EXEC", "LEFT", "LEFTCODE", "RIGHT", "RIGHTCODE",
70 "END", "ENDCODE", "SUID", "SETUID", "SGID", "SETGID", "STICKY",
71 "OTHER_WRITABLE", "OWR", "STICKY_OTHER_WRITABLE", "OWT", NULL
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", "do", "ex", "lc", "lc", "rc", "rc", "ec", "ec"
78 "su", "su", "sg", "sg", "st", "ow", "ow", "tw", "tw", NULL
81 static struct option
const long_options
[] =
83 {"bourne-shell", no_argument
, NULL
, 'b'},
84 {"sh", no_argument
, NULL
, 'b'},
85 {"csh", no_argument
, NULL
, 'c'},
86 {"c-shell", no_argument
, NULL
, 'c'},
87 {"print-database", no_argument
, NULL
, 'p'},
88 {GETOPT_HELP_OPTION_DECL
},
89 {GETOPT_VERSION_OPTION_DECL
},
98 if (status
!= EXIT_SUCCESS
)
99 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
103 printf (_("Usage: %s [OPTION]... [FILE]\n"), program_name
);
105 Output commands to set the LS_COLORS environment variable.\n\
107 Determine format of output:\n\
108 -b, --sh, --bourne-shell output Bourne shell code to set LS_COLORS\n\
109 -c, --csh, --c-shell output C shell code to set LS_COLORS\n\
110 -p, --print-database output defaults\n\
112 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
113 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
116 If FILE is specified, read it to determine which colors to use for which\n\
117 file types and extensions. Otherwise, a precompiled database is used.\n\
118 For details on the format of these files, run `dircolors --print-database'.\n\
120 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT
);
126 /* If the SHELL environment variable is set to `csh' or `tcsh,'
127 assume C shell. Else Bourne shell. */
129 static enum Shell_syntax
130 guess_shell_syntax (void)
134 shell
= getenv ("SHELL");
135 if (shell
== NULL
|| *shell
== '\0')
136 return SHELL_SYNTAX_UNKNOWN
;
138 shell
= base_name (shell
);
140 if (STREQ (shell
, "csh") || STREQ (shell
, "tcsh"))
141 return SHELL_SYNTAX_C
;
143 return SHELL_SYNTAX_BOURNE
;
147 parse_line (char const *line
, char **keyword
, char **arg
)
150 char const *keyword_start
;
151 char const *arg_start
;
156 for (p
= line
; ISSPACE (to_uchar (*p
)); ++p
)
159 /* Ignore blank lines and shell-style comments. */
160 if (*p
== '\0' || *p
== '#')
165 while (!ISSPACE (to_uchar (*p
)) && *p
!= '\0')
170 *keyword
= xstrndup (keyword_start
, p
- keyword_start
);
178 while (ISSPACE (to_uchar (*p
)));
180 if (*p
== '\0' || *p
== '#')
185 while (*p
!= '\0' && *p
!= '#')
188 for (--p
; ISSPACE (to_uchar (*p
)); --p
)
194 *arg
= xstrndup (arg_start
, p
- arg_start
);
197 /* FIXME: Write a string to standard out, while watching for "dangerous"
198 sequences like unescaped : and = characters. */
201 append_quoted (const char *str
)
203 bool need_backslash
= true;
211 need_backslash
= !need_backslash
;
221 need_backslash
= true;
230 /* Read the file open on FP (with name FILENAME). First, look for a
231 `TERM name' directive where name matches the current terminal type.
232 Once found, translate and accumulate the associated directives onto
233 the global obstack LSC_OBSTACK. Give a diagnostic
234 upon failure (unrecognized keyword is the only way to fail here).
235 Return true if successful. */
238 dc_parse_stream (FILE *fp
, const char *filename
)
240 size_t line_number
= 0;
241 char const *next_G_line
= G_line
;
242 char *input_line
= NULL
;
243 size_t input_line_size
= 0;
248 /* State for the parser. */
249 enum { ST_TERMNO
, ST_TERMYES
, ST_TERMSURE
, ST_GLOBAL
} state
= ST_GLOBAL
;
251 /* Get terminal type */
252 term
= getenv ("TERM");
253 if (term
== NULL
|| *term
== '\0')
265 if (getline (&input_line
, &input_line_size
, fp
) <= 0)
274 if (next_G_line
== G_line
+ sizeof G_line
)
277 next_G_line
+= strlen (next_G_line
) + 1;
280 parse_line (line
, &keywd
, &arg
);
287 error (0, 0, _("%s:%lu: invalid line; missing second token"),
288 filename
, (unsigned long int) line_number
);
294 unrecognized
= false;
295 if (strcasecmp (keywd
, "TERM") == 0)
297 if (STREQ (arg
, term
))
299 else if (state
!= ST_TERMSURE
)
304 if (state
== ST_TERMSURE
)
305 state
= ST_TERMYES
; /* Another TERM can cancel */
307 if (state
!= ST_TERMNO
)
312 append_quoted (keywd
);
317 else if (keywd
[0] == '*')
319 append_quoted (keywd
);
324 else if (strcasecmp (keywd
, "OPTIONS") == 0
325 || strcasecmp (keywd
, "COLOR") == 0
326 || strcasecmp (keywd
, "EIGHTBIT") == 0)
334 for (i
= 0; slack_codes
[i
] != NULL
; ++i
)
335 if (strcasecmp (keywd
, slack_codes
[i
]) == 0)
338 if (slack_codes
[i
] != NULL
)
340 APPEND_TWO_CHAR_STRING (ls_codes
[i
]);
357 if (unrecognized
&& (state
== ST_TERMSURE
|| state
== ST_TERMYES
))
359 error (0, 0, _("%s:%lu: unrecognized keyword %s"),
360 (filename
? quote (filename
) : _("<internal>")),
361 (unsigned long int) line_number
, keywd
);
373 dc_parse_file (const char *filename
)
377 if (! STREQ (filename
, "-") && freopen (filename
, "r", stdin
) == NULL
)
379 error (0, errno
, "%s", filename
);
383 ok
= dc_parse_stream (stdin
, filename
);
385 if (fclose (stdin
) != 0)
387 error (0, errno
, "%s", quote (filename
));
395 main (int argc
, char **argv
)
399 enum Shell_syntax syntax
= SHELL_SYNTAX_UNKNOWN
;
400 bool print_database
= false;
402 initialize_main (&argc
, &argv
);
403 program_name
= argv
[0];
404 setlocale (LC_ALL
, "");
405 bindtextdomain (PACKAGE
, LOCALEDIR
);
406 textdomain (PACKAGE
);
408 atexit (close_stdout
);
410 while ((optc
= getopt_long (argc
, argv
, "bcp", long_options
, NULL
)) != -1)
413 case 'b': /* Bourne shell syntax. */
414 syntax
= SHELL_SYNTAX_BOURNE
;
417 case 'c': /* C shell syntax. */
418 syntax
= SHELL_SYNTAX_C
;
422 print_database
= true;
425 case_GETOPT_HELP_CHAR
;
427 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
430 usage (EXIT_FAILURE
);
436 /* It doesn't make sense to use --print with either of
437 --bourne or --c-shell. */
438 if (print_database
&& syntax
!= SHELL_SYNTAX_UNKNOWN
)
441 _("the options to output dircolors' internal database and\n\
442 to select a shell syntax are mutually exclusive"));
443 usage (EXIT_FAILURE
);
446 if (!print_database
< argc
)
448 error (0, 0, _("extra operand %s"), quote (argv
[!print_database
]));
450 fprintf (stderr
, "%s\n",
451 _("File operands cannot be combined with "
452 "--print-database (-p)."));
453 usage (EXIT_FAILURE
);
458 char const *p
= G_line
;
459 while (p
< G_line
+ sizeof G_line
)
467 /* If shell syntax was not explicitly specified, try to guess it. */
468 if (syntax
== SHELL_SYNTAX_UNKNOWN
)
470 syntax
= guess_shell_syntax ();
471 if (syntax
== SHELL_SYNTAX_UNKNOWN
)
473 error (EXIT_FAILURE
, 0,
474 _("no SHELL environment variable, and no shell type option given"));
478 obstack_init (&lsc_obstack
);
480 ok
= dc_parse_stream (NULL
, NULL
);
482 ok
= dc_parse_file (argv
[0]);
486 size_t len
= obstack_object_size (&lsc_obstack
);
487 char *s
= obstack_finish (&lsc_obstack
);
491 if (syntax
== SHELL_SYNTAX_BOURNE
)
493 prefix
= "LS_COLORS='";
494 suffix
= "';\nexport LS_COLORS\n";
498 prefix
= "setenv LS_COLORS '";
501 fputs (prefix
, stdout
);
502 fwrite (s
, 1, len
, stdout
);
503 fputs (suffix
, stdout
);
507 exit (ok
? EXIT_SUCCESS
: EXIT_FAILURE
);