4 /* Copyright (C) 1989, 1990, 1991, 1992, 2000, 2001, 2002, 2003, 2004, 2005
5 Free Software Foundation, Inc.
6 Written by James Clark (jjc@jclark.com)
8 This file is part of groff.
10 groff is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 2, or (at your option) any later
15 groff is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 You should have received a copy of the GNU General Public License along
21 with groff; see the file COPYING. If not, write to the Free Software
22 Foundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA. */
26 #define MAX_POINT_SIZE 99
27 #define MAX_VERTICAL_SPACING 72
29 extern "C" const char *Version_string
;
31 int compatible_flag
= 0;
36 REREAD_T
, REREAD_TE
, REREAD_E
,
37 LEADER_1
, LEADER_2
, LEADER_3
, LEADER_4
,
43 int ended() { return unget_stack
.empty() && state
== END
; }
47 table_input::table_input(FILE *p
)
52 void table_input::unget(char c
)
60 int table_input::get()
62 int len
= unget_stack
.length();
64 unsigned char c
= unget_stack
[len
- 1];
65 unget_stack
.set_length(len
- 1);
74 if ((c
= getc(fp
)) == '.') {
75 if ((c
= getc(fp
)) == 'T') {
76 if ((c
= getc(fp
)) == 'E') {
77 if (compatible_flag
) {
85 if (c
== EOF
|| c
== ' ' || c
== '\n') {
117 error("invalid input character code 0");
125 // handle line continuation and uninterpreted leader character
126 if ((c
= getc(fp
)) == '\\') {
129 c
= getc(fp
); // perhaps state ought to be START now
130 else if (c
== 'a' && compatible_flag
) {
149 else if (c
== '\0') {
150 error("invalid input character code 0");
183 void process_input_file(FILE *);
184 void process_table(table_input
&in
);
186 void process_input_file(FILE *fp
)
188 enum { START
, MIDDLE
, HAD_DOT
, HAD_T
, HAD_TS
, HAD_l
, HAD_lf
} state
;
191 while ((c
= getc(fp
)) != EOF
)
243 if (c
== ' ' || c
== '\n' || compatible_flag
) {
249 error("end of file at beginning of table");
258 table_input
input(fp
);
259 process_table(input
);
260 set_troff_location(current_filename
, current_lineno
);
262 fputs(".TE", stdout
);
263 while ((c
= getc(fp
)) != '\n') {
277 fputs(".TS", stdout
);
298 if (c
== ' ' || c
== '\n' || compatible_flag
) {
309 interpret_lf_args(line
.contents());
310 printf(".lf%s", line
.contents());
314 fputs(".lf", stdout
);
329 fputs(".\n", stdout
);
332 fputs(".l\n", stdout
);
335 fputs(".T\n", stdout
);
338 fputs(".lf\n", stdout
);
341 fputs(".TS\n", stdout
);
353 char decimal_point_char
;
359 : flags(0), linesize(0), tab_char('\t'), decimal_point_char('.')
361 delim
[0] = delim
[1] = '\0';
364 // Return non-zero if p and q are the same ignoring case.
366 int strieq(const char *p
, const char *q
)
368 for (; cmlower(*p
) == cmlower(*q
); p
++, q
++)
374 // return 0 if we should give up in this table
376 options
*process_options(table_input
&in
)
378 options
*opt
= new options
;
384 int i
= line
.length();
391 int i
= line
.length();
400 else if (c
== ';' && level
== 0) {
410 while (!csalpha(*p
) && *p
!= '\0')
418 if (*q
!= '(' && *q
!= '\0')
425 while (*q
!= ')' && *q
!= '\0')
428 error("missing `)'");
434 error("argument without option");
436 else if (strieq(p
, "tab")) {
438 error("`tab' option requires argument in parentheses");
440 if (arg
[0] == '\0' || arg
[1] != '\0')
441 error("argument to `tab' option must be a single character");
443 opt
->tab_char
= arg
[0];
446 else if (strieq(p
, "linesize")) {
448 error("`linesize' option requires argument in parentheses");
450 if (sscanf(arg
, "%d", &opt
->linesize
) != 1)
451 error("bad linesize `%s'", arg
);
452 else if (opt
->linesize
<= 0) {
453 error("linesize must be positive");
458 else if (strieq(p
, "delim")) {
460 error("`delim' option requires argument in parentheses");
461 else if (arg
[0] == '\0' || arg
[1] == '\0' || arg
[2] != '\0')
462 error("argument to `delim' option must be two characters");
464 opt
->delim
[0] = arg
[0];
465 opt
->delim
[1] = arg
[1];
468 else if (strieq(p
, "center") || strieq(p
, "centre")) {
470 error("`center' option does not take an argument");
471 opt
->flags
|= table::CENTER
;
473 else if (strieq(p
, "expand")) {
475 error("`expand' option does not take an argument");
476 opt
->flags
|= table::EXPAND
;
478 else if (strieq(p
, "box") || strieq(p
, "frame")) {
480 error("`box' option does not take an argument");
481 opt
->flags
|= table::BOX
;
483 else if (strieq(p
, "doublebox") || strieq(p
, "doubleframe")) {
485 error("`doublebox' option does not take an argument");
486 opt
->flags
|= table::DOUBLEBOX
;
488 else if (strieq(p
, "allbox")) {
490 error("`allbox' option does not take an argument");
491 opt
->flags
|= table::ALLBOX
;
493 else if (strieq(p
, "nokeep")) {
495 error("`nokeep' option does not take an argument");
496 opt
->flags
|= table::NOKEEP
;
498 else if (strieq(p
, "nospaces")) {
500 error("`nospaces' option does not take an argument");
501 opt
->flags
|= table::NOSPACES
;
503 else if (strieq(p
, "decimalpoint")) {
505 error("`decimalpoint' option requires argument in parentheses");
507 if (arg
[0] == '\0' || arg
[1] != '\0')
508 error("argument to `decimalpoint' option must be a single character");
510 opt
->decimal_point_char
= arg
[0];
514 error("unrecognised global option `%1'", p
);
523 entry_modifier::entry_modifier()
524 : vertical_alignment(CENTER
), zero_width(0), stagger(0)
526 vertical_spacing
.inc
= vertical_spacing
.val
= 0;
527 point_size
.inc
= point_size
.val
= 0;
530 entry_modifier::~entry_modifier()
534 entry_format::entry_format() : type(FORMAT_LEFT
)
538 entry_format::entry_format(format_type t
) : type(t
)
542 void entry_format::debug_print() const
557 case FORMAT_ALPHABETIC
:
569 case FORMAT_DOUBLE_HLINE
:
576 if (point_size
.val
!= 0) {
578 if (point_size
.inc
> 0)
580 else if (point_size
.inc
< 0)
582 fprintf(stderr
, "%d ", point_size
.val
);
584 if (vertical_spacing
.val
!= 0) {
586 if (vertical_spacing
.inc
> 0)
588 else if (vertical_spacing
.inc
< 0)
590 fprintf(stderr
, "%d ", vertical_spacing
.val
);
594 put_string(font
, stderr
);
597 if (!macro
.empty()) {
599 put_string(macro
, stderr
);
602 switch (vertical_alignment
) {
603 case entry_modifier::CENTER
:
605 case entry_modifier::TOP
:
608 case entry_modifier::BOTTOM
:
624 entry_format
**entry
;
627 format(int nr
, int nc
);
629 void add_rows(int n
);
632 format::format(int nr
, int nc
) : nrows(nr
), ncolumns(nc
)
635 separation
= ncolumns
> 1 ? new int[ncolumns
- 1] : 0;
636 for (i
= 0; i
< ncolumns
-1; i
++)
638 width
= new string
[ncolumns
];
639 equal
= new char[ncolumns
];
640 for (i
= 0; i
< ncolumns
; i
++)
642 entry
= new entry_format
*[nrows
];
643 for (i
= 0; i
< nrows
; i
++)
644 entry
[i
] = new entry_format
[ncolumns
];
645 vline
= new char*[nrows
];
646 for (i
= 0; i
< nrows
; i
++) {
647 vline
[i
] = new char[ncolumns
+1];
648 for (int j
= 0; j
< ncolumns
+1; j
++)
653 void format::add_rows(int n
)
656 char **old_vline
= vline
;
657 vline
= new char*[nrows
+ n
];
658 for (i
= 0; i
< nrows
; i
++)
659 vline
[i
] = old_vline
[i
];
661 for (i
= 0; i
< n
; i
++) {
662 vline
[nrows
+ i
] = new char[ncolumns
+ 1];
663 for (int j
= 0; j
< ncolumns
+ 1; j
++)
664 vline
[nrows
+ i
][j
] = 0;
666 entry_format
**old_entry
= entry
;
667 entry
= new entry_format
*[nrows
+ n
];
668 for (i
= 0; i
< nrows
; i
++)
669 entry
[i
] = old_entry
[i
];
671 for (i
= 0; i
< n
; i
++)
672 entry
[nrows
+ i
] = new entry_format
[ncolumns
];
679 ad_delete(ncolumns
) width
;
681 for (int i
= 0; i
< nrows
; i
++) {
683 ad_delete(ncolumns
) entry
[i
];
689 struct input_entry_format
: public entry_format
{
690 input_entry_format
*next
;
697 input_entry_format(format_type
, input_entry_format
* = 0);
698 ~input_entry_format();
702 input_entry_format::input_entry_format(format_type t
, input_entry_format
*p
)
703 : entry_format(t
), next(p
)
712 input_entry_format::~input_entry_format()
716 void free_input_entry_format_list(input_entry_format
*list
)
719 input_entry_format
*tem
= list
;
725 void input_entry_format::debug_print()
728 for (i
= 0; i
< pre_vline
; i
++)
730 entry_format::debug_print();
731 if (!width
.empty()) {
734 put_string(width
, stderr
);
740 fprintf(stderr
, "%d", separation
);
741 for (i
= 0; i
< vline
; i
++)
747 // Return zero if we should give up on this table.
748 // If this is a continuation format line, current_format will be the current
751 format
*process_format(table_input
&in
, options
*opt
,
752 format
*current_format
= 0)
754 input_entry_format
*list
= 0;
760 format_type t
= FORMAT_LEFT
;
763 error("end of input while processing format");
764 free_input_entry_format_list(list
);
776 t
= FORMAT_ALPHABETIC
;
803 case '-': // tbl also accepts this
809 t
= FORMAT_DOUBLE_HLINE
;
822 if (c
== opt
->tab_char
)
824 error("unrecognised format `%1'", char(c
));
825 free_input_entry_format_list(list
);
836 list
= new input_entry_format(t
, list
);
838 list
->pre_vline
= pre_vline
;
845 list
->vertical_alignment
= entry_modifier::TOP
;
850 list
->vertical_alignment
= entry_modifier::BOTTOM
;
860 list
->zero_width
= 1;
875 w
= w
*10 + (c
- '0');
877 } while (c
!= EOF
&& csdigit(c
));
878 list
->separation
= w
;
885 } while (c
== ' ' || c
== '\t');
887 error("missing font name");
893 if (c
== EOF
|| c
== ' ' || c
== '\t') {
894 error("missing `)'");
901 list
->font
+= char(c
);
909 && c
!= EOF
&& c
!= ' ' && c
!= '\t' && c
!= '.' && c
!= '\n') {
910 list
->font
+= char(c
);
919 } while (c
== ' ' || c
== '\t');
921 error("missing macro name");
927 if (c
== EOF
|| c
== ' ' || c
== '\t') {
928 error("missing `)'");
935 list
->macro
+= char(c
);
943 && c
!= EOF
&& c
!= ' ' && c
!= '\t' && c
!= '.' && c
!= '\n') {
944 list
->macro
+= char(c
);
952 list
->vertical_spacing
.val
= 0;
953 list
->vertical_spacing
.inc
= 0;
954 if (c
== '+' || c
== '-') {
955 list
->vertical_spacing
.inc
= (c
== '+' ? 1 : -1);
958 if (c
== EOF
|| !csdigit(c
)) {
959 error("`v' modifier must be followed by number");
960 list
->vertical_spacing
.inc
= 0;
964 list
->vertical_spacing
.val
*= 10;
965 list
->vertical_spacing
.val
+= c
- '0';
967 } while (c
!= EOF
&& csdigit(c
));
969 if (list
->vertical_spacing
.val
> MAX_VERTICAL_SPACING
970 || list
->vertical_spacing
.val
< -MAX_VERTICAL_SPACING
) {
971 error("unreasonable vertical spacing");
972 list
->vertical_spacing
.val
= 0;
973 list
->vertical_spacing
.inc
= 0;
979 list
->point_size
.val
= 0;
980 list
->point_size
.inc
= 0;
981 if (c
== '+' || c
== '-') {
982 list
->point_size
.inc
= (c
== '+' ? 1 : -1);
985 if (c
== EOF
|| !csdigit(c
)) {
986 error("`p' modifier must be followed by number");
987 list
->point_size
.inc
= 0;
991 list
->point_size
.val
*= 10;
992 list
->point_size
.val
+= c
- '0';
994 } while (c
!= EOF
&& csdigit(c
));
996 if (list
->point_size
.val
> MAX_POINT_SIZE
997 || list
->point_size
.val
< -MAX_POINT_SIZE
) {
998 error("unreasonable point size");
999 list
->point_size
.val
= 0;
1000 list
->point_size
.inc
= 0;
1006 while (c
== ' ' || c
== '\t')
1012 if (c
== EOF
|| c
== '\n') {
1013 error("missing `)'");
1014 free_input_entry_format_list(list
);
1023 if (c
== '+' || c
== '-') {
1024 list
->width
= char(c
);
1029 if (c
== EOF
|| !csdigit(c
))
1030 error("bad argument for `w' modifier");
1033 list
->width
+= char(c
);
1035 } while (c
!= EOF
&& csdigit(c
));
1063 if (c
== opt
->tab_char
)
1070 if (list
->vline
> 2) {
1072 error("more than 2 vertical bars between key letters");
1074 if (c
== '\n' || c
== ',') {
1076 list
->last_column
= 1;
1082 } while (c
== ' ' || c
== '\t');
1084 error("`.' not last character on line");
1085 free_input_entry_format_list(list
);
1091 free_input_entry_format_list(list
);
1094 list
->last_column
= 1;
1095 // now reverse the list so that the first row is at the beginning
1096 input_entry_format
*rev
= 0;
1098 input_entry_format
*tem
= list
->next
;
1104 input_entry_format
*tem
;
1107 for (tem
= list
; tem
; tem
= tem
->next
)
1111 // compute number of columns and rows
1115 for (tem
= list
; tem
; tem
= tem
->next
) {
1116 if (tem
->last_column
) {
1117 if (col
>= ncolumns
)
1127 if (current_format
) {
1128 if (ncolumns
> current_format
->ncolumns
) {
1129 error("cannot increase the number of columns in a continued format");
1130 free_input_entry_format_list(list
);
1138 f
= new format(nrows
, ncolumns
);
1142 for (tem
= list
; tem
; tem
= tem
->next
) {
1143 f
->entry
[row
][col
] = *tem
;
1144 if (col
< ncolumns
-1) {
1145 // use the greatest separation
1146 if (tem
->separation
> f
->separation
[col
]) {
1148 error("cannot change column separation in continued format");
1150 f
->separation
[col
] = tem
->separation
;
1153 else if (tem
->separation
>= 0)
1154 error("column separation specified for last column");
1155 if (tem
->equal
&& !f
->equal
[col
]) {
1157 error("cannot change which columns are equal in continued format");
1161 if (!tem
->width
.empty()) {
1162 // use the last width
1163 if (!f
->width
[col
].empty() && f
->width
[col
] != tem
->width
)
1164 error("multiple widths for column %1", col
+1);
1165 f
->width
[col
] = tem
->width
;
1167 if (tem
->pre_vline
) {
1169 f
->vline
[row
][col
] = tem
->pre_vline
;
1171 f
->vline
[row
][col
+1] = tem
->vline
;
1172 if (tem
->last_column
) {
1179 free_input_entry_format_list(list
);
1180 for (col
= 0; col
< ncolumns
; col
++) {
1181 entry_format
*e
= f
->entry
[f
->nrows
-1] + col
;
1182 if (e
->type
!= FORMAT_HLINE
1183 && e
->type
!= FORMAT_DOUBLE_HLINE
1184 && e
->type
!= FORMAT_SPAN
)
1187 if (col
>= ncolumns
) {
1188 error("last row of format is all lines");
1195 table
*process_data(table_input
&in
, format
*f
, options
*opt
)
1197 char tab_char
= opt
->tab_char
;
1198 int ncolumns
= f
->ncolumns
;
1199 int current_row
= 0;
1200 int format_index
= 0;
1202 enum { DATA_INPUT_LINE
, TROFF_INPUT_LINE
, SINGLE_HLINE
, DOUBLE_HLINE
} type
;
1203 table
*tbl
= new table(ncolumns
, opt
->flags
, opt
->linesize
,
1204 opt
->decimal_point_char
);
1205 if (opt
->delim
[0] != '\0')
1206 tbl
->set_delim(opt
->delim
[0], opt
->delim
[1]);
1208 // first determine what type of line this is
1214 if (d
!= EOF
&& csdigit(d
)) {
1216 type
= DATA_INPUT_LINE
;
1220 type
= TROFF_INPUT_LINE
;
1223 else if (c
== '_' || c
== '=') {
1227 type
= SINGLE_HLINE
;
1229 type
= DOUBLE_HLINE
;
1233 type
= DATA_INPUT_LINE
;
1237 type
= DATA_INPUT_LINE
;
1240 case DATA_INPUT_LINE
:
1243 if (format_index
>= f
->nrows
)
1244 format_index
= f
->nrows
- 1;
1245 // A format row that is all lines doesn't use up a data line.
1246 while (format_index
< f
->nrows
- 1) {
1248 for (cnt
= 0; cnt
< ncolumns
; cnt
++) {
1249 entry_format
*e
= f
->entry
[format_index
] + cnt
;
1250 if (e
->type
!= FORMAT_HLINE
1251 && e
->type
!= FORMAT_DOUBLE_HLINE
1252 // Unfortunately tbl treats a span as needing data.
1253 // && e->type != FORMAT_SPAN
1259 for (cnt
= 0; cnt
< ncolumns
; cnt
++)
1260 tbl
->add_entry(current_row
, cnt
, input_entry
,
1261 f
->entry
[format_index
] + cnt
, current_filename
,
1263 tbl
->add_vlines(current_row
, f
->vline
[format_index
]);
1267 entry_format
*line_format
= f
->entry
[format_index
];
1269 int row_comment
= 0;
1271 if (c
== tab_char
|| c
== '\n') {
1272 int ln
= current_lineno
;
1275 if ((opt
->flags
& table::NOSPACES
))
1276 input_entry
.remove_spaces();
1277 while (col
< ncolumns
1278 && line_format
[col
].type
== FORMAT_SPAN
) {
1279 tbl
->add_entry(current_row
, col
, "", &line_format
[col
],
1280 current_filename
, ln
);
1283 if (c
== '\n' && input_entry
.length() == 2
1284 && input_entry
[0] == 'T' && input_entry
[1] == '{') {
1288 START
, MIDDLE
, GOT_T
, GOT_RIGHT_BRACE
, GOT_DOT
,
1291 while (state
!= END
) {
1309 state
= GOT_RIGHT_BRACE
;
1313 state
= c
== '\n' ? START
: MIDDLE
;
1322 state
= c
== '\n' ? START
: MIDDLE
;
1329 input_entry
+= ".l";
1331 state
= c
== '\n' ? START
: MIDDLE
;
1335 if (c
== ' ' || c
== '\n' || compatible_flag
) {
1337 input_entry
+= ".lf";
1345 interpret_lf_args(args
.contents());
1347 args
.set_length(args
.length() - 1);
1348 input_entry
+= args
;
1352 input_entry
+= ".lf";
1357 case GOT_RIGHT_BRACE
:
1358 if ((opt
->flags
& table::NOSPACES
)) {
1364 if (c
== '\n' || c
== tab_char
)
1384 error("end of data in middle of text block");
1389 if (col
>= ncolumns
) {
1390 if (!input_entry
.empty()) {
1391 if (input_entry
.length() >= 2
1392 && input_entry
[0] == '\\'
1393 && input_entry
[1] == '"')
1395 else if (!row_comment
) {
1398 input_entry
+= '\0';
1399 error("excess data entry `%1' discarded",
1400 input_entry
.contents());
1407 tbl
->add_entry(current_row
, col
, input_entry
,
1408 &line_format
[col
], current_filename
, ln
);
1423 for (; col
< ncolumns
; col
++)
1424 tbl
->add_entry(current_row
, col
, input_entry
, &line_format
[col
],
1425 current_filename
, current_lineno
- 1);
1426 tbl
->add_vlines(current_row
, f
->vline
[format_index
]);
1431 case TROFF_INPUT_LINE
:
1434 int ln
= current_lineno
;
1444 tbl
->add_text_line(current_row
, line
, current_filename
, ln
);
1445 if (line
.length() >= 4
1446 && line
[0] == '.' && line
[1] == 'T' && line
[2] == '&') {
1447 format
*newf
= process_format(in
, opt
, f
);
1453 if (line
.length() >= 3
1454 && line
[0] == '.' && line
[1] == 'l' && line
[2] == 'f') {
1456 interpret_lf_args(line
.contents() + 3);
1461 tbl
->add_single_hline(current_row
);
1464 tbl
->add_double_hline(current_row
);
1472 if (!give_up
&& current_row
== 0) {
1473 error("no real data");
1480 // Do this here rather than at the beginning in case continued formats
1483 for (i
= 0; i
< ncolumns
- 1; i
++)
1484 if (f
->separation
[i
] >= 0)
1485 tbl
->set_column_separation(i
, f
->separation
[i
]);
1486 for (i
= 0; i
< ncolumns
; i
++)
1487 if (!f
->width
[i
].empty())
1488 tbl
->set_minimum_width(i
, f
->width
[i
]);
1489 for (i
= 0; i
< ncolumns
; i
++)
1491 tbl
->set_equal_column(i
);
1495 void process_table(table_input
&in
)
1500 if ((opt
= process_options(in
)) != 0
1501 && (form
= process_format(in
, opt
)) != 0
1502 && (tbl
= process_data(in
, form
, opt
)) != 0) {
1507 error("giving up on this table");
1508 while (in
.get() != EOF
)
1514 error("premature end of file");
1517 static void usage(FILE *stream
)
1519 fprintf(stream
, "usage: %s [ -vC ] [ files... ]\n", program_name
);
1522 int main(int argc
, char **argv
)
1524 program_name
= argv
[0];
1525 static char stderr_buf
[BUFSIZ
];
1526 setbuf(stderr
, stderr_buf
);
1528 static const struct option long_options
[] = {
1529 { "help", no_argument
, 0, CHAR_MAX
+ 1 },
1530 { "version", no_argument
, 0, 'v' },
1533 while ((opt
= getopt_long(argc
, argv
, "vCT:", long_options
, NULL
)) != EOF
)
1536 compatible_flag
= 1;
1540 printf("GNU tbl (groff) version %s\n", Version_string
);
1545 // I'm sick of getting bug reports from IRIX users
1547 case CHAR_MAX
+ 1: // --help
1558 printf(".if !\\n(.g .ab GNU tbl requires GNU troff.\n"
1560 ".if !dTE .ds TE\n");
1561 if (argc
> optind
) {
1562 for (int i
= optind
; i
< argc
; i
++)
1563 if (argv
[i
][0] == '-' && argv
[i
][1] == '\0') {
1564 current_filename
= "-";
1566 printf(".lf 1 -\n");
1567 process_input_file(stdin
);
1571 FILE *fp
= fopen(argv
[i
], "r");
1573 fatal("can't open `%1': %2", argv
[i
], strerror(errno
));
1576 current_filename
= argv
[i
];
1577 printf(".lf 1 %s\n", current_filename
);
1578 process_input_file(fp
);
1583 current_filename
= "-";
1585 printf(".lf 1 -\n");
1586 process_input_file(stdin
);
1588 if (ferror(stdout
) || fflush(stdout
) < 0)
1589 fatal("output error");