summaryrefslogtreecommitdiff
path: root/sys/src/ape/9src
diff options
context:
space:
mode:
authorTaru Karttunen <taruti@taruti.net>2011-03-30 15:46:40 +0300
committerTaru Karttunen <taruti@taruti.net>2011-03-30 15:46:40 +0300
commite5888a1ffdae813d7575f5fb02275c6bb07e5199 (patch)
treed8d51eac403f07814b9e936eed0c9a79195e2450 /sys/src/ape/9src
Import sources from 2011-03-30 iso image
Diffstat (limited to 'sys/src/ape/9src')
-rwxr-xr-xsys/src/ape/9src/mkfile9
-rwxr-xr-xsys/src/ape/9src/stty.c266
-rwxr-xr-xsys/src/ape/9src/tar.c127
-rwxr-xr-xsys/src/ape/9src/tty.h127
4 files changed, 529 insertions, 0 deletions
diff --git a/sys/src/ape/9src/mkfile b/sys/src/ape/9src/mkfile
new file mode 100755
index 000000000..739ce3ed7
--- /dev/null
+++ b/sys/src/ape/9src/mkfile
@@ -0,0 +1,9 @@
+</$objtype/mkfile
+
+# ptyfs\ # for X11, not going to bother
+TARG=\
+ stty\
+ tar\
+
+BIN=/$objtype/bin/ape
+</sys/src/cmd/mkmany
diff --git a/sys/src/ape/9src/stty.c b/sys/src/ape/9src/stty.c
new file mode 100755
index 000000000..54d92ee81
--- /dev/null
+++ b/sys/src/ape/9src/stty.c
@@ -0,0 +1,266 @@
+#include <u.h>
+#include <libc.h>
+#include <tty.h>
+
+typedef struct Mode Mode;
+struct Mode
+{
+ char* name;
+ int bit;
+};
+
+Mode ou[] =
+{
+ "opost", OPOST,
+ "olcuc", OLCUC,
+ "onlcr", ONLCR,
+ "ocrnl", OCRNL,
+ "onocr", ONOCR,
+ "onlret", ONLRET,
+ "ofill", OFILL,
+ "ofdel", OFDEL,
+ 0
+};
+
+Mode in[] =
+{
+ "brkint", BRKINT,
+ "icrnl", ICRNL,
+ "ignbrk", IGNBRK,
+ "igncr", IGNCR,
+ "ignpar", IGNPAR,
+ "inlcr", INLCR,
+ "inpck", INPCK,
+ "istrip", ISTRIP,
+ "ixoff", IXOFF,
+ "ixon", IXON,
+ "parmrk", PARMRK,
+ 0
+};
+
+Mode lo[] =
+{
+ "echo", ECHO,
+ "echoe", ECHOE,
+ "echok", ECHOK,
+ "echonl", ECHONL,
+ "icanon", ICANON,
+ "iexten", IEXTEN,
+ "isig", ISIG,
+ "noflsh", NOFLSH,
+ "tostop", TOSTOP,
+ 0
+};
+
+Mode cc[] =
+{
+ "eof", VEOF,
+ "eol", VEOL,
+ "erase", VERASE,
+ "intr", VINTR,
+ "kill", VKILL,
+ "min", VMIN,
+ "quit", VQUIT,
+ "susp", VSUSP,
+ "time", VTIME,
+ "start", VSTART,
+ "stop", VSTOP,
+ 0,
+};
+
+int getmode(int, Termios*);
+int setmode(int, Termios*);
+
+char*
+ctlchar(char c)
+{
+ static char buf[10];
+
+ if(c == 0x7f)
+ return "DEL";
+ if(c == 0)
+ return "NUL";
+ if(c < 32) {
+ buf[0] = '^';
+ buf[1] = '@'+c;
+ buf[2] = '\0';
+ return buf;
+ }
+ buf[0] = c;
+ buf[1] = '\0';
+ return buf;
+}
+
+void
+showmode(Termios *t)
+{
+ int i;
+
+ for(i = 0; cc[i].name; i++) {
+ switch(cc[i].bit) {
+ case VMIN:
+ case VTIME:
+ if(t->cc[i] != 0)
+ print("%s %d ", cc[i].name, t->cc[i]);
+ break;
+ default:
+ print("%s %s ", cc[i].name, ctlchar(t->cc[i]));
+ break;
+ }
+ }
+ print("\n");
+
+ for(i = 0; ou[i].name; i++)
+ if(ou[i].bit & t->oflag)
+ print("%s ", ou[i].name);
+
+ for(i = 0; in[i].name; i++)
+ if(in[i].bit & t->iflag)
+ print("%s ", in[i].name);
+
+ print("\n");
+ for(i = 0; lo[i].name; i++)
+ if(lo[i].bit & t->lflag)
+ print("%s ", lo[i].name);
+ print("\n");
+}
+
+int
+setreset(char *mode, int *bits, Mode *t)
+{
+ int i, clr;
+
+ clr = 0;
+ if(mode[0] == '-') {
+ mode++;
+ clr = 1;
+ }
+ for(i = 0; t[i].name; i++) {
+ if(strcmp(mode, t[i].name) == 0) {
+ if(clr)
+ *bits &= ~t[i].bit;
+ else
+ *bits |= t[i].bit;
+
+ return 1;
+ }
+ }
+ return 0;
+}
+
+int
+ccname(char *name)
+{
+ int i;
+
+ for(i = 0; cc[i].name; i++)
+ if(strcmp(cc[i].name, name) == 0)
+ return i;
+
+ return -1;
+}
+
+void
+main(int argc, char **argv)
+{
+ Termios t;
+ int i, stdin, wmo, cc;
+
+ /* Try and get a seek pointer */
+ stdin = open("/fd/0", ORDWR);
+ if(stdin < 0)
+ stdin = 0;
+
+ if(getmode(stdin, &t) < 0) {
+ fprint(2, "stty: tiocget %r\n");
+ exits("1");
+ }
+
+ if(argc < 2) {
+ fprint(2, "usage: stty [-a|-g] modes...\n");
+ exits("1");
+ }
+ wmo = 0;
+ for(i = 1; i < argc; i++) {
+ if(strcmp(argv[i], "-a") == 0) {
+ showmode(&t);
+ continue;
+ }
+ if(setreset(argv[i], &t.iflag, in)) {
+ wmo++;
+ continue;
+ }
+ if(setreset(argv[i], &t.lflag, lo)) {
+ wmo++;
+ continue;
+ }
+ if(setreset(argv[i], &t.oflag, ou)) {
+ wmo++;
+ continue;
+ }
+ cc = ccname(argv[i]);
+ if(cc != -1 && i+1 < argc) {
+ wmo++;
+ t.cc[cc] = argv[++i][0];
+ continue;
+ }
+ fprint(2, "stty: bad option/mode %s\n", argv[i]);
+ exits("1");
+ }
+
+ if(wmo) {
+ if(setmode(stdin, &t) < 0) {
+ fprint(2, "stty: cant set mode %r\n");
+ exits("1");
+ }
+ }
+
+ exits(0);
+}
+
+int
+setmode(int fd, Termios *t)
+{
+ int n, i;
+ char buf[256];
+
+ n = sprint(buf, "IOW %4.4ux %4.4ux %4.4ux %4.4ux ",
+ t->iflag, t->oflag, t->cflag, t->lflag);
+ for(i = 0; i < NCCS; i++)
+ n += sprint(buf+n, "%2.2ux ", t->cc[i]);
+
+ if(seek(fd, -2, 0) != -2)
+ return -1;
+
+ n = write(fd, buf, n);
+ if(n < 0)
+ return -1;
+ return 0;
+}
+
+/*
+ * Format is: IOR iiii oooo cccc llll xx xx xx xx ...
+ */
+int
+getmode(int fd, Termios *t)
+{
+ int n;
+ char buf[256];
+
+ if(seek(fd, -2, 0) != -2)
+ return -1;
+
+ n = read(fd, buf, 57);
+ if(n < 0)
+ return -1;
+
+ t->iflag = strtoul(buf+4, 0, 16);
+ t->oflag = strtoul(buf+9, 0, 16);
+ t->cflag = strtoul(buf+14, 0, 16);
+ t->lflag = strtoul(buf+19, 0, 16);
+
+ for(n = 0; n < NCCS; n++)
+ t->cc[n] = strtoul(buf+24+(n*3), 0, 16);
+
+ return 0;
+}
diff --git a/sys/src/ape/9src/tar.c b/sys/src/ape/9src/tar.c
new file mode 100755
index 000000000..b72e8d701
--- /dev/null
+++ b/sys/src/ape/9src/tar.c
@@ -0,0 +1,127 @@
+/*
+ * Attempt at emulation of Unix tar by calling Plan 9 tar.
+ *
+ * The differences from Plan 9 tar are:
+ * In the absence of an "f" flag, the file /dev/tape is used.
+ * An "f" flag with argument "-" causes use of stdin/stdout
+ * by passing no "f" flag (nor argument) to Plan 9 tar.
+ * By default, the "T" flag is passed to Plan 9 tar.
+ * The "m" flag to this tar inhibits this behavior.
+ */
+
+#include <u.h>
+#include <libc.h>
+
+void
+usage(void)
+{
+ fprint(2, "usage: ape/tar [crtx][vfm] [args...] [file...]\n");
+ exits("usage");
+}
+
+void
+main(int argc, char **argv)
+{
+ int i, j, verb, vflag, fflag, Tflag, nargc;
+ char *p, *file, **nargv, *cpu, flagbuf[10], execbuf[128];
+ Waitmsg *w;
+
+ argv++, argc--;
+ if(argc < 1)
+ usage();
+
+ p = argv[0];
+ argv++, argc--;
+
+ if(*p == '-')
+ p++;
+
+ if(strchr("crtx", *p) == nil)
+ usage();
+ verb = *p++;
+
+ /* unix defaults */
+ fflag = 1;
+ file = "/dev/tape";
+ Tflag = 1;
+ vflag = 0;
+
+ for(; *p; p++) {
+ switch(*p) {
+ default:
+ usage();
+ case 'v':
+ vflag = 1;
+ break;
+ case 'f':
+ if(argc <= 0)
+ usage();
+
+ fflag = 1;
+ file = argv[0];
+ argv++, argc--;
+ if(strcmp(file, "-") == 0) {
+ /*
+ * plan9 doesn't know about "-" meaning stdin/stdout,
+ * but it's the default,
+ * so rewrite to not use f flag at all.
+ */
+ file = nil;
+ fflag = 0;
+ }
+ break;
+ case 'm':
+ Tflag = 0;
+ break;
+ case 'p': /* pretend nothing's wrong */
+ break;
+ }
+ }
+
+ nargc = 1 + 1 + fflag + argc + 1;
+ nargv = malloc(sizeof(char*) * nargc);
+ if(nargv == nil) {
+ fprint(2, "ape/tar: out of memory\n");
+ exits("memory");
+ }
+
+ cpu = getenv("cputype");
+ if(cpu == nil) {
+ fprint(2, "ape/tar: need cputype environment variable set\n");
+ exits("cputype");
+ }
+ snprint(execbuf, sizeof execbuf, "/%s/bin/tar", cpu);
+
+ nargv[0] = "tar";
+ sprint(flagbuf, "%c%s%s%s", verb, vflag ? "v" : "", Tflag ? "T" : "", fflag ? "f" : "");
+ nargv[1] = flagbuf;
+
+ i = 2;
+ if(fflag)
+ nargv[i++] = file;
+
+ for(j=0; j<argc; j++, i++)
+ nargv[i] = argv[j];
+
+ nargv[i++] = nil;
+ assert(i == nargc);
+
+ switch(fork()){
+ case -1:
+ fprint(2, "ape/tar: fork failed: %r\n");
+ exits("fork");
+ case 0:
+ exec(execbuf, nargv);
+ fprint(2, "exec %s fails: %r\n", execbuf);
+ _exits("exec");
+ default:
+ w = wait();
+ if(w == nil)
+ exits("wait failed");
+ if(w->msg[0] == '\0')
+ exits(nil);
+ else
+ exits(w->msg);
+ }
+ assert(0);
+}
diff --git a/sys/src/ape/9src/tty.h b/sys/src/ape/9src/tty.h
new file mode 100755
index 000000000..bbe3f87ee
--- /dev/null
+++ b/sys/src/ape/9src/tty.h
@@ -0,0 +1,127 @@
+/* input modes */
+#define BRKINT 0x001
+#define ICRNL 0x002
+#define IGNBRK 0x004
+#define IGNCR 0x008
+#define IGNPAR 0x010
+#define INLCR 0x020
+#define INPCK 0x040
+#define ISTRIP 0x080
+#define IXOFF 0x100
+#define IXON 0x200
+#define PARMRK 0x400
+
+/* output modes */
+#define OPOST 0000001
+#define OLCUC 0000002
+#define ONLCR 0000004
+#define OCRNL 0000010
+#define ONOCR 0000020
+#define ONLRET 0000040
+#define OFILL 0000100
+#define OFDEL 0000200
+#define NLDLY 0000400
+#define NL0 0
+#define NL1 0000400
+#define CRDLY 0003000
+#define CR0 0
+#define CR1 0001000
+#define CR2 0002000
+#define CR3 0003000
+#define TABDLY 0014000
+#define TAB0 0
+#define TAB1 0004000
+#define TAB2 0010000
+#define TAB3 0014000
+#define BSDLY 0020000
+#define BS0 0
+#define BS1 0020000
+#define VTDLY 0040000
+#define VT0 0
+#define VT1 0040000
+#define FFDLY 0100000
+#define FF0 0
+#define FF1 0100000
+
+/* control modes */
+#define CLOCAL 0x001
+#define CREAD 0x002
+#define CSIZE 0x01C
+#define CS5 0x004
+#define CS6 0x008
+#define CS7 0x00C
+#define CS8 0x010
+#define CSTOPB 0x020
+#define HUPCL 0x040
+#define PARENB 0x080
+#define PARODD 0x100
+
+/* local modes */
+#define ECHO 0x001
+#define ECHOE 0x002
+#define ECHOK 0x004
+#define ECHONL 0x008
+#define ICANON 0x010
+#define IEXTEN 0x020
+#define ISIG 0x040
+#define NOFLSH 0x080
+#define TOSTOP 0x100
+
+/* control characters */
+#define VEOF 0
+#define VEOL 1
+#define VERASE 2
+#define VINTR 3
+#define VKILL 4
+#define VMIN 5
+#define VQUIT 6
+#define VSUSP 7
+#define VTIME 8
+#define VSTART 9
+#define VSTOP 10
+#define NCCS 11
+
+/* baud rates */
+#define B0 0
+#define B50 1
+#define B75 2
+#define B110 3
+#define B134 4
+#define B150 5
+#define B200 6
+#define B300 7
+#define B600 8
+#define B1200 9
+#define B1800 10
+#define B2400 11
+#define B4800 12
+#define B9600 13
+#define B19200 14
+#define B38400 15
+
+#define CESC '\\'
+#define CINTR 0177 /* DEL */
+#define CQUIT 034 /* FS, cntl | */
+#define CERASE 010 /* BS */
+#define CKILL 025 /* cntl u */
+#define CEOF 04 /* cntl d */
+#define CSTART 021 /* cntl q */
+#define CSTOP 023 /* cntl s */
+#define CSWTCH 032 /* cntl z */
+#define CEOL 000
+#define CNSWTCH 0
+
+/* optional actions for tcsetattr */
+#define TCSANOW 1
+#define TCSADRAIN 2
+#define TCSAFLUSH 3
+
+typedef struct Termios Termios;
+struct Termios
+{
+ int iflag; /* input modes */
+ int oflag; /* output modes */
+ int cflag; /* control modes */
+ int lflag; /* local modes */
+ uchar cc[NCCS]; /* control characters */
+};