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