Debian Codegraph @

doc/yat2m.c

raw
1 /* yat2m.c - Yet Another Texi 2 Man converter
2 * Copyright (C) 2005, 2013, 2015, 2016, 2017, 2023 g10 Code GmbH
3 * Copyright (C) 2006, 2008, 2011 Free Software Foundation, Inc.
4 *
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 3 of the License, or
8 * (at your option) any later version.
9 *
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.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <https://www.gnu.org/licenses/>.
17 */
18
19 /*
20 This is a simple texinfo to man page converter. It needs some
21 special markup in th e texinfo and tries best to get a create man
22 page. It has been designed for the GnuPG man pages and thus only
23 a few texinfo commands are supported.
24
25 To use this you need to add the following macros into your texinfo
26 source:
27
28 @macro manpage {a}
29 @end macro
30 @macro mansect {a}
31 @end macro
32 @macro manpause
33 @end macro
34 @macro mancont
35 @end macro
36
37 They are used by yat2m to select parts of the Texinfo which should
38 go into the man page. These macros need to be used without leading
39 left space. Processing starts after a "manpage" macro has been
40 seen. "mansect" identifies the section and yat2m make sure to
41 emit the sections in the proper order. Note that @mansect skips
42 the next input line if that line begins with @section, @subsection or
43 @chapheading.
44
45 To insert verbatim troff markup, the following texinfo code may be
46 used:
47
48 @ifset manverb
49 .B whateever you want
50 @end ifset
51
52 alternatively a special comment may be used:
53
54 @c man:.B whatever you want
55
56 This is useful in case you need just one line. If you want to
57 include parts only in the man page but keep the texinfo
58 translation you may use:
59
60 @ifset isman
61 stuff to be rendered only on man pages
62 @end ifset
63
64 or to exclude stuff from man pages:
65
66 @ifclear isman
67 stuff not to be rendered on man pages
68 @end ifclear
69
70 the keyword @section is ignored, however @subsection gets rendered
71 as ".SS". @menu is completely skipped. Several man pages may be
72 extracted from one file, either using the --store or the --select
73 option.
74
75 If you want to indent tables in the source use this style:
76
77 @table foo
78 @item
79 @item
80 @table
81 @item
82 @end
83 @end
84
85 Don't change the indentation within a table and keep the same
86 number of white space at the start of the line. yat2m simply
87 detects the number of white spaces in front of an @item and removes
88 this number of spaces from all following lines until a new @item
89 is found or there are less spaces than for the last @item.
90
91 Note that @* does only work correctly if used at the end of an
92 input line.
93
94 */
95
96 #include <stdio.h>
97 #include <stdlib.h>
98 #include <stddef.h>
99 #include <string.h>
100 #include <errno.h>
101 #include <stdarg.h>
102 #include <assert.h>
103 #include <ctype.h>
104 #include <time.h>
105
106
107 #if __GNUC__
108 # define MY_GCC_VERSION (__GNUC__ * 10000 \
109 + __GNUC_MINOR__ * 100 \
110 + __GNUC_PATCHLEVEL__)
111 #else
112 # define MY_GCC_VERSION 0
113 #endif
114
115 #if MY_GCC_VERSION >= 20500
116 # define ATTR_PRINTF(f, a) __attribute__ ((format(printf,f,a)))
117 # define ATTR_NR_PRINTF(f, a) __attribute__ ((__noreturn__, format(printf,f,a)))
118 #else
119 # define ATTR_PRINTF(f, a)
120 # define ATTR_NR_PRINTF(f, a)
121 #endif
122 #if MY_GCC_VERSION >= 30200
123 # define ATTR_MALLOC __attribute__ ((__malloc__))
124 #else
125 # define ATTR_MALLOC
126 #endif
127 #if __GNUC__ >= 4
128 # define ATTR_SENTINEL(a) __attribute__ ((sentinel(a)))
129 #else
130 # define ATTR_SENTINEL(a)
131 #endif
132 #define DIM(v) (sizeof(v)/sizeof((v)[0]))
133 #define DIMof(type,member) DIM(((type *)0)->member)
134 #define spacep(p) (*(p) == ' ' || *(p) == '\t')
135 #define digitp(p) (*(p) >= '0' && *(p) <= '9')
136
137
138 #define PGM "yat2m"
139 #ifdef PACKAGE_VERSION
140 # define VERSION PACKAGE_VERSION
141 #else
142 # define VERSION "1.1"
143 #endif
144
145 /* The maximum length of a line including the linefeed and one extra
146 character. */
147 #define LINESIZE 1024
148
149 /* Number of allowed condition nestings. */
150 #define MAX_CONDITION_NESTING 10
151
152 /* Number of allowed table nestings. */
153 #define MAX_TABLE_NESTING 10
154
155 static char const default_css[] =
156 "<style type=\"text/css\">\n"
157 " .y2m {\n"
158 " font-family: monospace;\n"
159 " }\n"
160 " .y2m u {\n"
161 " text-decoration: underline;\n"
162 " }\n"
163 " .y2m-sc {\n"
164 " font-variant: small-caps;\n"
165 " }\n"
166 " .y2m li {\n"
167 " margin-top: 1em;\n"
168 " }\n"
169 " .y2m-item {\n"
170 " display: block;\n"
171 " font-weight: bold;\n"
172 " }\n"
173 " .y2m-args {\n"
174 " font-weight: normal;\n"
175 " }\n"
176 "</style>\n";
177
178
179
180 /* Option flags. */
181 static int verbose;
182 static int quiet;
183 static int debug;
184 static int htmlmode;
185 static int gnupgorgmode;
186 static const char *opt_source;
187 static const char *opt_release;
188 static const char *opt_date;
189 static const char *opt_select;
190 static const char *opt_include;
191 static int opt_store;
192
193 /* Flag to keep track whether any error occurred. */
194 static int any_error;
195
196
197 /* Memory buffer stuff. */
198 struct private_membuf_s
199 {
200 size_t len;
201 size_t size;
202 char *buf;
203 int out_of_core;
204 };
205 typedef struct private_membuf_s membuf_t;
206
207
208 /* Object to keep macro definitions. */
209 struct macro_s
210 {
211 struct macro_s *next;
212 char *value; /* Malloced value. */
213 char name[1];
214 };
215 typedef struct macro_s *macro_t;
216
217 /* List of all defined macros. */
218 static macro_t macrolist;
219
220 /* List of variables set by @set. */
221 static macro_t variablelist;
222
223 /* List of global macro names. The value part is not used. */
224 static macro_t predefinedmacrolist;
225
226 /* Object to keep track of @isset and @ifclear. */
227 struct condition_s
228 {
229 int manverb; /* "manverb" needs special treatment. */
230 int isset; /* This is an @isset condition. */
231 char name[1]; /* Name of the condition macro. */
232 };
233 typedef struct condition_s *condition_t;
234
235 /* The stack used to evaluate conditions. And the current states. */
236 static condition_t condition_stack[MAX_CONDITION_NESTING];
237 static int condition_stack_idx;
238 static int cond_is_active; /* State of ifset/ifclear */
239 static int cond_in_verbatim; /* State of "manverb". */
240
241 /* The stack used to evaluate table setting for @item and @itemx.
242 * 1: argument of @item should be handled \- as minus
243 * 0: argument of @item should be handled - as hyphen
244 */
245 static int table_item_stack[MAX_TABLE_NESTING];
246
247 /* Variable to parse en-dash and em-dash.
248 * -1: not parse until the end of the line
249 * 0: not parse
250 * 1: parse -- for en-dash, --- for em-dash
251 */
252 static int cond_parse_dash;
253
254 /* Variable to generate \- (minus) for roff, which may distinguishes
255 * minus and hyphen.
256 * > 0 : emit \-
257 * 0 : emit -
258 */
259 static int cond_2D_as_minus;
260
261 /* Object to store one line of content. */
262 struct line_buffer_s
263 {
264 struct line_buffer_s *next;
265 int verbatim; /* True if LINE contains verbatim data. The default
266 is Texinfo source. */
267 char *line;
268 };
269 typedef struct line_buffer_s *line_buffer_t;
270
271
272 /* Object to collect the data of a section. */
273 struct section_buffer_s
274 {
275 char *name; /* Malloced name of the section. This may be
276 NULL to indicate this slot is not used. */
277 line_buffer_t lines; /* Linked list with the lines of the section. */
278 line_buffer_t *lines_tail; /* Helper for faster appending to the
279 linked list. */
280 line_buffer_t last_line; /* Points to the last line appended. */
281 unsigned int is_see_also:1; /* This is the SEE ALSO section. */
282 unsigned int in_para:1; /* Track whether we are in paragraph. */
283 unsigned int in_pre:1; /* Track whether we are in a html pre. */
284 };
285 typedef struct section_buffer_s *section_buffer_t;
286
287 /* Variable to keep info about the current page together. */
288 static struct
289 {
290 /* Filename of the current page or NULL if no page is active. Malloced. */
291 char *name;
292
293 /* Number of allocated elements in SECTIONS below. */
294 size_t n_sections;
295 /* Array with the data of the sections. */
296 section_buffer_t sections;
297
298 } thepage;
299
300
301 /* The list of standard section names. COMMANDS and ASSUAN are GnuPG
302 specific. */
303 static const char * const standard_sections[] =
304 { "NAME", "SYNOPSIS", "DESCRIPTION",
305 "RETURN VALUE", "EXIT STATUS", "ERROR HANDLING", "ERRORS",
306 "COMMANDS", "OPTIONS", "USAGE", "EXAMPLES", "FILES",
307 "ENVIRONMENT", "DIAGNOSTICS", "SECURITY", "CONFORMING TO",
308 "ASSUAN", "NOTES", "BUGS", "AUTHOR", "SEE ALSO", NULL };
309
310
311 /* Actions to be done at the end of a line. */
312 enum eol_actions
313 {
314 EOL_NOTHING = 0,
315 EOL_CLOSE_SUBSECTION
316 };
317
318
319 /*-- Local prototypes. --*/
320 static void proc_texi_buffer (FILE *fp, const char *line, size_t len,
321 int *table_level, int *eol_action,
322 section_buffer_t sect, int char_2D_is_minus);
323
324 static void die (const char *format, ...) ATTR_NR_PRINTF(1,2);
325 static void err (const char *format, ...) ATTR_PRINTF(1,2);
326 static void inf (const char *format, ...) ATTR_PRINTF(1,2);
327 static void *xmalloc (size_t n) ATTR_MALLOC;
328 static void *xcalloc (size_t n, size_t m) ATTR_MALLOC;
329 static char *xstrconcat (const char *s1, ...) ATTR_SENTINEL(0);
330
331
332
333 /*-- Functions --*/
334
335 /* Print diagnostic message and exit with failure. */
336 static void
337 die (const char *format, ...)
338 {
339 va_list arg_ptr;
340
341 fflush (stdout);
342 fprintf (stderr, "%s: ", PGM);
343
344 va_start (arg_ptr, format);
345 vfprintf (stderr, format, arg_ptr);
346 va_end (arg_ptr);
347 putc ('\n', stderr);
348
349 exit (1);
350 }
351
352
353 /* Print diagnostic message. */
354 static void
355 err (const char *format, ...)
356 {
357 va_list arg_ptr;
358
359 fflush (stdout);
360 if (strncmp (format, "%s:%d:", 6))
361 fprintf (stderr, "%s: ", PGM);
362
363 va_start (arg_ptr, format);
364 vfprintf (stderr, format, arg_ptr);
365 va_end (arg_ptr);
366 putc ('\n', stderr);
367 any_error = 1;
368 }
369
370 /* Print diagnostic message. */
371 static void
372 inf (const char *format, ...)
373 {
374 va_list arg_ptr;
375
376 fflush (stdout);
377 fprintf (stderr, "%s: ", PGM);
378
379 va_start (arg_ptr, format);
380 vfprintf (stderr, format, arg_ptr);
381 va_end (arg_ptr);
382 putc ('\n', stderr);
383 }
384
385
386 static void *
387 xmalloc (size_t n)
388 {
389 void *p = malloc (n);
390 if (!p)
391 die ("out of core: %s", strerror (errno));
392 return p;
393 }
394
395 static void *
396 xcalloc (size_t n, size_t m)
397 {
398 void *p = calloc (n, m);
399 if (!p)
400 die ("out of core: %s", strerror (errno));
401 return p;
402 }
403
404 static void *
405 xrealloc (void *old, size_t n)
406 {
407 void *p = realloc (old, n);
408 if (!p)
409 die ("out of core: %s", strerror (errno));
410 return p;
411 }
412
413 static char *
414 xstrdup (const char *string)
415 {
416 void *p = malloc (strlen (string)+1);
417 if (!p)
418 die ("out of core: %s", strerror (errno));
419 strcpy (p, string);
420 return p;
421 }
422
423 static char *
424 mystpcpy (char *a,const char *b)
425 {
426 while( *b )
427 *a++ = *b++;
428 *a = 0;
429
430 return (char*)a;
431 }
432
433
434 /* Helper for xstrconcat and strconcat. */
435 static char *
436 do_strconcat (int xmode, const char *s1, va_list arg_ptr)
437 {
438 const char *argv[48];
439 size_t argc;
440 size_t needed;
441 char *buffer, *p;
442
443 argc = 0;
444 argv[argc++] = s1;
445 needed = strlen (s1);
446 while (((argv[argc] = va_arg (arg_ptr, const char *))))
447 {
448 needed += strlen (argv[argc]);
449 if (argc >= DIM (argv)-1)
450 die ("too may args for strconcat\n");
451 argc++;
452 }
453 needed++;
454 buffer = xmode? xmalloc (needed) : malloc (needed);
455 for (p = buffer, argc=0; argv[argc]; argc++)
456 p = mystpcpy (p, argv[argc]);
457
458 return buffer;
459 }
460
461
462 /* Concatenate the string S1 with all the following strings up to a
463 * NULL. Returns a malloced buffer with the new string or die. */
464 static char *
465 xstrconcat (const char *s1, ...)
466 {
467 va_list arg_ptr;
468 char *result;
469
470 if (!s1)
471 result = xstrdup ("");
472 else
473 {
474 va_start (arg_ptr, s1);
475 result = do_strconcat (1, s1, arg_ptr);
476 va_end (arg_ptr);
477 }
478 return result;
479 }
480
481
482 /* A simple implementation of a dynamic buffer. Use init_membuf() to
483 * create a buffer, put_membuf to append bytes and get_membuf to
484 * release and return the buffer. Allocation errors are detected but
485 * only returned at the final get_membuf(), this helps not to clutter
486 * the code with out of core checks. */
487 static void
488 init_membuf (membuf_t *mb, int initiallen)
489 {
490 mb->len = 0;
491 mb->size = initiallen;
492 mb->out_of_core = 0;
493 mb->buf = malloc (initiallen);
494 if (!mb->buf)
495 mb->out_of_core = errno;
496 }
497
498
499 /* Store (BUF,LEN) in the memory buffer MB. */
500 static void
501 put_membuf (membuf_t *mb, const void *buf, size_t len)
502 {
503 if (mb->out_of_core || !len)
504 return;
505
506 if (mb->len + len >= mb->size)
507 {
508 char *p;
509
510 mb->size += len + 1024;
511 p = realloc (mb->buf, mb->size);
512 if (!p)
513 {
514 mb->out_of_core = errno ? errno : ENOMEM;
515 return;
516 }
517 mb->buf = p;
518 }
519 if (buf)
520 memcpy (mb->buf + mb->len, buf, len);
521 else
522 memset (mb->buf + mb->len, 0, len);
523 mb->len += len;
524 }
525
526
527 /* Store STRING in the memory buffer MB. */
528 static void
529 put_membuf_str (membuf_t *mb, const char *string)
530 {
531 if (!string)
532 string= "";
533 put_membuf (mb, string, strlen (string));
534 }
535
536
537 /* Return the value of a memory buffer. LEN is optional. */
538 static void *
539 get_membuf (membuf_t *mb, size_t *len)
540 {
541 char *p;
542
543 if (mb->out_of_core)
544 {
545 if (mb->buf)
546 {
547 free (mb->buf);
548 mb->buf = NULL;
549 }
550 errno = mb->out_of_core;
551 }
552
553 p = mb->buf;
554 if (len)
555 *len = mb->len;
556 mb->buf = NULL;
557 mb->out_of_core = ENOMEM; /* hack to make sure it won't get reused. */
558 return p;
559 }
560
561
562 /* Return a STRING from the memory buffer MB or die. */
563 static char *
564 xget_membuf (membuf_t *mb)
565 {
566 char *p;
567 put_membuf (mb, "", 1);
568 p = get_membuf (mb, NULL);
569 if (!p)
570 die ("out of core in xget_membuf: %s", strerror (errno));
571 return p;
572 }
573
574
575 /* Uppercase the ascii characters in STRING. */
576 static char *
577 ascii_strupr (char *string)
578 {
579 char *p;
580
581 for (p = string; *p; p++)
582 if (!(*p & 0x80))
583 *p = toupper (*p);
584 return string;
585 }
586
587
588 /* Return the current date as an ISO string. */
589 const char *
590 isodatestring (void)
591 {
592 static char buffer[36];
593 struct tm *tp;
594 time_t atime;
595
596 if (opt_date && *opt_date)
597 atime = strtoul (opt_date, NULL, 10);
598 else
599 atime = time (NULL);
600 if (atime < 0)
601 strcpy (buffer, "????" "-??" "-??");
602 else
603 {
604 tp = gmtime (&atime);
605 sprintf (buffer,"%04d-%02d-%02d",
606 1900+tp->tm_year, tp->tm_mon+1, tp->tm_mday );
607 }
608 return buffer;
609 }
610
611
612 /* Add NAME to the list of predefined macros which are global for all
613 files. */
614 static void
615 add_predefined_macro (const char *name)
616 {
617 macro_t m;
618
619 for (m=predefinedmacrolist; m; m = m->next)
620 if (!strcmp (m->name, name))
621 break;
622 if (!m)
623 {
624 m = xcalloc (1, sizeof *m + strlen (name));
625 strcpy (m->name, name);
626 m->next = predefinedmacrolist;
627 predefinedmacrolist = m;
628 }
629 }
630
631
632 /* Create or update a macro with name MACRONAME and set its values TO
633 MACROVALUE. Note that ownership of the macro value is transferred
634 to this function. */
635 static void
636 set_macro (const char *macroname, char *macrovalue)
637 {
638 macro_t m;
639
640 for (m=macrolist; m; m = m->next)
641 if (!strcmp (m->name, macroname))
642 break;
643 if (m)
644 free (m->value);
645 else
646 {
647 m = xcalloc (1, sizeof *m + strlen (macroname));
648 strcpy (m->name, macroname);
649 m->next = macrolist;
650 macrolist = m;
651 }
652 m->value = macrovalue;
653 macrovalue = NULL;
654 }
655
656
657 /* Create or update a variable with name and value given in NAMEANDVALUE. */
658 static void
659 set_variable (char *nameandvalue)
660 {
661 macro_t m;
662 const char *value;
663 char *p;
664
665 for (p = nameandvalue; *p && *p != ' ' && *p != '\t'; p++)
666 ;
667 if (!*p)
668 value = "";
669 else
670 {
671 *p++ = 0;
672 while (*p == ' ' || *p == '\t')
673 p++;
674 value = p;
675 }
676
677 for (m=variablelist; m; m = m->next)
678 if (!strcmp (m->name, nameandvalue))
679 break;
680 if (m)
681 free (m->value);
682 else
683 {
684 m = xcalloc (1, sizeof *m + strlen (nameandvalue));
685 strcpy (m->name, nameandvalue);
686 m->next = variablelist;
687 variablelist = m;
688 }
689 m->value = xstrdup (value);
690 }
691
692
693 /* Return true if the macro or variable NAME is set, i.e. not the
694 empty string and not evaluating to 0. */
695 static int
696 macro_set_p (const char *name)
697 {
698 macro_t m;
699
700 for (m = macrolist; m ; m = m->next)
701 if (!strcmp (m->name, name))
702 break;
703 if (!m)
704 for (m = variablelist; m ; m = m->next)
705 if (!strcmp (m->name, name))
706 break;
707 if (!m || !m->value || !*m->value)
708 return 0;
709 if ((*m->value & 0x80) || !isdigit (*m->value))
710 return 1; /* Not a digit but some other string. */
711 return !!atoi (m->value);
712 }
713
714
715 /* Evaluate the current conditions. */
716 static void
717 evaluate_conditions (const char *fname, int lnr)
718 {
719 int i;
720
721 (void)fname;
722 (void)lnr;
723
724 /* for (i=0; i < condition_stack_idx; i++) */
725 /* inf ("%s:%d: stack[%d] %s %s %c", */
726 /* fname, lnr, i, condition_stack[i]->isset? "set":"clr", */
727 /* condition_stack[i]->name, */
728 /* (macro_set_p (condition_stack[i]->name) */
729 /* ^ !condition_stack[i]->isset)? 't':'f'); */
730
731 cond_is_active = 1;
732 cond_in_verbatim = 0;
733 if (condition_stack_idx)
734 {
735 for (i=0; i < condition_stack_idx; i++)
736 {
737 if (condition_stack[i]->manverb)
738 cond_in_verbatim = (macro_set_p (condition_stack[i]->name)
739 ^ !condition_stack[i]->isset);
740 else if (!(macro_set_p (condition_stack[i]->name)
741 ^ !condition_stack[i]->isset))
742 {
743 cond_is_active = 0;
744 break;
745 }
746 }
747 }
748
749 /* inf ("%s:%d: active=%d verbatim=%d", */
750 /* fname, lnr, cond_is_active, cond_in_verbatim); */
751 }
752
753
754 /* Push a condition with condition macro NAME onto the stack. If
755 ISSET is true, a @isset condition is pushed. */
756 static void
757 push_condition (const char *name, int isset, const char *fname, int lnr)
758 {
759 condition_t cond;
760 int manverb = 0;
761
762 if (condition_stack_idx >= MAX_CONDITION_NESTING)
763 {
764 err ("%s:%d: condition nested too deep", fname, lnr);
765 return;
766 }
767
768 if (!strcmp (name, "manverb"))
769 {
770 if (!isset)
771 {
772 err ("%s:%d: using \"@ifclear manverb\" is not allowed", fname, lnr);
773 return;
774 }
775 manverb = 1;
776 }
777
778 cond = xcalloc (1, sizeof *cond + strlen (name));
779 cond->manverb = manverb;
780 cond->isset = isset;
781 strcpy (cond->name, name);
782
783 condition_stack[condition_stack_idx++] = cond;
784 evaluate_conditions (fname, lnr);
785 }
786
787
788 /* Remove the last condition from the stack. ISSET is used for error
789 reporting. */
790 static void
791 pop_condition (int isset, const char *fname, int lnr)
792 {
793 if (!condition_stack_idx)
794 {
795 err ("%s:%d: unbalanced \"@end %s\"",
796 fname, lnr, isset?"isset":"isclear");
797 return;
798 }
799 condition_stack_idx--;
800 free (condition_stack[condition_stack_idx]);
801 condition_stack[condition_stack_idx] = NULL;
802 evaluate_conditions (fname, lnr);
803 }
804
805
806
807 /* Return a section buffer for the section NAME. Allocate a new buffer
808 if this is a new section. Keep track of the sections in THEPAGE.
809 This function may reallocate the section array in THEPAGE. */
810 static section_buffer_t
811 get_section_buffer (const char *name)
812 {
813 int i;
814 section_buffer_t sect;
815
816 /* If there is no section we put everything into the required NAME
817 section. Given that this is the first one listed it is likely
818 that error are easily visible. */
819 if (!name)
820 name = "NAME";
821
822 for (i=0; i < thepage.n_sections; i++)
823 {
824 sect = thepage.sections + i;
825 if (sect->name && !strcmp (name, sect->name))
826 return sect;
827 }
828 for (i=0; i < thepage.n_sections; i++)
829 if (!thepage.sections[i].name)
830 break;
831 if (thepage.n_sections && i < thepage.n_sections)
832 sect = thepage.sections + i;
833 else
834 {
835 /* We need to allocate or reallocate the section array. */
836 size_t old_n = thepage.n_sections;
837 size_t new_n = 20;
838
839 if (!old_n)
840 thepage.sections = xcalloc (new_n, sizeof *thepage.sections);
841 else
842 {
843 thepage.sections = xrealloc (thepage.sections,
844 ((old_n + new_n)
845 * sizeof *thepage.sections));
846 memset (thepage.sections + old_n, 0,
847 new_n * sizeof *thepage.sections);
848 }
849 thepage.n_sections += new_n;
850
851 /* Setup the tail pointers. */
852 for (i=old_n; i < thepage.n_sections; i++)
853 {
854 sect = thepage.sections + i;
855 sect->lines_tail = &sect->lines;
856 }
857 sect = thepage.sections + old_n;
858 }
859
860 /* Store the name. */
861 assert (!sect->name);
862 sect->name = xstrdup (name);
863 sect->is_see_also = !strcmp (name, "SEE ALSO");
864 return sect;
865 }
866
867
868 /* Return a malloced string alternating between bold and italics (or
869 * other) font attributes. */
870 static char *
871 roff_alternate (const char *line, const char *mode)
872 {
873 const char *s;
874 membuf_t mb;
875 enum {
876 x_init,
877 x_roman,
878 x_bold,
879 x_italics
880 } state, nextstate[2];
881 int toggle; /* values are 0 and 1 to index nextstate. */
882
883 init_membuf (&mb, 128);
884
885 state = x_init;
886 for (toggle = 0; toggle < 2; toggle++)
887 {
888 if (mode[toggle] == 'B')
889 nextstate[toggle] = x_bold;
890 else if (mode[toggle] == 'I')
891 nextstate[toggle] = x_italics;
892 else
893 nextstate[toggle] = x_roman;
894 }
895 toggle = 0;
896 for (s=line; *s; s++)
897 {
898 if (state == x_init)
899 {
900 if (!(*s == ' ' || *s == '\t'))
901 {
902 state = nextstate[toggle%2];
903 toggle++;
904 if (state == x_bold)
905 put_membuf_str (&mb, "<strong>");
906 else if (state == x_italics)
907 put_membuf_str (&mb, "<em>");
908 else
909 put_membuf_str (&mb, "<span>");
910 }
911 }
912 else
913 {
914 if (*s == ' ' || *s == '\t')
915 {
916 if (state == x_bold)
917 put_membuf_str (&mb, "</strong>");
918 else if (state == x_italics)
919 put_membuf_str (&mb, "</em>");
920 else
921 put_membuf_str (&mb, "</span>");
922
923 put_membuf (&mb, s, 1);
924 state = x_init;
925 }
926 }
927
928 put_membuf (&mb, s, 1);
929 }
930 if (state == x_bold)
931 put_membuf_str (&mb, "</strong>");
932 else if (state == x_italics)
933 put_membuf_str (&mb, "</em>");
934 else if (state == x_roman)
935 put_membuf_str (&mb, "</span>");
936
937 return xget_membuf (&mb);
938 }
939
940
941 /* Add the content of LINE to the section named SECTNAME. */
942 static void
943 add_content (const char *sectname, const char *line, int verbatim)
944 {
945 section_buffer_t sect;
946 line_buffer_t lb;
947 char *linebuffer = NULL;
948 char *src, *dst;
949 const char *s;
950
951 if (verbatim && htmlmode)
952 {
953 /* This is roff rendered - map this to HTML. */
954 if (!strncmp (line, ".B ", 3))
955 linebuffer = xstrconcat ("<strong>", line+3, "</strong>", NULL);
956 else if (!strncmp (line, ".I ", 3))
957 linebuffer = xstrconcat ("<em>", line+3, "</em>", NULL);
958 else if (!strncmp (line, ".BI ", 4))
959 linebuffer = roff_alternate (line+4, "BI");
960 else if (!strncmp (line, ".IB ", 4))
961 linebuffer = roff_alternate (line+4, "IB");
962 else if (!strncmp (line, ".BR ", 4))
963 linebuffer = roff_alternate (line+4, "BR");
964 else if (!strncmp (line, ".RB ", 4))
965 linebuffer = roff_alternate (line+4, "RB");
966 else if (!strncmp (line, ".RI ", 4))
967 linebuffer = roff_alternate (line+4, "RI");
968 else if (!strncmp (line, ".IR ", 4))
969 linebuffer = roff_alternate (line+4, "IR");
970 else if (!strncmp (line, ".br", 3))
971 linebuffer = xstrdup ("<br/>");
972 else if (!strncmp (line, "\\- ", 3))
973 linebuffer = xstrconcat (" &mdash; ", line+3, NULL);
974 else if (strchr (line, '\\')) /* We need to remove them later. */
975 linebuffer = xstrdup ("<br/>\n");
976
977 if (linebuffer)
978 {
979 /* Remove backslash escapes; */
980 for (src=dst=linebuffer; *src; src++)
981 {
982 if (*src == '\\' && src[1] == '\\')
983 *dst++ = '\\';
984 else if (*src == '\\')
985 ;
986 else
987 *dst++ = *src;
988 }
989 *dst = 0;
990 line = linebuffer;
991 }
992 }
993 else if (htmlmode && !*line)
994 line = linebuffer = xstrdup ("@para{}");
995 else if (htmlmode)
996 {
997 size_t n0, n1;
998
999 for (s=line, n0=n1=0; *s; s++, n0++)
1000 if (*s == '<' || *s == '>')
1001 n1 += 3;
1002 else if (*s == '&')
1003 n1 += 4;
1004 if (n1)
1005 {
1006 dst = linebuffer = xmalloc (n0 + n1 + 1);
1007 for (s=line; *s; s++)
1008 if (*s == '<')
1009 strcpy (dst, "&lt;"), dst += 4;
1010 else if (*s == '>')
1011 strcpy (dst, "&gt;"), dst += 4;
1012 else if (*s == '&')
1013 strcpy (dst, "&amp;"), dst += 5;
1014 else
1015 *dst++ = *s;
1016 *dst = 0;
1017 line = linebuffer;
1018 }
1019 }
1020
1021 sect = get_section_buffer (sectname);
1022 if (sect->last_line && !sect->last_line->verbatim == !verbatim)
1023 {
1024 /* Lets append that line to the last one. We do this to keep
1025 all lines of the same kind (i.e.verbatim or not) together in
1026 one large buffer. */
1027 size_t n1, n;
1028
1029 lb = sect->last_line;
1030 n1 = strlen (lb->line);
1031 n = n1 + 1 + strlen (line) + 1;
1032 lb->line = xrealloc (lb->line, n);
1033 strcpy (lb->line+n1, "\n");
1034 strcpy (lb->line+n1+1, line);
1035 }
1036 else
1037 {
1038 lb = xcalloc (1, sizeof *lb);
1039 lb->verbatim = verbatim;
1040 if (htmlmode && sect->lines_tail == &sect->lines)
1041 lb->line = xstrconcat ("@para{}", line, NULL);
1042 else
1043 lb->line = xstrdup (line);
1044 sect->last_line = lb;
1045 *sect->lines_tail = lb;
1046 sect->lines_tail = &lb->next;
1047 }
1048
1049 free (linebuffer);
1050 }
1051
1052
1053 /* Prepare for a new man page using the filename NAME. */
1054 static void
1055 start_page (char *name)
1056 {
1057 if (verbose)
1058 inf ("starting page '%s'", name);
1059 assert (!thepage.name);
1060 thepage.name = xstrdup (name);
1061 thepage.n_sections = 0;
1062 }
1063
1064
1065 /* Write a character to FP. */
1066 static void
1067 writechr (int c, FILE *fp)
1068 {
1069 putc (c, fp);
1070 }
1071
1072
1073 /* Write depending on HTMLMODE either ROFF or HTML to FP. */
1074 static void
1075 writestr (const char *roff, const char *html, FILE *fp)
1076 {
1077 const char *s = htmlmode? html : roff;
1078
1079 if (s)
1080 fputs (s, fp);
1081 }
1082
1083
1084 /* Write the .TH entry of the current page. Return -1 if there is a
1085 problem with the page. */
1086 static int
1087 write_th (FILE *fp)
1088 {
1089 char *name, *p;
1090
1091 writestr (".\\\" Created from Texinfo source by yat2m " VERSION "\n",
1092 "<!-- Created from Texinfo source by yat2m " VERSION " -->\n",
1093 fp);
1094
1095 name = ascii_strupr (xstrdup (thepage.name));
1096 p = strrchr (name, '.');
1097 if (!p || !p[1])
1098 {
1099 err ("no section name in man page '%s'", thepage.name);
1100 free (name);
1101 return -1;
1102 }
1103 *p++ = 0;
1104
1105 if (htmlmode)
1106 {
1107 if (gnupgorgmode)
1108 {
1109 fputs
1110 ("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
1111 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n"
1112 " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n"
1113 "<html xmlns=\"http://www.w3.org/1999/xhtml\""
1114 " lang=\"en\" xml:lang=\"en\">\n", fp);
1115 }
1116 else
1117 fputs ("<html>\n", fp);
1118 fputs ("<head>\n", fp);
1119 fprintf (fp, " <title>%s(%s)</title>\n", name, p);
1120 if (gnupgorgmode)
1121 {
1122 fputs ("<meta http-equiv=\"Content-Type\""
1123 " content=\"text/html;charset=utf-8\" />\n", fp);
1124 fputs ("<meta name=\"viewport\""
1125 " content=\"width=device-width, initial-scale=1\" />\n"
1126 "<link rel=\"stylesheet\" href=\"/share/site.css\""
1127 " type=\"text/css\" />\n", fp);
1128 }
1129 else
1130 fputs (default_css, fp);
1131 fputs ("</head>\n"
1132 "<body>\n", fp);
1133 if (gnupgorgmode)
1134 fputs ("<div id=\"wrapper\">\n"
1135 "<div id=\"content\">\n", fp);
1136 fputs ("<div class=\"y2m\">\n", fp);
1137 }
1138
1139 /* This roff source
1140 * .TH GPG 1 2016-12-20 "GnuPG 2.1.17" "GNU Privacy Guard 2.1"
1141 * is rendered by man like this:
1142 * GPG(1) GNU Privacy Guard 2.1 GPG(1)
1143 * [...]
1144 * GnuPG 2.1.17 2016-12-20 GPG(1)
1145 */
1146 if (htmlmode)
1147 {
1148 fprintf (fp, "<p class=\"y2m y2m-top\">"
1149 "<span class=\"y2m-left\">%s(%s)</span> "
1150 "<span class=\"y2m-center\">%s</span> "
1151 "<span class=\"y2m-right\">%s(%s)</span>"
1152 "</p>\n",
1153 name, p, opt_source, name, p);
1154 /* Note that the HTML footer is written by write_bottom(). */
1155
1156 }
1157 else
1158 fprintf (fp, ".TH %s %s %s \"%s\" \"%s\"\n",
1159 name, p, isodatestring (), opt_release, opt_source);
1160
1161 free (name);
1162 return 0;
1163 }
1164
1165
1166 /* In HTML mode we need to render a footer. */
1167 static int
1168 write_bottom (FILE *fp)
1169 {
1170 char *name, *p;
1171
1172 if (!htmlmode)
1173 return 0;
1174
1175 name = ascii_strupr (xstrdup (thepage.name));
1176 p = strrchr (name, '.');
1177 if (!p || !p[1])
1178 {
1179 err ("no section name in man page '%s'", thepage.name);
1180 free (name);
1181 return -1;
1182 }
1183 *p++ = 0;
1184
1185 /* This roff source
1186 * .TH GPG 1 2016-12-20 "GnuPG 2.1.17" "GNU Privacy Guard 2.1"
1187 * is rendered by man to this footer:
1188 * GnuPG 2.1.17 2016-12-20 GPG(1)
1189 */
1190 fprintf (fp, "<p class=\"y2m y2m-footer\">"
1191 "<span class=\"y2m-left\">%s</span> "
1192 "<span class=\"y2m-center\">%s</span> "
1193 "<span class=\"y2m-right\">%s(%s)</span>"
1194 "</p>\n",
1195 opt_release, isodatestring (), name, p);
1196 fputs ("</div><!-- class y2m -->\n", fp);
1197 if (gnupgorgmode)
1198 fputs ("</div><!-- end content -->\n"
1199 "</div><!-- end wrapper -->\n", fp);
1200 fputs ("</body>\n"
1201 "</html>\n", fp);
1202
1203 free (name);
1204 return 0;
1205 }
1206
1207
1208 /* Write the .SH header. With NULL passed for NAME just close a
1209 * section in html mode if there is an open section. */
1210 static void
1211 write_sh (FILE *fp, const char *name)
1212 {
1213 static int in_section;
1214
1215 if (htmlmode && in_section)
1216 fprintf (fp, "</div>\n");
1217 in_section = 0;
1218
1219 if (name)
1220 {
1221 if (htmlmode)
1222 fprintf (fp,
1223 "<div class=\"y2m-section\">\n"
1224 "<h2 class=\"y2m-sh\">%s</h2>\n", name);
1225 else
1226 fprintf (fp, ".SH %s\n", name);
1227 in_section = 1;
1228 }
1229 }
1230
1231 /* Render a @item line to HTML. (LINE,LEN) gives the arguments of
1232 * @item. Use NULL for LINE to close a possible open <li>. ITEMX
1233 * flags a @itemx line. */
1234 static void
1235 write_html_item (FILE *fp, const char *line, size_t len, int itemx)
1236 {
1237 static int in_li;
1238 const char *rest;
1239 size_t n, n0;
1240 int eol_action = 0;
1241 int table_level = 0;
1242
1243 if (!itemx && in_li)
1244 {
1245 fprintf (fp, "</li>\n");
1246 in_li = 0;
1247 }
1248
1249 if (line)
1250 {
1251 /* Trim the LF and skip leading spaces. */
1252 if (len && line[len-1] == '\n')
1253 len--;
1254 for (; len && (*line == ' ' || *line == '\t'); len--, line++)
1255 ;
1256 if (len)
1257 {
1258 rest = line;
1259 for (n=0; n < len && !(*rest == ' ' || *rest == '\t'); n++, rest++)
1260 ;
1261 n0 = n;
1262 for (; n < len && (*rest == ' ' || *rest == '\t'); n++, rest++)
1263 ;
1264 len -= n;
1265 /* Now the first word is (LINE,N0) and the args are (REST,LEN) */
1266 fprintf (fp, "%s<span class=\"y2m-item\">%.*s",
1267 itemx? " ":"<li>", (int)n0, line);
1268 if (len)
1269 {
1270 fputs (" <span class=\"y2m-args\">", fp);
1271 proc_texi_buffer (fp, rest, len, &table_level, &eol_action,
1272 NULL, 0);
1273 fputs ("</span>", fp);
1274 }
1275 fputs ("</span>\n", fp);
1276 in_li = 1;
1277 }
1278 }
1279 }
1280
1281
1282 /* Process the texinfo command COMMAND (without the leading @) and
1283 write output if needed to FP. REST is the remainder of the line
1284 which should either point to an opening brace or to a white space.
1285 The function returns the number of characters already processed
1286 from REST. LEN is the usable length of REST. TABLE_LEVEL is used
1287 to control the indentation of tables. SECT has info about the
1288 current section or is NULL. */
1289 static size_t
1290 proc_texi_cmd (FILE *fp, const char *command, const char *rest, size_t len,
1291 int *table_level, int *eol_action, section_buffer_t sect)
1292 {
1293 static struct {
1294 const char *name; /* Name of the command. */
1295 int what; /* What to do with this command. */
1296 int enable_2D_minus; /* Interpret '-' as minus. */
1297 const char *lead_in; /* String to print with a opening brace. */
1298 const char *lead_out;/* String to print with the closing brace. */
1299 const char *html_in; /* Same as LEAD_IN but for HTML. */
1300 const char *html_out;/* Same as LEAD_OUT but for HTML. */
1301 } cmdtbl[] = {
1302 { "command", 9, 1, "\\fB", "\\fP", "<i>", "</i>" },
1303 { "code", 0, 1, "\\fB", "\\fP", "<samp>", "</samp>" },
1304 { "url", 0, 1, "\\fB", "\\fP", "<strong>", "</strong>" },
1305 { "sc", 0, 0, "\\fB", "\\fP", "<span class=\"y2m-sc\">", "</span>" },
1306 { "var", 0, 0, "\\fI", "\\fP", "<u>", "</u>" },
1307 { "samp", 0, 1, "\\(oq", "\\(cq" },
1308 { "kbd", 0, 1, "\\(oq", "\\(cq" },
1309 { "file", 0, 1, "\\(oq\\fI","\\fP\\(cq" },
1310 { "env", 0, 1, "\\(oq\\fI","\\fP\\(cq" },
1311 { "acronym", 0, 0 },
1312 { "dfn", 0, 0 },
1313 { "option", 0, 1, "\\fB", "\\fP", "<samp>", "</samp>" },
1314 { "example", 10, 1, ".RS 2\n.nf\n", NULL, "\n<pre>\n", "\n</pre>\n" },
1315 { "smallexample", 10, 1, ".RS 2\n.nf\n", NULL, "\n<pre>\n", "\n</pre>\n" },
1316 { "asis", 7 },
1317 { "anchor", 7 },
1318 { "cartouche", 1 },
1319 { "ref", 0, 0, "[", "]" },
1320 { "xref", 0, 0, "See: [", "]" },
1321 { "pxref", 0, 0, "see: [", "]" },
1322 { "uref", 0, 0, "(\\fB", "\\fP)" },
1323 { "footnote",0, 0, " ([", "])" },
1324 { "emph", 0, 0, "\\fI", "\\fP", "<em>", "</em>" },
1325 { "w", 1 },
1326 { "c", 5 },
1327 { "efindex", 1 },
1328 { "opindex", 1 },
1329 { "cpindex", 1 },
1330 { "cindex", 1 },
1331 { "noindent", 0 },
1332 { "para", 11, },
1333 { "section", 1 },
1334 { "chapter", 1 },
1335 { "subsection", 6, 0, "\n.SS ", NULL, "<h3>" },
1336 { "chapheading", 0},
1337 { "item", 2, 0, ".TP\n.B " },
1338 { "itemx", 2, 0, ".TQ\n.B " },
1339 { "table", 3 },
1340 { "itemize", 3 },
1341 { "bullet", 0, 0, "* " },
1342 { "*", 0, 0, "\n.br"},
1343 { "/", 0 },
1344 { "end", 4 },
1345 { "quotation", 1, 0, ".RS\n\\fB" },
1346 { "value", 8 },
1347 { "dots", 0, 0, "...", NULL, "&hellip;" },
1348 { "minus", 0, 0, "\\-", NULL, "&minus;" },
1349 /* From here, macro for table */
1350 { "gcctabopt", 0, 1 },
1351 { "gnupgtabopt", 0, 1 },
1352 { NULL }
1353 };
1354 size_t n;
1355 int i;
1356 const char *s;
1357 const char *lead_out = NULL;
1358 const char *html_out = NULL;
1359 int ignore_args = 0;
1360 int see_also_command = 0;
1361 int enable_2D_minus = 0;
1362
1363 for (i=0; cmdtbl[i].name && strcmp (cmdtbl[i].name, command); i++)
1364 ;
1365 if (cmdtbl[i].name)
1366 {
1367 writestr (cmdtbl[i].lead_in, cmdtbl[i].html_in, fp);
1368 lead_out = cmdtbl[i].lead_out;
1369 html_out = cmdtbl[i].html_out;
1370 enable_2D_minus = cmdtbl[i].enable_2D_minus;
1371 switch (cmdtbl[i].what)
1372 {
1373 case 10:
1374 sect->in_pre = 1;
1375 cond_parse_dash = 0;
1376 cond_2D_as_minus = 1;
1377 /* Fallthrough */
1378 case 1: /* Throw away the entire line. */
1379 s = memchr (rest, '\n', len);
1380 return s? (s-rest)+1 : len;
1381 case 2: /* Handle @item. */
1382 cond_parse_dash = -1;
1383 if (htmlmode)
1384 {
1385 s = memchr (rest, '\n', len);
1386 n = s? (s-rest)+1 : len;
1387 write_html_item (fp, rest, n, !strcmp(cmdtbl[i].name, "itemx"));
1388 return n;
1389 }
1390 else
1391 {
1392 s = memchr (rest, '\n', len);
1393 if (s)
1394 {
1395 n = (s-rest)+1;
1396 proc_texi_buffer (fp, rest, n, table_level, eol_action,
1397 sect, table_item_stack[*table_level]);
1398 return n;
1399 }
1400 }
1401 break;
1402 case 3: /* Handle table. */
1403 ++*table_level;
1404 if (*table_level >= MAX_TABLE_NESTING)
1405 die ("too many nesting level of table\n");
1406 if (*table_level > (htmlmode? 0 : 1))
1407 {
1408 if (htmlmode)
1409 write_html_item (fp, NULL, 0, 0);
1410 writestr (".RS\n", "<ul>\n", fp);
1411 }
1412
1413 s = memchr (rest, '\n', len);
1414 if (s)
1415 {
1416 size_t n0 = n = (s-rest)+1;
1417
1418 for (s=rest; n && (*s == ' ' || *s == '\t'); s++, n--)
1419 ;
1420 if (n && *s == '@')
1421 {
1422 char argbuf[256];
1423 int argidx;
1424
1425 s++;
1426 n--;
1427 for (argidx = 0; argidx < n; argidx++)
1428 if (s[argidx] == ' ' || s[argidx] == '\t'
1429 || s[argidx] == '\n')
1430 break;
1431 else
1432 argbuf[argidx] = s[argidx];
1433 argbuf[argidx] = 0;
1434 for (i=0; cmdtbl[i].name; i++)
1435 if (!strcmp (cmdtbl[i].name, argbuf))
1436 break;
1437 if (cmdtbl[i].name && cmdtbl[i].enable_2D_minus)
1438 table_item_stack[*table_level] = 1;
1439 else
1440 table_item_stack[*table_level] = 0;
1441 return n0;
1442 }
1443 else
1444 return len;
1445 }
1446 else
1447 /* Now throw away the entire line. */
1448 return len;
1449 break;
1450 case 4: /* Handle end. */
1451 for (s=rest, n=len; n && (*s == ' ' || *s == '\t'); s++, n--)
1452 ;
1453 if (n >= 5 && !memcmp (s, "table", 5)
1454 && (!n || s[5] == ' ' || s[5] == '\t' || s[5] == '\n'))
1455 {
1456 if (htmlmode)
1457 write_html_item (fp, NULL, 0, 0);
1458 if ((*table_level)-- > 1)
1459 writestr (".RE\n", "</ul>\n", fp);
1460 else
1461 writestr (".P\n", "</ul>\n", fp);
1462 }
1463 else if (n >= 7 && !memcmp (s, "example", 7)
1464 && (!n || s[7] == ' ' || s[7] == '\t' || s[7] == '\n'))
1465 {
1466 cond_parse_dash = 1;
1467 cond_2D_as_minus = 0;
1468 writestr (".fi\n.RE\n", "</pre>\n", fp);
1469 sect->in_pre = 0;
1470 }
1471 else if (n >= 12 && !memcmp (s, "smallexample", 12)
1472 && (!n || s[12] == ' ' || s[12] == '\t' || s[12] == '\n'))
1473 {
1474 cond_parse_dash = 1;
1475 cond_2D_as_minus = 0;
1476 writestr (".fi\n.RE\n", "</pre>\n", fp);
1477 sect->in_pre = 0;
1478 }
1479 else if (n >= 9 && !memcmp (s, "quotation", 9)
1480 && (!n || s[9] == ' ' || s[9] == '\t' || s[9] == '\n'))
1481 {
1482 writestr ("\\fR\n.RE\n", "xx", fp);
1483 }
1484 /* Now throw away the entire line. */
1485 s = memchr (rest, '\n', len);
1486 return s? (s-rest)+1 : len;
1487 case 5: /* Handle special comments. */
1488 for (s=rest, n=len; n && (*s == ' ' || *s == '\t'); s++, n--)
1489 ;
1490 if (n >= 4 && !memcmp (s, "man:", 4))
1491 {
1492 s += 4;
1493 n -= 4;
1494 if (htmlmode)
1495 {
1496 if (!strncmp (s, ".RE\n", 4)
1497 || !strncmp (s, ".RS\n", 4))
1498 ;
1499 else
1500 inf ("unknown special comment \"man:\"");
1501 }
1502 else
1503 {
1504 for (; n && *s != '\n'; n--, s++)
1505 writechr (*s, fp);
1506 writechr ('\n', fp);
1507 }
1508 }
1509 /* Now throw away the entire line. */
1510 s = memchr (rest, '\n', len);
1511 return s? (s-rest)+1 : len;
1512 case 6:
1513 *eol_action = EOL_CLOSE_SUBSECTION;
1514 break;
1515 case 7:
1516 ignore_args = 1;
1517 break;
1518 case 8:
1519 ignore_args = 1;
1520 if (*rest != '{')
1521 {
1522 err ("opening brace for command '%s' missing", command);
1523 return len;
1524 }
1525 else
1526 {
1527 /* Find closing brace. */
1528 for (s=rest+1, n=1; *s && n < len; s++, n++)
1529 if (*s == '}')
1530 break;
1531 if (*s != '}')
1532 {
1533 err ("closing brace for command '%s' not found", command);
1534 return len;
1535 }
1536 else
1537 {
1538 size_t rlen = s - (rest + 1);
1539 macro_t m;
1540
1541 for (m = variablelist; m; m = m->next)
1542 {
1543 if (strlen (m->name) == rlen
1544 && !strncmp (m->name, rest+1, rlen))
1545 break;
1546 }
1547 if (m)
1548 writestr (m->value, m->value, fp);
1549 else
1550 inf ("texinfo variable '%.*s' is not set",
1551 (int)rlen, rest+1);
1552 }
1553 }
1554 break;
1555 case 9: /* @command{} */
1556 if (sect && sect->is_see_also)
1557 see_also_command = 1;
1558 break;
1559 case 11: /* @para{} inserted by htmlmode */
1560 if (!*table_level && !sect->in_pre)
1561 {
1562 if (sect->in_para)
1563 writestr (NULL, "</p>\n", fp);
1564 writestr (NULL, "\n<p>", fp);
1565 sect->in_para = 1;
1566 }
1567 break;
1568
1569 default:
1570 break;
1571 }
1572 }
1573 else /* macro */
1574 {
1575 macro_t m;
1576
1577 for (m = macrolist; m ; m = m->next)
1578 if (!strcmp (m->name, command))
1579 break;
1580 if (m)
1581 {
1582 proc_texi_buffer (fp, m->value, strlen (m->value),
1583 table_level, eol_action, NULL, 0);
1584 ignore_args = 1; /* Parameterized macros are not yet supported. */
1585 }
1586 else
1587 inf ("texinfo command '%s' not supported (%.*s)", command,
1588 (int)((s = memchr (rest, '\n', len)), (s? (s-rest) : len)), rest);
1589 }
1590
1591 if (*rest == '{')
1592 {
1593 /* Find matching closing brace. */
1594 for (s=rest+1, n=1, i=1; i && *s && n < len; s++, n++)
1595 if (*s == '{')
1596 i++;
1597 else if (*s == '}')
1598 i--;
1599 if (i)
1600 {
1601 err ("closing brace for command '%s' not found", command);
1602 return len;
1603 }
1604
1605
1606 if (n > 2 && len && !ignore_args)
1607 {
1608 char *workstr; /* Let's work on a copy. */
1609 char *p;
1610 macro_t m;
1611 const char *cmdname;
1612
1613 n -= 2;
1614
1615 workstr = xmalloc (len);
1616 memcpy (workstr, rest+1, len-1);
1617 workstr[len-1] = 0;
1618 if (see_also_command && htmlmode
1619 && workstr[n] == '}' && workstr[n+1] == '('
1620 && digitp (workstr+n+2)
1621 && (p = strchr (workstr + n + 2, ')')))
1622 {
1623 /* Seems to be a command with section number. P points
1624 * to the closing parentheses. Render as link. */
1625
1626 workstr[n] = 0;
1627 *p = 0;
1628 cmdname = workstr;
1629 if (*workstr == '@' && workstr[1])
1630 {
1631 for (m = macrolist; m ; m = m->next)
1632 if (!strcmp (m->name, workstr+1))
1633 break;
1634 if (m)
1635 cmdname = m->value;
1636 }
1637
1638
1639 fprintf (fp, "<a href=\"%s.%s.html\">%s</a>(%s)",
1640 cmdname, workstr + n + 2,
1641 cmdname, workstr + n + 2);
1642
1643 n = p + 2 - workstr;
1644 }
1645 else
1646 {
1647 proc_texi_buffer (fp, workstr, n, table_level, eol_action,
1648 NULL, enable_2D_minus);
1649 n += 2;
1650 }
1651 free (workstr);
1652 }
1653 }
1654 else
1655 n = 0;
1656
1657 writestr (lead_out, html_out, fp);
1658
1659 return n;
1660 }
1661
1662
1663
1664 /* Process the string LINE with LEN bytes of Texinfo content. SECT
1665 * has infos about the current secion or is NULL. */
1666 static void
1667 proc_texi_buffer (FILE *fp, const char *line, size_t len,
1668 int *table_level, int *eol_action, section_buffer_t sect,
1669 int char_2D_is_minus)
1670 {
1671 const char *s;
1672 char cmdbuf[256];
1673 int cmdidx = 0;
1674 int in_cmd = 0;
1675 size_t n;
1676
1677 for (s=line; *s && len; s++, len--)
1678 {
1679 if (in_cmd)
1680 {
1681 if (in_cmd == 1)
1682 {
1683 switch (*s)
1684 {
1685 case '@': case '{': case '}':
1686 writechr (*s, fp); in_cmd = 0;
1687 break;
1688 case ':': /* Not ending a sentence flag. */
1689 in_cmd = 0;
1690 break;
1691 case '.': case '!': case '?': /* Ending a sentence. */
1692 writechr (*s, fp); in_cmd = 0;
1693 break;
1694 case ' ': case '\t': case '\n': /* Non collapsing spaces. */
1695 writechr (*s, fp); in_cmd = 0;
1696 break;
1697 default:
1698 cmdidx = 0;
1699 cmdbuf[cmdidx++] = *s;
1700 in_cmd++;
1701 break;
1702 }
1703 }
1704 else if (*s == '{' || *s == ' ' || *s == '\t' || *s == '\n')
1705 {
1706 cmdbuf[cmdidx] = 0;
1707 n = proc_texi_cmd (fp, cmdbuf, s, len, table_level, eol_action,
1708 sect);
1709 assert (n <= len);
1710 s += n; len -= n;
1711 s--; len++;
1712 in_cmd = 0;
1713 }
1714 else if (cmdidx < sizeof cmdbuf -1)
1715 cmdbuf[cmdidx++] = *s;
1716 else
1717 {
1718 err ("texinfo command too long - ignored");
1719 in_cmd = 0;
1720 }
1721 }
1722 else if (*s == '@')
1723 in_cmd = 1;
1724 else if (*s == '\n')
1725 {
1726 switch (*eol_action)
1727 {
1728 case EOL_CLOSE_SUBSECTION:
1729 writestr ("\n\\ \n", "</h3>\n", fp);
1730 break;
1731 default:
1732 writechr (*s, fp);
1733 break;
1734 }
1735 *eol_action = 0;
1736 if (cond_parse_dash == -1)
1737 cond_parse_dash = 0;
1738 }
1739 else if (*s == '\\')
1740 writestr ("\\[rs]", "&bsol;", fp);
1741 else if (cond_parse_dash == 1 && sect && *s == '-')
1742 /* Handle -- and --- when it's _not_ in an argument. */
1743 {
1744 if (len < 2 || s[1] != '-')
1745 writechr (*s, fp);
1746 else if (len < 3 || s[2] != '-')
1747 {
1748 writestr ("\\[en]", "&ndash;", fp);
1749 len--;
1750 s++;
1751 }
1752 else
1753 {
1754 writestr ("\\[em]", "&mdash;", fp);
1755 len -= 2;
1756 s += 2;
1757 }
1758 }
1759 else if (*s == '-' && (cond_2D_as_minus || char_2D_is_minus))
1760 {
1761 writestr ("\\-", "-", fp);
1762 }
1763 else
1764 writechr (*s, fp);
1765 }
1766
1767 if (in_cmd > 1)
1768 {
1769 cmdbuf[cmdidx] = 0;
1770 n = proc_texi_cmd (fp, cmdbuf, s, len, table_level, eol_action, sect);
1771 assert (n <= len);
1772 s += n; len -= n;
1773 s--; len++;
1774 /* in_cmd = 0; -- doc only */
1775 }
1776 }
1777
1778
1779 /* Do something with the Texinfo line LINE. If SECT is not NULL is
1780 * has information about the current section. */
1781 static void
1782 parse_texi_line (FILE *fp, const char *line, int *table_level,
1783 section_buffer_t sect)
1784 {
1785 int eol_action = 0;
1786
1787 /* A quick test whether there are any texinfo commands. */
1788 if (!strchr (line, '@'))
1789 {
1790 /* FIXME: In html mode escape HTML stuff. */
1791 if (htmlmode && *line)
1792 fputs ("<p>", fp);
1793 writestr (line, line, fp);
1794 if (htmlmode && *line)
1795 fputs ("</p>", fp);
1796 writechr ('\n', fp);
1797 return;
1798 }
1799 proc_texi_buffer (fp, line, strlen (line), table_level, &eol_action,
1800 sect, 0);
1801 writechr ('\n', fp);
1802 }
1803
1804
1805 /* Write all the lines LINES to FP. */
1806 static void
1807 write_content (FILE *fp, section_buffer_t sect)
1808 {
1809 line_buffer_t line;
1810 int table_level = 0;
1811 line_buffer_t lines = sect->lines;
1812
1813 /* inf ("===BEGIN==============================================="); */
1814 /* for (line = lines; line; line = line->next) */
1815 /* inf ("line='%s'", line->line); */
1816 /* inf ("===END==============================================="); */
1817
1818
1819 for (line = lines; line; line = line->next)
1820 {
1821 if (line->verbatim)
1822 {
1823 /* FIXME: IN HTML mode we need to employ a parser for roff
1824 * markup. */
1825 writestr (line->line, line->line, fp);
1826 writechr ('\n', fp);
1827 }
1828 else
1829 {
1830 /* fputs ("TEXI---", fp); */
1831 /* fputs (line->line, fp); */
1832 /* fputs ("---\n", fp); */
1833 parse_texi_line (fp, line->line, &table_level, sect);
1834 }
1835 }
1836 }
1837
1838
1839
1840 static int
1841 is_standard_section (const char *name)
1842 {
1843 int i;
1844 const char *s;
1845
1846 for (i=0; (s=standard_sections[i]); i++)
1847 if (!strcmp (s, name))
1848 return 1;
1849 return 0;
1850 }
1851
1852
1853 /* Finish a page; that is sort the data and write it out to the file. */
1854 static void
1855 finish_page (void)
1856 {
1857 FILE *fp;
1858 section_buffer_t sect = NULL;
1859 int idx;
1860 const char *s;
1861 int i;
1862
1863 if (!thepage.name)
1864 return; /* No page active. */
1865
1866 if (verbose)
1867 inf ("finishing page '%s'", thepage.name);
1868
1869 if (opt_select)
1870 {
1871 if (!strcmp (opt_select, thepage.name))
1872 {
1873 inf ("selected '%s'", thepage.name );
1874 fp = stdout;
1875 }
1876 else
1877 {
1878 fp = fopen ( "/dev/null", "w" );
1879 if (!fp)
1880 die ("failed to open /dev/null: %s\n", strerror (errno));
1881 }
1882 }
1883 else if (opt_store)
1884 {
1885 char *fname = xstrconcat (thepage.name, htmlmode? ".html":NULL, NULL);
1886 if (verbose)
1887 inf ("writing '%s'", fname);
1888 fp = fopen (fname, "w" );
1889 if (!fp)
1890 die ("failed to create '%s': %s\n", fname, strerror (errno));
1891 free (fname);
1892 }
1893 else
1894 fp = stdout;
1895
1896 if (write_th (fp))
1897 goto leave;
1898
1899 for (idx=0; (s=standard_sections[idx]); idx++)
1900 {
1901 for (i=0; i < thepage.n_sections; i++)
1902 {
1903 sect = thepage.sections + i;
1904 if (sect->name && !strcmp (s, sect->name))
1905 break;
1906 }
1907 if (i == thepage.n_sections)
1908 sect = NULL;
1909
1910 if (sect)
1911 {
1912 write_sh (fp, sect->name);
1913 write_content (fp, sect);
1914 /* Now continue with all non standard sections directly
1915 following this one. */
1916 for (i++; i < thepage.n_sections; i++)
1917 {
1918 sect = thepage.sections + i;
1919 if (sect->name && is_standard_section (sect->name))
1920 break;
1921 if (sect->name)
1922 {
1923 write_sh (fp, sect->name);
1924 write_content (fp, sect);
1925 }
1926 }
1927
1928 }
1929 }
1930
1931 write_sh (fp, NULL);
1932 if (write_bottom (fp))
1933 goto leave;
1934
1935 leave:
1936 if (fp != stdout)
1937 fclose (fp);
1938 free (thepage.name);
1939 thepage.name = NULL;
1940
1941 for (i=0; i < thepage.n_sections; i++)
1942 {
1943 line_buffer_t line, line_next;
1944
1945 sect = thepage.sections + i;
1946 for (line = sect->lines; line; line = line_next)
1947 {
1948 line_next = line->next;
1949 free (line->line);
1950 free (line);
1951 }
1952
1953 free (sect->name);
1954 }
1955
1956 free (thepage.sections);
1957 thepage.n_sections = 0;
1958 }
1959
1960
1961
1962
1963 /* Parse one Texinfo file and create manpages according to the
1964 embedded instructions. */
1965 static void
1966 parse_file (const char *fname, FILE *fp, char **section_name, int in_pause)
1967 {
1968 char *line;
1969 int lnr = 0;
1970 /* Fixme: The following state variables don't carry over to include
1971 files. */
1972 int skip_to_end = 0; /* Used to skip over menu entries. */
1973 int skip_sect_line = 0; /* Skip after @mansect. */
1974 int item_indent = 0; /* How far is the current @item indented. */
1975
1976 /* Helper to define a macro. */
1977 char *macroname = NULL;
1978 char *macrovalue = NULL;
1979 size_t macrovaluesize = 0;
1980 size_t macrovalueused = 0;
1981
1982 line = xmalloc (LINESIZE);
1983 while (fgets (line, LINESIZE, fp))
1984 {
1985 size_t n = strlen (line);
1986 int got_line = 0;
1987 char *p, *pend;
1988
1989 lnr++;
1990 if (!n || line[n-1] != '\n')
1991 {
1992 err ("%s:%d: trailing linefeed missing, line too long or "
1993 "embedded Nul character", fname, lnr);
1994 break;
1995 }
1996 line[--n] = 0;
1997
1998 /* Kludge to allow indentation of tables. */
1999 for (p=line; *p == ' ' || *p == '\t'; p++)
2000 ;
2001 if (*p)
2002 {
2003 if (*p == '@' && !strncmp (p+1, "item", 4))
2004 item_indent = p - line; /* Set a new indent level. */
2005 else if (p - line < item_indent)
2006 item_indent = 0; /* Switch off indention. */
2007
2008 if (item_indent)
2009 {
2010 memmove (line, line+item_indent, n - item_indent + 1);
2011 n -= item_indent;
2012 }
2013 }
2014
2015
2016 if (*line == '@')
2017 {
2018 for (p=line+1, n=1; *p && *p != ' ' && *p != '\t'; p++)
2019 n++;
2020 while (*p == ' ' || *p == '\t')
2021 p++;
2022 }
2023 else
2024 p = line;
2025
2026 /* Take action on macro. */
2027 if (macroname)
2028 {
2029 if (n == 4 && !memcmp (line, "@end", 4)
2030 && (line[4]==' '||line[4]=='\t'||!line[4])
2031 && !strncmp (p, "macro", 5)
2032 && (p[5]==' '||p[5]=='\t'||!p[5]))
2033 {
2034 if (macrovalueused)
2035 macrovalue[--macrovalueused] = 0; /* Kill the last LF. */
2036 macrovalue[macrovalueused] = 0; /* Terminate macro. */
2037 macrovalue = xrealloc (macrovalue, macrovalueused+1);
2038
2039 set_macro (macroname, macrovalue);
2040 macrovalue = NULL;
2041 free (macroname);
2042 macroname = NULL;
2043 }
2044 else
2045 {
2046 if (macrovalueused + strlen (line) + 2 >= macrovaluesize)
2047 {
2048 macrovaluesize += strlen (line) + 256;
2049 macrovalue = xrealloc (macrovalue, macrovaluesize);
2050 }
2051 strcpy (macrovalue+macrovalueused, line);
2052 macrovalueused += strlen (line);
2053 macrovalue[macrovalueused++] = '\n';
2054 }
2055 continue;
2056 }
2057
2058
2059 if (n >= 5 && !memcmp (line, "@node", 5)
2060 && (line[5]==' '||line[5]=='\t'||!line[5]))
2061 {
2062 /* Completey ignore @node lines. */
2063 continue;
2064 }
2065
2066
2067 if (skip_sect_line)
2068 {
2069 skip_sect_line = 0;
2070 if (!strncmp (line, "@section", 8)
2071 || !strncmp (line, "@subsection", 11)
2072 || !strncmp (line, "@chapheading", 12))
2073 continue;
2074 }
2075
2076 /* We only parse lines we need and ignore the rest. There are a
2077 few macros used to control this as well as one @ifset
2078 command. Parts we know about are saved away into containers
2079 separate for each section. */
2080
2081 /* First process ifset/ifclear commands. */
2082 if (*line == '@')
2083 {
2084 if (n == 6 && !memcmp (line, "@ifset", 6)
2085 && (line[6]==' '||line[6]=='\t'))
2086 {
2087 for (p=line+7; *p == ' ' || *p == '\t'; p++)
2088 ;
2089 if (!*p)
2090 {
2091 err ("%s:%d: name missing after \"@ifset\"", fname, lnr);
2092 continue;
2093 }
2094 for (pend=p; *pend && *pend != ' ' && *pend != '\t'; pend++)
2095 ;
2096 *pend = 0; /* Ignore rest of the line. */
2097 push_condition (p, 1, fname, lnr);
2098 continue;
2099 }
2100 else if (n == 8 && !memcmp (line, "@ifclear", 8)
2101 && (line[8]==' '||line[8]=='\t'))
2102 {
2103 for (p=line+9; *p == ' ' || *p == '\t'; p++)
2104 ;
2105 if (!*p)
2106 {
2107 err ("%s:%d: name missing after \"@ifsclear\"", fname, lnr);
2108 continue;
2109 }
2110 for (pend=p; *pend && *pend != ' ' && *pend != '\t'; pend++)
2111 ;
2112 *pend = 0; /* Ignore rest of the line. */
2113 push_condition (p, 0, fname, lnr);
2114 continue;
2115 }
2116 else if (n == 4 && !memcmp (line, "@end", 4)
2117 && (line[4]==' '||line[4]=='\t')
2118 && !strncmp (p, "ifset", 5)
2119 && (p[5]==' '||p[5]=='\t'||!p[5]))
2120 {
2121 pop_condition (1, fname, lnr);
2122 continue;
2123 }
2124 else if (n == 4 && !memcmp (line, "@end", 4)
2125 && (line[4]==' '||line[4]=='\t')
2126 && !strncmp (p, "ifclear", 7)
2127 && (p[7]==' '||p[7]=='\t'||!p[7]))
2128 {
2129 pop_condition (0, fname, lnr);
2130 continue;
2131 }
2132 }
2133
2134 /* Take action on ifset/ifclear. */
2135 if (!cond_is_active)
2136 continue;
2137
2138 /* Process commands. */
2139 if (*line == '@')
2140 {
2141 if (skip_to_end
2142 && n == 4 && !memcmp (line, "@end", 4)
2143 && (line[4]==' '||line[4]=='\t'||!line[4]))
2144 {
2145 skip_to_end = 0;
2146 }
2147 else if (cond_in_verbatim)
2148 {
2149 got_line = 1;
2150 }
2151 else if (n == 6 && !memcmp (line, "@macro", 6))
2152 {
2153 macroname = xstrdup (p);
2154 macrovalue = xmalloc ((macrovaluesize = 1024));
2155 macrovalueused = 0;
2156 }
2157 else if (n == 4 && !memcmp (line, "@set", 4))
2158 {
2159 set_variable (p);
2160 }
2161 else if (n == 8 && !memcmp (line, "@manpage", 8))
2162 {
2163 free (*section_name);
2164 *section_name = NULL;
2165 finish_page ();
2166 start_page (p);
2167 in_pause = 0;
2168 }
2169 else if (n == 8 && !memcmp (line, "@mansect", 8))
2170 {
2171 if (!thepage.name)
2172 err ("%s:%d: section outside of a man page", fname, lnr);
2173 else
2174 {
2175 free (*section_name);
2176 *section_name = ascii_strupr (xstrdup (p));
2177 in_pause = 0;
2178 skip_sect_line = 1;
2179 }
2180 }
2181 else if (n == 9 && !memcmp (line, "@manpause", 9))
2182 {
2183 if (!*section_name)
2184 err ("%s:%d: pausing outside of a man section", fname, lnr);
2185 else if (in_pause)
2186 err ("%s:%d: already pausing", fname, lnr);
2187 else
2188 in_pause = 1;
2189 }
2190 else if (n == 8 && !memcmp (line, "@mancont", 8))
2191 {
2192 if (!*section_name)
2193 err ("%s:%d: continue outside of a man section", fname, lnr);
2194 else if (!in_pause)
2195 err ("%s:%d: continue while not pausing", fname, lnr);
2196 else
2197 in_pause = 0;
2198 }
2199 else if (n == 5 && !memcmp (line, "@menu", 5)
2200 && (line[5]==' '||line[5]=='\t'||!line[5]))
2201 {
2202 skip_to_end = 1;
2203 }
2204 else if (n == 8 && !memcmp (line, "@include", 8)
2205 && (line[8]==' '||line[8]=='\t'||!line[8]))
2206 {
2207 char *incname = xstrdup (p);
2208 FILE *incfp = fopen (incname, "r");
2209
2210 if (!incfp && opt_include && *opt_include && *p != '/')
2211 {
2212 free (incname);
2213 incname = xmalloc (strlen (opt_include) + 1
2214 + strlen (p) + 1);
2215 strcpy (incname, opt_include);
2216 if ( incname[strlen (incname)-1] != '/' )
2217 strcat (incname, "/");
2218 strcat (incname, p);
2219 incfp = fopen (incname, "r");
2220 }
2221
2222 if (!incfp)
2223 err ("can't open include file '%s': %s",
2224 incname, strerror (errno));
2225 else
2226 {
2227 parse_file (incname, incfp, section_name, in_pause);
2228 fclose (incfp);
2229 }
2230 free (incname);
2231 }
2232 else if (n == 4 && !memcmp (line, "@bye", 4)
2233 && (line[4]==' '||line[4]=='\t'||!line[4]))
2234 {
2235 break;
2236 }
2237 else if (!skip_to_end)
2238 got_line = 1;
2239 }
2240 else if (!skip_to_end)
2241 got_line = 1;
2242
2243 if (got_line && cond_in_verbatim)
2244 add_content (*section_name, line, 1);
2245 else if (got_line && thepage.name && *section_name && !in_pause)
2246 add_content (*section_name, line, 0);
2247 }
2248 if (ferror (fp))
2249 err ("%s:%d: read error: %s", fname, lnr, strerror (errno));
2250 free (macroname);
2251 free (macrovalue);
2252 free (line);
2253 }
2254
2255
2256 static void
2257 top_parse_file (const char *fname, FILE *fp)
2258 {
2259 char *section_name = NULL; /* Name of the current section or NULL
2260 if not in a section. */
2261 macro_t m;
2262
2263 while (macrolist)
2264 {
2265 macro_t next = macrolist->next;
2266 free (macrolist->value);
2267 free (macrolist);
2268 macrolist = next;
2269 }
2270 while (variablelist)
2271 {
2272 macro_t next = variablelist->next;
2273 free (variablelist->value);
2274 free (variablelist);
2275 variablelist = next;
2276 }
2277 for (m=predefinedmacrolist; m; m = m->next)
2278 set_macro (m->name, xstrdup ("1"));
2279 cond_is_active = 1;
2280 cond_in_verbatim = 0;
2281 cond_parse_dash = 1;
2282
2283 parse_file (fname, fp, &section_name, 0);
2284 free (section_name);
2285 finish_page ();
2286 }
2287
2288
2289 int
2290 main (int argc, char **argv)
2291 {
2292 int last_argc = -1;
2293 const char *s;
2294
2295 opt_source = "GNU";
2296 opt_release = "";
2297
2298 /* Define default macros. The trick is that these macros are not
2299 defined when using the actual texinfo renderer. */
2300 add_predefined_macro ("isman");
2301 add_predefined_macro ("manverb");
2302
2303 /* Option parsing. */
2304 if (argc)
2305 {
2306 argc--; argv++;
2307 }
2308 while (argc && last_argc != argc )
2309 {
2310 last_argc = argc;
2311 if (!strcmp (*argv, "--"))
2312 {
2313 argc--; argv++;
2314 break;
2315 }
2316 else if (!strcmp (*argv, "--help"))
2317 {
2318 puts (
2319 "Usage: " PGM " [OPTION] [FILE]\n"
2320 "Extract man pages from a Texinfo source.\n\n"
2321 " --html render output as HTML\n"
2322 " --source NAME use NAME as source field\n"
2323 " --release STRING use STRING as the release field\n"
2324 " --date EPOCH use EPOCH as publication date\n"
2325 " --store write output using @manpage name\n"
2326 " --select NAME only output pages with @manpage NAME\n"
2327 " --gnupgorg prepare for use at www.gnupg.org\n"
2328 " --verbose enable extra informational output\n"
2329 " --debug enable additional debug output\n"
2330 " --help display this help and exit\n"
2331 " -I DIR also search in include DIR\n"
2332 " -D MACRO define MACRO to 1\n\n"
2333 "With no FILE, or when FILE is -, read standard input.\n\n"
2334 "Report bugs to <https://bugs.gnupg.org>.");
2335 exit (0);
2336 }
2337 else if (!strcmp (*argv, "--version"))
2338 {
2339 puts (PGM " " VERSION "\n"
2340 "Copyright (C) 2005, 2017 g10 Code GmbH\n"
2341 "This program comes with ABSOLUTELY NO WARRANTY.\n"
2342 "This is free software, and you are welcome to redistribute it\n"
2343 "under certain conditions. See the file COPYING for details.");
2344 exit (0);
2345 }
2346 else if (!strcmp (*argv, "--html"))
2347 {
2348 htmlmode = 1;
2349 argc--; argv++;
2350 }
2351 else if (!strcmp (*argv, "--gnupgorg"))
2352 {
2353 gnupgorgmode = 1;
2354 argc--; argv++;
2355 }
2356 else if (!strcmp (*argv, "--verbose"))
2357 {
2358 verbose = 1;
2359 argc--; argv++;
2360 }
2361 else if (!strcmp (*argv, "--quiet"))
2362 {
2363 quiet = 1;
2364 argc--; argv++;
2365 }
2366 else if (!strcmp (*argv, "--debug"))
2367 {
2368 verbose = debug = 1;
2369 argc--; argv++;
2370 }
2371 else if (!strcmp (*argv, "--source"))
2372 {
2373 argc--; argv++;
2374 if (argc)
2375 {
2376 opt_source = *argv;
2377 argc--; argv++;
2378 }
2379 }
2380 else if (!strcmp (*argv, "--release"))
2381 {
2382 argc--; argv++;
2383 if (argc)
2384 {
2385 opt_release = *argv;
2386 argc--; argv++;
2387 }
2388 }
2389 else if (!strcmp (*argv, "--date"))
2390 {
2391 argc--; argv++;
2392 if (argc)
2393 {
2394 opt_date = *argv;
2395 argc--; argv++;
2396 }
2397 }
2398 else if (!strcmp (*argv, "--store"))
2399 {
2400 opt_store = 1;
2401 argc--; argv++;
2402 }
2403 else if (!strcmp (*argv, "--select"))
2404 {
2405 argc--; argv++;
2406 if (argc)
2407 {
2408 opt_select = strrchr (*argv, '/');
2409 if (opt_select)
2410 opt_select++;
2411 else
2412 opt_select = *argv;
2413 argc--; argv++;
2414 }
2415 }
2416 else if (!strcmp (*argv, "-I"))
2417 {
2418 argc--; argv++;
2419 if (argc)
2420 {
2421 opt_include = *argv;
2422 argc--; argv++;
2423 }
2424 }
2425 else if (!strcmp (*argv, "-D"))
2426 {
2427 argc--; argv++;
2428 if (argc)
2429 {
2430 add_predefined_macro (*argv);
2431 argc--; argv++;
2432 }
2433 }
2434 }
2435
2436 if (argc > 1)
2437 die ("usage: " PGM " [OPTION] [FILE] (try --help for more information)\n");
2438
2439 /* Take care of supplied timestamp for reproducible builds. See
2440 * https://reproducible-builds.org/specs/source-date-epoch/ */
2441 if (!opt_date && (s = getenv ("SOURCE_DATE_EPOCH")) && *s)
2442 opt_date = s;
2443
2444 /* Start processing. */
2445 if (argc && strcmp (*argv, "-"))
2446 {
2447 FILE *fp = fopen (*argv, "rb");
2448 if (!fp)
2449 die ("%s:0: can't open file: %s", *argv, strerror (errno));
2450 top_parse_file (*argv, fp);
2451 fclose (fp);
2452 }
2453 else
2454 top_parse_file ("-", stdin);
2455
2456 return !!any_error;
2457 }
2458
2459
2460 /*
2461 Local Variables:
2462 compile-command: "gcc -Wall -g -o yat2m yat2m.c"
2463 End:
2464 */