tawk: sync with Plan 9 - plan9port - [fork] Plan 9 from user space
git clone git://src.adamsgaard.dk/plan9port
Log
Files
Refs
README
LICENSE
---
commit 5a8bc78967fef8f97da30b4368ca7e79dfdd274f
parent 3b7ca01f981115207ddee1b91085595a3ccb608a
Author: Michael Teichgräber 
Date:   Tue, 11 Aug 2009 20:16:30 -0400

awk: sync with Plan 9

http://codereview.appspot.com/104087

Diffstat:
  M src/cmd/awk/run.c                   |      45 +++++++++++++++++++++++---------

1 file changed, 33 insertions(+), 12 deletions(-)
---
diff --git a/src/cmd/awk/run.c b/src/cmd/awk/run.c
t@@ -30,6 +30,7 @@ THIS SOFTWARE.
 #include 
 #include 
 #include 
+#include 
 #include "awk.h"
 #include "y.tab.h"
 
t@@ -1194,10 +1195,9 @@ Cell *dopa2(Node **a, int n)        /* a[0], a[1] { a[2] } */
 Cell *split(Node **a, int nnn)        /* split(a[0], a[1], a[2]); a[3] is type */
 {
         Cell *x = 0, *y, *ap;
-        char *s;
-        int sep;
-        char *t, temp, num[50], *fs = 0;
-        int n, arg3type;
+        char *s, *t, *fs = 0;
+        char temp, num[50];
+        int n, nb, sep, arg3type;
 
         y = execute(a[0]);        /* source string */
         s = getsval(y);
t@@ -1279,12 +1279,15 @@ Cell *split(Node **a, int nnn)        /* split(a[0], a[1], a[2]); a[3] is type */
                                 s++;
                 }
         } else if (sep == 0) {        /* new: split(s, a, "") => 1 char/elem */
-                for (n = 0; *s != 0; s++) {
-                        char buf[2];
+                for (n = 0; *s != 0; s += nb) {
+                        Rune r;
+                        char buf[UTFmax+1];
+
                         n++;
-                        sprintf(num, "%d", n);
-                        buf[0] = *s;
-                        buf[1] = 0;
+                        snprintf(num, sizeof num, "%d", n);
+                        nb = chartorune(&r, s);
+                        memmove(buf, s, nb);
+                        buf[nb] = '\0';
                         if (isdigit(buf[0]))
                                 setsymtab(num, buf, atof(buf), STR|NUM, (Array *) ap->sval);
                         else
t@@ -1451,14 +1454,20 @@ Cell *bltin(Node **a, int n)        /* builtin functions. a[0] is type, a[1] is arg lis
         char mbc[50];
         Node *nextarg;
         FILE *fp;
+        void flush_all(void);
 
         t = ptoi(a[0]);
         x = execute(a[1]);
         nextarg = a[1]->nnext;
         switch (t) {
         case FLENGTH:
-                p = getsval(x);
-                u = (Awkfloat) countposn(p, strlen(p)); break;
+                if (isarr(x))
+                        u = ((Array *) x->sval)->nelem;        /* GROT. should be function*/
+                else {
+                        p = getsval(x);
+                        u = (Awkfloat) countposn(p, strlen(p));
+                }
+                break;
         case FLOG:
                 u = errcheck(log(getfval(x)), "log"); break;
         case FINT:
t@@ -1515,7 +1524,10 @@ Cell *bltin(Node **a, int n)        /* builtin functions. a[0] is type, a[1] is arg lis
                 free(buf);
                 return x;
         case FFLUSH:
-                if ((fp = openfile(FFLUSH, getsval(x))) == NULL)
+                if (isrec(x) || strlen(getsval(x)) == 0) {
+                        flush_all();        /* fflush() or fflush("") -> all */
+                        u = 0;
+                } else if ((fp = openfile(FFLUSH, getsval(x))) == NULL)
                         u = EOF;
                 else
                         u = fflush(fp);
t@@ -1710,6 +1722,15 @@ void closeall(void)
                 }
 }
 
+void flush_all(void)
+{
+        int i;
+
+        for (i = 0; i < FOPEN_MAX; i++)
+                if (files[i].fp)
+                        fflush(files[i].fp);
+}
+
 void backsub(char **pb_ptr, char **sptr_ptr);
 
 Cell *sub(Node **a, int nnn)        /* substitute command */