shithub: gemnine

Download patch

ref: cca33c9b5be9c260dadac1a092f8f5007806a09b
parent: a38c9ea785ebf36491f5fa5e3e7cb3745ca2731c
author: Sigrid Haflínudóttir <ftrvxmtrx@gmail.com>
date: Sun May 17 16:40:52 EDT 2020

implement INPUT

--- a/main.c
+++ b/main.c
@@ -23,6 +23,8 @@
 	int fd;
 };
 
+#pragma varargck type "E" char*
+
 Url *
 parseurl(char *url)
 {
@@ -164,12 +166,54 @@
 	return nil;
 }
 
+int
+Efmt(Fmt *f)
+{
+	char *s;
+
+	s = va_arg(f->args, char*);
+	for(; *s; s++){
+		if(*s == '%' && isxdigit(s[1]) && isxdigit(s[2])){
+			fmtprint(f, "%%%c%c", toupper(s[1]), toupper(s[2]));
+			s += 2;
+		}else if(isalnum(*s) || strchr(".-_~!$&'()*,;=/:@ \n", *s) == nil){
+			fmtprint(f, "%c", *s);
+		}else{
+			fmtprint(f, "%%%.2X", *s & 0xff);
+		}
+	}
+	return 0;
+}
+
+char *
+readall(int fd)
+{
+	char *s;
+	int n, sz, bufsz;
+
+	bufsz = 1023;
+	s = nil;
+	for(sz = 0;; sz += n){
+		if(bufsz-sz < 1024){
+			bufsz *= 2;
+			s = realloc(s, bufsz);
+		}
+		if((n = read(fd, s+sz, bufsz-sz-1)) < 1)
+			break;
+	}
+	s[sz] = 0;
+	if(sz > 1 && s[sz-1] == '\n')
+		s[sz-1] = 0;
+
+	return s;
+}
+
 void
 main(int argc, char **argv)
 {
 	Response *r;
 	char *s, *t, *u, *url;
-	int len, wait, pl;
+	int len, wait, pl, fd;
 	Plumbmsg *m;
 	Biobuf out;
 
@@ -185,6 +229,7 @@
 		exits("usage");
 	}
 
+	fmtinstall('E', Efmt);
 	quotefmtinstall();
 	Binit(&out, 1, OWRITE);
 	pl = -1;
@@ -205,6 +250,7 @@
 		url = strdup(argv[0]);
 	}
 
+nextreq:
 	if((r = request(url)) != nil){
 		if(r->mime != nil && strncmp(r->mime, "text/", 5) != 0){
 			/* FIXME handle in a better way */
@@ -211,8 +257,19 @@
 			if(r->mime != nil)
 				fprint(2, "MIME %s\n", r->mime);
 		}else if(r->prompt != nil){
-			/* FIXME no idea */
-			fprint(2, "INPUT %s\n", r->prompt);
+			if((fd = open("/dev/consctl", OWRITE)) >= 0){
+				write(fd, "holdon", 6);
+				print("%s\n", r->prompt);
+				s = readall(0);
+				free(url);
+				url = smprint("%s?%E", r->url->url, s);
+				free(s);
+				freeresponse(r);
+				close(fd);
+				goto nextreq;
+			}else{
+				fprint(2, "INPUT %s\n", r->prompt);
+			}
 		}else{
 			while((s = Brdstr(&r->body, '\n', 1)) != nil){
 				if((len = Blinelen(&r->body)) > 0)