1327Sjkh/*
2228990Suqs * FreeBSD install - a package for the installation and maintenance
3327Sjkh * of non-core utilities.
4327Sjkh *
5327Sjkh * Redistribution and use in source and binary forms, with or without
6327Sjkh * modification, are permitted provided that the following conditions
7327Sjkh * are met:
8327Sjkh * 1. Redistributions of source code must retain the above copyright
9327Sjkh *    notice, this list of conditions and the following disclaimer.
10327Sjkh * 2. Redistributions in binary form must reproduce the above copyright
11327Sjkh *    notice, this list of conditions and the following disclaimer in the
12327Sjkh *    documentation and/or other materials provided with the distribution.
13327Sjkh *
14327Sjkh * Jordan K. Hubbard
15327Sjkh * 18 July 1993
16327Sjkh *
17327Sjkh * Miscellaneous message routines.
18327Sjkh *
19327Sjkh */
20327Sjkh
2193520Sobrien#include <sys/cdefs.h>
2293520Sobrien__FBSDID("$FreeBSD$");
2393520Sobrien
2474699Ssobomax#include "lib.h"
2530221Scharnier#include <err.h>
2669793Sobrien#include <paths.h>
27327Sjkh
28327Sjkh/* Die a relatively simple death */
29327Sjkhvoid
3084745Ssobomaxupchuck(const char *message)
31327Sjkh{
32327Sjkh    cleanup(0);
3384745Ssobomax    errx(1, "fatal error during execution: %s", message);
34327Sjkh}
35327Sjkh
36327Sjkh/*
37327Sjkh * As a yes/no question, prompting from the varargs string and using
38327Sjkh * default if user just hits return.
39327Sjkh */
40327SjkhBoolean
41327Sjkhy_or_n(Boolean def, const char *msg, ...)
42327Sjkh{
43327Sjkh    va_list args;
44327Sjkh    int ch = 0;
45327Sjkh    FILE *tty;
46327Sjkh
47327Sjkh    va_start(args, msg);
48327Sjkh    /*
49327Sjkh     * Need to open /dev/tty because file collection may have been
50327Sjkh     * collected on stdin
51327Sjkh     */
5269793Sobrien    tty = fopen(_PATH_TTY, "r");
5330387Sjkh    if (!tty) {
5430387Sjkh	cleanup(0);
5569793Sobrien	errx(2, "can't open %s!", _PATH_TTY);
5630387Sjkh    }
571338Sjkh    while (ch != 'Y' && ch != 'N') {
58327Sjkh	vfprintf(stderr, msg, args);
59327Sjkh	if (def)
60327Sjkh	    fprintf(stderr, " [yes]? ");
61327Sjkh	else
62327Sjkh	    fprintf(stderr, " [no]? ");
63327Sjkh	fflush(stderr);
641338Sjkh	if (AutoAnswer) {
651338Sjkh	    ch = (AutoAnswer == YES) ? 'Y' : 'N';
661338Sjkh	    fprintf(stderr, "%c\n", ch);
671338Sjkh	}
681338Sjkh	else
691338Sjkh	    ch = toupper(fgetc(tty));
70327Sjkh	if (ch == '\n')
71327Sjkh	    ch = (def) ? 'Y' : 'N';
72327Sjkh    }
731666Sasami    fclose(tty) ;
74236213Skevlo    va_end(args);
75327Sjkh    return (ch == 'Y') ? TRUE : FALSE;
76327Sjkh}
77