Xinqi Bao's Git
ef0fd17d00eaa7ea057fb7bb88093b4ed937f9a6
1 /* See LICENSE for licence details. */
13 die(const char *errstr
, ...) {
17 vfprintf(stderr
, errstr
, ap
);
24 char *args
[3] = {SHELL
, "-i", NULL
};
25 putenv("TERM=" TNAME
);
30 xbell(void) { /* visual bell */
31 XRectangle r
= { 0, 0, xw
.w
, xw
.h
};
32 XSetForeground(xw
.dis
, dc
.gc
, dc
.col
[BellCol
]);
33 XFillRectangles(xw
.dis
, xw
.win
, dc
.gc
, &r
, 1);
45 if((m
= posix_openpt(O_RDWR
| O_NOCTTY
)) < 0)
51 if((pts
= ptsname(m
)) == NULL
)
53 if((s
= open(pts
, O_RDWR
| O_NOCTTY
)) < 0)
55 fcntl(s
, F_SETFL
, O_NDELAY
);
56 switch(pid
= fork()) {
61 setsid(); /* create a new process group */
62 dup2(s
, STDIN_FILENO
);
63 dup2(s
, STDOUT_FILENO
);
64 dup2(s
, STDERR_FILENO
);
65 if(ioctl(s
, TIOCSCTTY
, NULL
) < 0)
66 die("slave TTIOCSTTY");
78 fprintf(stderr
, " %02x %c ", c
, isprint(c
)?c
:'.');
80 fprintf(stderr
, "\n");
85 char buf
[BUFSIZ
] = {0};
88 switch(ret
= read(cmdfd
, buf
, BUFSIZ
)) {
89 case -1: /* error or exit */
90 /* XXX: be more precise */
99 ttywrite(char *s
, size_t n
) {
100 if(write(cmdfd
, s
, n
) == -1)
101 die("write error on tty.");
105 ttyresize(int x
, int y
) {
110 w
.ws_xpixel
= w
.ws_ypixel
= 0;
111 if(ioctl(cmdfd
, TIOCSWINSZ
, &w
) < 0)
112 fprintf(stderr
, "Couldn't set window size: %m\n");
128 else if(BETWEEN(c
, 0x40, 0x7E))
139 x
= term
.c
.x
, y
= term
.c
.y
;
140 else if(mode
== CSload
)
145 tnew(int col
, int row
) { /* screen size */
146 term
.row
= row
, term
.col
= col
;
147 term
.top
= 0, term
.bot
= term
.row
- 1;
151 term
.c
.attr
.mode
= ATnone
;
152 term
.c
.attr
.fg
= DefaultFG
;
153 term
.c
.attr
.bg
= DefaultBG
;
154 term
.c
.x
= term
.c
.y
= 0;
156 /* allocate screen */
157 term
.line
= calloc(term
.row
, sizeof(Line
));
158 for(row
= 0 ; row
< term
.row
; row
++)
159 term
.line
[row
] = calloc(term
.col
, sizeof(Glyph
));
164 Line temp
= term
.line
[term
.top
];
167 for(i
= term
.top
; i
< term
.bot
; i
++)
168 term
.line
[i
] = term
.line
[i
+1];
169 memset(temp
, 0, sizeof(Glyph
) * term
.col
);
170 term
.line
[term
.bot
] = temp
;
176 int y
= term
.c
.y
+ 1;
179 tscroll(), y
= term
.bot
;
186 escseq
.buf
[escseq
.len
++] = c
;
187 if(escfinal(c
) || escseq
.len
>= ESCSIZ
) {
188 escparse(), eschandle();
197 char *p
= escseq
.buf
;
200 switch(escseq
.pre
= *p
++) {
203 escseq
.priv
= 1, p
++;
205 while(p
< escseq
.buf
+escseq
.len
) {
207 escseq
.arg
[escseq
.narg
] *= 10;
208 escseq
.arg
[escseq
.narg
] += *(p
++) - '0'/*, noarg = 0 */;
220 /* XXX: graphic character set */
226 tmoveto(int x
, int y
) {
227 term
.c
.x
= x
< 0 ? 0 : x
>= term
.col
? term
.col
-1 : x
;
228 term
.c
.y
= y
< 0 ? 0 : y
>= term
.row
? term
.row
-1 : y
;
233 int xf
= term
.c
.x
, yf
= term
.c
.y
;
245 xf
= term
.col
-1, yf
--;
247 yf
= term
.top
, xf
= 0;
255 yf
= term
.bot
, tscroll();
264 term
.line
[term
.c
.y
][term
.c
.x
] = term
.c
.attr
;
265 term
.line
[term
.c
.y
][term
.c
.x
].c
= c
;
266 term
.line
[term
.c
.y
][term
.c
.x
].state
|= CRset
| CRupdate
;
270 tclearregion(int x1
, int y1
, int x2
, int y2
) {
273 LIMIT(x1
, 0, term
.col
-1);
274 LIMIT(x2
, 0, term
.col
-1);
275 LIMIT(y1
, 0, term
.row
-1);
276 LIMIT(y2
, 0, term
.row
-1);
278 /* XXX: could be optimized */
279 for(x
= x1
; x
<= x2
; x
++)
280 for(y
= y1
; y
<= y2
; y
++)
281 memset(&term
.line
[y
][x
], 0, sizeof(Glyph
));
283 xclear(x1
, y1
, x2
, y2
);
288 int src
= term
.c
.x
+ n
;
290 int size
= term
.col
- src
;
292 if(src
>= term
.col
) {
293 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
296 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
297 tclearregion(term
.col
-size
, term
.c
.y
, term
.col
-1, term
.c
.y
);
301 tinsertblank(int n
) {
304 int size
= term
.col
- n
- src
;
306 if(dst
>= term
.col
) {
307 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
310 memmove(&term
.line
[term
.c
.y
][dst
], &term
.line
[term
.c
.y
][src
], size
* sizeof(Glyph
));
311 tclearregion(src
, term
.c
.y
, dst
, term
.c
.y
);
315 tinsertblankline (int n
) {
320 if(term
.c
.y
> term
.bot
)
322 else if(term
.c
.y
< term
.top
)
324 if(term
.c
.y
+ n
>= bot
) {
325 tclearregion(0, term
.c
.y
, term
.col
-1, bot
);
328 for(i
= bot
; i
>= term
.c
.y
+n
; i
--) {
329 /* swap deleted line <-> blanked line */
330 blank
= term
.line
[i
];
331 term
.line
[i
] = term
.line
[i
-n
];
332 term
.line
[i
-n
] = blank
;
334 memset(blank
, 0, term
.col
* sizeof(Glyph
));
345 if(term
.c
.y
> term
.bot
)
347 else if(term
.c
.y
< term
.top
)
349 if(term
.c
.y
+ n
>= bot
) {
350 tclearregion(0, term
.c
.y
, term
.col
-1, bot
);
353 for(i
= term
.c
.y
; i
<= bot
-n
; i
++) {
354 /* swap deleted line <-> blanked line */
355 blank
= term
.line
[i
];
356 term
.line
[i
] = term
.line
[i
+n
];
357 term
.line
[i
+n
] = blank
;
359 memset(blank
, 0, term
.col
* sizeof(Glyph
));
364 tsetattr(int *attr
, int l
) {
367 for(i
= 0; i
< l
; i
++) {
370 memset(&term
.c
.attr
, 0, sizeof(term
.c
.attr
));
371 term
.c
.attr
.fg
= DefaultFG
;
372 term
.c
.attr
.bg
= DefaultBG
;
375 term
.c
.attr
.mode
|= ATbold
;
378 term
.c
.attr
.mode
|= ATunderline
;
381 term
.c
.attr
.mode
|= ATreverse
;
384 term
.c
.hidden
= CShide
;
387 term
.c
.attr
.mode
&= ~ATbold
;
390 term
.c
.attr
.mode
&= ~ATunderline
;
393 term
.c
.attr
.mode
&= ~ATreverse
;
396 term
.c
.attr
.fg
= DefaultFG
;
399 term
.c
.attr
.fg
= DefaultBG
;
402 if(BETWEEN(attr
[i
], 30, 37))
403 term
.c
.attr
.fg
= attr
[i
] - 30;
404 else if(BETWEEN(attr
[i
], 40, 47))
405 term
.c
.attr
.bg
= attr
[i
] - 40;
412 tsetscroll(int t
, int b
) {
415 LIMIT(t
, 0, term
.row
-1);
416 LIMIT(b
, 0, term
.row
-1);
432 switch(escseq
.mode
) {
433 case '@': /* Insert <n> blank char */
434 DEFAULT(escseq
.arg
[0], 1);
435 tinsertblank(escseq
.arg
[0]);
437 case 'A': /* Cursor <n> Up */
439 DEFAULT(escseq
.arg
[0], 1);
440 tmoveto(term
.c
.x
, term
.c
.y
-escseq
.arg
[0]);
442 case 'B': /* Cursor <n> Down */
443 DEFAULT(escseq
.arg
[0], 1);
444 tmoveto(term
.c
.x
, term
.c
.y
+escseq
.arg
[0]);
446 case 'C': /* Cursor <n> Forward */
448 DEFAULT(escseq
.arg
[0], 1);
449 tmoveto(term
.c
.x
+escseq
.arg
[0], term
.c
.y
);
451 case 'D': /* Cursor <n> Backward */
452 DEFAULT(escseq
.arg
[0], 1);
453 tmoveto(term
.c
.x
-escseq
.arg
[0], term
.c
.y
);
455 case 'E': /* Cursor <n> Down and first col */
456 DEFAULT(escseq
.arg
[0], 1);
457 tmoveto(0, term
.c
.y
+escseq
.arg
[0]);
459 case 'F': /* Cursor <n> Up and first col */
460 DEFAULT(escseq
.arg
[0], 1);
461 tmoveto(0, term
.c
.y
-escseq
.arg
[0]);
463 case 'G': /* Move to <col> */
465 DEFAULT(escseq
.arg
[0], 1);
466 tmoveto(escseq
.arg
[0]-1, term
.c
.y
);
468 case 'H': /* Move to <row> <col> */
470 DEFAULT(escseq
.arg
[0], 1);
471 DEFAULT(escseq
.arg
[1], 1);
472 tmoveto(escseq
.arg
[1]-1, escseq
.arg
[0]-1);
474 case 'J': /* Clear screen */
475 switch(escseq
.arg
[0]) {
477 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.row
-1);
480 tclearregion(0, 0, term
.c
.x
, term
.c
.y
);
483 tclearregion(0, 0, term
.col
-1, term
.row
-1);
487 case 'K': /* Clear line */
488 switch(escseq
.arg
[0]) {
490 tclearregion(term
.c
.x
, term
.c
.y
, term
.col
-1, term
.c
.y
);
493 tclearregion(0, term
.c
.y
, term
.c
.x
, term
.c
.y
);
496 tclearregion(0, term
.c
.y
, term
.col
-1, term
.c
.y
);
500 case 'L': /* Insert <n> blank lines */
501 DEFAULT(escseq
.arg
[0], 1);
502 tinsertblankline(escseq
.arg
[0]);
504 case 'M': /* Delete <n> lines */
505 DEFAULT(escseq
.arg
[0], 1);
506 tdeleteline(escseq
.arg
[0]);
508 case 'P': /* Delete <n> char */
509 DEFAULT(escseq
.arg
[0], 1);
510 tdeletechar(escseq
.arg
[0]);
512 case 'd': /* Move to <row> */
513 DEFAULT(escseq
.arg
[0], 1);
514 tmoveto(term
.c
.x
, escseq
.arg
[0]-1);
516 case 'h': /* Set terminal mode */
518 case 'm': /* Terminal attribute (color) */
519 tsetattr(escseq
.arg
, escseq
.narg
);
525 DEFAULT(escseq
.arg
[0], 1);
526 DEFAULT(escseq
.arg
[1], term
.row
);
527 tsetscroll(escseq
.arg
[0]-1, escseq
.arg
[1]-1);
530 case 's': /* Save cursor position */
533 case 'u': /* Load cursor position */
545 printf("rawbuf : %s\n", escseq
.buf
);
546 printf("prechar : %c\n", escseq
.pre
);
547 printf("private : %c\n", escseq
.priv
? '?' : ' ');
548 printf("narg : %d\n", escseq
.narg
);
550 for(i
= 0; i
< escseq
.narg
; i
++)
551 printf("\targ %d = %d\n", i
, escseq
.arg
[i
]);
553 printf("mode : %c\n", escseq
.mode
);
558 memset(&escseq
, 0, sizeof(escseq
));
563 static int inesc
= 0;
566 /* start of escseq */
568 escreset(), inesc
= 1;
581 tmoveto(0, term
.c
.y
);
593 tputs(char *s
, int len
) {
594 for(; len
> 0; len
--)
603 for(row
= 0; row
< term
.row
; row
++) {
604 for(col
= 0; col
< term
.col
; col
++) {
605 if(col
== term
.c
.x
&& row
== term
.c
.y
)
608 c
= term
.line
[row
][col
];
609 putchar(c
.state
& CRset
? c
.c
: '.');
617 tresize(int col
, int row
) {
620 int minrow
= MIN(row
, term
.row
);
621 int mincol
= MIN(col
, term
.col
);
623 if(col
< 1 || row
< 1)
625 line
= calloc(row
, sizeof(Line
));
626 for(i
= 0 ; i
< row
; i
++)
627 line
[i
] = calloc(col
, sizeof(Glyph
));
628 for(i
= 0 ; i
< minrow
; i
++) {
629 memcpy(line
[i
], term
.line
[i
], mincol
* sizeof(Glyph
));
633 LIMIT(term
.c
.x
, 0, col
-1);
634 LIMIT(term
.c
.y
, 0, row
-1);
635 LIMIT(term
.top
, 0, row
-1);
636 LIMIT(term
.bot
, 0, row
-1);
637 // if(term.bot == term.row-1)
640 term
.col
= col
, term
.row
= row
;
644 xgetcol(const char *s
) {
646 Colormap cmap
= DefaultColormap(xw
.dis
, xw
.scr
);
648 if(!XAllocNamedColor(xw
.dis
, cmap
, s
, &color
, &color
)) {
649 color
.pixel
= WhitePixel(xw
.dis
, xw
.scr
);
650 fprintf(stderr
, "Could not allocate color '%s'\n", s
);
657 xclear(int x1
, int y1
, int x2
, int y2
) {
658 XClearArea(xw
.dis
, xw
.win
,
659 x1
* xw
.cw
, y1
* xw
.ch
,
660 (x2
-x1
+1) * xw
.cw
, (y2
-y1
+1) * xw
.ch
,
667 int srcy
= (term
.top
+1) * xw
.ch
;
668 int dsty
= term
.top
* xw
.ch
;
669 int height
= (term
.bot
-term
.top
) * xw
.ch
;
672 XCopyArea(xw
.dis
, xw
.win
, xw
.win
, dc
.gc
, 0, srcy
, xw
.w
, height
, 0, dsty
);
673 xclear(0, term
.bot
, term
.col
-1, term
.bot
);
682 unsigned long valuemask
;
686 char *args
[] = {NULL
};
689 xw
.dis
= XOpenDisplay(NULL
);
690 xw
.scr
= XDefaultScreen(xw
.dis
);
692 die("can not open display");
695 if(!(dc
.font
= XLoadQueryFont(xw
.dis
, FONT
)))
696 die("can not find font " FONT
);
698 xw
.cw
= dc
.font
->max_bounds
.rbearing
- dc
.font
->min_bounds
.lbearing
;
699 xw
.ch
= dc
.font
->ascent
+ dc
.font
->descent
+ LINESPACE
;
702 for(i
= 0; i
< LEN(colorname
); i
++)
703 dc
.col
[i
] = xgetcol(colorname
[i
]);
705 term
.c
.attr
.fg
= DefaultFG
;
706 term
.c
.attr
.bg
= DefaultBG
;
707 term
.c
.attr
.mode
= ATnone
;
709 xw
.h
= term
.row
* xw
.ch
;
710 xw
.w
= term
.col
* xw
.cw
;
711 /* XXX: this BORDER is useless after the first resize, handle it in xdraws() */
712 xw
.win
= XCreateSimpleWindow(xw
.dis
, XRootWindow(xw
.dis
, xw
.scr
), 0, 0,
717 values
.foreground
= XWhitePixel(xw
.dis
, xw
.scr
);
718 values
.font
= dc
.font
->fid
;
719 valuemask
= GCForeground
| GCFont
;
720 dc
.gc
= XCreateGC(xw
.dis
, xw
.win
, valuemask
, &values
);
721 XMapWindow(xw
.dis
, xw
.win
);
723 chint
.res_name
= TNAME
, chint
.res_class
= TNAME
;
724 wmhint
.input
= 1, wmhint
.flags
= InputHint
;
725 shint
.height_inc
= xw
.ch
, shint
.width_inc
= xw
.cw
;
726 shint
.height
= xw
.h
, shint
.width
= xw
.w
;
727 shint
.flags
= PSize
| PResizeInc
;
728 XSetWMProperties(xw
.dis
, xw
.win
, NULL
, NULL
, &args
[0], 0, &shint
, &wmhint
, &chint
);
729 XStoreName(xw
.dis
, xw
.win
, TNAME
);
735 xdrawc(int x
, int y
, Glyph g
) {
736 XRectangle r
= { x
* xw
.cw
, y
* xw
.ch
, xw
.cw
, xw
.ch
};
737 unsigned long xfg
, xbg
;
740 if(g
.mode
& ATreverse
)
741 xfg
= dc
.col
[g
.bg
], xbg
= dc
.col
[g
.fg
];
743 xfg
= dc
.col
[g
.fg
], xbg
= dc
.col
[g
.bg
];
745 XSetForeground(xw
.dis
, dc
.gc
, xbg
);
746 XFillRectangles(xw
.dis
, xw
.win
, dc
.gc
, &r
, 1);
748 XSetForeground(xw
.dis
, dc
.gc
, xfg
);
749 XDrawString(xw
.dis
, xw
.win
, dc
.gc
, r
.x
, r
.y
+dc
.font
->ascent
, &(g
.c
), 1);
750 if(g
.mode
& ATbold
) /* XXX: bold hack (draw again at x+1) */
751 XDrawString(xw
.dis
, xw
.win
, dc
.gc
, r
.x
+1, r
.y
+dc
.font
->ascent
, &(g
.c
), 1);
753 if(g
.mode
& ATunderline
) {
754 r
.y
+= dc
.font
->ascent
+ 1;
755 XDrawLine(xw
.dis
, xw
.win
, dc
.gc
, r
.x
, r
.y
, r
.x
+r
.width
-1, r
.y
);
763 Glyph g
= {' ', ATnone
, DefaultBG
, DefaultCS
, 0};
765 if(term
.line
[term
.c
.y
][term
.c
.x
].state
& CRset
)
766 g
.c
= term
.line
[term
.c
.y
][term
.c
.x
].c
;
767 /* remove the old cursor */
768 if(term
.line
[oldy
][oldx
].state
& CRset
)
769 xdrawc(oldx
, oldy
, term
.line
[oldy
][oldx
]);
770 else xclear(oldx
, oldy
, oldx
, oldy
); /* XXX: maybe a bug */
771 if(mode
== CSdraw
&& !term
.c
.hidden
) {
772 xdrawc(term
.c
.x
, term
.c
.y
, g
);
773 oldx
= term
.c
.x
, oldy
= term
.c
.y
;
779 draw(int redraw_all
) {
784 XClearWindow(xw
.dis
, xw
.win
);
785 /* XXX: drawing could be optimised */
786 for(y
= 0; y
< term
.row
; y
++) {
787 for(x
= 0; x
< term
.col
; x
++) {
788 changed
= term
.line
[y
][x
].state
& CRupdate
;
789 set
= term
.line
[y
][x
].state
& CRset
;
790 if((changed
&& set
) || (redraw_all
&& set
)) {
791 term
.line
[y
][x
].state
&= ~CRupdate
;
792 xdrawc(x
, y
, term
.line
[y
][x
]);
800 kpress(XKeyEvent
*e
) {
807 meta
= e
->state
& Mod4Mask
;
808 shift
= e
->state
& ShiftMask
;
809 len
= XLookupString(e
, buf
, sizeof(buf
), &ksym
, NULL
);
811 buf
[sizeof(buf
)-1] = '\0';
820 printf("errkey: %d\n", (int)ksym
);
827 sprintf(buf
, "\033[%c", "DACB"[ksym
- XK_Left
]);
830 case XK_Delete
: ttywrite(KEYDELETE
, sizeof(KEYDELETE
)-1); break;
831 case XK_Home
: ttywrite( KEYHOME
, sizeof( KEYHOME
)-1); break;
832 case XK_End
: ttywrite( KEYEND
, sizeof( KEYEND
)-1); break;
833 case XK_Prior
: ttywrite( KEYPREV
, sizeof( KEYPREV
)-1); break;
834 case XK_Next
: ttywrite( KEYNEXT
, sizeof( KEYNEXT
)-1); break;
836 /* XXX: paste X clipboard */
845 col
= e
->xconfigure
.width
/ xw
.cw
;
846 row
= e
->xconfigure
.height
/ xw
.ch
;
848 if(term
.col
!= col
&& term
.row
!= row
) {
851 xw
.w
= e
->xconfigure
.width
;
852 xw
.h
= e
->xconfigure
.height
;
863 struct timeval tv
= {0, 10000};
866 XSelectInput(xw
.dis
, xw
.win
, ExposureMask
| KeyPressMask
| StructureNotifyMask
);
867 XResizeWindow(xw
.dis
, xw
.win
, xw
.w
, xw
.h
); /* seems to fix the resize bug in wmii */
869 while(XPending(xw
.dis
)) {
870 XNextEvent(xw
.dis
, &ev
);
880 case ConfigureNotify
:
887 ret
= select(cmdfd
+1, &rfd
, NULL
, NULL
, &tv
);
889 fprintf(stderr
, "select: %m\n");
894 if(FD_ISSET(cmdfd
, &rfd
)) {
902 main(int argc
, char *argv
[]) {
903 if(argc
== 2 && !strncmp("-v", argv
[1], 3))
904 die("st-" VERSION
", © 2009 st engineers\n");
906 die("usage: st [-v]\n");
907 setlocale(LC_CTYPE
, "");