summaryrefslogtreecommitdiff
path: root/texinfo.c
blob: 1b464b637de30012cc28f1d763332368c81e4231 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/* $Id: texinfo.c,v 1.1 2004-05-03 05:17:48 behdad Exp $
 * functions for texinfo style output.
 */
#include "c2man.h"
#include "manpage.h"
#include "output.h"
#include <ctype.h>

static char *heading_not_in_contents[] =
	 {"@chapheading ", "@heading ", "@subheading ", "@subsubheading "};
static char *heading_in_contents[] =
	 {"@chapter ", "@section ", "@subsection ", "@subsubsection "};

#define n_levels  (sizeof(heading_not_in_contents) / sizeof(char *))

#define level(n) ((n) >= n_levels ? n_levels - 1 : (n))

/* section level for man page entry */
static int top_level = 1;

/* always output node for manpage, even if embedded */
static int embed_node_info = 0;

/* use title as name of section, rather than "NAME" */
static int title_name = 0;

/* the section title, filled in by texinfo_header */
static const char *title = "INTERNAL ERROR, BOGUS TITLE DUDE!";

/* do section titles get capitalized? */
static int capitalize_sections = 0;

void texinfo_char(c)
const int c;
{
    int i;

    switch(c)
    {
    case '\t':
	for (i = 0; i < NUM_TAB_SPACES; i++)
	    putchar(' ');
	break;
    default:
    	putchar(c);
	break;
    }
}

void texinfo_text(text)
const char *text;
{
    while (*text)
	texinfo_char(*text++);
}

void put_section(text)
const char *text;
{

   if (capitalize_sections)
   {
     int first_letter = 1;

     for ( ; *text ; text++)
     {
       texinfo_char(first_letter ? toupper(*text) : tolower(*text));
       first_letter = isspace(*text);
     }
   }
   else
     texinfo_text(text);
}

void texinfo_comment() { put_string("@c "); }

void texinfo_header(firstpage, input_files, grouped, name, terse, section)
ManualPage *firstpage;
int input_files;
boolean grouped;
const char *name;
const char *terse;
const char *section;
{
    if (! make_embeddable)
    {
	put_string("\\input texinfo @c -*-texinfo-*-\n");
	output_warning();
	put_string("@c %**start of header\n");
	put_string("@setfilename ");
	texinfo_text(name);
	put_string(".info\n@settitle ");
	texinfo_text(name);
	putchar('\n');
	put_string("@c %**end of header\n");

	put_string("@node Top, ");
	texinfo_text(name);
	put_string(", (dir), (dir)\n");
    }

    if (! make_embeddable || embed_node_info)
    {
      put_string("@node ");
      texinfo_text(name);
      put_string(", (dir), Top, (dir)\n");
    }

    title = name;
}

void texinfo_dash()	{ put_string("---"); }

void texinfo_section(name)
const char *name;
{
    put_string(heading_not_in_contents[level(top_level)]);
    put_section(name);
    putchar('\n');
    put_string("@noindent\n");
}

void texinfo_section_in_contents(name)
const char *name;
{
    put_string(heading_in_contents[level(top_level)]);
    put_section(name);
    putchar('\n');
    put_string("@noindent\n");
}

void texinfo_sub_section(name)
const char *name;
{
    put_string(heading_not_in_contents[level(top_level+1)]);
    put_section(name);
    putchar('\n');
    put_string("@noindent\n");
}

void texinfo_break_line() { /* put_string("@*\n"); */ }
void texinfo_blank_line() { put_string("@sp 1\n"); }

void texinfo_code_start() { put_string("@example\n"); }
void texinfo_code_end()	{ put_string("@end example\n"); }

void texinfo_code(text)
const char *text;
{
    put_string("@code{");
    texinfo_text(text);
    put_string("}");
}

void texinfo_tag_list_start()	{ put_string("@quotation\n@table @code\n"); }
void texinfo_tag_entry_start()	{ put_string("@item "); }
void texinfo_tag_entry_end()	{ putchar('\n'); }

void texinfo_tag_entry_end_extra(text)
const char *text;
{
    putchar('(');
    texinfo_text(text);
    putchar(')');
    texinfo_tag_entry_end();
}
void texinfo_tag_list_end()	{ put_string("@end table\n@end quotation\n"); }
	
void texinfo_table_start(longestag)
const char *longestag;
{ put_string("@quotation\n@table @code\n"); }

void texinfo_table_entry(name, description)
const char *name;
const char *description;
{
    put_string("@item ");
    texinfo_text(name);
    putchar('\n');
    if (description)
	output_comment(description);
    else
	putchar('\n');
}

void texinfo_table_end()	{ put_string("@end table\n@end quotation\n"); }

void texinfo_list_start()	{ }
void texinfo_list_entry(text)
const char *text;
{
    texinfo_code(text);
}
void texinfo_list_separator() { put_string(",\n"); }
void texinfo_list_end()	{ putchar('\n'); }

void texinfo_include(filename)
const char *filename;
{
	put_string("@include ");
	texinfo_text(filename);
	put_string("\n");
}

void texinfo_file_end() { put_string("@bye\n"); }

static first_name = 1;
void texinfo_name(name)
const char *name;
{
    if (name)
    {
	if (!first_name || !title_name || strcmp(title,name))
	   texinfo_text(name);
	first_name = 0;
    }
    else
    {
	first_name = 1;
	if (title_name)
	{
	    /* don't muck around with capitalization of title */
	    int capitalize_sections_save = capitalize_sections;
	    capitalize_sections = 0;

	    texinfo_section_in_contents(title);
	    capitalize_sections = capitalize_sections_save;
	}
	else
	    texinfo_section("NAME");
    }
}

