hack.topl.c revision 1.6 1 1.6 wiz /* $NetBSD: hack.topl.c,v 1.6 2002/05/26 00:12:12 wiz Exp $ */
2 1.4 christos
3 1.2 mycroft /*
4 1.2 mycroft * Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985.
5 1.2 mycroft */
6 1.2 mycroft
7 1.4 christos #include <sys/cdefs.h>
8 1.2 mycroft #ifndef lint
9 1.6 wiz __RCSID("$NetBSD: hack.topl.c,v 1.6 2002/05/26 00:12:12 wiz Exp $");
10 1.4 christos #endif /* not lint */
11 1.1 cgd
12 1.4 christos #include <stdlib.h>
13 1.1 cgd #include "hack.h"
14 1.4 christos #include "extern.h"
15 1.1 cgd
16 1.4 christos char toplines[BUFSZ];
17 1.4 christos xchar tlx, tly; /* set by pline; used by addtopl */
18 1.1 cgd
19 1.1 cgd struct topl {
20 1.4 christos struct topl *next_topl;
21 1.4 christos char *topl_text;
22 1.4 christos } *old_toplines, *last_redone_topl;
23 1.1 cgd #define OTLMAX 20 /* max nr of old toplines remembered */
24 1.1 cgd
25 1.4 christos int
26 1.4 christos doredotopl()
27 1.4 christos {
28 1.4 christos if (last_redone_topl)
29 1.1 cgd last_redone_topl = last_redone_topl->next_topl;
30 1.4 christos if (!last_redone_topl)
31 1.1 cgd last_redone_topl = old_toplines;
32 1.4 christos if (last_redone_topl) {
33 1.1 cgd (void) strcpy(toplines, last_redone_topl->topl_text);
34 1.1 cgd }
35 1.1 cgd redotoplin();
36 1.4 christos return (0);
37 1.1 cgd }
38 1.1 cgd
39 1.4 christos void
40 1.4 christos redotoplin()
41 1.4 christos {
42 1.1 cgd home();
43 1.4 christos if (strchr(toplines, '\n'))
44 1.4 christos cl_end();
45 1.1 cgd putstr(toplines);
46 1.1 cgd cl_end();
47 1.1 cgd tlx = curx;
48 1.1 cgd tly = cury;
49 1.1 cgd flags.toplin = 1;
50 1.4 christos if (tly > 1)
51 1.1 cgd more();
52 1.1 cgd }
53 1.1 cgd
54 1.4 christos void
55 1.4 christos remember_topl()
56 1.4 christos {
57 1.4 christos struct topl *tl;
58 1.4 christos int cnt = OTLMAX;
59 1.4 christos if (last_redone_topl &&
60 1.4 christos !strcmp(toplines, last_redone_topl->topl_text))
61 1.4 christos return;
62 1.4 christos if (old_toplines &&
63 1.4 christos !strcmp(toplines, old_toplines->topl_text))
64 1.4 christos return;
65 1.1 cgd last_redone_topl = 0;
66 1.1 cgd tl = (struct topl *)
67 1.4 christos alloc((unsigned) (strlen(toplines) + sizeof(struct topl) + 1));
68 1.1 cgd tl->next_topl = old_toplines;
69 1.4 christos tl->topl_text = (char *) (tl + 1);
70 1.1 cgd (void) strcpy(tl->topl_text, toplines);
71 1.1 cgd old_toplines = tl;
72 1.4 christos while (cnt && tl) {
73 1.1 cgd cnt--;
74 1.1 cgd tl = tl->next_topl;
75 1.1 cgd }
76 1.4 christos if (tl && tl->next_topl) {
77 1.1 cgd free((char *) tl->next_topl);
78 1.1 cgd tl->next_topl = 0;
79 1.1 cgd }
80 1.1 cgd }
81 1.1 cgd
82 1.4 christos void
83 1.4 christos addtopl(s)
84 1.5 jsm const char *s;
85 1.4 christos {
86 1.4 christos curs(tlx, tly);
87 1.4 christos if (tlx + strlen(s) > CO)
88 1.4 christos putsym('\n');
89 1.1 cgd putstr(s);
90 1.1 cgd tlx = curx;
91 1.1 cgd tly = cury;
92 1.1 cgd flags.toplin = 1;
93 1.1 cgd }
94 1.1 cgd
95 1.4 christos void
96 1.1 cgd xmore(s)
97 1.5 jsm const char *s; /* allowed chars besides space/return */
98 1.1 cgd {
99 1.4 christos if (flags.toplin) {
100 1.1 cgd curs(tlx, tly);
101 1.4 christos if (tlx + 8 > CO)
102 1.4 christos putsym('\n'), tly++;
103 1.1 cgd }
104 1.4 christos if (flags.standout)
105 1.1 cgd standoutbeg();
106 1.1 cgd putstr("--More--");
107 1.4 christos if (flags.standout)
108 1.1 cgd standoutend();
109 1.1 cgd
110 1.1 cgd xwaitforspace(s);
111 1.4 christos if (flags.toplin && tly > 1) {
112 1.1 cgd home();
113 1.1 cgd cl_end();
114 1.4 christos docorner(1, tly - 1);
115 1.1 cgd }
116 1.1 cgd flags.toplin = 0;
117 1.1 cgd }
118 1.1 cgd
119 1.4 christos void
120 1.4 christos more()
121 1.4 christos {
122 1.1 cgd xmore("");
123 1.1 cgd }
124 1.1 cgd
125 1.4 christos void
126 1.1 cgd cmore(s)
127 1.5 jsm const char *s;
128 1.1 cgd {
129 1.1 cgd xmore(s);
130 1.1 cgd }
131 1.1 cgd
132 1.4 christos void
133 1.4 christos clrlin()
134 1.4 christos {
135 1.4 christos if (flags.toplin) {
136 1.1 cgd home();
137 1.1 cgd cl_end();
138 1.4 christos if (tly > 1)
139 1.4 christos docorner(1, tly - 1);
140 1.1 cgd remember_topl();
141 1.1 cgd }
142 1.1 cgd flags.toplin = 0;
143 1.1 cgd }
144 1.1 cgd
145 1.4 christos void
146 1.4 christos pline(const char *fmt, ...)
147 1.4 christos {
148 1.4 christos va_list ap;
149 1.6 wiz
150 1.4 christos va_start(ap, fmt);
151 1.4 christos vpline(fmt, ap);
152 1.4 christos va_end(ap);
153 1.4 christos }
154 1.4 christos
155 1.4 christos void
156 1.4 christos vpline(line, ap)
157 1.4 christos const char *line;
158 1.4 christos va_list ap;
159 1.4 christos {
160 1.4 christos char pbuf[BUFSZ];
161 1.4 christos char *bp = pbuf, *tl;
162 1.4 christos int n, n0;
163 1.4 christos
164 1.4 christos if (!line || !*line)
165 1.4 christos return;
166 1.4 christos if (!strchr(line, '%'))
167 1.4 christos (void) strcpy(pbuf, line);
168 1.4 christos else
169 1.4 christos (void) vsprintf(pbuf, line, ap);
170 1.4 christos if (flags.toplin == 1 && !strcmp(pbuf, toplines))
171 1.4 christos return;
172 1.4 christos nscr(); /* %% */
173 1.1 cgd
174 1.1 cgd /* If there is room on the line, print message on same line */
175 1.1 cgd /* But messages like "You die..." deserve their own line */
176 1.1 cgd n0 = strlen(bp);
177 1.4 christos if (flags.toplin == 1 && tly == 1 &&
178 1.4 christos n0 + strlen(toplines) + 3 < CO - 8 && /* leave room for
179 1.4 christos * --More-- */
180 1.1 cgd strncmp(bp, "You ", 4)) {
181 1.1 cgd (void) strcat(toplines, " ");
182 1.1 cgd (void) strcat(toplines, bp);
183 1.1 cgd tlx += 2;
184 1.1 cgd addtopl(bp);
185 1.1 cgd return;
186 1.1 cgd }
187 1.4 christos if (flags.toplin == 1)
188 1.4 christos more();
189 1.1 cgd remember_topl();
190 1.1 cgd toplines[0] = 0;
191 1.4 christos while (n0) {
192 1.4 christos if (n0 >= CO) {
193 1.1 cgd /* look for appropriate cut point */
194 1.1 cgd n0 = 0;
195 1.4 christos for (n = 0; n < CO; n++)
196 1.4 christos if (bp[n] == ' ')
197 1.4 christos n0 = n;
198 1.4 christos if (!n0)
199 1.4 christos for (n = 0; n < CO - 1; n++)
200 1.4 christos if (!letter(bp[n]))
201 1.4 christos n0 = n;
202 1.4 christos if (!n0)
203 1.4 christos n0 = CO - 2;
204 1.1 cgd }
205 1.1 cgd (void) strncpy((tl = eos(toplines)), bp, n0);
206 1.1 cgd tl[n0] = 0;
207 1.1 cgd bp += n0;
208 1.1 cgd
209 1.1 cgd /* remove trailing spaces, but leave one */
210 1.4 christos while (n0 > 1 && tl[n0 - 1] == ' ' && tl[n0 - 2] == ' ')
211 1.1 cgd tl[--n0] = 0;
212 1.1 cgd
213 1.1 cgd n0 = strlen(bp);
214 1.4 christos if (n0 && tl[0])
215 1.4 christos (void) strcat(tl, "\n");
216 1.1 cgd }
217 1.1 cgd redotoplin();
218 1.1 cgd }
219 1.1 cgd
220 1.4 christos void
221 1.4 christos putsym(c)
222 1.4 christos char c;
223 1.4 christos {
224 1.4 christos switch (c) {
225 1.1 cgd case '\b':
226 1.1 cgd backsp();
227 1.1 cgd return;
228 1.1 cgd case '\n':
229 1.1 cgd curx = 1;
230 1.1 cgd cury++;
231 1.4 christos if (cury > tly)
232 1.4 christos tly = cury;
233 1.1 cgd break;
234 1.1 cgd default:
235 1.4 christos if (curx == CO)
236 1.1 cgd putsym('\n'); /* 1 <= curx <= CO; avoid CO */
237 1.1 cgd else
238 1.1 cgd curx++;
239 1.1 cgd }
240 1.1 cgd (void) putchar(c);
241 1.1 cgd }
242 1.1 cgd
243 1.4 christos void
244 1.4 christos putstr(s)
245 1.5 jsm const char *s;
246 1.4 christos {
247 1.4 christos while (*s)
248 1.4 christos putsym(*s++);
249 1.1 cgd }
250