xref: /openbsd-src/usr.sbin/config/misc.c (revision a9bbb35dbfb434a89c38c403e31cf2fae9de56f9)
1 /*	$OpenBSD: misc.c,v 1.11 2021/11/20 03:13:37 jcs 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
parse_cmd(cmd_t * cmd,char * lbuf)41 parse_cmd(cmd_t *cmd, char *lbuf)
42 {
43 	char *cp, *buf;
44 
45 	lbuf[strcspn(lbuf, "\n")] = '\0';
46 	if (verbose)
47 		printf("%s\n", lbuf);
48 
49 	/* Parse input */
50 	buf = lbuf;
51 	buf = &buf[strspn(buf, " \t")];
52 	cp = &buf[strcspn(buf, " \t")];
53 	*cp++ = '\0';
54 	strlcpy(cmd->cmd, buf, sizeof cmd->cmd);
55 	buf = &cp[strspn(cp, " \t")];
56 	strlcpy(cmd->args, buf, sizeof cmd->args);
57 
58 	return (0);
59 }
60 
61 int
ask_cmd(cmd_t * cmd)62 ask_cmd(cmd_t *cmd)
63 {
64 	char lbuf[100];
65 	extern FILE *cmdfp;
66 
67 	/* Get input */
68 	if (fgets(lbuf, sizeof lbuf, cmdfp ? cmdfp : stdin) == NULL) {
69 		if (cmdfp) {
70 			cmd->cmd[0] = '\0';
71 			return -1;
72 		}
73 		errx(1, "eof");
74 	}
75 	if (cmdfp)
76 		printf("%s", lbuf);
77 	return parse_cmd(cmd, lbuf);
78 }
79 
80 int
ask_yn(const char * str)81 ask_yn(const char *str)
82 {
83 	extern FILE *cmdfp;
84 	int ch, first;
85 
86 	printf("%s [n] ", str);
87 	fflush(stdout);
88 
89 	first = ch = getc(cmdfp ? cmdfp : stdin);
90 	if (verbose || (cmdfp && ch != EOF)) {
91 		printf("%c", ch);
92 		fflush(stdout);
93 	}
94 	while (ch != '\n' && ch != EOF) {
95 		ch = getc(cmdfp ? cmdfp : stdin);
96 		if (verbose) {
97 			printf("%c\n", ch);
98 			fflush(stdout);
99 		} else if (cmdfp)
100 			putchar('\n');
101 	}
102 	if (ch == EOF || first == EOF)
103 		errx(1, "eof");
104 
105 	return (first == 'y' || first == 'Y');
106 }
107