makelist revision 118427
1#!/bin/sh -
2#	$NetBSD: makelist,v 1.6 2000/09/04 23:45:18 lukem Exp $
3# $FreeBSD: head/lib/libedit/makelist 118427 2003-08-04 14:10:33Z ache $
4#
5# Copyright (c) 1992, 1993
6#	The Regents of the University of California.  All rights reserved.
7#
8# This code is derived from software contributed to Berkeley by
9# Christos Zoulas of Cornell University.
10#
11# Redistribution and use in source and binary forms, with or without
12# modification, are permitted provided that the following conditions
13# are met:
14# 1. Redistributions of source code must retain the above copyright
15#    notice, this list of conditions and the following disclaimer.
16# 2. Redistributions in binary form must reproduce the above copyright
17#    notice, this list of conditions and the following disclaimer in the
18#    documentation and/or other materials provided with the distribution.
19# 3. All advertising materials mentioning features or use of this software
20#    must display the following acknowledgement:
21#	This product includes software developed by the University of
22#	California, Berkeley and its contributors.
23# 4. Neither the name of the University nor the names of its contributors
24#    may be used to endorse or promote products derived from this software
25#    without specific prior written permission.
26#
27# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37# SUCH DAMAGE.
38#
39#	@(#)makelist	5.3 (Berkeley) 6/4/93
40
41# makelist.sh: Automatically generate header files...
42
43AWK=/usr/bin/awk
44USAGE="usage: $0 -h|-e|-fc|-fh|-bc|-bh|-m <filenames>"
45
46if [ "x$1" = "x" ]
47then
48    echo $USAGE 1>&2
49    exit 1
50fi
51
52FLAG="$1"
53shift
54
55FILES="$@"
56
57case $FLAG in
58
59#	generate foo.h file from foo.c
60#
61-h)
62    set - `echo $FILES | sed -e 's/\\./_/g'`
63    hdr="_h_`basename $1`"
64    cat $FILES | $AWK '
65	BEGIN {
66	    printf("/* Automatically generated file, do not edit */\n");
67	    printf("#ifndef %s\n#define %s\n", "'$hdr'", "'$hdr'");
68	}
69	/\(\):/ {
70	    pr = substr($2, 1, 2);
71	    if (pr == "vi" || pr == "em" || pr == "ed") {
72		name = substr($2, 1, length($2) - 3);
73#
74# XXX:	need a space between name and prototype so that -fc and -fh
75#	parsing is much easier
76#
77		printf("protected el_action_t\t%s (EditLine *, int);\n", name);
78	    }
79	}
80	END {
81	    printf("#endif /* %s */\n", "'$hdr'");
82	}'
83	;;
84
85#	generate help.c from various .c files
86#
87-bc)
88    cat $FILES | $AWK '
89	BEGIN {
90	    printf("/* Automatically generated file, do not edit */\n");
91	    printf("#include \"sys.h\"\n#include \"el.h\"\n");
92	    printf("private const struct el_bindings_t el_func_help[] = {\n");
93	    low = "abcdefghijklmnopqrstuvwxyz_";
94	    high = "ABCDEFGHIJKLMNOPQRSTUVWXYZ_";
95	    for (i = 1; i <= length(low); i++)
96		tr[substr(low, i, 1)] = substr(high, i, 1);
97	}
98	/\(\):/ {
99	    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 '[:lower:]' '[:upper:]' | $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