xref: /netbsd-src/bin/sh/mktokens (revision 0827e1f954e8b2f9629bd2144f99e35ea6d92a54)
161f28255Scgd#!/bin/sh -
2*0827e1f9Skre#	$NetBSD: mktokens,v 1.15 2023/04/07 10:34:13 kre Exp $
361f28255Scgd#
437ed7877Sjtc# Copyright (c) 1991, 1993
537ed7877Sjtc#	The Regents of the University of California.  All rights reserved.
661f28255Scgd#
761f28255Scgd# This code is derived from software contributed to Berkeley by
861f28255Scgd# Kenneth Almquist.
961f28255Scgd#
1061f28255Scgd# Redistribution and use in source and binary forms, with or without
1161f28255Scgd# modification, are permitted provided that the following conditions
1261f28255Scgd# are met:
1361f28255Scgd# 1. Redistributions of source code must retain the above copyright
1461f28255Scgd#    notice, this list of conditions and the following disclaimer.
1561f28255Scgd# 2. Redistributions in binary form must reproduce the above copyright
1661f28255Scgd#    notice, this list of conditions and the following disclaimer in the
1761f28255Scgd#    documentation and/or other materials provided with the distribution.
183538d265Sagc# 3. Neither the name of the University nor the names of its contributors
1961f28255Scgd#    may be used to endorse or promote products derived from this software
2061f28255Scgd#    without specific prior written permission.
2161f28255Scgd#
2261f28255Scgd# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2361f28255Scgd# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2461f28255Scgd# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2561f28255Scgd# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2661f28255Scgd# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2761f28255Scgd# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2861f28255Scgd# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2961f28255Scgd# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3061f28255Scgd# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3161f28255Scgd# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3261f28255Scgd# SUCH DAMAGE.
3361f28255Scgd#
3449f0ad86Scgd#	@(#)mktokens	8.1 (Berkeley) 5/31/93
3561f28255Scgd
36cd5c0f94Sapb: ${AWK:=awk}
37125b7c8cSapb: ${SED:=sed}
38cd5c0f94Sapb
3961f28255Scgd# The following is a list of tokens.  The second column is nonzero if the
4061f28255Scgd# token marks the end of a list.  The third column is the name to print in
4161f28255Scgd# error messages.
4261f28255Scgd
430915509cSkre# Note that all the keyword tokens come after TWORD, and all the others
440915509cSkre# come before it.   We rely upon that relationship - keep it!
450915509cSkre
4661f28255Scgdcat > /tmp/ka$$ <<\!
4761f28255ScgdTEOF	1	end of file
4861f28255ScgdTNL	0	newline
4961f28255ScgdTSEMI	0	";"
5061f28255ScgdTBACKGND 0	"&"
5161f28255ScgdTAND	0	"&&"
5261f28255ScgdTOR	0	"||"
5361f28255ScgdTPIPE	0	"|"
5461f28255ScgdTLP	0	"("
5561f28255ScgdTRP	1	")"
5661f28255ScgdTENDCASE 1	";;"
577d41ae4eSkreTCASEFALL 1	";&"
5861f28255ScgdTENDBQUOTE 1	"`"
5961f28255ScgdTREDIR	0	redirection
6061f28255ScgdTWORD	0	word
6161f28255ScgdTIF	0	"if"
6261f28255ScgdTTHEN	1	"then"
6361f28255ScgdTELSE	1	"else"
6461f28255ScgdTELIF	1	"elif"
6561f28255ScgdTFI	1	"fi"
6661f28255ScgdTWHILE	0	"while"
6761f28255ScgdTUNTIL	0	"until"
6861f28255ScgdTFOR	0	"for"
6961f28255ScgdTDO	1	"do"
7061f28255ScgdTDONE	1	"done"
7161f28255ScgdTBEGIN	0	"{"
7261f28255ScgdTEND	1	"}"
7361f28255ScgdTCASE	0	"case"
7461f28255ScgdTESAC	1	"esac"
7537ed7877SjtcTNOT	0	"!"
7661f28255Scgd!
7761f28255Scgdnl=`wc -l /tmp/ka$$`
7871ab1d0aSchristosexec > token.h
79cd5c0f94Sapb${AWK} '{print "#define " $1 " " NR-1}' /tmp/ka$$
8061f28255Scgdecho '
8161f28255Scgd/* Array indicating which tokens mark the end of a list */
8261f28255Scgdconst char tokendlist[] = {'
83cd5c0f94Sapb${AWK} '{print "\t" $2 ","}' /tmp/ka$$
8461f28255Scgdecho '};
8561f28255Scgd
863d424690Schristosconst char *const tokname[] = {'
87125b7c8cSapb${SED} -e 's/"/\\"/g' \
8861f28255Scgd    -e 's/[^	 ]*[	 ][	 ]*[^	 ]*[	 ][	 ]*\(.*\)/	"\1",/' \
8961f28255Scgd    /tmp/ka$$
9061f28255Scgdecho '};
9161f28255Scgd'
92125b7c8cSapb${SED} 's/"//g' /tmp/ka$$ | ${AWK} '
930915509cSkre/TWORD/{print "#define KWDOFFSET " NR; print "";
943d424690Schristos      print "const char *const parsekwd[] = {"}
9561f28255Scgd/TIF/,/neverfound/{print "	\"" $3 "\","}'
9661f28255Scgdecho '	0
9761f28255Scgd};'
9861f28255Scgd
9961f28255Scgdrm /tmp/ka$$
100