5c53e89761325416f32fdbdd7bae995a5e32c472
[dragonfly.git] / lib / libedit / makelist
1 #!/bin/sh -
2 #
3 # Copyright (c) 1992, 1993
4 #       The Regents of the University of California.  All rights reserved.
5 #
6 # This code is derived from software contributed to Berkeley by
7 # Christos Zoulas of Cornell University.
8 #
9 # Redistribution and use in source and binary forms, with or without
10 # modification, are permitted provided that the following conditions
11 # are met:
12 # 1. Redistributions of source code must retain the above copyright
13 #    notice, this list of conditions and the following disclaimer.
14 # 2. Redistributions in binary form must reproduce the above copyright
15 #    notice, this list of conditions and the following disclaimer in the
16 #    documentation and/or other materials provided with the distribution.
17 # 3. Neither the name of the University nor the names of its contributors
18 #    may be used to endorse or promote products derived from this software
19 #    without specific prior written permission.
20 #
21 # THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 # ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 # SUCH DAMAGE.
32 #
33 # @(#)makelist  5.3 (Berkeley) 6/4/93
34 # $NetBSD: makelist,v 1.9 2005/05/16 13:14:43 lukem Exp $
35 # $DragonFly: src/lib/libedit/makelist,v 1.4 2005/11/13 11:58:30 corecode Exp $
36
37 # makelist.sh: Automatically generate header files...
38
39 AWK=awk
40 USAGE="Usage: $0 -h|-e|-fc|-fh|-bc|-bh|-m <filenames>"
41
42 if [ "x$1" = "x" ]
43 then
44     echo $USAGE 1>&2
45     exit 1
46 fi
47
48 FLAG="$1"
49 shift
50
51 FILES="$@"
52
53 case $FLAG in
54
55 #       generate foo.h file from foo.c
56 #
57 -h)
58     set - `echo $FILES | sed -e 's/\\./_/g'`
59     hdr="_h_`basename $1`"
60     cat $FILES | $AWK '
61         BEGIN {
62             printf("/* Automatically generated file, do not edit */\n");
63             printf("#ifndef %s\n#define %s\n", "'$hdr'", "'$hdr'");
64         }
65         /\(\):/ {
66             pr = substr($2, 1, 2);
67             if (pr == "vi" || pr == "em" || pr == "ed") {
68                 name = substr($2, 1, length($2) - 3);
69 #
70 # XXX:  need a space between name and prototype so that -fc and -fh
71 #       parsing is much easier
72 #
73                 printf("protected el_action_t\t%s (EditLine *, int);\n", name);
74             }
75         }
76         END {
77             printf("#endif /* %s */\n", "'$hdr'");
78         }'
79         ;;
80
81 #       generate help.c from various .c files
82 #
83 -bc)
84     cat $FILES | $AWK '
85         BEGIN {
86             printf("/* Automatically generated file, do not edit */\n");
87             printf("#include \"sys.h\"\n#include \"el.h\"\n");
88             printf("private const struct el_bindings_t el_func_help[] = {\n");
89             low = "abcdefghijklmnopqrstuvwxyz_";
90             high = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_";
91             for (i = 1; i <= length(low); i++)
92                 tr[substr(low, i, 1)] = substr(high, i, 1);
93         }
94         /\(\):/ {
95             pr = substr($2, 1, 2);
96             if (pr == "vi" || pr == "em" || pr == "ed") {
97                 name = substr($2, 1, length($2) - 3);
98                 uname = "";
99                 fname = "";
100                 for (i = 1; i <= length(name); i++) {
101                     s = substr(name, i, 1);
102                     uname = uname tr[s];
103                     if (s == "_")
104                         s = "-";
105                     fname = fname s;
106                 }
107
108                 printf("    { %-30.30s %-30.30s\n","\"" fname "\",", uname ",");
109                 ok = 1;
110             }
111         }
112         /^ \*/ {
113             if (ok) {
114                 printf("      \"");
115                 for (i = 2; i < NF; i++)
116                     printf("%s ", $i);
117                 printf("%s\" },\n", $i);
118                 ok = 0;
119             }
120         }
121         END {
122             printf("    { NULL, 0, NULL }\n");
123             printf("};\n");
124             printf("\nprotected const el_bindings_t* help__get()");
125             printf("{ return el_func_help; }\n");
126         }'
127         ;;
128
129 #       generate help.h from various .c files
130 #
131 -bh)
132     $AWK '
133         BEGIN {
134             printf("/* Automatically generated file, do not edit */\n");
135             printf("#ifndef _h_help_c\n#define _h_help_c\n");
136             printf("protected const el_bindings_t *help__get(void);\n");
137             printf("#endif /* _h_help_c */\n");
138         }' /dev/null
139         ;;
140
141 #       generate fcns.h from various .h files
142 #
143 -fh)
144     cat $FILES | $AWK '/el_action_t/ { print $3 }' | \
145     sort | tr '[a-z]' '[A-Z]' | $AWK '
146         BEGIN {
147             printf("/* Automatically generated file, do not edit */\n");
148             printf("#ifndef _h_fcns_c\n#define _h_fcns_c\n");
149             count = 0;
150         }
151         {
152             printf("#define\t%-30.30s\t%3d\n", $1, count++);
153         }
154         END {
155             printf("#define\t%-30.30s\t%3d\n", "EL_NUM_FCNS", count);
156
157             printf("typedef el_action_t (*el_func_t)(EditLine *, int);");
158             printf("\nprotected const el_func_t* func__get(void);\n");
159             printf("#endif /* _h_fcns_c */\n");
160         }'
161         ;;
162
163 #       generate fcns.c from various .h files
164 #
165 -fc)
166     cat $FILES | $AWK '/el_action_t/ { print $3 }' | sort | $AWK '
167         BEGIN {
168             printf("/* Automatically generated file, do not edit */\n");
169             printf("#include \"sys.h\"\n#include \"el.h\"\n");
170             printf("private const el_func_t el_func[] = {");
171             maxlen = 80;
172             needn = 1;
173             len = 0;
174         }
175         {
176             clen = 25 + 2;
177             len += clen;
178             if (len >= maxlen)
179                 needn = 1;
180             if (needn) {
181                 printf("\n    ");
182                 needn = 0;
183                 len = 4 + clen;
184             }
185             s = $1 ",";
186             printf("%-26.26s ", s);
187         }
188         END {
189             printf("\n};\n");
190             printf("\nprotected const el_func_t* func__get() { return el_func; }\n");
191         }'
192         ;;
193
194 #       generate editline.c from various .c files
195 #
196 -e)
197         echo "$FILES" | tr ' ' '\012' | $AWK '
198         BEGIN {
199             printf("/* Automatically generated file, do not edit */\n");
200             printf("#define protected static\n");
201             printf("#define SCCSID\n");
202         }
203         {
204             printf("#include \"%s\"\n", $1);
205         }'
206         ;;
207
208 #       generate man page fragment from various .c files
209 #
210 -m)
211     cat $FILES | $AWK '
212         BEGIN {
213             printf(".\\\" Section automatically generated with makelist\n");
214             printf(".Bl -tag -width 4n\n");
215         }
216         /\(\):/ {
217             pr = substr($2, 1, 2);
218             if (pr == "vi" || pr == "em" || pr == "ed") {
219                 name = substr($2, 1, length($2) - 3);
220                 fname = "";
221                 for (i = 1; i <= length(name); i++) {
222                     s = substr(name, i, 1);
223                     if (s == "_")
224                         s = "-";
225                     fname = fname s;
226                 }
227
228                 printf(".It Ic %s\n", fname);
229                 ok = 1;
230             }
231         }
232         /^ \*/ {
233             if (ok) {
234                 for (i = 2; i < NF; i++)
235                     printf("%s ", $i);
236                 printf("%s.\n", $i);
237                 ok = 0;
238             }
239         }
240         END {
241             printf(".El\n");
242             printf(".\\\" End of section automatically generated with makelist\n");
243         }'
244         ;;
245
246 *)
247     echo $USAGE 1>&2
248     exit 1
249     ;;
250
251 esac