Xinqi Bao's Git
ef946dda3607aff79817ef797c1e744c8fd2b68e
2 #include <sys/select.h>
16 #define LENGTH(x) (sizeof (x) / sizeof (x)[0])
17 #define MAX(a,b) (((a) > (b)) ? (a) : (b))
18 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
21 void cmd(const char *cmdstr
, ...);
22 void *emallocz(unsigned int size
);
23 void eprint(const char *errstr
, ...);
24 void eprintn(const char *errstr
, ...);
26 void movea(int x
, int y
);
27 void mover(int x
, int y
);
34 enum { QuestionMark
= 1, Digit
= 2 };
37 unsigned char data
[BUFSIZ
];
42 int cols
= 80, lines
= 25;
53 if(buf
.n
< LENGTH(buf
.data
))
56 buf
.s
= (buf
.s
+ 1) % LENGTH(buf
.data
);
57 buf
.data
[buf
.e
++] = c
;
58 buf
.e
%= LENGTH(buf
.data
);
62 cmd(const char *cmdstr
, ...) {
68 vfprintf(stdout
, cmdstr
, ap
);
73 emallocz(unsigned int size
) {
74 void *res
= calloc(1, size
);
77 eprint("fatal: could not malloc() %u bytes\n", size
);
82 eprint(const char *errstr
, ...) {
86 vfprintf(stderr
, errstr
, ap
);
92 eprintn(const char *errstr
, ...) {
96 vfprintf(stderr
, errstr
, ap
);
98 fprintf(stderr
, ": %s\n", strerror(errno
));
106 #if defined(_GNU_SOURCE)
108 #elif _POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600
109 ptm
= posix_openpt(O_RDWR
);
111 ptm
= open("/dev/ptmx", O_RDWR
);
113 if(openpty(&ptm
, &pts
, NULL
, NULL
, NULL
) == -1)
114 eprintn("error, cannot open pty");
118 #if defined(_XOPEN_SOURCE)
120 if(grantpt(ptm
) == -1)
121 eprintn("error, cannot grant access to pty");
122 if(unlockpt(ptm
) == -1)
123 eprintn("error, cannot unlock pty");
124 ptsdev
= ptsname(ptm
);
126 eprintn("error, slave pty name undefined");
127 pts
= open(ptsdev
, O_RDWR
);
129 eprintn("error, cannot open slave pty");
133 eprintn("error, cannot open pty");
138 movea(int x
, int y
) {
143 cmd("s %d,%d", x
, y
);
147 mover(int x
, int y
) {
148 movea(cx
+ x
, cy
+ y
);
156 memset(arg
, 0, LENGTH(arg
));
162 for(j
= 0; j
< LENGTH(arg
);) {
172 eprint("syntax error");
189 mover(0, j
? arg
[0] : 1);
192 mover(0, j
? -arg
[0] : -1);
195 mover(j
? arg
[0] : 1, 0);
198 mover(j
? -arg
[0] : -1, 0);
201 /* movel(j ? arg[0] : 1); */
204 /* movel(j ? -arg[0] : -1); */
208 movea(j
? arg
[0] : 1, cy
);
212 movea(arg
[1] ? arg
[1] : 1, arg
[0] ? arg
[0] : 1);
214 /* insline(j ? arg[0] : 1); */
217 /* delline(j ? arg[0] : 1); */
222 scroll(j
? arg
[0] : 1);
225 scroll(j
? -arg
[0] : -1);
228 movea(cx
, j
? arg
[0] : 1);
231 for(i
= 0; i
< j
; i
++) {
232 if(arg
[i
] >= 30 && arg
[i
] <= 37)
233 cmd("#%d", arg
[i
] - 30);
234 if(arg
[i
] >= 40 && arg
[i
] <= 47)
235 cmd("|%d", arg
[i
] - 40);
236 /* xterm bright colors */
237 if(arg
[i
] >= 90 && arg
[i
] <= 97)
238 cmd("#%d", arg
[i
] - 90);
239 if(arg
[i
] >= 100 && arg
[i
] <= 107)
240 cmd("|%d", arg
[i
] - 100);
263 cmd("s %d, %d", cx
, cy
+ l
);
268 static char *shell
= NULL
;
270 if(!shell
&& !(shell
= getenv("SHELL")))
275 eprint("error, cannot fork\n");
278 dup2(pts
, STDIN_FILENO
);
279 dup2(pts
, STDOUT_FILENO
);
280 dup2(pts
, STDERR_FILENO
);
282 putenv("TERM=vt102");
287 signal(SIGCHLD
, sigchld
);
295 if(waitpid(pid
, &ret
, 0) == -1)
296 eprintn("error, waiting for child failed");
298 exit(WEXITSTATUS(ret
));
307 c
= buf
.data
[buf
.s
++];
308 buf
.s
%= LENGTH(buf
.data
);
314 main(int argc
, char *argv
[]) {
316 if(argc
== 2 && !strcmp("-v", argv
[1]))
317 eprint("std-"VERSION
", © 2008 Matthias-Christian Ott\n");
319 eprint("usage: st [-v]\n");
324 eprintn("cannot open slave pty");