#! /usr/bin/awk -f # CONF - Multi-purpose configurator in AWK # Copyright (c) 2008 Matous Jan Fialka # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in all # copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. BEGIN { variable = contents = "" error = 0 } /^#!/ && NR == 1 { gsub(/^#![[:blank:]]*/, "") gsub(/[[:blank:]]+/, " ") print chop() next } /^[[:blank:]]*#/ { next } /#.*$/ { gsub(/#.*$/, "") } /^[[:alpha:]_][[:alnum:]_]*[[:blank:]]*=/ { if(variable) print variable "=" normalize(contents) match($0, /=/) variable = chop(substr($0, 1, RSTART - 1)) contents = chop(shift()) next } /^[[:blank:]]+[^[:blank:]]+/ { contents = contents ? contents " " chop() : chop() next } /^[[:blank:]]*$/ { if(variable) { print variable "=" normalize(contents) variable = "" } next } { exit error = 1 } END { if(variable) print variable "=" normalize(contents) if(error) { print "Error: invalid entry on line " NR > "/dev/stderr" exit error } exit 0 } function shift(what ,nothing_given) { nothing_given = 0 if(!what) { what = $0 nothing_given = 1 } what = substr(what, match(what, /=/) + 1) if(nothing_given) $0 = what return what } function chop(what) { what = what ? what : $0 gsub(/^[[:blank:]]*/, "", what) gsub(/[[:blank:]]*$/, "", what) return what } function normalize(what) { if(what) gsub(/([[:blank:]]*,[[:blank:]]*|[[:blank:]]+)/, ",", what) return what } # vi:ft=awk: