1 /* $MirOS: contrib/code/jupp/main.c,v 1.27 2017/01/11 19:26:54 tg Exp $ */
3 #define JUPP_IS_COPYRIGHT_C_BY "2017 mirabilos"
6 * Copyright (c) 2004ff. Thorsten Glaser
7 * Copyright (C) 1992 Joseph H. Allen
9 * This file is part of "jupp", a variant of Joe's Own Editor "JOE".
11 * jupp is free software; you can redistribute and/or modify it, deal
12 * in the work, etc. under the terms of the GNU General Public Licen-
13 * se, version 1 (of February 1989) as published by the Free Software
14 * Foundation, reproduced in the file COPYING in the jupp source code
15 * distribution. If jupp is combined with other independent work, for
16 * example libraries or when using crunchgen, into a combined binary,
17 * that may be conveyed under any version of the GPL, as published by
18 * the Free Software Foundation, and any compatible licence permitted
19 * by any version of the GPL, as above.
21 * This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
22 * the utmost extent permitted by applicable law, neither express nor
23 * implied; without malicious intent or gross negligence. In no event
24 * may a licensor, author or contributor be held liable for indirect,
25 * direct, other damage, loss, or other issues arising in any way out
26 * of dealing in the work, even if advised of the possibility of such
27 * damage or existence of a defect, except proven that it results out
28 * of said person's immediate fault when using the work as intended.
30 * Editor startup and main edit loop
59 extern int mid, dspasis, help, pgamnt, nobackups, lightoff, exask, lines, columns, dopadding, marking, dobeep;
61 extern int idleout; /* Clear to use /dev/tty for screen */
62 extern unsigned char *joeterm;
63 int help = 0; /* Set to have help on when starting */
64 int nonotice = 0; /* Set to prevent copyright notice */
66 unsigned char *exmsg = NULL; /* Message to display when exiting the editor */
68 SCREEN *maint; /* Main edit screen */
70 const char null[] = "";
73 static unsigned char *cygwin32_cmdline(void);
76 /* Make windows follow cursor */
83 if (w->y != -1 && w->watom->follow && w->object)
84 w->watom->follow(w->object);
85 w = (W *) (w->link.next);
86 } while (w != maint->curwin);
104 if ((wid >= 2 && wid != maint->w) || (hei >= 1 && hei != maint->h)) {
105 nresize(maint->t, wid, hei);
115 if (w->object && w->watom->disp)
116 w->watom->disp(w->object, flg);
119 w = (W *) (w->link.next);
120 } while (w != maint->curwin);
121 cpos(maint->t, maint->curwin->x + maint->curwin->curx, maint->curwin->y + maint->curwin->cury);
125 static int ahead = 0;
126 static int ungot = 0;
127 static int ungotc = 0;
131 if (c != 'C' - '@' && c != 'M' - '@') {
144 if (maint->curwin->watom->what == TYPETW)
147 maint->curwin->notify = &term;
149 while (!leave && (!flg || !term)) {
166 if (!ahead && c == 10)
168 m = dokey(maint->curwin->kbd, c);
169 if (maint->curwin->main && maint->curwin->main != maint->curwin) {
170 int x = maint->curwin->kbd->x;
172 maint->curwin->main->kbd->x = x;
174 maint->curwin->main->kbd->seq[x - 1] = maint->curwin->kbd->seq[x - 1];
186 unsigned char **mainenv;
188 int main(int argc, char **argv, char **envp)
202 mainenv = (unsigned char **)envp;
203 run = namprt(argv[0]);
205 if ((s = (unsigned char *)getenv("LINES")) != NULL)
206 sscanf((char *)s, "%d", &lines);
207 if ((s = (unsigned char *)getenv("COLUMNS")) != NULL)
208 sscanf((char *)s, "%d", &columns);
209 if ((s = (unsigned char *)getenv("BAUD")) != NULL)
210 sscanf((char *)s, "%u", &Baud);
211 if (getenv("DOPADDING"))
215 if ((s = (unsigned char *)getenv("JOETERM")) != NULL)
218 if (!(cap = getcap(NULL, 9600, NULL, NULL))) {
219 fprintf(stderr, "Couldn't load termcap/terminfo entry\n");
223 s = (unsigned char *)getenv("HOME");
225 s = vsncpy(NULL, 0, sz(s));
226 s = vsncpy(sv(s), sc("/."));
227 s = vsncpy(sv(s), sv(run));
228 s = vsncpy(sv(s), sc("rc"));
233 unsigned char buf[8];
235 fprintf(stderr, "There were errors in '%s'. Use it anyway?", s);
237 if (fgets((char *)buf, 8, stdin) != NULL &&
238 (buf[0] == 'y' || buf[0] == 'Y'))
245 s = vsncpy(NULL, 0, sz(get_JOERC));
246 s = vsncpy(sv(s), sv(run));
247 s = vsncpy(sv(s), sc("rc"));
252 unsigned char buf[8];
254 fprintf(stderr, "There were errors in '%s'. Use it anyway?", s);
256 if (fgets((char *)buf, 8, stdin) != NULL &&
257 (buf[0] == 'y' || buf[0] == 'Y'))
262 /* Try built-in joerc */
264 s = vsncpy(NULL, 0, sc("*"));
265 s = vsncpy(sv(s), sv(run));
266 s = vsncpy(sv(s), sc("rc"));
271 unsigned char buf[8];
273 fprintf(stderr, "There were errors in '%s'. Use it anyway?", s);
275 if (fgets((char *)buf, 8, stdin) != NULL &&
276 (buf[0] == 'y' || buf[0] == 'Y'))
280 fprintf(stderr, "Couldn't open '%s'\n", s);
285 for (c = 1; argv[c]; ++c) {
286 if (argv[c][0] == '-') {
288 if (!strcmp(argv[c], "-CYGhack")) {
289 s = cygwin32_cmdline();
290 s = strstr(s, "-CYGhack");
292 s += /* strlen("-CYGhack") */ 8;
293 while (*s == ' ' || *s == '\t')
302 switch (glopt(argv[c] + 1, argv[c + 1], NULL, 1)) {
304 fprintf(stderr, "Unknown option '%s'\n", argv[c]);
316 if (!(n = nopen(cap)))
321 for (c = 1, backopt = 0; argv[c]; ++c)
322 if (argv[c][0] == '+' && argv[c][1]) {
325 } else if (argv[c][0] == '-' && argv[c][1]) {
328 if (glopt(argv[c] + 1, argv[c + 1], NULL, 0) == 2)
331 B *b = bfind(argv[c]);
335 if (!orphan || !opened) {
336 bw = wmktw(maint, b);
338 msgnwt(bw->parent, msgs[-er]);
344 bw->o.readonly = bw->b->rdonly;
346 unsigned char *old_context;
348 old_context = bw->o.context;
349 while (backopt != c) {
350 if (argv[backopt][0] == '+') {
351 sscanf((char *)(argv[backopt] + 1), "%ld", &lnum);
354 if (glopt(argv[backopt] + 1, argv[backopt + 1], &bw->o, 0) == 2)
361 if (old_context != bw->o.context) {
363 rmkbd(bw->parent->kbd);
365 mkkbd(kmap_getcontext(bw->o.context, 1));
369 bw->b->rdonly = bw->o.readonly;
370 if (!opened || opened == (void *)&opened)
372 maint->curwin = bw->parent;
373 if (er == -1 && bw->o.mnew)
375 if (er == 0 && bw->o.mold)
377 maint->curwin = opened;
379 pline(bw->cursor, lnum - 1);
382 opened = (void *)&opened;
386 maint->curwin = opened == (void *)&opened ? NULL : opened;
394 BW *bw = wmktw(maint, bfind(US ""));
399 maint->curwin = maint->topwin;
405 joe_snprintf_4((char *)msgbuf,JOE_MSGBUFSIZE,
406 "\\i[ Joe's Own Editor v" VERSION
407 " | %s | %s " JUPP_IS_COPYRIGHT_C_BY " ]%s%s\\i",
408 locale_map->name, locale_map->type ? "©" : "(c)",
409 fdefault.hmsg ? " " : "",
410 fdefault.hmsg ? fdefault.hmsg : "");
411 msgnw(((BASE *)lastw(maint)->object)->parent, msgbuf);
418 fprintf(stderr, "\n%s\n", exmsg);
425 /* return command line as passed to the .EXE (just like cygwin32’s dcrt0.cc) */
426 static unsigned char *
427 cygwin32_cmdline(void)
431 cp = strdup(GetCommandLineA());
432 if (!AreFileApisANSI())
434 return ((unsigned char *)cp);