2 /* Copyright (C) 1989, 1990, 1991, 1992, 2000, 2001, 2002, 2003, 2004
3 Free Software Foundation, Inc.
4 Written by James Clark (jjc@jclark.com)
6 This file is part of groff.
8 groff is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
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, 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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;
3412 const unsigned char *e
= bp
->s
+ char_block::SIZE
;
3417 unsigned char c
= *p
;
3418 if (c
== '\n' || c
== ESCAPE_NEWLINE
) {
3432 int string_iterator::peek()
3436 const unsigned char *p
= eptr
;
3437 if (p
>= bp
->s
+ char_block::SIZE
) {
3443 int string_iterator::get_location(int allow_macro
,
3444 const char **filep
, int *linep
)
3448 if (mac
.filename
== 0)
3450 *filep
= mac
.filename
;
3451 *linep
= mac
.lineno
+ lineno
- 1;
3455 void string_iterator::backtrace()
3458 errprint("%1:%2: backtrace", mac
.filename
, mac
.lineno
+ lineno
- 1);
3461 errprint(": %1 `%2'\n", how_invoked
, nm
.contents());
3463 errprint(": %1\n", how_invoked
);
3470 class temp_iterator
: public input_iterator
{
3471 unsigned char *base
;
3472 temp_iterator(const char *, int len
);
3475 friend input_iterator
*make_temp_iterator(const char *);
3481 temp_iterator::temp_iterator(const char *s
, int len
)
3483 base
= new unsigned char[len
];
3484 memcpy(base
, s
, len
);
3489 temp_iterator::~temp_iterator()
3494 class small_temp_iterator
: public input_iterator
{
3496 small_temp_iterator(const char *, int);
3497 ~small_temp_iterator();
3498 enum { BLOCK
= 16 };
3499 static small_temp_iterator
*free_list
;
3500 void *operator new(size_t);
3501 void operator delete(void *);
3503 unsigned char buf
[SIZE
];
3504 friend input_iterator
*make_temp_iterator(const char *);
3507 small_temp_iterator
*small_temp_iterator::free_list
= 0;
3509 void *small_temp_iterator::operator new(size_t n
)
3511 assert(n
== sizeof(small_temp_iterator
));
3514 (small_temp_iterator
*)new char[sizeof(small_temp_iterator
)*BLOCK
];
3515 for (int i
= 0; i
< BLOCK
- 1; i
++)
3516 free_list
[i
].next
= free_list
+ i
+ 1;
3517 free_list
[BLOCK
-1].next
= 0;
3519 small_temp_iterator
*p
= free_list
;
3520 free_list
= (small_temp_iterator
*)(free_list
->next
);
3528 void small_temp_iterator::operator delete(void *p
)
3531 ((small_temp_iterator
*)p
)->next
= free_list
;
3532 free_list
= (small_temp_iterator
*)p
;
3536 small_temp_iterator::~small_temp_iterator()
3543 small_temp_iterator::small_temp_iterator(const char *s
, int len
)
3545 for (int i
= 0; i
< len
; i
++)
3551 input_iterator
*make_temp_iterator(const char *s
)
3554 return new small_temp_iterator(s
, 0);
3557 if (n
<= small_temp_iterator::SIZE
)
3558 return new small_temp_iterator(s
, n
);
3560 return new temp_iterator(s
, n
);
3564 // this is used when macros with arguments are interpolated
3569 arg_list(const macro
&);
3573 arg_list::arg_list(const macro
&m
) : mac(m
), next(0)
3577 arg_list::~arg_list()
3581 class macro_iterator
: public string_iterator
{
3585 macro_iterator(symbol
, macro
&, const char *how_invoked
= "macro");
3588 int has_args() { return 1; }
3589 input_iterator
*get_arg(int i
);
3590 int nargs() { return argc
; }
3591 void add_arg(const macro
&m
);
3593 int is_macro() { return 1; }
3597 input_iterator
*macro_iterator::get_arg(int i
)
3600 return make_temp_iterator(nm
.contents());
3601 if (i
> 0 && i
<= argc
) {
3603 for (int j
= 1; j
< i
; j
++) {
3607 return new string_iterator(p
->mac
);
3613 void macro_iterator::add_arg(const macro
&m
)
3616 for (p
= &args
; *p
; p
= &((*p
)->next
))
3618 *p
= new arg_list(m
);
3622 void macro_iterator::shift(int n
)
3624 while (n
> 0 && argc
> 0) {
3625 arg_list
*tem
= args
;
3633 // This gets used by eg .if '\?xxx\?''.
3635 int operator==(const macro
&m1
, const macro
&m2
)
3637 if (m1
.len
!= m2
.len
)
3639 string_iterator
iter1(m1
);
3640 string_iterator
iter2(m2
);
3644 int c1
= iter1
.get(&nd1
);
3647 int c2
= iter2
.get(&nd2
);
3659 int are_same
= nd1
->type() == nd2
->type() && nd1
->same(nd2
);
3669 static void interpolate_macro(symbol nm
)
3671 request_or_macro
*p
= (request_or_macro
*)request_dictionary
.lookup(nm
);
3674 const char *s
= nm
.contents();
3675 if (strlen(s
) > 2) {
3676 request_or_macro
*r
;
3681 r
= (request_or_macro
*)request_dictionary
.lookup(symbol(buf
));
3683 macro
*m
= r
->to_macro();
3684 if (!m
|| !m
->empty())
3685 warned
= warning(WARN_SPACE
,
3686 "macro `%1' not defined "
3687 "(probably missing space after `%2')",
3688 nm
.contents(), buf
);
3692 warning(WARN_MAC
, "macro `%1' not defined", nm
.contents());
3694 request_dictionary
.define(nm
, p
);
3705 static void decode_args(macro_iterator
*mi
)
3707 if (!tok
.newline() && !tok
.eof()) {
3709 int c
= get_copy(&n
);
3713 if (c
== '\n' || c
== EOF
)
3716 int quote_input_level
= 0;
3717 int done_tab_warning
= 0;
3719 quote_input_level
= input_stack::get_level();
3722 arg
.append(compatible_flag
? PUSH_COMP_MODE
: PUSH_GROFF_MODE
);
3723 while (c
!= EOF
&& c
!= '\n' && !(c
== ' ' && quote_input_level
== 0)) {
3724 if (quote_input_level
> 0 && c
== '"'
3726 || input_stack::get_level() == quote_input_level
)) {
3739 if (c
== '\t' && quote_input_level
== 0 && !done_tab_warning
) {
3740 warning(WARN_TAB
, "tab character in unquoted macro argument");
3741 done_tab_warning
= 1;
3748 arg
.append(POP_GROFFCOMP_MODE
);
3754 static void decode_string_args(macro_iterator
*mi
)
3757 int c
= get_copy(&n
);
3761 if (c
== '\n' || c
== EOF
) {
3762 error("missing `]'");
3768 int quote_input_level
= 0;
3769 int done_tab_warning
= 0;
3771 quote_input_level
= input_stack::get_level();
3774 while (c
!= EOF
&& c
!= '\n'
3775 && !(c
== ']' && quote_input_level
== 0)
3776 && !(c
== ' ' && quote_input_level
== 0)) {
3777 if (quote_input_level
> 0 && c
== '"'
3778 && input_stack::get_level() == quote_input_level
) {
3791 if (c
== '\t' && quote_input_level
== 0 && !done_tab_warning
) {
3792 warning(WARN_TAB
, "tab character in unquoted string argument");
3793 done_tab_warning
= 1;
3804 void macro::invoke(symbol nm
)
3806 macro_iterator
*mi
= new macro_iterator(nm
, *this);
3808 input_stack::push(mi
);
3812 macro
*macro::to_macro()
3819 return empty_macro
== 1;
3822 macro_iterator::macro_iterator(symbol s
, macro
&m
, const char *how_called
)
3823 : string_iterator(m
, how_called
, s
), args(0), argc(0)
3827 macro_iterator::macro_iterator() : args(0), argc(0)
3831 macro_iterator::~macro_iterator()
3834 arg_list
*tem
= args
;
3840 dictionary
composite_dictionary(17);
3842 void composite_request()
3844 symbol from
= get_name(1);
3845 if (!from
.is_null()) {
3846 const char *from_gn
= glyph_name_to_unicode(from
.contents());
3848 from_gn
= check_unicode_name(from
.contents());
3850 error("invalid composite glyph name `%1'", from
.contents());
3855 const char *from_decomposed
= decompose_unicode(from_gn
);
3856 if (from_decomposed
)
3857 from_gn
= &from_decomposed
[1];
3858 symbol to
= get_name(1);
3860 composite_dictionary
.remove(symbol(from_gn
));
3862 const char *to_gn
= glyph_name_to_unicode(to
.contents());
3864 to_gn
= check_unicode_name(to
.contents());
3866 error("invalid composite glyph name `%1'", to
.contents());
3871 const char *to_decomposed
= decompose_unicode(to_gn
);
3873 to_gn
= &to_decomposed
[1];
3874 if (strcmp(from_gn
, to_gn
) == 0)
3875 composite_dictionary
.remove(symbol(from_gn
));
3877 (void)composite_dictionary
.lookup(symbol(from_gn
), (void *)to_gn
);
3883 static symbol
composite_glyph_name(symbol nm
)
3885 macro_iterator
*mi
= new macro_iterator();
3886 decode_string_args(mi
);
3887 input_stack::push(mi
);
3888 const char *gn
= glyph_name_to_unicode(nm
.contents());
3890 gn
= check_unicode_name(nm
.contents());
3892 error("invalid base glyph `%1' in composite glyph name", nm
.contents());
3893 return EMPTY_SYMBOL
;
3896 const char *gn_decomposed
= decompose_unicode(gn
);
3897 string
glyph_name(gn_decomposed
? &gn_decomposed
[1] : gn
);
3899 int n
= input_stack::nargs();
3900 for (int i
= 1; i
<= n
; i
++) {
3902 input_iterator
*p
= input_stack::get_arg(i
);
3905 while ((c
= p
->get(0)) != EOF
)
3908 const char *u
= glyph_name_to_unicode(gl
.contents());
3910 u
= check_unicode_name(gl
.contents());
3912 error("invalid component `%1' in composite glyph name",
3914 return EMPTY_SYMBOL
;
3917 const char *decomposed
= decompose_unicode(u
);
3920 void *mapped_composite
= composite_dictionary
.lookup(symbol(u
));
3921 if (mapped_composite
)
3922 u
= (const char *)mapped_composite
;
3926 const char *groff_gn
= unicode_to_glyph_name(glyph_name
.contents());
3928 return symbol(groff_gn
);
3932 return symbol(gl
.contents());
3935 int trap_sprung_flag
= 0;
3936 int postpone_traps_flag
= 0;
3937 symbol postponed_trap
;
3939 void spring_trap(symbol nm
)
3941 assert(!nm
.is_null());
3942 trap_sprung_flag
= 1;
3943 if (postpone_traps_flag
) {
3944 postponed_trap
= nm
;
3947 static char buf
[2] = { BEGIN_TRAP
, 0 };
3948 static char buf2
[2] = { END_TRAP
, '\0' };
3949 input_stack::push(make_temp_iterator(buf2
));
3950 request_or_macro
*p
= lookup_request(nm
);
3951 macro
*m
= p
->to_macro();
3953 input_stack::push(new macro_iterator(nm
, *m
, "trap-invoked macro"));
3955 error("you can't invoke a request with a trap");
3956 input_stack::push(make_temp_iterator(buf
));
3959 void postpone_traps()
3961 postpone_traps_flag
= 1;
3964 int unpostpone_traps()
3966 postpone_traps_flag
= 0;
3967 if (!postponed_trap
.is_null()) {
3968 spring_trap(postponed_trap
);
3969 postponed_trap
= NULL_SYMBOL
;
3978 macro_iterator
*mi
= new macro_iterator
;
3979 int reading_from_terminal
= isatty(fileno(stdin
));
3981 if (!tok
.newline() && !tok
.eof()) {
3982 int c
= get_copy(0);
3985 while (c
!= EOF
&& c
!= '\n' && c
!= ' ') {
3986 if (!invalid_input_char(c
)) {
3987 if (reading_from_terminal
)
3998 if (reading_from_terminal
) {
3999 fputc(had_prompt
? ':' : '\a', stderr
);
4002 input_stack::push(mi
);
4006 while ((c
= getchar()) != EOF
) {
4007 if (invalid_input_char(c
))
4008 warning(WARN_INPUT
, "invalid input character code %1", int(c
));
4021 if (reading_from_terminal
)
4023 input_stack::push(new string_iterator(mac
));
4027 enum define_mode
{ DEFINE_NORMAL
, DEFINE_APPEND
, DEFINE_IGNORE
};
4028 enum calling_mode
{ CALLING_NORMAL
, CALLING_INDIRECT
};
4029 enum comp_mode
{ COMP_IGNORE
, COMP_DISABLE
, COMP_ENABLE
};
4031 void do_define_string(define_mode mode
, comp_mode comp
)
4034 node
*n
= 0; // pacify compiler
4045 else if (!tok
.space()) {
4046 error("bad string definition");
4057 request_or_macro
*rm
= (request_or_macro
*)request_dictionary
.lookup(nm
);
4058 macro
*mm
= rm
? rm
->to_macro() : 0;
4059 if (mode
== DEFINE_APPEND
&& mm
)
4061 if (comp
== COMP_DISABLE
)
4062 mac
.append(PUSH_GROFF_MODE
);
4063 else if (comp
== COMP_ENABLE
)
4064 mac
.append(PUSH_COMP_MODE
);
4065 while (c
!= '\n' && c
!= EOF
) {
4069 mac
.append((unsigned char)c
);
4074 request_dictionary
.define(nm
, mm
);
4076 if (comp
== COMP_DISABLE
|| comp
== COMP_ENABLE
)
4077 mac
.append(POP_GROFFCOMP_MODE
);
4082 void define_string()
4084 do_define_string(DEFINE_NORMAL
,
4085 compatible_flag
? COMP_ENABLE
: COMP_IGNORE
);
4088 void define_nocomp_string()
4090 do_define_string(DEFINE_NORMAL
, COMP_DISABLE
);
4093 void append_string()
4095 do_define_string(DEFINE_APPEND
,
4096 compatible_flag
? COMP_ENABLE
: COMP_IGNORE
);
4099 void append_nocomp_string()
4101 do_define_string(DEFINE_APPEND
, COMP_DISABLE
);
4104 void do_define_character(char_mode mode
, const char *font_name
)
4106 node
*n
= 0; // pacify compiler
4109 charinfo
*ci
= tok
.get_char(1);
4115 string
s(font_name
);
4117 s
+= ci
->nm
.contents();
4119 ci
= get_charinfo(symbol(s
.contents()));
4126 else if (!tok
.space()) {
4127 error("bad character definition");
4133 while (c
== ' ' || c
== '\t')
4137 macro
*m
= new macro
;
4138 while (c
!= '\n' && c
!= EOF
) {
4142 m
->append((unsigned char)c
);
4145 m
= ci
->setx_macro(m
, mode
);
4151 void define_character()
4153 do_define_character(CHAR_NORMAL
);
4156 void define_fallback_character()
4158 do_define_character(CHAR_FALLBACK
);
4161 void define_special_character()
4163 do_define_character(CHAR_SPECIAL
);
4166 static void remove_character()
4169 while (!tok
.newline() && !tok
.eof()) {
4170 if (!tok
.space() && !tok
.tab()) {
4171 charinfo
*ci
= tok
.get_char(1);
4174 macro
*m
= ci
->set_macro(0);
4183 static void interpolate_string(symbol nm
)
4185 request_or_macro
*p
= lookup_request(nm
);
4186 macro
*m
= p
->to_macro();
4188 error("you can only invoke a string or macro using \\*");
4190 string_iterator
*si
= new string_iterator(*m
, "string", nm
);
4191 input_stack::push(si
);
4195 static void interpolate_string_with_args(symbol s
)
4197 request_or_macro
*p
= lookup_request(s
);
4198 macro
*m
= p
->to_macro();
4200 error("you can only invoke a string or macro using \\*");
4202 macro_iterator
*mi
= new macro_iterator(s
, *m
);
4203 decode_string_args(mi
);
4204 input_stack::push(mi
);
4208 static void interpolate_arg(symbol nm
)
4210 const char *s
= nm
.contents();
4211 if (!s
|| *s
== '\0')
4212 copy_mode_error("missing argument name");
4213 else if (s
[1] == 0 && csdigit(s
[0]))
4214 input_stack::push(input_stack::get_arg(s
[0] - '0'));
4215 else if (s
[0] == '*' && s
[1] == '\0') {
4216 int limit
= input_stack::nargs();
4218 for (int i
= 1; i
<= limit
; i
++) {
4219 input_iterator
*p
= input_stack::get_arg(i
);
4221 while ((c
= p
->get(0)) != EOF
)
4228 input_stack::push(make_temp_iterator(args
.contents()));
4231 else if (s
[0] == '@' && s
[1] == '\0') {
4232 int limit
= input_stack::nargs();
4234 for (int i
= 1; i
<= limit
; i
++) {
4236 args
+= BEGIN_QUOTE
;
4237 input_iterator
*p
= input_stack::get_arg(i
);
4239 while ((c
= p
->get(0)) != EOF
)
4248 input_stack::push(make_temp_iterator(args
.contents()));
4253 for (p
= s
; *p
&& csdigit(*p
); p
++)
4256 copy_mode_error("bad argument name `%1'", s
);
4258 input_stack::push(input_stack::get_arg(atoi(s
)));
4262 void handle_first_page_transition()
4265 topdiv
->begin_page();
4268 // We push back a token by wrapping it up in a token_node, and
4269 // wrapping that up in a string_iterator.
4271 static void push_token(const token
&t
)
4274 m
.append(new token_node(t
));
4275 input_stack::push(new string_iterator(m
));
4278 void push_page_ejector()
4280 static char buf
[2] = { PAGE_EJECTOR
, '\0' };
4281 input_stack::push(make_temp_iterator(buf
));
4284 void handle_initial_request(unsigned char code
)
4290 mac
.append(new token_node(tok
));
4291 input_stack::push(new string_iterator(mac
));
4292 input_stack::push(make_temp_iterator(buf
));
4293 topdiv
->begin_page();
4297 void handle_initial_title()
4299 handle_initial_request(TITLE_REQUEST
);
4302 // this should be local to define_macro, but cfront 1.2 doesn't support that
4303 static symbol
dot_symbol(".");
4305 void do_define_macro(define_mode mode
, calling_mode calling
, comp_mode comp
)
4308 if (calling
== CALLING_INDIRECT
) {
4309 symbol temp1
= get_name(1);
4310 if (temp1
.is_null()) {
4314 symbol temp2
= get_name();
4315 input_stack::push(make_temp_iterator("\n"));
4316 if (!temp2
.is_null()) {
4317 interpolate_string(temp2
);
4318 input_stack::push(make_temp_iterator(" "));
4320 interpolate_string(temp1
);
4321 input_stack::push(make_temp_iterator(" "));
4324 if (mode
== DEFINE_NORMAL
|| mode
== DEFINE_APPEND
) {
4331 term
= get_name(); // the request that terminates the definition
4334 while (!tok
.newline() && !tok
.eof())
4336 const char *start_filename
;
4338 int have_start_location
= input_stack::get_location(0, &start_filename
,
4341 // doing this here makes the line numbers come out right
4342 int c
= get_copy(&n
, 1);
4345 if (mode
== DEFINE_NORMAL
|| mode
== DEFINE_APPEND
) {
4346 request_or_macro
*rm
=
4347 (request_or_macro
*)request_dictionary
.lookup(nm
);
4349 mm
= rm
->to_macro();
4350 if (mm
&& mode
== DEFINE_APPEND
)
4354 if (comp
== COMP_DISABLE
)
4355 mac
.append(PUSH_GROFF_MODE
);
4356 else if (comp
== COMP_ENABLE
)
4357 mac
.append(PUSH_COMP_MODE
);
4359 while (c
== ESCAPE_NEWLINE
) {
4360 if (mode
== DEFINE_NORMAL
|| mode
== DEFINE_APPEND
)
4362 c
= get_copy(&n
, 1);
4364 if (bol
&& c
== '.') {
4365 const char *s
= term
.contents();
4367 // see if it matches term
4370 while ((d
= get_copy(&n
)) == ' ' || d
== '\t')
4372 if ((unsigned char)s
[0] == d
) {
4373 for (i
= 1; s
[i
] != 0; i
++) {
4375 if ((unsigned char)s
[i
] != d
)
4381 && ((i
== 2 && compatible_flag
)
4382 || (d
= get_copy(&n
)) == ' '
4383 || d
== '\n')) { // we found it
4388 if (mode
== DEFINE_APPEND
|| mode
== DEFINE_NORMAL
) {
4391 request_dictionary
.define(nm
, mm
);
4393 if (comp
== COMP_DISABLE
|| comp
== COMP_ENABLE
)
4394 mac
.append(POP_GROFFCOMP_MODE
);
4397 if (term
!= dot_symbol
) {
4399 interpolate_macro(term
);
4405 if (mode
== DEFINE_APPEND
|| mode
== DEFINE_NORMAL
) {
4407 for (int j
= 0; j
< i
; j
++)
4413 if (mode
== DEFINE_NORMAL
|| mode
== DEFINE_APPEND
) {
4414 if (have_start_location
)
4415 error_with_file_and_line(start_filename
, start_lineno
,
4416 "end of file while defining macro `%1'",
4419 error("end of file while defining macro `%1'", nm
.contents());
4422 if (have_start_location
)
4423 error_with_file_and_line(start_filename
, start_lineno
,
4424 "end of file while ignoring input lines");
4426 error("end of file while ignoring input lines");
4431 if (mode
== DEFINE_NORMAL
|| mode
== DEFINE_APPEND
) {
4438 c
= get_copy(&n
, 1);
4444 do_define_macro(DEFINE_NORMAL
, CALLING_NORMAL
,
4445 compatible_flag
? COMP_ENABLE
: COMP_IGNORE
);
4448 void define_nocomp_macro()
4450 do_define_macro(DEFINE_NORMAL
, CALLING_NORMAL
, COMP_DISABLE
);
4453 void define_indirect_macro()
4455 do_define_macro(DEFINE_NORMAL
, CALLING_INDIRECT
,
4456 compatible_flag
? COMP_ENABLE
: COMP_IGNORE
);
4459 void define_indirect_nocomp_macro()
4461 do_define_macro(DEFINE_NORMAL
, CALLING_INDIRECT
, COMP_DISABLE
);
4466 do_define_macro(DEFINE_APPEND
, CALLING_NORMAL
,
4467 compatible_flag
? COMP_ENABLE
: COMP_IGNORE
);
4470 void append_nocomp_macro()
4472 do_define_macro(DEFINE_APPEND
, CALLING_NORMAL
, COMP_DISABLE
);
4475 void append_indirect_macro()
4477 do_define_macro(DEFINE_APPEND
, CALLING_INDIRECT
,
4478 compatible_flag
? COMP_ENABLE
: COMP_IGNORE
);
4481 void append_indirect_nocomp_macro()
4483 do_define_macro(DEFINE_APPEND
, CALLING_INDIRECT
, COMP_DISABLE
);
4489 do_define_macro(DEFINE_IGNORE
, CALLING_NORMAL
, COMP_IGNORE
);
4496 symbol s
= get_name();
4499 request_dictionary
.remove(s
);
4506 symbol s1
= get_name(1);
4507 if (!s1
.is_null()) {
4508 symbol s2
= get_name(1);
4510 request_dictionary
.rename(s1
, s2
);
4517 symbol s1
= get_name(1);
4518 if (!s1
.is_null()) {
4519 symbol s2
= get_name(1);
4520 if (!s2
.is_null()) {
4521 if (!request_dictionary
.alias(s1
, s2
))
4522 warning(WARN_MAC
, "macro `%1' not defined", s2
.contents());
4530 symbol s
= get_name(1);
4532 request_or_macro
*p
= lookup_request(s
);
4533 macro
*m
= p
->to_macro();
4535 error("cannot chop request");
4536 else if (m
->empty())
4537 error("cannot chop empty macro");
4539 int have_restore
= 0;
4540 // we have to check for additional save/restore pairs which could be
4541 // there due to empty am1 requests.
4543 if (m
->get(m
->len
- 1) != POP_GROFFCOMP_MODE
)
4547 if (m
->get(m
->len
- 1) != PUSH_GROFF_MODE
4548 && m
->get(m
->len
- 1) != PUSH_COMP_MODE
)
4556 error("cannot chop empty macro");
4559 m
->set(POP_GROFFCOMP_MODE
, m
->len
- 1);
4568 void substring_request()
4570 int start
; // 0, 1, ..., n-1 or -1, -2, ...
4571 symbol s
= get_name(1);
4572 if (!s
.is_null() && get_integer(&start
)) {
4573 request_or_macro
*p
= lookup_request(s
);
4574 macro
*m
= p
->to_macro();
4576 error("cannot apply `substring' on a request");
4579 if (!has_arg() || get_integer(&end
)) {
4580 int real_length
= 0; // 1, 2, ..., n
4581 string_iterator
iter1(*m
);
4582 for (int l
= 0; l
< m
->len
; l
++) {
4583 int c
= iter1
.get(0);
4584 if (c
== PUSH_GROFF_MODE
4585 || c
== PUSH_COMP_MODE
4586 || c
== POP_GROFFCOMP_MODE
)
4593 start
+= real_length
;
4601 if (start
>= real_length
|| end
< 0) {
4603 "start and end index of substring out of range");
4606 if (--(m
->p
->count
) <= 0)
4615 "start index of substring out of range, set to 0");
4618 if (end
>= real_length
) {
4620 "end index of substring out of range, set to string length");
4621 end
= real_length
- 1;
4623 // now extract the substring
4624 string_iterator
iter(*m
);
4626 for (i
= 0; i
< start
; i
++) {
4627 int c
= iter
.get(0);
4628 while (c
== PUSH_GROFF_MODE
4629 || c
== PUSH_COMP_MODE
4630 || c
== POP_GROFFCOMP_MODE
)
4636 for (; i
<= end
; i
++) {
4637 node
*nd
= 0; // pacify compiler
4638 int c
= iter
.get(&nd
);
4639 while (c
== PUSH_GROFF_MODE
4640 || c
== PUSH_COMP_MODE
4641 || c
== POP_GROFFCOMP_MODE
)
4648 mac
.append((unsigned char)c
);
4657 void length_request()
4661 if (ret
.is_null()) {
4671 else if (!tok
.space()) {
4672 error("bad string definition");
4683 while (c
!= '\n' && c
!= EOF
) {
4687 reg
*r
= (reg
*)number_reg_dictionary
.lookup(ret
);
4691 set_number_reg(ret
, len
);
4695 void asciify_macro()
4697 symbol s
= get_name(1);
4699 request_or_macro
*p
= lookup_request(s
);
4700 macro
*m
= p
->to_macro();
4702 error("cannot asciify request");
4705 string_iterator
iter(*m
);
4707 node
*nd
= 0; // pacify compiler
4708 int c
= iter
.get(&nd
);
4722 void unformat_macro()
4724 symbol s
= get_name(1);
4726 request_or_macro
*p
= lookup_request(s
);
4727 macro
*m
= p
->to_macro();
4729 error("cannot unformat request");
4732 string_iterator
iter(*m
);
4734 node
*nd
= 0; // pacify compiler
4735 int c
= iter
.get(&nd
);
4741 if (nd
->set_unformat_flag())
4751 static void interpolate_environment_variable(symbol nm
)
4753 const char *s
= getenv(nm
.contents());
4755 input_stack::push(make_temp_iterator(s
));
4758 void interpolate_number_reg(symbol nm
, int inc
)
4760 reg
*r
= lookup_number_reg(nm
);
4765 input_stack::push(make_temp_iterator(r
->get_string()));
4768 static void interpolate_number_format(symbol nm
)
4770 reg
*r
= (reg
*)number_reg_dictionary
.lookup(nm
);
4772 input_stack::push(make_temp_iterator(r
->get_format()));
4775 static int get_delim_number(units
*n
, unsigned char si
, int prev_value
)
4779 if (start
.delimiter(1)) {
4781 if (get_number(n
, si
, prev_value
)) {
4783 warning(WARN_DELIM
, "closing delimiter does not match");
4790 static int get_delim_number(units
*n
, unsigned char si
)
4794 if (start
.delimiter(1)) {
4796 if (get_number(n
, si
)) {
4798 warning(WARN_DELIM
, "closing delimiter does not match");
4805 static int get_line_arg(units
*n
, unsigned char si
, charinfo
**cp
)
4809 int start_level
= input_stack::get_level();
4810 if (!start
.delimiter(1))
4813 if (get_number(n
, si
)) {
4814 if (tok
.dummy() || tok
.transparent_dummy())
4816 if (!(start
== tok
&& input_stack::get_level() == start_level
)) {
4817 *cp
= tok
.get_char(1);
4820 if (!(start
== tok
&& input_stack::get_level() == start_level
))
4821 warning(WARN_DELIM
, "closing delimiter does not match");
4827 static int read_size(int *x
)
4837 else if (c
== '+') {
4842 int val
= 0; // pacify compiler
4848 // allow an increment either before or after the left parenthesis
4854 else if (c
== '+') {
4869 val
= val
*10 + (c
- '0');
4874 else if (csdigit(c
)) {
4876 if (!inc
&& c
!= '0' && c
< '4') {
4882 val
= val
*10 + (c
- '0');
4886 else if (!tok
.delimiter(1))
4892 ? get_number(&val
, 'z')
4893 : get_number(&val
, 'z', curenv
->get_requested_point_size())))
4895 if (!(start
.ch() == '[' && tok
.ch() == ']') && start
!= tok
) {
4896 if (start
.ch() == '[')
4897 error("missing `]'");
4899 error("missing closing delimiter");
4907 // special case -- \s[0] and \s0 means to revert to previous size
4914 *x
= curenv
->get_requested_point_size() + val
;
4917 *x
= curenv
->get_requested_point_size() - val
;
4924 "\\s request results in non-positive point size; set to 1");
4930 error("bad digit in point size");
4935 static symbol
get_delim_name()
4940 error("end of input at start of delimited name");
4943 if (start
.newline()) {
4944 error("can't delimit name with a newline");
4947 int start_level
= input_stack::get_level();
4948 char abuf
[ABUF_SIZE
];
4950 int buf_size
= ABUF_SIZE
;
4953 if (i
+ 1 > buf_size
) {
4955 buf
= new char[ABUF_SIZE
*2];
4956 memcpy(buf
, abuf
, buf_size
);
4957 buf_size
= ABUF_SIZE
*2;
4960 char *old_buf
= buf
;
4961 buf
= new char[buf_size
*2];
4962 memcpy(buf
, old_buf
, buf_size
);
4969 && (compatible_flag
|| input_stack::get_level() == start_level
))
4971 if ((buf
[i
] = tok
.ch()) == 0) {
4972 error("missing delimiter (got %1)", tok
.description());
4982 error("empty delimited name");
4997 static void do_register()
5001 if (!start
.delimiter(1))
5004 symbol nm
= get_long_name(1);
5009 reg
*r
= (reg
*)number_reg_dictionary
.lookup(nm
);
5011 if (!r
|| !r
->get_value(&prev_value
))
5014 if (!get_number(&val
, 'u', prev_value
))
5017 warning(WARN_DELIM
, "closing delimiter does not match");
5021 set_number_reg(nm
, val
);
5024 // this implements the \w escape sequence
5026 static void do_width()
5030 int start_level
= input_stack::get_level();
5031 environment
env(curenv
);
5032 environment
*oldenv
= curenv
;
5037 warning(WARN_DELIM
, "missing closing delimiter");
5040 if (tok
.newline()) {
5041 warning(WARN_DELIM
, "missing closing delimiter");
5042 input_stack::push(make_temp_iterator("\n"));
5046 && (compatible_flag
|| input_stack::get_level() == start_level
))
5051 units x
= env
.get_input_line_position().to_units();
5052 input_stack::push(make_temp_iterator(i_to_a(x
)));
5053 env
.width_registers();
5058 charinfo
*page_character
;
5060 void set_page_character()
5062 page_character
= get_optional_char();
5066 static const symbol
percent_symbol("%");
5068 void read_title_parts(node
**part
, hunits
*part_width
)
5071 if (tok
.newline() || tok
.eof())
5074 int start_level
= input_stack::get_level();
5076 for (int i
= 0; i
< 3; i
++) {
5077 while (!tok
.newline() && !tok
.eof()) {
5079 && (compatible_flag
|| input_stack::get_level() == start_level
)) {
5083 if (page_character
!= 0 && tok
.get_char() == page_character
)
5084 interpolate_number_reg(percent_symbol
, 0);
5089 curenv
->wrap_up_tab();
5090 part_width
[i
] = curenv
->get_input_line_position();
5091 part
[i
] = curenv
->extract_output_line();
5093 while (!tok
.newline() && !tok
.eof())
5097 class non_interpreted_node
: public node
{
5100 non_interpreted_node(const macro
&);
5101 int interpret(macro
*);
5103 int ends_sentence();
5110 non_interpreted_node::non_interpreted_node(const macro
&m
) : mac(m
)
5114 int non_interpreted_node::ends_sentence()
5119 int non_interpreted_node::same(node
*nd
)
5121 return mac
== ((non_interpreted_node
*)nd
)->mac
;
5124 const char *non_interpreted_node::type()
5126 return "non_interpreted_node";
5129 int non_interpreted_node::force_tprint()
5134 int non_interpreted_node::is_tag()
5139 node
*non_interpreted_node::copy()
5141 return new non_interpreted_node(mac
);
5144 int non_interpreted_node::interpret(macro
*m
)
5146 string_iterator
si(mac
);
5147 node
*n
= 0; // pacify compiler
5160 static node
*do_non_interpreted()
5165 while ((c
= get_copy(&n
)) != ESCAPE_QUESTION
&& c
!= EOF
&& c
!= '\n')
5170 if (c
== EOF
|| c
== '\n') {
5171 error("missing \\?");
5174 return new non_interpreted_node(mac
);
5177 static void encode_char(macro
*mac
, char c
)
5180 if ((font::use_charnames_in_special
) && tok
.special()) {
5181 charinfo
*ci
= tok
.get_char(1);
5182 const char *s
= ci
->get_symbol()->contents();
5183 if (s
[0] != (char)0) {
5187 while (s
[i
] != (char)0) {
5195 else if (tok
.stretchable_space()
5196 || tok
.unstretchable_space())
5198 else if (!(tok
.hyphen_indicator()
5200 || tok
.transparent_dummy()
5201 || tok
.zero_width_break()))
5202 error("%1 is invalid within \\X", tok
.description());
5205 if ((font::use_charnames_in_special
) && (c
== '\\')) {
5207 * add escape escape sequence
5219 int start_level
= input_stack::get_level();
5222 tok
!= start
|| input_stack::get_level() != start_level
;
5225 warning(WARN_DELIM
, "missing closing delimiter");
5228 if (tok
.newline()) {
5229 input_stack::push(make_temp_iterator("\n"));
5230 warning(WARN_DELIM
, "missing closing delimiter");
5238 else if (tok
.leader())
5240 else if (tok
.backspace())
5244 encode_char(&mac
, c
);
5246 return new special_node(mac
);
5249 void output_request()
5251 if (!tok
.newline() && !tok
.eof()) {
5259 if (c
!= ' ' && c
!= '\t')
5262 for (; c
!= '\n' && c
!= EOF
; c
= get_copy(0))
5263 topdiv
->transparent_output(c
);
5264 topdiv
->transparent_output('\n');
5269 extern int image_no
; // from node.cpp
5271 static node
*do_suppress(symbol nm
)
5273 if (nm
.is_null() || nm
.is_empty()) {
5274 error("expecting an argument to escape \\O");
5277 const char *s
= nm
.contents();
5280 if (begin_level
== 0)
5281 // suppress generation of glyphs
5282 return new suppress_node(0, 0);
5285 if (begin_level
== 0)
5286 // enable generation of glyphs
5287 return new suppress_node(1, 0);
5290 if (begin_level
== 0)
5291 return new suppress_node(1, 1);
5301 s
++; // move over '5'
5303 if (*s
== (char)0) {
5304 error("missing position and filename in \\O");
5307 if (!(position
== 'l'
5310 || position
== 'i')) {
5311 error("l, r, c, or i position expected (got %1 in \\O)", position
);
5314 s
++; // onto image name
5315 if (s
== (char *)0) {
5316 error("missing image name for \\O");
5320 if (begin_level
== 0)
5321 return new suppress_node(symbol(s
), position
, image_no
);
5325 error("`%1' is an invalid argument to \\O", *s
);
5330 void special_node::tprint(troff_output_file
*out
)
5333 string_iterator
iter(mac
);
5335 int c
= iter
.get(0);
5338 for (const char *s
= ::asciify(c
); *s
; s
++)
5339 tprint_char(out
, *s
);
5344 int get_file_line(const char **filename
, int *lineno
)
5346 return input_stack::get_location(0, filename
, lineno
);
5352 if (get_integer(&n
)) {
5353 const char *filename
= 0;
5355 symbol s
= get_long_name();
5356 filename
= s
.contents();
5358 (void)input_stack::set_location(filename
, n
-1);
5363 static int nroff_mode
= 0;
5365 static void nroff_request()
5371 static void troff_request()
5377 static void skip_alternative()
5380 // ensure that ``.if 0\{'' works as expected
5381 if (tok
.left_brace())
5385 c
= input_stack::get(0);
5388 if (c
== ESCAPE_LEFT_BRACE
)
5390 else if (c
== ESCAPE_RIGHT_BRACE
)
5392 else if (c
== escape_char
&& escape_char
> 0)
5393 switch(input_stack::get(0)) {
5401 while ((c
= input_stack::get(0)) != '\n' && c
!= EOF
)
5405 Note that the level can properly be < 0, eg
5411 So don't give an error message in this case.
5413 if (level
<= 0 && c
== '\n')
5419 static void begin_alternative()
5421 while (tok
.space() || tok
.left_brace())
5431 static int_stack if_else_stack
;
5438 while (tok
.ch() == '!') {
5443 unsigned char c
= tok
.ch();
5446 result
= !nroff_mode
;
5448 else if (c
== 'n') {
5450 result
= nroff_mode
;
5452 else if (c
== 'v') {
5456 else if (c
== 'o') {
5457 result
= (topdiv
->get_page_number() & 1);
5460 else if (c
== 'e') {
5461 result
= !(topdiv
->get_page_number() & 1);
5464 else if (c
== 'd' || c
== 'r') {
5466 symbol nm
= get_name(1);
5472 ? request_dictionary
.lookup(nm
) != 0
5473 : number_reg_dictionary
.lookup(nm
) != 0);
5475 else if (c
== 'm') {
5477 symbol nm
= get_long_name(1);
5482 result
= (nm
== default_symbol
5483 || color_dictionary
.lookup(nm
) != 0);
5485 else if (c
== 'c') {
5488 charinfo
*ci
= tok
.get_char(1);
5493 result
= character_exists(ci
, curenv
);
5496 else if (c
== 'F') {
5498 symbol nm
= get_long_name(1);
5503 result
= check_font(curenv
->get_family()->nm
, nm
);
5505 else if (c
== 'S') {
5507 symbol nm
= get_long_name(1);
5512 result
= check_style(nm
);
5514 else if (tok
.space())
5516 else if (tok
.delimiter()) {
5518 int delim_level
= input_stack::get_level();
5519 environment
env1(curenv
);
5520 environment
env2(curenv
);
5521 environment
*oldenv
= curenv
;
5524 for (int i
= 0; i
< 2; i
++) {
5527 if (tok
.newline() || tok
.eof()) {
5528 warning(WARN_DELIM
, "missing closing delimiter");
5534 && (compatible_flag
|| input_stack::get_level() == delim_level
))
5540 node
*n1
= env1
.extract_output_line();
5541 node
*n2
= env2
.extract_output_line();
5542 result
= same_node_list(n1
, n2
);
5543 delete_node_list(n1
);
5544 delete_node_list(n2
);
5552 if (!get_number(&n
, 'u')) {
5562 begin_alternative();
5568 void if_else_request()
5570 if_else_stack
.push(do_if_request());
5580 if (if_else_stack
.is_empty()) {
5581 warning(WARN_EL
, "unbalanced .el request");
5585 if (if_else_stack
.pop())
5588 begin_alternative();
5592 static int while_depth
= 0;
5593 static int while_break_flag
= 0;
5595 void while_request()
5600 mac
.append(new token_node(tok
));
5602 node
*n
= 0; // pacify compiler
5603 int c
= input_stack::get(&n
);
5619 if (c
== ESCAPE_LEFT_BRACE
)
5621 else if (c
== ESCAPE_RIGHT_BRACE
)
5623 else if (c
== escape_char
)
5626 if (c
== '\n' && level
<= 0)
5631 error("unbalanced \\{ \\}");
5634 input_stack::add_boundary();
5636 input_stack::push(new string_iterator(mac
, "while loop"));
5638 if (!do_if_request()) {
5639 while (input_stack::get(0) != EOF
)
5643 process_input_stack();
5644 if (while_break_flag
|| input_stack::is_return_boundary()) {
5645 while_break_flag
= 0;
5649 input_stack::remove_boundary();
5655 void while_break_request()
5658 error("no while loop");
5662 while_break_flag
= 1;
5663 while (input_stack::get(0) != EOF
)
5669 void while_continue_request()
5672 error("no while loop");
5676 while (input_stack::get(0) != EOF
)
5686 symbol nm
= get_long_name(1);
5690 while (!tok
.newline() && !tok
.eof())
5693 FILE *fp
= include_search_path
.open_file_cautious(nm
.contents());
5695 input_stack::push(new file_iterator(fp
, nm
.contents()));
5697 error("can't open `%1': %2", nm
.contents(), strerror(errno
));
5702 // like .so but use popen()
5707 error(".pso request not allowed in safer mode");
5711 #ifdef POPEN_MISSING
5712 error("pipes not available on this system");
5714 #else /* not POPEN_MISSING */
5715 if (tok
.newline() || tok
.eof())
5716 error("missing command");
5719 while ((c
= get_copy(0)) == ' ' || c
== '\t')
5722 char *buf
= new char[buf_size
];
5724 for (; c
!= '\n' && c
!= EOF
; c
= get_copy(0)) {
5725 const char *s
= asciify(c
);
5726 int slen
= strlen(s
);
5727 if (buf_used
+ slen
+ 1> buf_size
) {
5728 char *old_buf
= buf
;
5729 int old_buf_size
= buf_size
;
5731 buf
= new char[buf_size
];
5732 memcpy(buf
, old_buf
, old_buf_size
);
5735 strcpy(buf
+ buf_used
, s
);
5738 buf
[buf_used
] = '\0';
5740 FILE *fp
= popen(buf
, POPEN_RT
);
5742 input_stack::push(new file_iterator(fp
, symbol(buf
).contents(), 1));
5744 error("can't open pipe to process `%1': %2", buf
, strerror(errno
));
5748 #endif /* not POPEN_MISSING */
5754 static int llx_reg_contents
= 0;
5755 static int lly_reg_contents
= 0;
5756 static int urx_reg_contents
= 0;
5757 static int ury_reg_contents
= 0;
5759 struct bounding_box
{
5760 int llx
, lly
, urx
, ury
;
5763 /* Parse the argument to a %%BoundingBox comment. Return 1 if it
5764 contains 4 numbers, 2 if it contains (atend), 0 otherwise. */
5766 int parse_bounding_box(char *p
, bounding_box
*bb
)
5768 if (sscanf(p
, "%d %d %d %d",
5769 &bb
->llx
, &bb
->lly
, &bb
->urx
, &bb
->ury
) == 4)
5772 /* The Document Structuring Conventions say that the numbers
5773 should be integers. Unfortunately some broken applications
5775 double x1
, x2
, x3
, x4
;
5776 if (sscanf(p
, "%lf %lf %lf %lf", &x1
, &x2
, &x3
, &x4
) == 4) {
5784 for (; *p
== ' ' || *p
== '\t'; p
++)
5786 if (strncmp(p
, "(atend)", 7) == 0) {
5791 bb
->llx
= bb
->lly
= bb
->urx
= bb
->ury
= 0;
5795 // This version is taken from psrm.cpp
5797 #define PS_LINE_MAX 255
5798 cset
white_space("\n\r \t");
5800 int ps_get_line(char *buf
, FILE *fp
, const char* filename
)
5809 while (c
!= '\r' && c
!= '\n' && c
!= EOF
) {
5810 if ((c
< 0x1b && !white_space(c
)) || c
== 0x7f)
5811 error("invalid input character code %1 in `%2'", int(c
), filename
);
5812 else if (i
< PS_LINE_MAX
)
5816 error("PostScript file `%1' is non-conforming "
5817 "because length of line exceeds 255", filename
);
5825 if (c
!= EOF
&& c
!= '\n')
5831 inline void assign_registers(int llx
, int lly
, int urx
, int ury
)
5833 llx_reg_contents
= llx
;
5834 lly_reg_contents
= lly
;
5835 urx_reg_contents
= urx
;
5836 ury_reg_contents
= ury
;
5839 void do_ps_file(FILE *fp
, const char* filename
)
5843 char buf
[PS_LINE_MAX
];
5844 llx_reg_contents
= lly_reg_contents
=
5845 urx_reg_contents
= ury_reg_contents
= 0;
5846 if (!ps_get_line(buf
, fp
, filename
)) {
5847 error("`%1' is empty", filename
);
5850 if (strncmp("%!PS-Adobe-", buf
, 11) != 0) {
5851 error("`%1' is not conforming to the Document Structuring Conventions",
5855 while (ps_get_line(buf
, fp
, filename
) != 0) {
5856 if (buf
[0] != '%' || buf
[1] != '%'
5857 || strncmp(buf
+ 2, "EndComments", 11) == 0)
5859 if (strncmp(buf
+ 2, "BoundingBox:", 12) == 0) {
5860 int res
= parse_bounding_box(buf
+ 14, &bb
);
5862 assign_registers(bb
.llx
, bb
.lly
, bb
.urx
, bb
.ury
);
5865 else if (res
== 2) {
5870 error("the arguments to the %%%%BoundingBox comment in `%1' are bad",
5879 /* in the trailer, the last BoundingBox comment is significant */
5880 for (offset
= 512; !last_try
; offset
*= 2) {
5881 int had_trailer
= 0;
5883 if (offset
> 32768 || fseek(fp
, -offset
, 2) == -1) {
5885 if (fseek(fp
, 0L, 0) == -1)
5888 while (ps_get_line(buf
, fp
, filename
) != 0) {
5889 if (buf
[0] == '%' && buf
[1] == '%') {
5891 if (strncmp(buf
+ 2, "Trailer", 7) == 0)
5895 if (strncmp(buf
+ 2, "BoundingBox:", 12) == 0) {
5896 int res
= parse_bounding_box(buf
+ 14, &bb
);
5899 else if (res
== 2) {
5900 error("`(atend)' not allowed in trailer of `%1'", filename
);
5904 error("the arguments to the %%%%BoundingBox comment in `%1' are bad",
5913 assign_registers(bb
.llx
, bb
.lly
, bb
.urx
, bb
.ury
);
5918 error("%%%%BoundingBox comment not found in `%1'", filename
);
5921 void ps_bbox_request()
5923 symbol nm
= get_long_name(1);
5927 while (!tok
.newline() && !tok
.eof())
5930 // PS files might contain non-printable characters, such as ^Z
5931 // and CRs not followed by an LF, so open them in binary mode.
5932 FILE *fp
= include_search_path
.open_file_cautious(nm
.contents(),
5935 do_ps_file(fp
, nm
.contents());
5939 error("can't open `%1': %2", nm
.contents(), strerror(errno
));
5944 const char *asciify(int c
)
5947 buf
[0] = escape_char
== '\0' ? '\\' : escape_char
;
5948 buf
[1] = buf
[2] = '\0';
5950 case ESCAPE_QUESTION
:
5953 case ESCAPE_AMPERSAND
:
5956 case ESCAPE_RIGHT_PARENTHESIS
:
5959 case ESCAPE_UNDERSCORE
:
5965 case ESCAPE_CIRCUMFLEX
:
5968 case ESCAPE_LEFT_BRACE
:
5971 case ESCAPE_RIGHT_BRACE
:
5974 case ESCAPE_LEFT_QUOTE
:
5977 case ESCAPE_RIGHT_QUOTE
:
5995 case ESCAPE_PERCENT
:
6007 case PUSH_GROFF_MODE
:
6008 case PUSH_COMP_MODE
:
6009 case POP_GROFFCOMP_MODE
:
6013 if (invalid_input_char(c
))
6022 const char *input_char_description(int c
)
6026 return "a newline character";
6028 return "a backspace character";
6030 return "a leader character";
6032 return "a tab character";
6034 return "a space character";
6038 static char buf
[sizeof("magic character code ") + 1 + INT_DIGITS
];
6039 if (invalid_input_char(c
)) {
6040 const char *s
= asciify(c
);
6047 sprintf(buf
, "magic character code %d", c
);
6056 sprintf(buf
, "character code %d", c
);
6062 if (!tok
.newline() && !tok
.eof()) {
6071 if (c
!= ' ' && c
!= '\t')
6075 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))
6103 curenv
->add_node(new tag_node(s
, 1));
6108 // .tm, .tm1, and .tmc
6110 void do_terminal(int newline
, int string_like
)
6112 if (!tok
.newline() && !tok
.eof()) {
6116 if (string_like
&& c
== '"') {
6120 if (c
!= ' ' && c
!= '\t')
6123 for (; c
!= '\n' && c
!= EOF
; c
= get_copy(0))
6124 fputs(asciify(c
), stderr
);
6127 fputc('\n', stderr
);
6142 void terminal_continue()
6147 dictionary
stream_dictionary(20);
6149 void do_open(int append
)
6151 symbol stream
= get_name(1);
6152 if (!stream
.is_null()) {
6153 symbol filename
= get_long_name(1);
6154 if (!filename
.is_null()) {
6156 FILE *fp
= fopen(filename
.contents(), append
? "a" : "w");
6158 error("can't open `%1' for %2: %3",
6159 filename
.contents(),
6160 append
? "appending" : "writing",
6162 fp
= (FILE *)stream_dictionary
.remove(stream
);
6165 fp
= (FILE *)stream_dictionary
.lookup(stream
, fp
);
6176 error(".open request not allowed in safer mode");
6183 void opena_request()
6186 error(".opena request not allowed in safer mode");
6193 void close_request()
6195 symbol stream
= get_name(1);
6196 if (!stream
.is_null()) {
6197 FILE *fp
= (FILE *)stream_dictionary
.remove(stream
);
6199 error("no stream named `%1'", stream
.contents());
6206 // .write and .writec
6208 void do_write_request(int newline
)
6210 symbol stream
= get_name(1);
6211 if (stream
.is_null()) {
6215 FILE *fp
= (FILE *)stream_dictionary
.lookup(stream
);
6217 error("no stream named `%1'", stream
.contents());
6222 while ((c
= get_copy(0)) == ' ')
6226 for (; c
!= '\n' && c
!= EOF
; c
= get_copy(0))
6227 fputs(asciify(c
), fp
);
6234 void write_request()
6236 do_write_request(1);
6239 void write_request_continue()
6241 do_write_request(0);
6244 void write_macro_request()
6246 symbol stream
= get_name(1);
6247 if (stream
.is_null()) {
6251 FILE *fp
= (FILE *)stream_dictionary
.lookup(stream
);
6253 error("no stream named `%1'", stream
.contents());
6257 symbol s
= get_name(1);
6262 request_or_macro
*p
= lookup_request(s
);
6263 macro
*m
= p
->to_macro();
6265 error("cannot write request");
6267 string_iterator
iter(*m
);
6269 int c
= iter
.get(0);
6272 fputs(asciify(c
), fp
);
6279 void warnscale_request()
6286 warn_scale
= (double)units_per_inch
;
6288 warn_scale
= (double)units_per_inch
/ 2.54;
6290 warn_scale
= (double)units_per_inch
/ 72.0;
6292 warn_scale
= (double)units_per_inch
/ 6.0;
6295 "invalid scaling indicator `%1', using `i' instead", c
);
6298 warn_scaling_indicator
= c
;
6303 void spreadwarn_request()
6306 if (has_arg() && get_hunits(&n
, 'm')) {
6309 hunits em
= curenv
->get_size();
6310 spread_limit
= (double)n
.to_units()
6311 / (em
.is_zero() ? hresolution
: em
.to_units());
6314 spread_limit
= -spread_limit
- 1; // no arg toggles on/off without
6315 // changing value; we mirror at
6316 // -0.5 to make zero a valid value
6320 static void init_charset_table()
6323 strcpy(buf
, "char");
6324 for (int i
= 0; i
< 256; i
++) {
6325 strcpy(buf
+ 4, i_to_a(i
));
6326 charset_table
[i
] = get_charinfo(symbol(buf
));
6327 charset_table
[i
]->set_ascii_code(i
);
6329 charset_table
[i
]->set_hyphenation_code(cmlower(i
));
6331 charset_table
['.']->set_flags(charinfo::ENDS_SENTENCE
);
6332 charset_table
['?']->set_flags(charinfo::ENDS_SENTENCE
);
6333 charset_table
['!']->set_flags(charinfo::ENDS_SENTENCE
);
6334 charset_table
['-']->set_flags(charinfo::BREAK_AFTER
);
6335 charset_table
['"']->set_flags(charinfo::TRANSPARENT
);
6336 charset_table
['\'']->set_flags(charinfo::TRANSPARENT
);
6337 charset_table
[')']->set_flags(charinfo::TRANSPARENT
);
6338 charset_table
[']']->set_flags(charinfo::TRANSPARENT
);
6339 charset_table
['*']->set_flags(charinfo::TRANSPARENT
);
6340 get_charinfo(symbol("dg"))->set_flags(charinfo::TRANSPARENT
);
6341 get_charinfo(symbol("rq"))->set_flags(charinfo::TRANSPARENT
);
6342 get_charinfo(symbol("em"))->set_flags(charinfo::BREAK_AFTER
);
6343 get_charinfo(symbol("ul"))->set_flags(charinfo::OVERLAPS_HORIZONTALLY
);
6344 get_charinfo(symbol("rn"))->set_flags(charinfo::OVERLAPS_HORIZONTALLY
);
6345 get_charinfo(symbol("radicalex"))->set_flags(charinfo::OVERLAPS_HORIZONTALLY
);
6346 get_charinfo(symbol("sqrtex"))->set_flags(charinfo::OVERLAPS_HORIZONTALLY
);
6347 get_charinfo(symbol("ru"))->set_flags(charinfo::OVERLAPS_HORIZONTALLY
);
6348 get_charinfo(symbol("br"))->set_flags(charinfo::OVERLAPS_VERTICALLY
);
6349 page_character
= charset_table
['%'];
6352 static void init_hpf_code_table()
6354 for (int i
= 0; i
< 256; i
++)
6355 hpf_code_table
[i
] = i
;
6358 static void do_translate(int translate_transparent
, int translate_input
)
6361 while (!tok
.newline() && !tok
.eof()) {
6363 // This is a really bizarre troff feature.
6365 translate_space_to_dummy
= tok
.dummy();
6366 if (tok
.newline() || tok
.eof())
6371 charinfo
*ci1
= tok
.get_char(1);
6375 if (tok
.newline() || tok
.eof()) {
6376 ci1
->set_special_translation(charinfo::TRANSLATE_SPACE
,
6377 translate_transparent
);
6381 ci1
->set_special_translation(charinfo::TRANSLATE_SPACE
,
6382 translate_transparent
);
6383 else if (tok
.stretchable_space())
6384 ci1
->set_special_translation(charinfo::TRANSLATE_STRETCHABLE_SPACE
,
6385 translate_transparent
);
6386 else if (tok
.dummy())
6387 ci1
->set_special_translation(charinfo::TRANSLATE_DUMMY
,
6388 translate_transparent
);
6389 else if (tok
.hyphen_indicator())
6390 ci1
->set_special_translation(charinfo::TRANSLATE_HYPHEN_INDICATOR
,
6391 translate_transparent
);
6393 charinfo
*ci2
= tok
.get_char(1);
6397 ci1
->set_translation(0, translate_transparent
, translate_input
);
6399 ci1
->set_translation(ci2
, translate_transparent
, translate_input
);
6411 void translate_no_transparent()
6416 void translate_input()
6424 if (get_integer(&flags
))
6426 charinfo
*ci
= tok
.get_char(1);
6428 charinfo
*tem
= ci
->get_translation();
6431 ci
->set_flags(flags
);
6438 void hyphenation_code()
6441 while (!tok
.newline() && !tok
.eof()) {
6442 charinfo
*ci
= tok
.get_char(1);
6447 unsigned char c
= tok
.ch();
6449 error("hyphenation code must be ordinary character");
6453 error("hyphenation code cannot be digit");
6456 ci
->set_hyphenation_code(c
);
6457 if (ci
->get_translation()
6458 && ci
->get_translation()->get_translation_input())
6459 ci
->get_translation()->set_hyphenation_code(c
);
6466 void hyphenation_patterns_file_code()
6469 while (!tok
.newline() && !tok
.eof()) {
6471 if (get_integer(&n1
) && (0 <= n1
&& n1
<= 255)) {
6473 error("missing output hyphenation code");
6476 if (get_integer(&n2
) && (0 <= n2
&& n2
<= 255)) {
6477 hpf_code_table
[n1
] = n2
;
6481 error("output hyphenation code must be integer in the range 0..255");
6486 error("input hyphenation code must be integer in the range 0..255");
6493 charinfo
*token::get_char(int required
)
6495 if (type
== TOKEN_CHAR
)
6496 return charset_table
[c
];
6497 if (type
== TOKEN_SPECIAL
)
6498 return get_charinfo(nm
);
6499 if (type
== TOKEN_NUMBERED_CHAR
)
6500 return get_charinfo_by_number(val
);
6501 if (type
== TOKEN_ESCAPE
) {
6502 if (escape_char
!= 0)
6503 return charset_table
[escape_char
];
6505 error("`\\e' used while no current escape character");
6510 if (type
== TOKEN_EOF
|| type
== TOKEN_NEWLINE
)
6511 warning(WARN_MISSING
, "missing normal or special character");
6513 error("normal or special character expected (got %1)", description());
6518 charinfo
*get_optional_char()
6522 charinfo
*ci
= tok
.get_char();
6524 check_missing_character();
6530 void check_missing_character()
6532 if (!tok
.newline() && !tok
.eof() && !tok
.right_brace() && !tok
.tab())
6533 error("normal or special character expected (got %1): "
6534 "treated as missing",
6540 int token::add_to_node_list(node
**pp
)
6547 *pp
= (*pp
)->add_char(charset_table
[c
], curenv
, &w
, &s
);
6553 if (escape_char
!= 0)
6554 *pp
= (*pp
)->add_char(charset_table
[escape_char
], curenv
, &w
, &s
);
6556 case TOKEN_HYPHEN_INDICATOR
:
6557 *pp
= (*pp
)->add_discretionary_hyphen();
6559 case TOKEN_ITALIC_CORRECTION
:
6560 *pp
= (*pp
)->add_italic_correction(&w
);
6562 case TOKEN_LEFT_BRACE
:
6564 case TOKEN_MARK_INPUT
:
6565 set_number_reg(nm
, curenv
->get_input_line_position().to_units());
6571 case TOKEN_NUMBERED_CHAR
:
6572 *pp
= (*pp
)->add_char(get_charinfo_by_number(val
), curenv
, &w
, &s
);
6574 case TOKEN_RIGHT_BRACE
:
6577 n
= new hmotion_node(curenv
->get_space_width(),
6578 curenv
->get_fill_color());
6581 *pp
= (*pp
)->add_char(get_charinfo(nm
), curenv
, &w
, &s
);
6583 case TOKEN_STRETCHABLE_SPACE
:
6584 n
= new unbreakable_space_node(curenv
->get_space_width(),
6585 curenv
->get_fill_color());
6587 case TOKEN_UNSTRETCHABLE_SPACE
:
6588 n
= new space_char_hmotion_node(curenv
->get_space_width(),
6589 curenv
->get_fill_color());
6591 case TOKEN_TRANSPARENT_DUMMY
:
6592 n
= new transparent_dummy_node
;
6594 case TOKEN_ZERO_WIDTH_BREAK
:
6595 n
= new space_node(H0
, curenv
->get_fill_color());
6597 n
->is_escape_colon();
6609 void token::process()
6611 if (possibly_handle_first_page_transition())
6614 case TOKEN_BACKSPACE
:
6615 curenv
->add_node(new hmotion_node(-curenv
->get_space_width(),
6616 curenv
->get_fill_color()));
6619 curenv
->add_char(charset_table
[c
]);
6622 curenv
->add_node(new dummy_node
);
6631 if (escape_char
!= 0)
6632 curenv
->add_char(charset_table
[escape_char
]);
6634 case TOKEN_BEGIN_TRAP
:
6635 case TOKEN_END_TRAP
:
6636 case TOKEN_PAGE_EJECTOR
:
6637 // these are all handled in process_input_stack()
6639 case TOKEN_HYPHEN_INDICATOR
:
6640 curenv
->add_hyphen_indicator();
6642 case TOKEN_INTERRUPT
:
6643 curenv
->interrupt();
6645 case TOKEN_ITALIC_CORRECTION
:
6646 curenv
->add_italic_correction();
6649 curenv
->handle_tab(1);
6651 case TOKEN_LEFT_BRACE
:
6653 case TOKEN_MARK_INPUT
:
6654 set_number_reg(nm
, curenv
->get_input_line_position().to_units());
6660 curenv
->add_node(nd
);
6663 case TOKEN_NUMBERED_CHAR
:
6664 curenv
->add_char(get_charinfo_by_number(val
));
6667 // handled in process_input_stack()
6669 case TOKEN_RIGHT_BRACE
:
6675 curenv
->add_char(get_charinfo(nm
));
6680 case TOKEN_STRETCHABLE_SPACE
:
6681 curenv
->add_node(new unbreakable_space_node(curenv
->get_space_width(),
6682 curenv
->get_fill_color()));
6684 case TOKEN_UNSTRETCHABLE_SPACE
:
6685 curenv
->add_node(new space_char_hmotion_node(curenv
->get_space_width(),
6686 curenv
->get_fill_color()));
6689 curenv
->handle_tab(0);
6691 case TOKEN_TRANSPARENT
:
6693 case TOKEN_TRANSPARENT_DUMMY
:
6694 curenv
->add_node(new transparent_dummy_node
);
6696 case TOKEN_ZERO_WIDTH_BREAK
:
6698 node
*tmp
= new space_node(H0
, curenv
->get_fill_color());
6699 tmp
->freeze_space();
6700 tmp
->is_escape_colon();
6701 curenv
->add_node(tmp
);
6709 class nargs_reg
: public reg
{
6711 const char *get_string();
6714 const char *nargs_reg::get_string()
6716 return i_to_a(input_stack::nargs());
6719 class lineno_reg
: public reg
{
6721 const char *get_string();
6724 const char *lineno_reg::get_string()
6728 if (!input_stack::get_location(0, &file
, &line
))
6730 return i_to_a(line
);
6733 class writable_lineno_reg
: public general_reg
{
6735 writable_lineno_reg();
6736 void set_value(units
);
6737 int get_value(units
*);
6740 writable_lineno_reg::writable_lineno_reg()
6744 int writable_lineno_reg::get_value(units
*res
)
6748 if (!input_stack::get_location(0, &file
, &line
))
6754 void writable_lineno_reg::set_value(units n
)
6756 input_stack::set_location(0, n
);
6759 class filename_reg
: public reg
{
6761 const char *get_string();
6764 const char *filename_reg::get_string()
6768 if (input_stack::get_location(0, &file
, &line
))
6774 class constant_reg
: public reg
{
6777 constant_reg(const char *);
6778 const char *get_string();
6781 constant_reg::constant_reg(const char *p
) : s(p
)
6785 const char *constant_reg::get_string()
6790 constant_int_reg::constant_int_reg(int *q
) : p(q
)
6794 const char *constant_int_reg::get_string()
6799 void abort_request()
6804 else if (tok
.newline())
6807 while ((c
= get_copy(0)) == ' ')
6810 if (c
== EOF
|| c
== '\n')
6811 fputs("User Abort.", stderr
);
6813 for (; c
!= '\n' && c
!= EOF
; c
= get_copy(0))
6814 fputs(asciify(c
), stderr
);
6816 fputc('\n', stderr
);
6817 cleanup_and_exit(1);
6823 char *s
= new char[len
];
6825 while ((c
= get_copy(0)) == ' ')
6828 while (c
!= '\n' && c
!= EOF
) {
6829 if (!invalid_input_char(c
)) {
6832 s
= new char[len
*2];
6833 memcpy(s
, tem
, len
);
6853 error(".pi request not allowed in safer mode");
6857 #ifdef POPEN_MISSING
6858 error("pipes not available on this system");
6860 #else /* not POPEN_MISSING */
6862 error("can't pipe: output already started");
6867 if ((pc
= read_string()) == 0)
6868 error("can't pipe to empty command");
6870 char *s
= new char[strlen(pipe_command
) + strlen(pc
) + 1 + 1];
6871 strcpy(s
, pipe_command
);
6874 a_delete pipe_command
;
6881 #endif /* not POPEN_MISSING */
6885 static int system_status
;
6887 void system_request()
6890 error(".sy request not allowed in safer mode");
6894 char *command
= read_string();
6896 error("empty command");
6898 system_status
= system(command
);
6906 if (curdiv
== topdiv
&& topdiv
->before_first_page
) {
6907 handle_initial_request(COPY_FILE_REQUEST
);
6910 symbol filename
= get_long_name(1);
6911 while (!tok
.newline() && !tok
.eof())
6915 if (!filename
.is_null())
6916 curdiv
->copy_file(filename
.contents());
6924 if (curdiv
== topdiv
&& topdiv
->before_first_page
) {
6925 handle_initial_request(VJUSTIFY_REQUEST
);
6928 symbol type
= get_long_name(1);
6929 if (!type
.is_null())
6930 curdiv
->vjustify(type
);
6936 void transparent_file()
6938 if (curdiv
== topdiv
&& topdiv
->before_first_page
) {
6939 handle_initial_request(TRANSPARENT_FILE_REQUEST
);
6942 symbol filename
= get_long_name(1);
6943 while (!tok
.newline() && !tok
.eof())
6947 if (!filename
.is_null()) {
6949 FILE *fp
= include_search_path
.open_file_cautious(filename
.contents());
6951 error("can't open `%1': %2", filename
.contents(), strerror(errno
));
6958 if (invalid_input_char(c
))
6959 warning(WARN_INPUT
, "invalid input character code %1", int(c
));
6961 curdiv
->transparent_output(c
);
6966 curdiv
->transparent_output('\n');
6978 page_range(int, int, page_range
*);
6979 int contains(int n
);
6982 page_range::page_range(int i
, int j
, page_range
*p
)
6983 : first(i
), last(j
), next(p
)
6987 int page_range::contains(int n
)
6989 return n
>= first
&& (last
<= 0 || n
<= last
);
6992 page_range
*output_page_list
= 0;
6994 int in_output_page_list(int n
)
6996 if (!output_page_list
)
6998 for (page_range
*p
= output_page_list
; p
; p
= p
->next
)
7004 static void parse_output_page_list(char *p
)
7010 else if (csdigit(*p
)) {
7013 i
= i
*10 + *p
++ - '0';
7014 while (csdigit(*p
));
7024 j
= j
*10 + *p
++ - '0';
7025 while (csdigit(*p
));
7031 last_page_number
= -1;
7032 else if (last_page_number
>= 0 && j
> last_page_number
)
7033 last_page_number
= j
;
7034 output_page_list
= new page_range(i
, j
, output_page_list
);
7040 error("bad output page list");
7041 output_page_list
= 0;
7045 static FILE *open_mac_file(const char *mac
, char **path
)
7047 // Try first FOOBAR.tmac, then tmac.FOOBAR
7048 char *s1
= new char[strlen(mac
)+strlen(MACRO_POSTFIX
)+1];
7050 strcat(s1
, MACRO_POSTFIX
);
7051 FILE *fp
= mac_path
->open_file(s1
, path
);
7054 char *s2
= new char[strlen(mac
)+strlen(MACRO_PREFIX
)+1];
7055 strcpy(s2
, MACRO_PREFIX
);
7057 fp
= mac_path
->open_file(s2
, path
);
7063 static void process_macro_file(const char *mac
)
7066 FILE *fp
= open_mac_file(mac
, &path
);
7068 fatal("can't find macro file %1", mac
);
7069 const char *s
= symbol(path
).contents();
7071 input_stack::push(new file_iterator(fp
, s
));
7073 process_input_stack();
7076 static void process_startup_file(const char *filename
)
7079 search_path
*orig_mac_path
= mac_path
;
7080 mac_path
= &config_macro_path
;
7081 FILE *fp
= mac_path
->open_file(filename
, &path
);
7083 input_stack::push(new file_iterator(fp
, symbol(path
).contents()));
7086 process_input_stack();
7088 mac_path
= orig_mac_path
;
7093 symbol nm
= get_long_name(1);
7097 while (!tok
.newline() && !tok
.eof())
7100 FILE *fp
= mac_path
->open_file(nm
.contents(), &path
);
7101 // .mso doesn't (and cannot) go through open_mac_file, so we
7102 // need to do it here manually: If we have tmac.FOOBAR, try
7103 // FOOBAR.tmac and vice versa
7105 const char *fn
= nm
.contents();
7106 if (strncasecmp(fn
, MACRO_PREFIX
, sizeof(MACRO_PREFIX
) - 1) == 0) {
7107 char *s
= new char[strlen(fn
) + sizeof(MACRO_POSTFIX
)];
7108 strcpy(s
, fn
+ sizeof(MACRO_PREFIX
) - 1);
7109 strcat(s
, MACRO_POSTFIX
);
7110 fp
= mac_path
->open_file(s
, &path
);
7114 if (strncasecmp(fn
+ strlen(fn
) - sizeof(MACRO_POSTFIX
) + 1,
7115 MACRO_POSTFIX
, sizeof(MACRO_POSTFIX
) - 1) == 0) {
7116 char *s
= new char[strlen(fn
) + sizeof(MACRO_PREFIX
)];
7117 strcpy(s
, MACRO_PREFIX
);
7118 strncat(s
, fn
, strlen(fn
) - sizeof(MACRO_POSTFIX
) + 1);
7119 fp
= mac_path
->open_file(s
, &path
);
7125 input_stack::push(new file_iterator(fp
, symbol(path
).contents()));
7129 error("can't find macro file `%1'", nm
.contents());
7134 static void process_input_file(const char *name
)
7137 if (strcmp(name
, "-") == 0) {
7143 fp
= include_search_path
.open_file_cautious(name
);
7145 fatal("can't open `%1': %2", name
, strerror(errno
));
7147 input_stack::push(new file_iterator(fp
, name
));
7149 process_input_stack();
7152 // make sure the_input is empty before calling this
7154 static int evaluate_expression(const char *expr
, units
*res
)
7156 input_stack::push(make_temp_iterator(expr
));
7158 int success
= get_number(res
, 'u');
7159 while (input_stack::get(0) != EOF
)
7164 static void do_register_assignment(const char *s
)
7166 const char *p
= strchr(s
, '=');
7172 if (evaluate_expression(s
+ 1, &n
))
7173 set_number_reg(buf
, n
);
7176 char *buf
= new char[p
- s
+ 1];
7177 memcpy(buf
, s
, p
- s
);
7180 if (evaluate_expression(p
+ 1, &n
))
7181 set_number_reg(buf
, n
);
7186 static void set_string(const char *name
, const char *value
)
7188 macro
*m
= new macro
;
7189 for (const char *p
= value
; *p
; p
++)
7190 if (!invalid_input_char((unsigned char)*p
))
7192 request_dictionary
.define(name
, m
);
7195 static void do_string_assignment(const char *s
)
7197 const char *p
= strchr(s
, '=');
7202 set_string(buf
, s
+ 1);
7205 char *buf
= new char[p
- s
+ 1];
7206 memcpy(buf
, s
, p
- s
);
7208 set_string(buf
, p
+ 1);
7213 struct string_list
{
7216 string_list(const char *ss
) : s(ss
), next(0) {}
7220 static void prepend_string(const char *s
, string_list
**p
)
7222 string_list
*l
= new string_list(s
);
7228 static void add_string(const char *s
, string_list
**p
)
7232 *p
= new string_list(s
);
7235 void usage(FILE *stream
, const char *prog
)
7238 "usage: %s -abcivzCERU -wname -Wname -dcs -ffam -mname -nnum -olist\n"
7239 " -rcn -Tname -Fdir -Idir -Mdir [files...]\n",
7243 int main(int argc
, char **argv
)
7245 program_name
= argv
[0];
7246 static char stderr_buf
[BUFSIZ
];
7247 setbuf(stderr
, stderr_buf
);
7249 string_list
*macros
= 0;
7250 string_list
*register_assignments
= 0;
7251 string_list
*string_assignments
= 0;
7256 int no_rc
= 0; // don't process troffrc and troffrc-end
7257 int next_page_number
= 0; // pacify compiler
7259 hresolution
= vresolution
= 1;
7260 // restore $PATH if called from groff
7261 char* groff_path
= getenv("GROFF_PATH__");
7268 if (putenv(strsave(e
.contents())))
7269 fatal("putenv failed");
7271 static const struct option long_options
[] = {
7272 { "help", no_argument
, 0, CHAR_MAX
+ 1 },
7273 { "version", no_argument
, 0, 'v' },
7276 #if defined(DEBUGGING)
7277 #define DEBUG_OPTION "D"
7279 while ((c
= getopt_long(argc
, argv
,
7280 "abciI:vw:W:zCEf:m:n:o:r:d:F:M:T:tqs:RU"
7281 DEBUG_OPTION
, long_options
, 0))
7286 printf("GNU troff (groff) version %s\n", Version_string
);
7291 // Search path for .psbb files
7292 // and most other non-system input files.
7293 include_search_path
.command_line_dir(optarg
);
7298 is_html
= (strcmp(device
, "html") == 0);
7301 compatible_flag
= 1;
7307 macro_path
.command_line_dir(optarg
);
7308 safer_macro_path
.command_line_dir(optarg
);
7309 config_macro_path
.command_line_dir(optarg
);
7312 font::command_line_font_dir(optarg
);
7315 add_string(optarg
, ¯os
);
7324 enable_warning(optarg
);
7327 disable_warning(optarg
);
7336 ascii_output_flag
= 1;
7339 suppress_output_flag
= 1;
7342 if (sscanf(optarg
, "%d", &next_page_number
) == 1)
7345 error("bad page number");
7348 parse_output_page_list(optarg
);
7351 if (*optarg
== '\0')
7352 error("`-d' requires non-empty argument");
7354 add_string(optarg
, &string_assignments
);
7357 if (*optarg
== '\0')
7358 error("`-r' requires non-empty argument");
7360 add_string(optarg
, ®ister_assignments
);
7363 default_family
= symbol(optarg
);
7369 // silently ignore these
7372 safer_flag
= 0; // unsafe behaviour
7374 #if defined(DEBUGGING)
7379 case CHAR_MAX
+ 1: // --help
7380 usage(stdout
, argv
[0]);
7384 usage(stderr
, argv
[0]);
7386 break; // never reached
7391 mac_path
= ¯o_path
;
7392 set_string(".T", device
);
7393 init_charset_table();
7394 init_hpf_code_table();
7395 if (!font::load_desc())
7396 fatal("sorry, I can't continue");
7397 units_per_inch
= font::res
;
7398 hresolution
= font::hor
;
7399 vresolution
= font::vert
;
7400 sizescale
= font::sizescale
;
7401 tcommand_flag
= font::tcommand
;
7402 warn_scale
= (double)units_per_inch
;
7403 warn_scaling_indicator
= 'i';
7404 if (!fflag
&& font::family
!= 0 && *font::family
!= '\0')
7405 default_family
= symbol(font::family
);
7406 font_size::init_size_table(font::sizes
);
7409 if (font::style_table
) {
7410 for (i
= 0; font::style_table
[i
]; i
++)
7411 mount_style(j
++, symbol(font::style_table
[i
]));
7413 for (i
= 0; font::font_name_table
[i
]; i
++, j
++)
7414 // In the DESC file a font name of 0 (zero) means leave this
7416 if (strcmp(font::font_name_table
[i
], "0") != 0)
7417 mount_font(j
, symbol(font::font_name_table
[i
]));
7418 curdiv
= topdiv
= new top_level_diversion
;
7420 topdiv
->set_next_page_number(next_page_number
);
7421 init_input_requests();
7422 init_env_requests();
7423 init_div_requests();
7425 init_column_requests();
7427 init_node_requests();
7428 number_reg_dictionary
.define(".T", new constant_reg(tflag
? "1" : "0"));
7430 init_reg_requests();
7431 init_hyphen_requests();
7432 init_environments();
7433 while (string_assignments
) {
7434 do_string_assignment(string_assignments
->s
);
7435 string_list
*tem
= string_assignments
;
7436 string_assignments
= string_assignments
->next
;
7439 while (register_assignments
) {
7440 do_register_assignment(register_assignments
->s
);
7441 string_list
*tem
= register_assignments
;
7442 register_assignments
= register_assignments
->next
;
7446 process_startup_file(INITIAL_STARTUP_FILE
);
7448 process_macro_file(macros
->s
);
7449 string_list
*tem
= macros
;
7450 macros
= macros
->next
;
7454 process_startup_file(FINAL_STARTUP_FILE
);
7455 for (i
= optind
; i
< argc
; i
++)
7456 process_input_file(argv
[i
]);
7457 if (optind
>= argc
|| iflag
)
7458 process_input_file("-");
7460 return 0; // not reached
7466 if (has_arg() && get_integer(&n
)) {
7467 if (n
& ~WARN_TOTAL
) {
7468 warning(WARN_RANGE
, "warning mask must be between 0 and %1", WARN_TOTAL
);
7474 warning_mask
= WARN_TOTAL
;
7478 static void init_registers()
7480 #ifdef LONG_FOR_TIME_T
7482 #else /* not LONG_FOR_TIME_T */
7484 #endif /* not LONG_FOR_TIME_T */
7486 // Use struct here to work around misfeature in old versions of g++.
7487 struct tm
*tt
= localtime(&t
);
7488 set_number_reg("seconds", int(tt
->tm_sec
));
7489 set_number_reg("minutes", int(tt
->tm_min
));
7490 set_number_reg("hours", int(tt
->tm_hour
));
7491 set_number_reg("dw", int(tt
->tm_wday
+ 1));
7492 set_number_reg("dy", int(tt
->tm_mday
));
7493 set_number_reg("mo", int(tt
->tm_mon
+ 1));
7494 set_number_reg("year", int(1900 + tt
->tm_year
));
7495 set_number_reg("yr", int(tt
->tm_year
));
7496 set_number_reg("$$", getpid());
7497 number_reg_dictionary
.define(".A",
7498 new constant_reg(ascii_output_flag
7504 * registers associated with \O
7507 static int output_reg_minx_contents
= -1;
7508 static int output_reg_miny_contents
= -1;
7509 static int output_reg_maxx_contents
= -1;
7510 static int output_reg_maxy_contents
= -1;
7512 void check_output_limits(int x
, int y
)
7514 if ((output_reg_minx_contents
== -1) || (x
< output_reg_minx_contents
))
7515 output_reg_minx_contents
= x
;
7516 if (x
> output_reg_maxx_contents
)
7517 output_reg_maxx_contents
= x
;
7518 if ((output_reg_miny_contents
== -1) || (y
< output_reg_miny_contents
))
7519 output_reg_miny_contents
= y
;
7520 if (y
> output_reg_maxy_contents
)
7521 output_reg_maxy_contents
= y
;
7524 void reset_output_registers()
7526 output_reg_minx_contents
= -1;
7527 output_reg_miny_contents
= -1;
7528 output_reg_maxx_contents
= -1;
7529 output_reg_maxy_contents
= -1;
7532 void get_output_registers(int *minx
, int *miny
, int *maxx
, int *maxy
)
7534 *minx
= output_reg_minx_contents
;
7535 *miny
= output_reg_miny_contents
;
7536 *maxx
= output_reg_maxx_contents
;
7537 *maxy
= output_reg_maxy_contents
;
7540 void init_input_requests()
7542 init_request("ab", abort_request
);
7543 init_request("als", alias_macro
);
7544 init_request("am", append_macro
);
7545 init_request("am1", append_nocomp_macro
);
7546 init_request("ami", append_indirect_macro
);
7547 init_request("ami1", append_indirect_nocomp_macro
);
7548 init_request("as", append_string
);
7549 init_request("as1", append_nocomp_string
);
7550 init_request("asciify", asciify_macro
);
7551 init_request("backtrace", backtrace_request
);
7552 init_request("blm", blank_line_macro
);
7553 init_request("break", while_break_request
);
7554 init_request("cf", copy_file
);
7555 init_request("cflags", char_flags
);
7556 init_request("char", define_character
);
7557 init_request("chop", chop_macro
);
7558 init_request("close", close_request
);
7559 init_request("color", activate_color
);
7560 init_request("composite", composite_request
);
7561 init_request("continue", while_continue_request
);
7562 init_request("cp", compatible
);
7563 init_request("de", define_macro
);
7564 init_request("de1", define_nocomp_macro
);
7565 init_request("defcolor", define_color
);
7566 init_request("dei", define_indirect_macro
);
7567 init_request("dei1", define_indirect_nocomp_macro
);
7568 init_request("do", do_request
);
7569 init_request("ds", define_string
);
7570 init_request("ds1", define_nocomp_string
);
7571 init_request("ec", set_escape_char
);
7572 init_request("ecr", restore_escape_char
);
7573 init_request("ecs", save_escape_char
);
7574 init_request("el", else_request
);
7575 init_request("em", end_macro
);
7576 init_request("eo", escape_off
);
7577 init_request("ex", exit_request
);
7578 init_request("fchar", define_fallback_character
);
7579 #ifdef WIDOW_CONTROL
7580 init_request("fpl", flush_pending_lines
);
7581 #endif /* WIDOW_CONTROL */
7582 init_request("hcode", hyphenation_code
);
7583 init_request("hpfcode", hyphenation_patterns_file_code
);
7584 init_request("ie", if_else_request
);
7585 init_request("if", if_request
);
7586 init_request("ig", ignore
);
7587 init_request("length", length_request
);
7588 init_request("lf", line_file
);
7589 init_request("mso", macro_source
);
7590 init_request("nop", nop_request
);
7591 init_request("nroff", nroff_request
);
7592 init_request("nx", next_file
);
7593 init_request("open", open_request
);
7594 init_request("opena", opena_request
);
7595 init_request("output", output_request
);
7596 init_request("pc", set_page_character
);
7597 init_request("pi", pipe_output
);
7598 init_request("pm", print_macros
);
7599 init_request("psbb", ps_bbox_request
);
7600 #ifndef POPEN_MISSING
7601 init_request("pso", pipe_source
);
7602 #endif /* not POPEN_MISSING */
7603 init_request("rchar", remove_character
);
7604 init_request("rd", read_request
);
7605 init_request("return", return_macro_request
);
7606 init_request("rm", remove_macro
);
7607 init_request("rn", rename_macro
);
7608 init_request("schar", define_special_character
);
7609 init_request("shift", shift
);
7610 init_request("so", source
);
7611 init_request("spreadwarn", spreadwarn_request
);
7612 init_request("substring", substring_request
);
7613 init_request("sy", system_request
);
7614 init_request("tag", tag
);
7615 init_request("taga", taga
);
7616 init_request("tm", terminal
);
7617 init_request("tm1", terminal1
);
7618 init_request("tmc", terminal_continue
);
7619 init_request("tr", translate
);
7620 init_request("trf", transparent_file
);
7621 init_request("trin", translate_input
);
7622 init_request("trnt", translate_no_transparent
);
7623 init_request("troff", troff_request
);
7624 init_request("unformat", unformat_macro
);
7626 init_request("vj", vjustify
);
7628 init_request("warn", warn_request
);
7629 init_request("warnscale", warnscale_request
);
7630 init_request("while", while_request
);
7631 init_request("write", write_request
);
7632 init_request("writec", write_request_continue
);
7633 init_request("writem", write_macro_request
);
7634 number_reg_dictionary
.define(".$", new nargs_reg
);
7635 number_reg_dictionary
.define(".C", new constant_int_reg(&compatible_flag
));
7636 number_reg_dictionary
.define(".c", new lineno_reg
);
7637 number_reg_dictionary
.define(".color", new constant_int_reg(&color_flag
));
7638 number_reg_dictionary
.define(".F", new filename_reg
);
7639 number_reg_dictionary
.define(".g", new constant_reg("1"));
7640 number_reg_dictionary
.define(".H", new constant_int_reg(&hresolution
));
7641 number_reg_dictionary
.define(".R", new constant_reg("10000"));
7642 number_reg_dictionary
.define(".U", new constant_int_reg(&safer_flag
));
7643 number_reg_dictionary
.define(".V", new constant_int_reg(&vresolution
));
7644 number_reg_dictionary
.define(".warn", new constant_int_reg(&warning_mask
));
7645 extern const char *major_version
;
7646 number_reg_dictionary
.define(".x", new constant_reg(major_version
));
7647 extern const char *revision
;
7648 number_reg_dictionary
.define(".Y", new constant_reg(revision
));
7649 extern const char *minor_version
;
7650 number_reg_dictionary
.define(".y", new constant_reg(minor_version
));
7651 number_reg_dictionary
.define("c.", new writable_lineno_reg
);
7652 number_reg_dictionary
.define("llx", new variable_reg(&llx_reg_contents
));
7653 number_reg_dictionary
.define("lly", new variable_reg(&lly_reg_contents
));
7654 number_reg_dictionary
.define("opmaxx",
7655 new variable_reg(&output_reg_maxx_contents
));
7656 number_reg_dictionary
.define("opmaxy",
7657 new variable_reg(&output_reg_maxy_contents
));
7658 number_reg_dictionary
.define("opminx",
7659 new variable_reg(&output_reg_minx_contents
));
7660 number_reg_dictionary
.define("opminy",
7661 new variable_reg(&output_reg_miny_contents
));
7662 number_reg_dictionary
.define("slimit",
7663 new variable_reg(&input_stack::limit
));
7664 number_reg_dictionary
.define("systat", new variable_reg(&system_status
));
7665 number_reg_dictionary
.define("urx", new variable_reg(&urx_reg_contents
));
7666 number_reg_dictionary
.define("ury", new variable_reg(&ury_reg_contents
));
7669 object_dictionary
request_dictionary(501);
7671 void init_request(const char *s
, REQUEST_FUNCP f
)
7673 request_dictionary
.define(s
, new request(f
));
7676 static request_or_macro
*lookup_request(symbol nm
)
7678 assert(!nm
.is_null());
7679 request_or_macro
*p
= (request_or_macro
*)request_dictionary
.lookup(nm
);
7681 warning(WARN_MAC
, "macro `%1' not defined", nm
.contents());
7683 request_dictionary
.define(nm
, p
);
7688 node
*charinfo_to_node_list(charinfo
*ci
, const environment
*envp
)
7690 // Don't interpret character definitions in compatible mode.
7691 int old_compatible_flag
= compatible_flag
;
7692 compatible_flag
= 0;
7693 int old_escape_char
= escape_char
;
7695 macro
*mac
= ci
->set_macro(0);
7697 environment
*oldenv
= curenv
;
7698 environment
env(envp
);
7700 curenv
->set_composite();
7701 token old_tok
= tok
;
7702 input_stack::add_boundary();
7703 string_iterator
*si
=
7704 new string_iterator(*mac
, "composite character", ci
->nm
);
7705 input_stack::push(si
);
7706 // we don't use process_input_stack, because we don't want to recognise
7712 if (tok
.newline()) {
7713 error("composite character mustn't contain newline");
7721 node
*n
= curenv
->extract_output_line();
7722 input_stack::remove_boundary();
7726 compatible_flag
= old_compatible_flag
;
7727 escape_char
= old_escape_char
;
7732 static node
*read_draw_node()
7736 if (!start
.delimiter(1)){
7739 } while (tok
!= start
&& !tok
.newline() && !tok
.eof());
7744 error("missing argument");
7746 unsigned char type
= tok
.ch();
7748 read_color_draw_node(start
);
7753 hvpair
*point
= new hvpair
[maxpoints
];
7758 for (i
= 0; tok
!= start
; i
++) {
7759 if (i
== maxpoints
) {
7760 hvpair
*oldpoint
= point
;
7761 point
= new hvpair
[maxpoints
*2];
7762 for (int j
= 0; j
< maxpoints
; j
++)
7763 point
[j
] = oldpoint
[j
];
7767 if (!get_hunits(&point
[i
].h
,
7768 type
== 'f' || type
== 't' ? 'u' : 'm')) {
7779 if (!get_vunits(&point
[i
].v
, 'v')) {
7785 while (tok
!= start
&& !tok
.newline() && !tok
.eof())
7790 if (npoints
!= 1 || no_last_v
) {
7791 error("two arguments needed for line");
7796 if (npoints
!= 1 || !no_last_v
) {
7797 error("one argument needed for circle");
7803 if (npoints
!= 1 || no_last_v
) {
7804 error("two arguments needed for ellipse");
7809 if (npoints
!= 2 || no_last_v
) {
7810 error("four arguments needed for arc");
7816 error("even number of arguments needed for spline");
7819 if (npoints
!= 1 || !no_last_v
) {
7820 error("one argument needed for gray shade");
7825 // silently pass it through
7828 draw_node
*dn
= new draw_node(type
, point
, npoints
,
7829 curenv
->get_font_size(),
7830 curenv
->get_glyph_color(),
7831 curenv
->get_fill_color());
7843 static void read_color_draw_node(token
&start
)
7847 error("missing color scheme");
7850 unsigned char scheme
= tok
.ch();
7853 char end
= start
.ch();
7856 col
= read_cmy(end
);
7859 col
= &default_color
;
7862 col
= read_gray(end
);
7865 col
= read_cmyk(end
);
7868 col
= read_rgb(end
);
7872 curenv
->set_fill_color(col
);
7873 while (tok
!= start
) {
7874 if (tok
.newline() || tok
.eof()) {
7875 warning(WARN_DELIM
, "missing closing delimiter");
7876 input_stack::push(make_temp_iterator("\n"));
7887 } warning_table
[] = {
7888 { "char", WARN_CHAR
},
7889 { "range", WARN_RANGE
},
7890 { "break", WARN_BREAK
},
7891 { "delim", WARN_DELIM
},
7893 { "scale", WARN_SCALE
},
7894 { "number", WARN_NUMBER
},
7895 { "syntax", WARN_SYNTAX
},
7896 { "tab", WARN_TAB
},
7897 { "right-brace", WARN_RIGHT_BRACE
},
7898 { "missing", WARN_MISSING
},
7899 { "input", WARN_INPUT
},
7900 { "escape", WARN_ESCAPE
},
7901 { "space", WARN_SPACE
},
7902 { "font", WARN_FONT
},
7904 { "mac", WARN_MAC
},
7905 { "reg", WARN_REG
},
7907 { "color", WARN_COLOR
},
7908 { "all", WARN_TOTAL
& ~(WARN_DI
| WARN_MAC
| WARN_REG
) },
7909 { "w", WARN_TOTAL
},
7910 { "default", DEFAULT_WARNING_MASK
},
7913 static int lookup_warning(const char *name
)
7915 for (unsigned int i
= 0;
7916 i
< sizeof(warning_table
)/sizeof(warning_table
[0]);
7918 if (strcmp(name
, warning_table
[i
].name
) == 0)
7919 return warning_table
[i
].mask
;
7923 static void enable_warning(const char *name
)
7925 int mask
= lookup_warning(name
);
7927 warning_mask
|= mask
;
7929 error("unknown warning `%1'", name
);
7932 static void disable_warning(const char *name
)
7934 int mask
= lookup_warning(name
);
7936 warning_mask
&= ~mask
;
7938 error("unknown warning `%1'", name
);
7941 static void copy_mode_error(const char *format
,
7947 static const char prefix
[] = "(in ignored input) ";
7948 char *s
= new char[sizeof(prefix
) + strlen(format
)];
7951 warning(WARN_IG
, s
, arg1
, arg2
, arg3
);
7955 error(format
, arg1
, arg2
, arg3
);
7958 enum error_type
{ WARNING
, OUTPUT_WARNING
, ERROR
, FATAL
};
7960 static void do_error(error_type type
,
7966 const char *filename
;
7968 if (inhibit_errors
&& type
< FATAL
)
7971 input_stack::backtrace();
7972 if (!get_file_line(&filename
, &lineno
))
7975 errprint("%1:%2: ", filename
, lineno
);
7976 else if (program_name
)
7977 fprintf(stderr
, "%s: ", program_name
);
7980 fputs("fatal error: ", stderr
);
7985 fputs("warning: ", stderr
);
7987 case OUTPUT_WARNING
:
7988 double fromtop
= topdiv
->get_vertical_position().to_units() / warn_scale
;
7989 fprintf(stderr
, "warning [p %d, %.1f%c",
7990 topdiv
->get_page_number(), fromtop
, warn_scaling_indicator
);
7991 if (topdiv
!= curdiv
) {
7992 double fromtop1
= curdiv
->get_vertical_position().to_units()
7994 fprintf(stderr
, ", div `%s', %.1f%c",
7995 curdiv
->get_diversion_name(), fromtop1
, warn_scaling_indicator
);
7997 fprintf(stderr
, "]: ");
8000 errprint(format
, arg1
, arg2
, arg3
);
8001 fputc('\n', stderr
);
8004 cleanup_and_exit(1);
8007 int warning(warning_type t
,
8013 if ((t
& warning_mask
) != 0) {
8014 do_error(WARNING
, format
, arg1
, arg2
, arg3
);
8021 int output_warning(warning_type t
,
8027 if ((t
& warning_mask
) != 0) {
8028 do_error(OUTPUT_WARNING
, format
, arg1
, arg2
, arg3
);
8035 void error(const char *format
,
8040 do_error(ERROR
, format
, arg1
, arg2
, arg3
);
8043 void fatal(const char *format
,
8048 do_error(FATAL
, format
, arg1
, arg2
, arg3
);
8051 void fatal_with_file_and_line(const char *filename
, int lineno
,
8057 fprintf(stderr
, "%s:%d: fatal error: ", filename
, lineno
);
8058 errprint(format
, arg1
, arg2
, arg3
);
8059 fputc('\n', stderr
);
8061 cleanup_and_exit(1);
8064 void error_with_file_and_line(const char *filename
, int lineno
,
8070 fprintf(stderr
, "%s:%d: error: ", filename
, lineno
);
8071 errprint(format
, arg1
, arg2
, arg3
);
8072 fputc('\n', stderr
);
8076 dictionary
charinfo_dictionary(501);
8078 charinfo
*get_charinfo(symbol nm
)
8080 void *p
= charinfo_dictionary
.lookup(nm
);
8082 return (charinfo
*)p
;
8083 charinfo
*cp
= new charinfo(nm
);
8084 (void)charinfo_dictionary
.lookup(nm
, cp
);
8088 int charinfo::next_index
= 0;
8090 charinfo::charinfo(symbol s
)
8091 : translation(0), mac(0), special_translation(TRANSLATE_NONE
),
8092 hyphenation_code(0), flags(0), ascii_code(0), asciify_code(0),
8093 not_found(0), transparent_translate(1), translate_input(0),
8094 mode(CHAR_NORMAL
), nm(s
)
8096 index
= next_index
++;
8099 void charinfo::set_hyphenation_code(unsigned char c
)
8101 hyphenation_code
= c
;
8104 void charinfo::set_translation(charinfo
*ci
, int tt
, int ti
)
8108 if (hyphenation_code
!= 0)
8109 ci
->set_hyphenation_code(hyphenation_code
);
8110 if (asciify_code
!= 0)
8111 ci
->set_asciify_code(asciify_code
);
8112 else if (ascii_code
!= 0)
8113 ci
->set_asciify_code(ascii_code
);
8114 ci
->set_translation_input();
8116 special_translation
= TRANSLATE_NONE
;
8117 transparent_translate
= tt
;
8120 void charinfo::set_special_translation(int c
, int tt
)
8122 special_translation
= c
;
8124 transparent_translate
= tt
;
8127 void charinfo::set_ascii_code(unsigned char c
)
8132 void charinfo::set_asciify_code(unsigned char c
)
8137 macro
*charinfo::set_macro(macro
*m
)
8144 macro
*charinfo::setx_macro(macro
*m
, char_mode cm
)
8152 void charinfo::set_number(int n
)
8158 int charinfo::get_number()
8160 assert(flags
& NUMBERED
);
8164 symbol
UNNAMED_SYMBOL("---");
8166 // For numbered characters not between 0 and 255, we make a symbol out
8167 // of the number and store them in this dictionary.
8169 dictionary
numbered_charinfo_dictionary(11);
8171 charinfo
*get_charinfo_by_number(int n
)
8173 static charinfo
*number_table
[256];
8175 if (n
>= 0 && n
< 256) {
8176 charinfo
*ci
= number_table
[n
];
8178 ci
= new charinfo(UNNAMED_SYMBOL
);
8180 number_table
[n
] = ci
;
8185 symbol
ns(i_to_a(n
));
8186 charinfo
*ci
= (charinfo
*)numbered_charinfo_dictionary
.lookup(ns
);
8188 ci
= new charinfo(UNNAMED_SYMBOL
);
8190 (void)numbered_charinfo_dictionary
.lookup(ns
, ci
);
8196 int font::name_to_index(const char *nm
)
8200 ci
= charset_table
[nm
[0] & 0xff];
8201 else if (nm
[0] == '\\' && nm
[2] == 0)
8202 ci
= get_charinfo(symbol(nm
+ 1));
8204 ci
= get_charinfo(symbol(nm
));
8208 return ci
->get_index();
8211 int font::number_to_index(int n
)
8213 return get_charinfo_by_number(n
)->get_index();