aboutsummaryrefslogtreecommitdiff
path: root/sys/fmtio/strmac.x
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@gmail.com>2015-07-08 20:46:52 -0400
committerJoseph Hunkeler <jhunkeler@gmail.com>2015-07-08 20:46:52 -0400
commitfa080de7afc95aa1c19a6e6fc0e0708ced2eadc4 (patch)
treebdda434976bc09c864f2e4fa6f16ba1952b1e555 /sys/fmtio/strmac.x
downloadiraf-linux-fa080de7afc95aa1c19a6e6fc0e0708ced2eadc4.tar.gz
Initial commit
Diffstat (limited to 'sys/fmtio/strmac.x')
-rw-r--r--sys/fmtio/strmac.x86
1 files changed, 86 insertions, 0 deletions
diff --git a/sys/fmtio/strmac.x b/sys/fmtio/strmac.x
new file mode 100644
index 00000000..1f3f3e98
--- /dev/null
+++ b/sys/fmtio/strmac.x
@@ -0,0 +1,86 @@
+# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.
+
+include <ctype.h>
+
+define MAX_ARGS 9
+
+.help strmac
+.nf ___________________________________________________________________________
+STRMAC -- Expand a macro (nonrecursively) by string substitution.
+The macro string may contain zero or more occurrences of the sequences
+"$1" through "$9". The substitution strings are passed in the string
+buffer "argstr", wherein successive strings are delimited by the EOS marker.
+A double EOS marks the end of the list.
+
+Macros $1-$9 are replaced by the substitution string. The sequence $$ is
+replaced by a single $. If any other character follows the $, both the $
+and the following character are passed to the output unchanged. An error
+action is taken if there are insufficient arguments or if the output buffer
+overflows. Bugs: null substitution strings don't work.
+.endhelp ______________________________________________________________________
+
+int procedure strmac (macro, argstr, outstr, maxch)
+
+char macro[ARB] # substitution string
+char argstr[ARB] # argument strings, if any
+char outstr[maxch] # output string
+int maxch
+
+short offset[MAX_ARGS]
+char ch
+int i, ip, op, arg, nargs, nchars
+int strlen()
+
+begin
+ # Determine the offsets of the argument strings.
+ ip = 1
+ for (nargs=1; nargs <= MAX_ARGS; nargs=nargs+1) {
+ nchars = strlen (argstr[ip])
+ if (nchars > 0) {
+ offset[nargs] = ip
+ ip = ip + nchars + 1
+ } else
+ break
+ }
+ nargs = nargs - 1
+
+ # Expand the macro.
+ op = 1
+ for (ip=1; macro[ip] != EOS; ip=ip+1) {
+ ch = macro[ip]
+
+ if (ch == '$') { # Process $ arg sequence.
+ ip = ip + 1
+ ch = macro[ip]
+ if (ch >= '1' && ch <= '9') {
+ arg = TO_INTEG(ch)
+ if (arg > nargs)
+ call error (1, "Strmac: too few substitution arguments")
+ for (i = offset[arg]; argstr[i] != EOS; i=i+1) {
+ outstr[op] = argstr[i]
+ op = op + 1
+ }
+
+ } else if (ch == '$') { # "$$" --> "$"
+ outstr[op] = '$'
+ op = op + 1
+
+ } else { # "$?" --> "$?"
+ outstr[op] = '$'
+ op = op + 1
+ outstr[op] = ch
+ op = op + 1
+ }
+
+ } else { # ordinary character
+ outstr[op] = ch
+ op = op + 1
+ }
+
+ if (op > maxch)
+ call error (2, "Strmac: output buffer overflow")
+ }
+
+ outstr[op] = EOS
+ return (op - 1)
+end