formatting
[s-roff.git] / src / roff / troff / input.cpp
blobd95ea4bda1640c098d59a8f9f42de0025d765650
1 // -*- C++ -*-
2 /* Copyright (C) 1989, 1990, 1991, 1992, 2000, 2001, 2002, 2003, 2004
3 Free Software Foundation, Inc.
4 Written by James Clark (jjc@jclark.com)
6 This file is part of groff.
8 groff is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
11 version.
13 groff is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License along
19 with groff; see the file COPYING. If not, write to the Free Software
20 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 #define DEBUGGING
24 #include "troff.h"
25 #include "dictionary.h"
26 #include "hvunits.h"
27 #include "stringclass.h"
28 #include "mtsm.h"
29 #include "env.h"
30 #include "request.h"
31 #include "node.h"
32 #include "token.h"
33 #include "div.h"
34 #include "reg.h"
35 #include "charinfo.h"
36 #include "macropath.h"
37 #include "input.h"
38 #include "defs.h"
39 #include "font.h"
40 #include "unicode.h"
42 // Needed for getpid() and isatty()
43 #include "posix.h"
45 #include "nonposix.h"
47 #ifdef NEED_DECLARATION_PUTENV
48 extern "C" {
49 int putenv(const char *);
51 #endif /* NEED_DECLARATION_PUTENV */
53 #define MACRO_PREFIX "tmac."
54 #define MACRO_POSTFIX ".tmac"
55 #define INITIAL_STARTUP_FILE "troffrc"
56 #define FINAL_STARTUP_FILE "troffrc-end"
57 #define DEFAULT_INPUT_STACK_LIMIT 1000
59 #ifndef DEFAULT_WARNING_MASK
60 // warnings that are enabled by default
61 #define DEFAULT_WARNING_MASK \
62 (WARN_CHAR|WARN_NUMBER|WARN_BREAK|WARN_SPACE|WARN_FONT)
63 #endif
65 // initial size of buffer for reading names; expanded as necessary
66 #define ABUF_SIZE 16
68 extern "C" const char *program_name;
69 extern "C" const char *Version_string;
71 #ifdef COLUMN
72 void init_column_requests();
73 #endif /* COLUMN */
75 static node *read_draw_node();
76 static void read_color_draw_node(token &);
77 static void push_token(const token &);
78 void copy_file();
79 #ifdef COLUMN
80 void vjustify();
81 #endif /* COLUMN */
82 void transparent_file();
84 token tok;
85 int break_flag = 0;
86 int color_flag = 1; // colors are on by default
87 static int backtrace_flag = 0;
88 #ifndef POPEN_MISSING
89 char *pipe_command = 0;
90 #endif
91 charinfo *charset_table[256];
92 unsigned char hpf_code_table[256];
94 static int warning_mask = DEFAULT_WARNING_MASK;
95 static int inhibit_errors = 0;
96 static int ignoring = 0;
98 static void enable_warning(const char *);
99 static void disable_warning(const char *);
101 static int escape_char = '\\';
102 static symbol end_macro_name;
103 static symbol blank_line_macro_name;
104 static int compatible_flag = 0;
105 int ascii_output_flag = 0;
106 int suppress_output_flag = 0;
107 int is_html = 0;
108 int begin_level = 0; // number of nested \O escapes
110 int have_input = 0; // whether \f, \F, \D'F...', \H, \m, \M,
111 // \R, \s, or \S has been processed in
112 // token::next()
113 int old_have_input = 0; // value of have_input right before \n
114 int tcommand_flag = 0;
115 int safer_flag = 1; // safer by default
117 int have_string_arg = 0; // whether we have \*[foo bar...]
119 double spread_limit = -3.0 - 1.0; // negative means deactivated
121 double warn_scale;
122 char warn_scaling_indicator;
123 int debug_state = 0; // turns on debugging of the html troff state
125 search_path *mac_path = &safer_macro_path;
127 // Defaults to the current directory.
128 search_path include_search_path(0, 0, 0, 1);
130 static int get_copy(node**, int = 0);
131 static void copy_mode_error(const char *,
132 const errarg & = empty_errarg,
133 const errarg & = empty_errarg,
134 const errarg & = empty_errarg);
136 enum read_mode { ALLOW_EMPTY, WITH_ARGS, NO_ARGS };
137 static symbol read_escape_name(read_mode mode = NO_ARGS);
138 static symbol read_long_escape_name(read_mode mode = NO_ARGS);
139 static void interpolate_string(symbol);
140 static void interpolate_string_with_args(symbol);
141 static void interpolate_macro(symbol);
142 static void interpolate_number_format(symbol);
143 static void interpolate_environment_variable(symbol);
145 static symbol composite_glyph_name(symbol);
146 static void interpolate_arg(symbol);
147 static request_or_macro *lookup_request(symbol);
148 static int get_delim_number(units *, unsigned char);
149 static int get_delim_number(units *, unsigned char, units);
150 static symbol do_get_long_name(int, char);
151 static int get_line_arg(units *res, unsigned char si, charinfo **cp);
152 static int read_size(int *);
153 static symbol get_delim_name();
154 static void init_registers();
155 static void trapping_blank_line();
157 class input_iterator;
158 input_iterator *make_temp_iterator(const char *);
159 const char *input_char_description(int);
161 void process_input_stack();
164 void set_escape_char()
166 if (has_arg()) {
167 if (tok.ch() == 0) {
168 error("bad escape character");
169 escape_char = '\\';
171 else
172 escape_char = tok.ch();
174 else
175 escape_char = '\\';
176 skip_line();
179 void escape_off()
181 escape_char = 0;
182 skip_line();
185 static int saved_escape_char = '\\';
187 void save_escape_char()
189 saved_escape_char = escape_char;
190 skip_line();
193 void restore_escape_char()
195 escape_char = saved_escape_char;
196 skip_line();
199 class input_iterator {
200 public:
201 input_iterator();
202 input_iterator(int is_div);
203 virtual ~input_iterator() {}
204 int get(node **);
205 friend class input_stack;
206 int is_diversion;
207 statem *diversion_state;
208 protected:
209 const unsigned char *ptr;
210 const unsigned char *eptr;
211 input_iterator *next;
212 private:
213 virtual int fill(node **);
214 virtual int peek();
215 virtual int has_args() { return 0; }
216 virtual int nargs() { return 0; }
217 virtual input_iterator *get_arg(int) { return 0; }
218 virtual int get_location(int, const char **, int *) { return 0; }
219 virtual void backtrace() {}
220 virtual int set_location(const char *, int) { return 0; }
221 virtual int next_file(FILE *, const char *) { return 0; }
222 virtual void shift(int) {}
223 virtual int is_boundary() {return 0; }
224 virtual int is_file() { return 0; }
225 virtual int is_macro() { return 0; }
226 virtual void save_compatible_flag(int) {}
227 virtual int get_compatible_flag() { return 0; }
230 input_iterator::input_iterator()
231 : is_diversion(0), ptr(0), eptr(0)
235 input_iterator::input_iterator(int is_div)
236 : is_diversion(is_div), ptr(0), eptr(0)
240 int input_iterator::fill(node **)
242 return EOF;
245 int input_iterator::peek()
247 return EOF;
250 inline int input_iterator::get(node **p)
252 return ptr < eptr ? *ptr++ : fill(p);
255 class input_boundary : public input_iterator {
256 public:
257 int is_boundary() { return 1; }
260 class input_return_boundary : public input_iterator {
261 public:
262 int is_boundary() { return 2; }
265 class file_iterator : public input_iterator {
266 FILE *fp;
267 int lineno;
268 const char *filename;
269 int popened;
270 int newline_flag;
271 int seen_escape;
272 enum { BUF_SIZE = 512 };
273 unsigned char buf[BUF_SIZE];
274 void close();
275 public:
276 file_iterator(FILE *, const char *, int = 0);
277 ~file_iterator();
278 int fill(node **);
279 int peek();
280 int get_location(int, const char **, int *);
281 void backtrace();
282 int set_location(const char *, int);
283 int next_file(FILE *, const char *);
284 int is_file();
287 file_iterator::file_iterator(FILE *f, const char *fn, int po)
288 : fp(f), lineno(1), filename(fn), popened(po),
289 newline_flag(0), seen_escape(0)
291 if ((font::use_charnames_in_special) && (fn != 0)) {
292 if (!the_output)
293 init_output();
294 the_output->put_filename(fn);
298 file_iterator::~file_iterator()
300 close();
303 void file_iterator::close()
305 if (fp == stdin)
306 clearerr(stdin);
307 #ifndef POPEN_MISSING
308 else if (popened)
309 pclose(fp);
310 #endif /* not POPEN_MISSING */
311 else
312 fclose(fp);
315 int file_iterator::is_file()
317 return 1;
320 int file_iterator::next_file(FILE *f, const char *s)
322 close();
323 filename = s;
324 fp = f;
325 lineno = 1;
326 newline_flag = 0;
327 seen_escape = 0;
328 popened = 0;
329 ptr = 0;
330 eptr = 0;
331 return 1;
334 int file_iterator::fill(node **)
336 if (newline_flag)
337 lineno++;
338 newline_flag = 0;
339 unsigned char *p = buf;
340 ptr = p;
341 unsigned char *e = p + BUF_SIZE;
342 while (p < e) {
343 int c = getc(fp);
344 if (c == EOF)
345 break;
346 if (invalid_input_char(c))
347 warning(WARN_INPUT, "invalid input character code %1", int(c));
348 else {
349 *p++ = c;
350 if (c == '\n') {
351 seen_escape = 0;
352 newline_flag = 1;
353 break;
355 seen_escape = (c == '\\');
358 if (p > buf) {
359 eptr = p;
360 return *ptr++;
362 else {
363 eptr = p;
364 return EOF;
368 int file_iterator::peek()
370 int c = getc(fp);
371 while (invalid_input_char(c)) {
372 warning(WARN_INPUT, "invalid input character code %1", int(c));
373 c = getc(fp);
375 if (c != EOF)
376 ungetc(c, fp);
377 return c;
380 int file_iterator::get_location(int /*allow_macro*/,
381 const char **filenamep, int *linenop)
383 *linenop = lineno;
384 if (filename != 0 && strcmp(filename, "-") == 0)
385 *filenamep = "<standard input>";
386 else
387 *filenamep = filename;
388 return 1;
391 void file_iterator::backtrace()
393 errprint("%1:%2: backtrace: %3 `%1'\n", filename, lineno,
394 popened ? "process" : "file");
397 int file_iterator::set_location(const char *f, int ln)
399 if (f) {
400 filename = f;
401 if (!the_output)
402 init_output();
403 the_output->put_filename(f);
405 lineno = ln;
406 return 1;
409 input_iterator nil_iterator;
411 class input_stack {
412 public:
413 static int get(node **);
414 static int peek();
415 static void push(input_iterator *);
416 static input_iterator *get_arg(int);
417 static int nargs();
418 static int get_location(int, const char **, int *);
419 static int set_location(const char *, int);
420 static void backtrace();
421 static void backtrace_all();
422 static void next_file(FILE *, const char *);
423 static void end_file();
424 static void shift(int n);
425 static void add_boundary();
426 static void add_return_boundary();
427 static int is_return_boundary();
428 static void remove_boundary();
429 static int get_level();
430 static int get_div_level();
431 static void increase_level();
432 static void decrease_level();
433 static void clear();
434 static void pop_macro();
435 static void save_compatible_flag(int);
436 static int get_compatible_flag();
437 static statem *get_diversion_state();
438 static void check_end_diversion(input_iterator *t);
439 static int limit;
440 static int div_level;
441 static statem *diversion_state;
442 private:
443 static input_iterator *top;
444 static int level;
445 static int finish_get(node **);
446 static int finish_peek();
449 input_iterator *input_stack::top = &nil_iterator;
450 int input_stack::level = 0;
451 int input_stack::limit = DEFAULT_INPUT_STACK_LIMIT;
452 int input_stack::div_level = 0;
453 statem *input_stack::diversion_state = NULL;
454 int suppress_push=0;
457 inline int input_stack::get_level()
459 return level;
462 inline void input_stack::increase_level()
464 level++;
467 inline void input_stack::decrease_level()
469 level--;
472 inline int input_stack::get_div_level()
474 return div_level;
477 inline int input_stack::get(node **np)
479 int res = (top->ptr < top->eptr) ? *top->ptr++ : finish_get(np);
480 if (res == '\n') {
481 old_have_input = have_input;
482 have_input = 0;
484 return res;
487 int input_stack::finish_get(node **np)
489 for (;;) {
490 int c = top->fill(np);
491 if (c != EOF || top->is_boundary())
492 return c;
493 if (top == &nil_iterator)
494 break;
495 input_iterator *tem = top;
496 check_end_diversion(tem);
497 #if defined(DEBUGGING)
498 if (debug_state)
499 if (tem->is_diversion)
500 fprintf(stderr,
501 "in diversion level = %d\n", input_stack::get_div_level());
502 #endif
503 top = top->next;
504 level--;
505 delete tem;
506 if (top->ptr < top->eptr)
507 return *top->ptr++;
509 assert(level == 0);
510 return EOF;
513 inline int input_stack::peek()
515 return (top->ptr < top->eptr) ? *top->ptr : finish_peek();
518 void input_stack::check_end_diversion(input_iterator *t)
520 if (t->is_diversion) {
521 div_level--;
522 diversion_state = t->diversion_state;
526 int input_stack::finish_peek()
528 for (;;) {
529 int c = top->peek();
530 if (c != EOF || top->is_boundary())
531 return c;
532 if (top == &nil_iterator)
533 break;
534 input_iterator *tem = top;
535 check_end_diversion(tem);
536 top = top->next;
537 level--;
538 delete tem;
539 if (top->ptr < top->eptr)
540 return *top->ptr;
542 assert(level == 0);
543 return EOF;
546 void input_stack::add_boundary()
548 push(new input_boundary);
551 void input_stack::add_return_boundary()
553 push(new input_return_boundary);
556 int input_stack::is_return_boundary()
558 return top->is_boundary() == 2;
561 void input_stack::remove_boundary()
563 assert(top->is_boundary());
564 input_iterator *temp = top->next;
565 check_end_diversion(top);
567 delete top;
568 top = temp;
569 level--;
572 void input_stack::push(input_iterator *in)
574 if (in == 0)
575 return;
576 if (++level > limit && limit > 0)
577 fatal("input stack limit exceeded (probable infinite loop)");
578 in->next = top;
579 top = in;
580 if (top->is_diversion) {
581 div_level++;
582 in->diversion_state = diversion_state;
583 diversion_state = curenv->construct_state(0);
584 #if defined(DEBUGGING)
585 if (debug_state) {
586 curenv->dump_troff_state();
587 fflush(stderr);
589 #endif
591 #if defined(DEBUGGING)
592 if (debug_state)
593 if (top->is_diversion) {
594 fprintf(stderr,
595 "in diversion level = %d\n", input_stack::get_div_level());
596 fflush(stderr);
598 #endif
601 statem *get_diversion_state()
603 return input_stack::get_diversion_state();
606 statem *input_stack::get_diversion_state()
608 if (diversion_state == NULL)
609 return NULL;
610 else
611 return new statem(diversion_state);
614 input_iterator *input_stack::get_arg(int i)
616 input_iterator *p;
617 for (p = top; p != 0; p = p->next)
618 if (p->has_args())
619 return p->get_arg(i);
620 return 0;
623 void input_stack::shift(int n)
625 for (input_iterator *p = top; p; p = p->next)
626 if (p->has_args()) {
627 p->shift(n);
628 return;
632 int input_stack::nargs()
634 for (input_iterator *p =top; p != 0; p = p->next)
635 if (p->has_args())
636 return p->nargs();
637 return 0;
640 int input_stack::get_location(int allow_macro, const char **filenamep, int *linenop)
642 for (input_iterator *p = top; p; p = p->next)
643 if (p->get_location(allow_macro, filenamep, linenop))
644 return 1;
645 return 0;
648 void input_stack::backtrace()
650 const char *f;
651 int n;
652 // only backtrace down to (not including) the topmost file
653 for (input_iterator *p = top;
654 p && !p->get_location(0, &f, &n);
655 p = p->next)
656 p->backtrace();
659 void input_stack::backtrace_all()
661 for (input_iterator *p = top; p; p = p->next)
662 p->backtrace();
665 int input_stack::set_location(const char *filename, int lineno)
667 for (input_iterator *p = top; p; p = p->next)
668 if (p->set_location(filename, lineno))
669 return 1;
670 return 0;
673 void input_stack::next_file(FILE *fp, const char *s)
675 input_iterator **pp;
676 for (pp = &top; *pp != &nil_iterator; pp = &(*pp)->next)
677 if ((*pp)->next_file(fp, s))
678 return;
679 if (++level > limit && limit > 0)
680 fatal("input stack limit exceeded");
681 *pp = new file_iterator(fp, s);
682 (*pp)->next = &nil_iterator;
685 void input_stack::end_file()
687 for (input_iterator **pp = &top; *pp != &nil_iterator; pp = &(*pp)->next)
688 if ((*pp)->is_file()) {
689 input_iterator *tem = *pp;
690 check_end_diversion(tem);
691 *pp = (*pp)->next;
692 delete tem;
693 level--;
694 return;
698 void input_stack::clear()
700 int nboundaries = 0;
701 while (top != &nil_iterator) {
702 if (top->is_boundary())
703 nboundaries++;
704 input_iterator *tem = top;
705 check_end_diversion(tem);
706 top = top->next;
707 level--;
708 delete tem;
710 // Keep while_request happy.
711 for (; nboundaries > 0; --nboundaries)
712 add_return_boundary();
715 void input_stack::pop_macro()
717 int nboundaries = 0;
718 int is_macro = 0;
719 do {
720 if (top->next == &nil_iterator)
721 break;
722 if (top->is_boundary())
723 nboundaries++;
724 is_macro = top->is_macro();
725 input_iterator *tem = top;
726 check_end_diversion(tem);
727 top = top->next;
728 level--;
729 delete tem;
730 } while (!is_macro);
731 // Keep while_request happy.
732 for (; nboundaries > 0; --nboundaries)
733 add_return_boundary();
736 inline void input_stack::save_compatible_flag(int f)
738 top->save_compatible_flag(f);
741 inline int input_stack::get_compatible_flag()
743 return top->get_compatible_flag();
746 void backtrace_request()
748 input_stack::backtrace_all();
749 fflush(stderr);
750 skip_line();
753 void next_file()
755 symbol nm = get_long_name();
756 while (!tok.newline() && !tok.eof())
757 tok.next();
758 if (nm.is_null())
759 input_stack::end_file();
760 else {
761 errno = 0;
762 FILE *fp = include_search_path.open_file_cautious(nm.contents());
763 if (!fp)
764 error("can't open `%1': %2", nm.contents(), strerror(errno));
765 else
766 input_stack::next_file(fp, nm.contents());
768 tok.next();
771 void shift()
773 int n;
774 if (!has_arg() || !get_integer(&n))
775 n = 1;
776 input_stack::shift(n);
777 skip_line();
780 static char get_char_for_escape_name(int allow_space = 0)
782 int c = get_copy(0);
783 switch (c) {
784 case EOF:
785 copy_mode_error("end of input in escape name");
786 return '\0';
787 default:
788 if (!invalid_input_char(c))
789 break;
790 // fall through
791 case '\n':
792 if (c == '\n')
793 input_stack::push(make_temp_iterator("\n"));
794 // fall through
795 case ' ':
796 if (c == ' ' && allow_space)
797 break;
798 // fall through
799 case '\t':
800 case '\001':
801 case '\b':
802 copy_mode_error("%1 is not allowed in an escape name",
803 input_char_description(c));
804 return '\0';
806 return c;
809 static symbol read_two_char_escape_name()
811 char buf[3];
812 buf[0] = get_char_for_escape_name();
813 if (buf[0] != '\0') {
814 buf[1] = get_char_for_escape_name();
815 if (buf[1] == '\0')
816 buf[0] = 0;
817 else
818 buf[2] = 0;
820 return symbol(buf);
823 static symbol read_long_escape_name(read_mode mode)
825 int start_level = input_stack::get_level();
826 char abuf[ABUF_SIZE];
827 char *buf = abuf;
828 int buf_size = ABUF_SIZE;
829 int i = 0;
830 char c;
831 int have_char = 0;
832 for (;;) {
833 c = get_char_for_escape_name(have_char && mode == WITH_ARGS);
834 if (c == 0) {
835 if (buf != abuf)
836 a_delete buf;
837 return NULL_SYMBOL;
839 have_char = 1;
840 if (mode == WITH_ARGS && c == ' ')
841 break;
842 if (i + 2 > buf_size) {
843 if (buf == abuf) {
844 buf = new char[ABUF_SIZE*2];
845 memcpy(buf, abuf, buf_size);
846 buf_size = ABUF_SIZE*2;
848 else {
849 char *old_buf = buf;
850 buf = new char[buf_size*2];
851 memcpy(buf, old_buf, buf_size);
852 buf_size *= 2;
853 a_delete old_buf;
856 if (c == ']' && input_stack::get_level() == start_level)
857 break;
858 buf[i++] = c;
860 buf[i] = 0;
861 if (c == ' ')
862 have_string_arg = 1;
863 if (buf == abuf) {
864 if (i == 0) {
865 if (mode != ALLOW_EMPTY)
866 copy_mode_error("empty escape name");
867 return EMPTY_SYMBOL;
869 return symbol(abuf);
871 else {
872 symbol s(buf);
873 a_delete buf;
874 return s;
878 static symbol read_escape_name(read_mode mode)
880 char c = get_char_for_escape_name();
881 if (c == 0)
882 return NULL_SYMBOL;
883 if (c == '(')
884 return read_two_char_escape_name();
885 if (c == '[' && !compatible_flag)
886 return read_long_escape_name(mode);
887 char buf[2];
888 buf[0] = c;
889 buf[1] = '\0';
890 return symbol(buf);
893 static symbol read_increment_and_escape_name(int *incp)
895 char c = get_char_for_escape_name();
896 switch (c) {
897 case 0:
898 *incp = 0;
899 return NULL_SYMBOL;
900 case '(':
901 *incp = 0;
902 return read_two_char_escape_name();
903 case '+':
904 *incp = 1;
905 return read_escape_name();
906 case '-':
907 *incp = -1;
908 return read_escape_name();
909 case '[':
910 if (!compatible_flag) {
911 *incp = 0;
912 return read_long_escape_name();
914 break;
916 *incp = 0;
917 char buf[2];
918 buf[0] = c;
919 buf[1] = '\0';
920 return symbol(buf);
923 static int get_copy(node **nd, int defining)
925 for (;;) {
926 int c = input_stack::get(nd);
927 if (c == PUSH_GROFF_MODE) {
928 input_stack::save_compatible_flag(compatible_flag);
929 compatible_flag = 0;
930 continue;
932 if (c == PUSH_COMP_MODE) {
933 input_stack::save_compatible_flag(compatible_flag);
934 compatible_flag = 1;
935 continue;
937 if (c == POP_GROFFCOMP_MODE) {
938 compatible_flag = input_stack::get_compatible_flag();
939 continue;
941 if (c == BEGIN_QUOTE) {
942 input_stack::increase_level();
943 continue;
945 if (c == END_QUOTE) {
946 input_stack::decrease_level();
947 continue;
949 if (c == ESCAPE_NEWLINE) {
950 if (defining)
951 return c;
952 do {
953 c = input_stack::get(nd);
954 } while (c == ESCAPE_NEWLINE);
956 if (c != escape_char || escape_char <= 0)
957 return c;
958 c = input_stack::peek();
959 switch(c) {
960 case 0:
961 return escape_char;
962 case '"':
963 (void)input_stack::get(0);
964 while ((c = input_stack::get(0)) != '\n' && c != EOF)
966 return c;
967 case '#': // Like \" but newline is ignored.
968 (void)input_stack::get(0);
969 while ((c = input_stack::get(0)) != '\n')
970 if (c == EOF)
971 return EOF;
972 break;
973 case '$':
975 (void)input_stack::get(0);
976 symbol s = read_escape_name();
977 if (!(s.is_null() || s.is_empty()))
978 interpolate_arg(s);
979 break;
981 case '*':
983 (void)input_stack::get(0);
984 symbol s = read_escape_name(WITH_ARGS);
985 if (!(s.is_null() || s.is_empty())) {
986 if (have_string_arg) {
987 have_string_arg = 0;
988 interpolate_string_with_args(s);
990 else
991 interpolate_string(s);
993 break;
995 case 'a':
996 (void)input_stack::get(0);
997 return '\001';
998 case 'e':
999 (void)input_stack::get(0);
1000 return ESCAPE_e;
1001 case 'E':
1002 (void)input_stack::get(0);
1003 return ESCAPE_E;
1004 case 'n':
1006 (void)input_stack::get(0);
1007 int inc;
1008 symbol s = read_increment_and_escape_name(&inc);
1009 if (!(s.is_null() || s.is_empty()))
1010 interpolate_number_reg(s, inc);
1011 break;
1013 case 'g':
1015 (void)input_stack::get(0);
1016 symbol s = read_escape_name();
1017 if (!(s.is_null() || s.is_empty()))
1018 interpolate_number_format(s);
1019 break;
1021 case 't':
1022 (void)input_stack::get(0);
1023 return '\t';
1024 case 'V':
1026 (void)input_stack::get(0);
1027 symbol s = read_escape_name();
1028 if (!(s.is_null() || s.is_empty()))
1029 interpolate_environment_variable(s);
1030 break;
1032 case '\n':
1033 (void)input_stack::get(0);
1034 if (defining)
1035 return ESCAPE_NEWLINE;
1036 break;
1037 case ' ':
1038 (void)input_stack::get(0);
1039 return ESCAPE_SPACE;
1040 case '~':
1041 (void)input_stack::get(0);
1042 return ESCAPE_TILDE;
1043 case ':':
1044 (void)input_stack::get(0);
1045 return ESCAPE_COLON;
1046 case '|':
1047 (void)input_stack::get(0);
1048 return ESCAPE_BAR;
1049 case '^':
1050 (void)input_stack::get(0);
1051 return ESCAPE_CIRCUMFLEX;
1052 case '{':
1053 (void)input_stack::get(0);
1054 return ESCAPE_LEFT_BRACE;
1055 case '}':
1056 (void)input_stack::get(0);
1057 return ESCAPE_RIGHT_BRACE;
1058 case '`':
1059 (void)input_stack::get(0);
1060 return ESCAPE_LEFT_QUOTE;
1061 case '\'':
1062 (void)input_stack::get(0);
1063 return ESCAPE_RIGHT_QUOTE;
1064 case '-':
1065 (void)input_stack::get(0);
1066 return ESCAPE_HYPHEN;
1067 case '_':
1068 (void)input_stack::get(0);
1069 return ESCAPE_UNDERSCORE;
1070 case 'c':
1071 (void)input_stack::get(0);
1072 return ESCAPE_c;
1073 case '!':
1074 (void)input_stack::get(0);
1075 return ESCAPE_BANG;
1076 case '?':
1077 (void)input_stack::get(0);
1078 return ESCAPE_QUESTION;
1079 case '&':
1080 (void)input_stack::get(0);
1081 return ESCAPE_AMPERSAND;
1082 case ')':
1083 (void)input_stack::get(0);
1084 return ESCAPE_RIGHT_PARENTHESIS;
1085 case '.':
1086 (void)input_stack::get(0);
1087 return c;
1088 case '%':
1089 (void)input_stack::get(0);
1090 return ESCAPE_PERCENT;
1091 default:
1092 if (c == escape_char) {
1093 (void)input_stack::get(0);
1094 return c;
1096 else
1097 return escape_char;
1102 class non_interpreted_char_node : public node {
1103 unsigned char c;
1104 public:
1105 non_interpreted_char_node(unsigned char);
1106 node *copy();
1107 int interpret(macro *);
1108 int same(node *);
1109 const char *type();
1110 int force_tprint();
1111 int is_tag();
1114 int non_interpreted_char_node::same(node *nd)
1116 return c == ((non_interpreted_char_node *)nd)->c;
1119 const char *non_interpreted_char_node::type()
1121 return "non_interpreted_char_node";
1124 int non_interpreted_char_node::force_tprint()
1126 return 0;
1129 int non_interpreted_char_node::is_tag()
1131 return 0;
1134 non_interpreted_char_node::non_interpreted_char_node(unsigned char n) : c(n)
1136 assert(n != 0);
1139 node *non_interpreted_char_node::copy()
1141 return new non_interpreted_char_node(c);
1144 int non_interpreted_char_node::interpret(macro *mac)
1146 mac->append(c);
1147 return 1;
1150 static void do_width();
1151 static node *do_non_interpreted();
1152 static node *do_special();
1153 static node *do_suppress(symbol nm);
1154 static void do_register();
1156 dictionary color_dictionary(501);
1158 static color *lookup_color(symbol nm)
1160 assert(!nm.is_null());
1161 if (nm == default_symbol)
1162 return &default_color;
1163 color *c = (color *)color_dictionary.lookup(nm);
1164 if (c == 0)
1165 warning(WARN_COLOR, "color `%1' not defined", nm.contents());
1166 return c;
1169 void do_glyph_color(symbol nm)
1171 if (nm.is_null())
1172 return;
1173 if (nm.is_empty())
1174 curenv->set_glyph_color(curenv->get_prev_glyph_color());
1175 else {
1176 color *tem = lookup_color(nm);
1177 if (tem)
1178 curenv->set_glyph_color(tem);
1179 else
1180 (void)color_dictionary.lookup(nm, new color(nm));
1184 void do_fill_color(symbol nm)
1186 if (nm.is_null())
1187 return;
1188 if (nm.is_empty())
1189 curenv->set_fill_color(curenv->get_prev_fill_color());
1190 else {
1191 color *tem = lookup_color(nm);
1192 if (tem)
1193 curenv->set_fill_color(tem);
1194 else
1195 (void)color_dictionary.lookup(nm, new color(nm));
1199 static unsigned int get_color_element(const char *scheme, const char *col)
1201 units val;
1202 if (!get_number(&val, 'f')) {
1203 warning(WARN_COLOR, "%1 in %2 definition set to 0", col, scheme);
1204 tok.next();
1205 return 0;
1207 if (val < 0) {
1208 warning(WARN_RANGE, "%1 cannot be negative: set to 0", col);
1209 return 0;
1211 if (val > color::MAX_COLOR_VAL+1) {
1212 warning(WARN_RANGE, "%1 cannot be greater than 1", col);
1213 // we change 0x10000 to 0xffff
1214 return color::MAX_COLOR_VAL;
1216 return (unsigned int)val;
1219 static color *read_rgb(char end = 0)
1221 symbol component = do_get_long_name(0, end);
1222 if (component.is_null()) {
1223 warning(WARN_COLOR, "missing rgb color values");
1224 return 0;
1226 const char *s = component.contents();
1227 color *col = new color;
1228 if (*s == '#') {
1229 if (!col->read_rgb(s)) {
1230 warning(WARN_COLOR, "expecting rgb color definition not `%1'", s);
1231 delete col;
1232 return 0;
1235 else {
1236 if (!end)
1237 input_stack::push(make_temp_iterator(" "));
1238 input_stack::push(make_temp_iterator(s));
1239 tok.next();
1240 unsigned int r = get_color_element("rgb color", "red component");
1241 unsigned int g = get_color_element("rgb color", "green component");
1242 unsigned int b = get_color_element("rgb color", "blue component");
1243 col->set_rgb(r, g, b);
1245 return col;
1248 static color *read_cmy(char end = 0)
1250 symbol component = do_get_long_name(0, end);
1251 if (component.is_null()) {
1252 warning(WARN_COLOR, "missing cmy color values");
1253 return 0;
1255 const char *s = component.contents();
1256 color *col = new color;
1257 if (*s == '#') {
1258 if (!col->read_cmy(s)) {
1259 warning(WARN_COLOR, "expecting cmy color definition not `%1'", s);
1260 delete col;
1261 return 0;
1264 else {
1265 if (!end)
1266 input_stack::push(make_temp_iterator(" "));
1267 input_stack::push(make_temp_iterator(s));
1268 tok.next();
1269 unsigned int c = get_color_element("cmy color", "cyan component");
1270 unsigned int m = get_color_element("cmy color", "magenta component");
1271 unsigned int y = get_color_element("cmy color", "yellow component");
1272 col->set_cmy(c, m, y);
1274 return col;
1277 static color *read_cmyk(char end = 0)
1279 symbol component = do_get_long_name(0, end);
1280 if (component.is_null()) {
1281 warning(WARN_COLOR, "missing cmyk color values");
1282 return 0;
1284 const char *s = component.contents();
1285 color *col = new color;
1286 if (*s == '#') {
1287 if (!col->read_cmyk(s)) {
1288 warning(WARN_COLOR, "`expecting a cmyk color definition not `%1'", s);
1289 delete col;
1290 return 0;
1293 else {
1294 if (!end)
1295 input_stack::push(make_temp_iterator(" "));
1296 input_stack::push(make_temp_iterator(s));
1297 tok.next();
1298 unsigned int c = get_color_element("cmyk color", "cyan component");
1299 unsigned int m = get_color_element("cmyk color", "magenta component");
1300 unsigned int y = get_color_element("cmyk color", "yellow component");
1301 unsigned int k = get_color_element("cmyk color", "black component");
1302 col->set_cmyk(c, m, y, k);
1304 return col;
1307 static color *read_gray(char end = 0)
1309 symbol component = do_get_long_name(0, end);
1310 if (component.is_null()) {
1311 warning(WARN_COLOR, "missing gray values");
1312 return 0;
1314 const char *s = component.contents();
1315 color *col = new color;
1316 if (*s == '#') {
1317 if (!col->read_gray(s)) {
1318 warning(WARN_COLOR, "`expecting a gray definition not `%1'", s);
1319 delete col;
1320 return 0;
1323 else {
1324 if (!end)
1325 input_stack::push(make_temp_iterator("\n"));
1326 input_stack::push(make_temp_iterator(s));
1327 tok.next();
1328 unsigned int g = get_color_element("gray", "gray value");
1329 col->set_gray(g);
1331 return col;
1334 static void activate_color()
1336 int n;
1337 if (has_arg() && get_integer(&n))
1338 color_flag = n != 0;
1339 else
1340 color_flag = 1;
1341 skip_line();
1344 static void define_color()
1346 symbol color_name = get_long_name(1);
1347 if (color_name.is_null()) {
1348 skip_line();
1349 return;
1351 if (color_name == default_symbol) {
1352 warning(WARN_COLOR, "default color can't be redefined");
1353 skip_line();
1354 return;
1356 symbol style = get_long_name(1);
1357 if (style.is_null()) {
1358 skip_line();
1359 return;
1361 color *col;
1362 if (strcmp(style.contents(), "rgb") == 0)
1363 col = read_rgb();
1364 else if (strcmp(style.contents(), "cmyk") == 0)
1365 col = read_cmyk();
1366 else if (strcmp(style.contents(), "gray") == 0)
1367 col = read_gray();
1368 else if (strcmp(style.contents(), "grey") == 0)
1369 col = read_gray();
1370 else if (strcmp(style.contents(), "cmy") == 0)
1371 col = read_cmy();
1372 else {
1373 warning(WARN_COLOR,
1374 "unknown color space `%1'; use rgb, cmyk, gray or cmy",
1375 style.contents());
1376 skip_line();
1377 return;
1379 if (col) {
1380 col->nm = color_name;
1381 (void)color_dictionary.lookup(color_name, col);
1383 skip_line();
1386 static node *do_overstrike()
1388 token start;
1389 overstrike_node *on = new overstrike_node;
1390 int start_level = input_stack::get_level();
1391 start.next();
1392 for (;;) {
1393 tok.next();
1394 if (tok.newline() || tok.eof()) {
1395 warning(WARN_DELIM, "missing closing delimiter");
1396 input_stack::push(make_temp_iterator("\n"));
1397 break;
1399 if (tok == start
1400 && (compatible_flag || input_stack::get_level() == start_level))
1401 break;
1402 charinfo *ci = tok.get_char(1);
1403 if (ci) {
1404 node *n = curenv->make_char_node(ci);
1405 if (n)
1406 on->overstrike(n);
1409 return on;
1412 static node *do_bracket()
1414 token start;
1415 bracket_node *bn = new bracket_node;
1416 start.next();
1417 int start_level = input_stack::get_level();
1418 for (;;) {
1419 tok.next();
1420 if (tok.eof()) {
1421 warning(WARN_DELIM, "missing closing delimiter");
1422 break;
1424 if (tok.newline()) {
1425 warning(WARN_DELIM, "missing closing delimiter");
1426 input_stack::push(make_temp_iterator("\n"));
1427 break;
1429 if (tok == start
1430 && (compatible_flag || input_stack::get_level() == start_level))
1431 break;
1432 charinfo *ci = tok.get_char(1);
1433 if (ci) {
1434 node *n = curenv->make_char_node(ci);
1435 if (n)
1436 bn->bracket(n);
1439 return bn;
1442 static int do_name_test()
1444 token start;
1445 start.next();
1446 int start_level = input_stack::get_level();
1447 int bad_char = 0;
1448 int some_char = 0;
1449 for (;;) {
1450 tok.next();
1451 if (tok.newline() || tok.eof()) {
1452 warning(WARN_DELIM, "missing closing delimiter");
1453 input_stack::push(make_temp_iterator("\n"));
1454 break;
1456 if (tok == start
1457 && (compatible_flag || input_stack::get_level() == start_level))
1458 break;
1459 if (!tok.ch())
1460 bad_char = 1;
1461 some_char = 1;
1463 return some_char && !bad_char;
1466 static int do_expr_test()
1468 token start;
1469 start.next();
1470 int start_level = input_stack::get_level();
1471 if (!start.delimiter(1))
1472 return 0;
1473 tok.next();
1474 // disable all warning and error messages temporarily
1475 int saved_warning_mask = warning_mask;
1476 int saved_inhibit_errors = inhibit_errors;
1477 warning_mask = 0;
1478 inhibit_errors = 1;
1479 int dummy;
1480 int result = get_number_rigidly(&dummy, 'u');
1481 warning_mask = saved_warning_mask;
1482 inhibit_errors = saved_inhibit_errors;
1483 if (tok == start && input_stack::get_level() == start_level)
1484 return result;
1485 // ignore everything up to the delimiter in case we aren't right there
1486 for (;;) {
1487 tok.next();
1488 if (tok.newline() || tok.eof()) {
1489 warning(WARN_DELIM, "missing closing delimiter");
1490 input_stack::push(make_temp_iterator("\n"));
1491 break;
1493 if (tok == start && input_stack::get_level() == start_level)
1494 break;
1496 return 0;
1499 #if 0
1500 static node *do_zero_width()
1502 token start;
1503 start.next();
1504 int start_level = input_stack::get_level();
1505 environment env(curenv);
1506 environment *oldenv = curenv;
1507 curenv = &env;
1508 for (;;) {
1509 tok.next();
1510 if (tok.newline() || tok.eof()) {
1511 error("missing closing delimiter");
1512 break;
1514 if (tok == start
1515 && (compatible_flag || input_stack::get_level() == start_level))
1516 break;
1517 tok.process();
1519 curenv = oldenv;
1520 node *rev = env.extract_output_line();
1521 node *n = 0;
1522 while (rev) {
1523 node *tem = rev;
1524 rev = rev->next;
1525 tem->next = n;
1526 n = tem;
1528 return new zero_width_node(n);
1531 #else
1533 // It's undesirable for \Z to change environments, because then
1534 // \n(.w won't work as expected.
1536 static node *do_zero_width()
1538 node *rev = new dummy_node;
1539 token start;
1540 start.next();
1541 int start_level = input_stack::get_level();
1542 for (;;) {
1543 tok.next();
1544 if (tok.newline() || tok.eof()) {
1545 warning(WARN_DELIM, "missing closing delimiter");
1546 input_stack::push(make_temp_iterator("\n"));
1547 break;
1549 if (tok == start
1550 && (compatible_flag || input_stack::get_level() == start_level))
1551 break;
1552 if (!tok.add_to_node_list(&rev))
1553 error("invalid token in argument to \\Z");
1555 node *n = 0;
1556 while (rev) {
1557 node *tem = rev;
1558 rev = rev->next;
1559 tem->next = n;
1560 n = tem;
1562 return new zero_width_node(n);
1565 #endif
1567 token_node *node::get_token_node()
1569 return 0;
1572 class token_node : public node {
1573 public:
1574 token tk;
1575 token_node(const token &t);
1576 node *copy();
1577 token_node *get_token_node();
1578 int same(node *);
1579 const char *type();
1580 int force_tprint();
1581 int is_tag();
1584 token_node::token_node(const token &t) : tk(t)
1588 node *token_node::copy()
1590 return new token_node(tk);
1593 token_node *token_node::get_token_node()
1595 return this;
1598 int token_node::same(node *nd)
1600 return tk == ((token_node *)nd)->tk;
1603 const char *token_node::type()
1605 return "token_node";
1608 int token_node::force_tprint()
1610 return 0;
1613 int token_node::is_tag()
1615 return 0;
1618 token::token() : nd(0), type(TOKEN_EMPTY)
1622 token::~token()
1624 delete nd;
1627 token::token(const token &t)
1628 : nm(t.nm), c(t.c), val(t.val), dim(t.dim), type(t.type)
1630 // Use two statements to work around bug in SGI C++.
1631 node *tem = t.nd;
1632 nd = tem ? tem->copy() : 0;
1635 void token::operator=(const token &t)
1637 delete nd;
1638 nm = t.nm;
1639 // Use two statements to work around bug in SGI C++.
1640 node *tem = t.nd;
1641 nd = tem ? tem->copy() : 0;
1642 c = t.c;
1643 val = t.val;
1644 dim = t.dim;
1645 type = t.type;
1648 void token::skip()
1650 while (space())
1651 next();
1654 int has_arg()
1656 while (tok.space())
1657 tok.next();
1658 return !tok.newline();
1661 void token::make_space()
1663 type = TOKEN_SPACE;
1666 void token::make_newline()
1668 type = TOKEN_NEWLINE;
1671 void token::next()
1673 if (nd) {
1674 delete nd;
1675 nd = 0;
1677 units x;
1678 for (;;) {
1679 node *n = 0;
1680 int cc = input_stack::get(&n);
1681 if (cc != escape_char || escape_char == 0) {
1682 handle_normal_char:
1683 switch(cc) {
1684 case PUSH_GROFF_MODE:
1685 input_stack::save_compatible_flag(compatible_flag);
1686 compatible_flag = 0;
1687 continue;
1688 case PUSH_COMP_MODE:
1689 input_stack::save_compatible_flag(compatible_flag);
1690 compatible_flag = 1;
1691 continue;
1692 case POP_GROFFCOMP_MODE:
1693 compatible_flag = input_stack::get_compatible_flag();
1694 continue;
1695 case BEGIN_QUOTE:
1696 input_stack::increase_level();
1697 continue;
1698 case END_QUOTE:
1699 input_stack::decrease_level();
1700 continue;
1701 case EOF:
1702 type = TOKEN_EOF;
1703 return;
1704 case TRANSPARENT_FILE_REQUEST:
1705 case TITLE_REQUEST:
1706 case COPY_FILE_REQUEST:
1707 #ifdef COLUMN
1708 case VJUSTIFY_REQUEST:
1709 #endif /* COLUMN */
1710 type = TOKEN_REQUEST;
1711 c = cc;
1712 return;
1713 case BEGIN_TRAP:
1714 type = TOKEN_BEGIN_TRAP;
1715 return;
1716 case END_TRAP:
1717 type = TOKEN_END_TRAP;
1718 return;
1719 case LAST_PAGE_EJECTOR:
1720 seen_last_page_ejector = 1;
1721 // fall through
1722 case PAGE_EJECTOR:
1723 type = TOKEN_PAGE_EJECTOR;
1724 return;
1725 case ESCAPE_PERCENT:
1726 ESCAPE_PERCENT:
1727 type = TOKEN_HYPHEN_INDICATOR;
1728 return;
1729 case ESCAPE_SPACE:
1730 ESCAPE_SPACE:
1731 type = TOKEN_UNSTRETCHABLE_SPACE;
1732 return;
1733 case ESCAPE_TILDE:
1734 ESCAPE_TILDE:
1735 type = TOKEN_STRETCHABLE_SPACE;
1736 return;
1737 case ESCAPE_COLON:
1738 ESCAPE_COLON:
1739 type = TOKEN_ZERO_WIDTH_BREAK;
1740 return;
1741 case ESCAPE_e:
1742 ESCAPE_e:
1743 type = TOKEN_ESCAPE;
1744 return;
1745 case ESCAPE_E:
1746 goto handle_escape_char;
1747 case ESCAPE_BAR:
1748 ESCAPE_BAR:
1749 type = TOKEN_NODE;
1750 nd = new hmotion_node(curenv->get_narrow_space_width(),
1751 curenv->get_fill_color());
1752 return;
1753 case ESCAPE_CIRCUMFLEX:
1754 ESCAPE_CIRCUMFLEX:
1755 type = TOKEN_NODE;
1756 nd = new hmotion_node(curenv->get_half_narrow_space_width(),
1757 curenv->get_fill_color());
1758 return;
1759 case ESCAPE_NEWLINE:
1760 have_input = 0;
1761 break;
1762 case ESCAPE_LEFT_BRACE:
1763 ESCAPE_LEFT_BRACE:
1764 type = TOKEN_LEFT_BRACE;
1765 return;
1766 case ESCAPE_RIGHT_BRACE:
1767 ESCAPE_RIGHT_BRACE:
1768 type = TOKEN_RIGHT_BRACE;
1769 return;
1770 case ESCAPE_LEFT_QUOTE:
1771 ESCAPE_LEFT_QUOTE:
1772 type = TOKEN_SPECIAL;
1773 nm = symbol("ga");
1774 return;
1775 case ESCAPE_RIGHT_QUOTE:
1776 ESCAPE_RIGHT_QUOTE:
1777 type = TOKEN_SPECIAL;
1778 nm = symbol("aa");
1779 return;
1780 case ESCAPE_HYPHEN:
1781 ESCAPE_HYPHEN:
1782 type = TOKEN_SPECIAL;
1783 nm = symbol("-");
1784 return;
1785 case ESCAPE_UNDERSCORE:
1786 ESCAPE_UNDERSCORE:
1787 type = TOKEN_SPECIAL;
1788 nm = symbol("ul");
1789 return;
1790 case ESCAPE_c:
1791 ESCAPE_c:
1792 type = TOKEN_INTERRUPT;
1793 return;
1794 case ESCAPE_BANG:
1795 ESCAPE_BANG:
1796 type = TOKEN_TRANSPARENT;
1797 return;
1798 case ESCAPE_QUESTION:
1799 ESCAPE_QUESTION:
1800 nd = do_non_interpreted();
1801 if (nd) {
1802 type = TOKEN_NODE;
1803 return;
1805 break;
1806 case ESCAPE_AMPERSAND:
1807 ESCAPE_AMPERSAND:
1808 type = TOKEN_DUMMY;
1809 return;
1810 case ESCAPE_RIGHT_PARENTHESIS:
1811 ESCAPE_RIGHT_PARENTHESIS:
1812 type = TOKEN_TRANSPARENT_DUMMY;
1813 return;
1814 case '\b':
1815 type = TOKEN_BACKSPACE;
1816 return;
1817 case ' ':
1818 type = TOKEN_SPACE;
1819 return;
1820 case '\t':
1821 type = TOKEN_TAB;
1822 return;
1823 case '\n':
1824 type = TOKEN_NEWLINE;
1825 return;
1826 case '\001':
1827 type = TOKEN_LEADER;
1828 return;
1829 case 0:
1831 assert(n != 0);
1832 token_node *tn = n->get_token_node();
1833 if (tn) {
1834 *this = tn->tk;
1835 delete tn;
1837 else {
1838 nd = n;
1839 type = TOKEN_NODE;
1842 return;
1843 default:
1844 type = TOKEN_CHAR;
1845 c = cc;
1846 return;
1849 else {
1850 handle_escape_char:
1851 cc = input_stack::get(&n);
1852 switch(cc) {
1853 case '(':
1854 nm = read_two_char_escape_name();
1855 type = TOKEN_SPECIAL;
1856 return;
1857 case EOF:
1858 type = TOKEN_EOF;
1859 error("end of input after escape character");
1860 return;
1861 case '`':
1862 goto ESCAPE_LEFT_QUOTE;
1863 case '\'':
1864 goto ESCAPE_RIGHT_QUOTE;
1865 case '-':
1866 goto ESCAPE_HYPHEN;
1867 case '_':
1868 goto ESCAPE_UNDERSCORE;
1869 case '%':
1870 goto ESCAPE_PERCENT;
1871 case ' ':
1872 goto ESCAPE_SPACE;
1873 case '0':
1874 nd = new hmotion_node(curenv->get_digit_width(),
1875 curenv->get_fill_color());
1876 type = TOKEN_NODE;
1877 return;
1878 case '|':
1879 goto ESCAPE_BAR;
1880 case '^':
1881 goto ESCAPE_CIRCUMFLEX;
1882 case '/':
1883 type = TOKEN_ITALIC_CORRECTION;
1884 return;
1885 case ',':
1886 type = TOKEN_NODE;
1887 nd = new left_italic_corrected_node;
1888 return;
1889 case '&':
1890 goto ESCAPE_AMPERSAND;
1891 case ')':
1892 goto ESCAPE_RIGHT_PARENTHESIS;
1893 case '!':
1894 goto ESCAPE_BANG;
1895 case '?':
1896 goto ESCAPE_QUESTION;
1897 case '~':
1898 goto ESCAPE_TILDE;
1899 case ':':
1900 goto ESCAPE_COLON;
1901 case '"':
1902 while ((cc = input_stack::get(0)) != '\n' && cc != EOF)
1904 if (cc == '\n')
1905 type = TOKEN_NEWLINE;
1906 else
1907 type = TOKEN_EOF;
1908 return;
1909 case '#': // Like \" but newline is ignored.
1910 while ((cc = input_stack::get(0)) != '\n')
1911 if (cc == EOF) {
1912 type = TOKEN_EOF;
1913 return;
1915 break;
1916 case '$':
1918 symbol s = read_escape_name();
1919 if (!(s.is_null() || s.is_empty()))
1920 interpolate_arg(s);
1921 break;
1923 case '*':
1925 symbol s = read_escape_name(WITH_ARGS);
1926 if (!(s.is_null() || s.is_empty())) {
1927 if (have_string_arg) {
1928 have_string_arg = 0;
1929 interpolate_string_with_args(s);
1931 else
1932 interpolate_string(s);
1934 break;
1936 case 'a':
1937 nd = new non_interpreted_char_node('\001');
1938 type = TOKEN_NODE;
1939 return;
1940 case 'A':
1941 c = '0' + do_name_test();
1942 type = TOKEN_CHAR;
1943 return;
1944 case 'b':
1945 nd = do_bracket();
1946 type = TOKEN_NODE;
1947 return;
1948 case 'B':
1949 c = '0' + do_expr_test();
1950 type = TOKEN_CHAR;
1951 return;
1952 case 'c':
1953 goto ESCAPE_c;
1954 case 'C':
1955 nm = get_delim_name();
1956 if (nm.is_null())
1957 break;
1958 type = TOKEN_SPECIAL;
1959 return;
1960 case 'd':
1961 type = TOKEN_NODE;
1962 nd = new vmotion_node(curenv->get_size() / 2,
1963 curenv->get_fill_color());
1964 return;
1965 case 'D':
1966 nd = read_draw_node();
1967 if (!nd)
1968 break;
1969 type = TOKEN_NODE;
1970 return;
1971 case 'e':
1972 goto ESCAPE_e;
1973 case 'E':
1974 goto handle_escape_char;
1975 case 'f':
1977 symbol s = read_escape_name(ALLOW_EMPTY);
1978 if (s.is_null())
1979 break;
1980 const char *p;
1981 for (p = s.contents(); *p != '\0'; p++)
1982 if (!csdigit(*p))
1983 break;
1984 if (*p || s.is_empty())
1985 curenv->set_font(s);
1986 else
1987 curenv->set_font(atoi(s.contents()));
1988 if (!compatible_flag)
1989 have_input = 1;
1990 break;
1992 case 'F':
1994 symbol s = read_escape_name(ALLOW_EMPTY);
1995 if (s.is_null())
1996 break;
1997 curenv->set_family(s);
1998 have_input = 1;
1999 break;
2001 case 'g':
2003 symbol s = read_escape_name();
2004 if (!(s.is_null() || s.is_empty()))
2005 interpolate_number_format(s);
2006 break;
2008 case 'h':
2009 if (!get_delim_number(&x, 'm'))
2010 break;
2011 type = TOKEN_NODE;
2012 nd = new hmotion_node(x, curenv->get_fill_color());
2013 return;
2014 case 'H':
2015 // don't take height increments relative to previous height if
2016 // in compatibility mode
2017 if (!compatible_flag && curenv->get_char_height())
2019 if (get_delim_number(&x, 'z', curenv->get_char_height()))
2020 curenv->set_char_height(x);
2022 else
2024 if (get_delim_number(&x, 'z', curenv->get_requested_point_size()))
2025 curenv->set_char_height(x);
2027 if (!compatible_flag)
2028 have_input = 1;
2029 break;
2030 case 'k':
2031 nm = read_escape_name();
2032 if (nm.is_null() || nm.is_empty())
2033 break;
2034 type = TOKEN_MARK_INPUT;
2035 return;
2036 case 'l':
2037 case 'L':
2039 charinfo *s = 0;
2040 if (!get_line_arg(&x, (cc == 'l' ? 'm': 'v'), &s))
2041 break;
2042 if (s == 0)
2043 s = get_charinfo(cc == 'l' ? "ru" : "br");
2044 type = TOKEN_NODE;
2045 node *char_node = curenv->make_char_node(s);
2046 if (cc == 'l')
2047 nd = new hline_node(x, char_node);
2048 else
2049 nd = new vline_node(x, char_node);
2050 return;
2052 case 'm':
2053 do_glyph_color(read_escape_name(ALLOW_EMPTY));
2054 if (!compatible_flag)
2055 have_input = 1;
2056 break;
2057 case 'M':
2058 do_fill_color(read_escape_name(ALLOW_EMPTY));
2059 if (!compatible_flag)
2060 have_input = 1;
2061 break;
2062 case 'n':
2064 int inc;
2065 symbol s = read_increment_and_escape_name(&inc);
2066 if (!(s.is_null() || s.is_empty()))
2067 interpolate_number_reg(s, inc);
2068 break;
2070 case 'N':
2071 if (!get_delim_number(&val, 0))
2072 break;
2073 type = TOKEN_NUMBERED_CHAR;
2074 return;
2075 case 'o':
2076 nd = do_overstrike();
2077 type = TOKEN_NODE;
2078 return;
2079 case 'O':
2080 nd = do_suppress(read_escape_name());
2081 if (!nd)
2082 break;
2083 type = TOKEN_NODE;
2084 return;
2085 case 'p':
2086 type = TOKEN_SPREAD;
2087 return;
2088 case 'r':
2089 type = TOKEN_NODE;
2090 nd = new vmotion_node(-curenv->get_size(), curenv->get_fill_color());
2091 return;
2092 case 'R':
2093 do_register();
2094 if (!compatible_flag)
2095 have_input = 1;
2096 break;
2097 case 's':
2098 if (read_size(&x))
2099 curenv->set_size(x);
2100 if (!compatible_flag)
2101 have_input = 1;
2102 break;
2103 case 'S':
2104 if (get_delim_number(&x, 0))
2105 curenv->set_char_slant(x);
2106 if (!compatible_flag)
2107 have_input = 1;
2108 break;
2109 case 't':
2110 type = TOKEN_NODE;
2111 nd = new non_interpreted_char_node('\t');
2112 return;
2113 case 'u':
2114 type = TOKEN_NODE;
2115 nd = new vmotion_node(-curenv->get_size() / 2,
2116 curenv->get_fill_color());
2117 return;
2118 case 'v':
2119 if (!get_delim_number(&x, 'v'))
2120 break;
2121 type = TOKEN_NODE;
2122 nd = new vmotion_node(x, curenv->get_fill_color());
2123 return;
2124 case 'V':
2126 symbol s = read_escape_name();
2127 if (!(s.is_null() || s.is_empty()))
2128 interpolate_environment_variable(s);
2129 break;
2131 case 'w':
2132 do_width();
2133 break;
2134 case 'x':
2135 if (!get_delim_number(&x, 'v'))
2136 break;
2137 type = TOKEN_NODE;
2138 nd = new extra_size_node(x);
2139 return;
2140 case 'X':
2141 nd = do_special();
2142 if (!nd)
2143 break;
2144 type = TOKEN_NODE;
2145 return;
2146 case 'Y':
2148 symbol s = read_escape_name();
2149 if (s.is_null() || s.is_empty())
2150 break;
2151 request_or_macro *p = lookup_request(s);
2152 macro *m = p->to_macro();
2153 if (!m) {
2154 error("can't transparently throughput a request");
2155 break;
2157 nd = new special_node(*m);
2158 type = TOKEN_NODE;
2159 return;
2161 case 'z':
2163 next();
2164 if (type == TOKEN_NODE)
2165 nd = new zero_width_node(nd);
2166 else {
2167 charinfo *ci = get_char(1);
2168 if (ci == 0)
2169 break;
2170 node *gn = curenv->make_char_node(ci);
2171 if (gn == 0)
2172 break;
2173 nd = new zero_width_node(gn);
2174 type = TOKEN_NODE;
2176 return;
2178 case 'Z':
2179 nd = do_zero_width();
2180 if (nd == 0)
2181 break;
2182 type = TOKEN_NODE;
2183 return;
2184 case '{':
2185 goto ESCAPE_LEFT_BRACE;
2186 case '}':
2187 goto ESCAPE_RIGHT_BRACE;
2188 case '\n':
2189 break;
2190 case '[':
2191 if (!compatible_flag) {
2192 symbol s = read_long_escape_name(WITH_ARGS);
2193 if (s.is_null() || s.is_empty())
2194 break;
2195 if (have_string_arg) {
2196 have_string_arg = 0;
2197 nm = composite_glyph_name(s);
2199 else {
2200 const char *gn = check_unicode_name(s.contents());
2201 if (gn) {
2202 const char *gn_decomposed = decompose_unicode(gn);
2203 if (gn_decomposed)
2204 gn = &gn_decomposed[1];
2205 const char *groff_gn = unicode_to_glyph_name(gn);
2206 if (groff_gn)
2207 nm = symbol(groff_gn);
2208 else {
2209 char *buf = new char[strlen(gn) + 1 + 1];
2210 strcpy(buf, "u");
2211 strcat(buf, gn);
2212 nm = symbol(buf);
2213 a_delete buf;
2216 else
2217 nm = symbol(s.contents());
2219 type = TOKEN_SPECIAL;
2220 return;
2222 goto handle_normal_char;
2223 default:
2224 if (cc != escape_char && cc != '.')
2225 warning(WARN_ESCAPE, "escape character ignored before %1",
2226 input_char_description(cc));
2227 goto handle_normal_char;
2233 int token::operator==(const token &t)
2235 if (type != t.type)
2236 return 0;
2237 switch(type) {
2238 case TOKEN_CHAR:
2239 return c == t.c;
2240 case TOKEN_SPECIAL:
2241 return nm == t.nm;
2242 case TOKEN_NUMBERED_CHAR:
2243 return val == t.val;
2244 default:
2245 return 1;
2249 int token::operator!=(const token &t)
2251 return !(*this == t);
2254 // is token a suitable delimiter (like ')?
2256 int token::delimiter(int err)
2258 switch(type) {
2259 case TOKEN_CHAR:
2260 switch(c) {
2261 case '0':
2262 case '1':
2263 case '2':
2264 case '3':
2265 case '4':
2266 case '5':
2267 case '6':
2268 case '7':
2269 case '8':
2270 case '9':
2271 case '+':
2272 case '-':
2273 case '/':
2274 case '*':
2275 case '%':
2276 case '<':
2277 case '>':
2278 case '=':
2279 case '&':
2280 case ':':
2281 case '(':
2282 case ')':
2283 case '.':
2284 if (err)
2285 error("cannot use character `%1' as a starting delimiter", char(c));
2286 return 0;
2287 default:
2288 return 1;
2290 case TOKEN_NODE:
2291 case TOKEN_SPACE:
2292 case TOKEN_STRETCHABLE_SPACE:
2293 case TOKEN_UNSTRETCHABLE_SPACE:
2294 case TOKEN_TAB:
2295 case TOKEN_NEWLINE:
2296 if (err)
2297 error("cannot use %1 as a starting delimiter", description());
2298 return 0;
2299 default:
2300 return 1;
2304 const char *token::description()
2306 static char buf[4];
2307 switch (type) {
2308 case TOKEN_BACKSPACE:
2309 return "a backspace character";
2310 case TOKEN_CHAR:
2311 buf[0] = '`';
2312 buf[1] = c;
2313 buf[2] = '\'';
2314 buf[3] = '\0';
2315 return buf;
2316 case TOKEN_DUMMY:
2317 return "`\\&'";
2318 case TOKEN_ESCAPE:
2319 return "`\\e'";
2320 case TOKEN_HYPHEN_INDICATOR:
2321 return "`\\%'";
2322 case TOKEN_INTERRUPT:
2323 return "`\\c'";
2324 case TOKEN_ITALIC_CORRECTION:
2325 return "`\\/'";
2326 case TOKEN_LEADER:
2327 return "a leader character";
2328 case TOKEN_LEFT_BRACE:
2329 return "`\\{'";
2330 case TOKEN_MARK_INPUT:
2331 return "`\\k'";
2332 case TOKEN_NEWLINE:
2333 return "newline";
2334 case TOKEN_NODE:
2335 return "a node";
2336 case TOKEN_NUMBERED_CHAR:
2337 return "`\\N'";
2338 case TOKEN_RIGHT_BRACE:
2339 return "`\\}'";
2340 case TOKEN_SPACE:
2341 return "a space";
2342 case TOKEN_SPECIAL:
2343 return "a special character";
2344 case TOKEN_SPREAD:
2345 return "`\\p'";
2346 case TOKEN_STRETCHABLE_SPACE:
2347 return "`\\~'";
2348 case TOKEN_UNSTRETCHABLE_SPACE:
2349 return "`\\ '";
2350 case TOKEN_TAB:
2351 return "a tab character";
2352 case TOKEN_TRANSPARENT:
2353 return "`\\!'";
2354 case TOKEN_TRANSPARENT_DUMMY:
2355 return "`\\)'";
2356 case TOKEN_ZERO_WIDTH_BREAK:
2357 return "`\\:'";
2358 case TOKEN_EOF:
2359 return "end of input";
2360 default:
2361 break;
2363 return "a magic token";
2366 void skip_line()
2368 while (!tok.newline())
2369 if (tok.eof())
2370 return;
2371 else
2372 tok.next();
2373 tok.next();
2376 void compatible()
2378 int n;
2379 if (has_arg() && get_integer(&n))
2380 compatible_flag = n != 0;
2381 else
2382 compatible_flag = 1;
2383 skip_line();
2386 static void empty_name_warning(int required)
2388 if (tok.newline() || tok.eof()) {
2389 if (required)
2390 warning(WARN_MISSING, "missing name");
2392 else if (tok.right_brace() || tok.tab()) {
2393 const char *start = tok.description();
2394 do {
2395 tok.next();
2396 } while (tok.space() || tok.right_brace() || tok.tab());
2397 if (!tok.newline() && !tok.eof())
2398 error("%1 is not allowed before an argument", start);
2399 else if (required)
2400 warning(WARN_MISSING, "missing name");
2402 else if (required)
2403 error("name expected (got %1)", tok.description());
2404 else
2405 error("name expected (got %1): treated as missing", tok.description());
2408 static void non_empty_name_warning()
2410 if (!tok.newline() && !tok.eof() && !tok.space() && !tok.tab()
2411 && !tok.right_brace()
2412 // We don't want to give a warning for .el\{
2413 && !tok.left_brace())
2414 error("%1 is not allowed in a name", tok.description());
2417 symbol get_name(int required)
2419 if (compatible_flag) {
2420 char buf[3];
2421 tok.skip();
2422 if ((buf[0] = tok.ch()) != 0) {
2423 tok.next();
2424 if ((buf[1] = tok.ch()) != 0) {
2425 buf[2] = 0;
2426 tok.make_space();
2428 else
2429 non_empty_name_warning();
2430 return symbol(buf);
2432 else {
2433 empty_name_warning(required);
2434 return NULL_SYMBOL;
2437 else
2438 return get_long_name(required);
2441 symbol get_long_name(int required)
2443 return do_get_long_name(required, 0);
2446 static symbol do_get_long_name(int required, char end)
2448 while (tok.space())
2449 tok.next();
2450 char abuf[ABUF_SIZE];
2451 char *buf = abuf;
2452 int buf_size = ABUF_SIZE;
2453 int i = 0;
2454 for (;;) {
2455 // If end != 0 we normally have to append a null byte
2456 if (i + 2 > buf_size) {
2457 if (buf == abuf) {
2458 buf = new char[ABUF_SIZE*2];
2459 memcpy(buf, abuf, buf_size);
2460 buf_size = ABUF_SIZE*2;
2462 else {
2463 char *old_buf = buf;
2464 buf = new char[buf_size*2];
2465 memcpy(buf, old_buf, buf_size);
2466 buf_size *= 2;
2467 a_delete old_buf;
2470 if ((buf[i] = tok.ch()) == 0 || buf[i] == end)
2471 break;
2472 i++;
2473 tok.next();
2475 if (i == 0) {
2476 empty_name_warning(required);
2477 return NULL_SYMBOL;
2479 if (end && buf[i] == end)
2480 buf[i+1] = '\0';
2481 else
2482 non_empty_name_warning();
2483 if (buf == abuf)
2484 return symbol(buf);
2485 else {
2486 symbol s(buf);
2487 a_delete buf;
2488 return s;
2492 void exit_troff()
2494 exit_started = 1;
2495 topdiv->set_last_page();
2496 if (!end_macro_name.is_null()) {
2497 spring_trap(end_macro_name);
2498 tok.next();
2499 process_input_stack();
2501 curenv->final_break();
2502 tok.next();
2503 process_input_stack();
2504 end_diversions();
2505 if (topdiv->get_page_length() > 0) {
2506 done_end_macro = 1;
2507 topdiv->set_ejecting();
2508 static unsigned char buf[2] = { LAST_PAGE_EJECTOR, '\0' };
2509 input_stack::push(make_temp_iterator((char *)buf));
2510 topdiv->space(topdiv->get_page_length(), 1);
2511 tok.next();
2512 process_input_stack();
2513 seen_last_page_ejector = 1; // should be set already
2514 topdiv->set_ejecting();
2515 push_page_ejector();
2516 topdiv->space(topdiv->get_page_length(), 1);
2517 tok.next();
2518 process_input_stack();
2520 // This will only happen if a trap-invoked macro starts a diversion,
2521 // or if vertical position traps have been disabled.
2522 cleanup_and_exit(0);
2525 // This implements .ex. The input stack must be cleared before calling
2526 // exit_troff().
2528 void exit_request()
2530 input_stack::clear();
2531 if (exit_started)
2532 tok.next();
2533 else
2534 exit_troff();
2537 void return_macro_request()
2539 if (has_arg() && tok.ch())
2540 input_stack::pop_macro();
2541 input_stack::pop_macro();
2542 tok.next();
2545 void end_macro()
2547 end_macro_name = get_name();
2548 skip_line();
2551 void blank_line_macro()
2553 blank_line_macro_name = get_name();
2554 skip_line();
2557 static void trapping_blank_line()
2559 if (!blank_line_macro_name.is_null())
2560 spring_trap(blank_line_macro_name);
2561 else
2562 blank_line();
2565 void do_request()
2567 int old_compatible_flag = compatible_flag;
2568 compatible_flag = 0;
2569 symbol nm = get_name();
2570 if (nm.is_null())
2571 skip_line();
2572 else
2573 interpolate_macro(nm);
2574 compatible_flag = old_compatible_flag;
2577 inline int possibly_handle_first_page_transition()
2579 if (topdiv->before_first_page && curdiv == topdiv && !curenv->is_dummy()) {
2580 handle_first_page_transition();
2581 return 1;
2583 else
2584 return 0;
2587 static int transparent_translate(int cc)
2589 if (!invalid_input_char(cc)) {
2590 charinfo *ci = charset_table[cc];
2591 switch (ci->get_special_translation(1)) {
2592 case charinfo::TRANSLATE_SPACE:
2593 return ' ';
2594 case charinfo::TRANSLATE_STRETCHABLE_SPACE:
2595 return ESCAPE_TILDE;
2596 case charinfo::TRANSLATE_DUMMY:
2597 return ESCAPE_AMPERSAND;
2598 case charinfo::TRANSLATE_HYPHEN_INDICATOR:
2599 return ESCAPE_PERCENT;
2601 // This is really ugly.
2602 ci = ci->get_translation(1);
2603 if (ci) {
2604 int c = ci->get_ascii_code();
2605 if (c != '\0')
2606 return c;
2607 error("can't translate %1 to special character `%2'"
2608 " in transparent throughput",
2609 input_char_description(cc),
2610 ci->nm.contents());
2613 return cc;
2616 class int_stack {
2617 struct int_stack_element {
2618 int n;
2619 int_stack_element *next;
2620 } *top;
2621 public:
2622 int_stack();
2623 ~int_stack();
2624 void push(int);
2625 int is_empty();
2626 int pop();
2629 int_stack::int_stack()
2631 top = 0;
2634 int_stack::~int_stack()
2636 while (top != 0) {
2637 int_stack_element *temp = top;
2638 top = top->next;
2639 delete temp;
2643 int int_stack::is_empty()
2645 return top == 0;
2648 void int_stack::push(int n)
2650 int_stack_element *p = new int_stack_element;
2651 p->next = top;
2652 p->n = n;
2653 top = p;
2656 int int_stack::pop()
2658 assert(top != 0);
2659 int_stack_element *p = top;
2660 top = top->next;
2661 int n = p->n;
2662 delete p;
2663 return n;
2666 int node::reread(int *)
2668 return 0;
2671 int global_diverted_space = 0;
2673 int diverted_space_node::reread(int *bolp)
2675 global_diverted_space = 1;
2676 if (curenv->get_fill())
2677 trapping_blank_line();
2678 else
2679 curdiv->space(n);
2680 global_diverted_space = 0;
2681 *bolp = 1;
2682 return 1;
2685 int diverted_copy_file_node::reread(int *bolp)
2687 curdiv->copy_file(filename.contents());
2688 *bolp = 1;
2689 return 1;
2692 int word_space_node::reread(int *)
2694 if (unformat) {
2695 for (width_list *w = orig_width; w; w = w->next)
2696 curenv->space(w->width, w->sentence_width);
2697 unformat = 0;
2698 return 1;
2700 return 0;
2703 int unbreakable_space_node::reread(int *)
2705 return 0;
2708 int hmotion_node::reread(int *)
2710 if (unformat && was_tab) {
2711 curenv->handle_tab(0);
2712 unformat = 0;
2713 return 1;
2715 return 0;
2718 void process_input_stack()
2720 int_stack trap_bol_stack;
2721 int bol = 1;
2722 for (;;) {
2723 int suppress_next = 0;
2724 switch (tok.type) {
2725 case token::TOKEN_CHAR:
2727 unsigned char ch = tok.c;
2728 if (bol && !have_input
2729 && (ch == curenv->control_char
2730 || ch == curenv->no_break_control_char)) {
2731 break_flag = ch == curenv->control_char;
2732 // skip tabs as well as spaces here
2733 do {
2734 tok.next();
2735 } while (tok.white_space());
2736 symbol nm = get_name();
2737 #if defined(DEBUGGING)
2738 if (debug_state) {
2739 if (! nm.is_null()) {
2740 if (strcmp(nm.contents(), "test") == 0) {
2741 fprintf(stderr, "found it!\n");
2742 fflush(stderr);
2744 fprintf(stderr, "interpreting [%s]", nm.contents());
2745 if (strcmp(nm.contents(), "di") == 0 && topdiv != curdiv)
2746 fprintf(stderr, " currently in diversion: %s",
2747 curdiv->get_diversion_name());
2748 fprintf(stderr, "\n");
2749 fflush(stderr);
2752 #endif
2753 if (nm.is_null())
2754 skip_line();
2755 else {
2756 interpolate_macro(nm);
2757 #if defined(DEBUGGING)
2758 if (debug_state) {
2759 fprintf(stderr, "finished interpreting [%s] and environment state is\n", nm.contents());
2760 curenv->dump_troff_state();
2762 #endif
2764 suppress_next = 1;
2766 else {
2767 if (possibly_handle_first_page_transition())
2769 else {
2770 for (;;) {
2771 #if defined(DEBUGGING)
2772 if (debug_state) {
2773 fprintf(stderr, "found [%c]\n", ch); fflush(stderr);
2775 #endif
2776 curenv->add_char(charset_table[ch]);
2777 tok.next();
2778 if (tok.type != token::TOKEN_CHAR)
2779 break;
2780 ch = tok.c;
2782 suppress_next = 1;
2783 bol = 0;
2786 break;
2788 case token::TOKEN_TRANSPARENT:
2790 if (bol) {
2791 if (possibly_handle_first_page_transition())
2793 else {
2794 int cc;
2795 do {
2796 node *n;
2797 cc = get_copy(&n);
2798 if (cc != EOF)
2799 if (cc != '\0')
2800 curdiv->transparent_output(transparent_translate(cc));
2801 else
2802 curdiv->transparent_output(n);
2803 } while (cc != '\n' && cc != EOF);
2804 if (cc == EOF)
2805 curdiv->transparent_output('\n');
2808 break;
2810 case token::TOKEN_NEWLINE:
2812 if (bol && !old_have_input
2813 && !curenv->get_prev_line_interrupted())
2814 trapping_blank_line();
2815 else {
2816 curenv->newline();
2817 bol = 1;
2819 break;
2821 case token::TOKEN_REQUEST:
2823 int request_code = tok.c;
2824 tok.next();
2825 switch (request_code) {
2826 case TITLE_REQUEST:
2827 title();
2828 break;
2829 case COPY_FILE_REQUEST:
2830 copy_file();
2831 break;
2832 case TRANSPARENT_FILE_REQUEST:
2833 transparent_file();
2834 break;
2835 #ifdef COLUMN
2836 case VJUSTIFY_REQUEST:
2837 vjustify();
2838 break;
2839 #endif /* COLUMN */
2840 default:
2841 assert(0);
2842 break;
2844 suppress_next = 1;
2845 break;
2847 case token::TOKEN_SPACE:
2849 if (possibly_handle_first_page_transition())
2851 else if (bol && !curenv->get_prev_line_interrupted()) {
2852 int nspaces = 0;
2853 // save space_width now so that it isn't changed by \f or \s
2854 // which we wouldn't notice here
2855 hunits space_width = curenv->get_space_width();
2856 do {
2857 nspaces += tok.nspaces();
2858 tok.next();
2859 } while (tok.space());
2860 if (tok.newline())
2861 trapping_blank_line();
2862 else {
2863 push_token(tok);
2864 curenv->do_break();
2865 curenv->add_node(new hmotion_node(space_width * nspaces,
2866 curenv->get_fill_color()));
2867 bol = 0;
2870 else {
2871 curenv->space();
2872 bol = 0;
2874 break;
2876 case token::TOKEN_EOF:
2877 return;
2878 case token::TOKEN_NODE:
2880 if (possibly_handle_first_page_transition())
2882 else if (tok.nd->reread(&bol)) {
2883 delete tok.nd;
2884 tok.nd = 0;
2886 else {
2887 curenv->add_node(tok.nd);
2888 tok.nd = 0;
2889 bol = 0;
2890 curenv->possibly_break_line(1);
2892 break;
2894 case token::TOKEN_PAGE_EJECTOR:
2896 continue_page_eject();
2897 // I think we just want to preserve bol.
2898 // bol = 1;
2899 break;
2901 case token::TOKEN_BEGIN_TRAP:
2903 trap_bol_stack.push(bol);
2904 bol = 1;
2905 have_input = 0;
2906 break;
2908 case token::TOKEN_END_TRAP:
2910 if (trap_bol_stack.is_empty())
2911 error("spurious end trap token detected!");
2912 else
2913 bol = trap_bol_stack.pop();
2914 have_input = 0;
2916 /* I'm not totally happy about this. But I can't think of any other
2917 way to do it. Doing an output_pending_lines() whenever a
2918 TOKEN_END_TRAP is detected doesn't work: for example,
2920 .wh -1i x
2921 .de x
2924 .wh -.5i y
2925 .de y
2926 .tl ''-%-''
2929 .ll .5i
2930 .sp |\n(.pu-1i-.5v
2931 a\%very\%very\%long\%word
2933 will print all but the first lines from the word immediately
2934 after the footer, rather than on the next page. */
2936 if (trap_bol_stack.is_empty())
2937 curenv->output_pending_lines();
2938 break;
2940 default:
2942 bol = 0;
2943 tok.process();
2944 break;
2947 if (!suppress_next)
2948 tok.next();
2949 trap_sprung_flag = 0;
2953 #ifdef WIDOW_CONTROL
2955 void flush_pending_lines()
2957 while (!tok.newline() && !tok.eof())
2958 tok.next();
2959 curenv->output_pending_lines();
2960 tok.next();
2963 #endif /* WIDOW_CONTROL */
2965 request_or_macro::request_or_macro()
2969 macro *request_or_macro::to_macro()
2971 return 0;
2974 request::request(REQUEST_FUNCP pp) : p(pp)
2978 void request::invoke(symbol)
2980 (*p)();
2983 struct char_block {
2984 enum { SIZE = 128 };
2985 unsigned char s[SIZE];
2986 char_block *next;
2987 char_block();
2990 char_block::char_block()
2991 : next(0)
2995 class char_list {
2996 public:
2997 char_list();
2998 ~char_list();
2999 void append(unsigned char);
3000 void set(unsigned char, int);
3001 unsigned char get(int);
3002 int length();
3003 private:
3004 unsigned char *ptr;
3005 int len;
3006 char_block *head;
3007 char_block *tail;
3008 friend class macro_header;
3009 friend class string_iterator;
3012 char_list::char_list()
3013 : ptr(0), len(0), head(0), tail(0)
3017 char_list::~char_list()
3019 while (head != 0) {
3020 char_block *tem = head;
3021 head = head->next;
3022 delete tem;
3026 int char_list::length()
3028 return len;
3031 void char_list::append(unsigned char c)
3033 if (tail == 0) {
3034 head = tail = new char_block;
3035 ptr = tail->s;
3037 else {
3038 if (ptr >= tail->s + char_block::SIZE) {
3039 tail->next = new char_block;
3040 tail = tail->next;
3041 ptr = tail->s;
3044 *ptr++ = c;
3045 len++;
3048 void char_list::set(unsigned char c, int offset)
3050 assert(len > offset);
3051 // optimization for access at the end
3052 int boundary = len - len % char_block::SIZE;
3053 if (offset >= boundary) {
3054 *(tail->s + offset - boundary) = c;
3055 return;
3057 char_block *tem = head;
3058 int l = 0;
3059 for (;;) {
3060 l += char_block::SIZE;
3061 if (l > offset) {
3062 *(tem->s + offset % char_block::SIZE) = c;
3063 return;
3065 tem = tem->next;
3069 unsigned char char_list::get(int offset)
3071 assert(len > offset);
3072 // optimization for access at the end
3073 int boundary = len - len % char_block::SIZE;
3074 if (offset >= boundary)
3075 return *(tail->s + offset - boundary);
3076 char_block *tem = head;
3077 int l = 0;
3078 for (;;) {
3079 l += char_block::SIZE;
3080 if (l > offset)
3081 return *(tem->s + offset % char_block::SIZE);
3082 tem = tem->next;
3086 class node_list {
3087 node *head;
3088 node *tail;
3089 public:
3090 node_list();
3091 ~node_list();
3092 void append(node *);
3093 int length();
3094 node *extract();
3096 friend class macro_header;
3097 friend class string_iterator;
3100 void node_list::append(node *n)
3102 if (head == 0) {
3103 n->next = 0;
3104 head = tail = n;
3106 else {
3107 n->next = 0;
3108 tail = tail->next = n;
3112 int node_list::length()
3114 int total = 0;
3115 for (node *n = head; n != 0; n = n->next)
3116 ++total;
3117 return total;
3120 node_list::node_list()
3122 head = tail = 0;
3125 node *node_list::extract()
3127 node *temp = head;
3128 head = tail = 0;
3129 return temp;
3132 node_list::~node_list()
3134 delete_node_list(head);
3137 class macro_header {
3138 public:
3139 int count;
3140 char_list cl;
3141 node_list nl;
3142 macro_header() { count = 1; }
3143 macro_header *copy(int);
3146 macro::~macro()
3148 if (p != 0 && --(p->count) <= 0)
3149 delete p;
3152 macro::macro()
3153 : is_a_diversion(0)
3155 if (!input_stack::get_location(1, &filename, &lineno)) {
3156 filename = 0;
3157 lineno = 0;
3159 len = 0;
3160 empty_macro = 1;
3161 p = 0;
3164 macro::macro(const macro &m)
3165 : filename(m.filename), lineno(m.lineno), len(m.len),
3166 empty_macro(m.empty_macro), is_a_diversion(m.is_a_diversion), p(m.p)
3168 if (p != 0)
3169 p->count++;
3172 macro::macro(int is_div)
3173 : is_a_diversion(is_div)
3175 if (!input_stack::get_location(1, &filename, &lineno)) {
3176 filename = 0;
3177 lineno = 0;
3179 len = 0;
3180 empty_macro = 1;
3181 p = 0;
3184 int macro::is_diversion()
3186 return is_a_diversion;
3189 macro &macro::operator=(const macro &m)
3191 // don't assign object
3192 if (m.p != 0)
3193 m.p->count++;
3194 if (p != 0 && --(p->count) <= 0)
3195 delete p;
3196 p = m.p;
3197 filename = m.filename;
3198 lineno = m.lineno;
3199 len = m.len;
3200 empty_macro = m.empty_macro;
3201 is_a_diversion = m.is_a_diversion;
3202 return *this;
3205 void macro::append(unsigned char c)
3207 assert(c != 0);
3208 if (p == 0)
3209 p = new macro_header;
3210 if (p->cl.length() != len) {
3211 macro_header *tem = p->copy(len);
3212 if (--(p->count) <= 0)
3213 delete p;
3214 p = tem;
3216 p->cl.append(c);
3217 ++len;
3218 if (c != PUSH_GROFF_MODE && c != PUSH_COMP_MODE && c != POP_GROFFCOMP_MODE)
3219 empty_macro = 0;
3222 void macro::set(unsigned char c, int offset)
3224 assert(p != 0);
3225 assert(c != 0);
3226 p->cl.set(c, offset);
3229 unsigned char macro::get(int offset)
3231 assert(p != 0);
3232 return p->cl.get(offset);
3235 int macro::length()
3237 return len;
3240 void macro::append_str(const char *s)
3242 int i = 0;
3244 if (s) {
3245 while (s[i] != (char)0) {
3246 append(s[i]);
3247 i++;
3252 void macro::append(node *n)
3254 assert(n != 0);
3255 if (p == 0)
3256 p = new macro_header;
3257 if (p->cl.length() != len) {
3258 macro_header *tem = p->copy(len);
3259 if (--(p->count) <= 0)
3260 delete p;
3261 p = tem;
3263 p->cl.append(0);
3264 p->nl.append(n);
3265 ++len;
3266 empty_macro = 0;
3269 void macro::append_unsigned(unsigned int i)
3271 unsigned int j = i / 10;
3272 if (j != 0)
3273 append_unsigned(j);
3274 append(((unsigned char)(((int)'0') + i % 10)));
3277 void macro::append_int(int i)
3279 if (i < 0) {
3280 append('-');
3281 i = -i;
3283 append_unsigned((unsigned int)i);
3286 void macro::print_size()
3288 errprint("%1", len);
3291 // make a copy of the first n bytes
3293 macro_header *macro_header::copy(int n)
3295 macro_header *p = new macro_header;
3296 char_block *bp = cl.head;
3297 unsigned char *ptr = bp->s;
3298 node *nd = nl.head;
3299 while (--n >= 0) {
3300 if (ptr >= bp->s + char_block::SIZE) {
3301 bp = bp->next;
3302 ptr = bp->s;
3304 unsigned char c = *ptr++;
3305 p->cl.append(c);
3306 if (c == 0) {
3307 p->nl.append(nd->copy());
3308 nd = nd->next;
3311 return p;
3314 void print_macros()
3316 object_dictionary_iterator iter(request_dictionary);
3317 request_or_macro *rm;
3318 symbol s;
3319 while (iter.get(&s, (object **)&rm)) {
3320 assert(!s.is_null());
3321 macro *m = rm->to_macro();
3322 if (m) {
3323 errprint("%1\t", s.contents());
3324 m->print_size();
3325 errprint("\n");
3328 fflush(stderr);
3329 skip_line();
3332 class string_iterator : public input_iterator {
3333 macro mac;
3334 const char *how_invoked;
3335 int newline_flag;
3336 int lineno;
3337 char_block *bp;
3338 int count; // of characters remaining
3339 node *nd;
3340 int saved_compatible_flag;
3341 protected:
3342 symbol nm;
3343 string_iterator();
3344 public:
3345 string_iterator(const macro &m, const char *p = 0, symbol s = NULL_SYMBOL);
3346 int fill(node **);
3347 int peek();
3348 int get_location(int, const char **, int *);
3349 void backtrace();
3350 void save_compatible_flag(int f) { saved_compatible_flag = f; }
3351 int get_compatible_flag() { return saved_compatible_flag; }
3352 int is_diversion();
3355 string_iterator::string_iterator(const macro &m, const char *p, symbol s)
3356 : input_iterator(m.is_a_diversion), mac(m), how_invoked(p), newline_flag(0),
3357 lineno(1), nm(s)
3359 count = mac.len;
3360 if (count != 0) {
3361 bp = mac.p->cl.head;
3362 nd = mac.p->nl.head;
3363 ptr = eptr = bp->s;
3365 else {
3366 bp = 0;
3367 nd = 0;
3368 ptr = eptr = 0;
3372 string_iterator::string_iterator()
3374 bp = 0;
3375 nd = 0;
3376 ptr = eptr = 0;
3377 newline_flag = 0;
3378 how_invoked = 0;
3379 lineno = 1;
3380 count = 0;
3383 int string_iterator::is_diversion()
3385 return mac.is_diversion();
3388 int string_iterator::fill(node **np)
3390 if (newline_flag)
3391 lineno++;
3392 newline_flag = 0;
3393 if (count <= 0)
3394 return EOF;
3395 const unsigned char *p = eptr;
3396 if (p >= bp->s + char_block::SIZE) {
3397 bp = bp->next;
3398 p = bp->s;
3400 if (*p == '\0') {
3401 if (np)
3402 *np = nd->copy();
3403 if (is_diversion())
3404 (*np)->div_nest_level = input_stack::get_div_level();
3405 else
3406 (*np)->div_nest_level = 0;
3407 nd = nd->next;
3408 eptr = ptr = p + 1;
3409 count--;
3410 return 0;
3412 const unsigned char *e = bp->s + char_block::SIZE;
3413 if (e - p > count)
3414 e = p + count;
3415 ptr = p;
3416 while (p < e) {
3417 unsigned char c = *p;
3418 if (c == '\n' || c == ESCAPE_NEWLINE) {
3419 newline_flag = 1;
3420 p++;
3421 break;
3423 if (c == '\0')
3424 break;
3425 p++;
3427 eptr = p;
3428 count -= p - ptr;
3429 return *ptr++;
3432 int string_iterator::peek()
3434 if (count <= 0)
3435 return EOF;
3436 const unsigned char *p = eptr;
3437 if (p >= bp->s + char_block::SIZE) {
3438 p = bp->next->s;
3440 return *p;
3443 int string_iterator::get_location(int allow_macro,
3444 const char **filep, int *linep)
3446 if (!allow_macro)
3447 return 0;
3448 if (mac.filename == 0)
3449 return 0;
3450 *filep = mac.filename;
3451 *linep = mac.lineno + lineno - 1;
3452 return 1;
3455 void string_iterator::backtrace()
3457 if (mac.filename) {
3458 errprint("%1:%2: backtrace", mac.filename, mac.lineno + lineno - 1);
3459 if (how_invoked) {
3460 if (!nm.is_null())
3461 errprint(": %1 `%2'\n", how_invoked, nm.contents());
3462 else
3463 errprint(": %1\n", how_invoked);
3465 else
3466 errprint("\n");
3470 class temp_iterator : public input_iterator {
3471 unsigned char *base;
3472 temp_iterator(const char *, int len);
3473 public:
3474 ~temp_iterator();
3475 friend input_iterator *make_temp_iterator(const char *);
3478 #ifdef __GNUG__
3479 inline
3480 #endif
3481 temp_iterator::temp_iterator(const char *s, int len)
3483 base = new unsigned char[len];
3484 memcpy(base, s, len);
3485 ptr = base;
3486 eptr = base + len;
3489 temp_iterator::~temp_iterator()
3491 a_delete base;
3494 class small_temp_iterator : public input_iterator {
3495 private:
3496 small_temp_iterator(const char *, int);
3497 ~small_temp_iterator();
3498 enum { BLOCK = 16 };
3499 static small_temp_iterator *free_list;
3500 void *operator new(size_t);
3501 void operator delete(void *);
3502 enum { SIZE = 12 };
3503 unsigned char buf[SIZE];
3504 friend input_iterator *make_temp_iterator(const char *);
3507 small_temp_iterator *small_temp_iterator::free_list = 0;
3509 void *small_temp_iterator::operator new(size_t n)
3511 assert(n == sizeof(small_temp_iterator));
3512 if (!free_list) {
3513 free_list =
3514 (small_temp_iterator *)new char[sizeof(small_temp_iterator)*BLOCK];
3515 for (int i = 0; i < BLOCK - 1; i++)
3516 free_list[i].next = free_list + i + 1;
3517 free_list[BLOCK-1].next = 0;
3519 small_temp_iterator *p = free_list;
3520 free_list = (small_temp_iterator *)(free_list->next);
3521 p->next = 0;
3522 return p;
3525 #ifdef __GNUG__
3526 inline
3527 #endif
3528 void small_temp_iterator::operator delete(void *p)
3530 if (p) {
3531 ((small_temp_iterator *)p)->next = free_list;
3532 free_list = (small_temp_iterator *)p;
3536 small_temp_iterator::~small_temp_iterator()
3540 #ifdef __GNUG__
3541 inline
3542 #endif
3543 small_temp_iterator::small_temp_iterator(const char *s, int len)
3545 for (int i = 0; i < len; i++)
3546 buf[i] = s[i];
3547 ptr = buf;
3548 eptr = buf + len;
3551 input_iterator *make_temp_iterator(const char *s)
3553 if (s == 0)
3554 return new small_temp_iterator(s, 0);
3555 else {
3556 int n = strlen(s);
3557 if (n <= small_temp_iterator::SIZE)
3558 return new small_temp_iterator(s, n);
3559 else
3560 return new temp_iterator(s, n);
3564 // this is used when macros with arguments are interpolated
3566 struct arg_list {
3567 macro mac;
3568 arg_list *next;
3569 arg_list(const macro &);
3570 ~arg_list();
3573 arg_list::arg_list(const macro &m) : mac(m), next(0)
3577 arg_list::~arg_list()
3581 class macro_iterator : public string_iterator {
3582 arg_list *args;
3583 int argc;
3584 public:
3585 macro_iterator(symbol, macro &, const char *how_invoked = "macro");
3586 macro_iterator();
3587 ~macro_iterator();
3588 int has_args() { return 1; }
3589 input_iterator *get_arg(int i);
3590 int nargs() { return argc; }
3591 void add_arg(const macro &m);
3592 void shift(int n);
3593 int is_macro() { return 1; }
3594 int is_diversion();
3597 input_iterator *macro_iterator::get_arg(int i)
3599 if (i == 0)
3600 return make_temp_iterator(nm.contents());
3601 if (i > 0 && i <= argc) {
3602 arg_list *p = args;
3603 for (int j = 1; j < i; j++) {
3604 assert(p != 0);
3605 p = p->next;
3607 return new string_iterator(p->mac);
3609 else
3610 return 0;
3613 void macro_iterator::add_arg(const macro &m)
3615 arg_list **p;
3616 for (p = &args; *p; p = &((*p)->next))
3618 *p = new arg_list(m);
3619 ++argc;
3622 void macro_iterator::shift(int n)
3624 while (n > 0 && argc > 0) {
3625 arg_list *tem = args;
3626 args = args->next;
3627 delete tem;
3628 --argc;
3629 --n;
3633 // This gets used by eg .if '\?xxx\?''.
3635 int operator==(const macro &m1, const macro &m2)
3637 if (m1.len != m2.len)
3638 return 0;
3639 string_iterator iter1(m1);
3640 string_iterator iter2(m2);
3641 int n = m1.len;
3642 while (--n >= 0) {
3643 node *nd1 = 0;
3644 int c1 = iter1.get(&nd1);
3645 assert(c1 != EOF);
3646 node *nd2 = 0;
3647 int c2 = iter2.get(&nd2);
3648 assert(c2 != EOF);
3649 if (c1 != c2) {
3650 if (c1 == 0)
3651 delete nd1;
3652 else if (c2 == 0)
3653 delete nd2;
3654 return 0;
3656 if (c1 == 0) {
3657 assert(nd1 != 0);
3658 assert(nd2 != 0);
3659 int are_same = nd1->type() == nd2->type() && nd1->same(nd2);
3660 delete nd1;
3661 delete nd2;
3662 if (!are_same)
3663 return 0;
3666 return 1;
3669 static void interpolate_macro(symbol nm)
3671 request_or_macro *p = (request_or_macro *)request_dictionary.lookup(nm);
3672 if (p == 0) {
3673 int warned = 0;
3674 const char *s = nm.contents();
3675 if (strlen(s) > 2) {
3676 request_or_macro *r;
3677 char buf[3];
3678 buf[0] = s[0];
3679 buf[1] = s[1];
3680 buf[2] = '\0';
3681 r = (request_or_macro *)request_dictionary.lookup(symbol(buf));
3682 if (r) {
3683 macro *m = r->to_macro();
3684 if (!m || !m->empty())
3685 warned = warning(WARN_SPACE,
3686 "macro `%1' not defined "
3687 "(probably missing space after `%2')",
3688 nm.contents(), buf);
3691 if (!warned) {
3692 warning(WARN_MAC, "macro `%1' not defined", nm.contents());
3693 p = new macro;
3694 request_dictionary.define(nm, p);
3697 if (p)
3698 p->invoke(nm);
3699 else {
3700 skip_line();
3701 return;
3705 static void decode_args(macro_iterator *mi)
3707 if (!tok.newline() && !tok.eof()) {
3708 node *n;
3709 int c = get_copy(&n);
3710 for (;;) {
3711 while (c == ' ')
3712 c = get_copy(&n);
3713 if (c == '\n' || c == EOF)
3714 break;
3715 macro arg;
3716 int quote_input_level = 0;
3717 int done_tab_warning = 0;
3718 if (c == '"') {
3719 quote_input_level = input_stack::get_level();
3720 c = get_copy(&n);
3722 arg.append(compatible_flag ? PUSH_COMP_MODE : PUSH_GROFF_MODE);
3723 while (c != EOF && c != '\n' && !(c == ' ' && quote_input_level == 0)) {
3724 if (quote_input_level > 0 && c == '"'
3725 && (compatible_flag
3726 || input_stack::get_level() == quote_input_level)) {
3727 c = get_copy(&n);
3728 if (c == '"') {
3729 arg.append(c);
3730 c = get_copy(&n);
3732 else
3733 break;
3735 else {
3736 if (c == 0)
3737 arg.append(n);
3738 else {
3739 if (c == '\t' && quote_input_level == 0 && !done_tab_warning) {
3740 warning(WARN_TAB, "tab character in unquoted macro argument");
3741 done_tab_warning = 1;
3743 arg.append(c);
3745 c = get_copy(&n);
3748 arg.append(POP_GROFFCOMP_MODE);
3749 mi->add_arg(arg);
3754 static void decode_string_args(macro_iterator *mi)
3756 node *n;
3757 int c = get_copy(&n);
3758 for (;;) {
3759 while (c == ' ')
3760 c = get_copy(&n);
3761 if (c == '\n' || c == EOF) {
3762 error("missing `]'");
3763 break;
3765 if (c == ']')
3766 break;
3767 macro arg;
3768 int quote_input_level = 0;
3769 int done_tab_warning = 0;
3770 if (c == '"') {
3771 quote_input_level = input_stack::get_level();
3772 c = get_copy(&n);
3774 while (c != EOF && c != '\n'
3775 && !(c == ']' && quote_input_level == 0)
3776 && !(c == ' ' && quote_input_level == 0)) {
3777 if (quote_input_level > 0 && c == '"'
3778 && input_stack::get_level() == quote_input_level) {
3779 c = get_copy(&n);
3780 if (c == '"') {
3781 arg.append(c);
3782 c = get_copy(&n);
3784 else
3785 break;
3787 else {
3788 if (c == 0)
3789 arg.append(n);
3790 else {
3791 if (c == '\t' && quote_input_level == 0 && !done_tab_warning) {
3792 warning(WARN_TAB, "tab character in unquoted string argument");
3793 done_tab_warning = 1;
3795 arg.append(c);
3797 c = get_copy(&n);
3800 mi->add_arg(arg);
3804 void macro::invoke(symbol nm)
3806 macro_iterator *mi = new macro_iterator(nm, *this);
3807 decode_args(mi);
3808 input_stack::push(mi);
3809 tok.next();
3812 macro *macro::to_macro()
3814 return this;
3817 int macro::empty()
3819 return empty_macro == 1;
3822 macro_iterator::macro_iterator(symbol s, macro &m, const char *how_called)
3823 : string_iterator(m, how_called, s), args(0), argc(0)
3827 macro_iterator::macro_iterator() : args(0), argc(0)
3831 macro_iterator::~macro_iterator()
3833 while (args != 0) {
3834 arg_list *tem = args;
3835 args = args->next;
3836 delete tem;
3840 dictionary composite_dictionary(17);
3842 void composite_request()
3844 symbol from = get_name(1);
3845 if (!from.is_null()) {
3846 const char *from_gn = glyph_name_to_unicode(from.contents());
3847 if (!from_gn) {
3848 from_gn = check_unicode_name(from.contents());
3849 if (!from_gn) {
3850 error("invalid composite glyph name `%1'", from.contents());
3851 skip_line();
3852 return;
3855 const char *from_decomposed = decompose_unicode(from_gn);
3856 if (from_decomposed)
3857 from_gn = &from_decomposed[1];
3858 symbol to = get_name(1);
3859 if (to.is_null())
3860 composite_dictionary.remove(symbol(from_gn));
3861 else {
3862 const char *to_gn = glyph_name_to_unicode(to.contents());
3863 if (!to_gn) {
3864 to_gn = check_unicode_name(to.contents());
3865 if (!to_gn) {
3866 error("invalid composite glyph name `%1'", to.contents());
3867 skip_line();
3868 return;
3871 const char *to_decomposed = decompose_unicode(to_gn);
3872 if (to_decomposed)
3873 to_gn = &to_decomposed[1];
3874 if (strcmp(from_gn, to_gn) == 0)
3875 composite_dictionary.remove(symbol(from_gn));
3876 else
3877 (void)composite_dictionary.lookup(symbol(from_gn), (void *)to_gn);
3880 skip_line();
3883 static symbol composite_glyph_name(symbol nm)
3885 macro_iterator *mi = new macro_iterator();
3886 decode_string_args(mi);
3887 input_stack::push(mi);
3888 const char *gn = glyph_name_to_unicode(nm.contents());
3889 if (!gn) {
3890 gn = check_unicode_name(nm.contents());
3891 if (!gn) {
3892 error("invalid base glyph `%1' in composite glyph name", nm.contents());
3893 return EMPTY_SYMBOL;
3896 const char *gn_decomposed = decompose_unicode(gn);
3897 string glyph_name(gn_decomposed ? &gn_decomposed[1] : gn);
3898 string gl;
3899 int n = input_stack::nargs();
3900 for (int i = 1; i <= n; i++) {
3901 glyph_name += '_';
3902 input_iterator *p = input_stack::get_arg(i);
3903 gl.clear();
3904 int c;
3905 while ((c = p->get(0)) != EOF)
3906 gl += c;
3907 gl += '\0';
3908 const char *u = glyph_name_to_unicode(gl.contents());
3909 if (!u) {
3910 u = check_unicode_name(gl.contents());
3911 if (!u) {
3912 error("invalid component `%1' in composite glyph name",
3913 gl.contents());
3914 return EMPTY_SYMBOL;
3917 const char *decomposed = decompose_unicode(u);
3918 if (decomposed)
3919 u = &decomposed[1];
3920 void *mapped_composite = composite_dictionary.lookup(symbol(u));
3921 if (mapped_composite)
3922 u = (const char *)mapped_composite;
3923 glyph_name += u;
3925 glyph_name += '\0';
3926 const char *groff_gn = unicode_to_glyph_name(glyph_name.contents());
3927 if (groff_gn)
3928 return symbol(groff_gn);
3929 gl.clear();
3930 gl += 'u';
3931 gl += glyph_name;
3932 return symbol(gl.contents());
3935 int trap_sprung_flag = 0;
3936 int postpone_traps_flag = 0;
3937 symbol postponed_trap;
3939 void spring_trap(symbol nm)
3941 assert(!nm.is_null());
3942 trap_sprung_flag = 1;
3943 if (postpone_traps_flag) {
3944 postponed_trap = nm;
3945 return;
3947 static char buf[2] = { BEGIN_TRAP, 0 };
3948 static char buf2[2] = { END_TRAP, '\0' };
3949 input_stack::push(make_temp_iterator(buf2));
3950 request_or_macro *p = lookup_request(nm);
3951 macro *m = p->to_macro();
3952 if (m)
3953 input_stack::push(new macro_iterator(nm, *m, "trap-invoked macro"));
3954 else
3955 error("you can't invoke a request with a trap");
3956 input_stack::push(make_temp_iterator(buf));
3959 void postpone_traps()
3961 postpone_traps_flag = 1;
3964 int unpostpone_traps()
3966 postpone_traps_flag = 0;
3967 if (!postponed_trap.is_null()) {
3968 spring_trap(postponed_trap);
3969 postponed_trap = NULL_SYMBOL;
3970 return 1;
3972 else
3973 return 0;
3976 void read_request()
3978 macro_iterator *mi = new macro_iterator;
3979 int reading_from_terminal = isatty(fileno(stdin));
3980 int had_prompt = 0;
3981 if (!tok.newline() && !tok.eof()) {
3982 int c = get_copy(0);
3983 while (c == ' ')
3984 c = get_copy(0);
3985 while (c != EOF && c != '\n' && c != ' ') {
3986 if (!invalid_input_char(c)) {
3987 if (reading_from_terminal)
3988 fputc(c, stderr);
3989 had_prompt = 1;
3991 c = get_copy(0);
3993 if (c == ' ') {
3994 tok.make_space();
3995 decode_args(mi);
3998 if (reading_from_terminal) {
3999 fputc(had_prompt ? ':' : '\a', stderr);
4000 fflush(stderr);
4002 input_stack::push(mi);
4003 macro mac;
4004 int nl = 0;
4005 int c;
4006 while ((c = getchar()) != EOF) {
4007 if (invalid_input_char(c))
4008 warning(WARN_INPUT, "invalid input character code %1", int(c));
4009 else {
4010 if (c == '\n') {
4011 if (nl)
4012 break;
4013 else
4014 nl = 1;
4016 else
4017 nl = 0;
4018 mac.append(c);
4021 if (reading_from_terminal)
4022 clearerr(stdin);
4023 input_stack::push(new string_iterator(mac));
4024 tok.next();
4027 enum define_mode { DEFINE_NORMAL, DEFINE_APPEND, DEFINE_IGNORE };
4028 enum calling_mode { CALLING_NORMAL, CALLING_INDIRECT };
4029 enum comp_mode { COMP_IGNORE, COMP_DISABLE, COMP_ENABLE };
4031 void do_define_string(define_mode mode, comp_mode comp)
4033 symbol nm;
4034 node *n = 0; // pacify compiler
4035 int c;
4036 nm = get_name(1);
4037 if (nm.is_null()) {
4038 skip_line();
4039 return;
4041 if (tok.newline())
4042 c = '\n';
4043 else if (tok.tab())
4044 c = '\t';
4045 else if (!tok.space()) {
4046 error("bad string definition");
4047 skip_line();
4048 return;
4050 else
4051 c = get_copy(&n);
4052 while (c == ' ')
4053 c = get_copy(&n);
4054 if (c == '"')
4055 c = get_copy(&n);
4056 macro mac;
4057 request_or_macro *rm = (request_or_macro *)request_dictionary.lookup(nm);
4058 macro *mm = rm ? rm->to_macro() : 0;
4059 if (mode == DEFINE_APPEND && mm)
4060 mac = *mm;
4061 if (comp == COMP_DISABLE)
4062 mac.append(PUSH_GROFF_MODE);
4063 else if (comp == COMP_ENABLE)
4064 mac.append(PUSH_COMP_MODE);
4065 while (c != '\n' && c != EOF) {
4066 if (c == 0)
4067 mac.append(n);
4068 else
4069 mac.append((unsigned char)c);
4070 c = get_copy(&n);
4072 if (!mm) {
4073 mm = new macro;
4074 request_dictionary.define(nm, mm);
4076 if (comp == COMP_DISABLE || comp == COMP_ENABLE)
4077 mac.append(POP_GROFFCOMP_MODE);
4078 *mm = mac;
4079 tok.next();
4082 void define_string()
4084 do_define_string(DEFINE_NORMAL,
4085 compatible_flag ? COMP_ENABLE: COMP_IGNORE);
4088 void define_nocomp_string()
4090 do_define_string(DEFINE_NORMAL, COMP_DISABLE);
4093 void append_string()
4095 do_define_string(DEFINE_APPEND,
4096 compatible_flag ? COMP_ENABLE : COMP_IGNORE);
4099 void append_nocomp_string()
4101 do_define_string(DEFINE_APPEND, COMP_DISABLE);
4104 void do_define_character(char_mode mode, const char *font_name)
4106 node *n = 0; // pacify compiler
4107 int c;
4108 tok.skip();
4109 charinfo *ci = tok.get_char(1);
4110 if (ci == 0) {
4111 skip_line();
4112 return;
4114 if (font_name) {
4115 string s(font_name);
4116 s += ' ';
4117 s += ci->nm.contents();
4118 s += '\0';
4119 ci = get_charinfo(symbol(s.contents()));
4121 tok.next();
4122 if (tok.newline())
4123 c = '\n';
4124 else if (tok.tab())
4125 c = '\t';
4126 else if (!tok.space()) {
4127 error("bad character definition");
4128 skip_line();
4129 return;
4131 else
4132 c = get_copy(&n);
4133 while (c == ' ' || c == '\t')
4134 c = get_copy(&n);
4135 if (c == '"')
4136 c = get_copy(&n);
4137 macro *m = new macro;
4138 while (c != '\n' && c != EOF) {
4139 if (c == 0)
4140 m->append(n);
4141 else
4142 m->append((unsigned char)c);
4143 c = get_copy(&n);
4145 m = ci->setx_macro(m, mode);
4146 if (m)
4147 delete m;
4148 tok.next();
4151 void define_character()
4153 do_define_character(CHAR_NORMAL);
4156 void define_fallback_character()
4158 do_define_character(CHAR_FALLBACK);
4161 void define_special_character()
4163 do_define_character(CHAR_SPECIAL);
4166 static void remove_character()
4168 tok.skip();
4169 while (!tok.newline() && !tok.eof()) {
4170 if (!tok.space() && !tok.tab()) {
4171 charinfo *ci = tok.get_char(1);
4172 if (!ci)
4173 break;
4174 macro *m = ci->set_macro(0);
4175 if (m)
4176 delete m;
4178 tok.next();
4180 skip_line();
4183 static void interpolate_string(symbol nm)
4185 request_or_macro *p = lookup_request(nm);
4186 macro *m = p->to_macro();
4187 if (!m)
4188 error("you can only invoke a string or macro using \\*");
4189 else {
4190 string_iterator *si = new string_iterator(*m, "string", nm);
4191 input_stack::push(si);
4195 static void interpolate_string_with_args(symbol s)
4197 request_or_macro *p = lookup_request(s);
4198 macro *m = p->to_macro();
4199 if (!m)
4200 error("you can only invoke a string or macro using \\*");
4201 else {
4202 macro_iterator *mi = new macro_iterator(s, *m);
4203 decode_string_args(mi);
4204 input_stack::push(mi);
4208 static void interpolate_arg(symbol nm)
4210 const char *s = nm.contents();
4211 if (!s || *s == '\0')
4212 copy_mode_error("missing argument name");
4213 else if (s[1] == 0 && csdigit(s[0]))
4214 input_stack::push(input_stack::get_arg(s[0] - '0'));
4215 else if (s[0] == '*' && s[1] == '\0') {
4216 int limit = input_stack::nargs();
4217 string args;
4218 for (int i = 1; i <= limit; i++) {
4219 input_iterator *p = input_stack::get_arg(i);
4220 int c;
4221 while ((c = p->get(0)) != EOF)
4222 args += c;
4223 if (i != limit)
4224 args += ' ';
4226 if (limit > 0) {
4227 args += '\0';
4228 input_stack::push(make_temp_iterator(args.contents()));
4231 else if (s[0] == '@' && s[1] == '\0') {
4232 int limit = input_stack::nargs();
4233 string args;
4234 for (int i = 1; i <= limit; i++) {
4235 args += '"';
4236 args += BEGIN_QUOTE;
4237 input_iterator *p = input_stack::get_arg(i);
4238 int c;
4239 while ((c = p->get(0)) != EOF)
4240 args += c;
4241 args += END_QUOTE;
4242 args += '"';
4243 if (i != limit)
4244 args += ' ';
4246 if (limit > 0) {
4247 args += '\0';
4248 input_stack::push(make_temp_iterator(args.contents()));
4251 else {
4252 const char *p;
4253 for (p = s; *p && csdigit(*p); p++)
4255 if (*p)
4256 copy_mode_error("bad argument name `%1'", s);
4257 else
4258 input_stack::push(input_stack::get_arg(atoi(s)));
4262 void handle_first_page_transition()
4264 push_token(tok);
4265 topdiv->begin_page();
4268 // We push back a token by wrapping it up in a token_node, and
4269 // wrapping that up in a string_iterator.
4271 static void push_token(const token &t)
4273 macro m;
4274 m.append(new token_node(t));
4275 input_stack::push(new string_iterator(m));
4278 void push_page_ejector()
4280 static char buf[2] = { PAGE_EJECTOR, '\0' };
4281 input_stack::push(make_temp_iterator(buf));
4284 void handle_initial_request(unsigned char code)
4286 char buf[2];
4287 buf[0] = code;
4288 buf[1] = '\0';
4289 macro mac;
4290 mac.append(new token_node(tok));
4291 input_stack::push(new string_iterator(mac));
4292 input_stack::push(make_temp_iterator(buf));
4293 topdiv->begin_page();
4294 tok.next();
4297 void handle_initial_title()
4299 handle_initial_request(TITLE_REQUEST);
4302 // this should be local to define_macro, but cfront 1.2 doesn't support that
4303 static symbol dot_symbol(".");
4305 void do_define_macro(define_mode mode, calling_mode calling, comp_mode comp)
4307 symbol nm, term;
4308 if (calling == CALLING_INDIRECT) {
4309 symbol temp1 = get_name(1);
4310 if (temp1.is_null()) {
4311 skip_line();
4312 return;
4314 symbol temp2 = get_name();
4315 input_stack::push(make_temp_iterator("\n"));
4316 if (!temp2.is_null()) {
4317 interpolate_string(temp2);
4318 input_stack::push(make_temp_iterator(" "));
4320 interpolate_string(temp1);
4321 input_stack::push(make_temp_iterator(" "));
4322 tok.next();
4324 if (mode == DEFINE_NORMAL || mode == DEFINE_APPEND) {
4325 nm = get_name(1);
4326 if (nm.is_null()) {
4327 skip_line();
4328 return;
4331 term = get_name(); // the request that terminates the definition
4332 if (term.is_null())
4333 term = dot_symbol;
4334 while (!tok.newline() && !tok.eof())
4335 tok.next();
4336 const char *start_filename;
4337 int start_lineno;
4338 int have_start_location = input_stack::get_location(0, &start_filename,
4339 &start_lineno);
4340 node *n;
4341 // doing this here makes the line numbers come out right
4342 int c = get_copy(&n, 1);
4343 macro mac;
4344 macro *mm = 0;
4345 if (mode == DEFINE_NORMAL || mode == DEFINE_APPEND) {
4346 request_or_macro *rm =
4347 (request_or_macro *)request_dictionary.lookup(nm);
4348 if (rm)
4349 mm = rm->to_macro();
4350 if (mm && mode == DEFINE_APPEND)
4351 mac = *mm;
4353 int bol = 1;
4354 if (comp == COMP_DISABLE)
4355 mac.append(PUSH_GROFF_MODE);
4356 else if (comp == COMP_ENABLE)
4357 mac.append(PUSH_COMP_MODE);
4358 for (;;) {
4359 while (c == ESCAPE_NEWLINE) {
4360 if (mode == DEFINE_NORMAL || mode == DEFINE_APPEND)
4361 mac.append(c);
4362 c = get_copy(&n, 1);
4364 if (bol && c == '.') {
4365 const char *s = term.contents();
4366 int d = 0;
4367 // see if it matches term
4368 int i = 0;
4369 if (s[0] != 0) {
4370 while ((d = get_copy(&n)) == ' ' || d == '\t')
4372 if ((unsigned char)s[0] == d) {
4373 for (i = 1; s[i] != 0; i++) {
4374 d = get_copy(&n);
4375 if ((unsigned char)s[i] != d)
4376 break;
4380 if (s[i] == 0
4381 && ((i == 2 && compatible_flag)
4382 || (d = get_copy(&n)) == ' '
4383 || d == '\n')) { // we found it
4384 if (d == '\n')
4385 tok.make_newline();
4386 else
4387 tok.make_space();
4388 if (mode == DEFINE_APPEND || mode == DEFINE_NORMAL) {
4389 if (!mm) {
4390 mm = new macro;
4391 request_dictionary.define(nm, mm);
4393 if (comp == COMP_DISABLE || comp == COMP_ENABLE)
4394 mac.append(POP_GROFFCOMP_MODE);
4395 *mm = mac;
4397 if (term != dot_symbol) {
4398 ignoring = 0;
4399 interpolate_macro(term);
4401 else
4402 skip_line();
4403 return;
4405 if (mode == DEFINE_APPEND || mode == DEFINE_NORMAL) {
4406 mac.append(c);
4407 for (int j = 0; j < i; j++)
4408 mac.append(s[j]);
4410 c = d;
4412 if (c == EOF) {
4413 if (mode == DEFINE_NORMAL || mode == DEFINE_APPEND) {
4414 if (have_start_location)
4415 error_with_file_and_line(start_filename, start_lineno,
4416 "end of file while defining macro `%1'",
4417 nm.contents());
4418 else
4419 error("end of file while defining macro `%1'", nm.contents());
4421 else {
4422 if (have_start_location)
4423 error_with_file_and_line(start_filename, start_lineno,
4424 "end of file while ignoring input lines");
4425 else
4426 error("end of file while ignoring input lines");
4428 tok.next();
4429 return;
4431 if (mode == DEFINE_NORMAL || mode == DEFINE_APPEND) {
4432 if (c == 0)
4433 mac.append(n);
4434 else
4435 mac.append(c);
4437 bol = (c == '\n');
4438 c = get_copy(&n, 1);
4442 void define_macro()
4444 do_define_macro(DEFINE_NORMAL, CALLING_NORMAL,
4445 compatible_flag ? COMP_ENABLE : COMP_IGNORE);
4448 void define_nocomp_macro()
4450 do_define_macro(DEFINE_NORMAL, CALLING_NORMAL, COMP_DISABLE);
4453 void define_indirect_macro()
4455 do_define_macro(DEFINE_NORMAL, CALLING_INDIRECT,
4456 compatible_flag ? COMP_ENABLE : COMP_IGNORE);
4459 void define_indirect_nocomp_macro()
4461 do_define_macro(DEFINE_NORMAL, CALLING_INDIRECT, COMP_DISABLE);
4464 void append_macro()
4466 do_define_macro(DEFINE_APPEND, CALLING_NORMAL,
4467 compatible_flag ? COMP_ENABLE : COMP_IGNORE);
4470 void append_nocomp_macro()
4472 do_define_macro(DEFINE_APPEND, CALLING_NORMAL, COMP_DISABLE);
4475 void append_indirect_macro()
4477 do_define_macro(DEFINE_APPEND, CALLING_INDIRECT,
4478 compatible_flag ? COMP_ENABLE : COMP_IGNORE);
4481 void append_indirect_nocomp_macro()
4483 do_define_macro(DEFINE_APPEND, CALLING_INDIRECT, COMP_DISABLE);
4486 void ignore()
4488 ignoring = 1;
4489 do_define_macro(DEFINE_IGNORE, CALLING_NORMAL, COMP_IGNORE);
4490 ignoring = 0;
4493 void remove_macro()
4495 for (;;) {
4496 symbol s = get_name();
4497 if (s.is_null())
4498 break;
4499 request_dictionary.remove(s);
4501 skip_line();
4504 void rename_macro()
4506 symbol s1 = get_name(1);
4507 if (!s1.is_null()) {
4508 symbol s2 = get_name(1);
4509 if (!s2.is_null())
4510 request_dictionary.rename(s1, s2);
4512 skip_line();
4515 void alias_macro()
4517 symbol s1 = get_name(1);
4518 if (!s1.is_null()) {
4519 symbol s2 = get_name(1);
4520 if (!s2.is_null()) {
4521 if (!request_dictionary.alias(s1, s2))
4522 warning(WARN_MAC, "macro `%1' not defined", s2.contents());
4525 skip_line();
4528 void chop_macro()
4530 symbol s = get_name(1);
4531 if (!s.is_null()) {
4532 request_or_macro *p = lookup_request(s);
4533 macro *m = p->to_macro();
4534 if (!m)
4535 error("cannot chop request");
4536 else if (m->empty())
4537 error("cannot chop empty macro");
4538 else {
4539 int have_restore = 0;
4540 // we have to check for additional save/restore pairs which could be
4541 // there due to empty am1 requests.
4542 for (;;) {
4543 if (m->get(m->len - 1) != POP_GROFFCOMP_MODE)
4544 break;
4545 have_restore = 1;
4546 m->len -= 1;
4547 if (m->get(m->len - 1) != PUSH_GROFF_MODE
4548 && m->get(m->len - 1) != PUSH_COMP_MODE)
4549 break;
4550 have_restore = 0;
4551 m->len -= 1;
4552 if (m->len == 0)
4553 break;
4555 if (m->len == 0)
4556 error("cannot chop empty macro");
4557 else {
4558 if (have_restore)
4559 m->set(POP_GROFFCOMP_MODE, m->len - 1);
4560 else
4561 m->len -= 1;
4565 skip_line();
4568 void substring_request()
4570 int start; // 0, 1, ..., n-1 or -1, -2, ...
4571 symbol s = get_name(1);
4572 if (!s.is_null() && get_integer(&start)) {
4573 request_or_macro *p = lookup_request(s);
4574 macro *m = p->to_macro();
4575 if (!m)
4576 error("cannot apply `substring' on a request");
4577 else {
4578 int end = -1;
4579 if (!has_arg() || get_integer(&end)) {
4580 int real_length = 0; // 1, 2, ..., n
4581 string_iterator iter1(*m);
4582 for (int l = 0; l < m->len; l++) {
4583 int c = iter1.get(0);
4584 if (c == PUSH_GROFF_MODE
4585 || c == PUSH_COMP_MODE
4586 || c == POP_GROFFCOMP_MODE)
4587 continue;
4588 if (c == EOF)
4589 break;
4590 real_length++;
4592 if (start < 0)
4593 start += real_length;
4594 if (end < 0)
4595 end += real_length;
4596 if (start > end) {
4597 int tem = start;
4598 start = end;
4599 end = tem;
4601 if (start >= real_length || end < 0) {
4602 warning(WARN_RANGE,
4603 "start and end index of substring out of range");
4604 m->len = 0;
4605 if (m->p) {
4606 if (--(m->p->count) <= 0)
4607 delete m->p;
4608 m->p = 0;
4610 skip_line();
4611 return;
4613 if (start < 0) {
4614 warning(WARN_RANGE,
4615 "start index of substring out of range, set to 0");
4616 start = 0;
4618 if (end >= real_length) {
4619 warning(WARN_RANGE,
4620 "end index of substring out of range, set to string length");
4621 end = real_length - 1;
4623 // now extract the substring
4624 string_iterator iter(*m);
4625 int i;
4626 for (i = 0; i < start; i++) {
4627 int c = iter.get(0);
4628 while (c == PUSH_GROFF_MODE
4629 || c == PUSH_COMP_MODE
4630 || c == POP_GROFFCOMP_MODE)
4631 c = iter.get(0);
4632 if (c == EOF)
4633 break;
4635 macro mac;
4636 for (; i <= end; i++) {
4637 node *nd = 0; // pacify compiler
4638 int c = iter.get(&nd);
4639 while (c == PUSH_GROFF_MODE
4640 || c == PUSH_COMP_MODE
4641 || c == POP_GROFFCOMP_MODE)
4642 c = iter.get(0);
4643 if (c == EOF)
4644 break;
4645 if (c == 0)
4646 mac.append(nd);
4647 else
4648 mac.append((unsigned char)c);
4650 *m = mac;
4654 skip_line();
4657 void length_request()
4659 symbol ret;
4660 ret = get_name(1);
4661 if (ret.is_null()) {
4662 skip_line();
4663 return;
4665 int c;
4666 node *n;
4667 if (tok.newline())
4668 c = '\n';
4669 else if (tok.tab())
4670 c = '\t';
4671 else if (!tok.space()) {
4672 error("bad string definition");
4673 skip_line();
4674 return;
4676 else
4677 c = get_copy(&n);
4678 while (c == ' ')
4679 c = get_copy(&n);
4680 if (c == '"')
4681 c = get_copy(&n);
4682 int len = 0;
4683 while (c != '\n' && c != EOF) {
4684 ++len;
4685 c = get_copy(&n);
4687 reg *r = (reg*)number_reg_dictionary.lookup(ret);
4688 if (r)
4689 r->set_value(len);
4690 else
4691 set_number_reg(ret, len);
4692 tok.next();
4695 void asciify_macro()
4697 symbol s = get_name(1);
4698 if (!s.is_null()) {
4699 request_or_macro *p = lookup_request(s);
4700 macro *m = p->to_macro();
4701 if (!m)
4702 error("cannot asciify request");
4703 else {
4704 macro am;
4705 string_iterator iter(*m);
4706 for (;;) {
4707 node *nd = 0; // pacify compiler
4708 int c = iter.get(&nd);
4709 if (c == EOF)
4710 break;
4711 if (c != 0)
4712 am.append(c);
4713 else
4714 nd->asciify(&am);
4716 *m = am;
4719 skip_line();
4722 void unformat_macro()
4724 symbol s = get_name(1);
4725 if (!s.is_null()) {
4726 request_or_macro *p = lookup_request(s);
4727 macro *m = p->to_macro();
4728 if (!m)
4729 error("cannot unformat request");
4730 else {
4731 macro am;
4732 string_iterator iter(*m);
4733 for (;;) {
4734 node *nd = 0; // pacify compiler
4735 int c = iter.get(&nd);
4736 if (c == EOF)
4737 break;
4738 if (c != 0)
4739 am.append(c);
4740 else {
4741 if (nd->set_unformat_flag())
4742 am.append(nd);
4745 *m = am;
4748 skip_line();
4751 static void interpolate_environment_variable(symbol nm)
4753 const char *s = getenv(nm.contents());
4754 if (s && *s)
4755 input_stack::push(make_temp_iterator(s));
4758 void interpolate_number_reg(symbol nm, int inc)
4760 reg *r = lookup_number_reg(nm);
4761 if (inc < 0)
4762 r->decrement();
4763 else if (inc > 0)
4764 r->increment();
4765 input_stack::push(make_temp_iterator(r->get_string()));
4768 static void interpolate_number_format(symbol nm)
4770 reg *r = (reg *)number_reg_dictionary.lookup(nm);
4771 if (r)
4772 input_stack::push(make_temp_iterator(r->get_format()));
4775 static int get_delim_number(units *n, unsigned char si, int prev_value)
4777 token start;
4778 start.next();
4779 if (start.delimiter(1)) {
4780 tok.next();
4781 if (get_number(n, si, prev_value)) {
4782 if (start != tok)
4783 warning(WARN_DELIM, "closing delimiter does not match");
4784 return 1;
4787 return 0;
4790 static int get_delim_number(units *n, unsigned char si)
4792 token start;
4793 start.next();
4794 if (start.delimiter(1)) {
4795 tok.next();
4796 if (get_number(n, si)) {
4797 if (start != tok)
4798 warning(WARN_DELIM, "closing delimiter does not match");
4799 return 1;
4802 return 0;
4805 static int get_line_arg(units *n, unsigned char si, charinfo **cp)
4807 token start;
4808 start.next();
4809 int start_level = input_stack::get_level();
4810 if (!start.delimiter(1))
4811 return 0;
4812 tok.next();
4813 if (get_number(n, si)) {
4814 if (tok.dummy() || tok.transparent_dummy())
4815 tok.next();
4816 if (!(start == tok && input_stack::get_level() == start_level)) {
4817 *cp = tok.get_char(1);
4818 tok.next();
4820 if (!(start == tok && input_stack::get_level() == start_level))
4821 warning(WARN_DELIM, "closing delimiter does not match");
4822 return 1;
4824 return 0;
4827 static int read_size(int *x)
4829 tok.next();
4830 int c = tok.ch();
4831 int inc = 0;
4832 if (c == '-') {
4833 inc = -1;
4834 tok.next();
4835 c = tok.ch();
4837 else if (c == '+') {
4838 inc = 1;
4839 tok.next();
4840 c = tok.ch();
4842 int val = 0; // pacify compiler
4843 int bad = 0;
4844 if (c == '(') {
4845 tok.next();
4846 c = tok.ch();
4847 if (!inc) {
4848 // allow an increment either before or after the left parenthesis
4849 if (c == '-') {
4850 inc = -1;
4851 tok.next();
4852 c = tok.ch();
4854 else if (c == '+') {
4855 inc = 1;
4856 tok.next();
4857 c = tok.ch();
4860 if (!csdigit(c))
4861 bad = 1;
4862 else {
4863 val = c - '0';
4864 tok.next();
4865 c = tok.ch();
4866 if (!csdigit(c))
4867 bad = 1;
4868 else {
4869 val = val*10 + (c - '0');
4870 val *= sizescale;
4874 else if (csdigit(c)) {
4875 val = c - '0';
4876 if (!inc && c != '0' && c < '4') {
4877 tok.next();
4878 c = tok.ch();
4879 if (!csdigit(c))
4880 bad = 1;
4881 else
4882 val = val*10 + (c - '0');
4884 val *= sizescale;
4886 else if (!tok.delimiter(1))
4887 return 0;
4888 else {
4889 token start(tok);
4890 tok.next();
4891 if (!(inc
4892 ? get_number(&val, 'z')
4893 : get_number(&val, 'z', curenv->get_requested_point_size())))
4894 return 0;
4895 if (!(start.ch() == '[' && tok.ch() == ']') && start != tok) {
4896 if (start.ch() == '[')
4897 error("missing `]'");
4898 else
4899 error("missing closing delimiter");
4900 return 0;
4903 if (!bad) {
4904 switch (inc) {
4905 case 0:
4906 if (val == 0) {
4907 // special case -- \s[0] and \s0 means to revert to previous size
4908 *x = 0;
4909 return 1;
4911 *x = val;
4912 break;
4913 case 1:
4914 *x = curenv->get_requested_point_size() + val;
4915 break;
4916 case -1:
4917 *x = curenv->get_requested_point_size() - val;
4918 break;
4919 default:
4920 assert(0);
4922 if (*x <= 0) {
4923 warning(WARN_RANGE,
4924 "\\s request results in non-positive point size; set to 1");
4925 *x = 1;
4927 return 1;
4929 else {
4930 error("bad digit in point size");
4931 return 0;
4935 static symbol get_delim_name()
4937 token start;
4938 start.next();
4939 if (start.eof()) {
4940 error("end of input at start of delimited name");
4941 return NULL_SYMBOL;
4943 if (start.newline()) {
4944 error("can't delimit name with a newline");
4945 return NULL_SYMBOL;
4947 int start_level = input_stack::get_level();
4948 char abuf[ABUF_SIZE];
4949 char *buf = abuf;
4950 int buf_size = ABUF_SIZE;
4951 int i = 0;
4952 for (;;) {
4953 if (i + 1 > buf_size) {
4954 if (buf == abuf) {
4955 buf = new char[ABUF_SIZE*2];
4956 memcpy(buf, abuf, buf_size);
4957 buf_size = ABUF_SIZE*2;
4959 else {
4960 char *old_buf = buf;
4961 buf = new char[buf_size*2];
4962 memcpy(buf, old_buf, buf_size);
4963 buf_size *= 2;
4964 a_delete old_buf;
4967 tok.next();
4968 if (tok == start
4969 && (compatible_flag || input_stack::get_level() == start_level))
4970 break;
4971 if ((buf[i] = tok.ch()) == 0) {
4972 error("missing delimiter (got %1)", tok.description());
4973 if (buf != abuf)
4974 a_delete buf;
4975 return NULL_SYMBOL;
4977 i++;
4979 buf[i] = '\0';
4980 if (buf == abuf) {
4981 if (i == 0) {
4982 error("empty delimited name");
4983 return NULL_SYMBOL;
4985 else
4986 return symbol(buf);
4988 else {
4989 symbol s(buf);
4990 a_delete buf;
4991 return s;
4995 // Implement \R
4997 static void do_register()
4999 token start;
5000 start.next();
5001 if (!start.delimiter(1))
5002 return;
5003 tok.next();
5004 symbol nm = get_long_name(1);
5005 if (nm.is_null())
5006 return;
5007 while (tok.space())
5008 tok.next();
5009 reg *r = (reg *)number_reg_dictionary.lookup(nm);
5010 int prev_value;
5011 if (!r || !r->get_value(&prev_value))
5012 prev_value = 0;
5013 int val;
5014 if (!get_number(&val, 'u', prev_value))
5015 return;
5016 if (start != tok)
5017 warning(WARN_DELIM, "closing delimiter does not match");
5018 if (r)
5019 r->set_value(val);
5020 else
5021 set_number_reg(nm, val);
5024 // this implements the \w escape sequence
5026 static void do_width()
5028 token start;
5029 start.next();
5030 int start_level = input_stack::get_level();
5031 environment env(curenv);
5032 environment *oldenv = curenv;
5033 curenv = &env;
5034 for (;;) {
5035 tok.next();
5036 if (tok.eof()) {
5037 warning(WARN_DELIM, "missing closing delimiter");
5038 break;
5040 if (tok.newline()) {
5041 warning(WARN_DELIM, "missing closing delimiter");
5042 input_stack::push(make_temp_iterator("\n"));
5043 break;
5045 if (tok == start
5046 && (compatible_flag || input_stack::get_level() == start_level))
5047 break;
5048 tok.process();
5050 env.wrap_up_tab();
5051 units x = env.get_input_line_position().to_units();
5052 input_stack::push(make_temp_iterator(i_to_a(x)));
5053 env.width_registers();
5054 curenv = oldenv;
5055 have_input = 0;
5058 charinfo *page_character;
5060 void set_page_character()
5062 page_character = get_optional_char();
5063 skip_line();
5066 static const symbol percent_symbol("%");
5068 void read_title_parts(node **part, hunits *part_width)
5070 tok.skip();
5071 if (tok.newline() || tok.eof())
5072 return;
5073 token start(tok);
5074 int start_level = input_stack::get_level();
5075 tok.next();
5076 for (int i = 0; i < 3; i++) {
5077 while (!tok.newline() && !tok.eof()) {
5078 if (tok == start
5079 && (compatible_flag || input_stack::get_level() == start_level)) {
5080 tok.next();
5081 break;
5083 if (page_character != 0 && tok.get_char() == page_character)
5084 interpolate_number_reg(percent_symbol, 0);
5085 else
5086 tok.process();
5087 tok.next();
5089 curenv->wrap_up_tab();
5090 part_width[i] = curenv->get_input_line_position();
5091 part[i] = curenv->extract_output_line();
5093 while (!tok.newline() && !tok.eof())
5094 tok.next();
5097 class non_interpreted_node : public node {
5098 macro mac;
5099 public:
5100 non_interpreted_node(const macro &);
5101 int interpret(macro *);
5102 node *copy();
5103 int ends_sentence();
5104 int same(node *);
5105 const char *type();
5106 int force_tprint();
5107 int is_tag();
5110 non_interpreted_node::non_interpreted_node(const macro &m) : mac(m)
5114 int non_interpreted_node::ends_sentence()
5116 return 2;
5119 int non_interpreted_node::same(node *nd)
5121 return mac == ((non_interpreted_node *)nd)->mac;
5124 const char *non_interpreted_node::type()
5126 return "non_interpreted_node";
5129 int non_interpreted_node::force_tprint()
5131 return 0;
5134 int non_interpreted_node::is_tag()
5136 return 0;
5139 node *non_interpreted_node::copy()
5141 return new non_interpreted_node(mac);
5144 int non_interpreted_node::interpret(macro *m)
5146 string_iterator si(mac);
5147 node *n = 0; // pacify compiler
5148 for (;;) {
5149 int c = si.get(&n);
5150 if (c == EOF)
5151 break;
5152 if (c == 0)
5153 m->append(n);
5154 else
5155 m->append(c);
5157 return 1;
5160 static node *do_non_interpreted()
5162 node *n;
5163 int c;
5164 macro mac;
5165 while ((c = get_copy(&n)) != ESCAPE_QUESTION && c != EOF && c != '\n')
5166 if (c == 0)
5167 mac.append(n);
5168 else
5169 mac.append(c);
5170 if (c == EOF || c == '\n') {
5171 error("missing \\?");
5172 return 0;
5174 return new non_interpreted_node(mac);
5177 static void encode_char(macro *mac, char c)
5179 if (c == '\0') {
5180 if ((font::use_charnames_in_special) && tok.special()) {
5181 charinfo *ci = tok.get_char(1);
5182 const char *s = ci->get_symbol()->contents();
5183 if (s[0] != (char)0) {
5184 mac->append('\\');
5185 mac->append('(');
5186 int i = 0;
5187 while (s[i] != (char)0) {
5188 mac->append(s[i]);
5189 i++;
5191 mac->append('\\');
5192 mac->append(')');
5195 else if (tok.stretchable_space()
5196 || tok.unstretchable_space())
5197 mac->append(' ');
5198 else if (!(tok.hyphen_indicator()
5199 || tok.dummy()
5200 || tok.transparent_dummy()
5201 || tok.zero_width_break()))
5202 error("%1 is invalid within \\X", tok.description());
5204 else {
5205 if ((font::use_charnames_in_special) && (c == '\\')) {
5207 * add escape escape sequence
5209 mac->append(c);
5211 mac->append(c);
5215 node *do_special()
5217 token start;
5218 start.next();
5219 int start_level = input_stack::get_level();
5220 macro mac;
5221 for (tok.next();
5222 tok != start || input_stack::get_level() != start_level;
5223 tok.next()) {
5224 if (tok.eof()) {
5225 warning(WARN_DELIM, "missing closing delimiter");
5226 return 0;
5228 if (tok.newline()) {
5229 input_stack::push(make_temp_iterator("\n"));
5230 warning(WARN_DELIM, "missing closing delimiter");
5231 break;
5233 unsigned char c;
5234 if (tok.space())
5235 c = ' ';
5236 else if (tok.tab())
5237 c = '\t';
5238 else if (tok.leader())
5239 c = '\001';
5240 else if (tok.backspace())
5241 c = '\b';
5242 else
5243 c = tok.ch();
5244 encode_char(&mac, c);
5246 return new special_node(mac);
5249 void output_request()
5251 if (!tok.newline() && !tok.eof()) {
5252 int c;
5253 for (;;) {
5254 c = get_copy(0);
5255 if (c == '"') {
5256 c = get_copy(0);
5257 break;
5259 if (c != ' ' && c != '\t')
5260 break;
5262 for (; c != '\n' && c != EOF; c = get_copy(0))
5263 topdiv->transparent_output(c);
5264 topdiv->transparent_output('\n');
5266 tok.next();
5269 extern int image_no; // from node.cpp
5271 static node *do_suppress(symbol nm)
5273 if (nm.is_null() || nm.is_empty()) {
5274 error("expecting an argument to escape \\O");
5275 return 0;
5277 const char *s = nm.contents();
5278 switch (*s) {
5279 case '0':
5280 if (begin_level == 0)
5281 // suppress generation of glyphs
5282 return new suppress_node(0, 0);
5283 break;
5284 case '1':
5285 if (begin_level == 0)
5286 // enable generation of glyphs
5287 return new suppress_node(1, 0);
5288 break;
5289 case '2':
5290 if (begin_level == 0)
5291 return new suppress_node(1, 1);
5292 break;
5293 case '3':
5294 begin_level++;
5295 break;
5296 case '4':
5297 begin_level--;
5298 break;
5299 case '5':
5301 s++; // move over '5'
5302 char position = *s;
5303 if (*s == (char)0) {
5304 error("missing position and filename in \\O");
5305 return 0;
5307 if (!(position == 'l'
5308 || position == 'r'
5309 || position == 'c'
5310 || position == 'i')) {
5311 error("l, r, c, or i position expected (got %1 in \\O)", position);
5312 return 0;
5314 s++; // onto image name
5315 if (s == (char *)0) {
5316 error("missing image name for \\O");
5317 return 0;
5319 image_no++;
5320 if (begin_level == 0)
5321 return new suppress_node(symbol(s), position, image_no);
5323 break;
5324 default:
5325 error("`%1' is an invalid argument to \\O", *s);
5327 return 0;
5330 void special_node::tprint(troff_output_file *out)
5332 tprint_start(out);
5333 string_iterator iter(mac);
5334 for (;;) {
5335 int c = iter.get(0);
5336 if (c == EOF)
5337 break;
5338 for (const char *s = ::asciify(c); *s; s++)
5339 tprint_char(out, *s);
5341 tprint_end(out);
5344 int get_file_line(const char **filename, int *lineno)
5346 return input_stack::get_location(0, filename, lineno);
5349 void line_file()
5351 int n;
5352 if (get_integer(&n)) {
5353 const char *filename = 0;
5354 if (has_arg()) {
5355 symbol s = get_long_name();
5356 filename = s.contents();
5358 (void)input_stack::set_location(filename, n-1);
5360 skip_line();
5363 static int nroff_mode = 0;
5365 static void nroff_request()
5367 nroff_mode = 1;
5368 skip_line();
5371 static void troff_request()
5373 nroff_mode = 0;
5374 skip_line();
5377 static void skip_alternative()
5379 int level = 0;
5380 // ensure that ``.if 0\{'' works as expected
5381 if (tok.left_brace())
5382 level++;
5383 int c;
5384 for (;;) {
5385 c = input_stack::get(0);
5386 if (c == EOF)
5387 break;
5388 if (c == ESCAPE_LEFT_BRACE)
5389 ++level;
5390 else if (c == ESCAPE_RIGHT_BRACE)
5391 --level;
5392 else if (c == escape_char && escape_char > 0)
5393 switch(input_stack::get(0)) {
5394 case '{':
5395 ++level;
5396 break;
5397 case '}':
5398 --level;
5399 break;
5400 case '"':
5401 while ((c = input_stack::get(0)) != '\n' && c != EOF)
5405 Note that the level can properly be < 0, eg
5407 .if 1 \{\
5408 .if 0 \{\
5409 .\}\}
5411 So don't give an error message in this case.
5413 if (level <= 0 && c == '\n')
5414 break;
5416 tok.next();
5419 static void begin_alternative()
5421 while (tok.space() || tok.left_brace())
5422 tok.next();
5425 void nop_request()
5427 while (tok.space())
5428 tok.next();
5431 static int_stack if_else_stack;
5433 int do_if_request()
5435 int invert = 0;
5436 while (tok.space())
5437 tok.next();
5438 while (tok.ch() == '!') {
5439 tok.next();
5440 invert = !invert;
5442 int result;
5443 unsigned char c = tok.ch();
5444 if (c == 't') {
5445 tok.next();
5446 result = !nroff_mode;
5448 else if (c == 'n') {
5449 tok.next();
5450 result = nroff_mode;
5452 else if (c == 'v') {
5453 tok.next();
5454 result = 0;
5456 else if (c == 'o') {
5457 result = (topdiv->get_page_number() & 1);
5458 tok.next();
5460 else if (c == 'e') {
5461 result = !(topdiv->get_page_number() & 1);
5462 tok.next();
5464 else if (c == 'd' || c == 'r') {
5465 tok.next();
5466 symbol nm = get_name(1);
5467 if (nm.is_null()) {
5468 skip_alternative();
5469 return 0;
5471 result = (c == 'd'
5472 ? request_dictionary.lookup(nm) != 0
5473 : number_reg_dictionary.lookup(nm) != 0);
5475 else if (c == 'm') {
5476 tok.next();
5477 symbol nm = get_long_name(1);
5478 if (nm.is_null()) {
5479 skip_alternative();
5480 return 0;
5482 result = (nm == default_symbol
5483 || color_dictionary.lookup(nm) != 0);
5485 else if (c == 'c') {
5486 tok.next();
5487 tok.skip();
5488 charinfo *ci = tok.get_char(1);
5489 if (ci == 0) {
5490 skip_alternative();
5491 return 0;
5493 result = character_exists(ci, curenv);
5494 tok.next();
5496 else if (c == 'F') {
5497 tok.next();
5498 symbol nm = get_long_name(1);
5499 if (nm.is_null()) {
5500 skip_alternative();
5501 return 0;
5503 result = check_font(curenv->get_family()->nm, nm);
5505 else if (c == 'S') {
5506 tok.next();
5507 symbol nm = get_long_name(1);
5508 if (nm.is_null()) {
5509 skip_alternative();
5510 return 0;
5512 result = check_style(nm);
5514 else if (tok.space())
5515 result = 0;
5516 else if (tok.delimiter()) {
5517 token delim = tok;
5518 int delim_level = input_stack::get_level();
5519 environment env1(curenv);
5520 environment env2(curenv);
5521 environment *oldenv = curenv;
5522 curenv = &env1;
5523 suppress_push = 1;
5524 for (int i = 0; i < 2; i++) {
5525 for (;;) {
5526 tok.next();
5527 if (tok.newline() || tok.eof()) {
5528 warning(WARN_DELIM, "missing closing delimiter");
5529 tok.next();
5530 curenv = oldenv;
5531 return 0;
5533 if (tok == delim
5534 && (compatible_flag || input_stack::get_level() == delim_level))
5535 break;
5536 tok.process();
5538 curenv = &env2;
5540 node *n1 = env1.extract_output_line();
5541 node *n2 = env2.extract_output_line();
5542 result = same_node_list(n1, n2);
5543 delete_node_list(n1);
5544 delete_node_list(n2);
5545 curenv = oldenv;
5546 have_input = 0;
5547 suppress_push = 0;
5548 tok.next();
5550 else {
5551 units n;
5552 if (!get_number(&n, 'u')) {
5553 skip_alternative();
5554 return 0;
5556 else
5557 result = n > 0;
5559 if (invert)
5560 result = !result;
5561 if (result)
5562 begin_alternative();
5563 else
5564 skip_alternative();
5565 return result;
5568 void if_else_request()
5570 if_else_stack.push(do_if_request());
5573 void if_request()
5575 do_if_request();
5578 void else_request()
5580 if (if_else_stack.is_empty()) {
5581 warning(WARN_EL, "unbalanced .el request");
5582 skip_alternative();
5584 else {
5585 if (if_else_stack.pop())
5586 skip_alternative();
5587 else
5588 begin_alternative();
5592 static int while_depth = 0;
5593 static int while_break_flag = 0;
5595 void while_request()
5597 macro mac;
5598 int escaped = 0;
5599 int level = 0;
5600 mac.append(new token_node(tok));
5601 for (;;) {
5602 node *n = 0; // pacify compiler
5603 int c = input_stack::get(&n);
5604 if (c == EOF)
5605 break;
5606 if (c == 0) {
5607 escaped = 0;
5608 mac.append(n);
5610 else if (escaped) {
5611 if (c == '{')
5612 level += 1;
5613 else if (c == '}')
5614 level -= 1;
5615 escaped = 0;
5616 mac.append(c);
5618 else {
5619 if (c == ESCAPE_LEFT_BRACE)
5620 level += 1;
5621 else if (c == ESCAPE_RIGHT_BRACE)
5622 level -= 1;
5623 else if (c == escape_char)
5624 escaped = 1;
5625 mac.append(c);
5626 if (c == '\n' && level <= 0)
5627 break;
5630 if (level != 0)
5631 error("unbalanced \\{ \\}");
5632 else {
5633 while_depth++;
5634 input_stack::add_boundary();
5635 for (;;) {
5636 input_stack::push(new string_iterator(mac, "while loop"));
5637 tok.next();
5638 if (!do_if_request()) {
5639 while (input_stack::get(0) != EOF)
5641 break;
5643 process_input_stack();
5644 if (while_break_flag || input_stack::is_return_boundary()) {
5645 while_break_flag = 0;
5646 break;
5649 input_stack::remove_boundary();
5650 while_depth--;
5652 tok.next();
5655 void while_break_request()
5657 if (!while_depth) {
5658 error("no while loop");
5659 skip_line();
5661 else {
5662 while_break_flag = 1;
5663 while (input_stack::get(0) != EOF)
5665 tok.next();
5669 void while_continue_request()
5671 if (!while_depth) {
5672 error("no while loop");
5673 skip_line();
5675 else {
5676 while (input_stack::get(0) != EOF)
5678 tok.next();
5682 // .so
5684 void source()
5686 symbol nm = get_long_name(1);
5687 if (nm.is_null())
5688 skip_line();
5689 else {
5690 while (!tok.newline() && !tok.eof())
5691 tok.next();
5692 errno = 0;
5693 FILE *fp = include_search_path.open_file_cautious(nm.contents());
5694 if (fp)
5695 input_stack::push(new file_iterator(fp, nm.contents()));
5696 else
5697 error("can't open `%1': %2", nm.contents(), strerror(errno));
5698 tok.next();
5702 // like .so but use popen()
5704 void pipe_source()
5706 if (safer_flag) {
5707 error(".pso request not allowed in safer mode");
5708 skip_line();
5710 else {
5711 #ifdef POPEN_MISSING
5712 error("pipes not available on this system");
5713 skip_line();
5714 #else /* not POPEN_MISSING */
5715 if (tok.newline() || tok.eof())
5716 error("missing command");
5717 else {
5718 int c;
5719 while ((c = get_copy(0)) == ' ' || c == '\t')
5721 int buf_size = 24;
5722 char *buf = new char[buf_size];
5723 int buf_used = 0;
5724 for (; c != '\n' && c != EOF; c = get_copy(0)) {
5725 const char *s = asciify(c);
5726 int slen = strlen(s);
5727 if (buf_used + slen + 1> buf_size) {
5728 char *old_buf = buf;
5729 int old_buf_size = buf_size;
5730 buf_size *= 2;
5731 buf = new char[buf_size];
5732 memcpy(buf, old_buf, old_buf_size);
5733 a_delete old_buf;
5735 strcpy(buf + buf_used, s);
5736 buf_used += slen;
5738 buf[buf_used] = '\0';
5739 errno = 0;
5740 FILE *fp = popen(buf, POPEN_RT);
5741 if (fp)
5742 input_stack::push(new file_iterator(fp, symbol(buf).contents(), 1));
5743 else
5744 error("can't open pipe to process `%1': %2", buf, strerror(errno));
5745 a_delete buf;
5747 tok.next();
5748 #endif /* not POPEN_MISSING */
5752 // .psbb
5754 static int llx_reg_contents = 0;
5755 static int lly_reg_contents = 0;
5756 static int urx_reg_contents = 0;
5757 static int ury_reg_contents = 0;
5759 struct bounding_box {
5760 int llx, lly, urx, ury;
5763 /* Parse the argument to a %%BoundingBox comment. Return 1 if it
5764 contains 4 numbers, 2 if it contains (atend), 0 otherwise. */
5766 int parse_bounding_box(char *p, bounding_box *bb)
5768 if (sscanf(p, "%d %d %d %d",
5769 &bb->llx, &bb->lly, &bb->urx, &bb->ury) == 4)
5770 return 1;
5771 else {
5772 /* The Document Structuring Conventions say that the numbers
5773 should be integers. Unfortunately some broken applications
5774 get this wrong. */
5775 double x1, x2, x3, x4;
5776 if (sscanf(p, "%lf %lf %lf %lf", &x1, &x2, &x3, &x4) == 4) {
5777 bb->llx = (int)x1;
5778 bb->lly = (int)x2;
5779 bb->urx = (int)x3;
5780 bb->ury = (int)x4;
5781 return 1;
5783 else {
5784 for (; *p == ' ' || *p == '\t'; p++)
5786 if (strncmp(p, "(atend)", 7) == 0) {
5787 return 2;
5791 bb->llx = bb->lly = bb->urx = bb->ury = 0;
5792 return 0;
5795 // This version is taken from psrm.cpp
5797 #define PS_LINE_MAX 255
5798 cset white_space("\n\r \t");
5800 int ps_get_line(char *buf, FILE *fp, const char* filename)
5802 int c = getc(fp);
5803 if (c == EOF) {
5804 buf[0] = '\0';
5805 return 0;
5807 int i = 0;
5808 int err = 0;
5809 while (c != '\r' && c != '\n' && c != EOF) {
5810 if ((c < 0x1b && !white_space(c)) || c == 0x7f)
5811 error("invalid input character code %1 in `%2'", int(c), filename);
5812 else if (i < PS_LINE_MAX)
5813 buf[i++] = c;
5814 else if (!err) {
5815 err = 1;
5816 error("PostScript file `%1' is non-conforming "
5817 "because length of line exceeds 255", filename);
5819 c = getc(fp);
5821 buf[i++] = '\n';
5822 buf[i] = '\0';
5823 if (c == '\r') {
5824 c = getc(fp);
5825 if (c != EOF && c != '\n')
5826 ungetc(c, fp);
5828 return 1;
5831 inline void assign_registers(int llx, int lly, int urx, int ury)
5833 llx_reg_contents = llx;
5834 lly_reg_contents = lly;
5835 urx_reg_contents = urx;
5836 ury_reg_contents = ury;
5839 void do_ps_file(FILE *fp, const char* filename)
5841 bounding_box bb;
5842 int bb_at_end = 0;
5843 char buf[PS_LINE_MAX];
5844 llx_reg_contents = lly_reg_contents =
5845 urx_reg_contents = ury_reg_contents = 0;
5846 if (!ps_get_line(buf, fp, filename)) {
5847 error("`%1' is empty", filename);
5848 return;
5850 if (strncmp("%!PS-Adobe-", buf, 11) != 0) {
5851 error("`%1' is not conforming to the Document Structuring Conventions",
5852 filename);
5853 return;
5855 while (ps_get_line(buf, fp, filename) != 0) {
5856 if (buf[0] != '%' || buf[1] != '%'
5857 || strncmp(buf + 2, "EndComments", 11) == 0)
5858 break;
5859 if (strncmp(buf + 2, "BoundingBox:", 12) == 0) {
5860 int res = parse_bounding_box(buf + 14, &bb);
5861 if (res == 1) {
5862 assign_registers(bb.llx, bb.lly, bb.urx, bb.ury);
5863 return;
5865 else if (res == 2) {
5866 bb_at_end = 1;
5867 break;
5869 else {
5870 error("the arguments to the %%%%BoundingBox comment in `%1' are bad",
5871 filename);
5872 return;
5876 if (bb_at_end) {
5877 long offset;
5878 int last_try = 0;
5879 /* in the trailer, the last BoundingBox comment is significant */
5880 for (offset = 512; !last_try; offset *= 2) {
5881 int had_trailer = 0;
5882 int got_bb = 0;
5883 if (offset > 32768 || fseek(fp, -offset, 2) == -1) {
5884 last_try = 1;
5885 if (fseek(fp, 0L, 0) == -1)
5886 break;
5888 while (ps_get_line(buf, fp, filename) != 0) {
5889 if (buf[0] == '%' && buf[1] == '%') {
5890 if (!had_trailer) {
5891 if (strncmp(buf + 2, "Trailer", 7) == 0)
5892 had_trailer = 1;
5894 else {
5895 if (strncmp(buf + 2, "BoundingBox:", 12) == 0) {
5896 int res = parse_bounding_box(buf + 14, &bb);
5897 if (res == 1)
5898 got_bb = 1;
5899 else if (res == 2) {
5900 error("`(atend)' not allowed in trailer of `%1'", filename);
5901 return;
5903 else {
5904 error("the arguments to the %%%%BoundingBox comment in `%1' are bad",
5905 filename);
5906 return;
5912 if (got_bb) {
5913 assign_registers(bb.llx, bb.lly, bb.urx, bb.ury);
5914 return;
5918 error("%%%%BoundingBox comment not found in `%1'", filename);
5921 void ps_bbox_request()
5923 symbol nm = get_long_name(1);
5924 if (nm.is_null())
5925 skip_line();
5926 else {
5927 while (!tok.newline() && !tok.eof())
5928 tok.next();
5929 errno = 0;
5930 // PS files might contain non-printable characters, such as ^Z
5931 // and CRs not followed by an LF, so open them in binary mode.
5932 FILE *fp = include_search_path.open_file_cautious(nm.contents(),
5933 0, FOPEN_RB);
5934 if (fp) {
5935 do_ps_file(fp, nm.contents());
5936 fclose(fp);
5938 else
5939 error("can't open `%1': %2", nm.contents(), strerror(errno));
5940 tok.next();
5944 const char *asciify(int c)
5946 static char buf[3];
5947 buf[0] = escape_char == '\0' ? '\\' : escape_char;
5948 buf[1] = buf[2] = '\0';
5949 switch (c) {
5950 case ESCAPE_QUESTION:
5951 buf[1] = '?';
5952 break;
5953 case ESCAPE_AMPERSAND:
5954 buf[1] = '&';
5955 break;
5956 case ESCAPE_RIGHT_PARENTHESIS:
5957 buf[1] = ')';
5958 break;
5959 case ESCAPE_UNDERSCORE:
5960 buf[1] = '_';
5961 break;
5962 case ESCAPE_BAR:
5963 buf[1] = '|';
5964 break;
5965 case ESCAPE_CIRCUMFLEX:
5966 buf[1] = '^';
5967 break;
5968 case ESCAPE_LEFT_BRACE:
5969 buf[1] = '{';
5970 break;
5971 case ESCAPE_RIGHT_BRACE:
5972 buf[1] = '}';
5973 break;
5974 case ESCAPE_LEFT_QUOTE:
5975 buf[1] = '`';
5976 break;
5977 case ESCAPE_RIGHT_QUOTE:
5978 buf[1] = '\'';
5979 break;
5980 case ESCAPE_HYPHEN:
5981 buf[1] = '-';
5982 break;
5983 case ESCAPE_BANG:
5984 buf[1] = '!';
5985 break;
5986 case ESCAPE_c:
5987 buf[1] = 'c';
5988 break;
5989 case ESCAPE_e:
5990 buf[1] = 'e';
5991 break;
5992 case ESCAPE_E:
5993 buf[1] = 'E';
5994 break;
5995 case ESCAPE_PERCENT:
5996 buf[1] = '%';
5997 break;
5998 case ESCAPE_SPACE:
5999 buf[1] = ' ';
6000 break;
6001 case ESCAPE_TILDE:
6002 buf[1] = '~';
6003 break;
6004 case ESCAPE_COLON:
6005 buf[1] = ':';
6006 break;
6007 case PUSH_GROFF_MODE:
6008 case PUSH_COMP_MODE:
6009 case POP_GROFFCOMP_MODE:
6010 buf[0] = '\0';
6011 break;
6012 default:
6013 if (invalid_input_char(c))
6014 buf[0] = '\0';
6015 else
6016 buf[0] = c;
6017 break;
6019 return buf;
6022 const char *input_char_description(int c)
6024 switch (c) {
6025 case '\n':
6026 return "a newline character";
6027 case '\b':
6028 return "a backspace character";
6029 case '\001':
6030 return "a leader character";
6031 case '\t':
6032 return "a tab character";
6033 case ' ':
6034 return "a space character";
6035 case '\0':
6036 return "a node";
6038 static char buf[sizeof("magic character code ") + 1 + INT_DIGITS];
6039 if (invalid_input_char(c)) {
6040 const char *s = asciify(c);
6041 if (*s) {
6042 buf[0] = '`';
6043 strcpy(buf + 1, s);
6044 strcat(buf, "'");
6045 return buf;
6047 sprintf(buf, "magic character code %d", c);
6048 return buf;
6050 if (csprint(c)) {
6051 buf[0] = '`';
6052 buf[1] = c;
6053 buf[2] = '\'';
6054 return buf;
6056 sprintf(buf, "character code %d", c);
6057 return buf;
6060 void tag()
6062 if (!tok.newline() && !tok.eof()) {
6063 string s;
6064 int c;
6065 for (;;) {
6066 c = get_copy(0);
6067 if (c == '"') {
6068 c = get_copy(0);
6069 break;
6071 if (c != ' ' && c != '\t')
6072 break;
6074 s = "x X ";
6075 for (; c != '\n' && c != EOF; c = get_copy(0))
6076 s += (char)c;
6077 s += '\n';
6078 if (is_html)
6079 curenv->add_node(new tag_node(s, 0));
6081 tok.next();
6084 void taga()
6086 if (!tok.newline() && !tok.eof()) {
6087 string s;
6088 int c;
6089 for (;;) {
6090 c = get_copy(0);
6091 if (c == '"') {
6092 c = get_copy(0);
6093 break;
6095 if (c != ' ' && c != '\t')
6096 break;
6098 s = "x X ";
6099 for (; c != '\n' && c != EOF; c = get_copy(0))
6100 s += (char)c;
6101 s += '\n';
6102 if (is_html)
6103 curenv->add_node(new tag_node(s, 1));
6105 tok.next();
6108 // .tm, .tm1, and .tmc
6110 void do_terminal(int newline, int string_like)
6112 if (!tok.newline() && !tok.eof()) {
6113 int c;
6114 for (;;) {
6115 c = get_copy(0);
6116 if (string_like && c == '"') {
6117 c = get_copy(0);
6118 break;
6120 if (c != ' ' && c != '\t')
6121 break;
6123 for (; c != '\n' && c != EOF; c = get_copy(0))
6124 fputs(asciify(c), stderr);
6126 if (newline)
6127 fputc('\n', stderr);
6128 fflush(stderr);
6129 tok.next();
6132 void terminal()
6134 do_terminal(1, 0);
6137 void terminal1()
6139 do_terminal(1, 1);
6142 void terminal_continue()
6144 do_terminal(0, 1);
6147 dictionary stream_dictionary(20);
6149 void do_open(int append)
6151 symbol stream = get_name(1);
6152 if (!stream.is_null()) {
6153 symbol filename = get_long_name(1);
6154 if (!filename.is_null()) {
6155 errno = 0;
6156 FILE *fp = fopen(filename.contents(), append ? "a" : "w");
6157 if (!fp) {
6158 error("can't open `%1' for %2: %3",
6159 filename.contents(),
6160 append ? "appending" : "writing",
6161 strerror(errno));
6162 fp = (FILE *)stream_dictionary.remove(stream);
6164 else
6165 fp = (FILE *)stream_dictionary.lookup(stream, fp);
6166 if (fp)
6167 fclose(fp);
6170 skip_line();
6173 void open_request()
6175 if (safer_flag) {
6176 error(".open request not allowed in safer mode");
6177 skip_line();
6179 else
6180 do_open(0);
6183 void opena_request()
6185 if (safer_flag) {
6186 error(".opena request not allowed in safer mode");
6187 skip_line();
6189 else
6190 do_open(1);
6193 void close_request()
6195 symbol stream = get_name(1);
6196 if (!stream.is_null()) {
6197 FILE *fp = (FILE *)stream_dictionary.remove(stream);
6198 if (!fp)
6199 error("no stream named `%1'", stream.contents());
6200 else
6201 fclose(fp);
6203 skip_line();
6206 // .write and .writec
6208 void do_write_request(int newline)
6210 symbol stream = get_name(1);
6211 if (stream.is_null()) {
6212 skip_line();
6213 return;
6215 FILE *fp = (FILE *)stream_dictionary.lookup(stream);
6216 if (!fp) {
6217 error("no stream named `%1'", stream.contents());
6218 skip_line();
6219 return;
6221 int c;
6222 while ((c = get_copy(0)) == ' ')
6224 if (c == '"')
6225 c = get_copy(0);
6226 for (; c != '\n' && c != EOF; c = get_copy(0))
6227 fputs(asciify(c), fp);
6228 if (newline)
6229 fputc('\n', fp);
6230 fflush(fp);
6231 tok.next();
6234 void write_request()
6236 do_write_request(1);
6239 void write_request_continue()
6241 do_write_request(0);
6244 void write_macro_request()
6246 symbol stream = get_name(1);
6247 if (stream.is_null()) {
6248 skip_line();
6249 return;
6251 FILE *fp = (FILE *)stream_dictionary.lookup(stream);
6252 if (!fp) {
6253 error("no stream named `%1'", stream.contents());
6254 skip_line();
6255 return;
6257 symbol s = get_name(1);
6258 if (s.is_null()) {
6259 skip_line();
6260 return;
6262 request_or_macro *p = lookup_request(s);
6263 macro *m = p->to_macro();
6264 if (!m)
6265 error("cannot write request");
6266 else {
6267 string_iterator iter(*m);
6268 for (;;) {
6269 int c = iter.get(0);
6270 if (c == EOF)
6271 break;
6272 fputs(asciify(c), fp);
6274 fflush(fp);
6276 skip_line();
6279 void warnscale_request()
6281 if (has_arg()) {
6282 char c = tok.ch();
6283 if (c == 'u')
6284 warn_scale = 1.0;
6285 else if (c == 'i')
6286 warn_scale = (double)units_per_inch;
6287 else if (c == 'c')
6288 warn_scale = (double)units_per_inch / 2.54;
6289 else if (c == 'p')
6290 warn_scale = (double)units_per_inch / 72.0;
6291 else if (c == 'P')
6292 warn_scale = (double)units_per_inch / 6.0;
6293 else {
6294 warning(WARN_SCALE,
6295 "invalid scaling indicator `%1', using `i' instead", c);
6296 c = 'i';
6298 warn_scaling_indicator = c;
6300 skip_line();
6303 void spreadwarn_request()
6305 hunits n;
6306 if (has_arg() && get_hunits(&n, 'm')) {
6307 if (n < 0)
6308 n = 0;
6309 hunits em = curenv->get_size();
6310 spread_limit = (double)n.to_units()
6311 / (em.is_zero() ? hresolution : em.to_units());
6313 else
6314 spread_limit = -spread_limit - 1; // no arg toggles on/off without
6315 // changing value; we mirror at
6316 // -0.5 to make zero a valid value
6317 skip_line();
6320 static void init_charset_table()
6322 char buf[16];
6323 strcpy(buf, "char");
6324 for (int i = 0; i < 256; i++) {
6325 strcpy(buf + 4, i_to_a(i));
6326 charset_table[i] = get_charinfo(symbol(buf));
6327 charset_table[i]->set_ascii_code(i);
6328 if (csalpha(i))
6329 charset_table[i]->set_hyphenation_code(cmlower(i));
6331 charset_table['.']->set_flags(charinfo::ENDS_SENTENCE);
6332 charset_table['?']->set_flags(charinfo::ENDS_SENTENCE);
6333 charset_table['!']->set_flags(charinfo::ENDS_SENTENCE);
6334 charset_table['-']->set_flags(charinfo::BREAK_AFTER);
6335 charset_table['"']->set_flags(charinfo::TRANSPARENT);
6336 charset_table['\'']->set_flags(charinfo::TRANSPARENT);
6337 charset_table[')']->set_flags(charinfo::TRANSPARENT);
6338 charset_table[']']->set_flags(charinfo::TRANSPARENT);
6339 charset_table['*']->set_flags(charinfo::TRANSPARENT);
6340 get_charinfo(symbol("dg"))->set_flags(charinfo::TRANSPARENT);
6341 get_charinfo(symbol("rq"))->set_flags(charinfo::TRANSPARENT);
6342 get_charinfo(symbol("em"))->set_flags(charinfo::BREAK_AFTER);
6343 get_charinfo(symbol("ul"))->set_flags(charinfo::OVERLAPS_HORIZONTALLY);
6344 get_charinfo(symbol("rn"))->set_flags(charinfo::OVERLAPS_HORIZONTALLY);
6345 get_charinfo(symbol("radicalex"))->set_flags(charinfo::OVERLAPS_HORIZONTALLY);
6346 get_charinfo(symbol("sqrtex"))->set_flags(charinfo::OVERLAPS_HORIZONTALLY);
6347 get_charinfo(symbol("ru"))->set_flags(charinfo::OVERLAPS_HORIZONTALLY);
6348 get_charinfo(symbol("br"))->set_flags(charinfo::OVERLAPS_VERTICALLY);
6349 page_character = charset_table['%'];
6352 static void init_hpf_code_table()
6354 for (int i = 0; i < 256; i++)
6355 hpf_code_table[i] = i;
6358 static void do_translate(int translate_transparent, int translate_input)
6360 tok.skip();
6361 while (!tok.newline() && !tok.eof()) {
6362 if (tok.space()) {
6363 // This is a really bizarre troff feature.
6364 tok.next();
6365 translate_space_to_dummy = tok.dummy();
6366 if (tok.newline() || tok.eof())
6367 break;
6368 tok.next();
6369 continue;
6371 charinfo *ci1 = tok.get_char(1);
6372 if (ci1 == 0)
6373 break;
6374 tok.next();
6375 if (tok.newline() || tok.eof()) {
6376 ci1->set_special_translation(charinfo::TRANSLATE_SPACE,
6377 translate_transparent);
6378 break;
6380 if (tok.space())
6381 ci1->set_special_translation(charinfo::TRANSLATE_SPACE,
6382 translate_transparent);
6383 else if (tok.stretchable_space())
6384 ci1->set_special_translation(charinfo::TRANSLATE_STRETCHABLE_SPACE,
6385 translate_transparent);
6386 else if (tok.dummy())
6387 ci1->set_special_translation(charinfo::TRANSLATE_DUMMY,
6388 translate_transparent);
6389 else if (tok.hyphen_indicator())
6390 ci1->set_special_translation(charinfo::TRANSLATE_HYPHEN_INDICATOR,
6391 translate_transparent);
6392 else {
6393 charinfo *ci2 = tok.get_char(1);
6394 if (ci2 == 0)
6395 break;
6396 if (ci1 == ci2)
6397 ci1->set_translation(0, translate_transparent, translate_input);
6398 else
6399 ci1->set_translation(ci2, translate_transparent, translate_input);
6401 tok.next();
6403 skip_line();
6406 void translate()
6408 do_translate(1, 0);
6411 void translate_no_transparent()
6413 do_translate(0, 0);
6416 void translate_input()
6418 do_translate(1, 1);
6421 void char_flags()
6423 int flags;
6424 if (get_integer(&flags))
6425 while (has_arg()) {
6426 charinfo *ci = tok.get_char(1);
6427 if (ci) {
6428 charinfo *tem = ci->get_translation();
6429 if (tem)
6430 ci = tem;
6431 ci->set_flags(flags);
6433 tok.next();
6435 skip_line();
6438 void hyphenation_code()
6440 tok.skip();
6441 while (!tok.newline() && !tok.eof()) {
6442 charinfo *ci = tok.get_char(1);
6443 if (ci == 0)
6444 break;
6445 tok.next();
6446 tok.skip();
6447 unsigned char c = tok.ch();
6448 if (c == 0) {
6449 error("hyphenation code must be ordinary character");
6450 break;
6452 if (csdigit(c)) {
6453 error("hyphenation code cannot be digit");
6454 break;
6456 ci->set_hyphenation_code(c);
6457 if (ci->get_translation()
6458 && ci->get_translation()->get_translation_input())
6459 ci->get_translation()->set_hyphenation_code(c);
6460 tok.next();
6461 tok.skip();
6463 skip_line();
6466 void hyphenation_patterns_file_code()
6468 tok.skip();
6469 while (!tok.newline() && !tok.eof()) {
6470 int n1, n2;
6471 if (get_integer(&n1) && (0 <= n1 && n1 <= 255)) {
6472 if (!has_arg()) {
6473 error("missing output hyphenation code");
6474 break;
6476 if (get_integer(&n2) && (0 <= n2 && n2 <= 255)) {
6477 hpf_code_table[n1] = n2;
6478 tok.skip();
6480 else {
6481 error("output hyphenation code must be integer in the range 0..255");
6482 break;
6485 else {
6486 error("input hyphenation code must be integer in the range 0..255");
6487 break;
6490 skip_line();
6493 charinfo *token::get_char(int required)
6495 if (type == TOKEN_CHAR)
6496 return charset_table[c];
6497 if (type == TOKEN_SPECIAL)
6498 return get_charinfo(nm);
6499 if (type == TOKEN_NUMBERED_CHAR)
6500 return get_charinfo_by_number(val);
6501 if (type == TOKEN_ESCAPE) {
6502 if (escape_char != 0)
6503 return charset_table[escape_char];
6504 else {
6505 error("`\\e' used while no current escape character");
6506 return 0;
6509 if (required) {
6510 if (type == TOKEN_EOF || type == TOKEN_NEWLINE)
6511 warning(WARN_MISSING, "missing normal or special character");
6512 else
6513 error("normal or special character expected (got %1)", description());
6515 return 0;
6518 charinfo *get_optional_char()
6520 while (tok.space())
6521 tok.next();
6522 charinfo *ci = tok.get_char();
6523 if (!ci)
6524 check_missing_character();
6525 else
6526 tok.next();
6527 return ci;
6530 void check_missing_character()
6532 if (!tok.newline() && !tok.eof() && !tok.right_brace() && !tok.tab())
6533 error("normal or special character expected (got %1): "
6534 "treated as missing",
6535 tok.description());
6538 // this is for \Z
6540 int token::add_to_node_list(node **pp)
6542 hunits w;
6543 int s;
6544 node *n = 0;
6545 switch (type) {
6546 case TOKEN_CHAR:
6547 *pp = (*pp)->add_char(charset_table[c], curenv, &w, &s);
6548 break;
6549 case TOKEN_DUMMY:
6550 n = new dummy_node;
6551 break;
6552 case TOKEN_ESCAPE:
6553 if (escape_char != 0)
6554 *pp = (*pp)->add_char(charset_table[escape_char], curenv, &w, &s);
6555 break;
6556 case TOKEN_HYPHEN_INDICATOR:
6557 *pp = (*pp)->add_discretionary_hyphen();
6558 break;
6559 case TOKEN_ITALIC_CORRECTION:
6560 *pp = (*pp)->add_italic_correction(&w);
6561 break;
6562 case TOKEN_LEFT_BRACE:
6563 break;
6564 case TOKEN_MARK_INPUT:
6565 set_number_reg(nm, curenv->get_input_line_position().to_units());
6566 break;
6567 case TOKEN_NODE:
6568 n = nd;
6569 nd = 0;
6570 break;
6571 case TOKEN_NUMBERED_CHAR:
6572 *pp = (*pp)->add_char(get_charinfo_by_number(val), curenv, &w, &s);
6573 break;
6574 case TOKEN_RIGHT_BRACE:
6575 break;
6576 case TOKEN_SPACE:
6577 n = new hmotion_node(curenv->get_space_width(),
6578 curenv->get_fill_color());
6579 break;
6580 case TOKEN_SPECIAL:
6581 *pp = (*pp)->add_char(get_charinfo(nm), curenv, &w, &s);
6582 break;
6583 case TOKEN_STRETCHABLE_SPACE:
6584 n = new unbreakable_space_node(curenv->get_space_width(),
6585 curenv->get_fill_color());
6586 break;
6587 case TOKEN_UNSTRETCHABLE_SPACE:
6588 n = new space_char_hmotion_node(curenv->get_space_width(),
6589 curenv->get_fill_color());
6590 break;
6591 case TOKEN_TRANSPARENT_DUMMY:
6592 n = new transparent_dummy_node;
6593 break;
6594 case TOKEN_ZERO_WIDTH_BREAK:
6595 n = new space_node(H0, curenv->get_fill_color());
6596 n->freeze_space();
6597 n->is_escape_colon();
6598 break;
6599 default:
6600 return 0;
6602 if (n) {
6603 n->next = *pp;
6604 *pp = n;
6606 return 1;
6609 void token::process()
6611 if (possibly_handle_first_page_transition())
6612 return;
6613 switch (type) {
6614 case TOKEN_BACKSPACE:
6615 curenv->add_node(new hmotion_node(-curenv->get_space_width(),
6616 curenv->get_fill_color()));
6617 break;
6618 case TOKEN_CHAR:
6619 curenv->add_char(charset_table[c]);
6620 break;
6621 case TOKEN_DUMMY:
6622 curenv->add_node(new dummy_node);
6623 break;
6624 case TOKEN_EMPTY:
6625 assert(0);
6626 break;
6627 case TOKEN_EOF:
6628 assert(0);
6629 break;
6630 case TOKEN_ESCAPE:
6631 if (escape_char != 0)
6632 curenv->add_char(charset_table[escape_char]);
6633 break;
6634 case TOKEN_BEGIN_TRAP:
6635 case TOKEN_END_TRAP:
6636 case TOKEN_PAGE_EJECTOR:
6637 // these are all handled in process_input_stack()
6638 break;
6639 case TOKEN_HYPHEN_INDICATOR:
6640 curenv->add_hyphen_indicator();
6641 break;
6642 case TOKEN_INTERRUPT:
6643 curenv->interrupt();
6644 break;
6645 case TOKEN_ITALIC_CORRECTION:
6646 curenv->add_italic_correction();
6647 break;
6648 case TOKEN_LEADER:
6649 curenv->handle_tab(1);
6650 break;
6651 case TOKEN_LEFT_BRACE:
6652 break;
6653 case TOKEN_MARK_INPUT:
6654 set_number_reg(nm, curenv->get_input_line_position().to_units());
6655 break;
6656 case TOKEN_NEWLINE:
6657 curenv->newline();
6658 break;
6659 case TOKEN_NODE:
6660 curenv->add_node(nd);
6661 nd = 0;
6662 break;
6663 case TOKEN_NUMBERED_CHAR:
6664 curenv->add_char(get_charinfo_by_number(val));
6665 break;
6666 case TOKEN_REQUEST:
6667 // handled in process_input_stack()
6668 break;
6669 case TOKEN_RIGHT_BRACE:
6670 break;
6671 case TOKEN_SPACE:
6672 curenv->space();
6673 break;
6674 case TOKEN_SPECIAL:
6675 curenv->add_char(get_charinfo(nm));
6676 break;
6677 case TOKEN_SPREAD:
6678 curenv->spread();
6679 break;
6680 case TOKEN_STRETCHABLE_SPACE:
6681 curenv->add_node(new unbreakable_space_node(curenv->get_space_width(),
6682 curenv->get_fill_color()));
6683 break;
6684 case TOKEN_UNSTRETCHABLE_SPACE:
6685 curenv->add_node(new space_char_hmotion_node(curenv->get_space_width(),
6686 curenv->get_fill_color()));
6687 break;
6688 case TOKEN_TAB:
6689 curenv->handle_tab(0);
6690 break;
6691 case TOKEN_TRANSPARENT:
6692 break;
6693 case TOKEN_TRANSPARENT_DUMMY:
6694 curenv->add_node(new transparent_dummy_node);
6695 break;
6696 case TOKEN_ZERO_WIDTH_BREAK:
6698 node *tmp = new space_node(H0, curenv->get_fill_color());
6699 tmp->freeze_space();
6700 tmp->is_escape_colon();
6701 curenv->add_node(tmp);
6702 break;
6704 default:
6705 assert(0);
6709 class nargs_reg : public reg {
6710 public:
6711 const char *get_string();
6714 const char *nargs_reg::get_string()
6716 return i_to_a(input_stack::nargs());
6719 class lineno_reg : public reg {
6720 public:
6721 const char *get_string();
6724 const char *lineno_reg::get_string()
6726 int line;
6727 const char *file;
6728 if (!input_stack::get_location(0, &file, &line))
6729 line = 0;
6730 return i_to_a(line);
6733 class writable_lineno_reg : public general_reg {
6734 public:
6735 writable_lineno_reg();
6736 void set_value(units);
6737 int get_value(units *);
6740 writable_lineno_reg::writable_lineno_reg()
6744 int writable_lineno_reg::get_value(units *res)
6746 int line;
6747 const char *file;
6748 if (!input_stack::get_location(0, &file, &line))
6749 return 0;
6750 *res = line;
6751 return 1;
6754 void writable_lineno_reg::set_value(units n)
6756 input_stack::set_location(0, n);
6759 class filename_reg : public reg {
6760 public:
6761 const char *get_string();
6764 const char *filename_reg::get_string()
6766 int line;
6767 const char *file;
6768 if (input_stack::get_location(0, &file, &line))
6769 return file;
6770 else
6771 return 0;
6774 class constant_reg : public reg {
6775 const char *s;
6776 public:
6777 constant_reg(const char *);
6778 const char *get_string();
6781 constant_reg::constant_reg(const char *p) : s(p)
6785 const char *constant_reg::get_string()
6787 return s;
6790 constant_int_reg::constant_int_reg(int *q) : p(q)
6794 const char *constant_int_reg::get_string()
6796 return i_to_a(*p);
6799 void abort_request()
6801 int c;
6802 if (tok.eof())
6803 c = EOF;
6804 else if (tok.newline())
6805 c = '\n';
6806 else {
6807 while ((c = get_copy(0)) == ' ')
6810 if (c == EOF || c == '\n')
6811 fputs("User Abort.", stderr);
6812 else {
6813 for (; c != '\n' && c != EOF; c = get_copy(0))
6814 fputs(asciify(c), stderr);
6816 fputc('\n', stderr);
6817 cleanup_and_exit(1);
6820 char *read_string()
6822 int len = 256;
6823 char *s = new char[len];
6824 int c;
6825 while ((c = get_copy(0)) == ' ')
6827 int i = 0;
6828 while (c != '\n' && c != EOF) {
6829 if (!invalid_input_char(c)) {
6830 if (i + 2 > len) {
6831 char *tem = s;
6832 s = new char[len*2];
6833 memcpy(s, tem, len);
6834 len *= 2;
6835 a_delete tem;
6837 s[i++] = c;
6839 c = get_copy(0);
6841 s[i] = '\0';
6842 tok.next();
6843 if (i == 0) {
6844 a_delete s;
6845 return 0;
6847 return s;
6850 void pipe_output()
6852 if (safer_flag) {
6853 error(".pi request not allowed in safer mode");
6854 skip_line();
6856 else {
6857 #ifdef POPEN_MISSING
6858 error("pipes not available on this system");
6859 skip_line();
6860 #else /* not POPEN_MISSING */
6861 if (the_output) {
6862 error("can't pipe: output already started");
6863 skip_line();
6865 else {
6866 char *pc;
6867 if ((pc = read_string()) == 0)
6868 error("can't pipe to empty command");
6869 if (pipe_command) {
6870 char *s = new char[strlen(pipe_command) + strlen(pc) + 1 + 1];
6871 strcpy(s, pipe_command);
6872 strcat(s, "|");
6873 strcat(s, pc);
6874 a_delete pipe_command;
6875 a_delete pc;
6876 pipe_command = s;
6878 else
6879 pipe_command = pc;
6881 #endif /* not POPEN_MISSING */
6885 static int system_status;
6887 void system_request()
6889 if (safer_flag) {
6890 error(".sy request not allowed in safer mode");
6891 skip_line();
6893 else {
6894 char *command = read_string();
6895 if (!command)
6896 error("empty command");
6897 else {
6898 system_status = system(command);
6899 a_delete command;
6904 void copy_file()
6906 if (curdiv == topdiv && topdiv->before_first_page) {
6907 handle_initial_request(COPY_FILE_REQUEST);
6908 return;
6910 symbol filename = get_long_name(1);
6911 while (!tok.newline() && !tok.eof())
6912 tok.next();
6913 if (break_flag)
6914 curenv->do_break();
6915 if (!filename.is_null())
6916 curdiv->copy_file(filename.contents());
6917 tok.next();
6920 #ifdef COLUMN
6922 void vjustify()
6924 if (curdiv == topdiv && topdiv->before_first_page) {
6925 handle_initial_request(VJUSTIFY_REQUEST);
6926 return;
6928 symbol type = get_long_name(1);
6929 if (!type.is_null())
6930 curdiv->vjustify(type);
6931 skip_line();
6934 #endif /* COLUMN */
6936 void transparent_file()
6938 if (curdiv == topdiv && topdiv->before_first_page) {
6939 handle_initial_request(TRANSPARENT_FILE_REQUEST);
6940 return;
6942 symbol filename = get_long_name(1);
6943 while (!tok.newline() && !tok.eof())
6944 tok.next();
6945 if (break_flag)
6946 curenv->do_break();
6947 if (!filename.is_null()) {
6948 errno = 0;
6949 FILE *fp = include_search_path.open_file_cautious(filename.contents());
6950 if (!fp)
6951 error("can't open `%1': %2", filename.contents(), strerror(errno));
6952 else {
6953 int bol = 1;
6954 for (;;) {
6955 int c = getc(fp);
6956 if (c == EOF)
6957 break;
6958 if (invalid_input_char(c))
6959 warning(WARN_INPUT, "invalid input character code %1", int(c));
6960 else {
6961 curdiv->transparent_output(c);
6962 bol = c == '\n';
6965 if (!bol)
6966 curdiv->transparent_output('\n');
6967 fclose(fp);
6970 tok.next();
6973 class page_range {
6974 int first;
6975 int last;
6976 public:
6977 page_range *next;
6978 page_range(int, int, page_range *);
6979 int contains(int n);
6982 page_range::page_range(int i, int j, page_range *p)
6983 : first(i), last(j), next(p)
6987 int page_range::contains(int n)
6989 return n >= first && (last <= 0 || n <= last);
6992 page_range *output_page_list = 0;
6994 int in_output_page_list(int n)
6996 if (!output_page_list)
6997 return 1;
6998 for (page_range *p = output_page_list; p; p = p->next)
6999 if (p->contains(n))
7000 return 1;
7001 return 0;
7004 static void parse_output_page_list(char *p)
7006 for (;;) {
7007 int i;
7008 if (*p == '-')
7009 i = 1;
7010 else if (csdigit(*p)) {
7011 i = 0;
7013 i = i*10 + *p++ - '0';
7014 while (csdigit(*p));
7016 else
7017 break;
7018 int j;
7019 if (*p == '-') {
7020 p++;
7021 j = 0;
7022 if (csdigit(*p)) {
7024 j = j*10 + *p++ - '0';
7025 while (csdigit(*p));
7028 else
7029 j = i;
7030 if (j == 0)
7031 last_page_number = -1;
7032 else if (last_page_number >= 0 && j > last_page_number)
7033 last_page_number = j;
7034 output_page_list = new page_range(i, j, output_page_list);
7035 if (*p != ',')
7036 break;
7037 ++p;
7039 if (*p != '\0') {
7040 error("bad output page list");
7041 output_page_list = 0;
7045 static FILE *open_mac_file(const char *mac, char **path)
7047 // Try first FOOBAR.tmac, then tmac.FOOBAR
7048 char *s1 = new char[strlen(mac)+strlen(MACRO_POSTFIX)+1];
7049 strcpy(s1, mac);
7050 strcat(s1, MACRO_POSTFIX);
7051 FILE *fp = mac_path->open_file(s1, path);
7052 a_delete s1;
7053 if (!fp) {
7054 char *s2 = new char[strlen(mac)+strlen(MACRO_PREFIX)+1];
7055 strcpy(s2, MACRO_PREFIX);
7056 strcat(s2, mac);
7057 fp = mac_path->open_file(s2, path);
7058 a_delete s2;
7060 return fp;
7063 static void process_macro_file(const char *mac)
7065 char *path;
7066 FILE *fp = open_mac_file(mac, &path);
7067 if (!fp)
7068 fatal("can't find macro file %1", mac);
7069 const char *s = symbol(path).contents();
7070 a_delete path;
7071 input_stack::push(new file_iterator(fp, s));
7072 tok.next();
7073 process_input_stack();
7076 static void process_startup_file(const char *filename)
7078 char *path;
7079 search_path *orig_mac_path = mac_path;
7080 mac_path = &config_macro_path;
7081 FILE *fp = mac_path->open_file(filename, &path);
7082 if (fp) {
7083 input_stack::push(new file_iterator(fp, symbol(path).contents()));
7084 a_delete path;
7085 tok.next();
7086 process_input_stack();
7088 mac_path = orig_mac_path;
7091 void macro_source()
7093 symbol nm = get_long_name(1);
7094 if (nm.is_null())
7095 skip_line();
7096 else {
7097 while (!tok.newline() && !tok.eof())
7098 tok.next();
7099 char *path;
7100 FILE *fp = mac_path->open_file(nm.contents(), &path);
7101 // .mso doesn't (and cannot) go through open_mac_file, so we
7102 // need to do it here manually: If we have tmac.FOOBAR, try
7103 // FOOBAR.tmac and vice versa
7104 if (!fp) {
7105 const char *fn = nm.contents();
7106 if (strncasecmp(fn, MACRO_PREFIX, sizeof(MACRO_PREFIX) - 1) == 0) {
7107 char *s = new char[strlen(fn) + sizeof(MACRO_POSTFIX)];
7108 strcpy(s, fn + sizeof(MACRO_PREFIX) - 1);
7109 strcat(s, MACRO_POSTFIX);
7110 fp = mac_path->open_file(s, &path);
7111 a_delete s;
7113 if (!fp) {
7114 if (strncasecmp(fn + strlen(fn) - sizeof(MACRO_POSTFIX) + 1,
7115 MACRO_POSTFIX, sizeof(MACRO_POSTFIX) - 1) == 0) {
7116 char *s = new char[strlen(fn) + sizeof(MACRO_PREFIX)];
7117 strcpy(s, MACRO_PREFIX);
7118 strncat(s, fn, strlen(fn) - sizeof(MACRO_POSTFIX) + 1);
7119 fp = mac_path->open_file(s, &path);
7120 a_delete s;
7124 if (fp) {
7125 input_stack::push(new file_iterator(fp, symbol(path).contents()));
7126 a_delete path;
7128 else
7129 error("can't find macro file `%1'", nm.contents());
7130 tok.next();
7134 static void process_input_file(const char *name)
7136 FILE *fp;
7137 if (strcmp(name, "-") == 0) {
7138 clearerr(stdin);
7139 fp = stdin;
7141 else {
7142 errno = 0;
7143 fp = include_search_path.open_file_cautious(name);
7144 if (!fp)
7145 fatal("can't open `%1': %2", name, strerror(errno));
7147 input_stack::push(new file_iterator(fp, name));
7148 tok.next();
7149 process_input_stack();
7152 // make sure the_input is empty before calling this
7154 static int evaluate_expression(const char *expr, units *res)
7156 input_stack::push(make_temp_iterator(expr));
7157 tok.next();
7158 int success = get_number(res, 'u');
7159 while (input_stack::get(0) != EOF)
7161 return success;
7164 static void do_register_assignment(const char *s)
7166 const char *p = strchr(s, '=');
7167 if (!p) {
7168 char buf[2];
7169 buf[0] = s[0];
7170 buf[1] = 0;
7171 units n;
7172 if (evaluate_expression(s + 1, &n))
7173 set_number_reg(buf, n);
7175 else {
7176 char *buf = new char[p - s + 1];
7177 memcpy(buf, s, p - s);
7178 buf[p - s] = 0;
7179 units n;
7180 if (evaluate_expression(p + 1, &n))
7181 set_number_reg(buf, n);
7182 a_delete buf;
7186 static void set_string(const char *name, const char *value)
7188 macro *m = new macro;
7189 for (const char *p = value; *p; p++)
7190 if (!invalid_input_char((unsigned char)*p))
7191 m->append(*p);
7192 request_dictionary.define(name, m);
7195 static void do_string_assignment(const char *s)
7197 const char *p = strchr(s, '=');
7198 if (!p) {
7199 char buf[2];
7200 buf[0] = s[0];
7201 buf[1] = 0;
7202 set_string(buf, s + 1);
7204 else {
7205 char *buf = new char[p - s + 1];
7206 memcpy(buf, s, p - s);
7207 buf[p - s] = 0;
7208 set_string(buf, p + 1);
7209 a_delete buf;
7213 struct string_list {
7214 const char *s;
7215 string_list *next;
7216 string_list(const char *ss) : s(ss), next(0) {}
7219 #if 0
7220 static void prepend_string(const char *s, string_list **p)
7222 string_list *l = new string_list(s);
7223 l->next = *p;
7224 *p = l;
7226 #endif
7228 static void add_string(const char *s, string_list **p)
7230 while (*p)
7231 p = &((*p)->next);
7232 *p = new string_list(s);
7235 void usage(FILE *stream, const char *prog)
7237 fprintf(stream,
7238 "usage: %s -abcivzCERU -wname -Wname -dcs -ffam -mname -nnum -olist\n"
7239 " -rcn -Tname -Fdir -Idir -Mdir [files...]\n",
7240 prog);
7243 int main(int argc, char **argv)
7245 program_name = argv[0];
7246 static char stderr_buf[BUFSIZ];
7247 setbuf(stderr, stderr_buf);
7248 int c;
7249 string_list *macros = 0;
7250 string_list *register_assignments = 0;
7251 string_list *string_assignments = 0;
7252 int iflag = 0;
7253 int tflag = 0;
7254 int fflag = 0;
7255 int nflag = 0;
7256 int no_rc = 0; // don't process troffrc and troffrc-end
7257 int next_page_number = 0; // pacify compiler
7258 opterr = 0;
7259 hresolution = vresolution = 1;
7260 // restore $PATH if called from groff
7261 char* groff_path = getenv("GROFF_PATH__");
7262 if (groff_path) {
7263 string e = "PATH";
7264 e += '=';
7265 if (*groff_path)
7266 e += groff_path;
7267 e += '\0';
7268 if (putenv(strsave(e.contents())))
7269 fatal("putenv failed");
7271 static const struct option long_options[] = {
7272 { "help", no_argument, 0, CHAR_MAX + 1 },
7273 { "version", no_argument, 0, 'v' },
7274 { 0, 0, 0, 0 }
7276 #if defined(DEBUGGING)
7277 #define DEBUG_OPTION "D"
7278 #endif
7279 while ((c = getopt_long(argc, argv,
7280 "abciI:vw:W:zCEf:m:n:o:r:d:F:M:T:tqs:RU"
7281 DEBUG_OPTION, long_options, 0))
7282 != EOF)
7283 switch(c) {
7284 case 'v':
7286 printf("GNU troff (groff) version %s\n", Version_string);
7287 exit(0);
7288 break;
7290 case 'I':
7291 // Search path for .psbb files
7292 // and most other non-system input files.
7293 include_search_path.command_line_dir(optarg);
7294 break;
7295 case 'T':
7296 device = optarg;
7297 tflag = 1;
7298 is_html = (strcmp(device, "html") == 0);
7299 break;
7300 case 'C':
7301 compatible_flag = 1;
7302 // fall through
7303 case 'c':
7304 color_flag = 0;
7305 break;
7306 case 'M':
7307 macro_path.command_line_dir(optarg);
7308 safer_macro_path.command_line_dir(optarg);
7309 config_macro_path.command_line_dir(optarg);
7310 break;
7311 case 'F':
7312 font::command_line_font_dir(optarg);
7313 break;
7314 case 'm':
7315 add_string(optarg, &macros);
7316 break;
7317 case 'E':
7318 inhibit_errors = 1;
7319 break;
7320 case 'R':
7321 no_rc = 1;
7322 break;
7323 case 'w':
7324 enable_warning(optarg);
7325 break;
7326 case 'W':
7327 disable_warning(optarg);
7328 break;
7329 case 'i':
7330 iflag = 1;
7331 break;
7332 case 'b':
7333 backtrace_flag = 1;
7334 break;
7335 case 'a':
7336 ascii_output_flag = 1;
7337 break;
7338 case 'z':
7339 suppress_output_flag = 1;
7340 break;
7341 case 'n':
7342 if (sscanf(optarg, "%d", &next_page_number) == 1)
7343 nflag++;
7344 else
7345 error("bad page number");
7346 break;
7347 case 'o':
7348 parse_output_page_list(optarg);
7349 break;
7350 case 'd':
7351 if (*optarg == '\0')
7352 error("`-d' requires non-empty argument");
7353 else
7354 add_string(optarg, &string_assignments);
7355 break;
7356 case 'r':
7357 if (*optarg == '\0')
7358 error("`-r' requires non-empty argument");
7359 else
7360 add_string(optarg, &register_assignments);
7361 break;
7362 case 'f':
7363 default_family = symbol(optarg);
7364 fflag = 1;
7365 break;
7366 case 'q':
7367 case 's':
7368 case 't':
7369 // silently ignore these
7370 break;
7371 case 'U':
7372 safer_flag = 0; // unsafe behaviour
7373 break;
7374 #if defined(DEBUGGING)
7375 case 'D':
7376 debug_state = 1;
7377 break;
7378 #endif
7379 case CHAR_MAX + 1: // --help
7380 usage(stdout, argv[0]);
7381 exit(0);
7382 break;
7383 case '?':
7384 usage(stderr, argv[0]);
7385 exit(1);
7386 break; // never reached
7387 default:
7388 assert(0);
7390 if (!safer_flag)
7391 mac_path = &macro_path;
7392 set_string(".T", device);
7393 init_charset_table();
7394 init_hpf_code_table();
7395 if (!font::load_desc())
7396 fatal("sorry, I can't continue");
7397 units_per_inch = font::res;
7398 hresolution = font::hor;
7399 vresolution = font::vert;
7400 sizescale = font::sizescale;
7401 tcommand_flag = font::tcommand;
7402 warn_scale = (double)units_per_inch;
7403 warn_scaling_indicator = 'i';
7404 if (!fflag && font::family != 0 && *font::family != '\0')
7405 default_family = symbol(font::family);
7406 font_size::init_size_table(font::sizes);
7407 int i;
7408 int j = 1;
7409 if (font::style_table) {
7410 for (i = 0; font::style_table[i]; i++)
7411 mount_style(j++, symbol(font::style_table[i]));
7413 for (i = 0; font::font_name_table[i]; i++, j++)
7414 // In the DESC file a font name of 0 (zero) means leave this
7415 // position empty.
7416 if (strcmp(font::font_name_table[i], "0") != 0)
7417 mount_font(j, symbol(font::font_name_table[i]));
7418 curdiv = topdiv = new top_level_diversion;
7419 if (nflag)
7420 topdiv->set_next_page_number(next_page_number);
7421 init_input_requests();
7422 init_env_requests();
7423 init_div_requests();
7424 #ifdef COLUMN
7425 init_column_requests();
7426 #endif /* COLUMN */
7427 init_node_requests();
7428 number_reg_dictionary.define(".T", new constant_reg(tflag ? "1" : "0"));
7429 init_registers();
7430 init_reg_requests();
7431 init_hyphen_requests();
7432 init_environments();
7433 while (string_assignments) {
7434 do_string_assignment(string_assignments->s);
7435 string_list *tem = string_assignments;
7436 string_assignments = string_assignments->next;
7437 delete tem;
7439 while (register_assignments) {
7440 do_register_assignment(register_assignments->s);
7441 string_list *tem = register_assignments;
7442 register_assignments = register_assignments->next;
7443 delete tem;
7445 if (!no_rc)
7446 process_startup_file(INITIAL_STARTUP_FILE);
7447 while (macros) {
7448 process_macro_file(macros->s);
7449 string_list *tem = macros;
7450 macros = macros->next;
7451 delete tem;
7453 if (!no_rc)
7454 process_startup_file(FINAL_STARTUP_FILE);
7455 for (i = optind; i < argc; i++)
7456 process_input_file(argv[i]);
7457 if (optind >= argc || iflag)
7458 process_input_file("-");
7459 exit_troff();
7460 return 0; // not reached
7463 void warn_request()
7465 int n;
7466 if (has_arg() && get_integer(&n)) {
7467 if (n & ~WARN_TOTAL) {
7468 warning(WARN_RANGE, "warning mask must be between 0 and %1", WARN_TOTAL);
7469 n &= WARN_TOTAL;
7471 warning_mask = n;
7473 else
7474 warning_mask = WARN_TOTAL;
7475 skip_line();
7478 static void init_registers()
7480 #ifdef LONG_FOR_TIME_T
7481 long
7482 #else /* not LONG_FOR_TIME_T */
7483 time_t
7484 #endif /* not LONG_FOR_TIME_T */
7485 t = time(0);
7486 // Use struct here to work around misfeature in old versions of g++.
7487 struct tm *tt = localtime(&t);
7488 set_number_reg("seconds", int(tt->tm_sec));
7489 set_number_reg("minutes", int(tt->tm_min));
7490 set_number_reg("hours", int(tt->tm_hour));
7491 set_number_reg("dw", int(tt->tm_wday + 1));
7492 set_number_reg("dy", int(tt->tm_mday));
7493 set_number_reg("mo", int(tt->tm_mon + 1));
7494 set_number_reg("year", int(1900 + tt->tm_year));
7495 set_number_reg("yr", int(tt->tm_year));
7496 set_number_reg("$$", getpid());
7497 number_reg_dictionary.define(".A",
7498 new constant_reg(ascii_output_flag
7499 ? "1"
7500 : "0"));
7504 * registers associated with \O
7507 static int output_reg_minx_contents = -1;
7508 static int output_reg_miny_contents = -1;
7509 static int output_reg_maxx_contents = -1;
7510 static int output_reg_maxy_contents = -1;
7512 void check_output_limits(int x, int y)
7514 if ((output_reg_minx_contents == -1) || (x < output_reg_minx_contents))
7515 output_reg_minx_contents = x;
7516 if (x > output_reg_maxx_contents)
7517 output_reg_maxx_contents = x;
7518 if ((output_reg_miny_contents == -1) || (y < output_reg_miny_contents))
7519 output_reg_miny_contents = y;
7520 if (y > output_reg_maxy_contents)
7521 output_reg_maxy_contents = y;
7524 void reset_output_registers()
7526 output_reg_minx_contents = -1;
7527 output_reg_miny_contents = -1;
7528 output_reg_maxx_contents = -1;
7529 output_reg_maxy_contents = -1;
7532 void get_output_registers(int *minx, int *miny, int *maxx, int *maxy)
7534 *minx = output_reg_minx_contents;
7535 *miny = output_reg_miny_contents;
7536 *maxx = output_reg_maxx_contents;
7537 *maxy = output_reg_maxy_contents;
7540 void init_input_requests()
7542 init_request("ab", abort_request);
7543 init_request("als", alias_macro);
7544 init_request("am", append_macro);
7545 init_request("am1", append_nocomp_macro);
7546 init_request("ami", append_indirect_macro);
7547 init_request("ami1", append_indirect_nocomp_macro);
7548 init_request("as", append_string);
7549 init_request("as1", append_nocomp_string);
7550 init_request("asciify", asciify_macro);
7551 init_request("backtrace", backtrace_request);
7552 init_request("blm", blank_line_macro);
7553 init_request("break", while_break_request);
7554 init_request("cf", copy_file);
7555 init_request("cflags", char_flags);
7556 init_request("char", define_character);
7557 init_request("chop", chop_macro);
7558 init_request("close", close_request);
7559 init_request("color", activate_color);
7560 init_request("composite", composite_request);
7561 init_request("continue", while_continue_request);
7562 init_request("cp", compatible);
7563 init_request("de", define_macro);
7564 init_request("de1", define_nocomp_macro);
7565 init_request("defcolor", define_color);
7566 init_request("dei", define_indirect_macro);
7567 init_request("dei1", define_indirect_nocomp_macro);
7568 init_request("do", do_request);
7569 init_request("ds", define_string);
7570 init_request("ds1", define_nocomp_string);
7571 init_request("ec", set_escape_char);
7572 init_request("ecr", restore_escape_char);
7573 init_request("ecs", save_escape_char);
7574 init_request("el", else_request);
7575 init_request("em", end_macro);
7576 init_request("eo", escape_off);
7577 init_request("ex", exit_request);
7578 init_request("fchar", define_fallback_character);
7579 #ifdef WIDOW_CONTROL
7580 init_request("fpl", flush_pending_lines);
7581 #endif /* WIDOW_CONTROL */
7582 init_request("hcode", hyphenation_code);
7583 init_request("hpfcode", hyphenation_patterns_file_code);
7584 init_request("ie", if_else_request);
7585 init_request("if", if_request);
7586 init_request("ig", ignore);
7587 init_request("length", length_request);
7588 init_request("lf", line_file);
7589 init_request("mso", macro_source);
7590 init_request("nop", nop_request);
7591 init_request("nroff", nroff_request);
7592 init_request("nx", next_file);
7593 init_request("open", open_request);
7594 init_request("opena", opena_request);
7595 init_request("output", output_request);
7596 init_request("pc", set_page_character);
7597 init_request("pi", pipe_output);
7598 init_request("pm", print_macros);
7599 init_request("psbb", ps_bbox_request);
7600 #ifndef POPEN_MISSING
7601 init_request("pso", pipe_source);
7602 #endif /* not POPEN_MISSING */
7603 init_request("rchar", remove_character);
7604 init_request("rd", read_request);
7605 init_request("return", return_macro_request);
7606 init_request("rm", remove_macro);
7607 init_request("rn", rename_macro);
7608 init_request("schar", define_special_character);
7609 init_request("shift", shift);
7610 init_request("so", source);
7611 init_request("spreadwarn", spreadwarn_request);
7612 init_request("substring", substring_request);
7613 init_request("sy", system_request);
7614 init_request("tag", tag);
7615 init_request("taga", taga);
7616 init_request("tm", terminal);
7617 init_request("tm1", terminal1);
7618 init_request("tmc", terminal_continue);
7619 init_request("tr", translate);
7620 init_request("trf", transparent_file);
7621 init_request("trin", translate_input);
7622 init_request("trnt", translate_no_transparent);
7623 init_request("troff", troff_request);
7624 init_request("unformat", unformat_macro);
7625 #ifdef COLUMN
7626 init_request("vj", vjustify);
7627 #endif /* COLUMN */
7628 init_request("warn", warn_request);
7629 init_request("warnscale", warnscale_request);
7630 init_request("while", while_request);
7631 init_request("write", write_request);
7632 init_request("writec", write_request_continue);
7633 init_request("writem", write_macro_request);
7634 number_reg_dictionary.define(".$", new nargs_reg);
7635 number_reg_dictionary.define(".C", new constant_int_reg(&compatible_flag));
7636 number_reg_dictionary.define(".c", new lineno_reg);
7637 number_reg_dictionary.define(".color", new constant_int_reg(&color_flag));
7638 number_reg_dictionary.define(".F", new filename_reg);
7639 number_reg_dictionary.define(".g", new constant_reg("1"));
7640 number_reg_dictionary.define(".H", new constant_int_reg(&hresolution));
7641 number_reg_dictionary.define(".R", new constant_reg("10000"));
7642 number_reg_dictionary.define(".U", new constant_int_reg(&safer_flag));
7643 number_reg_dictionary.define(".V", new constant_int_reg(&vresolution));
7644 number_reg_dictionary.define(".warn", new constant_int_reg(&warning_mask));
7645 extern const char *major_version;
7646 number_reg_dictionary.define(".x", new constant_reg(major_version));
7647 extern const char *revision;
7648 number_reg_dictionary.define(".Y", new constant_reg(revision));
7649 extern const char *minor_version;
7650 number_reg_dictionary.define(".y", new constant_reg(minor_version));
7651 number_reg_dictionary.define("c.", new writable_lineno_reg);
7652 number_reg_dictionary.define("llx", new variable_reg(&llx_reg_contents));
7653 number_reg_dictionary.define("lly", new variable_reg(&lly_reg_contents));
7654 number_reg_dictionary.define("opmaxx",
7655 new variable_reg(&output_reg_maxx_contents));
7656 number_reg_dictionary.define("opmaxy",
7657 new variable_reg(&output_reg_maxy_contents));
7658 number_reg_dictionary.define("opminx",
7659 new variable_reg(&output_reg_minx_contents));
7660 number_reg_dictionary.define("opminy",
7661 new variable_reg(&output_reg_miny_contents));
7662 number_reg_dictionary.define("slimit",
7663 new variable_reg(&input_stack::limit));
7664 number_reg_dictionary.define("systat", new variable_reg(&system_status));
7665 number_reg_dictionary.define("urx", new variable_reg(&urx_reg_contents));
7666 number_reg_dictionary.define("ury", new variable_reg(&ury_reg_contents));
7669 object_dictionary request_dictionary(501);
7671 void init_request(const char *s, REQUEST_FUNCP f)
7673 request_dictionary.define(s, new request(f));
7676 static request_or_macro *lookup_request(symbol nm)
7678 assert(!nm.is_null());
7679 request_or_macro *p = (request_or_macro *)request_dictionary.lookup(nm);
7680 if (p == 0) {
7681 warning(WARN_MAC, "macro `%1' not defined", nm.contents());
7682 p = new macro;
7683 request_dictionary.define(nm, p);
7685 return p;
7688 node *charinfo_to_node_list(charinfo *ci, const environment *envp)
7690 // Don't interpret character definitions in compatible mode.
7691 int old_compatible_flag = compatible_flag;
7692 compatible_flag = 0;
7693 int old_escape_char = escape_char;
7694 escape_char = '\\';
7695 macro *mac = ci->set_macro(0);
7696 assert(mac != 0);
7697 environment *oldenv = curenv;
7698 environment env(envp);
7699 curenv = &env;
7700 curenv->set_composite();
7701 token old_tok = tok;
7702 input_stack::add_boundary();
7703 string_iterator *si =
7704 new string_iterator(*mac, "composite character", ci->nm);
7705 input_stack::push(si);
7706 // we don't use process_input_stack, because we don't want to recognise
7707 // requests
7708 for (;;) {
7709 tok.next();
7710 if (tok.eof())
7711 break;
7712 if (tok.newline()) {
7713 error("composite character mustn't contain newline");
7714 while (!tok.eof())
7715 tok.next();
7716 break;
7718 else
7719 tok.process();
7721 node *n = curenv->extract_output_line();
7722 input_stack::remove_boundary();
7723 ci->set_macro(mac);
7724 tok = old_tok;
7725 curenv = oldenv;
7726 compatible_flag = old_compatible_flag;
7727 escape_char = old_escape_char;
7728 have_input = 0;
7729 return n;
7732 static node *read_draw_node()
7734 token start;
7735 start.next();
7736 if (!start.delimiter(1)){
7737 do {
7738 tok.next();
7739 } while (tok != start && !tok.newline() && !tok.eof());
7741 else {
7742 tok.next();
7743 if (tok == start)
7744 error("missing argument");
7745 else {
7746 unsigned char type = tok.ch();
7747 if (type == 'F') {
7748 read_color_draw_node(start);
7749 return 0;
7751 tok.next();
7752 int maxpoints = 10;
7753 hvpair *point = new hvpair[maxpoints];
7754 int npoints = 0;
7755 int no_last_v = 0;
7756 int err = 0;
7757 int i;
7758 for (i = 0; tok != start; i++) {
7759 if (i == maxpoints) {
7760 hvpair *oldpoint = point;
7761 point = new hvpair[maxpoints*2];
7762 for (int j = 0; j < maxpoints; j++)
7763 point[j] = oldpoint[j];
7764 maxpoints *= 2;
7765 a_delete oldpoint;
7767 if (!get_hunits(&point[i].h,
7768 type == 'f' || type == 't' ? 'u' : 'm')) {
7769 err = 1;
7770 break;
7772 ++npoints;
7773 tok.skip();
7774 point[i].v = V0;
7775 if (tok == start) {
7776 no_last_v = 1;
7777 break;
7779 if (!get_vunits(&point[i].v, 'v')) {
7780 err = 1;
7781 break;
7783 tok.skip();
7785 while (tok != start && !tok.newline() && !tok.eof())
7786 tok.next();
7787 if (!err) {
7788 switch (type) {
7789 case 'l':
7790 if (npoints != 1 || no_last_v) {
7791 error("two arguments needed for line");
7792 npoints = 1;
7794 break;
7795 case 'c':
7796 if (npoints != 1 || !no_last_v) {
7797 error("one argument needed for circle");
7798 npoints = 1;
7799 point[0].v = V0;
7801 break;
7802 case 'e':
7803 if (npoints != 1 || no_last_v) {
7804 error("two arguments needed for ellipse");
7805 npoints = 1;
7807 break;
7808 case 'a':
7809 if (npoints != 2 || no_last_v) {
7810 error("four arguments needed for arc");
7811 npoints = 2;
7813 break;
7814 case '~':
7815 if (no_last_v)
7816 error("even number of arguments needed for spline");
7817 break;
7818 case 'f':
7819 if (npoints != 1 || !no_last_v) {
7820 error("one argument needed for gray shade");
7821 npoints = 1;
7822 point[0].v = V0;
7824 default:
7825 // silently pass it through
7826 break;
7828 draw_node *dn = new draw_node(type, point, npoints,
7829 curenv->get_font_size(),
7830 curenv->get_glyph_color(),
7831 curenv->get_fill_color());
7832 a_delete point;
7833 return dn;
7835 else {
7836 a_delete point;
7840 return 0;
7843 static void read_color_draw_node(token &start)
7845 tok.next();
7846 if (tok == start) {
7847 error("missing color scheme");
7848 return;
7850 unsigned char scheme = tok.ch();
7851 tok.next();
7852 color *col = 0;
7853 char end = start.ch();
7854 switch (scheme) {
7855 case 'c':
7856 col = read_cmy(end);
7857 break;
7858 case 'd':
7859 col = &default_color;
7860 break;
7861 case 'g':
7862 col = read_gray(end);
7863 break;
7864 case 'k':
7865 col = read_cmyk(end);
7866 break;
7867 case 'r':
7868 col = read_rgb(end);
7869 break;
7871 if (col)
7872 curenv->set_fill_color(col);
7873 while (tok != start) {
7874 if (tok.newline() || tok.eof()) {
7875 warning(WARN_DELIM, "missing closing delimiter");
7876 input_stack::push(make_temp_iterator("\n"));
7877 break;
7879 tok.next();
7881 have_input = 1;
7884 static struct {
7885 const char *name;
7886 int mask;
7887 } warning_table[] = {
7888 { "char", WARN_CHAR },
7889 { "range", WARN_RANGE },
7890 { "break", WARN_BREAK },
7891 { "delim", WARN_DELIM },
7892 { "el", WARN_EL },
7893 { "scale", WARN_SCALE },
7894 { "number", WARN_NUMBER },
7895 { "syntax", WARN_SYNTAX },
7896 { "tab", WARN_TAB },
7897 { "right-brace", WARN_RIGHT_BRACE },
7898 { "missing", WARN_MISSING },
7899 { "input", WARN_INPUT },
7900 { "escape", WARN_ESCAPE },
7901 { "space", WARN_SPACE },
7902 { "font", WARN_FONT },
7903 { "di", WARN_DI },
7904 { "mac", WARN_MAC },
7905 { "reg", WARN_REG },
7906 { "ig", WARN_IG },
7907 { "color", WARN_COLOR },
7908 { "all", WARN_TOTAL & ~(WARN_DI | WARN_MAC | WARN_REG) },
7909 { "w", WARN_TOTAL },
7910 { "default", DEFAULT_WARNING_MASK },
7913 static int lookup_warning(const char *name)
7915 for (unsigned int i = 0;
7916 i < sizeof(warning_table)/sizeof(warning_table[0]);
7917 i++)
7918 if (strcmp(name, warning_table[i].name) == 0)
7919 return warning_table[i].mask;
7920 return 0;
7923 static void enable_warning(const char *name)
7925 int mask = lookup_warning(name);
7926 if (mask)
7927 warning_mask |= mask;
7928 else
7929 error("unknown warning `%1'", name);
7932 static void disable_warning(const char *name)
7934 int mask = lookup_warning(name);
7935 if (mask)
7936 warning_mask &= ~mask;
7937 else
7938 error("unknown warning `%1'", name);
7941 static void copy_mode_error(const char *format,
7942 const errarg &arg1,
7943 const errarg &arg2,
7944 const errarg &arg3)
7946 if (ignoring) {
7947 static const char prefix[] = "(in ignored input) ";
7948 char *s = new char[sizeof(prefix) + strlen(format)];
7949 strcpy(s, prefix);
7950 strcat(s, format);
7951 warning(WARN_IG, s, arg1, arg2, arg3);
7952 a_delete s;
7954 else
7955 error(format, arg1, arg2, arg3);
7958 enum error_type { WARNING, OUTPUT_WARNING, ERROR, FATAL };
7960 static void do_error(error_type type,
7961 const char *format,
7962 const errarg &arg1,
7963 const errarg &arg2,
7964 const errarg &arg3)
7966 const char *filename;
7967 int lineno;
7968 if (inhibit_errors && type < FATAL)
7969 return;
7970 if (backtrace_flag)
7971 input_stack::backtrace();
7972 if (!get_file_line(&filename, &lineno))
7973 filename = 0;
7974 if (filename)
7975 errprint("%1:%2: ", filename, lineno);
7976 else if (program_name)
7977 fprintf(stderr, "%s: ", program_name);
7978 switch (type) {
7979 case FATAL:
7980 fputs("fatal error: ", stderr);
7981 break;
7982 case ERROR:
7983 break;
7984 case WARNING:
7985 fputs("warning: ", stderr);
7986 break;
7987 case OUTPUT_WARNING:
7988 double fromtop = topdiv->get_vertical_position().to_units() / warn_scale;
7989 fprintf(stderr, "warning [p %d, %.1f%c",
7990 topdiv->get_page_number(), fromtop, warn_scaling_indicator);
7991 if (topdiv != curdiv) {
7992 double fromtop1 = curdiv->get_vertical_position().to_units()
7993 / warn_scale;
7994 fprintf(stderr, ", div `%s', %.1f%c",
7995 curdiv->get_diversion_name(), fromtop1, warn_scaling_indicator);
7997 fprintf(stderr, "]: ");
7998 break;
8000 errprint(format, arg1, arg2, arg3);
8001 fputc('\n', stderr);
8002 fflush(stderr);
8003 if (type == FATAL)
8004 cleanup_and_exit(1);
8007 int warning(warning_type t,
8008 const char *format,
8009 const errarg &arg1,
8010 const errarg &arg2,
8011 const errarg &arg3)
8013 if ((t & warning_mask) != 0) {
8014 do_error(WARNING, format, arg1, arg2, arg3);
8015 return 1;
8017 else
8018 return 0;
8021 int output_warning(warning_type t,
8022 const char *format,
8023 const errarg &arg1,
8024 const errarg &arg2,
8025 const errarg &arg3)
8027 if ((t & warning_mask) != 0) {
8028 do_error(OUTPUT_WARNING, format, arg1, arg2, arg3);
8029 return 1;
8031 else
8032 return 0;
8035 void error(const char *format,
8036 const errarg &arg1,
8037 const errarg &arg2,
8038 const errarg &arg3)
8040 do_error(ERROR, format, arg1, arg2, arg3);
8043 void fatal(const char *format,
8044 const errarg &arg1,
8045 const errarg &arg2,
8046 const errarg &arg3)
8048 do_error(FATAL, format, arg1, arg2, arg3);
8051 void fatal_with_file_and_line(const char *filename, int lineno,
8052 const char *format,
8053 const errarg &arg1,
8054 const errarg &arg2,
8055 const errarg &arg3)
8057 fprintf(stderr, "%s:%d: fatal error: ", filename, lineno);
8058 errprint(format, arg1, arg2, arg3);
8059 fputc('\n', stderr);
8060 fflush(stderr);
8061 cleanup_and_exit(1);
8064 void error_with_file_and_line(const char *filename, int lineno,
8065 const char *format,
8066 const errarg &arg1,
8067 const errarg &arg2,
8068 const errarg &arg3)
8070 fprintf(stderr, "%s:%d: error: ", filename, lineno);
8071 errprint(format, arg1, arg2, arg3);
8072 fputc('\n', stderr);
8073 fflush(stderr);
8076 dictionary charinfo_dictionary(501);
8078 charinfo *get_charinfo(symbol nm)
8080 void *p = charinfo_dictionary.lookup(nm);
8081 if (p != 0)
8082 return (charinfo *)p;
8083 charinfo *cp = new charinfo(nm);
8084 (void)charinfo_dictionary.lookup(nm, cp);
8085 return cp;
8088 int charinfo::next_index = 0;
8090 charinfo::charinfo(symbol s)
8091 : translation(0), mac(0), special_translation(TRANSLATE_NONE),
8092 hyphenation_code(0), flags(0), ascii_code(0), asciify_code(0),
8093 not_found(0), transparent_translate(1), translate_input(0),
8094 mode(CHAR_NORMAL), nm(s)
8096 index = next_index++;
8099 void charinfo::set_hyphenation_code(unsigned char c)
8101 hyphenation_code = c;
8104 void charinfo::set_translation(charinfo *ci, int tt, int ti)
8106 translation = ci;
8107 if (ci && ti) {
8108 if (hyphenation_code != 0)
8109 ci->set_hyphenation_code(hyphenation_code);
8110 if (asciify_code != 0)
8111 ci->set_asciify_code(asciify_code);
8112 else if (ascii_code != 0)
8113 ci->set_asciify_code(ascii_code);
8114 ci->set_translation_input();
8116 special_translation = TRANSLATE_NONE;
8117 transparent_translate = tt;
8120 void charinfo::set_special_translation(int c, int tt)
8122 special_translation = c;
8123 translation = 0;
8124 transparent_translate = tt;
8127 void charinfo::set_ascii_code(unsigned char c)
8129 ascii_code = c;
8132 void charinfo::set_asciify_code(unsigned char c)
8134 asciify_code = c;
8137 macro *charinfo::set_macro(macro *m)
8139 macro *tem = mac;
8140 mac = m;
8141 return tem;
8144 macro *charinfo::setx_macro(macro *m, char_mode cm)
8146 macro *tem = mac;
8147 mac = m;
8148 mode = cm;
8149 return tem;
8152 void charinfo::set_number(int n)
8154 number = n;
8155 flags |= NUMBERED;
8158 int charinfo::get_number()
8160 assert(flags & NUMBERED);
8161 return number;
8164 symbol UNNAMED_SYMBOL("---");
8166 // For numbered characters not between 0 and 255, we make a symbol out
8167 // of the number and store them in this dictionary.
8169 dictionary numbered_charinfo_dictionary(11);
8171 charinfo *get_charinfo_by_number(int n)
8173 static charinfo *number_table[256];
8175 if (n >= 0 && n < 256) {
8176 charinfo *ci = number_table[n];
8177 if (!ci) {
8178 ci = new charinfo(UNNAMED_SYMBOL);
8179 ci->set_number(n);
8180 number_table[n] = ci;
8182 return ci;
8184 else {
8185 symbol ns(i_to_a(n));
8186 charinfo *ci = (charinfo *)numbered_charinfo_dictionary.lookup(ns);
8187 if (!ci) {
8188 ci = new charinfo(UNNAMED_SYMBOL);
8189 ci->set_number(n);
8190 (void)numbered_charinfo_dictionary.lookup(ns, ci);
8192 return ci;
8196 int font::name_to_index(const char *nm)
8198 charinfo *ci;
8199 if (nm[1] == 0)
8200 ci = charset_table[nm[0] & 0xff];
8201 else if (nm[0] == '\\' && nm[2] == 0)
8202 ci = get_charinfo(symbol(nm + 1));
8203 else
8204 ci = get_charinfo(symbol(nm));
8205 if (ci == 0)
8206 return -1;
8207 else
8208 return ci->get_index();
8211 int font::number_to_index(int n)
8213 return get_charinfo_by_number(n)->get_index();