📄 texinfo2man.c
字号:
#include <stdio.h>#include <malloc.h>#include <string.h>#include <ctype.h>/* texinfo2man. * Convert a texinfo document to man format. * Author: Carlo Wood <carlo@alinoe.com>. * Copyright (c) 1999, 2000 Carlo Wood. All rights reserved. * * Redistribution and use in source and binary forms are permitted * provided that the above copyright notice and this paragraph are * duplicated in all such forms and that any documentation, advertising * materials, and other materials related to such distribution and use * acknowledge that the software was developed by Carlo Wood. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES * OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. * * HISTORY * 2002 Jan 17 Carlo Wood. Fixed handling of @@ within an @email{}. */ int at_start_of_line = 1;int at_start_of_line_for_dot = 1;int to_upper = 0;int ignore = 0;int disabled = 0;int in_section = 0;int in_item = 0;int in_table = 0;int no_break = 0;int indentation = 0;int bold = 0;int underlined = 0;char *texinfoname;char *where (int line_no){ static char b[256]; sprintf (b, "%s line %-3d", texinfoname, line_no); return b;}voidstart_bold (void){ if (!bold && !underlined) { if (at_start_of_line) { printf (".B "); at_start_of_line_for_dot = 0; } else { printf ("\\fB"); at_start_of_line_for_dot = at_start_of_line; } at_start_of_line = 0; } ++bold;}voidstop_bold (void){ if (--bold == 0 && !underlined) { printf ("\\fR"); at_start_of_line_for_dot = at_start_of_line; at_start_of_line = 0; }}voidstart_I (void){ if (!underlined) { if (at_start_of_line) { printf (".I "); at_start_of_line_for_dot = 0; } else { printf ("\\fI"); at_start_of_line_for_dot = at_start_of_line; } at_start_of_line = 0; } ++underlined;}voidstop_I (void){ if (--underlined == 0) { if (bold) { if (at_start_of_line) { printf (".B "); at_start_of_line_for_dot = 0; } else { printf ("\\fB"); at_start_of_line_for_dot = at_start_of_line; } } else { printf ("\\fR"); at_start_of_line_for_dot = at_start_of_line; } } at_start_of_line = 0;}voidputone (char c){ if (disabled || (in_item && c == ' ')) return; if (at_start_of_line_for_dot && c == '.') { if (!at_start_of_line) printf (" ."); else if (underlined) printf (".I .\fI"); else if (bold) printf (".B .\fB"); else printf (" ."); return; } if (no_break && c == '\n') c = ' '; if (no_break && c == ' ') putchar ('\\'); if (to_upper) c = toupper (c); if (c == '\'' || c == '`' || c == '\\') putchar ('\\'); putchar (c); at_start_of_line = at_start_of_line_for_dot = (c == '\n');}/* Replacement stack */int nr = 0;char *vars[256];char *replacement[256];int start_line[256];static char value_updated[64], value_edition[64], value_version[64];process_texi (FILE * in){ char buf[1024]; int in_block = 0; int line_no = 0; while (fgets (buf, sizeof (buf), in)) { ++line_no; if (strncmp (buf, "@include ", 9) == 0) { FILE *in2; char *p = strchr (buf, '\n'); if (p) *p = 0; in2 = fopen (buf + 9, "r"); if (p) *p = '\n'; if (in2) { process_texi (in2); fclose (in2); } } else if (strncmp (buf, "@set ", 5) == 0) { char *p = strchr (buf, '\n'); if (p) *p = 0; if (!strncmp (buf + 5, "UPDATED ", 8)) strcpy (value_updated, buf + 13); if (!strncmp (buf + 5, "EDITION ", 8)) strcpy (value_edition, buf + 13); if (!strncmp (buf + 5, "VERSION ", 8)) strcpy (value_version, buf + 13); if (p) *p = '\n'; } if (strncmp (buf, "@c !BEGIN ", 10) == 0) { char *p = buf + 10; size_t len = strlen (p); if (nr == 256) exit (-1); vars[nr] = (char *) malloc (len + 1); replacement[nr] = (char *) malloc (131072); start_line[nr] = line_no + 1; strcpy (vars[nr], p); if ((p = strchr (vars[nr], '\n'))) *p = 0; if ((p = strchr (vars[nr], ' '))) *p = 0; in_block = 1; } else if (strncmp (buf, "@c !END", 7) == 0) { size_t len = strlen (replacement[nr]); if (replacement[nr][len - 1] == '\n') { replacement[nr][len - 1] = 0; --len; } in_block = 0; replacement[nr] = (char *) realloc (replacement[nr], len + 1); ++nr; } else if (in_block) { if (in_block == 1) { strcpy (replacement[nr], buf); in_block = 2; } else strcat (replacement[nr], buf); } }}intmain (int argc, char *argv[]){ FILE *in; char buf[1024]; int line_no = 0; texinfoname = argv[2]; in = fopen (argv[2], "r"); process_texi (in); fclose (in); in = fopen (argv[1], "r"); while (fgets (buf, sizeof (buf), in)) { char *p; for (p = buf; *p; ++p) { if (*p != '@') putchar (*p); else { int i; for (i = 0; i < nr; ++i) { size_t len = strlen (vars[i]); if (strncmp (p + 1, vars[i], len) == 0 && p[len + 1] == '@') { int stack[16]; int *what = stack; int at_start_of_input_line = 1; char *q; *what = 0; p += strlen (vars[i]) + 1; line_no = start_line[i]; for (q = replacement[i]; *q; ++q) { if (*q != '@') { int was_at_start_of_input_line = at_start_of_input_line; at_start_of_input_line = (*q == '\n'); if (at_start_of_input_line) ++line_no; if (ignore) continue; if (in_section && *q == '\n') { in_section = 0; to_upper = 0; putone ('"'); } if (in_item && *q == '\n') { in_item = 0; stop_bold (); } if (in_table && *q == '\n') continue; if (*what && *q == '}') { switch (*what--) { case 6: to_upper = 0; --no_break; /* fall through */ case 1: if (!in_section) stop_bold (); break; case 2: putone ('"'); break; case 3: stop_I (); break; case 4: putone (']'); break; case 5: /* Emphasis */ /* putone('*'); */ stop_I (); break; case 7: putone ('\''); break; case 8: --no_break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -