summaryrefslogtreecommitdiff
path: root/src/dircolors.c
blob: 381f194dc38b37c66d2116d38d1514195dd6c85f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/* FIXME: accept, but ignore EIGHTBIT option
   FIXME: embed contents of default mapping file
   FIXME: add option to print that default mapping?

   Only distinction necessary: Bourne vs. C-shell
   Use obstack to accumulate rhs of setenv
 */
/* dircolors - parse a Slackware-style DIR_COLORS file.
   Copyright (C) 1994, 1995 H. Peter Anvin
   Copyright (C) 1996 Free Software Foundation, Inc.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <ctype.h>
#include <getopt.h>
#include <stdio.h>

#include "system.h"
#include "long-options.h"
#include "error.h"

char *xmalloc ();

#define USER_FILE ".dir_colors"	/* Versus user's home directory */
#define SYSTEM_FILE "DIR_COLORS" /* System-wide file in directory SYSTEM_DIR
				     (defined on the cc command line).  */

#define STRINGLEN 2048		/* Max length of a string */

enum modes { MO_SH, MO_CSH, MO_UNKNOWN, MO_ERR };

/* Parser needs these state variables.  */
enum states { ST_TERMNO, ST_TERMYES, ST_TERMSURE, ST_GLOBAL };

/* FIXME: associate with ls_codes? */
static const char *const slack_codes[] =
{
  "NORMAL", "NORM", "FILE", "DIR", "LNK", "LINK",
  "SYMLINK", "ORPHAN", "MISSING", "FIFO", "PIPE", "SOCK", "BLK", "BLOCK",
  "CHR", "CHAR", "EXEC", "LEFT", "LEFTCODE", "RIGHT", "RIGHTCODE", "END",
  "ENDCODE", NULL
};

static const char *const ls_codes[] =
{
  "no", "no", "fi", "di", "ln", "ln", "ln", "or", "mi", "pi", "pi",
  "so", "bd", "bd", "cd", "cd", "ex", "lc", "lc", "rc", "rc", "ec", "ec"
};

static struct option const long_options[] =
  {
    {"sh", no_argument, NULL, 'b'},
    {"csh", no_argument, NULL, 'c'},
    {"help", no_argument, NULL, 'h'},
    {"version", no_argument, NULL, 'v'},
  };

char *program_name;

static void
usage (int status)
{
  if (status != 0)
    fprintf (stderr, _("Try `%s --help' for more information.\n"),
             program_name);
  else
    {
      printf (_("Usage: %s [OPTION]... [FILE]\n"), program_name);
      printf (_("\
  -h, --help        display this help and exit\n\
      --version     output version information and exit\n\
Determine format of output:\n\
  -b, --sh          assume Bourne shell\n\
  -c, --csh         assume C-shell\n"));
    }

  exit (status);
}

/* If SHELL is csh or tcsh, assume C-shell.  Else Bourne shell.  */

static int
figure_mode (void)
{
  char *shell, *shellv;

  shellv = getenv ("SHELL");
  if (shellv == NULL || *shellv == '\0')
    return MO_UNKNOWN;

  shell = strrchr (shellv, '/');
  if (shell != NULL)
    ++shell;
  else
    shell = shellv;

  if (strcmp (shell, "csh") == 0
      || strcmp (shell, "tcsh") == 0)
    return MO_CSH;

  return MO_SH;
}

static void
parse_line (char **keyword, char **arg, char *line)
{
  char *p;

  *keyword = *arg = "";

  for (p = line; isspace (*p); ++p)
    ;

  if (*p == '\0' || *p == '#')
    return;

  *keyword = p;

  while (!isspace (*p))
    if (*p++ == '\0')
      return;

  *p++ = '\0';

  while (isspace (*p))
    ++p;

  if (*p == '\0' || *p == '#')
    return;

  *arg = p;

  while (*p != '\0' && *p != '#')
    ++p;
  for (--p; isspace (*p); --p)
    ;
  ++p;

  *p = '\0';
}

/* Write a string to standard out, while watching for "dangerous"
   sequences like unescaped : and = characters.  */

static void
put_seq (const char *str, char follow)
{
  int danger = 1;

  while (*str != '\0')
    {
      switch (*str)
	{
	case '\\':
	case '^':
	  danger = !danger;
	  break;

	case ':':
	case '=':
	  if (danger)
	    putchar ('\\');
	  /* Fall through */

	default:
	  danger = 1;
	  break;
	}

      putchar (*str++);
    }

  putchar (follow);		/* The character that ends the sequence.  */
}

