1 #include "lib.h" 2 #include <stdio.h> 3 #include <string.h> 4 #include <stdlib.h> 5 #include "sys9.h" 6 #include "dir.h" 7 8 static char qsep[] = " \t\r\n"; 9 10 static char* 11 qtoken(char *s) 12 { 13 int quoting; 14 char *t; 15 16 quoting = 0; 17 t = s; /* s is output string, t is input string */ 18 while(*t!='\0' && (quoting || strchr(qsep, *t)==nil)){ 19 if(*t != '\''){ 20 *s++ = *t++; 21 continue; 22 } 23 /* *t is a quote */ 24 if(!quoting){ 25 quoting = 1; 26 t++; 27 continue; 28 } 29 /* quoting and we're on a quote */ 30 if(t[1] != '\''){ 31 /* end of quoted section; absorb closing quote */ 32 t++; 33 quoting = 0; 34 continue; 35 } 36 /* doubled quote; fold one quote into two */ 37 t++; 38 *s++ = *t++; 39 } 40 if(*s != '\0'){ 41 *s = '\0'; 42 if(t == s) 43 t++; 44 } 45 return t; 46 } 47 48 static int 49 tokenize(char *s, char **args, int maxargs) 50 { 51 int nargs; 52 53 for(nargs=0; nargs<maxargs; nargs++){ 54 while(*s!='\0' && strchr(qsep, *s)!=nil) 55 s++; 56 if(*s == '\0') 57 break; 58 args[nargs] = s; 59 s = qtoken(s); 60 } 61 62 return nargs; 63 } 64 65 Waitmsg* 66 _WAIT(void) 67 { 68 int n, l; 69 char buf[512], *fld[5]; 70 Waitmsg *w; 71 72 n = _AWAIT(buf, sizeof buf-1); 73 if(n < 0) 74 return nil; 75 buf[n] = '\0'; 76 if(tokenize(buf, fld, 5) != 5){ 77 strcpy(buf, "couldn't parse wait message"); 78 _ERRSTR(buf, sizeof buf); 79 return nil; 80 } 81 l = strlen(fld[4])+1; 82 w = malloc(sizeof(Waitmsg)+l); 83 if(w == nil) 84 return nil; 85 w->pid = atoi(fld[0]); 86 w->time[0] = atoi(fld[1]); 87 w->time[1] = atoi(fld[2]); 88 w->time[2] = atoi(fld[3]); 89 w->msg = (char*)&w[1]; 90 memmove(w->msg, fld[4], l); 91 return w; 92 } 93 94