2 /* Copyright (C) 1989, 1990, 1991, 1992, 2000, 2001, 2002, 2003, 2004, 2005
3 Free Software Foundation, Inc.
4 Written by James Clark (jjc@jclark.com)
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
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
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, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA. */
25 #include "dictionary.h"
27 #include "stringclass.h"
36 #include "macropath.h"
42 // Needed for getpid() and isatty()
47 #ifdef NEED_DECLARATION_PUTENV
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)
65 // initial size of buffer for reading names; expanded as necessary
68 extern "C" const char *program_name
;
69 extern "C" const char *Version_string
;
72 void init_column_requests();
75 static node
*read_draw_node();
76 static void read_color_draw_node(token
&);
77 static void push_token(const token
&);
82 void transparent_file();
86 int color_flag
= 1; // colors are on by default
87 static int backtrace_flag
= 0;
89 char *pipe_command
= 0;
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;
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
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
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()
168 error("bad escape character");
172 escape_char
= tok
.ch();
185 static int saved_escape_char
= '\\';
187 void save_escape_char()
189 saved_escape_char
= escape_char
;
193 void restore_escape_char()
195 escape_char
= saved_escape_char
;
199 class input_iterator
{
202 input_iterator(int is_div
);
203 virtual ~input_iterator() {}
205 friend class input_stack
;
207 statem
*diversion_state
;
209 const unsigned char *ptr
;
210 const unsigned char *eptr
;
211 input_iterator
*next
;
213 virtual int fill(node
**);
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
**)
245 int input_iterator::peek()
250 inline int input_iterator::get(node
**p
)
252 return ptr
< eptr
? *ptr
++ : fill(p
);
255 class input_boundary
: public input_iterator
{
257 int is_boundary() { return 1; }
260 class input_return_boundary
: public input_iterator
{
262 int is_boundary() { return 2; }
265 class file_iterator
: public input_iterator
{
268 const char *filename
;
272 enum { BUF_SIZE
= 512 };
273 unsigned char buf
[BUF_SIZE
];
276 file_iterator(FILE *, const char *, int = 0);
280 int get_location(int, const char **, int *);
282 int set_location(const char *, int);
283 int next_file(FILE *, const char *);
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)) {
294 the_output
->put_filename(fn
);
298 file_iterator::~file_iterator()
303 void file_iterator::close()
307 #ifndef POPEN_MISSING
310 #endif /* not POPEN_MISSING */
315 int file_iterator::is_file()
320 int file_iterator::next_file(FILE *f
, const char *s
)
334 int file_iterator::fill(node
**)
339 unsigned char *p
= buf
;
341 unsigned char *e
= p
+ BUF_SIZE
;
346 if (invalid_input_char(c
))
347 warning(WARN_INPUT
, "invalid input character code %1", int(c
));
355 seen_escape
= (c
== '\\');
368 int file_iterator::peek()
371 while (invalid_input_char(c
)) {
372 warning(WARN_INPUT
, "invalid input character code %1", int(c
));
380 int file_iterator::get_location(int /*allow_macro*/,
381 const char **filenamep
, int *linenop
)
384 if (filename
!= 0 && strcmp(filename
, "-") == 0)
385 *filenamep
= "<standard input>";
387 *filenamep
= filename
;
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
)
403 the_output
->put_filename(f
);
409 input_iterator nil_iterator
;
413 static int get(node
**);
415 static void push(input_iterator
*);
416 static input_iterator
*get_arg(int);
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();
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
);
440 static int div_level
;
441 static statem
*diversion_state
;
443 static input_iterator
*top
;
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
;
457 inline int input_stack::get_level()
462 inline void input_stack::increase_level()
467 inline void input_stack::decrease_level()
472 inline int input_stack::get_div_level()
477 inline int input_stack::get(node
**np
)
479 int res
= (top
->ptr
< top
->eptr
) ? *top
->ptr
++ : finish_get(np
);
481 old_have_input
= have_input
;
487 int input_stack::finish_get(node
**np
)
490 int c
= top
->fill(np
);
491 if (c
!= EOF
|| top
->is_boundary())
493 if (top
== &nil_iterator
)
495 input_iterator
*tem
= top
;
496 check_end_diversion(tem
);
497 #if defined(DEBUGGING)
499 if (tem
->is_diversion
)
501 "in diversion level = %d\n", input_stack::get_div_level());
506 if (top
->ptr
< top
->eptr
)
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
) {
522 diversion_state
= t
->diversion_state
;
526 int input_stack::finish_peek()
530 if (c
!= EOF
|| top
->is_boundary())
532 if (top
== &nil_iterator
)
534 input_iterator
*tem
= top
;
535 check_end_diversion(tem
);
539 if (top
->ptr
< top
->eptr
)
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
);
572 void input_stack::push(input_iterator
*in
)
576 if (++level
> limit
&& limit
> 0)
577 fatal("input stack limit exceeded (probable infinite loop)");
580 if (top
->is_diversion
) {
582 in
->diversion_state
= diversion_state
;
583 diversion_state
= curenv
->construct_state(0);
584 #if defined(DEBUGGING)
586 curenv
->dump_troff_state();
591 #if defined(DEBUGGING)
593 if (top
->is_diversion
) {
595 "in diversion level = %d\n", input_stack::get_div_level());
601 statem
*get_diversion_state()
603 return input_stack::get_diversion_state();
606 statem
*input_stack::get_diversion_state()
608 if (diversion_state
== NULL
)
611 return new statem(diversion_state
);
614 input_iterator
*input_stack::get_arg(int i
)
617 for (p
= top
; p
!= 0; p
= p
->next
)
619 return p
->get_arg(i
);
623 void input_stack::shift(int n
)
625 for (input_iterator
*p
= top
; p
; p
= p
->next
)
632 int input_stack::nargs()
634 for (input_iterator
*p
=top
; p
!= 0; p
= p
->next
)
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
))
648 void input_stack::backtrace()
652 // only backtrace down to (not including) the topmost file
653 for (input_iterator
*p
= top
;
654 p
&& !p
->get_location(0, &f
, &n
);
659 void input_stack::backtrace_all()
661 for (input_iterator
*p
= top
; p
; p
= p
->next
)
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
))
673 void input_stack::next_file(FILE *fp
, const char *s
)
676 for (pp
= &top
; *pp
!= &nil_iterator
; pp
= &(*pp
)->next
)
677 if ((*pp
)->next_file(fp
, s
))
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
);
698 void input_stack::clear()
701 while (top
!= &nil_iterator
) {
702 if (top
->is_boundary())
704 input_iterator
*tem
= top
;
705 check_end_diversion(tem
);
710 // Keep while_request happy.
711 for (; nboundaries
> 0; --nboundaries
)
712 add_return_boundary();
715 void input_stack::pop_macro()
720 if (top
->next
== &nil_iterator
)
722 if (top
->is_boundary())
724 is_macro
= top
->is_macro();
725 input_iterator
*tem
= top
;
726 check_end_diversion(tem
);
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();
755 symbol nm
= get_long_name();
756 while (!tok
.newline() && !tok
.eof())
759 input_stack::end_file();
762 FILE *fp
= include_search_path
.open_file_cautious(nm
.contents());
764 error("can't open `%1': %2", nm
.contents(), strerror(errno
));
766 input_stack::next_file(fp
, nm
.contents());
774 if (!has_arg() || !get_integer(&n
))
776 input_stack::shift(n
);
780 static char get_char_for_escape_name(int allow_space
= 0)
785 copy_mode_error("end of input in escape name");
788 if (!invalid_input_char(c
))
793 input_stack::push(make_temp_iterator("\n"));
796 if (c
== ' ' && allow_space
)
802 copy_mode_error("%1 is not allowed in an escape name",
803 input_char_description(c
));
809 static symbol
read_two_char_escape_name()
812 buf
[0] = get_char_for_escape_name();
813 if (buf
[0] != '\0') {
814 buf
[1] = get_char_for_escape_name();
823 static symbol
read_long_escape_name(read_mode mode
)
825 int start_level
= input_stack::get_level();
826 char abuf
[ABUF_SIZE
];
828 int buf_size
= ABUF_SIZE
;
833 c
= get_char_for_escape_name(have_char
&& mode
== WITH_ARGS
);
840 if (mode
== WITH_ARGS
&& c
== ' ')
842 if (i
+ 2 > buf_size
) {
844 buf
= new char[ABUF_SIZE
*2];
845 memcpy(buf
, abuf
, buf_size
);
846 buf_size
= ABUF_SIZE
*2;
850 buf
= new char[buf_size
*2];
851 memcpy(buf
, old_buf
, buf_size
);
856 if (c
== ']' && input_stack::get_level() == start_level
)
865 if (mode
!= ALLOW_EMPTY
)
866 copy_mode_error("empty escape name");
878 static symbol
read_escape_name(read_mode mode
)
880 char c
= get_char_for_escape_name();
884 return read_two_char_escape_name();
885 if (c
== '[' && !compatible_flag
)
886 return read_long_escape_name(mode
);
893 static symbol
read_increment_and_escape_name(int *incp
)
895 char c
= get_char_for_escape_name();
902 return read_two_char_escape_name();
905 return read_escape_name();
908 return read_escape_name();
910 if (!compatible_flag
) {
912 return read_long_escape_name();
923 static int get_copy(node
**nd
, int defining
)
926 int c
= input_stack::get(nd
);
927 if (c
== PUSH_GROFF_MODE
) {
928 input_stack::save_compatible_flag(compatible_flag
);
932 if (c
== PUSH_COMP_MODE
) {
933 input_stack::save_compatible_flag(compatible_flag
);
937 if (c
== POP_GROFFCOMP_MODE
) {
938 compatible_flag
= input_stack::get_compatible_flag();
941 if (c
== BEGIN_QUOTE
) {
942 input_stack::increase_level();
945 if (c
== END_QUOTE
) {
946 input_stack::decrease_level();
949 if (c
== ESCAPE_NEWLINE
) {
953 c
= input_stack::get(nd
);
954 } while (c
== ESCAPE_NEWLINE
);
956 if (c
!= escape_char
|| escape_char
<= 0)
958 c
= input_stack::peek();
963 (void)input_stack::get(0);
964 while ((c
= input_stack::get(0)) != '\n' && c
!= EOF
)
967 case '#': // Like \" but newline is ignored.
968 (void)input_stack::get(0);
969 while ((c
= input_stack::get(0)) != '\n')
975 (void)input_stack::get(0);
976 symbol s
= read_escape_name();
977 if (!(s
.is_null() || s
.is_empty()))
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
) {
988 interpolate_string_with_args(s
);
991 interpolate_string(s
);
996 (void)input_stack::get(0);
999 (void)input_stack::get(0);
1002 (void)input_stack::get(0);
1006 (void)input_stack::get(0);
1008 symbol s
= read_increment_and_escape_name(&inc
);
1009 if (!(s
.is_null() || s
.is_empty()))
1010 interpolate_number_reg(s
, inc
);
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
);
1022 (void)input_stack::get(0);
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
);
1033 (void)input_stack::get(0);
1035 return ESCAPE_NEWLINE
;
1038 (void)input_stack::get(0);
1039 return ESCAPE_SPACE
;
1041 (void)input_stack::get(0);
1042 return ESCAPE_TILDE
;
1044 (void)input_stack::get(0);
1045 return ESCAPE_COLON
;
1047 (void)input_stack::get(0);
1050 (void)input_stack::get(0);
1051 return ESCAPE_CIRCUMFLEX
;
1053 (void)input_stack::get(0);
1054 return ESCAPE_LEFT_BRACE
;
1056 (void)input_stack::get(0);
1057 return ESCAPE_RIGHT_BRACE
;
1059 (void)input_stack::get(0);
1060 return ESCAPE_LEFT_QUOTE
;
1062 (void)input_stack::get(0);
1063 return ESCAPE_RIGHT_QUOTE
;
1065 (void)input_stack::get(0);
1066 return ESCAPE_HYPHEN
;
1068 (void)input_stack::get(0);
1069 return ESCAPE_UNDERSCORE
;
1071 (void)input_stack::get(0);
1074 (void)input_stack::get(0);
1077 (void)input_stack::get(0);
1078 return ESCAPE_QUESTION
;
1080 (void)input_stack::get(0);
1081 return ESCAPE_AMPERSAND
;
1083 (void)input_stack::get(0);
1084 return ESCAPE_RIGHT_PARENTHESIS
;
1086 (void)input_stack::get(0);
1089 (void)input_stack::get(0);
1090 return ESCAPE_PERCENT
;
1092 if (c
== escape_char
) {
1093 (void)input_stack::get(0);
1102 class non_interpreted_char_node
: public node
{
1105 non_interpreted_char_node(unsigned char);
1107 int interpret(macro
*);
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()
1129 int non_interpreted_char_node::is_tag()
1134 non_interpreted_char_node::non_interpreted_char_node(unsigned char n
) : c(n
)
1139 node
*non_interpreted_char_node::copy()
1141 return new non_interpreted_char_node(c
);
1144 int non_interpreted_char_node::interpret(macro
*mac
)
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
);
1165 warning(WARN_COLOR
, "color `%1' not defined", nm
.contents());
1169 void do_glyph_color(symbol nm
)
1174 curenv
->set_glyph_color(curenv
->get_prev_glyph_color());
1176 color
*tem
= lookup_color(nm
);
1178 curenv
->set_glyph_color(tem
);
1180 (void)color_dictionary
.lookup(nm
, new color(nm
));
1184 void do_fill_color(symbol nm
)
1189 curenv
->set_fill_color(curenv
->get_prev_fill_color());
1191 color
*tem
= lookup_color(nm
);
1193 curenv
->set_fill_color(tem
);
1195 (void)color_dictionary
.lookup(nm
, new color(nm
));
1199 static unsigned int get_color_element(const char *scheme
, const char *col
)
1202 if (!get_number(&val
, 'f')) {
1203 warning(WARN_COLOR
, "%1 in %2 definition set to 0", col
, scheme
);
1208 warning(WARN_RANGE
, "%1 cannot be negative: set to 0", col
);
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");
1226 const char *s
= component
.contents();
1227 color
*col
= new color
;
1229 if (!col
->read_rgb(s
)) {
1230 warning(WARN_COLOR
, "expecting rgb color definition not `%1'", s
);
1237 input_stack::push(make_temp_iterator(" "));
1238 input_stack::push(make_temp_iterator(s
));
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
);
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");
1255 const char *s
= component
.contents();
1256 color
*col
= new color
;
1258 if (!col
->read_cmy(s
)) {
1259 warning(WARN_COLOR
, "expecting cmy color definition not `%1'", s
);
1266 input_stack::push(make_temp_iterator(" "));
1267 input_stack::push(make_temp_iterator(s
));
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
);
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");
1284 const char *s
= component
.contents();
1285 color
*col
= new color
;
1287 if (!col
->read_cmyk(s
)) {
1288 warning(WARN_COLOR
, "`expecting a cmyk color definition not `%1'", s
);
1295 input_stack::push(make_temp_iterator(" "));
1296 input_stack::push(make_temp_iterator(s
));
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
);
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");
1314 const char *s
= component
.contents();
1315 color
*col
= new color
;
1317 if (!col
->read_gray(s
)) {
1318 warning(WARN_COLOR
, "`expecting a gray definition not `%1'", s
);
1325 input_stack::push(make_temp_iterator("\n"));
1326 input_stack::push(make_temp_iterator(s
));
1328 unsigned int g
= get_color_element("gray", "gray value");
1334 static void activate_color()
1337 if (has_arg() && get_integer(&n
))
1338 color_flag
= n
!= 0;
1344 static void define_color()
1346 symbol color_name
= get_long_name(1);
1347 if (color_name
.is_null()) {
1351 if (color_name
== default_symbol
) {
1352 warning(WARN_COLOR
, "default color can't be redefined");
1356 symbol style
= get_long_name(1);
1357 if (style
.is_null()) {
1362 if (strcmp(style
.contents(), "rgb") == 0)
1364 else if (strcmp(style
.contents(), "cmyk") == 0)
1366 else if (strcmp(style
.contents(), "gray") == 0)
1368 else if (strcmp(style
.contents(), "grey") == 0)
1370 else if (strcmp(style
.contents(), "cmy") == 0)
1374 "unknown color space `%1'; use rgb, cmyk, gray or cmy",
1380 col
->nm
= color_name
;
1381 (void)color_dictionary
.lookup(color_name
, col
);
1386 static node
*do_overstrike()
1389 overstrike_node
*on
= new overstrike_node
;
1390 int start_level
= input_stack::get_level();
1394 if (tok
.newline() || tok
.eof()) {
1395 warning(WARN_DELIM
, "missing closing delimiter");
1396 input_stack::push(make_temp_iterator("\n"));
1400 && (compatible_flag
|| input_stack::get_level() == start_level
))
1402 charinfo
*ci
= tok
.get_char(1);
1404 node
*n
= curenv
->make_char_node(ci
);
1412 static node
*do_bracket()
1415 bracket_node
*bn
= new bracket_node
;
1417 int start_level
= input_stack::get_level();
1421 warning(WARN_DELIM
, "missing closing delimiter");
1424 if (tok
.newline()) {
1425 warning(WARN_DELIM
, "missing closing delimiter");
1426 input_stack::push(make_temp_iterator("\n"));
1430 && (compatible_flag
|| input_stack::get_level() == start_level
))
1432 charinfo
*ci
= tok
.get_char(1);
1434 node
*n
= curenv
->make_char_node(ci
);
1442 static int do_name_test()
1446 int start_level
= input_stack::get_level();
1451 if (tok
.newline() || tok
.eof()) {
1452 warning(WARN_DELIM
, "missing closing delimiter");
1453 input_stack::push(make_temp_iterator("\n"));
1457 && (compatible_flag
|| input_stack::get_level() == start_level
))
1463 return some_char
&& !bad_char
;
1466 static int do_expr_test()
1470 int start_level
= input_stack::get_level();
1471 if (!start
.delimiter(1))
1474 // disable all warning and error messages temporarily
1475 int saved_warning_mask
= warning_mask
;
1476 int saved_inhibit_errors
= inhibit_errors
;
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
)
1485 // ignore everything up to the delimiter in case we aren't right there
1488 if (tok
.newline() || tok
.eof()) {
1489 warning(WARN_DELIM
, "missing closing delimiter");
1490 input_stack::push(make_temp_iterator("\n"));
1493 if (tok
== start
&& input_stack::get_level() == start_level
)
1500 static node
*do_zero_width()
1504 int start_level
= input_stack::get_level();
1505 environment
env(curenv
);
1506 environment
*oldenv
= curenv
;
1510 if (tok
.newline() || tok
.eof()) {
1511 error("missing closing delimiter");
1515 && (compatible_flag
|| input_stack::get_level() == start_level
))
1520 node
*rev
= env
.extract_output_line();
1528 return new zero_width_node(n
);
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
;
1541 int start_level
= input_stack::get_level();
1544 if (tok
.newline() || tok
.eof()) {
1545 warning(WARN_DELIM
, "missing closing delimiter");
1546 input_stack::push(make_temp_iterator("\n"));
1550 && (compatible_flag
|| input_stack::get_level() == start_level
))
1552 if (!tok
.add_to_node_list(&rev
))
1553 error("invalid token in argument to \\Z");
1562 return new zero_width_node(n
);
1567 token_node
*node::get_token_node()
1572 class token_node
: public node
{
1575 token_node(const token
&t
);
1577 token_node
*get_token_node();
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()
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()
1613 int token_node::is_tag()
1618 token::token() : nd(0), type(TOKEN_EMPTY
)
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++.
1632 nd
= tem
? tem
->copy() : 0;
1635 void token::operator=(const token
&t
)
1639 // Use two statements to work around bug in SGI C++.
1641 nd
= tem
? tem
->copy() : 0;
1658 return !tok
.newline();
1661 void token::make_space()
1666 void token::make_newline()
1668 type
= TOKEN_NEWLINE
;
1680 int cc
= input_stack::get(&n
);
1681 if (cc
!= escape_char
|| escape_char
== 0) {
1684 case PUSH_GROFF_MODE
:
1685 input_stack::save_compatible_flag(compatible_flag
);
1686 compatible_flag
= 0;
1688 case PUSH_COMP_MODE
:
1689 input_stack::save_compatible_flag(compatible_flag
);
1690 compatible_flag
= 1;
1692 case POP_GROFFCOMP_MODE
:
1693 compatible_flag
= input_stack::get_compatible_flag();
1696 input_stack::increase_level();
1699 input_stack::decrease_level();
1704 case TRANSPARENT_FILE_REQUEST
:
1706 case COPY_FILE_REQUEST
:
1708 case VJUSTIFY_REQUEST
:
1710 type
= TOKEN_REQUEST
;
1714 type
= TOKEN_BEGIN_TRAP
;
1717 type
= TOKEN_END_TRAP
;
1719 case LAST_PAGE_EJECTOR
:
1720 seen_last_page_ejector
= 1;
1723 type
= TOKEN_PAGE_EJECTOR
;
1725 case ESCAPE_PERCENT
:
1727 type
= TOKEN_HYPHEN_INDICATOR
;
1731 type
= TOKEN_UNSTRETCHABLE_SPACE
;
1735 type
= TOKEN_STRETCHABLE_SPACE
;
1739 type
= TOKEN_ZERO_WIDTH_BREAK
;
1743 type
= TOKEN_ESCAPE
;
1746 goto handle_escape_char
;
1750 nd
= new hmotion_node(curenv
->get_narrow_space_width(),
1751 curenv
->get_fill_color());
1753 case ESCAPE_CIRCUMFLEX
:
1756 nd
= new hmotion_node(curenv
->get_half_narrow_space_width(),
1757 curenv
->get_fill_color());
1759 case ESCAPE_NEWLINE
:
1762 case ESCAPE_LEFT_BRACE
:
1764 type
= TOKEN_LEFT_BRACE
;
1766 case ESCAPE_RIGHT_BRACE
:
1768 type
= TOKEN_RIGHT_BRACE
;
1770 case ESCAPE_LEFT_QUOTE
:
1772 type
= TOKEN_SPECIAL
;
1775 case ESCAPE_RIGHT_QUOTE
:
1777 type
= TOKEN_SPECIAL
;
1782 type
= TOKEN_SPECIAL
;
1785 case ESCAPE_UNDERSCORE
:
1787 type
= TOKEN_SPECIAL
;
1792 type
= TOKEN_INTERRUPT
;
1796 type
= TOKEN_TRANSPARENT
;
1798 case ESCAPE_QUESTION
:
1800 nd
= do_non_interpreted();
1806 case ESCAPE_AMPERSAND
:
1810 case ESCAPE_RIGHT_PARENTHESIS
:
1811 ESCAPE_RIGHT_PARENTHESIS
:
1812 type
= TOKEN_TRANSPARENT_DUMMY
;
1815 type
= TOKEN_BACKSPACE
;
1824 type
= TOKEN_NEWLINE
;
1827 type
= TOKEN_LEADER
;
1832 token_node
*tn
= n
->get_token_node();
1851 cc
= input_stack::get(&n
);
1854 nm
= read_two_char_escape_name();
1855 type
= TOKEN_SPECIAL
;
1859 error("end of input after escape character");
1862 goto ESCAPE_LEFT_QUOTE
;
1864 goto ESCAPE_RIGHT_QUOTE
;
1868 goto ESCAPE_UNDERSCORE
;
1870 goto ESCAPE_PERCENT
;
1874 nd
= new hmotion_node(curenv
->get_digit_width(),
1875 curenv
->get_fill_color());
1881 goto ESCAPE_CIRCUMFLEX
;
1883 type
= TOKEN_ITALIC_CORRECTION
;
1887 nd
= new left_italic_corrected_node
;
1890 goto ESCAPE_AMPERSAND
;
1892 goto ESCAPE_RIGHT_PARENTHESIS
;
1896 goto ESCAPE_QUESTION
;
1902 while ((cc
= input_stack::get(0)) != '\n' && cc
!= EOF
)
1905 type
= TOKEN_NEWLINE
;
1909 case '#': // Like \" but newline is ignored.
1910 while ((cc
= input_stack::get(0)) != '\n')
1918 symbol s
= read_escape_name();
1919 if (!(s
.is_null() || s
.is_empty()))
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
);
1932 interpolate_string(s
);
1937 nd
= new non_interpreted_char_node('\001');
1941 c
= '0' + do_name_test();
1949 c
= '0' + do_expr_test();
1955 nm
= get_delim_name();
1958 type
= TOKEN_SPECIAL
;
1962 nd
= new vmotion_node(curenv
->get_size() / 2,
1963 curenv
->get_fill_color());
1966 nd
= read_draw_node();
1974 goto handle_escape_char
;
1977 symbol s
= read_escape_name(ALLOW_EMPTY
);
1981 for (p
= s
.contents(); *p
!= '\0'; p
++)
1984 if (*p
|| s
.is_empty())
1985 curenv
->set_font(s
);
1987 curenv
->set_font(atoi(s
.contents()));
1988 if (!compatible_flag
)
1994 symbol s
= read_escape_name(ALLOW_EMPTY
);
1997 curenv
->set_family(s
);
2003 symbol s
= read_escape_name();
2004 if (!(s
.is_null() || s
.is_empty()))
2005 interpolate_number_format(s
);
2009 if (!get_delim_number(&x
, 'm'))
2012 nd
= new hmotion_node(x
, curenv
->get_fill_color());
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
);
2024 if (get_delim_number(&x
, 'z', curenv
->get_requested_point_size()))
2025 curenv
->set_char_height(x
);
2027 if (!compatible_flag
)
2031 nm
= read_escape_name();
2032 if (nm
.is_null() || nm
.is_empty())
2034 type
= TOKEN_MARK_INPUT
;
2040 if (!get_line_arg(&x
, (cc
== 'l' ? 'm': 'v'), &s
))
2043 s
= get_charinfo(cc
== 'l' ? "ru" : "br");
2045 node
*char_node
= curenv
->make_char_node(s
);
2047 nd
= new hline_node(x
, char_node
);
2049 nd
= new vline_node(x
, char_node
);
2053 do_glyph_color(read_escape_name(ALLOW_EMPTY
));
2054 if (!compatible_flag
)
2058 do_fill_color(read_escape_name(ALLOW_EMPTY
));
2059 if (!compatible_flag
)
2065 symbol s
= read_increment_and_escape_name(&inc
);
2066 if (!(s
.is_null() || s
.is_empty()))
2067 interpolate_number_reg(s
, inc
);
2071 if (!get_delim_number(&val
, 0))
2073 type
= TOKEN_NUMBERED_CHAR
;
2076 nd
= do_overstrike();
2080 nd
= do_suppress(read_escape_name());
2086 type
= TOKEN_SPREAD
;
2090 nd
= new vmotion_node(-curenv
->get_size(), curenv
->get_fill_color());
2094 if (!compatible_flag
)
2099 curenv
->set_size(x
);
2100 if (!compatible_flag
)
2104 if (get_delim_number(&x
, 0))
2105 curenv
->set_char_slant(x
);
2106 if (!compatible_flag
)
2111 nd
= new non_interpreted_char_node('\t');
2115 nd
= new vmotion_node(-curenv
->get_size() / 2,
2116 curenv
->get_fill_color());
2119 if (!get_delim_number(&x
, 'v'))
2122 nd
= new vmotion_node(x
, curenv
->get_fill_color());
2126 symbol s
= read_escape_name();
2127 if (!(s
.is_null() || s
.is_empty()))
2128 interpolate_environment_variable(s
);
2135 if (!get_delim_number(&x
, 'v'))
2138 nd
= new extra_size_node(x
);
2148 symbol s
= read_escape_name();
2149 if (s
.is_null() || s
.is_empty())
2151 request_or_macro
*p
= lookup_request(s
);
2152 macro
*m
= p
->to_macro();
2154 error("can't transparently throughput a request");
2157 nd
= new special_node(*m
);
2164 if (type
== TOKEN_NODE
)
2165 nd
= new zero_width_node(nd
);
2167 charinfo
*ci
= get_char(1);
2170 node
*gn
= curenv
->make_char_node(ci
);
2173 nd
= new zero_width_node(gn
);
2179 nd
= do_zero_width();
2185 goto ESCAPE_LEFT_BRACE
;
2187 goto ESCAPE_RIGHT_BRACE
;
2191 if (!compatible_flag
) {
2192 symbol s
= read_long_escape_name(WITH_ARGS
);
2193 if (s
.is_null() || s
.is_empty())
2195 if (have_string_arg
) {
2196 have_string_arg
= 0;
2197 nm
= composite_glyph_name(s
);
2200 const char *gn
= check_unicode_name(s
.contents());
2202 const char *gn_decomposed
= decompose_unicode(gn
);
2204 gn
= &gn_decomposed
[1];
2205 const char *groff_gn
= unicode_to_glyph_name(gn
);
2207 nm
= symbol(groff_gn
);
2209 char *buf
= new char[strlen(gn
) + 1 + 1];
2217 nm
= symbol(s
.contents());
2219 type
= TOKEN_SPECIAL
;
2222 goto handle_normal_char
;
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
)
2242 case TOKEN_NUMBERED_CHAR
:
2243 return val
== t
.val
;
2249 int token::operator!=(const token
&t
)
2251 return !(*this == t
);
2254 // is token a suitable delimiter (like ')?
2256 int token::delimiter(int err
)
2285 error("cannot use character `%1' as a starting delimiter", char(c
));
2292 case TOKEN_STRETCHABLE_SPACE
:
2293 case TOKEN_UNSTRETCHABLE_SPACE
:
2297 error("cannot use %1 as a starting delimiter", description());
2304 const char *token::description()
2308 case TOKEN_BACKSPACE
:
2309 return "a backspace character";
2320 case TOKEN_HYPHEN_INDICATOR
:
2322 case TOKEN_INTERRUPT
:
2324 case TOKEN_ITALIC_CORRECTION
:
2327 return "a leader character";
2328 case TOKEN_LEFT_BRACE
:
2330 case TOKEN_MARK_INPUT
:
2336 case TOKEN_NUMBERED_CHAR
:
2338 case TOKEN_RIGHT_BRACE
:
2343 return "a special character";
2346 case TOKEN_STRETCHABLE_SPACE
:
2348 case TOKEN_UNSTRETCHABLE_SPACE
:
2351 return "a tab character";
2352 case TOKEN_TRANSPARENT
:
2354 case TOKEN_TRANSPARENT_DUMMY
:
2356 case TOKEN_ZERO_WIDTH_BREAK
:
2359 return "end of input";
2363 return "a magic token";
2368 while (!tok
.newline())
2379 if (has_arg() && get_integer(&n
))
2380 compatible_flag
= n
!= 0;
2382 compatible_flag
= 1;
2386 static void empty_name_warning(int required
)
2388 if (tok
.newline() || tok
.eof()) {
2390 warning(WARN_MISSING
, "missing name");
2392 else if (tok
.right_brace() || tok
.tab()) {
2393 const char *start
= tok
.description();
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
);
2400 warning(WARN_MISSING
, "missing name");
2403 error("name expected (got %1)", tok
.description());
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
) {
2422 if ((buf
[0] = tok
.ch()) != 0) {
2424 if ((buf
[1] = tok
.ch()) != 0) {
2429 non_empty_name_warning();
2433 empty_name_warning(required
);
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
)
2450 char abuf
[ABUF_SIZE
];
2452 int buf_size
= ABUF_SIZE
;
2455 // If end != 0 we normally have to append a null byte
2456 if (i
+ 2 > buf_size
) {
2458 buf
= new char[ABUF_SIZE
*2];
2459 memcpy(buf
, abuf
, buf_size
);
2460 buf_size
= ABUF_SIZE
*2;
2463 char *old_buf
= buf
;
2464 buf
= new char[buf_size
*2];
2465 memcpy(buf
, old_buf
, buf_size
);
2470 if ((buf
[i
] = tok
.ch()) == 0 || buf
[i
] == end
)
2476 empty_name_warning(required
);
2479 if (end
&& buf
[i
] == end
)
2482 non_empty_name_warning();
2495 topdiv
->set_last_page();
2496 if (!end_macro_name
.is_null()) {
2497 spring_trap(end_macro_name
);
2499 process_input_stack();
2501 curenv
->final_break();
2503 process_input_stack();
2505 if (topdiv
->get_page_length() > 0) {
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);
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);
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
2530 input_stack::clear();
2537 void return_macro_request()
2539 if (has_arg() && tok
.ch())
2540 input_stack::pop_macro();
2541 input_stack::pop_macro();
2547 end_macro_name
= get_name();
2551 void blank_line_macro()
2553 blank_line_macro_name
= get_name();
2557 static void trapping_blank_line()
2559 if (!blank_line_macro_name
.is_null())
2560 spring_trap(blank_line_macro_name
);
2567 int old_compatible_flag
= compatible_flag
;
2568 compatible_flag
= 0;
2569 symbol nm
= get_name();
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();
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
:
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);
2604 int c
= ci
->get_ascii_code();
2607 error("can't translate %1 to special character `%2'"
2608 " in transparent throughput",
2609 input_char_description(cc
),
2617 struct int_stack_element
{
2619 int_stack_element
*next
;
2629 int_stack::int_stack()
2634 int_stack::~int_stack()
2637 int_stack_element
*temp
= top
;
2643 int int_stack::is_empty()
2648 void int_stack::push(int n
)
2650 int_stack_element
*p
= new int_stack_element
;
2656 int int_stack::pop()
2659 int_stack_element
*p
= top
;
2666 int node::reread(int *)
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();
2680 global_diverted_space
= 0;
2685 int diverted_copy_file_node::reread(int *bolp
)
2687 curdiv
->copy_file(filename
.contents());
2692 int word_space_node::reread(int *)
2695 for (width_list
*w
= orig_width
; w
; w
= w
->next
)
2696 curenv
->space(w
->width
, w
->sentence_width
);
2703 int unbreakable_space_node::reread(int *)
2708 int hmotion_node::reread(int *)
2710 if (unformat
&& was_tab
) {
2711 curenv
->handle_tab(0);
2718 void process_input_stack()
2720 int_stack trap_bol_stack
;
2723 int suppress_next
= 0;
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
2735 } while (tok
.white_space());
2736 symbol nm
= get_name();
2737 #if defined(DEBUGGING)
2739 if (! nm
.is_null()) {
2740 if (strcmp(nm
.contents(), "test") == 0) {
2741 fprintf(stderr
, "found it!\n");
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");
2756 interpolate_macro(nm
);
2757 #if defined(DEBUGGING)
2759 fprintf(stderr
, "finished interpreting [%s] and environment state is\n", nm
.contents());
2760 curenv
->dump_troff_state();
2767 if (possibly_handle_first_page_transition())
2771 #if defined(DEBUGGING)
2773 fprintf(stderr
, "found [%c]\n", ch
); fflush(stderr
);
2776 curenv
->add_char(charset_table
[ch
]);
2778 if (tok
.type
!= token::TOKEN_CHAR
)
2788 case token::TOKEN_TRANSPARENT
:
2791 if (possibly_handle_first_page_transition())
2800 curdiv
->transparent_output(transparent_translate(cc
));
2802 curdiv
->transparent_output(n
);
2803 } while (cc
!= '\n' && cc
!= EOF
);
2805 curdiv
->transparent_output('\n');
2810 case token::TOKEN_NEWLINE
:
2812 if (bol
&& !old_have_input
2813 && !curenv
->get_prev_line_interrupted())
2814 trapping_blank_line();
2821 case token::TOKEN_REQUEST
:
2823 int request_code
= tok
.c
;
2825 switch (request_code
) {
2829 case COPY_FILE_REQUEST
:
2832 case TRANSPARENT_FILE_REQUEST
:
2836 case VJUSTIFY_REQUEST
:
2847 case token::TOKEN_SPACE
:
2849 if (possibly_handle_first_page_transition())
2851 else if (bol
&& !curenv
->get_prev_line_interrupted()) {
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();
2857 nspaces
+= tok
.nspaces();
2859 } while (tok
.space());
2861 trapping_blank_line();
2865 curenv
->add_node(new hmotion_node(space_width
* nspaces
,
2866 curenv
->get_fill_color()));
2876 case token::TOKEN_EOF
:
2878 case token::TOKEN_NODE
:
2880 if (possibly_handle_first_page_transition())
2882 else if (tok
.nd
->reread(&bol
)) {
2887 curenv
->add_node(tok
.nd
);
2890 curenv
->possibly_break_line(1);
2894 case token::TOKEN_PAGE_EJECTOR
:
2896 continue_page_eject();
2897 // I think we just want to preserve bol.
2901 case token::TOKEN_BEGIN_TRAP
:
2903 trap_bol_stack
.push(bol
);
2908 case token::TOKEN_END_TRAP
:
2910 if (trap_bol_stack
.is_empty())
2911 error("spurious end trap token detected!");
2913 bol
= trap_bol_stack
.pop();
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,
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();
2949 trap_sprung_flag
= 0;
2953 #ifdef WIDOW_CONTROL
2955 void flush_pending_lines()
2957 while (!tok
.newline() && !tok
.eof())
2959 curenv
->output_pending_lines();
2963 #endif /* WIDOW_CONTROL */
2965 request_or_macro::request_or_macro()
2969 macro
*request_or_macro::to_macro()
2974 request::request(REQUEST_FUNCP pp
) : p(pp
)
2978 void request::invoke(symbol
)
2984 enum { SIZE
= 128 };
2985 unsigned char s
[SIZE
];
2990 char_block::char_block()
2999 void append(unsigned char);
3000 void set(unsigned char, int);
3001 unsigned char get(int);
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()
3020 char_block
*tem
= head
;
3026 int char_list::length()
3031 void char_list::append(unsigned char c
)
3034 head
= tail
= new char_block
;
3038 if (ptr
>= tail
->s
+ char_block::SIZE
) {
3039 tail
->next
= new char_block
;
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
;
3057 char_block
*tem
= head
;
3060 l
+= char_block::SIZE
;
3062 *(tem
->s
+ offset
% char_block::SIZE
) = c
;
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
;
3079 l
+= char_block::SIZE
;
3081 return *(tem
->s
+ offset
% char_block::SIZE
);
3092 void append(node
*);
3096 friend class macro_header
;
3097 friend class string_iterator
;
3100 void node_list::append(node
*n
)
3108 tail
= tail
->next
= n
;
3112 int node_list::length()
3115 for (node
*n
= head
; n
!= 0; n
= n
->next
)
3120 node_list::node_list()
3125 node
*node_list::extract()
3132 node_list::~node_list()
3134 delete_node_list(head
);
3137 class macro_header
{
3142 macro_header() { count
= 1; }
3143 macro_header
*copy(int);
3148 if (p
!= 0 && --(p
->count
) <= 0)
3155 if (!input_stack::get_location(1, &filename
, &lineno
)) {
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
)
3172 macro::macro(int is_div
)
3173 : is_a_diversion(is_div
)
3175 if (!input_stack::get_location(1, &filename
, &lineno
)) {
3184 int macro::is_diversion()
3186 return is_a_diversion
;
3189 macro
¯o::operator=(const macro
&m
)
3191 // don't assign object
3194 if (p
!= 0 && --(p
->count
) <= 0)
3197 filename
= m
.filename
;
3200 empty_macro
= m
.empty_macro
;
3201 is_a_diversion
= m
.is_a_diversion
;
3205 void macro::append(unsigned char c
)
3209 p
= new macro_header
;
3210 if (p
->cl
.length() != len
) {
3211 macro_header
*tem
= p
->copy(len
);
3212 if (--(p
->count
) <= 0)
3218 if (c
!= PUSH_GROFF_MODE
&& c
!= PUSH_COMP_MODE
&& c
!= POP_GROFFCOMP_MODE
)
3222 void macro::set(unsigned char c
, int offset
)
3226 p
->cl
.set(c
, offset
);
3229 unsigned char macro::get(int offset
)
3232 return p
->cl
.get(offset
);
3240 void macro::append_str(const char *s
)
3245 while (s
[i
] != (char)0) {
3252 void macro::append(node
*n
)
3256 p
= new macro_header
;
3257 if (p
->cl
.length() != len
) {
3258 macro_header
*tem
= p
->copy(len
);
3259 if (--(p
->count
) <= 0)
3269 void macro::append_unsigned(unsigned int i
)
3271 unsigned int j
= i
/ 10;
3274 append(((unsigned char)(((int)'0') + i
% 10)));
3277 void macro::append_int(int 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
;
3300 if (ptr
>= bp
->s
+ char_block::SIZE
) {
3304 unsigned char c
= *ptr
++;
3307 p
->nl
.append(nd
->copy());
3316 object_dictionary_iterator
iter(request_dictionary
);
3317 request_or_macro
*rm
;
3319 while (iter
.get(&s
, (object
**)&rm
)) {
3320 assert(!s
.is_null());
3321 macro
*m
= rm
->to_macro();
3323 errprint("%1\t", s
.contents());
3332 class string_iterator
: public input_iterator
{
3334 const char *how_invoked
;
3338 int count
; // of characters remaining
3340 int saved_compatible_flag
;
3345 string_iterator(const macro
&m
, const char *p
= 0, symbol s
= NULL_SYMBOL
);
3348 int get_location(int, const char **, int *);
3350 void save_compatible_flag(int f
) { saved_compatible_flag
= f
; }
3351 int get_compatible_flag() { return saved_compatible_flag
; }
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),
3361 bp
= mac
.p
->cl
.head
;
3362 nd
= mac
.p
->nl
.head
;
3372 string_iterator::string_iterator()
3383 int string_iterator::is_diversion()
3385 return mac
.is_diversion();
3388 int string_iterator::fill(node
**np
)
3395 const unsigned char *p
= eptr
;
3396 if (p
>= bp
->s
+ char_block::SIZE
) {
3404 (*np
)->div_nest_level
= input_stack::get_div_level();
3406 (*np
)->div_nest_level
= 0;
3413 const unsigned char *e
= bp
->s
+ char_block::SIZE
;
3418 unsigned char c
= *p
;
3419 if (c
== '\n' || c
== ESCAPE_NEWLINE
) {
3433 int string_iterator::peek()
3437 const unsigned char *p
= eptr
;
3438 if (p
>= bp
->s
+ char_block::SIZE
) {
3444 int string_iterator::get_location(int allow_macro
,
3445 const char **filep
, int *linep
)
3449 if (mac
.filename
== 0)
3451 *filep
= mac
.filename
;
3452 *linep
= mac
.lineno
+ lineno
- 1;
3456 void string_iterator::backtrace()
3459 errprint("%1:%2: backtrace", mac
.filename
, mac
.lineno
+ lineno
- 1);
3462 errprint(": %1 `%2'\n", how_invoked
, nm
.contents());
3464 errprint(": %1\n", how_invoked
);
3471 class temp_iterator
: public input_iterator
{
3472 unsigned char *base
;
3473 temp_iterator(const char *, int len
);
3476 friend input_iterator
*make_temp_iterator(const char *);
3482 temp_iterator::temp_iterator(const char *s
, int len
)
3484 base
= new unsigned char[len
];
3485 memcpy(base
, s
, len
);
3490 temp_iterator::~temp_iterator()
3495 class small_temp_iterator
: public input_iterator
{
3497 small_temp_iterator(const char *, int);
3498 ~small_temp_iterator();
3499 enum { BLOCK
= 16 };
3500 static small_temp_iterator
*free_list
;
3501 void *operator new(size_t);
3502 void operator delete(void *);
3504 unsigned char buf
[SIZE
];
3505 friend input_iterator
*make_temp_iterator(const char *);
3508 small_temp_iterator
*small_temp_iterator::free_list
= 0;
3510 void *small_temp_iterator::operator new(size_t n
)
3512 assert(n
== sizeof(small_temp_iterator
));
3515 (small_temp_iterator
*)new char[sizeof(small_temp_iterator
)*BLOCK
];
3516 for (int i
= 0; i
< BLOCK
- 1; i
++)
3517 free_list
[i
].next
= free_list
+ i
+ 1;
3518 free_list
[BLOCK
-1].next
= 0;
3520 small_temp_iterator
*p
= free_list
;
3521 free_list
= (small_temp_iterator
*)(free_list
->next
);
3529 void small_temp_iterator::operator delete(void *p
)
3532 ((small_temp_iterator
*)p
)->next
= free_list
;
3533 free_list
= (small_temp_iterator
*)p
;
3537 small_temp_iterator::~small_temp_iterator()
3544 small_temp_iterator::small_temp_iterator(const char *s
, int len
)
3546 for (int i
= 0; i
< len
; i
++)
3552 input_iterator
*make_temp_iterator(const char *s
)
3555 return new small_temp_iterator(s
, 0);
3558 if (n
<= small_temp_iterator::SIZE
)
3559 return new small_temp_iterator(s
, n
);
3561 return new temp_iterator(s
, n
);
3565 // this is used when macros with arguments are interpolated
3570 arg_list(const macro
&);
3574 arg_list::arg_list(const macro
&m
) : mac(m
), next(0)
3578 arg_list::~arg_list()
3582 class macro_iterator
: public string_iterator
{
3586 macro_iterator(symbol
, macro
&, const char *how_invoked
= "macro");
3589 int has_args() { return 1; }
3590 input_iterator
*get_arg(int i
);
3591 int nargs() { return argc
; }
3592 void add_arg(const macro
&m
);
3594 int is_macro() { return 1; }
3598 input_iterator
*macro_iterator::get_arg(int i
)
3601 return make_temp_iterator(nm
.contents());
3602 if (i
> 0 && i
<= argc
) {
3604 for (int j
= 1; j
< i
; j
++) {
3608 return new string_iterator(p
->mac
);
3614 void macro_iterator::add_arg(const macro
&m
)
3617 for (p
= &args
; *p
; p
= &((*p
)->next
))
3619 *p
= new arg_list(m
);
3623 void macro_iterator::shift(int n
)
3625 while (n
> 0 && argc
> 0) {
3626 arg_list
*tem
= args
;
3634 // This gets used by eg .if '\?xxx\?''.
3636 int operator==(const macro
&m1
, const macro
&m2
)
3638 if (m1
.len
!= m2
.len
)
3640 string_iterator
iter1(m1
);
3641 string_iterator
iter2(m2
);
3645 int c1
= iter1
.get(&nd1
);
3648 int c2
= iter2
.get(&nd2
);
3660 int are_same
= nd1
->type() == nd2
->type() && nd1
->same(nd2
);
3670 static void interpolate_macro(symbol nm
)
3672 request_or_macro
*p
= (request_or_macro
*)request_dictionary
.lookup(nm
);
3675 const char *s
= nm
.contents();
3676 if (strlen(s
) > 2) {
3677 request_or_macro
*r
;
3682 r
= (request_or_macro
*)request_dictionary
.lookup(symbol(buf
));
3684 macro
*m
= r
->to_macro();
3685 if (!m
|| !m
->empty())
3686 warned
= warning(WARN_SPACE
,
3687 "macro `%1' not defined "
3688 "(probably missing space after `%2')",
3689 nm
.contents(), buf
);
3693 warning(WARN_MAC
, "macro `%1' not defined", nm
.contents());
3695 request_dictionary
.define(nm
, p
);
3706 static void decode_args(macro_iterator
*mi
)
3708 if (!tok
.newline() && !tok
.eof()) {
3710 int c
= get_copy(&n
);
3714 if (c
== '\n' || c
== EOF
)
3717 int quote_input_level
= 0;
3718 int done_tab_warning
= 0;
3720 quote_input_level
= input_stack::get_level();
3723 arg
.append(compatible_flag
? PUSH_COMP_MODE
: PUSH_GROFF_MODE
);
3724 while (c
!= EOF
&& c
!= '\n' && !(c
== ' ' && quote_input_level
== 0)) {
3725 if (quote_input_level
> 0 && c
== '"'
3727 || input_stack::get_level() == quote_input_level
)) {
3740 if (c
== '\t' && quote_input_level
== 0 && !done_tab_warning
) {
3741 warning(WARN_TAB
, "tab character in unquoted macro argument");
3742 done_tab_warning
= 1;
3749 arg
.append(POP_GROFFCOMP_MODE
);
3755 static void decode_string_args(macro_iterator
*mi
)
3758 int c
= get_copy(&n
);
3762 if (c
== '\n' || c
== EOF
) {
3763 error("missing `]'");
3769 int quote_input_level
= 0;
3770 int done_tab_warning
= 0;
3772 quote_input_level
= input_stack::get_level();
3775 while (c
!= EOF
&& c
!= '\n'
3776 && !(c
== ']' && quote_input_level
== 0)
3777 && !(c
== ' ' && quote_input_level
== 0)) {
3778 if (quote_input_level
> 0 && c
== '"'
3779 && input_stack::get_level() == quote_input_level
) {
3792 if (c
== '\t' && quote_input_level
== 0 && !done_tab_warning
) {
3793 warning(WARN_TAB
, "tab character in unquoted string argument");
3794 done_tab_warning
= 1;
3805 void macro::invoke(symbol nm
)
3807 macro_iterator
*mi
= new macro_iterator(nm
, *this);
3809 input_stack::push(mi
);
3813 macro
*macro::to_macro()
3820 return empty_macro
== 1;
3823 macro_iterator::macro_iterator(symbol s
, macro
&m
, const char *how_called
)
3824 : string_iterator(m
, how_called
, s
), args(0), argc(0)
3828 macro_iterator::macro_iterator() : args(0), argc(0)
3832 macro_iterator::~macro_iterator()
3835 arg_list
*tem
= args
;
3841 dictionary
composite_dictionary(17);
3843 void composite_request()
3845 symbol from
= get_name(1);
3846 if (!from
.is_null()) {
3847 const char *from_gn
= glyph_name_to_unicode(from
.contents());
3849 from_gn
= check_unicode_name(from
.contents());
3851 error("invalid composite glyph name `%1'", from
.contents());
3856 const char *from_decomposed
= decompose_unicode(from_gn
);
3857 if (from_decomposed
)
3858 from_gn
= &from_decomposed
[1];
3859 symbol to
= get_name(1);
3861 composite_dictionary
.remove(symbol(from_gn
));
3863 const char *to_gn
= glyph_name_to_unicode(to
.contents());
3865 to_gn
= check_unicode_name(to
.contents());
3867 error("invalid composite glyph name `%1'", to
.contents());
3872 const char *to_decomposed
= decompose_unicode(to_gn
);
3874 to_gn
= &to_decomposed
[1];
3875 if (strcmp(from_gn
, to_gn
) == 0)
3876 composite_dictionary
.remove(symbol(from_gn
));
3878 (void)composite_dictionary
.lookup(symbol(from_gn
), (void *)to_gn
);
3884 static symbol
composite_glyph_name(symbol nm
)
3886 macro_iterator
*mi
= new macro_iterator();
3887 decode_string_args(mi
);
3888 input_stack::push(mi
);
3889 const char *gn
= glyph_name_to_unicode(nm
.contents());
3891 gn
= check_unicode_name(nm
.contents());
3893 error("invalid base glyph `%1' in composite glyph name", nm
.contents());
3894 return EMPTY_SYMBOL
;
3897 const char *gn_decomposed
= decompose_unicode(gn
);
3898 string
glyph_name(gn_decomposed
? &gn_decomposed
[1] : gn
);
3900 int n
= input_stack::nargs();
3901 for (int i
= 1; i
<= n
; i
++) {
3903 input_iterator
*p
= input_stack::get_arg(i
);
3906 while ((c
= p
->get(0)) != EOF
)
3909 const char *u
= glyph_name_to_unicode(gl
.contents());
3911 u
= check_unicode_name(gl
.contents());
3913 error("invalid component `%1' in composite glyph name",
3915 return EMPTY_SYMBOL
;
3918 const char *decomposed
= decompose_unicode(u
);
3921 void *mapped_composite
= composite_dictionary
.lookup(symbol(u
));
3922 if (mapped_composite
)
3923 u
= (const char *)mapped_composite
;
3927 const char *groff_gn
= unicode_to_glyph_name(glyph_name
.contents());
3929 return symbol(groff_gn
);
3933 return symbol(gl
.contents());
3936 int trap_sprung_flag
= 0;
3937 int postpone_traps_flag
= 0;
3938 symbol postponed_trap
;
3940 void spring_trap(symbol nm
)
3942 assert(!nm
.is_null());
3943 trap_sprung_flag
= 1;
3944 if (postpone_traps_flag
) {
3945 postponed_trap
= nm
;
3948 static char buf
[2] = { BEGIN_TRAP
, 0 };
3949 static char buf2
[2] = { END_TRAP
, '\0' };
3950 input_stack::push(make_temp_iterator(buf2
));
3951 request_or_macro
*p
= lookup_request(nm
);
3952 macro
*m
= p
->to_macro();
3954 input_stack::push(new macro_iterator(nm
, *m
, "trap-invoked macro"));
3956 error("you can't invoke a request with a trap");
3957 input_stack::push(make_temp_iterator(buf
));
3960 void postpone_traps()
3962 postpone_traps_flag
= 1;
3965 int unpostpone_traps()
3967 postpone_traps_flag
= 0;
3968 if (!postponed_trap
.is_null()) {
3969 spring_trap(postponed_trap
);
3970 postponed_trap
= NULL_SYMBOL
;
3979 macro_iterator
*mi
= new macro_iterator
;
3980 int reading_from_terminal
= isatty(fileno(stdin
));
3982 if (!tok
.newline() && !tok
.eof()) {
3983 int c
= get_copy(0);
3986 while (c
!= EOF
&& c
!= '\n' && c
!= ' ') {
3987 if (!invalid_input_char(c
)) {
3988 if (reading_from_terminal
)
3999 if (reading_from_terminal
) {
4000 fputc(had_prompt
? ':' : '\a', stderr
);
4003 input_stack::push(mi
);
4007 while ((c
= getchar()) != EOF
) {
4008 if (invalid_input_char(c
))
4009 warning(WARN_INPUT
, "invalid input character code %1", int(c
));
4022 if (reading_from_terminal
)
4024 input_stack::push(new string_iterator(mac
));
4028 enum define_mode
{ DEFINE_NORMAL
, DEFINE_APPEND
, DEFINE_IGNORE
};
4029 enum calling_mode
{ CALLING_NORMAL
, CALLING_INDIRECT
};
4030 enum comp_mode
{ COMP_IGNORE
, COMP_DISABLE
, COMP_ENABLE
};
4032 void do_define_string(define_mode mode
, comp_mode comp
)
4035 node
*n
= 0; // pacify compiler
4046 else if (!tok
.space()) {
4047 error("bad string definition");
4058 request_or_macro
*rm
= (request_or_macro
*)request_dictionary
.lookup(nm
);
4059 macro
*mm
= rm
? rm
->to_macro() : 0;
4060 if (mode
== DEFINE_APPEND
&& mm
)
4062 if (comp
== COMP_DISABLE
)
4063 mac
.append(PUSH_GROFF_MODE
);
4064 else if (comp
== COMP_ENABLE
)
4065 mac
.append(PUSH_COMP_MODE
);
4066 while (c
!= '\n' && c
!= EOF
) {
4070 mac
.append((unsigned char)c
);
4075 request_dictionary
.define(nm
, mm
);
4077 if (comp
== COMP_DISABLE
|| comp
== COMP_ENABLE
)
4078 mac
.append(POP_GROFFCOMP_MODE
);
4083 void define_string()
4085 do_define_string(DEFINE_NORMAL
,
4086 compatible_flag
? COMP_ENABLE
: COMP_IGNORE
);
4089 void define_nocomp_string()
4091 do_define_string(DEFINE_NORMAL
, COMP_DISABLE
);
4094 void append_string()
4096 do_define_string(DEFINE_APPEND
,
4097 compatible_flag
? COMP_ENABLE
: COMP_IGNORE
);
4100 void append_nocomp_string()
4102 do_define_string(DEFINE_APPEND
, COMP_DISABLE
);
4105 void do_define_character(char_mode mode
, const char *font_name
)
4107 node
*n
= 0; // pacify compiler
4110 charinfo
*ci
= tok
.get_char(1);
4116 string
s(font_name
);
4118 s
+= ci
->nm
.contents();
4120 ci
= get_charinfo(symbol(s
.contents()));
4127 else if (!tok
.space()) {
4128 error("bad character definition");
4134 while (c
== ' ' || c
== '\t')
4138 macro
*m
= new macro
;
4139 while (c
!= '\n' && c
!= EOF
) {
4143 m
->append((unsigned char)c
);
4146 m
= ci
->setx_macro(m
, mode
);
4152 void define_character()
4154 do_define_character(CHAR_NORMAL
);
4157 void define_fallback_character()
4159 do_define_character(CHAR_FALLBACK
);
4162 void define_special_character()
4164 do_define_character(CHAR_SPECIAL
);
4167 static void remove_character()
4170 while (!tok
.newline() && !tok
.eof()) {
4171 if (!tok
.space() && !tok
.tab()) {
4172 charinfo
*ci
= tok
.get_char(1);
4175 macro
*m
= ci
->set_macro(0);
4184 static void interpolate_string(symbol nm
)
4186 request_or_macro
*p
= lookup_request(nm
);
4187 macro
*m
= p
->to_macro();
4189 error("you can only invoke a string or macro using \\*");
4191 string_iterator
*si
= new string_iterator(*m
, "string", nm
);
4192 input_stack::push(si
);
4196 static void interpolate_string_with_args(symbol s
)
4198 request_or_macro
*p
= lookup_request(s
);
4199 macro
*m
= p
->to_macro();
4201 error("you can only invoke a string or macro using \\*");
4203 macro_iterator
*mi
= new macro_iterator(s
, *m
);
4204 decode_string_args(mi
);
4205 input_stack::push(mi
);
4209 static void interpolate_arg(symbol nm
)
4211 const char *s
= nm
.contents();
4212 if (!s
|| *s
== '\0')
4213 copy_mode_error("missing argument name");
4214 else if (s
[1] == 0 && csdigit(s
[0]))
4215 input_stack::push(input_stack::get_arg(s
[0] - '0'));
4216 else if (s
[0] == '*' && s
[1] == '\0') {
4217 int limit
= input_stack::nargs();
4219 for (int i
= 1; i
<= limit
; i
++) {
4220 input_iterator
*p
= input_stack::get_arg(i
);
4222 while ((c
= p
->get(0)) != EOF
)
4229 input_stack::push(make_temp_iterator(args
.contents()));
4232 else if (s
[0] == '@' && s
[1] == '\0') {
4233 int limit
= input_stack::nargs();
4235 for (int i
= 1; i
<= limit
; i
++) {
4237 args
+= BEGIN_QUOTE
;
4238 input_iterator
*p
= input_stack::get_arg(i
);
4240 while ((c
= p
->get(0)) != EOF
)
4249 input_stack::push(make_temp_iterator(args
.contents()));
4254 for (p
= s
; *p
&& csdigit(*p
); p
++)
4257 copy_mode_error("bad argument name `%1'", s
);
4259 input_stack::push(input_stack::get_arg(atoi(s
)));
4263 void handle_first_page_transition()
4266 topdiv
->begin_page();
4269 // We push back a token by wrapping it up in a token_node, and
4270 // wrapping that up in a string_iterator.
4272 static void push_token(const token
&t
)
4275 m
.append(new token_node(t
));
4276 input_stack::push(new string_iterator(m
));
4279 void push_page_ejector()
4281 static char buf
[2] = { PAGE_EJECTOR
, '\0' };
4282 input_stack::push(make_temp_iterator(buf
));
4285 void handle_initial_request(unsigned char code
)
4291 mac
.append(new token_node(tok
));
4292 input_stack::push(new string_iterator(mac
));
4293 input_stack::push(make_temp_iterator(buf
));
4294 topdiv
->begin_page();
4298 void handle_initial_title()
4300 handle_initial_request(TITLE_REQUEST
);
4303 // this should be local to define_macro, but cfront 1.2 doesn't support that
4304 static symbol
dot_symbol(".");
4306 void do_define_macro(define_mode mode
, calling_mode calling
, comp_mode comp
)
4309 if (calling
== CALLING_INDIRECT
) {
4310 symbol temp1
= get_name(1);
4311 if (temp1
.is_null()) {
4315 symbol temp2
= get_name();
4316 input_stack::push(make_temp_iterator("\n"));
4317 if (!temp2
.is_null()) {
4318 interpolate_string(temp2
);
4319 input_stack::push(make_temp_iterator(" "));
4321 interpolate_string(temp1
);
4322 input_stack::push(make_temp_iterator(" "));
4325 if (mode
== DEFINE_NORMAL
|| mode
== DEFINE_APPEND
) {
4332 term
= get_name(); // the request that terminates the definition
4335 while (!tok
.newline() && !tok
.eof())
4337 const char *start_filename
;
4339 int have_start_location
= input_stack::get_location(0, &start_filename
,
4342 // doing this here makes the line numbers come out right
4343 int c
= get_copy(&n
, 1);
4346 if (mode
== DEFINE_NORMAL
|| mode
== DEFINE_APPEND
) {
4347 request_or_macro
*rm
=
4348 (request_or_macro
*)request_dictionary
.lookup(nm
);
4350 mm
= rm
->to_macro();
4351 if (mm
&& mode
== DEFINE_APPEND
)
4355 if (comp
== COMP_DISABLE
)
4356 mac
.append(PUSH_GROFF_MODE
);
4357 else if (comp
== COMP_ENABLE
)
4358 mac
.append(PUSH_COMP_MODE
);
4360 while (c
== ESCAPE_NEWLINE
) {
4361 if (mode
== DEFINE_NORMAL
|| mode
== DEFINE_APPEND
)
4363 c
= get_copy(&n
, 1);
4365 if (bol
&& c
== '.') {
4366 const char *s
= term
.contents();
4368 // see if it matches term
4371 while ((d
= get_copy(&n
)) == ' ' || d
== '\t')
4373 if ((unsigned char)s
[0] == d
) {
4374 for (i
= 1; s
[i
] != 0; i
++) {
4376 if ((unsigned char)s
[i
] != d
)
4382 && ((i
== 2 && compatible_flag
)
4383 || (d
= get_copy(&n
)) == ' '
4384 || d
== '\n')) { // we found it
4389 if (mode
== DEFINE_APPEND
|| mode
== DEFINE_NORMAL
) {
4392 request_dictionary
.define(nm
, mm
);
4394 if (comp
== COMP_DISABLE
|| comp
== COMP_ENABLE
)
4395 mac
.append(POP_GROFFCOMP_MODE
);
4398 if (term
!= dot_symbol
) {
4400 interpolate_macro(term
);
4406 if (mode
== DEFINE_APPEND
|| mode
== DEFINE_NORMAL
) {
4408 for (int j
= 0; j
< i
; j
++)
4414 if (mode
== DEFINE_NORMAL
|| mode
== DEFINE_APPEND
) {
4415 if (have_start_location
)
4416 error_with_file_and_line(start_filename
, start_lineno
,
4417 "end of file while defining macro `%1'",
4420 error("end of file while defining macro `%1'", nm
.contents());
4423 if (have_start_location
)
4424 error_with_file_and_line(start_filename
, start_lineno
,
4425 "end of file while ignoring input lines");
4427 error("end of file while ignoring input lines");
4432 if (mode
== DEFINE_NORMAL
|| mode
== DEFINE_APPEND
) {
4439 c
= get_copy(&n
, 1);
4445 do_define_macro(DEFINE_NORMAL
, CALLING_NORMAL
,
4446 compatible_flag
? COMP_ENABLE
: COMP_IGNORE
);
4449 void define_nocomp_macro()
4451 do_define_macro(DEFINE_NORMAL
, CALLING_NORMAL
, COMP_DISABLE
);
4454 void define_indirect_macro()
4456 do_define_macro(DEFINE_NORMAL
, CALLING_INDIRECT
,
4457 compatible_flag
? COMP_ENABLE
: COMP_IGNORE
);
4460 void define_indirect_nocomp_macro()
4462 do_define_macro(DEFINE_NORMAL
, CALLING_INDIRECT
, COMP_DISABLE
);
4467 do_define_macro(DEFINE_APPEND
, CALLING_NORMAL
,
4468 compatible_flag
? COMP_ENABLE
: COMP_IGNORE
);
4471 void append_nocomp_macro()
4473 do_define_macro(DEFINE_APPEND
, CALLING_NORMAL
, COMP_DISABLE
);
4476 void append_indirect_macro()
4478 do_define_macro(DEFINE_APPEND
, CALLING_INDIRECT
,
4479 compatible_flag
? COMP_ENABLE
: COMP_IGNORE
);
4482 void append_indirect_nocomp_macro()
4484 do_define_macro(DEFINE_APPEND
, CALLING_INDIRECT
, COMP_DISABLE
);
4490 do_define_macro(DEFINE_IGNORE
, CALLING_NORMAL
, COMP_IGNORE
);
4497 symbol s
= get_name();
4500 request_dictionary
.remove(s
);
4507 symbol s1
= get_name(1);
4508 if (!s1
.is_null()) {
4509 symbol s2
= get_name(1);
4511 request_dictionary
.rename(s1
, s2
);
4518 symbol s1
= get_name(1);
4519 if (!s1
.is_null()) {
4520 symbol s2
= get_name(1);
4521 if (!s2
.is_null()) {
4522 if (!request_dictionary
.alias(s1
, s2
))
4523 warning(WARN_MAC
, "macro `%1' not defined", s2
.contents());
4531 symbol s
= get_name(1);
4533 request_or_macro
*p
= lookup_request(s
);
4534 macro
*m
= p
->to_macro();
4536 error("cannot chop request");
4537 else if (m
->empty())
4538 error("cannot chop empty macro");
4540 int have_restore
= 0;
4541 // we have to check for additional save/restore pairs which could be
4542 // there due to empty am1 requests.
4544 if (m
->get(m
->len
- 1) != POP_GROFFCOMP_MODE
)
4548 if (m
->get(m
->len
- 1) != PUSH_GROFF_MODE
4549 && m
->get(m
->len
- 1) != PUSH_COMP_MODE
)
4557 error("cannot chop empty macro");
4560 m
->set(POP_GROFFCOMP_MODE
, m
->len
- 1);
4569 void substring_request()
4571 int start
; // 0, 1, ..., n-1 or -1, -2, ...
4572 symbol s
= get_name(1);
4573 if (!s
.is_null() && get_integer(&start
)) {
4574 request_or_macro
*p
= lookup_request(s
);
4575 macro
*m
= p
->to_macro();
4577 error("cannot apply `substring' on a request");
4580 if (!has_arg() || get_integer(&end
)) {
4581 int real_length
= 0; // 1, 2, ..., n
4582 string_iterator
iter1(*m
);
4583 for (int l
= 0; l
< m
->len
; l
++) {
4584 int c
= iter1
.get(0);
4585 if (c
== PUSH_GROFF_MODE
4586 || c
== PUSH_COMP_MODE
4587 || c
== POP_GROFFCOMP_MODE
)
4594 start
+= real_length
;
4602 if (start
>= real_length
|| end
< 0) {
4604 "start and end index of substring out of range");
4607 if (--(m
->p
->count
) <= 0)
4616 "start index of substring out of range, set to 0");
4619 if (end
>= real_length
) {
4621 "end index of substring out of range, set to string length");
4622 end
= real_length
- 1;
4624 // now extract the substring
4625 string_iterator
iter(*m
);
4627 for (i
= 0; i
< start
; i
++) {
4628 int c
= iter
.get(0);
4629 while (c
== PUSH_GROFF_MODE
4630 || c
== PUSH_COMP_MODE
4631 || c
== POP_GROFFCOMP_MODE
)
4637 for (; i
<= end
; i
++) {
4638 node
*nd
= 0; // pacify compiler
4639 int c
= iter
.get(&nd
);
4640 while (c
== PUSH_GROFF_MODE
4641 || c
== PUSH_COMP_MODE
4642 || c
== POP_GROFFCOMP_MODE
)
4649 mac
.append((unsigned char)c
);
4658 void length_request()
4662 if (ret
.is_null()) {
4672 else if (!tok
.space()) {
4673 error("bad string definition");
4684 while (c
!= '\n' && c
!= EOF
) {
4688 reg
*r
= (reg
*)number_reg_dictionary
.lookup(ret
);
4692 set_number_reg(ret
, len
);
4696 void asciify_macro()
4698 symbol s
= get_name(1);
4700 request_or_macro
*p
= lookup_request(s
);
4701 macro
*m
= p
->to_macro();
4703 error("cannot asciify request");
4706 string_iterator
iter(*m
);
4708 node
*nd
= 0; // pacify compiler
4709 int c
= iter
.get(&nd
);
4723 void unformat_macro()
4725 symbol s
= get_name(1);
4727 request_or_macro
*p
= lookup_request(s
);
4728 macro
*m
= p
->to_macro();
4730 error("cannot unformat request");
4733 string_iterator
iter(*m
);
4735 node
*nd
= 0; // pacify compiler
4736 int c
= iter
.get(&nd
);
4742 if (nd
->set_unformat_flag())
4752 static void interpolate_environment_variable(symbol nm
)
4754 const char *s
= getenv(nm
.contents());
4756 input_stack::push(make_temp_iterator(s
));
4759 void interpolate_number_reg(symbol nm
, int inc
)
4761 reg
*r
= lookup_number_reg(nm
);
4766 input_stack::push(make_temp_iterator(r
->get_string()));
4769 static void interpolate_number_format(symbol nm
)
4771 reg
*r
= (reg
*)number_reg_dictionary
.lookup(nm
);
4773 input_stack::push(make_temp_iterator(r
->get_format()));
4776 static int get_delim_number(units
*n
, unsigned char si
, int prev_value
)
4780 if (start
.delimiter(1)) {
4782 if (get_number(n
, si
, prev_value
)) {
4784 warning(WARN_DELIM
, "closing delimiter does not match");
4791 static int get_delim_number(units
*n
, unsigned char si
)
4795 if (start
.delimiter(1)) {
4797 if (get_number(n
, si
)) {
4799 warning(WARN_DELIM
, "closing delimiter does not match");
4806 static int get_line_arg(units
*n
, unsigned char si
, charinfo
**cp
)
4810 int start_level
= input_stack::get_level();
4811 if (!start
.delimiter(1))
4814 if (get_number(n
, si
)) {
4815 if (tok
.dummy() || tok
.transparent_dummy())
4817 if (!(start
== tok
&& input_stack::get_level() == start_level
)) {
4818 *cp
= tok
.get_char(1);
4821 if (!(start
== tok
&& input_stack::get_level() == start_level
))
4822 warning(WARN_DELIM
, "closing delimiter does not match");
4828 static int read_size(int *x
)
4838 else if (c
== '+') {
4843 int val
= 0; // pacify compiler
4849 // allow an increment either before or after the left parenthesis
4855 else if (c
== '+') {
4870 val
= val
*10 + (c
- '0');
4875 else if (csdigit(c
)) {
4877 if (!inc
&& c
!= '0' && c
< '4') {
4883 val
= val
*10 + (c
- '0');
4887 else if (!tok
.delimiter(1))
4893 ? get_number(&val
, 'z')
4894 : get_number(&val
, 'z', curenv
->get_requested_point_size())))
4896 if (!(start
.ch() == '[' && tok
.ch() == ']') && start
!= tok
) {
4897 if (start
.ch() == '[')
4898 error("missing `]'");
4900 error("missing closing delimiter");
4908 // special case -- \s[0] and \s0 means to revert to previous size
4915 *x
= curenv
->get_requested_point_size() + val
;
4918 *x
= curenv
->get_requested_point_size() - val
;
4925 "\\s request results in non-positive point size; set to 1");
4931 error("bad digit in point size");
4936 static symbol
get_delim_name()
4941 error("end of input at start of delimited name");
4944 if (start
.newline()) {
4945 error("can't delimit name with a newline");
4948 int start_level
= input_stack::get_level();
4949 char abuf
[ABUF_SIZE
];
4951 int buf_size
= ABUF_SIZE
;
4954 if (i
+ 1 > buf_size
) {
4956 buf
= new char[ABUF_SIZE
*2];
4957 memcpy(buf
, abuf
, buf_size
);
4958 buf_size
= ABUF_SIZE
*2;
4961 char *old_buf
= buf
;
4962 buf
= new char[buf_size
*2];
4963 memcpy(buf
, old_buf
, buf_size
);
4970 && (compatible_flag
|| input_stack::get_level() == start_level
))
4972 if ((buf
[i
] = tok
.ch()) == 0) {
4973 error("missing delimiter (got %1)", tok
.description());
4983 error("empty delimited name");
4998 static void do_register()
5002 if (!start
.delimiter(1))
5005 symbol nm
= get_long_name(1);
5010 reg
*r
= (reg
*)number_reg_dictionary
.lookup(nm
);
5012 if (!r
|| !r
->get_value(&prev_value
))
5015 if (!get_number(&val
, 'u', prev_value
))
5018 warning(WARN_DELIM
, "closing delimiter does not match");
5022 set_number_reg(nm
, val
);
5025 // this implements the \w escape sequence
5027 static void do_width()
5031 int start_level
= input_stack::get_level();
5032 environment
env(curenv
);
5033 environment
*oldenv
= curenv
;
5038 warning(WARN_DELIM
, "missing closing delimiter");
5041 if (tok
.newline()) {
5042 warning(WARN_DELIM
, "missing closing delimiter");
5043 input_stack::push(make_temp_iterator("\n"));
5047 && (compatible_flag
|| input_stack::get_level() == start_level
))
5052 units x
= env
.get_input_line_position().to_units();
5053 input_stack::push(make_temp_iterator(i_to_a(x
)));
5054 env
.width_registers();
5059 charinfo
*page_character
;
5061 void set_page_character()
5063 page_character
= get_optional_char();
5067 static const symbol
percent_symbol("%");
5069 void read_title_parts(node
**part
, hunits
*part_width
)
5072 if (tok
.newline() || tok
.eof())
5075 int start_level
= input_stack::get_level();
5077 for (int i
= 0; i
< 3; i
++) {
5078 while (!tok
.newline() && !tok
.eof()) {
5080 && (compatible_flag
|| input_stack::get_level() == start_level
)) {
5084 if (page_character
!= 0 && tok
.get_char() == page_character
)
5085 interpolate_number_reg(percent_symbol
, 0);
5090 curenv
->wrap_up_tab();
5091 part_width
[i
] = curenv
->get_input_line_position();
5092 part
[i
] = curenv
->extract_output_line();
5094 while (!tok
.newline() && !tok
.eof())
5098 class non_interpreted_node
: public node
{
5101 non_interpreted_node(const macro
&);
5102 int interpret(macro
*);
5104 int ends_sentence();
5111 non_interpreted_node::non_interpreted_node(const macro
&m
) : mac(m
)
5115 int non_interpreted_node::ends_sentence()
5120 int non_interpreted_node::same(node
*nd
)
5122 return mac
== ((non_interpreted_node
*)nd
)->mac
;
5125 const char *non_interpreted_node::type()
5127 return "non_interpreted_node";
5130 int non_interpreted_node::force_tprint()
5135 int non_interpreted_node::is_tag()
5140 node
*non_interpreted_node::copy()
5142 return new non_interpreted_node(mac
);
5145 int non_interpreted_node::interpret(macro
*m
)
5147 string_iterator
si(mac
);
5148 node
*n
= 0; // pacify compiler
5161 static node
*do_non_interpreted()
5166 while ((c
= get_copy(&n
)) != ESCAPE_QUESTION
&& c
!= EOF
&& c
!= '\n')
5171 if (c
== EOF
|| c
== '\n') {
5172 error("missing \\?");
5175 return new non_interpreted_node(mac
);
5178 static void encode_char(macro
*mac
, char c
)
5181 if ((font::use_charnames_in_special
) && tok
.special()) {
5182 charinfo
*ci
= tok
.get_char(1);
5183 const char *s
= ci
->get_symbol()->contents();
5184 if (s
[0] != (char)0) {
5188 while (s
[i
] != (char)0) {
5196 else if (tok
.stretchable_space()
5197 || tok
.unstretchable_space())
5199 else if (!(tok
.hyphen_indicator()
5201 || tok
.transparent_dummy()
5202 || tok
.zero_width_break()))
5203 error("%1 is invalid within \\X", tok
.description());
5206 if ((font::use_charnames_in_special
) && (c
== '\\')) {
5208 * add escape escape sequence
5220 int start_level
= input_stack::get_level();
5223 tok
!= start
|| input_stack::get_level() != start_level
;
5226 warning(WARN_DELIM
, "missing closing delimiter");
5229 if (tok
.newline()) {
5230 input_stack::push(make_temp_iterator("\n"));
5231 warning(WARN_DELIM
, "missing closing delimiter");
5239 else if (tok
.leader())
5241 else if (tok
.backspace())
5245 encode_char(&mac
, c
);
5247 return new special_node(mac
);
5250 void output_request()
5252 if (!tok
.newline() && !tok
.eof()) {
5260 if (c
!= ' ' && c
!= '\t')
5263 for (; c
!= '\n' && c
!= EOF
; c
= get_copy(0))
5264 topdiv
->transparent_output(c
);
5265 topdiv
->transparent_output('\n');
5270 extern int image_no
; // from node.cpp
5272 static node
*do_suppress(symbol nm
)
5274 if (nm
.is_null() || nm
.is_empty()) {
5275 error("expecting an argument to escape \\O");
5278 const char *s
= nm
.contents();
5281 if (begin_level
== 0)
5282 // suppress generation of glyphs
5283 return new suppress_node(0, 0);
5286 if (begin_level
== 0)
5287 // enable generation of glyphs
5288 return new suppress_node(1, 0);
5291 if (begin_level
== 0)
5292 return new suppress_node(1, 1);
5302 s
++; // move over '5'
5304 if (*s
== (char)0) {
5305 error("missing position and filename in \\O");
5308 if (!(position
== 'l'
5311 || position
== 'i')) {
5312 error("l, r, c, or i position expected (got %1 in \\O)", position
);
5315 s
++; // onto image name
5316 if (s
== (char *)0) {
5317 error("missing image name for \\O");
5321 if (begin_level
== 0)
5322 return new suppress_node(symbol(s
), position
, image_no
);
5326 error("`%1' is an invalid argument to \\O", *s
);
5331 void special_node::tprint(troff_output_file
*out
)
5334 string_iterator
iter(mac
);
5336 int c
= iter
.get(0);
5339 for (const char *s
= ::asciify(c
); *s
; s
++)
5340 tprint_char(out
, *s
);
5345 int get_file_line(const char **filename
, int *lineno
)
5347 return input_stack::get_location(0, filename
, lineno
);
5353 if (get_integer(&n
)) {
5354 const char *filename
= 0;
5356 symbol s
= get_long_name();
5357 filename
= s
.contents();
5359 (void)input_stack::set_location(filename
, n
-1);
5364 static int nroff_mode
= 0;
5366 static void nroff_request()
5372 static void troff_request()
5378 static void skip_alternative()
5381 // ensure that ``.if 0\{'' works as expected
5382 if (tok
.left_brace())
5386 c
= input_stack::get(0);
5389 if (c
== ESCAPE_LEFT_BRACE
)
5391 else if (c
== ESCAPE_RIGHT_BRACE
)
5393 else if (c
== escape_char
&& escape_char
> 0)
5394 switch(input_stack::get(0)) {
5402 while ((c
= input_stack::get(0)) != '\n' && c
!= EOF
)
5406 Note that the level can properly be < 0, eg
5412 So don't give an error message in this case.
5414 if (level
<= 0 && c
== '\n')
5420 static void begin_alternative()
5422 while (tok
.space() || tok
.left_brace())
5432 static int_stack if_else_stack
;
5439 while (tok
.ch() == '!') {
5444 unsigned char c
= tok
.ch();
5447 result
= !nroff_mode
;
5449 else if (c
== 'n') {
5451 result
= nroff_mode
;
5453 else if (c
== 'v') {
5457 else if (c
== 'o') {
5458 result
= (topdiv
->get_page_number() & 1);
5461 else if (c
== 'e') {
5462 result
= !(topdiv
->get_page_number() & 1);
5465 else if (c
== 'd' || c
== 'r') {
5467 symbol nm
= get_name(1);
5473 ? request_dictionary
.lookup(nm
) != 0
5474 : number_reg_dictionary
.lookup(nm
) != 0);
5476 else if (c
== 'm') {
5478 symbol nm
= get_long_name(1);
5483 result
= (nm
== default_symbol
5484 || color_dictionary
.lookup(nm
) != 0);
5486 else if (c
== 'c') {
5489 charinfo
*ci
= tok
.get_char(1);
5494 result
= character_exists(ci
, curenv
);
5497 else if (c
== 'F') {
5499 symbol nm
= get_long_name(1);
5504 result
= check_font(curenv
->get_family()->nm
, nm
);
5506 else if (c
== 'S') {
5508 symbol nm
= get_long_name(1);
5513 result
= check_style(nm
);
5515 else if (tok
.space())
5517 else if (tok
.delimiter()) {
5519 int delim_level
= input_stack::get_level();
5520 environment
env1(curenv
);
5521 environment
env2(curenv
);
5522 environment
*oldenv
= curenv
;
5525 for (int i
= 0; i
< 2; i
++) {
5528 if (tok
.newline() || tok
.eof()) {
5529 warning(WARN_DELIM
, "missing closing delimiter");
5535 && (compatible_flag
|| input_stack::get_level() == delim_level
))
5541 node
*n1
= env1
.extract_output_line();
5542 node
*n2
= env2
.extract_output_line();
5543 result
= same_node_list(n1
, n2
);
5544 delete_node_list(n1
);
5545 delete_node_list(n2
);
5553 if (!get_number(&n
, 'u')) {
5563 begin_alternative();
5569 void if_else_request()
5571 if_else_stack
.push(do_if_request());
5581 if (if_else_stack
.is_empty()) {
5582 warning(WARN_EL
, "unbalanced .el request");
5586 if (if_else_stack
.pop())
5589 begin_alternative();
5593 static int while_depth
= 0;
5594 static int while_break_flag
= 0;
5596 void while_request()
5601 mac
.append(new token_node(tok
));
5603 node
*n
= 0; // pacify compiler
5604 int c
= input_stack::get(&n
);
5620 if (c
== ESCAPE_LEFT_BRACE
)
5622 else if (c
== ESCAPE_RIGHT_BRACE
)
5624 else if (c
== escape_char
)
5627 if (c
== '\n' && level
<= 0)
5632 error("unbalanced \\{ \\}");
5635 input_stack::add_boundary();
5637 input_stack::push(new string_iterator(mac
, "while loop"));
5639 if (!do_if_request()) {
5640 while (input_stack::get(0) != EOF
)
5644 process_input_stack();
5645 if (while_break_flag
|| input_stack::is_return_boundary()) {
5646 while_break_flag
= 0;
5650 input_stack::remove_boundary();
5656 void while_break_request()
5659 error("no while loop");
5663 while_break_flag
= 1;
5664 while (input_stack::get(0) != EOF
)
5670 void while_continue_request()
5673 error("no while loop");
5677 while (input_stack::get(0) != EOF
)
5687 symbol nm
= get_long_name(1);
5691 while (!tok
.newline() && !tok
.eof())
5694 FILE *fp
= include_search_path
.open_file_cautious(nm
.contents());
5696 input_stack::push(new file_iterator(fp
, nm
.contents()));
5698 error("can't open `%1': %2", nm
.contents(), strerror(errno
));
5703 // like .so but use popen()
5708 error(".pso request not allowed in safer mode");
5712 #ifdef POPEN_MISSING
5713 error("pipes not available on this system");
5715 #else /* not POPEN_MISSING */
5716 if (tok
.newline() || tok
.eof())
5717 error("missing command");
5720 while ((c
= get_copy(0)) == ' ' || c
== '\t')
5723 char *buf
= new char[buf_size
];
5725 for (; c
!= '\n' && c
!= EOF
; c
= get_copy(0)) {
5726 const char *s
= asciify(c
);
5727 int slen
= strlen(s
);
5728 if (buf_used
+ slen
+ 1> buf_size
) {
5729 char *old_buf
= buf
;
5730 int old_buf_size
= buf_size
;
5732 buf
= new char[buf_size
];
5733 memcpy(buf
, old_buf
, old_buf_size
);
5736 strcpy(buf
+ buf_used
, s
);
5739 buf
[buf_used
] = '\0';
5741 FILE *fp
= popen(buf
, POPEN_RT
);
5743 input_stack::push(new file_iterator(fp
, symbol(buf
).contents(), 1));
5745 error("can't open pipe to process `%1': %2", buf
, strerror(errno
));
5749 #endif /* not POPEN_MISSING */
5755 static int llx_reg_contents
= 0;
5756 static int lly_reg_contents
= 0;
5757 static int urx_reg_contents
= 0;
5758 static int ury_reg_contents
= 0;
5760 struct bounding_box
{
5761 int llx
, lly
, urx
, ury
;
5764 /* Parse the argument to a %%BoundingBox comment. Return 1 if it
5765 contains 4 numbers, 2 if it contains (atend), 0 otherwise. */
5767 int parse_bounding_box(char *p
, bounding_box
*bb
)
5769 if (sscanf(p
, "%d %d %d %d",
5770 &bb
->llx
, &bb
->lly
, &bb
->urx
, &bb
->ury
) == 4)
5773 /* The Document Structuring Conventions say that the numbers
5774 should be integers. Unfortunately some broken applications
5776 double x1
, x2
, x3
, x4
;
5777 if (sscanf(p
, "%lf %lf %lf %lf", &x1
, &x2
, &x3
, &x4
) == 4) {
5785 for (; *p
== ' ' || *p
== '\t'; p
++)
5787 if (strncmp(p
, "(atend)", 7) == 0) {
5792 bb
->llx
= bb
->lly
= bb
->urx
= bb
->ury
= 0;
5796 // This version is taken from psrm.cpp
5798 #define PS_LINE_MAX 255
5799 cset
white_space("\n\r \t");
5801 int ps_get_line(char *buf
, FILE *fp
, const char* filename
)
5810 while (c
!= '\r' && c
!= '\n' && c
!= EOF
) {
5811 if ((c
< 0x1b && !white_space(c
)) || c
== 0x7f)
5812 error("invalid input character code %1 in `%2'", int(c
), filename
);
5813 else if (i
< PS_LINE_MAX
)
5817 error("PostScript file `%1' is non-conforming "
5818 "because length of line exceeds 255", filename
);
5826 if (c
!= EOF
&& c
!= '\n')
5832 inline void assign_registers(int llx
, int lly
, int urx
, int ury
)
5834 llx_reg_contents
= llx
;
5835 lly_reg_contents
= lly
;
5836 urx_reg_contents
= urx
;
5837 ury_reg_contents
= ury
;
5840 void do_ps_file(FILE *fp
, const char* filename
)
5844 char buf
[PS_LINE_MAX
];
5845 llx_reg_contents
= lly_reg_contents
=
5846 urx_reg_contents
= ury_reg_contents
= 0;
5847 if (!ps_get_line(buf
, fp
, filename
)) {
5848 error("`%1' is empty", filename
);
5851 if (strncmp("%!PS-Adobe-", buf
, 11) != 0) {
5852 error("`%1' is not conforming to the Document Structuring Conventions",
5856 while (ps_get_line(buf
, fp
, filename
) != 0) {
5857 if (buf
[0] != '%' || buf
[1] != '%'
5858 || strncmp(buf
+ 2, "EndComments", 11) == 0)
5860 if (strncmp(buf
+ 2, "BoundingBox:", 12) == 0) {
5861 int res
= parse_bounding_box(buf
+ 14, &bb
);
5863 assign_registers(bb
.llx
, bb
.lly
, bb
.urx
, bb
.ury
);
5866 else if (res
== 2) {
5871 error("the arguments to the %%%%BoundingBox comment in `%1' are bad",
5880 /* in the trailer, the last BoundingBox comment is significant */
5881 for (offset
= 512; !last_try
; offset
*= 2) {
5882 int had_trailer
= 0;
5884 if (offset
> 32768 || fseek(fp
, -offset
, 2) == -1) {
5886 if (fseek(fp
, 0L, 0) == -1)
5889 while (ps_get_line(buf
, fp
, filename
) != 0) {
5890 if (buf
[0] == '%' && buf
[1] == '%') {
5892 if (strncmp(buf
+ 2, "Trailer", 7) == 0)
5896 if (strncmp(buf
+ 2, "BoundingBox:", 12) == 0) {
5897 int res
= parse_bounding_box(buf
+ 14, &bb
);
5900 else if (res
== 2) {
5901 error("`(atend)' not allowed in trailer of `%1'", filename
);
5905 error("the arguments to the %%%%BoundingBox comment in `%1' are bad",
5914 assign_registers(bb
.llx
, bb
.lly
, bb
.urx
, bb
.ury
);
5919 error("%%%%BoundingBox comment not found in `%1'", filename
);
5922 void ps_bbox_request()
5924 symbol nm
= get_long_name(1);
5928 while (!tok
.newline() && !tok
.eof())
5931 // PS files might contain non-printable characters, such as ^Z
5932 // and CRs not followed by an LF, so open them in binary mode.
5933 FILE *fp
= include_search_path
.open_file_cautious(nm
.contents(),
5936 do_ps_file(fp
, nm
.contents());
5940 error("can't open `%1': %2", nm
.contents(), strerror(errno
));
5945 const char *asciify(int c
)
5948 buf
[0] = escape_char
== '\0' ? '\\' : escape_char
;
5949 buf
[1] = buf
[2] = '\0';
5951 case ESCAPE_QUESTION
:
5954 case ESCAPE_AMPERSAND
:
5957 case ESCAPE_RIGHT_PARENTHESIS
:
5960 case ESCAPE_UNDERSCORE
:
5966 case ESCAPE_CIRCUMFLEX
:
5969 case ESCAPE_LEFT_BRACE
:
5972 case ESCAPE_RIGHT_BRACE
:
5975 case ESCAPE_LEFT_QUOTE
:
5978 case ESCAPE_RIGHT_QUOTE
:
5996 case ESCAPE_PERCENT
:
6008 case PUSH_GROFF_MODE
:
6009 case PUSH_COMP_MODE
:
6010 case POP_GROFFCOMP_MODE
:
6014 if (invalid_input_char(c
))
6023 const char *input_char_description(int c
)
6027 return "a newline character";
6029 return "a backspace character";
6031 return "a leader character";
6033 return "a tab character";
6035 return "a space character";
6039 static char buf
[sizeof("magic character code ") + 1 + INT_DIGITS
];
6040 if (invalid_input_char(c
)) {
6041 const char *s
= asciify(c
);
6048 sprintf(buf
, "magic character code %d", c
);
6057 sprintf(buf
, "character code %d", c
);
6063 if (!tok
.newline() && !tok
.eof()) {
6072 if (c
!= ' ' && c
!= '\t')
6076 for (; c
!= '\n' && c
!= EOF
; c
= get_copy(0))
6079 curenv
->add_node(new tag_node(s
, 0));
6086 if (!tok
.newline() && !tok
.eof()) {
6095 if (c
!= ' ' && c
!= '\t')
6099 for (; c
!= '\n' && c
!= EOF
; c
= get_copy(0))
6102 curenv
->add_node(new tag_node(s
, 1));
6107 // .tm, .tm1, and .tmc
6109 void do_terminal(int newline
, int string_like
)
6111 if (!tok
.newline() && !tok
.eof()) {
6115 if (string_like
&& c
== '"') {
6119 if (c
!= ' ' && c
!= '\t')
6122 for (; c
!= '\n' && c
!= EOF
; c
= get_copy(0))
6123 fputs(asciify(c
), stderr
);
6126 fputc('\n', stderr
);
6141 void terminal_continue()
6146 dictionary
stream_dictionary(20);
6148 void do_open(int append
)
6150 symbol stream
= get_name(1);
6151 if (!stream
.is_null()) {
6152 symbol filename
= get_long_name(1);
6153 if (!filename
.is_null()) {
6155 FILE *fp
= fopen(filename
.contents(), append
? "a" : "w");
6157 error("can't open `%1' for %2: %3",
6158 filename
.contents(),
6159 append
? "appending" : "writing",
6161 fp
= (FILE *)stream_dictionary
.remove(stream
);
6164 fp
= (FILE *)stream_dictionary
.lookup(stream
, fp
);
6175 error(".open request not allowed in safer mode");
6182 void opena_request()
6185 error(".opena request not allowed in safer mode");
6192 void close_request()
6194 symbol stream
= get_name(1);
6195 if (!stream
.is_null()) {
6196 FILE *fp
= (FILE *)stream_dictionary
.remove(stream
);
6198 error("no stream named `%1'", stream
.contents());
6205 // .write and .writec
6207 void do_write_request(int newline
)
6209 symbol stream
= get_name(1);
6210 if (stream
.is_null()) {
6214 FILE *fp
= (FILE *)stream_dictionary
.lookup(stream
);
6216 error("no stream named `%1'", stream
.contents());
6221 while ((c
= get_copy(0)) == ' ')
6225 for (; c
!= '\n' && c
!= EOF
; c
= get_copy(0))
6226 fputs(asciify(c
), fp
);
6233 void write_request()
6235 do_write_request(1);
6238 void write_request_continue()
6240 do_write_request(0);
6243 void write_macro_request()
6245 symbol stream
= get_name(1);
6246 if (stream
.is_null()) {
6250 FILE *fp
= (FILE *)stream_dictionary
.lookup(stream
);
6252 error("no stream named `%1'", stream
.contents());
6256 symbol s
= get_name(1);
6261 request_or_macro
*p
= lookup_request(s
);
6262 macro
*m
= p
->to_macro();
6264 error("cannot write request");
6266 string_iterator
iter(*m
);
6268 int c
= iter
.get(0);
6271 fputs(asciify(c
), fp
);
6278 void warnscale_request()
6285 warn_scale
= (double)units_per_inch
;
6287 warn_scale
= (double)units_per_inch
/ 2.54;
6289 warn_scale
= (double)units_per_inch
/ 72.0;
6291 warn_scale
= (double)units_per_inch
/ 6.0;
6294 "invalid scaling indicator `%1', using `i' instead", c
);
6297 warn_scaling_indicator
= c
;
6302 void spreadwarn_request()
6305 if (has_arg() && get_hunits(&n
, 'm')) {
6308 hunits em
= curenv
->get_size();
6309 spread_limit
= (double)n
.to_units()
6310 / (em
.is_zero() ? hresolution
: em
.to_units());
6313 spread_limit
= -spread_limit
- 1; // no arg toggles on/off without
6314 // changing value; we mirror at
6315 // -0.5 to make zero a valid value
6319 static void init_charset_table()
6322 strcpy(buf
, "char");
6323 for (int i
= 0; i
< 256; i
++) {
6324 strcpy(buf
+ 4, i_to_a(i
));
6325 charset_table
[i
] = get_charinfo(symbol(buf
));
6326 charset_table
[i
]->set_ascii_code(i
);
6328 charset_table
[i
]->set_hyphenation_code(cmlower(i
));
6330 charset_table
['.']->set_flags(charinfo::ENDS_SENTENCE
);
6331 charset_table
['?']->set_flags(charinfo::ENDS_SENTENCE
);
6332 charset_table
['!']->set_flags(charinfo::ENDS_SENTENCE
);
6333 charset_table
['-']->set_flags(charinfo::BREAK_AFTER
);
6334 charset_table
['"']->set_flags(charinfo::TRANSPARENT
);
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 get_charinfo(symbol("dg"))->set_flags(charinfo::TRANSPARENT
);
6340 get_charinfo(symbol("rq"))->set_flags(charinfo::TRANSPARENT
);
6341 get_charinfo(symbol("em"))->set_flags(charinfo::BREAK_AFTER
);
6342 get_charinfo(symbol("ul"))->set_flags(charinfo::OVERLAPS_HORIZONTALLY
);
6343 get_charinfo(symbol("rn"))->set_flags(charinfo::OVERLAPS_HORIZONTALLY
);
6344 get_charinfo(symbol("radicalex"))->set_flags(charinfo::OVERLAPS_HORIZONTALLY
);
6345 get_charinfo(symbol("sqrtex"))->set_flags(charinfo::OVERLAPS_HORIZONTALLY
);
6346 get_charinfo(symbol("ru"))->set_flags(charinfo::OVERLAPS_HORIZONTALLY
);
6347 get_charinfo(symbol("br"))->set_flags(charinfo::OVERLAPS_VERTICALLY
);
6348 page_character
= charset_table
['%'];
6351 static void init_hpf_code_table()
6353 for (int i
= 0; i
< 256; i
++)
6354 hpf_code_table
[i
] = i
;
6357 static void do_translate(int translate_transparent
, int translate_input
)
6360 while (!tok
.newline() && !tok
.eof()) {
6362 // This is a really bizarre troff feature.
6364 translate_space_to_dummy
= tok
.dummy();
6365 if (tok
.newline() || tok
.eof())
6370 charinfo
*ci1
= tok
.get_char(1);
6374 if (tok
.newline() || tok
.eof()) {
6375 ci1
->set_special_translation(charinfo::TRANSLATE_SPACE
,
6376 translate_transparent
);
6380 ci1
->set_special_translation(charinfo::TRANSLATE_SPACE
,
6381 translate_transparent
);
6382 else if (tok
.stretchable_space())
6383 ci1
->set_special_translation(charinfo::TRANSLATE_STRETCHABLE_SPACE
,
6384 translate_transparent
);
6385 else if (tok
.dummy())
6386 ci1
->set_special_translation(charinfo::TRANSLATE_DUMMY
,
6387 translate_transparent
);
6388 else if (tok
.hyphen_indicator())
6389 ci1
->set_special_translation(charinfo::TRANSLATE_HYPHEN_INDICATOR
,
6390 translate_transparent
);
6392 charinfo
*ci2
= tok
.get_char(1);
6396 ci1
->set_translation(0, translate_transparent
, translate_input
);
6398 ci1
->set_translation(ci2
, translate_transparent
, translate_input
);
6410 void translate_no_transparent()
6415 void translate_input()
6423 if (get_integer(&flags
))
6425 charinfo
*ci
= tok
.get_char(1);
6427 charinfo
*tem
= ci
->get_translation();
6430 ci
->set_flags(flags
);
6437 void hyphenation_code()
6440 while (!tok
.newline() && !tok
.eof()) {
6441 charinfo
*ci
= tok
.get_char(1);
6446 unsigned char c
= tok
.ch();
6448 error("hyphenation code must be ordinary character");
6452 error("hyphenation code cannot be digit");
6455 ci
->set_hyphenation_code(c
);
6456 if (ci
->get_translation()
6457 && ci
->get_translation()->get_translation_input())
6458 ci
->get_translation()->set_hyphenation_code(c
);
6465 void hyphenation_patterns_file_code()
6468 while (!tok
.newline() && !tok
.eof()) {
6470 if (get_integer(&n1
) && (0 <= n1
&& n1
<= 255)) {
6472 error("missing output hyphenation code");
6475 if (get_integer(&n2
) && (0 <= n2
&& n2
<= 255)) {
6476 hpf_code_table
[n1
] = n2
;
6480 error("output hyphenation code must be integer in the range 0..255");
6485 error("input hyphenation code must be integer in the range 0..255");
6492 charinfo
*token::get_char(int required
)
6494 if (type
== TOKEN_CHAR
)
6495 return charset_table
[c
];
6496 if (type
== TOKEN_SPECIAL
)
6497 return get_charinfo(nm
);
6498 if (type
== TOKEN_NUMBERED_CHAR
)
6499 return get_charinfo_by_number(val
);
6500 if (type
== TOKEN_ESCAPE
) {
6501 if (escape_char
!= 0)
6502 return charset_table
[escape_char
];
6504 error("`\\e' used while no current escape character");
6509 if (type
== TOKEN_EOF
|| type
== TOKEN_NEWLINE
)
6510 warning(WARN_MISSING
, "missing normal or special character");
6512 error("normal or special character expected (got %1)", description());
6517 charinfo
*get_optional_char()
6521 charinfo
*ci
= tok
.get_char();
6523 check_missing_character();
6529 void check_missing_character()
6531 if (!tok
.newline() && !tok
.eof() && !tok
.right_brace() && !tok
.tab())
6532 error("normal or special character expected (got %1): "
6533 "treated as missing",
6539 int token::add_to_node_list(node
**pp
)
6546 *pp
= (*pp
)->add_char(charset_table
[c
], curenv
, &w
, &s
);
6552 if (escape_char
!= 0)
6553 *pp
= (*pp
)->add_char(charset_table
[escape_char
], curenv
, &w
, &s
);
6555 case TOKEN_HYPHEN_INDICATOR
:
6556 *pp
= (*pp
)->add_discretionary_hyphen();
6558 case TOKEN_ITALIC_CORRECTION
:
6559 *pp
= (*pp
)->add_italic_correction(&w
);
6561 case TOKEN_LEFT_BRACE
:
6563 case TOKEN_MARK_INPUT
:
6564 set_number_reg(nm
, curenv
->get_input_line_position().to_units());
6570 case TOKEN_NUMBERED_CHAR
:
6571 *pp
= (*pp
)->add_char(get_charinfo_by_number(val
), curenv
, &w
, &s
);
6573 case TOKEN_RIGHT_BRACE
:
6576 n
= new hmotion_node(curenv
->get_space_width(),
6577 curenv
->get_fill_color());
6580 *pp
= (*pp
)->add_char(get_charinfo(nm
), curenv
, &w
, &s
);
6582 case TOKEN_STRETCHABLE_SPACE
:
6583 n
= new unbreakable_space_node(curenv
->get_space_width(),
6584 curenv
->get_fill_color());
6586 case TOKEN_UNSTRETCHABLE_SPACE
:
6587 n
= new space_char_hmotion_node(curenv
->get_space_width(),
6588 curenv
->get_fill_color());
6590 case TOKEN_TRANSPARENT_DUMMY
:
6591 n
= new transparent_dummy_node
;
6593 case TOKEN_ZERO_WIDTH_BREAK
:
6594 n
= new space_node(H0
, curenv
->get_fill_color());
6596 n
->is_escape_colon();
6608 void token::process()
6610 if (possibly_handle_first_page_transition())
6613 case TOKEN_BACKSPACE
:
6614 curenv
->add_node(new hmotion_node(-curenv
->get_space_width(),
6615 curenv
->get_fill_color()));
6618 curenv
->add_char(charset_table
[c
]);
6621 curenv
->add_node(new dummy_node
);
6630 if (escape_char
!= 0)
6631 curenv
->add_char(charset_table
[escape_char
]);
6633 case TOKEN_BEGIN_TRAP
:
6634 case TOKEN_END_TRAP
:
6635 case TOKEN_PAGE_EJECTOR
:
6636 // these are all handled in process_input_stack()
6638 case TOKEN_HYPHEN_INDICATOR
:
6639 curenv
->add_hyphen_indicator();
6641 case TOKEN_INTERRUPT
:
6642 curenv
->interrupt();
6644 case TOKEN_ITALIC_CORRECTION
:
6645 curenv
->add_italic_correction();
6648 curenv
->handle_tab(1);
6650 case TOKEN_LEFT_BRACE
:
6652 case TOKEN_MARK_INPUT
:
6653 set_number_reg(nm
, curenv
->get_input_line_position().to_units());
6659 curenv
->add_node(nd
);
6662 case TOKEN_NUMBERED_CHAR
:
6663 curenv
->add_char(get_charinfo_by_number(val
));
6666 // handled in process_input_stack()
6668 case TOKEN_RIGHT_BRACE
:
6674 curenv
->add_char(get_charinfo(nm
));
6679 case TOKEN_STRETCHABLE_SPACE
:
6680 curenv
->add_node(new unbreakable_space_node(curenv
->get_space_width(),
6681 curenv
->get_fill_color()));
6683 case TOKEN_UNSTRETCHABLE_SPACE
:
6684 curenv
->add_node(new space_char_hmotion_node(curenv
->get_space_width(),
6685 curenv
->get_fill_color()));
6688 curenv
->handle_tab(0);
6690 case TOKEN_TRANSPARENT
:
6692 case TOKEN_TRANSPARENT_DUMMY
:
6693 curenv
->add_node(new transparent_dummy_node
);
6695 case TOKEN_ZERO_WIDTH_BREAK
:
6697 node
*tmp
= new space_node(H0
, curenv
->get_fill_color());
6698 tmp
->freeze_space();
6699 tmp
->is_escape_colon();
6700 curenv
->add_node(tmp
);
6708 class nargs_reg
: public reg
{
6710 const char *get_string();
6713 const char *nargs_reg::get_string()
6715 return i_to_a(input_stack::nargs());
6718 class lineno_reg
: public reg
{
6720 const char *get_string();
6723 const char *lineno_reg::get_string()
6727 if (!input_stack::get_location(0, &file
, &line
))
6729 return i_to_a(line
);
6732 class writable_lineno_reg
: public general_reg
{
6734 writable_lineno_reg();
6735 void set_value(units
);
6736 int get_value(units
*);
6739 writable_lineno_reg::writable_lineno_reg()
6743 int writable_lineno_reg::get_value(units
*res
)
6747 if (!input_stack::get_location(0, &file
, &line
))
6753 void writable_lineno_reg::set_value(units n
)
6755 input_stack::set_location(0, n
);
6758 class filename_reg
: public reg
{
6760 const char *get_string();
6763 const char *filename_reg::get_string()
6767 if (input_stack::get_location(0, &file
, &line
))
6773 class constant_reg
: public reg
{
6776 constant_reg(const char *);
6777 const char *get_string();
6780 constant_reg::constant_reg(const char *p
) : s(p
)
6784 const char *constant_reg::get_string()
6789 constant_int_reg::constant_int_reg(int *q
) : p(q
)
6793 const char *constant_int_reg::get_string()
6798 void abort_request()
6803 else if (tok
.newline())
6806 while ((c
= get_copy(0)) == ' ')
6809 if (c
== EOF
|| c
== '\n')
6810 fputs("User Abort.", stderr
);
6812 for (; c
!= '\n' && c
!= EOF
; c
= get_copy(0))
6813 fputs(asciify(c
), stderr
);
6815 fputc('\n', stderr
);
6816 cleanup_and_exit(1);
6822 char *s
= new char[len
];
6824 while ((c
= get_copy(0)) == ' ')
6827 while (c
!= '\n' && c
!= EOF
) {
6828 if (!invalid_input_char(c
)) {
6831 s
= new char[len
*2];
6832 memcpy(s
, tem
, len
);
6852 error(".pi request not allowed in safer mode");
6856 #ifdef POPEN_MISSING
6857 error("pipes not available on this system");
6859 #else /* not POPEN_MISSING */
6861 error("can't pipe: output already started");
6866 if ((pc
= read_string()) == 0)
6867 error("can't pipe to empty command");
6869 char *s
= new char[strlen(pipe_command
) + strlen(pc
) + 1 + 1];
6870 strcpy(s
, pipe_command
);
6873 a_delete pipe_command
;
6880 #endif /* not POPEN_MISSING */
6884 static int system_status
;
6886 void system_request()
6889 error(".sy request not allowed in safer mode");
6893 char *command
= read_string();
6895 error("empty command");
6897 system_status
= system(command
);
6905 if (curdiv
== topdiv
&& topdiv
->before_first_page
) {
6906 handle_initial_request(COPY_FILE_REQUEST
);
6909 symbol filename
= get_long_name(1);
6910 while (!tok
.newline() && !tok
.eof())
6914 if (!filename
.is_null())
6915 curdiv
->copy_file(filename
.contents());
6923 if (curdiv
== topdiv
&& topdiv
->before_first_page
) {
6924 handle_initial_request(VJUSTIFY_REQUEST
);
6927 symbol type
= get_long_name(1);
6928 if (!type
.is_null())
6929 curdiv
->vjustify(type
);
6935 void transparent_file()
6937 if (curdiv
== topdiv
&& topdiv
->before_first_page
) {
6938 handle_initial_request(TRANSPARENT_FILE_REQUEST
);
6941 symbol filename
= get_long_name(1);
6942 while (!tok
.newline() && !tok
.eof())
6946 if (!filename
.is_null()) {
6948 FILE *fp
= include_search_path
.open_file_cautious(filename
.contents());
6950 error("can't open `%1': %2", filename
.contents(), strerror(errno
));
6957 if (invalid_input_char(c
))
6958 warning(WARN_INPUT
, "invalid input character code %1", int(c
));
6960 curdiv
->transparent_output(c
);
6965 curdiv
->transparent_output('\n');
6977 page_range(int, int, page_range
*);
6978 int contains(int n
);
6981 page_range::page_range(int i
, int j
, page_range
*p
)
6982 : first(i
), last(j
), next(p
)
6986 int page_range::contains(int n
)
6988 return n
>= first
&& (last
<= 0 || n
<= last
);
6991 page_range
*output_page_list
= 0;
6993 int in_output_page_list(int n
)
6995 if (!output_page_list
)
6997 for (page_range
*p
= output_page_list
; p
; p
= p
->next
)
7003 static void parse_output_page_list(char *p
)
7009 else if (csdigit(*p
)) {
7012 i
= i
*10 + *p
++ - '0';
7013 while (csdigit(*p
));
7023 j
= j
*10 + *p
++ - '0';
7024 while (csdigit(*p
));
7030 last_page_number
= -1;
7031 else if (last_page_number
>= 0 && j
> last_page_number
)
7032 last_page_number
= j
;
7033 output_page_list
= new page_range(i
, j
, output_page_list
);
7039 error("bad output page list");
7040 output_page_list
= 0;
7044 static FILE *open_mac_file(const char *mac
, char **path
)
7046 // Try first FOOBAR.tmac, then tmac.FOOBAR
7047 char *s1
= new char[strlen(mac
)+strlen(MACRO_POSTFIX
)+1];
7049 strcat(s1
, MACRO_POSTFIX
);
7050 FILE *fp
= mac_path
->open_file(s1
, path
);
7053 char *s2
= new char[strlen(mac
)+strlen(MACRO_PREFIX
)+1];
7054 strcpy(s2
, MACRO_PREFIX
);
7056 fp
= mac_path
->open_file(s2
, path
);
7062 static void process_macro_file(const char *mac
)
7065 FILE *fp
= open_mac_file(mac
, &path
);
7067 fatal("can't find macro file %1", mac
);
7068 const char *s
= symbol(path
).contents();
7070 input_stack::push(new file_iterator(fp
, s
));
7072 process_input_stack();
7075 static void process_startup_file(const char *filename
)
7078 search_path
*orig_mac_path
= mac_path
;
7079 mac_path
= &config_macro_path
;
7080 FILE *fp
= mac_path
->open_file(filename
, &path
);
7082 input_stack::push(new file_iterator(fp
, symbol(path
).contents()));
7085 process_input_stack();
7087 mac_path
= orig_mac_path
;
7092 symbol nm
= get_long_name(1);
7096 while (!tok
.newline() && !tok
.eof())
7099 FILE *fp
= mac_path
->open_file(nm
.contents(), &path
);
7100 // .mso doesn't (and cannot) go through open_mac_file, so we
7101 // need to do it here manually: If we have tmac.FOOBAR, try
7102 // FOOBAR.tmac and vice versa
7104 const char *fn
= nm
.contents();
7105 if (strncasecmp(fn
, MACRO_PREFIX
, sizeof(MACRO_PREFIX
) - 1) == 0) {
7106 char *s
= new char[strlen(fn
) + sizeof(MACRO_POSTFIX
)];
7107 strcpy(s
, fn
+ sizeof(MACRO_PREFIX
) - 1);
7108 strcat(s
, MACRO_POSTFIX
);
7109 fp
= mac_path
->open_file(s
, &path
);
7113 if (strncasecmp(fn
+ strlen(fn
) - sizeof(MACRO_POSTFIX
) + 1,
7114 MACRO_POSTFIX
, sizeof(MACRO_POSTFIX
) - 1) == 0) {
7115 char *s
= new char[strlen(fn
) + sizeof(MACRO_PREFIX
)];
7116 strcpy(s
, MACRO_PREFIX
);
7117 strncat(s
, fn
, strlen(fn
) - sizeof(MACRO_POSTFIX
) + 1);
7118 fp
= mac_path
->open_file(s
, &path
);
7124 input_stack::push(new file_iterator(fp
, symbol(path
).contents()));
7128 error("can't find macro file `%1'", nm
.contents());
7133 static void process_input_file(const char *name
)
7136 if (strcmp(name
, "-") == 0) {
7142 fp
= include_search_path
.open_file_cautious(name
);
7144 fatal("can't open `%1': %2", name
, strerror(errno
));
7146 input_stack::push(new file_iterator(fp
, name
));
7148 process_input_stack();
7151 // make sure the_input is empty before calling this
7153 static int evaluate_expression(const char *expr
, units
*res
)
7155 input_stack::push(make_temp_iterator(expr
));
7157 int success
= get_number(res
, 'u');
7158 while (input_stack::get(0) != EOF
)
7163 static void do_register_assignment(const char *s
)
7165 const char *p
= strchr(s
, '=');
7171 if (evaluate_expression(s
+ 1, &n
))
7172 set_number_reg(buf
, n
);
7175 char *buf
= new char[p
- s
+ 1];
7176 memcpy(buf
, s
, p
- s
);
7179 if (evaluate_expression(p
+ 1, &n
))
7180 set_number_reg(buf
, n
);
7185 static void set_string(const char *name
, const char *value
)
7187 macro
*m
= new macro
;
7188 for (const char *p
= value
; *p
; p
++)
7189 if (!invalid_input_char((unsigned char)*p
))
7191 request_dictionary
.define(name
, m
);
7194 static void do_string_assignment(const char *s
)
7196 const char *p
= strchr(s
, '=');
7201 set_string(buf
, s
+ 1);
7204 char *buf
= new char[p
- s
+ 1];
7205 memcpy(buf
, s
, p
- s
);
7207 set_string(buf
, p
+ 1);
7212 struct string_list
{
7215 string_list(const char *ss
) : s(ss
), next(0) {}
7219 static void prepend_string(const char *s
, string_list
**p
)
7221 string_list
*l
= new string_list(s
);
7227 static void add_string(const char *s
, string_list
**p
)
7231 *p
= new string_list(s
);
7234 void usage(FILE *stream
, const char *prog
)
7237 "usage: %s -abcivzCERU -wname -Wname -dcs -ffam -mname -nnum -olist\n"
7238 " -rcn -Tname -Fdir -Idir -Mdir [files...]\n",
7242 int main(int argc
, char **argv
)
7244 program_name
= argv
[0];
7245 static char stderr_buf
[BUFSIZ
];
7246 setbuf(stderr
, stderr_buf
);
7248 string_list
*macros
= 0;
7249 string_list
*register_assignments
= 0;
7250 string_list
*string_assignments
= 0;
7255 int no_rc
= 0; // don't process troffrc and troffrc-end
7256 int next_page_number
= 0; // pacify compiler
7258 hresolution
= vresolution
= 1;
7259 // restore $PATH if called from groff
7260 char* groff_path
= getenv("GROFF_PATH__");
7267 if (putenv(strsave(e
.contents())))
7268 fatal("putenv failed");
7270 static const struct option long_options
[] = {
7271 { "help", no_argument
, 0, CHAR_MAX
+ 1 },
7272 { "version", no_argument
, 0, 'v' },
7275 #if defined(DEBUGGING)
7276 #define DEBUG_OPTION "D"
7278 while ((c
= getopt_long(argc
, argv
,
7279 "abciI:vw:W:zCEf:m:n:o:r:d:F:M:T:tqs:RU"
7280 DEBUG_OPTION
, long_options
, 0))
7285 printf("GNU troff (groff) version %s\n", Version_string
);
7290 // Search path for .psbb files
7291 // and most other non-system input files.
7292 include_search_path
.command_line_dir(optarg
);
7297 is_html
= (strcmp(device
, "html") == 0);
7300 compatible_flag
= 1;
7306 macro_path
.command_line_dir(optarg
);
7307 safer_macro_path
.command_line_dir(optarg
);
7308 config_macro_path
.command_line_dir(optarg
);
7311 font::command_line_font_dir(optarg
);
7314 add_string(optarg
, ¯os
);
7323 enable_warning(optarg
);
7326 disable_warning(optarg
);
7335 ascii_output_flag
= 1;
7338 suppress_output_flag
= 1;
7341 if (sscanf(optarg
, "%d", &next_page_number
) == 1)
7344 error("bad page number");
7347 parse_output_page_list(optarg
);
7350 if (*optarg
== '\0')
7351 error("`-d' requires non-empty argument");
7353 add_string(optarg
, &string_assignments
);
7356 if (*optarg
== '\0')
7357 error("`-r' requires non-empty argument");
7359 add_string(optarg
, ®ister_assignments
);
7362 default_family
= symbol(optarg
);
7368 // silently ignore these
7371 safer_flag
= 0; // unsafe behaviour
7373 #if defined(DEBUGGING)
7378 case CHAR_MAX
+ 1: // --help
7379 usage(stdout
, argv
[0]);
7383 usage(stderr
, argv
[0]);
7385 break; // never reached
7390 mac_path
= ¯o_path
;
7391 set_string(".T", device
);
7392 init_charset_table();
7393 init_hpf_code_table();
7394 if (!font::load_desc())
7395 fatal("sorry, I can't continue");
7396 units_per_inch
= font::res
;
7397 hresolution
= font::hor
;
7398 vresolution
= font::vert
;
7399 sizescale
= font::sizescale
;
7400 tcommand_flag
= font::tcommand
;
7401 warn_scale
= (double)units_per_inch
;
7402 warn_scaling_indicator
= 'i';
7403 if (!fflag
&& font::family
!= 0 && *font::family
!= '\0')
7404 default_family
= symbol(font::family
);
7405 font_size::init_size_table(font::sizes
);
7408 if (font::style_table
) {
7409 for (i
= 0; font::style_table
[i
]; i
++)
7410 mount_style(j
++, symbol(font::style_table
[i
]));
7412 for (i
= 0; font::font_name_table
[i
]; i
++, j
++)
7413 // In the DESC file a font name of 0 (zero) means leave this
7415 if (strcmp(font::font_name_table
[i
], "0") != 0)
7416 mount_font(j
, symbol(font::font_name_table
[i
]));
7417 curdiv
= topdiv
= new top_level_diversion
;
7419 topdiv
->set_next_page_number(next_page_number
);
7420 init_input_requests();
7421 init_env_requests();
7422 init_div_requests();
7424 init_column_requests();
7426 init_node_requests();
7427 number_reg_dictionary
.define(".T", new constant_reg(tflag
? "1" : "0"));
7429 init_reg_requests();
7430 init_hyphen_requests();
7431 init_environments();
7432 while (string_assignments
) {
7433 do_string_assignment(string_assignments
->s
);
7434 string_list
*tem
= string_assignments
;
7435 string_assignments
= string_assignments
->next
;
7438 while (register_assignments
) {
7439 do_register_assignment(register_assignments
->s
);
7440 string_list
*tem
= register_assignments
;
7441 register_assignments
= register_assignments
->next
;
7445 process_startup_file(INITIAL_STARTUP_FILE
);
7447 process_macro_file(macros
->s
);
7448 string_list
*tem
= macros
;
7449 macros
= macros
->next
;
7453 process_startup_file(FINAL_STARTUP_FILE
);
7454 for (i
= optind
; i
< argc
; i
++)
7455 process_input_file(argv
[i
]);
7456 if (optind
>= argc
|| iflag
)
7457 process_input_file("-");
7459 return 0; // not reached
7465 if (has_arg() && get_integer(&n
)) {
7466 if (n
& ~WARN_TOTAL
) {
7467 warning(WARN_RANGE
, "warning mask must be between 0 and %1", WARN_TOTAL
);
7473 warning_mask
= WARN_TOTAL
;
7477 static void init_registers()
7479 #ifdef LONG_FOR_TIME_T
7481 #else /* not LONG_FOR_TIME_T */
7483 #endif /* not LONG_FOR_TIME_T */
7485 // Use struct here to work around misfeature in old versions of g++.
7486 struct tm
*tt
= localtime(&t
);
7487 set_number_reg("seconds", int(tt
->tm_sec
));
7488 set_number_reg("minutes", int(tt
->tm_min
));
7489 set_number_reg("hours", int(tt
->tm_hour
));
7490 set_number_reg("dw", int(tt
->tm_wday
+ 1));
7491 set_number_reg("dy", int(tt
->tm_mday
));
7492 set_number_reg("mo", int(tt
->tm_mon
+ 1));
7493 set_number_reg("year", int(1900 + tt
->tm_year
));
7494 set_number_reg("yr", int(tt
->tm_year
));
7495 set_number_reg("$$", getpid());
7496 number_reg_dictionary
.define(".A",
7497 new constant_reg(ascii_output_flag
7503 * registers associated with \O
7506 static int output_reg_minx_contents
= -1;
7507 static int output_reg_miny_contents
= -1;
7508 static int output_reg_maxx_contents
= -1;
7509 static int output_reg_maxy_contents
= -1;
7511 void check_output_limits(int x
, int y
)
7513 if ((output_reg_minx_contents
== -1) || (x
< output_reg_minx_contents
))
7514 output_reg_minx_contents
= x
;
7515 if (x
> output_reg_maxx_contents
)
7516 output_reg_maxx_contents
= x
;
7517 if ((output_reg_miny_contents
== -1) || (y
< output_reg_miny_contents
))
7518 output_reg_miny_contents
= y
;
7519 if (y
> output_reg_maxy_contents
)
7520 output_reg_maxy_contents
= y
;
7523 void reset_output_registers()
7525 output_reg_minx_contents
= -1;
7526 output_reg_miny_contents
= -1;
7527 output_reg_maxx_contents
= -1;
7528 output_reg_maxy_contents
= -1;
7531 void get_output_registers(int *minx
, int *miny
, int *maxx
, int *maxy
)
7533 *minx
= output_reg_minx_contents
;
7534 *miny
= output_reg_miny_contents
;
7535 *maxx
= output_reg_maxx_contents
;
7536 *maxy
= output_reg_maxy_contents
;
7539 void init_input_requests()
7541 init_request("ab", abort_request
);
7542 init_request("als", alias_macro
);
7543 init_request("am", append_macro
);
7544 init_request("am1", append_nocomp_macro
);
7545 init_request("ami", append_indirect_macro
);
7546 init_request("ami1", append_indirect_nocomp_macro
);
7547 init_request("as", append_string
);
7548 init_request("as1", append_nocomp_string
);
7549 init_request("asciify", asciify_macro
);
7550 init_request("backtrace", backtrace_request
);
7551 init_request("blm", blank_line_macro
);
7552 init_request("break", while_break_request
);
7553 init_request("cf", copy_file
);
7554 init_request("cflags", char_flags
);
7555 init_request("char", define_character
);
7556 init_request("chop", chop_macro
);
7557 init_request("close", close_request
);
7558 init_request("color", activate_color
);
7559 init_request("composite", composite_request
);
7560 init_request("continue", while_continue_request
);
7561 init_request("cp", compatible
);
7562 init_request("de", define_macro
);
7563 init_request("de1", define_nocomp_macro
);
7564 init_request("defcolor", define_color
);
7565 init_request("dei", define_indirect_macro
);
7566 init_request("dei1", define_indirect_nocomp_macro
);
7567 init_request("do", do_request
);
7568 init_request("ds", define_string
);
7569 init_request("ds1", define_nocomp_string
);
7570 init_request("ec", set_escape_char
);
7571 init_request("ecr", restore_escape_char
);
7572 init_request("ecs", save_escape_char
);
7573 init_request("el", else_request
);
7574 init_request("em", end_macro
);
7575 init_request("eo", escape_off
);
7576 init_request("ex", exit_request
);
7577 init_request("fchar", define_fallback_character
);
7578 #ifdef WIDOW_CONTROL
7579 init_request("fpl", flush_pending_lines
);
7580 #endif /* WIDOW_CONTROL */
7581 init_request("hcode", hyphenation_code
);
7582 init_request("hpfcode", hyphenation_patterns_file_code
);
7583 init_request("ie", if_else_request
);
7584 init_request("if", if_request
);
7585 init_request("ig", ignore
);
7586 init_request("length", length_request
);
7587 init_request("lf", line_file
);
7588 init_request("mso", macro_source
);
7589 init_request("nop", nop_request
);
7590 init_request("nroff", nroff_request
);
7591 init_request("nx", next_file
);
7592 init_request("open", open_request
);
7593 init_request("opena", opena_request
);
7594 init_request("output", output_request
);
7595 init_request("pc", set_page_character
);
7596 init_request("pi", pipe_output
);
7597 init_request("pm", print_macros
);
7598 init_request("psbb", ps_bbox_request
);
7599 #ifndef POPEN_MISSING
7600 init_request("pso", pipe_source
);
7601 #endif /* not POPEN_MISSING */
7602 init_request("rchar", remove_character
);
7603 init_request("rd", read_request
);
7604 init_request("return", return_macro_request
);
7605 init_request("rm", remove_macro
);
7606 init_request("rn", rename_macro
);
7607 init_request("schar", define_special_character
);
7608 init_request("shift", shift
);
7609 init_request("so", source
);
7610 init_request("spreadwarn", spreadwarn_request
);
7611 init_request("substring", substring_request
);
7612 init_request("sy", system_request
);
7613 init_request("tag", tag
);
7614 init_request("taga", taga
);
7615 init_request("tm", terminal
);
7616 init_request("tm1", terminal1
);
7617 init_request("tmc", terminal_continue
);
7618 init_request("tr", translate
);
7619 init_request("trf", transparent_file
);
7620 init_request("trin", translate_input
);
7621 init_request("trnt", translate_no_transparent
);
7622 init_request("troff", troff_request
);
7623 init_request("unformat", unformat_macro
);
7625 init_request("vj", vjustify
);
7627 init_request("warn", warn_request
);
7628 init_request("warnscale", warnscale_request
);
7629 init_request("while", while_request
);
7630 init_request("write", write_request
);
7631 init_request("writec", write_request_continue
);
7632 init_request("writem", write_macro_request
);
7633 number_reg_dictionary
.define(".$", new nargs_reg
);
7634 number_reg_dictionary
.define(".C", new constant_int_reg(&compatible_flag
));
7635 number_reg_dictionary
.define(".c", new lineno_reg
);
7636 number_reg_dictionary
.define(".color", new constant_int_reg(&color_flag
));
7637 number_reg_dictionary
.define(".F", new filename_reg
);
7638 number_reg_dictionary
.define(".g", new constant_reg("1"));
7639 number_reg_dictionary
.define(".H", new constant_int_reg(&hresolution
));
7640 number_reg_dictionary
.define(".R", new constant_reg("10000"));
7641 number_reg_dictionary
.define(".U", new constant_int_reg(&safer_flag
));
7642 number_reg_dictionary
.define(".V", new constant_int_reg(&vresolution
));
7643 number_reg_dictionary
.define(".warn", new constant_int_reg(&warning_mask
));
7644 extern const char *major_version
;
7645 number_reg_dictionary
.define(".x", new constant_reg(major_version
));
7646 extern const char *revision
;
7647 number_reg_dictionary
.define(".Y", new constant_reg(revision
));
7648 extern const char *minor_version
;
7649 number_reg_dictionary
.define(".y", new constant_reg(minor_version
));
7650 number_reg_dictionary
.define("c.", new writable_lineno_reg
);
7651 number_reg_dictionary
.define("llx", new variable_reg(&llx_reg_contents
));
7652 number_reg_dictionary
.define("lly", new variable_reg(&lly_reg_contents
));
7653 number_reg_dictionary
.define("opmaxx",
7654 new variable_reg(&output_reg_maxx_contents
));
7655 number_reg_dictionary
.define("opmaxy",
7656 new variable_reg(&output_reg_maxy_contents
));
7657 number_reg_dictionary
.define("opminx",
7658 new variable_reg(&output_reg_minx_contents
));
7659 number_reg_dictionary
.define("opminy",
7660 new variable_reg(&output_reg_miny_contents
));
7661 number_reg_dictionary
.define("slimit",
7662 new variable_reg(&input_stack::limit
));
7663 number_reg_dictionary
.define("systat", new variable_reg(&system_status
));
7664 number_reg_dictionary
.define("urx", new variable_reg(&urx_reg_contents
));
7665 number_reg_dictionary
.define("ury", new variable_reg(&ury_reg_contents
));
7668 object_dictionary
request_dictionary(501);
7670 void init_request(const char *s
, REQUEST_FUNCP f
)
7672 request_dictionary
.define(s
, new request(f
));
7675 static request_or_macro
*lookup_request(symbol nm
)
7677 assert(!nm
.is_null());
7678 request_or_macro
*p
= (request_or_macro
*)request_dictionary
.lookup(nm
);
7680 warning(WARN_MAC
, "macro `%1' not defined", nm
.contents());
7682 request_dictionary
.define(nm
, p
);
7687 node
*charinfo_to_node_list(charinfo
*ci
, const environment
*envp
)
7689 // Don't interpret character definitions in compatible mode.
7690 int old_compatible_flag
= compatible_flag
;
7691 compatible_flag
= 0;
7692 int old_escape_char
= escape_char
;
7694 macro
*mac
= ci
->set_macro(0);
7696 environment
*oldenv
= curenv
;
7697 environment
env(envp
);
7699 curenv
->set_composite();
7700 token old_tok
= tok
;
7701 input_stack::add_boundary();
7702 string_iterator
*si
=
7703 new string_iterator(*mac
, "composite character", ci
->nm
);
7704 input_stack::push(si
);
7705 // we don't use process_input_stack, because we don't want to recognise
7711 if (tok
.newline()) {
7712 error("composite character mustn't contain newline");
7720 node
*n
= curenv
->extract_output_line();
7721 input_stack::remove_boundary();
7725 compatible_flag
= old_compatible_flag
;
7726 escape_char
= old_escape_char
;
7731 static node
*read_draw_node()
7735 if (!start
.delimiter(1)){
7738 } while (tok
!= start
&& !tok
.newline() && !tok
.eof());
7743 error("missing argument");
7745 unsigned char type
= tok
.ch();
7747 read_color_draw_node(start
);
7752 hvpair
*point
= new hvpair
[maxpoints
];
7757 for (i
= 0; tok
!= start
; i
++) {
7758 if (i
== maxpoints
) {
7759 hvpair
*oldpoint
= point
;
7760 point
= new hvpair
[maxpoints
*2];
7761 for (int j
= 0; j
< maxpoints
; j
++)
7762 point
[j
] = oldpoint
[j
];
7766 if (!get_hunits(&point
[i
].h
,
7767 type
== 'f' || type
== 't' ? 'u' : 'm')) {
7778 if (!get_vunits(&point
[i
].v
, 'v')) {
7784 while (tok
!= start
&& !tok
.newline() && !tok
.eof())
7789 if (npoints
!= 1 || no_last_v
) {
7790 error("two arguments needed for line");
7795 if (npoints
!= 1 || !no_last_v
) {
7796 error("one argument needed for circle");
7802 if (npoints
!= 1 || no_last_v
) {
7803 error("two arguments needed for ellipse");
7808 if (npoints
!= 2 || no_last_v
) {
7809 error("four arguments needed for arc");
7815 error("even number of arguments needed for spline");
7818 if (npoints
!= 1 || !no_last_v
) {
7819 error("one argument needed for gray shade");
7824 // silently pass it through
7827 draw_node
*dn
= new draw_node(type
, point
, npoints
,
7828 curenv
->get_font_size(),
7829 curenv
->get_glyph_color(),
7830 curenv
->get_fill_color());
7842 static void read_color_draw_node(token
&start
)
7846 error("missing color scheme");
7849 unsigned char scheme
= tok
.ch();
7852 char end
= start
.ch();
7855 col
= read_cmy(end
);
7858 col
= &default_color
;
7861 col
= read_gray(end
);
7864 col
= read_cmyk(end
);
7867 col
= read_rgb(end
);
7871 curenv
->set_fill_color(col
);
7872 while (tok
!= start
) {
7873 if (tok
.newline() || tok
.eof()) {
7874 warning(WARN_DELIM
, "missing closing delimiter");
7875 input_stack::push(make_temp_iterator("\n"));
7886 } warning_table
[] = {
7887 { "char", WARN_CHAR
},
7888 { "range", WARN_RANGE
},
7889 { "break", WARN_BREAK
},
7890 { "delim", WARN_DELIM
},
7892 { "scale", WARN_SCALE
},
7893 { "number", WARN_NUMBER
},
7894 { "syntax", WARN_SYNTAX
},
7895 { "tab", WARN_TAB
},
7896 { "right-brace", WARN_RIGHT_BRACE
},
7897 { "missing", WARN_MISSING
},
7898 { "input", WARN_INPUT
},
7899 { "escape", WARN_ESCAPE
},
7900 { "space", WARN_SPACE
},
7901 { "font", WARN_FONT
},
7903 { "mac", WARN_MAC
},
7904 { "reg", WARN_REG
},
7906 { "color", WARN_COLOR
},
7907 { "all", WARN_TOTAL
& ~(WARN_DI
| WARN_MAC
| WARN_REG
) },
7908 { "w", WARN_TOTAL
},
7909 { "default", DEFAULT_WARNING_MASK
},
7912 static int lookup_warning(const char *name
)
7914 for (unsigned int i
= 0;
7915 i
< sizeof(warning_table
)/sizeof(warning_table
[0]);
7917 if (strcmp(name
, warning_table
[i
].name
) == 0)
7918 return warning_table
[i
].mask
;
7922 static void enable_warning(const char *name
)
7924 int mask
= lookup_warning(name
);
7926 warning_mask
|= mask
;
7928 error("unknown warning `%1'", name
);
7931 static void disable_warning(const char *name
)
7933 int mask
= lookup_warning(name
);
7935 warning_mask
&= ~mask
;
7937 error("unknown warning `%1'", name
);
7940 static void copy_mode_error(const char *format
,
7946 static const char prefix
[] = "(in ignored input) ";
7947 char *s
= new char[sizeof(prefix
) + strlen(format
)];
7950 warning(WARN_IG
, s
, arg1
, arg2
, arg3
);
7954 error(format
, arg1
, arg2
, arg3
);
7957 enum error_type
{ WARNING
, OUTPUT_WARNING
, ERROR
, FATAL
};
7959 static void do_error(error_type type
,
7965 const char *filename
;
7967 if (inhibit_errors
&& type
< FATAL
)
7970 input_stack::backtrace();
7971 if (!get_file_line(&filename
, &lineno
))
7974 errprint("%1:%2: ", filename
, lineno
);
7975 else if (program_name
)
7976 fprintf(stderr
, "%s: ", program_name
);
7979 fputs("fatal error: ", stderr
);
7984 fputs("warning: ", stderr
);
7986 case OUTPUT_WARNING
:
7987 double fromtop
= topdiv
->get_vertical_position().to_units() / warn_scale
;
7988 fprintf(stderr
, "warning [p %d, %.1f%c",
7989 topdiv
->get_page_number(), fromtop
, warn_scaling_indicator
);
7990 if (topdiv
!= curdiv
) {
7991 double fromtop1
= curdiv
->get_vertical_position().to_units()
7993 fprintf(stderr
, ", div `%s', %.1f%c",
7994 curdiv
->get_diversion_name(), fromtop1
, warn_scaling_indicator
);
7996 fprintf(stderr
, "]: ");
7999 errprint(format
, arg1
, arg2
, arg3
);
8000 fputc('\n', stderr
);
8003 cleanup_and_exit(1);
8006 int warning(warning_type t
,
8012 if ((t
& warning_mask
) != 0) {
8013 do_error(WARNING
, format
, arg1
, arg2
, arg3
);
8020 int output_warning(warning_type t
,
8026 if ((t
& warning_mask
) != 0) {
8027 do_error(OUTPUT_WARNING
, format
, arg1
, arg2
, arg3
);
8034 void error(const char *format
,
8039 do_error(ERROR
, format
, arg1
, arg2
, arg3
);
8042 void fatal(const char *format
,
8047 do_error(FATAL
, format
, arg1
, arg2
, arg3
);
8050 void fatal_with_file_and_line(const char *filename
, int lineno
,
8056 fprintf(stderr
, "%s:%d: fatal error: ", filename
, lineno
);
8057 errprint(format
, arg1
, arg2
, arg3
);
8058 fputc('\n', stderr
);
8060 cleanup_and_exit(1);
8063 void error_with_file_and_line(const char *filename
, int lineno
,
8069 fprintf(stderr
, "%s:%d: error: ", filename
, lineno
);
8070 errprint(format
, arg1
, arg2
, arg3
);
8071 fputc('\n', stderr
);
8075 dictionary
charinfo_dictionary(501);
8077 charinfo
*get_charinfo(symbol nm
)
8079 void *p
= charinfo_dictionary
.lookup(nm
);
8081 return (charinfo
*)p
;
8082 charinfo
*cp
= new charinfo(nm
);
8083 (void)charinfo_dictionary
.lookup(nm
, cp
);
8087 int charinfo::next_index
= 0;
8089 charinfo::charinfo(symbol s
)
8090 : translation(0), mac(0), special_translation(TRANSLATE_NONE
),
8091 hyphenation_code(0), flags(0), ascii_code(0), asciify_code(0),
8092 not_found(0), transparent_translate(1), translate_input(0),
8093 mode(CHAR_NORMAL
), nm(s
)
8095 index
= next_index
++;
8098 void charinfo::set_hyphenation_code(unsigned char c
)
8100 hyphenation_code
= c
;
8103 void charinfo::set_translation(charinfo
*ci
, int tt
, int ti
)
8107 if (hyphenation_code
!= 0)
8108 ci
->set_hyphenation_code(hyphenation_code
);
8109 if (asciify_code
!= 0)
8110 ci
->set_asciify_code(asciify_code
);
8111 else if (ascii_code
!= 0)
8112 ci
->set_asciify_code(ascii_code
);
8113 ci
->set_translation_input();
8115 special_translation
= TRANSLATE_NONE
;
8116 transparent_translate
= tt
;
8119 void charinfo::set_special_translation(int c
, int tt
)
8121 special_translation
= c
;
8123 transparent_translate
= tt
;
8126 void charinfo::set_ascii_code(unsigned char c
)
8131 void charinfo::set_asciify_code(unsigned char c
)
8136 macro
*charinfo::set_macro(macro
*m
)
8143 macro
*charinfo::setx_macro(macro
*m
, char_mode cm
)
8151 void charinfo::set_number(int n
)
8157 int charinfo::get_number()
8159 assert(flags
& NUMBERED
);
8163 symbol
UNNAMED_SYMBOL("---");
8165 // For numbered characters not between 0 and 255, we make a symbol out
8166 // of the number and store them in this dictionary.
8168 dictionary
numbered_charinfo_dictionary(11);
8170 charinfo
*get_charinfo_by_number(int n
)
8172 static charinfo
*number_table
[256];
8174 if (n
>= 0 && n
< 256) {
8175 charinfo
*ci
= number_table
[n
];
8177 ci
= new charinfo(UNNAMED_SYMBOL
);
8179 number_table
[n
] = ci
;
8184 symbol
ns(i_to_a(n
));
8185 charinfo
*ci
= (charinfo
*)numbered_charinfo_dictionary
.lookup(ns
);
8187 ci
= new charinfo(UNNAMED_SYMBOL
);
8189 (void)numbered_charinfo_dictionary
.lookup(ns
, ci
);
8195 int font::name_to_index(const char *nm
)
8199 ci
= charset_table
[nm
[0] & 0xff];
8200 else if (nm
[0] == '\\' && nm
[2] == 0)
8201 ci
= get_charinfo(symbol(nm
+ 1));
8203 ci
= get_charinfo(symbol(nm
));
8207 return ci
->get_index();
8210 int font::number_to_index(int n
)
8212 return get_charinfo_by_number(n
)->get_index();