void texinfo_terse_sep()
{
    if (!title_name || group_together)
    {
	texinfo_char(' ');
	texinfo_dash();
	texinfo_char(' ');
    }
}

void texinfo_reference(text)
const char *text;
{
    texinfo_text(text);
    texinfo_char('(');
    texinfo_text(manual_section);
    texinfo_char(')');
}

/* ideally, this should be made aware of embedded texinfo commands */
void texinfo_description(text)
const char *text;
{
    enum { TEXT, PERIOD, CAPITALISE } state = CAPITALISE;
    boolean new_line = TRUE;
    
    /* correct punctuation a bit as it goes out */
    for (;*text;text++)
    {
	int c = *text;

	if (new_line && (c == '-' || c == '*' || is_numbered(text)))
	{
	    output->break_line();
	    state = CAPITALISE;
	}
	else if (c == '.')
	    state = PERIOD;
	else if (isspace(c) && state == PERIOD)
	    state = CAPITALISE;
	else if (isalnum(c))
	{   
	    if (islower(c) && state == CAPITALISE)	c = toupper(c);
	    state = TEXT;
	}
           
	output->character(c);
	new_line = c == '\n';
    }

    /* do a full stop if there wasn't one */
    if (state == TEXT)	output->character('.');
}

/* ideally, this should be made aware of embedded texinfo commands */
void
texinfo_returns(comment)
const char *comment;
{
    enum { TEXT, PERIOD, CAPITALISE } state = CAPITALISE;
    char lastchar = '\n';
    boolean tag_list_started = FALSE;

    /* for each line... */
    while (*comment)
    {
	boolean tagged = FALSE;

	{
	    const char *c = comment;

	    /* search along until the end of a word */
	    while (*c && *c != ':' && !isspace(*c))
		c++;

	    /* skip all spaces or tabs after the first word */
	    while (*c && *c != '\n')
	    {
		if (*c == '\t' || *c == ':')
		{
		    tagged = TRUE;
		    break;
		}
		else if (!isspace(*c))
		    break;

		c++;
	    }
	}

	/* is it tagged?; explicitly reject dot commands */
	if (tagged)
	{
	    /* output lingering newline if necessary */
	    if (lastchar != '\n')
	    {
		if (state == TEXT && !ispunct(lastchar))	output->character('.');
		output->character(lastchar = '\n');
	    }

	    if (!tag_list_started)
	    {
		output->tag_list_start();
		tag_list_started = TRUE;
	    }

	    /* output the taggy bit */
	    output->tag_entry_start();
	    while (*comment && *comment != ':' && !isspace(*comment))
		output->character(*comment++);
	    output->tag_entry_end();

	    /* skip any extra tabs or spaces */
	    while (*comment == ':' || (isspace(*comment) && *comment != '\n'))
		comment++;

	    state = CAPITALISE;
	}

	/* terminate the previous line if necessary */
	if (lastchar != '\n')	output->character(lastchar = '\n');

	/* correct punctuation a bit as the line goes out */
	for (;*comment && *comment != '\n'; comment++)
	{
	    char c = *comment;

	    if (c == '.')
		state = PERIOD;
	    else if (isspace(c) && state == PERIOD)
		state = CAPITALISE;
	    else if (isalnum(c))
	    {   
		if (islower(c) && state == CAPITALISE && fixup_comments)
		    c = toupper(c);
		state = TEXT;
	    }

	    output->character(lastchar = c);
	}

	/* if it ended in punctuation, just output the nl straight away. */
	if (ispunct(lastchar))
	{
	    if (lastchar == '.')	state = CAPITALISE;
	    output->character(lastchar = '\n');
	}

	if (*comment)	comment++;
    }

    /* output lingering newline if necessary */
    if (lastchar != '\n')
    {
	if (state == TEXT && !ispunct(lastchar) && fixup_comments)
	    output->character('.');
	output->character('\n');
    }

    if (tag_list_started)
	output->tag_list_end();
}


int texinfo_parse_option(option)
const char *option;
{
    if	    (option[0] == 't')
	title_name = 1;
    else if (option[0] == 'n')
	embed_node_info = 1;
    else if (option[0] == 's')
    {
	top_level = atoi(&option[1]);
	if (top_level < 0) return 1;
    }
    else if (option[0] == 'C')
	capitalize_sections = 1;
    else return 1;

    return 0;
}

void texinfo_print_options()
{
    fputs("\ttexinfo options:\n", stderr);
    fputs("\tt\tuse manpage title as NAME title\n", stderr);
    fputs("\tn\toutput node info if embedded output\n", stderr);
    fputs("\ts<n>\tset top heading level to <n>\n", stderr);
    fputs("\tC\tcaptialize section titles\n", stderr);
}


struct Output texinfo_output =
{
    texinfo_comment,
    texinfo_header,
    texinfo_dash,
    texinfo_section,
    texinfo_sub_section,
    texinfo_break_line,
    texinfo_blank_line,
    texinfo_code_start,
    texinfo_code_end,
    texinfo_code,
    texinfo_tag_list_start,
    texinfo_tag_list_end,
    texinfo_tag_entry_start,
    texinfo_tag_entry_start,	/* entry_start_extra */
    texinfo_tag_entry_end,
    texinfo_tag_entry_end_extra,
    texinfo_table_start,
    texinfo_table_entry,
    texinfo_table_end,
    dummy,		/* texinfo_indent */
    texinfo_list_start,
    texinfo_list_entry,
    texinfo_list_separator,
    texinfo_list_end,
    texinfo_include,
    texinfo_file_end,
    texinfo_text,
    texinfo_char,
    texinfo_parse_option,
    texinfo_print_options,
    texinfo_name,
    texinfo_terse_sep,
    texinfo_reference,
    texinfo_text,
    texinfo_description,
    texinfo_returns
};