📄 man2hlp.c
字号:
/* Man page to help file converter
Copyright (C) 1994, 1995 Janne Kukonlehto
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include "help.h"
#define BUFFER_SIZE 256
static char *filename; /* The name of the input file */
static int width; /* Output width in characters */
static int col = 0; /* Current output column */
static FILE *index_file; /* HTML index file */
static int out_row = 1; /* Current output row */
static int in_row = 0; /* Current input row */
static int old_heading_level = 0;/* Level of the last heading */
static int no_split_flag = 0; /* Flag: Don't split section on next ".SH" */
static int skip_flag = 0; /* Flag: Skip this section.
0 = don't skip,
1 = skipping title,
2 = title skipped, skipping text */
static int link_flag = 0; /* Flag: Next line is a link */
static int verbatim_flag = 0; /* Flag: Copy input to output verbatim */
/* Report error in input */
void print_error (char *message)
{
fprintf (stderr, "man2hlp: %s in file \"%s\" at row %d\n", message, filename, in_row);
}
/* Change output line */
void newline (void)
{
out_row ++;
col = 0;
printf("\n");
}
/* Calculate the length of string */
int string_len (char *buffer)
{
static int anchor_flag = 0; /* Flag: Inside hypertext anchor name */
static int link_flag = 0; /* Flag: Inside hypertext link target name */
int backslash_flag = 0; /* Flag: Backslash quoting */
int i; /* Index */
int c; /* Current character */
int len = 0; /* Result: the length of the string */
for (i = 0; i < strlen (buffer); i ++)
{
c = buffer [i];
if (c == CHAR_LINK_POINTER)
link_flag = 1; /* Link target name starts */
else if (c == CHAR_LINK_END)
link_flag = 0; /* Link target name ends */
else if (c == CHAR_NODE_END){
/* Node anchor name starts */
anchor_flag = 1;
/* Ugly hack to prevent loss of one space */
len ++;
}
/* Don't add control characters to the length */
if (c < 32)
continue;
/* Attempt to handle backslash quoting */
if (c == '\\' && !backslash_flag){
backslash_flag = 1;
continue;
}
backslash_flag = 0;
/* Increase length if not inside anchor name or link target name */
if (!anchor_flag && !link_flag)
len ++;
if (anchor_flag && c == ']'){
/* Node anchor name ends */
anchor_flag = 0;
}
}
return len;
}
/* Output the string */
void print_string (char *buffer)
{
int len; /* The length of current word */
int i; /* Index */
int c; /* Current character */
int backslash_flag = 0;
/* Skipping lines? */
if (skip_flag)
return;
/* Copying verbatim? */
if (verbatim_flag){
printf("%s", buffer);
return;
}
if (width){
/* HLP format */
/* Split into words */
buffer = strtok (buffer, " \t\n");
/* Repeat for each word */
while (buffer){
/* Skip empty strings */
if (strlen (buffer) > 0){
len = string_len (buffer);
/* Change the line if about to break the right margin */
if (col + len >= width)
newline ();
/* Words are separated by spaces */
if (col > 0){
printf (" ");
col ++;
}
/* Attempt to handle backslash quoting */
for (i = 0; i < strlen (buffer); i++)
{
c = buffer [i];
if (c == '\\' && !backslash_flag){
backslash_flag = 1;
continue;
}
backslash_flag = 0;
printf ("%c", c);
}
/* Increase column */
col += len;
}
/* Get the next word */
buffer = strtok (NULL, " \t\n");
} /* while */
} else {
/* HTML format */
if (strlen (buffer) > 0){
/* Attempt to handle backslash quoting */
for (i = 0; i < strlen (buffer); i++)
{
c = buffer [i];
if (c == '\\' && !backslash_flag){
backslash_flag = 1;
continue;
}
backslash_flag = 0;
printf ("%c", c);
}
}
} /* if (width) */
}
/* Like print_string but with printf-like syntax */
void printf_string (char *format, ...)
{
va_list args;
char buffer [BUFFER_SIZE];
va_start (args, format);
vsprintf (buffer, format, args);
va_end (args);
print_string (buffer);
}
/* Handle all the roff dot commands */
void handle_command (char *buffer)
{
int i, len, heading_level;
/* Get the command name */
strtok (buffer, " \t");
if (strcmp (buffer, ".SH") == 0){
/* If we already skipped a section, don't skip another */
if (skip_flag == 2){
skip_flag = 0;
}
/* Get the command parameters */
buffer = strtok (NULL, "");
if (buffer == NULL){
print_error ("Syntax error: .SH: no title");
return;
} else {
/* Remove quotes */
if (buffer[0] == '"'){
buffer ++;
len = strlen (buffer);
if (buffer[len-1] == '"'){
len --;
buffer[len] = 0;
}
}
/* Calculate heading level */
heading_level = 0;
while (buffer [heading_level] == ' ')
heading_level ++;
/* Heading level must be even */
if (heading_level & 1)
print_error ("Syntax error: .SH: odd heading level");
if (no_split_flag){
/* Don't start a new section */
if (width){
/* HLP format */
newline ();
print_string (buffer);
newline ();
newline ();
} else {
/* HTML format */
newline ();
printf_string ("<h4>%s</h4>", buffer);
newline ();
}
no_split_flag = 0;
}
else if (skip_flag){
/* Skipping title and marking text for skipping */
skip_flag = 2;
}
else {
/* Start a new section */
if (width){
/* HLP format */
printf ("%c[%s]", CHAR_NODE_END, buffer);
col ++;
newline ();
print_string (buffer + heading_level);
newline ();
newline ();
} else {
/* HTML format */
if (buffer [0]){
if (heading_level > old_heading_level){
for (i = old_heading_level; i < heading_level; i += 2)
fprintf (index_file, "<ul>");
} else {
for (i = heading_level; i < old_heading_level; i += 2)
fprintf (index_file, "</ul>");
}
old_heading_level = heading_level;
printf_string ("<h%d><a name=\"%s\">%s</a></h%d>",
heading_level / 2 + 2, buffer + heading_level,
buffer + heading_level, heading_level / 2 + 2);
newline ();
fprintf (index_file, "<li><a href=\"#%s\">%s</a>\n",
buffer + heading_level, buffer + heading_level);
} else {
for (i = 0; i < old_heading_level; i += 2)
fprintf (index_file, "</ul>");
old_heading_level = 0;
fprintf (index_file, "</ul><p><ul>\n");
}
} /* if (width) */
} /* Start new section */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -