makelist revision 84260
1193323Sed#!/bin/sh -
2193323Sed#	$NetBSD: makelist,v 1.6 2000/09/04 23:45:18 lukem Exp $
3193323Sed# $FreeBSD: head/lib/libedit/makelist 84260 2001-10-01 08:41:27Z obrien $
4193323Sed#
5193323Sed# Copyright (c) 1992, 1993
6193323Sed#	The Regents of the University of California.  All rights reserved.
7193323Sed#
8193323Sed# This code is derived from software contributed to Berkeley by
9193323Sed# Christos Zoulas of Cornell University.
10193323Sed#
11193323Sed# Redistribution and use in source and binary forms, with or without
12193323Sed# modification, are permitted provided that the following conditions
13193323Sed# are met:
14193323Sed# 1. Redistributions of source code must retain the above copyright
15193323Sed#    notice, this list of conditions and the following disclaimer.
16193323Sed# 2. Redistributions in binary form must reproduce the above copyright
17193323Sed#    notice, this list of conditions and the following disclaimer in the
18193323Sed#    documentation and/or other materials provided with the distribution.
19193323Sed# 3. All advertising materials mentioning features or use of this software
20193323Sed#    must display the following acknowledgement:
21193323Sed#	This product includes software developed by the University of
22193323Sed#	California, Berkeley and its contributors.
23193323Sed# 4. Neither the name of the University nor the names of its contributors
24193323Sed#    may be used to endorse or promote products derived from this software
25193323Sed#    without specific prior written permission.
26193323Sed#
27193323Sed# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28193323Sed# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29193323Sed# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30193323Sed# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31193323Sed# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32193323Sed# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33193323Sed# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34193323Sed# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35193323Sed# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36193323Sed# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37193323Sed# SUCH DAMAGE.
38193323Sed#
39193323Sed#	@(#)makelist	5.3 (Berkeley) 6/4/93
40193323Sed
41193323Sed# makelist.sh: Automatically generate header files...
42193323Sed
43193323SedAWK=/usr/bin/awk
44193323SedUSAGE="Usage: $0 -h|-e|-fc|-fh|-bc|-bh|-m <filenames>"
45193323Sed
46193323Sedif [ "x$1" = "x" ]
47193323Sedthen
48193323Sed    echo $USAGE 1>&2
49193323Sed    exit 1
50193323Sedfi
51193323Sed
52193323SedFLAG="$1"
53193323Sedshift
54193323Sed
55193323SedFILES="$@"
56193323Sed
57193323Sedcase $FLAG in
58193323Sed
59193323Sed#	generate foo.h file from foo.c
60193323Sed#
61193323Sed-h)
62195098Sed    set - `echo $FILES | sed -e 's/\\./_/g'`
63195098Sed    hdr="_h_`basename $1`"
64193323Sed    cat $FILES | $AWK '
65193323Sed	BEGIN {
66193323Sed	    printf("/* Automatically generated file, do not edit */\n");
67193323Sed	    printf("#ifndef %s\n#define %s\n", "'$hdr'", "'$hdr'");
68193323Sed	}
69193323Sed	/\(\):/ {
70193323Sed	    pr = substr($2, 1, 2);
71193323Sed	    if (pr == "vi" || pr == "em" || pr == "ed") {
72193323Sed		name = substr($2, 1, length($2) - 3);
73193323Sed#
74193323Sed# XXX:	need a space between name and prototype so that -fc and -fh
75193323Sed#	parsing is much easier
76193323Sed#
77195098Sed		printf("protected el_action_t\t%s (EditLine *, int);\n", name);
78195098Sed	    }
79193323Sed	}
80193323Sed	END {
81193323Sed	    printf("#endif /* %s */\n", "'$hdr'");
82193323Sed	}'
83193323Sed	;;
84193323Sed
85193323Sed#	generate help.c from various .c files
86193323Sed#
87193323Sed-bc)
88193323Sed    cat $FILES | $AWK '
89195098Sed	BEGIN {
90193323Sed	    printf("/* Automatically generated file, do not edit */\n");
91193323Sed	    printf("#include \"sys.h\"\n#include \"el.h\"\n");
92193323Sed	    printf("private const struct el_bindings_t el_func_help[] = {\n");
93195098Sed	    low = "abcdefghijklmnopqrstuvwxyz_";
94193323Sed	    high = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_";
95193323Sed	    for (i = 1; i <= length(low); i++)
96193323Sed		tr[substr(low, i, 1)] = substr(high, i, 1);
97193323Sed	}
98193323Sed	/\(\):/ {
99193323Sed	    pr = substr($2, 1, 2);
100	    if (pr == "vi" || pr == "em" || pr == "ed") {
101		name = substr($2, 1, length($2) - 3);
102		uname = "";
103		fname = "";
104		for (i = 1; i <= length(name); i++) {
105		    s = substr(name, i, 1);
106		    uname = uname tr[s];
107		    if (s == "_")
108			s = "-";
109		    fname = fname s;
110		}
111
112		printf("    { %-30.30s %-30.30s\n","\"" fname "\",", uname ",");
113		ok = 1;
114	    }
115	}
116	/^ \*/ {
117	    if (ok) {
118		printf("      \"");
119		for (i = 2; i < NF; i++)
120		    printf("%s ", $i);
121		printf("%s\" },\n", $i);
122		ok = 0;
123	    }
124	}
125	END {
126	    printf("    { NULL, 0, NULL }\n");
127	    printf("};\n");
128	    printf("\nprotected const el_bindings_t* help__get()");
129	    printf("{ return el_func_help; }\n");
130	}'
131	;;
132
133#	generate help.h from various .c files
134#
135-bh)
136    $AWK '
137	BEGIN {
138	    printf("/* Automatically generated file, do not edit */\n");
139	    printf("#ifndef _h_help_c\n#define _h_help_c\n");
140	    printf("protected const el_bindings_t *help__get(void);\n");
141	    printf("#endif /* _h_help_c */\n");
142	}' /dev/null
143	;;
144
145#	generate fcns.h from various .h files
146#
147-fh)
148    cat $FILES | $AWK '/el_action_t/ { print $3 }' | \
149    sort | tr '[a-z]' '[A-Z]' | $AWK '
150	BEGIN {
151	    printf("/* Automatically generated file, do not edit */\n");
152	    printf("#ifndef _h_fcns_c\n#define _h_fcns_c\n");
153	    count = 0;
154	}
155	{
156	    printf("#define\t%-30.30s\t%3d\n", $1, count++);
157	}
158	END {
159	    printf("#define\t%-30.30s\t%3d\n", "EL_NUM_FCNS", count);
160
161	    printf("typedef el_action_t (*el_func_t)(EditLine *, int);");
162	    printf("\nprotected const el_func_t* func__get(void);\n");
163	    printf("#endif /* _h_fcns_c */\n");
164	}'
165	;;
166
167#	generate fcns.c from various .h files
168#
169-fc)
170    cat $FILES | $AWK '/el_action_t/ { print $3 }' | sort | $AWK '
171	BEGIN {
172	    printf("/* Automatically generated file, do not edit */\n");
173	    printf("#include \"sys.h\"\n#include \"el.h\"\n");
174	    printf("private const el_func_t el_func[] = {");
175	    maxlen = 80;
176	    needn = 1;
177	    len = 0;
178	}
179	{
180	    clen = 25 + 2;
181	    len += clen;
182	    if (len >= maxlen)
183		needn = 1;
184	    if (needn) {
185		printf("\n    ");
186		needn = 0;
187		len = 4 + clen;
188	    }
189	    s = $1 ",";
190	    printf("%-26.26s ", s);
191	}
192	END {
193	    printf("\n};\n");
194	    printf("\nprotected const el_func_t* func__get() { return el_func; }\n");
195	}'
196	;;
197
198#	generate editline.c from various .c files
199#
200-e)
201	echo "$FILES" | tr ' ' '\012' | $AWK '
202	BEGIN {
203	    printf("/* Automatically generated file, do not edit */\n");
204	    printf("#define protected static\n");
205	    printf("#define SCCSID\n");
206	}
207	{
208	    printf("#include \"%s\"\n", $1);
209	}'
210	;;
211
212#	generate man page fragment from various .c files
213#
214-m)
215    cat $FILES | $AWK '
216	BEGIN {
217	    printf(".\\\" Section automatically generated with makelist\n");
218	    printf(".Bl -tag -width 4n\n");
219	}
220	/\(\):/ {
221	    pr = substr($2, 1, 2);
222	    if (pr == "vi" || pr == "em" || pr == "ed") {
223		name = substr($2, 1, length($2) - 3);
224		fname = "";
225		for (i = 1; i <= length(name); i++) {
226		    s = substr(name, i, 1);
227		    if (s == "_")
228			s = "-";
229		    fname = fname s;
230		}
231
232		printf(".It Ic %s\n", fname);
233		ok = 1;
234	    }
235	}
236	/^ \*/ {
237	    if (ok) {
238		for (i = 2; i < NF; i++)
239		    printf("%s ", $i);
240		printf("%s.\n", $i);
241		ok = 0;
242	    }
243	}
244	END {
245	    printf(".El\n");
246	    printf(".\\\" End of section automatically generated with makelist\n");
247	}'
248	;;
249
250*)
251    echo $USAGE 1>&2
252    exit 1
253    ;;
254
255esac
256