msg_sys.def revision 1.9 1 /* $NetBSD: msg_sys.def,v 1.9 1999/06/23 17:42:11 cgd Exp $ */
2
3 /*
4 * Copyright 1997 Piermont Information Systems Inc.
5 * All rights reserved.
6 *
7 * Written by Philip A. Nelson for Piermont Information Systems Inc.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software develooped for the NetBSD Project by
20 * Piermont Information Systems Inc.
21 * 4. The name of Piermont Information Systems Inc. may not be used to endorse
22 * or promote products derived from this software without specific prior
23 * written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
29 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
35 * THE POSSIBILITY OF SUCH DAMAGE.
36 *
37 */
38
39 static WINDOW *msg_win = NULL;
40 static char *cbuffer;
41 static size_t cbuffersize;
42 static int do_echo = 1;
43
44 #if defined(DYNAMIC_MESSAGE_LAYOUT)
45 static int last_i_was_nl, last_i_was_space;
46 static int last_o_was_punct, last_o_was_space;
47 #endif /* defined(DYNAMIC_MESSAGE_LAYOUT) */
48
49
50 /* Routines */
51
52 void msg_beep (void)
53 {
54 fprintf (stderr, "\a");
55 }
56
57 int msg_window(WINDOW *window)
58 {
59 size_t ncbuffersize;
60 char *ncbuffer;
61
62 msg_win = window;
63
64 ncbuffersize = getmaxx(window) * getmaxy(window) + 1;
65 if (ncbuffersize > cbuffersize) {
66 ncbuffer = malloc(ncbuffersize);
67 if (ncbuffer == NULL)
68 return 1;
69 if (cbuffer != NULL)
70 free(cbuffer);
71 cbuffer = ncbuffer;
72 cbuffersize = ncbuffersize;
73 }
74 return 0;
75 }
76
77 char *msg_string (int msg_no)
78 {
79 return msg_list[msg_no];
80 }
81
82 void msg_clear(void)
83 {
84 wclear (msg_win);
85 wrefresh (msg_win);
86 #if defined(DYNAMIC_MESSAGE_LAYOUT)
87 last_o_was_punct = 0;
88 last_o_was_space = 1;
89 #endif /* defined(DYNAMIC_MESSAGE_LAYOUT) */
90 }
91
92 void msg_standout(void)
93 {
94 wstandout(msg_win);
95 }
96
97 void msg_standend(void)
98 {
99 wstandend(msg_win);
100 }
101
102 int msg_vprintf (char *fmt, va_list ap)
103 {
104 #if defined(DYNAMIC_MESSAGE_LAYOUT)
105 const char *wstart, *afterw;
106 int wordlen, nspaces;
107 #endif /* defined(DYNAMIC_MESSAGE_LAYOUT) */
108 int ret;
109
110 ret = vsnprintf (cbuffer, cbuffersize, fmt, ap);
111
112 #if defined(DYNAMIC_MESSAGE_LAYOUT)
113 for (wstart = afterw = cbuffer; *wstart; wstart = afterw) {
114
115 /* eat one space, or a whole word of non-spaces */
116 if (isspace(*afterw))
117 afterw++;
118 else
119 while (*afterw && !isspace(*afterw))
120 afterw++;
121
122 /* last was an nl, this is an nl: paragraph break */
123 /* this is an nl: special formatting necessary */
124 if (*wstart == '\n') {
125 if (last_i_was_nl || last_i_was_space) {
126
127 if (getcurx(msg_win) != 0)
128 waddch(msg_win, '\n');
129 if (last_i_was_nl) {
130 /* last was an nl: paragraph break */
131 waddch(msg_win, '\n');
132 } else {
133 /* last was space: line break */
134 }
135 last_o_was_punct = 0;
136 last_o_was_space = 1;
137 } else {
138 /* last_o_was_punct unchanged */
139 /* last_o_was_space unchanged */
140 }
141 last_i_was_space = 1;
142 last_i_was_nl = 1;
143 continue;
144 }
145
146 /* this is a tab: special formatting necessary. */
147 if (*wstart == '\t') {
148 if (last_i_was_nl) {
149 /* last was an nl: list indent */
150 if (getcurx(msg_win) != 0)
151 waddch(msg_win, '\n');
152 } else {
153 /* last was not an nl: columns */
154 }
155 waddch(msg_win, '\t');
156 last_i_was_nl = 0;
157 last_i_was_space = 1;
158 last_o_was_punct = 0;
159 last_o_was_space = 1;
160 continue;
161 }
162
163 /* this is a space: ignore it but set flags */
164 last_i_was_nl = 0; /* all newlines handled above */
165 last_i_was_space = isspace(*wstart);
166 if (last_i_was_space)
167 continue;
168
169 /*
170 * we have a real "word," i.e. a sequence of non-space
171 * characters. wstart is now the start of the word,
172 * afterw is the next character after the end.
173 */
174 wordlen = afterw - wstart;
175 nspaces = last_o_was_space ? 0 : (last_o_was_punct ? 2 : 1);
176 if ((getcurx(msg_win) + nspaces + wordlen) >=
177 getmaxx(msg_win) &&
178 wordlen < (getmaxx(msg_win) / 3)) {
179 /* wrap the line */
180 waddch(msg_win, '\n');
181 nspaces = 0;
182 }
183
184 /* output the word, preceded by spaces if necessary */
185 while (nspaces-- > 0)
186 waddch(msg_win, ' ');
187 waddbytes(msg_win, wstart, wordlen);
188
189 /* set up the 'last' state for the next time around */
190 switch (afterw[-1]) {
191 case '.':
192 case '?':
193 case '!':
194 last_o_was_punct = 1;
195 break;
196 default:
197 last_o_was_punct = 0;
198 break;
199 }
200 last_o_was_space = 0;
201
202 /* ... and do it all again! */
203 }
204
205 /* String ended with a newline. They really want a line break. */
206 if (last_i_was_nl) {
207 if (getcurx(msg_win) != 0)
208 waddch(msg_win, '\n');
209 last_o_was_punct = 0;
210 last_o_was_space = 1;
211 }
212 #else /* defined(DYNAMIC_MESSAGE_LAYOUT) */
213 waddstr (msg_win, cbuffer);
214 #endif /* defined(DYNAMIC_MESSAGE_LAYOUT) */
215
216 wrefresh (msg_win);
217 return ret;
218 }
219
220 void msg_display(int msg_no, ...)
221 {
222 va_list ap;
223
224 msg_clear();
225
226 va_start(ap, msg_no);
227 (void)msg_vprintf (msg_list[msg_no], ap);
228 va_end(ap);
229 }
230
231 void msg_display_add(int msg_no, ...)
232 {
233 va_list ap;
234
235 va_start (ap, msg_no);
236 (void)msg_vprintf (msg_list[msg_no], ap);
237 va_end (ap);
238 }
239
240 int msg_printf (char *fmt, ...)
241 {
242 va_list ap;
243 int res;
244
245 msg_clear();
246
247 va_start (ap, fmt);
248 res = msg_vprintf (fmt, ap);
249 va_end (ap);
250 return res;
251 }
252
253 int msg_printf_add (char *fmt, ...)
254 {
255 va_list ap;
256 int res;
257
258 va_start (ap, fmt);
259 res = msg_vprintf (fmt, ap);
260 va_end (ap);
261 return res;
262 }
263
264
265 static void msg_vprompt (char *msg, char *def, char *val, int max_chars,
266 va_list ap)
267 {
268 int ch;
269 int count = 0;
270 int y,x;
271 char *ibuf = alloca(max_chars);
272
273 msg_vprintf (msg, ap);
274 if (def != NULL && *def) {
275 waddstr (msg_win, " [");
276 waddstr (msg_win, def);
277 waddstr (msg_win, "]");
278 }
279 waddstr (msg_win, ": ");
280 wrefresh (msg_win);
281
282 while ((ch = wgetch(msg_win)) != '\n') {
283 if (ch == 0x08 || ch == 0x7f) { /* bs or del */
284 if (count > 0) {
285 count--;
286 if (do_echo) {
287 getyx(msg_win, y, x);
288 x--;
289 wmove(msg_win, y, x);
290 wdelch(msg_win);
291 }
292 } else
293 msg_beep ();
294 } else if (ch == 0x15) { /* ^U; line kill */
295 while (count > 0) {
296 count--;
297 if (do_echo) {
298 getyx(msg_win, y, x);
299 x--;
300 wmove(msg_win, y, x);
301 wdelch(msg_win);
302 }
303 }
304 } else if (ch == 0x17) { /* ^W; word kill */
305 /*
306 * word kill kills the spaces and the 'word'
307 * (non-spaces) last typed. the spaces before
308 * the 'word' aren't killed.
309 */
310 while (count > 0 && isspace(ibuf[count - 1])) {
311 count--;
312 if (do_echo) {
313 getyx(msg_win, y, x);
314 x--;
315 wmove(msg_win, y, x);
316 wdelch(msg_win);
317 }
318 }
319 while (count > 0 && !isspace(ibuf[count - 1])) {
320 count--;
321 if (do_echo) {
322 getyx(msg_win, y, x);
323 x--;
324 wmove(msg_win, y, x);
325 wdelch(msg_win);
326 }
327 }
328 } else if (count < (max_chars - 1) && isprint(ch)) {
329 if (do_echo)
330 waddch (msg_win, ch);
331 ibuf[count++] = ch;
332 } else
333 msg_beep ();
334 if (do_echo)
335 wrefresh(msg_win);
336 }
337 if (do_echo) {
338 waddch(msg_win, '\n');
339 #if defined(DYNAMIC_MESSAGE_LAYOUT)
340 last_o_was_punct = 0;
341 last_o_was_space = 1;
342 #endif /* defined(DYNAMIC_MESSAGE_LAYOUT) */
343 }
344
345 /* copy the appropriate string to the output */
346 if (count != 0) {
347 ibuf[count] = '\0';
348 strcpy(val, ibuf); /* size known to be OK */
349 } else if (def != NULL && val != def) {
350 strncpy(val, def, max_chars);
351 val[max_chars - 1] = '\0';
352 }
353 }
354
355 void msg_prompt_addstr (char *fmt, char *def, char *val, int max_chars, ...)
356 {
357 va_list ap;
358
359 va_start (ap, max_chars);
360 msg_vprompt (fmt, def, val, max_chars, ap);
361 va_end(ap);
362 }
363
364 void msg_prompt_add (int msg_no, char *def, char *val, int max_chars, ...)
365 {
366 va_list ap;
367
368 va_start (ap, max_chars);
369 msg_vprompt (msg_list[msg_no], def, val, max_chars, ap);
370 va_end(ap);
371 }
372
373 void msg_prompt_str (char *msg, char *def, char *val, int max_chars, ...)
374 {
375 va_list ap;
376
377 msg_clear();
378
379 va_start (ap, max_chars);
380 msg_vprompt (msg, def, val, max_chars, ap);
381 va_end (ap);
382 }
383
384 void msg_prompt (int msg_no, char *def, char *val, int max_chars, ...)
385 {
386 va_list ap;
387
388 msg_clear();
389
390 va_start (ap, max_chars);
391 msg_vprompt (msg_list[msg_no], def, val, max_chars, ap);
392 va_end (ap);
393 }
394
395 void msg_noecho()
396 {
397 do_echo = 0;
398 }
399
400 void msg_echo()
401 {
402 do_echo = 1;
403 }
404