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