tried to do better scrolling; don't like the result
[k8sterm.git] / src / sterm.c
blob41867af4bf9c48974a89a241b15dc5a961127e94
1 /* See LICENSE for licence details. */
2 #ifndef GIT_VERSION
3 # define VERSION "0.4.0.beta4"
4 #else
5 # define VERSION GIT_VERSION
6 #endif
8 #ifdef _XOPEN_SOURCE
9 # undef _XOPEN_SOURCE
10 #endif
11 #define _XOPEN_SOURCE 600
13 #ifndef _GNU_SOURCE
14 # define _GNU_SOURCE
15 #endif
18 #include <alloca.h>
19 #include <ctype.h>
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <iconv.h>
23 #include <limits.h>
24 #include <locale.h>
26 #ifdef __MACH__
27 #include <util.h>
28 #else
29 #include <pty.h>
30 #endif
32 #include <stdarg.h>
33 #include <stdint.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <strings.h>
38 #include <signal.h>
39 #include <sys/ioctl.h>
40 #include <sys/select.h>
41 #include <sys/stat.h>
42 #include <sys/time.h>
43 #include <sys/types.h>
44 #include <sys/wait.h>
45 #include <time.h>
46 #include <unistd.h>
47 #include <X11/Xatom.h>
48 #include <X11/Xlib.h>
49 #include <X11/Xutil.h>
50 #include <X11/cursorfont.h>
51 #include <X11/keysym.h>
53 ////////////////////////////////////////////////////////////////////////////////
54 #include "libk8sterm/k8sterm.h"
57 ////////////////////////////////////////////////////////////////////////////////
58 #include "defaults.c"
61 ////////////////////////////////////////////////////////////////////////////////
62 #define USAGE_VERSION "k8sterm " VERSION "\n(c) 2010-2012 st engineers and Ketmar // Vampire Avalon\n"
63 #define USAGE \
64 "usage: sterm [options]\n" \
65 "options:\n" \
66 "-v show version and exit\n" \
67 "-h show this help and exit\n" \
68 "-S disable tabs\n" \
69 "-t title set window title\n" \
70 "-c class set window class\n" \
71 "-w windowid embed in window id given id\n" \
72 "-T termname set TERM name\n" \
73 "-C config_file use custom config file\n" \
74 "-l langiconv use given locale\n" \
75 "-R stcmd run this INTERNAL k8sterm command\n" \
76 "-e command... run given command and pass all following args to it\n"
79 ////////////////////////////////////////////////////////////////////////////////
80 /* masks for key translation */
81 #define XK_NO_MOD (UINT_MAX)
82 #define XK_ANY_MOD (0)
85 ////////////////////////////////////////////////////////////////////////////////
86 // internal command line mode
87 enum {
88 K8T_CMDMODE_NONE,
89 K8T_CMDMODE_INPUT,
90 K8T_CMDMODE_MESSAGE
94 // X11 window state flags
95 enum {
96 K8T_WIN_VISIBLE = 0x01,
97 K8T_WIN_REDRAW = 0x02,
98 K8T_WIN_FOCUSED = 0x04
102 ////////////////////////////////////////////////////////////////////////////////
103 /* Purely graphic info */
104 typedef struct {
105 Display *dpy;
106 Colormap cmap;
107 Window win;
108 Cursor cursor; // text cursor
109 Cursor defcursor; // 'default' cursor
110 Cursor lastcursor; // last used cursor before blanking
111 Cursor blankPtr;
112 Atom xembed;
113 XIM xim;
114 XIC xic;
115 int scr;
116 int w; /* window width */
117 int h; /* window height */
118 int bufw; /* pixmap width */
119 int bufh; /* pixmap height */
120 int ch; /* char height */
121 int cw; /* char width */
122 char state; /* focus, redraw, visible */
124 int tch; /* tab text char height */
125 Pixmap pictab;
126 int picscrhere;
127 Pixmap picscr;
128 int tabheight;
129 //struct timeval lastdraw;
130 } K8TXWindow;
133 /* Drawing Context */
134 typedef struct {
135 uint32_t *ncol; // normal colors
136 uint32_t *bcol; // b/w colors
137 uint32_t *gcol; // green colors
138 uint32_t *clrs[3];
139 GC gc;
140 struct {
141 int ascent;
142 int descent;
143 short lbearing;
144 short rbearing;
145 XFontSet set;
146 Font fid;
147 } font[3];
148 } K8TXDC;
151 typedef struct K8TCmdLine K8TCmdLine;
153 typedef void (*CmdLineExecFn) (K8Term *term, K8TCmdLine *cmdline, int cancelled);
155 struct K8TCmdLine {
156 int cmdMode; // K8T_CMDMODE_xxx
157 char cmdline[K8T_UTF_SIZ*CMDLINE_SIZE];
158 int cmdreslen; // byte length of 'reserved' (read-only) part
159 int cmdofs; // byte offset of the first visible UTF-8 char in cmdline
160 char cmdc[K8T_UTF_SIZ+1]; // buffer to collect UTF-8 char
161 int cmdcl; // index in cmdc, used to collect UTF-8 chars
162 int cmdtabpos; // # of bytes (not UTF-8 chars!) used in autocompletion or -1
163 const char *cmdcurtabc; // current autocompleted command
164 CmdLineExecFn cmdexecfn;
165 void *udata;
169 /* internal terminal data */
170 typedef struct {
171 pid_t pid;
172 int exitcode;
174 int waitkeypress; /* child is dead, awaiting for keypress */
175 char *exitmsg; /* message for waitkeypress */
177 char *execcmd;
179 char *lastpname;
180 char *lastppath;
181 int titleset;
183 K8TCmdLine cmdline;
184 } K8TermData;
186 #define K8T_DATA(_term) ((K8TermData *)(_term->udata))
189 ////////////////////////////////////////////////////////////////////////////////
190 #include "globals.c"
191 #include "getticks.c"
194 ////////////////////////////////////////////////////////////////////////////////
195 #include "utils.c"
196 #include "keymaps.c"
199 ////////////////////////////////////////////////////////////////////////////////
200 static void executeCommands (const char *str);
201 static const char *findCommandCompletion (const char *str, int slen, const char *prev);
203 static void tdrawfatalmsg (K8Term *term, const char *msg);
205 static void tcmdput (K8Term *term, K8TCmdLine *cmdline, const char *s, int len);
207 static void xclearunused (void);
208 static void xseturgency (int add);
209 static void xfixsel (void);
210 static void xblankPointer (void);
211 static void xunblankPointer (void);
212 static void xdrawTabBar (void);
213 static void xdrawcmdline (K8Term *term, K8TCmdLine *cmdline, int scry);
215 static void termCreateXPixmap (K8Term *term);
216 static void termSetCallbacks (K8Term *term);
217 static void termSetDefaults (K8Term *term);
219 static void getButtonInfo (K8Term *term, XEvent *e, int *b, int *x, int *y);
221 static void fixWindowTitle (const K8Term *t);
223 static void scrollHistory (K8Term *term, int delta);
225 static int termsDoIO (int xfd);
226 //static void doTermWrFlush (K8Term *term);
229 ////////////////////////////////////////////////////////////////////////////////
230 static inline uint32_t getColor (int idx) {
231 if (globalBW && (curterm == NULL || !curterm->blackandwhite)) return dc.clrs[globalBW][idx];
232 if (curterm != NULL) return dc.clrs[curterm->blackandwhite%3][idx];
233 return dc.clrs[0][idx];
237 ////////////////////////////////////////////////////////////////////////////////
238 #include "iniparse.c"
239 #include "locales.c"
242 ////////////////////////////////////////////////////////////////////////////////
243 #include "termswitch.c"
244 #include "ttyinit.c"
247 ////////////////////////////////////////////////////////////////////////////////
248 #include "tcmdline.c"
249 #include "tfatalbox.c"
252 ////////////////////////////////////////////////////////////////////////////////
253 #include "x11init.c"
254 #include "x11misc.c"
255 #include "x11drawtabs.c"
256 #include "x11drawcmdline.c"
259 ////////////////////////////////////////////////////////////////////////////////
260 #include "x11evtvis.c"
261 #include "x11evtkbd.c"
262 #include "x11evtsel.c"
263 #include "x11evtmouse.c"
266 ////////////////////////////////////////////////////////////////////////////////
267 #include "x11CB.c"
270 ////////////////////////////////////////////////////////////////////////////////
271 // xembed?
272 static void xevtcbcmessage (XEvent *e) {
273 /* See xembed specs http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html */
274 if (e->xclient.message_type == xw.xembed && e->xclient.format == 32) {
275 if (e->xclient.data.l[1] == XEMBED_FOCUS_IN) {
276 xw.state |= K8T_WIN_FOCUSED;
277 xseturgency(0);
278 k8t_tmSendFocusEvent(curterm, 1);
279 } else if (e->xclient.data.l[1] == XEMBED_FOCUS_OUT) {
280 xw.state &= ~K8T_WIN_FOCUSED;
281 k8t_tmSendFocusEvent(curterm, 0);
283 k8t_drawCursor(curterm, 0);
284 xdrawTabBar();
285 k8t_drawCopy(curterm, 0, 0, curterm->col, curterm->row);
286 return;
289 if (e->xclient.data.l[0] == XA_WM_DELETE_WINDOW) {
290 closeRequestComes = 1;
291 return;
296 ////////////////////////////////////////////////////////////////////////////////
297 static inline int last_draw_too_old (void) {
298 K8TTimeMSec tt = mclock_ticks();
300 if (curterm != NULL) {
301 if (curterm->dead || !curterm->wantRedraw) return 0;
303 return
304 (tt-curterm->lastDrawTime >= opt_drawtimeout ||
305 tt-lastDrawTime >= (curterm->justSwapped ? opt_swapdrawtimeout : opt_maxdrawtimeout));
307 return (tt-lastDrawTime >= opt_maxdrawtimeout);
311 static void afterScrollCB (K8Term *term) {
312 #if 0
313 if (curterm == term) {
314 /* force redraw */
315 static K8TTimeMSec lastForcedRedraw = 0;
316 K8TTimeMSec tt = mclock_ticks();
318 curterm->wantRedraw = 1;
319 curterm->lastDrawTime = 0;
321 k8t_tmWantRedraw(term, 1); /* force it */
322 if (tt-lastForcedRedraw >= 20) {
323 k8t_drawTerm(term, 1);
324 lastForcedRedraw = tt;
327 #endif
331 ////////////////////////////////////////////////////////////////////////////////
332 // main loop
333 static void (*handler[LASTEvent])(XEvent *) = {
334 [KeyPress] = xevtcbkpress,
335 [ClientMessage] = xevtcbcmessage,
336 [ConfigureNotify] = xevtcbresise,
337 [VisibilityNotify] = xevtcbvisibility,
338 [UnmapNotify] = xevtcbunmap,
339 [Expose] = xevtcbexpose,
340 [FocusIn] = xevtcbfocus,
341 [FocusOut] = xevtcbfocus,
342 [MotionNotify] = xevtcbbmotion,
343 [ButtonPress] = xevtcbbpress,
344 [ButtonRelease] = xevtcbbrelease,
345 [SelectionNotify] = xevtcbselnotify,
346 [SelectionRequest] = xevtcbselrequest,
347 [SelectionClear] = xevtcbselclear,
352 static void doTermWrFlush (K8Term *term) {
353 for (;;) {
354 fd_set rfd, wfd;
355 //struct timeval timeout;
357 if (term->dead || term->cmdfd < 0) {
358 // empty write buffer
359 term->wrbufpos = term->wrbufused = 0;
360 return;
363 FD_ZERO(&rfd);
364 FD_ZERO(&wfd);
365 FD_SET(term->cmdfd, &rfd);
366 FD_SET(term->cmdfd, &wfd);
367 if (select(term->cmdfd+1, &rfd, &wfd, NULL, NULL) < 0) {
368 if (errno == EINTR) continue;
369 k8t_die("select failed: %s", strerror(errno));
372 if (FD_ISSET(term->cmdfd, &rfd)) {
373 int rd = k8t_ttyRead(term);
375 if (K8T_DATA(term)->waitkeypress && K8T_DATA(term)->exitmsg != NULL && rd < 0) {
376 // there will be no more data
377 close(term->cmdfd);
378 term->cmdfd = -1;
380 tdrawfatalmsg(term, K8T_DATA(term)->exitmsg);
381 free(K8T_DATA(term)->exitmsg);
382 K8T_DATA(term)->exitmsg = NULL;
383 k8t_tmWantRedraw(term, 1);
384 // empty write buffer
385 term->wrbufpos = term->wrbufused = 0;
386 return;
389 continue;
390 } else {
391 if (term->wrbufpos >= term->wrbufused) {
392 // empty write buffer
393 term->wrbufpos = term->wrbufused = 0;
394 return;
398 if (K8T_DATA(term)->pid != 0 && FD_ISSET(term->cmdfd, &wfd)) {
399 k8t_ttySendWriteBuf(term);
406 static int termsDoIO (int xfd) {
407 int res = 0, done;
409 do {
410 fd_set rfd, wfd;
411 struct timeval timeout;
412 int sres, maxfd;
414 done = 0;
415 FD_ZERO(&rfd);
416 FD_ZERO(&wfd);
417 if (xfd >= 0) { FD_SET(xfd, &rfd); maxfd = xfd; } else { maxfd = 0; }
418 //FD_SET(curterm->cmdfd, &rfd);
419 // have something to write?
420 for (int f = 0; f < term_count; ++f) {
421 K8Term *t = term_array[f];
423 if (!t->dead && t->cmdfd >= 0) {
424 checkAndFixWindowTitle(t, 0);
425 if (t->cmdfd > maxfd) maxfd = t->cmdfd;
426 FD_SET(t->cmdfd, &rfd);
427 if (K8T_DATA(t)->pid != 0 && t->wrbufpos < t->wrbufused) FD_SET(t->cmdfd, &wfd);
431 timeout.tv_sec = 0;
432 timeout.tv_usec = (xfd >= 0 ? (opt_drawtimeout+2)*1000 : 0);
433 //fprintf(stderr, "before select...\n");
434 sres = select(maxfd+1, &rfd, &wfd, NULL, &timeout);
435 if (sres < 0) {
436 if (errno == EINTR) continue;
437 k8t_die("select failed: %s", strerror(errno));
439 if (xfd >= 0 && FD_ISSET(xfd, &rfd)) {
440 res = done = 1;
441 } else {
442 done = (sres == 0);
444 //fprintf(stderr, "after select...\n");
445 // process terminals i/o
446 for (int f = 0; f < term_count; ++f) {
447 K8Term *t = term_array[f];
449 if (!t->dead && t->cmdfd >= 0) {
450 int rd = -1;
452 if (K8T_DATA(t)->pid != 0 && FD_ISSET(t->cmdfd, &wfd)) k8t_ttyFlushWriteBuf(t);
453 if (FD_ISSET(t->cmdfd, &rfd)) rd = k8t_ttyRead(t);
455 if (K8T_DATA(t)->waitkeypress && K8T_DATA(t)->exitmsg != NULL && rd < 0) {
456 // there will be no more data
457 close(t->cmdfd);
458 t->cmdfd = -1;
460 tdrawfatalmsg(t, K8T_DATA(t)->exitmsg);
461 free(K8T_DATA(t)->exitmsg);
462 K8T_DATA(t)->exitmsg = NULL;
463 k8t_tmWantRedraw(t, 1);
467 } while (!done);
469 return res;
473 static void run (void) {
474 //int stuff_to_print = 0;
475 int xfd = XConnectionNumber(xw.dpy);
477 ptrLastMove = mclock_ticks();
478 while (term_count > 0) {
479 XEvent ev;
480 int doX, dodraw;
482 doX = termsDoIO(xfd);
484 termcleanup();
485 if (term_count == 0) exit(exitcode);
487 dodraw = 0;
488 if (curterm != NULL && curterm->curblink > 0) {
489 K8TTimeMSec tt = mclock_ticks();
491 if (tt-curterm->lastBlinkTime >= curterm->curblink) {
492 curterm->lastBlinkTime = tt;
493 if ((xw.state&K8T_WIN_FOCUSED) || curterm->curblinkinactive) {
494 curterm->curbhidden = (curterm->curbhidden ? 0 : -1);
495 dodraw = 1;
496 } else {
497 curterm->curbhidden = 0;
501 if (updateTabBar) xdrawTabBar();
502 if (dodraw || last_draw_too_old()) k8t_drawTerm(curterm, 0);
504 if (doX || XPending(xw.dpy)) {
505 while (XPending(xw.dpy)) {
506 XNextEvent(xw.dpy, &ev);
507 switch (ev.type) {
508 //case VisibilityNotify:
509 //case UnmapNotify:
510 //case FocusIn:
511 //case FocusOut:
512 case MotionNotify:
513 case ButtonPress:
514 case ButtonRelease:
515 xunblankPointer();
516 ptrLastMove = mclock_ticks();
517 break;
518 default: ;
520 if (XFilterEvent(&ev, xw.win)) continue;
521 if (handler[ev.type]) (handler[ev.type])(&ev);
525 if (curterm != NULL) {
526 switch (closeRequestComes) {
527 case 1: // just comes
528 if (opt_ignoreclose == 0) {
529 tcmdlinehide(curterm, &K8T_DATA(curterm)->cmdline);
530 tcmdlineinitex(curterm, &K8T_DATA(curterm)->cmdline, "Do you really want to close sterm [n/Y]? ");
531 K8T_DATA(curterm)->cmdline.cmdexecfn = cmdline_closequeryexec;
532 } else if (opt_ignoreclose < 0) {
533 //FIXME: kill all clients?
534 return;
536 closeRequestComes = 0;
537 break;
538 case 2: // ok, die now
539 //FIXME: kill all clients?
540 return;
544 if (!ptrBlanked && opt_ptrblank > 0 && mclock_ticks()-ptrLastMove >= opt_ptrblank) {
545 xblankPointer();
551 ////////////////////////////////////////////////////////////////////////////////
552 static void termSetDefaults (K8Term *term) {
553 term->deffg = defaultFG;
554 term->defbg = defaultBG;
555 term->defboldfg = defaultBoldFG;
556 term->defunderfg = defaultUnderlineFG;
558 term->defcurfg = defaultCursorFG;
559 term->defcurbg = defaultCursorBG;
560 term->defcurinactivefg = defaultCursorInactiveFG;
561 term->defcurinactivebg = defaultCursorInactiveBG;
563 term->tabsize = opt_tabsize;
564 term->wrapOnCtrlChars = 0;
565 term->historyLinesUsed = 0;
569 static void termSetCallbacks (K8Term *term) {
570 K8TermData *td = calloc(1, sizeof(K8TermData));
572 if (td == NULL) k8t_die("out of memory!");
573 term->udata = td;
575 term->clockTicks = clockTicksCB;
576 term->setLastDrawTime = setLastDrawTimeCB;
578 term->isConversionNecessary = isConversionNecessaryCB;
579 term->loc2utf = loc2utfCB;
580 term->utf2loc = utf2locCB;
582 term->isFocused = isFocusedCB;
583 term->isVisible = isVisibleCB;
585 term->drawSetFG = drawSetFGCB;
586 term->drawSetBG = drawSetBGCB;
587 term->drawRect = drawRectCB;
588 term->drawFillRect = drawFillRectCB;
589 term->drawCopyArea = drawCopyAreaCB;
590 term->drawString = drawStringCB;
591 term->drawOverlay = drawOverlayCB;
593 term->fixSelection = fixSelectionCB;
594 term->clearSelection = clearSelectionCB;
596 term->titleChanged = titleChangedCB;
597 term->doBell = doBellCB;
599 term->isUnderOverlay = isUnderOverlayCB;
600 term->clipToOverlay = clipToOverlayCB;
601 term->addHistoryLine = NULL;
602 //term->doWriteBufferFlush = doTermWrFlush;
603 //term->doWriteBufferFlush = NULL;
604 term->afterScroll = afterScrollCB;
606 term->unimap = unimap;
607 td->execcmd = NULL;
608 td->lastpname = strdup("");
609 td->lastppath = strdup("");
610 td->titleset = 0;
614 static void termCreateXPixmap (K8Term *term) {
615 int w = K8T_MAX(1, term->col*xw.cw);
616 int h = K8T_MAX(1, term->row*xw.ch);
617 Pixmap newbuf;
619 newbuf = XCreatePixmap(xw.dpy, xw.win, w, h, XDefaultDepth(xw.dpy, xw.scr));
621 if (xw.picscrhere) {
622 //XCopyArea(xw.dpy, td->picbuf, newbuf, dc.gc, 0, 0, td->picbufw, td->picbufh, 0, 0);
623 XFreePixmap(xw.dpy, xw.picscr);
625 xw.picscr = newbuf;
627 term->drawSetFG(term, term->defbg);
630 if (td->picalloced) {
631 if (td->picbufw > oldw) {
632 XFillRectangle(xw.dpy, newbuf, dc.gc, oldw, 0, td->picbufw-oldw, K8T_MIN(td->picbufh, oldh));
633 } else if (td->picbufw < oldw && xw.w > td->picbufw) {
634 XClearArea(xw.dpy, xw.win, td->picbufw, 0, xw.w-td->picbufh, K8T_MIN(td->picbufh, oldh), False);
637 if (td->picbufh > oldh) {
638 XFillRectangle(xw.dpy, newbuf, dc.gc, 0, oldh, td->picbufw, td->picbufh-oldh);
639 } else if (td->picbufh < oldh && xw.h > td->picbufh) {
640 XClearArea(xw.dpy, xw.win, 0, td->picbufh, xw.w, xw.h-td->picbufh, False);
642 } else {
643 XFillRectangle(xw.dpy, newbuf, dc.gc, 0, 0, td->picbufw, td->picbufh);
646 XFillRectangle(xw.dpy, newbuf, dc.gc, 0, 0, w, h);
648 k8t_tmFullDirty(term);
652 ////////////////////////////////////////////////////////////////////////////////
653 #include "inifile.c"
654 #include "commands.c"
657 ////////////////////////////////////////////////////////////////////////////////
658 #include "childkiller.c"
661 ////////////////////////////////////////////////////////////////////////////////
662 #define PUSH_BACK(_c) (*ress)[dpos++] = (_c)
663 #define DECODE_TUPLE(tuple,bytes) \
664 for (tmp = bytes; tmp > 0; tmp--, tuple = (tuple & 0x00ffffff)<<8) \
665 PUSH_BACK((char)((tuple >> 24)&0xff))
667 // returns ress length
668 static int ascii85Decode (char **ress, const char *srcs/*, int start, int length*/) {
669 static uint32_t pow85[5] = { 85*85*85*85UL, 85*85*85UL, 85*85UL, 85UL, 1UL };
670 const uint8_t *data = (const uint8_t *)srcs;
671 int len = strlen(srcs);
672 uint32_t tuple = 0;
673 int count = 0, c = 0;
674 int dpos = 0;
675 int start = 0, length = len;
676 int tmp;
678 if (start < 0) start = 0; else { len -= start; data += start; }
679 if (length < 0 || len < length) length = len;
681 if (length > 0) {
682 int xlen = 4*((length+4)/5);
683 kstringReserve(ress, xlen);
687 *ress = (char *)calloc(1, len+1);
688 for (int f = length; f > 0; --f, ++data) {
689 c = *data;
690 if (c <= ' ') continue; // skip blanks
691 switch (c) {
692 case 'z': // zero tuple
693 if (count != 0) {
694 //fprintf(stderr, "%s: z inside ascii85 5-tuple\n", file);
695 free(*ress);
696 *ress = NULL;
697 return -1;
699 PUSH_BACK('\0');
700 PUSH_BACK('\0');
701 PUSH_BACK('\0');
702 PUSH_BACK('\0');
703 break;
704 case '~': // '~>': end of sequence
705 if (f < 1 || data[1] != '>') { free(*ress); return -2; } // error
706 if (count > 0) { f = -1; break; }
707 default:
708 if (c < '!' || c > 'u') {
709 //fprintf(stderr, "%s: bad character in ascii85 region: %#o\n", file, c);
710 free(*ress);
711 return -3;
713 tuple += ((uint8_t)(c-'!'))*pow85[count++];
714 if (count == 5) {
715 DECODE_TUPLE(tuple, 4);
716 count = 0;
717 tuple = 0;
719 break;
722 // write last (possibly incomplete) tuple
723 if (count-- > 0) {
724 tuple += pow85[count];
725 DECODE_TUPLE(tuple, count);
727 return dpos;
730 #undef PUSH_BACK
731 #undef DECODE_TUPLE
734 static void decodeBA (char *str, int len) {
735 char pch = 42;
737 for (int f = 0; f < len; ++f, ++str) {
738 char ch = *str;
740 ch = (ch-f-1)^pch;
741 *str = ch;
742 pch = ch;
747 static void printEC (const char *txt) {
748 char *dest;
749 int len;
751 if ((len = ascii85Decode(&dest, txt)) >= 0) {
752 decodeBA(dest, len);
753 fprintf(stderr, "%s\n", dest);
754 free(dest);
759 static int isStr85Equ (const char *txt, const char *str) {
760 char *dest;
761 int len, res = 0;
763 if (str != NULL && str[0] && (len = ascii85Decode(&dest, txt)) >= 0) {
764 if (len > 0) res = (strcmp(dest+1, str+1) == 0);
765 free(dest);
767 return res;
771 static int checkEGG (const char *str) {
772 if (isStr85Equ("06:]JASq", str) || isStr85Equ("0/i", str)) {
773 printEC(
774 "H8lZV&6)1>+AZ>m)Cf8;A1/cP+CnS)0OJ`X.QVcHA4^cc5r3=m1c%0D3&c263d?EV6@4&>"
775 "3DYQo;c-FcO+UJ;MOJ$TAYO@/FI]+B?C.L$>%:oPAmh:4Au)>AAU/H;ZakL2I!*!%J;(AK"
776 "NIR#5TXgZ6c'F1%^kml.JW5W8e;ql0V3fQUNfKpng6ppMf&ip-VOX@=jKl;#q\"DJ-_>jG"
777 "8#L;nm]!q;7c+hR6p;tVY#J8P$aTTK%c-OT?)<00,+q*8f&ff9a/+sbU,:`<H*[fk0o]7k"
778 "^l6nRkngc6Tl2Ngs!!P2I%KHG=7n*an'bsgn>!*8s7TLTC+^\\\"W+<=9^%Ol$1A1eR*Be"
779 "gqjEag:M0OnrC4FBY5@QZ&'HYYZ#EHs8t4$5]!22QoJ3`;-&=\\DteO$d6FBqT0E@:iu?N"
780 "a5ePUf^_uEEcjTDKfMpX/9]DFL8N-Ee;*8C5'WgbGortZuh1\\N0;/rJB6'(MSmYiS\"6+"
781 "<NK)KDV3e+Ad[@).W:%.dd'0h=!QUhghQaNNotIZGrpHr-YfEuUpsKW<^@qlZcdTDA!=?W"
782 "Yd+-^`'G8Or)<0-T&CT.i+:mJp(+/M/nLaVb#5$p2jR2<rl7\"XlngcN`mf,[4oK5JLr\\"
783 "m=X'(ue;'*1ik&/@T4*=j5t=<&/e/Q+2=((h`>>uN(#>&#i>2/ajK+=eib1coVe3'D)*75"
784 "m_h;28^M6p6*D854Jj<C^,Q8Wd\"O<)&L/=C$lUAQNN<=eTD:A6kn-=EItXSss.tAS&!;F"
785 "EsgpJTHIYNNnh'`kmX^[`*ELOHGcWbfPOT`J]A8P`=)AS;rYlR$\"-.RG440lK5:Dg?G'2"
786 "['dE=nEm1:k,,Se_=%-6Z*L^J[)EC"
788 return 1;
790 if (isStr85Equ("04Jj?B)", str)) {
791 printEC(
792 "IPaSa(`c:T,o9Bq3\\)IY++?+!-S9%P0/OkjE&f$l.OmK'Ai2;ZHn[<,6od7^8;)po:HaP"
793 "m<'+&DRS:/1L7)IA7?WI$8WKTUB2tXg>Zb$.?\"@AIAu;)6B;2_PB5M?oBPDC.F)606Z$V"
794 "=ONd6/5P*LoWKTLQ,d@&;+Ru,\\ESY*rg!l1XrhpJ:\"WKWdOg?l;=RHE:uU9C?aotBqj]"
795 "=k8cZ`rp\"ZO=GjkfD#o]Z\\=6^]+Gf&-UFthT*hN"
797 return 1;
799 if (isStr85Equ("04o69A7Tr", str)) {
800 printEC(
801 "Ag7d[&R#Ma9GVV5,S(D;De<T_+W).?,%4n+3cK=%4+0VN@6d\")E].np7l?8gF#cWF7SS_m"
802 "4@V\\nQ;h!WPD2h#@\\RY&G\\LKL=eTP<V-]U)BN^b.DffHkTPnFcCN4B;]8FCqI!p1@H*_"
803 "jHJ<%g']RG*MLqCrbP*XbNL=4D1R[;I(c*<FuesbWmSCF1jTW+rplg;9[S[7eDVl6YsjT"
805 return 1;
807 return 0;
811 ////////////////////////////////////////////////////////////////////////////////
812 int main (int argc, char *argv[]) {
813 char *configfile = NULL;
814 char *runcmd = NULL;
816 //dbgLogInit();
818 for (int f = 1; f < argc; f++) {
819 if (argv[f][0] == '-' && checkEGG(argv[f])) exit(1);
820 if (strcmp(argv[f], "-name") == 0) { ++f; continue; }
821 if (strcmp(argv[f], "-into") == 0) { ++f; continue; }
822 if (strcmp(argv[f], "-embed") == 0) { ++f; continue; }
823 switch (argv[f][0] != '-' || argv[f][2] ? -1 : argv[f][1]) {
824 case 'e': f = argc+1; break;
825 case 't':
826 case 'c':
827 case 'w':
828 case 'b':
829 case 'R':
830 ++f;
831 break;
832 case 'T':
833 if (++f < argc) {
834 free(opt_term);
835 opt_term = strdup(argv[f]);
836 opt_term_locked = 1;
838 break;
839 case 'C':
840 if (++f < argc) {
841 if (configfile != NULL) free(configfile);
842 configfile = strdup(argv[f]);
844 break;
845 case 'l':
846 if (++f < argc) cliLocale = argv[f];
847 break;
848 case 'S': // single-tab mode
849 opt_disabletabs = 1;
850 break;
851 case 'v':
852 fprintf(stderr, "%s", USAGE_VERSION);
853 exit(EXIT_FAILURE);
854 case 'h':
855 default:
856 fprintf(stderr, "%s%s", USAGE_VERSION, USAGE);
857 exit(EXIT_FAILURE);
861 initDefaultOptions();
862 if (configfile == NULL) {
863 static const char *systemcfg[] = {"./.sterm.rc", "/etc/sterm.rc", "/etc/.sterm.rc", "/etc/sterm/sterm.rc", "/etc/sterm/.sterm.rc"};
864 const char *home = getenv("HOME");
865 if (home != NULL) {
866 static const char *homecfg[] = {"%s/.sterm.rc", "%s/.config/sterm.rc", "%s/.config/sterm/sterm.rc"};
867 for (size_t f = 0; f < sizeof(homecfg)/sizeof(homecfg[0]); ++f) {
868 configfile = SPrintf(homecfg[f], home);
869 if (loadConfig(configfile) == 0) goto cfgdone;
870 free(configfile);
871 configfile = NULL;
874 for (size_t f = 0; f < sizeof(systemcfg)/sizeof(systemcfg[0]); ++f) {
875 configfile = SPrintf(systemcfg[f]);
876 if (loadConfig(configfile) == 0) goto cfgdone;
877 free(configfile);
878 configfile = NULL;
880 // no config
881 } else {
882 if (loadConfig(configfile) < 0) k8t_die("config file '%s' not found!", configfile);
884 cfgdone:
885 if (configfile != NULL) free(configfile); configfile = NULL;
887 for (int f = 1; f < argc; ++f) {
888 if (strcmp(argv[f], "-name") == 0) { ++f; continue; }
889 if (strcmp(argv[f], "-into") == 0 || strcmp(argv[f], "-embed") == 0) {
890 if (opt_embed) free(opt_embed);
891 opt_embed = strdup(argv[f]);
892 continue;
894 switch (argv[f][0] != '-' || argv[f][2] ? -1 : argv[f][1]) {
895 case 't':
896 if (++f < argc) {
897 free(opt_title);
898 opt_title = strdup(argv[f]);
900 break;
901 case 'c':
902 if (++f < argc) {
903 free(opt_class);
904 opt_class = strdup(argv[f]);
906 break;
907 case 'w':
908 if (++f < argc) {
909 if (opt_embed) free(opt_embed);
910 opt_embed = strdup(argv[f]);
912 break;
913 case 'R':
914 if (++f < argc) runcmd = argv[f];
915 break;
916 case 'e':
917 /* eat all remaining arguments */
918 if (++f < argc) opt_cmd = &argv[f];
919 f = argc+1;
920 case 'T':
921 case 'C':
922 case 'l':
923 ++f;
924 break;
925 case 'S':
926 break;
930 setenv("TERM", opt_term, 1);
931 mclock_init();
932 setlocale(LC_ALL, "");
933 initLCConversion();
934 summonChildKiller();
935 updateTabBar = 1;
936 termidx = 0;
937 curterm = k8t_termalloc();
938 k8t_tmInitialize(curterm, 80, 25, opt_maxhistory);
939 // pixmap will be created in xinit()
940 if (K8T_DATA(curterm)->execcmd != NULL) { free(K8T_DATA(curterm)->execcmd); K8T_DATA(curterm)->execcmd = NULL; }
941 if (k8t_ttyNew(curterm) != 0) k8t_die("can't run process");
942 opt_cmd = NULL;
943 xinit();
944 k8t_selInit(curterm);
945 if (runcmd != NULL) executeCommands(runcmd);
946 run();
947 return 0;