3 # Man page to help file converter
4 # Copyright (C) 1994, 1995, 1998, 2000, 2001, 2002, 2003, 2004, 2005,
5 # 2007, 2010 Free Software Foundation, Inc.
7 # Originally written by:
8 # 2002 Andrew V. Samoilov
11 # Completely rewriten on perl by:
12 # 2010 Alexandr Prenko
14 # This program is free software; you can redistribute it and/or modify
15 # it under the terms of the GNU General Public License as published by
16 # the Free Software Foundation; either version 2 of the License, or
17 # (at your option) any later version.
19 # This program is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 # GNU General Public License for more details.
24 # You should have received a copy of the GNU General Public License
25 # along with this program; if not, write to the Free Software
26 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
29 # \brief Source: man page to help file converter
32 # end of include "help.h"
37 # Perl have no static variables, so this hash emulates them
39 "string_len anchor_flag" => 0,
40 "string_len lc_link_flag" => 0,
41 "handle_link old" => undef
45 my $CHAR_LINK_START = chr(01); # Ctrl-A
46 my $CHAR_LINK_POINTER = chr(02); # Ctrl-B
47 my $CHAR_LINK_END = chr(03); # Ctrl-C
48 my $CHAR_NODE_END = chr(04); # Ctrl-D
49 my $CHAR_ALTERNATE = chr(05); # Ctrl-E
50 my $CHAR_NORMAL = chr(06); # Ctrl-F
51 my $CHAR_VERSION = chr(07); # Ctrl-G
52 my $CHAR_FONT_BOLD = chr(010); # Ctrl-H
53 my $CHAR_FONT_NORMAL = chr(013); # Ctrl-K
54 my $CHAR_FONT_ITALIC = chr(024); # Ctrl-T
57 my $col = 0; # Current output column
58 my $out_row = 1; # Current output row
59 my $in_row = 0; # Current input row
60 my $no_split_flag = 0; # Flag: Don't split section on next ".SH"
61 my $skip_flag = 0; # Flag: Skip this section.
64 # 2 = title skipped, skipping text
65 my $link_flag = 0; # Flag: Next line is a link
66 my $verbatim_flag = 0; # Flag: Copy input to output verbatim
67 my $node = 0; # Flag: This line is an original ".SH"
69 my $c_out; # Output filename
70 my $f_out; # Output file
72 my $c_in; # Current input filename
74 my $indentation; # Indentation level, n spaces
75 my $tp_flag; # Flag: .TP paragraph
76 # 1 = this line is .TP label,
77 # 2 = first line of label description.
84 my ($str, $chars) = @_;
86 if (! defined $chars || $chars eq "")
93 $str = $strtok unless defined $str;
94 return undef unless defined $str;
97 $str =~ s/^[$chars]+//;
98 ($result, $strtok) = split /[$chars]+/, $str, 2;
99 ($result, $strtok) = split /[$chars]+/, $strtok, 2 if defined $result && $result eq "";
100 $strtok = undef if ! defined $strtok || $strtok eq "";
106 "node" => undef, # Section name
107 "lname" => undef, # Translated .SH, undef if not translated
109 "heading_level" => undef
113 my $nodes = struct_node();
114 my $cnode; # Current node
116 # Report error in input
120 warn sprintf "man2hlp: %s in file \"%s\" on line %d\n", $message, $c_in, $in_row;
123 # Do open, exit if it fails
126 my ($mode, $filename) = @_;
129 unless (open $f, $mode, $filename)
131 warn sprintf("man2hlp: Cannot open file \"%s\" ($!)\n", $filename);
137 # Do close, exit if it fails
143 warn "man2hlp: Cannot close file ($!)\n";
156 # Calculate the length of string
160 my $anchor_flag = \$static{"string_len anchor_flag"}; # Flag: Inside hypertext anchor name ho4u_v_Ariom
161 my $lc_link_flag = \$static{"string_len lc_link_flag"}; # Flag: Inside hypertext link target name
162 my $backslash_flag = 0; # Flag: Backslash quoting
163 my $len = 0; # Result: the length of the string
166 foreach my $c (split //, $buffer)
168 if ($c eq $CHAR_LINK_POINTER)
170 $$lc_link_flag = 1; # Link target name starts
172 elsif ($c eq $CHAR_LINK_END)
174 $$lc_link_flag = 0; # Link target name ends
176 elsif ($c eq $CHAR_NODE_END)
178 # Node anchor name starts
180 # Ugly hack to prevent loss of one space
183 # Don't add control characters to the length
184 next if ord($c) >= 0 && ord($c) < 32;
185 # Attempt to handle backslash quoting
186 if ($c eq '\\' && !$backslash_flag)
192 # Increase length if not inside anchor name or link target name
193 $len++ if !$$anchor_flag && !$$lc_link_flag;
194 if ($$anchor_flag && $c eq ']')
196 # Node anchor name ends
207 my $len; # The length of current word
208 my $backslash_flag = 0;
211 return if $skip_flag;
215 # Attempt to handle backslash quoting
216 foreach (split //, $buffer)
218 if ($_ eq '\\' && !$backslash_flag)
230 $buffer = strtok($buffer, " \t\n");
231 # Repeat for each word
232 while (defined $buffer)
237 $len = string_len($buffer);
238 # Words are separated by spaces
246 print $f_out ' ' while $col++ < $indentation;
248 # Attempt to handle backslash quoting
249 foreach (split //, $buffer)
251 if ($_ eq '\\' && !$backslash_flag)
263 $buffer = strtok(undef, " \t\n");
268 # Like print_string but with printf-like syntax
271 print_string sprintf shift, @_;
274 # Handle NODE and .SH commands. is_sh is 1 for .SH, 0 for NODE
275 # FIXME: Consider to remove first parameter
278 my ($buffer, $is_sh) = @_;
279 my ($len, $heading_level);
281 # If we already skipped a section, don't skip another
282 $skip_flag = 0 if $skip_flag == 2;
284 # Get the command parameters
285 $buffer = strtok(undef, "");
286 if (! defined $buffer)
288 print_error "Syntax error: .SH: no title";
294 $buffer =~ s/^"// and $buffer =~ s/"$//;
295 # Calculate heading level
297 $heading_level++ while substr($buffer, $heading_level, 1) eq ' ';
298 # Heading level must be even
299 if ($heading_level % 2)
301 print_error "Syntax error: .SH: odd heading level";
305 # Don't start a new section
307 print_string $buffer;
314 # Skipping title and marking text for skipping
319 $buffer = substr($buffer, $heading_level);
320 if (! $is_sh || ! $node)
322 # Start a new section, but omit empty section names
325 printf $f_out "%s[%s]", $CHAR_NODE_END, $buffer;
329 # Add section to the linked list
330 if (! defined $cnode)
336 $cnode->{'next'} = struct_node();
337 $cnode = $cnode->{'next'};
339 $cnode->{'node'} = $buffer;
340 $cnode->{'lname'} = undef;
341 $cnode->{'next'} = undef;
342 $cnode->{'heading_level'} = $heading_level;
346 $cnode->{'lname'} = $buffer;
347 print_string $buffer;
351 } # Start new section
356 # Convert character from the macro name to the font marker
361 'R' => $CHAR_FONT_NORMAL,
362 'B' => $CHAR_FONT_BOLD,
363 'I' => $CHAR_FONT_ITALIC
365 return exists $font{$c} ? $font{$c} : chr(0);
369 # Handle alternate font commands (.BR, .IR, .RB, .RI, .BI, .IB)
370 # Return 0 if the command wasn't recognized, 1 otherwise
372 sub handle_alt_font($)
378 return 0 if length($buffer) != 3;
379 return 0 if substr($buffer, 0, 1) ne '.';
382 char_to_font substr($buffer, 1, 1),
383 char_to_font substr($buffer, 2, 1)
386 # Exclude names with unknown characters, .BB, .II and .RR
387 if ($font[0] eq chr(0) || $font[1] eq chr(0) || $font[0] eq $font[1])
392 my $p = strtok(undef, "");
393 return 1 unless defined $p;
397 my @p = split //, $p;
403 $in_quotes = !$in_quotes;
408 if ($p[0] eq ' ' && !$in_quotes)
411 # Don't change font if we are at the end
414 $alt_state = $alt_state ? 0 : 1;
415 $buffer .= $font[$alt_state];
419 shift @p while $p[0] eq ' ';
427 # Turn off attributes if necessary
428 if ($font[$alt_state] ne $CHAR_FONT_NORMAL)
430 $buffer .= $CHAR_FONT_NORMAL;
433 print_string $buffer;
438 # Handle .IP and .TP commands. is_tp is 1 for .TP, 0 for .IP */
455 # Handle all the roff dot commands. See man groff_man for details
456 sub handle_command($)
461 # Get the command name
462 $buffer = strtok($buffer, " \t");
464 if ($buffer eq ".SH")
467 handle_node $buffer, 1;
469 elsif ($buffer eq ".\\\"NODE")
471 handle_node $buffer, 0;
473 elsif ($buffer eq ".\\\"DONT_SPLIT\"")
477 elsif ($buffer eq ".\\\"SKIP_SECTION\"")
481 elsif ($buffer eq ".\\\"LINK2\"")
483 # Next two input lines form a link
486 elsif ($buffer eq ".PP" || $buffer eq ".P" || $buffer eq ".LP")
493 elsif ($buffer eq ".nf")
495 # Following input lines are to be handled verbatim
499 elsif ($buffer eq ".I" || $buffer eq ".B" || $buffer eq ".SB")
501 # Bold text or italics text
502 my $backslash_flag = 0;
505 # Causes the text on the same line or the text on the
506 # next line to appear in boldface font, one point
507 # size smaller than the default font.
510 # FIXME: text is optional, so there is no error
512 my $p = strtok(undef, "");
515 print_error "Syntax error: .I | .B | .SB : no text";
519 $buffer = substr($buffer, 1, 1) eq 'I' ? $CHAR_FONT_ITALIC : $CHAR_FONT_BOLD;
521 # Attempt to handle backslash quoting
522 my @p = split //, $p;
526 if ($_ eq '\\' && !$backslash_flag)
534 print_string $buffer . $p . $CHAR_FONT_NORMAL;
536 elsif ($buffer eq ".TP")
540 elsif ($buffer eq ".IP")
544 elsif ($buffer eq ".\\\"TOPICS")
548 print_error "Syntax error: .\\\"TOPICS must be first command";
551 $buffer = strtok(undef, "");
552 if (! defined $buffer)
554 print_error "Syntax error: .\\\"TOPICS: no text";
558 $buffer =~ s/^"// and $buffer =~ s/"$//;
561 elsif ($buffer eq ".br")
565 elsif ($buffer =~ /^\.\\"/)
567 # Comment { Hello from K.O. ;-) }
569 elsif ($buffer eq ".TH")
573 elsif ($buffer eq ".SM")
575 # Causes the text on the same line or the text on the
576 # next line to appear in a font that is one point
577 # size smaller than the default font.
578 $buffer = strtok(undef, "");
579 print_string $buffer if defined $buffer;
581 elsif (handle_alt_font($buffer) == 1)
587 # Other commands are ignored
588 print_error sprintf "Warning: unsupported command %s", $buffer;
596 'linkname' => undef, # Section name
597 'line' => undef, # Input line in ...
603 my $links = struct_links();
610 my $old = \$static{"handle_link old"};
617 # Old format link, not supported
619 elsif ($link_flag == 2)
621 # First part of new format link
622 # Bold text or italics text
623 if (substr($buffer, 0, 2) eq '.I' || substr($buffer, 0, 2) eq '.B')
625 $buffer =~ s/^..[\s\t]*//;
631 elsif ($link_flag == 3)
633 # Second part of new format link
639 # "Layout\&)," -- "Layout" should be highlighted, but not "),"
640 ($$old, $amp_arg) = split /\\&/, $$old, 2;
641 $amp_arg = "" unless defined $amp_arg;
642 printf_string "%s%s%s%s%s%s\n", $CHAR_LINK_START, $$old,
643 $CHAR_LINK_POINTER, $buffer, $CHAR_LINK_END, $amp_arg;
645 # Add to the linked list
646 if (defined $current_link)
648 $current_link->{'next'} = struct_links();
649 $current_link = $current_link->{'next'};
650 $current_link->{'next'} = undef;
654 $current_link = $links;
656 $current_link->{'linkname'} = $buffer;
657 $current_link->{'filename'} = $c_in;
658 $current_link->{'line'} = $in_row;
664 my $len; # Length of input line
665 my $c_man; # Manual filename
666 my $c_tmpl; # Template filename
667 my $f_man; # Manual file
668 my $f_tmpl; # Template file
669 my $buffer; # Full input line
671 my $outfile_buffer; # Large buffer to keep the output file
672 my $cont_start; # Start of [Contents]
673 my $file_end; # Length of the output file
675 # Validity check for arguments
678 warn "Usage: man2hlp file.man template_file helpfile\n";
686 # First stage - process the manual, write to the output file
688 $f_man = fopen_check "<", $c_man;
689 $f_out = fopen_check ">", $c_out;
692 # Repeat for each input line
695 # Remove terminating newline
698 my $input_line; # Input line without initial "\&"
700 if (substr($buffer, 0, 2) eq '\\&')
702 $input_line = substr($buffer, 2);
706 $input_line = $buffer;
710 $len = length($input_line);
714 # Copy the line verbatim
715 if ($input_line eq ".fi")
721 print_string $input_line;
728 handle_link $input_line;
730 elsif (substr($buffer, 0, 1) eq '.')
732 # The line is a roff command
733 handle_command $input_line;
737 #A normal line, just output it
738 print_string $input_line;
740 # .TP label processed as usual line
751 if ($col >= $indentation)
757 print $f_out " " while ++$col < $indentation;
765 # First stage ends here, closing the manual
767 # Second stage - process the template file
768 $f_tmpl = fopen_check "<", $c_tmpl;
771 # Repeat for each input line
776 if (defined $lc_node)
780 $cnode->{'lname'} = $buffer;
781 chomp $cnode->{'lname'};
787 my $char_node_end = index($buffer, $CHAR_NODE_END);
788 $lc_node = $char_node_end < 0 ? undef : substr($buffer, $char_node_end);
790 if (defined $lc_node && substr($lc_node, 1, 1) eq '[')
792 my $p = index($lc_node, ']');
794 if (substr($lc_node, 1, 6) eq '[main]')
800 if (! defined $cnode)
806 $cnode->{'next'} = struct_node();
807 $cnode = $cnode->{'next'};
809 $cnode->{'node'} = substr($lc_node, 2, $p-2);
810 $cnode->{'lname'} = undef;
811 $cnode->{'next'} = undef;
812 $cnode->{'heading_level'} = 0;
825 print $f_out $buffer;
828 $cont_start = tell $f_out;
829 if ($cont_start <= 0)
837 printf $f_out "\004[Contents]\n%s\n\n", $topics;
841 print $f_out "\004[Contents]\n";
844 for ($current_link = $links; defined $current_link && defined $current_link->{'linkname'};)
847 my $next = $current_link->{'next'};
849 if ($current_link->{'linkname'} eq "Contents")
855 for ($cnode = $nodes; defined $cnode && defined $cnode->{'node'}; $cnode = $cnode->{'next'})
857 if ($cnode->{'node'} eq $current_link->{'linkname'})
866 $buffer = sprintf "Stale link \"%s\"", $current_link->{'linkname'};
867 $c_in = $current_link->{'filename'};
868 $in_row = $current_link->{'line'};
872 $current_link = $next;
875 for ($cnode = $nodes; defined $cnode && defined $cnode->{'node'};)
877 my $next = $cnode->{'next'};
878 $lc_node = $cnode->{'node'};
880 if (defined $lc_node && $lc_node ne '') {
881 printf $f_out " %*s\001%s\002%s\003", $cnode->{'heading_level'},
882 "", $cnode->{'lname'} ? $cnode->{'lname'} : $lc_node, $lc_node;
888 $file_end = tell $f_out;
891 if (($file_end <= 0) || ($file_end - $cont_start <= 0))
898 fclose_check $f_tmpl;
899 # Second stage ends here, closing all files, note the end of output
902 # Third stage - swap two parts of the output file.
903 # First, open the output file for reading and load it into the memory.
905 ## TODO: replace writing to f_out by writing to a string
906 $outfile_buffer = '';
907 $f_out = fopen_check '<', $c_out;
908 $outfile_buffer .= $_ while <$f_out>;
910 # Now the output file is in the memory
912 # Again open output file for writing
913 $f_out = fopen_check '>', $c_out;
915 # Write part after the "Contents" node
916 print $f_out substr($outfile_buffer, $cont_start, $file_end - $cont_start);
918 # Write part before the "Contents" node
919 print $f_out substr($outfile_buffer, 0, $cont_start-1);