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