xref: /netbsd-src/usr.bin/msgc/msg_sys.def (revision f3cfa6f6ce31685c6c4a758bc430e69eb99f50a4)
1/*	$NetBSD: msg_sys.def,v 1.44 2019/03/01 17:02:21 martin 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. The name of Piermont Information Systems Inc. may not be used to endorse
18 *    or promote products derived from this software without specific prior
19 *    written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY PIERMONT INFORMATION SYSTEMS INC. ``AS IS''
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL PIERMONT INFORMATION SYSTEMS INC. BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 * THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 */
34
35static WINDOW *msg_win = NULL;
36static char *cbuffer;
37static size_t cbuffersize;
38
39static int last_i_was_nl, last_i_was_space;
40static int last_o_was_punct, last_o_was_space;
41
42static void	_msg_beep(void);
43static int	_msg_vprintf(int, const char *, va_list);
44#define MSG_PROMPT_ECHO		1
45#define MSG_PROMPT_HIDE_DFLT	2
46static void	_msg_vprompt(const char *, int, const char *, char *,
47			size_t, va_list);
48
49static char *msgmap = MAP_FAILED;
50static size_t msgmapsz;
51static unsigned int msgmapcount;
52
53/* Routines */
54
55static void
56_msg_beep(void)
57{
58
59	fprintf(stderr, "\a");
60}
61
62WINDOW *
63msg_window(WINDOW *window)
64{
65	size_t ncbuffersize;
66	char *ncbuffer;
67	WINDOW *old;
68
69	old = msg_win;
70	if (!window)
71		return old;
72	msg_win = window;
73
74	ncbuffersize = getmaxx(window) * getmaxy(window) + 1;
75	while (ncbuffersize > cbuffersize) {
76		ncbuffer = malloc(ncbuffersize);
77		if (ncbuffer == NULL) {
78			/* we might get truncated messages... */
79			ncbuffersize <<= 1;
80			continue;
81		}
82		if (cbuffer != NULL)
83			free(cbuffer);
84		cbuffer = ncbuffer;
85		cbuffersize = ncbuffersize;
86		break;
87	}
88	last_o_was_punct = 0;
89	last_o_was_space = 1;
90	return old;
91}
92
93int
94msg_file(const char *file)
95{
96	int fd;
97
98	if (msgmap != MAP_FAILED)
99		munmap(msgmap, msgmapsz);
100	msgmap = MAP_FAILED;
101	if (!file)
102		return 0;
103	fd = open(file, O_RDONLY, 0);
104	if (fd == -1)
105		return -1;
106	msgmapsz = lseek(fd, 0, SEEK_END);
107	msgmap = mmap(0, msgmapsz, PROT_READ, MAP_SHARED, fd, 0);
108	close(fd);
109	if (msgmap == MAP_FAILED)
110		return -1;
111	/* check_magic */
112	if (strcmp(msgmap, "MSGTXTS") != 0) {
113		msg_file(NULL);
114		return -1;
115	}
116	msgmapcount = atoi(msgmap + 8);
117	return 0;
118}
119
120const char *
121msg_string(msg msg_no)
122{
123	uintptr_t m = (uintptr_t)msg_no;
124
125	if (m > sizeof msg_list / sizeof msg_list[0])
126		/* guess that we were passed a text string */
127		return msg_no;
128
129	if (msgmap != MAP_FAILED && m != 0 && m <= msgmapcount) {
130		unsigned int offset = atoi(msgmap + 8 + 8 * m);
131		if (offset != 0 && offset < msgmapsz)
132			return msgmap + offset;
133	}
134
135	return msg_list[m];
136}
137
138void
139msg_clear(void)
140{
141
142	wclear(msg_win);
143	last_o_was_punct = 0;
144	last_o_was_space = 1;
145}
146
147void
148msg_standout(void)
149{
150
151	wstandout(msg_win);
152}
153
154void
155msg_standend(void)
156{
157
158	wstandend(msg_win);
159}
160
161static int
162_msg_vprintf(int auto_fill, const char *fmt, va_list ap)
163{
164	const char *wstart, *afterw;
165	int wordlen, nspaces;
166	int ret;
167
168	ret = vsnprintf(cbuffer, cbuffersize, fmt, ap);
169
170	if (!auto_fill) {
171		waddstr(msg_win, cbuffer);
172
173		/*
174		 * nothing is perfect if they flow text after a table,
175		 * but this may be decent.
176		 */
177		last_i_was_nl = last_i_was_space = 1;
178		last_o_was_punct = 0;
179		last_o_was_space = 1;
180		goto out;
181	}
182
183	for (wstart = afterw = cbuffer; *wstart; wstart = afterw) {
184
185		/* eat one space, or a whole word of non-spaces */
186		if (isspace((unsigned char)*afterw))
187			afterw++;
188		else
189			while (*afterw && !isspace((unsigned char)*afterw))
190				afterw++;
191
192		/* this is an nl: special formatting necessary */
193		if (*wstart == '\n') {
194			if (last_i_was_nl || last_i_was_space) {
195
196				if (getcurx(msg_win) != 0)
197					waddch(msg_win, '\n');
198				if (last_i_was_nl) {
199					/* last was an nl: paragraph break */
200					waddch(msg_win, '\n');
201				} else {
202					/* last was space: line break */
203				}
204				last_o_was_punct = 0;
205				last_o_was_space = 1;
206			} else {
207				/* last_o_was_punct unchanged */
208				/* last_o_was_space unchanged */
209			}
210			last_i_was_space = 1;
211			last_i_was_nl = 1;
212			continue;
213		}
214
215		/* this is a tab: special formatting necessary. */
216		if (*wstart == '\t') {
217			if (last_i_was_nl) {
218				/* last was an nl: list indent */
219				if (getcurx(msg_win) != 0)
220					waddch(msg_win, '\n');
221			} else {
222				/* last was not an nl: columns */
223			}
224			waddch(msg_win, '\t');
225			last_i_was_nl = 0;
226			last_i_was_space = 1;
227			last_o_was_punct = 0;
228			last_o_was_space = 1;
229			continue;
230		}
231
232		/* this is a space: ignore it but set flags */
233		last_i_was_nl = 0;	/* all newlines handled above */
234		last_i_was_space = isspace((unsigned char)*wstart);
235		if (last_i_was_space)
236			continue;
237
238		/*
239		 * we have a real "word," i.e. a sequence of non-space
240		 * characters.  wstart is now the start of the word,
241		 * afterw is the next character after the end.
242		 */
243		wordlen = afterw - wstart;
244		nspaces = last_o_was_space ? 0 : (last_o_was_punct ? 2 : 1);
245		if ((getcurx(msg_win) + nspaces + wordlen) >=
246		      getmaxx(msg_win) &&
247		    wordlen < (getmaxx(msg_win) / 3)) {
248			/* wrap the line */
249			waddch(msg_win, '\n');
250			nspaces = 0;
251		}
252
253		/* output the word, preceded by spaces if necessary */
254		while (nspaces-- > 0)
255			waddch(msg_win, ' ');
256		waddbytes(msg_win, wstart, wordlen);
257
258		/* set up the 'last' state for the next time around */
259		switch (afterw[-1]) {
260		case '.':
261		case '?':
262		case '!':
263			last_o_was_punct = 1;
264			break;
265		default:
266			last_o_was_punct = 0;
267			break;
268		}
269		last_o_was_space = 0;
270
271		/* ... and do it all again! */
272	}
273
274	/* String ended with a newline.  They really want a line break. */
275	if (last_i_was_nl) {
276		if (getcurx(msg_win) != 0)
277			waddch(msg_win, '\n');
278		last_o_was_punct = 0;
279		last_o_was_space = 1;
280	}
281
282out:
283	wrefresh(msg_win);
284	return ret;
285}
286
287void
288msg_display(msg msg_no, ...)
289{
290	va_list ap;
291
292	msg_clear();
293
294	va_start(ap, msg_no);
295	(void)_msg_vprintf(1, msg_string(msg_no), ap);
296	va_end(ap);
297}
298
299void
300msg_display_add(msg msg_no, ...)
301{
302	va_list ap;
303
304	va_start(ap, msg_no);
305	(void)_msg_vprintf(1, msg_string(msg_no), ap);
306	va_end(ap);
307}
308
309void
310msg_printf(const char *fmt, ...)
311{
312	va_list ap;
313
314	va_start(ap, fmt);
315	(void)_msg_vprintf(1, fmt, ap);
316	va_end(ap);
317}
318
319static void
320_msg_vprompt(const char *fmt, int flags, const char *def, char *val,
321    size_t val_buf_len, va_list ap)
322{
323	int ch;
324	int len, pos, npos, off;
325	int first;
326	int txt_y, txt_x;
327	char *ibuf;
328	int maxx;
329
330	if (val == NULL || val_buf_len == 0) {
331		/* No answer wanted */
332		val = NULL;
333		val_buf_len = 1;
334	}
335
336	ibuf = malloc(val_buf_len);
337
338	keypad(msg_win, TRUE);
339	_msg_vprintf(0, fmt, ap);
340	ibuf[0] = 0;
341	if (def != NULL && *def) {
342		if (flags & MSG_PROMPT_HIDE_DFLT)
343			strlcpy(ibuf, def, val_buf_len);
344		else {
345			waddstr(msg_win, " [");
346			waddstr(msg_win, def);
347			waddstr(msg_win, "]");
348		}
349	}
350	waddstr(msg_win, ": ");
351	len = strlen(ibuf);
352	pos = len;
353	getyx(msg_win, txt_y, txt_x);
354	maxx = getmaxx(msg_win) - txt_x - 1;
355	off = 0;
356
357	for (first = 1; ; first = 0) {
358
359		if (flags & MSG_PROMPT_ECHO) {
360			/* shift text right as we near the buffer start */
361			if (pos - off < 4)
362				off = pos - 4;
363			/* keep offset to a minimum if we are at the end */
364			if (pos == len)
365				off = pos - maxx;
366			if (off < 0 || len <= maxx)
367				off = 0;
368			/* shift text left as we near the buffer end */
369			npos = pos + 4;
370			if (npos > len)
371				npos = len;
372			if (npos - off > maxx)
373				off = npos - maxx;
374			/* calc. length to display */
375			npos = len - off;
376			if (npos > maxx)
377				npos = maxx;
378			mvwaddnstr(msg_win, txt_y, txt_x, ibuf + off, npos);
379			wclrtoeol(msg_win);
380			if (off != 0)
381				mvwaddstr(msg_win, txt_y, txt_x, "+");
382			wmove(msg_win, txt_y, txt_x + pos - off);
383			wrefresh(msg_win);
384		}
385
386		ch = wgetch(msg_win);
387		if (ch == '\n')
388			break;
389
390		switch (ch) {
391		case KEY_BACKSPACE:
392		case 'h' & 0x1f: case 0x7f:  /* bs or del - delete left */
393			if (first) {
394				/* delete all of default string */
395				len = pos = 0;
396				break;
397			}
398			if (pos > 0) {
399				memmove(ibuf + pos - 1, ibuf + pos, len - pos);
400				len--;
401				pos--;
402			} else
403				_msg_beep();
404			break;
405		case 'l' & 0x1f:
406			endwin();
407			doupdate();
408			break;
409		case 'u' & 0x1f:	/* ^U; line kill */
410			/* kill line */
411			len = pos = 0;
412			break;
413		case 'w' & 0x1f:        /* ^W; word kill */
414			/*
415			 * word kill kills the spaces and the 'word'
416			 * (non-spaces) last typed.  the spaces before
417			 * the 'word' aren't killed.
418			 */
419			npos = pos;
420			while (npos > 0 && isspace((unsigned char)ibuf[npos - 1]))
421				npos--;
422			while (npos > 0 && !isspace((unsigned char)ibuf[npos - 1]))
423				npos--;
424			memmove(ibuf + npos, ibuf + pos, len - pos);
425			len -= pos - npos;
426			pos = npos;
427			break;
428		case KEY_LEFT:
429			if (pos > 0)
430				pos--;
431			break;
432		case KEY_RIGHT:
433			if (len == 0 && pos == 0 && def != NULL) {
434				/* restore default! */
435				strlcpy(ibuf, def, val_buf_len);
436				len = pos = strlen(ibuf);
437				break;
438			}
439			if (pos < len)
440				pos++;
441			break;
442		default:
443			if (len < (int)(val_buf_len - 1) && isprint(ch)) {
444				memmove(ibuf + pos + 1, ibuf + pos, len - pos);
445				ibuf[pos++] = ch;
446				len++;
447			} else
448				_msg_beep();
449			break;
450		}
451	}
452
453	if (flags & MSG_PROMPT_ECHO) {
454		mvwaddch(msg_win, txt_y, txt_x + len - off, '\n');
455		last_o_was_punct = 0;
456		last_o_was_space = 1;
457	}
458
459	if (val != NULL) {
460		/* copy the appropriate string to the output */
461		if (len != 0 || flags & MSG_PROMPT_HIDE_DFLT) {
462			ibuf[len] = '\0';
463			strlcpy(val, ibuf, val_buf_len);
464		} else if (def != NULL && val != def) {
465			strlcpy(val, def, val_buf_len);
466		}
467	}
468	free(ibuf);
469}
470
471void
472msg_prompt(msg msg_no, const char *def, char *val, size_t val_buf_len, ...)
473{
474	va_list ap;
475
476	msg_clear();
477
478	va_start(ap, val_buf_len);
479	_msg_vprompt(msg_string(msg_no), MSG_PROMPT_ECHO,
480		def, val, val_buf_len, ap);
481	va_end(ap);
482}
483
484void
485msg_prompt_win(msg msg_no, int x, int y, int w, int h,
486	const char *def, char *val, size_t val_buf_len, ...)
487{
488	va_list ap;
489	WINDOW *win;
490	WINDOW *svmsg = NULL, *sv_win = NULL; /* XXX -Wuninitialized [many] */
491	int maxx, maxy;
492	int msg_flags = MSG_PROMPT_ECHO | MSG_PROMPT_HIDE_DFLT;
493	const char *np, *ep;
494
495	maxx = getmaxx(msg_win);
496	maxy = getmaxy(msg_win);
497	if (w == 0) {
498		va_start(ap, val_buf_len);
499		w = vsnprintf(NULL, 0, msg_string(msg_no), ap);
500		va_end(ap);
501		if (def != NULL && *def != 0 && w + (int)val_buf_len * 2 < maxx) {
502			w += 2 + strlen(def) + 1;
503			msg_flags &= ~MSG_PROMPT_HIDE_DFLT;
504		}
505		w += 1 + 2 + val_buf_len + 1;
506		if (w > maxx) {
507			if (!(msg_flags & MSG_PROMPT_HIDE_DFLT)) {
508				w -= 2 + strlen(def) + 1;
509				msg_flags |= MSG_PROMPT_HIDE_DFLT;
510			}
511			w = maxx;
512		}
513	} else if (w > 0 && def != NULL && *def != 0) {
514		size_t tl = strlen(def);
515		if (tl + 1 + 2 + val_buf_len + 1 < (unsigned)w)
516			msg_flags &= ~MSG_PROMPT_HIDE_DFLT;
517	}
518
519	if (x == -1)
520		x = (maxx - w) / 2 + 1;
521	if (h < 0) {
522		h = 3;
523		for (np = msg_string(msg_no); (ep = strchr(np, '\n'));
524		    np = ep + 1)
525			h++;
526	}
527	if (h < 3)
528		h = 3;
529	if (y < 3)
530		y = (maxy - h) / 2;
531	if (y + h > maxy)
532		y = maxy - h;
533
534	win = subwin(msg_win, h, w, y, x);
535	if (win == NULL)
536		wprintw(msg_win, "msg_prompt_win: "
537			"newwin(%d, %d, %d, %d) failed\n",
538			h, w, y, x);
539	else {
540		/*
541		 * Save screen contents from under our window
542		 * Due to a mis-feature of NetBSD curses, curscr contains
543		 * the data processed by doupdate() not that by wnoutrefresh().
544		 * We must call doupdate() here to ensure we save the correct
545		 * data.  See PR 26660
546		 */
547		doupdate();
548		sv_win = dupwin(win);
549		if (sv_win)
550			overwrite(curscr, sv_win);
551		wbkgd(win, getbkgd(msg_win));
552		wattrset(win, getattrs(msg_win));
553		box(win, 0, 0);
554		wrefresh(win);
555
556		/* Change message window to be our little box */
557		svmsg = msg_window(subwin(msg_win, h - 2, w - 2, y + 1, x + 1));
558		wbkgd(msg_win, getbkgd(win));
559		wattrset(msg_win, getattrs(win));
560
561		msg_clear();
562	}
563
564	va_start(ap, val_buf_len);
565	_msg_vprompt(msg_string(msg_no), msg_flags, def, val, val_buf_len, ap);
566	va_end(ap);
567
568	if (win != NULL) {
569		wclear(win);
570		if (sv_win) {
571			/* Restore original screen contents */
572			overwrite(sv_win, win);
573			delwin(sv_win);
574		}
575		wnoutrefresh(win);
576		/* Restore normal message window */
577		delwin(msg_window(svmsg));
578		delwin(win);
579	}
580}
581
582void
583msg_prompt_add(msg msg_no, const char *def, char *val, size_t val_buf_len, ...)
584{
585	va_list ap;
586
587	va_start(ap, val_buf_len);
588	_msg_vprompt(msg_string(msg_no), MSG_PROMPT_ECHO, def, val, val_buf_len, ap);
589	va_end(ap);
590}
591
592void
593msg_prompt_noecho(msg msg_no, const char *def, char *val, size_t val_buf_len, ...)
594{
595	va_list ap;
596
597	msg_clear();
598
599	va_start(ap, val_buf_len);
600	_msg_vprompt(msg_string(msg_no), 0, def, val, val_buf_len, ap);
601	va_end(ap);
602}
603
604void
605msg_table_add(msg msg_no, ...)
606{
607	va_list ap;
608
609	va_start(ap, msg_no);
610	(void)_msg_vprintf(0, msg_string(msg_no), ap);
611	va_end(ap);
612}
613
614int
615msg_row(void)
616{
617
618	return getcury(msg_win) + getbegy(msg_win);
619}
620