1 |
|
|
/* $OpenBSD: misc.c,v 1.9 2011/10/02 22:20:49 edd Exp $ */ |
2 |
|
|
|
3 |
|
|
/* |
4 |
|
|
* Copyright (c) 1997 Tobias Weingartner |
5 |
|
|
* All rights reserved. |
6 |
|
|
* |
7 |
|
|
* Redistribution and use in source and binary forms, with or without |
8 |
|
|
* modification, are permitted provided that the following conditions |
9 |
|
|
* are met: |
10 |
|
|
* 1. Redistributions of source code must retain the above copyright |
11 |
|
|
* notice, this list of conditions and the following disclaimer. |
12 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright |
13 |
|
|
* notice, this list of conditions and the following disclaimer in the |
14 |
|
|
* documentation and/or other materials provided with the distribution. |
15 |
|
|
* |
16 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
17 |
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
18 |
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 |
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 |
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 |
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 |
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 |
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 |
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 |
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 |
|
|
*/ |
27 |
|
|
|
28 |
|
|
#include <sys/types.h> |
29 |
|
|
|
30 |
|
|
#include <ctype.h> |
31 |
|
|
#include <err.h> |
32 |
|
|
#include <stdio.h> |
33 |
|
|
#include <stdlib.h> |
34 |
|
|
#include <string.h> |
35 |
|
|
|
36 |
|
|
#include "misc.h" |
37 |
|
|
|
38 |
|
|
extern int verbose; |
39 |
|
|
|
40 |
|
|
int |
41 |
|
|
ask_cmd(cmd_t *cmd) |
42 |
|
|
{ |
43 |
|
|
char lbuf[100], *cp, *buf; |
44 |
|
|
|
45 |
|
|
/* Get input */ |
46 |
|
|
if (fgets(lbuf, sizeof lbuf, stdin) == NULL) |
47 |
|
|
errx(1, "eof"); |
48 |
|
|
lbuf[strcspn(lbuf, "\n")] = '\0'; |
49 |
|
|
if (verbose) |
50 |
|
|
printf("%s\n", lbuf); |
51 |
|
|
|
52 |
|
|
/* Parse input */ |
53 |
|
|
buf = lbuf; |
54 |
|
|
buf = &buf[strspn(buf, " \t")]; |
55 |
|
|
cp = &buf[strcspn(buf, " \t")]; |
56 |
|
|
*cp++ = '\0'; |
57 |
|
|
strlcpy(cmd->cmd, buf, sizeof cmd->cmd); |
58 |
|
|
buf = &cp[strspn(cp, " \t")]; |
59 |
|
|
strlcpy(cmd->args, buf, sizeof cmd->args); |
60 |
|
|
|
61 |
|
|
return (0); |
62 |
|
|
} |
63 |
|
|
|
64 |
|
|
int |
65 |
|
|
ask_yn(const char *str) |
66 |
|
|
{ |
67 |
|
|
int ch, first; |
68 |
|
|
|
69 |
|
|
printf("%s [n] ", str); |
70 |
|
|
fflush(stdout); |
71 |
|
|
|
72 |
|
|
first = ch = getchar(); |
73 |
|
|
if (verbose) { |
74 |
|
|
printf("%c", ch); |
75 |
|
|
fflush(stdout); |
76 |
|
|
} |
77 |
|
|
while (ch != '\n' && ch != EOF) { |
78 |
|
|
ch = getchar(); |
79 |
|
|
if (verbose) { |
80 |
|
|
printf("%c\n", ch); |
81 |
|
|
fflush(stdout); |
82 |
|
|
} |
83 |
|
|
} |
84 |
|
|
if (ch == EOF || first == EOF) |
85 |
|
|
errx(1, "eof"); |
86 |
|
|
|
87 |
|
|
return (first == 'y' || first == 'Y'); |
88 |
|
|
} |