1 /* $MirOS: contrib/code/jupp/main.c,v 1.31 2017/08/08 21:39:28 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
60 extern int mid, dspasis, help, pgamnt, nobackups, lightoff, exask, lines, columns, dopadding, marking, dobeep;
62 extern int idleout; /* Clear to use /dev/tty for screen */
63 extern unsigned char *joeterm;
64 int help = 0; /* Set to have help on when starting */
65 int nonotice = 0; /* Set to prevent copyright notice */
67 unsigned char *exmsg = NULL; /* Message to display when exiting the editor */
69 SCREEN *maint; /* Main edit screen */
71 const char null[] = "";
74 extern unsigned char *cygwin32_cmdline(void);
77 /* Make windows follow cursor */
84 if (w->y != -1 && w->watom->follow && w->object)
85 w->watom->follow(w->object);
86 w = (W *) (w->link.next);
87 } while (w != maint->curwin);
105 if ((wid >= 2 && wid != maint->w) || (hei >= 1 && hei != maint->h)) {
106 nresize(maint->t, wid, hei);
116 if (w->object && w->watom->disp)
117 w->watom->disp(w->object, flg);
120 w = (W *) (w->link.next);
121 } while (w != maint->curwin);
122 cpos(maint->t, maint->curwin->x + maint->curwin->curx, maint->curwin->y + maint->curwin->cury);
126 static int ahead = 0;
127 static int ungot = 0;
128 static int ungotc = 0;
132 if (c != 'C' - '@' && c != 'M' - '@') {
145 if (maint->curwin->watom->what == TYPETW)
148 maint->curwin->notify = &term;
150 while (!leave && (!flg || !term)) {
167 if (!ahead && c == 10)
169 m = dokey(maint->curwin->kbd, c);
170 if (maint->curwin->main && maint->curwin->main != maint->curwin) {
171 int x = maint->curwin->kbd->x;
173 maint->curwin->main->kbd->x = x;
175 maint->curwin->main->kbd->seq[x - 1] = maint->curwin->kbd->seq[x - 1];
187 unsigned char **mainenv;
190 main_init(int argc, char **argv, char **envp, SCRN **np)
204 mainenv = (unsigned char **)envp;
205 run = namprt(argv[0]);
207 if ((s = (unsigned char *)getenv("LINES")) != NULL)
208 sscanf((char *)s, "%d", &lines);
209 if ((s = (unsigned char *)getenv("COLUMNS")) != NULL)
210 sscanf((char *)s, "%d", &columns);
211 if ((s = (unsigned char *)getenv("BAUD")) != NULL)
212 sscanf((char *)s, "%u", &Baud);
213 if (getenv("DOPADDING"))
217 if ((s = (unsigned char *)getenv("JOETERM")) != NULL)
220 if (!(cap = getcap(NULL, 9600, NULL, NULL))) {
221 fprintf(stderr, "Couldn't load termcap/terminfo entry\n");
225 s = (unsigned char *)getenv("HOME");
227 s = vsncpy(NULL, 0, sz(s));
228 s = vsncpy(sv(s), sc("/."));
229 s = vsncpy(sv(s), sv(run));
230 s = vsncpy(sv(s), sc("rc"));
235 unsigned char buf[8];
237 fprintf(stderr, "There were errors in '%s'. Use it anyway?", s);
239 if (fgets((char *)buf, 8, stdin) != NULL &&
240 (buf[0] == 'y' || buf[0] == 'Y'))
247 s = vsncpy(NULL, 0, sz(get_JOERC));
248 s = vsncpy(sv(s), sv(run));
249 s = vsncpy(sv(s), sc("rc"));
254 unsigned char buf[8];
256 fprintf(stderr, "There were errors in '%s'. Use it anyway?", s);
258 if (fgets((char *)buf, 8, stdin) != NULL &&
259 (buf[0] == 'y' || buf[0] == 'Y'))
264 /* Try built-in joerc */
266 s = vsncpy(NULL, 0, sc("*"));
267 s = vsncpy(sv(s), sv(run));
268 s = vsncpy(sv(s), sc("rc"));
273 unsigned char buf[8];
275 fprintf(stderr, "There were errors in '%s'. Use it anyway?", s);
277 if (fgets((char *)buf, 8, stdin) != NULL &&
278 (buf[0] == 'y' || buf[0] == 'Y'))
282 fprintf(stderr, "Couldn't open '%s'\n", s);
287 for (c = 1; argv[c]; ++c) {
288 if (argv[c][0] == '-') {
290 if (!strcmp(argv[c], "-CYGhack")) {
291 s = cygwin32_cmdline();
292 s = strstr(s, "-CYGhack");
294 s += /* strlen("-CYGhack") */ 8;
295 while (*s == ' ' || *s == '\t')
304 switch (glopt(argv[c] + 1, argv[c + 1], NULL, 1)) {
306 fprintf(stderr, "Unknown option '%s'\n", argv[c]);
319 if (idleout && (!isatty(0) || !isatty(1)))
323 if (!(n = nopen(cap)))
328 for (c = 1, backopt = 0; argv[c]; ++c)
329 if (argv[c][0] == '+' && argv[c][1]) {
332 } else if (argv[c][0] == '-' && argv[c][1]) {
335 if (glopt(argv[c] + 1, argv[c + 1], NULL, 0) == 2)
338 B *b = bfind(argv[c]);
342 if (!orphan || !opened) {
343 bw = wmktw(maint, b);
345 msgnwt(bw->parent, msgs[-er]);
351 bw->o.readonly = bw->b->rdonly;
353 unsigned char *old_context;
355 old_context = bw->o.context;
356 while (backopt != c) {
357 if (argv[backopt][0] == '+') {
358 sscanf((char *)(argv[backopt] + 1), "%ld", &lnum);
361 if (glopt(argv[backopt] + 1, argv[backopt + 1], &bw->o, 0) == 2)
368 if (old_context != bw->o.context) {
370 rmkbd(bw->parent->kbd);
372 mkkbd(kmap_getcontext(bw->o.context, 1));
376 bw->b->rdonly = bw->o.readonly;
377 if (!opened || opened == (void *)&opened)
379 maint->curwin = bw->parent;
380 if (er == -1 && bw->o.mnew)
382 if (er == 0 && bw->o.mold)
384 maint->curwin = opened;
386 pline(bw->cursor, lnum - 1);
389 opened = (void *)&opened;
393 maint->curwin = opened == (void *)&opened ? NULL : opened;
401 BW *bw = wmktw(maint, bfind(US ""));
406 maint->curwin = maint->topwin;
412 joe_snprintf_4((char *)msgbuf,JOE_MSGBUFSIZE,
413 "\\i[ Joe's Own Editor v" VERSION
414 " | %s | %s " JUPP_IS_COPYRIGHT_C_BY " ]%s%s\\i",
415 locale_map->name, locale_map->type ? "©" : "(c)",
416 fdefault.hmsg ? " " : "",
417 fdefault.hmsg ? fdefault.hmsg : "");
418 msgnw(((BASE *)lastw(maint)->object)->parent, msgbuf);
428 main(int argc, char **argv, char **envp)
432 if ((main_rv = main_init(argc, argv, envp, &n)))
439 fprintf(stderr, "\n%s\n", exmsg);