Merge Irssi 0.8.16-rc1
[silc.git] / apps / irssi / syntax.pl
1 #!/usr/bin/perl
2 #
3 # This script reads the syntaces of commands from irssi source tree.
4 # Then it browses through all '.in' files in the current directory and
5 # substitutes '@SYNTAX:foo@' tags with real syntaces found. This data
6 # is written into the corresponding files without the '.in' extension.
7 # For example:  help.in -> ../help
8 #
9 # This path has to be changed. It should point to your irssi/src directory
10 # Remember to include the asterisk ('*').
11 $SRC_PATH='src';
12
13 @files = `find src -name '*.c'`;
14
15 foreach $file (@files) {
16    open (FILE, "$file");
17    while (<FILE>) {
18       chomp;
19       if (m!/\*.SYNTAX\:! || $state) {
20          s/^\s+/ /;
21          s/.*SYNTAX: //;
22          if (/^ [A-Z]+/) {
23             push @lines, $line;
24             $line = "";
25             s/^ //;
26          }
27          $line .= $_;
28          if (m!\*/!) {
29             $line =~ s!\*/!!;
30             push @lines, $line;
31             $line = "";
32             $state = 0;
33          } else {
34             $state = 1;
35          }
36       }
37    }
38    close (FILE);
39 }
40 while (<docs/help/in/*.in>) {
41    next if (/Makefile/);
42
43    open (FILE, "$_");
44    @data = <FILE>;
45    close (FILE);
46    $count = 0;
47    foreach $DATARIVI (@data) {
48       if ($DATARIVI =~ /\@SYNTAX\:(.+)\@/) {
49           $SYNTAX = join "\n", (grep /^\U$1 /, @lines);
50           $SYNTAX .= "\n" if $SYNTAX;
51           $SYNTAX =~ s/ *$//; $SYNTAX =~ s/ *\n/\n/g;
52
53           # add %| after "COMMAND SUB " so parameters will indent correctly
54           $SYNTAX =~ s/^([A-Z ]+)/\1%|/;
55           $SYNTAX =~ s/(\n[A-Z ]+)/\1%|/g;
56           # no need for this if there's no parameters
57           $SYNTAX =~ s/%\|$//;
58           $DATARIVI = $SYNTAX;
59       } elsif ($DATARIVI =~ /^\S+/) {
60         if ($data[$count+1] =~ /^\S+/) {
61           chomp $DATARIVI;
62           $DATARIVI =~ s/ *$//g;
63           $DATARIVI .= " ";
64         }
65       } else {
66           $DATARIVI =~ s/^\t/         / while ($DATARIVI =~ /^\t/);
67       }
68       $count++;
69    }
70
71    # must always end with empty line
72    push @data, "\n" if ($data[@data-1] ne "\n");
73    push @data, "\n" if ($data[@data-2] !~ /\n$/);
74
75    $newfilename = $_; $newfilename =~ s/\.in$//;
76    $newfilename =~ s/\/in\//\//;
77    open (NEWFILE, ">$newfilename");
78    print NEWFILE @data;
79    close (NEWFILE);
80 }