msg.c revision 22997
1139825Simp#ifndef lint
242805Skatostatic const char *rcsid = "$Id$";
342805Skato#endif
442805Skato
542805Skato/*
642805Skato * FreeBSD install - a package for the installation and maintainance
742805Skato * of non-core utilities.
842805Skato *
942805Skato * Redistribution and use in source and binary forms, with or without
1042805Skato * modification, are permitted provided that the following conditions
1142805Skato * are met:
1242805Skato * 1. Redistributions of source code must retain the above copyright
1342805Skato *    notice, this list of conditions and the following disclaimer.
1442805Skato * 2. Redistributions in binary form must reproduce the above copyright
1542805Skato *    notice, this list of conditions and the following disclaimer in the
1642805Skato *    documentation and/or other materials provided with the distribution.
1742805Skato *
1842805Skato * Jordan K. Hubbard
1942805Skato
2042805Skato * 18 July 1993
2142805Skato *
2242805Skato * Miscellaneous message routines.
2342805Skato *
2442805Skato */
2542805Skato
2642805Skato#include "lib.h"
2742805Skato
2850477Speter/* Die a relatively simple death */
2942795Skatovoid
3042795Skatoupchuck(const char *err)
3142795Skato{
3242795Skato    fprintf(stderr, "Fatal error during execution: ");
3342795Skato    perror(err);
3442795Skato    cleanup(0);
3542795Skato    exit(1);
3642795Skato}
3742795Skato
38130174Sphk/* Die a more complex death */
39111311Snyanvoid
4042795Skatobarf(const char *err, ...)
4183547Snyan{
4283547Snyan    va_list args;
4383547Snyan
4442795Skato    va_start(args, err);
4567142Snyan    vfprintf(stderr, err, args);
46111311Snyan    fputc('\n', stderr);
4767142Snyan    va_end(args);
4842795Skato    cleanup(0);
4942795Skato    exit(2);
50130431Simp}
5142795Skato
5242795Skato/* Get annoyed about something but don't go to pieces over it */
5342795Skatovoid
5442795Skatowhinge(const char *err, ...)
5542795Skato{
5642795Skato    va_list args;
57139946Simp
58146049Snyan    va_start(args, err);
59139946Simp    vfprintf(stderr, err, args);
6042795Skato    fputc('\n', stderr);
6145783Skato    va_end(args);
6242795Skato}
6383547Snyan
6483547Snyan/*
6542805Skato * As a yes/no question, prompting from the varargs string and using
6642805Skato * default if user just hits return.
6742805Skato */
6842795SkatoBoolean
6942795Skatoy_or_n(Boolean def, const char *msg, ...)
7042795Skato{
7142795Skato    va_list args;
72183397Sed    int ch = 0;
7342795Skato    FILE *tty;
7442795Skato
7545783Skato    va_start(args, msg);
7645783Skato    /*
7783547Snyan     * Need to open /dev/tty because file collection may have been
7883547Snyan     * collected on stdin
7983547Snyan     */
80111311Snyan    tty = fopen("/dev/tty", "r");
81111311Snyan    if (!tty)
82111311Snyan	barf("Can't open /dev/tty!\n");
8345783Skato    while (ch != 'Y' && ch != 'N') {
8442795Skato	vfprintf(stderr, msg, args);
8545783Skato	if (def)
8645783Skato	    fprintf(stderr, " [yes]? ");
8745783Skato	else
8883547Snyan	    fprintf(stderr, " [no]? ");
8983547Snyan	fflush(stderr);
9048187Skato	if (AutoAnswer) {
9145783Skato	    ch = (AutoAnswer == YES) ? 'Y' : 'N';
9242795Skato	    fprintf(stderr, "%c\n", ch);
9342795Skato	}
9483547Snyan	else
9583547Snyan	    ch = toupper(fgetc(tty));
9642795Skato	if (ch == '\n')
97153165Sru	    ch = (def) ? 'Y' : 'N';
9842795Skato    }
9942795Skato    fclose(tty) ;
10042795Skato    return (ch == 'Y') ? TRUE : FALSE;
10142795Skato}
10248187Skato