#!/bin/csh
#
# Copyright (c) 2003 Seth Kingsley.  All rights reserved.
#
# 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.
# 3. All advertising materials mentioning features or use of this software
#    must display the following acknowledgment:
#    This product includes software developed by Seth Kingsley and
#    contributors.
# 4. Neither the author's name, nor any of the contributors names may be
#    used to endorse or promote products derived from this software without
#    specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
#
#   $Id: cbc,v 1.1.1.1 2003/08/10 23:43:06 sethk Exp $

if ($#argv != 1) then
    echo "${0:t}: No input files" > /dev/stderr
    exit 66 # EX_NOINPUT
endif
set base="$1:t"
awk -F: -v base="$base" '\
    BEGIN {\
	print "procedure";\
    }\
    /^\\!/ {next;}\
    NF == 2 {\
	print "    " base "_" $1 "(any);";\
	next;\
    }\
    {\
	print FILENAME ":" FNR ": unparsed line" >"/dev/stderr";\
	exit 1;\
    }\
    END {\
	print "";\
	print "identifier";\
	if (length(base) <= 2) {\
	    prefix = base;\
	} else {\
	    prefix = substr(base, 1, 1);\
	}\
	print "    " prefix "datap;";\
    }\
' "$1.cb" > "$1-cb.uih" || exit $status
awk -v base="$base" '\
    /^\\!/ {next;}\
    NF == 1 {\
	print "    " base "_" $1 "_widp;";\
    }\
' "$1.wids" >> "$1-cb.uih" || exit $status
awk -v base="$base" '\
    BEGIN {\
	print "#ifndef " toupper(base) "_CB_H";\
	print "#define " toupper(base) "_CB_H";\
	print "";\
	print "#include    <Xm/XmAll.h>";\
	print "#include    <Mrm/MrmPublic.h>";\
	print "";\
	printf "%s", "#define " toupper(base) "_WIDS ";\
    }\
    /^\\!/ {next;}\
    NF == 1 {\
	printf "%s", "\\\n\tWidget    " substr(base, 1, 1) "d_" $1 ";";\
    }\
    END {\
	print "";\
    }\
' "$1.wids" > "$1-cb.h" || exit $status
awk -F: -v base="$base" '\
    BEGIN {\
	if (length(base) <= 2) {\
	    prefix = base;\
	} else {\
	    prefix = substr(base, 1, 1);\
	}\
	print "";\
	print "struct " base "_data;";\
	print "";\
    }\
    /^\\!/ {next;}\
    NF == 2 {\
	print "void cb_" base "_" $1 "(Widget _wid,";\
	print "\tstruct " base "_data *_" substr(base, 1, 1) "dp,";\
	if (substr($2, 1, 1) == "*") {\
	    class = substr($2, 2, length($2) - 1);\
	} else {\
	    class = "Xm" $2;\
	}\
	print "\t" class "CallbackStruct *_xmdata);";\
    }\
    END {\
	print "";\
	print "u_short " base "_reg_names(struct " base "_data *_" substr(base, 1, 1) "dp);";\
	print "";\
	print "#endif /* !" toupper(base) "_CB_H */";\
    }\
' "$1.cb" >> "$1-cb.h" || exit $status
set nwid=`grep '^[^\\!]' "$1.wids" | wc -l`
awk -F: -v base="$base" '\
    BEGIN {\
	if (length(base) <= 2) {\
	    prefix = base;\
	} else {\
	    prefix = substr(base, 1, 1);\
	}\
	print "#include    \"" base ".h\"";\
	print "#include    \"" base "-cb.h\"";\
	print "";\
	print "u_short";\
	print base "_reg_names(struct " base "_data *" prefix "dp)";\
	print "{";\
	print "    MrmRegisterArg\tnames[] =";\
	print "    {";\
    }\
    /^\\!/ {next;}\
    NF == 2 {\
	print "\t{\"" base "_" $1 "\", (XtPointer)cb_" base "_" $1 "},";\
    }\
    END {\
	print "\t{\"" prefix "datap\", NULL}"\
	print "    };";\
    }\
' "$1.cb" > "$1-cb.c" || exit $status
awk -v base="$base" -v nwid=$nwid '\
    BEGIN {\
	if (length(base) <= 2) {\
	    prefix = base;\
	} else {\
	    prefix = substr(base, 1, 1);\
	}\
	print "    MrmRegisterArg\twids[" nwid "];";\
	print "";\
	i = 0;\
    }\
    /^\\!/ {next;}\
    NF == 1 {\
	print "    wids[" i "].name = \"" base "_" $0 "_widp\";";\
	print "    wids[" i "].value = &(" prefix "dp->" substr(base, 1, 1) "d_" $0 ");";\
	++i;\
    }\
    END {\
	print "    names[XtNumber(names) - 1].value = " prefix "dp;";\
	print "    return (MrmRegisterNames(names, XtNumber(names)) == MrmSUCCESS &&";\
	print "\t    MrmRegisterNames(wids, XtNumber(wids)) == MrmSUCCESS);";\
	print "}";\
	print "";\
    }\
' "$1.wids" >> "$1-cb.c" || exit $status

