Home | History | Annotate | Line # | Download | only in msgc
msg_sys.def revision 1.7
      1 /*	$NetBSD: msg_sys.def,v 1.7 1999/06/23 09:19:33 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 
     45 /* Routines */
     46 
     47 void msg_beep (void)
     48 {
     49 	fprintf (stderr, "\a");
     50 }
     51 
     52 int msg_window(WINDOW *window)
     53 {
     54 	size_t ncbuffersize;
     55 	char *ncbuffer;
     56 
     57 	msg_win = window;
     58 
     59 	ncbuffersize = getmaxx(window) * getmaxy(window) + 1;
     60 	if (ncbuffersize > cbuffersize) {
     61 		ncbuffer = malloc(ncbuffersize);
     62 		if (ncbuffer == NULL)
     63 			return 1;
     64 		if (cbuffer != NULL)
     65 			free(cbuffer);
     66 		cbuffer = ncbuffer;
     67 		cbuffersize = ncbuffersize;
     68 	}
     69 	return 0;
     70 }
     71 
     72 char *msg_string (int msg_no)
     73 {
     74 	return msg_list[msg_no];
     75 }
     76 
     77 void msg_clear(void)
     78 {
     79 	wclear (msg_win);
     80 	wrefresh (msg_win);
     81 }
     82 
     83 void msg_standout(void)
     84 {
     85 	wstandout(msg_win);
     86 }
     87 
     88 void msg_standend(void)
     89 {
     90 	wstandend(msg_win);
     91 }
     92 
     93 int msg_vprintf (char *fmt, va_list ap)
     94 {
     95 	int ret;
     96 
     97 	ret = vsnprintf (cbuffer, cbuffersize, fmt, ap);
     98 	waddstr (msg_win, cbuffer);
     99 	wrefresh (msg_win);
    100 	return ret;
    101 }
    102 
    103 void msg_display(int msg_no, ...)
    104 {
    105 	va_list ap;
    106 
    107 	msg_clear();
    108 
    109 	va_start(ap, msg_no);
    110 	(void)msg_vprintf (msg_list[msg_no], ap);
    111 	va_end(ap);
    112 }
    113 
    114 void msg_display_add(int msg_no, ...)
    115 {
    116 	va_list ap;
    117 
    118 	va_start (ap, msg_no);
    119 	(void)msg_vprintf (msg_list[msg_no], ap);
    120 	va_end (ap);
    121 }
    122 
    123 int msg_printf (char *fmt, ...)
    124 {
    125 	va_list ap;
    126 	int  res;
    127 
    128 	msg_clear();
    129 
    130 	va_start (ap, fmt);
    131 	res = msg_vprintf (fmt, ap);
    132 	va_end (ap);
    133 	return res;
    134 }
    135 
    136 int msg_printf_add (char *fmt, ...)
    137 {
    138 	va_list ap;
    139 	int  res;
    140 
    141 	va_start (ap, fmt);
    142 	res = msg_vprintf (fmt, ap);
    143 	va_end (ap);
    144 	return res;
    145 }
    146 
    147 
    148 static void msg_vprompt (char *msg, char *def, char *val, int max_chars,
    149 			 va_list ap)
    150 {
    151 	int ch;
    152 	int count = 0;
    153 	int y,x;
    154 	char *ibuf = alloca(max_chars);
    155 
    156 	msg_vprintf (msg, ap);
    157 	if (def != NULL && *def) {
    158 		waddstr (msg_win, " [");
    159 		waddstr (msg_win, def);
    160 		waddstr (msg_win, "]");
    161 	}
    162 	waddstr (msg_win, ": ");
    163 	wrefresh (msg_win);
    164 
    165 	while ((ch = wgetch(msg_win)) != '\n') {
    166 		if (ch == 0x08 || ch == 0x7f) {  /* bs or del */
    167 			if (count > 0) {
    168 				count--;
    169 				if (do_echo) {
    170 					getyx(msg_win, y, x);
    171 					x--;
    172 					wmove(msg_win, y, x);
    173 					wdelch(msg_win);
    174 				}
    175 			} else
    176 				msg_beep ();
    177 		} else if (ch == 0x15) {	/* ^U; line kill */
    178 			while (count > 0) {
    179 				count--;
    180 				if (do_echo) {
    181 					getyx(msg_win, y, x);
    182 					x--;
    183 					wmove(msg_win, y, x);
    184 					wdelch(msg_win);
    185 				}
    186 			}
    187 		} else if (count < (max_chars - 1) && isprint(ch)) {
    188 			if (do_echo)
    189 				waddch (msg_win, ch);
    190 			ibuf[count++] = ch;
    191 		} else
    192 			msg_beep ();
    193 		if (do_echo)
    194 			wrefresh(msg_win);
    195 	}
    196 	if (do_echo)
    197 		waddch(msg_win, '\n');
    198 
    199 	/* copy the appropriate string to the output */
    200 	if (count != 0) {
    201 		ibuf[count] = '\0';
    202 		strcpy(val, ibuf);		/* size known to be OK */
    203 	} else if (def != NULL && val != def) {
    204 		strncpy(val, def, max_chars);
    205 		val[max_chars - 1] = '\0';
    206 	}
    207 }
    208 
    209 void msg_prompt_addstr (char *fmt, char *def, char *val, int max_chars, ...)
    210 {
    211 	va_list ap;
    212 
    213 	va_start (ap, max_chars);
    214 	msg_vprompt (fmt, def, val, max_chars, ap);
    215 	va_end(ap);
    216 }
    217 
    218 void msg_prompt_add (int msg_no, char *def, char *val, int max_chars, ...)
    219 {
    220 	va_list ap;
    221 
    222 	va_start (ap, max_chars);
    223 	msg_vprompt (msg_list[msg_no], def, val, max_chars, ap);
    224 	va_end(ap);
    225 }
    226 
    227 void msg_prompt_str (char *msg, char *def, char *val, int max_chars, ...)
    228 {
    229 	va_list ap;
    230 
    231 	msg_clear();
    232 
    233 	va_start (ap, max_chars);
    234 	msg_vprompt (msg, def, val, max_chars, ap);
    235 	va_end (ap);
    236 }
    237 
    238 void msg_prompt (int msg_no, char *def, char *val, int max_chars, ...)
    239 {
    240 	va_list ap;
    241 
    242 	msg_clear();
    243 
    244 	va_start (ap, max_chars);
    245 	msg_vprompt (msg_list[msg_no], def, val, max_chars, ap);
    246 	va_end (ap);
    247 }
    248 
    249 void msg_noecho()
    250 {
    251 	do_echo = 0;
    252 }
    253 
    254 void msg_echo()
    255 {
    256 	do_echo = 1;
    257 }
    258