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