added LICENSE files
[bills-tools.git] / calc / calc
Commit [+]AuthorDateLineData
a99794b5
BB
Bill Burdick2010-10-22 13:30:30 +02001#!/usr/bin/env rc
2
2732974c
BB
Bill Burdick2010-10-22 13:35:42 +02003# calc, (c) Oct, 2010, by Bill Burdick
4# licensed with the ZLIB license (see the LICENSE file)
5
a99794b5
BB
Bill Burdick2010-10-22 13:30:30 +02006# assign var value -- find var in window and replace its old value with the new one
7# variables are words made of letters, numbers, and/or _, followed by '=' and, optionally, a number
8fn assign {
7f9452fe
BB
Bill Burdick2010-10-22 14:10:23 +02009 if (echo -n '/(^|[^a-zA-Z0-9_])'$1' *= */+#0;/[0-9.e+\-]*/'|9p write acme/$winid/addr >[2] /dev/null) {
10 echo -n 'dot=addr'|9p write acme/$winid/ctl
11 echo -n $2 | 9p write acme/$winid/data
a99794b5 Bill Burdick2010-10-22 13:30:30 +020012 }
7f9452fe Bill Burdick2010-10-22 14:10:23 +020013 if not echo 'Could not find variable: "'$1'"'
a99794b5
BB
Bill Burdick2010-10-22 13:30:30 +020014}
15
16# copy calc commands from window into /tmp/cmd
179p read acme/$winid/body|sed -e '/^#!/,$p;d'>/tmp/cmd
18chmod a+x /tmp/cmd
19rm -f /tmp/cmd.errs
7f9452fe Bill Burdick2010-10-22 14:10:23 +0200209p read acme/$winid/body |
a99794b5
BB
Bill Burdick2010-10-22 13:30:30 +020021 # extract variables and values from window
22 sed -n -e '/^#!/q;s/([a-zA-Z0-9_]+) *= *([0-9.e+\-]*)/\n\1=\2\n/g;h;:v;s/\n(.|\n)*//;s/^[a-zA-Z0-9_]+=$/&0/;/^[a-zA-Z0-9_]+=[0-9.e+\-]*/p;g;/\n/!d;s/[^\n]*\n//;h;bv' |
23 # pipe vars (VAR=VALUE) to command
24 /tmp/cmd |
25 # parse vars from commmand and convert to "assign" calls (above function)
26 awk '{loc=index($0, "=");print "assign (" substr($0, 1, loc - 1) ") (" substr($0, loc + 1) ")"}' |
27 # pipe assign commands to rc to plug the values back into the window
28 rc
29if (test -s /tmp/cmd.errs) 9p write acme/$winid/errors < /tmp/cmd.errs
30exit 0