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