diff options
author | taruti <taruti@violetti.org> | 2011-05-24 22:19:33 +0000 |
---|---|---|
committer | taruti <taruti@violetti.org> | 2011-05-24 22:19:33 +0000 |
commit | 9655db255097b84cf742b7a33c74432d4eb3425a (patch) | |
tree | 81dd1509453d330ac9e94ca6f5b1a7fe7bdbba2d /sys/src/cmd/cryptsetup/readcons.c | |
parent | f34231e16a9780a1aa1b9fe5dad1776dd82caa44 (diff) |
devfs crypto code - alpha version
Diffstat (limited to 'sys/src/cmd/cryptsetup/readcons.c')
-rw-r--r-- | sys/src/cmd/cryptsetup/readcons.c | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/sys/src/cmd/cryptsetup/readcons.c b/sys/src/cmd/cryptsetup/readcons.c new file mode 100644 index 000000000..cc2926cf0 --- /dev/null +++ b/sys/src/cmd/cryptsetup/readcons.c @@ -0,0 +1,77 @@ +/* From /sys/src/libauthsrv/readnvram.c, LPL licensed */ +#include <u.h> +#include <libc.h> + + +char* +readcons(char *prompt, char *def, int raw, char *buf, int nbuf) +{ + int fdin, fdout, ctl, n, m; + char line[10]; + + fdin = open("/dev/cons", OREAD); + if(fdin < 0) + fdin = 0; + fdout = open("/dev/cons", OWRITE); + if(fdout < 0) + fdout = 1; + if(def != nil) + fprint(fdout, "%s[%s]: ", prompt, def); + else + fprint(fdout, "%s: ", prompt); + if(raw){ + ctl = open("/dev/consctl", OWRITE); + if(ctl >= 0) + write(ctl, "rawon", 5); + } else + ctl = -1; + + m = 0; + for(;;){ + n = read(fdin, line, 1); + if(n == 0){ + close(ctl); + werrstr("readcons: EOF"); + return nil; + } + if(n < 0){ + close(ctl); + werrstr("can't read cons"); + return nil; + } + if(line[0] == 0x7f) + exits(0); + if(n == 0 || line[0] == '\n' || line[0] == '\r'){ + if(raw){ + write(ctl, "rawoff", 6); + write(fdout, "\n", 1); + close(ctl); + } + buf[m] = '\0'; + if(buf[0]=='\0' && def) + strcpy(buf, def); + return buf; + } + if(line[0] == '\b'){ + if(m > 0) + m--; + }else if(line[0] == 0x15){ /* ^U: line kill */ + m = 0; + if(def != nil) + fprint(fdout, "%s[%s]: ", prompt, def); + else + fprint(fdout, "%s: ", prompt); + }else{ + if(m >= nbuf-1){ + fprint(fdout, "line too long\n"); + m = 0; + if(def != nil) + fprint(fdout, "%s[%s]: ", prompt, def); + else + fprint(fdout, "%s: ", prompt); + }else + buf[m++] = line[0]; + } + } +} + |