ref: b4a91c5174ad687bc35971e2342112f975ba363c
parent: bd843e079f1b1fbac275c82b7144c5d4476268e9
author: qwx <qwx@sciops.net>
date: Thu Oct 27 23:43:19 EDT 2022
basic command interface with write command
--- /dev/null
+++ b/cmd.c
@@ -1,0 +1,96 @@
+#include <u.h>
+#include <libc.h>
+#include "dat.h"
+#include "fns.h"
+
+static int
+writediskbuf(int fd)
+{
+ int n, m, iosz;
+
+ seek(ifd, loops, 0);
+ if((iosz = iounit(fd)) == 0)
+ iosz = 8192;
+ for(m=loope-loops; m>0;){
+ n = m < iosz ? m : iosz;
+ if(read(ifd, pcmbuf, n) != n)
+ return -1;
+ if(write(fd, pcmbuf, n) != n)
+ return -1;
+ m -= n;
+ }
+ seek(ifd, seekp, 0);
+ return 0;
+}
+
+static int
+writebuf(int fd)
+{
+ int n, iosz;
+ uchar *p, *e;
+
+ if((iosz = iounit(fd)) == 0)
+ iosz = 8192;
+ for(p=pcmbuf+loops, e=pcmbuf+loope; p<e;){
+ n = e - p < iosz ? e - p : iosz;
+ if(write(fd, p, n) != n)
+ return -1;
+ p += n;
+ }
+ return 0;
+}
+
+/* the entire string is treated as the filename, ie.
+ * spaces and any other weird characters will be part
+ * of it */
+int
+writeto(char *arg)
+{
+ int r, fd;
+
+ if(loope - loops == 0){
+ werrstr("writeto: dot isn't a range");
+ return -1;
+ }
+ if((fd = create(arg, OWRITE, 0664)) < 0){
+ werrstr("writeto: %r");
+ return -1;
+ }
+ r = file ? writediskbuf(fd) : writebuf(fd);
+ close(fd);
+ return r;
+}
+
+int
+cmd(char *s)
+{
+ int n;
+ Rune r, r´;
+
+ /* FIXME: avoid potential conflicts with keys in main() */
+ assert(s != nil);
+ s += chartorune(&r, s);
+ for(;;){
+ n = chartorune(&r´, s);
+ if(r´ == Runeerror || r´ == 0){
+ werrstr("truncated or malformed input");
+ return -1;
+ }
+ if(r´ != ' ' && r´ != '\t')
+ break;
+ s += n;
+ }
+ switch(r){
+// case 'd': delete(s); break;
+// case 'c': cut(s); break;
+// case 'p': paste(s); break;
+// case 'x': crop(s); break;
+// case '^': exchange(s); break;
+// case '|': pipeto(s); break;
+// case '<': pipefrom(s); break;
+ case 'w': return writeto(s);
+// case 'r': readfrom(s); break;
+ default: werrstr("unknown command %C", r); break;
+ }
+ return -1;
+}
--- a/edit.c
+++ /dev/null
@@ -1,36 +1,0 @@
-#include <u.h>
-#include <libc.h>
-#include "dat.h"
-#include "fns.h"
-
-void
-writepcm(char *path)
-{
- int n, fd, sz;
- vlong ofs;
- uchar *p;
-
- if((fd = create(path, OWRITE, 0664)) < 0){
- fprint(2, "create: %r\n");
- return;
- }
- if((sz = iounit(fd)) == 0)
- sz = 8192;
- if(file)
- seek(ifd, loops, 0);
- p = pcmbuf;
- for(ofs=loops; ofs<loope; ofs+=sz){
- n = loope - ofs < sz ? loope - ofs : sz;
- if(!file)
- p = pcmbuf + ofs;
- else if(read(ifd, p, n) != n)
- fprint(2, "read: %r\n");
- if(write(fd, p, n) != n){
- fprint(2, "write: %r\n");
- break;
- }
- }
- close(fd);
- if(file)
- seek(ifd, seekp, 0);
-}
--- a/fns.h
+++ b/fns.h
@@ -1,4 +1,4 @@
-void writepcm(char*);
+int cmd(char*);
void update(void);
void setzoom(int, int);
void setpan(int);
--- a/mkfile
+++ b/mkfile
@@ -3,8 +3,8 @@
MAN=/sys/man/1
TARG=pplay
OFILES=\
+ cmd.$O\
draw.$O\
- edit.$O\
pplay.$O\
HFILES=dat.h fns.h
--- a/pplay.c
+++ b/pplay.c
@@ -77,13 +77,13 @@
}
static char *
-prompt(void)
+prompt(Rune r)
{
int n;
- static char buf[256];
+ static char buf[512];
- n = enter("path:", buf, sizeof(buf)-UTFmax, mc, kc, _screen);
- if(n < 0)
+ snprint(buf, sizeof buf, "%C", r);
+ if((n = enter(nil, buf, sizeof(buf)-UTFmax, mc, kc, _screen)) < 0)
return nil;
return buf;
}
@@ -189,7 +189,7 @@
switch(r){
case ' ': toggleplay(); break;
case 'b': setpos(loops); break;
- case 'r': loops = 0; loope = filesz; update(); break;
+ case Kesc: loops = 0; loope = filesz; update(); break;
case Kdel:
case 'q': threadexitsall(nil);
case 'z': setzoom(-zoom + 1, 0); break;
@@ -197,9 +197,11 @@
case '=': setzoom(1, 0); break;
case '_': setzoom(-1, 1); break;
case '+': setzoom(1, 1); break;
- case 'w':
- if((p = prompt()) != nil)
- writepcm(p);
+ default:
+ if((p = prompt(r)) == nil)
+ break;
+ if(cmd(p) < 0)
+ fprint(2, "cmd \"%s\" failed: %r\n", p);
break;
}
break;