int
main (int argc, char *argv[])
{
  char *p;
  int optc;
  int mode = MO_UNKNOWN;
  FILE *fp = NULL;
  char *term;
  int state;

  char line[STRINGLEN];
  char *keywd, *arg;

  char *input_file;

  program_name = argv[0];
  setlocale (LC_ALL, "");
  bindtextdomain (PACKAGE, LOCALEDIR);
  textdomain (PACKAGE);

  parse_long_options (argc, argv, "dircolors", PACKAGE_VERSION, usage);

  while ((optc = getopt_long (argc, argv, "bc", long_options, NULL))
	 != EOF)
    switch (optc)
      {
      case 'b':	/* Bourne shell mode.  */
	mode = MO_SH;
	break;

      case 'c':	/* Bourne shell mode.  */
	mode = MO_CSH;
	break;

      default:
	usage (1);
      }

  /* Use shell to determine mode, if not already done. */
  if (mode == MO_UNKNOWN)
    {
      mode = figure_mode ();
      if (mode == MO_UNKNOWN)
	{
	  error (1, 0, _("\
no SHELL environment variable, and no shell type option given"));
	}
    }

  /* Open dir_colors file */
  if (optind == argc)
    {
      p = getenv ("HOME");
      if (p != NULL && *p != '\0')
	{
	  /* Note: deliberate leak.  It's not worth freeing this.  */
	  input_file = xmalloc (strlen (p) + 1 + strlen (USER_FILE) + 1);
	  stpcpy (stpcpy (stpcpy (input_file, p), "/"), USER_FILE);
	  fp = fopen (input_file, "r");
	}

      if (fp == NULL)
	{
	  /* Note: deliberate leak.  It's not worth freeing this.  */
	  input_file = xmalloc (strlen (SHAREDIR) + 1
				+ strlen (USER_FILE) + 1);
	  stpcpy (stpcpy (stpcpy (input_file, SHAREDIR), "/"),
		  SYSTEM_FILE);
	  fp = fopen (input_file, "r");
	}
    }
  else
    {
      input_file = argv[optind];
      fp = fopen (input_file, "r");
    }

  if (fp == NULL)
    error (1, errno, _("while opening input file `%s'"), input_file);

  /* Get terminal type */
  term = getenv ("TERM");
  if (term == NULL || *term == '\0')
    term = "none";

  /* Write out common start */
  switch (mode)
    {
    case MO_CSH:
      puts ("set noglob;\nsetenv LS_COLORS \':");
      break;

    case MO_SH:
      fputs ("LS_COLORS=\'", stdout);
      break;
    }

  state = ST_GLOBAL;

  /* FIXME: use getline */
  while (fgets (line, STRINGLEN, fp) != NULL)
    {
      parse_line (&keywd, &arg, line);
      if (*keywd != '\0')
	{
	  if (strcasecmp (keywd, "TERM") == 0)
	    {
	      if (strcmp (arg, term) == 0)
		state = ST_TERMSURE;
	      else if (state != ST_TERMSURE)
		state = ST_TERMNO;
	    }
	  else
	    {
	      if (state == ST_TERMSURE)
		state = ST_TERMYES; /* Another TERM can cancel */

	      if (state != ST_TERMNO)
		{
		  if (keywd[0] == '.')
		    {
		      putchar ('*');
		      put_seq (keywd, '=');
		      put_seq (arg, ':');
		    }
		  else if (keywd[0] == '*')
		    {
		      put_seq (keywd, '=');
		      put_seq (arg, ':');
		    }
		  else if (strcasecmp (keywd, "OPTIONS") == 0)
		    {
		      /* Ignore.  */
		    }
		  else if (strcasecmp (keywd, "COLOR") == 0)
		    {
		       /* FIXME: ignored now.  */
		    }
		  else
		    {
		      int i;

		      for (i = 0; slack_codes[i] != NULL; ++i)
			if (strcasecmp (keywd, slack_codes[i]) == 0)
			  break;

		      if (slack_codes[i] != NULL)
			{
			  printf ("%s=", ls_codes[i]);
			  put_seq (arg, ':');
			}
		      else
			error (0, 0, _("Unknown keyword %s\n"), keywd);
		    }
		}
	    }
	}
    }

  fclose (fp);

  /* Write it out.  */
  switch (mode)
    {
    case MO_SH:
      printf ("\'\nexport LS_COLORS\n");
      break;

    case MO_CSH:
      printf ("\'\nunset noglob\n");
      break;

    default:
      abort ();
    }

  exit (0);
}