Source

LIME / lib / common.awk

##################################################################
# This AWK script contains functions commonly used in LIME.
#
#Copyright (C) 2008 NXP Semiconductors B.V.
#
#This file is part of LIME.
#
#LIME is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License version 2
#as published by the Free Software Foundation; either version 2
#of the License, or (at your option) any later version.
#
#LIME is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with LIME.  If not, see <http://www.gnu.org/licenses/>.
##################################################################

BEGIN { if (!STDERR) STDERR="/dev/stderr"
	App=""
}

BEGIN { if (Root) { gsub(/@/," ",Root);split(Root,a," "); RootTag=a[1]
	if (split(a[2],b,"=")==2) App=unquote(b[2])
	print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
	if (!dtd_root) dtd_root="http://bitbucket.org/pjotr/lime/raw/karma/doc"
	if (do_xsl) printf "<?xml-stylesheet type='text/xsl' href='%s.xsl'?>\n",RootTag
	if (do_dtd) print "<!DOCTYPE",RootTag,"PUBLIC '-//LIME/DTD",RootTag,
		"1.0 Transitional//EN' '" dtd_root "/" RootTag ".dtd'>"
	printf "<%s",Root
	if (use_xi)
		printf " xmlns:xi='http://www.w3.org/2001/XInclude'"
	if (use_xlink)
		printf " xmlns:xlink='http://www.w3.org/1999/xlink' xlink:type='%s'",use_xlink
	print ">"
}}

END { if (RootTag) {
        if (App) {
		printf "<xi:include href='%s.map'><xi:fallback/></xi:include>\n",App
        	printf "<xi:include href='%s-full.map'><xi:fallback/></xi:include>\n",App
	}
	print "</" RootTag ">" 
      }
}

func suff(a,b,c) { return a (a ? b : c) }
func pref(a,b,c) { return (a ? b : c) a }
func alt(a,b) { return a ? a : b }
func wrap(a,b,c,d) { return a!="" ? b a c : d }
func xmlwrap(t,b) { return wrap(b,"<" t ">","</" t ">") }
func attwrap(t,b) { return wrap(b,t "='","'") }
func attWrap(t,b) { return wrap(b,t "=\"","\"") }
func urlWrap(b) { return wrap(b,"URL=\"#","\"") }
func Print(m) { if (m) print m }

func append(a,b,c)  { return a (a ? (c ? c : (b ? " " : "")) : "") b }

func prepend(a,b,c) { return b (b ? (c ? c : (a ? " " : "")) : "") a }

func space(n, c, r,i) {
	for (i=1; i<=n; i++) r=r (c ? c : " ");
	return r
}
func prind(l,msg) { print space(l*2) msg }
func fail(msg) { print "ERROR:" f ":" msg > STDERR; exit(1) }
func warn(msg) { print "WARN:" f ":" msg > STDERR }

func handle_bindings(t,b, n)
{
#   if (DEBUG) print "BINDINGS: handling",t,"in",state >STDERR

    if (handle_xlink(t,b)) return b

    if (t=="bound-to" && state ~ /@node$/) {
    	n=id[lev]
    	gsub("^" n ":","",from[lev+1])
    	gsub("^" n ":","",to[lev+1])
        #print n,from[lev+1],to[lev+1] >STDERR
	int_edge[n,++nint_edge[n]]=from[lev+1] SUBSEP to[lev+1]
	delete from; delete to
    	return b
    }

    if (relax) {
#    	if (DEBUG) print "BINDINGS: ignoring",tag,"in",state >STDERR
    	return b
    }

    fail("BINDINGS: unknown tag " t)
    return b
}
func handle_xlink(t,b) {
    if (t == "@xlink:type") return type[lev]=b
    if (t == "@xlink:label") return label[lev]=b
    if (t == "@xlink:from") return from[lev]=b
    if (t == "@xlink:to") return to[lev]=b
    if (t == "@xlink:href") {
	if (split(b,href,"#")!=2) fail("can not split href:" b " " state)
	return b
    }

    return ""
}

func fromxml(a) {
    	a=gensub(/&?amp;/,"\\&","g",a)
    	a=gensub(/&?quot;/,"\"","g",a)
    	a=gensub(/&?apos;|&?#39;/,"'","g",a)
    	a=gensub(/&?lt;|&?#60;/,"<","g",a)
    	a=gensub(/&?gt;|&?#62;/,">","g",a)
	return a
}
func toxml(a) {
    	a=gensub("&","\\&amp;","g",a)
    	a=gensub("\"","\\&quot;","g",a)
    	a=gensub("'","\\&apos;","g",a)
    	a=gensub("<","\\&lt;","g",a)
    	a=gensub(">","\\&gt;","g",a)
	return a
}
# TODO: real analysis
func max(a,b) { return (a>=b) ? a : b }

func unquote(v) { return gensub(/^"(.*)"$/,"\\1","g",gensub(/^'(.*)'$/,"\\1","g",v)) }