2 /* Copyright (C) 1989, 1990, 1991, 1992, 2000, 2001, 2002, 2003
3 Free Software Foundation, Inc.
4 Written by James Clark (jjc@jclark.com)
6 This file is part of groff.
8 groff is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 groff is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License along
19 with groff; see the file COPYING. If not, write to the Free Software
20 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
24 #define MAX_POINT_SIZE 99
25 #define MAX_VERTICAL_SPACING 72
27 extern "C" const char *Version_string
;
29 static int compatible_flag
= 0;
33 enum { START
, MIDDLE
, REREAD_T
, REREAD_TE
, REREAD_E
, END
, ERROR
} state
;
38 int ended() { return unget_stack
.empty() && state
== END
; }
42 table_input::table_input(FILE *p
)
47 void table_input::unget(char c
)
55 int table_input::get()
57 int len
= unget_stack
.length();
59 unsigned char c
= unget_stack
[len
- 1];
60 unget_stack
.set_length(len
- 1);
69 if ((c
= getc(fp
)) == '.') {
70 if ((c
= getc(fp
)) == 'T') {
71 if ((c
= getc(fp
)) == 'E') {
72 if (compatible_flag
) {
80 if (c
== EOF
|| c
== ' ' || c
== '\n') {
112 error("invalid input character code 0");
120 // handle line continuation
121 if ((c
= getc(fp
)) == '\\') {
124 c
= getc(fp
); // perhaps state ought to be START now
140 else if (c
== '\0') {
141 error("invalid input character code 0");
162 void process_input_file(FILE *);
163 void process_table(table_input
&in
);
165 void process_input_file(FILE *fp
)
167 enum { START
, MIDDLE
, HAD_DOT
, HAD_T
, HAD_TS
, HAD_l
, HAD_lf
} state
;
170 while ((c
= getc(fp
)) != EOF
)
222 if (c
== ' ' || c
== '\n' || compatible_flag
) {
228 error("end of file at beginning of table");
237 table_input
input(fp
);
238 process_table(input
);
239 set_troff_location(current_filename
, current_lineno
);
241 fputs(".TE", stdout
);
242 while ((c
= getc(fp
)) != '\n') {
256 fputs(".TS", stdout
);
277 if (c
== ' ' || c
== '\n' || compatible_flag
) {
288 interpret_lf_args(line
.contents());
289 printf(".lf%s", line
.contents());
293 fputs(".lf", stdout
);
308 fputs(".\n", stdout
);
311 fputs(".l\n", stdout
);
314 fputs(".T\n", stdout
);
317 fputs(".lf\n", stdout
);
320 fputs(".TS\n", stdout
);
332 char decimal_point_char
;
338 : flags(0), linesize(0), tab_char('\t'), decimal_point_char('.')
340 delim
[0] = delim
[1] = '\0';
343 // Return non-zero if p and q are the same ignoring case.
345 int strieq(const char *p
, const char *q
)
347 for (; cmlower(*p
) == cmlower(*q
); p
++, q
++)
353 // return 0 if we should give up in this table
355 options
*process_options(table_input
&in
)
357 options
*opt
= new options
;
363 int i
= line
.length();
370 int i
= line
.length();
379 else if (c
== ';' && level
== 0) {
389 while (!csalpha(*p
) && *p
!= '\0')
397 if (*q
!= '(' && *q
!= '\0')
404 while (*q
!= ')' && *q
!= '\0')
407 error("missing `)'");
413 error("argument without option");
415 else if (strieq(p
, "tab")) {
417 error("`tab' option requires argument in parentheses");
419 if (arg
[0] == '\0' || arg
[1] != '\0')
420 error("argument to `tab' option must be a single character");
422 opt
->tab_char
= arg
[0];
425 else if (strieq(p
, "linesize")) {
427 error("`linesize' option requires argument in parentheses");
429 if (sscanf(arg
, "%d", &opt
->linesize
) != 1)
430 error("bad linesize `%s'", arg
);
431 else if (opt
->linesize
<= 0) {
432 error("linesize must be positive");
437 else if (strieq(p
, "delim")) {
439 error("`delim' option requires argument in parentheses");
440 else if (arg
[0] == '\0' || arg
[1] == '\0' || arg
[2] != '\0')
441 error("argument to `delim' option must be two characters");
443 opt
->delim
[0] = arg
[0];
444 opt
->delim
[1] = arg
[1];
447 else if (strieq(p
, "center") || strieq(p
, "centre")) {
449 error("`center' option does not take an argument");
450 opt
->flags
|= table::CENTER
;
452 else if (strieq(p
, "expand")) {
454 error("`expand' option does not take an argument");
455 opt
->flags
|= table::EXPAND
;
457 else if (strieq(p
, "box") || strieq(p
, "frame")) {
459 error("`box' option does not take an argument");
460 opt
->flags
|= table::BOX
;
462 else if (strieq(p
, "doublebox") || strieq(p
, "doubleframe")) {
464 error("`doublebox' option does not take an argument");
465 opt
->flags
|= table::DOUBLEBOX
;
467 else if (strieq(p
, "allbox")) {
469 error("`allbox' option does not take an argument");
470 opt
->flags
|= table::ALLBOX
;
472 else if (strieq(p
, "nokeep")) {
474 error("`nokeep' option does not take an argument");
475 opt
->flags
|= table::NOKEEP
;
477 else if (strieq(p
, "nospaces")) {
479 error("`nospaces' option does not take an argument");
480 opt
->flags
|= table::NOSPACES
;
482 else if (strieq(p
, "decimalpoint")) {
484 error("`decimalpoint' option requires argument in parentheses");
486 if (arg
[0] == '\0' || arg
[1] != '\0')
487 error("argument to `decimalpoint' option must be a single character");
489 opt
->decimal_point_char
= arg
[0];
493 error("unrecognised global option `%1'", p
);
502 entry_modifier::entry_modifier()
503 : vertical_alignment(CENTER
), zero_width(0), stagger(0)
505 vertical_spacing
.inc
= vertical_spacing
.val
= 0;
506 point_size
.inc
= point_size
.val
= 0;
509 entry_modifier::~entry_modifier()
513 entry_format::entry_format() : type(FORMAT_LEFT
)
517 entry_format::entry_format(format_type t
) : type(t
)
521 void entry_format::debug_print() const
536 case FORMAT_ALPHABETIC
:
548 case FORMAT_DOUBLE_HLINE
:
555 if (point_size
.val
!= 0) {
557 if (point_size
.inc
> 0)
559 else if (point_size
.inc
< 0)
561 fprintf(stderr
, "%d ", point_size
.val
);
563 if (vertical_spacing
.val
!= 0) {
565 if (vertical_spacing
.inc
> 0)
567 else if (vertical_spacing
.inc
< 0)
569 fprintf(stderr
, "%d ", vertical_spacing
.val
);
573 put_string(font
, stderr
);
576 if (!macro
.empty()) {
578 put_string(macro
, stderr
);
581 switch (vertical_alignment
) {
582 case entry_modifier::CENTER
:
584 case entry_modifier::TOP
:
587 case entry_modifier::BOTTOM
:
603 entry_format
**entry
;
606 format(int nr
, int nc
);
608 void add_rows(int n
);
611 format::format(int nr
, int nc
) : nrows(nr
), ncolumns(nc
)
614 separation
= ncolumns
> 1 ? new int[ncolumns
- 1] : 0;
615 for (i
= 0; i
< ncolumns
-1; i
++)
617 width
= new string
[ncolumns
];
618 equal
= new char[ncolumns
];
619 for (i
= 0; i
< ncolumns
; i
++)
621 entry
= new entry_format
*[nrows
];
622 for (i
= 0; i
< nrows
; i
++)
623 entry
[i
] = new entry_format
[ncolumns
];
624 vline
= new char*[nrows
];
625 for (i
= 0; i
< nrows
; i
++) {
626 vline
[i
] = new char[ncolumns
+1];
627 for (int j
= 0; j
< ncolumns
+1; j
++)
632 void format::add_rows(int n
)
635 char **old_vline
= vline
;
636 vline
= new char*[nrows
+ n
];
637 for (i
= 0; i
< nrows
; i
++)
638 vline
[i
] = old_vline
[i
];
640 for (i
= 0; i
< n
; i
++) {
641 vline
[nrows
+ i
] = new char[ncolumns
+ 1];
642 for (int j
= 0; j
< ncolumns
+ 1; j
++)
643 vline
[nrows
+ i
][j
] = 0;
645 entry_format
**old_entry
= entry
;
646 entry
= new entry_format
*[nrows
+ n
];
647 for (i
= 0; i
< nrows
; i
++)
648 entry
[i
] = old_entry
[i
];
650 for (i
= 0; i
< n
; i
++)
651 entry
[nrows
+ i
] = new entry_format
[ncolumns
];
658 ad_delete(ncolumns
) width
;
660 for (int i
= 0; i
< nrows
; i
++) {
662 ad_delete(ncolumns
) entry
[i
];
668 struct input_entry_format
: public entry_format
{
669 input_entry_format
*next
;
676 input_entry_format(format_type
, input_entry_format
* = 0);
677 ~input_entry_format();
681 input_entry_format::input_entry_format(format_type t
, input_entry_format
*p
)
682 : entry_format(t
), next(p
)
691 input_entry_format::~input_entry_format()
695 void free_input_entry_format_list(input_entry_format
*list
)
698 input_entry_format
*tem
= list
;
704 void input_entry_format::debug_print()
707 for (i
= 0; i
< pre_vline
; i
++)
709 entry_format::debug_print();
710 if (!width
.empty()) {
713 put_string(width
, stderr
);
719 fprintf(stderr
, "%d", separation
);
720 for (i
= 0; i
< vline
; i
++)
726 // Return zero if we should give up on this table.
727 // If this is a continuation format line, current_format will be the current
730 format
*process_format(table_input
&in
, options
*opt
,
731 format
*current_format
= 0)
733 input_entry_format
*list
= 0;
739 format_type t
= FORMAT_LEFT
;
742 error("end of input while processing format");
743 free_input_entry_format_list(list
);
755 t
= FORMAT_ALPHABETIC
;
782 case '-': // tbl also accepts this
788 t
= FORMAT_DOUBLE_HLINE
;
801 if (c
== opt
->tab_char
)
803 error("unrecognised format `%1'", char(c
));
804 free_input_entry_format_list(list
);
815 list
= new input_entry_format(t
, list
);
817 list
->pre_vline
= pre_vline
;
824 list
->vertical_alignment
= entry_modifier::TOP
;
829 list
->vertical_alignment
= entry_modifier::BOTTOM
;
839 list
->zero_width
= 1;
854 w
= w
*10 + (c
- '0');
856 } while (c
!= EOF
&& csdigit(c
));
857 list
->separation
= w
;
864 } while (c
== ' ' || c
== '\t');
866 error("missing font name");
872 if (c
== EOF
|| c
== ' ' || c
== '\t') {
873 error("missing `)'");
880 list
->font
+= char(c
);
888 && c
!= EOF
&& c
!= ' ' && c
!= '\t' && c
!= '.' && c
!= '\n') {
889 list
->font
+= char(c
);
898 } while (c
== ' ' || c
== '\t');
900 error("missing macro name");
906 if (c
== EOF
|| c
== ' ' || c
== '\t') {
907 error("missing `)'");
914 list
->macro
+= char(c
);
922 && c
!= EOF
&& c
!= ' ' && c
!= '\t' && c
!= '.' && c
!= '\n') {
923 list
->macro
+= char(c
);
931 list
->vertical_spacing
.val
= 0;
932 list
->vertical_spacing
.inc
= 0;
933 if (c
== '+' || c
== '-') {
934 list
->vertical_spacing
.inc
= (c
== '+' ? 1 : -1);
937 if (c
== EOF
|| !csdigit(c
)) {
938 error("`v' modifier must be followed by number");
939 list
->vertical_spacing
.inc
= 0;
943 list
->vertical_spacing
.val
*= 10;
944 list
->vertical_spacing
.val
+= c
- '0';
946 } while (c
!= EOF
&& csdigit(c
));
948 if (list
->vertical_spacing
.val
> MAX_VERTICAL_SPACING
949 || list
->vertical_spacing
.val
< -MAX_VERTICAL_SPACING
) {
950 error("unreasonable vertical spacing");
951 list
->vertical_spacing
.val
= 0;
952 list
->vertical_spacing
.inc
= 0;
958 list
->point_size
.val
= 0;
959 list
->point_size
.inc
= 0;
960 if (c
== '+' || c
== '-') {
961 list
->point_size
.inc
= (c
== '+' ? 1 : -1);
964 if (c
== EOF
|| !csdigit(c
)) {
965 error("`p' modifier must be followed by number");
966 list
->point_size
.inc
= 0;
970 list
->point_size
.val
*= 10;
971 list
->point_size
.val
+= c
- '0';
973 } while (c
!= EOF
&& csdigit(c
));
975 if (list
->point_size
.val
> MAX_POINT_SIZE
976 || list
->point_size
.val
< -MAX_POINT_SIZE
) {
977 error("unreasonable point size");
978 list
->point_size
.val
= 0;
979 list
->point_size
.inc
= 0;
985 while (c
== ' ' || c
== '\t')
991 if (c
== EOF
|| c
== '\n') {
992 error("missing `)'");
993 free_input_entry_format_list(list
);
1002 if (c
== '+' || c
== '-') {
1003 list
->width
= char(c
);
1008 if (c
== EOF
|| !csdigit(c
))
1009 error("bad argument for `w' modifier");
1012 list
->width
+= char(c
);
1014 } while (c
!= EOF
&& csdigit(c
));
1042 if (c
== opt
->tab_char
)
1049 if (list
->vline
> 2) {
1051 error("more than 2 vertical bars between key letters");
1053 if (c
== '\n' || c
== ',') {
1055 list
->last_column
= 1;
1061 } while (c
== ' ' || c
== '\t');
1063 error("`.' not last character on line");
1064 free_input_entry_format_list(list
);
1070 free_input_entry_format_list(list
);
1073 list
->last_column
= 1;
1074 // now reverse the list so that the first row is at the beginning
1075 input_entry_format
*rev
= 0;
1077 input_entry_format
*tem
= list
->next
;
1083 input_entry_format
*tem
;
1086 for (tem
= list
; tem
; tem
= tem
->next
)
1090 // compute number of columns and rows
1094 for (tem
= list
; tem
; tem
= tem
->next
) {
1095 if (tem
->last_column
) {
1096 if (col
>= ncolumns
)
1106 if (current_format
) {
1107 if (ncolumns
> current_format
->ncolumns
) {
1108 error("cannot increase the number of columns in a continued format");
1109 free_input_entry_format_list(list
);
1117 f
= new format(nrows
, ncolumns
);
1121 for (tem
= list
; tem
; tem
= tem
->next
) {
1122 f
->entry
[row
][col
] = *tem
;
1123 if (col
< ncolumns
-1) {
1124 // use the greatest separation
1125 if (tem
->separation
> f
->separation
[col
]) {
1127 error("cannot change column separation in continued format");
1129 f
->separation
[col
] = tem
->separation
;
1132 else if (tem
->separation
>= 0)
1133 error("column separation specified for last column");
1134 if (tem
->equal
&& !f
->equal
[col
]) {
1136 error("cannot change which columns are equal in continued format");
1140 if (!tem
->width
.empty()) {
1141 // use the last width
1142 if (!f
->width
[col
].empty() && f
->width
[col
] != tem
->width
)
1143 error("multiple widths for column %1", col
+1);
1144 f
->width
[col
] = tem
->width
;
1146 if (tem
->pre_vline
) {
1148 f
->vline
[row
][col
] = tem
->pre_vline
;
1150 f
->vline
[row
][col
+1] = tem
->vline
;
1151 if (tem
->last_column
) {
1158 free_input_entry_format_list(list
);
1159 for (col
= 0; col
< ncolumns
; col
++) {
1160 entry_format
*e
= f
->entry
[f
->nrows
-1] + col
;
1161 if (e
->type
!= FORMAT_HLINE
1162 && e
->type
!= FORMAT_DOUBLE_HLINE
1163 && e
->type
!= FORMAT_SPAN
)
1166 if (col
>= ncolumns
) {
1167 error("last row of format is all lines");
1174 table
*process_data(table_input
&in
, format
*f
, options
*opt
)
1176 char tab_char
= opt
->tab_char
;
1177 int ncolumns
= f
->ncolumns
;
1178 int current_row
= 0;
1179 int format_index
= 0;
1181 enum { DATA_INPUT_LINE
, TROFF_INPUT_LINE
, SINGLE_HLINE
, DOUBLE_HLINE
} type
;
1182 table
*tbl
= new table(ncolumns
, opt
->flags
, opt
->linesize
,
1183 opt
->decimal_point_char
);
1184 if (opt
->delim
[0] != '\0')
1185 tbl
->set_delim(opt
->delim
[0], opt
->delim
[1]);
1187 // first determine what type of line this is
1193 if (d
!= EOF
&& csdigit(d
)) {
1195 type
= DATA_INPUT_LINE
;
1199 type
= TROFF_INPUT_LINE
;
1202 else if (c
== '_' || c
== '=') {
1206 type
= SINGLE_HLINE
;
1208 type
= DOUBLE_HLINE
;
1212 type
= DATA_INPUT_LINE
;
1216 type
= DATA_INPUT_LINE
;
1219 case DATA_INPUT_LINE
:
1222 if (format_index
>= f
->nrows
)
1223 format_index
= f
->nrows
- 1;
1224 // A format row that is all lines doesn't use up a data line.
1225 while (format_index
< f
->nrows
- 1) {
1227 for (c
= 0; c
< ncolumns
; c
++) {
1228 entry_format
*e
= f
->entry
[format_index
] + c
;
1229 if (e
->type
!= FORMAT_HLINE
1230 && e
->type
!= FORMAT_DOUBLE_HLINE
1231 // Unfortunately tbl treats a span as needing data.
1232 // && e->type != FORMAT_SPAN
1238 for (c
= 0; c
< ncolumns
; c
++)
1239 tbl
->add_entry(current_row
, c
, input_entry
,
1240 f
->entry
[format_index
] + c
, current_filename
,
1242 tbl
->add_vlines(current_row
, f
->vline
[format_index
]);
1246 entry_format
*line_format
= f
->entry
[format_index
];
1248 int row_comment
= 0;
1250 if (c
== tab_char
|| c
== '\n') {
1251 int ln
= current_lineno
;
1254 if ((opt
->flags
& table::NOSPACES
))
1255 input_entry
.remove_spaces();
1256 while (col
< ncolumns
1257 && line_format
[col
].type
== FORMAT_SPAN
) {
1258 tbl
->add_entry(current_row
, col
, "", &line_format
[col
],
1259 current_filename
, ln
);
1262 if (c
== '\n' && input_entry
.length() == 2
1263 && input_entry
[0] == 'T' && input_entry
[1] == '{') {
1267 START
, MIDDLE
, GOT_T
, GOT_RIGHT_BRACE
, GOT_DOT
,
1270 while (state
!= END
) {
1288 state
= GOT_RIGHT_BRACE
;
1292 state
= c
== '\n' ? START
: MIDDLE
;
1301 state
= c
== '\n' ? START
: MIDDLE
;
1308 input_entry
+= ".l";
1310 state
= c
== '\n' ? START
: MIDDLE
;
1314 if (c
== ' ' || c
== '\n' || compatible_flag
) {
1316 input_entry
+= ".lf";
1324 interpret_lf_args(args
.contents());
1326 args
.set_length(args
.length() - 1);
1327 input_entry
+= args
;
1331 input_entry
+= ".lf";
1336 case GOT_RIGHT_BRACE
:
1337 if (c
== '\n' || c
== tab_char
)
1343 state
= c
== '\n' ? START
: MIDDLE
;
1357 error("end of data in middle of text block");
1362 if (col
>= ncolumns
) {
1363 if (!input_entry
.empty()) {
1364 if (input_entry
.length() >= 2
1365 && input_entry
[0] == '\\'
1366 && input_entry
[1] == '"')
1368 else if (!row_comment
) {
1371 input_entry
+= '\0';
1372 error("excess data entry `%1' discarded",
1373 input_entry
.contents());
1380 tbl
->add_entry(current_row
, col
, input_entry
,
1381 &line_format
[col
], current_filename
, ln
);
1396 for (; col
< ncolumns
; col
++)
1397 tbl
->add_entry(current_row
, col
, input_entry
, &line_format
[col
],
1398 current_filename
, current_lineno
- 1);
1399 tbl
->add_vlines(current_row
, f
->vline
[format_index
]);
1404 case TROFF_INPUT_LINE
:
1407 int ln
= current_lineno
;
1417 tbl
->add_text_line(current_row
, line
, current_filename
, ln
);
1418 if (line
.length() >= 4
1419 && line
[0] == '.' && line
[1] == 'T' && line
[2] == '&') {
1420 format
*newf
= process_format(in
, opt
, f
);
1426 if (line
.length() >= 3
1427 && line
[0] == '.' && line
[1] == 'l' && line
[2] == 'f') {
1429 interpret_lf_args(line
.contents() + 3);
1434 tbl
->add_single_hline(current_row
);
1437 tbl
->add_double_hline(current_row
);
1445 if (!give_up
&& current_row
== 0) {
1446 error("no real data");
1453 // Do this here rather than at the beginning in case continued formats
1456 for (i
= 0; i
< ncolumns
- 1; i
++)
1457 if (f
->separation
[i
] >= 0)
1458 tbl
->set_column_separation(i
, f
->separation
[i
]);
1459 for (i
= 0; i
< ncolumns
; i
++)
1460 if (!f
->width
[i
].empty())
1461 tbl
->set_minimum_width(i
, f
->width
[i
]);
1462 for (i
= 0; i
< ncolumns
; i
++)
1464 tbl
->set_equal_column(i
);
1468 void process_table(table_input
&in
)
1474 if ((opt
= process_options(in
)) != 0
1475 && (form
= process_format(in
, opt
)) != 0
1476 && (tbl
= process_data(in
, form
, opt
)) != 0) {
1481 error("giving up on this table");
1482 while ((c
= in
.get()) != EOF
)
1488 error("premature end of file");
1491 static void usage(FILE *stream
)
1493 fprintf(stream
, "usage: %s [ -vC ] [ files... ]\n", program_name
);
1496 int main(int argc
, char **argv
)
1498 program_name
= argv
[0];
1499 static char stderr_buf
[BUFSIZ
];
1500 setbuf(stderr
, stderr_buf
);
1502 static const struct option long_options
[] = {
1503 { "help", no_argument
, 0, CHAR_MAX
+ 1 },
1504 { "version", no_argument
, 0, 'v' },
1507 while ((opt
= getopt_long(argc
, argv
, "vCT:", long_options
, NULL
)) != EOF
)
1510 compatible_flag
= 1;
1514 printf("GNU tbl (groff) version %s\n", Version_string
);
1519 // I'm sick of getting bug reports from IRIX users
1521 case CHAR_MAX
+ 1: // --help
1532 printf(".if !\\n(.g .ab GNU tbl requires GNU troff.\n"
1534 ".if !dTE .ds TE\n");
1535 if (argc
> optind
) {
1536 for (int i
= optind
; i
< argc
; i
++)
1537 if (argv
[i
][0] == '-' && argv
[i
][1] == '\0') {
1538 current_filename
= "-";
1540 printf(".lf 1 -\n");
1541 process_input_file(stdin
);
1545 FILE *fp
= fopen(argv
[i
], "r");
1547 current_lineno
= -1;
1548 error("can't open `%1': %2", argv
[i
], strerror(errno
));
1552 current_filename
= argv
[i
];
1553 printf(".lf 1 %s\n", current_filename
);
1554 process_input_file(fp
);
1559 current_filename
= "-";
1561 printf(".lf 1 -\n");
1562 process_input_file(stdin
);
1564 if (ferror(stdout
) || fflush(stdout
) < 0)
1565 fatal("output error");