msg.c revision 50479
11553Srgrimes#ifndef lint
21553Srgrimesstatic const char rcsid[] =
31553Srgrimes  "$FreeBSD: head/usr.sbin/pkg_install/lib/msg.c 50479 1999-08-28 01:35:59Z peter $";
41553Srgrimes#endif
51553Srgrimes
61553Srgrimes/*
71553Srgrimes * FreeBSD install - a package for the installation and maintainance
81553Srgrimes * of non-core utilities.
91553Srgrimes *
101553Srgrimes * Redistribution and use in source and binary forms, with or without
111553Srgrimes * modification, are permitted provided that the following conditions
121553Srgrimes * are met:
131553Srgrimes * 1. Redistributions of source code must retain the above copyright
141553Srgrimes *    notice, this list of conditions and the following disclaimer.
151553Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
161553Srgrimes *    notice, this list of conditions and the following disclaimer in the
171553Srgrimes *    documentation and/or other materials provided with the distribution.
181553Srgrimes *
191553Srgrimes * Jordan K. Hubbard
201553Srgrimes
211553Srgrimes * 18 July 1993
221553Srgrimes *
231553Srgrimes * Miscellaneous message routines.
241553Srgrimes *
251553Srgrimes */
261553Srgrimes
271553Srgrimes#include <err.h>
281553Srgrimes#include "lib.h"
291553Srgrimes
301553Srgrimes/* Die a relatively simple death */
311553Srgrimesvoid
321553Srgrimesupchuck(const char *err)
331553Srgrimes{
341553Srgrimes    cleanup(0);
351553Srgrimes    errx(1, "fatal error during execution: %s", err);
361553Srgrimes}
371553Srgrimes
381553Srgrimes/*
391553Srgrimes * As a yes/no question, prompting from the varargs string and using
401553Srgrimes * default if user just hits return.
411553Srgrimes */
421553SrgrimesBoolean
431553Srgrimesy_or_n(Boolean def, const char *msg, ...)
441553Srgrimes{
451553Srgrimes    va_list args;
461553Srgrimes    int ch = 0;
471553Srgrimes    FILE *tty;
481553Srgrimes
491553Srgrimes    va_start(args, msg);
501553Srgrimes    /*
511553Srgrimes     * Need to open /dev/tty because file collection may have been
521553Srgrimes     * collected on stdin
531553Srgrimes     */
541553Srgrimes    tty = fopen("/dev/tty", "r");
551553Srgrimes    if (!tty) {
561553Srgrimes	cleanup(0);
571553Srgrimes	errx(2, "can't open /dev/tty!");
581553Srgrimes    }
591553Srgrimes    while (ch != 'Y' && ch != 'N') {
601553Srgrimes	vfprintf(stderr, msg, args);
611553Srgrimes	if (def)
621553Srgrimes	    fprintf(stderr, " [yes]? ");
631553Srgrimes	else
641553Srgrimes	    fprintf(stderr, " [no]? ");
651553Srgrimes	fflush(stderr);
661553Srgrimes	if (AutoAnswer) {
671553Srgrimes	    ch = (AutoAnswer == YES) ? 'Y' : 'N';
681553Srgrimes	    fprintf(stderr, "%c\n", ch);
691553Srgrimes	}
701553Srgrimes	else
711553Srgrimes	    ch = toupper(fgetc(tty));
721553Srgrimes	if (ch == '\n')
731553Srgrimes	    ch = (def) ? 'Y' : 'N';
741553Srgrimes    }
751553Srgrimes    fclose(tty) ;
761553Srgrimes    return (ch == 'Y') ? TRUE : FALSE;
771553Srgrimes}
781553Srgrimes