master xplshn/aruu / cmd / posix / sh / mkbuiltins
  1#!/bin/sh -
  2
  3#-
  4# Copyright (c) 1991, 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# Kenneth Almquist.
  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
 34temp=`mktemp -t ka.XXXXXX`
 35srcdir=$1
 36havejobs=0
 37if grep '^#define[	 ]*JOBS[	 ]*1' $srcdir/shell.h > /dev/null
 38then	havejobs=1
 39fi
 40
 41feature_histedit=0
 42feature_local=1
 43feature_let=1
 44feature_ulimit=1
 45feature_setvar=1
 46feature_wordexp=1
 47feature_easter=1
 48
 49if [ -f ../../../scripts/mk/config.kv ]; then
 50	. ../../../scripts/mk/config.kv
 51	feature_histedit=$FEATURE_SH_HISTEDIT
 52	feature_local=$FEATURE_SH_LOCAL
 53	feature_let=$FEATURE_SH_LET
 54	feature_ulimit=$FEATURE_SH_ULIMIT
 55	feature_setvar=$FEATURE_SH_SETVAR
 56	feature_wordexp=$FEATURE_SH_WORDEXP
 57	feature_easter=$FEATURE_SH_EASTER
 58fi
 59
 60exec > builtins.c
 61cat <<\!
 62/*
 63 * This file was generated by the mkbuiltins program.
 64 */
 65
 66#include <stdlib.h>
 67#include "shell.h"
 68#include "builtins.h"
 69
 70!
 71awk '/^[^#]/ {
 72	if ($0 ~ /#FEATURE_SH_HISTEDIT/ && '$feature_histedit' != 1) next;
 73	if ($0 ~ /#FEATURE_SH_LOCAL/ && '$feature_local' != 1) next;
 74	if ($0 ~ /#FEATURE_SH_LET/ && '$feature_let' != 1) next;
 75	if ($0 ~ /#FEATURE_SH_ULIMIT/ && '$feature_ulimit' != 1) next;
 76	if ($0 ~ /#FEATURE_SH_SETVAR/ && '$feature_setvar' != 1) next;
 77	if ($0 ~ /#FEATURE_SH_WORDEXP/ && '$feature_wordexp' != 1) next;
 78	if ($0 ~ /#FEATURE_SH_EASTER/ && '$feature_easter' != 1) next;
 79
 80	if ('$havejobs' || $2 != "-j") {
 81		sub(/#[A-Z0-9_]*/, "");
 82		print $0
 83	}
 84}' $srcdir/builtins.def | sed 's/-j//' > $temp
 85echo 'int (*const builtinfunc[])(int, char **) = {'
 86awk '/^[^#]/ {	printf "\t%s,\n", $1}' $temp
 87echo '};
 88
 89const unsigned char builtincmd[] = {'
 90awk '{	for (i = 2 ; i <= NF ; i++) {
 91		if ($i == "-s") {
 92			spc = 1;
 93		} else if ($i == "-n") {
 94			# Handled later for builtins.h
 95			continue
 96		} else {
 97			printf "\t\"\\%03o\\%03o%s\"\n", length($i), (spc ? 128 : 0) + NR-1, $i
 98			spc = 0;
 99		}
100	}}' $temp
101echo '};'
102
103exec > builtins.h
104cat <<\!
105/*
106 * This file was generated by the mkbuiltins program.
107 */
108
109!
110tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ < $temp |
111	awk '{	printf "#define %s %d\n", $1, NR-1}'
112echo '
113#define BUILTIN_SPECIAL 0x80
114
115extern int (*const builtinfunc[])(int, char **);
116extern const unsigned char builtincmd[];
117'
118awk '{	printf "int %s(int, char **);\n", $1}' $temp
119
120# Build safe_builtin_always()
121cat <<EOF
122
123static inline int
124safe_builtin_always(int idx)
125{
126EOF
127awk '
128BEGIN { printed = 0 }
129{
130	for (i = 2 ; i <= NF ; i++) {
131		if ($i == "-s") {
132			continue
133		} else if ($i == "-n") {
134			nofork = 1;
135		} else {
136			if (nofork == 0) {
137				continue
138			}
139			if (printed == 1) {
140				printf " || \n\t    "
141			} else {
142				printf "\tif ("
143			}
144			printf "idx == " toupper($1)
145			printed = 1
146			nofork = 0;
147			# Only need to check each once
148			break
149		}
150	}
151}' $temp
152
153cat << EOF
154)
155		return (1);
156	return(0);
157}
158EOF
159
160rm -f $temp