Xinqi Bao's Git
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 ttydev
= _getpty(&ptm
, O_RDWR
, 0622, 0);
113 ptm
= open("/dev/ptc", O_RDWR
);
115 ptm
= open("/dev/ptmx", O_RDWR
);
118 ptm
= open("/dev/ptym/clone", O_RDWR
);
121 if(openpty(&ptm
, &pts
, NULL
, NULL
, NULL
) == -1)
122 eprintn("error, cannot open pty");
127 #if defined(_XOPEN_SOURCE) || !defined(__sgi) || !defined(_AIX)
128 if(grantpt(ptm
) == -1)
129 eprintn("error, cannot grant access to pty");
130 if(unlockpt(ptm
) == -1)
131 eprintn("error, cannot unlock pty");
132 ptsdev
= ptsname(ptm
);
134 ptsdev
= ttyname(ptm
);
137 eprintn("error, slave pty name undefined");
138 pts
= open(ptsdev
, O_RDWR
);
140 eprintn("error, cannot open slave pty");
142 #if defined(__hpux) || defined(sun) || defined(__sun)
143 ioctl(pts
, I_PUSH
, "ptem");
144 ioctl(pts
, I_PUSH
, "ldterm");
148 eprintn("error, cannot open pty");
152 movea(int x
, int y
) {
157 cmd("s %d,%d", x
, y
);
161 mover(int x
, int y
) {
162 movea(cx
+ x
, cy
+ y
);
170 memset(arg
, 0, LENGTH(arg
));
176 for(j
= 0; j
< LENGTH(arg
);) {
186 eprint("syntax error");
203 mover(0, j
? arg
[0] : 1);
206 mover(0, j
? -arg
[0] : -1);
209 mover(j
? arg
[0] : 1, 0);
212 mover(j
? -arg
[0] : -1, 0);
215 /* movel(j ? arg[0] : 1); */
218 /* movel(j ? -arg[0] : -1); */
222 movea(j
? arg
[0] : 1, cy
);
226 movea(arg
[1] ? arg
[1] : 1, arg
[0] ? arg
[0] : 1);
228 /* insline(j ? arg[0] : 1); */
231 /* delline(j ? arg[0] : 1); */
236 scroll(j
? arg
[0] : 1);
239 scroll(j
? -arg
[0] : -1);
242 movea(cx
, j
? arg
[0] : 1);
245 for(i
= 0; i
< j
; i
++) {
246 if(arg
[i
] >= 30 && arg
[i
] <= 37)
247 cmd("#%d", arg
[i
] - 30);
248 if(arg
[i
] >= 40 && arg
[i
] <= 47)
249 cmd("|%d", arg
[i
] - 40);
250 /* xterm bright colors */
251 if(arg
[i
] >= 90 && arg
[i
] <= 97)
252 cmd("#%d", arg
[i
] - 90);
253 if(arg
[i
] >= 100 && arg
[i
] <= 107)
254 cmd("|%d", arg
[i
] - 100);
277 cmd("s %d, %d", cx
, cy
+ l
);
282 static char *shell
= NULL
;
284 if(!shell
&& !(shell
= getenv("SHELL")))
289 eprint("error, cannot fork\n");
292 dup2(pts
, STDIN_FILENO
);
293 dup2(pts
, STDOUT_FILENO
);
294 dup2(pts
, STDERR_FILENO
);
296 putenv("TERM=vt102");
301 signal(SIGCHLD
, sigchld
);
309 if(waitpid(pid
, &ret
, 0) == -1)
310 eprintn("error, waiting for child failed");
312 exit(WEXITSTATUS(ret
));
321 c
= buf
.data
[buf
.s
++];
322 buf
.s
%= LENGTH(buf
.data
);
328 main(int argc
, char *argv
[]) {
330 if(argc
== 2 && !strcmp("-v", argv
[1]))
331 eprint("std-"VERSION
", © 2008 Matthias-Christian Ott\n");
333 eprint("usage: st [-v]\n");
338 eprintn("cannot open slave pty");