2 /* Copyright (C) 1989, 1990, 1991, 1992, 2000, 2001, 2002, 2003, 2004, 2005,
4 Free Software Foundation, Inc.
5 Written by James Clark (jjc@jclark.com)
7 This file is part of groff.
9 groff is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 2, or (at your option) any later
14 groff is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 You should have received a copy of the GNU General Public License along
20 with groff; see the file COPYING. If not, write to the Free Software
21 Foundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA. */
26 #include "dictionary.h"
28 #include "stringclass.h"
31 #include "file_case.h"
39 #include "macropath.h"
44 // Needed for getpid() and isatty()
49 #ifdef NEED_DECLARATION_PUTENV
51 int putenv(const char *);
53 #endif /* NEED_DECLARATION_PUTENV */
55 #define MACRO_PREFIX "tmac."
56 #define MACRO_POSTFIX ".tmac"
57 #define INITIAL_STARTUP_FILE "troffrc"
58 #define FINAL_STARTUP_FILE "troffrc-end"
59 #define DEFAULT_INPUT_STACK_LIMIT 1000
61 #ifndef DEFAULT_WARNING_MASK
62 // warnings that are enabled by default
63 #define DEFAULT_WARNING_MASK \
64 (WARN_CHAR|WARN_NUMBER|WARN_BREAK|WARN_SPACE|WARN_FONT)
67 // initial size of buffer for reading names; expanded as necessary
70 extern "C" const char *program_name
;
71 extern "C" const char *Version_string
;
74 void init_column_requests();
77 static node
*read_draw_node();
78 static void read_color_draw_node(token
&);
79 static void push_token(const token
&);
84 void transparent_file();
88 int color_flag
= 1; // colors are on by default
89 static int backtrace_flag
= 0;
91 char *pipe_command
= 0;
93 charinfo
*charset_table
[256];
94 unsigned char hpf_code_table
[256];
96 static int warning_mask
= DEFAULT_WARNING_MASK
;
97 static int inhibit_errors
= 0;
98 static int ignoring
= 0;
100 static void enable_warning(const char *);
101 static void disable_warning(const char *);
103 static int escape_char
= '\\';
104 static symbol end_macro_name
;
105 static symbol blank_line_macro_name
;
106 static int compatible_flag
= 0;
107 int ascii_output_flag
= 0;
108 int suppress_output_flag
= 0;
110 int begin_level
= 0; // number of nested \O escapes
112 int have_input
= 0; // whether \f, \F, \D'F...', \H, \m, \M,
113 // \O[345], \R, \s, or \S has been processed
115 int old_have_input
= 0; // value of have_input right before \n
116 int tcommand_flag
= 0;
117 int unsafe_flag
= 0; // safer by default
119 int have_string_arg
= 0; // whether we have \*[foo bar...]
121 double spread_limit
= -3.0 - 1.0; // negative means deactivated
124 char warn_scaling_indicator
;
125 int debug_state
= 0; // turns on debugging of the html troff state
127 search_path
*mac_path
= &safer_macro_path
;
129 // Defaults to the current directory.
130 search_path
include_search_path(0, 0, 0, 1);
132 static int get_copy(node
**, int = 0, int = 0);
133 static void copy_mode_error(const char *,
134 const errarg
& = empty_errarg
,
135 const errarg
& = empty_errarg
,
136 const errarg
& = empty_errarg
);
138 enum read_mode
{ ALLOW_EMPTY
, WITH_ARGS
, NO_ARGS
};
139 static symbol
read_escape_name(read_mode
= NO_ARGS
);
140 static symbol
read_long_escape_name(read_mode
= NO_ARGS
);
141 static void interpolate_string(symbol
);
142 static void interpolate_string_with_args(symbol
);
143 static void interpolate_macro(symbol
, int = 0);
144 static void interpolate_number_format(symbol
);
145 static void interpolate_environment_variable(symbol
);
147 static symbol
composite_glyph_name(symbol
);
148 static void interpolate_arg(symbol
);
149 static request_or_macro
*lookup_request(symbol
);
150 static int get_delim_number(units
*, unsigned char);
151 static int get_delim_number(units
*, unsigned char, units
);
152 static symbol
do_get_long_name(int, char);
153 static int get_line_arg(units
*res
, unsigned char si
, charinfo
**cp
);
154 static int read_size(int *);
155 static symbol
get_delim_name();
156 static void init_registers();
157 static void trapping_blank_line();
159 class input_iterator
;
160 input_iterator
*make_temp_iterator(const char *);
161 const char *input_char_description(int);
163 void process_input_stack();
164 void chop_macro(); // declare to avoid friend name injection
167 void set_escape_char()
171 error("bad escape character");
175 escape_char
= tok
.ch();
188 static int saved_escape_char
= '\\';
190 void save_escape_char()
192 saved_escape_char
= escape_char
;
196 void restore_escape_char()
198 escape_char
= saved_escape_char
;
204 class input_iterator
{
207 input_iterator(int is_div
);
208 virtual ~input_iterator() {}
210 friend class input_stack
;
212 statem
*diversion_state
;
214 const unsigned char *ptr
;
215 const unsigned char *eptr
;
216 input_iterator
*next
;
218 virtual int fill(node
**);
220 virtual int has_args() { return 0; }
221 virtual int nargs() { return 0; }
222 virtual input_iterator
*get_arg(int) { return 0; }
223 virtual arg_list
*get_arg_list() { return 0; }
224 virtual symbol
get_macro_name() { return NULL_SYMBOL
; }
225 virtual int space_follows_arg(int) { return 0; }
226 virtual int get_break_flag() { return 0; }
227 virtual int get_location(int, const char **, int *) { return 0; }
228 virtual void backtrace() {}
229 virtual int set_location(const char *, int) { return 0; }
230 virtual int next_file(file_case
*, const char *) { return 0; }
231 virtual void shift(int) {}
232 virtual int is_boundary() {return 0; }
233 virtual int is_file() { return 0; }
234 virtual int is_macro() { return 0; }
235 virtual void save_compatible_flag(int) {}
236 virtual int get_compatible_flag() { return 0; }
239 input_iterator::input_iterator()
240 : is_diversion(0), ptr(0), eptr(0)
244 input_iterator::input_iterator(int is_div
)
245 : is_diversion(is_div
), ptr(0), eptr(0)
249 int input_iterator::fill(node
**)
254 int input_iterator::peek()
259 inline int input_iterator::get(node
**p
)
261 return ptr
< eptr
? *ptr
++ : fill(p
);
264 class input_boundary
: public input_iterator
{
266 int is_boundary() { return 1; }
269 class input_return_boundary
: public input_iterator
{
271 int is_boundary() { return 2; }
274 class file_iterator
: public input_iterator
{
277 const char *filename
;
280 enum { BUF_SIZE
= 512 };
281 unsigned char buf
[BUF_SIZE
];
284 file_iterator(file_case
*, const char *);
288 int get_location(int, const char **, int *);
290 int set_location(const char *, int);
291 int next_file(file_case
*, const char *);
295 file_iterator::file_iterator(file_case
*fcp
, const char *fn
)
296 : _fcp(fcp
), lineno(1), filename(fn
), newline_flag(0), seen_escape(0)
298 if ((font::use_charnames_in_special
) && (fn
!= 0)) {
301 the_output
->put_filename(fn
, _fcp
->is_pipe());
305 file_iterator::~file_iterator()
311 void file_iterator::close()
319 int file_iterator::is_file()
324 int file_iterator::next_file(file_case
*fcp
, const char *s
)
337 int file_iterator::fill(node
**)
342 unsigned char *p
= buf
;
344 unsigned char *e
= p
+ BUF_SIZE
;
346 int c
= _fcp
->get_c();
349 if (invalid_input_char(c
))
350 warning(WARN_INPUT
, "invalid input character code %1", int(c
));
358 seen_escape
= (c
== '\\');
371 int file_iterator::peek()
373 int c
= _fcp
->get_c();
374 while (invalid_input_char(c
)) {
375 warning(WARN_INPUT
, "invalid input character code %1", int(c
));
383 int file_iterator::get_location(int /*allow_macro*/,
384 const char **filenamep
, int *linenop
)
387 if (filename
!= 0 && strcmp(filename
, "-") == 0)
388 *filenamep
= "<standard input>";
390 *filenamep
= filename
;
394 void file_iterator::backtrace()
396 errprint("%1:%2: backtrace: %3 `%1'\n", filename
, lineno
,
397 _fcp
->is_pipe() ? "process" : "file");
400 int file_iterator::set_location(const char *f
, int ln
)
406 the_output
->put_filename(f
, 0);
412 input_iterator nil_iterator
;
416 static int get(node
**);
418 static void push(input_iterator
*);
419 static input_iterator
*get_arg(int);
420 static arg_list
*get_arg_list();
421 static symbol
get_macro_name();
422 static int space_follows_arg(int);
423 static int get_break_flag();
425 static int get_location(int, const char **, int *);
426 static int set_location(const char *, int);
427 static void backtrace();
428 static void backtrace_all();
429 static void next_file(file_case
*, const char *);
430 static void end_file();
431 static void shift(int n
);
432 static void add_boundary();
433 static void add_return_boundary();
434 static int is_return_boundary();
435 static void remove_boundary();
436 static int get_level();
437 static int get_div_level();
438 static void increase_level();
439 static void decrease_level();
441 static void pop_macro();
442 static void save_compatible_flag(int);
443 static int get_compatible_flag();
444 static statem
*get_diversion_state();
445 static void check_end_diversion(input_iterator
*t
);
447 static int div_level
;
448 static statem
*diversion_state
;
450 static input_iterator
*top
;
452 static int finish_get(node
**);
453 static int finish_peek();
456 input_iterator
*input_stack::top
= &nil_iterator
;
457 int input_stack::level
= 0;
458 int input_stack::limit
= DEFAULT_INPUT_STACK_LIMIT
;
459 int input_stack::div_level
= 0;
460 statem
*input_stack::diversion_state
= NULL
;
464 inline int input_stack::get_level()
469 inline void input_stack::increase_level()
474 inline void input_stack::decrease_level()
479 inline int input_stack::get_div_level()
484 inline int input_stack::get(node
**np
)
486 int res
= (top
->ptr
< top
->eptr
) ? *top
->ptr
++ : finish_get(np
);
488 old_have_input
= have_input
;
494 int input_stack::finish_get(node
**np
)
497 int c
= top
->fill(np
);
498 if (c
!= EOF
|| top
->is_boundary())
500 if (top
== &nil_iterator
)
502 input_iterator
*tem
= top
;
503 check_end_diversion(tem
);
504 #if defined(DEBUGGING)
506 if (tem
->is_diversion
)
508 "in diversion level = %d\n", input_stack::get_div_level());
513 if (top
->ptr
< top
->eptr
)
520 inline int input_stack::peek()
522 return (top
->ptr
< top
->eptr
) ? *top
->ptr
: finish_peek();
525 void input_stack::check_end_diversion(input_iterator
*t
)
527 if (t
->is_diversion
) {
529 diversion_state
= t
->diversion_state
;
533 int input_stack::finish_peek()
537 if (c
!= EOF
|| top
->is_boundary())
539 if (top
== &nil_iterator
)
541 input_iterator
*tem
= top
;
542 check_end_diversion(tem
);
546 if (top
->ptr
< top
->eptr
)
553 void input_stack::add_boundary()
555 push(new input_boundary
);
558 void input_stack::add_return_boundary()
560 push(new input_return_boundary
);
563 int input_stack::is_return_boundary()
565 return top
->is_boundary() == 2;
568 void input_stack::remove_boundary()
570 assert(top
->is_boundary());
571 input_iterator
*temp
= top
->next
;
572 check_end_diversion(top
);
579 void input_stack::push(input_iterator
*in
)
583 if (++level
> limit
&& limit
> 0)
584 fatal("input stack limit exceeded (probable infinite loop)");
587 if (top
->is_diversion
) {
589 in
->diversion_state
= diversion_state
;
590 diversion_state
= curenv
->construct_state(0);
591 #if defined(DEBUGGING)
593 curenv
->dump_troff_state();
598 #if defined(DEBUGGING)
600 if (top
->is_diversion
) {
602 "in diversion level = %d\n", input_stack::get_div_level());
608 statem
*get_diversion_state()
610 return input_stack::get_diversion_state();
613 statem
*input_stack::get_diversion_state()
615 if (diversion_state
== NULL
)
618 return new statem(diversion_state
);
621 input_iterator
*input_stack::get_arg(int i
)
624 for (p
= top
; p
!= 0; p
= p
->next
)
626 return p
->get_arg(i
);
630 arg_list
*input_stack::get_arg_list()
633 for (p
= top
; p
!= 0; p
= p
->next
)
635 return p
->get_arg_list();
639 symbol
input_stack::get_macro_name()
642 for (p
= top
; p
!= 0; p
= p
->next
)
644 return p
->get_macro_name();
648 int input_stack::space_follows_arg(int i
)
651 for (p
= top
; p
!= 0; p
= p
->next
)
653 return p
->space_follows_arg(i
);
657 int input_stack::get_break_flag()
659 return top
->get_break_flag();
662 void input_stack::shift(int n
)
664 for (input_iterator
*p
= top
; p
; p
= p
->next
)
671 int input_stack::nargs()
673 for (input_iterator
*p
=top
; p
!= 0; p
= p
->next
)
679 int input_stack::get_location(int allow_macro
, const char **filenamep
, int *linenop
)
681 for (input_iterator
*p
= top
; p
; p
= p
->next
)
682 if (p
->get_location(allow_macro
, filenamep
, linenop
))
687 void input_stack::backtrace()
691 // only backtrace down to (not including) the topmost file
692 for (input_iterator
*p
= top
;
693 p
&& !p
->get_location(0, &f
, &n
);
698 void input_stack::backtrace_all()
700 for (input_iterator
*p
= top
; p
; p
= p
->next
)
704 int input_stack::set_location(const char *filename
, int lineno
)
706 for (input_iterator
*p
= top
; p
; p
= p
->next
)
707 if (p
->set_location(filename
, lineno
))
712 void input_stack::next_file(file_case
*fcp
, const char *s
)
715 for (pp
= &top
; *pp
!= &nil_iterator
; pp
= &(*pp
)->next
)
716 if ((*pp
)->next_file(fcp
, s
))
718 if (++level
> limit
&& limit
> 0)
719 fatal("input stack limit exceeded");
720 *pp
= new file_iterator(fcp
, s
);
721 (*pp
)->next
= &nil_iterator
;
724 void input_stack::end_file()
726 for (input_iterator
**pp
= &top
; *pp
!= &nil_iterator
; pp
= &(*pp
)->next
)
727 if ((*pp
)->is_file()) {
728 input_iterator
*tem
= *pp
;
729 check_end_diversion(tem
);
737 void input_stack::clear()
740 while (top
!= &nil_iterator
) {
741 if (top
->is_boundary())
743 input_iterator
*tem
= top
;
744 check_end_diversion(tem
);
749 // Keep while_request happy.
750 for (; nboundaries
> 0; --nboundaries
)
751 add_return_boundary();
754 void input_stack::pop_macro()
759 if (top
->next
== &nil_iterator
)
761 if (top
->is_boundary())
763 is_macro
= top
->is_macro();
764 input_iterator
*tem
= top
;
765 check_end_diversion(tem
);
770 // Keep while_request happy.
771 for (; nboundaries
> 0; --nboundaries
)
772 add_return_boundary();
775 inline void input_stack::save_compatible_flag(int f
)
777 top
->save_compatible_flag(f
);
780 inline int input_stack::get_compatible_flag()
782 return top
->get_compatible_flag();
785 void backtrace_request()
787 input_stack::backtrace_all();
794 symbol nm
= get_long_name();
795 while (!tok
.newline() && !tok
.eof())
798 input_stack::end_file();
800 file_case
*fcp
= include_search_path
.open_file_cautious(nm
.contents(),
803 input_stack::next_file(fcp
, nm
.contents());
806 error("can't open `%1': %2", nm
.contents(), strerror(errno
));
814 if (!has_arg() || !get_integer(&n
))
816 input_stack::shift(n
);
820 static char get_char_for_escape_name(int allow_space
= 0)
822 int c
= get_copy(0, 0, 1);
825 copy_mode_error("end of input in escape name");
828 if (!invalid_input_char(c
))
833 input_stack::push(make_temp_iterator("\n"));
836 if (c
== ' ' && allow_space
)
842 copy_mode_error("%1 is not allowed in an escape name",
843 input_char_description(c
));
849 static symbol
read_two_char_escape_name()
852 buf
[0] = get_char_for_escape_name();
853 if (buf
[0] != '\0') {
854 buf
[1] = get_char_for_escape_name();
863 static symbol
read_long_escape_name(read_mode mode
)
865 int start_level
= input_stack::get_level();
866 char abuf
[ABUF_SIZE
];
868 int buf_size
= ABUF_SIZE
;
873 c
= get_char_for_escape_name(have_char
&& mode
== WITH_ARGS
);
880 if (mode
== WITH_ARGS
&& c
== ' ')
882 if (i
+ 2 > buf_size
) {
884 buf
= new char[ABUF_SIZE
*2];
885 memcpy(buf
, abuf
, buf_size
);
886 buf_size
= ABUF_SIZE
*2;
890 buf
= new char[buf_size
*2];
891 memcpy(buf
, old_buf
, buf_size
);
896 if (c
== ']' && input_stack::get_level() == start_level
)
905 if (mode
!= ALLOW_EMPTY
)
906 copy_mode_error("empty escape name");
918 static symbol
read_escape_name(read_mode mode
)
920 char c
= get_char_for_escape_name();
924 return read_two_char_escape_name();
925 if (c
== '[' && !compatible_flag
)
926 return read_long_escape_name(mode
);
933 static symbol
read_increment_and_escape_name(int *incp
)
935 char c
= get_char_for_escape_name();
942 return read_two_char_escape_name();
945 return read_escape_name();
948 return read_escape_name();
950 if (!compatible_flag
) {
952 return read_long_escape_name();
963 static int get_copy(node
**nd
, int defining
, int handle_escape_E
)
966 int c
= input_stack::get(nd
);
967 if (c
== PUSH_GROFF_MODE
) {
968 input_stack::save_compatible_flag(compatible_flag
);
972 if (c
== PUSH_COMP_MODE
) {
973 input_stack::save_compatible_flag(compatible_flag
);
977 if (c
== POP_GROFFCOMP_MODE
) {
978 compatible_flag
= input_stack::get_compatible_flag();
981 if (c
== BEGIN_QUOTE
) {
982 input_stack::increase_level();
985 if (c
== END_QUOTE
) {
986 input_stack::decrease_level();
989 if (c
== DOUBLE_QUOTE
)
991 if (c
== ESCAPE_E
&& handle_escape_E
)
993 if (c
== ESCAPE_NEWLINE
) {
997 c
= input_stack::get(nd
);
998 } while (c
== ESCAPE_NEWLINE
);
1000 if (c
!= escape_char
|| escape_char
<= 0)
1003 c
= input_stack::peek();
1008 (void)input_stack::get(0);
1009 while ((c
= input_stack::get(0)) != '\n' && c
!= EOF
)
1012 case '#': // Like \" but newline is ignored.
1013 (void)input_stack::get(0);
1014 while ((c
= input_stack::get(0)) != '\n')
1020 (void)input_stack::get(0);
1021 symbol s
= read_escape_name();
1022 if (!(s
.is_null() || s
.is_empty()))
1028 (void)input_stack::get(0);
1029 symbol s
= read_escape_name(WITH_ARGS
);
1030 if (!(s
.is_null() || s
.is_empty())) {
1031 if (have_string_arg
) {
1032 have_string_arg
= 0;
1033 interpolate_string_with_args(s
);
1036 interpolate_string(s
);
1041 (void)input_stack::get(0);
1044 (void)input_stack::get(0);
1047 (void)input_stack::get(0);
1048 if (handle_escape_E
)
1053 (void)input_stack::get(0);
1055 symbol s
= read_increment_and_escape_name(&inc
);
1056 if (!(s
.is_null() || s
.is_empty()))
1057 interpolate_number_reg(s
, inc
);
1062 (void)input_stack::get(0);
1063 symbol s
= read_escape_name();
1064 if (!(s
.is_null() || s
.is_empty()))
1065 interpolate_number_format(s
);
1069 (void)input_stack::get(0);
1073 (void)input_stack::get(0);
1074 symbol s
= read_escape_name();
1075 if (!(s
.is_null() || s
.is_empty()))
1076 interpolate_environment_variable(s
);
1080 (void)input_stack::get(0);
1082 return ESCAPE_NEWLINE
;
1085 (void)input_stack::get(0);
1086 return ESCAPE_SPACE
;
1088 (void)input_stack::get(0);
1089 return ESCAPE_TILDE
;
1091 (void)input_stack::get(0);
1092 return ESCAPE_COLON
;
1094 (void)input_stack::get(0);
1097 (void)input_stack::get(0);
1098 return ESCAPE_CIRCUMFLEX
;
1100 (void)input_stack::get(0);
1101 return ESCAPE_LEFT_BRACE
;
1103 (void)input_stack::get(0);
1104 return ESCAPE_RIGHT_BRACE
;
1106 (void)input_stack::get(0);
1107 return ESCAPE_LEFT_QUOTE
;
1109 (void)input_stack::get(0);
1110 return ESCAPE_RIGHT_QUOTE
;
1112 (void)input_stack::get(0);
1113 return ESCAPE_HYPHEN
;
1115 (void)input_stack::get(0);
1116 return ESCAPE_UNDERSCORE
;
1118 (void)input_stack::get(0);
1121 (void)input_stack::get(0);
1124 (void)input_stack::get(0);
1125 return ESCAPE_QUESTION
;
1127 (void)input_stack::get(0);
1128 return ESCAPE_AMPERSAND
;
1130 (void)input_stack::get(0);
1131 return ESCAPE_RIGHT_PARENTHESIS
;
1133 (void)input_stack::get(0);
1136 (void)input_stack::get(0);
1137 return ESCAPE_PERCENT
;
1139 if (c
== escape_char
) {
1140 (void)input_stack::get(0);
1149 class non_interpreted_char_node
: public node
{
1152 non_interpreted_char_node(unsigned char);
1154 int interpret(macro
*);
1161 int non_interpreted_char_node::same(node
*nd
)
1163 return c
== ((non_interpreted_char_node
*)nd
)->c
;
1166 const char *non_interpreted_char_node::type()
1168 return "non_interpreted_char_node";
1171 int non_interpreted_char_node::force_tprint()
1176 int non_interpreted_char_node::is_tag()
1181 non_interpreted_char_node::non_interpreted_char_node(unsigned char n
) : c(n
)
1186 node
*non_interpreted_char_node::copy()
1188 return new non_interpreted_char_node(c
);
1191 int non_interpreted_char_node::interpret(macro
*mac
)
1197 static void do_width();
1198 static node
*do_non_interpreted();
1199 static node
*do_special();
1200 static node
*do_suppress(symbol nm
);
1201 static void do_register();
1203 dictionary
color_dictionary(501);
1205 static color
*lookup_color(symbol nm
)
1207 assert(!nm
.is_null());
1208 if (nm
== default_symbol
)
1209 return &default_color
;
1210 color
*c
= (color
*)color_dictionary
.lookup(nm
);
1212 warning(WARN_COLOR
, "color `%1' not defined", nm
.contents());
1216 void do_glyph_color(symbol nm
)
1221 curenv
->set_glyph_color(curenv
->get_prev_glyph_color());
1223 color
*tem
= lookup_color(nm
);
1225 curenv
->set_glyph_color(tem
);
1227 (void)color_dictionary
.lookup(nm
, new color(nm
));
1231 void do_fill_color(symbol nm
)
1236 curenv
->set_fill_color(curenv
->get_prev_fill_color());
1238 color
*tem
= lookup_color(nm
);
1240 curenv
->set_fill_color(tem
);
1242 (void)color_dictionary
.lookup(nm
, new color(nm
));
1246 static unsigned int get_color_element(const char *scheme
, const char *col
)
1249 if (!get_number(&val
, 'f')) {
1250 warning(WARN_COLOR
, "%1 in %2 definition set to 0", col
, scheme
);
1255 warning(WARN_RANGE
, "%1 cannot be negative: set to 0", col
);
1258 if (val
> color::MAX_COLOR_VAL
+1) {
1259 warning(WARN_RANGE
, "%1 cannot be greater than 1", col
);
1260 // we change 0x10000 to 0xffff
1261 return color::MAX_COLOR_VAL
;
1263 return (unsigned int)val
;
1266 static color
*read_rgb(char end
= 0)
1268 symbol component
= do_get_long_name(0, end
);
1269 if (component
.is_null()) {
1270 warning(WARN_COLOR
, "missing rgb color values");
1273 const char *s
= component
.contents();
1274 color
*col
= new color
;
1276 if (!col
->read_rgb(s
)) {
1277 warning(WARN_COLOR
, "expecting rgb color definition not `%1'", s
);
1284 input_stack::push(make_temp_iterator(" "));
1285 input_stack::push(make_temp_iterator(s
));
1287 unsigned int r
= get_color_element("rgb color", "red component");
1288 unsigned int g
= get_color_element("rgb color", "green component");
1289 unsigned int b
= get_color_element("rgb color", "blue component");
1290 col
->set_rgb(r
, g
, b
);
1295 static color
*read_cmy(char end
= 0)
1297 symbol component
= do_get_long_name(0, end
);
1298 if (component
.is_null()) {
1299 warning(WARN_COLOR
, "missing cmy color values");
1302 const char *s
= component
.contents();
1303 color
*col
= new color
;
1305 if (!col
->read_cmy(s
)) {
1306 warning(WARN_COLOR
, "expecting cmy color definition not `%1'", s
);
1313 input_stack::push(make_temp_iterator(" "));
1314 input_stack::push(make_temp_iterator(s
));
1316 unsigned int c
= get_color_element("cmy color", "cyan component");
1317 unsigned int m
= get_color_element("cmy color", "magenta component");
1318 unsigned int y
= get_color_element("cmy color", "yellow component");
1319 col
->set_cmy(c
, m
, y
);
1324 static color
*read_cmyk(char end
= 0)
1326 symbol component
= do_get_long_name(0, end
);
1327 if (component
.is_null()) {
1328 warning(WARN_COLOR
, "missing cmyk color values");
1331 const char *s
= component
.contents();
1332 color
*col
= new color
;
1334 if (!col
->read_cmyk(s
)) {
1335 warning(WARN_COLOR
, "`expecting a cmyk color definition not `%1'", s
);
1342 input_stack::push(make_temp_iterator(" "));
1343 input_stack::push(make_temp_iterator(s
));
1345 unsigned int c
= get_color_element("cmyk color", "cyan component");
1346 unsigned int m
= get_color_element("cmyk color", "magenta component");
1347 unsigned int y
= get_color_element("cmyk color", "yellow component");
1348 unsigned int k
= get_color_element("cmyk color", "black component");
1349 col
->set_cmyk(c
, m
, y
, k
);
1354 static color
*read_gray(char end
= 0)
1356 symbol component
= do_get_long_name(0, end
);
1357 if (component
.is_null()) {
1358 warning(WARN_COLOR
, "missing gray values");
1361 const char *s
= component
.contents();
1362 color
*col
= new color
;
1364 if (!col
->read_gray(s
)) {
1365 warning(WARN_COLOR
, "`expecting a gray definition not `%1'", s
);
1372 input_stack::push(make_temp_iterator("\n"));
1373 input_stack::push(make_temp_iterator(s
));
1375 unsigned int g
= get_color_element("gray", "gray value");
1381 static void activate_color()
1384 if (has_arg() && get_integer(&n
))
1385 color_flag
= n
!= 0;
1391 static void define_color()
1393 symbol color_name
= get_long_name(1);
1394 if (color_name
.is_null()) {
1398 if (color_name
== default_symbol
) {
1399 warning(WARN_COLOR
, "default color can't be redefined");
1403 symbol style
= get_long_name(1);
1404 if (style
.is_null()) {
1409 if (strcmp(style
.contents(), "rgb") == 0)
1411 else if (strcmp(style
.contents(), "cmyk") == 0)
1413 else if (strcmp(style
.contents(), "gray") == 0)
1415 else if (strcmp(style
.contents(), "grey") == 0)
1417 else if (strcmp(style
.contents(), "cmy") == 0)
1421 "unknown color space `%1'; use rgb, cmyk, gray or cmy",
1427 col
->nm
= color_name
;
1428 (void)color_dictionary
.lookup(color_name
, col
);
1433 static node
*do_overstrike()
1436 overstrike_node
*on
= new overstrike_node
;
1437 int start_level
= input_stack::get_level();
1441 if (tok
.newline() || tok
.eof()) {
1442 warning(WARN_DELIM
, "missing closing delimiter");
1443 input_stack::push(make_temp_iterator("\n"));
1447 && (compatible_flag
|| input_stack::get_level() == start_level
))
1449 charinfo
*ci
= tok
.get_char(1);
1451 node
*n
= curenv
->make_char_node(ci
);
1459 static node
*do_bracket()
1462 bracket_node
*bn
= new bracket_node
;
1464 int start_level
= input_stack::get_level();
1468 warning(WARN_DELIM
, "missing closing delimiter");
1471 if (tok
.newline()) {
1472 warning(WARN_DELIM
, "missing closing delimiter");
1473 input_stack::push(make_temp_iterator("\n"));
1477 && (compatible_flag
|| input_stack::get_level() == start_level
))
1479 charinfo
*ci
= tok
.get_char(1);
1481 node
*n
= curenv
->make_char_node(ci
);
1489 static int do_name_test()
1493 int start_level
= input_stack::get_level();
1498 if (tok
.newline() || tok
.eof()) {
1499 warning(WARN_DELIM
, "missing closing delimiter");
1500 input_stack::push(make_temp_iterator("\n"));
1504 && (compatible_flag
|| input_stack::get_level() == start_level
))
1510 return some_char
&& !bad_char
;
1513 static int do_expr_test()
1517 int start_level
= input_stack::get_level();
1518 if (!start
.delimiter(1))
1521 // disable all warning and error messages temporarily
1522 int saved_warning_mask
= warning_mask
;
1523 int saved_inhibit_errors
= inhibit_errors
;
1527 int result
= get_number_rigidly(&dummy
, 'u');
1528 warning_mask
= saved_warning_mask
;
1529 inhibit_errors
= saved_inhibit_errors
;
1530 if (tok
== start
&& input_stack::get_level() == start_level
)
1532 // ignore everything up to the delimiter in case we aren't right there
1535 if (tok
.newline() || tok
.eof()) {
1536 warning(WARN_DELIM
, "missing closing delimiter");
1537 input_stack::push(make_temp_iterator("\n"));
1540 if (tok
== start
&& input_stack::get_level() == start_level
)
1547 static node
*do_zero_width()
1551 int start_level
= input_stack::get_level();
1552 environment
env(curenv
);
1553 environment
*oldenv
= curenv
;
1557 if (tok
.newline() || tok
.eof()) {
1558 error("missing closing delimiter");
1562 && (compatible_flag
|| input_stack::get_level() == start_level
))
1567 node
*rev
= env
.extract_output_line();
1575 return new zero_width_node(n
);
1580 // It's undesirable for \Z to change environments, because then
1581 // \n(.w won't work as expected.
1583 static node
*do_zero_width()
1585 node
*rev
= new dummy_node
;
1588 int start_level
= input_stack::get_level();
1591 if (tok
.newline() || tok
.eof()) {
1592 warning(WARN_DELIM
, "missing closing delimiter");
1593 input_stack::push(make_temp_iterator("\n"));
1597 && (compatible_flag
|| input_stack::get_level() == start_level
))
1599 if (!tok
.add_to_node_list(&rev
))
1600 error("invalid token in argument to \\Z");
1609 return new zero_width_node(n
);
1614 token_node
*node::get_token_node()
1619 class token_node
: public node
{
1622 token_node(const token
&t
);
1624 token_node
*get_token_node();
1631 token_node::token_node(const token
&t
) : tk(t
)
1635 node
*token_node::copy()
1637 return new token_node(tk
);
1640 token_node
*token_node::get_token_node()
1645 int token_node::same(node
*nd
)
1647 return tk
== ((token_node
*)nd
)->tk
;
1650 const char *token_node::type()
1652 return "token_node";
1655 int token_node::force_tprint()
1660 int token_node::is_tag()
1665 token::token() : nd(0), type(TOKEN_EMPTY
)
1674 token::token(const token
&t
)
1675 : nm(t
.nm
), c(t
.c
), val(t
.val
), dim(t
.dim
), type(t
.type
)
1677 // Use two statements to work around bug in SGI C++.
1679 nd
= tem
? tem
->copy() : 0;
1682 void token::operator=(const token
&t
)
1686 // Use two statements to work around bug in SGI C++.
1688 nd
= tem
? tem
->copy() : 0;
1705 return !tok
.newline();
1708 void token::make_space()
1713 void token::make_newline()
1715 type
= TOKEN_NEWLINE
;
1727 int cc
= input_stack::get(&n
);
1728 if (cc
!= escape_char
|| escape_char
== 0) {
1731 case PUSH_GROFF_MODE
:
1732 input_stack::save_compatible_flag(compatible_flag
);
1733 compatible_flag
= 0;
1735 case PUSH_COMP_MODE
:
1736 input_stack::save_compatible_flag(compatible_flag
);
1737 compatible_flag
= 1;
1739 case POP_GROFFCOMP_MODE
:
1740 compatible_flag
= input_stack::get_compatible_flag();
1743 input_stack::increase_level();
1746 input_stack::decrease_level();
1753 case TRANSPARENT_FILE_REQUEST
:
1755 case COPY_FILE_REQUEST
:
1757 case VJUSTIFY_REQUEST
:
1759 type
= TOKEN_REQUEST
;
1763 type
= TOKEN_BEGIN_TRAP
;
1766 type
= TOKEN_END_TRAP
;
1768 case LAST_PAGE_EJECTOR
:
1769 seen_last_page_ejector
= 1;
1772 type
= TOKEN_PAGE_EJECTOR
;
1774 case ESCAPE_PERCENT
:
1776 type
= TOKEN_HYPHEN_INDICATOR
;
1780 type
= TOKEN_UNSTRETCHABLE_SPACE
;
1784 type
= TOKEN_STRETCHABLE_SPACE
;
1788 type
= TOKEN_ZERO_WIDTH_BREAK
;
1792 type
= TOKEN_ESCAPE
;
1795 goto handle_escape_char
;
1799 nd
= new hmotion_node(curenv
->get_narrow_space_width(),
1800 curenv
->get_fill_color());
1802 case ESCAPE_CIRCUMFLEX
:
1805 nd
= new hmotion_node(curenv
->get_half_narrow_space_width(),
1806 curenv
->get_fill_color());
1808 case ESCAPE_NEWLINE
:
1811 case ESCAPE_LEFT_BRACE
:
1813 type
= TOKEN_LEFT_BRACE
;
1815 case ESCAPE_RIGHT_BRACE
:
1817 type
= TOKEN_RIGHT_BRACE
;
1819 case ESCAPE_LEFT_QUOTE
:
1821 type
= TOKEN_SPECIAL
;
1824 case ESCAPE_RIGHT_QUOTE
:
1826 type
= TOKEN_SPECIAL
;
1831 type
= TOKEN_SPECIAL
;
1834 case ESCAPE_UNDERSCORE
:
1836 type
= TOKEN_SPECIAL
;
1841 type
= TOKEN_INTERRUPT
;
1845 type
= TOKEN_TRANSPARENT
;
1847 case ESCAPE_QUESTION
:
1849 nd
= do_non_interpreted();
1855 case ESCAPE_AMPERSAND
:
1859 case ESCAPE_RIGHT_PARENTHESIS
:
1860 ESCAPE_RIGHT_PARENTHESIS
:
1861 type
= TOKEN_TRANSPARENT_DUMMY
;
1864 type
= TOKEN_BACKSPACE
;
1873 type
= TOKEN_NEWLINE
;
1876 type
= TOKEN_LEADER
;
1881 token_node
*tn
= n
->get_token_node();
1900 cc
= input_stack::get(&n
);
1903 nm
= read_two_char_escape_name();
1904 type
= TOKEN_SPECIAL
;
1908 error("end of input after escape character");
1911 goto ESCAPE_LEFT_QUOTE
;
1913 goto ESCAPE_RIGHT_QUOTE
;
1917 goto ESCAPE_UNDERSCORE
;
1919 goto ESCAPE_PERCENT
;
1923 nd
= new hmotion_node(curenv
->get_digit_width(),
1924 curenv
->get_fill_color());
1930 goto ESCAPE_CIRCUMFLEX
;
1932 type
= TOKEN_ITALIC_CORRECTION
;
1936 nd
= new left_italic_corrected_node
;
1939 goto ESCAPE_AMPERSAND
;
1941 goto ESCAPE_RIGHT_PARENTHESIS
;
1945 goto ESCAPE_QUESTION
;
1951 while ((cc
= input_stack::get(0)) != '\n' && cc
!= EOF
)
1954 type
= TOKEN_NEWLINE
;
1958 case '#': // Like \" but newline is ignored.
1959 while ((cc
= input_stack::get(0)) != '\n')
1967 symbol s
= read_escape_name();
1968 if (!(s
.is_null() || s
.is_empty()))
1974 symbol s
= read_escape_name(WITH_ARGS
);
1975 if (!(s
.is_null() || s
.is_empty())) {
1976 if (have_string_arg
) {
1977 have_string_arg
= 0;
1978 interpolate_string_with_args(s
);
1981 interpolate_string(s
);
1986 nd
= new non_interpreted_char_node('\001');
1990 c
= '0' + do_name_test();
1998 c
= '0' + do_expr_test();
2004 nm
= get_delim_name();
2007 type
= TOKEN_SPECIAL
;
2011 nd
= new vmotion_node(curenv
->get_size() / 2,
2012 curenv
->get_fill_color());
2015 nd
= read_draw_node();
2023 goto handle_escape_char
;
2026 symbol s
= read_escape_name(ALLOW_EMPTY
);
2030 for (p
= s
.contents(); *p
!= '\0'; p
++)
2033 if (*p
|| s
.is_empty())
2034 curenv
->set_font(s
);
2036 curenv
->set_font(atoi(s
.contents()));
2037 if (!compatible_flag
)
2043 symbol s
= read_escape_name(ALLOW_EMPTY
);
2046 curenv
->set_family(s
);
2052 symbol s
= read_escape_name();
2053 if (!(s
.is_null() || s
.is_empty()))
2054 interpolate_number_format(s
);
2058 if (!get_delim_number(&x
, 'm'))
2061 nd
= new hmotion_node(x
, curenv
->get_fill_color());
2064 // don't take height increments relative to previous height if
2065 // in compatibility mode
2066 if (!compatible_flag
&& curenv
->get_char_height()) {
2067 if (get_delim_number(&x
, 'z', curenv
->get_char_height()))
2068 curenv
->set_char_height(x
);
2071 if (get_delim_number(&x
, 'z', curenv
->get_requested_point_size()))
2072 curenv
->set_char_height(x
);
2074 if (!compatible_flag
)
2078 nm
= read_escape_name();
2079 if (nm
.is_null() || nm
.is_empty())
2081 type
= TOKEN_MARK_INPUT
;
2087 if (!get_line_arg(&x
, (cc
== 'l' ? 'm': 'v'), &s
))
2090 s
= get_charinfo(cc
== 'l' ? "ru" : "br");
2092 node
*char_node
= curenv
->make_char_node(s
);
2094 nd
= new hline_node(x
, char_node
);
2096 nd
= new vline_node(x
, char_node
);
2100 do_glyph_color(read_escape_name(ALLOW_EMPTY
));
2101 if (!compatible_flag
)
2105 do_fill_color(read_escape_name(ALLOW_EMPTY
));
2106 if (!compatible_flag
)
2112 symbol s
= read_increment_and_escape_name(&inc
);
2113 if (!(s
.is_null() || s
.is_empty()))
2114 interpolate_number_reg(s
, inc
);
2118 if (!get_delim_number(&val
, 0))
2121 warning(WARN_CHAR
, "invalid numbered character %1", val
);
2124 type
= TOKEN_NUMBERED_CHAR
;
2127 nd
= do_overstrike();
2131 nd
= do_suppress(read_escape_name());
2137 type
= TOKEN_SPREAD
;
2141 nd
= new vmotion_node(-curenv
->get_size(), curenv
->get_fill_color());
2145 if (!compatible_flag
)
2150 curenv
->set_size(x
);
2151 if (!compatible_flag
)
2155 if (get_delim_number(&x
, 0))
2156 curenv
->set_char_slant(x
);
2157 if (!compatible_flag
)
2162 nd
= new non_interpreted_char_node('\t');
2166 nd
= new vmotion_node(-curenv
->get_size() / 2,
2167 curenv
->get_fill_color());
2170 if (!get_delim_number(&x
, 'v'))
2173 nd
= new vmotion_node(x
, curenv
->get_fill_color());
2177 symbol s
= read_escape_name();
2178 if (!(s
.is_null() || s
.is_empty()))
2179 interpolate_environment_variable(s
);
2186 if (!get_delim_number(&x
, 'v'))
2189 nd
= new extra_size_node(x
);
2199 symbol s
= read_escape_name();
2200 if (s
.is_null() || s
.is_empty())
2202 request_or_macro
*p
= lookup_request(s
);
2203 macro
*m
= p
->to_macro();
2205 error("can't transparently throughput a request");
2208 nd
= new special_node(*m
);
2215 if (type
== TOKEN_NODE
)
2216 nd
= new zero_width_node(nd
);
2218 charinfo
*ci
= get_char(1);
2221 node
*gn
= curenv
->make_char_node(ci
);
2224 nd
= new zero_width_node(gn
);
2230 nd
= do_zero_width();
2236 goto ESCAPE_LEFT_BRACE
;
2238 goto ESCAPE_RIGHT_BRACE
;
2242 if (!compatible_flag
) {
2243 symbol s
= read_long_escape_name(WITH_ARGS
);
2244 if (s
.is_null() || s
.is_empty())
2246 if (have_string_arg
) {
2247 have_string_arg
= 0;
2248 nm
= composite_glyph_name(s
);
2251 const char *gn
= check_unicode_name(s
.contents());
2253 const char *gn_decomposed
= decompose_unicode(gn
);
2255 gn
= &gn_decomposed
[1];
2256 const char *groff_gn
= unicode_to_glyph_name(gn
);
2258 nm
= symbol(groff_gn
);
2260 char *buf
= new char[strlen(gn
) + 1 + 1];
2268 nm
= symbol(s
.contents());
2270 type
= TOKEN_SPECIAL
;
2273 goto handle_normal_char
;
2275 if (cc
!= escape_char
&& cc
!= '.')
2276 warning(WARN_ESCAPE
, "escape character ignored before %1",
2277 input_char_description(cc
));
2278 goto handle_normal_char
;
2284 int token::operator==(const token
&t
)
2293 case TOKEN_NUMBERED_CHAR
:
2294 return val
== t
.val
;
2300 int token::operator!=(const token
&t
)
2302 return !(*this == t
);
2305 // is token a suitable delimiter (like ')?
2307 int token::delimiter(int err
)
2336 error("cannot use character `%1' as a starting delimiter", char(c
));
2343 case TOKEN_STRETCHABLE_SPACE
:
2344 case TOKEN_UNSTRETCHABLE_SPACE
:
2348 error("cannot use %1 as a starting delimiter", description());
2355 const char *token::description()
2359 case TOKEN_BACKSPACE
:
2360 return "a backspace character";
2371 case TOKEN_HYPHEN_INDICATOR
:
2373 case TOKEN_INTERRUPT
:
2375 case TOKEN_ITALIC_CORRECTION
:
2378 return "a leader character";
2379 case TOKEN_LEFT_BRACE
:
2381 case TOKEN_MARK_INPUT
:
2387 case TOKEN_NUMBERED_CHAR
:
2389 case TOKEN_RIGHT_BRACE
:
2394 return "a special character";
2397 case TOKEN_STRETCHABLE_SPACE
:
2399 case TOKEN_UNSTRETCHABLE_SPACE
:
2402 return "a tab character";
2403 case TOKEN_TRANSPARENT
:
2405 case TOKEN_TRANSPARENT_DUMMY
:
2407 case TOKEN_ZERO_WIDTH_BREAK
:
2410 return "end of input";
2414 return "a magic token";
2419 while (!tok
.newline())
2430 if (has_arg() && get_integer(&n
))
2431 compatible_flag
= n
!= 0;
2433 compatible_flag
= 1;
2437 static void empty_name_warning(int required
)
2439 if (tok
.newline() || tok
.eof()) {
2441 warning(WARN_MISSING
, "missing name");
2443 else if (tok
.right_brace() || tok
.tab()) {
2444 const char *start
= tok
.description();
2447 } while (tok
.space() || tok
.right_brace() || tok
.tab());
2448 if (!tok
.newline() && !tok
.eof())
2449 error("%1 is not allowed before an argument", start
);
2451 warning(WARN_MISSING
, "missing name");
2454 error("name expected (got %1)", tok
.description());
2456 error("name expected (got %1): treated as missing", tok
.description());
2459 static void non_empty_name_warning()
2461 if (!tok
.newline() && !tok
.eof() && !tok
.space() && !tok
.tab()
2462 && !tok
.right_brace()
2463 // We don't want to give a warning for .el\{
2464 && !tok
.left_brace())
2465 error("%1 is not allowed in a name", tok
.description());
2468 symbol
get_name(int required
)
2470 if (compatible_flag
) {
2473 if ((buf
[0] = tok
.ch()) != 0) {
2475 if ((buf
[1] = tok
.ch()) != 0) {
2480 non_empty_name_warning();
2484 empty_name_warning(required
);
2489 return get_long_name(required
);
2492 symbol
get_long_name(int required
)
2494 return do_get_long_name(required
, 0);
2497 static symbol
do_get_long_name(int required
, char end
)
2501 char abuf
[ABUF_SIZE
];
2503 int buf_size
= ABUF_SIZE
;
2506 // If end != 0 we normally have to append a null byte
2507 if (i
+ 2 > buf_size
) {
2509 buf
= new char[ABUF_SIZE
*2];
2510 memcpy(buf
, abuf
, buf_size
);
2511 buf_size
= ABUF_SIZE
*2;
2514 char *old_buf
= buf
;
2515 buf
= new char[buf_size
*2];
2516 memcpy(buf
, old_buf
, buf_size
);
2521 if ((buf
[i
] = tok
.ch()) == 0 || buf
[i
] == end
)
2527 empty_name_warning(required
);
2530 if (end
&& buf
[i
] == end
)
2533 non_empty_name_warning();
2546 topdiv
->set_last_page();
2547 if (!end_macro_name
.is_null()) {
2548 spring_trap(end_macro_name
);
2550 process_input_stack();
2552 curenv
->final_break();
2554 process_input_stack();
2556 if (topdiv
->get_page_length() > 0) {
2558 topdiv
->set_ejecting();
2559 static unsigned char buf
[2] = { LAST_PAGE_EJECTOR
, '\0' };
2560 input_stack::push(make_temp_iterator((char *)buf
));
2561 topdiv
->space(topdiv
->get_page_length(), 1);
2563 process_input_stack();
2564 seen_last_page_ejector
= 1; // should be set already
2565 topdiv
->set_ejecting();
2566 push_page_ejector();
2567 topdiv
->space(topdiv
->get_page_length(), 1);
2569 process_input_stack();
2571 // This will only happen if a trap-invoked macro starts a diversion,
2572 // or if vertical position traps have been disabled.
2573 cleanup_and_exit(0);
2576 // This implements .ex. The input stack must be cleared before calling
2581 input_stack::clear();
2588 void return_macro_request()
2590 if (has_arg() && tok
.ch())
2591 input_stack::pop_macro();
2592 input_stack::pop_macro();
2598 end_macro_name
= get_name();
2602 void blank_line_macro()
2604 blank_line_macro_name
= get_name();
2608 static void trapping_blank_line()
2610 if (!blank_line_macro_name
.is_null())
2611 spring_trap(blank_line_macro_name
);
2618 int old_compatible_flag
= compatible_flag
;
2619 compatible_flag
= 0;
2620 symbol nm
= get_name();
2624 interpolate_macro(nm
, 1);
2625 compatible_flag
= old_compatible_flag
;
2626 request_or_macro
*p
= lookup_request(nm
);
2627 macro
*m
= p
->to_macro();
2632 inline int possibly_handle_first_page_transition()
2634 if (topdiv
->before_first_page
&& curdiv
== topdiv
&& !curenv
->is_dummy()) {
2635 handle_first_page_transition();
2642 static int transparent_translate(int cc
)
2644 if (!invalid_input_char(cc
)) {
2645 charinfo
*ci
= charset_table
[cc
];
2646 switch (ci
->get_special_translation(1)) {
2647 case charinfo::TRANSLATE_SPACE
:
2649 case charinfo::TRANSLATE_STRETCHABLE_SPACE
:
2650 return ESCAPE_TILDE
;
2651 case charinfo::TRANSLATE_DUMMY
:
2652 return ESCAPE_AMPERSAND
;
2653 case charinfo::TRANSLATE_HYPHEN_INDICATOR
:
2654 return ESCAPE_PERCENT
;
2656 // This is really ugly.
2657 ci
= ci
->get_translation(1);
2659 int c
= ci
->get_ascii_code();
2662 error("can't translate %1 to special character `%2'"
2663 " in transparent throughput",
2664 input_char_description(cc
),
2672 struct int_stack_element
{
2674 int_stack_element
*next
;
2684 int_stack::int_stack()
2689 int_stack::~int_stack()
2692 int_stack_element
*temp
= top
;
2698 int int_stack::is_empty()
2703 void int_stack::push(int n
)
2705 int_stack_element
*p
= new int_stack_element
;
2711 int int_stack::pop()
2714 int_stack_element
*p
= top
;
2721 int node::reread(int *)
2726 int global_diverted_space
= 0;
2728 int diverted_space_node::reread(int *bolp
)
2730 global_diverted_space
= 1;
2731 if (curenv
->get_fill())
2732 trapping_blank_line();
2735 global_diverted_space
= 0;
2740 int diverted_copy_file_node::reread(int *bolp
)
2742 curdiv
->copy_file(filename
.contents());
2747 int word_space_node::reread(int *)
2750 for (width_list
*w
= orig_width
; w
; w
= w
->next
)
2751 curenv
->space(w
->width
, w
->sentence_width
);
2758 int unbreakable_space_node::reread(int *)
2763 int hmotion_node::reread(int *)
2765 if (unformat
&& was_tab
) {
2766 curenv
->handle_tab(0);
2773 void process_input_stack()
2775 int_stack trap_bol_stack
;
2778 int suppress_next
= 0;
2780 case token::TOKEN_CHAR
:
2782 unsigned char ch
= tok
.c
;
2783 if (bol
&& !have_input
2784 && (ch
== curenv
->control_char
2785 || ch
== curenv
->no_break_control_char
)) {
2786 break_flag
= ch
== curenv
->control_char
;
2787 // skip tabs as well as spaces here
2790 } while (tok
.white_space());
2791 symbol nm
= get_name();
2792 #if defined(DEBUGGING)
2794 if (! nm
.is_null()) {
2795 if (strcmp(nm
.contents(), "test") == 0) {
2796 fprintf(stderr
, "found it!\n");
2799 fprintf(stderr
, "interpreting [%s]", nm
.contents());
2800 if (strcmp(nm
.contents(), "di") == 0 && topdiv
!= curdiv
)
2801 fprintf(stderr
, " currently in diversion: %s",
2802 curdiv
->get_diversion_name());
2803 fprintf(stderr
, "\n");
2811 interpolate_macro(nm
);
2812 #if defined(DEBUGGING)
2814 fprintf(stderr
, "finished interpreting [%s] and environment state is\n", nm
.contents());
2815 curenv
->dump_troff_state();
2822 if (possibly_handle_first_page_transition())
2826 #if defined(DEBUGGING)
2828 fprintf(stderr
, "found [%c]\n", ch
); fflush(stderr
);
2831 curenv
->add_char(charset_table
[ch
]);
2833 if (tok
.type
!= token::TOKEN_CHAR
)
2843 case token::TOKEN_TRANSPARENT
:
2846 if (possibly_handle_first_page_transition())
2855 curdiv
->transparent_output(transparent_translate(cc
));
2857 curdiv
->transparent_output(n
);
2859 } while (cc
!= '\n' && cc
!= EOF
);
2861 curdiv
->transparent_output('\n');
2866 case token::TOKEN_NEWLINE
:
2868 if (bol
&& !old_have_input
2869 && !curenv
->get_prev_line_interrupted())
2870 trapping_blank_line();
2877 case token::TOKEN_REQUEST
:
2879 int request_code
= tok
.c
;
2881 switch (request_code
) {
2885 case COPY_FILE_REQUEST
:
2888 case TRANSPARENT_FILE_REQUEST
:
2892 case VJUSTIFY_REQUEST
:
2903 case token::TOKEN_SPACE
:
2905 if (possibly_handle_first_page_transition())
2907 else if (bol
&& !curenv
->get_prev_line_interrupted()) {
2909 // save space_width now so that it isn't changed by \f or \s
2910 // which we wouldn't notice here
2911 hunits space_width
= curenv
->get_space_width();
2913 nspaces
+= tok
.nspaces();
2915 } while (tok
.space());
2917 trapping_blank_line();
2921 curenv
->add_node(new hmotion_node(space_width
* nspaces
,
2922 curenv
->get_fill_color()));
2932 case token::TOKEN_EOF
:
2934 case token::TOKEN_NODE
:
2936 if (possibly_handle_first_page_transition())
2938 else if (tok
.nd
->reread(&bol
)) {
2943 curenv
->add_node(tok
.nd
);
2946 curenv
->possibly_break_line(1);
2950 case token::TOKEN_PAGE_EJECTOR
:
2952 continue_page_eject();
2953 // I think we just want to preserve bol.
2957 case token::TOKEN_BEGIN_TRAP
:
2959 trap_bol_stack
.push(bol
);
2964 case token::TOKEN_END_TRAP
:
2966 if (trap_bol_stack
.is_empty())
2967 error("spurious end trap token detected!");
2969 bol
= trap_bol_stack
.pop();
2972 /* I'm not totally happy about this. But I can't think of any other
2973 way to do it. Doing an output_pending_lines() whenever a
2974 TOKEN_END_TRAP is detected doesn't work: for example,
2987 a\%very\%very\%long\%word
2989 will print all but the first lines from the word immediately
2990 after the footer, rather than on the next page. */
2992 if (trap_bol_stack
.is_empty())
2993 curenv
->output_pending_lines();
3005 trap_sprung_flag
= 0;
3009 #ifdef WIDOW_CONTROL
3011 void flush_pending_lines()
3013 while (!tok
.newline() && !tok
.eof())
3015 curenv
->output_pending_lines();
3019 #endif /* WIDOW_CONTROL */
3021 request_or_macro::request_or_macro()
3025 macro
*request_or_macro::to_macro()
3030 request::request(REQUEST_FUNCP pp
) : p(pp
)
3034 void request::invoke(symbol
, int)
3040 enum { SIZE
= 128 };
3041 unsigned char s
[SIZE
];
3046 char_block::char_block()
3055 void append(unsigned char);
3056 void set(unsigned char, int);
3057 unsigned char get(int);
3064 friend class macro_header
;
3065 friend class string_iterator
;
3068 char_list::char_list()
3069 : ptr(0), len(0), head(0), tail(0)
3073 char_list::~char_list()
3076 char_block
*tem
= head
;
3082 int char_list::length()
3087 void char_list::append(unsigned char c
)
3090 head
= tail
= new char_block
;
3094 if (ptr
>= tail
->s
+ char_block::SIZE
) {
3095 tail
->next
= new char_block
;
3104 void char_list::set(unsigned char c
, int offset
)
3106 assert(len
> offset
);
3107 // optimization for access at the end
3108 int boundary
= len
- len
% char_block::SIZE
;
3109 if (offset
>= boundary
) {
3110 *(tail
->s
+ offset
- boundary
) = c
;
3113 char_block
*tem
= head
;
3116 l
+= char_block::SIZE
;
3118 *(tem
->s
+ offset
% char_block::SIZE
) = c
;
3125 unsigned char char_list::get(int offset
)
3127 assert(len
> offset
);
3128 // optimization for access at the end
3129 int boundary
= len
- len
% char_block::SIZE
;
3130 if (offset
>= boundary
)
3131 return *(tail
->s
+ offset
- boundary
);
3132 char_block
*tem
= head
;
3135 l
+= char_block::SIZE
;
3137 return *(tem
->s
+ offset
% char_block::SIZE
);
3148 void append(node
*);
3152 friend class macro_header
;
3153 friend class string_iterator
;
3156 void node_list::append(node
*n
)
3164 tail
= tail
->next
= n
;
3168 int node_list::length()
3171 for (node
*n
= head
; n
!= 0; n
= n
->next
)
3176 node_list::node_list()
3181 node
*node_list::extract()
3188 node_list::~node_list()
3190 delete_node_list(head
);
3193 class macro_header
{
3198 macro_header() { count
= 1; }
3199 macro_header
*copy(int);
3204 if (p
!= 0 && --(p
->count
) <= 0)
3209 : is_a_diversion(0), is_a_string(1)
3211 if (!input_stack::get_location(1, &filename
, &lineno
)) {
3220 macro::macro(const macro
&m
)
3221 : filename(m
.filename
), lineno(m
.lineno
), len(m
.len
),
3222 empty_macro(m
.empty_macro
), is_a_diversion(m
.is_a_diversion
),
3223 is_a_string(m
.is_a_string
), p(m
.p
)
3229 macro::macro(int is_div
)
3230 : is_a_diversion(is_div
)
3232 if (!input_stack::get_location(1, &filename
, &lineno
)) {
3242 int macro::is_diversion()
3244 return is_a_diversion
;
3247 int macro::is_string()
3252 void macro::clear_string_flag()
3257 macro
¯o::operator=(const macro
&m
)
3259 // don't assign object
3262 if (p
!= 0 && --(p
->count
) <= 0)
3265 filename
= m
.filename
;
3268 empty_macro
= m
.empty_macro
;
3269 is_a_diversion
= m
.is_a_diversion
;
3270 is_a_string
= m
.is_a_string
;
3274 void macro::append(unsigned char c
)
3278 p
= new macro_header
;
3279 if (p
->cl
.length() != len
) {
3280 macro_header
*tem
= p
->copy(len
);
3281 if (--(p
->count
) <= 0)
3287 if (c
!= PUSH_GROFF_MODE
&& c
!= PUSH_COMP_MODE
&& c
!= POP_GROFFCOMP_MODE
)
3291 void macro::set(unsigned char c
, int offset
)
3295 p
->cl
.set(c
, offset
);
3298 unsigned char macro::get(int offset
)
3301 return p
->cl
.get(offset
);
3309 void macro::append_str(const char *s
)
3314 while (s
[i
] != (char)0) {
3321 void macro::append(node
*n
)
3325 p
= new macro_header
;
3326 if (p
->cl
.length() != len
) {
3327 macro_header
*tem
= p
->copy(len
);
3328 if (--(p
->count
) <= 0)
3338 void macro::append_unsigned(unsigned int i
)
3340 unsigned int j
= i
/ 10;
3343 append(((unsigned char)(((int)'0') + i
% 10)));
3346 void macro::append_int(int i
)
3352 append_unsigned((unsigned int)i
);
3355 void macro::print_size()
3357 errprint("%1", len
);
3360 // make a copy of the first n bytes
3362 macro_header
*macro_header::copy(int n
)
3364 macro_header
*p
= new macro_header
;
3365 char_block
*bp
= cl
.head
;
3366 unsigned char *ptr
= bp
->s
;
3369 if (ptr
>= bp
->s
+ char_block::SIZE
) {
3373 unsigned char c
= *ptr
++;
3376 p
->nl
.append(nd
->copy());
3385 object_dictionary_iterator
iter(request_dictionary
);
3386 request_or_macro
*rm
;
3388 while (iter
.get(&s
, (object
**)&rm
)) {
3389 assert(!s
.is_null());
3390 macro
*m
= rm
->to_macro();
3392 errprint("%1\t", s
.contents());
3401 class string_iterator
: public input_iterator
{
3403 const char *how_invoked
;
3407 int count
; // of characters remaining
3409 int saved_compatible_flag
;
3410 int with_break
; // inherited from the caller
3415 string_iterator(const macro
&, const char * = 0, symbol
= NULL_SYMBOL
);
3418 int get_location(int, const char **, int *);
3420 int get_break_flag() { return with_break
; }
3421 void save_compatible_flag(int f
) { saved_compatible_flag
= f
; }
3422 int get_compatible_flag() { return saved_compatible_flag
; }
3426 string_iterator::string_iterator(const macro
&m
, const char *p
, symbol s
)
3427 : input_iterator(m
.is_a_diversion
), mac(m
), how_invoked(p
), newline_flag(0),
3432 bp
= mac
.p
->cl
.head
;
3433 nd
= mac
.p
->nl
.head
;
3441 with_break
= input_stack::get_break_flag();
3444 string_iterator::string_iterator()
3453 with_break
= input_stack::get_break_flag();
3456 int string_iterator::is_diversion()
3458 return mac
.is_diversion();
3461 int string_iterator::fill(node
**np
)
3468 const unsigned char *p
= eptr
;
3469 if (p
>= bp
->s
+ char_block::SIZE
) {
3477 (*np
)->div_nest_level
= input_stack::get_div_level();
3479 (*np
)->div_nest_level
= 0;
3486 const unsigned char *e
= bp
->s
+ char_block::SIZE
;
3491 unsigned char c
= *p
;
3492 if (c
== '\n' || c
== ESCAPE_NEWLINE
) {
3506 int string_iterator::peek()
3510 const unsigned char *p
= eptr
;
3511 if (p
>= bp
->s
+ char_block::SIZE
) {
3517 int string_iterator::get_location(int allow_macro
,
3518 const char **filep
, int *linep
)
3522 if (mac
.filename
== 0)
3524 *filep
= mac
.filename
;
3525 *linep
= mac
.lineno
+ lineno
- 1;
3529 void string_iterator::backtrace()
3532 errprint("%1:%2: backtrace", mac
.filename
, mac
.lineno
+ lineno
- 1);
3535 errprint(": %1 `%2'\n", how_invoked
, nm
.contents());
3537 errprint(": %1\n", how_invoked
);
3544 class temp_iterator
: public input_iterator
{
3545 unsigned char *base
;
3546 temp_iterator(const char *, int len
);
3549 friend input_iterator
*make_temp_iterator(const char *);
3555 temp_iterator::temp_iterator(const char *s
, int len
)
3557 base
= new unsigned char[len
];
3558 memcpy(base
, s
, len
);
3563 temp_iterator::~temp_iterator()
3568 class small_temp_iterator
: public input_iterator
{
3570 small_temp_iterator(const char *, int);
3571 ~small_temp_iterator();
3572 enum { BLOCK
= 16 };
3573 static small_temp_iterator
*free_list
;
3574 void *operator new(size_t);
3575 void operator delete(void *);
3577 unsigned char buf
[SIZE
];
3578 friend input_iterator
*make_temp_iterator(const char *);
3581 small_temp_iterator
*small_temp_iterator::free_list
= 0;
3583 void *small_temp_iterator::operator new(size_t n
)
3585 assert(n
== sizeof(small_temp_iterator
));
3588 (small_temp_iterator
*)new char[sizeof(small_temp_iterator
)*BLOCK
];
3589 for (int i
= 0; i
< BLOCK
- 1; i
++)
3590 free_list
[i
].next
= free_list
+ i
+ 1;
3591 free_list
[BLOCK
-1].next
= 0;
3593 small_temp_iterator
*p
= free_list
;
3594 free_list
= (small_temp_iterator
*)(free_list
->next
);
3602 void small_temp_iterator::operator delete(void *p
)
3605 ((small_temp_iterator
*)p
)->next
= free_list
;
3606 free_list
= (small_temp_iterator
*)p
;
3610 small_temp_iterator::~small_temp_iterator()
3617 small_temp_iterator::small_temp_iterator(const char *s
, int len
)
3619 for (int i
= 0; i
< len
; i
++)
3625 input_iterator
*make_temp_iterator(const char *s
)
3628 return new small_temp_iterator(s
, 0);
3631 if (n
<= small_temp_iterator::SIZE
)
3632 return new small_temp_iterator(s
, n
);
3634 return new temp_iterator(s
, n
);
3638 // this is used when macros with arguments are interpolated
3644 arg_list(const macro
&, int);
3645 arg_list(const arg_list
*);
3649 arg_list::arg_list(const macro
&m
, int s
) : mac(m
), space_follows(s
), next(0)
3653 arg_list::arg_list(const arg_list
*al
)
3657 space_follows
= al
->space_follows
;
3658 arg_list
**a
= &next
;
3659 arg_list
*p
= al
->next
;
3661 *a
= new arg_list(p
->mac
, p
->space_follows
);
3667 arg_list::~arg_list()
3671 class macro_iterator
: public string_iterator
{
3674 int with_break
; // whether called as .foo or 'foo
3676 macro_iterator(symbol
, macro
&, const char * = "macro", int = 0);
3679 int has_args() { return 1; }
3680 input_iterator
*get_arg(int);
3681 arg_list
*get_arg_list();
3682 symbol
get_macro_name();
3683 int space_follows_arg(int);
3684 int get_break_flag() { return with_break
; }
3685 int nargs() { return argc
; }
3686 void add_arg(const macro
&, int);
3688 int is_macro() { return 1; }
3692 input_iterator
*macro_iterator::get_arg(int i
)
3695 return make_temp_iterator(nm
.contents());
3696 if (i
> 0 && i
<= argc
) {
3698 for (int j
= 1; j
< i
; j
++) {
3702 return new string_iterator(p
->mac
);
3708 arg_list
*macro_iterator::get_arg_list()
3713 symbol
macro_iterator::get_macro_name()
3718 int macro_iterator::space_follows_arg(int i
)
3720 if (i
> 0 && i
<= argc
) {
3722 for (int j
= 1; j
< i
; j
++) {
3726 return p
->space_follows
;
3732 void macro_iterator::add_arg(const macro
&m
, int s
)
3735 for (p
= &args
; *p
; p
= &((*p
)->next
))
3737 *p
= new arg_list(m
, s
);
3741 void macro_iterator::shift(int n
)
3743 while (n
> 0 && argc
> 0) {
3744 arg_list
*tem
= args
;
3752 // This gets used by eg .if '\?xxx\?''.
3754 int operator==(const macro
&m1
, const macro
&m2
)
3756 if (m1
.len
!= m2
.len
)
3758 string_iterator
iter1(m1
);
3759 string_iterator
iter2(m2
);
3763 int c1
= iter1
.get(&nd1
);
3766 int c2
= iter2
.get(&nd2
);
3778 int are_same
= nd1
->type() == nd2
->type() && nd1
->same(nd2
);
3788 static void interpolate_macro(symbol nm
, int no_next
)
3790 request_or_macro
*p
= (request_or_macro
*)request_dictionary
.lookup(nm
);
3793 const char *s
= nm
.contents();
3794 if (strlen(s
) > 2) {
3795 request_or_macro
*r
;
3800 r
= (request_or_macro
*)request_dictionary
.lookup(symbol(buf
));
3802 macro
*m
= r
->to_macro();
3803 if (!m
|| !m
->empty())
3804 warned
= warning(WARN_SPACE
,
3805 "macro `%1' not defined "
3806 "(possibly missing space after `%2')",
3807 nm
.contents(), buf
);
3811 warning(WARN_MAC
, "macro `%1' not defined", nm
.contents());
3813 request_dictionary
.define(nm
, p
);
3817 p
->invoke(nm
, no_next
);
3824 static void decode_args(macro_iterator
*mi
)
3826 if (!tok
.newline() && !tok
.eof()) {
3828 int c
= get_copy(&n
);
3832 if (c
== '\n' || c
== EOF
)
3835 int quote_input_level
= 0;
3836 int done_tab_warning
= 0;
3837 arg
.append(compatible_flag
? PUSH_COMP_MODE
: PUSH_GROFF_MODE
);
3838 // we store discarded double quotes for \$^
3840 arg
.append(DOUBLE_QUOTE
);
3841 quote_input_level
= input_stack::get_level();
3844 while (c
!= EOF
&& c
!= '\n' && !(c
== ' ' && quote_input_level
== 0)) {
3845 if (quote_input_level
> 0 && c
== '"'
3847 || input_stack::get_level() == quote_input_level
)) {
3848 arg
.append(DOUBLE_QUOTE
);
3861 if (c
== '\t' && quote_input_level
== 0 && !done_tab_warning
) {
3862 warning(WARN_TAB
, "tab character in unquoted macro argument");
3863 done_tab_warning
= 1;
3870 arg
.append(POP_GROFFCOMP_MODE
);
3871 mi
->add_arg(arg
, (c
== ' '));
3876 static void decode_string_args(macro_iterator
*mi
)
3879 int c
= get_copy(&n
);
3883 if (c
== '\n' || c
== EOF
) {
3884 error("missing `]'");
3890 int quote_input_level
= 0;
3891 int done_tab_warning
= 0;
3893 quote_input_level
= input_stack::get_level();
3896 while (c
!= EOF
&& c
!= '\n'
3897 && !(c
== ']' && quote_input_level
== 0)
3898 && !(c
== ' ' && quote_input_level
== 0)) {
3899 if (quote_input_level
> 0 && c
== '"'
3900 && input_stack::get_level() == quote_input_level
) {
3913 if (c
== '\t' && quote_input_level
== 0 && !done_tab_warning
) {
3914 warning(WARN_TAB
, "tab character in unquoted string argument");
3915 done_tab_warning
= 1;
3922 mi
->add_arg(arg
, (c
== ' '));
3926 void macro::invoke(symbol nm
, int no_next
)
3928 macro_iterator
*mi
= new macro_iterator(nm
, *this);
3930 input_stack::push(mi
);
3931 // we must delay tok.next() in case the function has been called by
3932 // do_request to assure proper handling of compatible_flag
3937 macro
*macro::to_macro()
3944 return empty_macro
== 1;
3947 macro_iterator::macro_iterator(symbol s
, macro
&m
, const char *how_called
,
3949 : string_iterator(m
, how_called
, s
), args(0), argc(0), with_break(break_flag
)
3952 arg_list
*al
= input_stack::get_arg_list();
3954 args
= new arg_list(al
);
3955 argc
= input_stack::nargs();
3960 macro_iterator::macro_iterator() : args(0), argc(0), with_break(break_flag
)
3964 macro_iterator::~macro_iterator()
3967 arg_list
*tem
= args
;
3973 dictionary
composite_dictionary(17);
3975 void composite_request()
3977 symbol from
= get_name(1);
3978 if (!from
.is_null()) {
3979 const char *from_gn
= glyph_name_to_unicode(from
.contents());
3981 from_gn
= check_unicode_name(from
.contents());
3983 error("invalid composite glyph name `%1'", from
.contents());
3988 const char *from_decomposed
= decompose_unicode(from_gn
);
3989 if (from_decomposed
)
3990 from_gn
= &from_decomposed
[1];
3991 symbol to
= get_name(1);
3993 composite_dictionary
.remove(symbol(from_gn
));
3995 const char *to_gn
= glyph_name_to_unicode(to
.contents());
3997 to_gn
= check_unicode_name(to
.contents());
3999 error("invalid composite glyph name `%1'", to
.contents());
4004 const char *to_decomposed
= decompose_unicode(to_gn
);
4006 to_gn
= &to_decomposed
[1];
4007 if (strcmp(from_gn
, to_gn
) == 0)
4008 composite_dictionary
.remove(symbol(from_gn
));
4010 (void)composite_dictionary
.lookup(symbol(from_gn
), (void *)to_gn
);
4016 static symbol
composite_glyph_name(symbol nm
)
4018 macro_iterator
*mi
= new macro_iterator();
4019 decode_string_args(mi
);
4020 input_stack::push(mi
);
4021 const char *gn
= glyph_name_to_unicode(nm
.contents());
4023 gn
= check_unicode_name(nm
.contents());
4025 error("invalid base glyph `%1' in composite glyph name", nm
.contents());
4026 return EMPTY_SYMBOL
;
4029 const char *gn_decomposed
= decompose_unicode(gn
);
4030 string
glyph_name(gn_decomposed
? &gn_decomposed
[1] : gn
);
4032 int n
= input_stack::nargs();
4033 for (int i
= 1; i
<= n
; i
++) {
4035 input_iterator
*p
= input_stack::get_arg(i
);
4038 while ((c
= p
->get(0)) != EOF
)
4039 if (c
!= DOUBLE_QUOTE
)
4042 const char *u
= glyph_name_to_unicode(gl
.contents());
4044 u
= check_unicode_name(gl
.contents());
4046 error("invalid component `%1' in composite glyph name",
4048 return EMPTY_SYMBOL
;
4051 const char *decomposed
= decompose_unicode(u
);
4054 void *mapped_composite
= composite_dictionary
.lookup(symbol(u
));
4055 if (mapped_composite
)
4056 u
= (const char *)mapped_composite
;
4060 const char *groff_gn
= unicode_to_glyph_name(glyph_name
.contents());
4062 return symbol(groff_gn
);
4066 return symbol(gl
.contents());
4069 int trap_sprung_flag
= 0;
4070 int postpone_traps_flag
= 0;
4071 symbol postponed_trap
;
4073 void spring_trap(symbol nm
)
4075 assert(!nm
.is_null());
4076 trap_sprung_flag
= 1;
4077 if (postpone_traps_flag
) {
4078 postponed_trap
= nm
;
4081 static char buf
[2] = { BEGIN_TRAP
, 0 };
4082 static char buf2
[2] = { END_TRAP
, '\0' };
4083 input_stack::push(make_temp_iterator(buf2
));
4084 request_or_macro
*p
= lookup_request(nm
);
4085 macro
*m
= p
->to_macro();
4087 input_stack::push(new macro_iterator(nm
, *m
, "trap-invoked macro"));
4089 error("you can't invoke a request with a trap");
4090 input_stack::push(make_temp_iterator(buf
));
4093 void postpone_traps()
4095 postpone_traps_flag
= 1;
4098 int unpostpone_traps()
4100 postpone_traps_flag
= 0;
4101 if (!postponed_trap
.is_null()) {
4102 spring_trap(postponed_trap
);
4103 postponed_trap
= NULL_SYMBOL
;
4112 macro_iterator
*mi
= new macro_iterator
;
4113 int reading_from_terminal
= isatty(fileno(stdin
));
4115 if (!tok
.newline() && !tok
.eof()) {
4116 int c
= get_copy(0);
4119 while (c
!= EOF
&& c
!= '\n' && c
!= ' ') {
4120 if (!invalid_input_char(c
)) {
4121 if (reading_from_terminal
)
4132 if (reading_from_terminal
) {
4133 fputc(had_prompt
? ':' : '\a', stderr
);
4136 input_stack::push(mi
);
4140 while ((c
= getchar()) != EOF
) {
4141 if (invalid_input_char(c
))
4142 warning(WARN_INPUT
, "invalid input character code %1", int(c
));
4155 if (reading_from_terminal
)
4157 input_stack::push(new string_iterator(mac
));
4161 enum define_mode
{ DEFINE_NORMAL
, DEFINE_APPEND
, DEFINE_IGNORE
};
4162 enum calling_mode
{ CALLING_NORMAL
, CALLING_INDIRECT
};
4163 enum comp_mode
{ COMP_IGNORE
, COMP_DISABLE
, COMP_ENABLE
};
4165 void do_define_string(define_mode mode
, comp_mode comp
)
4168 node
*n
= 0; // pacify compiler
4179 else if (!tok
.space()) {
4180 error("bad string definition");
4191 request_or_macro
*rm
= (request_or_macro
*)request_dictionary
.lookup(nm
);
4192 macro
*mm
= rm
? rm
->to_macro() : 0;
4193 if (mode
== DEFINE_APPEND
&& mm
)
4195 if (comp
== COMP_DISABLE
)
4196 mac
.append(PUSH_GROFF_MODE
);
4197 else if (comp
== COMP_ENABLE
)
4198 mac
.append(PUSH_COMP_MODE
);
4199 while (c
!= '\n' && c
!= EOF
) {
4203 mac
.append((unsigned char)c
);
4206 if (comp
== COMP_DISABLE
|| comp
== COMP_ENABLE
)
4207 mac
.append(POP_GROFFCOMP_MODE
);
4210 request_dictionary
.define(nm
, mm
);
4216 void define_string()
4218 do_define_string(DEFINE_NORMAL
,
4219 compatible_flag
? COMP_ENABLE
: COMP_IGNORE
);
4222 void define_nocomp_string()
4224 do_define_string(DEFINE_NORMAL
, COMP_DISABLE
);
4227 void append_string()
4229 do_define_string(DEFINE_APPEND
,
4230 compatible_flag
? COMP_ENABLE
: COMP_IGNORE
);
4233 void append_nocomp_string()
4235 do_define_string(DEFINE_APPEND
, COMP_DISABLE
);
4238 void do_define_character(char_mode mode
, const char *font_name
)
4240 node
*n
= 0; // pacify compiler
4243 charinfo
*ci
= tok
.get_char(1);
4249 string
s(font_name
);
4251 s
+= ci
->nm
.contents();
4253 ci
= get_charinfo(symbol(s
.contents()));
4260 else if (!tok
.space()) {
4261 error("bad character definition");
4267 while (c
== ' ' || c
== '\t')
4271 macro
*m
= new macro
;
4272 while (c
!= '\n' && c
!= EOF
) {
4276 m
->append((unsigned char)c
);
4279 m
= ci
->setx_macro(m
, mode
);
4285 void define_character()
4287 do_define_character(CHAR_NORMAL
);
4290 void define_fallback_character()
4292 do_define_character(CHAR_FALLBACK
);
4295 void define_special_character()
4297 do_define_character(CHAR_SPECIAL
);
4300 static void remove_character()
4303 while (!tok
.newline() && !tok
.eof()) {
4304 if (!tok
.space() && !tok
.tab()) {
4305 charinfo
*ci
= tok
.get_char(1);
4308 macro
*m
= ci
->set_macro(0);
4317 static void interpolate_string(symbol nm
)
4319 request_or_macro
*p
= lookup_request(nm
);
4320 macro
*m
= p
->to_macro();
4322 error("you can only invoke a string or macro using \\*");
4324 if (m
->is_string()) {
4325 string_iterator
*si
= new string_iterator(*m
, "string", nm
);
4326 input_stack::push(si
);
4329 // if a macro is called as a string, \$0 doesn't get changed
4330 macro_iterator
*mi
= new macro_iterator(input_stack::get_macro_name(),
4332 input_stack::push(mi
);
4337 static void interpolate_string_with_args(symbol s
)
4339 request_or_macro
*p
= lookup_request(s
);
4340 macro
*m
= p
->to_macro();
4342 error("you can only invoke a string or macro using \\*");
4344 macro_iterator
*mi
= new macro_iterator(s
, *m
);
4345 decode_string_args(mi
);
4346 input_stack::push(mi
);
4350 static void interpolate_arg(symbol nm
)
4352 const char *s
= nm
.contents();
4353 if (!s
|| *s
== '\0')
4354 copy_mode_error("missing argument name");
4355 else if (s
[1] == 0 && csdigit(s
[0]))
4356 input_stack::push(input_stack::get_arg(s
[0] - '0'));
4357 else if (s
[0] == '*' && s
[1] == '\0') {
4358 int limit
= input_stack::nargs();
4360 for (int i
= 1; i
<= limit
; i
++) {
4361 input_iterator
*p
= input_stack::get_arg(i
);
4363 while ((c
= p
->get(0)) != EOF
)
4364 if (c
!= DOUBLE_QUOTE
)
4371 input_stack::push(make_temp_iterator(args
.contents()));
4374 else if (s
[0] == '@' && s
[1] == '\0') {
4375 int limit
= input_stack::nargs();
4377 for (int i
= 1; i
<= limit
; i
++) {
4379 args
+= char(BEGIN_QUOTE
);
4380 input_iterator
*p
= input_stack::get_arg(i
);
4382 while ((c
= p
->get(0)) != EOF
)
4383 if (c
!= DOUBLE_QUOTE
)
4385 args
+= char(END_QUOTE
);
4392 input_stack::push(make_temp_iterator(args
.contents()));
4395 else if (s
[0] == '^' && s
[1] == '\0') {
4396 int limit
= input_stack::nargs();
4398 int c
= input_stack::peek();
4399 for (int i
= 1; i
<= limit
; i
++) {
4400 input_iterator
*p
= input_stack::get_arg(i
);
4401 while ((c
= p
->get(0)) != EOF
) {
4402 if (c
== DOUBLE_QUOTE
)
4406 if (input_stack::space_follows_arg(i
))
4411 input_stack::push(make_temp_iterator(args
.contents()));
4416 for (p
= s
; *p
&& csdigit(*p
); p
++)
4419 copy_mode_error("bad argument name `%1'", s
);
4421 input_stack::push(input_stack::get_arg(atoi(s
)));
4425 void handle_first_page_transition()
4428 topdiv
->begin_page();
4431 // We push back a token by wrapping it up in a token_node, and
4432 // wrapping that up in a string_iterator.
4434 static void push_token(const token
&t
)
4437 m
.append(new token_node(t
));
4438 input_stack::push(new string_iterator(m
));
4441 void push_page_ejector()
4443 static char buf
[2] = { PAGE_EJECTOR
, '\0' };
4444 input_stack::push(make_temp_iterator(buf
));
4447 void handle_initial_request(unsigned char code
)
4453 mac
.append(new token_node(tok
));
4454 input_stack::push(new string_iterator(mac
));
4455 input_stack::push(make_temp_iterator(buf
));
4456 topdiv
->begin_page();
4460 void handle_initial_title()
4462 handle_initial_request(TITLE_REQUEST
);
4465 // this should be local to define_macro, but cfront 1.2 doesn't support that
4466 static symbol
dot_symbol(".");
4468 void do_define_macro(define_mode mode
, calling_mode calling
, comp_mode comp
)
4471 if (calling
== CALLING_INDIRECT
) {
4472 symbol temp1
= get_name(1);
4473 if (temp1
.is_null()) {
4477 symbol temp2
= get_name();
4478 input_stack::push(make_temp_iterator("\n"));
4479 if (!temp2
.is_null()) {
4480 interpolate_string(temp2
);
4481 input_stack::push(make_temp_iterator(" "));
4483 interpolate_string(temp1
);
4484 input_stack::push(make_temp_iterator(" "));
4487 if (mode
== DEFINE_NORMAL
|| mode
== DEFINE_APPEND
) {
4494 term
= get_name(); // the request that terminates the definition
4497 while (!tok
.newline() && !tok
.eof())
4499 const char *start_filename
;
4501 int have_start_location
= input_stack::get_location(0, &start_filename
,
4504 // doing this here makes the line numbers come out right
4505 int c
= get_copy(&n
, 1);
4508 if (mode
== DEFINE_NORMAL
|| mode
== DEFINE_APPEND
) {
4509 request_or_macro
*rm
=
4510 (request_or_macro
*)request_dictionary
.lookup(nm
);
4512 mm
= rm
->to_macro();
4513 if (mm
&& mode
== DEFINE_APPEND
)
4517 if (comp
== COMP_DISABLE
)
4518 mac
.append(PUSH_GROFF_MODE
);
4519 else if (comp
== COMP_ENABLE
)
4520 mac
.append(PUSH_COMP_MODE
);
4523 mac
.clear_string_flag();
4524 while (c
== ESCAPE_NEWLINE
) {
4525 if (mode
== DEFINE_NORMAL
|| mode
== DEFINE_APPEND
)
4527 c
= get_copy(&n
, 1);
4529 if (bol
&& c
== '.') {
4530 const char *s
= term
.contents();
4532 // see if it matches term
4535 while ((d
= get_copy(&n
)) == ' ' || d
== '\t')
4537 if ((unsigned char)s
[0] == d
) {
4538 for (i
= 1; s
[i
] != 0; i
++) {
4540 if ((unsigned char)s
[i
] != d
)
4546 && ((i
== 2 && compatible_flag
)
4547 || (d
= get_copy(&n
)) == ' '
4548 || d
== '\n')) { // we found it
4553 if (mode
== DEFINE_APPEND
|| mode
== DEFINE_NORMAL
) {
4556 request_dictionary
.define(nm
, mm
);
4558 if (comp
== COMP_DISABLE
|| comp
== COMP_ENABLE
)
4559 mac
.append(POP_GROFFCOMP_MODE
);
4562 if (term
!= dot_symbol
) {
4564 interpolate_macro(term
);
4570 if (mode
== DEFINE_APPEND
|| mode
== DEFINE_NORMAL
) {
4572 for (int j
= 0; j
< i
; j
++)
4578 if (mode
== DEFINE_NORMAL
|| mode
== DEFINE_APPEND
) {
4579 if (have_start_location
)
4580 error_with_file_and_line(start_filename
, start_lineno
,
4581 "end of file while defining macro `%1'",
4584 error("end of file while defining macro `%1'", nm
.contents());
4587 if (have_start_location
)
4588 error_with_file_and_line(start_filename
, start_lineno
,
4589 "end of file while ignoring input lines");
4591 error("end of file while ignoring input lines");
4596 if (mode
== DEFINE_NORMAL
|| mode
== DEFINE_APPEND
) {
4603 c
= get_copy(&n
, 1);
4609 do_define_macro(DEFINE_NORMAL
, CALLING_NORMAL
,
4610 compatible_flag
? COMP_ENABLE
: COMP_IGNORE
);
4613 void define_nocomp_macro()
4615 do_define_macro(DEFINE_NORMAL
, CALLING_NORMAL
, COMP_DISABLE
);
4618 void define_indirect_macro()
4620 do_define_macro(DEFINE_NORMAL
, CALLING_INDIRECT
,
4621 compatible_flag
? COMP_ENABLE
: COMP_IGNORE
);
4624 void define_indirect_nocomp_macro()
4626 do_define_macro(DEFINE_NORMAL
, CALLING_INDIRECT
, COMP_DISABLE
);
4631 do_define_macro(DEFINE_APPEND
, CALLING_NORMAL
,
4632 compatible_flag
? COMP_ENABLE
: COMP_IGNORE
);
4635 void append_nocomp_macro()
4637 do_define_macro(DEFINE_APPEND
, CALLING_NORMAL
, COMP_DISABLE
);
4640 void append_indirect_macro()
4642 do_define_macro(DEFINE_APPEND
, CALLING_INDIRECT
,
4643 compatible_flag
? COMP_ENABLE
: COMP_IGNORE
);
4646 void append_indirect_nocomp_macro()
4648 do_define_macro(DEFINE_APPEND
, CALLING_INDIRECT
, COMP_DISABLE
);
4654 do_define_macro(DEFINE_IGNORE
, CALLING_NORMAL
, COMP_IGNORE
);
4661 symbol s
= get_name();
4664 request_dictionary
.remove(s
);
4671 symbol s1
= get_name(1);
4672 if (!s1
.is_null()) {
4673 symbol s2
= get_name(1);
4675 request_dictionary
.rename(s1
, s2
);
4682 symbol s1
= get_name(1);
4683 if (!s1
.is_null()) {
4684 symbol s2
= get_name(1);
4685 if (!s2
.is_null()) {
4686 if (!request_dictionary
.alias(s1
, s2
))
4687 warning(WARN_MAC
, "macro `%1' not defined", s2
.contents());
4695 symbol s
= get_name(1);
4697 request_or_macro
*p
= lookup_request(s
);
4698 macro
*m
= p
->to_macro();
4700 error("cannot chop request");
4701 else if (m
->empty())
4702 error("cannot chop empty macro");
4704 int have_restore
= 0;
4705 // we have to check for additional save/restore pairs which could be
4706 // there due to empty am1 requests.
4708 if (m
->get(m
->len
- 1) != POP_GROFFCOMP_MODE
)
4712 if (m
->get(m
->len
- 1) != PUSH_GROFF_MODE
4713 && m
->get(m
->len
- 1) != PUSH_COMP_MODE
)
4721 error("cannot chop empty macro");
4724 m
->set(POP_GROFFCOMP_MODE
, m
->len
- 1);
4733 void substring_request()
4735 int start
; // 0, 1, ..., n-1 or -1, -2, ...
4736 symbol s
= get_name(1);
4737 if (!s
.is_null() && get_integer(&start
)) {
4738 request_or_macro
*p
= lookup_request(s
);
4739 macro
*m
= p
->to_macro();
4741 error("cannot apply `substring' on a request");
4744 if (!has_arg() || get_integer(&end
)) {
4745 int real_length
= 0; // 1, 2, ..., n
4746 string_iterator
iter1(*m
);
4747 for (int l
= 0; l
< m
->len
; l
++) {
4748 int c
= iter1
.get(0);
4749 if (c
== PUSH_GROFF_MODE
4750 || c
== PUSH_COMP_MODE
4751 || c
== POP_GROFFCOMP_MODE
)
4758 start
+= real_length
;
4766 if (start
>= real_length
|| end
< 0) {
4768 "start and end index of substring out of range");
4771 if (--(m
->p
->count
) <= 0)
4780 "start index of substring out of range, set to 0");
4783 if (end
>= real_length
) {
4785 "end index of substring out of range, set to string length");
4786 end
= real_length
- 1;
4788 // now extract the substring
4789 string_iterator
iter(*m
);
4791 for (i
= 0; i
< start
; i
++) {
4792 int c
= iter
.get(0);
4793 while (c
== PUSH_GROFF_MODE
4794 || c
== PUSH_COMP_MODE
4795 || c
== POP_GROFFCOMP_MODE
)
4801 for (; i
<= end
; i
++) {
4802 node
*nd
= 0; // pacify compiler
4803 int c
= iter
.get(&nd
);
4804 while (c
== PUSH_GROFF_MODE
4805 || c
== PUSH_COMP_MODE
4806 || c
== POP_GROFFCOMP_MODE
)
4813 mac
.append((unsigned char)c
);
4822 void length_request()
4826 if (ret
.is_null()) {
4836 else if (!tok
.space()) {
4837 error("bad string definition");
4848 while (c
!= '\n' && c
!= EOF
) {
4852 reg
*r
= (reg
*)number_reg_dictionary
.lookup(ret
);
4856 set_number_reg(ret
, len
);
4860 void asciify_macro()
4862 symbol s
= get_name(1);
4864 request_or_macro
*p
= lookup_request(s
);
4865 macro
*m
= p
->to_macro();
4867 error("cannot asciify request");
4870 string_iterator
iter(*m
);
4872 node
*nd
= 0; // pacify compiler
4873 int c
= iter
.get(&nd
);
4887 void unformat_macro()
4889 symbol s
= get_name(1);
4891 request_or_macro
*p
= lookup_request(s
);
4892 macro
*m
= p
->to_macro();
4894 error("cannot unformat request");
4897 string_iterator
iter(*m
);
4899 node
*nd
= 0; // pacify compiler
4900 int c
= iter
.get(&nd
);
4906 if (nd
->set_unformat_flag())
4916 static void interpolate_environment_variable(symbol nm
)
4918 const char *s
= getenv(nm
.contents());
4920 input_stack::push(make_temp_iterator(s
));
4923 void interpolate_number_reg(symbol nm
, int inc
)
4925 reg
*r
= lookup_number_reg(nm
);
4930 input_stack::push(make_temp_iterator(r
->get_string()));
4933 static void interpolate_number_format(symbol nm
)
4935 reg
*r
= (reg
*)number_reg_dictionary
.lookup(nm
);
4937 input_stack::push(make_temp_iterator(r
->get_format()));
4940 static int get_delim_number(units
*n
, unsigned char si
, int prev_value
)
4944 if (start
.delimiter(1)) {
4946 if (get_number(n
, si
, prev_value
)) {
4948 warning(WARN_DELIM
, "closing delimiter does not match");
4955 static int get_delim_number(units
*n
, unsigned char si
)
4959 if (start
.delimiter(1)) {
4961 if (get_number(n
, si
)) {
4963 warning(WARN_DELIM
, "closing delimiter does not match");
4970 static int get_line_arg(units
*n
, unsigned char si
, charinfo
**cp
)
4974 int start_level
= input_stack::get_level();
4975 if (!start
.delimiter(1))
4978 if (get_number(n
, si
)) {
4979 if (tok
.dummy() || tok
.transparent_dummy())
4981 if (!(start
== tok
&& input_stack::get_level() == start_level
)) {
4982 *cp
= tok
.get_char(1);
4985 if (!(start
== tok
&& input_stack::get_level() == start_level
))
4986 warning(WARN_DELIM
, "closing delimiter does not match");
4992 static int read_size(int *x
)
5002 else if (c
== '+') {
5007 int val
= 0; // pacify compiler
5013 // allow an increment either before or after the left parenthesis
5019 else if (c
== '+') {
5034 val
= val
*10 + (c
- '0');
5039 else if (csdigit(c
)) {
5041 if (!inc
&& c
!= '0' && c
< '4') {
5047 val
= val
*10 + (c
- '0');
5051 else if (!tok
.delimiter(1))
5057 if (!inc
&& (c
== '-' || c
== '+')) {
5058 inc
= c
== '+' ? 1 : -1;
5061 if (!get_number(&val
, 'z'))
5063 if (!(start
.ch() == '[' && tok
.ch() == ']') && start
!= tok
) {
5064 if (start
.ch() == '[')
5065 error("missing `]'");
5067 error("missing closing delimiter");
5075 // special case -- \s[0] and \s0 means to revert to previous size
5082 *x
= curenv
->get_requested_point_size() + val
;
5085 *x
= curenv
->get_requested_point_size() - val
;
5092 "\\s escape results in non-positive point size; set to 1");
5098 error("bad digit in point size");
5103 static symbol
get_delim_name()
5108 error("end of input at start of delimited name");
5111 if (start
.newline()) {
5112 error("can't delimit name with a newline");
5115 int start_level
= input_stack::get_level();
5116 char abuf
[ABUF_SIZE
];
5118 int buf_size
= ABUF_SIZE
;
5121 if (i
+ 1 > buf_size
) {
5123 buf
= new char[ABUF_SIZE
*2];
5124 memcpy(buf
, abuf
, buf_size
);
5125 buf_size
= ABUF_SIZE
*2;
5128 char *old_buf
= buf
;
5129 buf
= new char[buf_size
*2];
5130 memcpy(buf
, old_buf
, buf_size
);
5137 && (compatible_flag
|| input_stack::get_level() == start_level
))
5139 if ((buf
[i
] = tok
.ch()) == 0) {
5140 error("missing delimiter (got %1)", tok
.description());
5150 error("empty delimited name");
5165 static void do_register()
5169 if (!start
.delimiter(1))
5172 symbol nm
= get_long_name(1);
5177 reg
*r
= (reg
*)number_reg_dictionary
.lookup(nm
);
5179 if (!r
|| !r
->get_value(&prev_value
))
5182 if (!get_number(&val
, 'u', prev_value
))
5185 warning(WARN_DELIM
, "closing delimiter does not match");
5189 set_number_reg(nm
, val
);
5192 // this implements the \w escape sequence
5194 static void do_width()
5198 int start_level
= input_stack::get_level();
5199 environment
env(curenv
);
5200 environment
*oldenv
= curenv
;
5205 warning(WARN_DELIM
, "missing closing delimiter");
5208 if (tok
.newline()) {
5209 warning(WARN_DELIM
, "missing closing delimiter");
5210 input_stack::push(make_temp_iterator("\n"));
5214 && (compatible_flag
|| input_stack::get_level() == start_level
))
5219 units x
= env
.get_input_line_position().to_units();
5220 input_stack::push(make_temp_iterator(i_to_a(x
)));
5221 env
.width_registers();
5226 charinfo
*page_character
;
5228 void set_page_character()
5230 page_character
= get_optional_char();
5234 static const symbol
percent_symbol("%");
5236 void read_title_parts(node
**part
, hunits
*part_width
)
5239 if (tok
.newline() || tok
.eof())
5242 int start_level
= input_stack::get_level();
5244 for (int i
= 0; i
< 3; i
++) {
5245 while (!tok
.newline() && !tok
.eof()) {
5247 && (compatible_flag
|| input_stack::get_level() == start_level
)) {
5251 if (page_character
!= 0 && tok
.get_char() == page_character
)
5252 interpolate_number_reg(percent_symbol
, 0);
5257 curenv
->wrap_up_tab();
5258 part_width
[i
] = curenv
->get_input_line_position();
5259 part
[i
] = curenv
->extract_output_line();
5261 while (!tok
.newline() && !tok
.eof())
5265 class non_interpreted_node
: public node
{
5268 non_interpreted_node(const macro
&);
5269 int interpret(macro
*);
5271 int ends_sentence();
5278 non_interpreted_node::non_interpreted_node(const macro
&m
) : mac(m
)
5282 int non_interpreted_node::ends_sentence()
5287 int non_interpreted_node::same(node
*nd
)
5289 return mac
== ((non_interpreted_node
*)nd
)->mac
;
5292 const char *non_interpreted_node::type()
5294 return "non_interpreted_node";
5297 int non_interpreted_node::force_tprint()
5302 int non_interpreted_node::is_tag()
5307 node
*non_interpreted_node::copy()
5309 return new non_interpreted_node(mac
);
5312 int non_interpreted_node::interpret(macro
*m
)
5314 string_iterator
si(mac
);
5315 node
*n
= 0; // pacify compiler
5328 static node
*do_non_interpreted()
5333 while ((c
= get_copy(&n
)) != ESCAPE_QUESTION
&& c
!= EOF
&& c
!= '\n')
5338 if (c
== EOF
|| c
== '\n') {
5339 error("missing \\?");
5342 return new non_interpreted_node(mac
);
5345 static void encode_char(macro
*mac
, char c
)
5348 if ((font::use_charnames_in_special
) && tok
.special()) {
5349 charinfo
*ci
= tok
.get_char(1);
5350 const char *s
= ci
->get_symbol()->contents();
5351 if (s
[0] != (char)0) {
5355 while (s
[i
] != (char)0) {
5362 else if (tok
.stretchable_space()
5363 || tok
.unstretchable_space())
5365 else if (!(tok
.hyphen_indicator()
5367 || tok
.transparent_dummy()
5368 || tok
.zero_width_break()))
5369 error("%1 is invalid within \\X", tok
.description());
5372 if ((font::use_charnames_in_special
) && (c
== '\\')) {
5374 * add escape escape sequence
5386 int start_level
= input_stack::get_level();
5389 tok
!= start
|| input_stack::get_level() != start_level
;
5392 warning(WARN_DELIM
, "missing closing delimiter");
5395 if (tok
.newline()) {
5396 input_stack::push(make_temp_iterator("\n"));
5397 warning(WARN_DELIM
, "missing closing delimiter");
5405 else if (tok
.leader())
5407 else if (tok
.backspace())
5411 encode_char(&mac
, c
);
5413 return new special_node(mac
);
5416 void device_request()
5418 if (!tok
.newline() && !tok
.eof()) {
5427 if (c
!= ' ' && c
!= '\t')
5430 for (; c
!= '\n' && c
!= EOF
; c
= get_copy(0))
5432 curenv
->add_node(new special_node(mac
));
5437 void device_macro_request()
5439 symbol s
= get_name(1);
5440 if (!(s
.is_null() || s
.is_empty())) {
5441 request_or_macro
*p
= lookup_request(s
);
5442 macro
*m
= p
->to_macro();
5444 curenv
->add_node(new special_node(*m
));
5446 error("can't transparently throughput a request");
5451 void output_request()
5453 if (!tok
.newline() && !tok
.eof()) {
5461 if (c
!= ' ' && c
!= '\t')
5464 for (; c
!= '\n' && c
!= EOF
; c
= get_copy(0))
5465 topdiv
->transparent_output(c
);
5466 topdiv
->transparent_output('\n');
5471 extern int image_no
; // from node.cpp
5473 static node
*do_suppress(symbol nm
)
5475 if (nm
.is_null() || nm
.is_empty()) {
5476 error("expecting an argument to escape \\O");
5479 const char *s
= nm
.contents();
5482 if (begin_level
== 0)
5483 // suppress generation of glyphs
5484 return new suppress_node(0, 0);
5487 if (begin_level
== 0)
5488 // enable generation of glyphs
5489 return new suppress_node(1, 0);
5492 if (begin_level
== 0)
5493 return new suppress_node(1, 1);
5505 s
++; // move over '5'
5507 if (*s
== (char)0) {
5508 error("missing position and filename in \\O");
5511 if (!(position
== 'l'
5514 || position
== 'i')) {
5515 error("l, r, c, or i position expected (got %1 in \\O)", position
);
5518 s
++; // onto image name
5519 if (s
== (char *)0) {
5520 error("missing image name for \\O");
5524 if (begin_level
== 0)
5525 return new suppress_node(symbol(s
), position
, image_no
);
5531 error("`%1' is an invalid argument to \\O", *s
);
5536 void special_node::tprint(troff_output_file
*out
)
5539 string_iterator
iter(mac
);
5541 int c
= iter
.get(0);
5544 for (const char *s
= ::asciify(c
); *s
; s
++)
5545 tprint_char(out
, *s
);
5550 int get_file_line(const char **filename
, int *lineno
)
5552 return input_stack::get_location(0, filename
, lineno
);
5558 if (get_integer(&n
)) {
5559 const char *filename
= 0;
5561 symbol s
= get_long_name();
5562 filename
= s
.contents();
5564 (void)input_stack::set_location(filename
, n
-1);
5569 static int nroff_mode
= 0;
5571 static void nroff_request()
5577 static void troff_request()
5583 static void skip_alternative()
5586 // ensure that ``.if 0\{'' works as expected
5587 if (tok
.left_brace())
5591 c
= input_stack::get(0);
5594 if (c
== ESCAPE_LEFT_BRACE
)
5596 else if (c
== ESCAPE_RIGHT_BRACE
)
5598 else if (c
== escape_char
&& escape_char
> 0)
5599 switch(input_stack::get(0)) {
5607 while ((c
= input_stack::get(0)) != '\n' && c
!= EOF
)
5611 Note that the level can properly be < 0, eg
5617 So don't give an error message in this case.
5619 if (level
<= 0 && c
== '\n')
5625 static void begin_alternative()
5627 while (tok
.space() || tok
.left_brace())
5637 static int_stack if_else_stack
;
5644 while (tok
.ch() == '!') {
5649 unsigned char c
= tok
.ch();
5652 result
= !nroff_mode
;
5654 else if (c
== 'n') {
5656 result
= nroff_mode
;
5658 else if (c
== 'v') {
5662 else if (c
== 'o') {
5663 result
= (topdiv
->get_page_number() & 1);
5666 else if (c
== 'e') {
5667 result
= !(topdiv
->get_page_number() & 1);
5670 else if (c
== 'd' || c
== 'r') {
5672 symbol nm
= get_name(1);
5678 ? request_dictionary
.lookup(nm
) != 0
5679 : number_reg_dictionary
.lookup(nm
) != 0);
5681 else if (c
== 'm') {
5683 symbol nm
= get_long_name(1);
5688 result
= (nm
== default_symbol
5689 || color_dictionary
.lookup(nm
) != 0);
5691 else if (c
== 'c') {
5694 charinfo
*ci
= tok
.get_char(1);
5699 result
= character_exists(ci
, curenv
);
5702 else if (c
== 'F') {
5704 symbol nm
= get_long_name(1);
5709 result
= check_font(curenv
->get_family()->nm
, nm
);
5711 else if (c
== 'S') {
5713 symbol nm
= get_long_name(1);
5718 result
= check_style(nm
);
5720 else if (tok
.space())
5722 else if (tok
.delimiter()) {
5724 int delim_level
= input_stack::get_level();
5725 environment
env1(curenv
);
5726 environment
env2(curenv
);
5727 environment
*oldenv
= curenv
;
5730 for (int i
= 0; i
< 2; i
++) {
5733 if (tok
.newline() || tok
.eof()) {
5734 warning(WARN_DELIM
, "missing closing delimiter");
5740 && (compatible_flag
|| input_stack::get_level() == delim_level
))
5746 node
*n1
= env1
.extract_output_line();
5747 node
*n2
= env2
.extract_output_line();
5748 result
= same_node_list(n1
, n2
);
5749 delete_node_list(n1
);
5750 delete_node_list(n2
);
5758 if (!get_number(&n
, 'u')) {
5768 begin_alternative();
5774 void if_else_request()
5776 if_else_stack
.push(do_if_request());
5786 if (if_else_stack
.is_empty()) {
5787 warning(WARN_EL
, "unbalanced .el request");
5791 if (if_else_stack
.pop())
5794 begin_alternative();
5798 static int while_depth
= 0;
5799 static int while_break_flag
= 0;
5801 void while_request()
5806 mac
.append(new token_node(tok
));
5808 node
*n
= 0; // pacify compiler
5809 int c
= input_stack::get(&n
);
5825 if (c
== ESCAPE_LEFT_BRACE
)
5827 else if (c
== ESCAPE_RIGHT_BRACE
)
5829 else if (c
== escape_char
)
5832 if (c
== '\n' && level
<= 0)
5837 error("unbalanced \\{ \\}");
5840 input_stack::add_boundary();
5842 input_stack::push(new string_iterator(mac
, "while loop"));
5844 if (!do_if_request()) {
5845 while (input_stack::get(0) != EOF
)
5849 process_input_stack();
5850 if (while_break_flag
|| input_stack::is_return_boundary()) {
5851 while_break_flag
= 0;
5855 input_stack::remove_boundary();
5861 void while_break_request()
5864 error("no while loop");
5868 while_break_flag
= 1;
5869 while (input_stack::get(0) != EOF
)
5875 void while_continue_request()
5878 error("no while loop");
5882 while (input_stack::get(0) != EOF
)
5892 symbol nm
= get_long_name(1);
5896 while (!tok
.newline() && !tok
.eof())
5898 file_case
*fcp
= include_search_path
.open_file_cautious(nm
.contents(),
5901 input_stack::push(new file_iterator(fcp
, nm
.contents()));
5903 error("can't open `%1': %2", nm
.contents(), strerror(errno
));
5908 // like .so but use popen()
5913 error(".pso request not allowed in safer mode");
5917 #ifdef POPEN_MISSING
5918 error("pipes not available on this system");
5920 #else /* not POPEN_MISSING */
5921 if (tok
.newline() || tok
.eof())
5922 error("missing command");
5925 while ((c
= get_copy(0)) == ' ' || c
== '\t')
5928 char *buf
= new char[buf_size
];
5930 for (; c
!= '\n' && c
!= EOF
; c
= get_copy(0)) {
5931 const char *s
= asciify(c
);
5932 int slen
= strlen(s
);
5933 if (buf_used
+ slen
+ 1> buf_size
) {
5934 char *old_buf
= buf
;
5935 int old_buf_size
= buf_size
;
5937 buf
= new char[buf_size
];
5938 memcpy(buf
, old_buf
, old_buf_size
);
5941 strcpy(buf
+ buf_used
, s
);
5944 buf
[buf_used
] = '\0';
5946 FILE *fp
= popen(buf
, POPEN_RT
);
5948 input_stack::push(new file_iterator(
5949 new file_case(fp
, buf
, file_case::fc_pipe
| file_case::fc_take_path
),
5950 symbol(buf
).contents()));
5952 error("can't open pipe to process `%1': %2", buf
, strerror(errno
));
5957 #endif /* not POPEN_MISSING */
5963 static int llx_reg_contents
= 0;
5964 static int lly_reg_contents
= 0;
5965 static int urx_reg_contents
= 0;
5966 static int ury_reg_contents
= 0;
5968 struct bounding_box
{
5969 int llx
, lly
, urx
, ury
;
5972 /* Parse the argument to a %%BoundingBox comment. Return 1 if it
5973 contains 4 numbers, 2 if it contains (atend), 0 otherwise. */
5975 int parse_bounding_box(char *p
, bounding_box
*bb
)
5977 if (sscanf(p
, "%d %d %d %d",
5978 &bb
->llx
, &bb
->lly
, &bb
->urx
, &bb
->ury
) == 4)
5981 /* The Document Structuring Conventions say that the numbers
5982 should be integers. Unfortunately some broken applications
5984 double x1
, x2
, x3
, x4
;
5985 if (sscanf(p
, "%lf %lf %lf %lf", &x1
, &x2
, &x3
, &x4
) == 4) {
5993 for (; *p
== ' ' || *p
== '\t'; p
++)
5995 if (strncmp(p
, "(atend)", 7) == 0) {
6000 bb
->llx
= bb
->lly
= bb
->urx
= bb
->ury
= 0;
6004 // This version is taken from psrm.cpp
6006 #define PS_LINE_MAX 255
6007 cset
white_space("\n\r \t");
6009 int ps_get_line(char *buf
, file_case
*fcp
, const char* filename
)
6011 int c
= fcp
->get_c();
6018 while (c
!= '\r' && c
!= '\n' && c
!= EOF
) {
6019 if ((c
< 0x1b && !white_space(c
)) || c
== 0x7f)
6020 error("invalid input character code %1 in `%2'", int(c
), filename
);
6021 else if (i
< PS_LINE_MAX
)
6025 error("PostScript file `%1' is non-conforming "
6026 "because length of line exceeds 255", filename
);
6034 if (c
!= EOF
&& c
!= '\n')
6040 inline void assign_registers(int llx
, int lly
, int urx
, int ury
)
6042 llx_reg_contents
= llx
;
6043 lly_reg_contents
= lly
;
6044 urx_reg_contents
= urx
;
6045 ury_reg_contents
= ury
;
6048 void do_ps_file(file_case
*fcp
, const char* filename
)
6052 char buf
[PS_LINE_MAX
];
6053 llx_reg_contents
= lly_reg_contents
=
6054 urx_reg_contents
= ury_reg_contents
= 0;
6055 if (!ps_get_line(buf
, fcp
, filename
)) {
6056 error("`%1' is empty", filename
);
6059 if (strncmp("%!PS-Adobe-", buf
, 11) != 0) {
6060 error("`%1' is not conforming to the Document Structuring Conventions",
6064 while (ps_get_line(buf
, fcp
, filename
) != 0) {
6065 // in header comments, `%X' (`X' any printable character except
6066 // whitespace) is possible too
6067 if (buf
[0] == '%') {
6068 if (strncmp(buf
+ 1, "%EndComments", 12) == 0)
6070 if (white_space(buf
[1]))
6075 if (strncmp(buf
+ 1, "%BoundingBox:", 13) == 0) {
6076 int res
= parse_bounding_box(buf
+ 14, &bb
);
6078 assign_registers(bb
.llx
, bb
.lly
, bb
.urx
, bb
.ury
);
6081 else if (res
== 2) {
6086 error("the arguments to the %%%%BoundingBox comment in `%1' are bad",
6095 // in the trailer, the last BoundingBox comment is significant
6096 for (offset
= 512; !last_try
; offset
*= 2) {
6097 int had_trailer
= 0;
6099 if (offset
> 32768 || fcp
->seek(-offset
, fcp
->seek_end
) == -1) {
6101 if (fcp
->seek(0L, fcp
->seek_set
) == -1)
6104 while (ps_get_line(buf
, fcp
, filename
) != 0) {
6105 if (buf
[0] == '%' && buf
[1] == '%') {
6107 if (strncmp(buf
+ 2, "Trailer", 7) == 0)
6111 if (strncmp(buf
+ 2, "BoundingBox:", 12) == 0) {
6112 int res
= parse_bounding_box(buf
+ 14, &bb
);
6115 else if (res
== 2) {
6116 error("`(atend)' not allowed in trailer of `%1'", filename
);
6120 error("the arguments to the %%%%BoundingBox comment in `%1' are bad",
6129 assign_registers(bb
.llx
, bb
.lly
, bb
.urx
, bb
.ury
);
6134 error("%%%%BoundingBox comment not found in `%1'", filename
);
6137 void ps_bbox_request()
6139 symbol nm
= get_long_name(1);
6143 while (!tok
.newline() && !tok
.eof())
6145 // PS files might contain non-printable characters, such as ^Z
6146 // and CRs not followed by an LF, so open them in binary mode.
6147 file_case
*fcp
= include_search_path
.open_file_cautious(nm
.contents(),
6148 fcp
->mux_need_seek
| fcp
->mux_need_binary
);
6150 do_ps_file(fcp
, nm
.contents());
6153 error("can't open `%1': %2", nm
.contents(), strerror(errno
));
6158 const char *asciify(int c
)
6161 buf
[0] = escape_char
== '\0' ? '\\' : escape_char
;
6162 buf
[1] = buf
[2] = '\0';
6164 case ESCAPE_QUESTION
:
6167 case ESCAPE_AMPERSAND
:
6170 case ESCAPE_RIGHT_PARENTHESIS
:
6173 case ESCAPE_UNDERSCORE
:
6179 case ESCAPE_CIRCUMFLEX
:
6182 case ESCAPE_LEFT_BRACE
:
6185 case ESCAPE_RIGHT_BRACE
:
6188 case ESCAPE_LEFT_QUOTE
:
6191 case ESCAPE_RIGHT_QUOTE
:
6209 case ESCAPE_PERCENT
:
6221 case PUSH_GROFF_MODE
:
6222 case PUSH_COMP_MODE
:
6223 case POP_GROFFCOMP_MODE
:
6227 if (invalid_input_char(c
))
6236 const char *input_char_description(int c
)
6240 return "a newline character";
6242 return "a backspace character";
6244 return "a leader character";
6246 return "a tab character";
6248 return "a space character";
6252 static char buf
[sizeof("magic character code ") + 1 + INT_DIGITS
];
6253 if (invalid_input_char(c
)) {
6254 const char *s
= asciify(c
);
6261 sprintf(buf
, "magic character code %d", c
);
6270 sprintf(buf
, "character code %d", c
);
6276 if (!tok
.newline() && !tok
.eof()) {
6285 if (c
!= ' ' && c
!= '\t')
6289 for (; c
!= '\n' && c
!= EOF
; c
= get_copy(0))
6292 curenv
->add_node(new tag_node(s
, 0));
6299 if (!tok
.newline() && !tok
.eof()) {
6308 if (c
!= ' ' && c
!= '\t')
6312 for (; c
!= '\n' && c
!= EOF
; c
= get_copy(0))
6315 curenv
->add_node(new tag_node(s
, 1));
6320 // .tm, .tm1, and .tmc
6322 void do_terminal(int newline
, int string_like
)
6324 if (!tok
.newline() && !tok
.eof()) {
6328 if (string_like
&& c
== '"') {
6332 if (c
!= ' ' && c
!= '\t')
6335 for (; c
!= '\n' && c
!= EOF
; c
= get_copy(0))
6336 fputs(asciify(c
), stderr
);
6339 fputc('\n', stderr
);
6354 void terminal_continue()
6359 dictionary
stream_dictionary(20);
6361 void do_open(int append
)
6363 symbol stream
= get_name(1);
6364 if (!stream
.is_null()) {
6365 symbol filename
= get_long_name(1);
6366 if (!filename
.is_null()) {
6368 FILE *fp
= fopen(filename
.contents(), append
? "a" : "w");
6370 error("can't open `%1' for %2: %3",
6371 filename
.contents(),
6372 append
? "appending" : "writing",
6374 fp
= (FILE *)stream_dictionary
.remove(stream
);
6377 fp
= (FILE *)stream_dictionary
.lookup(stream
, fp
);
6388 error(".open request not allowed in safer mode");
6395 void opena_request()
6398 error(".opena request not allowed in safer mode");
6405 void close_request()
6407 symbol stream
= get_name(1);
6408 if (!stream
.is_null()) {
6409 FILE *fp
= (FILE *)stream_dictionary
.remove(stream
);
6411 error("no stream named `%1'", stream
.contents());
6418 // .write and .writec
6420 void do_write_request(int newline
)
6422 symbol stream
= get_name(1);
6423 if (stream
.is_null()) {
6427 FILE *fp
= (FILE *)stream_dictionary
.lookup(stream
);
6429 error("no stream named `%1'", stream
.contents());
6434 while ((c
= get_copy(0)) == ' ')
6438 for (; c
!= '\n' && c
!= EOF
; c
= get_copy(0))
6439 fputs(asciify(c
), fp
);
6446 void write_request()
6448 do_write_request(1);
6451 void write_request_continue()
6453 do_write_request(0);
6456 void write_macro_request()
6458 symbol stream
= get_name(1);
6459 if (stream
.is_null()) {
6463 FILE *fp
= (FILE *)stream_dictionary
.lookup(stream
);
6465 error("no stream named `%1'", stream
.contents());
6469 symbol s
= get_name(1);
6474 request_or_macro
*p
= lookup_request(s
);
6475 macro
*m
= p
->to_macro();
6477 error("cannot write request");
6479 string_iterator
iter(*m
);
6481 int c
= iter
.get(0);
6484 fputs(asciify(c
), fp
);
6491 void warnscale_request()
6498 warn_scale
= (double)units_per_inch
;
6500 warn_scale
= (double)units_per_inch
/ 2.54;
6502 warn_scale
= (double)units_per_inch
/ 72.0;
6504 warn_scale
= (double)units_per_inch
/ 6.0;
6507 "invalid scaling indicator `%1', using `i' instead", c
);
6510 warn_scaling_indicator
= c
;
6515 void spreadwarn_request()
6518 if (has_arg() && get_hunits(&n
, 'm')) {
6521 hunits em
= curenv
->get_size();
6522 spread_limit
= (double)n
.to_units()
6523 / (em
.is_zero() ? hresolution
: em
.to_units());
6526 spread_limit
= -spread_limit
- 1; // no arg toggles on/off without
6527 // changing value; we mirror at
6528 // -0.5 to make zero a valid value
6532 static void init_charset_table()
6535 strcpy(buf
, "char");
6536 for (int i
= 0; i
< 256; i
++) {
6537 strcpy(buf
+ 4, i_to_a(i
));
6538 charset_table
[i
] = get_charinfo(symbol(buf
));
6539 charset_table
[i
]->set_ascii_code(i
);
6541 charset_table
[i
]->set_hyphenation_code(cmlower(i
));
6543 charset_table
['.']->set_flags(charinfo::ENDS_SENTENCE
);
6544 charset_table
['?']->set_flags(charinfo::ENDS_SENTENCE
);
6545 charset_table
['!']->set_flags(charinfo::ENDS_SENTENCE
);
6546 charset_table
['-']->set_flags(charinfo::BREAK_AFTER
);
6547 charset_table
['"']->set_flags(charinfo::TRANSPARENT
);
6548 charset_table
['\'']->set_flags(charinfo::TRANSPARENT
);
6549 charset_table
[')']->set_flags(charinfo::TRANSPARENT
);
6550 charset_table
[']']->set_flags(charinfo::TRANSPARENT
);
6551 charset_table
['*']->set_flags(charinfo::TRANSPARENT
);
6552 get_charinfo(symbol("dg"))->set_flags(charinfo::TRANSPARENT
);
6553 get_charinfo(symbol("rq"))->set_flags(charinfo::TRANSPARENT
);
6554 get_charinfo(symbol("em"))->set_flags(charinfo::BREAK_AFTER
);
6555 get_charinfo(symbol("hy"))->set_flags(charinfo::BREAK_AFTER
);
6556 get_charinfo(symbol("ul"))->set_flags(charinfo::OVERLAPS_HORIZONTALLY
);
6557 get_charinfo(symbol("rn"))->set_flags(charinfo::OVERLAPS_HORIZONTALLY
);
6558 get_charinfo(symbol("radicalex"))->set_flags(charinfo::OVERLAPS_HORIZONTALLY
);
6559 get_charinfo(symbol("sqrtex"))->set_flags(charinfo::OVERLAPS_HORIZONTALLY
);
6560 get_charinfo(symbol("ru"))->set_flags(charinfo::OVERLAPS_HORIZONTALLY
);
6561 get_charinfo(symbol("br"))->set_flags(charinfo::OVERLAPS_VERTICALLY
);
6562 page_character
= charset_table
['%'];
6565 static void init_hpf_code_table()
6567 for (int i
= 0; i
< 256; i
++)
6568 hpf_code_table
[i
] = i
;
6571 static void do_translate(int translate_transparent
, int translate_input
)
6574 while (!tok
.newline() && !tok
.eof()) {
6576 // This is a really bizarre troff feature.
6578 translate_space_to_dummy
= tok
.dummy();
6579 if (tok
.newline() || tok
.eof())
6584 charinfo
*ci1
= tok
.get_char(1);
6588 if (tok
.newline() || tok
.eof()) {
6589 ci1
->set_special_translation(charinfo::TRANSLATE_SPACE
,
6590 translate_transparent
);
6594 ci1
->set_special_translation(charinfo::TRANSLATE_SPACE
,
6595 translate_transparent
);
6596 else if (tok
.stretchable_space())
6597 ci1
->set_special_translation(charinfo::TRANSLATE_STRETCHABLE_SPACE
,
6598 translate_transparent
);
6599 else if (tok
.dummy())
6600 ci1
->set_special_translation(charinfo::TRANSLATE_DUMMY
,
6601 translate_transparent
);
6602 else if (tok
.hyphen_indicator())
6603 ci1
->set_special_translation(charinfo::TRANSLATE_HYPHEN_INDICATOR
,
6604 translate_transparent
);
6606 charinfo
*ci2
= tok
.get_char(1);
6610 ci1
->set_translation(0, translate_transparent
, translate_input
);
6612 ci1
->set_translation(ci2
, translate_transparent
, translate_input
);
6624 void translate_no_transparent()
6629 void translate_input()
6637 if (get_integer(&flags
))
6639 charinfo
*ci
= tok
.get_char(1);
6641 charinfo
*tem
= ci
->get_translation();
6644 ci
->set_flags(flags
);
6651 void hyphenation_code()
6654 while (!tok
.newline() && !tok
.eof()) {
6655 charinfo
*ci
= tok
.get_char(1);
6660 unsigned char c
= tok
.ch();
6662 error("hyphenation code must be ordinary character");
6666 error("hyphenation code cannot be digit");
6669 ci
->set_hyphenation_code(c
);
6670 if (ci
->get_translation()
6671 && ci
->get_translation()->get_translation_input())
6672 ci
->get_translation()->set_hyphenation_code(c
);
6679 void hyphenation_patterns_file_code()
6682 while (!tok
.newline() && !tok
.eof()) {
6684 if (get_integer(&n1
) && (0 <= n1
&& n1
<= 255)) {
6686 error("missing output hyphenation code");
6689 if (get_integer(&n2
) && (0 <= n2
&& n2
<= 255)) {
6690 hpf_code_table
[n1
] = n2
;
6694 error("output hyphenation code must be integer in the range 0..255");
6699 error("input hyphenation code must be integer in the range 0..255");
6706 charinfo
*token::get_char(int required
)
6708 if (type
== TOKEN_CHAR
)
6709 return charset_table
[c
];
6710 if (type
== TOKEN_SPECIAL
)
6711 return get_charinfo(nm
);
6712 if (type
== TOKEN_NUMBERED_CHAR
)
6713 return get_charinfo_by_number(val
);
6714 if (type
== TOKEN_ESCAPE
) {
6715 if (escape_char
!= 0)
6716 return charset_table
[escape_char
];
6718 error("`\\e' used while no current escape character");
6723 if (type
== TOKEN_EOF
|| type
== TOKEN_NEWLINE
)
6724 warning(WARN_MISSING
, "missing normal or special character");
6726 error("normal or special character expected (got %1)", description());
6731 charinfo
*get_optional_char()
6735 charinfo
*ci
= tok
.get_char();
6737 check_missing_character();
6743 void check_missing_character()
6745 if (!tok
.newline() && !tok
.eof() && !tok
.right_brace() && !tok
.tab())
6746 error("normal or special character expected (got %1): "
6747 "treated as missing",
6753 int token::add_to_node_list(node
**pp
)
6760 *pp
= (*pp
)->add_char(charset_table
[c
], curenv
, &w
, &s
);
6766 if (escape_char
!= 0)
6767 *pp
= (*pp
)->add_char(charset_table
[escape_char
], curenv
, &w
, &s
);
6769 case TOKEN_HYPHEN_INDICATOR
:
6770 *pp
= (*pp
)->add_discretionary_hyphen();
6772 case TOKEN_ITALIC_CORRECTION
:
6773 *pp
= (*pp
)->add_italic_correction(&w
);
6775 case TOKEN_LEFT_BRACE
:
6777 case TOKEN_MARK_INPUT
:
6778 set_number_reg(nm
, curenv
->get_input_line_position().to_units());
6784 case TOKEN_NUMBERED_CHAR
:
6785 *pp
= (*pp
)->add_char(get_charinfo_by_number(val
), curenv
, &w
, &s
);
6787 case TOKEN_RIGHT_BRACE
:
6790 n
= new hmotion_node(curenv
->get_space_width(),
6791 curenv
->get_fill_color());
6794 *pp
= (*pp
)->add_char(get_charinfo(nm
), curenv
, &w
, &s
);
6796 case TOKEN_STRETCHABLE_SPACE
:
6797 n
= new unbreakable_space_node(curenv
->get_space_width(),
6798 curenv
->get_fill_color());
6800 case TOKEN_UNSTRETCHABLE_SPACE
:
6801 n
= new space_char_hmotion_node(curenv
->get_space_width(),
6802 curenv
->get_fill_color());
6804 case TOKEN_TRANSPARENT_DUMMY
:
6805 n
= new transparent_dummy_node
;
6807 case TOKEN_ZERO_WIDTH_BREAK
:
6808 n
= new space_node(H0
, curenv
->get_fill_color());
6810 n
->is_escape_colon();
6822 void token::process()
6824 if (possibly_handle_first_page_transition())
6827 case TOKEN_BACKSPACE
:
6828 curenv
->add_node(new hmotion_node(-curenv
->get_space_width(),
6829 curenv
->get_fill_color()));
6832 curenv
->add_char(charset_table
[c
]);
6835 curenv
->add_node(new dummy_node
);
6844 if (escape_char
!= 0)
6845 curenv
->add_char(charset_table
[escape_char
]);
6847 case TOKEN_BEGIN_TRAP
:
6848 case TOKEN_END_TRAP
:
6849 case TOKEN_PAGE_EJECTOR
:
6850 // these are all handled in process_input_stack()
6852 case TOKEN_HYPHEN_INDICATOR
:
6853 curenv
->add_hyphen_indicator();
6855 case TOKEN_INTERRUPT
:
6856 curenv
->interrupt();
6858 case TOKEN_ITALIC_CORRECTION
:
6859 curenv
->add_italic_correction();
6862 curenv
->handle_tab(1);
6864 case TOKEN_LEFT_BRACE
:
6866 case TOKEN_MARK_INPUT
:
6867 set_number_reg(nm
, curenv
->get_input_line_position().to_units());
6873 curenv
->add_node(nd
);
6876 case TOKEN_NUMBERED_CHAR
:
6877 curenv
->add_char(get_charinfo_by_number(val
));
6880 // handled in process_input_stack()
6882 case TOKEN_RIGHT_BRACE
:
6888 curenv
->add_char(get_charinfo(nm
));
6893 case TOKEN_STRETCHABLE_SPACE
:
6894 curenv
->add_node(new unbreakable_space_node(curenv
->get_space_width(),
6895 curenv
->get_fill_color()));
6897 case TOKEN_UNSTRETCHABLE_SPACE
:
6898 curenv
->add_node(new space_char_hmotion_node(curenv
->get_space_width(),
6899 curenv
->get_fill_color()));
6902 curenv
->handle_tab(0);
6904 case TOKEN_TRANSPARENT
:
6906 case TOKEN_TRANSPARENT_DUMMY
:
6907 curenv
->add_node(new transparent_dummy_node
);
6909 case TOKEN_ZERO_WIDTH_BREAK
:
6911 node
*tmp
= new space_node(H0
, curenv
->get_fill_color());
6912 tmp
->freeze_space();
6913 tmp
->is_escape_colon();
6914 curenv
->add_node(tmp
);
6922 class nargs_reg
: public reg
{
6924 const char *get_string();
6927 const char *nargs_reg::get_string()
6929 return i_to_a(input_stack::nargs());
6932 class lineno_reg
: public reg
{
6934 const char *get_string();
6937 const char *lineno_reg::get_string()
6941 if (!input_stack::get_location(0, &file
, &line
))
6943 return i_to_a(line
);
6946 class writable_lineno_reg
: public general_reg
{
6948 writable_lineno_reg();
6949 void set_value(units
);
6950 int get_value(units
*);
6953 writable_lineno_reg::writable_lineno_reg()
6957 int writable_lineno_reg::get_value(units
*res
)
6961 if (!input_stack::get_location(0, &file
, &line
))
6967 void writable_lineno_reg::set_value(units n
)
6969 input_stack::set_location(0, n
);
6972 class filename_reg
: public reg
{
6974 const char *get_string();
6977 const char *filename_reg::get_string()
6981 if (input_stack::get_location(0, &file
, &line
))
6987 class break_flag_reg
: public reg
{
6989 const char *get_string();
6992 const char *break_flag_reg::get_string()
6994 return i_to_a(input_stack::get_break_flag());
6997 class constant_reg
: public reg
{
7000 constant_reg(const char *);
7001 const char *get_string();
7004 constant_reg::constant_reg(const char *p
) : s(p
)
7008 const char *constant_reg::get_string()
7013 constant_int_reg::constant_int_reg(int *q
) : p(q
)
7017 const char *constant_int_reg::get_string()
7022 void abort_request()
7027 else if (tok
.newline())
7030 while ((c
= get_copy(0)) == ' ')
7033 if (c
== EOF
|| c
== '\n')
7034 fputs("User Abort.", stderr
);
7036 for (; c
!= '\n' && c
!= EOF
; c
= get_copy(0))
7037 fputs(asciify(c
), stderr
);
7039 fputc('\n', stderr
);
7040 cleanup_and_exit(1);
7046 char *s
= new char[len
];
7048 while ((c
= get_copy(0)) == ' ')
7051 while (c
!= '\n' && c
!= EOF
) {
7052 if (!invalid_input_char(c
)) {
7055 s
= new char[len
*2];
7056 memcpy(s
, tem
, len
);
7076 error(".pi request not allowed in safer mode");
7080 #ifdef POPEN_MISSING
7081 error("pipes not available on this system");
7083 #else /* not POPEN_MISSING */
7085 error("can't pipe: output already started");
7090 if ((pc
= read_string()) == 0)
7091 error("can't pipe to empty command");
7093 char *s
= new char[strlen(pipe_command
) + strlen(pc
) + 1 + 1];
7094 strcpy(s
, pipe_command
);
7097 a_delete pipe_command
;
7104 #endif /* not POPEN_MISSING */
7108 static int system_status
;
7110 void system_request()
7113 error(".sy request not allowed in safer mode");
7117 char *command
= read_string();
7119 error("empty command");
7121 system_status
= system(command
);
7129 if (curdiv
== topdiv
&& topdiv
->before_first_page
) {
7130 handle_initial_request(COPY_FILE_REQUEST
);
7133 symbol filename
= get_long_name(1);
7134 while (!tok
.newline() && !tok
.eof())
7138 if (!filename
.is_null())
7139 curdiv
->copy_file(filename
.contents());
7147 if (curdiv
== topdiv
&& topdiv
->before_first_page
) {
7148 handle_initial_request(VJUSTIFY_REQUEST
);
7151 symbol type
= get_long_name(1);
7152 if (!type
.is_null())
7153 curdiv
->vjustify(type
);
7159 void transparent_file()
7161 if (curdiv
== topdiv
&& topdiv
->before_first_page
) {
7162 handle_initial_request(TRANSPARENT_FILE_REQUEST
);
7165 symbol filename
= get_long_name(1);
7166 while (!tok
.newline() && !tok
.eof())
7170 if (!filename
.is_null()) {
7171 file_case
*fcp
= include_search_path
7172 .open_file_cautious(filename
.contents());
7174 error("can't open `%1': %2", filename
.contents(), strerror(errno
));
7178 int c
= fcp
->get_c();
7181 if (invalid_input_char(c
))
7182 warning(WARN_INPUT
, "invalid input character code %1", int(c
));
7184 curdiv
->transparent_output(c
);
7189 curdiv
->transparent_output('\n');
7201 page_range(int, int, page_range
*);
7202 int contains(int n
);
7205 page_range::page_range(int i
, int j
, page_range
*p
)
7206 : first(i
), last(j
), next(p
)
7210 int page_range::contains(int n
)
7212 return n
>= first
&& (last
<= 0 || n
<= last
);
7215 page_range
*output_page_list
= 0;
7217 int in_output_page_list(int n
)
7219 if (!output_page_list
)
7221 for (page_range
*p
= output_page_list
; p
; p
= p
->next
)
7227 static void parse_output_page_list(char *p
)
7233 else if (csdigit(*p
)) {
7236 i
= i
*10 + *p
++ - '0';
7237 while (csdigit(*p
));
7247 j
= j
*10 + *p
++ - '0';
7248 while (csdigit(*p
));
7254 last_page_number
= -1;
7255 else if (last_page_number
>= 0 && j
> last_page_number
)
7256 last_page_number
= j
;
7257 output_page_list
= new page_range(i
, j
, output_page_list
);
7263 error("bad output page list");
7264 output_page_list
= 0;
7268 static file_case
*open_mac_file(const char *mac
)
7270 // Try first FOOBAR.tmac, then tmac.FOOBAR
7271 char *s
= new char[strlen(mac
) + strlen(MACRO_POSTFIX
) +1];
7273 strcat(s
, MACRO_POSTFIX
);
7276 if ((fcp
= mac_path
->open_file(s
, fcp
->fc_take_path
)) == NULL
) {
7277 s
= new char[strlen(mac
) + strlen(MACRO_PREFIX
) +1];
7278 strcpy(s
, MACRO_PREFIX
);
7280 fcp
= mac_path
->open_file(s
, fcp
->fc_take_path
);
7285 static void process_macro_file(const char *mac
)
7287 file_case
*fcp
= open_mac_file(mac
);
7289 fatal("can't find macro file %1", mac
);
7290 const char *s
= symbol(fcp
->path()).contents();
7291 input_stack::push(new file_iterator(fcp
, s
));
7293 process_input_stack();
7296 static void process_startup_file(const char *filename
)
7298 search_path
*orig_mac_path
= mac_path
;
7299 mac_path
= &config_macro_path
;
7301 if ((fcp
= mac_path
->open_file(filename
)) != NULL
) {
7302 input_stack::push(new file_iterator(fcp
, symbol(fcp
->path()).contents()));
7304 process_input_stack();
7306 mac_path
= orig_mac_path
;
7311 symbol nm
= get_long_name(1);
7315 while (!tok
.newline() && !tok
.eof())
7317 // .mso doesn't (and cannot) go through open_mac_file, so we
7318 // need to do it here manually: If we have tmac.FOOBAR, try
7319 // FOOBAR.tmac and vice versa
7321 if ((fcp
= mac_path
->open_file(nm
.contents())) == NULL
) {
7322 const char *fn
= nm
.contents();
7324 if (strncasecmp(fn
, MACRO_PREFIX
, sizeof(MACRO_PREFIX
) - 1) == 0) {
7325 char *s
= new char[strlen(fn
) + sizeof(MACRO_POSTFIX
)];
7326 strcpy(s
, fn
+ sizeof(MACRO_PREFIX
) - 1);
7327 strcat(s
, MACRO_POSTFIX
);
7328 fcp
= mac_path
->open_file(s
, fcp
->fc_take_path
);
7332 if (strncasecmp(fn
+ strlen(fn
) - sizeof(MACRO_POSTFIX
) + 1,
7333 MACRO_POSTFIX
, sizeof(MACRO_POSTFIX
) - 1) == 0) {
7334 char *s
= new char[strlen(fn
) + sizeof(MACRO_PREFIX
)];
7335 strcpy(s
, MACRO_PREFIX
);
7336 strncat(s
, fn
, strlen(fn
) - sizeof(MACRO_POSTFIX
) + 1);
7337 fcp
= mac_path
->open_file(s
, fcp
->fc_take_path
);
7343 input_stack::push(new file_iterator(fcp
, symbol(fcp
->path()).contents()));
7345 warning(WARN_FILE
, "can't find macro file `%1'", nm
.contents());
7350 static void process_input_file(const char *name
)
7353 if ((fcp
= include_search_path
.open_file_cautious(name
)) == NULL
) {
7354 assert(strcmp(name
, "-"));
7355 fatal("can't open `%1': %2", name
, strerror(errno
));
7357 input_stack::push(new file_iterator(fcp
, name
));
7359 process_input_stack();
7362 // make sure the_input is empty before calling this
7364 static int evaluate_expression(const char *expr
, units
*res
)
7366 input_stack::push(make_temp_iterator(expr
));
7368 int success
= get_number(res
, 'u');
7369 while (input_stack::get(0) != EOF
)
7374 static void do_register_assignment(const char *s
)
7376 const char *p
= strchr(s
, '=');
7382 if (evaluate_expression(s
+ 1, &n
))
7383 set_number_reg(buf
, n
);
7386 char *buf
= new char[p
- s
+ 1];
7387 memcpy(buf
, s
, p
- s
);
7390 if (evaluate_expression(p
+ 1, &n
))
7391 set_number_reg(buf
, n
);
7396 static void set_string(const char *name
, const char *value
)
7398 macro
*m
= new macro
;
7399 for (const char *p
= value
; *p
; p
++)
7400 if (!invalid_input_char((unsigned char)*p
))
7402 request_dictionary
.define(name
, m
);
7405 static void do_string_assignment(const char *s
)
7407 const char *p
= strchr(s
, '=');
7412 set_string(buf
, s
+ 1);
7415 char *buf
= new char[p
- s
+ 1];
7416 memcpy(buf
, s
, p
- s
);
7418 set_string(buf
, p
+ 1);
7423 struct string_list
{
7426 string_list(const char *ss
) : s(ss
), next(0) {}
7430 static void prepend_string(const char *s
, string_list
**p
)
7432 string_list
*l
= new string_list(s
);
7438 static void add_string(const char *s
, string_list
**p
)
7442 *p
= new string_list(s
);
7445 void usage(FILE *stream
, const char *prog
)
7448 "usage: %s -abcivzCERU -wname -Wname -dcs -ffam -mname -nnum -olist\n"
7449 " -rcn -Tname -Fdir -Idir -Mdir [files...]\n",
7453 int main(int argc
, char **argv
)
7455 program_name
= argv
[0];
7456 static char stderr_buf
[BUFSIZ
];
7457 setbuf(stderr
, stderr_buf
);
7459 string_list
*macros
= 0;
7460 string_list
*register_assignments
= 0;
7461 string_list
*string_assignments
= 0;
7466 int no_rc
= 0; // don't process troffrc and troffrc-end
7467 int next_page_number
= 0; // pacify compiler
7469 hresolution
= vresolution
= 1;
7470 // restore $PATH if called from groff
7471 char* groff_path
= getenv("GROFF_PATH__");
7478 if (putenv(strsave(e
.contents())))
7479 fatal("putenv failed");
7481 static const struct option long_options
[] = {
7482 { "help", no_argument
, 0, CHAR_MAX
+ 1 },
7483 { "version", no_argument
, 0, 'v' },
7486 #if defined(DEBUGGING)
7487 #define DEBUG_OPTION "D"
7489 while ((c
= getopt_long(argc
, argv
,
7490 "abciI:vw:W:zCEf:m:n:o:r:d:F:M:T:tqs:RU"
7491 DEBUG_OPTION
, long_options
, 0))
7496 printf("GNU troff (groff) version %s\n", Version_string
);
7501 // Search path for .psbb files
7502 // and most other non-system input files.
7503 include_search_path
.command_line_dir(optarg
);
7508 is_html
= (strcmp(device
, "html") == 0);
7511 compatible_flag
= 1;
7517 macro_path
.command_line_dir(optarg
);
7518 safer_macro_path
.command_line_dir(optarg
);
7519 config_macro_path
.command_line_dir(optarg
);
7522 font::command_line_font_dir(optarg
);
7525 add_string(optarg
, ¯os
);
7534 enable_warning(optarg
);
7537 disable_warning(optarg
);
7546 ascii_output_flag
= 1;
7549 suppress_output_flag
= 1;
7552 if (sscanf(optarg
, "%d", &next_page_number
) == 1)
7555 error("bad page number");
7558 parse_output_page_list(optarg
);
7561 if (*optarg
== '\0')
7562 error("`-d' requires non-empty argument");
7564 add_string(optarg
, &string_assignments
);
7567 if (*optarg
== '\0')
7568 error("`-r' requires non-empty argument");
7570 add_string(optarg
, ®ister_assignments
);
7573 default_family
= symbol(optarg
);
7579 // silently ignore these
7582 unsafe_flag
= 1; // unsafe behaviour
7584 #if defined(DEBUGGING)
7589 case CHAR_MAX
+ 1: // --help
7590 usage(stdout
, argv
[0]);
7594 usage(stderr
, argv
[0]);
7596 break; // never reached
7601 mac_path
= ¯o_path
;
7602 set_string(".T", device
);
7603 init_charset_table();
7604 init_hpf_code_table();
7605 if (!font::load_desc())
7606 fatal("sorry, I can't continue");
7607 units_per_inch
= font::res
;
7608 hresolution
= font::hor
;
7609 vresolution
= font::vert
;
7610 sizescale
= font::sizescale
;
7611 tcommand_flag
= font::tcommand
;
7612 warn_scale
= (double)units_per_inch
;
7613 warn_scaling_indicator
= 'i';
7614 if (!fflag
&& font::family
!= 0 && *font::family
!= '\0')
7615 default_family
= symbol(font::family
);
7616 font_size::init_size_table(font::sizes
);
7619 if (font::style_table
) {
7620 for (i
= 0; font::style_table
[i
]; i
++)
7621 mount_style(j
++, symbol(font::style_table
[i
]));
7623 for (i
= 0; font::font_name_table
[i
]; i
++, j
++)
7624 // In the DESC file a font name of 0 (zero) means leave this
7626 if (strcmp(font::font_name_table
[i
], "0") != 0)
7627 mount_font(j
, symbol(font::font_name_table
[i
]));
7628 curdiv
= topdiv
= new top_level_diversion
;
7630 topdiv
->set_next_page_number(next_page_number
);
7631 init_input_requests();
7632 init_env_requests();
7633 init_div_requests();
7635 init_column_requests();
7637 init_node_requests();
7638 number_reg_dictionary
.define(".T", new constant_reg(tflag
? "1" : "0"));
7640 init_reg_requests();
7641 init_hyphen_requests();
7642 init_environments();
7643 while (string_assignments
) {
7644 do_string_assignment(string_assignments
->s
);
7645 string_list
*tem
= string_assignments
;
7646 string_assignments
= string_assignments
->next
;
7649 while (register_assignments
) {
7650 do_register_assignment(register_assignments
->s
);
7651 string_list
*tem
= register_assignments
;
7652 register_assignments
= register_assignments
->next
;
7656 process_startup_file(INITIAL_STARTUP_FILE
);
7658 process_macro_file(macros
->s
);
7659 string_list
*tem
= macros
;
7660 macros
= macros
->next
;
7664 process_startup_file(FINAL_STARTUP_FILE
);
7665 for (i
= optind
; i
< argc
; i
++)
7666 process_input_file(argv
[i
]);
7667 if (optind
>= argc
|| iflag
)
7668 process_input_file("-");
7670 return 0; // not reached
7676 if (has_arg() && get_integer(&n
)) {
7677 if (n
& ~WARN_TOTAL
) {
7678 warning(WARN_RANGE
, "warning mask must be between 0 and %1", WARN_TOTAL
);
7684 warning_mask
= WARN_TOTAL
;
7688 static void init_registers()
7690 #ifdef LONG_FOR_TIME_T
7692 #else /* not LONG_FOR_TIME_T */
7694 #endif /* not LONG_FOR_TIME_T */
7696 // Use struct here to work around misfeature in old versions of g++.
7697 struct tm
*tt
= localtime(&t
);
7698 set_number_reg("seconds", int(tt
->tm_sec
));
7699 set_number_reg("minutes", int(tt
->tm_min
));
7700 set_number_reg("hours", int(tt
->tm_hour
));
7701 set_number_reg("dw", int(tt
->tm_wday
+ 1));
7702 set_number_reg("dy", int(tt
->tm_mday
));
7703 set_number_reg("mo", int(tt
->tm_mon
+ 1));
7704 set_number_reg("year", int(1900 + tt
->tm_year
));
7705 set_number_reg("yr", int(tt
->tm_year
));
7706 set_number_reg("$$", getpid());
7707 number_reg_dictionary
.define(".A",
7708 new constant_reg(ascii_output_flag
7714 * registers associated with \O
7717 static int output_reg_minx_contents
= -1;
7718 static int output_reg_miny_contents
= -1;
7719 static int output_reg_maxx_contents
= -1;
7720 static int output_reg_maxy_contents
= -1;
7722 void check_output_limits(int x
, int y
)
7724 if ((output_reg_minx_contents
== -1) || (x
< output_reg_minx_contents
))
7725 output_reg_minx_contents
= x
;
7726 if (x
> output_reg_maxx_contents
)
7727 output_reg_maxx_contents
= x
;
7728 if ((output_reg_miny_contents
== -1) || (y
< output_reg_miny_contents
))
7729 output_reg_miny_contents
= y
;
7730 if (y
> output_reg_maxy_contents
)
7731 output_reg_maxy_contents
= y
;
7734 void reset_output_registers()
7736 output_reg_minx_contents
= -1;
7737 output_reg_miny_contents
= -1;
7738 output_reg_maxx_contents
= -1;
7739 output_reg_maxy_contents
= -1;
7742 void get_output_registers(int *minx
, int *miny
, int *maxx
, int *maxy
)
7744 *minx
= output_reg_minx_contents
;
7745 *miny
= output_reg_miny_contents
;
7746 *maxx
= output_reg_maxx_contents
;
7747 *maxy
= output_reg_maxy_contents
;
7750 void init_input_requests()
7752 init_request("ab", abort_request
);
7753 init_request("als", alias_macro
);
7754 init_request("am", append_macro
);
7755 init_request("am1", append_nocomp_macro
);
7756 init_request("ami", append_indirect_macro
);
7757 init_request("ami1", append_indirect_nocomp_macro
);
7758 init_request("as", append_string
);
7759 init_request("as1", append_nocomp_string
);
7760 init_request("asciify", asciify_macro
);
7761 init_request("backtrace", backtrace_request
);
7762 init_request("blm", blank_line_macro
);
7763 init_request("break", while_break_request
);
7764 init_request("cf", copy_file
);
7765 init_request("cflags", char_flags
);
7766 init_request("char", define_character
);
7767 init_request("chop", chop_macro
);
7768 init_request("close", close_request
);
7769 init_request("color", activate_color
);
7770 init_request("composite", composite_request
);
7771 init_request("continue", while_continue_request
);
7772 init_request("cp", compatible
);
7773 init_request("de", define_macro
);
7774 init_request("de1", define_nocomp_macro
);
7775 init_request("defcolor", define_color
);
7776 init_request("dei", define_indirect_macro
);
7777 init_request("dei1", define_indirect_nocomp_macro
);
7778 init_request("device", device_request
);
7779 init_request("devicem", device_macro_request
);
7780 init_request("do", do_request
);
7781 init_request("ds", define_string
);
7782 init_request("ds1", define_nocomp_string
);
7783 init_request("ec", set_escape_char
);
7784 init_request("ecr", restore_escape_char
);
7785 init_request("ecs", save_escape_char
);
7786 init_request("el", else_request
);
7787 init_request("em", end_macro
);
7788 init_request("eo", escape_off
);
7789 init_request("ex", exit_request
);
7790 init_request("fchar", define_fallback_character
);
7791 #ifdef WIDOW_CONTROL
7792 init_request("fpl", flush_pending_lines
);
7793 #endif /* WIDOW_CONTROL */
7794 init_request("hcode", hyphenation_code
);
7795 init_request("hpfcode", hyphenation_patterns_file_code
);
7796 init_request("ie", if_else_request
);
7797 init_request("if", if_request
);
7798 init_request("ig", ignore
);
7799 init_request("length", length_request
);
7800 init_request("lf", line_file
);
7801 init_request("mso", macro_source
);
7802 init_request("nop", nop_request
);
7803 init_request("nroff", nroff_request
);
7804 init_request("nx", next_file
);
7805 init_request("open", open_request
);
7806 init_request("opena", opena_request
);
7807 init_request("output", output_request
);
7808 init_request("pc", set_page_character
);
7809 init_request("pi", pipe_output
);
7810 init_request("pm", print_macros
);
7811 init_request("psbb", ps_bbox_request
);
7812 #ifndef POPEN_MISSING
7813 init_request("pso", pipe_source
);
7814 #endif /* not POPEN_MISSING */
7815 init_request("rchar", remove_character
);
7816 init_request("rd", read_request
);
7817 init_request("return", return_macro_request
);
7818 init_request("rm", remove_macro
);
7819 init_request("rn", rename_macro
);
7820 init_request("schar", define_special_character
);
7821 init_request("shift", shift
);
7822 init_request("so", source
);
7823 init_request("spreadwarn", spreadwarn_request
);
7824 init_request("substring", substring_request
);
7825 init_request("sy", system_request
);
7826 init_request("tag", tag
);
7827 init_request("taga", taga
);
7828 init_request("tm", terminal
);
7829 init_request("tm1", terminal1
);
7830 init_request("tmc", terminal_continue
);
7831 init_request("tr", translate
);
7832 init_request("trf", transparent_file
);
7833 init_request("trin", translate_input
);
7834 init_request("trnt", translate_no_transparent
);
7835 init_request("troff", troff_request
);
7836 init_request("unformat", unformat_macro
);
7838 init_request("vj", vjustify
);
7840 init_request("warn", warn_request
);
7841 init_request("warnscale", warnscale_request
);
7842 init_request("while", while_request
);
7843 init_request("write", write_request
);
7844 init_request("writec", write_request_continue
);
7845 init_request("writem", write_macro_request
);
7846 number_reg_dictionary
.define(".$", new nargs_reg
);
7847 number_reg_dictionary
.define(".br", new break_flag_reg
);
7848 number_reg_dictionary
.define(".C", new constant_int_reg(&compatible_flag
));
7849 number_reg_dictionary
.define(".O", new variable_reg(&begin_level
));
7850 number_reg_dictionary
.define(".c", new lineno_reg
);
7851 number_reg_dictionary
.define(".color", new constant_int_reg(&color_flag
));
7852 number_reg_dictionary
.define(".F", new filename_reg
);
7853 number_reg_dictionary
.define(".g", new constant_reg("1"));
7854 number_reg_dictionary
.define(".H", new constant_int_reg(&hresolution
));
7855 number_reg_dictionary
.define(".R", new constant_reg("10000"));
7856 number_reg_dictionary
.define(".U", new constant_int_reg(&unsafe_flag
));
7857 number_reg_dictionary
.define(".V", new constant_int_reg(&vresolution
));
7858 number_reg_dictionary
.define(".warn", new constant_int_reg(&warning_mask
));
7859 extern const char *major_version
;
7860 number_reg_dictionary
.define(".x", new constant_reg(major_version
));
7861 extern const char *revision
;
7862 number_reg_dictionary
.define(".Y", new constant_reg(revision
));
7863 extern const char *minor_version
;
7864 number_reg_dictionary
.define(".y", new constant_reg(minor_version
));
7865 number_reg_dictionary
.define("c.", new writable_lineno_reg
);
7866 number_reg_dictionary
.define("llx", new variable_reg(&llx_reg_contents
));
7867 number_reg_dictionary
.define("lly", new variable_reg(&lly_reg_contents
));
7868 number_reg_dictionary
.define("opmaxx",
7869 new variable_reg(&output_reg_maxx_contents
));
7870 number_reg_dictionary
.define("opmaxy",
7871 new variable_reg(&output_reg_maxy_contents
));
7872 number_reg_dictionary
.define("opminx",
7873 new variable_reg(&output_reg_minx_contents
));
7874 number_reg_dictionary
.define("opminy",
7875 new variable_reg(&output_reg_miny_contents
));
7876 number_reg_dictionary
.define("slimit",
7877 new variable_reg(&input_stack::limit
));
7878 number_reg_dictionary
.define("systat", new variable_reg(&system_status
));
7879 number_reg_dictionary
.define("urx", new variable_reg(&urx_reg_contents
));
7880 number_reg_dictionary
.define("ury", new variable_reg(&ury_reg_contents
));
7883 object_dictionary
request_dictionary(501);
7885 void init_request(const char *s
, REQUEST_FUNCP f
)
7887 request_dictionary
.define(s
, new request(f
));
7890 static request_or_macro
*lookup_request(symbol nm
)
7892 assert(!nm
.is_null());
7893 request_or_macro
*p
= (request_or_macro
*)request_dictionary
.lookup(nm
);
7895 warning(WARN_MAC
, "macro `%1' not defined", nm
.contents());
7897 request_dictionary
.define(nm
, p
);
7902 node
*charinfo_to_node_list(charinfo
*ci
, const environment
*envp
)
7904 // Don't interpret character definitions in compatible mode.
7905 int old_compatible_flag
= compatible_flag
;
7906 compatible_flag
= 0;
7907 int old_escape_char
= escape_char
;
7909 macro
*mac
= ci
->set_macro(0);
7911 environment
*oldenv
= curenv
;
7912 environment
env(envp
);
7914 curenv
->set_composite();
7915 token old_tok
= tok
;
7916 input_stack::add_boundary();
7917 string_iterator
*si
=
7918 new string_iterator(*mac
, "composite character", ci
->nm
);
7919 input_stack::push(si
);
7920 // we don't use process_input_stack, because we don't want to recognise
7926 if (tok
.newline()) {
7927 error("composite character mustn't contain newline");
7935 node
*n
= curenv
->extract_output_line();
7936 input_stack::remove_boundary();
7940 compatible_flag
= old_compatible_flag
;
7941 escape_char
= old_escape_char
;
7946 static node
*read_draw_node()
7950 if (!start
.delimiter(1)){
7953 } while (tok
!= start
&& !tok
.newline() && !tok
.eof());
7958 error("missing argument");
7960 unsigned char type
= tok
.ch();
7962 read_color_draw_node(start
);
7967 hvpair
*point
= new hvpair
[maxpoints
];
7972 for (i
= 0; tok
!= start
; i
++) {
7973 if (i
== maxpoints
) {
7974 hvpair
*oldpoint
= point
;
7975 point
= new hvpair
[maxpoints
*2];
7976 for (int j
= 0; j
< maxpoints
; j
++)
7977 point
[j
] = oldpoint
[j
];
7981 if (!get_hunits(&point
[i
].h
,
7982 type
== 'f' || type
== 't' ? 'u' : 'm')) {
7993 if (!get_vunits(&point
[i
].v
, 'v')) {
7999 while (tok
!= start
&& !tok
.newline() && !tok
.eof())
8004 if (npoints
!= 1 || no_last_v
) {
8005 error("two arguments needed for line");
8010 if (npoints
!= 1 || !no_last_v
) {
8011 error("one argument needed for circle");
8017 if (npoints
!= 1 || no_last_v
) {
8018 error("two arguments needed for ellipse");
8023 if (npoints
!= 2 || no_last_v
) {
8024 error("four arguments needed for arc");
8030 error("even number of arguments needed for spline");
8033 if (npoints
!= 1 || !no_last_v
) {
8034 error("one argument needed for gray shade");
8039 // silently pass it through
8042 draw_node
*dn
= new draw_node(type
, point
, npoints
,
8043 curenv
->get_font_size(),
8044 curenv
->get_glyph_color(),
8045 curenv
->get_fill_color());
8057 static void read_color_draw_node(token
&start
)
8061 error("missing color scheme");
8064 unsigned char scheme
= tok
.ch();
8067 char end
= start
.ch();
8070 col
= read_cmy(end
);
8073 col
= &default_color
;
8076 col
= read_gray(end
);
8079 col
= read_cmyk(end
);
8082 col
= read_rgb(end
);
8086 curenv
->set_fill_color(col
);
8087 while (tok
!= start
) {
8088 if (tok
.newline() || tok
.eof()) {
8089 warning(WARN_DELIM
, "missing closing delimiter");
8090 input_stack::push(make_temp_iterator("\n"));
8101 } warning_table
[] = {
8102 { "char", WARN_CHAR
},
8103 { "range", WARN_RANGE
},
8104 { "break", WARN_BREAK
},
8105 { "delim", WARN_DELIM
},
8107 { "scale", WARN_SCALE
},
8108 { "number", WARN_NUMBER
},
8109 { "syntax", WARN_SYNTAX
},
8110 { "tab", WARN_TAB
},
8111 { "right-brace", WARN_RIGHT_BRACE
},
8112 { "missing", WARN_MISSING
},
8113 { "input", WARN_INPUT
},
8114 { "escape", WARN_ESCAPE
},
8115 { "space", WARN_SPACE
},
8116 { "font", WARN_FONT
},
8118 { "mac", WARN_MAC
},
8119 { "reg", WARN_REG
},
8121 { "color", WARN_COLOR
},
8122 { "all", WARN_TOTAL
& ~(WARN_DI
| WARN_MAC
| WARN_REG
) },
8123 { "w", WARN_TOTAL
},
8124 { "default", DEFAULT_WARNING_MASK
},
8127 static int lookup_warning(const char *name
)
8129 for (unsigned int i
= 0;
8130 i
< sizeof(warning_table
)/sizeof(warning_table
[0]);
8132 if (strcmp(name
, warning_table
[i
].name
) == 0)
8133 return warning_table
[i
].mask
;
8137 static void enable_warning(const char *name
)
8139 int mask
= lookup_warning(name
);
8141 warning_mask
|= mask
;
8143 error("unknown warning `%1'", name
);
8146 static void disable_warning(const char *name
)
8148 int mask
= lookup_warning(name
);
8150 warning_mask
&= ~mask
;
8152 error("unknown warning `%1'", name
);
8155 static void copy_mode_error(const char *format
,
8161 static const char prefix
[] = "(in ignored input) ";
8162 char *s
= new char[sizeof(prefix
) + strlen(format
)];
8165 warning(WARN_IG
, s
, arg1
, arg2
, arg3
);
8169 error(format
, arg1
, arg2
, arg3
);
8172 enum error_type
{ WARNING
, OUTPUT_WARNING
, ERROR
, FATAL
};
8174 static void do_error(error_type type
,
8180 const char *filename
;
8182 if (inhibit_errors
&& type
< FATAL
)
8185 input_stack::backtrace();
8186 if (!get_file_line(&filename
, &lineno
))
8189 errprint("%1:%2: ", filename
, lineno
);
8190 else if (program_name
)
8191 fprintf(stderr
, "%s: ", program_name
);
8194 fputs("fatal error: ", stderr
);
8199 fputs("warning: ", stderr
);
8201 case OUTPUT_WARNING
:
8202 double fromtop
= topdiv
->get_vertical_position().to_units() / warn_scale
;
8203 fprintf(stderr
, "warning [p %d, %.1f%c",
8204 topdiv
->get_page_number(), fromtop
, warn_scaling_indicator
);
8205 if (topdiv
!= curdiv
) {
8206 double fromtop1
= curdiv
->get_vertical_position().to_units()
8208 fprintf(stderr
, ", div `%s', %.1f%c",
8209 curdiv
->get_diversion_name(), fromtop1
, warn_scaling_indicator
);
8211 fprintf(stderr
, "]: ");
8214 errprint(format
, arg1
, arg2
, arg3
);
8215 fputc('\n', stderr
);
8218 cleanup_and_exit(1);
8221 int warning(warning_type t
,
8227 if ((t
& warning_mask
) != 0) {
8228 do_error(WARNING
, format
, arg1
, arg2
, arg3
);
8235 int output_warning(warning_type t
,
8241 if ((t
& warning_mask
) != 0) {
8242 do_error(OUTPUT_WARNING
, format
, arg1
, arg2
, arg3
);
8249 void error(const char *format
,
8254 do_error(ERROR
, format
, arg1
, arg2
, arg3
);
8257 void fatal(const char *format
,
8262 do_error(FATAL
, format
, arg1
, arg2
, arg3
);
8265 void fatal_with_file_and_line(const char *filename
, int lineno
,
8271 fprintf(stderr
, "%s:%d: fatal error: ", filename
, lineno
);
8272 errprint(format
, arg1
, arg2
, arg3
);
8273 fputc('\n', stderr
);
8275 cleanup_and_exit(1);
8278 void error_with_file_and_line(const char *filename
, int lineno
,
8284 fprintf(stderr
, "%s:%d: error: ", filename
, lineno
);
8285 errprint(format
, arg1
, arg2
, arg3
);
8286 fputc('\n', stderr
);
8290 dictionary
charinfo_dictionary(501);
8292 charinfo
*get_charinfo(symbol nm
)
8294 void *p
= charinfo_dictionary
.lookup(nm
);
8296 return (charinfo
*)p
;
8297 charinfo
*cp
= new charinfo(nm
);
8298 (void)charinfo_dictionary
.lookup(nm
, cp
);
8302 int charinfo::next_index
= 0;
8304 charinfo::charinfo(symbol s
)
8305 : translation(0), mac(0), special_translation(TRANSLATE_NONE
),
8306 hyphenation_code(0), flags(0), ascii_code(0), asciify_code(0),
8307 not_found(0), transparent_translate(1), translate_input(0),
8308 mode(CHAR_NORMAL
), nm(s
)
8310 index
= next_index
++;
8314 void charinfo::set_hyphenation_code(unsigned char c
)
8316 hyphenation_code
= c
;
8319 void charinfo::set_translation(charinfo
*ci
, int tt
, int ti
)
8323 if (hyphenation_code
!= 0)
8324 ci
->set_hyphenation_code(hyphenation_code
);
8325 if (asciify_code
!= 0)
8326 ci
->set_asciify_code(asciify_code
);
8327 else if (ascii_code
!= 0)
8328 ci
->set_asciify_code(ascii_code
);
8329 ci
->set_translation_input();
8331 special_translation
= TRANSLATE_NONE
;
8332 transparent_translate
= tt
;
8335 void charinfo::set_special_translation(int c
, int tt
)
8337 special_translation
= c
;
8339 transparent_translate
= tt
;
8342 void charinfo::set_ascii_code(unsigned char c
)
8347 void charinfo::set_asciify_code(unsigned char c
)
8352 macro
*charinfo::set_macro(macro
*m
)
8359 macro
*charinfo::setx_macro(macro
*m
, char_mode cm
)
8367 void charinfo::set_number(int n
)
8373 int charinfo::get_number()
8375 assert(number
>= 0);
8379 symbol
UNNAMED_SYMBOL("---");
8381 // For numbered characters not between 0 and 255, we make a symbol out
8382 // of the number and store them in this dictionary.
8384 dictionary
numbered_charinfo_dictionary(11);
8386 charinfo
*get_charinfo_by_number(int n
)
8388 static charinfo
*number_table
[256];
8390 if (n
>= 0 && n
< 256) {
8391 charinfo
*ci
= number_table
[n
];
8393 ci
= new charinfo(UNNAMED_SYMBOL
);
8395 number_table
[n
] = ci
;
8400 symbol
ns(i_to_a(n
));
8401 charinfo
*ci
= (charinfo
*)numbered_charinfo_dictionary
.lookup(ns
);
8403 ci
= new charinfo(UNNAMED_SYMBOL
);
8405 (void)numbered_charinfo_dictionary
.lookup(ns
, ci
);
8411 // This overrides the same function from libgroff; while reading font
8412 // definition files it puts single-letter glyph names into `charset_table'
8413 // and converts glyph names of the form `\x' (`x' a single letter) into `x'.
8414 // Consequently, symbol("x") refers to glyph name `\x', not `x'.
8416 glyph
*name_to_glyph(const char *nm
)
8420 ci
= charset_table
[nm
[0] & 0xff];
8421 else if (nm
[0] == '\\' && nm
[2] == 0)
8422 ci
= get_charinfo(symbol(nm
+ 1));
8424 ci
= get_charinfo(symbol(nm
));
8425 return ci
->as_glyph();
8428 glyph
*number_to_glyph(int n
)
8430 return get_charinfo_by_number(n
)->as_glyph();
8433 const char *glyph_to_name(glyph
*g
)
8435 charinfo
*ci
= (charinfo
*)g
; // Every glyph is actually a charinfo.
8436 return (ci
->nm
!= UNNAMED_SYMBOL
? ci
->nm
.contents() : NULL
);