some fields moved out from K8Term structure
[k8sterm.git] / src / sterm.c
blob3a35b975fc247510b36c530e8d5f0bf61a0824b4
1 /* See LICENSE for licence details. */
2 #ifndef GIT_VERSION
3 # define VERSION "0.4.0.beta2"
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>
20 #include <pty.h>
21 #include <stdarg.h>
22 #include <stdint.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <strings.h>
27 #include <signal.h>
28 #include <sys/ioctl.h>
29 #include <sys/select.h>
30 #include <sys/stat.h>
31 #include <sys/time.h>
32 #include <sys/types.h>
33 #include <sys/wait.h>
34 #include <time.h>
35 #include <unistd.h>
36 #include <X11/Xatom.h>
37 #include <X11/Xlib.h>
38 #include <X11/Xutil.h>
39 #include <X11/cursorfont.h>
40 #include <X11/keysym.h>
42 ////////////////////////////////////////////////////////////////////////////////
43 #include "libk8sterm/k8sterm.h"
46 ////////////////////////////////////////////////////////////////////////////////
47 #include "defaults.c"
50 ////////////////////////////////////////////////////////////////////////////////
51 #define USAGE_VERSION "k8sterm " VERSION "\n(c) 2010-2012 st engineers and Ketmar // Vampire Avalon\n"
52 #define USAGE \
53 "usage: sterm [options]\n" \
54 "options:\n" \
55 "-v show version and exit\n" \
56 "-h show this help and exit\n" \
57 "-S disable tabs\n" \
58 "-t title set window title\n" \
59 "-c class set window class\n" \
60 "-w windowid embed in window id given id\n" \
61 "-T termname set TERM name\n" \
62 "-C config_file use custom config file\n" \
63 "-l langiconv use given locale\n" \
64 "-R stcmd run this INTERNAL k8sterm command\n" \
65 "-e command... run given command and pass all following args to it\n"
68 ////////////////////////////////////////////////////////////////////////////////
69 /* masks for key translation */
70 #define XK_NO_MOD (UINT_MAX)
71 #define XK_ANY_MOD (0)
74 ////////////////////////////////////////////////////////////////////////////////
75 // internal command line mode
76 enum {
77 K8T_CMDMODE_NONE,
78 K8T_CMDMODE_INPUT,
79 K8T_CMDMODE_MESSAGE
83 // X11 window state flags
84 enum {
85 K8T_WIN_VISIBLE = 0x01,
86 K8T_WIN_REDRAW = 0x02,
87 K8T_WIN_FOCUSED = 0x04
91 ////////////////////////////////////////////////////////////////////////////////
92 /* Purely graphic info */
93 typedef struct {
94 Display *dpy;
95 Colormap cmap;
96 Window win;
97 Cursor cursor; // text cursor
98 Cursor defcursor; // 'default' cursor
99 Cursor lastcursor; // last used cursor before blanking
100 Cursor blankPtr;
101 Atom xembed;
102 XIM xim;
103 XIC xic;
104 int scr;
105 int w; /* window width */
106 int h; /* window height */
107 int bufw; /* pixmap width */
108 int bufh; /* pixmap height */
109 int ch; /* char height */
110 int cw; /* char width */
111 char state; /* focus, redraw, visible */
113 int tch; /* tab text char height */
114 Pixmap pictab;
115 int tabheight;
116 //struct timeval lastdraw;
117 } K8TXWindow;
120 /* Drawing Context */
121 typedef struct {
122 uint32_t *ncol; // normal colors
123 uint32_t *bcol; // b/w colors
124 uint32_t *gcol; // green colors
125 uint32_t *clrs[3];
126 GC gc;
127 struct {
128 int ascent;
129 int descent;
130 short lbearing;
131 short rbearing;
132 XFontSet set;
133 Font fid;
134 } font[3];
135 } K8TXDC;
138 typedef struct K8TCmdLine K8TCmdLine;
139 typedef struct K8Term K8Term;
141 typedef void (*CmdLineExecFn) (K8Term *term, K8TCmdLine *cmdline, int cancelled);
143 struct K8TCmdLine {
144 int cmdMode; // K8T_CMDMODE_xxx
145 char cmdline[K8T_UTF_SIZ*CMDLINE_SIZE];
146 int cmdreslen; // byte length of 'reserved' (read-only) part
147 int cmdofs; // byte offset of the first visible UTF-8 char in cmdline
148 char cmdc[K8T_UTF_SIZ+1]; // buffer to collect UTF-8 char
149 int cmdcl; // index in cmdc, used to collect UTF-8 chars
150 int cmdtabpos; // # of bytes (not UTF-8 chars!) used in autocompletion or -1
151 const char *cmdcurtabc; // current autocompleted command
152 CmdLineExecFn cmdexecfn;
153 void *udata;
157 /* internal terminal data */
158 typedef struct {
159 pid_t pid;
160 int exitcode;
162 int waitkeypress; /* child is dead, awaiting for keypress */
163 char *exitmsg; /* message for waitkeypress */
165 char *execcmd;
167 int picalloced;
168 Pixmap picbuf;
169 int picbufw;
170 int picbufh;
172 K8TCmdLine cmdline;
173 } K8TermData;
175 #define K8T_DATA(_term) ((K8TermData *)(_term->udata))
178 ////////////////////////////////////////////////////////////////////////////////
179 #include "globals.c"
180 #include "getticks.c"
183 ////////////////////////////////////////////////////////////////////////////////
184 #include "utils.c"
185 #include "keymaps.c"
188 ////////////////////////////////////////////////////////////////////////////////
189 static void executeCommands (const char *str);
190 static const char *findCommandCompletion (const char *str, int slen, const char *prev);
192 static void tdrawfatalmsg (K8Term *term, const char *msg);
194 static void tcmdput (K8Term *term, K8TCmdLine *cmdline, const char *s, int len);
196 static void xclearunused (void);
197 static void xseturgency (int add);
198 static void xfixsel (void);
199 static void xblankPointer (void);
200 static void xunblankPointer (void);
201 static void xdrawTabBar (void);
202 static void xdrawcmdline (K8Term *term, K8TCmdLine *cmdline, int scry);
204 static void termCreateXPixmap (K8Term *term);
205 static void termSetCallbacks (K8Term *term);
206 static void termSetDefaults (K8Term *term);
208 static void getButtonInfo (K8Term *term, XEvent *e, int *b, int *x, int *y);
210 static void fixWindowTitle (const K8Term *t);
212 static void scrollHistory (K8Term *term, int delta);
215 ////////////////////////////////////////////////////////////////////////////////
216 static inline uint32_t getColor (int idx) {
217 if (globalBW && (curterm == NULL || !curterm->blackandwhite)) return dc.clrs[globalBW][idx];
218 if (curterm != NULL) return dc.clrs[curterm->blackandwhite%3][idx];
219 return dc.clrs[0][idx];
223 ////////////////////////////////////////////////////////////////////////////////
224 #include "iniparse.c"
225 #include "locales.c"
228 ////////////////////////////////////////////////////////////////////////////////
229 #include "termswitch.c"
230 #include "ttyinit.c"
233 ////////////////////////////////////////////////////////////////////////////////
234 #include "tcmdline.c"
235 #include "tfatalbox.c"
238 ////////////////////////////////////////////////////////////////////////////////
239 #include "x11init.c"
240 #include "x11misc.c"
241 #include "x11drawtabs.c"
242 #include "x11drawcmdline.c"
245 ////////////////////////////////////////////////////////////////////////////////
246 #include "x11evtvis.c"
247 #include "x11evtkbd.c"
248 #include "x11evtsel.c"
249 #include "x11evtmouse.c"
252 ////////////////////////////////////////////////////////////////////////////////
253 #include "x11CB.c"
256 ////////////////////////////////////////////////////////////////////////////////
257 // xembed?
258 static void xevtcbcmessage (XEvent *e) {
259 /* See xembed specs http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html */
260 if (e->xclient.message_type == xw.xembed && e->xclient.format == 32) {
261 if (e->xclient.data.l[1] == XEMBED_FOCUS_IN) {
262 xw.state |= K8T_WIN_FOCUSED;
263 xseturgency(0);
264 k8t_tmSendFocusEvent(curterm, 1);
265 } else if (e->xclient.data.l[1] == XEMBED_FOCUS_OUT) {
266 xw.state &= ~K8T_WIN_FOCUSED;
267 k8t_tmSendFocusEvent(curterm, 0);
269 k8t_drawCursor(curterm, 0);
270 xdrawTabBar();
271 k8t_drawCopy(curterm, 0, 0, curterm->col, curterm->row);
272 return;
275 if (e->xclient.data.l[0] == XA_WM_DELETE_WINDOW) {
276 closeRequestComes = 1;
277 return;
282 ////////////////////////////////////////////////////////////////////////////////
283 static inline int last_draw_too_old (void) {
284 K8TTimeMSec tt = mclock_ticks();
286 if (curterm != NULL) {
287 if (curterm->dead || !curterm->wantRedraw) return 0;
289 return
290 (tt-curterm->lastDrawTime >= opt_drawtimeout ||
291 tt-lastDrawTime >= (curterm->justSwapped ? opt_swapdrawtimeout : opt_maxdrawtimeout));
293 return (tt-lastDrawTime >= opt_maxdrawtimeout);
297 ////////////////////////////////////////////////////////////////////////////////
298 // main loop
299 static void (*handler[LASTEvent])(XEvent *) = {
300 [KeyPress] = xevtcbkpress,
301 [ClientMessage] = xevtcbcmessage,
302 [ConfigureNotify] = xevtcbresise,
303 [VisibilityNotify] = xevtcbvisibility,
304 [UnmapNotify] = xevtcbunmap,
305 [Expose] = xevtcbexpose,
306 [FocusIn] = xevtcbfocus,
307 [FocusOut] = xevtcbfocus,
308 [MotionNotify] = xevtcbbmotion,
309 [ButtonPress] = xevtcbbpress,
310 [ButtonRelease] = xevtcbbrelease,
311 [SelectionNotify] = xevtcbselnotify,
312 [SelectionRequest] = xevtcbselrequest,
313 [SelectionClear] = xevtcbselclear,
317 static void run (void) {
318 //int stuff_to_print = 0;
319 int xfd = XConnectionNumber(xw.dpy);
321 ptrLastMove = mclock_ticks();
322 while (term_count > 0) {
323 XEvent ev;
324 fd_set rfd, wfd;
325 struct timeval timeout;
326 int maxfd = xfd;
327 int dodraw;
329 FD_ZERO(&rfd);
330 FD_ZERO(&wfd);
331 FD_SET(xfd, &rfd);
332 //FD_SET(curterm->cmdfd, &rfd);
333 // have something to write?
334 for (int f = 0; f < term_count; ++f) {
335 K8Term *t = term_array[f];
337 if (!t->dead && t->cmdfd >= 0) {
338 if (t->cmdfd > maxfd) maxfd = t->cmdfd;
339 FD_SET(t->cmdfd, &rfd);
340 if (K8T_DATA(t)->pid != 0 && t->wrbufpos < t->wrbufused) FD_SET(t->cmdfd, &wfd);
344 timeout.tv_sec = 0;
345 timeout.tv_usec = (opt_drawtimeout+2)*1000;
346 //fprintf(stderr, "before select...\n");
347 if (select(maxfd+1, &rfd, &wfd, NULL, &timeout) < 0) {
348 if (errno == EINTR) continue;
349 k8t_die("select failed: %s", strerror(errno));
351 //fprintf(stderr, "after select...\n");
352 // process terminals i/o
353 for (int f = 0; f < term_count; ++f) {
354 K8Term *t = term_array[f];
356 if (!t->dead && t->cmdfd >= 0) {
357 int rd = -1;
359 if (K8T_DATA(t)->pid != 0 && FD_ISSET(t->cmdfd, &wfd)) k8t_ttyFlushWriteBuf(t);
360 if (FD_ISSET(t->cmdfd, &rfd)) rd = k8t_ttyRead(t);
362 if (K8T_DATA(t)->waitkeypress && K8T_DATA(t)->exitmsg != NULL && rd < 0) {
363 // there will be no more data
364 close(t->cmdfd);
365 t->cmdfd = -1;
367 tdrawfatalmsg(t, K8T_DATA(t)->exitmsg);
368 free(K8T_DATA(t)->exitmsg);
369 K8T_DATA(t)->exitmsg = NULL;
370 k8t_tmWantRedraw(t, 1);
375 termcleanup();
376 if (term_count == 0) exit(exitcode);
378 dodraw = 0;
379 if (curterm != NULL && curterm->curblink > 0) {
380 K8TTimeMSec tt = mclock_ticks();
382 if (tt-curterm->lastBlinkTime >= curterm->curblink) {
383 curterm->lastBlinkTime = tt;
384 if ((xw.state&K8T_WIN_FOCUSED) || curterm->curblinkinactive) {
385 curterm->curbhidden = (curterm->curbhidden ? 0 : -1);
386 dodraw = 1;
387 } else {
388 curterm->curbhidden = 0;
392 if (updateTabBar) xdrawTabBar();
393 if (dodraw || last_draw_too_old()) k8t_drawTerm(curterm, 0);
395 if (XPending(xw.dpy)) {
396 while (XPending(xw.dpy)) {
397 XNextEvent(xw.dpy, &ev);
398 switch (ev.type) {
399 //case VisibilityNotify:
400 //case UnmapNotify:
401 //case FocusIn:
402 //case FocusOut:
403 case MotionNotify:
404 case ButtonPress:
405 case ButtonRelease:
406 xunblankPointer();
407 ptrLastMove = mclock_ticks();
408 break;
409 default: ;
411 if (XFilterEvent(&ev, xw.win)) continue;
412 if (handler[ev.type]) (handler[ev.type])(&ev);
416 if (curterm != NULL) {
417 switch (closeRequestComes) {
418 case 1: // just comes
419 if (opt_ignoreclose == 0) {
420 tcmdlinehide(curterm, &K8T_DATA(curterm)->cmdline);
421 tcmdlineinitex(curterm, &K8T_DATA(curterm)->cmdline, "Do you really want to close sterm [Y/n]? ");
422 K8T_DATA(curterm)->cmdline.cmdexecfn = cmdline_closequeryexec;
423 } else if (opt_ignoreclose < 0) {
424 //FIXME: kill all clients?
425 return;
427 closeRequestComes = 0;
428 break;
429 case 2: // ok, die now
430 //FIXME: kill all clients?
431 return;
435 if (!ptrBlanked && opt_ptrblank > 0 && mclock_ticks()-ptrLastMove >= opt_ptrblank) {
436 xblankPointer();
442 ////////////////////////////////////////////////////////////////////////////////
443 static void termSetDefaults (K8Term *term) {
444 term->deffg = defaultFG;
445 term->defbg = defaultBG;
446 term->defboldfg = defaultBoldFG;
447 term->defunderfg = defaultUnderlineFG;
449 term->defcurfg = defaultCursorFG;
450 term->defcurbg = defaultCursorBG;
451 term->defcurinactivefg = defaultCursorInactiveFG;
452 term->defcurinactivebg = defaultCursorInactiveBG;
454 term->tabsize = opt_tabsize;
458 static void termSetCallbacks (K8Term *term) {
459 K8TermData *td = calloc(1, sizeof(K8TermData));
461 if (td == NULL) k8t_die("out of memory!");
462 term->udata = td;
464 term->clockTicks = clockTicksCB;
465 term->setLastDrawTime = setLastDrawTimeCB;
467 term->isConversionNecessary = isConversionNecessaryCB;
468 term->loc2utf = loc2utfCB;
469 term->utf2loc = utf2locCB;
471 term->isFocused = isFocusedCB;
472 term->isVisible = isVisibleCB;
474 term->drawSetFG = drawSetFGCB;
475 term->drawSetBG = drawSetBGCB;
476 term->drawRect = drawRectCB;
477 term->drawFillRect = drawFillRectCB;
478 term->drawCopyArea = drawCopyAreaCB;
479 term->drawString = drawStringCB;
480 term->drawOverlay = drawOverlayCB;
482 term->fixSelection = fixSelectionCB;
483 term->clearSelection = clearSelectionCB;
485 term->titleChanged = titleChangedCB;
486 term->doBell = doBellCB;
488 term->isUnderOverlay = isUnderOverlayCB;
489 term->clipToOverlay = clipToOverlayCB;
491 term->unimap = unimap;
492 td->execcmd = NULL;
496 static void termCreateXPixmap (K8Term *term) {
497 K8TermData *td = K8T_DATA(term);
498 Pixmap newbuf;
499 int oldw = td->picbufw, oldh = td->picbufh;
501 td->picbufw = K8T_MAX(1, term->col*xw.cw);
502 td->picbufh = K8T_MAX(1, term->row*xw.ch);
504 newbuf = XCreatePixmap(xw.dpy, xw.win, td->picbufw, td->picbufh, XDefaultDepth(xw.dpy, xw.scr));
506 if (td->picalloced) {
507 XCopyArea(xw.dpy, td->picbuf, newbuf, dc.gc, 0, 0, td->picbufw, td->picbufh, 0, 0);
508 XFreePixmap(xw.dpy, td->picbuf);
511 term->drawSetFG(term, term->defbg);
513 if (td->picalloced) {
514 if (td->picbufw > oldw) {
515 XFillRectangle(xw.dpy, newbuf, dc.gc, oldw, 0, td->picbufw-oldw, K8T_MIN(td->picbufh, oldh));
516 } else if (td->picbufw < oldw && xw.w > td->picbufw) {
517 XClearArea(xw.dpy, xw.win, td->picbufw, 0, xw.w-td->picbufh, K8T_MIN(td->picbufh, oldh), False);
520 if (td->picbufh > oldh) {
521 XFillRectangle(xw.dpy, newbuf, dc.gc, 0, oldh, td->picbufw, td->picbufh-oldh);
522 } else if (td->picbufh < oldh && xw.h > td->picbufh) {
523 XClearArea(xw.dpy, xw.win, 0, td->picbufh, xw.w, xw.h-td->picbufh, False);
525 } else {
526 XFillRectangle(xw.dpy, newbuf, dc.gc, 0, 0, td->picbufw, td->picbufh);
529 td->picalloced = 1;
530 td->picbuf = newbuf;
532 k8t_tmFullDirty(term);
536 ////////////////////////////////////////////////////////////////////////////////
537 #include "inifile.c"
538 #include "commands.c"
541 ////////////////////////////////////////////////////////////////////////////////
542 #include "childkiller.c"
545 ////////////////////////////////////////////////////////////////////////////////
546 #define PUSH_BACK(_c) (*ress)[dpos++] = (_c)
547 #define DECODE_TUPLE(tuple,bytes) \
548 for (tmp = bytes; tmp > 0; tmp--, tuple = (tuple & 0x00ffffff)<<8) \
549 PUSH_BACK((char)((tuple >> 24)&0xff))
551 // returns ress length
552 static int ascii85Decode (char **ress, const char *srcs/*, int start, int length*/) {
553 static uint32_t pow85[5] = { 85*85*85*85UL, 85*85*85UL, 85*85UL, 85UL, 1UL };
554 const uint8_t *data = (const uint8_t *)srcs;
555 int len = strlen(srcs);
556 uint32_t tuple = 0;
557 int count = 0, c = 0;
558 int dpos = 0;
559 int start = 0, length = len;
560 int tmp;
562 if (start < 0) start = 0; else { len -= start; data += start; }
563 if (length < 0 || len < length) length = len;
565 if (length > 0) {
566 int xlen = 4*((length+4)/5);
567 kstringReserve(ress, xlen);
571 *ress = (char *)calloc(1, len+1);
572 for (int f = length; f > 0; --f, ++data) {
573 c = *data;
574 if (c <= ' ') continue; // skip blanks
575 switch (c) {
576 case 'z': // zero tuple
577 if (count != 0) {
578 //fprintf(stderr, "%s: z inside ascii85 5-tuple\n", file);
579 free(*ress);
580 *ress = NULL;
581 return -1;
583 PUSH_BACK('\0');
584 PUSH_BACK('\0');
585 PUSH_BACK('\0');
586 PUSH_BACK('\0');
587 break;
588 case '~': // '~>': end of sequence
589 if (f < 1 || data[1] != '>') { free(*ress); return -2; } // error
590 if (count > 0) { f = -1; break; }
591 default:
592 if (c < '!' || c > 'u') {
593 //fprintf(stderr, "%s: bad character in ascii85 region: %#o\n", file, c);
594 free(*ress);
595 return -3;
597 tuple += ((uint8_t)(c-'!'))*pow85[count++];
598 if (count == 5) {
599 DECODE_TUPLE(tuple, 4);
600 count = 0;
601 tuple = 0;
603 break;
606 // write last (possibly incomplete) tuple
607 if (count-- > 0) {
608 tuple += pow85[count];
609 DECODE_TUPLE(tuple, count);
611 return dpos;
614 #undef PUSH_BACK
615 #undef DECODE_TUPLE
618 static void decodeBA (char *str, int len) {
619 char pch = 42;
621 for (int f = 0; f < len; ++f, ++str) {
622 char ch = *str;
624 ch = (ch-f-1)^pch;
625 *str = ch;
626 pch = ch;
631 static void printEC (const char *txt) {
632 char *dest;
633 int len;
635 if ((len = ascii85Decode(&dest, txt)) >= 0) {
636 decodeBA(dest, len);
637 fprintf(stderr, "%s\n", dest);
638 free(dest);
643 static int isStr85Equ (const char *txt, const char *str) {
644 char *dest;
645 int len, res = 0;
647 if (str != NULL && str[0] && (len = ascii85Decode(&dest, txt)) >= 0) {
648 if (len > 0) res = (strcmp(dest+1, str+1) == 0);
649 free(dest);
651 return res;
655 static int checkEGG (const char *str) {
656 if (isStr85Equ("06:]JASq", str) || isStr85Equ("0/i", str)) {
657 printEC(
658 "H8lZV&6)1>+AZ>m)Cf8;A1/cP+CnS)0OJ`X.QVcHA4^cc5r3=m1c%0D3&c263d?EV6@4&>"
659 "3DYQo;c-FcO+UJ;MOJ$TAYO@/FI]+B?C.L$>%:oPAmh:4Au)>AAU/H;ZakL2I!*!%J;(AK"
660 "NIR#5TXgZ6c'F1%^kml.JW5W8e;ql0V3fQUNfKpng6ppMf&ip-VOX@=jKl;#q\"DJ-_>jG"
661 "8#L;nm]!q;7c+hR6p;tVY#J8P$aTTK%c-OT?)<00,+q*8f&ff9a/+sbU,:`<H*[fk0o]7k"
662 "^l6nRkngc6Tl2Ngs!!P2I%KHG=7n*an'bsgn>!*8s7TLTC+^\\\"W+<=9^%Ol$1A1eR*Be"
663 "gqjEag:M0OnrC4FBY5@QZ&'HYYZ#EHs8t4$5]!22QoJ3`;-&=\\DteO$d6FBqT0E@:iu?N"
664 "a5ePUf^_uEEcjTDKfMpX/9]DFL8N-Ee;*8C5'WgbGortZuh1\\N0;/rJB6'(MSmYiS\"6+"
665 "<NK)KDV3e+Ad[@).W:%.dd'0h=!QUhghQaNNotIZGrpHr-YfEuUpsKW<^@qlZcdTDA!=?W"
666 "Yd+-^`'G8Or)<0-T&CT.i+:mJp(+/M/nLaVb#5$p2jR2<rl7\"XlngcN`mf,[4oK5JLr\\"
667 "m=X'(ue;'*1ik&/@T4*=j5t=<&/e/Q+2=((h`>>uN(#>&#i>2/ajK+=eib1coVe3'D)*75"
668 "m_h;28^M6p6*D854Jj<C^,Q8Wd\"O<)&L/=C$lUAQNN<=eTD:A6kn-=EItXSss.tAS&!;F"
669 "EsgpJTHIYNNnh'`kmX^[`*ELOHGcWbfPOT`J]A8P`=)AS;rYlR$\"-.RG440lK5:Dg?G'2"
670 "['dE=nEm1:k,,Se_=%-6Z*L^J[)EC"
672 return 1;
674 if (isStr85Equ("04Jj?B)", str)) {
675 printEC(
676 "IPaSa(`c:T,o9Bq3\\)IY++?+!-S9%P0/OkjE&f$l.OmK'Ai2;ZHn[<,6od7^8;)po:HaP"
677 "m<'+&DRS:/1L7)IA7?WI$8WKTUB2tXg>Zb$.?\"@AIAu;)6B;2_PB5M?oBPDC.F)606Z$V"
678 "=ONd6/5P*LoWKTLQ,d@&;+Ru,\\ESY*rg!l1XrhpJ:\"WKWdOg?l;=RHE:uU9C?aotBqj]"
679 "=k8cZ`rp\"ZO=GjkfD#o]Z\\=6^]+Gf&-UFthT*hN"
681 return 1;
683 if (isStr85Equ("04o69A7Tr", str)) {
684 printEC(
685 "Ag7d[&R#Ma9GVV5,S(D;De<T_+W).?,%4n+3cK=%4+0VN@6d\")E].np7l?8gF#cWF7SS_m"
686 "4@V\\nQ;h!WPD2h#@\\RY&G\\LKL=eTP<V-]U)BN^b.DffHkTPnFcCN4B;]8FCqI!p1@H*_"
687 "jHJ<%g']RG*MLqCrbP*XbNL=4D1R[;I(c*<FuesbWmSCF1jTW+rplg;9[S[7eDVl6YsjT"
689 return 1;
691 return 0;
695 ////////////////////////////////////////////////////////////////////////////////
696 int main (int argc, char *argv[]) {
697 char *configfile = NULL;
698 char *runcmd = NULL;
700 //dbgLogInit();
702 for (int f = 1; f < argc; f++) {
703 if (argv[f][0] == '-' && checkEGG(argv[f])) exit(1);
704 if (strcmp(argv[f], "-name") == 0) { ++f; continue; }
705 if (strcmp(argv[f], "-into") == 0) { ++f; continue; }
706 if (strcmp(argv[f], "-embed") == 0) { ++f; continue; }
707 switch (argv[f][0] != '-' || argv[f][2] ? -1 : argv[f][1]) {
708 case 'e': f = argc+1; break;
709 case 't':
710 case 'c':
711 case 'w':
712 case 'b':
713 case 'R':
714 ++f;
715 break;
716 case 'T':
717 if (++f < argc) {
718 free(opt_term);
719 opt_term = strdup(argv[f]);
720 opt_term_locked = 1;
722 break;
723 case 'C':
724 if (++f < argc) {
725 if (configfile != NULL) free(configfile);
726 configfile = strdup(argv[f]);
728 break;
729 case 'l':
730 if (++f < argc) cliLocale = argv[f];
731 break;
732 case 'S': // single-tab mode
733 opt_disabletabs = 1;
734 break;
735 case 'v':
736 fprintf(stderr, "%s", USAGE_VERSION);
737 exit(EXIT_FAILURE);
738 case 'h':
739 default:
740 fprintf(stderr, "%s%s", USAGE_VERSION, USAGE);
741 exit(EXIT_FAILURE);
745 initDefaultOptions();
746 if (configfile == NULL) {
747 const char *home = getenv("HOME");
749 if (home != NULL) {
750 configfile = SPrintf("%s/.sterm.rc", home);
751 if (loadConfig(configfile) == 0) goto cfgdone;
752 free(configfile); configfile = NULL;
754 configfile = SPrintf("%s/.config/sterm.rc", home);
755 if (loadConfig(configfile) == 0) goto cfgdone;
756 free(configfile); configfile = NULL;
759 configfile = SPrintf("/etc/sterm.rc");
760 if (loadConfig(configfile) == 0) goto cfgdone;
761 free(configfile); configfile = NULL;
763 configfile = SPrintf("/etc/sterm/sterm.rc");
764 if (loadConfig(configfile) == 0) goto cfgdone;
765 free(configfile); configfile = NULL;
767 configfile = SPrintf("./.sterm.rc");
768 if (loadConfig(configfile) == 0) goto cfgdone;
769 free(configfile); configfile = NULL;
770 // no config
771 } else {
772 if (loadConfig(configfile) < 0) k8t_die("config file '%s' not found!", configfile);
774 cfgdone:
775 if (configfile != NULL) free(configfile); configfile = NULL;
777 for (int f = 1; f < argc; f++) {
778 if (strcmp(argv[f], "-name") == 0) { ++f; continue; }
779 if (strcmp(argv[f], "-into") == 0 || strcmp(argv[f], "-embed") == 0) {
780 if (opt_embed) free(opt_embed);
781 opt_embed = strdup(argv[f]);
782 continue;
784 switch (argv[f][0] != '-' || argv[f][2] ? -1 : argv[f][1]) {
785 case 't':
786 if (++f < argc) {
787 free(opt_title);
788 opt_title = strdup(argv[f]);
790 break;
791 case 'c':
792 if (++f < argc) {
793 free(opt_class);
794 opt_class = strdup(argv[f]);
796 break;
797 case 'w':
798 if (++f < argc) {
799 if (opt_embed) free(opt_embed);
800 opt_embed = strdup(argv[f]);
802 break;
803 case 'R':
804 if (++f < argc) runcmd = argv[f];
805 break;
806 case 'e':
807 /* eat all remaining arguments */
808 if (++f < argc) opt_cmd = &argv[f];
809 f = argc+1;
810 case 'T':
811 case 'C':
812 case 'l':
813 ++f;
814 break;
815 case 'S':
816 break;
820 setenv("TERM", opt_term, 1);
821 mclock_init();
822 setlocale(LC_ALL, "");
823 initLCConversion();
824 summonChildKiller();
825 updateTabBar = 1;
826 termidx = 0;
827 curterm = k8t_termalloc();
828 k8t_tmInitialize(curterm, 80, 25, opt_maxhistory);
829 // pixmap will be created in xinit()
830 if (K8T_DATA(curterm)->execcmd != NULL) { free(K8T_DATA(curterm)->execcmd); K8T_DATA(curterm)->execcmd = NULL; }
831 if (k8t_ttyNew(curterm) != 0) k8t_die("can't run process");
832 opt_cmd = NULL;
833 xinit();
834 k8t_selInit(curterm);
835 if (runcmd != NULL) executeCommands(runcmd);
836 run();
837 return 0;