xref: /illumos-gate/usr/src/contrib/ast/src/lib/libast/port/astquery.c (revision b30d193948be5a7794d7ae3ba0ed9c2f72c88e0f)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1985-2011 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                 Eclipse Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *          http://www.eclipse.org/org/documents/epl-v10.html           *
11 *         (with md5 checksum b35adb5213ca9657e911e9befb180842)         *
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
astquery(int quit,const char * format,...)43 astquery(int quit, const char* format, ...)
44 {
45 	va_list		ap;
46 	register int	n;
47 	register int	c;
48 	int		r;
49 	Sfio_t*		ip;
50 	Sfio_t*		op;
51 
52 	static Sfio_t*	rfp;
53 	static Sfio_t*	wfp;
54 
55 	r = 0;
56 	va_start(ap, format);
57 	if (!format)
58 		goto done;
59 	r = -1;
60 	if (!rfp)
61 	{
62 		c = errno;
63 		if (isatty(sffileno(sfstdin)))
64 			rfp = sfstdin;
65 		else if (!(rfp = sfopen(NiL, "/dev/tty", "r")))
66 			goto done;
67 		if (isatty(sffileno(sfstderr)))
68 			wfp = sfstderr;
69 		else if (!(wfp = sfopen(NiL, "/dev/tty", "w")))
70 			goto done;
71 		errno = c;
72 	}
73 	if (quit & ERROR_PROMPT)
74 	{
75 		quit &= ~ERROR_PROMPT;
76 		ip = rfp;
77 		op = wfp;
78 	}
79 	else
80 	{
81 		ip = sfstdin;
82 		op = sfstderr;
83 	}
84 	sfsync(sfstdout);
85 	sfvprintf(op, format, ap);
86 	sfsync(op);
87 	for (n = c = sfgetc(ip);; c = sfgetc(ip))
88 		switch (c)
89 		{
90 		case EOF:
91 			n = c;
92 			/*FALLTHROUGH*/
93 		case '\n':
94 			switch (n)
95 			{
96 			case EOF:
97 			case 'q':
98 			case 'Q':
99 				if (quit >= 0)
100 					exit(quit);
101 				goto done;
102 			case '1':
103 			case 'y':
104 			case 'Y':
105 			case '+':
106 				r = 0;
107 				goto done;
108 			}
109 			return 1;
110 		}
111  done:
112 	va_end(ap);
113 	return r;
114 }
115