1
1

* man2hlp.c (main): Use only stream I/O. Fix checks for error

conditions.  Reformat the whole file.
Этот коммит содержится в:
Pavel Roskin 2002-07-30 22:57:01 +00:00
родитель 56a685c3e6
Коммит 80561f39a7
2 изменённых файлов: 199 добавлений и 169 удалений

Просмотреть файл

@ -1,5 +1,8 @@
2002-07-30 Pavel Roskin <proski@gnu.org>
* man2hlp.c (main): Use only stream I/O. Fix checks for error
conditions. Reformat the whole file.
* man2hlp.c: Avoid the need to use gindex.pl.
From Andrew V. Samoilov <sav@bcs.zp.ua>
* Makefile.am: Use man2hlp directly.

Просмотреть файл

@ -21,11 +21,6 @@
#include <stdarg.h>
#include <string.h>
#include <fcntl.h> /* O_RDONLY, O_WRONLY */
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#include "help.h"
#define BUFFER_SIZE 256
@ -54,13 +49,16 @@ static struct node {
} nodes, *cnode;
/* Report error in input */
static void print_error (char *message)
static void
print_error (char *message)
{
fprintf (stderr, "man2hlp: %s in file \"%s\" at row %d\n", message, filename, in_row);
fprintf (stderr, "man2hlp: %s in file \"%s\" at row %d\n", message,
filename, in_row);
}
/* Change output line */
static void newline (void)
static void
newline (void)
{
out_row++;
col = 0;
@ -68,7 +66,8 @@ static void newline (void)
}
/* Calculate the length of string */
static int string_len (char *buffer)
static 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 */
@ -76,8 +75,7 @@ static int string_len (char *buffer)
int c; /* Current character */
int len = 0; /* Result: the length of the string */
while (*(buffer))
{
while (*(buffer)) {
c = *buffer++;
if (c == CHAR_LINK_POINTER)
link_flag = 1; /* Link target name starts */
@ -110,7 +108,8 @@ static int string_len (char *buffer)
}
/* Output the string */
static void print_string (char *buffer)
static void
print_string (char *buffer)
{
int len; /* The length of current word */
int c; /* Current character */
@ -148,8 +147,7 @@ static void print_string (char *buffer)
col++;
}
/* Attempt to handle backslash quoting */
while (*(buffer))
{
while (*(buffer)) {
c = *buffer++;
if (c == '\\' && !backslash_flag) {
backslash_flag = 1;
@ -168,7 +166,8 @@ static void print_string (char *buffer)
}
/* Like print_string but with printf-like syntax */
static void printf_string (char *format, ...)
static void
printf_string (char *format, ...)
{
va_list args;
char buffer[BUFFER_SIZE];
@ -180,13 +179,15 @@ static void printf_string (char *format, ...)
}
/* Handle all the roff dot commands */
static void handle_command (char *buffer)
static void
handle_command (char *buffer)
{
int len, heading_level;
/* Get the command name */
strtok (buffer, " \t");
if ((strcmp (buffer, ".SH") == 0) || (strcmp (buffer, ".\\\"NODE") == 0)){
if ((strcmp (buffer, ".SH") == 0)
|| (strcmp (buffer, ".\\\"NODE") == 0)) {
int SH = (strcmp (buffer, ".SH") == 0);
/* If we already skipped a section, don't skip another */
if (skip_flag == 2) {
@ -221,18 +222,17 @@ static void handle_command (char *buffer)
newline ();
newline ();
no_split_flag = 0;
}
else if (skip_flag){
} else if (skip_flag) {
/* Skipping title and marking text for skipping */
skip_flag = 2;
}
else {
} else {
if (!SH || !node) {
/* Start a new section */
if (!output)
printf ("%c[%s]", CHAR_NODE_END, buffer);
else {
printf ("%c[%s]", CHAR_NODE_END, buffer + heading_level);
printf ("%c[%s]", CHAR_NODE_END,
buffer + heading_level);
if (!cnode) {
cnode = &nodes;
cnode->next = NULL;
@ -260,25 +260,22 @@ static void handle_command (char *buffer)
} /* Command .SH */
else if (strcmp (buffer, ".\\\"DONT_SPLIT\"") == 0) {
no_split_flag = 1;
}
else if (strcmp (buffer, ".\\\"SKIP_SECTION\"") == 0){
} else if (strcmp (buffer, ".\\\"SKIP_SECTION\"") == 0) {
skip_flag = 1;
}
else if (strcmp (buffer, ".\\\"LINK2\"") == 0){
} else if (strcmp (buffer, ".\\\"LINK2\"") == 0) {
/* Next two input lines form a link */
link_flag = 2;
}
else if (strcmp (buffer, ".PP") == 0){
} else if (strcmp (buffer, ".PP") == 0) {
/* End of paragraph */
if (col > 0) newline();
if (col > 0)
newline ();
}
else if (strcmp (buffer, ".nf") == 0){
newline ();
} else if (strcmp (buffer, ".nf") == 0) {
/* Following input lines are to be handled verbatim */
verbatim_flag = 1;
if (col > 0) newline ();
}
else if (strcmp (buffer, ".I") == 0 || strcmp (buffer, ".B") == 0){
if (col > 0)
newline ();
} else if (strcmp (buffer, ".I") == 0 || strcmp (buffer, ".B") == 0) {
/* Bold text or italics text */
char type = buffer[1];
char *p;
@ -306,15 +303,15 @@ static void handle_command (char *buffer)
*w++ = CHAR_BOLD_OFF;
*w = 0;
print_string (buffer);
}
else if (strcmp (buffer, ".TP") == 0){
} else if (strcmp (buffer, ".TP") == 0) {
/* End of paragraph? */
if (col > 0) newline ();
if (col > 0)
newline ();
}
else if (strcmp (buffer, ".\\\"TOPICS") == 0){
newline ();
} else if (strcmp (buffer, ".\\\"TOPICS") == 0) {
if (out_row > 1) {
print_error ("Syntax error: .\\\"TOPICS must be first command");
print_error
("Syntax error: .\\\"TOPICS must be first command");
return;
}
buffer = strtok (NULL, "");
@ -326,13 +323,13 @@ static void handle_command (char *buffer)
Topics = strdup (buffer);
else
printf ("%s\n", buffer);
}
else {
} else {
/* Other commands are ignored */
}
}
static void handle_link (char *buffer)
static void
handle_link (char *buffer)
{
static char old[80];
int len;
@ -345,8 +342,7 @@ static void handle_link (char *buffer)
/* First part of new format link */
/* Bold text or italics text */
if (buffer[0] == '.' && (buffer[1] == 'I' || buffer[1] == 'B'))
for (buffer += 2; *buffer == ' ' || *buffer == '\t'; buffer++)
;
for (buffer += 2; *buffer == ' ' || *buffer == '\t'; buffer++);
strcpy (old, buffer);
link_flag = 3;
break;
@ -362,26 +358,29 @@ static void handle_link (char *buffer)
if (len && buffer[len - 1] == '"') {
buffer[--len] = 0;
}
printf_string ("%c%s%c%s%c\n", CHAR_LINK_START, old, CHAR_LINK_POINTER, buffer, CHAR_LINK_END);
printf_string ("%c%s%c%s%c\n", CHAR_LINK_START, old,
CHAR_LINK_POINTER, buffer, CHAR_LINK_END);
link_flag = 0;
break;
}
}
int main (int argc, char **argv)
int
main (int argc, char **argv)
{
int len; /* Length of input line */
FILE *file; /* Input file */
FILE *fout; /* Output file */
char buffer2[BUFFER_SIZE]; /* Temp input line */
char *buffer = buffer2; /* Input line */
char *node = NULL;
int file_fd;
long contents_beginning, file_end;
long cont_start, file_end;
/* Validity check for arguments */
if ((argc != 3 && argc != 5) || ((width = atoi (argv[1])) <= 10)) {
fprintf (stderr, "Usage: man2hlp <width> <file.man> [template_file helpfile]\n");
fprintf (stderr,
"Usage: man2hlp <width> <file.man> [template_file helpfile]\n");
return 3;
}
@ -416,8 +415,7 @@ int main (int argc, char **argv)
in_row++;
len = strlen (buffer);
/* Remove terminating newline */
if (buffer [len-1] == '\n')
{
if (buffer[len - 1] == '\n') {
len--;
buffer[len] = 0;
}
@ -429,15 +427,13 @@ int main (int argc, char **argv)
print_string (buffer);
newline ();
}
}
else if (link_flag)
} else if (link_flag)
/* The line is a link */
handle_link (buffer);
else if (buffer[0] == '.')
/* The line is a roff command */
handle_command (buffer);
else
{
else {
/* A normal line, just output it */
print_string (buffer);
}
@ -476,7 +472,8 @@ int main (int argc, char **argv)
node = strchr (buffer, CHAR_NODE_END);
if (node && (node[1] == '[')) {
char *p = strrchr (node, ']');
if (p && strncmp (node+2, "main", 4) == 0 && node[6] == ']') {
if (p && strncmp (node + 2, "main", 4) == 0
&& node[6] == ']') {
node = 0;
} else {
if (!cnode) {
@ -496,7 +493,7 @@ int main (int argc, char **argv)
fputs (buffer, stdout);
}
contents_beginning = ftell (stdout);
cont_start = ftell (stdout);
printf ("\004[Contents]\n%s\n\n", Topics);
for (cnode = &nodes; cnode && cnode->node; cnode = cnode->next) {
@ -508,8 +505,7 @@ int main (int argc, char **argv)
}
if (*node)
printf (" %*s\001 %s \002%s\003", heading_level, "",
cnode->lname ? cnode->lname : node,
node);
cnode->lname ? cnode->lname : node, node);
printf ("\n");
free (cnode->node);
@ -523,17 +519,48 @@ int main (int argc, char **argv)
fclose (file);
Topics = malloc (file_end);
file_fd = open (output, O_RDONLY);
if (file_fd == -1)
if (!Topics)
return 1;
fout = fopen (output, "r");
if (!fout) {
perror (output);
if (read (file_fd, Topics, file_end) != file_end)
return 1;
}
if (fread (Topics, 1, file_end, fout) != file_end) {
perror (output);
if (close (file_fd) == -1)
return 1;
}
if (fclose (fout) != 0) {
perror (output);
file_fd = open (output, O_WRONLY);
write (file_fd, Topics + contents_beginning, file_end - contents_beginning);
write (file_fd, Topics, contents_beginning);
close (file_fd);
return 1;
}
fout = fopen (output, "w");
if (!fout) {
perror (output);
return 1;
}
if (fwrite (Topics + cont_start, 1, file_end - cont_start, fout) !=
file_end - cont_start) {
perror (output);
return 1;
}
if (fwrite (Topics, 1, cont_start, fout) != cont_start) {
perror (output);
return 1;
}
free (Topics);
if (fclose (fout) != 0) {
perror (output);
return 1;
}
return 0;
}