ztoken.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /* Copyright (C) 1994, 2000 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: ztoken.c,v 1.6 2000/09/19 19:00:55 lpd Exp $ */
  16. /* Token reading operators */
  17. #include "string_.h"
  18. #include "ghost.h"
  19. #include "oper.h"
  20. #include "dstack.h" /* for dict_find_name */
  21. #include "estack.h"
  22. #include "gsstruct.h" /* for iscan.h */
  23. #include "stream.h"
  24. #include "files.h"
  25. #include "store.h"
  26. #include "strimpl.h" /* for sfilter.h */
  27. #include "sfilter.h" /* for iscan.h */
  28. #include "idict.h"
  29. #include "iname.h"
  30. #include "iscan.h"
  31. #include "itoken.h" /* for prototypes */
  32. /* <file> token <obj> -true- */
  33. /* <string> token <post> <obj> -true- */
  34. /* <string|file> token -false- */
  35. private int ztoken_continue(P1(i_ctx_t *));
  36. private int token_continue(P4(i_ctx_t *, stream *, scanner_state *, bool));
  37. int
  38. ztoken(i_ctx_t *i_ctx_p)
  39. {
  40. os_ptr op = osp;
  41. switch (r_type(op)) {
  42. default:
  43. return_op_typecheck(op);
  44. case t_file: {
  45. stream *s;
  46. scanner_state state;
  47. check_read_file(s, op);
  48. check_ostack(1);
  49. scanner_state_init(&state, false);
  50. return token_continue(i_ctx_p, s, &state, true);
  51. }
  52. case t_string: {
  53. ref token;
  54. int code = scan_string_token(i_ctx_p, op, &token);
  55. switch (code) {
  56. case scan_EOF: /* no tokens */
  57. make_false(op);
  58. return 0;
  59. default:
  60. if (code < 0)
  61. return code;
  62. }
  63. push(2);
  64. op[-1] = token;
  65. make_true(op);
  66. return 0;
  67. }
  68. }
  69. }
  70. /* Continue reading a token after an interrupt or callout. */
  71. /* *op is the scanner state; op[-1] is the file. */
  72. private int
  73. ztoken_continue(i_ctx_t *i_ctx_p)
  74. {
  75. os_ptr op = osp;
  76. stream *s;
  77. scanner_state *pstate;
  78. check_read_file(s, op - 1);
  79. check_stype(*op, st_scanner_state);
  80. pstate = r_ptr(op, scanner_state);
  81. pop(1);
  82. return token_continue(i_ctx_p, s, pstate, false);
  83. }
  84. /* Common code for token reading. */
  85. private int
  86. token_continue(i_ctx_t *i_ctx_p, stream * s, scanner_state * pstate,
  87. bool save)
  88. {
  89. os_ptr op = osp;
  90. int code;
  91. ref token;
  92. /* Note that scan_token may change osp! */
  93. /* Also, we must temporarily remove the file from the o-stack */
  94. /* when calling scan_token, in case we are scanning a procedure. */
  95. ref fref;
  96. ref_assign(&fref, op);
  97. again:
  98. pop(1);
  99. code = scan_token(i_ctx_p, s, &token, pstate);
  100. op = osp;
  101. switch (code) {
  102. default: /* error */
  103. if (code > 0) /* comment, not possible */
  104. code = gs_note_error(e_syntaxerror);
  105. push(1);
  106. ref_assign(op, &fref);
  107. break;
  108. case scan_BOS:
  109. code = 0;
  110. case 0: /* read a token */
  111. push(2);
  112. ref_assign(op - 1, &token);
  113. make_true(op);
  114. break;
  115. case scan_EOF: /* no tokens */
  116. push(1);
  117. make_false(op);
  118. code = 0;
  119. break;
  120. case scan_Refill: /* need more data */
  121. push(1);
  122. ref_assign(op, &fref);
  123. code = scan_handle_refill(i_ctx_p, op, pstate, save, false,
  124. ztoken_continue);
  125. switch (code) {
  126. case 0: /* state is not copied to the heap */
  127. goto again;
  128. case o_push_estack:
  129. return code;
  130. }
  131. break; /* error */
  132. }
  133. if (code <= 0 && !save) { /* Deallocate the scanner state record. */
  134. ifree_object(pstate, "token_continue");
  135. }
  136. return code;
  137. }
  138. /* <file> .tokenexec - */
  139. /* Read a token and do what the interpreter would do with it. */
  140. /* This is different from token + exec because literal procedures */
  141. /* are not executed (although binary object sequences ARE executed). */
  142. int ztokenexec_continue(P1(i_ctx_t *)); /* export for interpreter */
  143. private int tokenexec_continue(P4(i_ctx_t *, stream *, scanner_state *, bool));
  144. int
  145. ztokenexec(i_ctx_t *i_ctx_p)
  146. {
  147. os_ptr op = osp;
  148. stream *s;
  149. scanner_state state;
  150. check_read_file(s, op);
  151. check_estack(1);
  152. scanner_state_init(&state, false);
  153. return tokenexec_continue(i_ctx_p, s, &state, true);
  154. }
  155. /* Continue reading a token for execution after an interrupt or callout. */
  156. /* *op is the scanner state; op[-1] is the file. */
  157. /* We export this because this is how the interpreter handles a */
  158. /* scan_Refill for an executable file. */
  159. int
  160. ztokenexec_continue(i_ctx_t *i_ctx_p)
  161. {
  162. os_ptr op = osp;
  163. stream *s;
  164. scanner_state *pstate;
  165. check_read_file(s, op - 1);
  166. check_stype(*op, st_scanner_state);
  167. pstate = r_ptr(op, scanner_state);
  168. pop(1);
  169. return tokenexec_continue(i_ctx_p, s, pstate, false);
  170. }
  171. /* Common code for token reading + execution. */
  172. private int
  173. tokenexec_continue(i_ctx_t *i_ctx_p, stream * s, scanner_state * pstate,
  174. bool save)
  175. {
  176. os_ptr op = osp;
  177. int code;
  178. /* Note that scan_token may change osp! */
  179. /* Also, we must temporarily remove the file from the o-stack */
  180. /* when calling scan_token, in case we are scanning a procedure. */
  181. ref fref;
  182. ref_assign(&fref, op);
  183. pop(1);
  184. again:
  185. check_estack(1);
  186. code = scan_token(i_ctx_p, s, (ref *) (esp + 1), pstate);
  187. op = osp;
  188. switch (code) {
  189. case 0:
  190. if (r_is_proc(esp + 1)) { /* Treat procedure as a literal. */
  191. push(1);
  192. ref_assign(op, esp + 1);
  193. code = 0;
  194. break;
  195. }
  196. /* falls through */
  197. case scan_BOS:
  198. ++esp;
  199. code = o_push_estack;
  200. break;
  201. case scan_EOF: /* no tokens */
  202. code = 0;
  203. break;
  204. case scan_Refill: /* need more data */
  205. code = scan_handle_refill(i_ctx_p, &fref, pstate, save, true,
  206. ztokenexec_continue);
  207. switch (code) {
  208. case 0: /* state is not copied to the heap */
  209. goto again;
  210. case o_push_estack:
  211. return code;
  212. }
  213. break; /* error */
  214. case scan_Comment:
  215. case scan_DSC_Comment:
  216. return ztoken_handle_comment(i_ctx_p, &fref, pstate, esp + 1, code,
  217. save, true, ztokenexec_continue);
  218. default: /* error */
  219. break;
  220. }
  221. if (code < 0) { /* Push the operand back on the stack. */
  222. push(1);
  223. ref_assign(op, &fref);
  224. }
  225. if (!save) { /* Deallocate the scanner state record. */
  226. ifree_object(pstate, "token_continue");
  227. }
  228. return code;
  229. }
  230. /*
  231. * Handle a scan_Comment or scan_DSC_Comment return from scan_token
  232. * (scan_code) by calling out to %Process[DSC]Comment. The continuation
  233. * procedure expects the file and scanner state on the o-stack.
  234. */
  235. int
  236. ztoken_handle_comment(i_ctx_t *i_ctx_p, const ref *fop, scanner_state *sstate,
  237. const ref *ptoken, int scan_code,
  238. bool save, bool push_file, op_proc_t cont)
  239. {
  240. const char *proc_name;
  241. scanner_state *pstate;
  242. os_ptr op;
  243. ref *ppcproc;
  244. int code;
  245. switch (scan_code) {
  246. case scan_Comment:
  247. proc_name = "%ProcessComment";
  248. break;
  249. case scan_DSC_Comment:
  250. proc_name = "%ProcessDSCComment";
  251. break;
  252. default:
  253. return_error(e_Fatal); /* can't happen */
  254. }
  255. /*
  256. * We can't use check_ostack here, because it returns on overflow.
  257. */
  258. /*check_ostack(2);*/
  259. if (ostop - osp < 2) {
  260. code = ref_stack_extend(&o_stack, 2);
  261. if (code < 0)
  262. return code;
  263. }
  264. check_estack(4);
  265. code = name_enter_string(proc_name, esp + 4);
  266. if (code < 0)
  267. return code;
  268. if (save) {
  269. pstate = ialloc_struct(scanner_state, &st_scanner_state,
  270. "ztoken_handle_comment");
  271. if (pstate == 0)
  272. return_error(e_VMerror);
  273. *pstate = *sstate;
  274. } else
  275. pstate = sstate;
  276. /* Save the token now -- it might be on the e-stack. */
  277. if (!pstate->s_pstack)
  278. osp[2] = *ptoken;
  279. /*
  280. * Push the continuation, scanner state, file, and callout procedure
  281. * on the e-stack.
  282. */
  283. make_op_estack(esp + 1, cont);
  284. make_istruct(esp + 2, 0, pstate);
  285. esp[3] = *fop;
  286. r_clear_attrs(esp + 3, a_executable);
  287. ppcproc = dict_find_name(esp + 4);
  288. if (ppcproc == 0) {
  289. /*
  290. * This can only happen during initialization.
  291. * Pop the comment string from the o-stack if needed (see below).
  292. */
  293. if (pstate->s_pstack)
  294. --osp;
  295. esp += 3; /* do run the continuation */
  296. } else {
  297. /*
  298. * Push the file and comment string on the o-stack.
  299. * If we were inside { }, the comment string is already on the stack.
  300. */
  301. if (pstate->s_pstack) {
  302. op = ++osp;
  303. *op = op[-1];
  304. } else {
  305. op = osp += 2;
  306. /* *op = *ptoken; */ /* saved above */
  307. }
  308. op[-1] = *fop;
  309. esp[4] = *ppcproc;
  310. esp += 4;
  311. }
  312. return o_push_estack;
  313. }
  314. /*
  315. * Update the cached scanner_options in the context state after doing a
  316. * setuserparams. (We might move this procedure somewhere else eventually.)
  317. */
  318. int
  319. ztoken_scanner_options(const ref *upref, int old_options)
  320. {
  321. typedef struct named_scanner_option_s {
  322. const char *pname;
  323. int option;
  324. } named_scanner_option_t;
  325. static const named_scanner_option_t named_options[2] = {
  326. {"ProcessComment", SCAN_PROCESS_COMMENTS},
  327. {"ProcessDSCComment", SCAN_PROCESS_DSC_COMMENTS}
  328. };
  329. int options = old_options;
  330. int i;
  331. for (i = 0; i < countof(named_options); ++i) {
  332. const named_scanner_option_t *pnso = &named_options[i];
  333. ref *ppcproc;
  334. int code = dict_find_string(upref, pnso->pname, &ppcproc);
  335. /* Update the options only if the parameter has changed. */
  336. if (code >= 0) {
  337. if (r_has_type(ppcproc, t_null))
  338. options &= ~pnso->option;
  339. else
  340. options |= pnso->option;
  341. }
  342. }
  343. return options;
  344. }
  345. /* ------ Initialization procedure ------ */
  346. const op_def ztoken_op_defs[] =
  347. {
  348. {"1token", ztoken},
  349. {"1.tokenexec", ztokenexec},
  350. /* Internal operators */
  351. {"2%ztokenexec_continue", ztokenexec_continue},
  352. op_def_end(0)
  353. };