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