zfileio.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  1. /* Copyright (C) 1989, 2000, 2001 Aladdin Enterprises. 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: zfileio.c,v 1.18 2005/08/30 23:19:01 ray Exp $ */
  14. /* File I/O operators */
  15. #include "memory_.h"
  16. #include "ghost.h"
  17. #include "gp.h"
  18. #include "oper.h"
  19. #include "stream.h"
  20. #include "files.h"
  21. #include "store.h"
  22. #include "strimpl.h" /* for ifilter.h */
  23. #include "ifilter.h" /* for procedure streams */
  24. #include "interp.h" /* for gs_errorinfo_put_string */
  25. #include "gsmatrix.h" /* for gxdevice.h */
  26. #include "gxdevice.h"
  27. #include "gxdevmem.h"
  28. #include "estack.h"
  29. /* Forward references */
  30. private int write_string(ref *, stream *);
  31. private int handle_read_status(i_ctx_t *, int, const ref *, const uint *,
  32. op_proc_t);
  33. private int handle_write_status(i_ctx_t *, int, const ref *, const uint *,
  34. op_proc_t);
  35. /* ------ Operators ------ */
  36. /* <file> closefile - */
  37. int
  38. zclosefile(i_ctx_t *i_ctx_p)
  39. {
  40. os_ptr op = osp;
  41. stream *s;
  42. check_type(*op, t_file);
  43. if (file_is_valid(s, op)) { /* closing a closed file is a no-op */
  44. int status = sclose(s);
  45. if (status != 0 && status != EOFC) {
  46. if (s_is_writing(s))
  47. return handle_write_status(i_ctx_p, status, op, NULL,
  48. zclosefile);
  49. else
  50. return handle_read_status(i_ctx_p, status, op, NULL,
  51. zclosefile);
  52. }
  53. }
  54. pop(1);
  55. return 0;
  56. }
  57. /* <file> read <int> -true- */
  58. /* <file> read -false- */
  59. private int
  60. zread(i_ctx_t *i_ctx_p)
  61. {
  62. os_ptr op = osp;
  63. stream *s;
  64. int ch;
  65. check_read_file(s, op);
  66. /* We 'push' first in case of ostack block overflow and the */
  67. /* usual case is we will need to push anyway. If we get EOF */
  68. /* we will need to 'pop' and decrement the 'op' pointer. */
  69. /* This is required since the 'push' macro might return with*/
  70. /* stackoverflow which will result in another stack block */
  71. /* added on, then the operator being retried. We can't read */
  72. /* (sgetc) prior to having a place on the ostack to return */
  73. /* the character. */
  74. push(1);
  75. ch = sgetc(s);
  76. if (ch >= 0) {
  77. make_int(op - 1, ch);
  78. make_bool(op, 1);
  79. } else {
  80. pop(1); /* Adjust ostack back from preparatory 'pop' */
  81. op--;
  82. if (ch == EOFC)
  83. make_bool(op, 0);
  84. else
  85. return handle_read_status(i_ctx_p, ch, op, NULL, zread);
  86. }
  87. return 0;
  88. }
  89. /* <file> <int> write - */
  90. int
  91. zwrite(i_ctx_t *i_ctx_p)
  92. {
  93. os_ptr op = osp;
  94. stream *s;
  95. byte ch;
  96. int status;
  97. check_write_file(s, op - 1);
  98. check_type(*op, t_integer);
  99. ch = (byte) op->value.intval;
  100. status = sputc(s, (byte) ch);
  101. if (status >= 0) {
  102. pop(2);
  103. return 0;
  104. }
  105. return handle_write_status(i_ctx_p, status, op - 1, NULL, zwrite);
  106. }
  107. /* <file> <string> readhexstring <substring> <filled_bool> */
  108. private int zreadhexstring_continue(i_ctx_t *);
  109. /* We keep track of the odd digit in the next byte of the string */
  110. /* beyond the bytes already used. (This is just for convenience; */
  111. /* we could do the same thing by passing 2 state parameters to the */
  112. /* continuation procedure instead of 1.) */
  113. private int
  114. zreadhexstring_at(i_ctx_t *i_ctx_p, os_ptr op, uint start)
  115. {
  116. stream *s;
  117. uint len, nread;
  118. byte *str;
  119. int odd;
  120. stream_cursor_write cw;
  121. int status;
  122. check_read_file(s, op - 1);
  123. /*check_write_type(*op, t_string); *//* done by caller */
  124. str = op->value.bytes;
  125. len = r_size(op);
  126. if (start < len) {
  127. odd = str[start];
  128. if (odd > 0xf)
  129. odd = -1;
  130. } else
  131. odd = -1;
  132. cw.ptr = str + start - 1;
  133. cw.limit = str + len - 1;
  134. for (;;) {
  135. status = s_hex_process(&s->cursor.r, &cw, &odd,
  136. hex_ignore_garbage);
  137. if (status == 1) { /* filled the string */
  138. ref_assign_inline(op - 1, op);
  139. make_true(op);
  140. return 0;
  141. } else if (status != 0) /* error or EOF */
  142. break;
  143. /* Didn't fill, keep going. */
  144. status = spgetc(s);
  145. if (status < 0)
  146. break;
  147. sputback(s);
  148. }
  149. nread = cw.ptr + 1 - str;
  150. if (status != EOFC) { /* Error */
  151. if (nread < len)
  152. str[nread] = (odd < 0 ? 0x10 : odd);
  153. return handle_read_status(i_ctx_p, status, op - 1, &nread,
  154. zreadhexstring_continue);
  155. }
  156. /* Reached end-of-file before filling the string. */
  157. /* Return an appropriate substring. */
  158. ref_assign_inline(op - 1, op);
  159. r_set_size(op - 1, nread);
  160. make_false(op);
  161. return 0;
  162. }
  163. private int
  164. zreadhexstring(i_ctx_t *i_ctx_p)
  165. {
  166. os_ptr op = osp;
  167. check_write_type(*op, t_string);
  168. if (r_size(op) > 0)
  169. *op->value.bytes = 0x10;
  170. return zreadhexstring_at(i_ctx_p, op, 0);
  171. }
  172. /* Continue a readhexstring operation after a callout. */
  173. /* *op is the index within the string. */
  174. private int
  175. zreadhexstring_continue(i_ctx_t *i_ctx_p)
  176. {
  177. os_ptr op = osp;
  178. int code;
  179. check_type(*op, t_integer);
  180. if (op->value.intval < 0 || op->value.intval > r_size(op - 1))
  181. return_error(e_rangecheck);
  182. check_write_type(op[-1], t_string);
  183. code = zreadhexstring_at(i_ctx_p, op - 1, (uint) op->value.intval);
  184. if (code >= 0)
  185. pop(1);
  186. return code;
  187. }
  188. /* <file> <string> writehexstring - */
  189. private int zwritehexstring_continue(i_ctx_t *);
  190. private int
  191. zwritehexstring_at(i_ctx_t *i_ctx_p, os_ptr op, uint odd)
  192. {
  193. register stream *s;
  194. register byte ch;
  195. register const byte *p;
  196. register const char *const hex_digits = "0123456789abcdef";
  197. register uint len;
  198. int status;
  199. #define MAX_HEX 128
  200. byte buf[MAX_HEX];
  201. check_write_file(s, op - 1);
  202. check_read_type(*op, t_string);
  203. p = op->value.bytes;
  204. len = r_size(op);
  205. while (len) {
  206. uint len1 = min(len, MAX_HEX / 2);
  207. register byte *q = buf;
  208. uint count = len1;
  209. ref rbuf;
  210. do {
  211. ch = *p++;
  212. *q++ = hex_digits[ch >> 4];
  213. *q++ = hex_digits[ch & 0xf];
  214. }
  215. while (--count);
  216. r_set_size(&rbuf, (len1 << 1) - odd);
  217. rbuf.value.bytes = buf + odd;
  218. status = write_string(&rbuf, s);
  219. switch (status) {
  220. default:
  221. return_error(e_ioerror);
  222. case 0:
  223. len -= len1;
  224. odd = 0;
  225. continue;
  226. case INTC:
  227. case CALLC:
  228. count = rbuf.value.bytes - buf;
  229. op->value.bytes += count >> 1;
  230. r_set_size(op, len - (count >> 1));
  231. count &= 1;
  232. return handle_write_status(i_ctx_p, status, op - 1, &count,
  233. zwritehexstring_continue);
  234. }
  235. }
  236. pop(2);
  237. return 0;
  238. #undef MAX_HEX
  239. }
  240. private int
  241. zwritehexstring(i_ctx_t *i_ctx_p)
  242. {
  243. os_ptr op = osp;
  244. return zwritehexstring_at(i_ctx_p, op, 0);
  245. }
  246. /* Continue a writehexstring operation after a callout. */
  247. /* *op is the odd/even hex digit flag for the first byte. */
  248. private int
  249. zwritehexstring_continue(i_ctx_t *i_ctx_p)
  250. {
  251. os_ptr op = osp;
  252. int code;
  253. check_type(*op, t_integer);
  254. if ((op->value.intval & ~1) != 0)
  255. return_error(e_rangecheck);
  256. code = zwritehexstring_at(i_ctx_p, op - 1, (uint) op->value.intval);
  257. if (code >= 0)
  258. pop(1);
  259. return code;
  260. }
  261. /* <file> <string> readstring <substring> <filled_bool> */
  262. private int zreadstring_continue(i_ctx_t *);
  263. private int
  264. zreadstring_at(i_ctx_t *i_ctx_p, os_ptr op, uint start)
  265. {
  266. stream *s;
  267. uint len, rlen;
  268. int status;
  269. check_read_file(s, op - 1);
  270. check_write_type(*op, t_string);
  271. len = r_size(op);
  272. status = sgets(s, op->value.bytes + start, len - start, &rlen);
  273. rlen += start;
  274. switch (status) {
  275. case EOFC:
  276. case 0:
  277. break;
  278. default:
  279. return handle_read_status(i_ctx_p, status, op - 1, &rlen,
  280. zreadstring_continue);
  281. }
  282. /*
  283. * The most recent Adobe specification says that readstring
  284. * must signal a rangecheck if the string length is zero.
  285. * I can't imagine the motivation for this, but we emulate it.
  286. * It's safe to check it here, rather than earlier, because if
  287. * len is zero, sgets will return 0 immediately with rlen = 0.
  288. */
  289. if (len == 0)
  290. return_error(e_rangecheck);
  291. r_set_size(op, rlen);
  292. op[-1] = *op;
  293. make_bool(op, (rlen == len ? 1 : 0));
  294. return 0;
  295. }
  296. private int
  297. zreadstring(i_ctx_t *i_ctx_p)
  298. {
  299. os_ptr op = osp;
  300. return zreadstring_at(i_ctx_p, op, 0);
  301. }
  302. /* Continue a readstring operation after a callout. */
  303. /* *op is the index within the string. */
  304. private int
  305. zreadstring_continue(i_ctx_t *i_ctx_p)
  306. {
  307. os_ptr op = osp;
  308. int code;
  309. check_type(*op, t_integer);
  310. if (op->value.intval < 0 || op->value.intval > r_size(op - 1))
  311. return_error(e_rangecheck);
  312. code = zreadstring_at(i_ctx_p, op - 1, (uint) op->value.intval);
  313. if (code >= 0)
  314. pop(1);
  315. return code;
  316. }
  317. /* <file> <string> writestring - */
  318. private int
  319. zwritestring(i_ctx_t *i_ctx_p)
  320. {
  321. os_ptr op = osp;
  322. stream *s;
  323. int status;
  324. check_write_file(s, op - 1);
  325. check_read_type(*op, t_string);
  326. status = write_string(op, s);
  327. if (status >= 0) {
  328. pop(2);
  329. return 0;
  330. }
  331. return handle_write_status(i_ctx_p, status, op - 1, NULL, zwritestring);
  332. }
  333. /* <file> <string> readline <substring> <bool> */
  334. private int zreadline(i_ctx_t *);
  335. private int zreadline_continue(i_ctx_t *);
  336. /*
  337. * We could handle readline the same way as readstring,
  338. * except for the anomalous situation where we get interrupted
  339. * between the CR and the LF of an end-of-line marker.
  340. * We hack around this in the following way: if we get interrupted
  341. * before we've read any characters, we just restart the readline;
  342. * if we get interrupted at any other time, we use readline_continue;
  343. * we use start=0 (which we have just ruled out as a possible start value
  344. * for readline_continue) to indicate interruption after the CR.
  345. */
  346. private int
  347. zreadline_at(i_ctx_t *i_ctx_p, os_ptr op, uint count, bool in_eol)
  348. {
  349. stream *s;
  350. int status;
  351. gs_string str;
  352. check_read_file(s, op - 1);
  353. check_write_type(*op, t_string);
  354. str.data = op->value.bytes;
  355. str.size = r_size(op);
  356. status = zreadline_from(s, &str, NULL, &count, &in_eol);
  357. switch (status) {
  358. case 0:
  359. case EOFC:
  360. break;
  361. case 1:
  362. return_error(e_rangecheck);
  363. default:
  364. if (count == 0 && !in_eol)
  365. return handle_read_status(i_ctx_p, status, op - 1, NULL,
  366. zreadline);
  367. else {
  368. if (in_eol) {
  369. r_set_size(op, count);
  370. count = 0;
  371. }
  372. return handle_read_status(i_ctx_p, status, op - 1, &count,
  373. zreadline_continue);
  374. }
  375. }
  376. r_set_size(op, count);
  377. op[-1] = *op;
  378. make_bool(op, status == 0);
  379. return 0;
  380. }
  381. private int
  382. zreadline(i_ctx_t *i_ctx_p)
  383. {
  384. os_ptr op = osp;
  385. return zreadline_at(i_ctx_p, op, 0, false);
  386. }
  387. /* Continue a readline operation after a callout. */
  388. /* *op is the index within the string, or 0 for an interrupt after a CR. */
  389. private int
  390. zreadline_continue(i_ctx_t *i_ctx_p)
  391. {
  392. os_ptr op = osp;
  393. uint size = r_size(op - 1);
  394. uint start;
  395. int code;
  396. check_type(*op, t_integer);
  397. if (op->value.intval < 0 || op->value.intval > size)
  398. return_error(e_rangecheck);
  399. start = (uint) op->value.intval;
  400. code = (start == 0 ? zreadline_at(i_ctx_p, op - 1, size, true) :
  401. zreadline_at(i_ctx_p, op - 1, start, false));
  402. if (code >= 0)
  403. pop(1);
  404. return code;
  405. }
  406. /* Internal readline routine. */
  407. /* Returns a stream status value, or 1 if we overflowed the string. */
  408. /* This is exported for %lineedit. */
  409. int
  410. zreadline_from(stream *s, gs_string *buf, gs_memory_t *bufmem,
  411. uint *pcount, bool *pin_eol)
  412. {
  413. sreadline_proc((*readline));
  414. if (zis_stdin(s))
  415. readline = gp_readline;
  416. else
  417. readline = sreadline;
  418. return readline(s, NULL, NULL /*WRONG*/, NULL, buf, bufmem,
  419. pcount, pin_eol, zis_stdin);
  420. }
  421. /* <file> bytesavailable <int> */
  422. private int
  423. zbytesavailable(i_ctx_t *i_ctx_p)
  424. {
  425. os_ptr op = osp;
  426. stream *s;
  427. long avail;
  428. check_read_file(s, op);
  429. switch (savailable(s, &avail)) {
  430. default:
  431. return_error(e_ioerror);
  432. case EOFC:
  433. avail = -1;
  434. case 0:
  435. ;
  436. }
  437. make_int(op, avail);
  438. return 0;
  439. }
  440. /* - flush - */
  441. int
  442. zflush(i_ctx_t *i_ctx_p)
  443. {
  444. stream *s;
  445. int status;
  446. ref rstdout;
  447. int code = zget_stdout(i_ctx_p, &s);
  448. if (code < 0)
  449. return code;
  450. make_stream_file(&rstdout, s, "w");
  451. status = sflush(s);
  452. if (status == 0 || status == EOFC) {
  453. return 0;
  454. }
  455. return
  456. (s_is_writing(s) ?
  457. handle_write_status(i_ctx_p, status, &rstdout, NULL, zflush) :
  458. handle_read_status(i_ctx_p, status, &rstdout, NULL, zflush));
  459. }
  460. /* <file> flushfile - */
  461. private int
  462. zflushfile(i_ctx_t *i_ctx_p)
  463. {
  464. os_ptr op = osp;
  465. stream *s;
  466. int status;
  467. check_type(*op, t_file);
  468. /*
  469. * We think flushfile is a no-op on closed input files, but causes an
  470. * error on closed output files.
  471. */
  472. if (file_is_invalid(s, op)) {
  473. if (r_has_attr(op, a_write))
  474. return_error(e_invalidaccess);
  475. pop(1);
  476. return 0;
  477. }
  478. status = sflush(s);
  479. if (status == 0 || status == EOFC) {
  480. pop(1);
  481. return 0;
  482. }
  483. return
  484. (s_is_writing(s) ?
  485. handle_write_status(i_ctx_p, status, op, NULL, zflushfile) :
  486. handle_read_status(i_ctx_p, status, op, NULL, zflushfile));
  487. }
  488. /* <file> resetfile - */
  489. private int
  490. zresetfile(i_ctx_t *i_ctx_p)
  491. {
  492. os_ptr op = osp;
  493. stream *s;
  494. /* According to Adobe, resetfile is a no-op on closed files. */
  495. check_type(*op, t_file);
  496. if (file_is_valid(s, op))
  497. sreset(s);
  498. pop(1);
  499. return 0;
  500. }
  501. /* <string> print - */
  502. private int
  503. zprint(i_ctx_t *i_ctx_p)
  504. {
  505. os_ptr op = osp;
  506. stream *s;
  507. int status;
  508. ref rstdout;
  509. int code;
  510. check_read_type(*op, t_string);
  511. code = zget_stdout(i_ctx_p, &s);
  512. if (code < 0)
  513. return code;
  514. status = write_string(op, s);
  515. if (status >= 0) {
  516. pop(1);
  517. return 0;
  518. }
  519. /* Convert print to writestring on the fly. */
  520. make_stream_file(&rstdout, s, "w");
  521. code = handle_write_status(i_ctx_p, status, &rstdout, NULL,
  522. zwritestring);
  523. if (code != o_push_estack)
  524. return code;
  525. push(1);
  526. *op = op[-1];
  527. op[-1] = rstdout;
  528. return code;
  529. }
  530. /* <bool> echo - */
  531. private int
  532. zecho(i_ctx_t *i_ctx_p)
  533. {
  534. os_ptr op = osp;
  535. check_type(*op, t_boolean);
  536. /****** NOT IMPLEMENTED YET ******/
  537. pop(1);
  538. return 0;
  539. }
  540. /* ------ Level 2 extensions ------ */
  541. /* <file> fileposition <int> */
  542. private int
  543. zfileposition(i_ctx_t *i_ctx_p)
  544. {
  545. os_ptr op = osp;
  546. stream *s;
  547. check_file(s, op);
  548. /*
  549. * The PLRM says fileposition must give an error for non-seekable
  550. * streams.
  551. */
  552. if (!s_can_seek(s))
  553. return_error(e_ioerror);
  554. make_int(op, stell(s));
  555. return 0;
  556. }
  557. /* <file> .fileposition <int> */
  558. private int
  559. zxfileposition(i_ctx_t *i_ctx_p)
  560. {
  561. os_ptr op = osp;
  562. stream *s;
  563. check_file(s, op);
  564. /*
  565. * This version of fileposition doesn't give the error, so we can
  566. * use it to get the position of string or procedure streams.
  567. */
  568. make_int(op, stell(s));
  569. return 0;
  570. }
  571. /* <file> <int> setfileposition - */
  572. private int
  573. zsetfileposition(i_ctx_t *i_ctx_p)
  574. {
  575. os_ptr op = osp;
  576. stream *s;
  577. check_file(s, op - 1);
  578. check_type(*op, t_integer);
  579. if (sseek(s, op->value.intval) < 0)
  580. return_error(e_ioerror);
  581. pop(2);
  582. return 0;
  583. }
  584. /* ------ Non-standard extensions ------ */
  585. /* <file> .filename <string> true */
  586. /* <file> .filename false */
  587. private int
  588. zfilename(i_ctx_t *i_ctx_p)
  589. {
  590. os_ptr op = osp;
  591. stream *s;
  592. gs_const_string fname;
  593. byte *str;
  594. check_file(s, op);
  595. if (sfilename(s, &fname) < 0) {
  596. make_false(op);
  597. return 0;
  598. }
  599. check_ostack(1);
  600. str = ialloc_string(fname.size, "filename");
  601. if (str == 0)
  602. return_error(e_VMerror);
  603. memcpy(str, fname.data, fname.size);
  604. push(1); /* can't fail */
  605. make_const_string( op - 1 ,
  606. a_all | imemory_space((const struct gs_ref_memory_s*) imemory),
  607. fname.size,
  608. str);
  609. make_true(op);
  610. return 0;
  611. }
  612. /* <file> .isprocfilter <bool> */
  613. private int
  614. zisprocfilter(i_ctx_t *i_ctx_p)
  615. {
  616. os_ptr op = osp;
  617. stream *s;
  618. check_file(s, op);
  619. while (s->strm != 0)
  620. s = s->strm;
  621. make_bool(op, s_is_proc(s));
  622. return 0;
  623. }
  624. /* <file> <string> .peekstring <substring> <filled_bool> */
  625. private int
  626. zpeekstring(i_ctx_t *i_ctx_p)
  627. {
  628. os_ptr op = osp;
  629. stream *s;
  630. uint len, rlen;
  631. check_read_file(s, op - 1);
  632. check_write_type(*op, t_string);
  633. len = r_size(op);
  634. while ((rlen = sbufavailable(s)) < len) {
  635. int status = s->end_status;
  636. switch (status) {
  637. case EOFC:
  638. break;
  639. case 0:
  640. /*
  641. * The following is a HACK. It should reallocate the buffer to hold
  642. * at least len bytes. However, this raises messy problems about
  643. * which allocator to use and how it should interact with restore.
  644. */
  645. if (len >= s->bsize)
  646. return_error(e_rangecheck);
  647. s_process_read_buf(s);
  648. continue;
  649. default:
  650. return handle_read_status(i_ctx_p, status, op - 1, NULL,
  651. zpeekstring);
  652. }
  653. break;
  654. }
  655. if (rlen > len)
  656. rlen = len;
  657. /* Don't remove the data from the buffer. */
  658. memcpy(op->value.bytes, sbufptr(s), rlen);
  659. r_set_size(op, rlen);
  660. op[-1] = *op;
  661. make_bool(op, (rlen == len ? 1 : 0));
  662. return 0;
  663. }
  664. /* <file> <int> .unread - */
  665. private int
  666. zunread(i_ctx_t *i_ctx_p)
  667. {
  668. os_ptr op = osp;
  669. stream *s;
  670. ulong ch;
  671. check_read_file(s, op - 1);
  672. check_type(*op, t_integer);
  673. ch = op->value.intval;
  674. if (ch > 0xff)
  675. return_error(e_rangecheck);
  676. if (sungetc(s, (byte) ch) < 0)
  677. return_error(e_ioerror);
  678. pop(2);
  679. return 0;
  680. }
  681. /* <file> <obj> <==flag> .writecvp - */
  682. private int zwritecvp_continue(i_ctx_t *);
  683. private int
  684. zwritecvp_at(i_ctx_t *i_ctx_p, os_ptr op, uint start, bool first)
  685. {
  686. stream *s;
  687. byte str[100]; /* arbitrary */
  688. ref rstr;
  689. const byte *data = str;
  690. uint len;
  691. int code, status;
  692. check_write_file(s, op - 2);
  693. check_type(*op, t_integer);
  694. code = obj_cvp(op - 1, str, sizeof(str), &len, (int)op->value.intval,
  695. start, imemory);
  696. if (code == e_rangecheck) {
  697. code = obj_string_data(imemory, op - 1, &data, &len);
  698. if (len < start)
  699. return_error(e_rangecheck);
  700. data += start;
  701. len -= start;
  702. }
  703. if (code < 0)
  704. return code;
  705. r_set_size(&rstr, len);
  706. rstr.value.const_bytes = data;
  707. status = write_string(&rstr, s);
  708. switch (status) {
  709. default:
  710. return_error(e_ioerror);
  711. case 0:
  712. break;
  713. case INTC:
  714. case CALLC:
  715. len = start + len - r_size(&rstr);
  716. if (!first)
  717. --osp; /* pop(1) without affecting op */
  718. return handle_write_status(i_ctx_p, status, op - 2, &len,
  719. zwritecvp_continue);
  720. }
  721. if (code == 1) {
  722. if (first)
  723. check_ostack(1);
  724. push_op_estack(zwritecvp_continue);
  725. if (first)
  726. push(1);
  727. make_int(osp, start + len);
  728. return o_push_estack;
  729. }
  730. if (first) /* zwritecvp */
  731. pop(3);
  732. else /* zwritecvp_continue */
  733. pop(4);
  734. return 0;
  735. }
  736. private int
  737. zwritecvp(i_ctx_t *i_ctx_p)
  738. {
  739. return zwritecvp_at(i_ctx_p, osp, 0, true);
  740. }
  741. /* Continue a .writecvp after a callout. */
  742. /* *op is the index within the string. */
  743. private int
  744. zwritecvp_continue(i_ctx_t *i_ctx_p)
  745. {
  746. os_ptr op = osp;
  747. check_type(*op, t_integer);
  748. if (op->value.intval != (uint) op->value.intval)
  749. return_error(e_rangecheck);
  750. return zwritecvp_at(i_ctx_p, op - 1, (uint) op->value.intval, false);
  751. }
  752. /* Callout for stdin */
  753. /* - .needstdin - */
  754. int
  755. zneedstdin(i_ctx_t *i_ctx_p)
  756. {
  757. return e_NeedStdin; /* Interpreter will exit to caller. */
  758. }
  759. /* Callout for stdout */
  760. /* - .needstdout - */
  761. int
  762. zneedstdout(i_ctx_t *i_ctx_p)
  763. {
  764. return e_NeedStdout; /* Interpreter will exit to caller. */
  765. }
  766. /* Callout for stderr */
  767. /* - .needstderr - */
  768. int
  769. zneedstderr(i_ctx_t *i_ctx_p)
  770. {
  771. return e_NeedStderr; /* Interpreter will exit to caller. */
  772. }
  773. /* ------ Initialization procedure ------ */
  774. /* We need to split the table because of the 16-element limit. */
  775. const op_def zfileio1_op_defs[] = {
  776. {"1bytesavailable", zbytesavailable},
  777. {"1closefile", zclosefile},
  778. /* currentfile is in zcontrol.c */
  779. {"1echo", zecho},
  780. {"1.filename", zfilename},
  781. {"1.fileposition", zxfileposition},
  782. {"1fileposition", zfileposition},
  783. {"0flush", zflush},
  784. {"1flushfile", zflushfile},
  785. {"1.isprocfilter", zisprocfilter},
  786. {"2.peekstring", zpeekstring},
  787. {"1print", zprint},
  788. {"1read", zread},
  789. {"2readhexstring", zreadhexstring},
  790. {"2readline", zreadline},
  791. {"2readstring", zreadstring},
  792. op_def_end(0)
  793. };
  794. const op_def zfileio2_op_defs[] = {
  795. {"1resetfile", zresetfile},
  796. {"2setfileposition", zsetfileposition},
  797. {"2.unread", zunread},
  798. {"2write", zwrite},
  799. {"3.writecvp", zwritecvp},
  800. {"2writehexstring", zwritehexstring},
  801. {"2writestring", zwritestring},
  802. /* Internal operators */
  803. {"3%zreadhexstring_continue", zreadhexstring_continue},
  804. {"3%zreadline_continue", zreadline_continue},
  805. {"3%zreadstring_continue", zreadstring_continue},
  806. {"4%zwritecvp_continue", zwritecvp_continue},
  807. {"3%zwritehexstring_continue", zwritehexstring_continue},
  808. {"0.needstdin", zneedstdin},
  809. {"0.needstdout", zneedstdout},
  810. {"0.needstderr", zneedstderr},
  811. op_def_end(0)
  812. };
  813. /* ------ Non-operator routines ------ */
  814. /* Switch a file open for read/write access but currently in write mode */
  815. /* to read mode. */
  816. int
  817. file_switch_to_read(const ref * op)
  818. {
  819. stream *s = fptr(op);
  820. if (s->write_id != r_size(op) || s->file == 0) /* not valid */
  821. return_error(e_invalidaccess);
  822. if (sswitch(s, false) < 0)
  823. return_error(e_ioerror);
  824. s->read_id = s->write_id; /* enable reading */
  825. s->write_id = 0; /* disable writing */
  826. return 0;
  827. }
  828. /* Switch a file open for read/write access but currently in read mode */
  829. /* to write mode. */
  830. int
  831. file_switch_to_write(const ref * op)
  832. {
  833. stream *s = fptr(op);
  834. if (s->read_id != r_size(op) || s->file == 0) /* not valid */
  835. return_error(e_invalidaccess);
  836. if (sswitch(s, true) < 0)
  837. return_error(e_ioerror);
  838. s->write_id = s->read_id; /* enable writing */
  839. s->read_id = 0; /* disable reading */
  840. return 0;
  841. }
  842. /* ------ Internal routines ------ */
  843. /* Write a string on a file. The file and string have been validated. */
  844. /* If the status is INTC or CALLC, updates the string on the o-stack. */
  845. private int
  846. write_string(ref * op, stream * s)
  847. {
  848. const byte *data = op->value.const_bytes;
  849. uint len = r_size(op);
  850. uint wlen;
  851. int status = sputs(s, data, len, &wlen);
  852. switch (status) {
  853. case INTC:
  854. case CALLC:
  855. op->value.const_bytes = data + wlen;
  856. r_set_size(op, len - wlen);
  857. /* falls through */
  858. default: /* 0, EOFC, ERRC */
  859. return status;
  860. }
  861. }
  862. /*
  863. * Look for a stream error message that needs to be copied to
  864. * $error.errorinfo, if any.
  865. */
  866. private int
  867. copy_error_string(i_ctx_t *i_ctx_p, const ref *fop)
  868. {
  869. stream *s;
  870. for (s = fptr(fop); s->strm != 0 && s->state->error_string[0] == 0;)
  871. s = s->strm;
  872. if (s->state->error_string[0]) {
  873. int code = gs_errorinfo_put_string(i_ctx_p, s->state->error_string);
  874. if (code < 0)
  875. return code;
  876. s->state->error_string[0] = 0; /* just do it once */
  877. }
  878. return_error(e_ioerror);
  879. }
  880. /* Handle an exceptional status return from a read stream. */
  881. /* fop points to the ref for the stream. */
  882. /* ch may be any stream exceptional value. */
  883. /* Return 0, 1 (EOF), o_push_estack, or an error. */
  884. private int
  885. handle_read_status(i_ctx_t *i_ctx_p, int ch, const ref * fop,
  886. const uint * pindex, op_proc_t cont)
  887. {
  888. switch (ch) {
  889. default: /* error */
  890. return copy_error_string(i_ctx_p, fop);
  891. case EOFC:
  892. return 1;
  893. case INTC:
  894. case CALLC:
  895. if (pindex) {
  896. ref index;
  897. make_int(&index, *pindex);
  898. return s_handle_read_exception(i_ctx_p, ch, fop, &index, 1,
  899. cont);
  900. } else
  901. return s_handle_read_exception(i_ctx_p, ch, fop, NULL, 0,
  902. cont);
  903. }
  904. }
  905. /* Handle an exceptional status return from a write stream. */
  906. /* fop points to the ref for the stream. */
  907. /* ch may be any stream exceptional value. */
  908. /* Return 0, 1 (EOF), o_push_estack, or an error. */
  909. private int
  910. handle_write_status(i_ctx_t *i_ctx_p, int ch, const ref * fop,
  911. const uint * pindex, op_proc_t cont)
  912. {
  913. switch (ch) {
  914. default: /* error */
  915. return copy_error_string(i_ctx_p, fop);
  916. case EOFC:
  917. return 1;
  918. case INTC:
  919. case CALLC:
  920. if (pindex) {
  921. ref index;
  922. make_int(&index, *pindex);
  923. return s_handle_write_exception(i_ctx_p, ch, fop, &index, 1,
  924. cont);
  925. } else
  926. return s_handle_write_exception(i_ctx_p, ch, fop, NULL, 0,
  927. cont);
  928. }
  929. }