xref: /netbsd/lib/libterminfo/genhash (revision c85eef2d)
14ca00e00Sroy#!/bin/sh
2*c85eef2dSroy# $NetBSD: genhash,v 1.9 2017/01/11 20:53:52 roy Exp $
34ca00e00Sroy
4101a2e3aSroy# Copyright (c) 2009, 2011 The NetBSD Foundation, Inc.
54ca00e00Sroy#
64ca00e00Sroy# This code is derived from software contributed to The NetBSD Foundation
74ca00e00Sroy# by Roy Marples.
84ca00e00Sroy#
94ca00e00Sroy# Redistribution and use in source and binary forms, with or without
104ca00e00Sroy# modification, are permitted provided that the following conditions
114ca00e00Sroy# are met:
124ca00e00Sroy# 1. Redistributions of source code must retain the above copyright
134ca00e00Sroy#    notice, this list of conditions and the following disclaimer.
144ca00e00Sroy# 2. Redistributions in binary form must reproduce the above copyright
154ca00e00Sroy#    notice, this list of conditions and the following disclaimer in the
164ca00e00Sroy#    documentation and/or other materials provided with the distribution.
174ca00e00Sroy#
184ca00e00Sroy# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
194ca00e00Sroy# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
204ca00e00Sroy# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
214ca00e00Sroy# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
224ca00e00Sroy# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
234ca00e00Sroy# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
244ca00e00Sroy# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
254ca00e00Sroy# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
264ca00e00Sroy# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
274ca00e00Sroy# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
284ca00e00Sroy
294ca00e00Sroy
304ca00e00Sroy# Generate string and hash tables for our terminfo strings in term.h
314ca00e00Sroy# We don't expose the hash or tables directly, but instead via functions.
323ca90ff8Ssnj# This allows us to freely change how we hash or store our string tables
334ca00e00Sroy# in the future.
344ca00e00Sroy
35ca77b139Sroyset -e
364ca00e00Sroy: ${TOOL_AWK:=awk}
374ca00e00Sroy: ${TOOL_NBPERF:=nbperf}
384ca00e00Sroy: ${TOOL_SED:=sed}
394ca00e00Sroy
404ca00e00SroyTERMH=${1:-term.h}
414ca00e00Sroy
424ca00e00Sroygenent()
434ca00e00Sroy{
444ca00e00Sroy	local name=$1 NAME=$2 len=
454ca00e00Sroy
464ca00e00Sroy	# Calculate the maximum word length plus terminator
47f207e61cSchristos	len=$($TOOL_SED -e "1,/enum TI${NAME}/d" -e '/};/,$d' \
484ca00e00Sroy            -e 's/.*TICODE_\([^,]*\).*/\1X/' $TERMH | \
49f207e61cSchristos	    $TOOL_AWK \
50f207e61cSchristos	    'BEGIN {L=0} {if (length($1)>L) L=length($1)} END {print L}')
514ca00e00Sroy
524ca00e00Sroy	echo
534ca00e00Sroy	echo "static const char _ti_${name}ids[][${len}] = {"
544ca00e00Sroy	$TOOL_SED -e "1,/enum TI${NAME}/d" -e '/};/,$d' \
554ca00e00Sroy            -e 's/.*TICODE_\([^,]*\).*/	"\1",/' $TERMH
564ca00e00Sroy	echo "};"
574ca00e00Sroy	echo
584ca00e00Sroy	$TOOL_SED -e "1,/enum TI${NAME}/d" -e '/};/,$d' \
594ca00e00Sroy	    -e 's/.*TICODE_\([^,]*\).*/\1/' $TERMH | \
60101a2e3aSroy	    $TOOL_NBPERF -p -s -n _ti_${name}hash;
614ca00e00Sroy
624ca00e00Sroy	cat <<EOF
634ca00e00Sroy
644ca00e00Sroyconst char *
654ca00e00Sroy_ti_${name}id(ssize_t idx)
664ca00e00Sroy{
674ca00e00Sroy
68*c85eef2dSroy	if ((size_t)idx >= __arraycount(_ti_${name}ids))
694ca00e00Sroy		return NULL;
704ca00e00Sroy	return _ti_${name}ids[idx];
714ca00e00Sroy}
724ca00e00Sroy
734ca00e00Sroyssize_t
744ca00e00Sroy_ti_${name}index(const char *key)
754ca00e00Sroy{
764ca00e00Sroy	uint32_t idx;
774ca00e00Sroy
784ca00e00Sroy	idx = _ti_${name}hash((const unsigned char *)key, strlen(key));
79*c85eef2dSroy	if (idx >= __arraycount(_ti_${name}ids) ||
804ca00e00Sroy	    strcmp(key, _ti_${name}ids[idx]) != 0)
814ca00e00Sroy		return -1;
824ca00e00Sroy	return idx;
834ca00e00Sroy}
844ca00e00SroyEOF
854ca00e00Sroy}
864ca00e00Sroy
874ca00e00Sroycat <<EOF
884ca00e00Sroy/* DO NOT EDIT
894ca00e00Sroy * Automatically generated from term.h */
904ca00e00Sroy
91d8609d06Sroy#if HAVE_NBTOOL_CONFIG_H
92d8609d06Sroy#include "nbtool_config.h"
93d8609d06Sroy#endif
94d8609d06Sroy
95eedd9adeSroy#include <stdint.h>
964ca00e00Sroy#include <stdlib.h>
974ca00e00Sroy#include <string.h>
984ca00e00Sroy#include <term_private.h>
994ca00e00Sroy#include <term.h>
1004ca00e00SroyEOF
1014ca00e00Sroy
1024ca00e00Sroygenent flag FLAG
1034ca00e00Sroygenent num NUM
1044ca00e00Sroygenent str STR
105