Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / usr.sbin / pkg_install / lib / msg.c
1 /*
2  * FreeBSD install - a package for the installation and maintainance
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  * $FreeBSD: src/usr.sbin/pkg_install/lib/msg.c,v 1.12.2.4 2002/08/20 06:35:08 obrien Exp $
20  * $DragonFly: src/usr.sbin/pkg_install/lib/Attic/msg.c,v 1.2 2003/06/17 04:29:59 dillon Exp $
21  */
22
23 #include "lib.h"
24 #include <err.h>
25 #include <paths.h>
26
27 /* Die a relatively simple death */
28 void
29 upchuck(const char *message)
30 {
31     cleanup(0);
32     errx(1, "fatal error during execution: %s", message);
33 }
34
35 /*
36  * As a yes/no question, prompting from the varargs string and using
37  * default if user just hits return.
38  */
39 Boolean
40 y_or_n(Boolean def, const char *msg, ...)
41 {
42     va_list args;
43     int ch = 0;
44     FILE *tty;
45
46     va_start(args, msg);
47     /*
48      * Need to open /dev/tty because file collection may have been
49      * collected on stdin
50      */
51     tty = fopen(_PATH_TTY, "r");
52     if (!tty) {
53         cleanup(0);
54         errx(2, "can't open %s!", _PATH_TTY);
55     }
56     while (ch != 'Y' && ch != 'N') {
57         vfprintf(stderr, msg, args);
58         if (def)
59             fprintf(stderr, " [yes]? ");
60         else
61             fprintf(stderr, " [no]? ");
62         fflush(stderr);
63         if (AutoAnswer) {
64             ch = (AutoAnswer == YES) ? 'Y' : 'N';
65             fprintf(stderr, "%c\n", ch);
66         }
67         else
68             ch = toupper(fgetc(tty));
69         if (ch == '\n')
70             ch = (def) ? 'Y' : 'N';
71     }
72     fclose(tty) ;
73     return (ch == 'Y') ? TRUE : FALSE;
74 }