clear selections in all tabs when selection ownership is lost
[k8sterm.git] / src / sterm.c
blobcbea85efc72c203e8b993ca741705da77a28967f
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 #include <alloca.h>
14 #include <ctype.h>
15 #include <errno.h>
16 #include <fcntl.h>
17 #include <iconv.h>
18 #include <limits.h>
19 #include <locale.h>
21 #ifdef __MACH__
22 #include <util.h>
23 #else
24 #include <pty.h>
25 #endif
27 #include <stdarg.h>
28 #include <stdint.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <strings.h>
33 #include <signal.h>
34 #include <sys/ioctl.h>
35 #include <sys/select.h>
36 #include <sys/stat.h>
37 #include <sys/time.h>
38 #include <sys/types.h>
39 #include <sys/wait.h>
40 #include <time.h>
41 #include <unistd.h>
42 #include <X11/Xatom.h>
43 #include <X11/Xlib.h>
44 #include <X11/Xutil.h>
45 #include <X11/cursorfont.h>
46 #include <X11/keysym.h>
48 ////////////////////////////////////////////////////////////////////////////////
49 #include "libk8sterm/k8sterm.h"
52 ////////////////////////////////////////////////////////////////////////////////
53 #include "defaults.c"
56 ////////////////////////////////////////////////////////////////////////////////
57 #define USAGE_VERSION "k8sterm " VERSION "\n(c) 2010-2012 st engineers and Ketmar // Vampire Avalon\n"
58 #define USAGE \
59 "usage: sterm [options]\n" \
60 "options:\n" \
61 "-v show version and exit\n" \
62 "-h show this help and exit\n" \
63 "-S disable tabs\n" \
64 "-t title set window title\n" \
65 "-c class set window class\n" \
66 "-w windowid embed in window id given id\n" \
67 "-T termname set TERM name\n" \
68 "-C config_file use custom config file\n" \
69 "-l langiconv use given locale\n" \
70 "-R stcmd run this INTERNAL k8sterm command\n" \
71 "-e command... run given command and pass all following args to it\n"
74 ////////////////////////////////////////////////////////////////////////////////
75 /* masks for key translation */
76 #define XK_NO_MOD (UINT_MAX)
77 #define XK_ANY_MOD (0)
80 ////////////////////////////////////////////////////////////////////////////////
81 // internal command line mode
82 enum {
83 K8T_CMDMODE_NONE,
84 K8T_CMDMODE_INPUT,
85 K8T_CMDMODE_MESSAGE
89 // X11 window state flags
90 enum {
91 K8T_WIN_VISIBLE = 0x01,
92 K8T_WIN_REDRAW = 0x02,
93 K8T_WIN_FOCUSED = 0x04
97 ////////////////////////////////////////////////////////////////////////////////
98 /* Purely graphic info */
99 typedef struct {
100 Display *dpy;
101 Colormap cmap;
102 Window win;
103 Cursor cursor; // text cursor
104 Cursor defcursor; // 'default' cursor
105 Cursor lastcursor; // last used cursor before blanking
106 Cursor blankPtr;
107 Atom xembed;
108 XIM xim;
109 XIC xic;
110 int scr;
111 int w; /* window width */
112 int h; /* window height */
113 int bufw; /* pixmap width */
114 int bufh; /* pixmap height */
115 int ch; /* char height */
116 int cw; /* char width */
117 char state; /* focus, redraw, visible */
119 int tch; /* tab text char height */
120 Pixmap pictab;
121 int picscrhere;
122 Pixmap picscr;
123 int tabheight;
124 //struct timeval lastdraw;
125 } K8TXWindow;
128 /* Drawing Context */
129 typedef struct {
130 uint32_t *ncol; // normal colors
131 uint32_t *bcol; // b/w colors
132 uint32_t *gcol; // green colors
133 uint32_t *clrs[3];
134 GC gc;
135 struct {
136 int ascent;
137 int descent;
138 short lbearing;
139 short rbearing;
140 XFontSet set;
141 Font fid;
142 } font[3];
143 } K8TXDC;
146 typedef struct K8TCmdLine K8TCmdLine;
148 typedef void (*CmdLineExecFn) (K8Term *term, K8TCmdLine *cmdline, int cancelled);
150 struct K8TCmdLine {
151 int cmdMode; // K8T_CMDMODE_xxx
152 char cmdline[K8T_UTF_SIZ*CMDLINE_SIZE];
153 int cmdreslen; // byte length of 'reserved' (read-only) part
154 int cmdofs; // byte offset of the first visible UTF-8 char in cmdline
155 char cmdc[K8T_UTF_SIZ+1]; // buffer to collect UTF-8 char
156 int cmdcl; // index in cmdc, used to collect UTF-8 chars
157 int cmdtabpos; // # of bytes (not UTF-8 chars!) used in autocompletion or -1
158 const char *cmdcurtabc; // current autocompleted command
159 CmdLineExecFn cmdexecfn;
160 void *udata;
164 /* internal terminal data */
165 typedef struct {
166 pid_t pid;
167 int exitcode;
169 int waitkeypress; /* child is dead, awaiting for keypress */
170 char *exitmsg; /* message for waitkeypress */
172 char *execcmd;
174 K8TCmdLine cmdline;
175 } K8TermData;
177 #define K8T_DATA(_term) ((K8TermData *)(_term->udata))
180 ////////////////////////////////////////////////////////////////////////////////
181 #include "globals.c"
182 #include "getticks.c"
185 ////////////////////////////////////////////////////////////////////////////////
186 #include "utils.c"
187 #include "keymaps.c"
190 ////////////////////////////////////////////////////////////////////////////////
191 static void executeCommands (const char *str);
192 static const char *findCommandCompletion (const char *str, int slen, const char *prev);
194 static void tdrawfatalmsg (K8Term *term, const char *msg);
196 static void tcmdput (K8Term *term, K8TCmdLine *cmdline, const char *s, int len);
198 static void xclearunused (void);
199 static void xseturgency (int add);
200 static void xfixsel (void);
201 static void xblankPointer (void);
202 static void xunblankPointer (void);
203 static void xdrawTabBar (void);
204 static void xdrawcmdline (K8Term *term, K8TCmdLine *cmdline, int scry);
206 static void termCreateXPixmap (K8Term *term);
207 static void termSetCallbacks (K8Term *term);
208 static void termSetDefaults (K8Term *term);
210 static void getButtonInfo (K8Term *term, XEvent *e, int *b, int *x, int *y);
212 static void fixWindowTitle (const K8Term *t);
214 static void scrollHistory (K8Term *term, int delta);
217 ////////////////////////////////////////////////////////////////////////////////
218 static inline uint32_t getColor (int idx) {
219 if (globalBW && (curterm == NULL || !curterm->blackandwhite)) return dc.clrs[globalBW][idx];
220 if (curterm != NULL) return dc.clrs[curterm->blackandwhite%3][idx];
221 return dc.clrs[0][idx];
225 ////////////////////////////////////////////////////////////////////////////////
226 #include "iniparse.c"
227 #include "locales.c"
230 ////////////////////////////////////////////////////////////////////////////////
231 #include "termswitch.c"
232 #include "ttyinit.c"
235 ////////////////////////////////////////////////////////////////////////////////
236 #include "tcmdline.c"
237 #include "tfatalbox.c"
240 ////////////////////////////////////////////////////////////////////////////////
241 #include "x11init.c"
242 #include "x11misc.c"
243 #include "x11drawtabs.c"
244 #include "x11drawcmdline.c"
247 ////////////////////////////////////////////////////////////////////////////////
248 #include "x11evtvis.c"
249 #include "x11evtkbd.c"
250 #include "x11evtsel.c"
251 #include "x11evtmouse.c"
254 ////////////////////////////////////////////////////////////////////////////////
255 #include "x11CB.c"
258 ////////////////////////////////////////////////////////////////////////////////
259 // xembed?
260 static void xevtcbcmessage (XEvent *e) {
261 /* See xembed specs http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html */
262 if (e->xclient.message_type == xw.xembed && e->xclient.format == 32) {
263 if (e->xclient.data.l[1] == XEMBED_FOCUS_IN) {
264 xw.state |= K8T_WIN_FOCUSED;
265 xseturgency(0);
266 k8t_tmSendFocusEvent(curterm, 1);
267 } else if (e->xclient.data.l[1] == XEMBED_FOCUS_OUT) {
268 xw.state &= ~K8T_WIN_FOCUSED;
269 k8t_tmSendFocusEvent(curterm, 0);
271 k8t_drawCursor(curterm, 0);
272 xdrawTabBar();
273 k8t_drawCopy(curterm, 0, 0, curterm->col, curterm->row);
274 return;
277 if (e->xclient.data.l[0] == XA_WM_DELETE_WINDOW) {
278 closeRequestComes = 1;
279 return;
284 ////////////////////////////////////////////////////////////////////////////////
285 static inline int last_draw_too_old (void) {
286 K8TTimeMSec tt = mclock_ticks();
288 if (curterm != NULL) {
289 if (curterm->dead || !curterm->wantRedraw) return 0;
291 return
292 (tt-curterm->lastDrawTime >= opt_drawtimeout ||
293 tt-lastDrawTime >= (curterm->justSwapped ? opt_swapdrawtimeout : opt_maxdrawtimeout));
295 return (tt-lastDrawTime >= opt_maxdrawtimeout);
299 ////////////////////////////////////////////////////////////////////////////////
300 // main loop
301 static void (*handler[LASTEvent])(XEvent *) = {
302 [KeyPress] = xevtcbkpress,
303 [ClientMessage] = xevtcbcmessage,
304 [ConfigureNotify] = xevtcbresise,
305 [VisibilityNotify] = xevtcbvisibility,
306 [UnmapNotify] = xevtcbunmap,
307 [Expose] = xevtcbexpose,
308 [FocusIn] = xevtcbfocus,
309 [FocusOut] = xevtcbfocus,
310 [MotionNotify] = xevtcbbmotion,
311 [ButtonPress] = xevtcbbpress,
312 [ButtonRelease] = xevtcbbrelease,
313 [SelectionNotify] = xevtcbselnotify,
314 [SelectionRequest] = xevtcbselrequest,
315 [SelectionClear] = xevtcbselclear,
319 static void run (void) {
320 //int stuff_to_print = 0;
321 int xfd = XConnectionNumber(xw.dpy);
323 ptrLastMove = mclock_ticks();
324 while (term_count > 0) {
325 XEvent ev;
326 fd_set rfd, wfd;
327 struct timeval timeout;
328 int maxfd = xfd;
329 int dodraw;
331 FD_ZERO(&rfd);
332 FD_ZERO(&wfd);
333 FD_SET(xfd, &rfd);
334 //FD_SET(curterm->cmdfd, &rfd);
335 // have something to write?
336 for (int f = 0; f < term_count; ++f) {
337 K8Term *t = term_array[f];
339 if (!t->dead && t->cmdfd >= 0) {
340 if (t->cmdfd > maxfd) maxfd = t->cmdfd;
341 FD_SET(t->cmdfd, &rfd);
342 if (K8T_DATA(t)->pid != 0 && t->wrbufpos < t->wrbufused) FD_SET(t->cmdfd, &wfd);
346 timeout.tv_sec = 0;
347 timeout.tv_usec = (opt_drawtimeout+2)*1000;
348 //fprintf(stderr, "before select...\n");
349 if (select(maxfd+1, &rfd, &wfd, NULL, &timeout) < 0) {
350 if (errno == EINTR) continue;
351 k8t_die("select failed: %s", strerror(errno));
353 //fprintf(stderr, "after select...\n");
354 // process terminals i/o
355 for (int f = 0; f < term_count; ++f) {
356 K8Term *t = term_array[f];
358 if (!t->dead && t->cmdfd >= 0) {
359 int rd = -1;
361 if (K8T_DATA(t)->pid != 0 && FD_ISSET(t->cmdfd, &wfd)) k8t_ttyFlushWriteBuf(t);
362 if (FD_ISSET(t->cmdfd, &rfd)) rd = k8t_ttyRead(t);
364 if (K8T_DATA(t)->waitkeypress && K8T_DATA(t)->exitmsg != NULL && rd < 0) {
365 // there will be no more data
366 close(t->cmdfd);
367 t->cmdfd = -1;
369 tdrawfatalmsg(t, K8T_DATA(t)->exitmsg);
370 free(K8T_DATA(t)->exitmsg);
371 K8T_DATA(t)->exitmsg = NULL;
372 k8t_tmWantRedraw(t, 1);
377 termcleanup();
378 if (term_count == 0) exit(exitcode);
380 dodraw = 0;
381 if (curterm != NULL && curterm->curblink > 0) {
382 K8TTimeMSec tt = mclock_ticks();
384 if (tt-curterm->lastBlinkTime >= curterm->curblink) {
385 curterm->lastBlinkTime = tt;
386 if ((xw.state&K8T_WIN_FOCUSED) || curterm->curblinkinactive) {
387 curterm->curbhidden = (curterm->curbhidden ? 0 : -1);
388 dodraw = 1;
389 } else {
390 curterm->curbhidden = 0;
394 if (updateTabBar) xdrawTabBar();
395 if (dodraw || last_draw_too_old()) k8t_drawTerm(curterm, 0);
397 if (XPending(xw.dpy)) {
398 while (XPending(xw.dpy)) {
399 XNextEvent(xw.dpy, &ev);
400 switch (ev.type) {
401 //case VisibilityNotify:
402 //case UnmapNotify:
403 //case FocusIn:
404 //case FocusOut:
405 case MotionNotify:
406 case ButtonPress:
407 case ButtonRelease:
408 xunblankPointer();
409 ptrLastMove = mclock_ticks();
410 break;
411 default: ;
413 if (XFilterEvent(&ev, xw.win)) continue;
414 if (handler[ev.type]) (handler[ev.type])(&ev);
418 if (curterm != NULL) {
419 switch (closeRequestComes) {
420 case 1: // just comes
421 if (opt_ignoreclose == 0) {
422 tcmdlinehide(curterm, &K8T_DATA(curterm)->cmdline);
423 tcmdlineinitex(curterm, &K8T_DATA(curterm)->cmdline, "Do you really want to close sterm [Y/n]? ");
424 K8T_DATA(curterm)->cmdline.cmdexecfn = cmdline_closequeryexec;
425 } else if (opt_ignoreclose < 0) {
426 //FIXME: kill all clients?
427 return;
429 closeRequestComes = 0;
430 break;
431 case 2: // ok, die now
432 //FIXME: kill all clients?
433 return;
437 if (!ptrBlanked && opt_ptrblank > 0 && mclock_ticks()-ptrLastMove >= opt_ptrblank) {
438 xblankPointer();
444 ////////////////////////////////////////////////////////////////////////////////
445 static void termSetDefaults (K8Term *term) {
446 term->deffg = defaultFG;
447 term->defbg = defaultBG;
448 term->defboldfg = defaultBoldFG;
449 term->defunderfg = defaultUnderlineFG;
451 term->defcurfg = defaultCursorFG;
452 term->defcurbg = defaultCursorBG;
453 term->defcurinactivefg = defaultCursorInactiveFG;
454 term->defcurinactivebg = defaultCursorInactiveBG;
456 term->tabsize = opt_tabsize;
457 term->wrapOnCtrlChars = 0;
458 term->historyLinesUsed = 0;
462 static void termSetCallbacks (K8Term *term) {
463 K8TermData *td = calloc(1, sizeof(K8TermData));
465 if (td == NULL) k8t_die("out of memory!");
466 term->udata = td;
468 term->clockTicks = clockTicksCB;
469 term->setLastDrawTime = setLastDrawTimeCB;
471 term->isConversionNecessary = isConversionNecessaryCB;
472 term->loc2utf = loc2utfCB;
473 term->utf2loc = utf2locCB;
475 term->isFocused = isFocusedCB;
476 term->isVisible = isVisibleCB;
478 term->drawSetFG = drawSetFGCB;
479 term->drawSetBG = drawSetBGCB;
480 term->drawRect = drawRectCB;
481 term->drawFillRect = drawFillRectCB;
482 term->drawCopyArea = drawCopyAreaCB;
483 term->drawString = drawStringCB;
484 term->drawOverlay = drawOverlayCB;
486 term->fixSelection = fixSelectionCB;
487 term->clearSelection = clearSelectionCB;
489 term->titleChanged = titleChangedCB;
490 term->doBell = doBellCB;
492 term->isUnderOverlay = isUnderOverlayCB;
493 term->clipToOverlay = clipToOverlayCB;
494 term->addHistoryLine = NULL;
496 term->unimap = unimap;
497 td->execcmd = NULL;
501 static void termCreateXPixmap (K8Term *term) {
502 int w = K8T_MAX(1, term->col*xw.cw);
503 int h = K8T_MAX(1, term->row*xw.ch);
504 Pixmap newbuf;
506 newbuf = XCreatePixmap(xw.dpy, xw.win, w, h, XDefaultDepth(xw.dpy, xw.scr));
508 if (xw.picscrhere) {
509 //XCopyArea(xw.dpy, td->picbuf, newbuf, dc.gc, 0, 0, td->picbufw, td->picbufh, 0, 0);
510 XFreePixmap(xw.dpy, xw.picscr);
512 xw.picscr = newbuf;
514 term->drawSetFG(term, term->defbg);
517 if (td->picalloced) {
518 if (td->picbufw > oldw) {
519 XFillRectangle(xw.dpy, newbuf, dc.gc, oldw, 0, td->picbufw-oldw, K8T_MIN(td->picbufh, oldh));
520 } else if (td->picbufw < oldw && xw.w > td->picbufw) {
521 XClearArea(xw.dpy, xw.win, td->picbufw, 0, xw.w-td->picbufh, K8T_MIN(td->picbufh, oldh), False);
524 if (td->picbufh > oldh) {
525 XFillRectangle(xw.dpy, newbuf, dc.gc, 0, oldh, td->picbufw, td->picbufh-oldh);
526 } else if (td->picbufh < oldh && xw.h > td->picbufh) {
527 XClearArea(xw.dpy, xw.win, 0, td->picbufh, xw.w, xw.h-td->picbufh, False);
529 } else {
530 XFillRectangle(xw.dpy, newbuf, dc.gc, 0, 0, td->picbufw, td->picbufh);
533 XFillRectangle(xw.dpy, newbuf, dc.gc, 0, 0, w, h);
535 k8t_tmFullDirty(term);
539 ////////////////////////////////////////////////////////////////////////////////
540 #include "inifile.c"
541 #include "commands.c"
544 ////////////////////////////////////////////////////////////////////////////////
545 #include "childkiller.c"
548 ////////////////////////////////////////////////////////////////////////////////
549 #define PUSH_BACK(_c) (*ress)[dpos++] = (_c)
550 #define DECODE_TUPLE(tuple,bytes) \
551 for (tmp = bytes; tmp > 0; tmp--, tuple = (tuple & 0x00ffffff)<<8) \
552 PUSH_BACK((char)((tuple >> 24)&0xff))
554 // returns ress length
555 static int ascii85Decode (char **ress, const char *srcs/*, int start, int length*/) {
556 static uint32_t pow85[5] = { 85*85*85*85UL, 85*85*85UL, 85*85UL, 85UL, 1UL };
557 const uint8_t *data = (const uint8_t *)srcs;
558 int len = strlen(srcs);
559 uint32_t tuple = 0;
560 int count = 0, c = 0;
561 int dpos = 0;
562 int start = 0, length = len;
563 int tmp;
565 if (start < 0) start = 0; else { len -= start; data += start; }
566 if (length < 0 || len < length) length = len;
568 if (length > 0) {
569 int xlen = 4*((length+4)/5);
570 kstringReserve(ress, xlen);
574 *ress = (char *)calloc(1, len+1);
575 for (int f = length; f > 0; --f, ++data) {
576 c = *data;
577 if (c <= ' ') continue; // skip blanks
578 switch (c) {
579 case 'z': // zero tuple
580 if (count != 0) {
581 //fprintf(stderr, "%s: z inside ascii85 5-tuple\n", file);
582 free(*ress);
583 *ress = NULL;
584 return -1;
586 PUSH_BACK('\0');
587 PUSH_BACK('\0');
588 PUSH_BACK('\0');
589 PUSH_BACK('\0');
590 break;
591 case '~': // '~>': end of sequence
592 if (f < 1 || data[1] != '>') { free(*ress); return -2; } // error
593 if (count > 0) { f = -1; break; }
594 default:
595 if (c < '!' || c > 'u') {
596 //fprintf(stderr, "%s: bad character in ascii85 region: %#o\n", file, c);
597 free(*ress);
598 return -3;
600 tuple += ((uint8_t)(c-'!'))*pow85[count++];
601 if (count == 5) {
602 DECODE_TUPLE(tuple, 4);
603 count = 0;
604 tuple = 0;
606 break;
609 // write last (possibly incomplete) tuple
610 if (count-- > 0) {
611 tuple += pow85[count];
612 DECODE_TUPLE(tuple, count);
614 return dpos;
617 #undef PUSH_BACK
618 #undef DECODE_TUPLE
621 static void decodeBA (char *str, int len) {
622 char pch = 42;
624 for (int f = 0; f < len; ++f, ++str) {
625 char ch = *str;
627 ch = (ch-f-1)^pch;
628 *str = ch;
629 pch = ch;
634 static void printEC (const char *txt) {
635 char *dest;
636 int len;
638 if ((len = ascii85Decode(&dest, txt)) >= 0) {
639 decodeBA(dest, len);
640 fprintf(stderr, "%s\n", dest);
641 free(dest);
646 static int isStr85Equ (const char *txt, const char *str) {
647 char *dest;
648 int len, res = 0;
650 if (str != NULL && str[0] && (len = ascii85Decode(&dest, txt)) >= 0) {
651 if (len > 0) res = (strcmp(dest+1, str+1) == 0);
652 free(dest);
654 return res;
658 static int checkEGG (const char *str) {
659 if (isStr85Equ("06:]JASq", str) || isStr85Equ("0/i", str)) {
660 printEC(
661 "H8lZV&6)1>+AZ>m)Cf8;A1/cP+CnS)0OJ`X.QVcHA4^cc5r3=m1c%0D3&c263d?EV6@4&>"
662 "3DYQo;c-FcO+UJ;MOJ$TAYO@/FI]+B?C.L$>%:oPAmh:4Au)>AAU/H;ZakL2I!*!%J;(AK"
663 "NIR#5TXgZ6c'F1%^kml.JW5W8e;ql0V3fQUNfKpng6ppMf&ip-VOX@=jKl;#q\"DJ-_>jG"
664 "8#L;nm]!q;7c+hR6p;tVY#J8P$aTTK%c-OT?)<00,+q*8f&ff9a/+sbU,:`<H*[fk0o]7k"
665 "^l6nRkngc6Tl2Ngs!!P2I%KHG=7n*an'bsgn>!*8s7TLTC+^\\\"W+<=9^%Ol$1A1eR*Be"
666 "gqjEag:M0OnrC4FBY5@QZ&'HYYZ#EHs8t4$5]!22QoJ3`;-&=\\DteO$d6FBqT0E@:iu?N"
667 "a5ePUf^_uEEcjTDKfMpX/9]DFL8N-Ee;*8C5'WgbGortZuh1\\N0;/rJB6'(MSmYiS\"6+"
668 "<NK)KDV3e+Ad[@).W:%.dd'0h=!QUhghQaNNotIZGrpHr-YfEuUpsKW<^@qlZcdTDA!=?W"
669 "Yd+-^`'G8Or)<0-T&CT.i+:mJp(+/M/nLaVb#5$p2jR2<rl7\"XlngcN`mf,[4oK5JLr\\"
670 "m=X'(ue;'*1ik&/@T4*=j5t=<&/e/Q+2=((h`>>uN(#>&#i>2/ajK+=eib1coVe3'D)*75"
671 "m_h;28^M6p6*D854Jj<C^,Q8Wd\"O<)&L/=C$lUAQNN<=eTD:A6kn-=EItXSss.tAS&!;F"
672 "EsgpJTHIYNNnh'`kmX^[`*ELOHGcWbfPOT`J]A8P`=)AS;rYlR$\"-.RG440lK5:Dg?G'2"
673 "['dE=nEm1:k,,Se_=%-6Z*L^J[)EC"
675 return 1;
677 if (isStr85Equ("04Jj?B)", str)) {
678 printEC(
679 "IPaSa(`c:T,o9Bq3\\)IY++?+!-S9%P0/OkjE&f$l.OmK'Ai2;ZHn[<,6od7^8;)po:HaP"
680 "m<'+&DRS:/1L7)IA7?WI$8WKTUB2tXg>Zb$.?\"@AIAu;)6B;2_PB5M?oBPDC.F)606Z$V"
681 "=ONd6/5P*LoWKTLQ,d@&;+Ru,\\ESY*rg!l1XrhpJ:\"WKWdOg?l;=RHE:uU9C?aotBqj]"
682 "=k8cZ`rp\"ZO=GjkfD#o]Z\\=6^]+Gf&-UFthT*hN"
684 return 1;
686 if (isStr85Equ("04o69A7Tr", str)) {
687 printEC(
688 "Ag7d[&R#Ma9GVV5,S(D;De<T_+W).?,%4n+3cK=%4+0VN@6d\")E].np7l?8gF#cWF7SS_m"
689 "4@V\\nQ;h!WPD2h#@\\RY&G\\LKL=eTP<V-]U)BN^b.DffHkTPnFcCN4B;]8FCqI!p1@H*_"
690 "jHJ<%g']RG*MLqCrbP*XbNL=4D1R[;I(c*<FuesbWmSCF1jTW+rplg;9[S[7eDVl6YsjT"
692 return 1;
694 return 0;
698 ////////////////////////////////////////////////////////////////////////////////
699 int main (int argc, char *argv[]) {
700 char *configfile = NULL;
701 char *runcmd = NULL;
703 //dbgLogInit();
705 for (int f = 1; f < argc; f++) {
706 if (argv[f][0] == '-' && checkEGG(argv[f])) exit(1);
707 if (strcmp(argv[f], "-name") == 0) { ++f; continue; }
708 if (strcmp(argv[f], "-into") == 0) { ++f; continue; }
709 if (strcmp(argv[f], "-embed") == 0) { ++f; continue; }
710 switch (argv[f][0] != '-' || argv[f][2] ? -1 : argv[f][1]) {
711 case 'e': f = argc+1; break;
712 case 't':
713 case 'c':
714 case 'w':
715 case 'b':
716 case 'R':
717 ++f;
718 break;
719 case 'T':
720 if (++f < argc) {
721 free(opt_term);
722 opt_term = strdup(argv[f]);
723 opt_term_locked = 1;
725 break;
726 case 'C':
727 if (++f < argc) {
728 if (configfile != NULL) free(configfile);
729 configfile = strdup(argv[f]);
731 break;
732 case 'l':
733 if (++f < argc) cliLocale = argv[f];
734 break;
735 case 'S': // single-tab mode
736 opt_disabletabs = 1;
737 break;
738 case 'v':
739 fprintf(stderr, "%s", USAGE_VERSION);
740 exit(EXIT_FAILURE);
741 case 'h':
742 default:
743 fprintf(stderr, "%s%s", USAGE_VERSION, USAGE);
744 exit(EXIT_FAILURE);
748 initDefaultOptions();
749 if (configfile == NULL) {
750 const char *home = getenv("HOME");
752 if (home != NULL) {
753 configfile = SPrintf("%s/.sterm.rc", home);
754 if (loadConfig(configfile) == 0) goto cfgdone;
755 free(configfile); configfile = NULL;
757 configfile = SPrintf("%s/.config/sterm.rc", home);
758 if (loadConfig(configfile) == 0) goto cfgdone;
759 free(configfile); configfile = NULL;
762 configfile = SPrintf("/etc/sterm.rc");
763 if (loadConfig(configfile) == 0) goto cfgdone;
764 free(configfile); configfile = NULL;
766 configfile = SPrintf("/etc/sterm/sterm.rc");
767 if (loadConfig(configfile) == 0) goto cfgdone;
768 free(configfile); configfile = NULL;
770 configfile = SPrintf("./.sterm.rc");
771 if (loadConfig(configfile) == 0) goto cfgdone;
772 free(configfile); configfile = NULL;
773 // no config
774 } else {
775 if (loadConfig(configfile) < 0) k8t_die("config file '%s' not found!", configfile);
777 cfgdone:
778 if (configfile != NULL) free(configfile); configfile = NULL;
780 for (int f = 1; f < argc; f++) {
781 if (strcmp(argv[f], "-name") == 0) { ++f; continue; }
782 if (strcmp(argv[f], "-into") == 0 || strcmp(argv[f], "-embed") == 0) {
783 if (opt_embed) free(opt_embed);
784 opt_embed = strdup(argv[f]);
785 continue;
787 switch (argv[f][0] != '-' || argv[f][2] ? -1 : argv[f][1]) {
788 case 't':
789 if (++f < argc) {
790 free(opt_title);
791 opt_title = strdup(argv[f]);
793 break;
794 case 'c':
795 if (++f < argc) {
796 free(opt_class);
797 opt_class = strdup(argv[f]);
799 break;
800 case 'w':
801 if (++f < argc) {
802 if (opt_embed) free(opt_embed);
803 opt_embed = strdup(argv[f]);
805 break;
806 case 'R':
807 if (++f < argc) runcmd = argv[f];
808 break;
809 case 'e':
810 /* eat all remaining arguments */
811 if (++f < argc) opt_cmd = &argv[f];
812 f = argc+1;
813 case 'T':
814 case 'C':
815 case 'l':
816 ++f;
817 break;
818 case 'S':
819 break;
823 setenv("TERM", opt_term, 1);
824 mclock_init();
825 setlocale(LC_ALL, "");
826 initLCConversion();
827 summonChildKiller();
828 updateTabBar = 1;
829 termidx = 0;
830 curterm = k8t_termalloc();
831 k8t_tmInitialize(curterm, 80, 25, opt_maxhistory);
832 // pixmap will be created in xinit()
833 if (K8T_DATA(curterm)->execcmd != NULL) { free(K8T_DATA(curterm)->execcmd); K8T_DATA(curterm)->execcmd = NULL; }
834 if (k8t_ttyNew(curterm) != 0) k8t_die("can't run process");
835 opt_cmd = NULL;
836 xinit();
837 k8t_selInit(curterm);
838 if (runcmd != NULL) executeCommands(runcmd);
839 run();
840 return 0;