]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - usr.sbin/pkg_install/lib/msg.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / usr.sbin / pkg_install / lib / msg.c
1 /*
2  * FreeBSD install - a package for the installation and maintenance
3  * of non-core utilities.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * Jordan K. Hubbard
15  * 18 July 1993
16  *
17  * Miscellaneous message routines.
18  *
19  */
20
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
23
24 #include "lib.h"
25 #include <err.h>
26 #include <paths.h>
27
28 /* Die a relatively simple death */
29 void
30 upchuck(const char *message)
31 {
32     cleanup(0);
33     errx(1, "fatal error during execution: %s", message);
34 }
35
36 /*
37  * As a yes/no question, prompting from the varargs string and using
38  * default if user just hits return.
39  */
40 Boolean
41 y_or_n(Boolean def, const char *msg, ...)
42 {
43     va_list args;
44     int ch = 0;
45     FILE *tty;
46
47     va_start(args, msg);
48     /*
49      * Need to open /dev/tty because file collection may have been
50      * collected on stdin
51      */
52     tty = fopen(_PATH_TTY, "r");
53     if (!tty) {
54         cleanup(0);
55         errx(2, "can't open %s!", _PATH_TTY);
56     }
57     while (ch != 'Y' && ch != 'N') {
58         vfprintf(stderr, msg, args);
59         if (def)
60             fprintf(stderr, " [yes]? ");
61         else
62             fprintf(stderr, " [no]? ");
63         fflush(stderr);
64         if (AutoAnswer) {
65             ch = (AutoAnswer == YES) ? 'Y' : 'N';
66             fprintf(stderr, "%c\n", ch);
67         }
68         else
69             ch = toupper(fgetc(tty));
70         if (ch == '\n')
71             ch = (def) ? 'Y' : 'N';
72     }
73     fclose(tty) ;
74     va_end(args);
75     return (ch == 'Y') ? TRUE : FALSE;
76 }