]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/cryptsetup/readcons.c
kernel: keep segment locked for data2txt
[plan9front.git] / sys / src / cmd / cryptsetup / readcons.c
1 /* From /sys/src/libauthsrv/readnvram.c, LPL licensed */
2 #include <u.h>
3 #include <libc.h>
4
5
6 char*
7 readcons(char *prompt, char *def, int raw, char *buf, int nbuf)
8 {
9         int fdin, fdout, ctl, n, m;
10         char line[10];
11
12         fdin = open("/dev/cons", OREAD);
13         if(fdin < 0)
14                 fdin = 0;
15         fdout = open("/dev/cons", OWRITE);
16         if(fdout < 0)
17                 fdout = 1;
18         if(def != nil)
19                 fprint(fdout, "%s[%s]: ", prompt, def);
20         else
21                 fprint(fdout, "%s: ", prompt);
22         if(raw){
23                 ctl = open("/dev/consctl", OWRITE);
24                 if(ctl >= 0)
25                         write(ctl, "rawon", 5);
26         } else
27                 ctl = -1;
28
29         m = 0;
30         for(;;){
31                 n = read(fdin, line, 1);
32                 if(n == 0){
33                         close(ctl);
34                         werrstr("readcons: EOF");
35                         return nil;
36                 }
37                 if(n < 0){
38                         close(ctl);
39                         werrstr("can't read cons");
40                         return nil;
41                 }
42                 if(line[0] == 0x7f)
43                         exits(0);
44                 if(n == 0 || line[0] == '\n' || line[0] == '\r'){
45                         if(raw){
46                                 write(ctl, "rawoff", 6);
47                                 write(fdout, "\n", 1);
48                                 close(ctl);
49                         }
50                         buf[m] = '\0';
51                         if(buf[0]=='\0' && def)
52                                 strcpy(buf, def);
53                         return buf;
54                 }
55                 if(line[0] == '\b'){
56                         if(m > 0)
57                                 m--;
58                 }else if(line[0] == 0x15){      /* ^U: line kill */
59                         m = 0;
60                         if(def != nil)
61                                 fprint(fdout, "%s[%s]: ", prompt, def);
62                         else
63                                 fprint(fdout, "%s: ", prompt);
64                 }else{
65                         if(m >= nbuf-1){
66                                 fprint(fdout, "line too long\n");
67                                 m = 0;
68                                 if(def != nil)
69                                         fprint(fdout, "%s[%s]: ", prompt, def);
70                                 else
71                                         fprint(fdout, "%s: ", prompt);
72                         }else
73                                 buf[m++] = line[0];
74                 }
75         }
76 }
77