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 #define BUFFER_SIZE 256
28 static int width
; /* Output width in characters */
29 static int col
= 0; /* Current output column */
30 static FILE *toc_file
; /* TOC file */
31 static int out_row
= 1; /* Current output row */
32 static int in_row
= 0; /* Current input row */
33 static int indent
= 1;
34 static int curindent
= 1;
35 static int freshnl
= 1;
36 static int firstlen
= 0;
37 static int verbatim
= 0;
39 /* Report error in input */
40 void print_error (char *message
)
42 fprintf (stderr
, "fixhlp: %s at row %d\n", message
, in_row
);
45 /* Change output line */
51 printf("\n%*s", indent
, "");
56 /* Calculate the length of string */
57 int string_len (char *buffer
)
59 static int anchor_flag
= 0; /* Flag: Inside hypertext anchor name */
60 static int link_flag
= 0; /* Flag: Inside hypertext link target name */
62 int c
; /* Current character */
63 int len
= 0; /* Result: the length of the string */
65 for (i
= 0; i
< strlen (buffer
); i
++)
68 if (c
== CHAR_LINK_POINTER
)
69 link_flag
= 1; /* Link target name starts */
70 else if (c
== CHAR_LINK_END
)
71 link_flag
= 0; /* Link target name ends */
72 else if (c
== CHAR_NODE_END
){
73 /* Node anchor name starts */
75 /* Ugly hack to prevent loss of one space */
78 /* Don't add control characters to the length */
81 /* Attempt to handle backslash quoting */
82 /* Increase length if not inside anchor name or link target name */
83 if (!anchor_flag
&& !link_flag
)
85 if (anchor_flag
&& c
== ']'){
86 /* Node anchor name ends */
93 /* Output the string */
94 void print_string (char *buffer
)
96 int len
; /* The length of current word */
98 int c
; /* Current character */
101 /* Split into words */
103 printf ("%s", buffer
);
107 p
= strchr (buffer
, CHAR_LINK_POINTER
);
112 print_string (buffer
);
113 q
= strchr (p
+ 1, CHAR_LINK_END
);
116 printf ("%c%s%c", CHAR_LINK_POINTER
, p
+ 1, CHAR_LINK_END
);
117 print_string (q
+ 1);
119 /* Error, but try to handle it somehow */
120 printf ("%c", CHAR_LINK_END
);
124 buffer
= strtok (buffer
, " \t\n");
125 /* Repeat for each word */
127 /* Skip empty strings */
128 if (strlen (buffer
) > 0){
129 len
= string_len (buffer
);
130 /* Change the line if about to break the right margin */
131 if (col
+ len
>= width
)
133 /* Words are separated by spaces */
134 if (col
> curindent
){
138 printf ("%s", buffer
);
139 /* Increase column */
142 /* Get the next word */
143 buffer
= strtok (NULL
, " \t\n");
146 firstlen
= col
- curindent
;
151 /* Like print_string but with printf-like syntax */
152 void printf_string (char *format
, ...)
155 char buffer
[BUFFER_SIZE
];
157 va_start (args
, format
);
158 vsprintf (buffer
, format
, args
);
160 print_string (buffer
);
163 int main (int argc
, char **argv
)
165 int len
; /* Length of input line */
166 char buffer
[BUFFER_SIZE
]; /* Input line */
169 int ignore_newline
= 0;
171 /* Validity check for arguments */
172 if (argc
!= 3 || (width
= atoi (argv
[1])) <= 10){
173 fprintf (stderr
, _("Usage: fixhlp <width> <tocname>\n"));
177 if ((toc_file
= fopen (argv
[2], "w")) == NULL
) {
178 fprintf (stderr
, _("fixhlp: Cannot open toc for writing"));
181 fprintf (toc_file
, _("\04[Contents]\n Topics:\n\n"));
183 /* Repeat for each input line */
184 while (!feof (stdin
)){
186 if (!fgets (buffer
, BUFFER_SIZE
, stdin
)){
190 len
= strlen (buffer
);
191 /* Remove terminating newline */
192 if (buffer
[len
-1] == '\n')
203 if (buffer
[0] == 4 && buffer
[1] == '[') {
204 for (p
= buffer
+ 2; *p
== ' '; p
++);
205 fprintf (toc_file
, "%*s\01 %s \02%s\03\n", p
- buffer
+ 1, "", p
, p
);
206 printf ("\04[%s]\n %s", p
, p
);
207 } else if (buffer
[0] == CHAR_RESERVED
&& buffer
[1] == '"') {
213 for (p
= buffer
, q
= strchr (p
, CHAR_RESERVED
); q
!= NULL
;
214 p
= q
+ 1, q
= strchr (p
, CHAR_RESERVED
)) {
227 if (ignore_newline
) {
229 if (i
> indent
- curindent
- 1)
232 i
= indent
- curindent
- i
- 1;
233 printf ("%*s", i
, "");