home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / sharewar / dos / program / gs300sr1 / gs300sr1.exe / ECHOGS.C < prev    next >
C/C++ Source or Header  |  1994-07-30  |  6KB  |  243 lines

  1. /* Copyright (C) 1992, 1994 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* echogs.c */
  20. /* 'echo'-like utility */
  21. #include <stdio.h>
  22. /* Some brain-damaged environments (e.g. Sun) don't include */
  23. /* prototypes for fputc/fputs in stdio.h! */
  24. extern int fputc(), fputs();
  25. /* Some systems have time_t in sys/types.h rather than time.h. */
  26. #include <sys/types.h>
  27. #include <ctype.h>
  28. #include <string.h>
  29. #include <time.h>        /* for ctime */
  30. /* The VMS environment uses different values for success/failure exits: */
  31. #ifdef VMS
  32. #include <stdlib.h>
  33. #  define exit_OK 1
  34. #  define exit_FAILED 18
  35. #else
  36. #  define exit_OK 0
  37. #  define exit_FAILED 1
  38. #endif
  39.  
  40. /*
  41.  * This program exists solely to get around omissions, problems, and
  42.  * incompatibilities in various shells and utility environments.
  43.  * Don't count on it staying the same from one release to another!
  44.  */
  45.  
  46. /*
  47.  * Usage:
  48.     echogs [-e .extn] [-(w|a)[b] file] [-h] [-n]
  49.       (-D | -x hexstring | -q string | -s | -i | -r file | -R file | -X)*
  50.       [-] string*
  51.  * Echoes string(s), or the binary equivalent of hexstring(s).
  52.  * If -w, writes to file; if -a, appends to file; if neither,
  53.  *   writes to stdout.  -wb and -ab open the file in binary mode.
  54.  * -e specifies an extension to be added to the file name.
  55.  * If -h, write the output in hex instead of literally.
  56.  * If -n, does not append a newline to the output.
  57.  * -D means insert the date and time.
  58.  * -q means write the next string literally.
  59.  * -s means write a space.
  60.  * -i means read from stdin, treating each line as an argument.
  61.  * -r means read from a named file in the same way.
  62.  * -R means read from a named file with no interpretation
  63.  *   (but convert to hex if -h is in effect).
  64.  * -X means treat any following literals as hex rather than string data.
  65.  * - alone means treat the rest of the line as literal data,
  66.  *   even if the first string begins with a -.
  67.  * Inserts spaces automatically between the trailing strings,
  68.  * but nowhere else; in particular,
  69.     echogs -q a b
  70.  * writes 'ab', in contrast to
  71.     echogs -q a -s b
  72.  * which writes 'a b'.
  73.  */
  74.  
  75. static int hputc(), hputs();
  76.  
  77. main(argc, argv)
  78.     int argc;
  79.     char *argv[];
  80. {    FILE *out = stdout;
  81.     FILE *in;
  82.     char *extn = "";
  83.     char *fmode;
  84. #define FNSIZE 100
  85.     char fname[FNSIZE];
  86.     int newline = 1;
  87.     int interact = 0;
  88.     int (*eputc)() = fputc, (*eputs)() = fputs;
  89. #define LINESIZE 1000
  90.     char line[LINESIZE];
  91.     char sw = 0, sp = 0, hexx = 0;
  92.     char **argp = argv + 1;
  93.     int nargs = argc - 1;
  94.     if ( nargs > 0 && !strcmp(*argp, "-e") )
  95.     {    if ( nargs < 2 ) return 1;
  96.         extn = argp[1];
  97.         argp += 2, nargs -= 2;
  98.     }
  99.     if ( nargs > 0 && (*argp)[0] == '-' &&
  100.           ((*argp)[1] == 'w' || (*argp)[1] == 'a')
  101.        )
  102.     {    if ( nargs < 2 ) return 1;
  103.         fmode = *argp + 1;
  104.         strcpy(fname, argp[1]);
  105.         strcat(fname, extn);
  106.         argp += 2, nargs -= 2;
  107.     }
  108.     else
  109.         strcpy(fname, "");
  110.     if ( nargs > 0 && !strcmp(*argp, "-h") )
  111.     {    eputc = hputc, eputs = hputs;
  112.         argp++, nargs--;
  113.     }
  114.     if ( nargs > 0 && !strcmp(*argp, "-n") )
  115.     {    newline = 0;
  116.         argp++, nargs--;
  117.     }
  118.     if ( strlen(fname) != 0 )
  119.     {    out = fopen(fname, fmode);
  120.         if ( out == 0 ) return 1;
  121.     }
  122.     while ( 1 )
  123.     {    char *arg;
  124.         if ( interact )
  125.         {    if ( fgets(line, LINESIZE, in) == NULL )
  126.             {    interact = 0;
  127.                 if ( in != stdin ) fclose(in);
  128.                 continue;
  129.             }
  130.             /* Remove the terminating \n. */
  131.             line[strlen(line) - 1] = 0;
  132.             arg = line;
  133.         }
  134.         else
  135.         {    if ( nargs == 0 ) break;
  136.             arg = *argp;
  137.             argp++, nargs--;
  138.         }
  139.         if ( sw == 0 && arg[0] == '-' )
  140.         {    sp = 0;
  141.             switch ( arg[1] )
  142.             {
  143.             case 'q':        /* literal string */
  144.             case 'r':        /* read from a file */
  145.             case 'R':        /* insert file literally */
  146.             case 'x':        /* hex string */
  147.                 sw = arg[1];
  148.                 break;
  149.             case 's':        /* write a space */
  150.                 (*eputc)(' ', out);
  151.                 break;
  152.             case 'i':        /* read interactively */
  153.                 interact = 1;
  154.                 in = stdin;
  155.                 break;
  156.             case 'D':        /* insert date/time */
  157.             {    time_t t;
  158.                 char str[26];
  159.                 time(&t);
  160.                 strcpy(str, ctime(&t));
  161.                 str[24] = 0;    /* remove \n */
  162.                 (*eputs)(str, out);
  163.             }    break;
  164.             case 'X':        /* treat literals as hex */
  165.                 hexx = 1;
  166.                 break;
  167.             case 0:            /* just '-' */
  168.                 sw = '-';
  169.                 break;
  170.             }
  171.         }
  172.         else
  173.           switch ( sw )
  174.         {
  175.         case 0:
  176.         case '-':
  177.             if ( hexx ) goto xx;
  178.             if ( sp ) (*eputc)(' ', out);
  179.             (*eputs)(arg, out);
  180.             sp = 1;
  181.             break;
  182.         case 'q':
  183.             sw = 0;
  184.             (*eputs)(arg, out);
  185.             break;
  186.         case 'r':
  187.             sw = 0;
  188.             in = fopen(arg, "r");
  189.             if ( in == NULL ) exit(exit_FAILED);
  190.             interact = 1;
  191.             break;
  192.         case 'R':
  193.             sw = 0;
  194.             in = fopen(arg, "r");
  195.             if ( in == NULL ) exit(exit_FAILED);
  196.             {    int count;
  197.                 while ( (count = fread(line, 1, 1, in)) > 0 )
  198.                   (*eputc)(line[0], out);
  199.             }
  200.             fclose(in);
  201.             break;
  202.         case 'x':
  203. xx:        {    char *xp;
  204.             unsigned int xchr = 1;
  205.             for ( xp = arg; *xp; xp++ )
  206.             {    char ch = *xp;
  207.                 if ( !isxdigit(ch) ) return 1;
  208.                 xchr <<= 4;
  209.                 xchr += (isdigit(ch) ? ch - '0' :
  210.                      (isupper(ch) ? tolower(ch) : ch)
  211.                       - 'a' + 10);
  212.                 if ( xchr >= 0x100 )
  213.                 {    (*eputc)(xchr & 0xff, out);
  214.                     xchr = 1;
  215.                 }
  216.             }
  217.         }    sw = 0;
  218.             break;
  219.         }
  220.     }
  221.     if ( newline ) (*eputc)('\n', out);
  222.     if ( out != stdout ) fclose(out);
  223.     return exit_OK;
  224. }
  225.  
  226. static int
  227. hputc(ch, out)
  228.     int ch;
  229.     FILE *out;
  230. {    static char *hex = "0123456789abcdef";
  231.     putc(hex[ch >> 4], out);
  232.     putc(hex[ch & 0xf], out);
  233.     return 0;
  234. }
  235.  
  236. static int
  237. hputs(str, out)
  238.     char *str;
  239.     FILE *out;
  240. {    while ( *str ) hputc(*str++ & 0xff, out);
  241.     return 0;
  242. }
  243.