Home | History | Annotate | Line # | Download | only in msgc
msg_sys.def revision 1.18
      1  1.18  christos /*	$NetBSD: msg_sys.def,v 1.18 2003/01/10 20:01:12 christos 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.18  christos 	return msg_list[(size_t)(intptr_t)msg_no];
     82   1.1      phil }
     83   1.1      phil 
     84   1.1      phil void msg_clear(void)
     85   1.1      phil {
     86   1.1      phil 	wclear (msg_win);
     87   1.1      phil 	wrefresh (msg_win);
     88   1.8       cgd 	last_o_was_punct = 0;
     89   1.8       cgd 	last_o_was_space = 1;
     90   1.1      phil }
     91   1.1      phil 
     92   1.1      phil void msg_standout(void)
     93   1.1      phil {
     94   1.1      phil 	wstandout(msg_win);
     95   1.1      phil }
     96   1.1      phil 
     97   1.1      phil void msg_standend(void)
     98   1.1      phil {
     99   1.1      phil 	wstandend(msg_win);
    100   1.1      phil }
    101   1.1      phil 
    102  1.12       cgd static int
    103  1.16       cgd _msg_vprintf(int auto_fill, const char *fmt, va_list ap)
    104   1.1      phil {
    105   1.8       cgd 	const char *wstart, *afterw;
    106   1.8       cgd 	int wordlen, nspaces;
    107   1.1      phil 	int ret;
    108   1.1      phil 
    109   1.6       cgd 	ret = vsnprintf (cbuffer, cbuffersize, fmt, ap);
    110   1.8       cgd 
    111  1.10       cgd 	if (!auto_fill) {
    112  1.10       cgd 		waddstr(msg_win, cbuffer);
    113  1.10       cgd 
    114  1.10       cgd 		/*
    115  1.10       cgd 		 * nothing is perfect if they flow text after a table,
    116  1.10       cgd 		 * but this may be decent.
    117  1.10       cgd 		 */
    118  1.10       cgd 		last_i_was_nl = last_i_was_space = 1;
    119  1.10       cgd 		last_o_was_punct = 0;
    120  1.10       cgd 		last_o_was_space = 1;
    121  1.10       cgd 		goto out;
    122  1.10       cgd 	}
    123  1.10       cgd 
    124   1.8       cgd 	for (wstart = afterw = cbuffer; *wstart; wstart = afterw) {
    125   1.8       cgd 
    126   1.8       cgd 		/* eat one space, or a whole word of non-spaces */
    127   1.8       cgd 		if (isspace(*afterw))
    128   1.8       cgd 			afterw++;
    129   1.8       cgd 		else
    130   1.8       cgd 			while (*afterw && !isspace(*afterw))
    131   1.8       cgd 				afterw++;
    132   1.8       cgd 
    133   1.8       cgd 		/* this is an nl: special formatting necessary */
    134   1.8       cgd 		if (*wstart == '\n') {
    135   1.8       cgd 			if (last_i_was_nl || last_i_was_space) {
    136   1.8       cgd 
    137   1.8       cgd 				if (getcurx(msg_win) != 0)
    138   1.8       cgd 					waddch(msg_win, '\n');
    139   1.8       cgd 				if (last_i_was_nl) {
    140   1.8       cgd 					/* last was an nl: paragraph break */
    141   1.8       cgd 					waddch(msg_win, '\n');
    142   1.8       cgd 				} else {
    143   1.8       cgd 					/* last was space: line break */
    144   1.8       cgd 				}
    145   1.8       cgd 				last_o_was_punct = 0;
    146   1.8       cgd 				last_o_was_space = 1;
    147   1.8       cgd 			} else {
    148   1.8       cgd 				/* last_o_was_punct unchanged */
    149   1.8       cgd 				/* last_o_was_space unchanged */
    150   1.8       cgd 			}
    151   1.8       cgd 			last_i_was_space = 1;
    152   1.8       cgd 			last_i_was_nl = 1;
    153   1.8       cgd 			continue;
    154   1.8       cgd 		}
    155   1.8       cgd 
    156   1.8       cgd 		/* this is a tab: special formatting necessary. */
    157   1.8       cgd 		if (*wstart == '\t') {
    158   1.8       cgd 			if (last_i_was_nl) {
    159   1.8       cgd 				/* last was an nl: list indent */
    160   1.8       cgd 				if (getcurx(msg_win) != 0)
    161   1.8       cgd 					waddch(msg_win, '\n');
    162   1.8       cgd 			} else {
    163   1.8       cgd 				/* last was not an nl: columns */
    164   1.8       cgd 			}
    165   1.8       cgd 			waddch(msg_win, '\t');
    166   1.8       cgd 			last_i_was_nl = 0;
    167   1.8       cgd 			last_i_was_space = 1;
    168   1.8       cgd 			last_o_was_punct = 0;
    169   1.8       cgd 			last_o_was_space = 1;
    170   1.8       cgd 			continue;
    171   1.8       cgd 		}
    172   1.8       cgd 
    173   1.8       cgd 		/* this is a space: ignore it but set flags */
    174   1.8       cgd 		last_i_was_nl = 0;	/* all newlines handled above */
    175   1.8       cgd 		last_i_was_space = isspace(*wstart);
    176   1.8       cgd 		if (last_i_was_space)
    177   1.8       cgd 			continue;
    178   1.8       cgd 
    179   1.8       cgd 		/*
    180   1.8       cgd 		 * we have a real "word," i.e. a sequence of non-space
    181   1.8       cgd 		 * characters.  wstart is now the start of the word,
    182   1.8       cgd 		 * afterw is the next character after the end.
    183   1.8       cgd 		 */
    184   1.8       cgd 		wordlen = afterw - wstart;
    185   1.8       cgd 		nspaces = last_o_was_space ? 0 : (last_o_was_punct ? 2 : 1);
    186   1.8       cgd 		if ((getcurx(msg_win) + nspaces + wordlen) >=
    187   1.8       cgd 		      getmaxx(msg_win) &&
    188   1.8       cgd 		    wordlen < (getmaxx(msg_win) / 3)) {
    189   1.8       cgd 			/* wrap the line */
    190   1.8       cgd 			waddch(msg_win, '\n');
    191   1.8       cgd 			nspaces = 0;
    192   1.8       cgd 		}
    193   1.8       cgd 
    194   1.8       cgd 		/* output the word, preceded by spaces if necessary */
    195   1.8       cgd 		while (nspaces-- > 0)
    196   1.8       cgd 			waddch(msg_win, ' ');
    197   1.8       cgd 		waddbytes(msg_win, wstart, wordlen);
    198   1.8       cgd 
    199   1.8       cgd 		/* set up the 'last' state for the next time around */
    200   1.8       cgd 		switch (afterw[-1]) {
    201   1.8       cgd 		case '.':
    202   1.8       cgd 		case '?':
    203   1.8       cgd 		case '!':
    204   1.8       cgd 			last_o_was_punct = 1;
    205   1.8       cgd 			break;
    206   1.8       cgd 		default:
    207   1.8       cgd 			last_o_was_punct = 0;
    208   1.8       cgd 			break;
    209   1.8       cgd 		}
    210   1.8       cgd 		last_o_was_space = 0;
    211   1.8       cgd 
    212   1.8       cgd 		/* ... and do it all again! */
    213   1.8       cgd 	}
    214   1.8       cgd 
    215   1.8       cgd 	/* String ended with a newline.  They really want a line break. */
    216   1.8       cgd 	if (last_i_was_nl) {
    217   1.8       cgd 		if (getcurx(msg_win) != 0)
    218   1.8       cgd 			waddch(msg_win, '\n');
    219   1.8       cgd 		last_o_was_punct = 0;
    220   1.8       cgd 		last_o_was_space = 1;
    221   1.8       cgd 	}
    222   1.8       cgd 
    223  1.10       cgd out:
    224   1.1      phil 	wrefresh (msg_win);
    225   1.1      phil 	return ret;
    226   1.1      phil }
    227   1.1      phil 
    228  1.16       cgd void msg_display(msg msg_no, ...)
    229   1.1      phil {
    230   1.1      phil 	va_list ap;
    231   1.1      phil 
    232   1.7       cgd 	msg_clear();
    233   1.7       cgd 
    234   1.1      phil 	va_start(ap, msg_no);
    235  1.16       cgd 	(void)_msg_vprintf(1, msg_string(msg_no), ap);
    236   1.1      phil 	va_end(ap);
    237   1.1      phil }
    238   1.1      phil 
    239  1.16       cgd void msg_display_add(msg msg_no, ...)
    240   1.1      phil {
    241   1.1      phil 	va_list ap;
    242   1.1      phil 
    243   1.1      phil 	va_start (ap, msg_no);
    244  1.16       cgd 	(void)_msg_vprintf(1, msg_string(msg_no), ap);
    245   1.1      phil 	va_end (ap);
    246   1.1      phil }
    247   1.1      phil 
    248   1.1      phil 
    249  1.12       cgd static void
    250  1.16       cgd _msg_vprompt(const char *msg, int do_echo, const char *def, char *val,
    251  1.18  christos     size_t max_chars, va_list ap)
    252   1.1      phil {
    253   1.1      phil 	int ch;
    254   1.1      phil 	int count = 0;
    255   1.1      phil 	int y,x;
    256   1.3       cgd 	char *ibuf = alloca(max_chars);
    257   1.1      phil 
    258  1.12       cgd 	_msg_vprintf(0, msg, ap);
    259   1.1      phil 	if (def != NULL && *def) {
    260   1.1      phil 		waddstr (msg_win, " [");
    261   1.1      phil 		waddstr (msg_win, def);
    262   1.1      phil 		waddstr (msg_win, "]");
    263   1.1      phil 	}
    264   1.1      phil 	waddstr (msg_win, ": ");
    265   1.1      phil 	wrefresh (msg_win);
    266   1.1      phil 
    267   1.1      phil 	while ((ch = wgetch(msg_win)) != '\n') {
    268   1.1      phil 		if (ch == 0x08 || ch == 0x7f) {  /* bs or del */
    269   1.1      phil 			if (count > 0) {
    270   1.1      phil 				count--;
    271   1.1      phil 				if (do_echo) {
    272   1.1      phil 					getyx(msg_win, y, x);
    273   1.1      phil 					x--;
    274   1.1      phil 					wmove(msg_win, y, x);
    275   1.1      phil 					wdelch(msg_win);
    276   1.1      phil 				}
    277   1.1      phil 			} else
    278  1.14       cgd 				_msg_beep();
    279   1.4       cgd 		} else if (ch == 0x15) {	/* ^U; line kill */
    280   1.4       cgd 			while (count > 0) {
    281   1.9       cgd 				count--;
    282   1.9       cgd 				if (do_echo) {
    283   1.9       cgd 					getyx(msg_win, y, x);
    284   1.9       cgd 					x--;
    285   1.9       cgd 					wmove(msg_win, y, x);
    286   1.9       cgd 					wdelch(msg_win);
    287   1.9       cgd 				}
    288   1.9       cgd 			}
    289   1.9       cgd 		} else if (ch == 0x17) {        /* ^W; word kill */
    290   1.9       cgd 			/*
    291   1.9       cgd 			 * word kill kills the spaces and the 'word'
    292   1.9       cgd 			 * (non-spaces) last typed.  the spaces before
    293   1.9       cgd 			 * the 'word' aren't killed.
    294   1.9       cgd 			 */
    295   1.9       cgd 			while (count > 0 && isspace(ibuf[count - 1])) {
    296   1.9       cgd 				count--;
    297   1.9       cgd 				if (do_echo) {
    298   1.9       cgd 					getyx(msg_win, y, x);
    299   1.9       cgd 					x--;
    300   1.9       cgd 					wmove(msg_win, y, x);
    301   1.9       cgd 					wdelch(msg_win);
    302   1.9       cgd 				}
    303   1.9       cgd 			}
    304   1.9       cgd 			while (count > 0 && !isspace(ibuf[count - 1])) {
    305   1.4       cgd 				count--;
    306   1.4       cgd 				if (do_echo) {
    307   1.4       cgd 					getyx(msg_win, y, x);
    308   1.4       cgd 					x--;
    309   1.4       cgd 					wmove(msg_win, y, x);
    310   1.4       cgd 					wdelch(msg_win);
    311   1.4       cgd 				}
    312   1.4       cgd 			}
    313   1.4       cgd 		} else if (count < (max_chars - 1) && isprint(ch)) {
    314   1.1      phil 			if (do_echo)
    315   1.1      phil 				waddch (msg_win, ch);
    316   1.3       cgd 			ibuf[count++] = ch;
    317   1.1      phil 		} else
    318  1.14       cgd 			_msg_beep();
    319   1.1      phil 		if (do_echo)
    320   1.1      phil 			wrefresh(msg_win);
    321   1.1      phil 	}
    322   1.8       cgd 	if (do_echo) {
    323   1.1      phil 		waddch(msg_win, '\n');
    324   1.8       cgd 		last_o_was_punct = 0;
    325   1.8       cgd 		last_o_was_space = 1;
    326   1.8       cgd 	}
    327   1.1      phil 
    328   1.3       cgd 	/* copy the appropriate string to the output */
    329   1.3       cgd 	if (count != 0) {
    330   1.3       cgd 		ibuf[count] = '\0';
    331   1.3       cgd 		strcpy(val, ibuf);		/* size known to be OK */
    332   1.5       cgd 	} else if (def != NULL && val != def) {
    333   1.3       cgd 		strncpy(val, def, max_chars);
    334   1.3       cgd 		val[max_chars - 1] = '\0';
    335   1.3       cgd 	}
    336   1.1      phil }
    337   1.1      phil 
    338  1.13       cgd void
    339  1.18  christos msg_prompt(msg msg_no, const char *def, char *val, size_t max_chars, ...)
    340   1.1      phil {
    341   1.1      phil 	va_list ap;
    342   1.1      phil 
    343  1.13       cgd 	msg_clear();
    344  1.13       cgd 
    345   1.1      phil 	va_start (ap, max_chars);
    346  1.16       cgd 	_msg_vprompt(msg_string(msg_no), 1, def, val, max_chars, ap);
    347  1.13       cgd 	va_end (ap);
    348  1.13       cgd }
    349  1.13       cgd 
    350  1.13       cgd void
    351  1.18  christos msg_prompt_add(msg msg_no, const char *def, char *val, size_t max_chars, ...)
    352  1.13       cgd {
    353  1.13       cgd 	va_list ap;
    354  1.13       cgd 
    355  1.13       cgd 	va_start (ap, max_chars);
    356  1.16       cgd 	_msg_vprompt(msg_string(msg_no), 1, def, val, max_chars, ap);
    357   1.1      phil 	va_end(ap);
    358   1.1      phil }
    359   1.1      phil 
    360  1.13       cgd void
    361  1.18  christos msg_prompt_noecho(msg msg_no, const char *def, char *val, size_t max_chars, ...)
    362   1.1      phil {
    363   1.1      phil 	va_list ap;
    364   1.1      phil 
    365   1.7       cgd 	msg_clear();
    366   1.7       cgd 
    367   1.1      phil 	va_start (ap, max_chars);
    368  1.16       cgd 	_msg_vprompt(msg_string(msg_no), 0, def, val, max_chars, ap);
    369   1.1      phil 	va_end (ap);
    370   1.1      phil }
    371  1.10       cgd 
    372  1.16       cgd void msg_table_add(msg msg_no, ...)
    373  1.10       cgd {
    374  1.10       cgd 	va_list ap;
    375  1.10       cgd 
    376  1.10       cgd 	va_start (ap, msg_no);
    377  1.16       cgd 	(void)_msg_vprintf(0, msg_string(msg_no), ap);
    378  1.10       cgd 	va_end (ap);
    379  1.10       cgd }
    380  1.10       cgd 
    381