blob: e926853e648897c4e129c038f48bce0bf28da5a2 [file] [log] [blame] [edit]
#! /usr/bin/awk -f
# unfold <spice.in >spice.out
# unfold spice.in >spice.out
#
# remove continuation lines from spice netlist.
# Also absorbs (deletes) CR at end of line (i.e. Windows style CRLF endings convert to unix LF).
#
# NOTE: If there are comments or blank lines ahead/between continuation lines, they
# are "pushed": move such sets of comment/blank-lines AFTER the net resultant active line.
# For example:
#
# * --> *
# .param .param abc=123 xyz=456
#
# * Comment * Comment
# +abc=123 *...
# *... *---
# +xyz=456 .save all
# *---
# .save all
#
# What to do if line.2 is a continuation and line.1 is a comment?
# (Shouldn't spice treat this as a syntax error)?
# Here we never recognize line.1 as a comment, so line.2 gets
# joined onto end of line.1. Arguably line.1 should be preserved as-is
# always; suppress recognition of continuation at line.2: then if
# line.3 is also a continuation, join it to end of line.2 despite
# result being a longer line.2 but that still starts with '+'.
# remove leading,trailing whitespace or both
function ltrim(s) { sub(/^[[:space:]]+/, "", s); return s }
function rtrim(s) { sub( /[[:space:]]+$/, "", s); return s }
function trim(s) { return rtrim(ltrim(s)); }
function unfold(s) { gsub(/[[:space:]]*\n[+][[:space:]]*/, " ", s); return s }
{ sub(/\r$/,"") }
# collect comments and blank lines retaining newlines
NR > 1 && ( /^\*/ || /^[:space:]*$/ ) {
prevc = prevc $0 "\n"
next
}
# concatenate continuation lines
# We strip all '\n+' (plus neighboring whitespace) later at print of
# the final/total line (fast, compared to editting an ever lengthening
# intermediate line again and again).
NR > 1 && /^\+/ {
prev = prev "\n" $0
next
}
{ if (NR > 1) print unfold(prev); prev = $0; printf "%s", prevc; prevc = "" }
END { if (NR > 0) print unfold(prev); printf "%s", prevc }