getfields.c
authorRonald G. Minnich <rminnich@google.com>
Fri, 17 Jan 2014 20:53:27 +0000 (12:53 -0800)
committerRonald G. Minnich <rminnich@google.com>
Fri, 17 Jan 2014 20:53:27 +0000 (12:53 -0800)
I did this in a way that we can go back to utf if we wish.

Signed-off-by: Ronald G. Minnich <rminnich@google.com>
kern/src/ns/Kbuild
kern/src/ns/getfields.c [new file with mode: 0644]

index 7616cc5..e921d55 100644 (file)
@@ -7,6 +7,7 @@ obj-y                                           += convM2D.o
 obj-y                                          += convM2S.o
 obj-y                                          += convS2M.o
 obj-y                                          += error.o
+obj-y                                          += getfields.o
 obj-y                                          += parse.o
 obj-y                                          += pgrp.o
 obj-y                                          += qio.o
diff --git a/kern/src/ns/getfields.c b/kern/src/ns/getfields.c
new file mode 100644 (file)
index 0000000..580a652
--- /dev/null
@@ -0,0 +1,55 @@
+// INFERNO
+#include <vfs.h>
+#include <kfs.h>
+#include <slab.h>
+#include <kmalloc.h>
+#include <kref.h>
+#include <string.h>
+#include <stdio.h>
+#include <assert.h>
+#include <error.h>
+#include <cpio.h>
+#include <pmap.h>
+#include <smp.h>
+#include <ip.h>
+
+int
+getfields(char *str, char **args, int max, int mflag, char *set)
+{
+       //Rune r;
+       int next = 0;
+       int r;
+       int nr, intok, narg;
+
+       if(max <= 0)
+               return 0;
+
+       narg = 0;
+       args[narg] = str;
+       if(!mflag)
+               narg++;
+       intok = 0;
+       for(;; str += nr) {
+               r = str[next++];
+               //nr = chartorune(&r, str);
+               nr = 1;
+               
+               if(r == 0)
+                       break;
+               //if(utfrune(set, r)) {
+               if(strchr(set, r)) {
+                       if(narg >= max)
+                               break;
+                       *str = 0;
+                       intok = 0;
+                       args[narg] = str + nr;
+                       if(!mflag)
+                               narg++;
+               } else {
+                       if(!intok && mflag)
+                               narg++;
+                       intok = 1;
+               }
+       }
+       return narg;
+}