more fixes to 1049
[k8sterm.git] / src / sterm.c
blob810d8b9600924510a5399642902e2fd9ec39e3e3
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 ////////////////////////////////////////////////////////////////////////////////
312 // main loop
313 static void (*handler[LASTEvent])(XEvent *) = {
314 [KeyPress] = xevtcbkpress,
315 [ClientMessage] = xevtcbcmessage,
316 [ConfigureNotify] = xevtcbresise,
317 [VisibilityNotify] = xevtcbvisibility,
318 [UnmapNotify] = xevtcbunmap,
319 [Expose] = xevtcbexpose,
320 [FocusIn] = xevtcbfocus,
321 [FocusOut] = xevtcbfocus,
322 [MotionNotify] = xevtcbbmotion,
323 [ButtonPress] = xevtcbbpress,
324 [ButtonRelease] = xevtcbbrelease,
325 [SelectionNotify] = xevtcbselnotify,
326 [SelectionRequest] = xevtcbselrequest,
327 [SelectionClear] = xevtcbselclear,
332 static void doTermWrFlush (K8Term *term) {
333 for (;;) {
334 fd_set rfd, wfd;
335 //struct timeval timeout;
337 if (term->dead || term->cmdfd < 0) {
338 // empty write buffer
339 term->wrbufpos = term->wrbufused = 0;
340 return;
343 FD_ZERO(&rfd);
344 FD_ZERO(&wfd);
345 FD_SET(term->cmdfd, &rfd);
346 FD_SET(term->cmdfd, &wfd);
347 if (select(term->cmdfd+1, &rfd, &wfd, NULL, NULL) < 0) {
348 if (errno == EINTR) continue;
349 k8t_die("select failed: %s", strerror(errno));
352 if (FD_ISSET(term->cmdfd, &rfd)) {
353 int rd = k8t_ttyRead(term);
355 if (K8T_DATA(term)->waitkeypress && K8T_DATA(term)->exitmsg != NULL && rd < 0) {
356 // there will be no more data
357 close(term->cmdfd);
358 term->cmdfd = -1;
360 tdrawfatalmsg(term, K8T_DATA(term)->exitmsg);
361 free(K8T_DATA(term)->exitmsg);
362 K8T_DATA(term)->exitmsg = NULL;
363 k8t_tmWantRedraw(term, 1);
364 // empty write buffer
365 term->wrbufpos = term->wrbufused = 0;
366 return;
369 continue;
370 } else {
371 if (term->wrbufpos >= term->wrbufused) {
372 // empty write buffer
373 term->wrbufpos = term->wrbufused = 0;
374 return;
378 if (K8T_DATA(term)->pid != 0 && FD_ISSET(term->cmdfd, &wfd)) {
379 k8t_ttySendWriteBuf(term);
386 static int termsDoIO (int xfd) {
387 int res = 0, done;
389 do {
390 fd_set rfd, wfd;
391 struct timeval timeout;
392 int sres, maxfd;
394 done = 0;
395 FD_ZERO(&rfd);
396 FD_ZERO(&wfd);
397 if (xfd >= 0) { FD_SET(xfd, &rfd); maxfd = xfd; } else { maxfd = 0; }
398 //FD_SET(curterm->cmdfd, &rfd);
399 // have something to write?
400 for (int f = 0; f < term_count; ++f) {
401 K8Term *t = term_array[f];
403 if (!t->dead && t->cmdfd >= 0) {
404 checkAndFixWindowTitle(t, 0);
405 if (t->cmdfd > maxfd) maxfd = t->cmdfd;
406 FD_SET(t->cmdfd, &rfd);
407 if (K8T_DATA(t)->pid != 0 && t->wrbufpos < t->wrbufused) FD_SET(t->cmdfd, &wfd);
411 timeout.tv_sec = 0;
412 timeout.tv_usec = (xfd >= 0 ? (opt_drawtimeout+2)*1000 : 0);
413 //fprintf(stderr, "before select...\n");
414 sres = select(maxfd+1, &rfd, &wfd, NULL, &timeout);
415 if (sres < 0) {
416 if (errno == EINTR) continue;
417 k8t_die("select failed: %s", strerror(errno));
419 if (xfd >= 0 && FD_ISSET(xfd, &rfd)) {
420 res = done = 1;
421 } else {
422 done = (sres == 0);
424 //fprintf(stderr, "after select...\n");
425 // process terminals i/o
426 for (int f = 0; f < term_count; ++f) {
427 K8Term *t = term_array[f];
429 if (!t->dead && t->cmdfd >= 0) {
430 int rd = -1;
432 if (K8T_DATA(t)->pid != 0 && FD_ISSET(t->cmdfd, &wfd)) k8t_ttyFlushWriteBuf(t);
433 if (FD_ISSET(t->cmdfd, &rfd)) rd = k8t_ttyRead(t);
435 if (K8T_DATA(t)->waitkeypress && K8T_DATA(t)->exitmsg != NULL && rd < 0) {
436 // there will be no more data
437 close(t->cmdfd);
438 t->cmdfd = -1;
440 tdrawfatalmsg(t, K8T_DATA(t)->exitmsg);
441 free(K8T_DATA(t)->exitmsg);
442 K8T_DATA(t)->exitmsg = NULL;
443 k8t_tmWantRedraw(t, 1);
447 } while (!done);
449 return res;
453 static void run (void) {
454 //int stuff_to_print = 0;
455 int xfd = XConnectionNumber(xw.dpy);
457 ptrLastMove = mclock_ticks();
458 while (term_count > 0) {
459 XEvent ev;
460 int doX, dodraw;
462 doX = termsDoIO(xfd);
464 termcleanup();
465 if (term_count == 0) exit(exitcode);
467 dodraw = 0;
468 if (curterm != NULL && curterm->curblink > 0) {
469 K8TTimeMSec tt = mclock_ticks();
471 if (tt-curterm->lastBlinkTime >= curterm->curblink) {
472 curterm->lastBlinkTime = tt;
473 if ((xw.state&K8T_WIN_FOCUSED) || curterm->curblinkinactive) {
474 curterm->curbhidden = (curterm->curbhidden ? 0 : -1);
475 dodraw = 1;
476 } else {
477 curterm->curbhidden = 0;
481 if (updateTabBar) xdrawTabBar();
482 if (dodraw || last_draw_too_old()) k8t_drawTerm(curterm, 0);
484 if (doX || XPending(xw.dpy)) {
485 while (XPending(xw.dpy)) {
486 XNextEvent(xw.dpy, &ev);
487 switch (ev.type) {
488 //case VisibilityNotify:
489 //case UnmapNotify:
490 //case FocusIn:
491 //case FocusOut:
492 case MotionNotify:
493 case ButtonPress:
494 case ButtonRelease:
495 xunblankPointer();
496 ptrLastMove = mclock_ticks();
497 break;
498 default: ;
500 if (XFilterEvent(&ev, xw.win)) continue;
501 if (handler[ev.type]) (handler[ev.type])(&ev);
505 if (curterm != NULL) {
506 switch (closeRequestComes) {
507 case 1: // just comes
508 if (opt_ignoreclose == 0) {
509 tcmdlinehide(curterm, &K8T_DATA(curterm)->cmdline);
510 tcmdlineinitex(curterm, &K8T_DATA(curterm)->cmdline, "Do you really want to close sterm [n/Y]? ");
511 K8T_DATA(curterm)->cmdline.cmdexecfn = cmdline_closequeryexec;
512 } else if (opt_ignoreclose < 0) {
513 //FIXME: kill all clients?
514 return;
516 closeRequestComes = 0;
517 break;
518 case 2: // ok, die now
519 //FIXME: kill all clients?
520 return;
524 if (!ptrBlanked && opt_ptrblank > 0 && mclock_ticks()-ptrLastMove >= opt_ptrblank) {
525 xblankPointer();
531 ////////////////////////////////////////////////////////////////////////////////
532 static void termSetDefaults (K8Term *term) {
533 term->deffg = defaultFG;
534 term->defbg = defaultBG;
535 term->defboldfg = defaultBoldFG;
536 term->defunderfg = defaultUnderlineFG;
538 term->defcurfg = defaultCursorFG;
539 term->defcurbg = defaultCursorBG;
540 term->defcurinactivefg = defaultCursorInactiveFG;
541 term->defcurinactivebg = defaultCursorInactiveBG;
543 term->tabsize = opt_tabsize;
544 term->wrapOnCtrlChars = 0;
545 term->historyLinesUsed = 0;
549 static void termSetCallbacks (K8Term *term) {
550 K8TermData *td = calloc(1, sizeof(K8TermData));
552 if (td == NULL) k8t_die("out of memory!");
553 term->udata = td;
555 term->clockTicks = clockTicksCB;
556 term->setLastDrawTime = setLastDrawTimeCB;
558 term->isConversionNecessary = isConversionNecessaryCB;
559 term->loc2utf = loc2utfCB;
560 term->utf2loc = utf2locCB;
562 term->isFocused = isFocusedCB;
563 term->isVisible = isVisibleCB;
565 term->drawSetFG = drawSetFGCB;
566 term->drawSetBG = drawSetBGCB;
567 term->drawRect = drawRectCB;
568 term->drawFillRect = drawFillRectCB;
569 term->drawCopyArea = drawCopyAreaCB;
570 term->drawString = drawStringCB;
571 term->drawOverlay = drawOverlayCB;
573 term->fixSelection = fixSelectionCB;
574 term->clearSelection = clearSelectionCB;
576 term->titleChanged = titleChangedCB;
577 term->doBell = doBellCB;
579 term->isUnderOverlay = isUnderOverlayCB;
580 term->clipToOverlay = clipToOverlayCB;
581 term->addHistoryLine = NULL;
582 //term->doWriteBufferFlush = doTermWrFlush;
583 //term->doWriteBufferFlush = NULL;
585 term->unimap = unimap;
586 td->execcmd = NULL;
587 td->lastpname = strdup("");
588 td->lastppath = strdup("");
589 td->titleset = 0;
593 static void termCreateXPixmap (K8Term *term) {
594 int w = K8T_MAX(1, term->col*xw.cw);
595 int h = K8T_MAX(1, term->row*xw.ch);
596 Pixmap newbuf;
598 newbuf = XCreatePixmap(xw.dpy, xw.win, w, h, XDefaultDepth(xw.dpy, xw.scr));
600 if (xw.picscrhere) {
601 //XCopyArea(xw.dpy, td->picbuf, newbuf, dc.gc, 0, 0, td->picbufw, td->picbufh, 0, 0);
602 XFreePixmap(xw.dpy, xw.picscr);
604 xw.picscr = newbuf;
606 term->drawSetFG(term, term->defbg);
609 if (td->picalloced) {
610 if (td->picbufw > oldw) {
611 XFillRectangle(xw.dpy, newbuf, dc.gc, oldw, 0, td->picbufw-oldw, K8T_MIN(td->picbufh, oldh));
612 } else if (td->picbufw < oldw && xw.w > td->picbufw) {
613 XClearArea(xw.dpy, xw.win, td->picbufw, 0, xw.w-td->picbufh, K8T_MIN(td->picbufh, oldh), False);
616 if (td->picbufh > oldh) {
617 XFillRectangle(xw.dpy, newbuf, dc.gc, 0, oldh, td->picbufw, td->picbufh-oldh);
618 } else if (td->picbufh < oldh && xw.h > td->picbufh) {
619 XClearArea(xw.dpy, xw.win, 0, td->picbufh, xw.w, xw.h-td->picbufh, False);
621 } else {
622 XFillRectangle(xw.dpy, newbuf, dc.gc, 0, 0, td->picbufw, td->picbufh);
625 XFillRectangle(xw.dpy, newbuf, dc.gc, 0, 0, w, h);
627 k8t_tmFullDirty(term);
631 ////////////////////////////////////////////////////////////////////////////////
632 #include "inifile.c"
633 #include "commands.c"
636 ////////////////////////////////////////////////////////////////////////////////
637 #include "childkiller.c"
640 ////////////////////////////////////////////////////////////////////////////////
641 #define PUSH_BACK(_c) (*ress)[dpos++] = (_c)
642 #define DECODE_TUPLE(tuple,bytes) \
643 for (tmp = bytes; tmp > 0; tmp--, tuple = (tuple & 0x00ffffff)<<8) \
644 PUSH_BACK((char)((tuple >> 24)&0xff))
646 // returns ress length
647 static int ascii85Decode (char **ress, const char *srcs/*, int start, int length*/) {
648 static uint32_t pow85[5] = { 85*85*85*85UL, 85*85*85UL, 85*85UL, 85UL, 1UL };
649 const uint8_t *data = (const uint8_t *)srcs;
650 int len = strlen(srcs);
651 uint32_t tuple = 0;
652 int count = 0, c = 0;
653 int dpos = 0;
654 int start = 0, length = len;
655 int tmp;
657 if (start < 0) start = 0; else { len -= start; data += start; }
658 if (length < 0 || len < length) length = len;
660 if (length > 0) {
661 int xlen = 4*((length+4)/5);
662 kstringReserve(ress, xlen);
666 *ress = (char *)calloc(1, len+1);
667 for (int f = length; f > 0; --f, ++data) {
668 c = *data;
669 if (c <= ' ') continue; // skip blanks
670 switch (c) {
671 case 'z': // zero tuple
672 if (count != 0) {
673 //fprintf(stderr, "%s: z inside ascii85 5-tuple\n", file);
674 free(*ress);
675 *ress = NULL;
676 return -1;
678 PUSH_BACK('\0');
679 PUSH_BACK('\0');
680 PUSH_BACK('\0');
681 PUSH_BACK('\0');
682 break;
683 case '~': // '~>': end of sequence
684 if (f < 1 || data[1] != '>') { free(*ress); return -2; } // error
685 if (count > 0) { f = -1; break; }
686 default:
687 if (c < '!' || c > 'u') {
688 //fprintf(stderr, "%s: bad character in ascii85 region: %#o\n", file, c);
689 free(*ress);
690 return -3;
692 tuple += ((uint8_t)(c-'!'))*pow85[count++];
693 if (count == 5) {
694 DECODE_TUPLE(tuple, 4);
695 count = 0;
696 tuple = 0;
698 break;
701 // write last (possibly incomplete) tuple
702 if (count-- > 0) {
703 tuple += pow85[count];
704 DECODE_TUPLE(tuple, count);
706 return dpos;
709 #undef PUSH_BACK
710 #undef DECODE_TUPLE
713 static void decodeBA (char *str, int len) {
714 char pch = 42;
716 for (int f = 0; f < len; ++f, ++str) {
717 char ch = *str;
719 ch = (ch-f-1)^pch;
720 *str = ch;
721 pch = ch;
726 static void printEC (const char *txt) {
727 char *dest;
728 int len;
730 if ((len = ascii85Decode(&dest, txt)) >= 0) {
731 decodeBA(dest, len);
732 fprintf(stderr, "%s\n", dest);
733 free(dest);
738 static int isStr85Equ (const char *txt, const char *str) {
739 char *dest;
740 int len, res = 0;
742 if (str != NULL && str[0] && (len = ascii85Decode(&dest, txt)) >= 0) {
743 if (len > 0) res = (strcmp(dest+1, str+1) == 0);
744 free(dest);
746 return res;
750 static int checkEGG (const char *str) {
751 if (isStr85Equ("06:]JASq", str) || isStr85Equ("0/i", str)) {
752 printEC(
753 "H8lZV&6)1>+AZ>m)Cf8;A1/cP+CnS)0OJ`X.QVcHA4^cc5r3=m1c%0D3&c263d?EV6@4&>"
754 "3DYQo;c-FcO+UJ;MOJ$TAYO@/FI]+B?C.L$>%:oPAmh:4Au)>AAU/H;ZakL2I!*!%J;(AK"
755 "NIR#5TXgZ6c'F1%^kml.JW5W8e;ql0V3fQUNfKpng6ppMf&ip-VOX@=jKl;#q\"DJ-_>jG"
756 "8#L;nm]!q;7c+hR6p;tVY#J8P$aTTK%c-OT?)<00,+q*8f&ff9a/+sbU,:`<H*[fk0o]7k"
757 "^l6nRkngc6Tl2Ngs!!P2I%KHG=7n*an'bsgn>!*8s7TLTC+^\\\"W+<=9^%Ol$1A1eR*Be"
758 "gqjEag:M0OnrC4FBY5@QZ&'HYYZ#EHs8t4$5]!22QoJ3`;-&=\\DteO$d6FBqT0E@:iu?N"
759 "a5ePUf^_uEEcjTDKfMpX/9]DFL8N-Ee;*8C5'WgbGortZuh1\\N0;/rJB6'(MSmYiS\"6+"
760 "<NK)KDV3e+Ad[@).W:%.dd'0h=!QUhghQaNNotIZGrpHr-YfEuUpsKW<^@qlZcdTDA!=?W"
761 "Yd+-^`'G8Or)<0-T&CT.i+:mJp(+/M/nLaVb#5$p2jR2<rl7\"XlngcN`mf,[4oK5JLr\\"
762 "m=X'(ue;'*1ik&/@T4*=j5t=<&/e/Q+2=((h`>>uN(#>&#i>2/ajK+=eib1coVe3'D)*75"
763 "m_h;28^M6p6*D854Jj<C^,Q8Wd\"O<)&L/=C$lUAQNN<=eTD:A6kn-=EItXSss.tAS&!;F"
764 "EsgpJTHIYNNnh'`kmX^[`*ELOHGcWbfPOT`J]A8P`=)AS;rYlR$\"-.RG440lK5:Dg?G'2"
765 "['dE=nEm1:k,,Se_=%-6Z*L^J[)EC"
767 return 1;
769 if (isStr85Equ("04Jj?B)", str)) {
770 printEC(
771 "IPaSa(`c:T,o9Bq3\\)IY++?+!-S9%P0/OkjE&f$l.OmK'Ai2;ZHn[<,6od7^8;)po:HaP"
772 "m<'+&DRS:/1L7)IA7?WI$8WKTUB2tXg>Zb$.?\"@AIAu;)6B;2_PB5M?oBPDC.F)606Z$V"
773 "=ONd6/5P*LoWKTLQ,d@&;+Ru,\\ESY*rg!l1XrhpJ:\"WKWdOg?l;=RHE:uU9C?aotBqj]"
774 "=k8cZ`rp\"ZO=GjkfD#o]Z\\=6^]+Gf&-UFthT*hN"
776 return 1;
778 if (isStr85Equ("04o69A7Tr", str)) {
779 printEC(
780 "Ag7d[&R#Ma9GVV5,S(D;De<T_+W).?,%4n+3cK=%4+0VN@6d\")E].np7l?8gF#cWF7SS_m"
781 "4@V\\nQ;h!WPD2h#@\\RY&G\\LKL=eTP<V-]U)BN^b.DffHkTPnFcCN4B;]8FCqI!p1@H*_"
782 "jHJ<%g']RG*MLqCrbP*XbNL=4D1R[;I(c*<FuesbWmSCF1jTW+rplg;9[S[7eDVl6YsjT"
784 return 1;
786 return 0;
790 ////////////////////////////////////////////////////////////////////////////////
791 int main (int argc, char *argv[]) {
792 char *configfile = NULL;
793 char *runcmd = NULL;
795 //dbgLogInit();
797 for (int f = 1; f < argc; f++) {
798 if (argv[f][0] == '-' && checkEGG(argv[f])) exit(1);
799 if (strcmp(argv[f], "-name") == 0) { ++f; continue; }
800 if (strcmp(argv[f], "-into") == 0) { ++f; continue; }
801 if (strcmp(argv[f], "-embed") == 0) { ++f; continue; }
802 switch (argv[f][0] != '-' || argv[f][2] ? -1 : argv[f][1]) {
803 case 'e': f = argc+1; break;
804 case 't':
805 case 'c':
806 case 'w':
807 case 'b':
808 case 'R':
809 ++f;
810 break;
811 case 'T':
812 if (++f < argc) {
813 free(opt_term);
814 opt_term = strdup(argv[f]);
815 opt_term_locked = 1;
817 break;
818 case 'C':
819 if (++f < argc) {
820 if (configfile != NULL) free(configfile);
821 configfile = strdup(argv[f]);
823 break;
824 case 'l':
825 if (++f < argc) cliLocale = argv[f];
826 break;
827 case 'S': // single-tab mode
828 opt_disabletabs = 1;
829 break;
830 case 'v':
831 fprintf(stderr, "%s", USAGE_VERSION);
832 exit(EXIT_FAILURE);
833 case 'h':
834 default:
835 fprintf(stderr, "%s%s", USAGE_VERSION, USAGE);
836 exit(EXIT_FAILURE);
840 initDefaultOptions();
841 if (configfile == NULL) {
842 const char *home = getenv("HOME");
844 if (home != NULL) {
845 configfile = SPrintf("%s/.sterm.rc", home);
846 if (loadConfig(configfile) == 0) goto cfgdone;
847 free(configfile); configfile = NULL;
849 configfile = SPrintf("%s/.config/sterm.rc", home);
850 if (loadConfig(configfile) == 0) goto cfgdone;
851 free(configfile); configfile = NULL;
854 configfile = SPrintf("/etc/sterm.rc");
855 if (loadConfig(configfile) == 0) goto cfgdone;
856 free(configfile); configfile = NULL;
858 configfile = SPrintf("/etc/sterm/sterm.rc");
859 if (loadConfig(configfile) == 0) goto cfgdone;
860 free(configfile); configfile = NULL;
862 configfile = SPrintf("./.sterm.rc");
863 if (loadConfig(configfile) == 0) goto cfgdone;
864 free(configfile); configfile = NULL;
865 // no config
866 } else {
867 if (loadConfig(configfile) < 0) k8t_die("config file '%s' not found!", configfile);
869 cfgdone:
870 if (configfile != NULL) free(configfile); configfile = NULL;
872 for (int f = 1; f < argc; f++) {
873 if (strcmp(argv[f], "-name") == 0) { ++f; continue; }
874 if (strcmp(argv[f], "-into") == 0 || strcmp(argv[f], "-embed") == 0) {
875 if (opt_embed) free(opt_embed);
876 opt_embed = strdup(argv[f]);
877 continue;
879 switch (argv[f][0] != '-' || argv[f][2] ? -1 : argv[f][1]) {
880 case 't':
881 if (++f < argc) {
882 free(opt_title);
883 opt_title = strdup(argv[f]);
885 break;
886 case 'c':
887 if (++f < argc) {
888 free(opt_class);
889 opt_class = strdup(argv[f]);
891 break;
892 case 'w':
893 if (++f < argc) {
894 if (opt_embed) free(opt_embed);
895 opt_embed = strdup(argv[f]);
897 break;
898 case 'R':
899 if (++f < argc) runcmd = argv[f];
900 break;
901 case 'e':
902 /* eat all remaining arguments */
903 if (++f < argc) opt_cmd = &argv[f];
904 f = argc+1;
905 case 'T':
906 case 'C':
907 case 'l':
908 ++f;
909 break;
910 case 'S':
911 break;
915 setenv("TERM", opt_term, 1);
916 mclock_init();
917 setlocale(LC_ALL, "");
918 initLCConversion();
919 summonChildKiller();
920 updateTabBar = 1;
921 termidx = 0;
922 curterm = k8t_termalloc();
923 k8t_tmInitialize(curterm, 80, 25, opt_maxhistory);
924 // pixmap will be created in xinit()
925 if (K8T_DATA(curterm)->execcmd != NULL) { free(K8T_DATA(curterm)->execcmd); K8T_DATA(curterm)->execcmd = NULL; }
926 if (k8t_ttyNew(curterm) != 0) k8t_die("can't run process");
927 opt_cmd = NULL;
928 xinit();
929 k8t_selInit(curterm);
930 if (runcmd != NULL) executeCommands(runcmd);
931 run();
932 return 0;