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