Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

#!/bin/sh -
#	$NetBSD: mkops,v 1.8 2011/02/07 23:56:18 christos Exp $
#
# Copyright (c) 2011 The NetBSD Foundation, Inc.
# All rights reserved.
#
# This code is derived from software contributed to The NetBSD Foundation
# by Christos Zoulas.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

# allow AWK to be overriden
: ${AWK:=awk}


while getopts ch f
do
	case $f in
	c)
		v=c;;
	h)
		v=h;;
	*)
		echo "Usage: $0 -c|-h";;
	esac
done

shift $(expr ${OPTIND} - 1)

$AWK -F'	' -v v=$v '
function display(fmt, last, comment)
{
	printf(fmt, last);
	if (comment != "")
		printf("\t/* pseudo op not used in trees */");
	printf("\n");
}

BEGIN {
	print "/* Automatically generated file; do not edit */";
	if (v == "h") {
		printf("typedef enum {\n");
		FIRST = "";
		LAST = "";
		LASTCOMMENT= "";
	}
	if (v == "c") {
		printf("#include <sys/types.h>\n");
		printf("#include \"op.h\"\n");
		printf("#include \"param.h\"\n");
		printf("#ifndef __arraycount\n");
		printf("#define __arraycount(a) (sizeof(a) / sizeof(a[0]))\n");
		printf("#endif /* __arraycount */\n");
		printf("mod_t modtab[NOPS];\n");
		printf("static const struct {\n");
		printf("\tmod_t\tm;\n");
		printf("\tunsigned char\tok;\n");
		printf("} imods[] = {\n");
	}
}

{
	if (v == "h") {
		if (LAST != "") {
			if (FIRST == "") {
				display("\t%s\t= 0,", LAST, LASTCOMMENT);
			} else {
				display("\t%s,", LAST, LASTCOMMENT);
			}
		}
		FIRST = LAST;
		LAST = $2;
		LASTCOMMENT = $4;
	}
	if (v == "c") {
		if ($3 == "X") {
			m = "0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,";
			act = 0;
		} else {
			m = $3;
			act = 1;
		}
		printf("\t{ /* %s */\t{ %s \"%s\" }, %d },\n", $2, m, $1, act); 
	}
}

END {
	if (v == "h") {
		display("\t%s,", LAST, LASTCOMMENT);
		printf("#define\tNOPS\t((int)%s + 1)\n", LAST);
		printf("} op_t;\n");
		printf("const char *getopname(op_t);\n");
		printf("void initmtab(void);\n");
	} 
	if (v == "c") {
		printf("};\n");
		printf("const char *\n");
		printf("getopname(op_t op) {\n");
		printf("\treturn imods[op].m.m_name;\n");
		printf("}\n");
		printf("void\n");
		printf("initmtab(void)\n");
		printf("{\n");
		printf("\tsize_t i;\n");
		printf("\n");
		printf("\tfor (i = 0; i < __arraycount(imods); i++)\n");
		printf("\t\tif (imods[i].ok)\n");
		printf("\t\t\tSTRUCT_ASSIGN(modtab[i], imods[i].m);\n");
		printf("}\n");
	}
}
' "$@"