(add_tabstop): Give correct size when reallocating tab_list buffer.
[platform/upstream/coreutils.git] / src / paste.c
1 /* paste - merge lines of files
2    Copyright (C) 1984 by David M. Ihnat
3
4    This program is a total rewrite of the Bell Laboratories Unix(Tm)
5    command of the same name, as of System V.  It contains no proprietary
6    code, and therefore may be used without violation of any proprietary
7    agreements whatsoever.  However, you will notice that the program is
8    copyrighted by me.  This is to assure the program does *not* fall
9    into the public domain.  Thus, I may specify just what I am now:
10    This program may be freely copied and distributed, provided this notice
11    remains; it may not be sold for profit without express written consent of
12    the author.
13    Please note that I recreated the behavior of the Unix(Tm) 'paste' command
14    as faithfully as possible, with minor exceptions; however,
15    I haven't run a full set of regression tests.  Thus, the user of
16    this program accepts full responsibility for any effects or loss;
17    in particular, the author is not responsible for any losses,
18    explicit or incidental, that may be incurred through use of this program.
19
20    I ask that any bugs (and, if possible, fixes) be reported to me when
21    possible.  -David Ihnat (312) 784-4544 ignatz@homebru.chi.il.us
22
23    The list of valid escape sequences has been expanded over the Unix
24    version, to include \b, \f, \r, and \v.
25
26    POSIX changes, bug fixes, long-named options, and cleanup
27    by David MacKenzie <djm@gnu.ai.mit.edu>.
28
29    Options:
30    --serial
31    -s                           Paste one file at a time rather than
32                                 one line from each file.
33    --delimiters=delim-list
34    -d delim-list                Consecutively use the characters in
35                                 DELIM-LIST instead of tab to separate
36                                 merged lines.  When DELIM-LIST is exhausted,
37                                 start again at its beginning.
38    A FILE of `-' means standard input.
39    If no FILEs are given, standard input is used. */
40
41 #include <config.h>
42
43 #include <stdio.h>
44 #include <getopt.h>
45 #include <sys/types.h>
46 #include "system.h"
47 #include "version.h"
48 #include "error.h"
49
50 char *xmalloc ();
51 char *xrealloc ();
52
53 static char *collapse_escapes ();
54 static int paste_parallel ();
55 static int paste_serial ();
56 static void usage ();
57
58 /* Indicates that no delimiter should be added in the current position. */
59 #define EMPTY_DELIM '\0'
60
61 /* Element marking a file that has reached EOF and been closed. */
62 #define CLOSED ((FILE *) -1)
63
64 /* Element marking end of list of open files. */
65 #define ENDLIST ((FILE *) -2)
66
67 /* Name this program was run with. */
68 char *program_name;
69
70 /* If nonzero, we have read standard input at some point. */
71 static int have_read_stdin;
72
73 /* If nonzero, merge subsequent lines of each file rather than
74    corresponding lines from each file in parallel. */
75 static int serial_merge;
76
77 /* The delimeters between lines of input files (used cyclically). */
78 static char *delims;
79
80 /* A pointer to the character after the end of `delims'. */
81 static char *delim_end;
82
83 /* If non-zero, display usage information and exit.  */
84 static int show_help;
85
86 /* If non-zero, print the version on standard output then exit.  */
87 static int show_version;
88
89 static struct option const longopts[] =
90 {
91   {"serial", no_argument, 0, 's'},
92   {"delimiters", required_argument, 0, 'd'},
93   {"help", no_argument, &show_help, 1},
94   {"version", no_argument, &show_version, 1},
95   {0, 0, 0, 0}
96 };
97
98 void
99 main (argc, argv)
100      int argc;
101      char **argv;
102 {
103   int optc, exit_status;
104   char default_delims[2], zero_delims[3];
105
106   program_name = argv[0];
107   have_read_stdin = 0;
108   serial_merge = 0;
109   delims = default_delims;
110   strcpy (delims, "\t");
111   strcpy (zero_delims, "\\0");
112
113   while ((optc = getopt_long (argc, argv, "d:s", longopts, (int *) 0))
114          != EOF)
115     {
116       switch (optc)
117         {
118         case 0:
119           break;
120
121         case 'd':
122           /* Delimiter character(s). */
123           if (optarg[0] == '\0')
124             optarg = zero_delims;
125           delims = optarg;
126           break;
127
128         case 's':
129           serial_merge++;
130           break;
131
132         default:
133           usage (1);
134         }
135     }
136
137   if (show_version)
138     {
139       printf ("paste - %s\n", version_string);
140       exit (0);
141     }
142
143   if (show_help)
144     usage (0);
145
146   if (optind == argc)
147     argv[argc++] = "-";
148
149   delim_end = collapse_escapes (delims);
150
151   if (!serial_merge)
152     exit_status = paste_parallel (argc - optind, &argv[optind]);
153   else
154     exit_status = paste_serial (argc - optind, &argv[optind]);
155   if (have_read_stdin && fclose (stdin) == EOF)
156     error (1, errno, "-");
157   if (ferror (stdout) || fclose (stdout) == EOF)
158     error (1, errno, "write error");
159   exit (exit_status);
160 }
161
162 /* Replace backslash representations of special characters in
163    STRPTR with their actual values.
164    The set of possible backslash characters has been expanded beyond
165    that recognized by the Unix version.
166
167    Return a pointer to the character after the new end of STRPTR. */
168
169 static char *
170 collapse_escapes (strptr)
171      char *strptr;
172 {
173   register char *strout;
174
175   strout = strptr;              /* Start at the same place, anyway. */
176
177   while (*strptr)
178     {
179       if (*strptr != '\\')      /* Is it an escape character? */
180         *strout++ = *strptr++;  /* No, just transfer it. */
181       else
182         {
183           switch (*++strptr)
184             {
185             case '0':
186               *strout++ = EMPTY_DELIM;
187               break;
188
189             case 'b':
190               *strout++ = '\b';
191               break;
192
193             case 'f':
194               *strout++ = '\f';
195               break;
196
197             case 'n':
198               *strout++ = '\n';
199               break;
200
201             case 'r':
202               *strout++ = '\r';
203               break;
204
205             case 't':
206               *strout++ = '\t';
207               break;
208
209             case 'v':
210               *strout++ = '\v';
211               break;
212
213             default:
214               *strout++ = *strptr;
215               break;
216             }
217           strptr++;
218         }
219     }
220   return strout;
221 }
222
223 /* Perform column paste on the NFILES files named in FNAMPTR.
224    Return 0 if no errors, 1 if one or more files could not be
225    opened or read. */
226
227 static int
228 paste_parallel (nfiles, fnamptr)
229      int nfiles;
230      char **fnamptr;
231 {
232   int errors = 0;               /* 1 if open or read errors occur. */
233   /* Number of files for which space is allocated in `delbuf' and `fileptr'.
234      Enlarged as necessary. */
235   int file_list_size = 12;
236   int chr;                      /* Input character. */
237   int line_length;              /* Number of chars in line. */
238   int somedone;                 /* 0 if all files empty for this line. */
239   /* If all files are just ready to be closed, or will be on this
240      round, the string of delimiters must be preserved.
241      delbuf[0] through delbuf[file_list_size]
242      store the delimiters for closed files. */
243   char *delbuf;
244   int delims_saved;             /* Number of delims saved in `delbuf'. */
245   register char *delimptr;      /* Cycling pointer into `delims'. */
246   FILE **fileptr;               /* Streams open to the files to process. */
247   int files_open;               /* Number of files still open to process. */
248   int i;                        /* Loop index. */
249   int opened_stdin = 0;         /* Nonzero if any fopen got fd 0. */
250
251 #ifdef lint  /* Suppress `used before initialized' warning.  */
252   chr = 0;
253 #endif
254
255   delbuf = (char *) xmalloc (file_list_size + 2);
256   fileptr = (FILE **) xmalloc ((file_list_size + 1) * sizeof (FILE *));
257
258   /* Attempt to open all files.  This could be expanded to an infinite
259      number of files, but at the (considerable) expense of remembering
260      each file and its current offset, then opening/reading/closing.  */
261
262   for (files_open = 0; files_open < nfiles; ++files_open)
263     {
264       if (files_open == file_list_size - 2)
265         {
266           file_list_size += 12;
267           delbuf = (char *) xrealloc (delbuf, file_list_size + 2);
268           fileptr = (FILE **) xrealloc (fileptr, (file_list_size + 1)
269                                         * sizeof (FILE *));
270         }
271       if (!strcmp (fnamptr[files_open], "-"))
272         {
273           have_read_stdin = 1;
274           fileptr[files_open] = stdin;
275         }
276       else
277         {
278           fileptr[files_open] = fopen (fnamptr[files_open], "r");
279           if (fileptr[files_open] == NULL)
280             error (1, errno, "%s", fnamptr[files_open]);
281           else if (fileno (fileptr[files_open]) == 0)
282             opened_stdin = 1;
283         }
284     }
285
286   fileptr[files_open] = ENDLIST;
287
288   if (opened_stdin && have_read_stdin)
289     error (1, 0, "standard input is closed");
290
291   /* Read a line from each file and output it to stdout separated by a
292      delimiter, until we go through the loop without successfully
293      reading from any of the files. */
294
295   while (files_open)
296     {
297       /* Set up for the next line. */
298       somedone = 0;
299       delimptr = delims;
300       delims_saved = 0;
301
302       for (i = 0; fileptr[i] != ENDLIST && files_open; i++)
303         {
304           line_length = 0;      /* Clear so we can easily detect EOF. */
305           if (fileptr[i] != CLOSED)
306             {
307               chr = getc (fileptr[i]);
308               if (chr != EOF && delims_saved)
309                 {
310                   fwrite (delbuf, sizeof (char), delims_saved, stdout);
311                   delims_saved = 0;
312                 }
313
314               while (chr != EOF)
315                 {
316                   line_length++;
317                   if (chr == '\n')
318                     break;
319                   putc (chr, stdout);
320                   chr = getc (fileptr[i]);
321                 }
322             }
323
324           if (line_length == 0)
325             {
326               /* EOF, read error, or closed file.
327                  If an EOF or error, close the file and mark it in the list. */
328               if (fileptr[i] != CLOSED)
329                 {
330                   if (ferror (fileptr[i]))
331                     {
332                       error (0, errno, "%s", fnamptr[i]);
333                       errors = 1;
334                     }
335                   if (fileptr[i] == stdin)
336                     clearerr (fileptr[i]); /* Also clear EOF. */
337                   else if (fclose (fileptr[i]) == EOF)
338                     {
339                       error (0, errno, "%s", fnamptr[i]);
340                       errors = 1;
341                     }
342
343                   fileptr[i] = CLOSED;
344                   files_open--;
345                 }
346
347               if (fileptr[i + 1] == ENDLIST)
348                 {
349                   /* End of this output line.
350                      Is this the end of the whole thing? */
351                   if (somedone)
352                     {
353                       /* No.  Some files were not closed for this line. */
354                       if (delims_saved)
355                         {
356                           fwrite (delbuf, sizeof (char), delims_saved, stdout);
357                           delims_saved = 0;
358                         }
359                       putc ('\n', stdout);
360                     }
361                   continue;     /* Next read of files, or exit. */
362                 }
363               else
364                 {
365                   /* Closed file; add delimiter to `delbuf'. */
366                   if (*delimptr != EMPTY_DELIM)
367                     delbuf[delims_saved++] = *delimptr;
368                   if (++delimptr == delim_end)
369                     delimptr = delims;
370                 }
371             }
372           else
373             {
374               /* Some data read. */
375               somedone++;
376
377               /* Except for last file, replace last newline with delim. */
378               if (fileptr[i + 1] != ENDLIST)
379                 {
380                   if (chr != '\n')
381                     putc (chr, stdout);
382                   if (*delimptr != EMPTY_DELIM)
383                     putc (*delimptr, stdout);
384                   if (++delimptr == delim_end)
385                     delimptr = delims;
386                 }
387               else
388                 putc (chr, stdout);
389             }
390         }
391     }
392   return errors;
393 }
394
395 /* Perform serial paste on the NFILES files named in FNAMPTR.
396    Return 0 if no errors, 1 if one or more files could not be
397    opened or read. */
398
399 static int
400 paste_serial (nfiles, fnamptr)
401      int nfiles;
402      char **fnamptr;
403 {
404   int errors = 0;               /* 1 if open or read errors occur. */
405   register int charnew, charold; /* Current and previous char read. */
406   register char *delimptr;      /* Current delimiter char. */
407   register FILE *fileptr;       /* Open for reading current file. */
408
409   for (; nfiles; nfiles--, fnamptr++)
410     {
411       if (!strcmp (*fnamptr, "-"))
412         {
413           have_read_stdin = 1;
414           fileptr = stdin;
415         }
416       else
417         {
418           fileptr = fopen (*fnamptr, "r");
419           if (fileptr == NULL)
420             {
421               error (0, errno, "%s", *fnamptr);
422               errors = 1;
423               continue;
424             }
425         }
426
427       delimptr = delims;        /* Set up for delimiter string. */
428
429       charold = getc (fileptr);
430       if (charold != EOF)
431         {
432           /* `charold' is set up.  Hit it!
433              Keep reading characters, stashing them in `charnew';
434              output `charold', converting to the appropriate delimiter
435              character if needed.  After the EOF, output `charold'
436              if it's a newline; otherwise, output it and then a newline. */
437
438           while ((charnew = getc (fileptr)) != EOF)
439             {
440               /* Process the old character. */
441               if (charold == '\n')
442                 {
443                   if (*delimptr != EMPTY_DELIM)
444                     putc (*delimptr, stdout);
445
446                   if (++delimptr == delim_end)
447                     delimptr = delims;
448                 }
449               else
450                 putc (charold, stdout);
451
452               charold = charnew;
453             }
454
455           /* Hit EOF.  Process that last character. */
456           putc (charold, stdout);
457         }
458
459       if (charold != '\n')
460         putc ('\n', stdout);
461
462       if (ferror (fileptr))
463         {
464           error (0, errno, "%s", *fnamptr);
465           errors = 1;
466         }
467       if (fileptr == stdin)
468         clearerr (fileptr);     /* Also clear EOF. */
469       else if (fclose (fileptr) == EOF)
470         {
471           error (0, errno, "%s", *fnamptr);
472           errors = 1;
473         }
474     }
475   return errors;
476 }
477
478 static void
479 usage (status)
480      int status;
481 {
482   if (status != 0)
483     fprintf (stderr, "Try `%s --help' for more information.\n",
484              program_name);
485   else
486     {
487       printf ("\
488 Usage: %s [OPTION]... [FILE]...\n\
489 ",
490               program_name);
491       printf ("\
492 \n\
493   -d, --delimiters=LIST   reuse characters from LIST instead of TABs\n\
494   -s, --serial            paste one file at a time instead of in parallel\n\
495       --help              display this help and exit\n\
496       --version           output version information and exit\n\
497 \n\
498 With no INPUT, or when INPUT is -, read standard input.\n\
499 ");
500     }
501   exit (status);
502 }