Home | History | Annotate | Line # | Download | only in msgc
msg_sys.def revision 1.3
      1 /*	$NetBSD: msg_sys.def,v 1.3 1999/06/19 00:00:48 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 		}
    161 		else if (count < (max_chars - 1) && isprint(ch)) {
    162 			if (do_echo)
    163 				waddch (msg_win, ch);
    164 			ibuf[count++] = ch;
    165 		} else
    166 			msg_beep ();
    167 		if (do_echo)
    168 			wrefresh(msg_win);
    169 	}
    170 	if (do_echo)
    171 		waddch(msg_win, '\n');
    172 
    173 	/* copy the appropriate string to the output */
    174 	if (count != 0) {
    175 		ibuf[count] = '\0';
    176 		strcpy(val, ibuf);		/* size known to be OK */
    177 	} else if (val != def) {
    178 		strncpy(val, def, max_chars);
    179 		val[max_chars - 1] = '\0';
    180 	}
    181 }
    182 
    183 void msg_prompt_addstr (char *fmt, char *def, char *val, int max_chars, ...)
    184 {
    185 	va_list ap;
    186 
    187 	va_start (ap, max_chars);
    188 	msg_vprompt (fmt, def, val, max_chars, ap);
    189 	va_end(ap);
    190 }
    191 
    192 void msg_prompt_add (int msg_no, char *def, char *val, int max_chars, ...)
    193 {
    194 	va_list ap;
    195 
    196 	va_start (ap, max_chars);
    197 	msg_vprompt (msg_list[msg_no], def, val, max_chars, ap);
    198 	va_end(ap);
    199 }
    200 
    201 void msg_prompt_str (char *msg, char *def, char *val, int max_chars, ...)
    202 {
    203 	va_list ap;
    204 
    205 	va_start (ap, max_chars);
    206 	wclear (msg_win);
    207 	wmove (msg_win, 0, 0);
    208 	msg_vprompt (msg, def, val, max_chars, ap);
    209 	va_end (ap);
    210 }
    211 
    212 void msg_prompt (int msg_no, char *def, char *val, int max_chars, ...)
    213 {
    214 	va_list ap;
    215 
    216 	va_start (ap, max_chars);
    217 	wclear (msg_win);
    218 	wmove (msg_win, 0, 0);
    219 	msg_vprompt (msg_list[msg_no], def, val, max_chars, ap);
    220 	va_end (ap);
    221 }
    222 
    223 void msg_noecho()
    224 {
    225 	do_echo = 0;
    226 }
    227 
    228 void msg_echo()
    229 {
    230 	do_echo = 1;
    231 }
    232