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