Debian Codegraph @

extend.c

raw
1 /**************************************************************************
2 * This program is Copyright (C) 1986-2002 by Jonathan Payne. JOVE is *
3 * provided by Jonathan and Jovehacks without charge and without *
4 * warranty. You may copy, modify, and/or distribute JOVE, provided that *
5 * this notice is included in all the source files and documentation. *
6 **************************************************************************/
7
8 #include "jove.h"
9 #include "fp.h"
10 #include "jctype.h"
11 #include "chars.h"
12 #include "commands.h"
13 #include "disp.h"
14 #include "re.h"
15 #include "ask.h"
16 #include "extend.h"
17 #include "fmt.h"
18 #include "insert.h"
19 #include "move.h"
20 #include "proc.h"
21 #include "vars.h"
22 #include "macros.h"
23
24 #ifdef MAC
25 # include "mac.h"
26 #endif
27
28 private void
29 DefAutoExec(const data_obj *(*)(const char *));
30
31 int InJoverc = 0;
32
33 /* Auto execute code */
34
35 #define NEXECS 20
36
37 private struct AutoExec {
38 const char *a_pattern;
39 const data_obj *a_cmd;
40 int a_arg_state;
41 long a_arg_count;
42 } AutoExecs[NEXECS]; /* must be initialized by system to 0 */
43
44 private int ExecIndex = 0;
45
46 /* Command auto-execute. */
47
48 void
49 CAutoExec(void)
50 {
51 DefAutoExec(findcom);
52 }
53
54 /* Macro auto-execute. */
55
56 void
57 MAutoExec(void)
58 {
59 DefAutoExec(findmac);
60 }
61
62 private void
63 DefAutoExec(const data_obj *(*proc)(const char *))
64 {
65 const data_obj *d = (*proc)(ProcFmt);
66 const char *pattern;
67 struct AutoExec *p;
68
69 pattern = do_ask("\r\n", NULL_ASK_EXT, (char *) NULL, ": %f %s ", d->Name);
70 for (p = AutoExecs; p != &AutoExecs[ExecIndex]; p++)
71 if (p->a_cmd == d
72 && ((pattern == NULL || p->a_pattern == NULL)?
73 (pattern == p->a_pattern) : (strcmp(pattern, p->a_pattern) == 0))
74 && p->a_arg_state == arg_state
75 && p->a_arg_count == arg_count)
76 return; /* eliminate duplicates */
77
78 if (ExecIndex >= NEXECS) {
79 complain("Too many auto-executes, max %d.", NEXECS);
80 /* NOTREACHED */
81 }
82 p->a_pattern = copystr(pattern);
83 p->a_cmd = d;
84 p->a_arg_state = arg_state;
85 p->a_arg_count = arg_count;
86 ExecIndex += 1;
87 }
88
89 /* DoAutoExec: NEW and OLD are file names, and if NEW and OLD aren't the
90 * same kind of file (i.e., match the same pattern) or OLD is NULL and it
91 * matches, OR if the pattern is NULL (none was specified) then, we execute
92 * the command associated with that kind of file.
93 */
94 void
95 DoAutoExec(char *new, char *old)
96 {
97 struct AutoExec *p;
98
99 for (p = AutoExecs; p != &AutoExecs[ExecIndex]; p++) {
100 if (p->a_pattern == NULL
101 || ((new != NULL && LookingAt(p->a_pattern, new, 0))
102 && !(old != NULL && LookingAt(p->a_pattern, old, 0))))
103 {
104 arg_state = p->a_arg_state;
105 arg_count = p->a_arg_count;
106 ExecCmd(p->a_cmd);
107 clr_arg_value();
108 }
109 }
110 }
111
112 ZXchar
113 addgetc(void) /* NOTE: can return EOF */
114 {
115 ZXchar c;
116
117 if (!InJoverc) {
118 Asking = YES;
119 AskingWidth = strlen(mesgbuf);
120 c = getch();
121 Asking = NO;
122 add_mess("%p ", c);
123 } else {
124 c = getch();
125 switch (c) {
126 case '\n':
127 c = EOF; /* this isn't part of the sequence */
128 break;
129 case '\\':
130 case '^':
131 c = DecodePair(c, getch());
132 break;
133 }
134 }
135 return c;
136 }
137
138 void
139 Extend(void)
140 {
141 ExecCmd(findcom(": "));
142 }
143
144 /* Read a positive long integer from CP. It must be in base BASE, and
145 * complains if it isn't. If allints, all the characters
146 * in the string must be integers or we return NO (failure); otherwise
147 * we stop reading at the first nondigit and return YES (success).
148 */
149 jbool
150 chr_to_long(const char *cp, int base, jbool allints, long *result)
151 {
152 char c;
153 long value = 0;
154 int sign;
155
156 if ((c = *cp) == '-') {
157 sign = -1;
158 cp += 1;
159 } else
160 sign = 1;
161 while ((c = *cp++) != '\0') {
162 if (!jisdigit(c)) {
163 if (allints)
164 return NO;
165
166 break;
167 }
168 c = c - '0';
169 if (c >= base) {
170 complain("You must specify in base %d.", base);
171 /* NOTREACHED */
172 }
173 value = value * base + c;
174 }
175 *result = value * sign;
176 return YES;
177 }
178
179 /* Read a positive integer from CP. It must be in base BASE, and
180 * complains if it isn't. If allints, all the characters
181 * in the string must be integers or we return NO (failure); otherwise
182 * we stop reading at the first nondigit and return YES (success).
183 */
184 jbool
185 chr_to_int(const char *cp, int base, jbool allints, int *result)
186 {
187 long value;
188 jbool ret = chr_to_long(cp, base, allints, &value);
189
190 if (ret == YES)
191 *result = (int)(value & ~0); /* XXX but no worse than before */
192 return ret;
193 }
194
195 long
196 ask_long(const char *def, const char *prompt, int base)
197 {
198 const char *val = ask(def, prompt);
199 long value;
200
201 if (!chr_to_long(val, base, YES, &value)) {
202 complain("That's not a number!");
203 /* NOTREACHED */
204 }
205 return value;
206 }
207
208 int
209 ask_int(const char *def, const char *prompt, int base)
210 {
211 const char *val = ask(def, prompt);
212 int value = 0; /* avoid gcc complaint */
213
214 if (!chr_to_int(val, base, YES, &value)) {
215 complain("That's not a number!");
216 /* NOTREACHED */
217 }
218 return value;
219 }
220
221 void
222 vpr_aux(const struct variable *vp, char *buf, size_t size)
223 {
224 switch (vp->v_flags & V_TYPEMASK) {
225 case V_INT:
226 case V_WHOLEX:
227 case V_WHOLE:
228 case V_NAT:
229 swritef(buf, size, (vp->v_flags & V_FMODE)? "%03o" : "%d",
230 *((int *) vp->v_value));
231 break;
232
233 case V_BOOL:
234 swritef(buf, size, (*((int *) vp->v_value)) ? "on" : "off");
235 break;
236
237 case V_STRING:
238 case V_FILENAME:
239 swritef(buf, size, "%s", vp->v_value);
240 break;
241
242 case V_CHAR:
243 swritef(buf, size, "%p", *((ZXchar *) vp->v_value));
244 break;
245 }
246 }
247
248 void
249 PrVar(void)
250 {
251 struct variable *vp = (struct variable *) findvar(ProcFmt);
252 char prbuf[MAXCOLS];
253
254 vpr_aux(vp, prbuf, sizeof(prbuf));
255 f_mess(": %f %s => %s", vp->Name, prbuf);
256 stickymsg = YES;
257 }
258
259 void
260 InsVar(void)
261 {
262 struct variable *vp = (struct variable *) findvar(ProcFmt);
263 char prbuf[MAXCOLS];
264
265 vpr_aux(vp, prbuf, sizeof(prbuf));
266 ins_str(prbuf);
267 stickymsg = YES;
268 }
269
270 void
271 vset_aux(const struct variable *vp, char *prompt)
272 {
273 if (vp->v_flags & V_READONLY)
274 complain("[cannot set readonly variable %s]", vp->Name);
275 switch (vp->v_flags & V_TYPEMASK) {
276 case V_INT:
277 case V_WHOLEX:
278 case V_WHOLE:
279 case V_NAT:
280 {
281 char def[30];
282 static const int lwbt[] = {
283 ~0, /* V_INT -- I hope we are two's complement */
284 -1, /* V_WHOLEX */
285 0, /* V_WHOLE */
286 1, /* V_NAT */
287 };
288 int
289 val,
290 lwb = lwbt[(vp->v_flags & V_TYPEMASK) - V_INT];
291
292 vpr_aux(vp, def, sizeof(def));
293 val = ask_int(def, prompt, (vp->v_flags & V_FMODE)? 8 : 10);
294 if (val < lwb) {
295 complain("[%s must not be less than %d]", vp->Name, lwb);
296 /* NOTREACHED */
297 }
298 *((int *) vp->v_value) = val;
299 break;
300 }
301
302 case V_BOOL:
303 {
304 static const char
305 *possible[/*jbool*/] = {"off", "on", NULL };
306 jbool *valp = (jbool *) vp->v_value;
307 int newval = complete(possible, possible[!*valp], prompt,
308 CASEIND | ALLOW_OLD | ALLOW_EMPTY);
309
310 if (newval < 0)
311 newval = !*valp;
312 *valp = newval;
313 #ifdef MAC
314 MarkVar(vp, -1, 0); /* mark the menu item */
315 #endif
316 s_mess("%s%s", prompt, possible[newval]);
317 break;
318 }
319
320 case V_FILENAME:
321 {
322 char fbuf[FILESIZE];
323
324 strcpy((char *) vp->v_value,
325 ask_file(prompt, (char *) vp->v_value, fbuf));
326 break;
327 }
328
329 case V_STRING:
330 {
331 /* do_ask() so you can set string to "" if you so desire */
332 const char *str = do_ask("\r\n", NULL_ASK_EXT,
333 (char *) vp->v_value, prompt);
334
335 jamstrsub((char *) vp->v_value, str == NULL? NullStr : str,
336 vp->v_size);
337 break;
338 }
339
340 case V_CHAR:
341 s_mess(prompt);
342 *((ZXchar *) vp->v_value) = addgetc();
343 break;
344 }
345
346 if (vp->v_flags & V_MODELINE)
347 UpdModLine = YES;
348 if (vp->v_flags & V_CLRSCREEN)
349 ClAndRedraw();
350 if (vp->v_flags & V_TTY_RESET)
351 tty_adjust();
352 #ifdef UNIX
353 if (vp->v_flags & V_UPDFREQ)
354 SetClockAlarm(YES);
355 #endif
356 #if defined(USE_CTYPE) && !defined(NO_SETLOCALE)
357 if (vp->v_flags & V_LOCALE) {
358 locale_adjust();
359 ClAndRedraw();
360 }
361 #endif
362 }
363
364 void
365 SetVar(void)
366 {
367 struct variable *vp = (struct variable *) findvar(ProcFmt);
368 char prompt[128];
369
370 swritef(prompt, sizeof(prompt), ": %f %s ", vp->Name);
371 vset_aux(vp, prompt);
372 }
373
374 /* complete: buffer/command/keymap/macro/variable name completion.
375 *
376 * possible: an array of strings
377 * prompt: the prompt to use
378 * flags: a set of flags:
379 * CASEIND: ignore case
380 * ALLOW_OLD: allow answer listed in possible
381 * ALLOW_NEW: allow answer not listed in possible
382 * ALLOW_EMPTY: allow empty answer
383 *
384 * complete returns an index into possible, or -1 if there is no
385 * right answer there, in which case what the user typed is in Minibuf.
386 *
387 * aux_complete is called by real_ask, on behalf of complete, to handle:
388 *
389 * ? Typeout possible completions (does not change the answer)
390 *
391 * TAB Extend answer as much as possible (this might actually
392 * involve shrinking the answer until it is the prefix of
393 * at least one Possible; if so, error).
394 *
395 * SP If answer is complete (i.e. matches a whole entry in Possible),
396 * accept it. Otherwise, extend answer as much as possible;
397 * if the result is unique, and we didn't shrink the answer,
398 * accept it.
399 *
400 * CR or NL If ALLOW_NEW and answer non-empty: accept the answer.
401 * If ALLOW_EMPTY and answer empty: accept the answer.
402 * If ALLOW_OLD and answer is complete: accept it.
403 * If ALLOW_OLD and answer is prefix of a unique Possible: accept that Possible.
404 * If ALLOW_INDEX and answer is a numeral that is a legitimate
405 * index into possible, accept as that possible.
406 * Otherwise: error
407 *
408 * If we are InJoverc, we cannot be interactive, so ? is forbidden
409 * and all the others are treated as CR; an error is fatal.
410 *
411 * The following file-static variables smuggle values from complete
412 * to aux_complete and vice versa, behind the back of do_ask etc.
413 * This could be nicer if C supported nested procedures (closures);
414 * perhaps we should simulate them.
415 */
416
417 private const char *const *Possible; /* possible arg of complete */
418 private int
419 comp_flags, /* flags arg of complete */
420 comp_value; /* return value for complete; set by aux_complete */
421
422 private jbool
423 aux_complete(ZXchar c); /* needed to comfort dumb MS Visual C */
424
425 private jbool
426 aux_complete(ZXchar c)
427 {
428 if (comp_flags & CASEIND) {
429 char lc;
430 char *lp;
431
432 for (lp = linebuf; (lc = *lp) != '\0'; lp++)
433 *lp = CharDowncase(lc);
434 }
435 if (c == '?') {
436 int i;
437 size_t len = strlen(linebuf);
438
439 if (InJoverc) {
440 complain("[invalid `?']");
441 /* NOTREACHED */
442 }
443 /* kludge: in case we're using UseBuffers, in which case
444 * linebuf gets written all over (but restored by TOstop/TOabort)
445 */
446 strcpy(Minibuf, linebuf);
447 TOstart("Completion"); /* for now ... */
448 for (i = 0; Possible[i] != NULL && !TOabort; i++) {
449 if ((comp_flags & ALLOW_INDEX) && len == 0)
450 Typeout("%2d %s", i+1, Possible[i]);
451 else if (strncmp(Possible[i], Minibuf, len) == 0)
452 Typeout("%s", Possible[i]);
453 }
454
455 TOstop();
456 } else {
457 /* let's do some completion! */
458 int
459 i,
460 numeral = 0, /* avoid gcc complaint */
461 len = strlen(linebuf),
462 minmatch = 1000, /* init with dummy to placate picky compilers */
463 maxmatch = 0,
464 numfound = 0,
465 lastmatch = -1; /* init with dummy to placate picky compilers */
466
467 if (InJoverc || c == '\n')
468 c = '\r'; /* NL is synonym for CR; InJoverc, all are treated as CR */
469 for (i = 0; Possible[i] != NULL; i++) {
470 int this_len = *Possible[i] == *linebuf
471 ? numcomp(Possible[i], linebuf) : 0;
472
473 if (maxmatch < this_len)
474 maxmatch = this_len;
475 if (this_len >= len) {
476 if (Possible[i][len] == '\0' && c != '\t') {
477 /* an exact match */
478 if (comp_flags & ALLOW_OLD) {
479 comp_value = i; /* good: done */
480 return NO;
481 } else {
482 if (InJoverc) {
483 complain("[%s already exists]");
484 /* NOTREACHED */
485 }
486 add_mess(" [already exists]");
487 SitFor(7);
488 return YES;
489 }
490 }
491 minmatch = numfound == 0
492 ? (int)strlen(Possible[i])
493 : jmin(minmatch, numcomp(Possible[lastmatch], Possible[i]));
494 numfound += 1;
495 lastmatch = i;
496 }
497 }
498
499 if (c == '\r' && len > 0
500 && (comp_flags & ALLOW_INDEX)
501 && chr_to_int(linebuf, 10, YES, &numeral)
502 && 0 < numeral && numeral <= i
503 ) {
504 comp_value = numeral - 1; /* accept as an index into possible */
505 return NO;
506 }
507
508 if (c == '\r' && (comp_flags & (len == 0? ALLOW_EMPTY : ALLOW_NEW))) {
509 comp_value = -1; /* accept as new value (perhaps empty) */
510 return NO;
511 }
512 if (numfound == 1 && c != '\t' && (comp_flags & ALLOW_OLD)) {
513 comp_value = lastmatch;
514 return NO;
515 }
516 if (InJoverc) {
517 complain("[\"%s\" %s]", linebuf,
518 numfound == 0? "unknown" : "ambiguous");
519 /* NOTREACHED */
520 }
521 if (numfound == 0) {
522 /* Unknown: either not ALLOW_NEW (bad) or not CR (not good enough) */
523 add_mess(" [unknown]");
524 SitFor(7);
525 if (maxmatch < len && (comp_flags & ALLOW_NEW) == 0) {
526 linebuf[maxmatch] = '\0';
527 Eol();
528 }
529 } else {
530 /* Ambiguous (or unique, but TAB): extend as much as
531 * possible without precluding any of the ambiguous matches.
532 * Explain if we were expected to be done (CR)
533 * or if we made no progress.
534 */
535 null_ncpy(linebuf, Possible[lastmatch], (size_t) minmatch);
536 modify();
537 makedirty(curline);
538 Eol();
539 if (c == '\r' || minmatch == len) {
540 add_mess(numfound == 1? " [complete]" : " [ambiguous]");
541 SitFor(7);
542 }
543 }
544 }
545 return YES;
546 }
547
548 int
549 complete(const char *const *possible,
550 const char *def, const char *prompt,
551 int flags)
552 {
553 /* protect static "Possible" etc. from being overwritten due to recursion */
554 if (InRealAsk) {
555 complain(NULL);
556 /* NOTREACHED */
557 }
558 Possible = possible;
559 comp_flags = flags;
560 (void) do_ask("\r\n \t?", aux_complete, def, prompt);
561 return comp_value;
562 }
563
564 void
565 Source(void)
566 {
567 char
568 fnamebuf[FILESIZE];
569 jbool silence = is_an_arg();
570
571 PathCat(fnamebuf, sizeof(fnamebuf), HomeDir,
572 #ifdef MSFILESYSTEM
573 "jove.rc"
574 #else
575 ".joverc"
576 #endif
577 );
578 (void) ask_file(NULL, fnamebuf, fnamebuf);
579 if (!joverc(fnamebuf) && !silence) {
580 message(IOerr("read", fnamebuf));
581 complain(NULL);
582 /* NOTREACHED */
583 }
584 }
585
586 /* TODO: Make this unsigned long when we dump support for pre-ANSI C */
587 /* calculate percentage without float and no overflow */
588 private int
589 calc_percent(long a, long b)
590 {
591 int v;
592 if (b == 0) {
593 v = 100;
594 } else if (a > (~(1L << ((sizeof(long)*CHAR_BIT)-1)))/100) {
595 v = (int) (a / (b / 100));
596 } else {
597 v = (int) ((a * 100) / b);
598 }
599 return v;
600 }
601
602 void
603 BufPos(void)
604 {
605 LinePtr lp = curbuf->b_first;
606 long i,
607 dotline = 0;
608 unsigned long dotchar = 0,
609 nchars;
610
611 for (i = nchars = 0; lp != NULL; i++, lp = lp->l_next) {
612 if (lp == curline) {
613 dotchar = nchars + curchar;
614 dotline = i + 1;
615 }
616 nchars += length(lp) + (lp->l_next != NULL); /* include the NL */
617 }
618
619 f_mess("[\"%s\" line %D/%D, char %D/%D (%d%%), cursor = %d/%d]",
620 filename(curbuf), dotline, i, dotchar, nchars,
621 calc_percent(dotchar, nchars),
622 calc_pos(linebuf, curchar),
623 calc_pos(linebuf, (int)strlen(linebuf)));
624 stickymsg = YES;
625 }
626
627 #ifdef SUBSHELL
628
629 private jbool
630 do_if(char *cmd)
631 {
632 char *args[12];
633
634 /* Parse cmd into an argv. Handle various quotes
635 * but not environment variable references.
636 */
637 {
638 char
639 *ip = cmd,
640 *op = cmd,
641 **ap = args;
642
643 for (;;) {
644 while (jiswhite(*ip))
645 ip++;
646 if (*ip == '\0')
647 break;
648
649 if (ap == &args[elemsof(args)]) {
650 complain("Too many args for IF shell command");
651 /* NOTREACHED */
652 }
653 *ap++ = op;
654 for (;;) {
655 char
656 c = *ip++,
657 c2;
658
659 switch (c) {
660 case '\0':
661 ip -= 1;
662 /*FALLTHROUGH*/
663 case ' ':
664 case '\t':
665 break;
666 case '"':
667 case '\'':
668 while ((c2 = *ip++) != c) {
669 switch (c2) {
670 case '\0':
671 complain("Unpaired quote in IF command");
672 /*NOTREACHED*/
673 case '\\':
674 if (c == '"') {
675 c2 = *ip++;
676 if (c2 == '\0') {
677 complain("Misplaced \\ in IF command");
678 /* NOTREACHED */
679 }
680 }
681 /*FALLTHROUGH*/
682 default:
683 *op++ = c2;
684 break;
685 }
686 }
687 continue;
688 case '\\':
689 c = *ip++;
690 if (c == '\0') {
691 complain("Misplaced \\ in IF command");
692 /* NOTREACHED */
693 }
694 /*FALLTHROUGH*/
695 default:
696 *op++ = c;
697 continue;
698 }
699 break;
700 }
701 *op++ = '\0';
702 }
703 *ap = NULL;
704 }
705
706 /* Exec the parsed command */
707 # ifdef UNIX
708 {
709 wait_status_t status;
710
711 switch (ChildPid = fork()) {
712 case -1:
713 complain("[Fork failed for IF: %s]", strerror(errno));
714 /*NOTREACHED*/
715
716 case 0:
717 close(0); /* we want reads to fail */
718 /* close(1); but not writes or ioctl's */
719 /* close(2); */
720 (void) execvp(args[0], args);
721 _exit(errno);
722 /*NOTREACHED*/
723 }
724 dowait(&status);
725 if (!WIFEXITED(status)) {
726 complain("[no status returned from child in IF test]");
727 /* NOTREACHED */
728 }
729 if (WIFSIGNALED(status)) {
730 complain("[IF test terminated by signal %d]", WTERMSIG(status));
731 /* NOTREACHED */
732 }
733 return WEXITSTATUS(status)==0;
734 }
735 # else
736 # ifdef MSDOS_PROCS
737 {
738 int status;
739
740 if ((status = spawnvp(0, args[0], args)) < 0) {
741 complain("[Spawn failed: IF]");
742 /* NOTREACHED */
743 }
744 return (status == 0); /* 0 means successful */
745 }
746 # else /* !MSDOS_PROCS */
747 I do not know how to do this
748 # endif /* !MSDOS_PROCS */
749 # endif /* !UNIX */
750 }
751 #endif /* SUBSHELL */
752
753 private jbool
754 cmdmatch(char *inp, char *verb, char *oppat)
755 {
756 int len = strlen(verb);
757
758 if (caseeqn(inp, verb, (size_t)len) && (inp[len] == '\0' || jiswhite(inp[len]))) {
759 if (!LookingAt(oppat, inp, len)) {
760 complain("[malformed %s]", verb);
761 /* NOTREACHED */
762 }
763 return YES;
764 }
765 return NO;
766 }
767
768 jbool
769 joverc(char *file)
770 {
771 char fbuf[LBSIZE],
772 lbuf[LBSIZE];
773 jmp_buf savejmp;
774 volatile int lnum = 0;
775 File *volatile fp;
776 volatile jbool eof = NO;
777 volatile unsigned int /* bitstrings */
778 finger = 1,
779 skipping = 0,
780 inelse = 0;
781
782
783 /*
784 * joverc is a weird quasi-event loop (reading a line, setting
785 * Inputp to point to the line, doing some special hackery to handle
786 * a number as a numeric arg, invoking Extend() to parse the line
787 * (with getch() taking chars from from Inputp rather than from the
788 * keyboard) and execute the parsed command. If the command was
789 * "execute-macro", then it will push the macro onto the stack, but
790 * in order to run that macro immediately, we must perform a
791 * mac_getc()+dispatch() loop immediately after each command (note
792 * that a running macro might in-turn execute macros by pushing new
793 * ones on the stack, so we must drain the macro stack fully before
794 * proceeding to read the next line in the joverc). However, this
795 * macro dispatch would go badly wrong if it called "source"
796 * recursively into joverc() because the new joverc() loop would
797 * then pop all the remaining strokes of the invoking macro from the
798 * outer joverc(). While we could generalize this by saving and
799 * restoring macro_stack before/after each command in joverc, that
800 * seems excessive for typical use-cases of joverc, so instead, we
801 * just disallow joverc from being invoked from a macro. If someone
802 * encounters a valid use case for this, we can reconsider.
803 */
804 if (in_macro()) {
805 complain("cannot source file %s from within a macro", file);
806 /* NOTREACHED */
807 }
808 fp = open_file(file, fbuf, F_READ, NO);
809 if (fp == NULL)
810 return NO;
811
812 /* Catch any errors, here, and do the right thing with them,
813 * and then restore the error handle to whoever did a setjmp
814 * last.
815 */
816 InJoverc += 1;
817 push_env(savejmp);
818 if (setjmp(mainjmp)) {
819 Buffer *savebuf = curbuf;
820
821 SetBuf(do_select((Window *)NULL, "RC errors"));
822 ins_str(sprint("%s:%d:%s", pr_name(file, YES), lnum, lbuf));
823 ins_str(sprint("\t%s\n", mesgbuf));
824 unmodify();
825 SetBuf(savebuf);
826 Asking = NO;
827 unwind_macro_stack();
828 }
829 while (!eof) {
830 /* This peculiar delayed EOF testing allows the last line to
831 * end without a NL. We add NL later, so we leave room for it.
832 */
833 eof = f_gets(fp, lbuf, sizeof(lbuf)-1);
834 lnum += 1;
835 Inputp = lbuf;
836 while (jiswhite(*Inputp))
837 Inputp += 1; /* skip white space */
838 if (*Inputp == '#' || *Inputp == '\0') {
839 /* a comment */
840 #ifdef SUBSHELL
841 } else if (cmdmatch(Inputp, "if", "[ \t]*\\(.*\\)$")) {
842 char cmd[128];
843
844 finger <<= 1;
845 if (finger == 0) {
846 complain("[`if' nested too deeply]");
847 /* NOTREACHED */
848 }
849 putmatch(1, cmd, sizeof cmd);
850 jdbg("%s:%d:if finger=0x%x skipping=0x%x inelse=0x%x cmd=\"%s\"\n", file, lnum, finger, skipping, inelse, cmd);
851 if (skipping == 0 && !do_if(cmd))
852 skipping |= finger;
853 #endif /* SUBSHELL */
854 #ifndef MAC /* no environment in MacOS */
855 } else if (cmdmatch(Inputp, "ifenv", "\\>[ \t]*\\<\\([^ \t][^ \t]*\\)\\>[ \t]\\(.*\\)$")) {
856 finger <<= 1;
857 if (finger == 0) {
858 complain("[`ifenv' nested too deeply]");
859 /* NOTREACHED */
860 }
861 jdbg("%s:%d:ifenv finger=0x%x skipping=0x%x inelse=0x%x\n", file, lnum, finger, skipping, inelse);
862 if (skipping == 0) {
863 char envname[128],
864 envpat[128],
865 *envval;
866
867 putmatch(1, envname, sizeof envname);
868 putmatch(2, envpat, sizeof envpat);
869 envval = getenv(envname);
870 if (envval==NULL || !LookingAt(envpat, envval, 0))
871 skipping |= finger;
872 }
873 #endif
874 } else if (cmdmatch(Inputp, "ifvar", "\\>[ \t]*\\<\\([^ \t][^ \t]*\\)\\>[ \t]\\(.*\\)$")) {
875 finger <<= 1;
876 if (finger == 0) {
877 complain("[`if' nested too deeply]");
878 /* NOTREACHED */
879 }
880 jdbg("%s:%d:ifvar finger=0x%x skipping=0x%x inelse=0x%x\n", file, lnum, finger, skipping, inelse);
881 if (skipping == 0) {
882 char vname[128],
883 vpat[128],
884 vbuf[128],
885 *val;
886 jbool matched = NO;
887
888 putmatch(1, vname, sizeof vname);
889 putmatch(2, vpat, sizeof vpat);
890 val = (char *)getvar(vname, vbuf, sizeof(vbuf));
891 jdbg("ifvar var \"%s\" => \"%s\" vpat \"%s\"\n", vname, val != NULL? val : "(null)", vpat);
892 if (val==NULL || !(matched = LookingAt(vpat, val, 0)))
893 skipping |= finger;
894 jdbg("ifvar matched=%d skipping=0x%x\n", matched, skipping);
895 }
896 } else if (cmdmatch(Inputp, "else", "[ \t]*$")) {
897 if (finger == 1 || (inelse & finger)) {
898 complain("[Unexpected `else']");
899 /* NOTREACHED */
900 }
901 inelse |= finger;
902 skipping ^= finger;
903 jdbg("%s:%d:else finger=0x%x skipping=0x%x inelse 0x%x\n", file, lnum, finger, skipping, inelse);
904 } else if (cmdmatch(Inputp, "endif", "[ \t]*$")) {
905 if (finger == 1) {
906 complain("[Unexpected `endif']");
907 /* NOTREACHED */
908 }
909 inelse &= ~finger;
910 skipping &= ~finger;
911 finger >>= 1;
912 jdbg("%s:%d:endif finger=0x%x skipping=0x%x inelse=0x%x\n", file, lnum, finger, skipping, inelse);
913 } else if (skipping == 0) {
914 (void) strcat(Inputp, "\n");
915 for (;;) {
916 ZXchar c;
917
918 cmd_sync();
919 this_cmd = OTHER_CMD;
920
921 if ((c = ZXC(*Inputp)) == '-' || jisdigit(c)) {
922 LastKeyStruck = c;
923 Inputp += 1;
924 Digit();
925 } else {
926 Extend(); /* run command, which might call getch, which might make Inputp NULL */
927 }
928 /* get any pending input hiding in the peek buffer */
929 if ((c = peekchar) != EOF) {
930 peekchar = EOF;
931 if (Inputp != NULL && ZXRC(Inputp[-1]) == c) {
932 Inputp -= 1;
933 } else if (!jiswhite(c) && c != '\n' && c != '\0') {
934 complain("[junk at end of line, c = %d %c]", c, jisprint(c)?c:' ');
935 /* NOTREACHED */
936 }
937 }
938
939 while (Inputp && jiswhite(*Inputp))
940 Inputp += 1; /* skip white space */
941
942 if (Inputp == NULL || *Inputp == '\0' || *Inputp == '\n') {
943 Inputp = NULL;
944 /*
945 * processed the line, now finish
946 * running any macros that might
947 * have been executed by the line.
948 */
949 dispatch_macros();
950 break;
951 }
952
953 if (this_cmd != ARG_CMD) {
954 complain("[junk at end of line, this_cmd = %d]", this_cmd);
955 /* NOTREACHED */
956 }
957 }
958 } else {
959 jdprintf("%s:%d:skip finger=0x%x skipping=0x%x inelse=0x%x \"%s\"\n", file, lnum, finger, skipping, inelse, Inputp);
960 }
961 }
962
963 #ifdef SUBSHELL
964 if (finger != 1) {
965 finger = 1;
966 complain("[Missing endif]");
967 /* NOTREACHED */
968 }
969 #endif
970 f_close(fp);
971 pop_env(savejmp);
972 Inputp = NULL;
973 Asking = NO;
974 InJoverc -= 1;
975 return YES;
976 }