9front - general discussion about 9front
 help / color / mirror / Atom feed
* [9front] Quoted string support in proto.c
@ 2021-06-02  0:10 adr
  2021-06-05  5:11 ` [9front] " adr
  0 siblings, 1 reply; 2+ messages in thread
From: adr @ 2021-06-02  0:10 UTC (permalink / raw)
  To: 9front

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


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-06-05 11:13 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-02  0:10 [9front] Quoted string support in proto.c adr
2021-06-05  5:11 ` [9front] " adr

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).