xref: /onnv-gate/usr/src/lib/libast/common/port/astquery.c (revision 4887:feebf9260c2e)
1*4887Schin /***********************************************************************
2*4887Schin *                                                                      *
3*4887Schin *               This software is part of the ast package               *
4*4887Schin *           Copyright (c) 1985-2007 AT&T Knowledge Ventures            *
5*4887Schin *                      and is licensed under the                       *
6*4887Schin *                  Common Public License, Version 1.0                  *
7*4887Schin *                      by AT&T Knowledge Ventures                      *
8*4887Schin *                                                                      *
9*4887Schin *                A copy of the License is available at                 *
10*4887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
11*4887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12*4887Schin *                                                                      *
13*4887Schin *              Information and Software Systems Research               *
14*4887Schin *                            AT&T Research                             *
15*4887Schin *                           Florham Park NJ                            *
16*4887Schin *                                                                      *
17*4887Schin *                 Glenn Fowler <gsf@research.att.com>                  *
18*4887Schin *                  David Korn <dgk@research.att.com>                   *
19*4887Schin *                   Phong Vo <kpv@research.att.com>                    *
20*4887Schin *                                                                      *
21*4887Schin ***********************************************************************/
22*4887Schin #pragma prototyped
23*4887Schin /*
24*4887Schin  * AT&T Research
25*4887Schin  *
26*4887Schin  * output printf prompt and read response
27*4887Schin  * if format==0 then verify that interaction is possible
28*4887Schin  *
29*4887Schin  * return:
30*4887Schin  *
31*4887Schin  *	0	[1yY+]
32*4887Schin  *	-1	[qQ] or EOF
33*4887Schin  *	1	otherwise
34*4887Schin  *
35*4887Schin  * if (quit&ERROR_PROMPT) then tty forced for IO
36*4887Schin  * if quit>=0 then [qQ] or EOF calls exit(quit)
37*4887Schin  */
38*4887Schin 
39*4887Schin #include <ast.h>
40*4887Schin #include <error.h>
41*4887Schin 
42*4887Schin int
43*4887Schin astquery(int quit, const char* format, ...)
44*4887Schin {
45*4887Schin 	va_list		ap;
46*4887Schin 	register int	n;
47*4887Schin 	register int	c;
48*4887Schin 	Sfio_t*		ip;
49*4887Schin 	Sfio_t*		op;
50*4887Schin 
51*4887Schin 	static Sfio_t*	rfp;
52*4887Schin 	static Sfio_t*	wfp;
53*4887Schin 
54*4887Schin 	va_start(ap, format);
55*4887Schin 	if (!format)
56*4887Schin 		return 0;
57*4887Schin 	if (!rfp)
58*4887Schin 	{
59*4887Schin 		c = errno;
60*4887Schin 		if (isatty(sffileno(sfstdin)))
61*4887Schin 			rfp = sfstdin;
62*4887Schin 		else if (!(rfp = sfopen(NiL, "/dev/tty", "r")))
63*4887Schin 			return -1;
64*4887Schin 		if (isatty(sffileno(sfstderr)))
65*4887Schin 			wfp = sfstderr;
66*4887Schin 		else if (!(wfp = sfopen(NiL, "/dev/tty", "w")))
67*4887Schin 			return -1;
68*4887Schin 		errno = c;
69*4887Schin 	}
70*4887Schin 	if (quit & ERROR_PROMPT)
71*4887Schin 	{
72*4887Schin 		quit &= ~ERROR_PROMPT;
73*4887Schin 		ip = rfp;
74*4887Schin 		op = wfp;
75*4887Schin 	}
76*4887Schin 	else
77*4887Schin 	{
78*4887Schin 		ip = sfstdin;
79*4887Schin 		op = sfstderr;
80*4887Schin 	}
81*4887Schin 	sfsync(sfstdout);
82*4887Schin 	sfvprintf(op, format, ap);
83*4887Schin 	sfsync(op);
84*4887Schin 	for (n = c = sfgetc(ip);; c = sfgetc(ip))
85*4887Schin 		switch (c)
86*4887Schin 		{
87*4887Schin 		case EOF:
88*4887Schin 			n = c;
89*4887Schin 			/*FALLTHROUGH*/
90*4887Schin 		case '\n':
91*4887Schin 			switch (n)
92*4887Schin 			{
93*4887Schin 			case EOF:
94*4887Schin 			case 'q':
95*4887Schin 			case 'Q':
96*4887Schin 				if (quit >= 0)
97*4887Schin 					exit(quit);
98*4887Schin 				return -1;
99*4887Schin 			case '1':
100*4887Schin 			case 'y':
101*4887Schin 			case 'Y':
102*4887Schin 			case '+':
103*4887Schin 				return 0;
104*4887Schin 			}
105*4887Schin 			return 1;
106*4887Schin 		}
107*4887Schin 	va_end(ap);
108*4887Schin 	/*NOTREACHED*/
109*4887Schin }
110