From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 11345 invoked from network); 2 Jun 2021 12:21:59 -0000 Received: from 1ess.inri.net (216.126.196.35) by inbox.vuxu.org with ESMTPUTF8; 2 Jun 2021 12:21:59 -0000 Received: from mx.sdf.org ([205.166.94.24]) by 1ess; Tue Jun 1 20:13:26 -0400 2021 Received: from sdf.org (IDENT:adr@sdf.org [205.166.94.16]) by mx.sdf.org (8.15.2/8.14.5) with ESMTPS id 1520A2tD011781 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256 bits) verified NO) for <9front@9front.org>; Wed, 2 Jun 2021 00:10:02 GMT Received: (from adr@localhost) by sdf.org (8.15.2/8.12.8/Submit) id 1520A29g003314 for 9front@9front.org; Wed, 2 Jun 2021 00:10:02 GMT Date: Wed, 2 Jun 2021 00:10:02 +0000 From: adr@SDF.ORG To: 9front@9front.org Message-ID: Mail-Followup-To: 9front@9front.org MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline List-ID: <9front.9front.org> List-Help: X-Glyph: ➈ X-Bullshit: responsive object-oriented replication reduce/map callback optimizer Subject: [9front] Quoted string support in proto.c Reply-To: 9front@9front.org Precedence: bulk Hi, this patch allows to use quoted strings on proto files. I use it to copy files with special characters with mkfs. It is a dirty hack, the code should be rewritten with tokenize(2) in mind. Regards, adr. diff -r cc48d5f4eaa7 sys/src/libdisk/proto.c --- a/sys/src/libdisk/proto.c Mon May 31 19:30:07 2021 -0700 +++ b/sys/src/libdisk/proto.c Wed Jun 02 00:01:00 2021 +0000 @@ -67,8 +67,8 @@ static void freeoptptr(Opt*, void*); static char* getline(Mkaux*); static File* getfile(Mkaux*, File*); -static char* getmode(Mkaux*, char*, ulong*); -static char* getname(Mkaux*, char*, char**); +static int getmode(Mkaux*, char*, ulong*); +static int getname(Mkaux*, char*, char**); static char* getpath(Mkaux*, char*); static int mkfile(Mkaux*, File*); static char* mkpath(Mkaux*, char*, char*); @@ -532,8 +532,9 @@ getfile(Mkaux *mkaux, File *old) { File *f; - char *elem; - char *p, *s; + char *elem, *tbuf[5]; + char *p, *s, c; + int n, i; loop: if((p = getline(mkaux)) == nil) @@ -548,7 +549,15 @@ }else p[strlen(p)] = '\n'; - if((p = getname(mkaux, p, &elem)) == nil) + while((c = *p) == ' ' || c == '\t') + p++; + + n = tokenize(p, tbuf, 5); + if(n < 5) + for(i = n; i < 5; i++) + tbuf[i] = ""; + + if((getname(mkaux, tbuf[0], &elem)) == -1) return nil; f = emalloc(mkaux, sizeof *f); @@ -556,26 +565,27 @@ free(elem); f->elem = utfrrune(f->new, L'/') + 1; - if((p = getmode(mkaux, p, &f->mode)) == nil){ + if(getmode(mkaux, tbuf[1], &f->mode) == -1){ freefile(f); return nil; } - if((p = getname(mkaux, p, &f->uid)) == nil){ + if(getname(mkaux, tbuf[2], &f->uid) == -1){ freefile(f); return nil; } if(*f->uid == 0) strcpy(f->uid, "-"); - if((p = getname(mkaux, p, &f->gid)) == nil){ + if(getname(mkaux, tbuf[3], &f->gid) == -1){ freefile(f); return nil; } if(*f->gid == 0) strcpy(f->gid, "-"); - f->old = getpath(mkaux, p); + f->old = getpath(mkaux, tbuf[4]); + if(f->old != nil && strcmp(f->old, "-") == 0){ free(f->old); f->old = nil; @@ -589,42 +599,31 @@ static char* getpath(Mkaux *mkaux, char *p) { - char *q, *new; - int c, n; - - while((c = *p) == ' ' || c == '\t') - p++; - q = p; - while((c = *q) != '\n' && c != ' ' && c != '\t') - q++; - if(q == p) + char *new; + int n; + + n = strlen(p); + if(n == 0) return nil; - n = q - p; new = emalloc(mkaux, n + 1); memcpy(new, p, n); new[n] = 0; return new; } -static char* +static int getname(Mkaux *mkaux, char *p, char **buf) { - char *s, *start; - int c; + char *s; + int namelen; - while((c = *p) == ' ' || c == '\t') - p++; + namelen = strlen(p); + *buf = malloc(namelen+2); /* +2: need at least 2 bytes; might strcpy "-" into buf */ + if(*buf == nil) + return -1; + memmove(*buf, p, namelen); - start = p; - while((c = *p) != '\n' && c != ' ' && c != '\t') - p++; - - *buf = malloc(p+2-start); /* +2: need at least 2 bytes; might strcpy "-" into buf */ - if(*buf == nil) - return nil; - memmove(*buf, start, p-start); - - (*buf)[p-start] = 0; + (*buf)[namelen] = 0; if(**buf == '$'){ s = getenv(*buf+1); @@ -632,27 +631,27 @@ warn(mkaux, "can't read environment variable %s", *buf+1); free(*buf); skipdir(mkaux); - return nil; + return -1; } free(*buf); *buf = s; } - return p; + return 0; } -static char* +static int getmode(Mkaux *mkaux, char *p, ulong *xmode) { char *buf, *s; ulong m; *xmode = ~0; - if((p = getname(mkaux, p, &buf)) == nil) - return nil; + if(getname(mkaux, p, &buf) == -1) + return -1; s = buf; if(*s == 0 || strcmp(s, "-") == 0) - return p; + return 0; m = 0; if(*s == 'd'){ m |= DMDIR; @@ -672,11 +671,11 @@ || s[3]){ warn(mkaux, "bad mode specification %s", buf); free(buf); - return p; + return 0; } *xmode = m | strtoul(s, 0, 8); free(buf); - return p; + return 0; } static void