some redraw fixes
[k8sterm.git] / src / sterm.c
blobd30217c04eb73cb90afda48fcedde55e4e4e200a
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 void afterNewLineCB (K8Term *term) {
298 if (curterm == term && opt_fastupdate) {
299 /* force redraw */
300 static K8TTimeMSec lastForcedRedraw = 0; //FIXME: nasty global!
301 K8TTimeMSec tt = mclock_ticks();
303 curterm->wantRedraw = 1;
304 curterm->lastDrawTime = 0;
306 k8t_tmWantRedraw(term, 1); /* force it */
307 if (tt >= lastForcedRedraw) {
308 k8t_drawTerm(term, 1);
309 lastForcedRedraw = tt+100;
315 static inline int last_draw_too_old (void) {
316 K8TTimeMSec tt = mclock_ticks();
318 if (curterm != NULL) {
319 if (curterm->dead || !curterm->wantRedraw) return 0;
321 if (curterm->justSwapped) {
322 afterNewLineCB(curterm);
323 //curterm->justSwapped = 0; repainter will fix it
326 return
327 (tt-curterm->lastDrawTime >= opt_drawtimeout ||
328 tt-lastDrawTime >= (curterm->justSwapped ? opt_swapdrawtimeout : opt_maxdrawtimeout));
330 return (tt-lastDrawTime >= opt_maxdrawtimeout);
334 ////////////////////////////////////////////////////////////////////////////////
335 // main loop
336 static void (*handler[LASTEvent])(XEvent *) = {
337 [KeyPress] = xevtcbkpress,
338 [ClientMessage] = xevtcbcmessage,
339 [ConfigureNotify] = xevtcbresise,
340 [VisibilityNotify] = xevtcbvisibility,
341 [UnmapNotify] = xevtcbunmap,
342 [Expose] = xevtcbexpose,
343 [FocusIn] = xevtcbfocus,
344 [FocusOut] = xevtcbfocus,
345 [MotionNotify] = xevtcbbmotion,
346 [ButtonPress] = xevtcbbpress,
347 [ButtonRelease] = xevtcbbrelease,
348 [SelectionNotify] = xevtcbselnotify,
349 [SelectionRequest] = xevtcbselrequest,
350 [SelectionClear] = xevtcbselclear,
355 static void doTermWrFlush (K8Term *term) {
356 for (;;) {
357 fd_set rfd, wfd;
358 //struct timeval timeout;
360 if (term->dead || term->cmdfd < 0) {
361 // empty write buffer
362 term->wrbufpos = term->wrbufused = 0;
363 return;
366 FD_ZERO(&rfd);
367 FD_ZERO(&wfd);
368 FD_SET(term->cmdfd, &rfd);
369 FD_SET(term->cmdfd, &wfd);
370 if (select(term->cmdfd+1, &rfd, &wfd, NULL, NULL) < 0) {
371 if (errno == EINTR) continue;
372 k8t_die("select failed: %s", strerror(errno));
375 if (FD_ISSET(term->cmdfd, &rfd)) {
376 int rd = k8t_ttyRead(term);
378 if (K8T_DATA(term)->waitkeypress && K8T_DATA(term)->exitmsg != NULL && rd < 0) {
379 // there will be no more data
380 close(term->cmdfd);
381 term->cmdfd = -1;
383 tdrawfatalmsg(term, K8T_DATA(term)->exitmsg);
384 free(K8T_DATA(term)->exitmsg);
385 K8T_DATA(term)->exitmsg = NULL;
386 k8t_tmWantRedraw(term, 1);
387 // empty write buffer
388 term->wrbufpos = term->wrbufused = 0;
389 return;
392 continue;
393 } else {
394 if (term->wrbufpos >= term->wrbufused) {
395 // empty write buffer
396 term->wrbufpos = term->wrbufused = 0;
397 return;
401 if (K8T_DATA(term)->pid != 0 && FD_ISSET(term->cmdfd, &wfd)) {
402 k8t_ttySendWriteBuf(term);
409 static int termsDoIO (int xfd) {
410 int res = 0, done;
412 do {
413 fd_set rfd, wfd;
414 struct timeval timeout;
415 int sres, maxfd;
417 done = 0;
418 FD_ZERO(&rfd);
419 FD_ZERO(&wfd);
420 if (xfd >= 0) { FD_SET(xfd, &rfd); maxfd = xfd; } else { maxfd = 0; }
421 //FD_SET(curterm->cmdfd, &rfd);
422 // have something to write?
423 for (int f = 0; f < term_count; ++f) {
424 K8Term *t = term_array[f];
426 if (!t->dead && t->cmdfd >= 0) {
427 checkAndFixWindowTitle(t, 0);
428 if (t->cmdfd > maxfd) maxfd = t->cmdfd;
429 FD_SET(t->cmdfd, &rfd);
430 if (K8T_DATA(t)->pid != 0 && t->wrbufpos < t->wrbufused) FD_SET(t->cmdfd, &wfd);
434 timeout.tv_sec = 0;
435 timeout.tv_usec = (xfd >= 0 ? (opt_drawtimeout+2)*1000 : 0);
436 //fprintf(stderr, "before select...\n");
437 sres = select(maxfd+1, &rfd, &wfd, NULL, &timeout);
438 if (sres < 0) {
439 if (errno == EINTR) continue;
440 k8t_die("select failed: %s", strerror(errno));
442 if (xfd >= 0 && FD_ISSET(xfd, &rfd)) {
443 res = done = 1;
444 } else {
445 done = (sres == 0);
447 //fprintf(stderr, "after select...\n");
448 // process terminals i/o
449 for (int f = 0; f < term_count; ++f) {
450 K8Term *t = term_array[f];
452 if (!t->dead && t->cmdfd >= 0) {
453 int rd = -1;
455 if (K8T_DATA(t)->pid != 0 && FD_ISSET(t->cmdfd, &wfd)) k8t_ttyFlushWriteBuf(t);
456 if (FD_ISSET(t->cmdfd, &rfd)) rd = k8t_ttyRead(t);
458 if (K8T_DATA(t)->waitkeypress && K8T_DATA(t)->exitmsg != NULL && rd < 0) {
459 // there will be no more data
460 close(t->cmdfd);
461 t->cmdfd = -1;
463 tdrawfatalmsg(t, K8T_DATA(t)->exitmsg);
464 free(K8T_DATA(t)->exitmsg);
465 K8T_DATA(t)->exitmsg = NULL;
466 k8t_tmWantRedraw(t, 1);
470 } while (!done);
472 return res;
476 static void run (void) {
477 //int stuff_to_print = 0;
478 int xfd = XConnectionNumber(xw.dpy);
480 ptrLastMove = mclock_ticks();
481 while (term_count > 0) {
482 XEvent ev;
483 int doX, dodraw;
485 doX = termsDoIO(xfd);
487 termcleanup();
488 if (term_count == 0) exit(exitcode);
490 dodraw = 0;
491 if (curterm != NULL && curterm->curblink > 0) {
492 K8TTimeMSec tt = mclock_ticks();
494 if (tt-curterm->lastBlinkTime >= curterm->curblink) {
495 curterm->lastBlinkTime = tt;
496 if ((xw.state&K8T_WIN_FOCUSED) || curterm->curblinkinactive) {
497 curterm->curbhidden = (curterm->curbhidden ? 0 : -1);
498 dodraw = 1;
499 } else {
500 curterm->curbhidden = 0;
504 if (updateTabBar) xdrawTabBar();
505 if (dodraw || last_draw_too_old()) k8t_drawTerm(curterm, 0);
507 if (doX || XPending(xw.dpy)) {
508 while (XPending(xw.dpy)) {
509 XNextEvent(xw.dpy, &ev);
510 switch (ev.type) {
511 //case VisibilityNotify:
512 //case UnmapNotify:
513 //case FocusIn:
514 //case FocusOut:
515 case MotionNotify:
516 case ButtonPress:
517 case ButtonRelease:
518 xunblankPointer();
519 ptrLastMove = mclock_ticks();
520 break;
521 default: ;
523 if (XFilterEvent(&ev, xw.win)) continue;
524 if (handler[ev.type]) (handler[ev.type])(&ev);
528 if (curterm != NULL) {
529 switch (closeRequestComes) {
530 case 1: // just comes
531 if (opt_ignoreclose == 0) {
532 tcmdlinehide(curterm, &K8T_DATA(curterm)->cmdline);
533 tcmdlineinitex(curterm, &K8T_DATA(curterm)->cmdline, "Do you really want to close sterm [N/y]? ");
534 K8T_DATA(curterm)->cmdline.cmdexecfn = cmdline_closequeryexec;
535 } else if (opt_ignoreclose < 0) {
536 //FIXME: kill all clients?
537 return;
539 closeRequestComes = 0;
540 break;
541 case 2: // ok, die now
542 //FIXME: kill all clients?
543 return;
547 if (!ptrBlanked && opt_ptrblank > 0 && mclock_ticks()-ptrLastMove >= opt_ptrblank) {
548 xblankPointer();
554 ////////////////////////////////////////////////////////////////////////////////
555 static void termSetDefaults (K8Term *term) {
556 term->deffg = defaultFG;
557 term->defbg = defaultBG;
558 term->defboldfg = defaultBoldFG;
559 term->defunderfg = defaultUnderlineFG;
561 term->defcurfg = defaultCursorFG;
562 term->defcurbg = defaultCursorBG;
563 term->defcurinactivefg = defaultCursorInactiveFG;
564 term->defcurinactivebg = defaultCursorInactiveBG;
566 term->tabsize = opt_tabsize;
567 term->wrapOnCtrlChars = 0;
568 term->historyLinesUsed = 0;
572 static void termSetCallbacks (K8Term *term) {
573 K8TermData *td = calloc(1, sizeof(K8TermData));
575 if (td == NULL) k8t_die("out of memory!");
576 term->udata = td;
578 term->clockTicks = clockTicksCB;
579 term->setLastDrawTime = setLastDrawTimeCB;
581 term->isConversionNecessary = isConversionNecessaryCB;
582 term->loc2utf = loc2utfCB;
583 term->utf2loc = utf2locCB;
585 term->isFocused = isFocusedCB;
586 term->isVisible = isVisibleCB;
588 term->drawSetFG = drawSetFGCB;
589 term->drawSetBG = drawSetBGCB;
590 term->drawRect = drawRectCB;
591 term->drawFillRect = drawFillRectCB;
592 term->drawCopyArea = drawCopyAreaCB;
593 term->drawString = drawStringCB;
594 term->drawOverlay = drawOverlayCB;
596 term->fixSelection = fixSelectionCB;
597 term->clearSelection = clearSelectionCB;
599 term->titleChanged = titleChangedCB;
600 term->doBell = doBellCB;
602 term->isUnderOverlay = isUnderOverlayCB;
603 term->clipToOverlay = clipToOverlayCB;
604 term->addHistoryLine = NULL;
605 //term->doWriteBufferFlush = doTermWrFlush;
606 //term->doWriteBufferFlush = NULL;
607 term->afterNewLine = afterNewLineCB;
608 term->afterScreenSwap = afterNewLineCB; // the same
610 term->unimap = unimap;
611 td->execcmd = NULL;
612 td->lastpname = strdup("");
613 td->lastppath = strdup("");
614 td->titleset = 0;
618 static void termCreateXPixmap (K8Term *term) {
619 int w = K8T_MAX(1, term->col*xw.cw);
620 int h = K8T_MAX(1, term->row*xw.ch);
621 Pixmap newbuf;
623 newbuf = XCreatePixmap(xw.dpy, xw.win, w, h, XDefaultDepth(xw.dpy, xw.scr));
625 if (xw.picscrhere) {
626 //XCopyArea(xw.dpy, td->picbuf, newbuf, dc.gc, 0, 0, td->picbufw, td->picbufh, 0, 0);
627 XFreePixmap(xw.dpy, xw.picscr);
629 xw.picscr = newbuf;
631 term->drawSetFG(term, term->defbg);
634 if (td->picalloced) {
635 if (td->picbufw > oldw) {
636 XFillRectangle(xw.dpy, newbuf, dc.gc, oldw, 0, td->picbufw-oldw, K8T_MIN(td->picbufh, oldh));
637 } else if (td->picbufw < oldw && xw.w > td->picbufw) {
638 XClearArea(xw.dpy, xw.win, td->picbufw, 0, xw.w-td->picbufh, K8T_MIN(td->picbufh, oldh), False);
641 if (td->picbufh > oldh) {
642 XFillRectangle(xw.dpy, newbuf, dc.gc, 0, oldh, td->picbufw, td->picbufh-oldh);
643 } else if (td->picbufh < oldh && xw.h > td->picbufh) {
644 XClearArea(xw.dpy, xw.win, 0, td->picbufh, xw.w, xw.h-td->picbufh, False);
646 } else {
647 XFillRectangle(xw.dpy, newbuf, dc.gc, 0, 0, td->picbufw, td->picbufh);
650 XFillRectangle(xw.dpy, newbuf, dc.gc, 0, 0, w, h);
652 k8t_tmFullDirty(term);
656 ////////////////////////////////////////////////////////////////////////////////
657 #include "inifile.c"
658 #include "commands.c"
661 ////////////////////////////////////////////////////////////////////////////////
662 #include "childkiller.c"
665 ////////////////////////////////////////////////////////////////////////////////
666 #define PUSH_BACK(_c) (*ress)[dpos++] = (_c)
667 #define DECODE_TUPLE(tuple,bytes) \
668 for (tmp = bytes; tmp > 0; tmp--, tuple = (tuple & 0x00ffffff)<<8) \
669 PUSH_BACK((char)((tuple >> 24)&0xff))
671 // returns ress length
672 static int ascii85Decode (char **ress, const char *srcs/*, int start, int length*/) {
673 static uint32_t pow85[5] = { 85*85*85*85UL, 85*85*85UL, 85*85UL, 85UL, 1UL };
674 const uint8_t *data = (const uint8_t *)srcs;
675 int len = strlen(srcs);
676 uint32_t tuple = 0;
677 int count = 0, c = 0;
678 int dpos = 0;
679 int start = 0, length = len;
680 int tmp;
682 if (start < 0) start = 0; else { len -= start; data += start; }
683 if (length < 0 || len < length) length = len;
685 if (length > 0) {
686 int xlen = 4*((length+4)/5);
687 kstringReserve(ress, xlen);
691 *ress = (char *)calloc(1, len+1);
692 for (int f = length; f > 0; --f, ++data) {
693 c = *data;
694 if (c <= ' ') continue; // skip blanks
695 switch (c) {
696 case 'z': // zero tuple
697 if (count != 0) {
698 //fprintf(stderr, "%s: z inside ascii85 5-tuple\n", file);
699 free(*ress);
700 *ress = NULL;
701 return -1;
703 PUSH_BACK('\0');
704 PUSH_BACK('\0');
705 PUSH_BACK('\0');
706 PUSH_BACK('\0');
707 break;
708 case '~': // '~>': end of sequence
709 if (f < 1 || data[1] != '>') { free(*ress); return -2; } // error
710 if (count > 0) { f = -1; break; }
711 default:
712 if (c < '!' || c > 'u') {
713 //fprintf(stderr, "%s: bad character in ascii85 region: %#o\n", file, c);
714 free(*ress);
715 return -3;
717 tuple += ((uint8_t)(c-'!'))*pow85[count++];
718 if (count == 5) {
719 DECODE_TUPLE(tuple, 4);
720 count = 0;
721 tuple = 0;
723 break;
726 // write last (possibly incomplete) tuple
727 if (count-- > 0) {
728 tuple += pow85[count];
729 DECODE_TUPLE(tuple, count);
731 return dpos;
734 #undef PUSH_BACK
735 #undef DECODE_TUPLE
738 static void decodeBA (char *str, int len) {
739 char pch = 42;
741 for (int f = 0; f < len; ++f, ++str) {
742 char ch = *str;
744 ch = (ch-f-1)^pch;
745 *str = ch;
746 pch = ch;
751 static void printEC (const char *txt) {
752 char *dest;
753 int len;
755 if ((len = ascii85Decode(&dest, txt)) >= 0) {
756 decodeBA(dest, len);
757 fprintf(stderr, "%s\n", dest);
758 free(dest);
763 static int isStr85Equ (const char *txt, const char *str) {
764 char *dest;
765 int len, res = 0;
767 if (str != NULL && str[0] && (len = ascii85Decode(&dest, txt)) >= 0) {
768 if (len > 0) res = (strcmp(dest+1, str+1) == 0);
769 free(dest);
771 return res;
775 static int checkEGG (const char *str) {
776 if (isStr85Equ("06:]JASq", str) || isStr85Equ("0/i", str)) {
777 printEC(
778 "H8lZV&6)1>+AZ>m)Cf8;A1/cP+CnS)0OJ`X.QVcHA4^cc5r3=m1c%0D3&c263d?EV6@4&>"
779 "3DYQo;c-FcO+UJ;MOJ$TAYO@/FI]+B?C.L$>%:oPAmh:4Au)>AAU/H;ZakL2I!*!%J;(AK"
780 "NIR#5TXgZ6c'F1%^kml.JW5W8e;ql0V3fQUNfKpng6ppMf&ip-VOX@=jKl;#q\"DJ-_>jG"
781 "8#L;nm]!q;7c+hR6p;tVY#J8P$aTTK%c-OT?)<00,+q*8f&ff9a/+sbU,:`<H*[fk0o]7k"
782 "^l6nRkngc6Tl2Ngs!!P2I%KHG=7n*an'bsgn>!*8s7TLTC+^\\\"W+<=9^%Ol$1A1eR*Be"
783 "gqjEag:M0OnrC4FBY5@QZ&'HYYZ#EHs8t4$5]!22QoJ3`;-&=\\DteO$d6FBqT0E@:iu?N"
784 "a5ePUf^_uEEcjTDKfMpX/9]DFL8N-Ee;*8C5'WgbGortZuh1\\N0;/rJB6'(MSmYiS\"6+"
785 "<NK)KDV3e+Ad[@).W:%.dd'0h=!QUhghQaNNotIZGrpHr-YfEuUpsKW<^@qlZcdTDA!=?W"
786 "Yd+-^`'G8Or)<0-T&CT.i+:mJp(+/M/nLaVb#5$p2jR2<rl7\"XlngcN`mf,[4oK5JLr\\"
787 "m=X'(ue;'*1ik&/@T4*=j5t=<&/e/Q+2=((h`>>uN(#>&#i>2/ajK+=eib1coVe3'D)*75"
788 "m_h;28^M6p6*D854Jj<C^,Q8Wd\"O<)&L/=C$lUAQNN<=eTD:A6kn-=EItXSss.tAS&!;F"
789 "EsgpJTHIYNNnh'`kmX^[`*ELOHGcWbfPOT`J]A8P`=)AS;rYlR$\"-.RG440lK5:Dg?G'2"
790 "['dE=nEm1:k,,Se_=%-6Z*L^J[)EC"
792 return 1;
794 if (isStr85Equ("04Jj?B)", str)) {
795 printEC(
796 "IPaSa(`c:T,o9Bq3\\)IY++?+!-S9%P0/OkjE&f$l.OmK'Ai2;ZHn[<,6od7^8;)po:HaP"
797 "m<'+&DRS:/1L7)IA7?WI$8WKTUB2tXg>Zb$.?\"@AIAu;)6B;2_PB5M?oBPDC.F)606Z$V"
798 "=ONd6/5P*LoWKTLQ,d@&;+Ru,\\ESY*rg!l1XrhpJ:\"WKWdOg?l;=RHE:uU9C?aotBqj]"
799 "=k8cZ`rp\"ZO=GjkfD#o]Z\\=6^]+Gf&-UFthT*hN"
801 return 1;
803 if (isStr85Equ("04o69A7Tr", str)) {
804 printEC(
805 "Ag7d[&R#Ma9GVV5,S(D;De<T_+W).?,%4n+3cK=%4+0VN@6d\")E].np7l?8gF#cWF7SS_m"
806 "4@V\\nQ;h!WPD2h#@\\RY&G\\LKL=eTP<V-]U)BN^b.DffHkTPnFcCN4B;]8FCqI!p1@H*_"
807 "jHJ<%g']RG*MLqCrbP*XbNL=4D1R[;I(c*<FuesbWmSCF1jTW+rplg;9[S[7eDVl6YsjT"
809 return 1;
811 return 0;
815 ////////////////////////////////////////////////////////////////////////////////
816 int main (int argc, char *argv[]) {
817 char *configfile = NULL;
818 char *runcmd = NULL;
820 //dbgLogInit();
822 for (int f = 1; f < argc; f++) {
823 if (argv[f][0] == '-' && checkEGG(argv[f])) exit(1);
824 if (strcmp(argv[f], "-name") == 0) { ++f; continue; }
825 if (strcmp(argv[f], "-into") == 0) { ++f; continue; }
826 if (strcmp(argv[f], "-embed") == 0) { ++f; continue; }
827 switch (argv[f][0] != '-' || argv[f][2] ? -1 : argv[f][1]) {
828 case 'e': f = argc+1; break;
829 case 't':
830 case 'c':
831 case 'w':
832 case 'b':
833 case 'R':
834 ++f;
835 break;
836 case 'T':
837 if (++f < argc) {
838 free(opt_term);
839 opt_term = strdup(argv[f]);
840 opt_term_locked = 1;
842 break;
843 case 'C':
844 if (++f < argc) {
845 if (configfile != NULL) free(configfile);
846 configfile = strdup(argv[f]);
848 break;
849 case 'l':
850 if (++f < argc) cliLocale = argv[f];
851 break;
852 case 'S': // single-tab mode
853 opt_disabletabs = 1;
854 break;
855 case 'v':
856 fprintf(stderr, "%s", USAGE_VERSION);
857 exit(EXIT_FAILURE);
858 case 'h':
859 default:
860 fprintf(stderr, "%s%s", USAGE_VERSION, USAGE);
861 exit(EXIT_FAILURE);
865 initDefaultOptions();
866 if (configfile == NULL) {
867 static const char *systemcfg[] = {"./.sterm.rc", "/etc/sterm.rc", "/etc/.sterm.rc", "/etc/sterm/sterm.rc", "/etc/sterm/.sterm.rc"};
868 const char *home = getenv("HOME");
869 if (home != NULL) {
870 static const char *homecfg[] = {"%s/.sterm.rc", "%s/.config/sterm.rc", "%s/.config/sterm/sterm.rc"};
871 for (size_t f = 0; f < sizeof(homecfg)/sizeof(homecfg[0]); ++f) {
872 configfile = SPrintf(homecfg[f], home);
873 if (loadConfig(configfile) == 0) goto cfgdone;
874 free(configfile);
875 configfile = NULL;
878 for (size_t f = 0; f < sizeof(systemcfg)/sizeof(systemcfg[0]); ++f) {
879 configfile = SPrintf(systemcfg[f]);
880 if (loadConfig(configfile) == 0) goto cfgdone;
881 free(configfile);
882 configfile = NULL;
884 // no config
885 } else {
886 if (loadConfig(configfile) < 0) k8t_die("config file '%s' not found!", configfile);
888 cfgdone:
889 if (configfile != NULL) free(configfile); configfile = NULL;
891 for (int f = 1; f < argc; ++f) {
892 if (strcmp(argv[f], "-name") == 0) { ++f; continue; }
893 if (strcmp(argv[f], "-into") == 0 || strcmp(argv[f], "-embed") == 0) {
894 if (opt_embed) free(opt_embed);
895 opt_embed = strdup(argv[f]);
896 continue;
898 switch (argv[f][0] != '-' || argv[f][2] ? -1 : argv[f][1]) {
899 case 't':
900 if (++f < argc) {
901 free(opt_title);
902 opt_title = strdup(argv[f]);
904 break;
905 case 'c':
906 if (++f < argc) {
907 free(opt_class);
908 opt_class = strdup(argv[f]);
910 break;
911 case 'w':
912 if (++f < argc) {
913 if (opt_embed) free(opt_embed);
914 opt_embed = strdup(argv[f]);
916 break;
917 case 'R':
918 if (++f < argc) runcmd = argv[f];
919 break;
920 case 'e':
921 /* eat all remaining arguments */
922 if (++f < argc) opt_cmd = &argv[f];
923 f = argc+1;
924 case 'T':
925 case 'C':
926 case 'l':
927 ++f;
928 break;
929 case 'S':
930 break;
934 setenv("TERM", opt_term, 1);
935 mclock_init();
936 setlocale(LC_ALL, "");
937 initLCConversion();
938 summonChildKiller();
939 updateTabBar = 1;
940 termidx = 0;
941 curterm = k8t_termalloc();
942 k8t_tmInitialize(curterm, 80, 25, opt_maxhistory);
943 // pixmap will be created in xinit()
944 if (K8T_DATA(curterm)->execcmd != NULL) { free(K8T_DATA(curterm)->execcmd); K8T_DATA(curterm)->execcmd = NULL; }
945 if (k8t_ttyNew(curterm) != 0) k8t_die("can't run process");
946 opt_cmd = NULL;
947 xinit();
948 k8t_selInit(curterm);
949 if (runcmd != NULL) executeCommands(runcmd);
950 run();
951 return 0;