xref: /onnv-gate/usr/src/lib/libast/common/port/astquery.c (revision 12068:08a39a083754)
14887Schin /***********************************************************************
24887Schin *                                                                      *
34887Schin *               This software is part of the ast package               *
4*12068SRoger.Faulkner@Oracle.COM *          Copyright (c) 1985-2010 AT&T Intellectual Property          *
54887Schin *                      and is licensed under the                       *
64887Schin *                  Common Public License, Version 1.0                  *
78462SApril.Chin@Sun.COM *                    by AT&T Intellectual Property                     *
84887Schin *                                                                      *
94887Schin *                A copy of the License is available at                 *
104887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
114887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
124887Schin *                                                                      *
134887Schin *              Information and Software Systems Research               *
144887Schin *                            AT&T Research                             *
154887Schin *                           Florham Park NJ                            *
164887Schin *                                                                      *
174887Schin *                 Glenn Fowler <gsf@research.att.com>                  *
184887Schin *                  David Korn <dgk@research.att.com>                   *
194887Schin *                   Phong Vo <kpv@research.att.com>                    *
204887Schin *                                                                      *
214887Schin ***********************************************************************/
224887Schin #pragma prototyped
234887Schin /*
244887Schin  * AT&T Research
254887Schin  *
264887Schin  * output printf prompt and read response
274887Schin  * if format==0 then verify that interaction is possible
284887Schin  *
294887Schin  * return:
304887Schin  *
314887Schin  *	0	[1yY+]
324887Schin  *	-1	[qQ] or EOF
334887Schin  *	1	otherwise
344887Schin  *
354887Schin  * if (quit&ERROR_PROMPT) then tty forced for IO
364887Schin  * if quit>=0 then [qQ] or EOF calls exit(quit)
374887Schin  */
384887Schin 
394887Schin #include <ast.h>
404887Schin #include <error.h>
414887Schin 
424887Schin int
astquery(int quit,const char * format,...)434887Schin astquery(int quit, const char* format, ...)
444887Schin {
454887Schin 	va_list		ap;
464887Schin 	register int	n;
474887Schin 	register int	c;
484887Schin 	Sfio_t*		ip;
494887Schin 	Sfio_t*		op;
504887Schin 
514887Schin 	static Sfio_t*	rfp;
524887Schin 	static Sfio_t*	wfp;
534887Schin 
544887Schin 	va_start(ap, format);
554887Schin 	if (!format)
564887Schin 		return 0;
574887Schin 	if (!rfp)
584887Schin 	{
594887Schin 		c = errno;
604887Schin 		if (isatty(sffileno(sfstdin)))
614887Schin 			rfp = sfstdin;
624887Schin 		else if (!(rfp = sfopen(NiL, "/dev/tty", "r")))
634887Schin 			return -1;
644887Schin 		if (isatty(sffileno(sfstderr)))
654887Schin 			wfp = sfstderr;
664887Schin 		else if (!(wfp = sfopen(NiL, "/dev/tty", "w")))
674887Schin 			return -1;
684887Schin 		errno = c;
694887Schin 	}
704887Schin 	if (quit & ERROR_PROMPT)
714887Schin 	{
724887Schin 		quit &= ~ERROR_PROMPT;
734887Schin 		ip = rfp;
744887Schin 		op = wfp;
754887Schin 	}
764887Schin 	else
774887Schin 	{
784887Schin 		ip = sfstdin;
794887Schin 		op = sfstderr;
804887Schin 	}
814887Schin 	sfsync(sfstdout);
824887Schin 	sfvprintf(op, format, ap);
834887Schin 	sfsync(op);
844887Schin 	for (n = c = sfgetc(ip);; c = sfgetc(ip))
854887Schin 		switch (c)
864887Schin 		{
874887Schin 		case EOF:
884887Schin 			n = c;
894887Schin 			/*FALLTHROUGH*/
904887Schin 		case '\n':
914887Schin 			switch (n)
924887Schin 			{
934887Schin 			case EOF:
944887Schin 			case 'q':
954887Schin 			case 'Q':
964887Schin 				if (quit >= 0)
974887Schin 					exit(quit);
984887Schin 				return -1;
994887Schin 			case '1':
1004887Schin 			case 'y':
1014887Schin 			case 'Y':
1024887Schin 			case '+':
1034887Schin 				return 0;
1044887Schin 			}
1054887Schin 			return 1;
1064887Schin 		}
1074887Schin 	va_end(ap);
1084887Schin 	/*NOTREACHED*/
1094887Schin }
110