*** empty log message ***
[coreutils.git] / src / paste.c
blob3814c5a088b38998a5073869b9425eb472de9683
1 /* paste - merge lines of files
2 Copyright (C) 1984, 1997, 1998, 1999, 2000, 2001 by David M. Ihnat
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.
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.
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. */
18 /* Written by David Ihnat. */
20 /* The list of valid escape sequences has been expanded over the Unix
21 version, to include \b, \f, \r, and \v.
23 POSIX changes, bug fixes, long-named options, and cleanup
24 by David MacKenzie <djm@gnu.ai.mit.edu>.
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. */
38 #include <config.h>
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"
47 /* The official name of this program (e.g., no `g' prefix). */
48 #define PROGRAM_NAME "paste"
50 #define AUTHORS N_ ("David M. Ihnat and David MacKenzie")
52 /* Indicates that no delimiter should be added in the current position. */
53 #define EMPTY_DELIM '\0'
55 static FILE dummy_closed;
56 /* Element marking a file that has reached EOF and been closed. */
57 #define CLOSED (&dummy_closed)
59 static FILE dummy_endlist;
60 /* Element marking end of list of open files. */
61 #define ENDLIST (&dummy_endlist)
63 /* Name this program was run with. */
64 char *program_name;
66 /* If nonzero, we have read standard input at some point. */
67 static int have_read_stdin;
69 /* If nonzero, merge subsequent lines of each file rather than
70 corresponding lines from each file in parallel. */
71 static int serial_merge;
73 /* The delimeters between lines of input files (used cyclically). */
74 static char *delims;
76 /* A pointer to the character after the end of `delims'. */
77 static char *delim_end;
79 static struct option const longopts[] =
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}
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.
93 Return a pointer to the character after the new end of STRPTR. */
95 static char *
96 collapse_escapes (char *strptr)
98 register char *strout;
100 strout = strptr; /* Start at the same place, anyway. */
102 while (*strptr)
104 if (*strptr != '\\') /* Is it an escape character? */
105 *strout++ = *strptr++; /* No, just transfer it. */
106 else
108 switch (*++strptr)
110 case '0':
111 *strout++ = EMPTY_DELIM;
112 break;
114 case 'b':
115 *strout++ = '\b';
116 break;
118 case 'f':
119 *strout++ = '\f';
120 break;
122 case 'n':
123 *strout++ = '\n';
124 break;
126 case 'r':
127 *strout++ = '\r';
128 break;
130 case 't':
131 *strout++ = '\t';
132 break;
134 case 'v':
135 *strout++ = '\v';
136 break;
138 default:
139 *strout++ = *strptr;
140 break;
142 strptr++;
145 return strout;
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. */
152 static int
153 paste_parallel (int nfiles, char **fnamptr)
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. */
174 delbuf = (char *) xmalloc (file_list_size + 2);
175 fileptr = (FILE **) xmalloc ((file_list_size + 1) * sizeof (FILE *));
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. */
181 for (files_open = 0; files_open < nfiles; ++files_open)
183 if (files_open == file_list_size - 2)
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 *));
190 if (STREQ (fnamptr[files_open], "-"))
192 have_read_stdin = 1;
193 fileptr[files_open] = stdin;
195 else
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;
205 fileptr[files_open] = ENDLIST;
207 if (opened_stdin && have_read_stdin)
208 error (EXIT_FAILURE, 0, _("standard input is closed"));
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. */
214 while (files_open)
216 /* Set up for the next line. */
217 somedone = 0;
218 delimptr = delims;
219 delims_saved = 0;
221 for (i = 0; fileptr[i] != ENDLIST && files_open; i++)
223 line_length = 0; /* Clear so we can easily detect EOF. */
224 if (fileptr[i] != CLOSED)
226 chr = getc (fileptr[i]);
227 if (chr != EOF && delims_saved)
229 fwrite (delbuf, sizeof (char), delims_saved, stdout);
230 delims_saved = 0;
233 while (chr != EOF)
235 line_length++;
236 if (chr == '\n')
237 break;
238 putc (chr, stdout);
239 chr = getc (fileptr[i]);
243 if (line_length == 0)
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)
249 if (ferror (fileptr[i]))
251 error (0, errno, "%s", fnamptr[i]);
252 errors = 1;
254 if (fileptr[i] == stdin)
255 clearerr (fileptr[i]); /* Also clear EOF. */
256 else if (fclose (fileptr[i]) == EOF)
258 error (0, errno, "%s", fnamptr[i]);
259 errors = 1;
262 fileptr[i] = CLOSED;
263 files_open--;
266 if (fileptr[i + 1] == ENDLIST)
268 /* End of this output line.
269 Is this the end of the whole thing? */
270 if (somedone)
272 /* No. Some files were not closed for this line. */
273 if (delims_saved)
275 fwrite (delbuf, sizeof (char), delims_saved, stdout);
276 delims_saved = 0;
278 putc ('\n', stdout);
280 continue; /* Next read of files, or exit. */
282 else
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;
291 else
293 /* Some data read. */
294 somedone++;
296 /* Except for last file, replace last newline with delim. */
297 if (fileptr[i + 1] != ENDLIST)
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;
306 else
307 putc (chr, stdout);
311 return errors;
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. */
318 static int
319 paste_serial (int nfiles, char **fnamptr)
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. */
326 for (; nfiles; nfiles--, fnamptr++)
328 if (STREQ (*fnamptr, "-"))
330 have_read_stdin = 1;
331 fileptr = stdin;
333 else
335 fileptr = fopen (*fnamptr, "r");
336 if (fileptr == NULL)
338 error (0, errno, "%s", *fnamptr);
339 errors = 1;
340 continue;
344 delimptr = delims; /* Set up for delimiter string. */
346 charold = getc (fileptr);
347 if (charold != EOF)
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. */
355 while ((charnew = getc (fileptr)) != EOF)
357 /* Process the old character. */
358 if (charold == '\n')
360 if (*delimptr != EMPTY_DELIM)
361 putc (*delimptr, stdout);
363 if (++delimptr == delim_end)
364 delimptr = delims;
366 else
367 putc (charold, stdout);
369 charold = charnew;
372 /* Hit EOF. Process that last character. */
373 putc (charold, stdout);
376 if (charold != '\n')
377 putc ('\n', stdout);
379 if (ferror (fileptr))
381 error (0, errno, "%s", *fnamptr);
382 errors = 1;
384 if (fileptr == stdin)
385 clearerr (fileptr); /* Also clear EOF. */
386 else if (fclose (fileptr) == EOF)
388 error (0, errno, "%s", *fnamptr);
389 errors = 1;
392 return errors;
395 void
396 usage (int status)
398 if (status != 0)
399 fprintf (stderr, _("Try `%s --help' for more information.\n"),
400 program_name);
401 else
403 printf (_("\
404 Usage: %s [OPTION]... [FILE]...\n\
406 program_name);
407 fputs (_("\
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\
412 "), stdout);
413 fputs (_("\
414 Mandatory arguments to long options are mandatory for short options too.\n\
415 "), stdout);
416 fputs (_("\
417 -d, --delimiters=LIST reuse characters from LIST instead of TABs\n\
418 -s, --serial paste one file at a time instead of in parallel\n\
419 "), stdout);
420 fputs (HELP_OPTION_DESCRIPTION, stdout);
421 fputs (VERSION_OPTION_DESCRIPTION, stdout);
422 /* FIXME: add a couple of examples. */
423 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
425 exit (status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
429 main (int argc, char **argv)
431 int optc, exit_status;
432 char default_delims[2], zero_delims[3];
434 program_name = argv[0];
435 setlocale (LC_ALL, "");
436 bindtextdomain (PACKAGE, LOCALEDIR);
437 textdomain (PACKAGE);
439 atexit (close_stdout);
441 have_read_stdin = 0;
442 serial_merge = 0;
443 delims = default_delims;
444 strcpy (delims, "\t");
445 strcpy (zero_delims, "\\0");
447 while ((optc = getopt_long (argc, argv, "d:s", longopts, NULL)) != -1)
449 switch (optc)
451 case 0:
452 break;
454 case 'd':
455 /* Delimiter character(s). */
456 if (optarg[0] == '\0')
457 optarg = zero_delims;
458 delims = optarg;
459 break;
461 case 's':
462 serial_merge++;
463 break;
465 case_GETOPT_HELP_CHAR;
467 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
469 default:
470 usage (1);
474 if (optind == argc)
475 argv[argc++] = "-";
477 delim_end = collapse_escapes (delims);
479 if (!serial_merge)
480 exit_status = paste_parallel (argc - optind, &argv[optind]);
481 else
482 exit_status = paste_serial (argc - optind, &argv[optind]);
483 if (have_read_stdin && fclose (stdin) == EOF)
484 error (EXIT_FAILURE, errno, "-");
485 exit (exit_status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);