home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / g / gs252src.zip / GS252 / ECHOGS.C < prev    next >
C/C++ Source or Header  |  1992-09-19  |  5KB  |  177 lines

  1. /* Copyright (C) 1992 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* echogs.c */
  21. /* 'echo'-like utility */
  22. #include <stdio.h>
  23. #include <ctype.h>
  24. #include <string.h>
  25. #include <time.h>        /* for ctime */
  26.  
  27. /*
  28.  * Usage:
  29.     echogs [-w[b] file | -a[b] file] [-n]
  30.       (-D | -x hexstring | -q string | -s | -i | -r file)*
  31.       [-] string*
  32.  * Echoes string(s), or the binary equivalent of hexstring(s).
  33.  * If -w, writes to file; if -a, appends to file; if neither,
  34.  * writes to stdout.  -wb and -ab open the file in binary mode.
  35.  * If -n, does not append a newline to the output.  -s writes a space.
  36.  * -D means insert the date and time.
  37.  * -i means read from stdin, treating each line as an argument.
  38.  * -r means read from a named file in the same way.
  39.  * -X means treat any following literals as hex rather than string data.
  40.  * - alone means treat the rest of the line as literal data,
  41.  * even if the first string begins with a -.
  42.  * Inserts spaces automatically between the trailing strings,
  43.  * but nowhere else; in particular,
  44.     echogs -q a b
  45.  * writes 'ab', in contrast to
  46.     echogs -q a -s b
  47.  * which writes 'a b'.
  48.  *
  49.  * This program exists solely to get around omissions, problems, and
  50.  * incompatibilities in the various shells and utility environments
  51.  * that Ghostscript must deal with.  Don't count on it staying the same
  52.  * from one Ghostscript release to another!
  53.  */
  54.  
  55. main(argc, argv)
  56.     int argc;
  57.     char *argv[];
  58. {    FILE *out = stdout;
  59.     FILE *in;
  60.     char *fmode;
  61.     char *fname = 0;
  62.     int newline = 1;
  63.     int interact = 0;
  64. #define LINESIZE 1000
  65.     char line[LINESIZE];
  66.     char sw = 0, sp = 0, hexx = 0;
  67.     char **argp = argv + 1;
  68.     int nargs = argc - 1;
  69.     if ( nargs > 0 && (*argp)[0] == '-' &&
  70.           ((*argp)[1] == 'w' || (*argp)[1] == 'a')
  71.        )
  72.     {    if ( nargs < 2 ) return 1;
  73.         fmode = *argp + 1;
  74.         fname = argp[1];
  75.         argp += 2, nargs -= 2;
  76.     }
  77.     if ( nargs > 0 && !strcmp(*argp, "-n") )
  78.     {    newline = 0;
  79.         argp++, nargs--;
  80.     }
  81.     if ( fname != 0 )
  82.     {    out = fopen(fname, fmode);
  83.         if ( out == 0 ) return 1;
  84.     }
  85.     while ( 1 )
  86.     {    char *arg;
  87.         if ( interact )
  88.         {    if ( fgets(line, LINESIZE, in) == NULL )
  89.             {    interact = 0;
  90.                 if ( in != stdin ) fclose(in);
  91.                 continue;
  92.             }
  93.             /* Remove the terminating \n. */
  94.             line[strlen(line) - 1] = 0;
  95.             arg = line;
  96.         }
  97.         else
  98.         {    if ( nargs == 0 ) break;
  99.             arg = *argp;
  100.             argp++, nargs--;
  101.         }
  102.         if ( sw == 0 && arg[0] == '-' )
  103.         {    sp = 0;
  104.             switch ( arg[1] )
  105.             {
  106.             case 'q':        /* literal string */
  107.             case 'r':        /* read from a file */
  108.             case 'x':        /* hex string */
  109.                 sw = arg[1];
  110.                 break;
  111.             case 's':        /* write a space */
  112.                 fputc(' ', out);
  113.                 break;
  114.             case 'i':        /* read interactively */
  115.                 interact = 1;
  116.                 in = stdin;
  117.                 break;
  118.             case 'D':        /* insert date/time */
  119.             {    long t;
  120.                 char str[26];
  121.                 time(&t);
  122.                 strcpy(str, ctime(&t));
  123.                 str[24] = 0;    /* remove \n */
  124.                 fputs(str, out);
  125.             }    break;
  126.             case 'X':        /* treat literals as hex */
  127.                 hexx = 1;
  128.                 break;
  129.             case 0:            /* just '-' */
  130.                 sw = '-';
  131.                 break;
  132.             }
  133.         }
  134.         else
  135.           switch ( sw )
  136.         {
  137.         case 0:
  138.         case '-':
  139.             if ( hexx ) goto xx;
  140.             if ( sp ) fputc(' ', out);
  141.             fputs(arg, out);
  142.             sp = 1;
  143.             break;
  144.         case 'q':
  145.             sw = 0;
  146.             fputs(arg, out);
  147.             break;
  148.         case 'r':
  149.             sw = 0;
  150.             in = fopen(arg, "r");
  151.             if ( in == NULL ) exit(1);
  152.             interact = 1;
  153.             break;
  154.         case 'x':
  155. xx:        {    char *xp;
  156.             unsigned int xchr = 1;
  157.             for ( xp = arg; *xp; xp++ )
  158.             {    char ch = *xp;
  159.                 if ( !isxdigit(ch) ) return 1;
  160.                 xchr <<= 4;
  161.                 xchr += (isdigit(ch) ? ch - '0' :
  162.                      (isupper(ch) ? tolower(ch) : ch)
  163.                       - 'a' + 10);
  164.                 if ( xchr >= 0x100 )
  165.                 {    fputc(xchr & 0xff, out);
  166.                     xchr = 1;
  167.                 }
  168.             }
  169.         }    sw = 0;
  170.             break;
  171.         }
  172.     }
  173.     if ( newline ) fputc('\n', out);
  174.     if ( out != stdout ) fclose(out);
  175.     return 0;
  176. }
  177.