2 Copyright (C) 1994, 1995 Janne Kukonlehto
3 Copyright (C) 1995 Jakub Jelinek
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
26 static int width
; /* Output width in characters */
27 static int col
= 0; /* Current output column */
28 static FILE *toc_file
; /* TOC file */
29 static int out_row
= 1; /* Current output row */
30 static int in_row
= 0; /* Current input row */
31 static int indent
= 1;
32 static int curindent
= 1;
33 static int freshnl
= 1;
34 static int firstlen
= 0;
35 static int verbatim
= 0;
37 /* Report error in input */
38 void print_error (char *message
)
40 fprintf (stderr
, "fixhlp: %s at row %d\n", message
, in_row
);
43 /* Change output line */
49 printf("\n%*s", indent
, "");
54 /* Calculate the length of string */
55 int string_len (char *buffer
)
57 static int anchor_flag
= 0; /* Flag: Inside hypertext anchor name */
58 static int link_flag
= 0; /* Flag: Inside hypertext link target name */
60 int c
; /* Current character */
61 int len
= 0; /* Result: the length of the string */
63 for (i
= 0; i
< strlen (buffer
); i
++)
66 if (c
== CHAR_LINK_POINTER
)
67 link_flag
= 1; /* Link target name starts */
68 else if (c
== CHAR_LINK_END
)
69 link_flag
= 0; /* Link target name ends */
70 else if (c
== CHAR_NODE_END
){
71 /* Node anchor name starts */
73 /* Ugly hack to prevent loss of one space */
76 /* Don't add control characters to the length */
79 /* Attempt to handle backslash quoting */
80 /* Increase length if not inside anchor name or link target name */
81 if (!anchor_flag
&& !link_flag
)
83 if (anchor_flag
&& c
== ']'){
84 /* Node anchor name ends */
91 /* Output the string */
92 void print_string (char *buffer
)
94 int len
; /* The length of current word */
96 int c
; /* Current character */
99 /* Split into words */
101 printf ("%s", buffer
);
105 p
= strchr (buffer
, CHAR_LINK_POINTER
);
110 print_string (buffer
);
111 q
= strchr (p
+ 1, CHAR_LINK_END
);
114 printf ("%c%s%c", CHAR_LINK_POINTER
, p
+ 1, CHAR_LINK_END
);
115 print_string (q
+ 1);
117 /* Error, but try to handle it somehow */
118 printf ("%c", CHAR_LINK_END
);
122 buffer
= strtok (buffer
, " \t\n");
123 /* Repeat for each word */
125 /* Skip empty strings */
126 if (strlen (buffer
) > 0){
127 len
= string_len (buffer
);
128 /* Change the line if about to break the right margin */
129 if (col
+ len
>= width
)
131 /* Words are separated by spaces */
132 if (col
> curindent
){
136 printf ("%s", buffer
);
137 /* Increase column */
140 /* Get the next word */
141 buffer
= strtok (NULL
, " \t\n");
144 firstlen
= col
- curindent
;
149 /* Like print_string but with printf-like syntax */
150 void printf_string (char *format
, ...)
153 char buffer
[BUF_LARGE
];
155 va_start (args
, format
);
156 vsprintf (buffer
, format
, args
);
158 print_string (buffer
);
161 int main (int argc
, char **argv
)
163 int len
; /* Length of input line */
164 char buffer
[BUF_LARGE
]; /* Input line */
167 int ignore_newline
= 0;
169 /* Validity check for arguments */
170 if (argc
!= 3 || (width
= atoi (argv
[1])) <= 10){
171 fprintf (stderr
, _("Usage: fixhlp <width> <tocname>\n"));
175 if ((toc_file
= fopen (argv
[2], "w")) == NULL
) {
176 fprintf (stderr
, _("fixhlp: Cannot open toc for writing"));
179 fprintf (toc_file
, _("\04[Contents]\n Topics:\n\n"));
181 /* Repeat for each input line */
182 while (!feof (stdin
)){
184 if (!fgets (buffer
, sizeof(buffer
), stdin
)){
188 len
= strlen (buffer
);
189 /* Remove terminating newline */
190 if (buffer
[len
-1] == '\n')
201 if (buffer
[0] == 4 && buffer
[1] == '[') {
202 for (p
= buffer
+ 2; *p
== ' '; p
++);
203 fprintf (toc_file
, "%*s\01 %s \02%s\03\n", p
- buffer
+ 1, "", p
, p
);
204 printf ("\04[%s]\n %s", p
, p
);
205 } else if (buffer
[0] == CHAR_RESERVED
&& buffer
[1] == '"') {
211 for (p
= buffer
, q
= strchr (p
, CHAR_RESERVED
); q
!= NULL
;
212 p
= q
+ 1, q
= strchr (p
, CHAR_RESERVED
)) {
225 if (ignore_newline
) {
227 if (i
> indent
- curindent
- 1)
230 i
= indent
- curindent
- i
- 1;
231 printf ("%*s", i
, "");