{
\File{dbtex.awk},{17:51},{Apr 08 1991}
\L{\LB{\C{}\# dbTeX.awk \- a simple preprocessor for generating TeX output from}}
\CE{}\L{\LB{\C{}\# }\Tab{8}{database files}}
\CE{}\L{\LB{\C{}\# Version 1.1.beta (1991\-02\-12) dr. w. kraml, KPMG Alpen\-Treuhand Wien}}
\CE{}\L{\LB{\C{}\# see dbTeX.tex for documentation!}}
\CE{}\L{\LB{}}
\L{\LB{}}
\L{\LB{\K{BEGIN} \{ console\_msg(\S{}\"This is dbTeX Version 1.1.beta (1991\-02\-12)\"\SE{})}}
\L{\LB{        \K{printf}(\S{}\"\%\% dbTeX (1.1.beta) Output generated \"\SE{} ctime() \S{}\"\!n\"\SE{})}}
\L{\LB{}\Tab{52}{\C{}\# \^ PolyAwk specific!}}
\CE{}\L{\LB{        \K{printf}(\S{}\"\%\% dbTeX       Input file: \%s\!n\"\SE{}, \K{ARGV}[1])}}
\L{\LB{\C{}\# a number of initialisations and defs follow here:}}
\CE{}\L{\LB{        FieldS = \K{FS}}}
\L{\LB{        RecordS = \K{RS}}}
\L{\LB{        NRincluded = 0}}
\L{\LB{        example\_flag = 0                           }}
\L{\LB{        buffer = \S{}\"\"\SE{}}}
\L{\LB{        STANDARD = 0}}
\L{\LB{        STRING = 1}}
\L{\LB{        TEMPLATE = 2}}
\L{\LB{        ID = 3        }}
\L{\LB{        USER = 1        }}
\L{\LB{      \}}}
\L{\LB{}}
\L{\LB{      }}
\L{\LB{\C{}\# main loop \- do this for each line in script}}
\CE{}\L{\LB{}}
\L{\LB{\{       \K{if} (\$0 \~ \/\^\%!\/) \{}\Tab{44}{\C{}\# embedded dbTeX\-command}}
\CE{}\L{\LB{            in\_template = 0}}
\L{\LB{            buffer = \K{substr}(\$0,3)}}
\L{\LB{            \K{print} \$0}\Tab{44}{\C{}\# output for reference}}
\CE{}\L{\LB{            dbTeX\_command()}}
\L{\LB{        \}}}
\L{\LB{        \K{else} \K{if} (\$0 \~ \/\^\%\_\/) \{}\Tab{44}{\C{}\# template contin. line}}
\CE{}\L{\LB{            \K{print} \$0}}
\L{\LB{            \K{if} (in\_template) \{}\Tab{44}{\C{}\# start cont. with newlines}}
\CE{}\L{\LB{                nl = \K{length}(template[t\_name]) ? \S{}\"\!n\"\SE{} : \S{}\"\"\SE{}}}
\L{\LB{                template[t\_name] = template[t\_name] nl \K{substr}(\$0,3)}}
\L{\LB{            \}}}
\L{\LB{            \K{else} error\_msg(\S{}\"INVALID CONTINUATION\"\SE{})}}
\L{\LB{        \}}}
\L{\LB{        \K{else} \K{if} (example\_flag) \{}\Tab{44}{\C{}\# embedded examples}}
\CE{}\L{\LB{            in\_template = 0}}
\L{\LB{            \K{print} \S{}\"\%\"\SE{} \$0}}
\L{\LB{        \}}}
\L{\LB{        \K{else} \{}}
\L{\LB{            in\_template = 0}}
\L{\LB{            \K{print} \$0  }\Tab{44}{\C{}\# original TeX line}}
\CE{}\L{\LB{        \}}}
\L{\LB{\}}}
\L{\LB{}}
\L{\LB{\K{END} \{   s = \K{NR} \S{}\" line(s) input, \"\SE{} NRincluded \S{}\" record(s) included, \"\SE{}}}
\L{\LB{        s = s (errors+0) \S{}\" error(s)\"\SE{} }}
\L{\LB{        info\_msg(s)}}
\L{\LB{    \}}}
\L{\LB{}}
\L{\LB{}}
\L{\LB{}}
\L{\LB{\C{}\# functions performing helpful tasks with various messages}}
\CE{}\L{\LB{}}
\L{\LB{\Proc{console\_msg}\K{function} console\_msg(s) \{ }\Tab{44}{\C{}\# output to console}}
\CE{}\L{\LB{    \K{print} s \> \S{}\"\/dev\/tty\"\SE{}}}
\L{\LB{    \}}}
\L{\LB{    }}
\L{\LB{\Proc{error\_msg}\K{function} error\_msg(msg,     s) \{}\Tab{44}{\C{}\# print error on console}}
\CE{}\L{\LB{    s = \S{}\"\% (dbTeX) Error: \"\SE{} msg \S{}\"!\"\SE{}}\Tab{44}{\C{}\# and in output file}}
\CE{}\L{\LB{    ++errors}}
\L{\LB{    \K{print} s            }}
\L{\LB{    console\_msg(s \S{}\" \-\> line \"\SE{} \K{NR} \S{}\":\"\SE{})}}
\L{\LB{    console\_msg(\$0)}}
\L{\LB{\}}}
\L{\LB{}}
\L{\LB{\Proc{info\_msg}\K{function} info\_msg(msg,     s) \{}\Tab{44}{\C{}\# print info on console}}
\CE{}\L{\LB{    s = \S{}\"\% (dbTeX) Info: \"\SE{} msg \S{}\".\"\SE{}}\Tab{44}{\C{}\# and in output file}}
\CE{}\L{\LB{    \K{print} s            }}
\L{\LB{    console\_msg(s)}}
\L{\LB{\}}}
\L{\LB{}}
\L{\LB{}}
\L{\LB{\C{}\# dbTeX functions}}
\CE{}\L{\LB{}}
\L{\LB{\Proc{dbTeX\_command}\K{function} dbTeX\_command() \{}\Tab{44}{\C{}\# the dbTeX main function}}
\CE{}\L{\LB{    advance(ID)}}
\L{\LB{    example\_flag = 0}}
\L{\LB{    \K{if} (tok == \S{}\"examples\"\SE{}) example\_flag = 1}\Tab{44}{\C{}\# \%! examples}}
\CE{}\L{\LB{    \K{else} \K{if} (tok == \S{}\"record\"\SE{}) \{ }\Tab{44}{\C{}\# \%! record \<id\> \<FS\> [\<RS\>]}}
\CE{}\L{\LB{        \K{if} (advance(ID)) \{}}
\L{\LB{            \K{if} (tok \~ \/\^\%[0\-9]+\%\$\/) \{}\Tab{44}{\C{}\# data\-named template}}
\CE{}\L{\LB{                templ\_typ = 1}}
\L{\LB{                templ\_field = \K{substr}(tok,2,\K{length}(tok)\-2)}}
\L{\LB{            \}}}
\L{\LB{            \K{else} \{  }\Tab{44}{\C{}\# script\-named template}}
\CE{}\L{\LB{                templ\_typ = 0}}
\L{\LB{                templ\_name = tok}}
\L{\LB{            \}}}
\L{\LB{        \}}}
\L{\LB{        FieldS  = advance(STRING) ? tok : \K{FS}}}
\L{\LB{        RecordS = advance(STRING) ? tok : \K{RS}}}
\L{\LB{    \}}}
\L{\LB{    \K{else} \K{if} (tok==\S{}\"template\"\SE{}) \{   }\Tab{44}{\C{}\# \%! template \<id\> \<template\>}}
\CE{}\L{\LB{        in\_template = 1}}
\L{\LB{        \K{if} (advance(ID))    }\Tab{44}{\C{}\# get name}}
\CE{}\L{\LB{            t\_name = tok}}
\L{\LB{        \K{if} (advance(TEMPLATE)) }}
\L{\LB{            template[t\_name] = tok}}
\L{\LB{    \}}}
\L{\LB{    \K{else} \K{if} (tok==\S{}\"break\"\SE{}) \{ }\Tab{44}{\C{}\# \%! break \%f\% \<id\>}}
\CE{}\L{\LB{        \K{if} (advance(ID)) \{}}
\L{\LB{            \K{if} (tok \~ \/\^\%[0\-9]+\%\$\/)}}
\L{\LB{                breakfield = \K{substr}(tok,2,\K{length}(tok)\-2)}}
\L{\LB{            \K{else}}}
\L{\LB{                breakfield = tok}}
\L{\LB{        \}}}
\L{\LB{        \K{if} (advance(ID))}}
\L{\LB{            breaktemplate[breakfield] = tok}}
\L{\LB{        \K{if} (advance(ID))}}
\L{\LB{            breakmode[breakfield] = 1}}
\L{\LB{        \K{else} breakmode[breakfield] = 0}}
\L{\LB{    \}}}
\L{\LB{    \K{else} \K{if} (tok==\S{}\"clearbreaks\"\SE{}) \{ }\Tab{44}{\C{}\# \%! clearbreaks}}
\CE{}\L{\LB{          \K{for} (i \K{in} oldvalue)}}
\L{\LB{            \K{delete} oldvalue[i]  }}
\L{\LB{          \K{for} (i \K{in} breaktemplate)}}
\L{\LB{            \K{delete} breaktemplate[i]}}
\L{\LB{          \K{for} (i \K{in} breakmode)}}
\L{\LB{            \K{delete} breakmode[i]}}
\L{\LB{    \}}}
\L{\LB{    \K{else} \K{if} (tok==\S{}\"translate\"\SE{}) \{ }\Tab{44}{\C{}\# \%! translate \<id\>}}
\CE{}\L{\LB{        \K{if} (advance(ID))}}
\L{\LB{            set\_translation(tok)}}
\L{\LB{    \}}}
\L{\LB{    \K{else} \K{if} (tok==\S{}\"change\"\SE{}) \{ }\Tab{44}{\C{}\# \%! change \<s1\> \<s2\>}}
\CE{}\L{\LB{        add\_trtbl(USER)}}
\L{\LB{    \}}}
\L{\LB{    \K{else} \K{if} (tok==\S{}\"include\"\SE{}) \{ }\Tab{44}{\C{}\# \%! include \<filename\>}}
\CE{}\L{\LB{        \K{if} (advance(ID))}}
\L{\LB{            do\_include(tok)}}
\L{\LB{    \}}}
\L{\LB{    \K{else} error\_msg(\S{}\"UNKNOWN COMMAND\"\SE{})}}
\L{\LB{\}}}
\L{\LB{}}
\L{\LB{}}
\L{\LB{\Proc{advance}\K{function} advance(type,    sep,pos) \{ }\Tab{44}{\C{}\# get next token of type}}
\CE{}\L{\LB{    \K{sub}(\/\^[ \!t]+\/, \S{}\"\"\SE{}, buffer)  }\Tab{44}{\C{}\# remove white space}}
\CE{}\L{\LB{    \K{if} (type == ID) \{}}
\L{\LB{        \K{match}(buffer, \/\^[\^ \!t]+\/) }\Tab{44}{\C{}\# all up to white space}}
\CE{}\L{\LB{        tok = tolower(\K{substr}(buffer, 1, \K{RLENGTH})) \C{}\# tolower(): PolyAwk \& GNU only!}}
\CE{}\L{\LB{        buffer = \K{substr}(buffer, \K{RLENGTH}+1)}}
\L{\LB{        \K{return} \K{RLENGTH}}}
\L{\LB{    \}}}
\L{\LB{    \K{else} \K{if} (type == TEMPLATE) \{ }\Tab{44}{\C{}\# rest of line}}
\CE{}\L{\LB{        tok = buffer}}
\L{\LB{        buffer = \S{}\"\"\SE{}}}
\L{\LB{        \K{return} \K{length}(tok)}}
\L{\LB{    \}}}
\L{\LB{    \K{else} \K{if} (type == STRING) \{ }\Tab{44}{\C{}\# string delimited by}}
\CE{}\L{\LB{        sep = \K{substr}(buffer,1,1) }\Tab{44}{\C{}\# unique character}}
\CE{}\L{\LB{        buffer = \K{substr}(buffer,2)}}
\L{\LB{        pos = \K{index}(buffer, sep)}}
\L{\LB{        \K{if} (pos)  \{}}
\L{\LB{            tok = \K{substr}(buffer,1,pos\-1)}}
\L{\LB{            buffer = \K{substr}(buffer, pos+1)}}
\L{\LB{            \K{return} pos}}
\L{\LB{        \}}}
\L{\LB{        \K{else} \{}}
\L{\LB{            tok = \S{}\"\"\SE{}}}
\L{\LB{            \K{return} 0}}
\L{\LB{        \}}}
\L{\LB{    \}}}
\L{\LB{    \K{else} \K{return} 0}}
\L{\LB{\}}}
\L{\LB{}}
\L{\LB{}}
\L{\LB{\Proc{add\_trtbl}\K{function} add\_trtbl(tbl) \{ }\Tab{44}{\C{}\# add an entry to tr table}}
\CE{}\L{\LB{    \K{if} (advance(STRING)) \{}}
\L{\LB{        tmp = tok}}
\L{\LB{        \K{if} (advance(STRING))}}
\L{\LB{          tbl==USER ? (usr\_tbl[tmp] = tok) : (std\_tbl[tmp] = tok)}}
\L{\LB{        \K{else} }}
\L{\LB{          tbl==USER ? (usr\_tbl[tmp] = \S{}\"\"\SE{}) : (std\_tbl[tmp] = \S{}\"\"\SE{})}}
\L{\LB{    \}}}
\L{\LB{\}}}
\L{\LB{}}
\L{\LB{}}
\L{\LB{\Proc{set\_translation}\K{function} set\_translation(id) \{}\Tab{44}{\C{}\# install translation table}}
\CE{}\L{\LB{    \K{if} (toupper(id) == \S{}\"OFF\"\SE{}) \{}}
\L{\LB{        \K{for} (i \K{in} std\_tbl)}}
\L{\LB{            \K{delete} std\_tbl[i]}}
\L{\LB{        \K{for} (i \K{in} usr\_tbl)}}
\L{\LB{            \K{delete} usr\_tbl[i]}}
\L{\LB{    \}}}
\L{\LB{    \K{else} \{}\Tab{44}{\C{}\# read translation table}}
\CE{}\L{\LB{        \K{while} ((status = \K{getline} line \< id) \> 0) \{}}
\L{\LB{            buffer = line}}
\L{\LB{            \K{if} (buffer !\~ \/\^\C{}\#\/)}}
\CE{}\L{\LB{                add\_trtbl(STANDARD)}}
\L{\LB{        \}        }}
\L{\LB{        \K{if} (status == \-1) }}
\L{\LB{            error\_msg(\S{}\"TRANSLATION TABLE \"\SE{} id \S{}\" NOT FOUND\"\SE{})}}
\L{\LB{    \}}}
\L{\LB{\}}}
\L{\LB{}}
\L{\LB{}}
\L{\LB{\Proc{do\_include}\K{function} do\_include(file) \{}\Tab{44}{\C{}\# include records of \<file\>}}
\CE{}\L{\LB{    old\_FS = \K{FS}; old\_RS = \K{RS}}}
\L{\LB{    \K{FS} = FieldS; \K{RS} = RecordS}}
\L{\LB{    inc = 0}}
\L{\LB{    \K{while} (\K{getline} \< file \> 0) \{}}
\L{\LB{        \K{for} (i \K{in} breaktemplate)}\Tab{44}{\C{}\# check for breaks}}
\CE{}\L{\LB{            \K{if} (oldvalue[i] != \$i) \{       }}
\L{\LB{                oldvalue[i] = \$i}}
\L{\LB{                \K{if} (inc \|\| breakmode[i])}\Tab{44}{\C{}\# no break before 1st rec.,}}
\CE{}\L{\LB{                    incl\_(template[breaktemplate[i]]) }}
\L{\LB{            \} }\Tab{44}{\C{}\# if not explicitly wanted!}}
\CE{}\L{\LB{}}
\L{\LB{        \K{if} (templ\_typ) }}
\L{\LB{            incl\_(template[\$templ\_field])}}
\L{\LB{        \K{else} incl\_(template[templ\_name])}}
\L{\LB{        ++NRincluded; ++inc}}
\L{\LB{    \}}}
\L{\LB{    \K{FS} = old\_FS; \K{RS} = old\_RS}}
\L{\LB{    \K{close}(file)}}
\L{\LB{    \K{if} (!inc) error\_msg(\S{}\"FILE \"\SE{} file \S{}\" NOT FOUND OR NOT ACCESSIBLE!\"\SE{})}}
\L{\LB{    info\_msg(inc \S{}\" record(s) included from \"\SE{} file)}}
\L{\LB{\}}}
\L{\LB{}}
\L{\LB{}}
\L{\LB{\Proc{incl\_}\K{function} incl\_(t) \{}\Tab{44}{\C{}\# include record with template t}}
\CE{}\L{\LB{    s = \S{}\"\"\SE{}}}
\L{\LB{    \K{while} (\K{match}(t, \/\%[0\-9]+\%\/)) \{}}
\L{\LB{        l = \K{RSTART}\-1}}
\L{\LB{        s = s \K{substr}(t,1,l)  txlate(\$(\K{substr}(t,\K{RSTART}+1,\K{RLENGTH}\-2)))}}
\L{\LB{}\Tab{44}{\C{}\# translated data field}}
\CE{}\L{\LB{        t = \K{substr}(t,l+\K{RLENGTH}+1)}}
\L{\LB{    \}}}
\L{\LB{    \K{print} s t                           }}
\L{\LB{\}}}
\L{\LB{}}
\L{\LB{}}
\L{\LB{\Proc{txlate}\K{function} txlate(s) \{}\Tab{44}{\C{}\# perform translations on string s}}
\CE{}\L{\LB{    \K{for} (target \K{in} std\_tbl) }}
\L{\LB{        \K{gsub}(target, std\_tbl[target], s)}}
\L{\LB{    \K{for} (target \K{in} usr\_tbl) }}
\L{\LB{        \K{gsub}(target, usr\_tbl[target], s)}}
\L{\LB{    \K{return} s}}
\L{\LB{\}}}
\L{\LB{    }}
}