initial commit
[bills-tools.git] / calc / calc
bloba112ba72b9bfa3be7bab98a0a228b6a24b475ed4
1 #!/usr/bin/env rc
3 # assign var value -- find var in window and replace its old value with the new one
4 # variables are words made of letters, numbers, and/or _,  followed by '=' and, optionally,  a number
5 fn assign {
6         if (echo -n '/(^|[^a-zA-Z0-9_])'$1' *= */+#0;/[0-9.e+\-]*/'|9p write acme/7/addr >[2] /dev/null) {
7                 echo -n 'dot=addr'|9p write acme/7/ctl
8                 echo -n $2 | 9p write acme/7/data
9         }
10         if not echo 'Could not find variable:' $1
13 # copy calc commands from window into /tmp/cmd
14 9p read acme/$winid/body|sed -e '/^#!/,$p;d'>/tmp/cmd
15 chmod a+x /tmp/cmd
16 rm -f /tmp/cmd.errs
17 9p read acme/7/body |
18                 # extract variables and values from window
19         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' |
20                 # pipe vars (VAR=VALUE) to command
21         /tmp/cmd |
22                 # parse vars from commmand and convert to "assign" calls (above function)
23         awk '{loc=index($0, "=");print "assign (" substr($0, 1, loc - 1) ") (" substr($0, loc + 1) ")"}' |
24                 # pipe assign commands to rc to plug the values back into the window
25         rc
26 if (test -s /tmp/cmd.errs) 9p write acme/$winid/errors < /tmp/cmd.errs
27 exit 0