summaryrefslogtreecommitdiff
path: root/src/cut.c
blob: c53402f63333fc99bbd19eb4bdef2d29a41b41f1 (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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
/* cut - remove parts of lines of files
   Copyright (C) 1984 by David M. Ihnat

   This program is a total rewrite of the Bell Laboratories Unix(Tm)
   command of the same name, as of System V.  It contains no proprietary
   code, and therefore may be used without violation of any proprietary
   agreements whatsoever.  However, you will notice that the program is
   copyrighted by me.  This is to assure the program does *not* fall
   into the public domain.  Thus, I may specify just what I am now:
   This program may be freely copied and distributed, provided this notice
   remains; it may not be sold for profit without express written consent of
   the author.
   Please note that I recreated the behavior of the Unix(Tm) 'cut' command
   as faithfully as possible; however, I haven't run a full set of regression
   tests.  Thus, the user of this program accepts full responsibility for any
   effects or loss; in particular, the author is not responsible for any losses,
   explicit or incidental, that may be incurred through use of this program.

   I ask that any bugs (and, if possible, fixes) be reported to me when
   possible.  -David Ihnat (312) 784-4544 ignatz@homebru.chi.il.us

   POSIX changes, bug fixes, long-named options, and cleanup
   by David MacKenzie <djm@gnu.ai.mit.edu>.

   Options:
   --bytes=byte-list
   -b byte-list			Print only the bytes in positions listed
				in BYTE-LIST.
				Tabs and backspaces are treated like any
				other character; they take up 1 byte.

   --characters=character-list
   -c character-list		Print only characters in positions listed
				in CHARACTER-LIST.
				The same as -b for now, but
				internationalization will change that.
				Tabs and backspaces are treated like any
				other character; they take up 1 character.

   --fields=field-list
   -f field-list		Print only the fields listed in FIELD-LIST.
				Fields are separated by a TAB by default.

   --delimiter=delim
   -d delim			For -f, fields are separated by the first
				character in DELIM instead of TAB.

   -n				Do not split multibyte chars (no-op for now).

   --only-delimited
   -s				For -f, do not print lines that do not contain
				the field separator character.

   The BYTE-LIST, CHARACTER-LIST, and FIELD-LIST are one or more numbers
   or ranges separated by commas.  The first byte, character, and field
   are numbered 1.

   A FILE of `-' means standard input. */

#ifdef HAVE_CONFIG_H
#if defined (CONFIG_BROKETS)
/* We use <config.h> instead of "config.h" so that a compilation
   using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
   (which it would do because it found this file in $srcdir).  */
#include <config.h>
#else
#include "config.h"
#endif
#endif

/* Get isblank from GNU libc.  */
#define _GNU_SOURCE

#include <stdio.h>
#include <getopt.h>
#include <sys/types.h>
#include "system.h"
#include "version.h"

char *xmalloc ();
char *xrealloc ();
void error ();

static int set_fields ();
static int cut_file ();
static void cut_stream ();
static void cut_bytes ();
static void cut_fields ();
static void enlarge_line ();
static void invalid_list ();
static void usage ();

/* The number of elements allocated for the input line
   and the byte or field number.
   Enlarged as necessary. */
static int line_size;

/* Processed output buffer. */
static char *outbuf;

/* Where to save next char to output. */
static char *outbufptr;

/* Raw line buffer for field mode. */
static char *inbuf;

/* Where to save next input char. */
static char *inbufptr;

/* What can be done about a byte or field. */
enum field_action
{
  field_omit,
  field_output
};

/* In byte mode, which bytes to output.
   In field mode, which `delim'-separated fields to output.
   Both bytes and fields are numbered starting with 1,
   so the first element of `fields' is unused. */
static enum field_action *fields;

enum operating_mode
{
  undefined_mode,

  /* Output characters that are in the given bytes. */
  byte_mode,

  /* Output the given delimeter-separated fields. */
  field_mode
};

/* The name this program was run with. */
char *program_name;

static enum operating_mode operating_mode;

/* If nonzero,
   for field mode, do not output lines containing no delimeter characters. */
static int delimited_lines_only;

/* The delimeter character for field mode. */
static unsigned char delim;

/* Nonzero if we have ever read standard input. */
static int have_read_stdin;

/* If non-zero, display usage information and exit.  */
static int show_help;

/* If non-zero, print the version on standard output then exit.  */
static int show_version;

static struct option const longopts[] =
{
  {"bytes", required_argument, 0, 'b'},
  {"characters", required_argument, 0, 'c'},
  {"fields", required_argument, 0, 'f'},
  {"delimiter", required_argument, 0, 'd'},
  {"only-delimited", no_argument, 0, 's'},
  {"help", no_argument, &show_help, 1},
  {"version", no_argument, &show_version, 1},
  {0, 0, 0, 0}
};

void
main (argc, argv)
     int argc;
     char **argv;
{
  int optc, exit_status = 0;

  program_name = argv[0];

  line_size = 512;
  operating_mode = undefined_mode;
  delimited_lines_only = 0;
  delim = '\0';
  have_read_stdin = 0;

  fields = (enum field_action *)
    xmalloc (line_size * sizeof (enum field_action));
  outbuf = (char *) xmalloc (line_size);
  inbuf = (char *) xmalloc (line_size);

  for (optc = 0; optc < line_size; optc++)
    fields[optc] = field_omit;

  while ((optc = getopt_long (argc, argv, "b:c:d:f:ns", longopts, (int *) 0))
	 != EOF)
    {
      switch (optc)
	{
	case 0:
	  break;

	case 'b':
	case 'c':
	  /* Build the byte list. */
	  if (operating_mode != undefined_mode)
	    usage (2);
	  operating_mode = byte_mode;
	  if (set_fields (optarg) == 0)
	    error (2, 0, "no fields given");
	  break;

	case 'f':
	  /* Build the field list. */
	  if (operating_mode != undefined_mode)
	    usage (2);
	  operating_mode = field_mode;
	  if (set_fields (optarg) == 0)
	    error (2, 0, "no fields given");
	  break;

	case 'd':
	  /* New delimiter. */
	  if (optarg[0] == '\0')
	    error (2, 0, "no delimiter given");
	  if (optarg[1] != '\0')
	    error (2, 0, "delimiter must be a single character");
	  delim = optarg[0];
	  break;

	case 'n':
	  break;

	case 's':
	  delimited_lines_only++;
	  break;

	default:
	  usage (2);
	}
    }

  if (show_version)
    {
      printf ("%s\n", version_string);
      exit (0);
    }

  if (show_help)
    usage (0);

  if (operating_mode == undefined_mode)
    usage (2);

  if ((delimited_lines_only || delim != '\0') && operating_mode != field_mode)
    usage (2);

  if (delim == '\0')
    delim = '\t';

  if (optind == argc)
    exit_status |= cut_file ("-");
  else
    for (; optind < argc; optind++)
      exit_status |= cut_file (argv[optind]);

  if (have_read_stdin && fclose (stdin) == EOF)
    {
      error (0, errno, "-");
      exit_status = 1;
    }
  if (ferror (stdout) || fclose (stdout) == EOF)
    error (1, errno, "write error");

  exit (exit_status);
}

/* Select for printing the positions in `fields' that are listed in
   byte or field specification FIELDSTR.  FIELDSTR should be
   composed of one or more numbers or ranges of numbers, separated by
   blanks or commas.  Incomplete ranges may be given: `-m' means
   `1-m'; `n-' means `n' through end of line or last field.

   Return the number of fields selected. */

static int
set_fields (fieldstr)
     char *fieldstr;
{
  int initial = 1;		/* Value of first number in a range. */
  int dash_found = 0;		/* Nonzero if a '-' is found in this field. */
  int value = 0;		/* If nonzero, a number being accumulated. */
  int fields_selected = 0;	/* Number of fields selected so far. */
  /* If nonzero, index of first field in a range that goes to end of line. */
  int eol_range_start = 0;

  for (;;)
    {
      if (*fieldstr == '-')
	{
	  /* Starting a range. */
	  if (dash_found)
	    invalid_list ();
	  dash_found++;
	  fieldstr++;

	  if (value)
	    {
	      if (value >= line_size)
		enlarge_line (value);
	      initial = value;
	      value = 0;
	    }
	  else
	    initial = 1;
	}
      else if (*fieldstr == ',' || ISBLANK (*fieldstr) || *fieldstr == '\0')
	{
	  /* Ending the string, or this field/byte sublist. */
	  if (dash_found)
	    {
	      dash_found = 0;

	      /* A range.  Possibilites: -n, m-n, n-.
		 In any case, `initial' contains the start of the range. */
	      if (value == 0)
		{
		  /* `n-'.  From `initial' to end of line. */
		  eol_range_start = initial;
		  fields_selected++;
		}
	      else
		{
		  /* `m-n' or `-n' (1-n). */
		  if (value < initial)
		    invalid_list ();

		  if (value >= line_size)
		    enlarge_line (value);

		  /* Is there already a range going to end of line? */
		  if (eol_range_start != 0)
		    {
		      /* Yes.  Is the new sequence already contained
			 in the old one?  If so, no processing is
			 necessary. */
		      if (initial < eol_range_start)
			{
			  /* No, the new sequence starts before the
			     old.  Does the old range going to end of line
			     extend into the new range?  */
			  if (eol_range_start < value)
			    /* Yes.  Simply move the end of line marker. */
			    eol_range_start = initial;
			  else
			    {
			      /* No.  A simple range, before and disjoint from
				 the range going to end of line.  Fill it. */
			      for (; initial <= value; initial++)
				fields[initial] = field_output;
			    }

			  /* In any case, some fields were selected. */
			  fields_selected++;
			}
		    }
		  else
		    {
		      /* There is no range going to end of line. */
		      for (; initial <= value; initial++)
			fields[initial] = field_output;
		      fields_selected++;
		    }
		  value = 0;
		}
	    }
	  else if (value != 0)
	    {
	      /* A simple field number, not a range. */
	      if (value >= line_size)
		enlarge_line (value);

	      fields[value] = field_output;
	      value = 0;
	      fields_selected++;
	    }

	  if (*fieldstr == '\0')
	    {
	      /* If there was a range going to end of line, fill the
		 array from the end of line point.  */
	      if (eol_range_start)
		for (initial = eol_range_start; initial < line_size; initial++)
		  fields[initial] = field_output;

	      return fields_selected;
	    }

	  fieldstr++;
	}
      else if (ISDIGIT (*fieldstr))
	{
	  value = 10 * value + *fieldstr - '0';
	  fieldstr++;
	}
      else
	invalid_list ();
    }
}

/* Process file FILE to standard output.
   Return 0 if successful, 1 if not. */

static int
cut_file (file)
     char *file;
{
  FILE *stream;

  if (!strcmp (file, "-"))
    {
      have_read_stdin = 1;
      stream = stdin;
    }
  else
    {
      stream = fopen (file, "r");
      if (stream == NULL)
	{
	  error (0, errno, "%s", file);
	  return 1;
	}
    }

  cut_stream (stream);

  if (ferror (stream))
    {
      error (0, errno, "%s", file);
      return 1;
    }
  if (!strcmp (file, "-"))
    clearerr (stream);		/* Also clear EOF. */
  else if (fclose (stream) == EOF)
    {
      error (0, errno, "%s", file);
      return 1;
    }
  return 0;
}

static void
cut_stream (stream)
     FILE *stream;
{
  if (operating_mode == byte_mode)
    cut_bytes (stream);
  else
    cut_fields (stream);
}

/* Print the file open for reading on stream STREAM
   with the bytes marked `field_omit' in `fields' removed from each line. */

static void
cut_bytes (stream)
     FILE *stream;
{
  register int c;		/* Each character from the file. */
  int doneflag = 0;		/* Nonzero if EOF reached. */
  int char_count;		/* Number of chars in the line so far. */

  while (doneflag == 0)
    {
      /* Start processing a line. */
      outbufptr = outbuf;
      char_count = 0;

      do
	{
	  c = getc (stream);
	  if (c == EOF)
	    {
	      doneflag++;
	      break;
	    }

	  /* If this character is to be sent, stow it in the outbuffer. */

	  if (++char_count == line_size - 1)
	    enlarge_line (char_count);

	  if (fields[char_count] == field_output || c == '\n')
	    *outbufptr++ = c;
	}
      while (c != '\n');

      if (char_count)
	fwrite (outbuf, sizeof (char), outbufptr - outbuf, stdout);
    }
}

/* Print the file open for reading on stream STREAM
   with the fields marked `field_omit' in `fields' removed from each line.
   All characters are initially stowed in the raw input buffer, until
   at least one field has been found. */

static void
cut_fields (stream)
     FILE *stream;
{
  register int c;		/* Each character from the file. */
  int doneflag = 0;		/* Nonzero if EOF reached. */
  int char_count;		/* Number of chars in line before any delim. */
  int fieldfound;		/* Nonzero if any fields to print found. */
  int curr_field;		/* Current index in `fields'. */

  while (doneflag == 0)
    {
      char_count = 0;
      fieldfound = 0;
      curr_field = 1;
      outbufptr = outbuf;
      inbufptr = inbuf;

      do
	{
	  c = getc (stream);
	  if (c == EOF)
	    {
	      doneflag++;
	      break;
	    }

	  if (fields[curr_field] == field_output && c != '\n')
	    {
	      /* Working on a field.  It, and its terminating
		 delimiter, go only into the processed buffer. */
	      fieldfound = 1;
	      if (outbufptr - outbuf == line_size - 2)
		enlarge_line (outbufptr - outbuf);
	      *outbufptr++ = c;
	    }
	  else if (fieldfound == 0)
	    {
	      if (++char_count == line_size - 1)
		enlarge_line (char_count);
	      *inbufptr++ = c;
	    }

	  if (c == delim && ++curr_field == line_size - 1)
	    enlarge_line (curr_field);
	}
      while (c != '\n');

      if (fieldfound)
	{
	  /* Something was found. Print it. */
	  fwrite (outbuf, sizeof (char), outbufptr - outbuf, stdout);
	  if (c == '\n')
	    putc (c, stdout);
	}
      else if (!delimited_lines_only && char_count)
	/* A line with some characters, no delimiters, and no
	   suppression.  Print it. */
	fwrite (inbuf, sizeof (char), inbufptr - inbuf, stdout);
    }
}

/* Extend the buffers to accomodate at least NEW_SIZE characters. */

static void
enlarge_line (new_size)
     int new_size;
{
  char *newp;
  int i;

  new_size += 256;		/* Leave some room to grow. */

  fields = (enum field_action *)
    xrealloc (fields, new_size * sizeof (enum field_action));

  newp = (char *) xrealloc (outbuf, new_size);
  outbufptr += newp - outbuf;
  outbuf = newp;

  newp = (char *) xrealloc (inbuf, new_size);
  inbufptr += newp - inbuf;
  inbuf = newp;

  for (i = line_size; i < new_size; i++)
    fields[i] = field_omit;
  line_size = new_size;
}

static void
invalid_list ()
{
  error (2, 0, "invalid byte or field list");
}

static void
usage (status)
     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 ("\
\n\
  -b, --bytes LIST        output only these bytes\n\
  -c, --characters LIST   output only these characters\n\
  -d, --delimiter DELIM   use DELIM instead of TAB for field delimiter\n\
  -f, --fields LIST       output only these fields\n\
  -n                      (ignored)\n\
  -s, --only-delimited    do not print lines not containing delimiters\n\
      --help              display this help and exit\n\
      --version           output version information and exit\n\
\n\
Use one, and only one of -b, -c or -f.  Each LIST is made up of one\n\
range, or many ranges separated by commas.  Each range is one of:\n\
\n\
  N     N'th byte, character or field, counted from 1\n\
  N-    from N'th byte, character or field, to end of line\n\
  N-M   from N'th to M'th (included) byte, character or field\n\
  -M    from first to M'th (included) byte, character or field\n\
\n\
With no FILE, or when FILE is -, read standard input.\n\
");
    }
  exit (status);
}