|
|
1.1 root 1: /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1984. */
2: /* $Header: b2scr.c,v 1.2 84/07/17 09:51:48 frank Exp $ */
3:
4: /* B screen handling */
5: #include "b.h"
6: #include "b1obj.h"
7: #include "b0con.h" /*for CLEAR_EOF*/
8: #include "b2scr.h"
9: #include "b2fil.h"
10:
11: bool interactive, read_interactive, outeractive, filtered;
12: bool at_nwl= Yes; /*Yes if currently at the start of an output line*/
13: bool Eof, Eof0;
14: FILE *ofile= stdout; bool woa, wnwl;
15: FILE *ifile; /* input file */
16: FILE *sv_ifile; /* copy of ifile for restoring after switching to read unit */
17: value iname= Vnil; /* input name */
18:
19: jmp_buf reading[MAX_NMB_ACT_READS];
20: intlet active_reads;
21:
22: string cmd_prompt= ">>> "; /* commands */
23: string eg_prompt= "?\b"; /* READ EG */
24: string raw_prompt= "?\b"; /* READ RAW */
25: string qn_prompt= "?\b"; /* questions */
26:
27:
28: #define USE_YES_OR_NO \
29: "\r*** Answer with yes or no (or use interrupt to duck the question)\n"
30:
31: #define LAST_CHANCE \
32: "*** This is your last chance. Take it. I really don't know what you want if\n\
33: %s\n\
34: So answer the question\n"
35:
36: #define NO_THEN \
37: "*** Well, I shall assume that your refusal to answer the question means no!\n"
38:
39: Visible bool is_intended(m) string m; {
40: char answer; intlet try, k;
41: if (!interactive) return Yes;
42: if (outeractive) line();
43: fprintf(stderr, "*** %s\n", m);
44: for (try= 1; try<=4; try++){
45: answer= '?';
46: if (outeractive) at_nwl= No;
47: fprintf(stderr, qn_prompt);
48: while ((k= getchar()) != EOF && k != '\n') {
49: if ((k == ' ' || k == '\t'));
50: else if (answer == '?') answer= k;
51: }
52: if (k == EOF) {
53: if (filtered) bye(0); /* Editor has died */
54: CLEAR_EOF;
55: }
56: if (k == EOF && try < 4) {
57: fprintf(stderr, USE_YES_OR_NO);
58: if (outeractive) at_nwl= Yes;
59: } else {
60: if (outeractive && k == '\n') at_nwl= Yes;
61: if (answer == 'y' || answer == 'Y') return Yes;
62: if (answer == 'n' || answer == 'N') return No;
63: if (outeractive) line();
64: fprintf(stderr,
65: try == 1 ? "*** Please answer with yes or no\n" :
66: try == 2 ? "*** Just yes or no, please\n" :
67: try == 3 ? LAST_CHANCE :
68: NO_THEN, m);
69: }
70: } /* end for */
71: return No;
72: }
73:
74: Visible Procedure redirect(of) FILE *of; {
75: ofile= of;
76: if (of == stdout) {
77: outeractive= woa;
78: at_nwl= wnwl;
79: } else {
80: woa= outeractive; outeractive= No;
81: wnwl= at_nwl; at_nwl= Yes;
82: }
83: }
84:
85: Hidden Procedure putch(c) char c; {
86: putc(c, ofile);
87: if (c == '\n') at_nwl= Yes;
88: else at_nwl= No;
89: }
90:
91: Visible Procedure newline() {
92: putch('\n');
93: fflush(stdout);
94: }
95:
96: Visible Procedure line() {
97: if (!at_nwl) newline();
98: }
99:
100: Visible Procedure wri_space() { /* Experiment: no space before outer strings */
101: if (!at_nwl) putch(' ');
102: }
103:
104: Visible Procedure writ(v) value v; {
105: wri(v, Yes, Yes, No);
106: fflush(stdout);
107: }
108:
109: #define Putch_sp() {if (!perm) putch(' ');}
110:
111: bool lwt, last_was_text= No;
112:
113: Visible Procedure wri(v, coll, outer, perm) value v; bool coll, outer, perm; {
114: if (outer && !at_nwl && (!Is_text(v) || !last_was_text)
115: && (!Is_compound(v) || !coll)) putch(' ');
116: lwt= No;
117: if (Is_number(v)) {
118: if (perm) printnum(ofile, v);
119: else {
120: string cp= convnum(v);
121: while(*cp) putch(*cp++);
122: }
123: } else if (Is_text(v)) {
124: value ch; char c;
125: value s= size(v); int k, len;
126: if (large(s)) error("text too long to output");
127: else { len= intval(s); release(s); }
128: lwt= outer;
129: if (!outer) putch('\'');
130: k_Over_len {
131: ch= thof(k+1, v);
132: putch(c= charval(ch));
133: if (!outer && (c == '\'' || c == '`'))
134: putch(c);
135: release(ch);
136: }
137: if (!outer) putch('\'');
138: } else if (Is_compound(v)) {
139: intlet k, len= Nfields(v);
140: outer&= coll;
141: if (!coll) putch('(');
142: k_Overfields {
143: wri(*field(v, k), No, outer, perm);
144: if (!Lastfield(k)) {
145: if (!outer){
146: putch(',');
147: Putch_sp();
148: }
149: }
150: }
151: if (!coll) putch(')');
152: } else if (Is_list(v) || Is_ELT(v)) {
153: value ve; value s= size(v); int k, len;
154: if (large(s)) error("list too big to output");
155: else { len= intval(s); release(s); }
156: putch('{');
157: k_Over_len {
158: wri(ve= thof(k+1, v), No, No, perm);
159: release(ve);
160: if (!Last(k)) {
161: putch(';');
162: Putch_sp();
163: }
164: }
165: putch('}');
166: } else if (Is_table(v)) {
167: value s= size(v); int k, len;
168: if (large(s)) error("table too big to output");
169: else { len= intval(s); release(s); }
170: putch('{');
171: k_Over_len {
172: putch('['); wri(*key(v, k), Yes, No, perm);
173: putch(']'); putch(':'); Putch_sp();
174: wri(*assoc(v, k), No, No, perm);
175: if (!Last(k)) {
176: putch(';');
177: Putch_sp();
178: }
179: }
180: putch('}');
181: } else {
182: if (bugs) { putch('?'); putch(Type(v)); putch('?'); }
183: else syserr("writing value of unknown type");
184: }
185: last_was_text= lwt;
186: }
187:
188: Visible Procedure vs_ifile() {
189: ifile= sv_ifile;
190: }
191:
192: Visible Procedure re_files() {
193: if (interactive && sv_ifile != ifile) {
194: if (ifile != stdin) fclose(ifile);
195: vs_ifile();
196: Eof= Eof0= No;
197: }
198: }
199:
200: Visible Procedure initscr() {
201: read_interactive= f_interactive(stdin) || filtered;
202: outeractive= f_interactive(stdout) || filtered;
203: }
204:
205: Visible Procedure re_screen() {
206: sv_ifile= ifile;
207: interactive= f_interactive(ifile) || (ifile == stdin && filtered);
208: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.