mirror of https://github.com/MidnightCommander/mc
* man2hlp.c: Avoid the need to use gindex.pl.
From Andrew V. Samoilov <sav@bcs.zp.ua> * Makefile.am: Use man2hlp directly. (EXTRA_DIST): Remove gindex.pl. * gindex.pl: Remove.
This commit is contained in:
parent
bfe29a4ac2
commit
7e2fd7960e
|
@ -1,5 +1,11 @@
|
|||
2002-07-30 Pavel Roskin <proski@gnu.org>
|
||||
|
||||
* man2hlp.c: Avoid the need to use gindex.pl.
|
||||
From Andrew V. Samoilov <sav@bcs.zp.ua>
|
||||
* Makefile.am: Use man2hlp directly.
|
||||
(EXTRA_DIST): Remove gindex.pl.
|
||||
* gindex.pl: Remove.
|
||||
|
||||
* ext.c (regex_command): Additional check to prevent reading
|
||||
unallocated memory when matching "shell/". Found by Valgrind.
|
||||
|
||||
|
|
|
@ -70,12 +70,12 @@ mc_SOURCES = $(SRCS)
|
|||
endif
|
||||
|
||||
EXTRA_DIST = TODO ChangeLog OChangeLog man2hlp.c \
|
||||
gindex.pl ncurses.patch mc.hlp $(CHARSET_SRC)
|
||||
ncurses.patch mc.hlp $(CHARSET_SRC)
|
||||
|
||||
$(srcdir)/mc.hlp: $(top_builddir)/doc/mc.1 $(top_srcdir)/lib/xnc.hlp $(srcdir)/gindex.pl man2hlp.c
|
||||
$(srcdir)/mc.hlp: $(top_builddir)/doc/mc.1 $(top_srcdir)/lib/xnc.hlp man2hlp.c
|
||||
- $(MAKE) man2hlp
|
||||
- perl $(srcdir)/gindex.pl $(top_builddir)/doc/mc.1 \
|
||||
$(top_srcdir)/lib/xnc.hlp $(srcdir)/mc.hlp
|
||||
- ./man2hlp 58 $(top_builddir)/doc/mc.1 $(top_srcdir)/lib/xnc.hlp \
|
||||
$(srcdir)/mc.hlp
|
||||
- touch $(srcdir)/mc.hlp
|
||||
|
||||
install-exec-hook:
|
||||
|
|
|
@ -1,73 +0,0 @@
|
|||
#! /usr/bin/perl -w
|
||||
# Since we use a linear search trought the block and the license and
|
||||
# the warranty are quite big, we leave them at the end of the help file,
|
||||
# the index will be consulted quite frequently, so we put it at the beginning.
|
||||
|
||||
$man2hlp = './man2hlp';
|
||||
if ($#ARGV == 4) {
|
||||
$Topics = "$ARGV[3]";
|
||||
$man2hlp = "$ARGV[4]";
|
||||
} elsif ($#ARGV == 3) {
|
||||
$Topics = "$ARGV[3]";
|
||||
} elsif ($#ARGV == 2) {
|
||||
$Topics = 'Topics:';
|
||||
} else {
|
||||
die "Usage: gindex.pl man_file tmpl_file out_file [Topic section header] [man2hlp]";
|
||||
}
|
||||
|
||||
$man_file = "$ARGV[0]";
|
||||
$tmpl_file = "$ARGV[1]";
|
||||
$out_file = "$ARGV[2]";
|
||||
|
||||
$help_width = 58;
|
||||
open (HELP1, "$man2hlp $help_width $man_file |") or
|
||||
die "Cannot open read output of man2hlp: $!\n";;
|
||||
@help_file = <HELP1>;
|
||||
close (HELP1);
|
||||
|
||||
open (HELP2, "< $tmpl_file") or die "Cannot open $tmpl_file: $!\n";
|
||||
push @help_file, <HELP2>;
|
||||
close (HELP2);
|
||||
|
||||
if ($Topics eq ''){
|
||||
$Topics = shift (@help_file);
|
||||
chomp ($Topics);
|
||||
}
|
||||
|
||||
foreach $line (@help_file){
|
||||
if ($line =~ /\x04\[(.*)\]/ && $line !~ /\x04\[main\]/){
|
||||
if (length $1) {
|
||||
$node = $1;
|
||||
} else {
|
||||
push @nodes, '';
|
||||
}
|
||||
$line =~ s/(\x04\[) */$1/;
|
||||
} elsif (defined ($node)){
|
||||
if ($line ne "\n") {
|
||||
push @nodes, "$node\x02$line";
|
||||
} else {
|
||||
push @nodes, "$node\x02$node";
|
||||
}
|
||||
undef ($node);
|
||||
}
|
||||
}
|
||||
|
||||
unlink ("$out_file");
|
||||
if (-e "$out_file") {
|
||||
die "Cannot remove $out_file\n";
|
||||
}
|
||||
|
||||
open (OUTPUT, "> $out_file") or die "Cannot open $out_file: $!\n";
|
||||
|
||||
print OUTPUT "\x04[Contents]\n$Topics\n\n";
|
||||
foreach $node (@nodes){
|
||||
if (length $node){
|
||||
$node =~ m/^( *)(.*)\x02(.*)$/;
|
||||
print OUTPUT (" $1\x01 $3 \x02$2\x03");
|
||||
}
|
||||
print OUTPUT "\n";
|
||||
}
|
||||
|
||||
print OUTPUT @help_file;
|
||||
|
||||
close (OUTPUT);
|
142
src/man2hlp.c
142
src/man2hlp.c
|
@ -20,6 +20,12 @@
|
|||
#include <stdlib.h>
|
||||
#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
|
||||
|
@ -38,6 +44,15 @@ static int link_flag = 0; /* Flag: Next line is a link */
|
|||
static int verbatim_flag = 0; /* Flag: Copy input to output verbatim */
|
||||
static int node = 0; /* Flag: This line is an original ".SH" */
|
||||
|
||||
static char *output; /* The name of the output mc.hlp */
|
||||
static char *Topics = "Topics:";
|
||||
|
||||
static struct node {
|
||||
char *node;
|
||||
char *lname;
|
||||
struct node *next;
|
||||
} nodes, *cnode;
|
||||
|
||||
/* Report error in input */
|
||||
static void print_error (char *message)
|
||||
{
|
||||
|
@ -214,11 +229,27 @@ static void handle_command (char *buffer)
|
|||
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);
|
||||
if (!cnode){
|
||||
cnode = &nodes;
|
||||
cnode->next = NULL;
|
||||
} else {
|
||||
cnode->next = malloc (sizeof (nodes));
|
||||
cnode = cnode->next;
|
||||
}
|
||||
cnode->node = strdup (buffer);
|
||||
cnode->lname = NULL;
|
||||
}
|
||||
col ++;
|
||||
newline ();
|
||||
}
|
||||
if (SH){
|
||||
if (output)
|
||||
/* print_string() strtok()es buffer, so */
|
||||
cnode->lname = strdup (buffer + heading_level);
|
||||
print_string (buffer + heading_level);
|
||||
newline ();
|
||||
newline ();
|
||||
|
@ -291,7 +322,10 @@ static void handle_command (char *buffer)
|
|||
print_error ("Syntax error: .\\\"TOPICS: no text");
|
||||
return;
|
||||
}
|
||||
printf ("%s\n", buffer);
|
||||
if (output)
|
||||
Topics = strdup (buffer);
|
||||
else
|
||||
printf ("%s\n", buffer);
|
||||
}
|
||||
else {
|
||||
/* Other commands are ignored */
|
||||
|
@ -340,13 +374,20 @@ int main (int argc, char **argv)
|
|||
FILE *file; /* Input file */
|
||||
char buffer2 [BUFFER_SIZE]; /* Temp input line */
|
||||
char *buffer = buffer2; /* Input line */
|
||||
char *node = NULL;
|
||||
|
||||
int file_fd;
|
||||
long contents_beginning, file_end;
|
||||
|
||||
/* Validity check for arguments */
|
||||
if (argc != 3 || ((width = atoi (argv[1])) <= 10)){
|
||||
fprintf (stderr, "Usage: man2hlp <width> <file.man>\n");
|
||||
if ((argc != 3 && argc != 5) || ((width = atoi (argv[1])) <= 10)){
|
||||
fprintf (stderr, "Usage: man2hlp <width> <file.man> [template_file helpfile]\n");
|
||||
return 3;
|
||||
}
|
||||
|
||||
if (argc == 5)
|
||||
output = argv[4];
|
||||
|
||||
/* Open the input file */
|
||||
filename = argv[2];
|
||||
file = fopen (filename, "r");
|
||||
|
@ -356,6 +397,12 @@ int main (int argc, char **argv)
|
|||
return 3;
|
||||
}
|
||||
|
||||
if (argc == 5 && freopen (argv [4], "w", stdout) == NULL){
|
||||
sprintf (buffer, "man2hlp: Cannot open file \"%s\"", argv[4]);
|
||||
perror (buffer);
|
||||
return 3;
|
||||
}
|
||||
|
||||
/* Repeat for each input line */
|
||||
while (!feof (file)){
|
||||
/* Read a line */
|
||||
|
@ -399,5 +446,94 @@ int main (int argc, char **argv)
|
|||
/* All done */
|
||||
newline ();
|
||||
fclose (file);
|
||||
|
||||
if (argc != 5)
|
||||
return 0;
|
||||
|
||||
/* Open the template file */
|
||||
filename = argv[3];
|
||||
file = fopen (filename, "r");
|
||||
if (file == NULL){
|
||||
sprintf (buffer, "man2hlp: Cannot open file \"%s\"", filename);
|
||||
perror (buffer);
|
||||
return 3;
|
||||
}
|
||||
/* Repeat for each input line */
|
||||
while (!feof (file)){
|
||||
/* Read a line */
|
||||
if (!fgets (buffer2, BUFFER_SIZE, file)){
|
||||
break;
|
||||
}
|
||||
if (node){
|
||||
if (*buffer2 && *buffer2 != '\n'){
|
||||
cnode->lname = strdup (buffer2);
|
||||
node = strchr (cnode->lname, '\n');
|
||||
if (node)
|
||||
*node = 0;
|
||||
}
|
||||
node = NULL;
|
||||
} else {
|
||||
node = strchr (buffer, CHAR_NODE_END);
|
||||
if (node && (node[1] == '[')){
|
||||
char *p = strrchr (node, ']');
|
||||
if (p && strncmp (node+2, "main", 4) == 0 && node[6] == ']') {
|
||||
node = 0;
|
||||
} else {
|
||||
if (!cnode){
|
||||
cnode = &nodes;
|
||||
cnode->next = NULL;
|
||||
} else {
|
||||
cnode->next = malloc (sizeof (nodes));
|
||||
cnode = cnode->next;
|
||||
}
|
||||
cnode->node = strdup (node + 2);
|
||||
cnode->node [p - node - 2] = 0;
|
||||
cnode->lname = NULL;
|
||||
}
|
||||
} else
|
||||
node = NULL;
|
||||
}
|
||||
fputs (buffer, stdout);
|
||||
}
|
||||
|
||||
contents_beginning = ftell (stdout);
|
||||
printf ("\004[Contents]\n%s\n\n", Topics);
|
||||
|
||||
for (cnode = &nodes; cnode && cnode->node; cnode = cnode->next){
|
||||
char *node = cnode->node;
|
||||
int heading_level = 0;
|
||||
while (*node == ' '){
|
||||
heading_level++;
|
||||
node++;
|
||||
}
|
||||
if (*node)
|
||||
printf (" %*s\001 %s \002%s\003", heading_level, "",
|
||||
cnode->lname ? cnode->lname : node,
|
||||
node);
|
||||
printf ("\n");
|
||||
|
||||
free (cnode->node);
|
||||
if (cnode->lname)
|
||||
free (cnode->lname);
|
||||
if (cnode != &nodes)
|
||||
free (cnode);
|
||||
}
|
||||
|
||||
file_end = ftell (stdout);
|
||||
fclose (file);
|
||||
|
||||
Topics = malloc (file_end);
|
||||
file_fd = open (output, O_RDONLY);
|
||||
if (file_fd == -1)
|
||||
perror (output);
|
||||
if (read (file_fd, Topics, file_end) != file_end)
|
||||
perror (output);
|
||||
if (close (file_fd) == -1)
|
||||
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 0;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue