]> jfr.im git - irc/quakenet/newserv.git/blob - chanserv/mkcommandlist.pl
BUILD: add require-all build mode
[irc/quakenet/newserv.git] / chanserv / mkcommandlist.pl
1 #!/usr/bin/perl -w
2
3 use strict;
4
5 my @cmdnames;
6 my @cmdlevels;
7 my @cmdargs;
8 my @cmddesc;
9 my @cmdfunc;
10 my @protos;
11 my @files;
12 my @help;
13 my @cmdaliases;
14
15 my @filelist = <*.c>;
16
17 my $modname;
18
19 my %cmdhash;
20
21 unless (@ARGV) {
22 print "Usage: $0 <module name>\n";
23 exit(0);
24 } else {
25 $modname=$ARGV[0];
26 }
27
28 my $rootpath;
29 if($#ARGV >= 1) {
30 $rootpath=$ARGV[1];
31 } else {
32 $rootpath="..";
33 }
34
35 my $smallname;
36 $smallname=$modname;
37 $smallname=~s/\.so$//;
38
39 my $cname;
40 $cname=$smallname . ".c";
41 $smallname=~s/^chanserv_//;
42
43
44 for (@filelist) {
45 next if (/commandlist.c/);
46 next if ($_ eq $cname);
47
48 my $fname = $_;
49 my ($cn, $cl, $ca, $cd, $cf, $cp, $ch, $cal);
50 $ch="";
51 $cal="";
52
53 open INFILE,"<$fname";
54
55 while(<INFILE>) {
56 chomp;
57
58 if (/CMDNAME: (.*)/) {
59 $cn=$1;
60 }
61
62 if (/CMDLEVEL: (.*)/) {
63 $cl=$1;
64 }
65
66 if (/CMDARGS: (.*)/) {
67 $ca=$1;
68 }
69
70 if (/CMDDESC: (.*)/) {
71 $cd=$1;
72 }
73
74 if (/CMDFUNC: (.*)/) {
75 $cf=$1;
76 }
77
78 if (/CMDPROTO: (.*)/) {
79 $cp=$1;
80 }
81
82 if (/CMDHELP: (.*)/) {
83 $ch.=$1."\\n";
84 }
85
86 if (/CMDALIASES: (.*)/) {
87 $cal=$1;
88 }
89 }
90
91 if (defined $cn and defined $cl and defined $ca and defined $cd and defined $cf and defined $cp) {
92 # valid command found
93 $cmdhash{$cn} = scalar @cmdnames;
94
95 push @files, $fname;
96 push @cmdnames, $cn;
97 push @cmdlevels, $cl;
98 push @cmdargs, $ca;
99 push @cmddesc, $cd;
100 push @cmdfunc, $cf;
101 push @protos, $cp;
102 push @help, $ch;
103 push @cmdaliases, $cal;
104 } else {
105 print "Warning: found source file $fname without complete tags, skipping...\n";
106 }
107 }
108
109 if (@files < 1) {
110 print "No commands found - are you in the right directory?\n";
111 print "Exiting before I destroy something important.\n";
112 exit(0);
113 }
114
115
116 open CL, ">commandlist.c";
117
118 print CL "/* Automatically generated by mkcommandlist.pl, do not edit. */\n\n";
119 print CL "#include \"".$rootpath."/chanserv.h\"\n\n";
120
121 # Print prototypes
122 print CL "/* Prototypes */\n";
123 foreach (@protos) {
124 print CL "$_\n";
125 }
126
127 print CL "void ".$smallname."_init(void);\n";
128 print CL "void ".$smallname."_fini(void);\n\n";
129
130 print CL "\nvoid _init() {\n";
131 print CL " ".$smallname."_init();\n";
132
133 sub generate_help_text {
134 my ($cn, $ch) = @_;
135
136 my %SUBS;
137
138 $SUBS{"COMMAND"} = $cn;
139 $SUBS{"UCOMMAND"} = uc($cn);
140
141 my %realsubs;
142 while(my ($key, $value) = each(%SUBS)) {
143 $key = '@'.quotemeta($key).'@';
144 $value = quotemeta($value);
145 $ch =~ s/$key/$value/g;
146 }
147
148 return $ch;
149 }
150
151 sub writecmd {
152 my ($cn, $i, $isalias) = @_;
153
154 my $cl = $cmdlevels[$i];
155 if ($isalias) {
156 $cl .= " | " if ($cl ne "");
157 $cl .= "QCMD_ALIAS";
158 }
159
160 print CL " chanservaddcommand(\"".$cn."\", ".($cl).", ".($cmdargs[$i]).", ";
161 print CL ($cmdfunc[$i]).", \"".($cmddesc[$i])."\", \"".(generate_help_text($cn,$help[$i])),"\");\n";
162 }
163
164 for (my $i=0;$i<scalar @cmdnames;$i++) {
165 writecmd($cmdnames[$i], $i, 0);
166
167 foreach (split / /, $cmdaliases[$i]) {
168 writecmd($_, $i, 1);
169 }
170 }
171
172 print CL "}\n\nvoid _fini() {\n";
173 print CL " ".$smallname."_fini();\n";
174
175 sub writercmd {
176 my ($cn, $i) = @_;
177 print CL " chanservremovecommand(\"".$cn."\", ".($cmdfunc[$i]).");\n";
178 }
179
180 for (my $i=0;$i<scalar @cmdnames;$i++) {
181 writercmd($cmdnames[$i], $i);
182
183 foreach (split / /, $cmdaliases[$i]) {
184 writercmd($_, $i);
185 }
186 }
187
188 print CL "}\n";
189
190 close CL;
191
192 open MF,">.autobuild.mk";
193
194 print MF "# Automatically generated Makefile, do not edit.\n";
195
196 print MF "\n$modname: ";
197
198 push @files,"commandlist.c";
199
200 push @files,$cname;
201
202 foreach (@files) {
203 s/.c$/.o/;
204 print MF "$_ ";
205 }
206
207 print MF "\n";
208
209 close MF;