xref: /netbsd-src/lib/libterminfo/genhash (revision 4ca00e00d84c6d6fe8dddcfc20bbb3204c93d655)
1#!/bin/sh
2# $NetBSD: genhash,v 1.1 2010/02/03 15:16:32 roy Exp $
3
4# Copyright (c) 2009 The NetBSD Foundation, Inc.
5#
6# This code is derived from software contributed to The NetBSD Foundation
7# by Roy Marples.
8#
9# Redistribution and use in source and binary forms, with or without
10# modification, are permitted provided that the following conditions
11# are met:
12# 1. Redistributions of source code must retain the above copyright
13#    notice, this list of conditions and the following disclaimer.
14# 2. Redistributions in binary form must reproduce the above copyright
15#    notice, this list of conditions and the following disclaimer in the
16#    documentation and/or other materials provided with the distribution.
17#
18# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29
30# Generate string and hash tables for our terminfo strings in term.h
31# We don't expose the hash or tables directly, but instead via functions.
32# This allows use to freely change how we hash or store our string tables
33# in the future.
34
35: ${TOOL_AWK:=awk}
36: ${TOOL_NBPERF:=nbperf}
37: ${TOOL_SED:=sed}
38
39TERMH=${1:-term.h}
40
41genent()
42{
43	local name=$1 NAME=$2 len=
44
45	# Calculate the maximum word length plus terminator
46	len=`$TOOL_SED -e "1,/enum TI${NAME}/d" -e '/};/,$d' \
47            -e 's/.*TICODE_\([^,]*\).*/\1X/' $TERMH | \
48	    $TOOL_AWK 'BEGIN {L=0} {if (length($1)>L) L=length($1)} END {print L}'`
49
50	echo
51	echo "static const char _ti_${name}ids[][${len}] = {"
52	$TOOL_SED -e "1,/enum TI${NAME}/d" -e '/};/,$d' \
53            -e 's/.*TICODE_\([^,]*\).*/	"\1",/' $TERMH
54	echo "};"
55	echo
56	$TOOL_SED -e "1,/enum TI${NAME}/d" -e '/};/,$d' \
57	    -e 's/.*TICODE_\([^,]*\).*/\1/' $TERMH | \
58	    $TOOL_NBPERF -sn _ti_${name}hash;
59
60	cat <<EOF
61
62const char *
63_ti_${name}id(ssize_t idx)
64{
65
66	if ((size_t)idx > __arraycount(_ti_${name}ids))
67		return NULL;
68	return _ti_${name}ids[idx];
69}
70
71ssize_t
72_ti_${name}index(const char *key)
73{
74	uint32_t idx;
75
76	idx = _ti_${name}hash((const unsigned char *)key, strlen(key));
77	if (idx > __arraycount(_ti_${name}ids) ||
78	    strcmp(key, _ti_${name}ids[idx]) != 0)
79		return -1;
80	return idx;
81}
82EOF
83}
84
85cat <<EOF
86/* \$NetBSD\$ */
87/* DO NOT EDIT
88 * Automatically generated from term.h */
89
90#include <sys/cdefs.h>
91__RCSID("\$NetBSD: genhash,v 1.1 2010/02/03 15:16:32 roy Exp $");
92
93#include <sys/types.h>
94
95#include <stdlib.h>
96#include <string.h>
97#include <term_private.h>
98#include <term.h>
99EOF
100
101genent flag FLAG
102genent num NUM
103genent str STR
104