imainarg.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  1. /* Copyright (C) 1996-2003 artofcode LLC. All rights reserved.
  2. This software is provided AS-IS with no warranty, either express or
  3. implied.
  4. This software is distributed under license and may not be copied,
  5. modified or distributed except as expressly authorized under the terms
  6. of the license contained in the file LICENSE in this distribution.
  7. For more information about licensing, please refer to
  8. http://www.ghostscript.com/licensing/. For information on
  9. commercial licensing, go to http://www.artifex.com/licensing/ or
  10. contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  11. San Rafael, CA 94903, U.S.A., +1(415)492-9861.
  12. */
  13. /* $Id: imainarg.c,v 1.34 2004/11/30 20:31:54 ghostgum Exp $ */
  14. /* Command line parsing and dispatching */
  15. #include "ctype_.h"
  16. #include "memory_.h"
  17. #include "string_.h"
  18. #include <stdlib.h> /* for qsort */
  19. #include "ghost.h"
  20. #include "gp.h"
  21. #include "gsargs.h"
  22. #include "gscdefs.h"
  23. #include "gsmalloc.h" /* for gs_malloc_limit */
  24. #include "gsmdebug.h"
  25. #include "gxdevice.h"
  26. #include "gxdevmem.h"
  27. #include "gsdevice.h"
  28. #include "stream.h"
  29. #include "ierrors.h"
  30. #include "estack.h"
  31. #include "ialloc.h"
  32. #include "strimpl.h" /* for sfilter.h */
  33. #include "sfilter.h" /* for iscan.h */
  34. #include "ostack.h" /* must precede iscan.h */
  35. #include "iscan.h"
  36. #include "iconf.h"
  37. #include "imain.h"
  38. #include "imainarg.h"
  39. #include "iapi.h"
  40. #include "iminst.h"
  41. #include "iname.h"
  42. #include "store.h"
  43. #include "files.h" /* requires stream.h */
  44. #include "interp.h"
  45. #include "iutil.h"
  46. #include "ivmspace.h"
  47. #include "vdtrace.h"
  48. /* Import operator procedures */
  49. extern int zflush(i_ctx_t *);
  50. extern int zflushpage(i_ctx_t *);
  51. #ifndef GS_LIB
  52. # define GS_LIB "GS_LIB"
  53. #endif
  54. #ifndef GS_OPTIONS
  55. # define GS_OPTIONS "GS_OPTIONS"
  56. #endif
  57. #ifndef GS_MAX_LIB_DIRS
  58. # define GS_MAX_LIB_DIRS 25
  59. #endif
  60. #ifndef GS_BUG_MAILBOX
  61. # define GS_BUG_MAILBOX "bug-gs@ghostscript.com"
  62. #endif
  63. #define MAX_BUFFERED_SIZE 1024
  64. /* Note: sscanf incorrectly defines its first argument as char * */
  65. /* rather than const char *. This accounts for the ugly casts below. */
  66. /* Redefine puts to use outprintf, */
  67. /* so it will work even without stdio. */
  68. #undef puts
  69. #define puts(mem, str) outprintf(mem, "%s\n", str)
  70. /* Forward references */
  71. #define runInit 1
  72. #define runFlush 2
  73. #define runBuffer 4
  74. private int swproc(gs_main_instance *, const char *, arg_list *);
  75. private int argproc(gs_main_instance *, const char *);
  76. private int run_buffered(gs_main_instance *, const char *);
  77. private int esc_strlen(const char *);
  78. private void esc_strcat(char *, const char *);
  79. private int runarg(gs_main_instance *, const char *, const char *, const char *, int);
  80. private int run_string(gs_main_instance *, const char *, int);
  81. private int run_finish(gs_main_instance *, int, int, ref *);
  82. private int try_stdout_redirect(gs_main_instance * minst,
  83. const char *command, const char *filename);
  84. /* Forward references for help printout */
  85. private void print_help(gs_main_instance *);
  86. private void print_revision(const gs_main_instance *);
  87. private void print_version(const gs_main_instance *);
  88. private void print_usage(const gs_main_instance *);
  89. private void print_devices(const gs_main_instance *);
  90. private void print_emulators(const gs_main_instance *);
  91. private void print_paths(gs_main_instance *);
  92. private void print_help_trailer(const gs_main_instance *);
  93. /* ------ Main program ------ */
  94. /* Process the command line with a given instance. */
  95. private FILE *
  96. gs_main_arg_fopen(const char *fname, void *vminst)
  97. {
  98. gs_main_set_lib_paths((gs_main_instance *) vminst);
  99. return lib_fopen(&((gs_main_instance *)vminst)->lib_path,
  100. ((gs_main_instance *)vminst)->heap, fname);
  101. }
  102. private void
  103. set_debug_flags(const char *arg, char *flags)
  104. {
  105. byte value = (*arg == '-' ? (++arg, 0) : 0xff);
  106. while (*arg)
  107. flags[*arg++ & 127] = value;
  108. }
  109. int
  110. gs_main_init_with_args(gs_main_instance * minst, int argc, char *argv[])
  111. {
  112. const char *arg;
  113. arg_list args;
  114. int code;
  115. arg_init(&args, (const char **)argv, argc,
  116. gs_main_arg_fopen, (void *)minst);
  117. code = gs_main_init0(minst, 0, 0, 0, GS_MAX_LIB_DIRS);
  118. if (code < 0)
  119. return code;
  120. /* This first check is not needed on VMS since GS_LIB evaluates to the same
  121. value as that returned by gs_lib_default_path. Also, since GS_LIB is
  122. defined as a searchlist logical and getenv only returns the first entry
  123. in the searchlist, it really doesn't make sense to search that particular
  124. directory twice.
  125. */
  126. #ifndef __VMS
  127. {
  128. int len = 0;
  129. int code = gp_getenv(GS_LIB, (char *)0, &len);
  130. if (code < 0) { /* key present, value doesn't fit */
  131. char *path = (char *)gs_alloc_bytes(minst->heap, len, "GS_LIB");
  132. gp_getenv(GS_LIB, path, &len); /* can't fail */
  133. minst->lib_path.env = path;
  134. }
  135. }
  136. #endif /* __VMS */
  137. minst->lib_path.final = gs_lib_default_path;
  138. code = gs_main_set_lib_paths(minst);
  139. if (code < 0)
  140. return code;
  141. /* Prescan the command line for --help and --version. */
  142. {
  143. int i;
  144. bool helping = false;
  145. for (i = 1; i < argc; ++i)
  146. if (!strcmp(argv[i], "--")) {
  147. /* A PostScript program will be interpreting all the */
  148. /* remaining switches, so stop scanning. */
  149. helping = false;
  150. break;
  151. } else if (!strcmp(argv[i], "--help")) {
  152. print_help(minst);
  153. helping = true;
  154. } else if (!strcmp(argv[i], "--version")) {
  155. print_version(minst);
  156. puts(minst->heap, ""); /* \n */
  157. helping = true;
  158. }
  159. if (helping)
  160. return e_Info;
  161. }
  162. /* Execute files named in the command line, */
  163. /* processing options along the way. */
  164. /* Wait until the first file name (or the end */
  165. /* of the line) to finish initialization. */
  166. minst->run_start = true;
  167. {
  168. int len = 0;
  169. int code = gp_getenv(GS_OPTIONS, (char *)0, &len);
  170. if (code < 0) { /* key present, value doesn't fit */
  171. char *opts =
  172. (char *)gs_alloc_bytes(minst->heap, len, "GS_OPTIONS");
  173. gp_getenv(GS_OPTIONS, opts, &len); /* can't fail */
  174. if (arg_push_memory_string(&args, opts, minst->heap))
  175. return e_Fatal;
  176. }
  177. }
  178. while ((arg = arg_next(&args, &code)) != 0) {
  179. switch (*arg) {
  180. case '-':
  181. code = swproc(minst, arg, &args);
  182. if (code < 0)
  183. return code;
  184. if (code > 0)
  185. outprintf(minst->heap, "Unknown switch %s - ignoring\n", arg);
  186. break;
  187. default:
  188. code = argproc(minst, arg);
  189. if (code < 0)
  190. return code;
  191. }
  192. }
  193. if (code < 0)
  194. return code;
  195. code = gs_main_init2(minst);
  196. if (code < 0)
  197. return code;
  198. if (!minst->run_start)
  199. return e_Quit;
  200. return code ;
  201. }
  202. /*
  203. * Run the 'start' procedure (after processing the command line).
  204. */
  205. int
  206. gs_main_run_start(gs_main_instance * minst)
  207. {
  208. return run_string(minst, "systemdict /start get exec", runFlush);
  209. }
  210. /* Process switches. Return 0 if processed, 1 for unknown switch, */
  211. /* <0 if error. */
  212. private int
  213. swproc(gs_main_instance * minst, const char *arg, arg_list * pal)
  214. {
  215. char sw = arg[1];
  216. ref vtrue;
  217. int code = 0;
  218. #undef initial_enter_name
  219. #define initial_enter_name(nstr, pvalue)\
  220. i_initial_enter_name(minst->i_ctx_p, nstr, pvalue)
  221. make_true(&vtrue);
  222. arg += 2; /* skip - and letter */
  223. switch (sw) {
  224. default:
  225. return 1;
  226. case 0: /* read stdin as a file char-by-char */
  227. /* This is a ******HACK****** for Ghostview. */
  228. minst->heap->gs_lib_ctx->stdin_is_interactive = true;
  229. goto run_stdin;
  230. case '_': /* read stdin with normal buffering */
  231. minst->heap->gs_lib_ctx->stdin_is_interactive = false;
  232. run_stdin:
  233. minst->run_start = false; /* don't run 'start' */
  234. /* Set NOPAUSE so showpage won't try to read from stdin. */
  235. code = swproc(minst, "-dNOPAUSE", pal);
  236. if (code)
  237. return code;
  238. code = gs_main_init2(minst); /* Finish initialization */
  239. if (code < 0)
  240. return code;
  241. code = run_string(minst, ".runstdin", runFlush);
  242. if (code < 0)
  243. return code;
  244. break;
  245. case '-': /* run with command line args */
  246. case '+':
  247. pal->expand_ats = false;
  248. case '@': /* ditto with @-expansion */
  249. {
  250. const char *psarg = arg_next(pal, &code);
  251. if (code < 0)
  252. return e_Fatal;
  253. if (psarg == 0) {
  254. outprintf(minst->heap, "Usage: gs ... -%c file.ps arg1 ... argn\n", sw);
  255. arg_finit(pal);
  256. return e_Fatal;
  257. }
  258. psarg = arg_copy(psarg, minst->heap);
  259. if (psarg == NULL)
  260. return e_Fatal;
  261. code = gs_main_init2(minst);
  262. if (code < 0)
  263. return code;
  264. code = run_string(minst, "userdict/ARGUMENTS[", 0);
  265. if (code < 0)
  266. return code;
  267. while ((arg = arg_next(pal, &code)) != 0) {
  268. char *fname = arg_copy(arg, minst->heap);
  269. if (fname == NULL)
  270. return e_Fatal;
  271. code = runarg(minst, "", fname, "", runInit);
  272. if (code < 0)
  273. return code;
  274. }
  275. if (code < 0)
  276. return e_Fatal;
  277. runarg(minst, "]put", psarg, ".runfile", runInit | runFlush);
  278. return e_Quit;
  279. }
  280. case 'A': /* trace allocator */
  281. switch (*arg) {
  282. case 0:
  283. gs_alloc_debug = 1;
  284. break;
  285. case '-':
  286. gs_alloc_debug = 0;
  287. break;
  288. default:
  289. puts(minst->heap, "-A may only be followed by -");
  290. return e_Fatal;
  291. }
  292. break;
  293. case 'B': /* set run_string buffer size */
  294. if (*arg == '-')
  295. minst->run_buffer_size = 0;
  296. else {
  297. uint bsize;
  298. if (sscanf((const char *)arg, "%u", &bsize) != 1 ||
  299. bsize <= 0 || bsize > MAX_BUFFERED_SIZE
  300. ) {
  301. outprintf(minst->heap,
  302. "-B must be followed by - or size between 1 and %u\n",
  303. MAX_BUFFERED_SIZE);
  304. return e_Fatal;
  305. }
  306. minst->run_buffer_size = bsize;
  307. }
  308. break;
  309. case 'c': /* code follows */
  310. {
  311. bool ats = pal->expand_ats;
  312. code = gs_main_init2(minst);
  313. if (code < 0)
  314. return code;
  315. pal->expand_ats = false;
  316. while ((arg = arg_next(pal, &code)) != 0) {
  317. char *sarg;
  318. if (arg[0] == '@' ||
  319. (arg[0] == '-' && !isdigit(arg[1]))
  320. )
  321. break;
  322. sarg = arg_copy(arg, minst->heap);
  323. if (sarg == NULL)
  324. return e_Fatal;
  325. code = runarg(minst, "", sarg, ".runstring", 0);
  326. if (code < 0)
  327. return code;
  328. }
  329. if (code < 0)
  330. return e_Fatal;
  331. if (arg != 0) {
  332. char *p = arg_copy(arg, minst->heap);
  333. if (p == NULL)
  334. return e_Fatal;
  335. arg_push_string(pal, p);
  336. }
  337. pal->expand_ats = ats;
  338. break;
  339. }
  340. case 'E': /* log errors */
  341. switch (*arg) {
  342. case 0:
  343. gs_log_errors = 1;
  344. break;
  345. case '-':
  346. gs_log_errors = 0;
  347. break;
  348. default:
  349. puts(minst->heap, "-E may only be followed by -");
  350. return e_Fatal;
  351. }
  352. break;
  353. case 'f': /* run file of arbitrary name */
  354. if (*arg != 0) {
  355. code = argproc(minst, arg);
  356. if (code < 0)
  357. return code;
  358. }
  359. break;
  360. case 'F': /* run file with buffer_size = 1 */
  361. if (!*arg) {
  362. puts(minst->heap, "-F requires a file name");
  363. return e_Fatal;
  364. } {
  365. uint bsize = minst->run_buffer_size;
  366. minst->run_buffer_size = 1;
  367. code = argproc(minst, arg);
  368. minst->run_buffer_size = bsize;
  369. if (code < 0)
  370. return code;
  371. }
  372. break;
  373. case 'g': /* define device geometry */
  374. {
  375. long width, height;
  376. ref value;
  377. if ((code = gs_main_init1(minst)) < 0)
  378. return code;
  379. if (sscanf((const char *)arg, "%ldx%ld", &width, &height) != 2) {
  380. puts(minst->heap, "-g must be followed by <width>x<height>");
  381. return e_Fatal;
  382. }
  383. make_int(&value, width);
  384. initial_enter_name("DEVICEWIDTH", &value);
  385. make_int(&value, height);
  386. initial_enter_name("DEVICEHEIGHT", &value);
  387. initial_enter_name("FIXEDMEDIA", &vtrue);
  388. break;
  389. }
  390. case 'h': /* print help */
  391. case '?': /* ditto */
  392. print_help(minst);
  393. return e_Info; /* show usage info on exit */
  394. case 'I': /* specify search path */
  395. {
  396. char *path = arg_copy(arg, minst->heap);
  397. if (path == NULL)
  398. return e_Fatal;
  399. gs_main_add_lib_path(minst, path);
  400. }
  401. break;
  402. case 'K': /* set malloc limit */
  403. {
  404. long msize = 0;
  405. gs_malloc_memory_t *rawheap = gs_malloc_wrapped_contents(minst->heap);
  406. sscanf((const char *)arg, "%ld", &msize);
  407. if (msize <= 0 || msize > max_long >> 10) {
  408. outprintf(minst->heap, "-K<numK> must have 1 <= numK <= %ld\n",
  409. max_long >> 10);
  410. return e_Fatal;
  411. }
  412. rawheap->limit = msize << 10;
  413. }
  414. break;
  415. case 'M': /* set memory allocation increment */
  416. {
  417. unsigned msize = 0;
  418. sscanf((const char *)arg, "%u", &msize);
  419. #if arch_ints_are_short
  420. if (msize <= 0 || msize >= 64) {
  421. puts(minst->heap, "-M must be between 1 and 63");
  422. return e_Fatal;
  423. }
  424. #endif
  425. minst->memory_chunk_size = msize << 10;
  426. }
  427. break;
  428. case 'N': /* set size of name table */
  429. {
  430. unsigned nsize = 0;
  431. sscanf((const char *)arg, "%d", &nsize);
  432. #if arch_ints_are_short
  433. if (nsize < 2 || nsize > 64) {
  434. puts(minst->heap, "-N must be between 2 and 64");
  435. return e_Fatal;
  436. }
  437. #endif
  438. minst->name_table_size = (ulong) nsize << 10;
  439. }
  440. break;
  441. case 'P': /* choose whether search '.' first */
  442. if (!strcmp(arg, ""))
  443. minst->search_here_first = true;
  444. else if (!strcmp(arg, "-"))
  445. minst->search_here_first = false;
  446. else {
  447. puts(minst->heap, "Only -P or -P- is allowed.");
  448. return e_Fatal;
  449. }
  450. break;
  451. case 'q': /* quiet startup */
  452. if ((code = gs_main_init1(minst)) < 0)
  453. return code;
  454. initial_enter_name("QUIET", &vtrue);
  455. break;
  456. case 'r': /* define device resolution */
  457. {
  458. float xres, yres;
  459. ref value;
  460. if ((code = gs_main_init1(minst)) < 0)
  461. return code;
  462. switch (sscanf((const char *)arg, "%fx%f", &xres, &yres)) {
  463. default:
  464. puts(minst->heap, "-r must be followed by <res> or <xres>x<yres>");
  465. return e_Fatal;
  466. case 1: /* -r<res> */
  467. yres = xres;
  468. case 2: /* -r<xres>x<yres> */
  469. make_real(&value, xres);
  470. initial_enter_name("DEVICEXRESOLUTION", &value);
  471. make_real(&value, yres);
  472. initial_enter_name("DEVICEYRESOLUTION", &value);
  473. initial_enter_name("FIXEDRESOLUTION", &vtrue);
  474. }
  475. break;
  476. }
  477. case 'D': /* define name */
  478. case 'd':
  479. case 'S': /* define name as string */
  480. case 's':
  481. {
  482. char *adef = arg_copy(arg, minst->heap);
  483. char *eqp;
  484. bool isd = (sw == 'D' || sw == 'd');
  485. ref value;
  486. if (adef == NULL)
  487. return e_Fatal;
  488. eqp = strchr(adef, '=');
  489. if (eqp == NULL)
  490. eqp = strchr(adef, '#');
  491. /* Initialize the object memory, scanner, and */
  492. /* name table now if needed. */
  493. if ((code = gs_main_init1(minst)) < 0)
  494. return code;
  495. if (eqp == adef) {
  496. puts(minst->heap, "Usage: -dname, -dname=token, -sname=string");
  497. return e_Fatal;
  498. }
  499. if (eqp == NULL) {
  500. if (isd)
  501. make_true(&value);
  502. else
  503. make_empty_string(&value, a_readonly);
  504. } else {
  505. int code;
  506. i_ctx_t *i_ctx_p = minst->i_ctx_p;
  507. uint space = icurrent_space;
  508. *eqp++ = 0;
  509. ialloc_set_space(idmemory, avm_system);
  510. if (isd) {
  511. stream astream;
  512. scanner_state state;
  513. s_init(&astream, NULL);
  514. sread_string(&astream,
  515. (const byte *)eqp, strlen(eqp));
  516. scanner_state_init(&state, false);
  517. code = scan_token(minst->i_ctx_p, &astream, &value,
  518. &state);
  519. if (code) {
  520. puts(minst->heap, "-dname= must be followed by a valid token");
  521. return e_Fatal;
  522. }
  523. if (r_has_type_attrs(&value, t_name,
  524. a_executable)) {
  525. ref nsref;
  526. name_string_ref(minst->heap, &value, &nsref);
  527. #define string_is(nsref, str, len)\
  528. (r_size(&(nsref)) == (len) &&\
  529. !strncmp((const char *)(nsref).value.const_bytes, str, (len)))
  530. if (string_is(nsref, "null", 4))
  531. make_null(&value);
  532. else if (string_is(nsref, "true", 4))
  533. make_true(&value);
  534. else if (string_is(nsref, "false", 5))
  535. make_false(&value);
  536. else {
  537. puts(minst->heap,
  538. "-dvar=name requires name=null, true, or false");
  539. return e_Fatal;
  540. }
  541. #undef name_is_string
  542. }
  543. } else {
  544. int len = strlen(eqp);
  545. char *str =
  546. (char *)gs_alloc_bytes(minst->heap,
  547. (uint) len, "-s");
  548. if (str == 0) {
  549. lprintf("Out of memory!\n");
  550. return e_Fatal;
  551. }
  552. memcpy(str, eqp, len);
  553. make_const_string(&value,
  554. a_readonly | avm_foreign,
  555. len, (const byte *)str);
  556. if ((code = try_stdout_redirect(minst, adef, eqp)) < 0)
  557. return code;
  558. }
  559. ialloc_set_space(idmemory, space);
  560. }
  561. /* Enter the name in systemdict. */
  562. initial_enter_name(adef, &value);
  563. break;
  564. }
  565. case 'T':
  566. set_debug_flags(arg, vd_flags);
  567. break;
  568. case 'u': /* undefine name */
  569. if (!*arg) {
  570. puts(minst->heap, "-u requires a name to undefine.");
  571. return e_Fatal;
  572. }
  573. if ((code = gs_main_init1(minst)) < 0)
  574. return code;
  575. i_initial_remove_name(minst->i_ctx_p, arg);
  576. break;
  577. case 'v': /* print revision */
  578. print_revision(minst);
  579. return e_Info;
  580. /*#ifdef DEBUG */
  581. /*
  582. * Here we provide a place for inserting debugging code that can be
  583. * run in place of the normal interpreter code.
  584. */
  585. case 'X':
  586. code = gs_main_init2(minst);
  587. if (code < 0)
  588. return code;
  589. {
  590. int xec; /* exit_code */
  591. ref xeo; /* error_object */
  592. #define start_x()\
  593. gs_main_run_string_begin(minst, 1, &xec, &xeo)
  594. #define run_x(str)\
  595. gs_main_run_string_continue(minst, str, strlen(str), 1, &xec, &xeo)
  596. #define stop_x()\
  597. gs_main_run_string_end(minst, 1, &xec, &xeo)
  598. start_x();
  599. run_x("\216\003abc");
  600. run_x("== flush\n");
  601. stop_x();
  602. }
  603. return e_Quit;
  604. /*#endif */
  605. case 'Z':
  606. set_debug_flags(arg, gs_debug);
  607. break;
  608. }
  609. return 0;
  610. }
  611. /* Define versions of strlen and strcat that encode strings in hex. */
  612. /* This is so we can enter escaped characters regardless of whether */
  613. /* the Level 1 convention of ignoring \s in strings-within-strings */
  614. /* is being observed (sigh). */
  615. private int
  616. esc_strlen(const char *str)
  617. {
  618. return strlen(str) * 2 + 2;
  619. }
  620. private void
  621. esc_strcat(char *dest, const char *src)
  622. {
  623. char *d = dest + strlen(dest);
  624. const char *p;
  625. static const char *const hex = "0123456789abcdef";
  626. *d++ = '<';
  627. for (p = src; *p; p++) {
  628. byte c = (byte) * p;
  629. *d++ = hex[c >> 4];
  630. *d++ = hex[c & 0xf];
  631. }
  632. *d++ = '>';
  633. *d = 0;
  634. }
  635. /* Process file names */
  636. private int
  637. argproc(gs_main_instance * minst, const char *arg)
  638. {
  639. int code = gs_main_init1(minst); /* need i_ctx_p to proceed */
  640. char *filearg;
  641. if (code < 0)
  642. return code;
  643. filearg = arg_copy(arg, minst->heap);
  644. if (filearg == NULL)
  645. return e_Fatal;
  646. if (minst->run_buffer_size) {
  647. /* Run file with run_string. */
  648. return run_buffered(minst, filearg);
  649. } else {
  650. /* Run file directly in the normal way. */
  651. return runarg(minst, "", filearg, ".runfile", runInit | runFlush);
  652. }
  653. }
  654. private int
  655. run_buffered(gs_main_instance * minst, const char *arg)
  656. {
  657. FILE *in = gp_fopen(arg, gp_fmode_rb);
  658. int exit_code;
  659. ref error_object;
  660. int code;
  661. if (in == 0) {
  662. outprintf(minst->heap, "Unable to open %s for reading", arg);
  663. return_error(e_invalidfileaccess);
  664. }
  665. code = gs_main_init2(minst);
  666. if (code < 0)
  667. return code;
  668. code = gs_main_run_string_begin(minst, minst->user_errors,
  669. &exit_code, &error_object);
  670. if (!code) {
  671. char buf[MAX_BUFFERED_SIZE];
  672. int count;
  673. code = e_NeedInput;
  674. while ((count = fread(buf, 1, minst->run_buffer_size, in)) > 0) {
  675. code = gs_main_run_string_continue(minst, buf, count,
  676. minst->user_errors,
  677. &exit_code, &error_object);
  678. if (code != e_NeedInput)
  679. break;
  680. }
  681. if (code == e_NeedInput) {
  682. code = gs_main_run_string_end(minst, minst->user_errors,
  683. &exit_code, &error_object);
  684. }
  685. }
  686. fclose(in);
  687. zflush(minst->i_ctx_p);
  688. zflushpage(minst->i_ctx_p);
  689. return run_finish(minst, code, exit_code, &error_object);
  690. }
  691. private int
  692. runarg(gs_main_instance * minst, const char *pre, const char *arg,
  693. const char *post, int options)
  694. {
  695. int len = strlen(pre) + esc_strlen(arg) + strlen(post) + 1;
  696. int code;
  697. char *line;
  698. if (options & runInit) {
  699. code = gs_main_init2(minst); /* Finish initialization */
  700. if (code < 0)
  701. return code;
  702. }
  703. line = (char *)gs_alloc_bytes(minst->heap, len, "argproc");
  704. if (line == 0) {
  705. lprintf("Out of memory!\n");
  706. return_error(e_VMerror);
  707. }
  708. strcpy(line, pre);
  709. esc_strcat(line, arg);
  710. strcat(line, post);
  711. minst->i_ctx_p->starting_arg_file = true;
  712. code = run_string(minst, line, options);
  713. minst->i_ctx_p->starting_arg_file = false;
  714. return code;
  715. }
  716. private int
  717. run_string(gs_main_instance * minst, const char *str, int options)
  718. {
  719. int exit_code;
  720. ref error_object;
  721. int code = gs_main_run_string(minst, str, minst->user_errors,
  722. &exit_code, &error_object);
  723. if ((options & runFlush) || code != 0) {
  724. zflush(minst->i_ctx_p); /* flush stdout */
  725. zflushpage(minst->i_ctx_p); /* force display update */
  726. }
  727. return run_finish(minst, code, exit_code, &error_object);
  728. }
  729. private int
  730. run_finish(gs_main_instance *minst, int code, int exit_code,
  731. ref * perror_object)
  732. {
  733. switch (code) {
  734. case e_Quit:
  735. case 0:
  736. break;
  737. case e_Fatal:
  738. eprintf1("Unrecoverable error, exit code %d\n", exit_code);
  739. break;
  740. default:
  741. gs_main_dump_stack(minst, code, perror_object);
  742. }
  743. return code;
  744. }
  745. /* Redirect stdout to a file:
  746. * -sstdout=filename
  747. * -sstdout=-
  748. * -sstdout=%stdout
  749. * -sstdout=%stderr
  750. * -sOutputFile=- is not affected.
  751. * File is closed at program exit (if not stdout/err)
  752. * or when -sstdout is used again.
  753. */
  754. private int
  755. try_stdout_redirect(gs_main_instance * minst,
  756. const char *command, const char *filename)
  757. {
  758. if (strcmp(command, "stdout") == 0) {
  759. minst->heap->gs_lib_ctx->stdout_to_stderr = 0;
  760. minst->heap->gs_lib_ctx->stdout_is_redirected = 0;
  761. /* If stdout already being redirected and it is not stdout
  762. * or stderr, close it
  763. */
  764. if (minst->heap->gs_lib_ctx->fstdout2
  765. && (minst->heap->gs_lib_ctx->fstdout2 != minst->heap->gs_lib_ctx->fstdout)
  766. && (minst->heap->gs_lib_ctx->fstdout2 != minst->heap->gs_lib_ctx->fstderr)) {
  767. fclose(minst->heap->gs_lib_ctx->fstdout2);
  768. minst->heap->gs_lib_ctx->fstdout2 = (FILE *)NULL;
  769. }
  770. /* If stdout is being redirected, set minst->fstdout2 */
  771. if ( (filename != 0) && strlen(filename) &&
  772. strcmp(filename, "-") && strcmp(filename, "%stdout") ) {
  773. if (strcmp(filename, "%stderr") == 0) {
  774. minst->heap->gs_lib_ctx->stdout_to_stderr = 1;
  775. }
  776. else if ((minst->heap->gs_lib_ctx->fstdout2 =
  777. fopen(filename, "w")) == (FILE *)NULL)
  778. return_error(e_invalidfileaccess);
  779. minst->heap->gs_lib_ctx->stdout_is_redirected = 1;
  780. }
  781. return 0;
  782. }
  783. return 1;
  784. }
  785. /* ---------------- Print information ---------------- */
  786. /*
  787. * Help strings. We have to break them up into parts, because
  788. * the Watcom compiler has a limit of 510 characters for a single token.
  789. * For PC displays, we want to limit the strings to 24 lines.
  790. */
  791. private const char help_usage1[] = "\
  792. Usage: gs [switches] [file1.ps file2.ps ...]\n\
  793. Most frequently used switches: (you can use # in place of =)\n\
  794. -dNOPAUSE no pause after page | -q `quiet', fewer messages\n\
  795. -g<width>x<height> page size in pixels | -r<res> pixels/inch resolution\n";
  796. private const char help_usage2[] = "\
  797. -sDEVICE=<devname> select device | -dBATCH exit after last file\n\
  798. -sOutputFile=<file> select output file: - for stdout, |command for pipe,\n\
  799. embed %d or %ld for page #\n";
  800. private const char help_trailer[] = "\
  801. For more information, see %s.\n\
  802. Report bugs to %s, using the form in Bug-form.htm.\n";
  803. private const char help_devices[] = "Available devices:";
  804. private const char help_default_device[] = "Default output device:";
  805. private const char help_emulators[] = "Input formats:";
  806. private const char help_paths[] = "Search path:";
  807. /* Print the standard help message. */
  808. private void
  809. print_help(gs_main_instance * minst)
  810. {
  811. print_revision(minst);
  812. print_usage(minst);
  813. print_emulators(minst);
  814. print_devices(minst);
  815. print_paths(minst);
  816. if (gs_init_string_sizeof > 0) {
  817. outprintf(minst->heap, "Initialization files are compiled into the executable.\n");
  818. }
  819. print_help_trailer(minst);
  820. }
  821. /* Print the revision, revision date, and copyright. */
  822. private void
  823. print_revision(const gs_main_instance *minst)
  824. {
  825. printf_program_ident(minst->heap, gs_product, gs_revision);
  826. outprintf(minst->heap, " (%d-%02d-%02d)\n%s\n",
  827. (int)(gs_revisiondate / 10000),
  828. (int)(gs_revisiondate / 100 % 100),
  829. (int)(gs_revisiondate % 100),
  830. gs_copyright);
  831. }
  832. /* Print the version number. */
  833. private void
  834. print_version(const gs_main_instance *minst)
  835. {
  836. printf_program_ident(minst->heap, NULL, gs_revision);
  837. }
  838. /* Print usage information. */
  839. private void
  840. print_usage(const gs_main_instance *minst)
  841. {
  842. outprintf(minst->heap, "%s", help_usage1);
  843. outprintf(minst->heap, "%s", help_usage2);
  844. }
  845. /* compare function for qsort */
  846. private int
  847. cmpstr(const void *v1, const void *v2)
  848. {
  849. return strcmp( *(char * const *)v1, *(char * const *)v2 );
  850. }
  851. /* Print the list of available devices. */
  852. private void
  853. print_devices(const gs_main_instance *minst)
  854. {
  855. outprintf(minst->heap, "%s", help_default_device);
  856. outprintf(minst->heap, " %s\n", gs_devicename(gs_getdevice(0)));
  857. outprintf(minst->heap, "%s", help_devices);
  858. {
  859. int i;
  860. int pos = 100;
  861. const gx_device *pdev;
  862. const char **names;
  863. size_t ndev = 0;
  864. for (i = 0; (pdev = gs_getdevice(i)) != 0; i++)
  865. ;
  866. ndev = (size_t)i;
  867. names = (const char **)gs_alloc_bytes(minst->heap, ndev * sizeof(const char*), "print_devices");
  868. if (names == (const char **)NULL) { /* old-style unsorted device list */
  869. for (i = 0; (pdev = gs_getdevice(i)) != 0; i++) {
  870. const char *dname = gs_devicename(pdev);
  871. int len = strlen(dname);
  872. if (pos + 1 + len > 76)
  873. outprintf(minst->heap, "\n "), pos = 2;
  874. outprintf(minst->heap, " %s", dname);
  875. pos += 1 + len;
  876. }
  877. }
  878. else { /* new-style sorted device list */
  879. for (i = 0; (pdev = gs_getdevice(i)) != 0; i++)
  880. names[i] = gs_devicename(pdev);
  881. qsort((void*)names, ndev, sizeof(const char*), cmpstr);
  882. for (i = 0; i < ndev; i++) {
  883. int len = strlen(names[i]);
  884. if (pos + 1 + len > 76)
  885. outprintf(minst->heap, "\n "), pos = 2;
  886. outprintf(minst->heap, " %s", names[i]);
  887. pos += 1 + len;
  888. }
  889. gs_free(minst->heap, (char *)names, ndev * sizeof(const char*), 1, "print_devices");
  890. }
  891. }
  892. outprintf(minst->heap, "\n");
  893. }
  894. /* Print the list of language emulators. */
  895. private void
  896. print_emulators(const gs_main_instance *minst)
  897. {
  898. outprintf(minst->heap, "%s", help_emulators);
  899. {
  900. const ref *pes;
  901. for (pes = gs_emulator_name_array;
  902. pes->value.const_bytes != 0; pes++
  903. )
  904. /*
  905. * Even though gs_emulator_name_array is declared and used as
  906. * an array of string refs, each string is actually a
  907. * (null terminated) C string.
  908. */
  909. outprintf(minst->heap, " %s", (const char *)pes->value.const_bytes);
  910. }
  911. outprintf(minst->heap, "\n");
  912. }
  913. /* Print the search paths. */
  914. private void
  915. print_paths(gs_main_instance * minst)
  916. {
  917. outprintf(minst->heap, "%s", help_paths);
  918. gs_main_set_lib_paths(minst);
  919. {
  920. uint count = r_size(&minst->lib_path.list);
  921. uint i;
  922. int pos = 100;
  923. char fsepr[3];
  924. fsepr[0] = ' ', fsepr[1] = gp_file_name_list_separator,
  925. fsepr[2] = 0;
  926. for (i = 0; i < count; ++i) {
  927. const ref *prdir =
  928. minst->lib_path.list.value.refs + i;
  929. uint len = r_size(prdir);
  930. const char *sepr = (i == count - 1 ? "" : fsepr);
  931. if (1 + pos + strlen(sepr) + len > 76)
  932. outprintf(minst->heap, "\n "), pos = 2;
  933. outprintf(minst->heap, " ");
  934. /*
  935. * This is really ugly, but it's necessary because some
  936. * platforms rely on all console output being funneled through
  937. * outprintf. We wish we could just do:
  938. fwrite(prdir->value.bytes, 1, len, minst->fstdout);
  939. */
  940. {
  941. const char *p = (const char *)prdir->value.bytes;
  942. uint j;
  943. for (j = len; j; j--)
  944. outprintf(minst->heap, "%c", *p++);
  945. }
  946. outprintf(minst->heap, "%s", sepr);
  947. pos += 1 + len + strlen(sepr);
  948. }
  949. }
  950. outprintf(minst->heap, "\n");
  951. }
  952. /* Print the help trailer. */
  953. private void
  954. print_help_trailer(const gs_main_instance *minst)
  955. {
  956. char buffer[gp_file_name_sizeof];
  957. const char *use_htm = "Use.htm", *p = buffer;
  958. uint blen = sizeof(buffer);
  959. if (gp_file_name_combine(gs_doc_directory, strlen(gs_doc_directory),
  960. use_htm, strlen(use_htm), false, buffer, &blen) != gp_combine_success)
  961. p = use_htm;
  962. outprintf(minst->heap, help_trailer, p, GS_BUG_MAILBOX);
  963. }