Clean menu -- made to use standart GNOME menu entries
[midnight-commander.git] / src / fixhlp.c
blobd3fcf1d2496f5cdfc2b2bd315c3ca1b427dade31
1 /* HLP converter
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. */
19 #include <config.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <stdarg.h>
23 #include <string.h>
24 #include "help.h"
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 */
44 void newline (void)
46 out_row ++;
47 col = indent;
48 curindent = indent;
49 printf("\n%*s", indent, "");
50 freshnl = 1;
51 firstlen = 0;
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 */
59 int i; /* Index */
60 int c; /* Current character */
61 int len = 0; /* Result: the length of the string */
63 for (i = 0; i < strlen (buffer); i ++)
65 c = 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 */
72 anchor_flag = 1;
73 /* Ugly hack to prevent loss of one space */
74 len ++;
76 /* Don't add control characters to the length */
77 if (c < 32)
78 continue;
79 /* Attempt to handle backslash quoting */
80 /* Increase length if not inside anchor name or link target name */
81 if (!anchor_flag && !link_flag)
82 len ++;
83 if (anchor_flag && c == ']'){
84 /* Node anchor name ends */
85 anchor_flag = 0;
88 return len;
91 /* Output the string */
92 void print_string (char *buffer)
94 int len; /* The length of current word */
95 int i; /* Index */
96 int c; /* Current character */
97 char *p;
99 /* Split into words */
100 if (verbatim) {
101 printf ("%s", buffer);
102 newline ();
103 return;
105 p = strchr (buffer, CHAR_LINK_POINTER);
106 if (p) {
107 char *q;
109 *p = 0;
110 print_string (buffer);
111 q = strchr (p + 1, CHAR_LINK_END);
112 if (q) {
113 *q = 0;
114 printf ("%c%s%c", CHAR_LINK_POINTER, p + 1, CHAR_LINK_END);
115 print_string (q + 1);
116 } else {
117 /* Error, but try to handle it somehow */
118 printf ("%c", CHAR_LINK_END);
120 return;
122 buffer = strtok (buffer, " \t\n");
123 /* Repeat for each word */
124 while (buffer){
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)
130 newline ();
131 /* Words are separated by spaces */
132 if (col > curindent){
133 printf (" ");
134 col ++;
136 printf ("%s", buffer);
137 /* Increase column */
138 col += len;
140 /* Get the next word */
141 buffer = strtok (NULL, " \t\n");
142 } /* while */
143 if (freshnl) {
144 firstlen = col - curindent;
145 freshnl = 0;
149 /* Like print_string but with printf-like syntax */
150 void printf_string (char *format, ...)
152 va_list args;
153 char buffer [BUF_LARGE];
155 va_start (args, format);
156 vsprintf (buffer, format, args);
157 va_end (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 */
165 int i, j;
166 char *p;
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"));
172 return 3;
175 if ((toc_file = fopen (argv[2], "w")) == NULL) {
176 fprintf (stderr, _("fixhlp: Cannot open toc for writing"));
177 return 4;
179 fprintf (toc_file, _("\04[Contents]\n Topics:\n\n"));
181 /* Repeat for each input line */
182 while (!feof (stdin)){
183 /* Read a line */
184 if (!fgets (buffer, sizeof(buffer), stdin)){
185 break;
187 in_row ++;
188 len = strlen (buffer);
189 /* Remove terminating newline */
190 if (buffer [len-1] == '\n')
192 len --;
193 buffer [len] = 0;
195 if (!buffer[0]) {
196 if (ignore_newline)
197 ignore_newline = 0;
198 else
199 newline ();
200 } else {
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] == '"') {
206 continue;
207 } else {
208 char *p, *q;
209 int i;
211 for (p = buffer, q = strchr (p, CHAR_RESERVED); q != NULL;
212 p = q + 1, q = strchr (p, CHAR_RESERVED)) {
213 *q = 0;
214 if (*p)
215 print_string (p);
216 q++;
217 if (*q == '/')
218 ignore_newline = 1;
219 else if (*q == 'v')
220 verbatim = 1;
221 else if (*q == 'n')
222 verbatim = 0;
223 else {
224 indent = *q - '0';
225 if (ignore_newline) {
226 i = firstlen;
227 if (i > indent - curindent - 1)
228 ignore_newline = 0;
229 else {
230 i = indent - curindent - i - 1;
231 printf ("%*s", i, "");
232 col += i;
237 print_string (p);
242 /* All done */
243 newline ();
244 return 0;