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