OSDN Git Service

am e06f9faf: (-s ours) am 97809c21: Regenerate generated files.
[android-x86/external-toybox.git] / lib / interestingtimes.c
1 /* interestingtimes.c - cursor control
2  *
3  * Copyright 2015 Rob Landley <rob@landley.net>
4  */
5
6 #include "toys.h"
7
8 int xgettty(void)
9 {
10   int i, j;
11
12   for (i = 0; i<3; i++) if (isatty(j = (i+1)%3)) return j;
13
14   return xopen("/dev/tty", O_RDWR);
15 }
16
17 // Quick and dirty query size of terminal, doesn't do ANSI probe fallback.
18 // set x=80 y=25 before calling to provide defaults. Returns 0 if couldn't
19 // determine size.
20
21 int terminal_size(unsigned *xx, unsigned *yy)
22 {
23   struct winsize ws;
24   unsigned i, x = 0, y = 0;
25   char *s;
26
27   // stdin, stdout, stderr
28   for (i=0; i<3; i++) {
29     memset(&ws, 0, sizeof(ws));
30     if (!ioctl(i, TIOCGWINSZ, &ws)) {
31       if (ws.ws_col) x = ws.ws_col;
32       if (ws.ws_row) y = ws.ws_row;
33
34       break;
35     }
36   }
37   s = getenv("COLUMNS");
38   if (s) sscanf(s, "%u", &x);
39   s = getenv("LINES");
40   if (s) sscanf(s, "%u", &y);
41
42   // Never return 0 for either value, leave it at default instead.
43   if (xx && x) *xx = x;
44   if (yy && y) *yy = y;
45
46   return x || y;
47 }
48
49 // Reset terminal to known state, saving copy of old state if old != NULL.
50 int set_terminal(int fd, int raw, struct termios *old)
51 {
52   struct termios termio;
53
54   // Fetch local copy of old terminfo, and copy struct contents to *old if set
55   if (!tcgetattr(fd, &termio) && old) *old = termio;
56
57   // the following are the bits set for an xterm. Linux text mode TTYs by
58   // default add two additional bits that only matter for serial processing
59   // (turn serial line break into an interrupt, and XON/XOFF flow control)
60
61   // Any key unblocks output, swap CR and NL on input
62   termio.c_iflag = IXANY|ICRNL|INLCR;
63   if (toys.which->flags & TOYFLAG_LOCALE) termio.c_iflag |= IUTF8;
64
65   // Output appends CR to NL, does magic undocumented postprocessing
66   termio.c_oflag = ONLCR|OPOST;
67
68   // Leave serial port speed alone
69   // termio.c_cflag = C_READ|CS8|EXTB;
70
71   // Generate signals, input entire line at once, echo output
72   // erase, line kill, escape control characters with ^
73   // erase line char at a time
74   // "extended" behavior: ctrl-V quotes next char, ctrl-R reprints unread chars,
75   // ctrl-W erases word
76   termio.c_lflag = ISIG|ICANON|ECHO|ECHOE|ECHOK|ECHOCTL|ECHOKE|IEXTEN;
77
78   if (raw) cfmakeraw(&termio);
79
80   return tcsetattr(fd, TCSANOW, &termio);
81 }
82
83 // Scan stdin for a keypress, parsing known escape sequences
84 // Returns: 0-255=literal, -1=EOF, -2=NONE, 256-...=index into seq
85 // scratch space is necessary because last char of !seq could start new seq
86 // Zero out first byte of scratch before first call to scan_key
87 // block=0 allows fetching multiple characters before updating display
88 int scan_key(char *scratch, int block)
89 {
90   // up down right left pgup pgdn home end ins
91   char *seqs[] = {"\033[A", "\033[B", "\033[C", "\033[D", "\033[5~", "\033[6~",
92                   "\033OH", "\033OF", "\033[2~", 0};
93   struct pollfd pfd;
94   int maybe, i, j;
95   char *test;
96
97   for (;;) {
98     pfd.fd = 0;
99     pfd.events = POLLIN;
100     pfd.revents = 0;
101
102     // check sequences
103     maybe = 0;
104     if (*scratch) {
105       for (i = maybe = 0; (test = seqs[i]); i++) {
106         for (j = 0; j<*scratch; j++) if (scratch[j+1] != test[j]) break;
107         if (j == *scratch) {
108           maybe = 1;
109           if (!test[j]) {
110             // We recognized current sequence: consume and return
111             *scratch = 0;
112             return 256+i;
113           }
114         }
115       }
116       // If current data can't be a known sequence, return next raw char
117       if (!maybe) break;
118     }
119
120     // Need more data to decide
121
122     // 30 miliseconds is about the gap between characters at 300 baud 
123     if (maybe || !block) if (!xpoll(&pfd, 1, 30*maybe)) break;
124
125     if (1 != read(0, scratch+1+*scratch, 1)) return -1;
126     ++*scratch;
127   }
128
129   // Was not a sequence
130   if (!*scratch) return -2;
131   i = scratch[1];
132   if (--*scratch) memmove(scratch+1, scratch+2, *scratch);
133
134   return i;
135 }
136
137 void tty_esc(char *s)
138 {
139   printf("\033[%s", s);
140 }
141
142 void tty_jump(int x, int y)
143 {
144   char s[32];
145
146   sprintf(s, "%d;%dH", y+1, x+1);
147   tty_esc(s);
148 }
149
150 void tty_reset(void)
151 {
152   set_terminal(1, 0, 0);
153   tty_esc("?25h");
154   tty_esc("0m");
155   tty_jump(0, 999);
156   tty_esc("K");
157 }
158
159 void tty_sigreset(int i)
160 {
161   tty_reset();
162   _exit(128+i);
163 }