home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / f / futi14as.zip / PASTE.C < prev    next >
C/C++ Source or Header  |  1992-02-22  |  14KB  |  541 lines

  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@ai.mit.edu>.
  28.  
  29.    Usage: paste [-s] [-d delim-list] [+serial] [+delimiters delim-list]
  30.           [file...]
  31.  
  32.    Options:
  33.    +serial
  34.    -s                Paste one file at a time rather than
  35.                 one line from each file.
  36.    +delimiters delim-list
  37.    -d delim-list        Consecutively use the characters in
  38.                 DELIM-LIST instead of tab to separate
  39.                 merged lines.  When DELIM-LIST is exhausted,
  40.                 start again at its beginning.
  41.    A FILE of `-' means standard input.
  42.    If no FILEs are given, standard input is used. */
  43. /* MS-DOS port (c) 1990 by Thorsten Ohl, ohl@gnu.ai.mit.edu
  44.    This port is also distributed under the terms of the
  45.    GNU General Public License as published by the
  46.    Free Software Foundation.
  47.  
  48.    Please note that this file is not identical to the
  49.    original GNU release, you should have received this
  50.    code as patch to the official release.  */
  51.  
  52. #ifdef MSDOS
  53. static char RCS_Id[] =
  54.   "$Header: e:/gnu/fileutil/RCS/paste.c 1.4.0.3 90/09/19 12:11:19 tho Exp $";
  55.  
  56. static char Program_Id[] = "paste";
  57. static char RCS_Revision[] = "$Revision: 1.4.0.3 $";
  58.  
  59. #define VERSION \
  60.   "GNU %s, Version %.*s (compiled %s %s for MS-DOS)\n", Program_Id, \
  61.   (sizeof RCS_Revision - 14), (RCS_Revision + 11), __DATE__, __TIME__
  62.  
  63. #define COPYING \
  64.   "This is free software, distributed under the terms of the\n" \
  65.   "GNU General Public License.  For details, see the file COPYING.\n"
  66. #endif /* MSDOS */
  67.  
  68. #include <stdio.h>
  69. #include <errno.h>
  70. #include <getopt.h>
  71. #include <sys/types.h>
  72. #include "system.h"
  73.  
  74. #ifdef STDC_HEADERS
  75. #include <stdlib.h>
  76. #else
  77. char *malloc ();
  78. char *realloc ();
  79. void exit ();
  80.  
  81. extern int errno;
  82. #endif
  83.  
  84. #ifdef MSDOS
  85.  
  86. #include <gnulib.h>
  87.  
  88. void main (int argc, char **argv);
  89. char *collapse_escapes (char *strptr);
  90. int paste_parallel (int nfiles, char **fnamptr);
  91. int paste_serial (int nfiles, char **fnamptr);
  92. void usage (void);
  93.  
  94. #else /* not MSDOS */
  95.  
  96. char *collapse_escapes ();
  97. char *xmalloc ();
  98. char *xrealloc ();
  99. int paste_parallel ();
  100. int paste_serial ();
  101. void error ();
  102. void usage ();
  103.  
  104. #endif /* not MSDOS */
  105.  
  106.  
  107. /* Indicates that no delimiter should be added in the current position. */
  108. #define EMPTY_DELIM '\0'
  109.  
  110. /* Element marking a file that has reached EOF and been closed. */
  111. #define    CLOSED ((FILE *) -1)
  112.  
  113. /* Element marking end of list of open files. */
  114. #define ENDLIST ((FILE *) -2)
  115.  
  116. /* Name this program was run with. */
  117. char *program_name;
  118.  
  119. /* If nonzero, merge subsequent lines of each file rather than
  120.    corresponding lines from each file in parallel. */
  121. int serial_merge;
  122.  
  123. /* The delimeters between lines of input files (used cyclically). */
  124. char *delims;
  125.  
  126. /* A pointer to the character after the end of `delims'. */
  127. char *delim_end;
  128.  
  129. struct option longopts[] =
  130. {
  131. #ifdef MSDOS
  132.   {"copying", 0, NULL, 30},
  133.   {"version", 0, NULL, 31},
  134. #endif
  135.   {"serial", 0, 0, 's'},
  136.   {"delimiters", 1, 0, 'd'},
  137.   {0, 0, 0, 0}
  138. };
  139.  
  140. void
  141. main (argc, argv)
  142.      int argc;
  143.      char **argv;
  144. {
  145.   int optc;
  146.   int longind;
  147.   char default_delims[2];
  148.  
  149.   program_name = argv[0];
  150.   serial_merge = 0;
  151.   delims = default_delims;
  152.   strcpy (delims, "\t");
  153.  
  154.   while ((optc = getopt_long (argc, argv, "d:s", longopts, &longind))
  155.      != EOF)
  156.     {
  157.       switch (optc)
  158.     {
  159.     case 'd':
  160.       /* Delimiter character(s). */
  161.       if (optarg[0] == '\0')
  162.         error (2, 0, "no delimiters given");
  163.       delims = optarg;
  164.       break;
  165.  
  166.     case 's':
  167.       serial_merge++;
  168.       break;
  169.  
  170. #ifdef MSDOS
  171.     case 30:
  172.       fprintf (stderr, COPYING);
  173.       exit (0);
  174.       break;
  175.  
  176.     case 31:
  177.       fprintf (stderr, VERSION);
  178.       exit (0);
  179.       break;
  180. #endif
  181.  
  182.     default:
  183.       usage ();
  184.     }
  185.     }
  186.  
  187.   if (optind == argc)
  188.     argv[argc++] = "-";
  189.  
  190.   delim_end = collapse_escapes (delims);
  191.  
  192.   if (!serial_merge)
  193.     exit (paste_parallel (argc - optind, &argv[optind]));
  194.   else
  195.     exit (paste_serial (argc - optind, &argv[optind]));
  196. }
  197.  
  198. /* Replace backslash representations of special characters in
  199.    STRPTR with their actual values.
  200.    The set of possible backslash characters has been expanded beyond
  201.    that recognized by the Unix version.
  202.  
  203.    Return a pointer to the character after the new end of STRPTR. */
  204.  
  205. char *
  206. collapse_escapes (strptr)
  207.      char *strptr;
  208. {
  209.   register char *strout;
  210.  
  211.   strout = strptr;        /* Start at the same place, anyway. */
  212.  
  213.   while (*strptr)
  214.     {
  215.       if (*strptr != '\\')    /* Is it an escape character? */
  216.     *strout++ = *strptr++;    /* No, just transfer it. */
  217.       else
  218.     {
  219.       switch (*++strptr)
  220.         {
  221.         case '0':
  222.           *strout++ = EMPTY_DELIM;
  223.           break;
  224.  
  225.         case 'b':
  226.           *strout++ = '\b';
  227.           break;
  228.  
  229.         case 'f':
  230.           *strout++ = '\f';
  231.           break;
  232.  
  233.         case 'n':
  234.           *strout++ = '\n';
  235.           break;
  236.  
  237.         case 'r':
  238.           *strout++ = '\r';
  239.           break;
  240.  
  241.         case 't':
  242.           *strout++ = '\t';
  243.           break;
  244.  
  245.         case 'v':
  246.           *strout++ = '\v';
  247.           break;
  248.  
  249.         default:
  250.           *strout++ = *strptr;
  251.           break;
  252.         }
  253.       strptr++;
  254.     }
  255.     }
  256.   return strout;
  257. }
  258.  
  259. /* Perform column paste on the NFILES files named in FNAMPTR.
  260.    Return 0 if no errors, 1 if one or more files could not be
  261.    opened or read. */
  262.  
  263. int
  264. paste_parallel (nfiles, fnamptr)
  265.      int nfiles;
  266.      char **fnamptr;
  267. {
  268.   int errors = 0;        /* 1 if open or read errors occur. */
  269.   /* Number of files for which space is allocated in `delbuf' and `fileptr'.
  270.      Enlarged as necessary. */
  271.   int file_list_size = 12;
  272.   int chr;            /* Input character. */
  273.   int line_length;        /* Number of chars in line. */
  274.   int somedone;            /* 0 if all files empty for this line. */
  275.   /* If all files are just ready to be closed, or will be on this
  276.      round, the string of delimiters must be preserved.
  277.      delbuf[0] through delbuf[file_list_size]
  278.      store the delimiters for closed files. */
  279.   char *delbuf;
  280.   int delims_saved;        /* Number of delims saved in `delbuf'. */
  281.   register char *delimptr;    /* Cycling pointer into `delims'. */
  282.   FILE **fileptr;        /* Streams open to the files to process. */
  283.   int files_open;        /* Number of files still open to process. */
  284.   int i;            /* Loop index. */
  285.  
  286.   delbuf = (char *) xmalloc (file_list_size + 2);
  287.   fileptr = (FILE **) xmalloc ((file_list_size + 1) * sizeof (FILE *));
  288.  
  289.   /* Attempt to open all files.  This could be expanded to an infinite
  290.      number of files, but at the (considerable) expense of remembering
  291.      each file and its current offset, then opening/reading/closing.  */
  292.  
  293.   for (files_open = 0; files_open < nfiles; ++files_open)
  294.     {
  295.       if (files_open == file_list_size - 2)
  296.     {
  297.       file_list_size += 12;
  298.       delbuf = (char *) xrealloc (delbuf, file_list_size + 2);
  299.       fileptr = (FILE **) xrealloc (fileptr, (file_list_size + 1)
  300.                     * sizeof (FILE *));
  301.     }
  302.       if (!strcmp (fnamptr[files_open], "-"))
  303.     fileptr[files_open] = stdin;
  304.       else
  305.     {
  306.       fileptr[files_open] = fopen (fnamptr[files_open], "r");
  307.       if (fileptr[files_open] == NULL)
  308.         error (1, errno, "%s", fnamptr[files_open]);
  309.     }
  310.     }
  311.  
  312.   fileptr[files_open] = ENDLIST;
  313.  
  314.   /* Read a line from each file and output it to stdout separated by a
  315.      delimiter, until we go through the loop without successfully
  316.      reading from any of the files. */
  317.  
  318.   while (files_open)
  319.     {
  320.       /* Set up for the next line. */
  321.       somedone = 0;
  322.       delimptr = delims;
  323.       delims_saved = 0;
  324.  
  325.       for (i = 0; fileptr[i] != ENDLIST && files_open; i++)
  326.     {
  327.       line_length = 0;    /* Clear so we can easily detect EOF. */
  328.       if (fileptr[i] != CLOSED)
  329.         {
  330.           chr = getc (fileptr[i]);
  331.           if (chr != EOF && delims_saved)
  332.         {
  333.           fwrite (delbuf, sizeof (char), delims_saved, stdout);
  334.           delims_saved = 0;
  335.         }
  336.  
  337.           while (chr != EOF)
  338.         {
  339.           line_length++;
  340.           if (chr == '\n')
  341.             break;
  342.           putc (chr, stdout);
  343.           chr = getc (fileptr[i]);
  344.         }
  345.         }
  346.  
  347.       if (line_length == 0)
  348.         {
  349.           /* EOF, read error, or closed file.
  350.          If an EOF or error, close the file and mark it in the list. */
  351.           if (fileptr[i] != CLOSED)
  352.         {
  353.           if (ferror (fileptr[i]))
  354.             {
  355.               error (0, errno, "%s", fnamptr[i]);
  356.               errors = 1;
  357.             }
  358.           if (fileptr[i] != stdin)
  359.             fclose (fileptr[i]);
  360.           fileptr[i] = CLOSED;
  361.           files_open--;
  362.         }
  363.  
  364.           if (fileptr[i + 1] == ENDLIST)
  365.         {
  366.           /* End of this output line.
  367.              Is this the end of the whole thing? */
  368.           if (somedone)
  369.             {
  370.               /* No.  Some files were not closed for this line. */
  371.               if (delims_saved)
  372.             {
  373.               fwrite (delbuf, sizeof (char), delims_saved, stdout);
  374.               delims_saved = 0;
  375.             }
  376.               putc ('\n', stdout);
  377.             }
  378.           continue;    /* Next read of files, or exit. */
  379.         }
  380.           else
  381.         {
  382.           /* Closed file; add delimiter to `delbuf'. */
  383.           if (*delimptr != EMPTY_DELIM)
  384.             delbuf[delims_saved++] = *delimptr;
  385.           if (++delimptr == delim_end)
  386.             delimptr = delims;
  387.         }
  388.         }
  389.       else
  390.         {
  391.           /* Some data read. */
  392.           somedone++;
  393.  
  394.           /* Except for last file, replace last newline with delim. */
  395.           if (fileptr[i + 1] != ENDLIST)
  396.         {
  397.           if (chr != '\n')
  398.             putc (chr, stdout);
  399.           if (*delimptr != EMPTY_DELIM)
  400.             putc (*delimptr, stdout);
  401.           if (++delimptr == delim_end)
  402.             delimptr = delims;
  403.         }
  404.           else
  405.         putc (chr, stdout);
  406.         }
  407.     }
  408.     }
  409.   return errors;
  410. }
  411.  
  412. /* Perform serial paste on the NFILES files named in FNAMPTR.
  413.    Return 0 if no errors, 1 if one or more files could not be
  414.    opened or read. */
  415.  
  416. int
  417. paste_serial (nfiles, fnamptr)
  418.      int nfiles;
  419.      char **fnamptr;
  420. {
  421.   int errors = 0;        /* 1 if open or read errors occur. */
  422.   register int charnew, charold; /* Current and previous char read. */
  423.   register char *delimptr;    /* Current delimiter char. */
  424.   register FILE *fileptr;    /* Open for reading current file. */
  425.  
  426.   for (; nfiles; nfiles--, fnamptr++)
  427.     {
  428.       if (!strcmp (*fnamptr, "-"))
  429.     fileptr = stdin;
  430.       else
  431.     {
  432.       fileptr = fopen (*fnamptr, "r");
  433.       if (fileptr == NULL)
  434.         {
  435.           error (0, errno, "%s", *fnamptr);
  436.           errors = 1;
  437.           continue;
  438.         }
  439.     }
  440.  
  441.       delimptr = delims;    /* Set up for delimiter string. */
  442.  
  443.       charold = getc (fileptr);
  444.       if (charold == EOF)
  445.     {
  446.       /* Empty file! */
  447.       if (ferror (fileptr))
  448.         {
  449.           error (0, errno, "error reading %s", *fnamptr);
  450.           errors = 1;
  451.         }
  452.       putc ('\n', stdout);
  453.       continue;        /* Go on to the next file. */
  454.     }
  455.  
  456.       /* `charold' is set up.  Hit it!
  457.      Keep reading characters, stashing them in `charnew';
  458.      output `charold', converting to the appropriate delimiter
  459.      character if needed.  After the EOF, output `charold'
  460.      if it's a newline; otherwise, output it and then a newline. */
  461.  
  462.       while ((charnew = getc (fileptr)) != EOF)
  463.     {
  464.       /* Process the old character. */
  465.       if (charold == '\n')
  466.         {
  467.           if (*delimptr != EMPTY_DELIM)
  468.         putc (*delimptr, stdout);
  469.  
  470.           if (++delimptr == delim_end)
  471.         delimptr = delims;
  472.         }
  473.       else
  474.         putc (charold, stdout);
  475.  
  476.       charold = charnew;
  477.     }
  478.  
  479.       if (ferror (fileptr))
  480.     {
  481.       error (0, errno, "error reading %s", *fnamptr);
  482.       errors = 1;
  483.     }
  484.  
  485.       /* Hit EOF.  Process that last character. */
  486.       putc (charold, stdout);
  487.       if (charold != '\n')
  488.     putc ('\n', stdout);
  489.  
  490.       if (fileptr != stdin)
  491.     fclose (fileptr);
  492.     }
  493.   return errors;
  494. }
  495.  
  496. #ifndef MSDOS            /* We have it in gnulib  */
  497.  
  498. /* Allocate N bytes of memory dynamically, with error checking.  */
  499.  
  500. char *
  501. xmalloc (n)
  502.      unsigned n;
  503. {
  504.   char *p;
  505.  
  506.   p = malloc (n);
  507.   if (p == 0)
  508.     error (2, 0, "virtual memory exhausted");
  509.   return p;
  510. }
  511.  
  512. char *
  513. xrealloc (p, n)
  514.      char *p;
  515.      unsigned n;
  516. {
  517.   p = realloc (p, n);
  518.   if (p == 0)
  519.     error (2, 0, "virtual memory exhausted");
  520.   return p;
  521. }
  522.  
  523. #endif /* not MSDOS */
  524.  
  525. void
  526. usage ()
  527. {
  528.  
  529. #ifdef MSDOS
  530.   fprintf (stderr, "\
  531. Usage: %s [-s] [-d delim-list] [+serial] [+delimiters delim-list]\n\
  532.        [+copying] [+version] [file...]\n",
  533. #else /* not MSDOS */
  534.   fprintf (stderr, "\
  535. Usage: %s [-s] [-d delim-list] [+serial] [+delimiters delim-list]\n\
  536.        [file...]\n",
  537. #endif /* not MSDOS */
  538.        program_name);
  539.   exit (1);
  540. }
  541.