PageRenderTime 85ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/contrib/groff/src/roff/troff/input.cpp

https://bitbucket.org/freebsd/freebsd-head/
C++ | 8214 lines | 7726 code | 378 blank | 110 comment | 1285 complexity | 62b837aa80c4497c3fc2edb1fd1e5325 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, BSD-3-Clause, LGPL-2.0, LGPL-2.1, BSD-2-Clause, 0BSD, JSON, AGPL-1.0, GPL-2.0
  1. // -*- C++ -*-
  2. /* Copyright (C) 1989, 1990, 1991, 1992, 2000, 2001, 2002, 2003, 2004, 2005
  3. Free Software Foundation, Inc.
  4. Written by James Clark (jjc@jclark.com)
  5. This file is part of groff.
  6. groff is free software; you can redistribute it and/or modify it under
  7. the terms of the GNU General Public License as published by the Free
  8. Software Foundation; either version 2, or (at your option) any later
  9. version.
  10. groff is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. for more details.
  14. You should have received a copy of the GNU General Public License along
  15. with groff; see the file COPYING. If not, write to the Free Software
  16. Foundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA. */
  17. #define DEBUGGING
  18. #include "troff.h"
  19. #include "dictionary.h"
  20. #include "hvunits.h"
  21. #include "stringclass.h"
  22. #include "mtsm.h"
  23. #include "env.h"
  24. #include "request.h"
  25. #include "node.h"
  26. #include "token.h"
  27. #include "div.h"
  28. #include "reg.h"
  29. #include "charinfo.h"
  30. #include "macropath.h"
  31. #include "input.h"
  32. #include "defs.h"
  33. #include "font.h"
  34. #include "unicode.h"
  35. // Needed for getpid() and isatty()
  36. #include "posix.h"
  37. #include "nonposix.h"
  38. #ifdef NEED_DECLARATION_PUTENV
  39. extern "C" {
  40. int putenv(const char *);
  41. }
  42. #endif /* NEED_DECLARATION_PUTENV */
  43. #define MACRO_PREFIX "tmac."
  44. #define MACRO_POSTFIX ".tmac"
  45. #define INITIAL_STARTUP_FILE "troffrc"
  46. #define FINAL_STARTUP_FILE "troffrc-end"
  47. #define DEFAULT_INPUT_STACK_LIMIT 1000
  48. #ifndef DEFAULT_WARNING_MASK
  49. // warnings that are enabled by default
  50. #define DEFAULT_WARNING_MASK \
  51. (WARN_CHAR|WARN_NUMBER|WARN_BREAK|WARN_SPACE|WARN_FONT)
  52. #endif
  53. // initial size of buffer for reading names; expanded as necessary
  54. #define ABUF_SIZE 16
  55. extern "C" const char *program_name;
  56. extern "C" const char *Version_string;
  57. #ifdef COLUMN
  58. void init_column_requests();
  59. #endif /* COLUMN */
  60. static node *read_draw_node();
  61. static void read_color_draw_node(token &);
  62. static void push_token(const token &);
  63. void copy_file();
  64. #ifdef COLUMN
  65. void vjustify();
  66. #endif /* COLUMN */
  67. void transparent_file();
  68. token tok;
  69. int break_flag = 0;
  70. int color_flag = 1; // colors are on by default
  71. static int backtrace_flag = 0;
  72. #ifndef POPEN_MISSING
  73. char *pipe_command = 0;
  74. #endif
  75. charinfo *charset_table[256];
  76. unsigned char hpf_code_table[256];
  77. static int warning_mask = DEFAULT_WARNING_MASK;
  78. static int inhibit_errors = 0;
  79. static int ignoring = 0;
  80. static void enable_warning(const char *);
  81. static void disable_warning(const char *);
  82. static int escape_char = '\\';
  83. static symbol end_macro_name;
  84. static symbol blank_line_macro_name;
  85. static int compatible_flag = 0;
  86. int ascii_output_flag = 0;
  87. int suppress_output_flag = 0;
  88. int is_html = 0;
  89. int begin_level = 0; // number of nested \O escapes
  90. int have_input = 0; // whether \f, \F, \D'F...', \H, \m, \M,
  91. // \R, \s, or \S has been processed in
  92. // token::next()
  93. int old_have_input = 0; // value of have_input right before \n
  94. int tcommand_flag = 0;
  95. int safer_flag = 1; // safer by default
  96. int have_string_arg = 0; // whether we have \*[foo bar...]
  97. double spread_limit = -3.0 - 1.0; // negative means deactivated
  98. double warn_scale;
  99. char warn_scaling_indicator;
  100. int debug_state = 0; // turns on debugging of the html troff state
  101. search_path *mac_path = &safer_macro_path;
  102. // Defaults to the current directory.
  103. search_path include_search_path(0, 0, 0, 1);
  104. static int get_copy(node**, int = 0);
  105. static void copy_mode_error(const char *,
  106. const errarg & = empty_errarg,
  107. const errarg & = empty_errarg,
  108. const errarg & = empty_errarg);
  109. enum read_mode { ALLOW_EMPTY, WITH_ARGS, NO_ARGS };
  110. static symbol read_escape_name(read_mode mode = NO_ARGS);
  111. static symbol read_long_escape_name(read_mode mode = NO_ARGS);
  112. static void interpolate_string(symbol);
  113. static void interpolate_string_with_args(symbol);
  114. static void interpolate_macro(symbol);
  115. static void interpolate_number_format(symbol);
  116. static void interpolate_environment_variable(symbol);
  117. static symbol composite_glyph_name(symbol);
  118. static void interpolate_arg(symbol);
  119. static request_or_macro *lookup_request(symbol);
  120. static int get_delim_number(units *, unsigned char);
  121. static int get_delim_number(units *, unsigned char, units);
  122. static symbol do_get_long_name(int, char);
  123. static int get_line_arg(units *res, unsigned char si, charinfo **cp);
  124. static int read_size(int *);
  125. static symbol get_delim_name();
  126. static void init_registers();
  127. static void trapping_blank_line();
  128. class input_iterator;
  129. input_iterator *make_temp_iterator(const char *);
  130. const char *input_char_description(int);
  131. void process_input_stack();
  132. void chop_macro(); // declare to avoid friend name injection
  133. void set_escape_char()
  134. {
  135. if (has_arg()) {
  136. if (tok.ch() == 0) {
  137. error("bad escape character");
  138. escape_char = '\\';
  139. }
  140. else
  141. escape_char = tok.ch();
  142. }
  143. else
  144. escape_char = '\\';
  145. skip_line();
  146. }
  147. void escape_off()
  148. {
  149. escape_char = 0;
  150. skip_line();
  151. }
  152. static int saved_escape_char = '\\';
  153. void save_escape_char()
  154. {
  155. saved_escape_char = escape_char;
  156. skip_line();
  157. }
  158. void restore_escape_char()
  159. {
  160. escape_char = saved_escape_char;
  161. skip_line();
  162. }
  163. class input_iterator {
  164. public:
  165. input_iterator();
  166. input_iterator(int is_div);
  167. virtual ~input_iterator() {}
  168. int get(node **);
  169. friend class input_stack;
  170. int is_diversion;
  171. statem *diversion_state;
  172. protected:
  173. const unsigned char *ptr;
  174. const unsigned char *eptr;
  175. input_iterator *next;
  176. private:
  177. virtual int fill(node **);
  178. virtual int peek();
  179. virtual int has_args() { return 0; }
  180. virtual int nargs() { return 0; }
  181. virtual input_iterator *get_arg(int) { return 0; }
  182. virtual int get_location(int, const char **, int *) { return 0; }
  183. virtual void backtrace() {}
  184. virtual int set_location(const char *, int) { return 0; }
  185. virtual int next_file(FILE *, const char *) { return 0; }
  186. virtual void shift(int) {}
  187. virtual int is_boundary() {return 0; }
  188. virtual int is_file() { return 0; }
  189. virtual int is_macro() { return 0; }
  190. virtual void save_compatible_flag(int) {}
  191. virtual int get_compatible_flag() { return 0; }
  192. };
  193. input_iterator::input_iterator()
  194. : is_diversion(0), ptr(0), eptr(0)
  195. {
  196. }
  197. input_iterator::input_iterator(int is_div)
  198. : is_diversion(is_div), ptr(0), eptr(0)
  199. {
  200. }
  201. int input_iterator::fill(node **)
  202. {
  203. return EOF;
  204. }
  205. int input_iterator::peek()
  206. {
  207. return EOF;
  208. }
  209. inline int input_iterator::get(node **p)
  210. {
  211. return ptr < eptr ? *ptr++ : fill(p);
  212. }
  213. class input_boundary : public input_iterator {
  214. public:
  215. int is_boundary() { return 1; }
  216. };
  217. class input_return_boundary : public input_iterator {
  218. public:
  219. int is_boundary() { return 2; }
  220. };
  221. class file_iterator : public input_iterator {
  222. FILE *fp;
  223. int lineno;
  224. const char *filename;
  225. int popened;
  226. int newline_flag;
  227. int seen_escape;
  228. enum { BUF_SIZE = 512 };
  229. unsigned char buf[BUF_SIZE];
  230. void close();
  231. public:
  232. file_iterator(FILE *, const char *, int = 0);
  233. ~file_iterator();
  234. int fill(node **);
  235. int peek();
  236. int get_location(int, const char **, int *);
  237. void backtrace();
  238. int set_location(const char *, int);
  239. int next_file(FILE *, const char *);
  240. int is_file();
  241. };
  242. file_iterator::file_iterator(FILE *f, const char *fn, int po)
  243. : fp(f), lineno(1), filename(fn), popened(po),
  244. newline_flag(0), seen_escape(0)
  245. {
  246. if ((font::use_charnames_in_special) && (fn != 0)) {
  247. if (!the_output)
  248. init_output();
  249. the_output->put_filename(fn);
  250. }
  251. }
  252. file_iterator::~file_iterator()
  253. {
  254. close();
  255. }
  256. void file_iterator::close()
  257. {
  258. if (fp == stdin)
  259. clearerr(stdin);
  260. #ifndef POPEN_MISSING
  261. else if (popened)
  262. pclose(fp);
  263. #endif /* not POPEN_MISSING */
  264. else
  265. fclose(fp);
  266. }
  267. int file_iterator::is_file()
  268. {
  269. return 1;
  270. }
  271. int file_iterator::next_file(FILE *f, const char *s)
  272. {
  273. close();
  274. filename = s;
  275. fp = f;
  276. lineno = 1;
  277. newline_flag = 0;
  278. seen_escape = 0;
  279. popened = 0;
  280. ptr = 0;
  281. eptr = 0;
  282. return 1;
  283. }
  284. int file_iterator::fill(node **)
  285. {
  286. if (newline_flag)
  287. lineno++;
  288. newline_flag = 0;
  289. unsigned char *p = buf;
  290. ptr = p;
  291. unsigned char *e = p + BUF_SIZE;
  292. while (p < e) {
  293. int c = getc(fp);
  294. if (c == EOF)
  295. break;
  296. if (invalid_input_char(c))
  297. warning(WARN_INPUT, "invalid input character code %1", int(c));
  298. else {
  299. *p++ = c;
  300. if (c == '\n') {
  301. seen_escape = 0;
  302. newline_flag = 1;
  303. break;
  304. }
  305. seen_escape = (c == '\\');
  306. }
  307. }
  308. if (p > buf) {
  309. eptr = p;
  310. return *ptr++;
  311. }
  312. else {
  313. eptr = p;
  314. return EOF;
  315. }
  316. }
  317. int file_iterator::peek()
  318. {
  319. int c = getc(fp);
  320. while (invalid_input_char(c)) {
  321. warning(WARN_INPUT, "invalid input character code %1", int(c));
  322. c = getc(fp);
  323. }
  324. if (c != EOF)
  325. ungetc(c, fp);
  326. return c;
  327. }
  328. int file_iterator::get_location(int /*allow_macro*/,
  329. const char **filenamep, int *linenop)
  330. {
  331. *linenop = lineno;
  332. if (filename != 0 && strcmp(filename, "-") == 0)
  333. *filenamep = "<standard input>";
  334. else
  335. *filenamep = filename;
  336. return 1;
  337. }
  338. void file_iterator::backtrace()
  339. {
  340. errprint("%1:%2: backtrace: %3 `%1'\n", filename, lineno,
  341. popened ? "process" : "file");
  342. }
  343. int file_iterator::set_location(const char *f, int ln)
  344. {
  345. if (f) {
  346. filename = f;
  347. if (!the_output)
  348. init_output();
  349. the_output->put_filename(f);
  350. }
  351. lineno = ln;
  352. return 1;
  353. }
  354. input_iterator nil_iterator;
  355. class input_stack {
  356. public:
  357. static int get(node **);
  358. static int peek();
  359. static void push(input_iterator *);
  360. static input_iterator *get_arg(int);
  361. static int nargs();
  362. static int get_location(int, const char **, int *);
  363. static int set_location(const char *, int);
  364. static void backtrace();
  365. static void backtrace_all();
  366. static void next_file(FILE *, const char *);
  367. static void end_file();
  368. static void shift(int n);
  369. static void add_boundary();
  370. static void add_return_boundary();
  371. static int is_return_boundary();
  372. static void remove_boundary();
  373. static int get_level();
  374. static int get_div_level();
  375. static void increase_level();
  376. static void decrease_level();
  377. static void clear();
  378. static void pop_macro();
  379. static void save_compatible_flag(int);
  380. static int get_compatible_flag();
  381. static statem *get_diversion_state();
  382. static void check_end_diversion(input_iterator *t);
  383. static int limit;
  384. static int div_level;
  385. static statem *diversion_state;
  386. private:
  387. static input_iterator *top;
  388. static int level;
  389. static int finish_get(node **);
  390. static int finish_peek();
  391. };
  392. input_iterator *input_stack::top = &nil_iterator;
  393. int input_stack::level = 0;
  394. int input_stack::limit = DEFAULT_INPUT_STACK_LIMIT;
  395. int input_stack::div_level = 0;
  396. statem *input_stack::diversion_state = NULL;
  397. int suppress_push=0;
  398. inline int input_stack::get_level()
  399. {
  400. return level;
  401. }
  402. inline void input_stack::increase_level()
  403. {
  404. level++;
  405. }
  406. inline void input_stack::decrease_level()
  407. {
  408. level--;
  409. }
  410. inline int input_stack::get_div_level()
  411. {
  412. return div_level;
  413. }
  414. inline int input_stack::get(node **np)
  415. {
  416. int res = (top->ptr < top->eptr) ? *top->ptr++ : finish_get(np);
  417. if (res == '\n') {
  418. old_have_input = have_input;
  419. have_input = 0;
  420. }
  421. return res;
  422. }
  423. int input_stack::finish_get(node **np)
  424. {
  425. for (;;) {
  426. int c = top->fill(np);
  427. if (c != EOF || top->is_boundary())
  428. return c;
  429. if (top == &nil_iterator)
  430. break;
  431. input_iterator *tem = top;
  432. check_end_diversion(tem);
  433. #if defined(DEBUGGING)
  434. if (debug_state)
  435. if (tem->is_diversion)
  436. fprintf(stderr,
  437. "in diversion level = %d\n", input_stack::get_div_level());
  438. #endif
  439. top = top->next;
  440. level--;
  441. delete tem;
  442. if (top->ptr < top->eptr)
  443. return *top->ptr++;
  444. }
  445. assert(level == 0);
  446. return EOF;
  447. }
  448. inline int input_stack::peek()
  449. {
  450. return (top->ptr < top->eptr) ? *top->ptr : finish_peek();
  451. }
  452. void input_stack::check_end_diversion(input_iterator *t)
  453. {
  454. if (t->is_diversion) {
  455. div_level--;
  456. diversion_state = t->diversion_state;
  457. }
  458. }
  459. int input_stack::finish_peek()
  460. {
  461. for (;;) {
  462. int c = top->peek();
  463. if (c != EOF || top->is_boundary())
  464. return c;
  465. if (top == &nil_iterator)
  466. break;
  467. input_iterator *tem = top;
  468. check_end_diversion(tem);
  469. top = top->next;
  470. level--;
  471. delete tem;
  472. if (top->ptr < top->eptr)
  473. return *top->ptr;
  474. }
  475. assert(level == 0);
  476. return EOF;
  477. }
  478. void input_stack::add_boundary()
  479. {
  480. push(new input_boundary);
  481. }
  482. void input_stack::add_return_boundary()
  483. {
  484. push(new input_return_boundary);
  485. }
  486. int input_stack::is_return_boundary()
  487. {
  488. return top->is_boundary() == 2;
  489. }
  490. void input_stack::remove_boundary()
  491. {
  492. assert(top->is_boundary());
  493. input_iterator *temp = top->next;
  494. check_end_diversion(top);
  495. delete top;
  496. top = temp;
  497. level--;
  498. }
  499. void input_stack::push(input_iterator *in)
  500. {
  501. if (in == 0)
  502. return;
  503. if (++level > limit && limit > 0)
  504. fatal("input stack limit exceeded (probable infinite loop)");
  505. in->next = top;
  506. top = in;
  507. if (top->is_diversion) {
  508. div_level++;
  509. in->diversion_state = diversion_state;
  510. diversion_state = curenv->construct_state(0);
  511. #if defined(DEBUGGING)
  512. if (debug_state) {
  513. curenv->dump_troff_state();
  514. fflush(stderr);
  515. }
  516. #endif
  517. }
  518. #if defined(DEBUGGING)
  519. if (debug_state)
  520. if (top->is_diversion) {
  521. fprintf(stderr,
  522. "in diversion level = %d\n", input_stack::get_div_level());
  523. fflush(stderr);
  524. }
  525. #endif
  526. }
  527. statem *get_diversion_state()
  528. {
  529. return input_stack::get_diversion_state();
  530. }
  531. statem *input_stack::get_diversion_state()
  532. {
  533. if (diversion_state == NULL)
  534. return NULL;
  535. else
  536. return new statem(diversion_state);
  537. }
  538. input_iterator *input_stack::get_arg(int i)
  539. {
  540. input_iterator *p;
  541. for (p = top; p != 0; p = p->next)
  542. if (p->has_args())
  543. return p->get_arg(i);
  544. return 0;
  545. }
  546. void input_stack::shift(int n)
  547. {
  548. for (input_iterator *p = top; p; p = p->next)
  549. if (p->has_args()) {
  550. p->shift(n);
  551. return;
  552. }
  553. }
  554. int input_stack::nargs()
  555. {
  556. for (input_iterator *p =top; p != 0; p = p->next)
  557. if (p->has_args())
  558. return p->nargs();
  559. return 0;
  560. }
  561. int input_stack::get_location(int allow_macro, const char **filenamep, int *linenop)
  562. {
  563. for (input_iterator *p = top; p; p = p->next)
  564. if (p->get_location(allow_macro, filenamep, linenop))
  565. return 1;
  566. return 0;
  567. }
  568. void input_stack::backtrace()
  569. {
  570. const char *f;
  571. int n;
  572. // only backtrace down to (not including) the topmost file
  573. for (input_iterator *p = top;
  574. p && !p->get_location(0, &f, &n);
  575. p = p->next)
  576. p->backtrace();
  577. }
  578. void input_stack::backtrace_all()
  579. {
  580. for (input_iterator *p = top; p; p = p->next)
  581. p->backtrace();
  582. }
  583. int input_stack::set_location(const char *filename, int lineno)
  584. {
  585. for (input_iterator *p = top; p; p = p->next)
  586. if (p->set_location(filename, lineno))
  587. return 1;
  588. return 0;
  589. }
  590. void input_stack::next_file(FILE *fp, const char *s)
  591. {
  592. input_iterator **pp;
  593. for (pp = &top; *pp != &nil_iterator; pp = &(*pp)->next)
  594. if ((*pp)->next_file(fp, s))
  595. return;
  596. if (++level > limit && limit > 0)
  597. fatal("input stack limit exceeded");
  598. *pp = new file_iterator(fp, s);
  599. (*pp)->next = &nil_iterator;
  600. }
  601. void input_stack::end_file()
  602. {
  603. for (input_iterator **pp = &top; *pp != &nil_iterator; pp = &(*pp)->next)
  604. if ((*pp)->is_file()) {
  605. input_iterator *tem = *pp;
  606. check_end_diversion(tem);
  607. *pp = (*pp)->next;
  608. delete tem;
  609. level--;
  610. return;
  611. }
  612. }
  613. void input_stack::clear()
  614. {
  615. int nboundaries = 0;
  616. while (top != &nil_iterator) {
  617. if (top->is_boundary())
  618. nboundaries++;
  619. input_iterator *tem = top;
  620. check_end_diversion(tem);
  621. top = top->next;
  622. level--;
  623. delete tem;
  624. }
  625. // Keep while_request happy.
  626. for (; nboundaries > 0; --nboundaries)
  627. add_return_boundary();
  628. }
  629. void input_stack::pop_macro()
  630. {
  631. int nboundaries = 0;
  632. int is_macro = 0;
  633. do {
  634. if (top->next == &nil_iterator)
  635. break;
  636. if (top->is_boundary())
  637. nboundaries++;
  638. is_macro = top->is_macro();
  639. input_iterator *tem = top;
  640. check_end_diversion(tem);
  641. top = top->next;
  642. level--;
  643. delete tem;
  644. } while (!is_macro);
  645. // Keep while_request happy.
  646. for (; nboundaries > 0; --nboundaries)
  647. add_return_boundary();
  648. }
  649. inline void input_stack::save_compatible_flag(int f)
  650. {
  651. top->save_compatible_flag(f);
  652. }
  653. inline int input_stack::get_compatible_flag()
  654. {
  655. return top->get_compatible_flag();
  656. }
  657. void backtrace_request()
  658. {
  659. input_stack::backtrace_all();
  660. fflush(stderr);
  661. skip_line();
  662. }
  663. void next_file()
  664. {
  665. symbol nm = get_long_name();
  666. while (!tok.newline() && !tok.eof())
  667. tok.next();
  668. if (nm.is_null())
  669. input_stack::end_file();
  670. else {
  671. errno = 0;
  672. FILE *fp = include_search_path.open_file_cautious(nm.contents());
  673. if (!fp)
  674. error("can't open `%1': %2", nm.contents(), strerror(errno));
  675. else
  676. input_stack::next_file(fp, nm.contents());
  677. }
  678. tok.next();
  679. }
  680. void shift()
  681. {
  682. int n;
  683. if (!has_arg() || !get_integer(&n))
  684. n = 1;
  685. input_stack::shift(n);
  686. skip_line();
  687. }
  688. static char get_char_for_escape_name(int allow_space = 0)
  689. {
  690. int c = get_copy(0);
  691. switch (c) {
  692. case EOF:
  693. copy_mode_error("end of input in escape name");
  694. return '\0';
  695. default:
  696. if (!invalid_input_char(c))
  697. break;
  698. // fall through
  699. case '\n':
  700. if (c == '\n')
  701. input_stack::push(make_temp_iterator("\n"));
  702. // fall through
  703. case ' ':
  704. if (c == ' ' && allow_space)
  705. break;
  706. // fall through
  707. case '\t':
  708. case '\001':
  709. case '\b':
  710. copy_mode_error("%1 is not allowed in an escape name",
  711. input_char_description(c));
  712. return '\0';
  713. }
  714. return c;
  715. }
  716. static symbol read_two_char_escape_name()
  717. {
  718. char buf[3];
  719. buf[0] = get_char_for_escape_name();
  720. if (buf[0] != '\0') {
  721. buf[1] = get_char_for_escape_name();
  722. if (buf[1] == '\0')
  723. buf[0] = 0;
  724. else
  725. buf[2] = 0;
  726. }
  727. return symbol(buf);
  728. }
  729. static symbol read_long_escape_name(read_mode mode)
  730. {
  731. int start_level = input_stack::get_level();
  732. char abuf[ABUF_SIZE];
  733. char *buf = abuf;
  734. int buf_size = ABUF_SIZE;
  735. int i = 0;
  736. char c;
  737. int have_char = 0;
  738. for (;;) {
  739. c = get_char_for_escape_name(have_char && mode == WITH_ARGS);
  740. if (c == 0) {
  741. if (buf != abuf)
  742. a_delete buf;
  743. return NULL_SYMBOL;
  744. }
  745. have_char = 1;
  746. if (mode == WITH_ARGS && c == ' ')
  747. break;
  748. if (i + 2 > buf_size) {
  749. if (buf == abuf) {
  750. buf = new char[ABUF_SIZE*2];
  751. memcpy(buf, abuf, buf_size);
  752. buf_size = ABUF_SIZE*2;
  753. }
  754. else {
  755. char *old_buf = buf;
  756. buf = new char[buf_size*2];
  757. memcpy(buf, old_buf, buf_size);
  758. buf_size *= 2;
  759. a_delete old_buf;
  760. }
  761. }
  762. if (c == ']' && input_stack::get_level() == start_level)
  763. break;
  764. buf[i++] = c;
  765. }
  766. buf[i] = 0;
  767. if (c == ' ')
  768. have_string_arg = 1;
  769. if (buf == abuf) {
  770. if (i == 0) {
  771. if (mode != ALLOW_EMPTY)
  772. copy_mode_error("empty escape name");
  773. return EMPTY_SYMBOL;
  774. }
  775. return symbol(abuf);
  776. }
  777. else {
  778. symbol s(buf);
  779. a_delete buf;
  780. return s;
  781. }
  782. }
  783. static symbol read_escape_name(read_mode mode)
  784. {
  785. char c = get_char_for_escape_name();
  786. if (c == 0)
  787. return NULL_SYMBOL;
  788. if (c == '(')
  789. return read_two_char_escape_name();
  790. if (c == '[' && !compatible_flag)
  791. return read_long_escape_name(mode);
  792. char buf[2];
  793. buf[0] = c;
  794. buf[1] = '\0';
  795. return symbol(buf);
  796. }
  797. static symbol read_increment_and_escape_name(int *incp)
  798. {
  799. char c = get_char_for_escape_name();
  800. switch (c) {
  801. case 0:
  802. *incp = 0;
  803. return NULL_SYMBOL;
  804. case '(':
  805. *incp = 0;
  806. return read_two_char_escape_name();
  807. case '+':
  808. *incp = 1;
  809. return read_escape_name();
  810. case '-':
  811. *incp = -1;
  812. return read_escape_name();
  813. case '[':
  814. if (!compatible_flag) {
  815. *incp = 0;
  816. return read_long_escape_name();
  817. }
  818. break;
  819. }
  820. *incp = 0;
  821. char buf[2];
  822. buf[0] = c;
  823. buf[1] = '\0';
  824. return symbol(buf);
  825. }
  826. static int get_copy(node **nd, int defining)
  827. {
  828. for (;;) {
  829. int c = input_stack::get(nd);
  830. if (c == PUSH_GROFF_MODE) {
  831. input_stack::save_compatible_flag(compatible_flag);
  832. compatible_flag = 0;
  833. continue;
  834. }
  835. if (c == PUSH_COMP_MODE) {
  836. input_stack::save_compatible_flag(compatible_flag);
  837. compatible_flag = 1;
  838. continue;
  839. }
  840. if (c == POP_GROFFCOMP_MODE) {
  841. compatible_flag = input_stack::get_compatible_flag();
  842. continue;
  843. }
  844. if (c == BEGIN_QUOTE) {
  845. input_stack::increase_level();
  846. continue;
  847. }
  848. if (c == END_QUOTE) {
  849. input_stack::decrease_level();
  850. continue;
  851. }
  852. if (c == ESCAPE_NEWLINE) {
  853. if (defining)
  854. return c;
  855. do {
  856. c = input_stack::get(nd);
  857. } while (c == ESCAPE_NEWLINE);
  858. }
  859. if (c != escape_char || escape_char <= 0)
  860. return c;
  861. c = input_stack::peek();
  862. switch(c) {
  863. case 0:
  864. return escape_char;
  865. case '"':
  866. (void)input_stack::get(0);
  867. while ((c = input_stack::get(0)) != '\n' && c != EOF)
  868. ;
  869. return c;
  870. case '#': // Like \" but newline is ignored.
  871. (void)input_stack::get(0);
  872. while ((c = input_stack::get(0)) != '\n')
  873. if (c == EOF)
  874. return EOF;
  875. break;
  876. case '$':
  877. {
  878. (void)input_stack::get(0);
  879. symbol s = read_escape_name();
  880. if (!(s.is_null() || s.is_empty()))
  881. interpolate_arg(s);
  882. break;
  883. }
  884. case '*':
  885. {
  886. (void)input_stack::get(0);
  887. symbol s = read_escape_name(WITH_ARGS);
  888. if (!(s.is_null() || s.is_empty())) {
  889. if (have_string_arg) {
  890. have_string_arg = 0;
  891. interpolate_string_with_args(s);
  892. }
  893. else
  894. interpolate_string(s);
  895. }
  896. break;
  897. }
  898. case 'a':
  899. (void)input_stack::get(0);
  900. return '\001';
  901. case 'e':
  902. (void)input_stack::get(0);
  903. return ESCAPE_e;
  904. case 'E':
  905. (void)input_stack::get(0);
  906. return ESCAPE_E;
  907. case 'n':
  908. {
  909. (void)input_stack::get(0);
  910. int inc;
  911. symbol s = read_increment_and_escape_name(&inc);
  912. if (!(s.is_null() || s.is_empty()))
  913. interpolate_number_reg(s, inc);
  914. break;
  915. }
  916. case 'g':
  917. {
  918. (void)input_stack::get(0);
  919. symbol s = read_escape_name();
  920. if (!(s.is_null() || s.is_empty()))
  921. interpolate_number_format(s);
  922. break;
  923. }
  924. case 't':
  925. (void)input_stack::get(0);
  926. return '\t';
  927. case 'V':
  928. {
  929. (void)input_stack::get(0);
  930. symbol s = read_escape_name();
  931. if (!(s.is_null() || s.is_empty()))
  932. interpolate_environment_variable(s);
  933. break;
  934. }
  935. case '\n':
  936. (void)input_stack::get(0);
  937. if (defining)
  938. return ESCAPE_NEWLINE;
  939. break;
  940. case ' ':
  941. (void)input_stack::get(0);
  942. return ESCAPE_SPACE;
  943. case '~':
  944. (void)input_stack::get(0);
  945. return ESCAPE_TILDE;
  946. case ':':
  947. (void)input_stack::get(0);
  948. return ESCAPE_COLON;
  949. case '|':
  950. (void)input_stack::get(0);
  951. return ESCAPE_BAR;
  952. case '^':
  953. (void)input_stack::get(0);
  954. return ESCAPE_CIRCUMFLEX;
  955. case '{':
  956. (void)input_stack::get(0);
  957. return ESCAPE_LEFT_BRACE;
  958. case '}':
  959. (void)input_stack::get(0);
  960. return ESCAPE_RIGHT_BRACE;
  961. case '`':
  962. (void)input_stack::get(0);
  963. return ESCAPE_LEFT_QUOTE;
  964. case '\'':
  965. (void)input_stack::get(0);
  966. return ESCAPE_RIGHT_QUOTE;
  967. case '-':
  968. (void)input_stack::get(0);
  969. return ESCAPE_HYPHEN;
  970. case '_':
  971. (void)input_stack::get(0);
  972. return ESCAPE_UNDERSCORE;
  973. case 'c':
  974. (void)input_stack::get(0);
  975. return ESCAPE_c;
  976. case '!':
  977. (void)input_stack::get(0);
  978. return ESCAPE_BANG;
  979. case '?':
  980. (void)input_stack::get(0);
  981. return ESCAPE_QUESTION;
  982. case '&':
  983. (void)input_stack::get(0);
  984. return ESCAPE_AMPERSAND;
  985. case ')':
  986. (void)input_stack::get(0);
  987. return ESCAPE_RIGHT_PARENTHESIS;
  988. case '.':
  989. (void)input_stack::get(0);
  990. return c;
  991. case '%':
  992. (void)input_stack::get(0);
  993. return ESCAPE_PERCENT;
  994. default:
  995. if (c == escape_char) {
  996. (void)input_stack::get(0);
  997. return c;
  998. }
  999. else
  1000. return escape_char;
  1001. }
  1002. }
  1003. }
  1004. class non_interpreted_char_node : public node {
  1005. unsigned char c;
  1006. public:
  1007. non_interpreted_char_node(unsigned char);
  1008. node *copy();
  1009. int interpret(macro *);
  1010. int same(node *);
  1011. const char *type();
  1012. int force_tprint();
  1013. int is_tag();
  1014. };
  1015. int non_interpreted_char_node::same(node *nd)
  1016. {
  1017. return c == ((non_interpreted_char_node *)nd)->c;
  1018. }
  1019. const char *non_interpreted_char_node::type()
  1020. {
  1021. return "non_interpreted_char_node";
  1022. }
  1023. int non_interpreted_char_node::force_tprint()
  1024. {
  1025. return 0;
  1026. }
  1027. int non_interpreted_char_node::is_tag()
  1028. {
  1029. return 0;
  1030. }
  1031. non_interpreted_char_node::non_interpreted_char_node(unsigned char n) : c(n)
  1032. {
  1033. assert(n != 0);
  1034. }
  1035. node *non_interpreted_char_node::copy()
  1036. {
  1037. return new non_interpreted_char_node(c);
  1038. }
  1039. int non_interpreted_char_node::interpret(macro *mac)
  1040. {
  1041. mac->append(c);
  1042. return 1;
  1043. }
  1044. static void do_width();
  1045. static node *do_non_interpreted();
  1046. static node *do_special();
  1047. static node *do_suppress(symbol nm);
  1048. static void do_register();
  1049. dictionary color_dictionary(501);
  1050. static color *lookup_color(symbol nm)
  1051. {
  1052. assert(!nm.is_null());
  1053. if (nm == default_symbol)
  1054. return &default_color;
  1055. color *c = (color *)color_dictionary.lookup(nm);
  1056. if (c == 0)
  1057. warning(WARN_COLOR, "color `%1' not defined", nm.contents());
  1058. return c;
  1059. }
  1060. void do_glyph_color(symbol nm)
  1061. {
  1062. if (nm.is_null())
  1063. return;
  1064. if (nm.is_empty())
  1065. curenv->set_glyph_color(curenv->get_prev_glyph_color());
  1066. else {
  1067. color *tem = lookup_color(nm);
  1068. if (tem)
  1069. curenv->set_glyph_color(tem);
  1070. else
  1071. (void)color_dictionary.lookup(nm, new color(nm));
  1072. }
  1073. }
  1074. void do_fill_color(symbol nm)
  1075. {
  1076. if (nm.is_null())
  1077. return;
  1078. if (nm.is_empty())
  1079. curenv->set_fill_color(curenv->get_prev_fill_color());
  1080. else {
  1081. color *tem = lookup_color(nm);
  1082. if (tem)
  1083. curenv->set_fill_color(tem);
  1084. else
  1085. (void)color_dictionary.lookup(nm, new color(nm));
  1086. }
  1087. }
  1088. static unsigned int get_color_element(const char *scheme, const char *col)
  1089. {
  1090. units val;
  1091. if (!get_number(&val, 'f')) {
  1092. warning(WARN_COLOR, "%1 in %2 definition set to 0", col, scheme);
  1093. tok.next();
  1094. return 0;
  1095. }
  1096. if (val < 0) {
  1097. warning(WARN_RANGE, "%1 cannot be negative: set to 0", col);
  1098. return 0;
  1099. }
  1100. if (val > color::MAX_COLOR_VAL+1) {
  1101. warning(WARN_RANGE, "%1 cannot be greater than 1", col);
  1102. // we change 0x10000 to 0xffff
  1103. return color::MAX_COLOR_VAL;
  1104. }
  1105. return (unsigned int)val;
  1106. }
  1107. static color *read_rgb(char end = 0)
  1108. {
  1109. symbol component = do_get_long_name(0, end);
  1110. if (component.is_null()) {
  1111. warning(WARN_COLOR, "missing rgb color values");
  1112. return 0;
  1113. }
  1114. const char *s = component.contents();
  1115. color *col = new color;
  1116. if (*s == '#') {
  1117. if (!col->read_rgb(s)) {
  1118. warning(WARN_COLOR, "expecting rgb color definition not `%1'", s);
  1119. delete col;
  1120. return 0;
  1121. }
  1122. }
  1123. else {
  1124. if (!end)
  1125. input_stack::push(make_temp_iterator(" "));
  1126. input_stack::push(make_temp_iterator(s));
  1127. tok.next();
  1128. unsigned int r = get_color_element("rgb color", "red component");
  1129. unsigned int g = get_color_element("rgb color", "green component");
  1130. unsigned int b = get_color_element("rgb color", "blue component");
  1131. col->set_rgb(r, g, b);
  1132. }
  1133. return col;
  1134. }
  1135. static color *read_cmy(char end = 0)
  1136. {
  1137. symbol component = do_get_long_name(0, end);
  1138. if (component.is_null()) {
  1139. warning(WARN_COLOR, "missing cmy color values");
  1140. return 0;
  1141. }
  1142. const char *s = component.contents();
  1143. color *col = new color;
  1144. if (*s == '#') {
  1145. if (!col->read_cmy(s)) {
  1146. warning(WARN_COLOR, "expecting cmy color definition not `%1'", s);
  1147. delete col;
  1148. return 0;
  1149. }
  1150. }
  1151. else {
  1152. if (!end)
  1153. input_stack::push(make_temp_iterator(" "));
  1154. input_stack::push(make_temp_iterator(s));
  1155. tok.next();
  1156. unsigned int c = get_color_element("cmy color", "cyan component");
  1157. unsigned int m = get_color_element("cmy color", "magenta component");
  1158. unsigned int y = get_color_element("cmy color", "yellow component");
  1159. col->set_cmy(c, m, y);
  1160. }
  1161. return col;
  1162. }
  1163. static color *read_cmyk(char end = 0)
  1164. {
  1165. symbol component = do_get_long_name(0, end);
  1166. if (component.is_null()) {
  1167. warning(WARN_COLOR, "missing cmyk color values");
  1168. return 0;
  1169. }
  1170. const char *s = component.contents();
  1171. color *col = new color;
  1172. if (*s == '#') {
  1173. if (!col->read_cmyk(s)) {
  1174. warning(WARN_COLOR, "`expecting a cmyk color definition not `%1'", s);
  1175. delete col;
  1176. return 0;
  1177. }
  1178. }
  1179. else {
  1180. if (!end)
  1181. input_stack::push(make_temp_iterator(" "));
  1182. input_stack::push(make_temp_iterator(s));
  1183. tok.next();
  1184. unsigned int c = get_color_element("cmyk color", "cyan component");
  1185. unsigned int m = get_color_element("cmyk color", "magenta component");
  1186. unsigned int y = get_color_element("cmyk color", "yellow component");
  1187. unsigned int k = get_color_element("cmyk color", "black component");
  1188. col->set_cmyk(c, m, y, k);
  1189. }
  1190. return col;
  1191. }
  1192. static color *read_gray(char end = 0)
  1193. {
  1194. symbol component = do_get_long_name(0, end);
  1195. if (component.is_null()) {
  1196. warning(WARN_COLOR, "missing gray values");
  1197. return 0;
  1198. }
  1199. const char *s = component.contents();
  1200. color *col = new color;
  1201. if (*s == '#') {
  1202. if (!col->read_gray(s)) {
  1203. warning(WARN_COLOR, "`expecting a gray definition not `%1'", s);
  1204. delete col;
  1205. return 0;
  1206. }
  1207. }
  1208. else {
  1209. if (!end)
  1210. input_stack::push(make_temp_iterator("\n"));
  1211. input_stack::push(make_temp_iterator(s));
  1212. tok.next();
  1213. unsigned int g = get_color_element("gray", "gray value");
  1214. col->set_gray(g);
  1215. }
  1216. return col;
  1217. }
  1218. static void activate_color()
  1219. {
  1220. int n;
  1221. if (has_arg() && get_integer(&n))
  1222. color_flag = n != 0;
  1223. else
  1224. color_flag = 1;
  1225. skip_line();
  1226. }
  1227. static void define_color()
  1228. {
  1229. symbol color_name = get_long_name(1);
  1230. if (color_name.is_null()) {
  1231. skip_line();
  1232. return;
  1233. }
  1234. if (color_name == default_symbol) {
  1235. warning(WARN_COLOR, "default color can't be redefined");
  1236. skip_line();
  1237. return;
  1238. }
  1239. symbol style = get_long_name(1);
  1240. if (style.is_null()) {
  1241. skip_line();
  1242. return;
  1243. }
  1244. color *col;
  1245. if (strcmp(style.contents(), "rgb") == 0)
  1246. col = read_rgb();
  1247. else if (strcmp(style.contents(), "cmyk") == 0)
  1248. col = read_cmyk();
  1249. else if (strcmp(style.contents(), "gray") == 0)
  1250. col = read_gray();
  1251. else if (strcmp(style.contents(), "grey") == 0)
  1252. col = read_gray();
  1253. else if (strcmp(style.contents(), "cmy") == 0)
  1254. col = read_cmy();
  1255. else {
  1256. warning(WARN_COLOR,
  1257. "unknown color space `%1'; use rgb, cmyk, gray or cmy",
  1258. style.contents());
  1259. skip_line();
  1260. return;
  1261. }
  1262. if (col) {
  1263. col->nm = color_name;
  1264. (void)color_dictionary.lookup(color_name, col);
  1265. }
  1266. skip_line();
  1267. }
  1268. static node *do_overstrike()
  1269. {
  1270. token start;
  1271. overstrike_node *on = new overstrike_node;
  1272. int start_level = input_stack::get_level();
  1273. start.next();
  1274. for (;;) {
  1275. tok.next();
  1276. if (tok.newline() || tok.eof()) {
  1277. warning(WARN_DELIM, "missing closing delimiter");
  1278. input_stack::push(make_temp_iterator("\n"));
  1279. break;
  1280. }
  1281. if (tok == start
  1282. && (compatible_flag || input_stack::get_level() == start_level))
  1283. break;
  1284. charinfo *ci = tok.get_char(1);
  1285. if (ci) {
  1286. node *n = curenv->make_char_node(ci);
  1287. if (n)
  1288. on->overstrike(n);
  1289. }
  1290. }
  1291. return on;
  1292. }
  1293. static node *do_bracket()
  1294. {
  1295. token start;
  1296. bracket_node *bn = new bracket_node;
  1297. start.next();
  1298. int start_level = input_stack::get_level();
  1299. for (;;) {
  1300. tok.next();
  1301. if (tok.eof()) {
  1302. warning(WARN_DELIM, "missing closing delimiter");
  1303. break;
  1304. }
  1305. if (tok.newline()) {
  1306. warning(WARN_DELIM, "missing closing delimiter");
  1307. input_stack::push(make_temp_iterator("\n"));
  1308. break;
  1309. }
  1310. if (tok == start
  1311. && (compatible_flag || input_stack::get_level() == start_level))
  1312. break;
  1313. charinfo *ci = tok.get_char(1);
  1314. if (ci) {
  1315. node *n = curenv->make_char_node(ci);
  1316. if (n)
  1317. bn->bracket(n);
  1318. }
  1319. }
  1320. return bn;
  1321. }
  1322. static int do_name_test()
  1323. {
  1324. token start;
  1325. start.next();
  1326. int start_level = input_stack::get_level();
  1327. int bad_char = 0;
  1328. int some_char = 0;
  1329. for (;;) {
  1330. tok.next();
  1331. if (tok.newline() || tok.eof()) {
  1332. warning(WARN_DELIM, "missing closing delimiter");
  1333. input_stack::push(make_temp_iterator("\n"));
  1334. break;
  1335. }
  1336. if (tok == start
  1337. && (compatible_flag || input_stack::get_level() == start_level))
  1338. break;
  1339. if (!tok.ch())
  1340. bad_char = 1;
  1341. some_char = 1;
  1342. }
  1343. return some_char && !bad_char;
  1344. }
  1345. static int do_expr_test()
  1346. {
  1347. token start;
  1348. start.next();
  1349. int start_level = input_stack::get_level();
  1350. if (!start.delimiter(1))
  1351. return 0;
  1352. tok.next();
  1353. // disable all warning and error messages temporarily
  1354. int saved_warning_mask = warning_mask;
  1355. int saved_inhibit_errors = inhibit_errors;
  1356. warning_mask = 0;
  1357. inhibit_errors = 1;
  1358. int dummy;
  1359. int result = get_number_rigidly(&dummy, 'u');
  1360. warning_mask = saved_warning_mask;
  1361. inhibit_errors = saved_inhibit_errors;
  1362. if (tok == start && input_stack::get_level() == start_level)
  1363. return result;
  1364. // ignore everything up to the delimiter in case we aren't right there
  1365. for (;;) {
  1366. tok.next();
  1367. if (tok.newline() || tok.eof()) {
  1368. warning(WARN_DELIM, "missing closing delimiter");
  1369. input_stack::push(make_temp_iterator("\n"));
  1370. break;
  1371. }
  1372. if (tok == start && input_stack::get_level() == start_level)
  1373. break;
  1374. }
  1375. return 0;
  1376. }
  1377. #if 0
  1378. static node *do_zero_width()
  1379. {
  1380. token start;
  1381. start.next();
  1382. int start_level = input_stack::get_level();
  1383. environment env(curenv);
  1384. environment *oldenv = curenv;
  1385. curenv = &env;
  1386. for (;;) {
  1387. tok.next();
  1388. if (tok.newline() || tok.eof()) {
  1389. error("missing closing delimiter");
  1390. break;
  1391. }
  1392. if (tok == start
  1393. && (compatible_flag || input_stack::get_level() == start_level))
  1394. break;
  1395. tok.process();
  1396. }
  1397. curenv = oldenv;
  1398. node *rev = env.extract_output_line();
  1399. node *n = 0;
  1400. while (rev) {
  1401. node *tem = rev;
  1402. rev = rev->next;
  1403. tem->next = n;
  1404. n = tem;
  1405. }
  1406. return new zero_width_node(n);
  1407. }
  1408. #else
  1409. // It's undesirable for \Z to change environments, because then
  1410. // \n(.w won't work as expected.
  1411. static node *do_zero_width()
  1412. {
  1413. node *rev = new dummy_node;
  1414. token start;
  1415. start.next();
  1416. int start_level = input_stack::get_level();
  1417. for (;;) {
  1418. tok.next();
  1419. if (tok.newline() || tok.eof()) {
  1420. warning(WARN_DELIM, "missing closing delimiter");
  1421. input_stack::push(make_temp_iterator("\n"));
  1422. break;
  1423. }
  1424. if (tok == start
  1425. && (compatible_flag || input_stack::get_level() == start_level))
  1426. break;
  1427. if (!tok.add_to_node_list(&rev))
  1428. error("invalid token in argument to \\Z");
  1429. }
  1430. node *n = 0;
  1431. while (rev) {
  1432. node *tem = rev;
  1433. rev = rev->next;
  1434. tem->next = n;
  1435. n = tem;
  1436. }
  1437. return new zero_width_node(n);
  1438. }
  1439. #endif
  1440. token_node *node::get_token_node()
  1441. {
  1442. return 0;
  1443. }
  1444. class token_node : public node {
  1445. public:
  1446. token tk;
  1447. token_node(const token &t);
  1448. node *copy();
  1449. token_node *get_token_node();
  1450. int same(node *);
  1451. const char *type();
  1452. int force_tprint();
  1453. int is_tag();
  1454. };
  1455. token_node::token_node(const token &t) : tk(t)
  1456. {
  1457. }
  1458. node *token_node::copy()
  1459. {
  1460. return new token_node(tk);
  1461. }
  1462. token_node *token_node::get_token_node()
  1463. {
  1464. return this;
  1465. }
  1466. int token_node::same(node *nd)
  1467. {
  1468. return tk == ((token_node *)nd)->tk;
  1469. }
  1470. const char *token_node::type()
  1471. {
  1472. return "token_node";
  1473. }
  1474. int token_node::force_tprint()
  1475. {
  1476. return 0;
  1477. }
  1478. int token_node::is_tag()
  1479. {
  1480. return 0;
  1481. }
  1482. token::token() : nd(0), type(TOKEN_EMPTY)
  1483. {
  1484. }
  1485. token::~token()
  1486. {
  1487. delete nd;
  1488. }
  1489. token::token(const token &t)
  1490. : nm(t.nm), c(t.c), val(t.val), dim(t.dim), type(t.type)
  1491. {
  1492. // Use two statements to work around bug in SGI C++.
  1493. node *tem = t.nd;
  1494. nd = tem ? tem->copy() : 0;
  1495. }
  1496. void token::operator=(const token &t)
  1497. {
  1498. delete nd;
  1499. nm = t.nm;
  1500. // Use two statements to work around bug in SGI C++.
  1501. node *tem = t.nd;
  1502. nd = tem ? tem->copy() : 0;
  1503. c = t.c;
  1504. val = t.val;
  1505. dim = t.dim;
  1506. type = t.type;
  1507. }
  1508. void token::skip()
  1509. {
  1510. while (space())
  1511. next();
  1512. }
  1513. int has_arg()
  1514. {
  1515. while (tok.space())
  1516. tok.next();
  1517. return !tok.newline();
  1518. }
  1519. void token::make_space()
  1520. {
  1521. type = TOKEN_SPACE;
  1522. }
  1523. void token::make_newline()
  1524. {
  1525. type = TOKEN_NEWLINE;
  1526. }
  1527. void token::next()
  1528. {
  1529. if (nd) {
  1530. delete nd;
  1531. nd = 0;
  1532. }
  1533. units x;
  1534. for (;;) {
  1535. node *n = 0;
  1536. int cc = input_stack::get(&n);
  1537. if (cc != escape_char || escape_char == 0) {
  1538. handle_normal_char:
  1539. switch(cc) {
  1540. case PUSH_GROFF_MODE:
  1541. input_stack::save_compatible_flag(compatible_flag);
  1542. compatible_flag = 0;
  1543. continue;
  1544. case PUSH_COMP_MODE:
  1545. input_stack::save_compatible_flag(compatible_flag);
  1546. compatible_flag = 1;
  1547. continue;
  1548. case POP_GROFFCOMP_MODE:
  1549. compatible_flag = input_stack::get_compatible_flag();
  1550. continue;
  1551. case BEGIN_QUOTE:
  1552. input_stack::increase_level();
  1553. continue;
  1554. case END_QUOTE:
  1555. input_stack::decrease_level();
  1556. continue;
  1557. case EOF:
  1558. type = TOKEN_EOF;
  1559. return;
  1560. case TRANSPARENT_FILE_REQUEST:
  1561. case TITLE_REQUEST:
  1562. case COPY_FILE_REQUEST:
  1563. #ifdef COLUMN
  1564. case VJUSTIFY_REQUEST:
  1565. #endif /* COLUMN */
  1566. type = TOKEN_REQUEST;
  1567. c = cc;
  1568. return;
  1569. case BEGIN_TRAP:
  1570. type = TOKEN_BEGIN_TRAP;
  1571. return;
  1572. case END_TRAP:
  1573. type = TOKEN_END_TRAP;
  1574. return;
  1575. case LAST_PAGE_EJECTOR:
  1576. seen_last_page_ejector = 1;
  1577. // fall through
  1578. case PAGE_EJECTOR:
  1579. type = TOKEN_PAGE_EJECTOR;
  1580. return;
  1581. case ESCAPE_PERCENT:
  1582. ESCAPE_PERCENT:
  1583. type = TOKEN_HYPHEN_INDICATOR;
  1584. return;
  1585. case ESCAPE_SPACE:
  1586. ESCAPE_SPACE:
  1587. type = TOKEN_UNSTRETCHABLE_SPACE;
  1588. return;
  1589. case ESCAPE_TILDE:
  1590. ESCAPE_TILDE:
  1591. type = TOKEN_STRETCHABLE_SPACE;
  1592. return;
  1593. case ESCAPE_COLON:
  1594. ESCAPE_COLON:
  1595. type = TOKEN_ZERO_WIDTH_BREAK;
  1596. return;
  1597. case ESCAPE_e:
  1598. ESCAPE_e:
  1599. type = TOKEN_ESCAPE;
  1600. return;
  1601. case ESCAPE_E:
  1602. goto handle_escape_char;
  1603. case ESCAPE_BAR:
  1604. ESCAPE_BAR:
  1605. type = TOKEN_NODE;
  1606. nd = new hmotion_node(curenv->get_narrow_space_width(),
  1607. curenv->get_fill_color());
  1608. return;
  1609. case ESCAPE_CIRCUMFLEX:
  1610. ESCAPE_CIRCUMFLEX:
  1611. type = TOKEN_NODE;
  1612. nd = new hmotion_node(curenv->get_half_narrow_space_width(),
  1613. curenv->get_fill_color());
  1614. return;
  1615. case ESCAPE_NEWLINE:
  1616. have_input = 0;
  1617. break;
  1618. case ESCAPE_LEFT_BRACE:
  1619. ESCAPE_LEFT_BRACE:
  1620. type = TOKEN_LEFT_BRACE;
  1621. return;
  1622. case ESCAPE_RIGHT_BRACE:
  1623. ESCAPE_RIGHT_BRACE:
  1624. type = TOKEN_RIGHT_BRACE;
  1625. return;
  1626. case ESCAPE_LEFT_QUOTE:
  1627. ESCAPE_LEFT_QUOTE:
  1628. type = TOKEN_SPECIAL;
  1629. nm = symbol("ga");
  1630. return;
  1631. case ESCAPE_RIGHT_QUOTE:
  1632. ESCAPE_RIGHT_QUOTE:
  1633. type = TOKEN_SPECIAL;
  1634. nm = symbol("aa");
  1635. return;
  1636. case ESCAPE_HYPHEN:
  1637. ESCAPE_HYPHEN:
  1638. type = TOKEN_SPECIAL;
  1639. nm = symbol("-");
  1640. return;
  1641. case ESCAPE_UNDERSCORE:
  1642. ESCAPE_UNDERSCORE:
  1643. type = TOKEN_SPECIAL;
  1644. nm = symbol("ul");
  1645. return;
  1646. case ESCAPE_c:
  1647. ESCAPE_c:
  1648. type = TOKEN_INTERRUPT;
  1649. return;
  1650. case ESCAPE_BANG:
  1651. ESCAPE_BANG:
  1652. type = TOKEN_TRANSPARENT;
  1653. return;
  1654. case ESCAPE_QUESTION:
  1655. ESCAPE_QUESTION:
  1656. nd = do_non_interpreted();
  1657. if (nd) {
  1658. type = TOKEN_NODE;
  1659. return;
  1660. }
  1661. break;
  1662. case ESCAPE_AMPERSAND:
  1663. ESCAPE_AMPERSAND:
  1664. type = TOKEN_DUMMY;
  1665. return;
  1666. case ESCAPE_RIGHT_PARENTHESIS:
  1667. ESCAPE_RIGHT_PARENTHESIS:
  1668. type = TOKEN_TRANSPARENT_DUMMY;
  1669. return;
  1670. case '\b':
  1671. type = TOKEN_BACKSPACE;
  1672. return;
  1673. case ' ':
  1674. type = TOKEN_SPACE;
  1675. return;
  1676. case '\t':
  1677. type = TOKEN_TAB;
  1678. return;
  1679. case '\n':
  1680. type = TOKEN_NEWLINE;
  1681. return;
  1682. case '\001':
  1683. type = TOKEN_LEADER;
  1684. return;
  1685. case 0:
  1686. {
  1687. assert(n != 0);
  1688. token_node *tn = n->get_token_node();
  1689. if (tn) {
  1690. *this = tn->tk;
  1691. delete tn;
  1692. }
  1693. else {
  1694. nd = n;
  1695. type = TOKEN_NODE;
  1696. }
  1697. }
  1698. return;
  1699. default:
  1700. type = TOKEN_CHAR;
  1701. c = cc;
  1702. return;
  1703. }
  1704. }
  1705. else {
  1706. handle_escape_char:
  1707. cc = input_stack::get(&n);
  1708. switch(cc) {
  1709. case '(':
  1710. nm = read_two_char_escape_name();
  1711. type = TOKEN_SPECIAL;
  1712. return;
  1713. case EOF:
  1714. type = TOKEN_EOF;
  1715. error("end of input after escape character");
  1716. return;
  1717. case '`':
  1718. goto ESCAPE_LEFT_QUOTE;
  1719. case '\'':
  1720. goto ESCAPE_RIGHT_QUOTE;
  1721. case '-':
  1722. goto ESCAPE_HYPHEN;
  1723. case '_':
  1724. goto ESCAPE_UNDERSCORE;
  1725. case '%':
  1726. goto ESCAPE_PERCENT;
  1727. case ' ':
  1728. goto ESCAPE_SPACE;
  1729. case '0':
  1730. nd = new hmotion_node(curenv->get_digit_width(),
  1731. curenv->get_fill_color());
  1732. type = TOKEN_NODE;
  1733. return;
  1734. case '|':
  1735. goto ESCAPE_BAR;
  1736. case '^':
  1737. goto ESCAPE_CIRCUMFLEX;
  1738. case '/':
  1739. type = TOKEN_ITALIC_CORRECTION;
  1740. return;
  1741. case ',':
  1742. type = TOKEN_NODE;
  1743. nd = new left_italic_corrected_node;
  1744. return;
  1745. case '&':
  1746. goto ESCAPE_AMPERSAND;
  1747. case ')':
  1748. goto ESCAPE_RIGHT_PARENTHESIS;
  1749. case '!':
  1750. goto ESCAPE_BANG;
  1751. case '?':
  1752. goto ESCAPE_QUESTION;
  1753. case '~':
  1754. goto ESCAPE_TILDE;
  1755. case ':':
  1756. goto ESCAPE_COLON;
  1757. case '"':
  1758. while ((cc = input_stack::get(0)) != '\n' && cc != EOF)
  1759. ;
  1760. if (cc == '\n')
  1761. type = TOKEN_NEWLINE;
  1762. else
  1763. type = TOKEN_EOF;
  1764. return;
  1765. case '#': // Like \" but newline is ignored.
  1766. while ((cc = input_stack::get(0)) != '\n')
  1767. if (cc == EOF) {
  1768. type = TOKEN_EOF;
  1769. return;
  1770. }
  1771. break;
  1772. case '$':
  1773. {
  1774. symbol s = read_escape_name();
  1775. if (!(s.is_null() || s.is_empty()))
  1776. interpolate_arg(s);
  1777. break;
  1778. }
  1779. case '*':
  1780. {
  1781. symbol s = read_escape_name(WITH_ARGS);
  1782. if (!(s.is_null() || s.is_empty())) {
  1783. if (have_string_arg) {
  1784. have_string_arg = 0;
  1785. interpolate_string_with_args(s);
  1786. }
  1787. else
  1788. interpolate_string(s);
  1789. }
  1790. break;
  1791. }
  1792. case 'a':
  1793. nd = new non_interpreted_char_node('\001');
  1794. type = TOKEN_NODE;
  1795. return;
  1796. case 'A':
  1797. c = '0' + do_name_test();
  1798. type = TOKEN_CHAR;
  1799. return;
  1800. case 'b':
  1801. nd = do_bracket();
  1802. type = TOKEN_NODE;
  1803. return;
  1804. case 'B':
  1805. c = '0' + do_expr_test();
  1806. type = TOKEN_CHAR;
  1807. return;
  1808. case 'c':
  1809. goto ESCAPE_c;
  1810. case 'C':
  1811. nm = get_delim_name();
  1812. if (nm.is_null())
  1813. break;
  1814. type = TOKEN_SPECIAL;
  1815. return;
  1816. case 'd':
  1817. type = TOKEN_NODE;
  1818. nd = new vmotion_node(curenv->get_size() / 2,
  1819. curenv->get_fill_color());
  1820. return;
  1821. case 'D':
  1822. nd = read_draw_node();
  1823. if (!nd)
  1824. break;
  1825. type = TOKEN_NODE;
  1826. return;
  1827. case 'e':
  1828. goto ESCAPE_e;
  1829. case 'E':
  1830. goto handle_escape_char;
  1831. case 'f':
  1832. {
  1833. symbol s = read_escape_name(ALLOW_EMPTY);
  1834. if (s.is_null())
  1835. break;
  1836. const char *p;
  1837. for (p = s.contents(); *p != '\0'; p++)
  1838. if (!csdigit(*p))
  1839. break;
  1840. if (*p || s.is_empty())
  1841. curenv->set_font(s);
  1842. else
  1843. curenv->set_font(atoi(s.contents()));
  1844. if (!compatible_flag)
  1845. have_input = 1;
  1846. break;
  1847. }
  1848. case 'F':
  1849. {
  1850. symbol s = read_escape_name(ALLOW_EMPTY);
  1851. if (s.is_null())
  1852. break;
  1853. curenv->set_family(s);
  1854. have_input = 1;
  1855. break;
  1856. }
  1857. case 'g':
  1858. {
  1859. symbol s = read_escape_name();
  1860. if (!(s.is_null() || s.is_empty()))
  1861. interpolate_number_format(s);
  1862. break;
  1863. }
  1864. case 'h':
  1865. if (!get_delim_number(&x, 'm'))
  1866. break;
  1867. type = TOKEN_NODE;
  1868. nd = new hmotion_node(x, curenv->get_fill_color());
  1869. return;
  1870. case 'H':
  1871. // don't take height increments relative to previous height if
  1872. // in compatibility mode
  1873. if (!compatible_flag && curenv->get_char_height())
  1874. {
  1875. if (get_delim_number(&x, 'z', curenv->get_char_height()))
  1876. curenv->set_char_height(x);
  1877. }
  1878. else
  1879. {
  1880. if (get_delim_number(&x, 'z', curenv->get_requested_point_size()))
  1881. curenv->set_char_height(x);
  1882. }
  1883. if (!compatible_flag)
  1884. have_input = 1;
  1885. break;
  1886. case 'k':
  1887. nm = read_escape_name();
  1888. if (nm.is_null() || nm.is_empty())
  1889. break;
  1890. type = TOKEN_MARK_INPUT;
  1891. return;
  1892. case 'l':
  1893. case 'L':
  1894. {
  1895. charinfo *s = 0;
  1896. if (!get_line_arg(&x, (cc == 'l' ? 'm': 'v'), &s))
  1897. break;
  1898. if (s == 0)
  1899. s = get_charinfo(cc == 'l' ? "ru" : "br");
  1900. type = TOKEN_NODE;
  1901. node *char_node = curenv->make_char_node(s);
  1902. if (cc == 'l')
  1903. nd = new hline_node(x, char_node);
  1904. else
  1905. nd = new vline_node(x, char_node);
  1906. return;
  1907. }
  1908. case 'm':
  1909. do_glyph_color(read_escape_name(ALLOW_EMPTY));
  1910. if (!compatible_flag)
  1911. have_input = 1;
  1912. break;
  1913. case 'M':
  1914. do_fill_color(read_escape_name(ALLOW_EMPTY));
  1915. if (!compatible_flag)
  1916. have_input = 1;
  1917. break;
  1918. case 'n':
  1919. {
  1920. int inc;
  1921. symbol s = read_increment_and_escape_name(&inc);
  1922. if (!(s.is_null() || s.is_empty()))
  1923. interpolate_number_reg(s, inc);
  1924. break;
  1925. }
  1926. case 'N':
  1927. if (!get_delim_number(&val, 0))
  1928. break;
  1929. type = TOKEN_NUMBERED_CHAR;
  1930. return;
  1931. case 'o':
  1932. nd = do_overstrike();
  1933. type = TOKEN_NODE;
  1934. return;
  1935. case 'O':
  1936. nd = do_suppress(read_escape_name());
  1937. if (!nd)
  1938. break;
  1939. type = TOKEN_NODE;
  1940. return;
  1941. case 'p':
  1942. type = TOKEN_SPREAD;
  1943. return;
  1944. case 'r':
  1945. type = TOKEN_NODE;
  1946. nd = new vmotion_node(-curenv->get_size(), curenv->get_fill_color());
  1947. return;
  1948. case 'R':
  1949. do_register();
  1950. if (!compatible_flag)
  1951. have_input = 1;
  1952. break;
  1953. case 's':
  1954. if (read_size(&x))
  1955. curenv->set_size(x);
  1956. if (!compatible_flag)
  1957. have_input = 1;
  1958. break;
  1959. case 'S':
  1960. if (get_delim_number(&x, 0))
  1961. curenv->set_char_slant(x);
  1962. if (!compatible_flag)
  1963. have_input = 1;
  1964. break;
  1965. case 't':
  1966. type = TOKEN_NODE;
  1967. nd = new non_interpreted_char_node('\t');
  1968. return;
  1969. case 'u':
  1970. type = TOKEN_NODE;
  1971. nd = new vmotion_node(-curenv->get_size() / 2,
  1972. curenv->get_fill_color());
  1973. return;
  1974. case 'v':
  1975. if (!get_delim_number(&x, 'v'))
  1976. break;
  1977. type = TOKEN_NODE;
  1978. nd = new vmotion_node(x, curenv->get_fill_color());
  1979. return;
  1980. case 'V':
  1981. {
  1982. symbol s = read_escape_name();
  1983. if (!(s.is_null() || s.is_empty()))
  1984. interpolate_environment_variable(s);
  1985. break;
  1986. }
  1987. case 'w':
  1988. do_width();
  1989. break;
  1990. case 'x':
  1991. if (!get_delim_number(&x, 'v'))
  1992. break;
  1993. type = TOKEN_NODE;
  1994. nd = new extra_size_node(x);
  1995. return;
  1996. case 'X':
  1997. nd = do_special();
  1998. if (!nd)
  1999. break;
  2000. type = TOKEN_NODE;
  2001. return;
  2002. case 'Y':
  2003. {
  2004. symbol s = read_escape_name();
  2005. if (s.is_null() || s.is_empty())
  2006. break;
  2007. request_or_macro *p = lookup_request(s);
  2008. macro *m = p->to_macro();
  2009. if (!m) {
  2010. error("can't transparently throughput a request");
  2011. break;
  2012. }
  2013. nd = new special_node(*m);
  2014. type = TOKEN_NODE;
  2015. return;
  2016. }
  2017. case 'z':
  2018. {
  2019. next();
  2020. if (type == TOKEN_NODE)
  2021. nd = new zero_width_node(nd);
  2022. else {
  2023. charinfo *ci = get_char(1);
  2024. if (ci == 0)
  2025. break;
  2026. node *gn = curenv->make_char_node(ci);
  2027. if (gn == 0)
  2028. break;
  2029. nd = new zero_width_node(gn);
  2030. type = TOKEN_NODE;
  2031. }
  2032. return;
  2033. }
  2034. case 'Z':
  2035. nd = do_zero_width();
  2036. if (nd == 0)
  2037. break;
  2038. type = TOKEN_NODE;
  2039. return;
  2040. case '{':
  2041. goto ESCAPE_LEFT_BRACE;
  2042. case '}':
  2043. goto ESCAPE_RIGHT_BRACE;
  2044. case '\n':
  2045. break;
  2046. case '[':
  2047. if (!compatible_flag) {
  2048. symbol s = read_long_escape_name(WITH_ARGS);
  2049. if (s.is_null() || s.is_empty())
  2050. break;
  2051. if (have_string_arg) {
  2052. have_string_arg = 0;
  2053. nm = composite_glyph_name(s);
  2054. }
  2055. else {
  2056. const char *gn = check_unicode_name(s.contents());
  2057. if (gn) {
  2058. const char *gn_decomposed = decompose_unicode(gn);
  2059. if (gn_decomposed)
  2060. gn = &gn_decomposed[1];
  2061. const char *groff_gn = unicode_to_glyph_name(gn);
  2062. if (groff_gn)
  2063. nm = symbol(groff_gn);
  2064. else {
  2065. char *buf = new char[strlen(gn) + 1 + 1];
  2066. strcpy(buf, "u");
  2067. strcat(buf, gn);
  2068. nm = symbol(buf);
  2069. a_delete buf;
  2070. }
  2071. }
  2072. else
  2073. nm = symbol(s.contents());
  2074. }
  2075. type = TOKEN_SPECIAL;
  2076. return;
  2077. }
  2078. goto handle_normal_char;
  2079. default:
  2080. if (cc != escape_char && cc != '.')
  2081. warning(WARN_ESCAPE, "escape character ignored before %1",
  2082. input_char_description(cc));
  2083. goto handle_normal_char;
  2084. }
  2085. }
  2086. }
  2087. }
  2088. int token::operator==(const token &t)
  2089. {
  2090. if (type != t.type)
  2091. return 0;
  2092. switch(type) {
  2093. case TOKEN_CHAR:
  2094. return c == t.c;
  2095. case TOKEN_SPECIAL:
  2096. return nm == t.nm;
  2097. case TOKEN_NUMBERED_CHAR:
  2098. return val == t.val;
  2099. default:
  2100. return 1;
  2101. }
  2102. }
  2103. int token::operator!=(const token &t)
  2104. {
  2105. return !(*this == t);
  2106. }
  2107. // is token a suitable delimiter (like ')?
  2108. int token::delimiter(int err)
  2109. {
  2110. switch(type) {
  2111. case TOKEN_CHAR:
  2112. switch(c) {
  2113. case '0':
  2114. case '1':
  2115. case '2':
  2116. case '3':
  2117. case '4':
  2118. case '5':
  2119. case '6':
  2120. case '7':
  2121. case '8':
  2122. case '9':
  2123. case '+':
  2124. case '-':
  2125. case '/':
  2126. case '*':
  2127. case '%':
  2128. case '<':
  2129. case '>':
  2130. case '=':
  2131. case '&':
  2132. case ':':
  2133. case '(':
  2134. case ')':
  2135. case '.':
  2136. if (err)
  2137. error("cannot use character `%1' as a starting delimiter", char(c));
  2138. return 0;
  2139. default:
  2140. return 1;
  2141. }
  2142. case TOKEN_NODE:
  2143. case TOKEN_SPACE:
  2144. case TOKEN_STRETCHABLE_SPACE:
  2145. case TOKEN_UNSTRETCHABLE_SPACE:
  2146. case TOKEN_TAB:
  2147. case TOKEN_NEWLINE:
  2148. if (err)
  2149. error("cannot use %1 as a starting delimiter", description());
  2150. return 0;
  2151. default:
  2152. return 1;
  2153. }
  2154. }
  2155. const char *token::description()
  2156. {
  2157. static char buf[4];
  2158. switch (type) {
  2159. case TOKEN_BACKSPACE:
  2160. return "a backspace character";
  2161. case TOKEN_CHAR:
  2162. buf[0] = '`';
  2163. buf[1] = c;
  2164. buf[2] = '\'';
  2165. buf[3] = '\0';
  2166. return buf;
  2167. case TOKEN_DUMMY:
  2168. return "`\\&'";
  2169. case TOKEN_ESCAPE:
  2170. return "`\\e'";
  2171. case TOKEN_HYPHEN_INDICATOR:
  2172. return "`\\%'";
  2173. case TOKEN_INTERRUPT:
  2174. return "`\\c'";
  2175. case TOKEN_ITALIC_CORRECTION:
  2176. return "`\\/'";
  2177. case TOKEN_LEADER:
  2178. return "a leader character";
  2179. case TOKEN_LEFT_BRACE:
  2180. return "`\\{'";
  2181. case TOKEN_MARK_INPUT:
  2182. return "`\\k'";
  2183. case TOKEN_NEWLINE:
  2184. return "newline";
  2185. case TOKEN_NODE:
  2186. return "a node";
  2187. case TOKEN_NUMBERED_CHAR:
  2188. return "`\\N'";
  2189. case TOKEN_RIGHT_BRACE:
  2190. return "`\\}'";
  2191. case TOKEN_SPACE:
  2192. return "a space";
  2193. case TOKEN_SPECIAL:
  2194. return "a special character";
  2195. case TOKEN_SPREAD:
  2196. return "`\\p'";
  2197. case TOKEN_STRETCHABLE_SPACE:
  2198. return "`\\~'";
  2199. case TOKEN_UNSTRETCHABLE_SPACE:
  2200. return "`\\ '";
  2201. case TOKEN_TAB:
  2202. return "a tab character";
  2203. case TOKEN_TRANSPARENT:
  2204. return "`\\!'";
  2205. case TOKEN_TRANSPARENT_DUMMY:
  2206. return "`\\)'";
  2207. case TOKEN_ZERO_WIDTH_BREAK:
  2208. return "`\\:'";
  2209. case TOKEN_EOF:
  2210. return "end of input";
  2211. default:
  2212. break;
  2213. }
  2214. return "a magic token";
  2215. }
  2216. void skip_line()
  2217. {
  2218. while (!tok.newline())
  2219. if (tok.eof())
  2220. return;
  2221. else
  2222. tok.next();
  2223. tok.next();
  2224. }
  2225. void compatible()
  2226. {
  2227. int n;
  2228. if (has_arg() && get_integer(&n))
  2229. compatible_flag = n != 0;
  2230. else
  2231. compatible_flag = 1;
  2232. skip_line();
  2233. }
  2234. static void empty_name_warning(int required)
  2235. {
  2236. if (tok.newline() || tok.eof()) {
  2237. if (required)
  2238. warning(WARN_MISSING, "missing name");
  2239. }
  2240. else if (tok.right_brace() || tok.tab()) {
  2241. const char *start = tok.description();
  2242. do {
  2243. tok.next();
  2244. } while (tok.space() || tok.right_brace() || tok.tab());
  2245. if (!tok.newline() && !tok.eof())
  2246. error("%1 is not allowed before an argument", start);
  2247. else if (required)
  2248. warning(WARN_MISSING, "missing name");
  2249. }
  2250. else if (required)
  2251. error("name expected (got %1)", tok.description());
  2252. else
  2253. error("name expected (got %1): treated as missing", tok.description());
  2254. }
  2255. static void non_empty_name_warning()
  2256. {
  2257. if (!tok.newline() && !tok.eof() && !tok.space() && !tok.tab()
  2258. && !tok.right_brace()
  2259. // We don't want to give a warning for .el\{
  2260. && !tok.left_brace())
  2261. error("%1 is not allowed in a name", tok.description());
  2262. }
  2263. symbol get_name(int required)
  2264. {
  2265. if (compatible_flag) {
  2266. char buf[3];
  2267. tok.skip();
  2268. if ((buf[0] = tok.ch()) != 0) {
  2269. tok.next();
  2270. if ((buf[1] = tok.ch()) != 0) {
  2271. buf[2] = 0;
  2272. tok.make_space();
  2273. }
  2274. else
  2275. non_empty_name_warning();
  2276. return symbol(buf);
  2277. }
  2278. else {
  2279. empty_name_warning(required);
  2280. return NULL_SYMBOL;
  2281. }
  2282. }
  2283. else
  2284. return get_long_name(required);
  2285. }
  2286. symbol get_long_name(int required)
  2287. {
  2288. return do_get_long_name(required, 0);
  2289. }
  2290. static symbol do_get_long_name(int required, char end)
  2291. {
  2292. while (tok.space())
  2293. tok.next();
  2294. char abuf[ABUF_SIZE];
  2295. char *buf = abuf;
  2296. int buf_size = ABUF_SIZE;
  2297. int i = 0;
  2298. for (;;) {
  2299. // If end != 0 we normally have to append a null byte
  2300. if (i + 2 > buf_size) {
  2301. if (buf == abuf) {
  2302. buf = new char[ABUF_SIZE*2];
  2303. memcpy(buf, abuf, buf_size);
  2304. buf_size = ABUF_SIZE*2;
  2305. }
  2306. else {
  2307. char *old_buf = buf;
  2308. buf = new char[buf_size*2];
  2309. memcpy(buf, old_buf, buf_size);
  2310. buf_size *= 2;
  2311. a_delete old_buf;
  2312. }
  2313. }
  2314. if ((buf[i] = tok.ch()) == 0 || buf[i] == end)
  2315. break;
  2316. i++;
  2317. tok.next();
  2318. }
  2319. if (i == 0) {
  2320. empty_name_warning(required);
  2321. return NULL_SYMBOL;
  2322. }
  2323. if (end && buf[i] == end)
  2324. buf[i+1] = '\0';
  2325. else
  2326. non_empty_name_warning();
  2327. if (buf == abuf)
  2328. return symbol(buf);
  2329. else {
  2330. symbol s(buf);
  2331. a_delete buf;
  2332. return s;
  2333. }
  2334. }
  2335. void exit_troff()
  2336. {
  2337. exit_started = 1;
  2338. topdiv->set_last_page();
  2339. if (!end_macro_name.is_null()) {
  2340. spring_trap(end_macro_name);
  2341. tok.next();
  2342. process_input_stack();
  2343. }
  2344. curenv->final_break();
  2345. tok.next();
  2346. process_input_stack();
  2347. end_diversions();
  2348. if (topdiv->get_page_length() > 0) {
  2349. done_end_macro = 1;
  2350. topdiv->set_ejecting();
  2351. static unsigned char buf[2] = { LAST_PAGE_EJECTOR, '\0' };
  2352. input_stack::push(make_temp_iterator((char *)buf));
  2353. topdiv->space(topdiv->get_page_length(), 1);
  2354. tok.next();
  2355. process_input_stack();
  2356. seen_last_page_ejector = 1; // should be set already
  2357. topdiv->set_ejecting();
  2358. push_page_ejector();
  2359. topdiv->space(topdiv->get_page_length(), 1);
  2360. tok.next();
  2361. process_input_stack();
  2362. }
  2363. // This will only happen if a trap-invoked macro starts a diversion,
  2364. // or if vertical position traps have been disabled.
  2365. cleanup_and_exit(0);
  2366. }
  2367. // This implements .ex. The input stack must be cleared before calling
  2368. // exit_troff().
  2369. void exit_request()
  2370. {
  2371. input_stack::clear();
  2372. if (exit_started)
  2373. tok.next();
  2374. else
  2375. exit_troff();
  2376. }
  2377. void return_macro_request()
  2378. {
  2379. if (has_arg() && tok.ch())
  2380. input_stack::pop_macro();
  2381. input_stack::pop_macro();
  2382. tok.next();
  2383. }
  2384. void end_macro()
  2385. {
  2386. end_macro_name = get_name();
  2387. skip_line();
  2388. }
  2389. void blank_line_macro()
  2390. {
  2391. blank_line_macro_name = get_name();
  2392. skip_line();
  2393. }
  2394. static void trapping_blank_line()
  2395. {
  2396. if (!blank_line_macro_name.is_null())
  2397. spring_trap(blank_line_macro_name);
  2398. else
  2399. blank_line();
  2400. }
  2401. void do_request()
  2402. {
  2403. int old_compatible_flag = compatible_flag;
  2404. compatible_flag = 0;
  2405. symbol nm = get_name();
  2406. if (nm.is_null())
  2407. skip_line();
  2408. else
  2409. interpolate_macro(nm);
  2410. compatible_flag = old_compatible_flag;
  2411. }
  2412. inline int possibly_handle_first_page_transition()
  2413. {
  2414. if (topdiv->before_first_page && curdiv == topdiv && !curenv->is_dummy()) {
  2415. handle_first_page_transition();
  2416. return 1;
  2417. }
  2418. else
  2419. return 0;
  2420. }
  2421. static int transparent_translate(int cc)
  2422. {
  2423. if (!invalid_input_char(cc)) {
  2424. charinfo *ci = charset_table[cc];
  2425. switch (ci->get_special_translation(1)) {
  2426. case charinfo::TRANSLATE_SPACE:
  2427. return ' ';
  2428. case charinfo::TRANSLATE_STRETCHABLE_SPACE:
  2429. return ESCAPE_TILDE;
  2430. case charinfo::TRANSLATE_DUMMY:
  2431. return ESCAPE_AMPERSAND;
  2432. case charinfo::TRANSLATE_HYPHEN_INDICATOR:
  2433. return ESCAPE_PERCENT;
  2434. }
  2435. // This is really ugly.
  2436. ci = ci->get_translation(1);
  2437. if (ci) {
  2438. int c = ci->get_ascii_code();
  2439. if (c != '\0')
  2440. return c;
  2441. error("can't translate %1 to special character `%2'"
  2442. " in transparent throughput",
  2443. input_char_description(cc),
  2444. ci->nm.contents());
  2445. }
  2446. }
  2447. return cc;
  2448. }
  2449. class int_stack {
  2450. struct int_stack_element {
  2451. int n;
  2452. int_stack_element *next;
  2453. } *top;
  2454. public:
  2455. int_stack();
  2456. ~int_stack();
  2457. void push(int);
  2458. int is_empty();
  2459. int pop();
  2460. };
  2461. int_stack::int_stack()
  2462. {
  2463. top = 0;
  2464. }
  2465. int_stack::~int_stack()
  2466. {
  2467. while (top != 0) {
  2468. int_stack_element *temp = top;
  2469. top = top->next;
  2470. delete temp;
  2471. }
  2472. }
  2473. int int_stack::is_empty()
  2474. {
  2475. return top == 0;
  2476. }
  2477. void int_stack::push(int n)
  2478. {
  2479. int_stack_element *p = new int_stack_element;
  2480. p->next = top;
  2481. p->n = n;
  2482. top = p;
  2483. }
  2484. int int_stack::pop()
  2485. {
  2486. assert(top != 0);
  2487. int_stack_element *p = top;
  2488. top = top->next;
  2489. int n = p->n;
  2490. delete p;
  2491. return n;
  2492. }
  2493. int node::reread(int *)
  2494. {
  2495. return 0;
  2496. }
  2497. int global_diverted_space = 0;
  2498. int diverted_space_node::reread(int *bolp)
  2499. {
  2500. global_diverted_space = 1;
  2501. if (curenv->get_fill())
  2502. trapping_blank_line();
  2503. else
  2504. curdiv->space(n);
  2505. global_diverted_space = 0;
  2506. *bolp = 1;
  2507. return 1;
  2508. }
  2509. int diverted_copy_file_node::reread(int *bolp)
  2510. {
  2511. curdiv->copy_file(filename.contents());
  2512. *bolp = 1;
  2513. return 1;
  2514. }
  2515. int word_space_node::reread(int *)
  2516. {
  2517. if (unformat) {
  2518. for (width_list *w = orig_width; w; w = w->next)
  2519. curenv->space(w->width, w->sentence_width);
  2520. unformat = 0;
  2521. return 1;
  2522. }
  2523. return 0;
  2524. }
  2525. int unbreakable_space_node::reread(int *)
  2526. {
  2527. return 0;
  2528. }
  2529. int hmotion_node::reread(int *)
  2530. {
  2531. if (unformat && was_tab) {
  2532. curenv->handle_tab(0);
  2533. unformat = 0;
  2534. return 1;
  2535. }
  2536. return 0;
  2537. }
  2538. void process_input_stack()
  2539. {
  2540. int_stack trap_bol_stack;
  2541. int bol = 1;
  2542. for (;;) {
  2543. int suppress_next = 0;
  2544. switch (tok.type) {
  2545. case token::TOKEN_CHAR:
  2546. {
  2547. unsigned char ch = tok.c;
  2548. if (bol && !have_input
  2549. && (ch == curenv->control_char
  2550. || ch == curenv->no_break_control_char)) {
  2551. break_flag = ch == curenv->control_char;
  2552. // skip tabs as well as spaces here
  2553. do {
  2554. tok.next();
  2555. } while (tok.white_space());
  2556. symbol nm = get_name();
  2557. #if defined(DEBUGGING)
  2558. if (debug_state) {
  2559. if (! nm.is_null()) {
  2560. if (strcmp(nm.contents(), "test") == 0) {
  2561. fprintf(stderr, "found it!\n");
  2562. fflush(stderr);
  2563. }
  2564. fprintf(stderr, "interpreting [%s]", nm.contents());
  2565. if (strcmp(nm.contents(), "di") == 0 && topdiv != curdiv)
  2566. fprintf(stderr, " currently in diversion: %s",
  2567. curdiv->get_diversion_name());
  2568. fprintf(stderr, "\n");
  2569. fflush(stderr);
  2570. }
  2571. }
  2572. #endif
  2573. if (nm.is_null())
  2574. skip_line();
  2575. else {
  2576. interpolate_macro(nm);
  2577. #if defined(DEBUGGING)
  2578. if (debug_state) {
  2579. fprintf(stderr, "finished interpreting [%s] and environment state is\n", nm.contents());
  2580. curenv->dump_troff_state();
  2581. }
  2582. #endif
  2583. }
  2584. suppress_next = 1;
  2585. }
  2586. else {
  2587. if (possibly_handle_first_page_transition())
  2588. ;
  2589. else {
  2590. for (;;) {
  2591. #if defined(DEBUGGING)
  2592. if (debug_state) {
  2593. fprintf(stderr, "found [%c]\n", ch); fflush(stderr);
  2594. }
  2595. #endif
  2596. curenv->add_char(charset_table[ch]);
  2597. tok.next();
  2598. if (tok.type != token::TOKEN_CHAR)
  2599. break;
  2600. ch = tok.c;
  2601. }
  2602. suppress_next = 1;
  2603. bol = 0;
  2604. }
  2605. }
  2606. break;
  2607. }
  2608. case token::TOKEN_TRANSPARENT:
  2609. {
  2610. if (bol) {
  2611. if (possibly_handle_first_page_transition())
  2612. ;
  2613. else {
  2614. int cc;
  2615. do {
  2616. node *n;
  2617. cc = get_copy(&n);
  2618. if (cc != EOF)
  2619. if (cc != '\0')
  2620. curdiv->transparent_output(transparent_translate(cc));
  2621. else
  2622. curdiv->transparent_output(n);
  2623. } while (cc != '\n' && cc != EOF);
  2624. if (cc == EOF)
  2625. curdiv->transparent_output('\n');
  2626. }
  2627. }
  2628. break;
  2629. }
  2630. case token::TOKEN_NEWLINE:
  2631. {
  2632. if (bol && !old_have_input
  2633. && !curenv->get_prev_line_interrupted())
  2634. trapping_blank_line();
  2635. else {
  2636. curenv->newline();
  2637. bol = 1;
  2638. }
  2639. break;
  2640. }
  2641. case token::TOKEN_REQUEST:
  2642. {
  2643. int request_code = tok.c;
  2644. tok.next();
  2645. switch (request_code) {
  2646. case TITLE_REQUEST:
  2647. title();
  2648. break;
  2649. case COPY_FILE_REQUEST:
  2650. copy_file();
  2651. break;
  2652. case TRANSPARENT_FILE_REQUEST:
  2653. transparent_file();
  2654. break;
  2655. #ifdef COLUMN
  2656. case VJUSTIFY_REQUEST:
  2657. vjustify();
  2658. break;
  2659. #endif /* COLUMN */
  2660. default:
  2661. assert(0);
  2662. break;
  2663. }
  2664. suppress_next = 1;
  2665. break;
  2666. }
  2667. case token::TOKEN_SPACE:
  2668. {
  2669. if (possibly_handle_first_page_transition())
  2670. ;
  2671. else if (bol && !curenv->get_prev_line_interrupted()) {
  2672. int nspaces = 0;
  2673. // save space_width now so that it isn't changed by \f or \s
  2674. // which we wouldn't notice here
  2675. hunits space_width = curenv->get_space_width();
  2676. do {
  2677. nspaces += tok.nspaces();
  2678. tok.next();
  2679. } while (tok.space());
  2680. if (tok.newline())
  2681. trapping_blank_line();
  2682. else {
  2683. push_token(tok);
  2684. curenv->do_break();
  2685. curenv->add_node(new hmotion_node(space_width * nspaces,
  2686. curenv->get_fill_color()));
  2687. bol = 0;
  2688. }
  2689. }
  2690. else {
  2691. curenv->space();
  2692. bol = 0;
  2693. }
  2694. break;
  2695. }
  2696. case token::TOKEN_EOF:
  2697. return;
  2698. case token::TOKEN_NODE:
  2699. {
  2700. if (possibly_handle_first_page_transition())
  2701. ;
  2702. else if (tok.nd->reread(&bol)) {
  2703. delete tok.nd;
  2704. tok.nd = 0;
  2705. }
  2706. else {
  2707. curenv->add_node(tok.nd);
  2708. tok.nd = 0;
  2709. bol = 0;
  2710. curenv->possibly_break_line(1);
  2711. }
  2712. break;
  2713. }
  2714. case token::TOKEN_PAGE_EJECTOR:
  2715. {
  2716. continue_page_eject();
  2717. // I think we just want to preserve bol.
  2718. // bol = 1;
  2719. break;
  2720. }
  2721. case token::TOKEN_BEGIN_TRAP:
  2722. {
  2723. trap_bol_stack.push(bol);
  2724. bol = 1;
  2725. have_input = 0;
  2726. break;
  2727. }
  2728. case token::TOKEN_END_TRAP:
  2729. {
  2730. if (trap_bol_stack.is_empty())
  2731. error("spurious end trap token detected!");
  2732. else
  2733. bol = trap_bol_stack.pop();
  2734. have_input = 0;
  2735. /* I'm not totally happy about this. But I can't think of any other
  2736. way to do it. Doing an output_pending_lines() whenever a
  2737. TOKEN_END_TRAP is detected doesn't work: for example,
  2738. .wh -1i x
  2739. .de x
  2740. 'bp
  2741. ..
  2742. .wh -.5i y
  2743. .de y
  2744. .tl ''-%-''
  2745. ..
  2746. .br
  2747. .ll .5i
  2748. .sp |\n(.pu-1i-.5v
  2749. a\%very\%very\%long\%word
  2750. will print all but the first lines from the word immediately
  2751. after the footer, rather than on the next page. */
  2752. if (trap_bol_stack.is_empty())
  2753. curenv->output_pending_lines();
  2754. break;
  2755. }
  2756. default:
  2757. {
  2758. bol = 0;
  2759. tok.process();
  2760. break;
  2761. }
  2762. }
  2763. if (!suppress_next)
  2764. tok.next();
  2765. trap_sprung_flag = 0;
  2766. }
  2767. }
  2768. #ifdef WIDOW_CONTROL
  2769. void flush_pending_lines()
  2770. {
  2771. while (!tok.newline() && !tok.eof())
  2772. tok.next();
  2773. curenv->output_pending_lines();
  2774. tok.next();
  2775. }
  2776. #endif /* WIDOW_CONTROL */
  2777. request_or_macro::request_or_macro()
  2778. {
  2779. }
  2780. macro *request_or_macro::to_macro()
  2781. {
  2782. return 0;
  2783. }
  2784. request::request(REQUEST_FUNCP pp) : p(pp)
  2785. {
  2786. }
  2787. void request::invoke(symbol)
  2788. {
  2789. (*p)();
  2790. }
  2791. struct char_block {
  2792. enum { SIZE = 128 };
  2793. unsigned char s[SIZE];
  2794. char_block *next;
  2795. char_block();
  2796. };
  2797. char_block::char_block()
  2798. : next(0)
  2799. {
  2800. }
  2801. class char_list {
  2802. public:
  2803. char_list();
  2804. ~char_list();
  2805. void append(unsigned char);
  2806. void set(unsigned char, int);
  2807. unsigned char get(int);
  2808. int length();
  2809. private:
  2810. unsigned char *ptr;
  2811. int len;
  2812. char_block *head;
  2813. char_block *tail;
  2814. friend class macro_header;
  2815. friend class string_iterator;
  2816. };
  2817. char_list::char_list()
  2818. : ptr(0), len(0), head(0), tail(0)
  2819. {
  2820. }
  2821. char_list::~char_list()
  2822. {
  2823. while (head != 0) {
  2824. char_block *tem = head;
  2825. head = head->next;
  2826. delete tem;
  2827. }
  2828. }
  2829. int char_list::length()
  2830. {
  2831. return len;
  2832. }
  2833. void char_list::append(unsigned char c)
  2834. {
  2835. if (tail == 0) {
  2836. head = tail = new char_block;
  2837. ptr = tail->s;
  2838. }
  2839. else {
  2840. if (ptr >= tail->s + char_block::SIZE) {
  2841. tail->next = new char_block;
  2842. tail = tail->next;
  2843. ptr = tail->s;
  2844. }
  2845. }
  2846. *ptr++ = c;
  2847. len++;
  2848. }
  2849. void char_list::set(unsigned char c, int offset)
  2850. {
  2851. assert(len > offset);
  2852. // optimization for access at the end
  2853. int boundary = len - len % char_block::SIZE;
  2854. if (offset >= boundary) {
  2855. *(tail->s + offset - boundary) = c;
  2856. return;
  2857. }
  2858. char_block *tem = head;
  2859. int l = 0;
  2860. for (;;) {
  2861. l += char_block::SIZE;
  2862. if (l > offset) {
  2863. *(tem->s + offset % char_block::SIZE) = c;
  2864. return;
  2865. }
  2866. tem = tem->next;
  2867. }
  2868. }
  2869. unsigned char char_list::get(int offset)
  2870. {
  2871. assert(len > offset);
  2872. // optimization for access at the end
  2873. int boundary = len - len % char_block::SIZE;
  2874. if (offset >= boundary)
  2875. return *(tail->s + offset - boundary);
  2876. char_block *tem = head;
  2877. int l = 0;
  2878. for (;;) {
  2879. l += char_block::SIZE;
  2880. if (l > offset)
  2881. return *(tem->s + offset % char_block::SIZE);
  2882. tem = tem->next;
  2883. }
  2884. }
  2885. class node_list {
  2886. node *head;
  2887. node *tail;
  2888. public:
  2889. node_list();
  2890. ~node_list();
  2891. void append(node *);
  2892. int length();
  2893. node *extract();
  2894. friend class macro_header;
  2895. friend class string_iterator;
  2896. };
  2897. void node_list::append(node *n)
  2898. {
  2899. if (head == 0) {
  2900. n->next = 0;
  2901. head = tail = n;
  2902. }
  2903. else {
  2904. n->next = 0;
  2905. tail = tail->next = n;
  2906. }
  2907. }
  2908. int node_list::length()
  2909. {
  2910. int total = 0;
  2911. for (node *n = head; n != 0; n = n->next)
  2912. ++total;
  2913. return total;
  2914. }
  2915. node_list::node_list()
  2916. {
  2917. head = tail = 0;
  2918. }
  2919. node *node_list::extract()
  2920. {
  2921. node *temp = head;
  2922. head = tail = 0;
  2923. return temp;
  2924. }
  2925. node_list::~node_list()
  2926. {
  2927. delete_node_list(head);
  2928. }
  2929. class macro_header {
  2930. public:
  2931. int count;
  2932. char_list cl;
  2933. node_list nl;
  2934. macro_header() { count = 1; }
  2935. macro_header *copy(int);
  2936. };
  2937. macro::~macro()
  2938. {
  2939. if (p != 0 && --(p->count) <= 0)
  2940. delete p;
  2941. }
  2942. macro::macro()
  2943. : is_a_diversion(0)
  2944. {
  2945. if (!input_stack::get_location(1, &filename, &lineno)) {
  2946. filename = 0;
  2947. lineno = 0;
  2948. }
  2949. len = 0;
  2950. empty_macro = 1;
  2951. p = 0;
  2952. }
  2953. macro::macro(const macro &m)
  2954. : filename(m.filename), lineno(m.lineno), len(m.len),
  2955. empty_macro(m.empty_macro), is_a_diversion(m.is_a_diversion), p(m.p)
  2956. {
  2957. if (p != 0)
  2958. p->count++;
  2959. }
  2960. macro::macro(int is_div)
  2961. : is_a_diversion(is_div)
  2962. {
  2963. if (!input_stack::get_location(1, &filename, &lineno)) {
  2964. filename = 0;
  2965. lineno = 0;
  2966. }
  2967. len = 0;
  2968. empty_macro = 1;
  2969. p = 0;
  2970. }
  2971. int macro::is_diversion()
  2972. {
  2973. return is_a_diversion;
  2974. }
  2975. macro &macro::operator=(const macro &m)
  2976. {
  2977. // don't assign object
  2978. if (m.p != 0)
  2979. m.p->count++;
  2980. if (p != 0 && --(p->count) <= 0)
  2981. delete p;
  2982. p = m.p;
  2983. filename = m.filename;
  2984. lineno = m.lineno;
  2985. len = m.len;
  2986. empty_macro = m.empty_macro;
  2987. is_a_diversion = m.is_a_diversion;
  2988. return *this;
  2989. }
  2990. void macro::append(unsigned char c)
  2991. {
  2992. assert(c != 0);
  2993. if (p == 0)
  2994. p = new macro_header;
  2995. if (p->cl.length() != len) {
  2996. macro_header *tem = p->copy(len);
  2997. if (--(p->count) <= 0)
  2998. delete p;
  2999. p = tem;
  3000. }
  3001. p->cl.append(c);
  3002. ++len;
  3003. if (c != PUSH_GROFF_MODE && c != PUSH_COMP_MODE && c != POP_GROFFCOMP_MODE)
  3004. empty_macro = 0;
  3005. }
  3006. void macro::set(unsigned char c, int offset)
  3007. {
  3008. assert(p != 0);
  3009. assert(c != 0);
  3010. p->cl.set(c, offset);
  3011. }
  3012. unsigned char macro::get(int offset)
  3013. {
  3014. assert(p != 0);
  3015. return p->cl.get(offset);
  3016. }
  3017. int macro::length()
  3018. {
  3019. return len;
  3020. }
  3021. void macro::append_str(const char *s)
  3022. {
  3023. int i = 0;
  3024. if (s) {
  3025. while (s[i] != (char)0) {
  3026. append(s[i]);
  3027. i++;
  3028. }
  3029. }
  3030. }
  3031. void macro::append(node *n)
  3032. {
  3033. assert(n != 0);
  3034. if (p == 0)
  3035. p = new macro_header;
  3036. if (p->cl.length() != len) {
  3037. macro_header *tem = p->copy(len);
  3038. if (--(p->count) <= 0)
  3039. delete p;
  3040. p = tem;
  3041. }
  3042. p->cl.append(0);
  3043. p->nl.append(n);
  3044. ++len;
  3045. empty_macro = 0;
  3046. }
  3047. void macro::append_unsigned(unsigned int i)
  3048. {
  3049. unsigned int j = i / 10;
  3050. if (j != 0)
  3051. append_unsigned(j);
  3052. append(((unsigned char)(((int)'0') + i % 10)));
  3053. }
  3054. void macro::append_int(int i)
  3055. {
  3056. if (i < 0) {
  3057. append('-');
  3058. i = -i;
  3059. }
  3060. append_unsigned((unsigned int)i);
  3061. }
  3062. void macro::print_size()
  3063. {
  3064. errprint("%1", len);
  3065. }
  3066. // make a copy of the first n bytes
  3067. macro_header *macro_header::copy(int n)
  3068. {
  3069. macro_header *p = new macro_header;
  3070. char_block *bp = cl.head;
  3071. unsigned char *ptr = bp->s;
  3072. node *nd = nl.head;
  3073. while (--n >= 0) {
  3074. if (ptr >= bp->s + char_block::SIZE) {
  3075. bp = bp->next;
  3076. ptr = bp->s;
  3077. }
  3078. unsigned char c = *ptr++;
  3079. p->cl.append(c);
  3080. if (c == 0) {
  3081. p->nl.append(nd->copy());
  3082. nd = nd->next;
  3083. }
  3084. }
  3085. return p;
  3086. }
  3087. void print_macros()
  3088. {
  3089. object_dictionary_iterator iter(request_dictionary);
  3090. request_or_macro *rm;
  3091. symbol s;
  3092. while (iter.get(&s, (object **)&rm)) {
  3093. assert(!s.is_null());
  3094. macro *m = rm->to_macro();
  3095. if (m) {
  3096. errprint("%1\t", s.contents());
  3097. m->print_size();
  3098. errprint("\n");
  3099. }
  3100. }
  3101. fflush(stderr);
  3102. skip_line();
  3103. }
  3104. class string_iterator : public input_iterator {
  3105. macro mac;
  3106. const char *how_invoked;
  3107. int newline_flag;
  3108. int lineno;
  3109. char_block *bp;
  3110. int count; // of characters remaining
  3111. node *nd;
  3112. int saved_compatible_flag;
  3113. protected:
  3114. symbol nm;
  3115. string_iterator();
  3116. public:
  3117. string_iterator(const macro &m, const char *p = 0, symbol s = NULL_SYMBOL);
  3118. int fill(node **);
  3119. int peek();
  3120. int get_location(int, const char **, int *);
  3121. void backtrace();
  3122. void save_compatible_flag(int f) { saved_compatible_flag = f; }
  3123. int get_compatible_flag() { return saved_compatible_flag; }
  3124. int is_diversion();
  3125. };
  3126. string_iterator::string_iterator(const macro &m, const char *p, symbol s)
  3127. : input_iterator(m.is_a_diversion), mac(m), how_invoked(p), newline_flag(0),
  3128. lineno(1), nm(s)
  3129. {
  3130. count = mac.len;
  3131. if (count != 0) {
  3132. bp = mac.p->cl.head;
  3133. nd = mac.p->nl.head;
  3134. ptr = eptr = bp->s;
  3135. }
  3136. else {
  3137. bp = 0;
  3138. nd = 0;
  3139. ptr = eptr = 0;
  3140. }
  3141. }
  3142. string_iterator::string_iterator()
  3143. {
  3144. bp = 0;
  3145. nd = 0;
  3146. ptr = eptr = 0;
  3147. newline_flag = 0;
  3148. how_invoked = 0;
  3149. lineno = 1;
  3150. count = 0;
  3151. }
  3152. int string_iterator::is_diversion()
  3153. {
  3154. return mac.is_diversion();
  3155. }
  3156. int string_iterator::fill(node **np)
  3157. {
  3158. if (newline_flag)
  3159. lineno++;
  3160. newline_flag = 0;
  3161. if (count <= 0)
  3162. return EOF;
  3163. const unsigned char *p = eptr;
  3164. if (p >= bp->s + char_block::SIZE) {
  3165. bp = bp->next;
  3166. p = bp->s;
  3167. }
  3168. if (*p == '\0') {
  3169. if (np) {
  3170. *np = nd->copy();
  3171. if (is_diversion())
  3172. (*np)->div_nest_level = input_stack::get_div_level();
  3173. else
  3174. (*np)->div_nest_level = 0;
  3175. }
  3176. nd = nd->next;
  3177. eptr = ptr = p + 1;
  3178. count--;
  3179. return 0;
  3180. }
  3181. const unsigned char *e = bp->s + char_block::SIZE;
  3182. if (e - p > count)
  3183. e = p + count;
  3184. ptr = p;
  3185. while (p < e) {
  3186. unsigned char c = *p;
  3187. if (c == '\n' || c == ESCAPE_NEWLINE) {
  3188. newline_flag = 1;
  3189. p++;
  3190. break;
  3191. }
  3192. if (c == '\0')
  3193. break;
  3194. p++;
  3195. }
  3196. eptr = p;
  3197. count -= p - ptr;
  3198. return *ptr++;
  3199. }
  3200. int string_iterator::peek()
  3201. {
  3202. if (count <= 0)
  3203. return EOF;
  3204. const unsigned char *p = eptr;
  3205. if (p >= bp->s + char_block::SIZE) {
  3206. p = bp->next->s;
  3207. }
  3208. return *p;
  3209. }
  3210. int string_iterator::get_location(int allow_macro,
  3211. const char **filep, int *linep)
  3212. {
  3213. if (!allow_macro)
  3214. return 0;
  3215. if (mac.filename == 0)
  3216. return 0;
  3217. *filep = mac.filename;
  3218. *linep = mac.lineno + lineno - 1;
  3219. return 1;
  3220. }
  3221. void string_iterator::backtrace()
  3222. {
  3223. if (mac.filename) {
  3224. errprint("%1:%2: backtrace", mac.filename, mac.lineno + lineno - 1);
  3225. if (how_invoked) {
  3226. if (!nm.is_null())
  3227. errprint(": %1 `%2'\n", how_invoked, nm.contents());
  3228. else
  3229. errprint(": %1\n", how_invoked);
  3230. }
  3231. else
  3232. errprint("\n");
  3233. }
  3234. }
  3235. class temp_iterator : public input_iterator {
  3236. unsigned char *base;
  3237. temp_iterator(const char *, int len);
  3238. public:
  3239. ~temp_iterator();
  3240. friend input_iterator *make_temp_iterator(const char *);
  3241. };
  3242. #ifdef __GNUG__
  3243. inline
  3244. #endif
  3245. temp_iterator::temp_iterator(const char *s, int len)
  3246. {
  3247. base = new unsigned char[len];
  3248. memcpy(base, s, len);
  3249. ptr = base;
  3250. eptr = base + len;
  3251. }
  3252. temp_iterator::~temp_iterator()
  3253. {
  3254. a_delete base;
  3255. }
  3256. class small_temp_iterator : public input_iterator {
  3257. private:
  3258. small_temp_iterator(const char *, int);
  3259. ~small_temp_iterator();
  3260. enum { BLOCK = 16 };
  3261. static small_temp_iterator *free_list;
  3262. void *operator new(size_t);
  3263. void operator delete(void *);
  3264. enum { SIZE = 12 };
  3265. unsigned char buf[SIZE];
  3266. friend input_iterator *make_temp_iterator(const char *);
  3267. };
  3268. small_temp_iterator *small_temp_iterator::free_list = 0;
  3269. void *small_temp_iterator::operator new(size_t n)
  3270. {
  3271. assert(n == sizeof(small_temp_iterator));
  3272. if (!free_list) {
  3273. free_list =
  3274. (small_temp_iterator *)new char[sizeof(small_temp_iterator)*BLOCK];
  3275. for (int i = 0; i < BLOCK - 1; i++)
  3276. free_list[i].next = free_list + i + 1;
  3277. free_list[BLOCK-1].next = 0;
  3278. }
  3279. small_temp_iterator *p = free_list;
  3280. free_list = (small_temp_iterator *)(free_list->next);
  3281. p->next = 0;
  3282. return p;
  3283. }
  3284. #ifdef __GNUG__
  3285. inline
  3286. #endif
  3287. void small_temp_iterator::operator delete(void *p)
  3288. {
  3289. if (p) {
  3290. ((small_temp_iterator *)p)->next = free_list;
  3291. free_list = (small_temp_iterator *)p;
  3292. }
  3293. }
  3294. small_temp_iterator::~small_temp_iterator()
  3295. {
  3296. }
  3297. #ifdef __GNUG__
  3298. inline
  3299. #endif
  3300. small_temp_iterator::small_temp_iterator(const char *s, int len)
  3301. {
  3302. for (int i = 0; i < len; i++)
  3303. buf[i] = s[i];
  3304. ptr = buf;
  3305. eptr = buf + len;
  3306. }
  3307. input_iterator *make_temp_iterator(const char *s)
  3308. {
  3309. if (s == 0)
  3310. return new small_temp_iterator(s, 0);
  3311. else {
  3312. int n = strlen(s);
  3313. if (n <= small_temp_iterator::SIZE)
  3314. return new small_temp_iterator(s, n);
  3315. else
  3316. return new temp_iterator(s, n);
  3317. }
  3318. }
  3319. // this is used when macros with arguments are interpolated
  3320. struct arg_list {
  3321. macro mac;
  3322. arg_list *next;
  3323. arg_list(const macro &);
  3324. ~arg_list();
  3325. };
  3326. arg_list::arg_list(const macro &m) : mac(m), next(0)
  3327. {
  3328. }
  3329. arg_list::~arg_list()
  3330. {
  3331. }
  3332. class macro_iterator : public string_iterator {
  3333. arg_list *args;
  3334. int argc;
  3335. public:
  3336. macro_iterator(symbol, macro &, const char *how_invoked = "macro");
  3337. macro_iterator();
  3338. ~macro_iterator();
  3339. int has_args() { return 1; }
  3340. input_iterator *get_arg(int i);
  3341. int nargs() { return argc; }
  3342. void add_arg(const macro &m);
  3343. void shift(int n);
  3344. int is_macro() { return 1; }
  3345. int is_diversion();
  3346. };
  3347. input_iterator *macro_iterator::get_arg(int i)
  3348. {
  3349. if (i == 0)
  3350. return make_temp_iterator(nm.contents());
  3351. if (i > 0 && i <= argc) {
  3352. arg_list *p = args;
  3353. for (int j = 1; j < i; j++) {
  3354. assert(p != 0);
  3355. p = p->next;
  3356. }
  3357. return new string_iterator(p->mac);
  3358. }
  3359. else
  3360. return 0;
  3361. }
  3362. void macro_iterator::add_arg(const macro &m)
  3363. {
  3364. arg_list **p;
  3365. for (p = &args; *p; p = &((*p)->next))
  3366. ;
  3367. *p = new arg_list(m);
  3368. ++argc;
  3369. }
  3370. void macro_iterator::shift(int n)
  3371. {
  3372. while (n > 0 && argc > 0) {
  3373. arg_list *tem = args;
  3374. args = args->next;
  3375. delete tem;
  3376. --argc;
  3377. --n;
  3378. }
  3379. }
  3380. // This gets used by eg .if '\?xxx\?''.
  3381. int operator==(const macro &m1, const macro &m2)
  3382. {
  3383. if (m1.len != m2.len)
  3384. return 0;
  3385. string_iterator iter1(m1);
  3386. string_iterator iter2(m2);
  3387. int n = m1.len;
  3388. while (--n >= 0) {
  3389. node *nd1 = 0;
  3390. int c1 = iter1.get(&nd1);
  3391. assert(c1 != EOF);
  3392. node *nd2 = 0;
  3393. int c2 = iter2.get(&nd2);
  3394. assert(c2 != EOF);
  3395. if (c1 != c2) {
  3396. if (c1 == 0)
  3397. delete nd1;
  3398. else if (c2 == 0)
  3399. delete nd2;
  3400. return 0;
  3401. }
  3402. if (c1 == 0) {
  3403. assert(nd1 != 0);
  3404. assert(nd2 != 0);
  3405. int are_same = nd1->type() == nd2->type() && nd1->same(nd2);
  3406. delete nd1;
  3407. delete nd2;
  3408. if (!are_same)
  3409. return 0;
  3410. }
  3411. }
  3412. return 1;
  3413. }
  3414. static void interpolate_macro(symbol nm)
  3415. {
  3416. request_or_macro *p = (request_or_macro *)request_dictionary.lookup(nm);
  3417. if (p == 0) {
  3418. int warned = 0;
  3419. const char *s = nm.contents();
  3420. if (strlen(s) > 2) {
  3421. request_or_macro *r;
  3422. char buf[3];
  3423. buf[0] = s[0];
  3424. buf[1] = s[1];
  3425. buf[2] = '\0';
  3426. r = (request_or_macro *)request_dictionary.lookup(symbol(buf));
  3427. if (r) {
  3428. macro *m = r->to_macro();
  3429. if (!m || !m->empty())
  3430. warned = warning(WARN_SPACE,
  3431. "macro `%1' not defined "
  3432. "(probably missing space after `%2')",
  3433. nm.contents(), buf);
  3434. }
  3435. }
  3436. if (!warned) {
  3437. warning(WARN_MAC, "macro `%1' not defined", nm.contents());
  3438. p = new macro;
  3439. request_dictionary.define(nm, p);
  3440. }
  3441. }
  3442. if (p)
  3443. p->invoke(nm);
  3444. else {
  3445. skip_line();
  3446. return;
  3447. }
  3448. }
  3449. static void decode_args(macro_iterator *mi)
  3450. {
  3451. if (!tok.newline() && !tok.eof()) {
  3452. node *n;
  3453. int c = get_copy(&n);
  3454. for (;;) {
  3455. while (c == ' ')
  3456. c = get_copy(&n);
  3457. if (c == '\n' || c == EOF)
  3458. break;
  3459. macro arg;
  3460. int quote_input_level = 0;
  3461. int done_tab_warning = 0;
  3462. if (c == '"') {
  3463. quote_input_level = input_stack::get_level();
  3464. c = get_copy(&n);
  3465. }
  3466. arg.append(compatible_flag ? PUSH_COMP_MODE : PUSH_GROFF_MODE);
  3467. while (c != EOF && c != '\n' && !(c == ' ' && quote_input_level == 0)) {
  3468. if (quote_input_level > 0 && c == '"'
  3469. && (compatible_flag
  3470. || input_stack::get_level() == quote_input_level)) {
  3471. c = get_copy(&n);
  3472. if (c == '"') {
  3473. arg.append(c);
  3474. c = get_copy(&n);
  3475. }
  3476. else
  3477. break;
  3478. }
  3479. else {
  3480. if (c == 0)
  3481. arg.append(n);
  3482. else {
  3483. if (c == '\t' && quote_input_level == 0 && !done_tab_warning) {
  3484. warning(WARN_TAB, "tab character in unquoted macro argument");
  3485. done_tab_warning = 1;
  3486. }
  3487. arg.append(c);
  3488. }
  3489. c = get_copy(&n);
  3490. }
  3491. }
  3492. arg.append(POP_GROFFCOMP_MODE);
  3493. mi->add_arg(arg);
  3494. }
  3495. }
  3496. }
  3497. static void decode_string_args(macro_iterator *mi)
  3498. {
  3499. node *n;
  3500. int c = get_copy(&n);
  3501. for (;;) {
  3502. while (c == ' ')
  3503. c = get_copy(&n);
  3504. if (c == '\n' || c == EOF) {
  3505. error("missing `]'");
  3506. break;
  3507. }
  3508. if (c == ']')
  3509. break;
  3510. macro arg;
  3511. int quote_input_level = 0;
  3512. int done_tab_warning = 0;
  3513. if (c == '"') {
  3514. quote_input_level = input_stack::get_level();
  3515. c = get_copy(&n);
  3516. }
  3517. while (c != EOF && c != '\n'
  3518. && !(c == ']' && quote_input_level == 0)
  3519. && !(c == ' ' && quote_input_level == 0)) {
  3520. if (quote_input_level > 0 && c == '"'
  3521. && input_stack::get_level() == quote_input_level) {
  3522. c = get_copy(&n);
  3523. if (c == '"') {
  3524. arg.append(c);
  3525. c = get_copy(&n);
  3526. }
  3527. else
  3528. break;
  3529. }
  3530. else {
  3531. if (c == 0)
  3532. arg.append(n);
  3533. else {
  3534. if (c == '\t' && quote_input_level == 0 && !done_tab_warning) {
  3535. warning(WARN_TAB, "tab character in unquoted string argument");
  3536. done_tab_warning = 1;
  3537. }
  3538. arg.append(c);
  3539. }
  3540. c = get_copy(&n);
  3541. }
  3542. }
  3543. mi->add_arg(arg);
  3544. }
  3545. }
  3546. void macro::invoke(symbol nm)
  3547. {
  3548. macro_iterator *mi = new macro_iterator(nm, *this);
  3549. decode_args(mi);
  3550. input_stack::push(mi);
  3551. tok.next();
  3552. }
  3553. macro *macro::to_macro()
  3554. {
  3555. return this;
  3556. }
  3557. int macro::empty()
  3558. {
  3559. return empty_macro == 1;
  3560. }
  3561. macro_iterator::macro_iterator(symbol s, macro &m, const char *how_called)
  3562. : string_iterator(m, how_called, s), args(0), argc(0)
  3563. {
  3564. }
  3565. macro_iterator::macro_iterator() : args(0), argc(0)
  3566. {
  3567. }
  3568. macro_iterator::~macro_iterator()
  3569. {
  3570. while (args != 0) {
  3571. arg_list *tem = args;
  3572. args = args->next;
  3573. delete tem;
  3574. }
  3575. }
  3576. dictionary composite_dictionary(17);
  3577. void composite_request()
  3578. {
  3579. symbol from = get_name(1);
  3580. if (!from.is_null()) {
  3581. const char *from_gn = glyph_name_to_unicode(from.contents());
  3582. if (!from_gn) {
  3583. from_gn = check_unicode_name(from.contents());
  3584. if (!from_gn) {
  3585. error("invalid composite glyph name `%1'", from.contents());
  3586. skip_line();
  3587. return;
  3588. }
  3589. }
  3590. const char *from_decomposed = decompose_unicode(from_gn);
  3591. if (from_decomposed)
  3592. from_gn = &from_decomposed[1];
  3593. symbol to = get_name(1);
  3594. if (to.is_null())
  3595. composite_dictionary.remove(symbol(from_gn));
  3596. else {
  3597. const char *to_gn = glyph_name_to_unicode(to.contents());
  3598. if (!to_gn) {
  3599. to_gn = check_unicode_name(to.contents());
  3600. if (!to_gn) {
  3601. error("invalid composite glyph name `%1'", to.contents());
  3602. skip_line();
  3603. return;
  3604. }
  3605. }
  3606. const char *to_decomposed = decompose_unicode(to_gn);
  3607. if (to_decomposed)
  3608. to_gn = &to_decomposed[1];
  3609. if (strcmp(from_gn, to_gn) == 0)
  3610. composite_dictionary.remove(symbol(from_gn));
  3611. else
  3612. (void)composite_dictionary.lookup(symbol(from_gn), (void *)to_gn);
  3613. }
  3614. }
  3615. skip_line();
  3616. }
  3617. static symbol composite_glyph_name(symbol nm)
  3618. {
  3619. macro_iterator *mi = new macro_iterator();
  3620. decode_string_args(mi);
  3621. input_stack::push(mi);
  3622. const char *gn = glyph_name_to_unicode(nm.contents());
  3623. if (!gn) {
  3624. gn = check_unicode_name(nm.contents());
  3625. if (!gn) {
  3626. error("invalid base glyph `%1' in composite glyph name", nm.contents());
  3627. return EMPTY_SYMBOL;
  3628. }
  3629. }
  3630. const char *gn_decomposed = decompose_unicode(gn);
  3631. string glyph_name(gn_decomposed ? &gn_decomposed[1] : gn);
  3632. string gl;
  3633. int n = input_stack::nargs();
  3634. for (int i = 1; i <= n; i++) {
  3635. glyph_name += '_';
  3636. input_iterator *p = input_stack::get_arg(i);
  3637. gl.clear();
  3638. int c;
  3639. while ((c = p->get(0)) != EOF)
  3640. gl += c;
  3641. gl += '\0';
  3642. const char *u = glyph_name_to_unicode(gl.contents());
  3643. if (!u) {
  3644. u = check_unicode_name(gl.contents());
  3645. if (!u) {
  3646. error("invalid component `%1' in composite glyph name",
  3647. gl.contents());
  3648. return EMPTY_SYMBOL;
  3649. }
  3650. }
  3651. const char *decomposed = decompose_unicode(u);
  3652. if (decomposed)
  3653. u = &decomposed[1];
  3654. void *mapped_composite = composite_dictionary.lookup(symbol(u));
  3655. if (mapped_composite)
  3656. u = (const char *)mapped_composite;
  3657. glyph_name += u;
  3658. }
  3659. glyph_name += '\0';
  3660. const char *groff_gn = unicode_to_glyph_name(glyph_name.contents());
  3661. if (groff_gn)
  3662. return symbol(groff_gn);
  3663. gl.clear();
  3664. gl += 'u';
  3665. gl += glyph_name;
  3666. return symbol(gl.contents());
  3667. }
  3668. int trap_sprung_flag = 0;
  3669. int postpone_traps_flag = 0;
  3670. symbol postponed_trap;
  3671. void spring_trap(symbol nm)
  3672. {
  3673. assert(!nm.is_null());
  3674. trap_sprung_flag = 1;
  3675. if (postpone_traps_flag) {
  3676. postponed_trap = nm;
  3677. return;
  3678. }
  3679. static char buf[2] = { BEGIN_TRAP, 0 };
  3680. static char buf2[2] = { END_TRAP, '\0' };
  3681. input_stack::push(make_temp_iterator(buf2));
  3682. request_or_macro *p = lookup_request(nm);
  3683. macro *m = p->to_macro();
  3684. if (m)
  3685. input_stack::push(new macro_iterator(nm, *m, "trap-invoked macro"));
  3686. else
  3687. error("you can't invoke a request with a trap");
  3688. input_stack::push(make_temp_iterator(buf));
  3689. }
  3690. void postpone_traps()
  3691. {
  3692. postpone_traps_flag = 1;
  3693. }
  3694. int unpostpone_traps()
  3695. {
  3696. postpone_traps_flag = 0;
  3697. if (!postponed_trap.is_null()) {
  3698. spring_trap(postponed_trap);
  3699. postponed_trap = NULL_SYMBOL;
  3700. return 1;
  3701. }
  3702. else
  3703. return 0;
  3704. }
  3705. void read_request()
  3706. {
  3707. macro_iterator *mi = new macro_iterator;
  3708. int reading_from_terminal = isatty(fileno(stdin));
  3709. int had_prompt = 0;
  3710. if (!tok.newline() && !tok.eof()) {
  3711. int c = get_copy(0);
  3712. while (c == ' ')
  3713. c = get_copy(0);
  3714. while (c != EOF && c != '\n' && c != ' ') {
  3715. if (!invalid_input_char(c)) {
  3716. if (reading_from_terminal)
  3717. fputc(c, stderr);
  3718. had_prompt = 1;
  3719. }
  3720. c = get_copy(0);
  3721. }
  3722. if (c == ' ') {
  3723. tok.make_space();
  3724. decode_args(mi);
  3725. }
  3726. }
  3727. if (reading_from_terminal) {
  3728. fputc(had_prompt ? ':' : '\a', stderr);
  3729. fflush(stderr);
  3730. }
  3731. input_stack::push(mi);
  3732. macro mac;
  3733. int nl = 0;
  3734. int c;
  3735. while ((c = getchar()) != EOF) {
  3736. if (invalid_input_char(c))
  3737. warning(WARN_INPUT, "invalid input character code %1", int(c));
  3738. else {
  3739. if (c == '\n') {
  3740. if (nl)
  3741. break;
  3742. else
  3743. nl = 1;
  3744. }
  3745. else
  3746. nl = 0;
  3747. mac.append(c);
  3748. }
  3749. }
  3750. if (reading_from_terminal)
  3751. clearerr(stdin);
  3752. input_stack::push(new string_iterator(mac));
  3753. tok.next();
  3754. }
  3755. enum define_mode { DEFINE_NORMAL, DEFINE_APPEND, DEFINE_IGNORE };
  3756. enum calling_mode { CALLING_NORMAL, CALLING_INDIRECT };
  3757. enum comp_mode { COMP_IGNORE, COMP_DISABLE, COMP_ENABLE };
  3758. void do_define_string(define_mode mode, comp_mode comp)
  3759. {
  3760. symbol nm;
  3761. node *n = 0; // pacify compiler
  3762. int c;
  3763. nm = get_name(1);
  3764. if (nm.is_null()) {
  3765. skip_line();
  3766. return;
  3767. }
  3768. if (tok.newline())
  3769. c = '\n';
  3770. else if (tok.tab())
  3771. c = '\t';
  3772. else if (!tok.space()) {
  3773. error("bad string definition");
  3774. skip_line();
  3775. return;
  3776. }
  3777. else
  3778. c = get_copy(&n);
  3779. while (c == ' ')
  3780. c = get_copy(&n);
  3781. if (c == '"')
  3782. c = get_copy(&n);
  3783. macro mac;
  3784. request_or_macro *rm = (request_or_macro *)request_dictionary.lookup(nm);
  3785. macro *mm = rm ? rm->to_macro() : 0;
  3786. if (mode == DEFINE_APPEND && mm)
  3787. mac = *mm;
  3788. if (comp == COMP_DISABLE)
  3789. mac.append(PUSH_GROFF_MODE);
  3790. else if (comp == COMP_ENABLE)
  3791. mac.append(PUSH_COMP_MODE);
  3792. while (c != '\n' && c != EOF) {
  3793. if (c == 0)
  3794. mac.append(n);
  3795. else
  3796. mac.append((unsigned char)c);
  3797. c = get_copy(&n);
  3798. }
  3799. if (!mm) {
  3800. mm = new macro;
  3801. request_dictionary.define(nm, mm);
  3802. }
  3803. if (comp == COMP_DISABLE || comp == COMP_ENABLE)
  3804. mac.append(POP_GROFFCOMP_MODE);
  3805. *mm = mac;
  3806. tok.next();
  3807. }
  3808. void define_string()
  3809. {
  3810. do_define_string(DEFINE_NORMAL,
  3811. compatible_flag ? COMP_ENABLE: COMP_IGNORE);
  3812. }
  3813. void define_nocomp_string()
  3814. {
  3815. do_define_string(DEFINE_NORMAL, COMP_DISABLE);
  3816. }
  3817. void append_string()
  3818. {
  3819. do_define_string(DEFINE_APPEND,
  3820. compatible_flag ? COMP_ENABLE : COMP_IGNORE);
  3821. }
  3822. void append_nocomp_string()
  3823. {
  3824. do_define_string(DEFINE_APPEND, COMP_DISABLE);
  3825. }
  3826. void do_define_character(char_mode mode, const char *font_name)
  3827. {
  3828. node *n = 0; // pacify compiler
  3829. int c;
  3830. tok.skip();
  3831. charinfo *ci = tok.get_char(1);
  3832. if (ci == 0) {
  3833. skip_line();
  3834. return;
  3835. }
  3836. if (font_name) {
  3837. string s(font_name);
  3838. s += ' ';
  3839. s += ci->nm.contents();
  3840. s += '\0';
  3841. ci = get_charinfo(symbol(s.contents()));
  3842. }
  3843. tok.next();
  3844. if (tok.newline())
  3845. c = '\n';
  3846. else if (tok.tab())
  3847. c = '\t';
  3848. else if (!tok.space()) {
  3849. error("bad character definition");
  3850. skip_line();
  3851. return;
  3852. }
  3853. else
  3854. c = get_copy(&n);
  3855. while (c == ' ' || c == '\t')
  3856. c = get_copy(&n);
  3857. if (c == '"')
  3858. c = get_copy(&n);
  3859. macro *m = new macro;
  3860. while (c != '\n' && c != EOF) {
  3861. if (c == 0)
  3862. m->append(n);
  3863. else
  3864. m->append((unsigned char)c);
  3865. c = get_copy(&n);
  3866. }
  3867. m = ci->setx_macro(m, mode);
  3868. if (m)
  3869. delete m;
  3870. tok.next();
  3871. }
  3872. void define_character()
  3873. {
  3874. do_define_character(CHAR_NORMAL);
  3875. }
  3876. void define_fallback_character()
  3877. {
  3878. do_define_character(CHAR_FALLBACK);
  3879. }
  3880. void define_special_character()
  3881. {
  3882. do_define_character(CHAR_SPECIAL);
  3883. }
  3884. static void remove_character()
  3885. {
  3886. tok.skip();
  3887. while (!tok.newline() && !tok.eof()) {
  3888. if (!tok.space() && !tok.tab()) {
  3889. charinfo *ci = tok.get_char(1);
  3890. if (!ci)
  3891. break;
  3892. macro *m = ci->set_macro(0);
  3893. if (m)
  3894. delete m;
  3895. }
  3896. tok.next();
  3897. }
  3898. skip_line();
  3899. }
  3900. static void interpolate_string(symbol nm)
  3901. {
  3902. request_or_macro *p = lookup_request(nm);
  3903. macro *m = p->to_macro();
  3904. if (!m)
  3905. error("you can only invoke a string or macro using \\*");
  3906. else {
  3907. string_iterator *si = new string_iterator(*m, "string", nm);
  3908. input_stack::push(si);
  3909. }
  3910. }
  3911. static void interpolate_string_with_args(symbol s)
  3912. {
  3913. request_or_macro *p = lookup_request(s);
  3914. macro *m = p->to_macro();
  3915. if (!m)
  3916. error("you can only invoke a string or macro using \\*");
  3917. else {
  3918. macro_iterator *mi = new macro_iterator(s, *m);
  3919. decode_string_args(mi);
  3920. input_stack::push(mi);
  3921. }
  3922. }
  3923. static void interpolate_arg(symbol nm)
  3924. {
  3925. const char *s = nm.contents();
  3926. if (!s || *s == '\0')
  3927. copy_mode_error("missing argument name");
  3928. else if (s[1] == 0 && csdigit(s[0]))
  3929. input_stack::push(input_stack::get_arg(s[0] - '0'));
  3930. else if (s[0] == '*' && s[1] == '\0') {
  3931. int limit = input_stack::nargs();
  3932. string args;
  3933. for (int i = 1; i <= limit; i++) {
  3934. input_iterator *p = input_stack::get_arg(i);
  3935. int c;
  3936. while ((c = p->get(0)) != EOF)
  3937. args += c;
  3938. if (i != limit)
  3939. args += ' ';
  3940. }
  3941. if (limit > 0) {
  3942. args += '\0';
  3943. input_stack::push(make_temp_iterator(args.contents()));
  3944. }
  3945. }
  3946. else if (s[0] == '@' && s[1] == '\0') {
  3947. int limit = input_stack::nargs();
  3948. string args;
  3949. for (int i = 1; i <= limit; i++) {
  3950. args += '"';
  3951. args += BEGIN_QUOTE;
  3952. input_iterator *p = input_stack::get_arg(i);
  3953. int c;
  3954. while ((c = p->get(0)) != EOF)
  3955. args += c;
  3956. args += END_QUOTE;
  3957. args += '"';
  3958. if (i != limit)
  3959. args += ' ';
  3960. }
  3961. if (limit > 0) {
  3962. args += '\0';
  3963. input_stack::push(make_temp_iterator(args.contents()));
  3964. }
  3965. }
  3966. else {
  3967. const char *p;
  3968. for (p = s; *p && csdigit(*p); p++)
  3969. ;
  3970. if (*p)
  3971. copy_mode_error("bad argument name `%1'", s);
  3972. else
  3973. input_stack::push(input_stack::get_arg(atoi(s)));
  3974. }
  3975. }
  3976. void handle_first_page_transition()
  3977. {
  3978. push_token(tok);
  3979. topdiv->begin_page();
  3980. }
  3981. // We push back a token by wrapping it up in a token_node, and
  3982. // wrapping that up in a string_iterator.
  3983. static void push_token(const token &t)
  3984. {
  3985. macro m;
  3986. m.append(new token_node(t));
  3987. input_stack::push(new string_iterator(m));
  3988. }
  3989. void push_page_ejector()
  3990. {
  3991. static char buf[2] = { PAGE_EJECTOR, '\0' };
  3992. input_stack::push(make_temp_iterator(buf));
  3993. }
  3994. void handle_initial_request(unsigned char code)
  3995. {
  3996. char buf[2];
  3997. buf[0] = code;
  3998. buf[1] = '\0';
  3999. macro mac;
  4000. mac.append(new token_node(tok));
  4001. input_stack::push(new string_iterator(mac));
  4002. input_stack::push(make_temp_iterator(buf));
  4003. topdiv->begin_page();
  4004. tok.next();
  4005. }
  4006. void handle_initial_title()
  4007. {
  4008. handle_initial_request(TITLE_REQUEST);
  4009. }
  4010. // this should be local to define_macro, but cfront 1.2 doesn't support that
  4011. static symbol dot_symbol(".");
  4012. void do_define_macro(define_mode mode, calling_mode calling, comp_mode comp)
  4013. {
  4014. symbol nm, term;
  4015. if (calling == CALLING_INDIRECT) {
  4016. symbol temp1 = get_name(1);
  4017. if (temp1.is_null()) {
  4018. skip_line();
  4019. return;
  4020. }
  4021. symbol temp2 = get_name();
  4022. input_stack::push(make_temp_iterator("\n"));
  4023. if (!temp2.is_null()) {
  4024. interpolate_string(temp2);
  4025. input_stack::push(make_temp_iterator(" "));
  4026. }
  4027. interpolate_string(temp1);
  4028. input_stack::push(make_temp_iterator(" "));
  4029. tok.next();
  4030. }
  4031. if (mode == DEFINE_NORMAL || mode == DEFINE_APPEND) {
  4032. nm = get_name(1);
  4033. if (nm.is_null()) {
  4034. skip_line();
  4035. return;
  4036. }
  4037. }
  4038. term = get_name(); // the request that terminates the definition
  4039. if (term.is_null())
  4040. term = dot_symbol;
  4041. while (!tok.newline() && !tok.eof())
  4042. tok.next();
  4043. const char *start_filename;
  4044. int start_lineno;
  4045. int have_start_location = input_stack::get_location(0, &start_filename,
  4046. &start_lineno);
  4047. node *n;
  4048. // doing this here makes the line numbers come out right
  4049. int c = get_copy(&n, 1);
  4050. macro mac;
  4051. macro *mm = 0;
  4052. if (mode == DEFINE_NORMAL || mode == DEFINE_APPEND) {
  4053. request_or_macro *rm =
  4054. (request_or_macro *)request_dictionary.lookup(nm);
  4055. if (rm)
  4056. mm = rm->to_macro();
  4057. if (mm && mode == DEFINE_APPEND)
  4058. mac = *mm;
  4059. }
  4060. int bol = 1;
  4061. if (comp == COMP_DISABLE)
  4062. mac.append(PUSH_GROFF_MODE);
  4063. else if (comp == COMP_ENABLE)
  4064. mac.append(PUSH_COMP_MODE);
  4065. for (;;) {
  4066. while (c == ESCAPE_NEWLINE) {
  4067. if (mode == DEFINE_NORMAL || mode == DEFINE_APPEND)
  4068. mac.append(c);
  4069. c = get_copy(&n, 1);
  4070. }
  4071. if (bol && c == '.') {
  4072. const char *s = term.contents();
  4073. int d = 0;
  4074. // see if it matches term
  4075. int i = 0;
  4076. if (s[0] != 0) {
  4077. while ((d = get_copy(&n)) == ' ' || d == '\t')
  4078. ;
  4079. if ((unsigned char)s[0] == d) {
  4080. for (i = 1; s[i] != 0; i++) {
  4081. d = get_copy(&n);
  4082. if ((unsigned char)s[i] != d)
  4083. break;
  4084. }
  4085. }
  4086. }
  4087. if (s[i] == 0
  4088. && ((i == 2 && compatible_flag)
  4089. || (d = get_copy(&n)) == ' '
  4090. || d == '\n')) { // we found it
  4091. if (d == '\n')
  4092. tok.make_newline();
  4093. else
  4094. tok.make_space();
  4095. if (mode == DEFINE_APPEND || mode == DEFINE_NORMAL) {
  4096. if (!mm) {
  4097. mm = new macro;
  4098. request_dictionary.define(nm, mm);
  4099. }
  4100. if (comp == COMP_DISABLE || comp == COMP_ENABLE)
  4101. mac.append(POP_GROFFCOMP_MODE);
  4102. *mm = mac;
  4103. }
  4104. if (term != dot_symbol) {
  4105. ignoring = 0;
  4106. interpolate_macro(term);
  4107. }
  4108. else
  4109. skip_line();
  4110. return;
  4111. }
  4112. if (mode == DEFINE_APPEND || mode == DEFINE_NORMAL) {
  4113. mac.append(c);
  4114. for (int j = 0; j < i; j++)
  4115. mac.append(s[j]);
  4116. }
  4117. c = d;
  4118. }
  4119. if (c == EOF) {
  4120. if (mode == DEFINE_NORMAL || mode == DEFINE_APPEND) {
  4121. if (have_start_location)
  4122. error_with_file_and_line(start_filename, start_lineno,
  4123. "end of file while defining macro `%1'",
  4124. nm.contents());
  4125. else
  4126. error("end of file while defining macro `%1'", nm.contents());
  4127. }
  4128. else {
  4129. if (have_start_location)
  4130. error_with_file_and_line(start_filename, start_lineno,
  4131. "end of file while ignoring input lines");
  4132. else
  4133. error("end of file while ignoring input lines");
  4134. }
  4135. tok.next();
  4136. return;
  4137. }
  4138. if (mode == DEFINE_NORMAL || mode == DEFINE_APPEND) {
  4139. if (c == 0)
  4140. mac.append(n);
  4141. else
  4142. mac.append(c);
  4143. }
  4144. bol = (c == '\n');
  4145. c = get_copy(&n, 1);
  4146. }
  4147. }
  4148. void define_macro()
  4149. {
  4150. do_define_macro(DEFINE_NORMAL, CALLING_NORMAL,
  4151. compatible_flag ? COMP_ENABLE : COMP_IGNORE);
  4152. }
  4153. void define_nocomp_macro()
  4154. {
  4155. do_define_macro(DEFINE_NORMAL, CALLING_NORMAL, COMP_DISABLE);
  4156. }
  4157. void define_indirect_macro()
  4158. {
  4159. do_define_macro(DEFINE_NORMAL, CALLING_INDIRECT,
  4160. compatible_flag ? COMP_ENABLE : COMP_IGNORE);
  4161. }
  4162. void define_indirect_nocomp_macro()
  4163. {
  4164. do_define_macro(DEFINE_NORMAL, CALLING_INDIRECT, COMP_DISABLE);
  4165. }
  4166. void append_macro()
  4167. {
  4168. do_define_macro(DEFINE_APPEND, CALLING_NORMAL,
  4169. compatible_flag ? COMP_ENABLE : COMP_IGNORE);
  4170. }
  4171. void append_nocomp_macro()
  4172. {
  4173. do_define_macro(DEFINE_APPEND, CALLING_NORMAL, COMP_DISABLE);
  4174. }
  4175. void append_indirect_macro()
  4176. {
  4177. do_define_macro(DEFINE_APPEND, CALLING_INDIRECT,
  4178. compatible_flag ? COMP_ENABLE : COMP_IGNORE);
  4179. }
  4180. void append_indirect_nocomp_macro()
  4181. {
  4182. do_define_macro(DEFINE_APPEND, CALLING_INDIRECT, COMP_DISABLE);
  4183. }
  4184. void ignore()
  4185. {
  4186. ignoring = 1;
  4187. do_define_macro(DEFINE_IGNORE, CALLING_NORMAL, COMP_IGNORE);
  4188. ignoring = 0;
  4189. }
  4190. void remove_macro()
  4191. {
  4192. for (;;) {
  4193. symbol s = get_name();
  4194. if (s.is_null())
  4195. break;
  4196. request_dictionary.remove(s);
  4197. }
  4198. skip_line();
  4199. }
  4200. void rename_macro()
  4201. {
  4202. symbol s1 = get_name(1);
  4203. if (!s1.is_null()) {
  4204. symbol s2 = get_name(1);
  4205. if (!s2.is_null())
  4206. request_dictionary.rename(s1, s2);
  4207. }
  4208. skip_line();
  4209. }
  4210. void alias_macro()
  4211. {
  4212. symbol s1 = get_name(1);
  4213. if (!s1.is_null()) {
  4214. symbol s2 = get_name(1);
  4215. if (!s2.is_null()) {
  4216. if (!request_dictionary.alias(s1, s2))
  4217. warning(WARN_MAC, "macro `%1' not defined", s2.contents());
  4218. }
  4219. }
  4220. skip_line();
  4221. }
  4222. void chop_macro()
  4223. {
  4224. symbol s = get_name(1);
  4225. if (!s.is_null()) {
  4226. request_or_macro *p = lookup_request(s);
  4227. macro *m = p->to_macro();
  4228. if (!m)
  4229. error("cannot chop request");
  4230. else if (m->empty())
  4231. error("cannot chop empty macro");
  4232. else {
  4233. int have_restore = 0;
  4234. // we have to check for additional save/restore pairs which could be
  4235. // there due to empty am1 requests.
  4236. for (;;) {
  4237. if (m->get(m->len - 1) != POP_GROFFCOMP_MODE)
  4238. break;
  4239. have_restore = 1;
  4240. m->len -= 1;
  4241. if (m->get(m->len - 1) != PUSH_GROFF_MODE
  4242. && m->get(m->len - 1) != PUSH_COMP_MODE)
  4243. break;
  4244. have_restore = 0;
  4245. m->len -= 1;
  4246. if (m->len == 0)
  4247. break;
  4248. }
  4249. if (m->len == 0)
  4250. error("cannot chop empty macro");
  4251. else {
  4252. if (have_restore)
  4253. m->set(POP_GROFFCOMP_MODE, m->len - 1);
  4254. else
  4255. m->len -= 1;
  4256. }
  4257. }
  4258. }
  4259. skip_line();
  4260. }
  4261. void substring_request()
  4262. {
  4263. int start; // 0, 1, ..., n-1 or -1, -2, ...
  4264. symbol s = get_name(1);
  4265. if (!s.is_null() && get_integer(&start)) {
  4266. request_or_macro *p = lookup_request(s);
  4267. macro *m = p->to_macro();
  4268. if (!m)
  4269. error("cannot apply `substring' on a request");
  4270. else {
  4271. int end = -1;
  4272. if (!has_arg() || get_integer(&end)) {
  4273. int real_length = 0; // 1, 2, ..., n
  4274. string_iterator iter1(*m);
  4275. for (int l = 0; l < m->len; l++) {
  4276. int c = iter1.get(0);
  4277. if (c == PUSH_GROFF_MODE
  4278. || c == PUSH_COMP_MODE
  4279. || c == POP_GROFFCOMP_MODE)
  4280. continue;
  4281. if (c == EOF)
  4282. break;
  4283. real_length++;
  4284. }
  4285. if (start < 0)
  4286. start += real_length;
  4287. if (end < 0)
  4288. end += real_length;
  4289. if (start > end) {
  4290. int tem = start;
  4291. start = end;
  4292. end = tem;
  4293. }
  4294. if (start >= real_length || end < 0) {
  4295. warning(WARN_RANGE,
  4296. "start and end index of substring out of range");
  4297. m->len = 0;
  4298. if (m->p) {
  4299. if (--(m->p->count) <= 0)
  4300. delete m->p;
  4301. m->p = 0;
  4302. }
  4303. skip_line();
  4304. return;
  4305. }
  4306. if (start < 0) {
  4307. warning(WARN_RANGE,
  4308. "start index of substring out of range, set to 0");
  4309. start = 0;
  4310. }
  4311. if (end >= real_length) {
  4312. warning(WARN_RANGE,
  4313. "end index of substring out of range, set to string length");
  4314. end = real_length - 1;
  4315. }
  4316. // now extract the substring
  4317. string_iterator iter(*m);
  4318. int i;
  4319. for (i = 0; i < start; i++) {
  4320. int c = iter.get(0);
  4321. while (c == PUSH_GROFF_MODE
  4322. || c == PUSH_COMP_MODE
  4323. || c == POP_GROFFCOMP_MODE)
  4324. c = iter.get(0);
  4325. if (c == EOF)
  4326. break;
  4327. }
  4328. macro mac;
  4329. for (; i <= end; i++) {
  4330. node *nd = 0; // pacify compiler
  4331. int c = iter.get(&nd);
  4332. while (c == PUSH_GROFF_MODE
  4333. || c == PUSH_COMP_MODE
  4334. || c == POP_GROFFCOMP_MODE)
  4335. c = iter.get(0);
  4336. if (c == EOF)
  4337. break;
  4338. if (c == 0)
  4339. mac.append(nd);
  4340. else
  4341. mac.append((unsigned char)c);
  4342. }
  4343. *m = mac;
  4344. }
  4345. }
  4346. }
  4347. skip_line();
  4348. }
  4349. void length_request()
  4350. {
  4351. symbol ret;
  4352. ret = get_name(1);
  4353. if (ret.is_null()) {
  4354. skip_line();
  4355. return;
  4356. }
  4357. int c;
  4358. node *n;
  4359. if (tok.newline())
  4360. c = '\n';
  4361. else if (tok.tab())
  4362. c = '\t';
  4363. else if (!tok.space()) {
  4364. error("bad string definition");
  4365. skip_line();
  4366. return;
  4367. }
  4368. else
  4369. c = get_copy(&n);
  4370. while (c == ' ')
  4371. c = get_copy(&n);
  4372. if (c == '"')
  4373. c = get_copy(&n);
  4374. int len = 0;
  4375. while (c != '\n' && c != EOF) {
  4376. ++len;
  4377. c = get_copy(&n);
  4378. }
  4379. reg *r = (reg*)number_reg_dictionary.lookup(ret);
  4380. if (r)
  4381. r->set_value(len);
  4382. else
  4383. set_number_reg(ret, len);
  4384. tok.next();
  4385. }
  4386. void asciify_macro()
  4387. {
  4388. symbol s = get_name(1);
  4389. if (!s.is_null()) {
  4390. request_or_macro *p = lookup_request(s);
  4391. macro *m = p->to_macro();
  4392. if (!m)
  4393. error("cannot asciify request");
  4394. else {
  4395. macro am;
  4396. string_iterator iter(*m);
  4397. for (;;) {
  4398. node *nd = 0; // pacify compiler
  4399. int c = iter.get(&nd);
  4400. if (c == EOF)
  4401. break;
  4402. if (c != 0)
  4403. am.append(c);
  4404. else
  4405. nd->asciify(&am);
  4406. }
  4407. *m = am;
  4408. }
  4409. }
  4410. skip_line();
  4411. }
  4412. void unformat_macro()
  4413. {
  4414. symbol s = get_name(1);
  4415. if (!s.is_null()) {
  4416. request_or_macro *p = lookup_request(s);
  4417. macro *m = p->to_macro();
  4418. if (!m)
  4419. error("cannot unformat request");
  4420. else {
  4421. macro am;
  4422. string_iterator iter(*m);
  4423. for (;;) {
  4424. node *nd = 0; // pacify compiler
  4425. int c = iter.get(&nd);
  4426. if (c == EOF)
  4427. break;
  4428. if (c != 0)
  4429. am.append(c);
  4430. else {
  4431. if (nd->set_unformat_flag())
  4432. am.append(nd);
  4433. }
  4434. }
  4435. *m = am;
  4436. }
  4437. }
  4438. skip_line();
  4439. }
  4440. static void interpolate_environment_variable(symbol nm)
  4441. {
  4442. const char *s = getenv(nm.contents());
  4443. if (s && *s)
  4444. input_stack::push(make_temp_iterator(s));
  4445. }
  4446. void interpolate_number_reg(symbol nm, int inc)
  4447. {
  4448. reg *r = lookup_number_reg(nm);
  4449. if (inc < 0)
  4450. r->decrement();
  4451. else if (inc > 0)
  4452. r->increment();
  4453. input_stack::push(make_temp_iterator(r->get_string()));
  4454. }
  4455. static void interpolate_number_format(symbol nm)
  4456. {
  4457. reg *r = (reg *)number_reg_dictionary.lookup(nm);
  4458. if (r)
  4459. input_stack::push(make_temp_iterator(r->get_format()));
  4460. }
  4461. static int get_delim_number(units *n, unsigned char si, int prev_value)
  4462. {
  4463. token start;
  4464. start.next();
  4465. if (start.delimiter(1)) {
  4466. tok.next();
  4467. if (get_number(n, si, prev_value)) {
  4468. if (start != tok)
  4469. warning(WARN_DELIM, "closing delimiter does not match");
  4470. return 1;
  4471. }
  4472. }
  4473. return 0;
  4474. }
  4475. static int get_delim_number(units *n, unsigned char si)
  4476. {
  4477. token start;
  4478. start.next();
  4479. if (start.delimiter(1)) {
  4480. tok.next();
  4481. if (get_number(n, si)) {
  4482. if (start != tok)
  4483. warning(WARN_DELIM, "closing delimiter does not match");
  4484. return 1;
  4485. }
  4486. }
  4487. return 0;
  4488. }
  4489. static int get_line_arg(units *n, unsigned char si, charinfo **cp)
  4490. {
  4491. token start;
  4492. start.next();
  4493. int start_level = input_stack::get_level();
  4494. if (!start.delimiter(1))
  4495. return 0;
  4496. tok.next();
  4497. if (get_number(n, si)) {
  4498. if (tok.dummy() || tok.transparent_dummy())
  4499. tok.next();
  4500. if (!(start == tok && input_stack::get_level() == start_level)) {
  4501. *cp = tok.get_char(1);
  4502. tok.next();
  4503. }
  4504. if (!(start == tok && input_stack::get_level() == start_level))
  4505. warning(WARN_DELIM, "closing delimiter does not match");
  4506. return 1;
  4507. }
  4508. return 0;
  4509. }
  4510. static int read_size(int *x)
  4511. {
  4512. tok.next();
  4513. int c = tok.ch();
  4514. int inc = 0;
  4515. if (c == '-') {
  4516. inc = -1;
  4517. tok.next();
  4518. c = tok.ch();
  4519. }
  4520. else if (c == '+') {
  4521. inc = 1;
  4522. tok.next();
  4523. c = tok.ch();
  4524. }
  4525. int val = 0; // pacify compiler
  4526. int bad = 0;
  4527. if (c == '(') {
  4528. tok.next();
  4529. c = tok.ch();
  4530. if (!inc) {
  4531. // allow an increment either before or after the left parenthesis
  4532. if (c == '-') {
  4533. inc = -1;
  4534. tok.next();
  4535. c = tok.ch();
  4536. }
  4537. else if (c == '+') {
  4538. inc = 1;
  4539. tok.next();
  4540. c = tok.ch();
  4541. }
  4542. }
  4543. if (!csdigit(c))
  4544. bad = 1;
  4545. else {
  4546. val = c - '0';
  4547. tok.next();
  4548. c = tok.ch();
  4549. if (!csdigit(c))
  4550. bad = 1;
  4551. else {
  4552. val = val*10 + (c - '0');
  4553. val *= sizescale;
  4554. }
  4555. }
  4556. }
  4557. else if (csdigit(c)) {
  4558. val = c - '0';
  4559. if (!inc && c != '0' && c < '4') {
  4560. tok.next();
  4561. c = tok.ch();
  4562. if (!csdigit(c))
  4563. bad = 1;
  4564. else
  4565. val = val*10 + (c - '0');
  4566. }
  4567. val *= sizescale;
  4568. }
  4569. else if (!tok.delimiter(1))
  4570. return 0;
  4571. else {
  4572. token start(tok);
  4573. tok.next();
  4574. if (!(inc
  4575. ? get_number(&val, 'z')
  4576. : get_number(&val, 'z', curenv->get_requested_point_size())))
  4577. return 0;
  4578. if (!(start.ch() == '[' && tok.ch() == ']') && start != tok) {
  4579. if (start.ch() == '[')
  4580. error("missing `]'");
  4581. else
  4582. error("missing closing delimiter");
  4583. return 0;
  4584. }
  4585. }
  4586. if (!bad) {
  4587. switch (inc) {
  4588. case 0:
  4589. if (val == 0) {
  4590. // special case -- \s[0] and \s0 means to revert to previous size
  4591. *x = 0;
  4592. return 1;
  4593. }
  4594. *x = val;
  4595. break;
  4596. case 1:
  4597. *x = curenv->get_requested_point_size() + val;
  4598. break;
  4599. case -1:
  4600. *x = curenv->get_requested_point_size() - val;
  4601. break;
  4602. default:
  4603. assert(0);
  4604. }
  4605. if (*x <= 0) {
  4606. warning(WARN_RANGE,
  4607. "\\s request results in non-positive point size; set to 1");
  4608. *x = 1;
  4609. }
  4610. return 1;
  4611. }
  4612. else {
  4613. error("bad digit in point size");
  4614. return 0;
  4615. }
  4616. }
  4617. static symbol get_delim_name()
  4618. {
  4619. token start;
  4620. start.next();
  4621. if (start.eof()) {
  4622. error("end of input at start of delimited name");
  4623. return NULL_SYMBOL;
  4624. }
  4625. if (start.newline()) {
  4626. error("can't delimit name with a newline");
  4627. return NULL_SYMBOL;
  4628. }
  4629. int start_level = input_stack::get_level();
  4630. char abuf[ABUF_SIZE];
  4631. char *buf = abuf;
  4632. int buf_size = ABUF_SIZE;
  4633. int i = 0;
  4634. for (;;) {
  4635. if (i + 1 > buf_size) {
  4636. if (buf == abuf) {
  4637. buf = new char[ABUF_SIZE*2];
  4638. memcpy(buf, abuf, buf_size);
  4639. buf_size = ABUF_SIZE*2;
  4640. }
  4641. else {
  4642. char *old_buf = buf;
  4643. buf = new char[buf_size*2];
  4644. memcpy(buf, old_buf, buf_size);
  4645. buf_size *= 2;
  4646. a_delete old_buf;
  4647. }
  4648. }
  4649. tok.next();
  4650. if (tok == start
  4651. && (compatible_flag || input_stack::get_level() == start_level))
  4652. break;
  4653. if ((buf[i] = tok.ch()) == 0) {
  4654. error("missing delimiter (got %1)", tok.description());
  4655. if (buf != abuf)
  4656. a_delete buf;
  4657. return NULL_SYMBOL;
  4658. }
  4659. i++;
  4660. }
  4661. buf[i] = '\0';
  4662. if (buf == abuf) {
  4663. if (i == 0) {
  4664. error("empty delimited name");
  4665. return NULL_SYMBOL;
  4666. }
  4667. else
  4668. return symbol(buf);
  4669. }
  4670. else {
  4671. symbol s(buf);
  4672. a_delete buf;
  4673. return s;
  4674. }
  4675. }
  4676. // Implement \R
  4677. static void do_register()
  4678. {
  4679. token start;
  4680. start.next();
  4681. if (!start.delimiter(1))
  4682. return;
  4683. tok.next();
  4684. symbol nm = get_long_name(1);
  4685. if (nm.is_null())
  4686. return;
  4687. while (tok.space())
  4688. tok.next();
  4689. reg *r = (reg *)number_reg_dictionary.lookup(nm);
  4690. int prev_value;
  4691. if (!r || !r->get_value(&prev_value))
  4692. prev_value = 0;
  4693. int val;
  4694. if (!get_number(&val, 'u', prev_value))
  4695. return;
  4696. if (start != tok)
  4697. warning(WARN_DELIM, "closing delimiter does not match");
  4698. if (r)
  4699. r->set_value(val);
  4700. else
  4701. set_number_reg(nm, val);
  4702. }
  4703. // this implements the \w escape sequence
  4704. static void do_width()
  4705. {
  4706. token start;
  4707. start.next();
  4708. int start_level = input_stack::get_level();
  4709. environment env(curenv);
  4710. environment *oldenv = curenv;
  4711. curenv = &env;
  4712. for (;;) {
  4713. tok.next();
  4714. if (tok.eof()) {
  4715. warning(WARN_DELIM, "missing closing delimiter");
  4716. break;
  4717. }
  4718. if (tok.newline()) {
  4719. warning(WARN_DELIM, "missing closing delimiter");
  4720. input_stack::push(make_temp_iterator("\n"));
  4721. break;
  4722. }
  4723. if (tok == start
  4724. && (compatible_flag || input_stack::get_level() == start_level))
  4725. break;
  4726. tok.process();
  4727. }
  4728. env.wrap_up_tab();
  4729. units x = env.get_input_line_position().to_units();
  4730. input_stack::push(make_temp_iterator(i_to_a(x)));
  4731. env.width_registers();
  4732. curenv = oldenv;
  4733. have_input = 0;
  4734. }
  4735. charinfo *page_character;
  4736. void set_page_character()
  4737. {
  4738. page_character = get_optional_char();
  4739. skip_line();
  4740. }
  4741. static const symbol percent_symbol("%");
  4742. void read_title_parts(node **part, hunits *part_width)
  4743. {
  4744. tok.skip();
  4745. if (tok.newline() || tok.eof())
  4746. return;
  4747. token start(tok);
  4748. int start_level = input_stack::get_level();
  4749. tok.next();
  4750. for (int i = 0; i < 3; i++) {
  4751. while (!tok.newline() && !tok.eof()) {
  4752. if (tok == start
  4753. && (compatible_flag || input_stack::get_level() == start_level)) {
  4754. tok.next();
  4755. break;
  4756. }
  4757. if (page_character != 0 && tok.get_char() == page_character)
  4758. interpolate_number_reg(percent_symbol, 0);
  4759. else
  4760. tok.process();
  4761. tok.next();
  4762. }
  4763. curenv->wrap_up_tab();
  4764. part_width[i] = curenv->get_input_line_position();
  4765. part[i] = curenv->extract_output_line();
  4766. }
  4767. while (!tok.newline() && !tok.eof())
  4768. tok.next();
  4769. }
  4770. class non_interpreted_node : public node {
  4771. macro mac;
  4772. public:
  4773. non_interpreted_node(const macro &);
  4774. int interpret(macro *);
  4775. node *copy();
  4776. int ends_sentence();
  4777. int same(node *);
  4778. const char *type();
  4779. int force_tprint();
  4780. int is_tag();
  4781. };
  4782. non_interpreted_node::non_interpreted_node(const macro &m) : mac(m)
  4783. {
  4784. }
  4785. int non_interpreted_node::ends_sentence()
  4786. {
  4787. return 2;
  4788. }
  4789. int non_interpreted_node::same(node *nd)
  4790. {
  4791. return mac == ((non_interpreted_node *)nd)->mac;
  4792. }
  4793. const char *non_interpreted_node::type()
  4794. {
  4795. return "non_interpreted_node";
  4796. }
  4797. int non_interpreted_node::force_tprint()
  4798. {
  4799. return 0;
  4800. }
  4801. int non_interpreted_node::is_tag()
  4802. {
  4803. return 0;
  4804. }
  4805. node *non_interpreted_node::copy()
  4806. {
  4807. return new non_interpreted_node(mac);
  4808. }
  4809. int non_interpreted_node::interpret(macro *m)
  4810. {
  4811. string_iterator si(mac);
  4812. node *n = 0; // pacify compiler
  4813. for (;;) {
  4814. int c = si.get(&n);
  4815. if (c == EOF)
  4816. break;
  4817. if (c == 0)
  4818. m->append(n);
  4819. else
  4820. m->append(c);
  4821. }
  4822. return 1;
  4823. }
  4824. static node *do_non_interpreted()
  4825. {
  4826. node *n;
  4827. int c;
  4828. macro mac;
  4829. while ((c = get_copy(&n)) != ESCAPE_QUESTION && c != EOF && c != '\n')
  4830. if (c == 0)
  4831. mac.append(n);
  4832. else
  4833. mac.append(c);
  4834. if (c == EOF || c == '\n') {
  4835. error("missing \\?");
  4836. return 0;
  4837. }
  4838. return new non_interpreted_node(mac);
  4839. }
  4840. static void encode_char(macro *mac, char c)
  4841. {
  4842. if (c == '\0') {
  4843. if ((font::use_charnames_in_special) && tok.special()) {
  4844. charinfo *ci = tok.get_char(1);
  4845. const char *s = ci->get_symbol()->contents();
  4846. if (s[0] != (char)0) {
  4847. mac->append('\\');
  4848. mac->append('(');
  4849. int i = 0;
  4850. while (s[i] != (char)0) {
  4851. mac->append(s[i]);
  4852. i++;
  4853. }
  4854. mac->append('\\');
  4855. mac->append(')');
  4856. }
  4857. }
  4858. else if (tok.stretchable_space()
  4859. || tok.unstretchable_space())
  4860. mac->append(' ');
  4861. else if (!(tok.hyphen_indicator()
  4862. || tok.dummy()
  4863. || tok.transparent_dummy()
  4864. || tok.zero_width_break()))
  4865. error("%1 is invalid within \\X", tok.description());
  4866. }
  4867. else {
  4868. if ((font::use_charnames_in_special) && (c == '\\')) {
  4869. /*
  4870. * add escape escape sequence
  4871. */
  4872. mac->append(c);
  4873. }
  4874. mac->append(c);
  4875. }
  4876. }
  4877. node *do_special()
  4878. {
  4879. token start;
  4880. start.next();
  4881. int start_level = input_stack::get_level();
  4882. macro mac;
  4883. for (tok.next();
  4884. tok != start || input_stack::get_level() != start_level;
  4885. tok.next()) {
  4886. if (tok.eof()) {
  4887. warning(WARN_DELIM, "missing closing delimiter");
  4888. return 0;
  4889. }
  4890. if (tok.newline()) {
  4891. input_stack::push(make_temp_iterator("\n"));
  4892. warning(WARN_DELIM, "missing closing delimiter");
  4893. break;
  4894. }
  4895. unsigned char c;
  4896. if (tok.space())
  4897. c = ' ';
  4898. else if (tok.tab())
  4899. c = '\t';
  4900. else if (tok.leader())
  4901. c = '\001';
  4902. else if (tok.backspace())
  4903. c = '\b';
  4904. else
  4905. c = tok.ch();
  4906. encode_char(&mac, c);
  4907. }
  4908. return new special_node(mac);
  4909. }
  4910. void output_request()
  4911. {
  4912. if (!tok.newline() && !tok.eof()) {
  4913. int c;
  4914. for (;;) {
  4915. c = get_copy(0);
  4916. if (c == '"') {
  4917. c = get_copy(0);
  4918. break;
  4919. }
  4920. if (c != ' ' && c != '\t')
  4921. break;
  4922. }
  4923. for (; c != '\n' && c != EOF; c = get_copy(0))
  4924. topdiv->transparent_output(c);
  4925. topdiv->transparent_output('\n');
  4926. }
  4927. tok.next();
  4928. }
  4929. extern int image_no; // from node.cpp
  4930. static node *do_suppress(symbol nm)
  4931. {
  4932. if (nm.is_null() || nm.is_empty()) {
  4933. error("expecting an argument to escape \\O");
  4934. return 0;
  4935. }
  4936. const char *s = nm.contents();
  4937. switch (*s) {
  4938. case '0':
  4939. if (begin_level == 0)
  4940. // suppress generation of glyphs
  4941. return new suppress_node(0, 0);
  4942. break;
  4943. case '1':
  4944. if (begin_level == 0)
  4945. // enable generation of glyphs
  4946. return new suppress_node(1, 0);
  4947. break;
  4948. case '2':
  4949. if (begin_level == 0)
  4950. return new suppress_node(1, 1);
  4951. break;
  4952. case '3':
  4953. begin_level++;
  4954. break;
  4955. case '4':
  4956. begin_level--;
  4957. break;
  4958. case '5':
  4959. {
  4960. s++; // move over '5'
  4961. char position = *s;
  4962. if (*s == (char)0) {
  4963. error("missing position and filename in \\O");
  4964. return 0;
  4965. }
  4966. if (!(position == 'l'
  4967. || position == 'r'
  4968. || position == 'c'
  4969. || position == 'i')) {
  4970. error("l, r, c, or i position expected (got %1 in \\O)", position);
  4971. return 0;
  4972. }
  4973. s++; // onto image name
  4974. if (s == (char *)0) {
  4975. error("missing image name for \\O");
  4976. return 0;
  4977. }
  4978. image_no++;
  4979. if (begin_level == 0)
  4980. return new suppress_node(symbol(s), position, image_no);
  4981. }
  4982. break;
  4983. default:
  4984. error("`%1' is an invalid argument to \\O", *s);
  4985. }
  4986. return 0;
  4987. }
  4988. void special_node::tprint(troff_output_file *out)
  4989. {
  4990. tprint_start(out);
  4991. string_iterator iter(mac);
  4992. for (;;) {
  4993. int c = iter.get(0);
  4994. if (c == EOF)
  4995. break;
  4996. for (const char *s = ::asciify(c); *s; s++)
  4997. tprint_char(out, *s);
  4998. }
  4999. tprint_end(out);
  5000. }
  5001. int get_file_line(const char **filename, int *lineno)
  5002. {
  5003. return input_stack::get_location(0, filename, lineno);
  5004. }
  5005. void line_file()
  5006. {
  5007. int n;
  5008. if (get_integer(&n)) {
  5009. const char *filename = 0;
  5010. if (has_arg()) {
  5011. symbol s = get_long_name();
  5012. filename = s.contents();
  5013. }
  5014. (void)input_stack::set_location(filename, n-1);
  5015. }
  5016. skip_line();
  5017. }
  5018. static int nroff_mode = 0;
  5019. static void nroff_request()
  5020. {
  5021. nroff_mode = 1;
  5022. skip_line();
  5023. }
  5024. static void troff_request()
  5025. {
  5026. nroff_mode = 0;
  5027. skip_line();
  5028. }
  5029. static void skip_alternative()
  5030. {
  5031. int level = 0;
  5032. // ensure that ``.if 0\{'' works as expected
  5033. if (tok.left_brace())
  5034. level++;
  5035. int c;
  5036. for (;;) {
  5037. c = input_stack::get(0);
  5038. if (c == EOF)
  5039. break;
  5040. if (c == ESCAPE_LEFT_BRACE)
  5041. ++level;
  5042. else if (c == ESCAPE_RIGHT_BRACE)
  5043. --level;
  5044. else if (c == escape_char && escape_char > 0)
  5045. switch(input_stack::get(0)) {
  5046. case '{':
  5047. ++level;
  5048. break;
  5049. case '}':
  5050. --level;
  5051. break;
  5052. case '"':
  5053. while ((c = input_stack::get(0)) != '\n' && c != EOF)
  5054. ;
  5055. }
  5056. /*
  5057. Note that the level can properly be < 0, eg
  5058. .if 1 \{\
  5059. .if 0 \{\
  5060. .\}\}
  5061. So don't give an error message in this case.
  5062. */
  5063. if (level <= 0 && c == '\n')
  5064. break;
  5065. }
  5066. tok.next();
  5067. }
  5068. static void begin_alternative()
  5069. {
  5070. while (tok.space() || tok.left_brace())
  5071. tok.next();
  5072. }
  5073. void nop_request()
  5074. {
  5075. while (tok.space())
  5076. tok.next();
  5077. }
  5078. static int_stack if_else_stack;
  5079. int do_if_request()
  5080. {
  5081. int invert = 0;
  5082. while (tok.space())
  5083. tok.next();
  5084. while (tok.ch() == '!') {
  5085. tok.next();
  5086. invert = !invert;
  5087. }
  5088. int result;
  5089. unsigned char c = tok.ch();
  5090. if (c == 't') {
  5091. tok.next();
  5092. result = !nroff_mode;
  5093. }
  5094. else if (c == 'n') {
  5095. tok.next();
  5096. result = nroff_mode;
  5097. }
  5098. else if (c == 'v') {
  5099. tok.next();
  5100. result = 0;
  5101. }
  5102. else if (c == 'o') {
  5103. result = (topdiv->get_page_number() & 1);
  5104. tok.next();
  5105. }
  5106. else if (c == 'e') {
  5107. result = !(topdiv->get_page_number() & 1);
  5108. tok.next();
  5109. }
  5110. else if (c == 'd' || c == 'r') {
  5111. tok.next();
  5112. symbol nm = get_name(1);
  5113. if (nm.is_null()) {
  5114. skip_alternative();
  5115. return 0;
  5116. }
  5117. result = (c == 'd'
  5118. ? request_dictionary.lookup(nm) != 0
  5119. : number_reg_dictionary.lookup(nm) != 0);
  5120. }
  5121. else if (c == 'm') {
  5122. tok.next();
  5123. symbol nm = get_long_name(1);
  5124. if (nm.is_null()) {
  5125. skip_alternative();
  5126. return 0;
  5127. }
  5128. result = (nm == default_symbol
  5129. || color_dictionary.lookup(nm) != 0);
  5130. }
  5131. else if (c == 'c') {
  5132. tok.next();
  5133. tok.skip();
  5134. charinfo *ci = tok.get_char(1);
  5135. if (ci == 0) {
  5136. skip_alternative();
  5137. return 0;
  5138. }
  5139. result = character_exists(ci, curenv);
  5140. tok.next();
  5141. }
  5142. else if (c == 'F') {
  5143. tok.next();
  5144. symbol nm = get_long_name(1);
  5145. if (nm.is_null()) {
  5146. skip_alternative();
  5147. return 0;
  5148. }
  5149. result = check_font(curenv->get_family()->nm, nm);
  5150. }
  5151. else if (c == 'S') {
  5152. tok.next();
  5153. symbol nm = get_long_name(1);
  5154. if (nm.is_null()) {
  5155. skip_alternative();
  5156. return 0;
  5157. }
  5158. result = check_style(nm);
  5159. }
  5160. else if (tok.space())
  5161. result = 0;
  5162. else if (tok.delimiter()) {
  5163. token delim = tok;
  5164. int delim_level = input_stack::get_level();
  5165. environment env1(curenv);
  5166. environment env2(curenv);
  5167. environment *oldenv = curenv;
  5168. curenv = &env1;
  5169. suppress_push = 1;
  5170. for (int i = 0; i < 2; i++) {
  5171. for (;;) {
  5172. tok.next();
  5173. if (tok.newline() || tok.eof()) {
  5174. warning(WARN_DELIM, "missing closing delimiter");
  5175. tok.next();
  5176. curenv = oldenv;
  5177. return 0;
  5178. }
  5179. if (tok == delim
  5180. && (compatible_flag || input_stack::get_level() == delim_level))
  5181. break;
  5182. tok.process();
  5183. }
  5184. curenv = &env2;
  5185. }
  5186. node *n1 = env1.extract_output_line();
  5187. node *n2 = env2.extract_output_line();
  5188. result = same_node_list(n1, n2);
  5189. delete_node_list(n1);
  5190. delete_node_list(n2);
  5191. curenv = oldenv;
  5192. have_input = 0;
  5193. suppress_push = 0;
  5194. tok.next();
  5195. }
  5196. else {
  5197. units n;
  5198. if (!get_number(&n, 'u')) {
  5199. skip_alternative();
  5200. return 0;
  5201. }
  5202. else
  5203. result = n > 0;
  5204. }
  5205. if (invert)
  5206. result = !result;
  5207. if (result)
  5208. begin_alternative();
  5209. else
  5210. skip_alternative();
  5211. return result;
  5212. }
  5213. void if_else_request()
  5214. {
  5215. if_else_stack.push(do_if_request());
  5216. }
  5217. void if_request()
  5218. {
  5219. do_if_request();
  5220. }
  5221. void else_request()
  5222. {
  5223. if (if_else_stack.is_empty()) {
  5224. warning(WARN_EL, "unbalanced .el request");
  5225. skip_alternative();
  5226. }
  5227. else {
  5228. if (if_else_stack.pop())
  5229. skip_alternative();
  5230. else
  5231. begin_alternative();
  5232. }
  5233. }
  5234. static int while_depth = 0;
  5235. static int while_break_flag = 0;
  5236. void while_request()
  5237. {
  5238. macro mac;
  5239. int escaped = 0;
  5240. int level = 0;
  5241. mac.append(new token_node(tok));
  5242. for (;;) {
  5243. node *n = 0; // pacify compiler
  5244. int c = input_stack::get(&n);
  5245. if (c == EOF)
  5246. break;
  5247. if (c == 0) {
  5248. escaped = 0;
  5249. mac.append(n);
  5250. }
  5251. else if (escaped) {
  5252. if (c == '{')
  5253. level += 1;
  5254. else if (c == '}')
  5255. level -= 1;
  5256. escaped = 0;
  5257. mac.append(c);
  5258. }
  5259. else {
  5260. if (c == ESCAPE_LEFT_BRACE)
  5261. level += 1;
  5262. else if (c == ESCAPE_RIGHT_BRACE)
  5263. level -= 1;
  5264. else if (c == escape_char)
  5265. escaped = 1;
  5266. mac.append(c);
  5267. if (c == '\n' && level <= 0)
  5268. break;
  5269. }
  5270. }
  5271. if (level != 0)
  5272. error("unbalanced \\{ \\}");
  5273. else {
  5274. while_depth++;
  5275. input_stack::add_boundary();
  5276. for (;;) {
  5277. input_stack::push(new string_iterator(mac, "while loop"));
  5278. tok.next();
  5279. if (!do_if_request()) {
  5280. while (input_stack::get(0) != EOF)
  5281. ;
  5282. break;
  5283. }
  5284. process_input_stack();
  5285. if (while_break_flag || input_stack::is_return_boundary()) {
  5286. while_break_flag = 0;
  5287. break;
  5288. }
  5289. }
  5290. input_stack::remove_boundary();
  5291. while_depth--;
  5292. }
  5293. tok.next();
  5294. }
  5295. void while_break_request()
  5296. {
  5297. if (!while_depth) {
  5298. error("no while loop");
  5299. skip_line();
  5300. }
  5301. else {
  5302. while_break_flag = 1;
  5303. while (input_stack::get(0) != EOF)
  5304. ;
  5305. tok.next();
  5306. }
  5307. }
  5308. void while_continue_request()
  5309. {
  5310. if (!while_depth) {
  5311. error("no while loop");
  5312. skip_line();
  5313. }
  5314. else {
  5315. while (input_stack::get(0) != EOF)
  5316. ;
  5317. tok.next();
  5318. }
  5319. }
  5320. // .so
  5321. void source()
  5322. {
  5323. symbol nm = get_long_name(1);
  5324. if (nm.is_null())
  5325. skip_line();
  5326. else {
  5327. while (!tok.newline() && !tok.eof())
  5328. tok.next();
  5329. errno = 0;
  5330. FILE *fp = include_search_path.open_file_cautious(nm.contents());
  5331. if (fp)
  5332. input_stack::push(new file_iterator(fp, nm.contents()));
  5333. else
  5334. error("can't open `%1': %2", nm.contents(), strerror(errno));
  5335. tok.next();
  5336. }
  5337. }
  5338. // like .so but use popen()
  5339. void pipe_source()
  5340. {
  5341. if (safer_flag) {
  5342. error(".pso request not allowed in safer mode");
  5343. skip_line();
  5344. }
  5345. else {
  5346. #ifdef POPEN_MISSING
  5347. error("pipes not available on this system");
  5348. skip_line();
  5349. #else /* not POPEN_MISSING */
  5350. if (tok.newline() || tok.eof())
  5351. error("missing command");
  5352. else {
  5353. int c;
  5354. while ((c = get_copy(0)) == ' ' || c == '\t')
  5355. ;
  5356. int buf_size = 24;
  5357. char *buf = new char[buf_size];
  5358. int buf_used = 0;
  5359. for (; c != '\n' && c != EOF; c = get_copy(0)) {
  5360. const char *s = asciify(c);
  5361. int slen = strlen(s);
  5362. if (buf_used + slen + 1> buf_size) {
  5363. char *old_buf = buf;
  5364. int old_buf_size = buf_size;
  5365. buf_size *= 2;
  5366. buf = new char[buf_size];
  5367. memcpy(buf, old_buf, old_buf_size);
  5368. a_delete old_buf;
  5369. }
  5370. strcpy(buf + buf_used, s);
  5371. buf_used += slen;
  5372. }
  5373. buf[buf_used] = '\0';
  5374. errno = 0;
  5375. FILE *fp = popen(buf, POPEN_RT);
  5376. if (fp)
  5377. input_stack::push(new file_iterator(fp, symbol(buf).contents(), 1));
  5378. else
  5379. error("can't open pipe to process `%1': %2", buf, strerror(errno));
  5380. a_delete buf;
  5381. }
  5382. tok.next();
  5383. #endif /* not POPEN_MISSING */
  5384. }
  5385. }
  5386. // .psbb
  5387. static int llx_reg_contents = 0;
  5388. static int lly_reg_contents = 0;
  5389. static int urx_reg_contents = 0;
  5390. static int ury_reg_contents = 0;
  5391. struct bounding_box {
  5392. int llx, lly, urx, ury;
  5393. };
  5394. /* Parse the argument to a %%BoundingBox comment. Return 1 if it
  5395. contains 4 numbers, 2 if it contains (atend), 0 otherwise. */
  5396. int parse_bounding_box(char *p, bounding_box *bb)
  5397. {
  5398. if (sscanf(p, "%d %d %d %d",
  5399. &bb->llx, &bb->lly, &bb->urx, &bb->ury) == 4)
  5400. return 1;
  5401. else {
  5402. /* The Document Structuring Conventions say that the numbers
  5403. should be integers. Unfortunately some broken applications
  5404. get this wrong. */
  5405. double x1, x2, x3, x4;
  5406. if (sscanf(p, "%lf %lf %lf %lf", &x1, &x2, &x3, &x4) == 4) {
  5407. bb->llx = (int)x1;
  5408. bb->lly = (int)x2;
  5409. bb->urx = (int)x3;
  5410. bb->ury = (int)x4;
  5411. return 1;
  5412. }
  5413. else {
  5414. for (; *p == ' ' || *p == '\t'; p++)
  5415. ;
  5416. if (strncmp(p, "(atend)", 7) == 0) {
  5417. return 2;
  5418. }
  5419. }
  5420. }
  5421. bb->llx = bb->lly = bb->urx = bb->ury = 0;
  5422. return 0;
  5423. }
  5424. // This version is taken from psrm.cpp
  5425. #define PS_LINE_MAX 255
  5426. cset white_space("\n\r \t");
  5427. int ps_get_line(char *buf, FILE *fp, const char* filename)
  5428. {
  5429. int c = getc(fp);
  5430. if (c == EOF) {
  5431. buf[0] = '\0';
  5432. return 0;
  5433. }
  5434. int i = 0;
  5435. int err = 0;
  5436. while (c != '\r' && c != '\n' && c != EOF) {
  5437. if ((c < 0x1b && !white_space(c)) || c == 0x7f)
  5438. error("invalid input character code %1 in `%2'", int(c), filename);
  5439. else if (i < PS_LINE_MAX)
  5440. buf[i++] = c;
  5441. else if (!err) {
  5442. err = 1;
  5443. error("PostScript file `%1' is non-conforming "
  5444. "because length of line exceeds 255", filename);
  5445. }
  5446. c = getc(fp);
  5447. }
  5448. buf[i++] = '\n';
  5449. buf[i] = '\0';
  5450. if (c == '\r') {
  5451. c = getc(fp);
  5452. if (c != EOF && c != '\n')
  5453. ungetc(c, fp);
  5454. }
  5455. return 1;
  5456. }
  5457. inline void assign_registers(int llx, int lly, int urx, int ury)
  5458. {
  5459. llx_reg_contents = llx;
  5460. lly_reg_contents = lly;
  5461. urx_reg_contents = urx;
  5462. ury_reg_contents = ury;
  5463. }
  5464. void do_ps_file(FILE *fp, const char* filename)
  5465. {
  5466. bounding_box bb;
  5467. int bb_at_end = 0;
  5468. char buf[PS_LINE_MAX];
  5469. llx_reg_contents = lly_reg_contents =
  5470. urx_reg_contents = ury_reg_contents = 0;
  5471. if (!ps_get_line(buf, fp, filename)) {
  5472. error("`%1' is empty", filename);
  5473. return;
  5474. }
  5475. if (strncmp("%!PS-Adobe-", buf, 11) != 0) {
  5476. error("`%1' is not conforming to the Document Structuring Conventions",
  5477. filename);
  5478. return;
  5479. }
  5480. while (ps_get_line(buf, fp, filename) != 0) {
  5481. if (buf[0] != '%' || buf[1] != '%'
  5482. || strncmp(buf + 2, "EndComments", 11) == 0)
  5483. break;
  5484. if (strncmp(buf + 2, "BoundingBox:", 12) == 0) {
  5485. int res = parse_bounding_box(buf + 14, &bb);
  5486. if (res == 1) {
  5487. assign_registers(bb.llx, bb.lly, bb.urx, bb.ury);
  5488. return;
  5489. }
  5490. else if (res == 2) {
  5491. bb_at_end = 1;
  5492. break;
  5493. }
  5494. else {
  5495. error("the arguments to the %%%%BoundingBox comment in `%1' are bad",
  5496. filename);
  5497. return;
  5498. }
  5499. }
  5500. }
  5501. if (bb_at_end) {
  5502. long offset;
  5503. int last_try = 0;
  5504. /* in the trailer, the last BoundingBox comment is significant */
  5505. for (offset = 512; !last_try; offset *= 2) {
  5506. int had_trailer = 0;
  5507. int got_bb = 0;
  5508. if (offset > 32768 || fseek(fp, -offset, 2) == -1) {
  5509. last_try = 1;
  5510. if (fseek(fp, 0L, 0) == -1)
  5511. break;
  5512. }
  5513. while (ps_get_line(buf, fp, filename) != 0) {
  5514. if (buf[0] == '%' && buf[1] == '%') {
  5515. if (!had_trailer) {
  5516. if (strncmp(buf + 2, "Trailer", 7) == 0)
  5517. had_trailer = 1;
  5518. }
  5519. else {
  5520. if (strncmp(buf + 2, "BoundingBox:", 12) == 0) {
  5521. int res = parse_bounding_box(buf + 14, &bb);
  5522. if (res == 1)
  5523. got_bb = 1;
  5524. else if (res == 2) {
  5525. error("`(atend)' not allowed in trailer of `%1'", filename);
  5526. return;
  5527. }
  5528. else {
  5529. error("the arguments to the %%%%BoundingBox comment in `%1' are bad",
  5530. filename);
  5531. return;
  5532. }
  5533. }
  5534. }
  5535. }
  5536. }
  5537. if (got_bb) {
  5538. assign_registers(bb.llx, bb.lly, bb.urx, bb.ury);
  5539. return;
  5540. }
  5541. }
  5542. }
  5543. error("%%%%BoundingBox comment not found in `%1'", filename);
  5544. }
  5545. void ps_bbox_request()
  5546. {
  5547. symbol nm = get_long_name(1);
  5548. if (nm.is_null())
  5549. skip_line();
  5550. else {
  5551. while (!tok.newline() && !tok.eof())
  5552. tok.next();
  5553. errno = 0;
  5554. // PS files might contain non-printable characters, such as ^Z
  5555. // and CRs not followed by an LF, so open them in binary mode.
  5556. FILE *fp = include_search_path.open_file_cautious(nm.contents(),
  5557. 0, FOPEN_RB);
  5558. if (fp) {
  5559. do_ps_file(fp, nm.contents());
  5560. fclose(fp);
  5561. }
  5562. else
  5563. error("can't open `%1': %2", nm.contents(), strerror(errno));
  5564. tok.next();
  5565. }
  5566. }
  5567. const char *asciify(int c)
  5568. {
  5569. static char buf[3];
  5570. buf[0] = escape_char == '\0' ? '\\' : escape_char;
  5571. buf[1] = buf[2] = '\0';
  5572. switch (c) {
  5573. case ESCAPE_QUESTION:
  5574. buf[1] = '?';
  5575. break;
  5576. case ESCAPE_AMPERSAND:
  5577. buf[1] = '&';
  5578. break;
  5579. case ESCAPE_RIGHT_PARENTHESIS:
  5580. buf[1] = ')';
  5581. break;
  5582. case ESCAPE_UNDERSCORE:
  5583. buf[1] = '_';
  5584. break;
  5585. case ESCAPE_BAR:
  5586. buf[1] = '|';
  5587. break;
  5588. case ESCAPE_CIRCUMFLEX:
  5589. buf[1] = '^';
  5590. break;
  5591. case ESCAPE_LEFT_BRACE:
  5592. buf[1] = '{';
  5593. break;
  5594. case ESCAPE_RIGHT_BRACE:
  5595. buf[1] = '}';
  5596. break;
  5597. case ESCAPE_LEFT_QUOTE:
  5598. buf[1] = '`';
  5599. break;
  5600. case ESCAPE_RIGHT_QUOTE:
  5601. buf[1] = '\'';
  5602. break;
  5603. case ESCAPE_HYPHEN:
  5604. buf[1] = '-';
  5605. break;
  5606. case ESCAPE_BANG:
  5607. buf[1] = '!';
  5608. break;
  5609. case ESCAPE_c:
  5610. buf[1] = 'c';
  5611. break;
  5612. case ESCAPE_e:
  5613. buf[1] = 'e';
  5614. break;
  5615. case ESCAPE_E:
  5616. buf[1] = 'E';
  5617. break;
  5618. case ESCAPE_PERCENT:
  5619. buf[1] = '%';
  5620. break;
  5621. case ESCAPE_SPACE:
  5622. buf[1] = ' ';
  5623. break;
  5624. case ESCAPE_TILDE:
  5625. buf[1] = '~';
  5626. break;
  5627. case ESCAPE_COLON:
  5628. buf[1] = ':';
  5629. break;
  5630. case PUSH_GROFF_MODE:
  5631. case PUSH_COMP_MODE:
  5632. case POP_GROFFCOMP_MODE:
  5633. buf[0] = '\0';
  5634. break;
  5635. default:
  5636. if (invalid_input_char(c))
  5637. buf[0] = '\0';
  5638. else
  5639. buf[0] = c;
  5640. break;
  5641. }
  5642. return buf;
  5643. }
  5644. const char *input_char_description(int c)
  5645. {
  5646. switch (c) {
  5647. case '\n':
  5648. return "a newline character";
  5649. case '\b':
  5650. return "a backspace character";
  5651. case '\001':
  5652. return "a leader character";
  5653. case '\t':
  5654. return "a tab character";
  5655. case ' ':
  5656. return "a space character";
  5657. case '\0':
  5658. return "a node";
  5659. }
  5660. static char buf[sizeof("magic character code ") + 1 + INT_DIGITS];
  5661. if (invalid_input_char(c)) {
  5662. const char *s = asciify(c);
  5663. if (*s) {
  5664. buf[0] = '`';
  5665. strcpy(buf + 1, s);
  5666. strcat(buf, "'");
  5667. return buf;
  5668. }
  5669. sprintf(buf, "magic character code %d", c);
  5670. return buf;
  5671. }
  5672. if (csprint(c)) {
  5673. buf[0] = '`';
  5674. buf[1] = c;
  5675. buf[2] = '\'';
  5676. return buf;
  5677. }
  5678. sprintf(buf, "character code %d", c);
  5679. return buf;
  5680. }
  5681. void tag()
  5682. {
  5683. if (!tok.newline() && !tok.eof()) {
  5684. string s;
  5685. int c;
  5686. for (;;) {
  5687. c = get_copy(0);
  5688. if (c == '"') {
  5689. c = get_copy(0);
  5690. break;
  5691. }
  5692. if (c != ' ' && c != '\t')
  5693. break;
  5694. }
  5695. s = "x X ";
  5696. for (; c != '\n' && c != EOF; c = get_copy(0))
  5697. s += (char)c;
  5698. s += '\n';
  5699. curenv->add_node(new tag_node(s, 0));
  5700. }
  5701. tok.next();
  5702. }
  5703. void taga()
  5704. {
  5705. if (!tok.newline() && !tok.eof()) {
  5706. string s;
  5707. int c;
  5708. for (;;) {
  5709. c = get_copy(0);
  5710. if (c == '"') {
  5711. c = get_copy(0);
  5712. break;
  5713. }
  5714. if (c != ' ' && c != '\t')
  5715. break;
  5716. }
  5717. s = "x X ";
  5718. for (; c != '\n' && c != EOF; c = get_copy(0))
  5719. s += (char)c;
  5720. s += '\n';
  5721. curenv->add_node(new tag_node(s, 1));
  5722. }
  5723. tok.next();
  5724. }
  5725. // .tm, .tm1, and .tmc
  5726. void do_terminal(int newline, int string_like)
  5727. {
  5728. if (!tok.newline() && !tok.eof()) {
  5729. int c;
  5730. for (;;) {
  5731. c = get_copy(0);
  5732. if (string_like && c == '"') {
  5733. c = get_copy(0);
  5734. break;
  5735. }
  5736. if (c != ' ' && c != '\t')
  5737. break;
  5738. }
  5739. for (; c != '\n' && c != EOF; c = get_copy(0))
  5740. fputs(asciify(c), stderr);
  5741. }
  5742. if (newline)
  5743. fputc('\n', stderr);
  5744. fflush(stderr);
  5745. tok.next();
  5746. }
  5747. void terminal()
  5748. {
  5749. do_terminal(1, 0);
  5750. }
  5751. void terminal1()
  5752. {
  5753. do_terminal(1, 1);
  5754. }
  5755. void terminal_continue()
  5756. {
  5757. do_terminal(0, 1);
  5758. }
  5759. dictionary stream_dictionary(20);
  5760. void do_open(int append)
  5761. {
  5762. symbol stream = get_name(1);
  5763. if (!stream.is_null()) {
  5764. symbol filename = get_long_name(1);
  5765. if (!filename.is_null()) {
  5766. errno = 0;
  5767. FILE *fp = fopen(filename.contents(), append ? "a" : "w");
  5768. if (!fp) {
  5769. error("can't open `%1' for %2: %3",
  5770. filename.contents(),
  5771. append ? "appending" : "writing",
  5772. strerror(errno));
  5773. fp = (FILE *)stream_dictionary.remove(stream);
  5774. }
  5775. else
  5776. fp = (FILE *)stream_dictionary.lookup(stream, fp);
  5777. if (fp)
  5778. fclose(fp);
  5779. }
  5780. }
  5781. skip_line();
  5782. }
  5783. void open_request()
  5784. {
  5785. if (safer_flag) {
  5786. error(".open request not allowed in safer mode");
  5787. skip_line();
  5788. }
  5789. else
  5790. do_open(0);
  5791. }
  5792. void opena_request()
  5793. {
  5794. if (safer_flag) {
  5795. error(".opena request not allowed in safer mode");
  5796. skip_line();
  5797. }
  5798. else
  5799. do_open(1);
  5800. }
  5801. void close_request()
  5802. {
  5803. symbol stream = get_name(1);
  5804. if (!stream.is_null()) {
  5805. FILE *fp = (FILE *)stream_dictionary.remove(stream);
  5806. if (!fp)
  5807. error("no stream named `%1'", stream.contents());
  5808. else
  5809. fclose(fp);
  5810. }
  5811. skip_line();
  5812. }
  5813. // .write and .writec
  5814. void do_write_request(int newline)
  5815. {
  5816. symbol stream = get_name(1);
  5817. if (stream.is_null()) {
  5818. skip_line();
  5819. return;
  5820. }
  5821. FILE *fp = (FILE *)stream_dictionary.lookup(stream);
  5822. if (!fp) {
  5823. error("no stream named `%1'", stream.contents());
  5824. skip_line();
  5825. return;
  5826. }
  5827. int c;
  5828. while ((c = get_copy(0)) == ' ')
  5829. ;
  5830. if (c == '"')
  5831. c = get_copy(0);
  5832. for (; c != '\n' && c != EOF; c = get_copy(0))
  5833. fputs(asciify(c), fp);
  5834. if (newline)
  5835. fputc('\n', fp);
  5836. fflush(fp);
  5837. tok.next();
  5838. }
  5839. void write_request()
  5840. {
  5841. do_write_request(1);
  5842. }
  5843. void write_request_continue()
  5844. {
  5845. do_write_request(0);
  5846. }
  5847. void write_macro_request()
  5848. {
  5849. symbol stream = get_name(1);
  5850. if (stream.is_null()) {
  5851. skip_line();
  5852. return;
  5853. }
  5854. FILE *fp = (FILE *)stream_dictionary.lookup(stream);
  5855. if (!fp) {
  5856. error("no stream named `%1'", stream.contents());
  5857. skip_line();
  5858. return;
  5859. }
  5860. symbol s = get_name(1);
  5861. if (s.is_null()) {
  5862. skip_line();
  5863. return;
  5864. }
  5865. request_or_macro *p = lookup_request(s);
  5866. macro *m = p->to_macro();
  5867. if (!m)
  5868. error("cannot write request");
  5869. else {
  5870. string_iterator iter(*m);
  5871. for (;;) {
  5872. int c = iter.get(0);
  5873. if (c == EOF)
  5874. break;
  5875. fputs(asciify(c), fp);
  5876. }
  5877. fflush(fp);
  5878. }
  5879. skip_line();
  5880. }
  5881. void warnscale_request()
  5882. {
  5883. if (has_arg()) {
  5884. char c = tok.ch();
  5885. if (c == 'u')
  5886. warn_scale = 1.0;
  5887. else if (c == 'i')
  5888. warn_scale = (double)units_per_inch;
  5889. else if (c == 'c')
  5890. warn_scale = (double)units_per_inch / 2.54;
  5891. else if (c == 'p')
  5892. warn_scale = (double)units_per_inch / 72.0;
  5893. else if (c == 'P')
  5894. warn_scale = (double)units_per_inch / 6.0;
  5895. else {
  5896. warning(WARN_SCALE,
  5897. "invalid scaling indicator `%1', using `i' instead", c);
  5898. c = 'i';
  5899. }
  5900. warn_scaling_indicator = c;
  5901. }
  5902. skip_line();
  5903. }
  5904. void spreadwarn_request()
  5905. {
  5906. hunits n;
  5907. if (has_arg() && get_hunits(&n, 'm')) {
  5908. if (n < 0)
  5909. n = 0;
  5910. hunits em = curenv->get_size();
  5911. spread_limit = (double)n.to_units()
  5912. / (em.is_zero() ? hresolution : em.to_units());
  5913. }
  5914. else
  5915. spread_limit = -spread_limit - 1; // no arg toggles on/off without
  5916. // changing value; we mirror at
  5917. // -0.5 to make zero a valid value
  5918. skip_line();
  5919. }
  5920. static void init_charset_table()
  5921. {
  5922. char buf[16];
  5923. strcpy(buf, "char");
  5924. for (int i = 0; i < 256; i++) {
  5925. strcpy(buf + 4, i_to_a(i));
  5926. charset_table[i] = get_charinfo(symbol(buf));
  5927. charset_table[i]->set_ascii_code(i);
  5928. if (csalpha(i))
  5929. charset_table[i]->set_hyphenation_code(cmlower(i));
  5930. }
  5931. charset_table['.']->set_flags(charinfo::ENDS_SENTENCE);
  5932. charset_table['?']->set_flags(charinfo::ENDS_SENTENCE);
  5933. charset_table['!']->set_flags(charinfo::ENDS_SENTENCE);
  5934. charset_table['-']->set_flags(charinfo::BREAK_AFTER);
  5935. charset_table['"']->set_flags(charinfo::TRANSPARENT);
  5936. charset_table['\'']->set_flags(charinfo::TRANSPARENT);
  5937. charset_table[')']->set_flags(charinfo::TRANSPARENT);
  5938. charset_table[']']->set_flags(charinfo::TRANSPARENT);
  5939. charset_table['*']->set_flags(charinfo::TRANSPARENT);
  5940. get_charinfo(symbol("dg"))->set_flags(charinfo::TRANSPARENT);
  5941. get_charinfo(symbol("rq"))->set_flags(charinfo::TRANSPARENT);
  5942. get_charinfo(symbol("em"))->set_flags(charinfo::BREAK_AFTER);
  5943. get_charinfo(symbol("ul"))->set_flags(charinfo::OVERLAPS_HORIZONTALLY);
  5944. get_charinfo(symbol("rn"))->set_flags(charinfo::OVERLAPS_HORIZONTALLY);
  5945. get_charinfo(symbol("radicalex"))->set_flags(charinfo::OVERLAPS_HORIZONTALLY);
  5946. get_charinfo(symbol("sqrtex"))->set_flags(charinfo::OVERLAPS_HORIZONTALLY);
  5947. get_charinfo(symbol("ru"))->set_flags(charinfo::OVERLAPS_HORIZONTALLY);
  5948. get_charinfo(symbol("br"))->set_flags(charinfo::OVERLAPS_VERTICALLY);
  5949. page_character = charset_table['%'];
  5950. }
  5951. static void init_hpf_code_table()
  5952. {
  5953. for (int i = 0; i < 256; i++)
  5954. hpf_code_table[i] = i;
  5955. }
  5956. static void do_translate(int translate_transparent, int translate_input)
  5957. {
  5958. tok.skip();
  5959. while (!tok.newline() && !tok.eof()) {
  5960. if (tok.space()) {
  5961. // This is a really bizarre troff feature.
  5962. tok.next();
  5963. translate_space_to_dummy = tok.dummy();
  5964. if (tok.newline() || tok.eof())
  5965. break;
  5966. tok.next();
  5967. continue;
  5968. }
  5969. charinfo *ci1 = tok.get_char(1);
  5970. if (ci1 == 0)
  5971. break;
  5972. tok.next();
  5973. if (tok.newline() || tok.eof()) {
  5974. ci1->set_special_translation(charinfo::TRANSLATE_SPACE,
  5975. translate_transparent);
  5976. break;
  5977. }
  5978. if (tok.space())
  5979. ci1->set_special_translation(charinfo::TRANSLATE_SPACE,
  5980. translate_transparent);
  5981. else if (tok.stretchable_space())
  5982. ci1->set_special_translation(charinfo::TRANSLATE_STRETCHABLE_SPACE,
  5983. translate_transparent);
  5984. else if (tok.dummy())
  5985. ci1->set_special_translation(charinfo::TRANSLATE_DUMMY,
  5986. translate_transparent);
  5987. else if (tok.hyphen_indicator())
  5988. ci1->set_special_translation(charinfo::TRANSLATE_HYPHEN_INDICATOR,
  5989. translate_transparent);
  5990. else {
  5991. charinfo *ci2 = tok.get_char(1);
  5992. if (ci2 == 0)
  5993. break;
  5994. if (ci1 == ci2)
  5995. ci1->set_translation(0, translate_transparent, translate_input);
  5996. else
  5997. ci1->set_translation(ci2, translate_transparent, translate_input);
  5998. }
  5999. tok.next();
  6000. }
  6001. skip_line();
  6002. }
  6003. void translate()
  6004. {
  6005. do_translate(1, 0);
  6006. }
  6007. void translate_no_transparent()
  6008. {
  6009. do_translate(0, 0);
  6010. }
  6011. void translate_input()
  6012. {
  6013. do_translate(1, 1);
  6014. }
  6015. void char_flags()
  6016. {
  6017. int flags;
  6018. if (get_integer(&flags))
  6019. while (has_arg()) {
  6020. charinfo *ci = tok.get_char(1);
  6021. if (ci) {
  6022. charinfo *tem = ci->get_translation();
  6023. if (tem)
  6024. ci = tem;
  6025. ci->set_flags(flags);
  6026. }
  6027. tok.next();
  6028. }
  6029. skip_line();
  6030. }
  6031. void hyphenation_code()
  6032. {
  6033. tok.skip();
  6034. while (!tok.newline() && !tok.eof()) {
  6035. charinfo *ci = tok.get_char(1);
  6036. if (ci == 0)
  6037. break;
  6038. tok.next();
  6039. tok.skip();
  6040. unsigned char c = tok.ch();
  6041. if (c == 0) {
  6042. error("hyphenation code must be ordinary character");
  6043. break;
  6044. }
  6045. if (csdigit(c)) {
  6046. error("hyphenation code cannot be digit");
  6047. break;
  6048. }
  6049. ci->set_hyphenation_code(c);
  6050. if (ci->get_translation()
  6051. && ci->get_translation()->get_translation_input())
  6052. ci->get_translation()->set_hyphenation_code(c);
  6053. tok.next();
  6054. tok.skip();
  6055. }
  6056. skip_line();
  6057. }
  6058. void hyphenation_patterns_file_code()
  6059. {
  6060. tok.skip();
  6061. while (!tok.newline() && !tok.eof()) {
  6062. int n1, n2;
  6063. if (get_integer(&n1) && (0 <= n1 && n1 <= 255)) {
  6064. if (!has_arg()) {
  6065. error("missing output hyphenation code");
  6066. break;
  6067. }
  6068. if (get_integer(&n2) && (0 <= n2 && n2 <= 255)) {
  6069. hpf_code_table[n1] = n2;
  6070. tok.skip();
  6071. }
  6072. else {
  6073. error("output hyphenation code must be integer in the range 0..255");
  6074. break;
  6075. }
  6076. }
  6077. else {
  6078. error("input hyphenation code must be integer in the range 0..255");
  6079. break;
  6080. }
  6081. }
  6082. skip_line();
  6083. }
  6084. charinfo *token::get_char(int required)
  6085. {
  6086. if (type == TOKEN_CHAR)
  6087. return charset_table[c];
  6088. if (type == TOKEN_SPECIAL)
  6089. return get_charinfo(nm);
  6090. if (type == TOKEN_NUMBERED_CHAR)
  6091. return get_charinfo_by_number(val);
  6092. if (type == TOKEN_ESCAPE) {
  6093. if (escape_char != 0)
  6094. return charset_table[escape_char];
  6095. else {
  6096. error("`\\e' used while no current escape character");
  6097. return 0;
  6098. }
  6099. }
  6100. if (required) {
  6101. if (type == TOKEN_EOF || type == TOKEN_NEWLINE)
  6102. warning(WARN_MISSING, "missing normal or special character");
  6103. else
  6104. error("normal or special character expected (got %1)", description());
  6105. }
  6106. return 0;
  6107. }
  6108. charinfo *get_optional_char()
  6109. {
  6110. while (tok.space())
  6111. tok.next();
  6112. charinfo *ci = tok.get_char();
  6113. if (!ci)
  6114. check_missing_character();
  6115. else
  6116. tok.next();
  6117. return ci;
  6118. }
  6119. void check_missing_character()
  6120. {
  6121. if (!tok.newline() && !tok.eof() && !tok.right_brace() && !tok.tab())
  6122. error("normal or special character expected (got %1): "
  6123. "treated as missing",
  6124. tok.description());
  6125. }
  6126. // this is for \Z
  6127. int token::add_to_node_list(node **pp)
  6128. {
  6129. hunits w;
  6130. int s;
  6131. node *n = 0;
  6132. switch (type) {
  6133. case TOKEN_CHAR:
  6134. *pp = (*pp)->add_char(charset_table[c], curenv, &w, &s);
  6135. break;
  6136. case TOKEN_DUMMY:
  6137. n = new dummy_node;
  6138. break;
  6139. case TOKEN_ESCAPE:
  6140. if (escape_char != 0)
  6141. *pp = (*pp)->add_char(charset_table[escape_char], curenv, &w, &s);
  6142. break;
  6143. case TOKEN_HYPHEN_INDICATOR:
  6144. *pp = (*pp)->add_discretionary_hyphen();
  6145. break;
  6146. case TOKEN_ITALIC_CORRECTION:
  6147. *pp = (*pp)->add_italic_correction(&w);
  6148. break;
  6149. case TOKEN_LEFT_BRACE:
  6150. break;
  6151. case TOKEN_MARK_INPUT:
  6152. set_number_reg(nm, curenv->get_input_line_position().to_units());
  6153. break;
  6154. case TOKEN_NODE:
  6155. n = nd;
  6156. nd = 0;
  6157. break;
  6158. case TOKEN_NUMBERED_CHAR:
  6159. *pp = (*pp)->add_char(get_charinfo_by_number(val), curenv, &w, &s);
  6160. break;
  6161. case TOKEN_RIGHT_BRACE:
  6162. break;
  6163. case TOKEN_SPACE:
  6164. n = new hmotion_node(curenv->get_space_width(),
  6165. curenv->get_fill_color());
  6166. break;
  6167. case TOKEN_SPECIAL:
  6168. *pp = (*pp)->add_char(get_charinfo(nm), curenv, &w, &s);
  6169. break;
  6170. case TOKEN_STRETCHABLE_SPACE:
  6171. n = new unbreakable_space_node(curenv->get_space_width(),
  6172. curenv->get_fill_color());
  6173. break;
  6174. case TOKEN_UNSTRETCHABLE_SPACE:
  6175. n = new space_char_hmotion_node(curenv->get_space_width(),
  6176. curenv->get_fill_color());
  6177. break;
  6178. case TOKEN_TRANSPARENT_DUMMY:
  6179. n = new transparent_dummy_node;
  6180. break;
  6181. case TOKEN_ZERO_WIDTH_BREAK:
  6182. n = new space_node(H0, curenv->get_fill_color());
  6183. n->freeze_space();
  6184. n->is_escape_colon();
  6185. break;
  6186. default:
  6187. return 0;
  6188. }
  6189. if (n) {
  6190. n->next = *pp;
  6191. *pp = n;
  6192. }
  6193. return 1;
  6194. }
  6195. void token::process()
  6196. {
  6197. if (possibly_handle_first_page_transition())
  6198. return;
  6199. switch (type) {
  6200. case TOKEN_BACKSPACE:
  6201. curenv->add_node(new hmotion_node(-curenv->get_space_width(),
  6202. curenv->get_fill_color()));
  6203. break;
  6204. case TOKEN_CHAR:
  6205. curenv->add_char(charset_table[c]);
  6206. break;
  6207. case TOKEN_DUMMY:
  6208. curenv->add_node(new dummy_node);
  6209. break;
  6210. case TOKEN_EMPTY:
  6211. assert(0);
  6212. break;
  6213. case TOKEN_EOF:
  6214. assert(0);
  6215. break;
  6216. case TOKEN_ESCAPE:
  6217. if (escape_char != 0)
  6218. curenv->add_char(charset_table[escape_char]);
  6219. break;
  6220. case TOKEN_BEGIN_TRAP:
  6221. case TOKEN_END_TRAP:
  6222. case TOKEN_PAGE_EJECTOR:
  6223. // these are all handled in process_input_stack()
  6224. break;
  6225. case TOKEN_HYPHEN_INDICATOR:
  6226. curenv->add_hyphen_indicator();
  6227. break;
  6228. case TOKEN_INTERRUPT:
  6229. curenv->interrupt();
  6230. break;
  6231. case TOKEN_ITALIC_CORRECTION:
  6232. curenv->add_italic_correction();
  6233. break;
  6234. case TOKEN_LEADER:
  6235. curenv->handle_tab(1);
  6236. break;
  6237. case TOKEN_LEFT_BRACE:
  6238. break;
  6239. case TOKEN_MARK_INPUT:
  6240. set_number_reg(nm, curenv->get_input_line_position().to_units());
  6241. break;
  6242. case TOKEN_NEWLINE:
  6243. curenv->newline();
  6244. break;
  6245. case TOKEN_NODE:
  6246. curenv->add_node(nd);
  6247. nd = 0;
  6248. break;
  6249. case TOKEN_NUMBERED_CHAR:
  6250. curenv->add_char(get_charinfo_by_number(val));
  6251. break;
  6252. case TOKEN_REQUEST:
  6253. // handled in process_input_stack()
  6254. break;
  6255. case TOKEN_RIGHT_BRACE:
  6256. break;
  6257. case TOKEN_SPACE:
  6258. curenv->space();
  6259. break;
  6260. case TOKEN_SPECIAL:
  6261. curenv->add_char(get_charinfo(nm));
  6262. break;
  6263. case TOKEN_SPREAD:
  6264. curenv->spread();
  6265. break;
  6266. case TOKEN_STRETCHABLE_SPACE:
  6267. curenv->add_node(new unbreakable_space_node(curenv->get_space_width(),
  6268. curenv->get_fill_color()));
  6269. break;
  6270. case TOKEN_UNSTRETCHABLE_SPACE:
  6271. curenv->add_node(new space_char_hmotion_node(curenv->get_space_width(),
  6272. curenv->get_fill_color()));
  6273. break;
  6274. case TOKEN_TAB:
  6275. curenv->handle_tab(0);
  6276. break;
  6277. case TOKEN_TRANSPARENT:
  6278. break;
  6279. case TOKEN_TRANSPARENT_DUMMY:
  6280. curenv->add_node(new transparent_dummy_node);
  6281. break;
  6282. case TOKEN_ZERO_WIDTH_BREAK:
  6283. {
  6284. node *tmp = new space_node(H0, curenv->get_fill_color());
  6285. tmp->freeze_space();
  6286. tmp->is_escape_colon();
  6287. curenv->add_node(tmp);
  6288. break;
  6289. }
  6290. default:
  6291. assert(0);
  6292. }
  6293. }
  6294. class nargs_reg : public reg {
  6295. public:
  6296. const char *get_string();
  6297. };
  6298. const char *nargs_reg::get_string()
  6299. {
  6300. return i_to_a(input_stack::nargs());
  6301. }
  6302. class lineno_reg : public reg {
  6303. public:
  6304. const char *get_string();
  6305. };
  6306. const char *lineno_reg::get_string()
  6307. {
  6308. int line;
  6309. const char *file;
  6310. if (!input_stack::get_location(0, &file, &line))
  6311. line = 0;
  6312. return i_to_a(line);
  6313. }
  6314. class writable_lineno_reg : public general_reg {
  6315. public:
  6316. writable_lineno_reg();
  6317. void set_value(units);
  6318. int get_value(units *);
  6319. };
  6320. writable_lineno_reg::writable_lineno_reg()
  6321. {
  6322. }
  6323. int writable_lineno_reg::get_value(units *res)
  6324. {
  6325. int line;
  6326. const char *file;
  6327. if (!input_stack::get_location(0, &file, &line))
  6328. return 0;
  6329. *res = line;
  6330. return 1;
  6331. }
  6332. void writable_lineno_reg::set_value(units n)
  6333. {
  6334. input_stack::set_location(0, n);
  6335. }
  6336. class filename_reg : public reg {
  6337. public:
  6338. const char *get_string();
  6339. };
  6340. const char *filename_reg::get_string()
  6341. {
  6342. int line;
  6343. const char *file;
  6344. if (input_stack::get_location(0, &file, &line))
  6345. return file;
  6346. else
  6347. return 0;
  6348. }
  6349. class constant_reg : public reg {
  6350. const char *s;
  6351. public:
  6352. constant_reg(const char *);
  6353. const char *get_string();
  6354. };
  6355. constant_reg::constant_reg(const char *p) : s(p)
  6356. {
  6357. }
  6358. const char *constant_reg::get_string()
  6359. {
  6360. return s;
  6361. }
  6362. constant_int_reg::constant_int_reg(int *q) : p(q)
  6363. {
  6364. }
  6365. const char *constant_int_reg::get_string()
  6366. {
  6367. return i_to_a(*p);
  6368. }
  6369. void abort_request()
  6370. {
  6371. int c;
  6372. if (tok.eof())
  6373. c = EOF;
  6374. else if (tok.newline())
  6375. c = '\n';
  6376. else {
  6377. while ((c = get_copy(0)) == ' ')
  6378. ;
  6379. }
  6380. if (c == EOF || c == '\n')
  6381. fputs("User Abort.", stderr);
  6382. else {
  6383. for (; c != '\n' && c != EOF; c = get_copy(0))
  6384. fputs(asciify(c), stderr);
  6385. }
  6386. fputc('\n', stderr);
  6387. cleanup_and_exit(1);
  6388. }
  6389. char *read_string()
  6390. {
  6391. int len = 256;
  6392. char *s = new char[len];
  6393. int c;
  6394. while ((c = get_copy(0)) == ' ')
  6395. ;
  6396. int i = 0;
  6397. while (c != '\n' && c != EOF) {
  6398. if (!invalid_input_char(c)) {
  6399. if (i + 2 > len) {
  6400. char *tem = s;
  6401. s = new char[len*2];
  6402. memcpy(s, tem, len);
  6403. len *= 2;
  6404. a_delete tem;
  6405. }
  6406. s[i++] = c;
  6407. }
  6408. c = get_copy(0);
  6409. }
  6410. s[i] = '\0';
  6411. tok.next();
  6412. if (i == 0) {
  6413. a_delete s;
  6414. return 0;
  6415. }
  6416. return s;
  6417. }
  6418. void pipe_output()
  6419. {
  6420. if (safer_flag) {
  6421. error(".pi request not allowed in safer mode");
  6422. skip_line();
  6423. }
  6424. else {
  6425. #ifdef POPEN_MISSING
  6426. error("pipes not available on this system");
  6427. skip_line();
  6428. #else /* not POPEN_MISSING */
  6429. if (the_output) {
  6430. error("can't pipe: output already started");
  6431. skip_line();
  6432. }
  6433. else {
  6434. char *pc;
  6435. if ((pc = read_string()) == 0)
  6436. error("can't pipe to empty command");
  6437. if (pipe_command) {
  6438. char *s = new char[strlen(pipe_command) + strlen(pc) + 1 + 1];
  6439. strcpy(s, pipe_command);
  6440. strcat(s, "|");
  6441. strcat(s, pc);
  6442. a_delete pipe_command;
  6443. a_delete pc;
  6444. pipe_command = s;
  6445. }
  6446. else
  6447. pipe_command = pc;
  6448. }
  6449. #endif /* not POPEN_MISSING */
  6450. }
  6451. }
  6452. static int system_status;
  6453. void system_request()
  6454. {
  6455. if (safer_flag) {
  6456. error(".sy request not allowed in safer mode");
  6457. skip_line();
  6458. }
  6459. else {
  6460. char *command = read_string();
  6461. if (!command)
  6462. error("empty command");
  6463. else {
  6464. system_status = system(command);
  6465. a_delete command;
  6466. }
  6467. }
  6468. }
  6469. void copy_file()
  6470. {
  6471. if (curdiv == topdiv && topdiv->before_first_page) {
  6472. handle_initial_request(COPY_FILE_REQUEST);
  6473. return;
  6474. }
  6475. symbol filename = get_long_name(1);
  6476. while (!tok.newline() && !tok.eof())
  6477. tok.next();
  6478. if (break_flag)
  6479. curenv->do_break();
  6480. if (!filename.is_null())
  6481. curdiv->copy_file(filename.contents());
  6482. tok.next();
  6483. }
  6484. #ifdef COLUMN
  6485. void vjustify()
  6486. {
  6487. if (curdiv == topdiv && topdiv->before_first_page) {
  6488. handle_initial_request(VJUSTIFY_REQUEST);
  6489. return;
  6490. }
  6491. symbol type = get_long_name(1);
  6492. if (!type.is_null())
  6493. curdiv->vjustify(type);
  6494. skip_line();
  6495. }
  6496. #endif /* COLUMN */
  6497. void transparent_file()
  6498. {
  6499. if (curdiv == topdiv && topdiv->before_first_page) {
  6500. handle_initial_request(TRANSPARENT_FILE_REQUEST);
  6501. return;
  6502. }
  6503. symbol filename = get_long_name(1);
  6504. while (!tok.newline() && !tok.eof())
  6505. tok.next();
  6506. if (break_flag)
  6507. curenv->do_break();
  6508. if (!filename.is_null()) {
  6509. errno = 0;
  6510. FILE *fp = include_search_path.open_file_cautious(filename.contents());
  6511. if (!fp)
  6512. error("can't open `%1': %2", filename.contents(), strerror(errno));
  6513. else {
  6514. int bol = 1;
  6515. for (;;) {
  6516. int c = getc(fp);
  6517. if (c == EOF)
  6518. break;
  6519. if (invalid_input_char(c))
  6520. warning(WARN_INPUT, "invalid input character code %1", int(c));
  6521. else {
  6522. curdiv->transparent_output(c);
  6523. bol = c == '\n';
  6524. }
  6525. }
  6526. if (!bol)
  6527. curdiv->transparent_output('\n');
  6528. fclose(fp);
  6529. }
  6530. }
  6531. tok.next();
  6532. }
  6533. class page_range {
  6534. int first;
  6535. int last;
  6536. public:
  6537. page_range *next;
  6538. page_range(int, int, page_range *);
  6539. int contains(int n);
  6540. };
  6541. page_range::page_range(int i, int j, page_range *p)
  6542. : first(i), last(j), next(p)
  6543. {
  6544. }
  6545. int page_range::contains(int n)
  6546. {
  6547. return n >= first && (last <= 0 || n <= last);
  6548. }
  6549. page_range *output_page_list = 0;
  6550. int in_output_page_list(int n)
  6551. {
  6552. if (!output_page_list)
  6553. return 1;
  6554. for (page_range *p = output_page_list; p; p = p->next)
  6555. if (p->contains(n))
  6556. return 1;
  6557. return 0;
  6558. }
  6559. static void parse_output_page_list(char *p)
  6560. {
  6561. for (;;) {
  6562. int i;
  6563. if (*p == '-')
  6564. i = 1;
  6565. else if (csdigit(*p)) {
  6566. i = 0;
  6567. do
  6568. i = i*10 + *p++ - '0';
  6569. while (csdigit(*p));
  6570. }
  6571. else
  6572. break;
  6573. int j;
  6574. if (*p == '-') {
  6575. p++;
  6576. j = 0;
  6577. if (csdigit(*p)) {
  6578. do
  6579. j = j*10 + *p++ - '0';
  6580. while (csdigit(*p));
  6581. }
  6582. }
  6583. else
  6584. j = i;
  6585. if (j == 0)
  6586. last_page_number = -1;
  6587. else if (last_page_number >= 0 && j > last_page_number)
  6588. last_page_number = j;
  6589. output_page_list = new page_range(i, j, output_page_list);
  6590. if (*p != ',')
  6591. break;
  6592. ++p;
  6593. }
  6594. if (*p != '\0') {
  6595. error("bad output page list");
  6596. output_page_list = 0;
  6597. }
  6598. }
  6599. static FILE *open_mac_file(const char *mac, char **path)
  6600. {
  6601. // Try first FOOBAR.tmac, then tmac.FOOBAR
  6602. char *s1 = new char[strlen(mac)+strlen(MACRO_POSTFIX)+1];
  6603. strcpy(s1, mac);
  6604. strcat(s1, MACRO_POSTFIX);
  6605. FILE *fp = mac_path->open_file(s1, path);
  6606. a_delete s1;
  6607. if (!fp) {
  6608. char *s2 = new char[strlen(mac)+strlen(MACRO_PREFIX)+1];
  6609. strcpy(s2, MACRO_PREFIX);
  6610. strcat(s2, mac);
  6611. fp = mac_path->open_file(s2, path);
  6612. a_delete s2;
  6613. }
  6614. return fp;
  6615. }
  6616. static void process_macro_file(const char *mac)
  6617. {
  6618. char *path;
  6619. FILE *fp = open_mac_file(mac, &path);
  6620. if (!fp)
  6621. fatal("can't find macro file %1", mac);
  6622. const char *s = symbol(path).contents();
  6623. a_delete path;
  6624. input_stack::push(new file_iterator(fp, s));
  6625. tok.next();
  6626. process_input_stack();
  6627. }
  6628. static void process_startup_file(const char *filename)
  6629. {
  6630. char *path;
  6631. search_path *orig_mac_path = mac_path;
  6632. mac_path = &config_macro_path;
  6633. FILE *fp = mac_path->open_file(filename, &path);
  6634. if (fp) {
  6635. input_stack::push(new file_iterator(fp, symbol(path).contents()));
  6636. a_delete path;
  6637. tok.next();
  6638. process_input_stack();
  6639. }
  6640. mac_path = orig_mac_path;
  6641. }
  6642. void macro_source()
  6643. {
  6644. symbol nm = get_long_name(1);
  6645. if (nm.is_null())
  6646. skip_line();
  6647. else {
  6648. while (!tok.newline() && !tok.eof())
  6649. tok.next();
  6650. char *path;
  6651. FILE *fp = mac_path->open_file(nm.contents(), &path);
  6652. // .mso doesn't (and cannot) go through open_mac_file, so we
  6653. // need to do it here manually: If we have tmac.FOOBAR, try
  6654. // FOOBAR.tmac and vice versa
  6655. if (!fp) {
  6656. const char *fn = nm.contents();
  6657. if (strncasecmp(fn, MACRO_PREFIX, sizeof(MACRO_PREFIX) - 1) == 0) {
  6658. char *s = new char[strlen(fn) + sizeof(MACRO_POSTFIX)];
  6659. strcpy(s, fn + sizeof(MACRO_PREFIX) - 1);
  6660. strcat(s, MACRO_POSTFIX);
  6661. fp = mac_path->open_file(s, &path);
  6662. a_delete s;
  6663. }
  6664. if (!fp) {
  6665. if (strncasecmp(fn + strlen(fn) - sizeof(MACRO_POSTFIX) + 1,
  6666. MACRO_POSTFIX, sizeof(MACRO_POSTFIX) - 1) == 0) {
  6667. char *s = new char[strlen(fn) + sizeof(MACRO_PREFIX)];
  6668. strcpy(s, MACRO_PREFIX);
  6669. strncat(s, fn, strlen(fn) - sizeof(MACRO_POSTFIX) + 1);
  6670. fp = mac_path->open_file(s, &path);
  6671. a_delete s;
  6672. }
  6673. }
  6674. }
  6675. if (fp) {
  6676. input_stack::push(new file_iterator(fp, symbol(path).contents()));
  6677. a_delete path;
  6678. }
  6679. else
  6680. error("can't find macro file `%1'", nm.contents());
  6681. tok.next();
  6682. }
  6683. }
  6684. static void process_input_file(const char *name)
  6685. {
  6686. FILE *fp;
  6687. if (strcmp(name, "-") == 0) {
  6688. clearerr(stdin);
  6689. fp = stdin;
  6690. }
  6691. else {
  6692. errno = 0;
  6693. fp = include_search_path.open_file_cautious(name);
  6694. if (!fp)
  6695. fatal("can't open `%1': %2", name, strerror(errno));
  6696. }
  6697. input_stack::push(new file_iterator(fp, name));
  6698. tok.next();
  6699. process_input_stack();
  6700. }
  6701. // make sure the_input is empty before calling this
  6702. static int evaluate_expression(const char *expr, units *res)
  6703. {
  6704. input_stack::push(make_temp_iterator(expr));
  6705. tok.next();
  6706. int success = get_number(res, 'u');
  6707. while (input_stack::get(0) != EOF)
  6708. ;
  6709. return success;
  6710. }
  6711. static void do_register_assignment(const char *s)
  6712. {
  6713. const char *p = strchr(s, '=');
  6714. if (!p) {
  6715. char buf[2];
  6716. buf[0] = s[0];
  6717. buf[1] = 0;
  6718. units n;
  6719. if (evaluate_expression(s + 1, &n))
  6720. set_number_reg(buf, n);
  6721. }
  6722. else {
  6723. char *buf = new char[p - s + 1];
  6724. memcpy(buf, s, p - s);
  6725. buf[p - s] = 0;
  6726. units n;
  6727. if (evaluate_expression(p + 1, &n))
  6728. set_number_reg(buf, n);
  6729. a_delete buf;
  6730. }
  6731. }
  6732. static void set_string(const char *name, const char *value)
  6733. {
  6734. macro *m = new macro;
  6735. for (const char *p = value; *p; p++)
  6736. if (!invalid_input_char((unsigned char)*p))
  6737. m->append(*p);
  6738. request_dictionary.define(name, m);
  6739. }
  6740. static void do_string_assignment(const char *s)
  6741. {
  6742. const char *p = strchr(s, '=');
  6743. if (!p) {
  6744. char buf[2];
  6745. buf[0] = s[0];
  6746. buf[1] = 0;
  6747. set_string(buf, s + 1);
  6748. }
  6749. else {
  6750. char *buf = new char[p - s + 1];
  6751. memcpy(buf, s, p - s);
  6752. buf[p - s] = 0;
  6753. set_string(buf, p + 1);
  6754. a_delete buf;
  6755. }
  6756. }
  6757. struct string_list {
  6758. const char *s;
  6759. string_list *next;
  6760. string_list(const char *ss) : s(ss), next(0) {}
  6761. };
  6762. #if 0
  6763. static void prepend_string(const char *s, string_list **p)
  6764. {
  6765. string_list *l = new string_list(s);
  6766. l->next = *p;
  6767. *p = l;
  6768. }
  6769. #endif
  6770. static void add_string(const char *s, string_list **p)
  6771. {
  6772. while (*p)
  6773. p = &((*p)->next);
  6774. *p = new string_list(s);
  6775. }
  6776. void usage(FILE *stream, const char *prog)
  6777. {
  6778. fprintf(stream,
  6779. "usage: %s -abcivzCERU -wname -Wname -dcs -ffam -mname -nnum -olist\n"
  6780. " -rcn -Tname -Fdir -Idir -Mdir [files...]\n",
  6781. prog);
  6782. }
  6783. int main(int argc, char **argv)
  6784. {
  6785. program_name = argv[0];
  6786. static char stderr_buf[BUFSIZ];
  6787. setbuf(stderr, stderr_buf);
  6788. int c;
  6789. string_list *macros = 0;
  6790. string_list *register_assignments = 0;
  6791. string_list *string_assignments = 0;
  6792. int iflag = 0;
  6793. int tflag = 0;
  6794. int fflag = 0;
  6795. int nflag = 0;
  6796. int no_rc = 0; // don't process troffrc and troffrc-end
  6797. int next_page_number = 0; // pacify compiler
  6798. opterr = 0;
  6799. hresolution = vresolution = 1;
  6800. // restore $PATH if called from groff
  6801. char* groff_path = getenv("GROFF_PATH__");
  6802. if (groff_path) {
  6803. string e = "PATH";
  6804. e += '=';
  6805. if (*groff_path)
  6806. e += groff_path;
  6807. e += '\0';
  6808. if (putenv(strsave(e.contents())))
  6809. fatal("putenv failed");
  6810. }
  6811. static const struct option long_options[] = {
  6812. { "help", no_argument, 0, CHAR_MAX + 1 },
  6813. { "version", no_argument, 0, 'v' },
  6814. { 0, 0, 0, 0 }
  6815. };
  6816. #if defined(DEBUGGING)
  6817. #define DEBUG_OPTION "D"
  6818. #endif
  6819. while ((c = getopt_long(argc, argv,
  6820. "abciI:vw:W:zCEf:m:n:o:r:d:F:M:T:tqs:RU"
  6821. DEBUG_OPTION, long_options, 0))
  6822. != EOF)
  6823. switch(c) {
  6824. case 'v':
  6825. {
  6826. printf("GNU troff (groff) version %s\n", Version_string);
  6827. exit(0);
  6828. break;
  6829. }
  6830. case 'I':
  6831. // Search path for .psbb files
  6832. // and most other non-system input files.
  6833. include_search_path.command_line_dir(optarg);
  6834. break;
  6835. case 'T':
  6836. device = optarg;
  6837. tflag = 1;
  6838. is_html = (strcmp(device, "html") == 0);
  6839. break;
  6840. case 'C':
  6841. compatible_flag = 1;
  6842. // fall through
  6843. case 'c':
  6844. color_flag = 0;
  6845. break;
  6846. case 'M':
  6847. macro_path.command_line_dir(optarg);
  6848. safer_macro_path.command_line_dir(optarg);
  6849. config_macro_path.command_line_dir(optarg);
  6850. break;
  6851. case 'F':
  6852. font::command_line_font_dir(optarg);
  6853. break;
  6854. case 'm':
  6855. add_string(optarg, &macros);
  6856. break;
  6857. case 'E':
  6858. inhibit_errors = 1;
  6859. break;
  6860. case 'R':
  6861. no_rc = 1;
  6862. break;
  6863. case 'w':
  6864. enable_warning(optarg);
  6865. break;
  6866. case 'W':
  6867. disable_warning(optarg);
  6868. break;
  6869. case 'i':
  6870. iflag = 1;
  6871. break;
  6872. case 'b':
  6873. backtrace_flag = 1;
  6874. break;
  6875. case 'a':
  6876. ascii_output_flag = 1;
  6877. break;
  6878. case 'z':
  6879. suppress_output_flag = 1;
  6880. break;
  6881. case 'n':
  6882. if (sscanf(optarg, "%d", &next_page_number) == 1)
  6883. nflag++;
  6884. else
  6885. error("bad page number");
  6886. break;
  6887. case 'o':
  6888. parse_output_page_list(optarg);
  6889. break;
  6890. case 'd':
  6891. if (*optarg == '\0')
  6892. error("`-d' requires non-empty argument");
  6893. else
  6894. add_string(optarg, &string_assignments);
  6895. break;
  6896. case 'r':
  6897. if (*optarg == '\0')
  6898. error("`-r' requires non-empty argument");
  6899. else
  6900. add_string(optarg, &register_assignments);
  6901. break;
  6902. case 'f':
  6903. default_family = symbol(optarg);
  6904. fflag = 1;
  6905. break;
  6906. case 'q':
  6907. case 's':
  6908. case 't':
  6909. // silently ignore these
  6910. break;
  6911. case 'U':
  6912. safer_flag = 0; // unsafe behaviour
  6913. break;
  6914. #if defined(DEBUGGING)
  6915. case 'D':
  6916. debug_state = 1;
  6917. break;
  6918. #endif
  6919. case CHAR_MAX + 1: // --help
  6920. usage(stdout, argv[0]);
  6921. exit(0);
  6922. break;
  6923. case '?':
  6924. usage(stderr, argv[0]);
  6925. exit(1);
  6926. break; // never reached
  6927. default:
  6928. assert(0);
  6929. }
  6930. if (!safer_flag)
  6931. mac_path = &macro_path;
  6932. set_string(".T", device);
  6933. init_charset_table();
  6934. init_hpf_code_table();
  6935. if (!font::load_desc())
  6936. fatal("sorry, I can't continue");
  6937. units_per_inch = font::res;
  6938. hresolution = font::hor;
  6939. vresolution = font::vert;
  6940. sizescale = font::sizescale;
  6941. tcommand_flag = font::tcommand;
  6942. warn_scale = (double)units_per_inch;
  6943. warn_scaling_indicator = 'i';
  6944. if (!fflag && font::family != 0 && *font::family != '\0')
  6945. default_family = symbol(font::family);
  6946. font_size::init_size_table(font::sizes);
  6947. int i;
  6948. int j = 1;
  6949. if (font::style_table) {
  6950. for (i = 0; font::style_table[i]; i++)
  6951. mount_style(j++, symbol(font::style_table[i]));
  6952. }
  6953. for (i = 0; font::font_name_table[i]; i++, j++)
  6954. // In the DESC file a font name of 0 (zero) means leave this
  6955. // position empty.
  6956. if (strcmp(font::font_name_table[i], "0") != 0)
  6957. mount_font(j, symbol(font::font_name_table[i]));
  6958. curdiv = topdiv = new top_level_diversion;
  6959. if (nflag)
  6960. topdiv->set_next_page_number(next_page_number);
  6961. init_input_requests();
  6962. init_env_requests();
  6963. init_div_requests();
  6964. #ifdef COLUMN
  6965. init_column_requests();
  6966. #endif /* COLUMN */
  6967. init_node_requests();
  6968. number_reg_dictionary.define(".T", new constant_reg(tflag ? "1" : "0"));
  6969. init_registers();
  6970. init_reg_requests();
  6971. init_hyphen_requests();
  6972. init_environments();
  6973. while (string_assignments) {
  6974. do_string_assignment(string_assignments->s);
  6975. string_list *tem = string_assignments;
  6976. string_assignments = string_assignments->next;
  6977. delete tem;
  6978. }
  6979. while (register_assignments) {
  6980. do_register_assignment(register_assignments->s);
  6981. string_list *tem = register_assignments;
  6982. register_assignments = register_assignments->next;
  6983. delete tem;
  6984. }
  6985. if (!no_rc)
  6986. process_startup_file(INITIAL_STARTUP_FILE);
  6987. while (macros) {
  6988. process_macro_file(macros->s);
  6989. string_list *tem = macros;
  6990. macros = macros->next;
  6991. delete tem;
  6992. }
  6993. if (!no_rc)
  6994. process_startup_file(FINAL_STARTUP_FILE);
  6995. for (i = optind; i < argc; i++)
  6996. process_input_file(argv[i]);
  6997. if (optind >= argc || iflag)
  6998. process_input_file("-");
  6999. exit_troff();
  7000. return 0; // not reached
  7001. }
  7002. void warn_request()
  7003. {
  7004. int n;
  7005. if (has_arg() && get_integer(&n)) {
  7006. if (n & ~WARN_TOTAL) {
  7007. warning(WARN_RANGE, "warning mask must be between 0 and %1", WARN_TOTAL);
  7008. n &= WARN_TOTAL;
  7009. }
  7010. warning_mask = n;
  7011. }
  7012. else
  7013. warning_mask = WARN_TOTAL;
  7014. skip_line();
  7015. }
  7016. static void init_registers()
  7017. {
  7018. #ifdef LONG_FOR_TIME_T
  7019. long
  7020. #else /* not LONG_FOR_TIME_T */
  7021. time_t
  7022. #endif /* not LONG_FOR_TIME_T */
  7023. t = time(0);
  7024. // Use struct here to work around misfeature in old versions of g++.
  7025. struct tm *tt = localtime(&t);
  7026. set_number_reg("seconds", int(tt->tm_sec));
  7027. set_number_reg("minutes", int(tt->tm_min));
  7028. set_number_reg("hours", int(tt->tm_hour));
  7029. set_number_reg("dw", int(tt->tm_wday + 1));
  7030. set_number_reg("dy", int(tt->tm_mday));
  7031. set_number_reg("mo", int(tt->tm_mon + 1));
  7032. set_number_reg("year", int(1900 + tt->tm_year));
  7033. set_number_reg("yr", int(tt->tm_year));
  7034. set_number_reg("$$", getpid());
  7035. number_reg_dictionary.define(".A",
  7036. new constant_reg(ascii_output_flag
  7037. ? "1"
  7038. : "0"));
  7039. }
  7040. /*
  7041. * registers associated with \O
  7042. */
  7043. static int output_reg_minx_contents = -1;
  7044. static int output_reg_miny_contents = -1;
  7045. static int output_reg_maxx_contents = -1;
  7046. static int output_reg_maxy_contents = -1;
  7047. void check_output_limits(int x, int y)
  7048. {
  7049. if ((output_reg_minx_contents == -1) || (x < output_reg_minx_contents))
  7050. output_reg_minx_contents = x;
  7051. if (x > output_reg_maxx_contents)
  7052. output_reg_maxx_contents = x;
  7053. if ((output_reg_miny_contents == -1) || (y < output_reg_miny_contents))
  7054. output_reg_miny_contents = y;
  7055. if (y > output_reg_maxy_contents)
  7056. output_reg_maxy_contents = y;
  7057. }
  7058. void reset_output_registers()
  7059. {
  7060. output_reg_minx_contents = -1;
  7061. output_reg_miny_contents = -1;
  7062. output_reg_maxx_contents = -1;
  7063. output_reg_maxy_contents = -1;
  7064. }
  7065. void get_output_registers(int *minx, int *miny, int *maxx, int *maxy)
  7066. {
  7067. *minx = output_reg_minx_contents;
  7068. *miny = output_reg_miny_contents;
  7069. *maxx = output_reg_maxx_contents;
  7070. *maxy = output_reg_maxy_contents;
  7071. }
  7072. void init_input_requests()
  7073. {
  7074. init_request("ab", abort_request);
  7075. init_request("als", alias_macro);
  7076. init_request("am", append_macro);
  7077. init_request("am1", append_nocomp_macro);
  7078. init_request("ami", append_indirect_macro);
  7079. init_request("ami1", append_indirect_nocomp_macro);
  7080. init_request("as", append_string);
  7081. init_request("as1", append_nocomp_string);
  7082. init_request("asciify", asciify_macro);
  7083. init_request("backtrace", backtrace_request);
  7084. init_request("blm", blank_line_macro);
  7085. init_request("break", while_break_request);
  7086. init_request("cf", copy_file);
  7087. init_request("cflags", char_flags);
  7088. init_request("char", define_character);
  7089. init_request("chop", chop_macro);
  7090. init_request("close", close_request);
  7091. init_request("color", activate_color);
  7092. init_request("composite", composite_request);
  7093. init_request("continue", while_continue_request);
  7094. init_request("cp", compatible);
  7095. init_request("de", define_macro);
  7096. init_request("de1", define_nocomp_macro);
  7097. init_request("defcolor", define_color);
  7098. init_request("dei", define_indirect_macro);
  7099. init_request("dei1", define_indirect_nocomp_macro);
  7100. init_request("do", do_request);
  7101. init_request("ds", define_string);
  7102. init_request("ds1", define_nocomp_string);
  7103. init_request("ec", set_escape_char);
  7104. init_request("ecr", restore_escape_char);
  7105. init_request("ecs", save_escape_char);
  7106. init_request("el", else_request);
  7107. init_request("em", end_macro);
  7108. init_request("eo", escape_off);
  7109. init_request("ex", exit_request);
  7110. init_request("fchar", define_fallback_character);
  7111. #ifdef WIDOW_CONTROL
  7112. init_request("fpl", flush_pending_lines);
  7113. #endif /* WIDOW_CONTROL */
  7114. init_request("hcode", hyphenation_code);
  7115. init_request("hpfcode", hyphenation_patterns_file_code);
  7116. init_request("ie", if_else_request);
  7117. init_request("if", if_request);
  7118. init_request("ig", ignore);
  7119. init_request("length", length_request);
  7120. init_request("lf", line_file);
  7121. init_request("mso", macro_source);
  7122. init_request("nop", nop_request);
  7123. init_request("nroff", nroff_request);
  7124. init_request("nx", next_file);
  7125. init_request("open", open_request);
  7126. init_request("opena", opena_request);
  7127. init_request("output", output_request);
  7128. init_request("pc", set_page_character);
  7129. init_request("pi", pipe_output);
  7130. init_request("pm", print_macros);
  7131. init_request("psbb", ps_bbox_request);
  7132. #ifndef POPEN_MISSING
  7133. init_request("pso", pipe_source);
  7134. #endif /* not POPEN_MISSING */
  7135. init_request("rchar", remove_character);
  7136. init_request("rd", read_request);
  7137. init_request("return", return_macro_request);
  7138. init_request("rm", remove_macro);
  7139. init_request("rn", rename_macro);
  7140. init_request("schar", define_special_character);
  7141. init_request("shift", shift);
  7142. init_request("so", source);
  7143. init_request("spreadwarn", spreadwarn_request);
  7144. init_request("substring", substring_request);
  7145. init_request("sy", system_request);
  7146. init_request("tag", tag);
  7147. init_request("taga", taga);
  7148. init_request("tm", terminal);
  7149. init_request("tm1", terminal1);
  7150. init_request("tmc", terminal_continue);
  7151. init_request("tr", translate);
  7152. init_request("trf", transparent_file);
  7153. init_request("trin", translate_input);
  7154. init_request("trnt", translate_no_transparent);
  7155. init_request("troff", troff_request);
  7156. init_request("unformat", unformat_macro);
  7157. #ifdef COLUMN
  7158. init_request("vj", vjustify);
  7159. #endif /* COLUMN */
  7160. init_request("warn", warn_request);
  7161. init_request("warnscale", warnscale_request);
  7162. init_request("while", while_request);
  7163. init_request("write", write_request);
  7164. init_request("writec", write_request_continue);
  7165. init_request("writem", write_macro_request);
  7166. number_reg_dictionary.define(".$", new nargs_reg);
  7167. number_reg_dictionary.define(".C", new constant_int_reg(&compatible_flag));
  7168. number_reg_dictionary.define(".c", new lineno_reg);
  7169. number_reg_dictionary.define(".color", new constant_int_reg(&color_flag));
  7170. number_reg_dictionary.define(".F", new filename_reg);
  7171. number_reg_dictionary.define(".g", new constant_reg("1"));
  7172. number_reg_dictionary.define(".H", new constant_int_reg(&hresolution));
  7173. number_reg_dictionary.define(".R", new constant_reg("10000"));
  7174. number_reg_dictionary.define(".U", new constant_int_reg(&safer_flag));
  7175. number_reg_dictionary.define(".V", new constant_int_reg(&vresolution));
  7176. number_reg_dictionary.define(".warn", new constant_int_reg(&warning_mask));
  7177. extern const char *major_version;
  7178. number_reg_dictionary.define(".x", new constant_reg(major_version));
  7179. extern const char *revision;
  7180. number_reg_dictionary.define(".Y", new constant_reg(revision));
  7181. extern const char *minor_version;
  7182. number_reg_dictionary.define(".y", new constant_reg(minor_version));
  7183. number_reg_dictionary.define("c.", new writable_lineno_reg);
  7184. number_reg_dictionary.define("llx", new variable_reg(&llx_reg_contents));
  7185. number_reg_dictionary.define("lly", new variable_reg(&lly_reg_contents));
  7186. number_reg_dictionary.define("opmaxx",
  7187. new variable_reg(&output_reg_maxx_contents));
  7188. number_reg_dictionary.define("opmaxy",
  7189. new variable_reg(&output_reg_maxy_contents));
  7190. number_reg_dictionary.define("opminx",
  7191. new variable_reg(&output_reg_minx_contents));
  7192. number_reg_dictionary.define("opminy",
  7193. new variable_reg(&output_reg_miny_contents));
  7194. number_reg_dictionary.define("slimit",
  7195. new variable_reg(&input_stack::limit));
  7196. number_reg_dictionary.define("systat", new variable_reg(&system_status));
  7197. number_reg_dictionary.define("urx", new variable_reg(&urx_reg_contents));
  7198. number_reg_dictionary.define("ury", new variable_reg(&ury_reg_contents));
  7199. }
  7200. object_dictionary request_dictionary(501);
  7201. void init_request(const char *s, REQUEST_FUNCP f)
  7202. {
  7203. request_dictionary.define(s, new request(f));
  7204. }
  7205. static request_or_macro *lookup_request(symbol nm)
  7206. {
  7207. assert(!nm.is_null());
  7208. request_or_macro *p = (request_or_macro *)request_dictionary.lookup(nm);
  7209. if (p == 0) {
  7210. warning(WARN_MAC, "macro `%1' not defined", nm.contents());
  7211. p = new macro;
  7212. request_dictionary.define(nm, p);
  7213. }
  7214. return p;
  7215. }
  7216. node *charinfo_to_node_list(charinfo *ci, const environment *envp)
  7217. {
  7218. // Don't interpret character definitions in compatible mode.
  7219. int old_compatible_flag = compatible_flag;
  7220. compatible_flag = 0;
  7221. int old_escape_char = escape_char;
  7222. escape_char = '\\';
  7223. macro *mac = ci->set_macro(0);
  7224. assert(mac != 0);
  7225. environment *oldenv = curenv;
  7226. environment env(envp);
  7227. curenv = &env;
  7228. curenv->set_composite();
  7229. token old_tok = tok;
  7230. input_stack::add_boundary();
  7231. string_iterator *si =
  7232. new string_iterator(*mac, "composite character", ci->nm);
  7233. input_stack::push(si);
  7234. // we don't use process_input_stack, because we don't want to recognise
  7235. // requests
  7236. for (;;) {
  7237. tok.next();
  7238. if (tok.eof())
  7239. break;
  7240. if (tok.newline()) {
  7241. error("composite character mustn't contain newline");
  7242. while (!tok.eof())
  7243. tok.next();
  7244. break;
  7245. }
  7246. else
  7247. tok.process();
  7248. }
  7249. node *n = curenv->extract_output_line();
  7250. input_stack::remove_boundary();
  7251. ci->set_macro(mac);
  7252. tok = old_tok;
  7253. curenv = oldenv;
  7254. compatible_flag = old_compatible_flag;
  7255. escape_char = old_escape_char;
  7256. have_input = 0;
  7257. return n;
  7258. }
  7259. static node *read_draw_node()
  7260. {
  7261. token start;
  7262. start.next();
  7263. if (!start.delimiter(1)){
  7264. do {
  7265. tok.next();
  7266. } while (tok != start && !tok.newline() && !tok.eof());
  7267. }
  7268. else {
  7269. tok.next();
  7270. if (tok == start)
  7271. error("missing argument");
  7272. else {
  7273. unsigned char type = tok.ch();
  7274. if (type == 'F') {
  7275. read_color_draw_node(start);
  7276. return 0;
  7277. }
  7278. tok.next();
  7279. int maxpoints = 10;
  7280. hvpair *point = new hvpair[maxpoints];
  7281. int npoints = 0;
  7282. int no_last_v = 0;
  7283. int err = 0;
  7284. int i;
  7285. for (i = 0; tok != start; i++) {
  7286. if (i == maxpoints) {
  7287. hvpair *oldpoint = point;
  7288. point = new hvpair[maxpoints*2];
  7289. for (int j = 0; j < maxpoints; j++)
  7290. point[j] = oldpoint[j];
  7291. maxpoints *= 2;
  7292. a_delete oldpoint;
  7293. }
  7294. if (!get_hunits(&point[i].h,
  7295. type == 'f' || type == 't' ? 'u' : 'm')) {
  7296. err = 1;
  7297. break;
  7298. }
  7299. ++npoints;
  7300. tok.skip();
  7301. point[i].v = V0;
  7302. if (tok == start) {
  7303. no_last_v = 1;
  7304. break;
  7305. }
  7306. if (!get_vunits(&point[i].v, 'v')) {
  7307. err = 1;
  7308. break;
  7309. }
  7310. tok.skip();
  7311. }
  7312. while (tok != start && !tok.newline() && !tok.eof())
  7313. tok.next();
  7314. if (!err) {
  7315. switch (type) {
  7316. case 'l':
  7317. if (npoints != 1 || no_last_v) {
  7318. error("two arguments needed for line");
  7319. npoints = 1;
  7320. }
  7321. break;
  7322. case 'c':
  7323. if (npoints != 1 || !no_last_v) {
  7324. error("one argument needed for circle");
  7325. npoints = 1;
  7326. point[0].v = V0;
  7327. }
  7328. break;
  7329. case 'e':
  7330. if (npoints != 1 || no_last_v) {
  7331. error("two arguments needed for ellipse");
  7332. npoints = 1;
  7333. }
  7334. break;
  7335. case 'a':
  7336. if (npoints != 2 || no_last_v) {
  7337. error("four arguments needed for arc");
  7338. npoints = 2;
  7339. }
  7340. break;
  7341. case '~':
  7342. if (no_last_v)
  7343. error("even number of arguments needed for spline");
  7344. break;
  7345. case 'f':
  7346. if (npoints != 1 || !no_last_v) {
  7347. error("one argument needed for gray shade");
  7348. npoints = 1;
  7349. point[0].v = V0;
  7350. }
  7351. default:
  7352. // silently pass it through
  7353. break;
  7354. }
  7355. draw_node *dn = new draw_node(type, point, npoints,
  7356. curenv->get_font_size(),
  7357. curenv->get_glyph_color(),
  7358. curenv->get_fill_color());
  7359. a_delete point;
  7360. return dn;
  7361. }
  7362. else {
  7363. a_delete point;
  7364. }
  7365. }
  7366. }
  7367. return 0;
  7368. }
  7369. static void read_color_draw_node(token &start)
  7370. {
  7371. tok.next();
  7372. if (tok == start) {
  7373. error("missing color scheme");
  7374. return;
  7375. }
  7376. unsigned char scheme = tok.ch();
  7377. tok.next();
  7378. color *col = 0;
  7379. char end = start.ch();
  7380. switch (scheme) {
  7381. case 'c':
  7382. col = read_cmy(end);
  7383. break;
  7384. case 'd':
  7385. col = &default_color;
  7386. break;
  7387. case 'g':
  7388. col = read_gray(end);
  7389. break;
  7390. case 'k':
  7391. col = read_cmyk(end);
  7392. break;
  7393. case 'r':
  7394. col = read_rgb(end);
  7395. break;
  7396. }
  7397. if (col)
  7398. curenv->set_fill_color(col);
  7399. while (tok != start) {
  7400. if (tok.newline() || tok.eof()) {
  7401. warning(WARN_DELIM, "missing closing delimiter");
  7402. input_stack::push(make_temp_iterator("\n"));
  7403. break;
  7404. }
  7405. tok.next();
  7406. }
  7407. have_input = 1;
  7408. }
  7409. static struct {
  7410. const char *name;
  7411. int mask;
  7412. } warning_table[] = {
  7413. { "char", WARN_CHAR },
  7414. { "range", WARN_RANGE },
  7415. { "break", WARN_BREAK },
  7416. { "delim", WARN_DELIM },
  7417. { "el", WARN_EL },
  7418. { "scale", WARN_SCALE },
  7419. { "number", WARN_NUMBER },
  7420. { "syntax", WARN_SYNTAX },
  7421. { "tab", WARN_TAB },
  7422. { "right-brace", WARN_RIGHT_BRACE },
  7423. { "missing", WARN_MISSING },
  7424. { "input", WARN_INPUT },
  7425. { "escape", WARN_ESCAPE },
  7426. { "space", WARN_SPACE },
  7427. { "font", WARN_FONT },
  7428. { "di", WARN_DI },
  7429. { "mac", WARN_MAC },
  7430. { "reg", WARN_REG },
  7431. { "ig", WARN_IG },
  7432. { "color", WARN_COLOR },
  7433. { "all", WARN_TOTAL & ~(WARN_DI | WARN_MAC | WARN_REG) },
  7434. { "w", WARN_TOTAL },
  7435. { "default", DEFAULT_WARNING_MASK },
  7436. };
  7437. static int lookup_warning(const char *name)
  7438. {
  7439. for (unsigned int i = 0;
  7440. i < sizeof(warning_table)/sizeof(warning_table[0]);
  7441. i++)
  7442. if (strcmp(name, warning_table[i].name) == 0)
  7443. return warning_table[i].mask;
  7444. return 0;
  7445. }
  7446. static void enable_warning(const char *name)
  7447. {
  7448. int mask = lookup_warning(name);
  7449. if (mask)
  7450. warning_mask |= mask;
  7451. else
  7452. error("unknown warning `%1'", name);
  7453. }
  7454. static void disable_warning(const char *name)
  7455. {
  7456. int mask = lookup_warning(name);
  7457. if (mask)
  7458. warning_mask &= ~mask;
  7459. else
  7460. error("unknown warning `%1'", name);
  7461. }
  7462. static void copy_mode_error(const char *format,
  7463. const errarg &arg1,
  7464. const errarg &arg2,
  7465. const errarg &arg3)
  7466. {
  7467. if (ignoring) {
  7468. static const char prefix[] = "(in ignored input) ";
  7469. char *s = new char[sizeof(prefix) + strlen(format)];
  7470. strcpy(s, prefix);
  7471. strcat(s, format);
  7472. warning(WARN_IG, s, arg1, arg2, arg3);
  7473. a_delete s;
  7474. }
  7475. else
  7476. error(format, arg1, arg2, arg3);
  7477. }
  7478. enum error_type { WARNING, OUTPUT_WARNING, ERROR, FATAL };
  7479. static void do_error(error_type type,
  7480. const char *format,
  7481. const errarg &arg1,
  7482. const errarg &arg2,
  7483. const errarg &arg3)
  7484. {
  7485. const char *filename;
  7486. int lineno;
  7487. if (inhibit_errors && type < FATAL)
  7488. return;
  7489. if (backtrace_flag)
  7490. input_stack::backtrace();
  7491. if (!get_file_line(&filename, &lineno))
  7492. filename = 0;
  7493. if (filename)
  7494. errprint("%1:%2: ", filename, lineno);
  7495. else if (program_name)
  7496. fprintf(stderr, "%s: ", program_name);
  7497. switch (type) {
  7498. case FATAL:
  7499. fputs("fatal error: ", stderr);
  7500. break;
  7501. case ERROR:
  7502. break;
  7503. case WARNING:
  7504. fputs("warning: ", stderr);
  7505. break;
  7506. case OUTPUT_WARNING:
  7507. double fromtop = topdiv->get_vertical_position().to_units() / warn_scale;
  7508. fprintf(stderr, "warning [p %d, %.1f%c",
  7509. topdiv->get_page_number(), fromtop, warn_scaling_indicator);
  7510. if (topdiv != curdiv) {
  7511. double fromtop1 = curdiv->get_vertical_position().to_units()
  7512. / warn_scale;
  7513. fprintf(stderr, ", div `%s', %.1f%c",
  7514. curdiv->get_diversion_name(), fromtop1, warn_scaling_indicator);
  7515. }
  7516. fprintf(stderr, "]: ");
  7517. break;
  7518. }
  7519. errprint(format, arg1, arg2, arg3);
  7520. fputc('\n', stderr);
  7521. fflush(stderr);
  7522. if (type == FATAL)
  7523. cleanup_and_exit(1);
  7524. }
  7525. int warning(warning_type t,
  7526. const char *format,
  7527. const errarg &arg1,
  7528. const errarg &arg2,
  7529. const errarg &arg3)
  7530. {
  7531. if ((t & warning_mask) != 0) {
  7532. do_error(WARNING, format, arg1, arg2, arg3);
  7533. return 1;
  7534. }
  7535. else
  7536. return 0;
  7537. }
  7538. int output_warning(warning_type t,
  7539. const char *format,
  7540. const errarg &arg1,
  7541. const errarg &arg2,
  7542. const errarg &arg3)
  7543. {
  7544. if ((t & warning_mask) != 0) {
  7545. do_error(OUTPUT_WARNING, format, arg1, arg2, arg3);
  7546. return 1;
  7547. }
  7548. else
  7549. return 0;
  7550. }
  7551. void error(const char *format,
  7552. const errarg &arg1,
  7553. const errarg &arg2,
  7554. const errarg &arg3)
  7555. {
  7556. do_error(ERROR, format, arg1, arg2, arg3);
  7557. }
  7558. void fatal(const char *format,
  7559. const errarg &arg1,
  7560. const errarg &arg2,
  7561. const errarg &arg3)
  7562. {
  7563. do_error(FATAL, format, arg1, arg2, arg3);
  7564. }
  7565. void fatal_with_file_and_line(const char *filename, int lineno,
  7566. const char *format,
  7567. const errarg &arg1,
  7568. const errarg &arg2,
  7569. const errarg &arg3)
  7570. {
  7571. fprintf(stderr, "%s:%d: fatal error: ", filename, lineno);
  7572. errprint(format, arg1, arg2, arg3);
  7573. fputc('\n', stderr);
  7574. fflush(stderr);
  7575. cleanup_and_exit(1);
  7576. }
  7577. void error_with_file_and_line(const char *filename, int lineno,
  7578. const char *format,
  7579. const errarg &arg1,
  7580. const errarg &arg2,
  7581. const errarg &arg3)
  7582. {
  7583. fprintf(stderr, "%s:%d: error: ", filename, lineno);
  7584. errprint(format, arg1, arg2, arg3);
  7585. fputc('\n', stderr);
  7586. fflush(stderr);
  7587. }
  7588. dictionary charinfo_dictionary(501);
  7589. charinfo *get_charinfo(symbol nm)
  7590. {
  7591. void *p = charinfo_dictionary.lookup(nm);
  7592. if (p != 0)
  7593. return (charinfo *)p;
  7594. charinfo *cp = new charinfo(nm);
  7595. (void)charinfo_dictionary.lookup(nm, cp);
  7596. return cp;
  7597. }
  7598. int charinfo::next_index = 0;
  7599. charinfo::charinfo(symbol s)
  7600. : translation(0), mac(0), special_translation(TRANSLATE_NONE),
  7601. hyphenation_code(0), flags(0), ascii_code(0), asciify_code(0),
  7602. not_found(0), transparent_translate(1), translate_input(0),
  7603. mode(CHAR_NORMAL), nm(s)
  7604. {
  7605. index = next_index++;
  7606. }
  7607. void charinfo::set_hyphenation_code(unsigned char c)
  7608. {
  7609. hyphenation_code = c;
  7610. }
  7611. void charinfo::set_translation(charinfo *ci, int tt, int ti)
  7612. {
  7613. translation = ci;
  7614. if (ci && ti) {
  7615. if (hyphenation_code != 0)
  7616. ci->set_hyphenation_code(hyphenation_code);
  7617. if (asciify_code != 0)
  7618. ci->set_asciify_code(asciify_code);
  7619. else if (ascii_code != 0)
  7620. ci->set_asciify_code(ascii_code);
  7621. ci->set_translation_input();
  7622. }
  7623. special_translation = TRANSLATE_NONE;
  7624. transparent_translate = tt;
  7625. }
  7626. void charinfo::set_special_translation(int c, int tt)
  7627. {
  7628. special_translation = c;
  7629. translation = 0;
  7630. transparent_translate = tt;
  7631. }
  7632. void charinfo::set_ascii_code(unsigned char c)
  7633. {
  7634. ascii_code = c;
  7635. }
  7636. void charinfo::set_asciify_code(unsigned char c)
  7637. {
  7638. asciify_code = c;
  7639. }
  7640. macro *charinfo::set_macro(macro *m)
  7641. {
  7642. macro *tem = mac;
  7643. mac = m;
  7644. return tem;
  7645. }
  7646. macro *charinfo::setx_macro(macro *m, char_mode cm)
  7647. {
  7648. macro *tem = mac;
  7649. mac = m;
  7650. mode = cm;
  7651. return tem;
  7652. }
  7653. void charinfo::set_number(int n)
  7654. {
  7655. number = n;
  7656. flags |= NUMBERED;
  7657. }
  7658. int charinfo::get_number()
  7659. {
  7660. assert(flags & NUMBERED);
  7661. return number;
  7662. }
  7663. symbol UNNAMED_SYMBOL("---");
  7664. // For numbered characters not between 0 and 255, we make a symbol out
  7665. // of the number and store them in this dictionary.
  7666. dictionary numbered_charinfo_dictionary(11);
  7667. charinfo *get_charinfo_by_number(int n)
  7668. {
  7669. static charinfo *number_table[256];
  7670. if (n >= 0 && n < 256) {
  7671. charinfo *ci = number_table[n];
  7672. if (!ci) {
  7673. ci = new charinfo(UNNAMED_SYMBOL);
  7674. ci->set_number(n);
  7675. number_table[n] = ci;
  7676. }
  7677. return ci;
  7678. }
  7679. else {
  7680. symbol ns(i_to_a(n));
  7681. charinfo *ci = (charinfo *)numbered_charinfo_dictionary.lookup(ns);
  7682. if (!ci) {
  7683. ci = new charinfo(UNNAMED_SYMBOL);
  7684. ci->set_number(n);
  7685. (void)numbered_charinfo_dictionary.lookup(ns, ci);
  7686. }
  7687. return ci;
  7688. }
  7689. }
  7690. int font::name_to_index(const char *nm)
  7691. {
  7692. charinfo *ci;
  7693. if (nm[1] == 0)
  7694. ci = charset_table[nm[0] & 0xff];
  7695. else if (nm[0] == '\\' && nm[2] == 0)
  7696. ci = get_charinfo(symbol(nm + 1));
  7697. else
  7698. ci = get_charinfo(symbol(nm));
  7699. if (ci == 0)
  7700. return -1;
  7701. else
  7702. return ci->get_index();
  7703. }
  7704. int font::number_to_index(int n)
  7705. {
  7706. return get_charinfo_by_number(n)->get_index();
  7707. }