1
1

* man2hlp.c: Don't redirect stdout. Check result of ftell.

Drop support for invocation with 2 arguments.
Этот коммит содержится в:
Pavel Roskin 2002-08-20 16:13:13 +00:00
родитель e5a783c1f9
Коммит f486423122
2 изменённых файлов: 79 добавлений и 77 удалений

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

@ -1,3 +1,8 @@
2002-08-20 Pavel Roskin <proski@gnu.org>
* man2hlp.c: Don't redirect stdout. Check result of ftell.
Drop support for invocation with 2 arguments.
2002-08-19 Pavel Roskin <proski@gnu.org> 2002-08-19 Pavel Roskin <proski@gnu.org>
* cons.handler.c (handle_console): Use _exit(), not exit() in * cons.handler.c (handle_console): Use _exit(), not exit() in

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

@ -25,7 +25,6 @@
#define BUFFER_SIZE 256 #define BUFFER_SIZE 256
static char *filename; /* The name of the input file */
static int width; /* Output width in characters */ static int width; /* Output width in characters */
static int col = 0; /* Current output column */ static int col = 0; /* Current output column */
static int out_row = 1; /* Current output row */ static int out_row = 1; /* Current output row */
@ -39,7 +38,9 @@ static int link_flag = 0; /* Flag: Next line is a link */
static int verbatim_flag = 0; /* Flag: Copy input to output verbatim */ static int verbatim_flag = 0; /* Flag: Copy input to output verbatim */
static int node = 0; /* Flag: This line is an original ".SH" */ static int node = 0; /* Flag: This line is an original ".SH" */
static char *output; /* The name of the output mc.hlp */ static const char *c_out; /* Output filename */
static FILE *f_out; /* Output file */
static char *Topics = "Topics:"; static char *Topics = "Topics:";
static struct node { static struct node {
@ -111,7 +112,7 @@ static void
print_error (char *message) print_error (char *message)
{ {
fprintf (stderr, "man2hlp: %s in file \"%s\" at row %d\n", message, fprintf (stderr, "man2hlp: %s in file \"%s\" at row %d\n", message,
filename, in_row); c_out, in_row);
} }
/* Change output line */ /* Change output line */
@ -120,7 +121,7 @@ newline (void)
{ {
out_row++; out_row++;
col = 0; col = 0;
printf ("\n"); fprintf (f_out, "\n");
} }
/* Calculate the length of string */ /* Calculate the length of string */
@ -186,7 +187,7 @@ print_string (char *buffer)
continue; continue;
} }
backslash_flag = 0; backslash_flag = 0;
printf ("%c", c); fprintf (f_out, "%c", c);
} }
} else { } else {
/* Split into words */ /* Split into words */
@ -201,7 +202,7 @@ print_string (char *buffer)
newline (); newline ();
/* Words are separated by spaces */ /* Words are separated by spaces */
if (col > 0) { if (col > 0) {
printf (" "); fprintf (f_out, " ");
col++; col++;
} }
/* Attempt to handle backslash quoting */ /* Attempt to handle backslash quoting */
@ -212,7 +213,7 @@ print_string (char *buffer)
continue; continue;
} }
backslash_flag = 0; backslash_flag = 0;
printf ("%c", c); fprintf (f_out, "%c", c);
} }
/* Increase column */ /* Increase column */
col += len; col += len;
@ -286,28 +287,23 @@ handle_command (char *buffer)
} else { } else {
if (!SH || !node) { if (!SH || !node) {
/* Start a new section */ /* Start a new section */
if (!output) fprintf (f_out, "%c[%s]", CHAR_NODE_END,
printf ("%c[%s]", CHAR_NODE_END, buffer); buffer + heading_level);
else { if (!cnode) {
printf ("%c[%s]", CHAR_NODE_END, cnode = &nodes;
buffer + heading_level); cnode->next = NULL;
if (!cnode) { } else {
cnode = &nodes; cnode->next = malloc (sizeof (nodes));
cnode->next = NULL; cnode = cnode->next;
} else {
cnode->next = malloc (sizeof (nodes));
cnode = cnode->next;
}
cnode->node = strdup (buffer);
cnode->lname = NULL;
} }
cnode->node = strdup (buffer);
cnode->lname = NULL;
col++; col++;
newline (); newline ();
} }
if (SH) { if (SH) {
if (output) /* print_string() strtok()es buffer, so */
/* print_string() strtok()es buffer, so */ cnode->lname = strdup (buffer + heading_level);
cnode->lname = strdup (buffer + heading_level);
print_string (buffer + heading_level); print_string (buffer + heading_level);
newline (); newline ();
newline (); newline ();
@ -377,10 +373,7 @@ handle_command (char *buffer)
print_error ("Syntax error: .\\\"TOPICS: no text"); print_error ("Syntax error: .\\\"TOPICS: no text");
return; return;
} }
if (output) Topics = strdup (buffer);
Topics = strdup (buffer);
else
printf ("%s\n", buffer);
} else { } else {
/* Other commands are ignored */ /* Other commands are ignored */
} }
@ -427,8 +420,10 @@ int
main (int argc, char **argv) main (int argc, char **argv)
{ {
int len; /* Length of input line */ int len; /* Length of input line */
FILE *file; /* Input file */ const char *c_man; /* Manual filename */
FILE *fout; /* Output file */ const char *c_tmpl; /* Template filename */
FILE *f_man; /* Manual file */
FILE *f_tmpl; /* Template file */
char buffer2[BUFFER_SIZE]; /* Temp input line */ char buffer2[BUFFER_SIZE]; /* Temp input line */
char *buffer = buffer2; /* Input line */ char *buffer = buffer2; /* Input line */
char *node = NULL; char *node = NULL;
@ -436,34 +431,35 @@ main (int argc, char **argv)
long cont_start, file_end; long cont_start, file_end;
/* Validity check for arguments */ /* Validity check for arguments */
if ((argc != 3 && argc != 5) || ((width = atoi (argv[1])) <= 10)) { if ((argc != 5) || ((width = atoi (argv[1])) <= 10)) {
fprintf (stderr, fprintf (stderr,
"Usage: man2hlp <width> <file.man> [template_file helpfile]\n"); "Usage: man2hlp width file.man template_file helpfile\n");
return 3; return 3;
} }
if (argc == 5) c_man = argv[2];
output = argv[4]; c_tmpl = argv[3];
c_out = argv[4];
/* Open the input file */ /* Open the input file (manual) */
filename = argv[2]; f_man = fopen (c_man, "r");
file = fopen (filename, "r"); if (f_man == NULL) {
if (file == NULL) { sprintf (buffer, "man2hlp: Cannot open file \"%s\"", c_man);
sprintf (buffer, "man2hlp: Cannot open file \"%s\"", filename);
perror (buffer); perror (buffer);
return 3; return 3;
} }
if (argc == 5 && freopen (argv[4], "w", stdout) == NULL) { f_out = fopen (c_out, "w");
sprintf (buffer, "man2hlp: Cannot open file \"%s\"", argv[4]); if (f_out == NULL) {
sprintf (buffer, "man2hlp: Cannot open file \"%s\"", c_out);
perror (buffer); perror (buffer);
return 3; return 3;
} }
/* Repeat for each input line */ /* Repeat for each input line */
while (!feof (file)) { while (!feof (f_man)) {
/* Read a line */ /* Read a line */
if (!fgets (buffer2, BUFFER_SIZE, file)) { if (!fgets (buffer2, BUFFER_SIZE, f_man)) {
break; break;
} }
if (buffer2[0] == '\\' && buffer2[1] == '&') if (buffer2[0] == '\\' && buffer2[1] == '&')
@ -499,23 +495,20 @@ main (int argc, char **argv)
/* All done */ /* All done */
newline (); newline ();
fclose (file); fclose (f_man);
if (argc != 5)
return 0;
/* Open the template file */ /* Open the template file */
filename = argv[3]; f_tmpl = fopen (c_tmpl, "r");
file = fopen (filename, "r"); if (f_tmpl == NULL) {
if (file == NULL) { sprintf (buffer, "man2hlp: Cannot open file \"%s\"", c_tmpl);
sprintf (buffer, "man2hlp: Cannot open file \"%s\"", filename);
perror (buffer); perror (buffer);
return 3; return 3;
} }
/* Repeat for each input line */ /* Repeat for each input line */
while (!feof (file)) { while (!feof (f_tmpl)) {
/* Read a line */ /* Read a line */
if (!fgets (buffer2, BUFFER_SIZE, file)) { if (!fgets (buffer2, BUFFER_SIZE, f_tmpl)) {
break; break;
} }
if (node) { if (node) {
@ -548,11 +541,11 @@ main (int argc, char **argv)
} else } else
node = NULL; node = NULL;
} }
fputs (buffer, stdout); fputs (buffer, f_out);
} }
cont_start = ftell (stdout); cont_start = ftell (f_out);
printf ("\004[Contents]\n%s\n\n", Topics); fprintf (f_out, "\004[Contents]\n%s\n\n", Topics);
for (cnode = &nodes; cnode && cnode->node;) { for (cnode = &nodes; cnode && cnode->node;) {
char *node = cnode->node; char *node = cnode->node;
@ -564,9 +557,9 @@ main (int argc, char **argv)
node++; node++;
} }
if (*node) if (*node)
printf (" %*s\001 %s \002%s\003", heading_level, "", fprintf (f_out, " %*s\001 %s \002%s\003", heading_level, "",
cnode->lname ? cnode->lname : node, node); cnode->lname ? cnode->lname : node, node);
printf ("\n"); fprintf (f_out, "\n");
free (cnode->node); free (cnode->node);
if (cnode->lname) if (cnode->lname)
@ -576,51 +569,55 @@ main (int argc, char **argv)
cnode = next; cnode = next;
} }
file_end = ftell (stdout); file_end = ftell (f_out);
fclose (file); fclose (f_out);
if (file_end <= 0) {
perror (c_out);
}
Topics = malloc (file_end); Topics = malloc (file_end);
if (!Topics) if (!Topics)
return 1; return 1;
fout = fopen (output, "r"); f_out = fopen (c_out, "r");
if (!fout) { if (!f_out) {
perror (output); perror (c_out);
return 1; return 1;
} }
if (persistent_fread (Topics, file_end, fout) < 0) { if (persistent_fread (Topics, file_end, f_out) < 0) {
perror (output); perror (c_out);
return 1; return 1;
} }
if (fclose (fout) != 0) { if (fclose (f_out) != 0) {
perror (output); perror (c_out);
return 1; return 1;
} }
fout = fopen (output, "w"); f_out = fopen (c_out, "w");
if (!fout) { if (!f_out) {
perror (output); perror (c_out);
return 1; return 1;
} }
if (persistent_fwrite if (persistent_fwrite
(Topics + cont_start, file_end - cont_start, fout) (Topics + cont_start, file_end - cont_start, f_out)
< 0) { < 0) {
perror (output); perror (c_out);
return 1; return 1;
} }
if (persistent_fwrite (Topics, cont_start, fout) < 0) { if (persistent_fwrite (Topics, cont_start, f_out) < 0) {
perror (output); perror (c_out);
return 1; return 1;
} }
free (Topics); free (Topics);
if (fclose (fout) != 0) { if (fclose (f_out) != 0) {
perror (output); perror (c_out);
return 1; return 1;
} }