echogs.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /* Copyright (C) 1992, 1995, 1997, 1998, 1999 Aladdin Enterprises. All rights reserved.
  2. This file is part of AFPL Ghostscript.
  3. AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND. No author or
  4. distributor accepts any responsibility for the consequences of using it, or
  5. for whether it serves any particular purpose or works at all, unless he or
  6. she says so in writing. Refer to the Aladdin Free Public License (the
  7. "License") for full details.
  8. Every copy of AFPL Ghostscript must include a copy of the License, normally
  9. in a plain ASCII text file named PUBLIC. The License grants you the right
  10. to copy, modify and redistribute AFPL Ghostscript, but only under certain
  11. conditions described in the License. Among other things, the License
  12. requires that the copyright notice and this notice be preserved on all
  13. copies.
  14. */
  15. /*$Id: echogs.c,v 1.3 2001/02/22 03:59:39 raph Exp $ */
  16. /* 'echo'-like utility */
  17. #include "stdpre.h"
  18. #include <stdio.h>
  19. #if defined(__sun__) && !defined(const)
  20. /* Apparently, there are archaic Sun platforms which don't include
  21. * prototypes for fputc/fputs in stdio.h. The following prototype can
  22. * cause type mismatches if const has been defined (usually with
  23. * -Dconst=), so it's only included if const is undefined.
  24. */
  25. extern int fputc(P2(int, FILE *)), fputs(P2(const char *, FILE *));
  26. #endif
  27. /* Some systems have time_t in sys/types.h rather than time.h. */
  28. #include <sys/types.h>
  29. #include <ctype.h>
  30. #include <string.h>
  31. #include <time.h> /* for ctime */
  32. #ifdef VMS
  33. #include <stdlib.h>
  34. #endif
  35. /*
  36. * This program exists solely to get around omissions, problems, and
  37. * incompatibilities in various shells and utility environments.
  38. * Don't count on it staying the same from one release to another!
  39. */
  40. /*
  41. * Usage:
  42. echogs [-e .extn] [-(w|a)[b][-] file] [-h] [-n]
  43. (-b|-B | -d|-D | -f|-F | -x hexstring | -(l|q|Q) string | -(l|q|Q)string |
  44. -s | -u string | -i | -r file | -R file | -X)*
  45. [-] string*
  46. * Echoes string(s), or the binary equivalent of hexstring(s).
  47. * If -w, writes to file; if -a, appends to file; if neither,
  48. * writes to stdout. -wb and -ab open the file in binary mode.
  49. * -w and -a search forward for the next argument that is not a switch.
  50. * An appended - means the same as - alone, taking effect after the file
  51. * argument.
  52. * -e specifies an extension to be added to the file name.
  53. * If -h, write the output in hex instead of literally.
  54. * If -n, does not append a newline to the output.
  55. * -b or -B means insert the base part (minus directories) of the file name
  56. * passed as the argument of -w or -a.
  57. * -d or -D means insert the date and time.
  58. * -f or -F means insert the file name passed as the argument of -w or -a.
  59. * -q means write the next string literally.
  60. * -l or -Q means the same as -q followed by -s.
  61. * -s means write a space.
  62. * -u means convert the next string to upper case.
  63. * -i means read from stdin, treating each line as an argument.
  64. * -r means read from a named file in the same way.
  65. * -R means copy a named file with no interpretation
  66. * (but convert to hex if -h is in effect).
  67. * -X means treat any following literals as hex rather than string data.
  68. * - or -+ alone means treat the rest of the line as literal data,
  69. * even if the first string begins with a -.
  70. * -+<letter> is equivalent to -<Letter>, i.e., it upper-cases the letter.
  71. * Inserts spaces automatically between the trailing strings,
  72. * but nowhere else; in particular,
  73. echogs -q a b
  74. * writes 'ab', in contrast to
  75. echogs -q a -s b
  76. * which writes 'a b'.
  77. */
  78. static int hputc(P2(int, FILE *)), hputs(P2(const char *, FILE *));
  79. int
  80. main(int argc, char *argv[])
  81. {
  82. FILE *out = stdout;
  83. /*
  84. * The initialization in = 0 is unnecessary: in is only referenced if
  85. * interact = 1, in which case in has always been initialized.
  86. * We initialize in = 0 solely to pacify stupid compilers.
  87. */
  88. FILE *in = 0;
  89. const char *extn = "";
  90. char fmode[4];
  91. #define FNSIZE 100
  92. char *fnparam;
  93. char fname[FNSIZE];
  94. int newline = 1;
  95. int interact = 0;
  96. int (*eputc)(P2(int, FILE *)) = fputc;
  97. int (*eputs)(P2(const char *, FILE *)) = fputs;
  98. #define LINESIZE 1000
  99. char line[LINESIZE];
  100. char sw = 0, sp = 0, hexx = 0;
  101. char **argp = argv + 1;
  102. int nargs = argc - 1;
  103. if (nargs > 0 && !strcmp(*argp, "-e")) {
  104. if (nargs < 2)
  105. return 1;
  106. extn = argp[1];
  107. argp += 2, nargs -= 2;
  108. }
  109. if (nargs > 0 && (*argp)[0] == '-' &&
  110. ((*argp)[1] == 'w' || (*argp)[1] == 'a')
  111. ) {
  112. size_t len = strlen(*argp);
  113. int i;
  114. if (len > 4)
  115. return 1;
  116. for (i = 1; i < nargs; i++)
  117. if (argp[i][0] != '-')
  118. break;
  119. if (i == nargs)
  120. return 1;
  121. fnparam = argp[i];
  122. strcpy(fmode, *argp + 1);
  123. strcpy(fname, fnparam);
  124. strcat(fname, extn);
  125. if (fmode[len - 2] == '-') {
  126. /*
  127. * The referents of argp are actually const, but they can't be
  128. * declared that way, so we have to make a writable constant.
  129. */
  130. static char dash[2] = { '-', 0 };
  131. fmode[len - 2] = 0;
  132. argp[i] = dash;
  133. argp++, nargs--;
  134. } else {
  135. for (; i > 1; i--)
  136. argp[i] = argp[i - 1];
  137. argp += 2, nargs -= 2;
  138. }
  139. } else
  140. strcpy(fname, "");
  141. if (nargs > 0 && !strcmp(*argp, "-h")) {
  142. eputc = hputc, eputs = hputs;
  143. argp++, nargs--;
  144. }
  145. if (nargs > 0 && !strcmp(*argp, "-n")) {
  146. newline = 0;
  147. argp++, nargs--;
  148. }
  149. if (strlen(fname) != 0) {
  150. out = fopen(fname, fmode);
  151. if (out == 0)
  152. return 1;
  153. }
  154. while (1) {
  155. char *arg;
  156. if (interact) {
  157. if (fgets(line, LINESIZE, in) == NULL) {
  158. interact = 0;
  159. if (in != stdin)
  160. fclose(in);
  161. continue;
  162. }
  163. /* Remove the terminating \n. */
  164. line[strlen(line) - 1] = 0;
  165. arg = line;
  166. } else {
  167. if (nargs == 0)
  168. break;
  169. arg = *argp;
  170. argp++, nargs--;
  171. }
  172. if (sw == 0 && arg[0] == '-') {
  173. char chr = arg[1];
  174. sp = 0;
  175. swc:switch (chr) {
  176. case 'l': /* literal string, then -s */
  177. chr = 'Q';
  178. /* falls through */
  179. case 'q': /* literal string */
  180. case 'Q': /* literal string, then -s */
  181. if (arg[2] != 0) {
  182. (*eputs) (arg + 2, out);
  183. if (chr == 'Q')
  184. (*eputc) (' ', out);
  185. break;
  186. }
  187. /* falls through */
  188. case 'r': /* read from a file */
  189. case 'R':
  190. case 'u': /* upper-case string */
  191. case 'x': /* hex string */
  192. sw = chr;
  193. break;
  194. case 's': /* write a space */
  195. (*eputc) (' ', out);
  196. break;
  197. case 'i': /* read interactively */
  198. interact = 1;
  199. in = stdin;
  200. break;
  201. case 'b': /* insert base file name */
  202. case 'B':
  203. arg = fnparam + strlen(fnparam);
  204. while (arg > fnparam &&
  205. (isalnum(arg[-1]) || arg[-1] == '_'))
  206. --arg;
  207. (*eputs) (arg, out);
  208. break;
  209. case 'd': /* insert date/time */
  210. case 'D':
  211. {
  212. time_t t;
  213. char str[26];
  214. time(&t);
  215. strcpy(str, ctime(&t));
  216. str[24] = 0; /* remove \n */
  217. (*eputs) (str, out);
  218. } break;
  219. case 'f': /* insert file name */
  220. case 'F':
  221. (*eputs) (fnparam, out);
  222. break;
  223. case 'X': /* treat literals as hex */
  224. hexx = 1;
  225. break;
  226. case '+': /* upper-case command */
  227. if (arg[1]) {
  228. ++arg;
  229. chr = toupper(arg[1]);
  230. goto swc;
  231. }
  232. /* falls through */
  233. case 0: /* just '-' */
  234. sw = '-';
  235. break;
  236. }
  237. } else
  238. switch (sw) {
  239. case 0:
  240. case '-':
  241. if (hexx)
  242. goto xx;
  243. if (sp)
  244. (*eputc) (' ', out);
  245. (*eputs) (arg, out);
  246. sp = 1;
  247. break;
  248. case 'q':
  249. sw = 0;
  250. (*eputs) (arg, out);
  251. break;
  252. case 'Q':
  253. sw = 0;
  254. (*eputs) (arg, out);
  255. (*eputc) (' ', out);
  256. break;
  257. case 'r':
  258. sw = 0;
  259. in = fopen(arg, "r");
  260. if (in == NULL)
  261. exit(exit_FAILED);
  262. interact = 1;
  263. break;
  264. case 'R':
  265. sw = 0;
  266. in = fopen(arg, "r");
  267. if (in == NULL)
  268. exit(exit_FAILED);
  269. while (fread(line, 1, 1, in) > 0)
  270. (*eputc) (line[0], out);
  271. fclose(in);
  272. break;
  273. case 'u':
  274. {
  275. char *up;
  276. for (up = arg; *up; up++)
  277. (*eputc) (toupper(*up), out);
  278. }
  279. sw = 0;
  280. break;
  281. case 'x':
  282. xx:{
  283. char *xp;
  284. unsigned int xchr = 1;
  285. for (xp = arg; *xp; xp++) {
  286. char ch = *xp;
  287. if (!isxdigit(ch))
  288. return 1;
  289. xchr <<= 4;
  290. xchr += (isdigit(ch) ? ch - '0' :
  291. (isupper(ch) ? tolower(ch) : ch)
  292. - 'a' + 10);
  293. if (xchr >= 0x100) {
  294. (*eputc) (xchr & 0xff, out);
  295. xchr = 1;
  296. }
  297. }
  298. }
  299. sw = 0;
  300. break;
  301. }
  302. }
  303. if (newline)
  304. (*eputc) ('\n', out);
  305. if (out != stdout)
  306. fclose(out);
  307. return exit_OK;
  308. }
  309. static int
  310. hputc(int ch, FILE * out)
  311. {
  312. static const char *hex = "0123456789abcdef";
  313. /* In environments where char is signed, ch may be negative (!). */
  314. putc(hex[(ch >> 4) & 0xf], out);
  315. putc(hex[ch & 0xf], out);
  316. return 0;
  317. }
  318. static int
  319. hputs(const char *str, FILE * out)
  320. {
  321. while (*str)
  322. hputc(*str++ & 0xff, out);
  323. return 0;
  324. }