diff options
author | cinap_lenrek <cinap_lenrek@felloff.net> | 2016-07-31 20:04:02 +0200 |
---|---|---|
committer | cinap_lenrek <cinap_lenrek@felloff.net> | 2016-07-31 20:04:02 +0200 |
commit | cf37a1010f7c9aabce1d3598d5a62489eb01d48c (patch) | |
tree | ab850b791e22ff243c0fa37dec7d757f0cba8cfb /sys/src/libauthsrv/readcons.c | |
parent | d91c4e407d4fa98101ade0bcdc524c1f6864b2e5 (diff) |
libauthsrv: export common readcons() routine and introduce PASSWDLEN constant
drawterm, factotum, secstore and the auth commands
all had ther own implementation of readcons. we
want to have one common function for this to avoid
the duplication, so putting that in libauthsrv.
introduce PASSWDLEN which makes the use more explicit
than ANAMELEN.
Diffstat (limited to 'sys/src/libauthsrv/readcons.c')
-rw-r--r-- | sys/src/libauthsrv/readcons.c | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/sys/src/libauthsrv/readcons.c b/sys/src/libauthsrv/readcons.c new file mode 100644 index 000000000..d7f9b99ee --- /dev/null +++ b/sys/src/libauthsrv/readcons.c @@ -0,0 +1,82 @@ +#include <u.h> +#include <libc.h> + +/* + * prompt for a string with a possible default response + */ +char* +readcons(char *prompt, char *def, int raw) +{ + int fdin, fdout, ctl, n; + char *s, *p; + + s = p = nil; + fdout = ctl = -1; + + if((fdin = open("/dev/cons", OREAD)) < 0) + goto Out; + if((fdout = open("/dev/cons", OWRITE)) < 0) + goto Out; + + if(raw){ + if((ctl = open("/dev/consctl", OWRITE)) < 0) + goto Out; + write(ctl, "rawon", 5); + } + + if(def != nil) + fprint(fdout, "%s[%s]: ", prompt, def); + else + fprint(fdout, "%s: ", prompt); + + for(;;){ + n = p - s; + if((n % 32) == 0){ + if((p = realloc(s, n+32)) == nil) + break; + s = p, p += n; + } + + if(read(fdin, p, 1) <= 0 || *p == 0x7f) + break; + + if(*p == '\n' || *p == '\r'){ + if(p == s && def != nil){ + free(s); + s = strdup(def); + } else + *p = 0; + if(raw) + write(fdout, "\n", 1); + goto Out; + } else if(*p == '\b') { + while(p > s && (p[-1] & 0xc0) == 0x80) + *p-- = 0; + if(p > s) + *p-- = 0; + } else if(*p == 0x15) { /* ^U: line kill */ + if(def != nil) + fprint(fdout, "\n%s[%s]: ", prompt, def); + else + fprint(fdout, "\n%s: ", prompt); + while(p > s) + *p-- = 0; + } else if(*p >= ' ') + p++; + } + free(s); + s = nil; + if(raw) + write(fdout, "\n", 1); +Out: + if(ctl >= 0){ + write(ctl, "rawoff", 6); + close(ctl); + } + if(fdin >= 0) + close(fdin); + if(fdout >= 0) + close(fdout); + + return s; +} |