8354 sync regcomp(3C) with upstream (fix make catalog)
[unleashed/tickless.git] / usr / src / lib / libast / common / port / astquery.c
blob3284783314f6551b97e3b4d186fa73685df586cc
1 /***********************************************************************
2 * *
3 * This software is part of the ast package *
4 * Copyright (c) 1985-2010 AT&T Intellectual Property *
5 * and is licensed under the *
6 * Common Public License, Version 1.0 *
7 * by AT&T Intellectual Property *
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
24 * AT&T Research
26 * output printf prompt and read response
27 * if format==0 then verify that interaction is possible
29 * return:
31 * 0 [1yY+]
32 * -1 [qQ] or EOF
33 * 1 otherwise
35 * if (quit&ERROR_PROMPT) then tty forced for IO
36 * if quit>=0 then [qQ] or EOF calls exit(quit)
39 #include <ast.h>
40 #include <error.h>
42 int
43 astquery(int quit, const char* format, ...)
45 va_list ap;
46 register int n;
47 register int c;
48 Sfio_t* ip;
49 Sfio_t* op;
51 static Sfio_t* rfp;
52 static Sfio_t* wfp;
54 va_start(ap, format);
55 if (!format)
56 return 0;
57 if (!rfp)
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;
70 if (quit & ERROR_PROMPT)
72 quit &= ~ERROR_PROMPT;
73 ip = rfp;
74 op = wfp;
76 else
78 ip = sfstdin;
79 op = sfstderr;
81 sfsync(sfstdout);
82 sfvprintf(op, format, ap);
83 sfsync(op);
84 for (n = c = sfgetc(ip);; c = sfgetc(ip))
85 switch (c)
87 case EOF:
88 n = c;
89 /*FALLTHROUGH*/
90 case '\n':
91 switch (n)
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;
105 return 1;
107 va_end(ap);
108 /*NOTREACHED*/