xref: /openbsd-src/usr.sbin/config/misc.c (revision a9bbb35dbfb434a89c38c403e31cf2fae9de56f9)
1*a9bbb35dSjcs /*	$OpenBSD: misc.c,v 1.11 2021/11/20 03:13:37 jcs Exp $	*/
235319decSderaadt 
335319decSderaadt /*
435319decSderaadt  * Copyright (c) 1997 Tobias Weingartner
535319decSderaadt  * All rights reserved.
635319decSderaadt  *
735319decSderaadt  * Redistribution and use in source and binary forms, with or without
835319decSderaadt  * modification, are permitted provided that the following conditions
935319decSderaadt  * are met:
1035319decSderaadt  * 1. Redistributions of source code must retain the above copyright
1135319decSderaadt  *    notice, this list of conditions and the following disclaimer.
1235319decSderaadt  * 2. Redistributions in binary form must reproduce the above copyright
1335319decSderaadt  *    notice, this list of conditions and the following disclaimer in the
1435319decSderaadt  *    documentation and/or other materials provided with the distribution.
1535319decSderaadt  *
1635319decSderaadt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1735319decSderaadt  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1835319decSderaadt  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1935319decSderaadt  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2035319decSderaadt  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2135319decSderaadt  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2235319decSderaadt  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2335319decSderaadt  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2435319decSderaadt  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2535319decSderaadt  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2635319decSderaadt  */
2735319decSderaadt 
2835319decSderaadt #include <sys/types.h>
29c6b62359Sedd 
30c6b62359Sedd #include <ctype.h>
3135319decSderaadt #include <err.h>
3235319decSderaadt #include <stdio.h>
3335319decSderaadt #include <stdlib.h>
3435319decSderaadt #include <string.h>
3535319decSderaadt 
3635319decSderaadt #include "misc.h"
3735319decSderaadt 
38c1a66a41Sderaadt extern int verbose;
39c1a66a41Sderaadt 
4035319decSderaadt int
parse_cmd(cmd_t * cmd,char * lbuf)41f7dd4d03Stedu parse_cmd(cmd_t *cmd, char *lbuf)
4235319decSderaadt {
43f7dd4d03Stedu 	char *cp, *buf;
4435319decSderaadt 
45673a9bc7Sgilles 	lbuf[strcspn(lbuf, "\n")] = '\0';
46c1a66a41Sderaadt 	if (verbose)
47c1a66a41Sderaadt 		printf("%s\n", lbuf);
4835319decSderaadt 
4935319decSderaadt 	/* Parse input */
5035319decSderaadt 	buf = lbuf;
5135319decSderaadt 	buf = &buf[strspn(buf, " \t")];
5235319decSderaadt 	cp = &buf[strcspn(buf, " \t")];
5335319decSderaadt 	*cp++ = '\0';
540a264ab6Sderaadt 	strlcpy(cmd->cmd, buf, sizeof cmd->cmd);
5535319decSderaadt 	buf = &cp[strspn(cp, " \t")];
560a264ab6Sderaadt 	strlcpy(cmd->args, buf, sizeof cmd->args);
5735319decSderaadt 
5835319decSderaadt 	return (0);
5935319decSderaadt }
6035319decSderaadt 
6135319decSderaadt int
ask_cmd(cmd_t * cmd)62f7dd4d03Stedu ask_cmd(cmd_t *cmd)
63f7dd4d03Stedu {
64f7dd4d03Stedu 	char lbuf[100];
65*a9bbb35dSjcs 	extern FILE *cmdfp;
66f7dd4d03Stedu 
67f7dd4d03Stedu 	/* Get input */
68*a9bbb35dSjcs 	if (fgets(lbuf, sizeof lbuf, cmdfp ? cmdfp : stdin) == NULL) {
69*a9bbb35dSjcs 		if (cmdfp) {
70*a9bbb35dSjcs 			cmd->cmd[0] = '\0';
71*a9bbb35dSjcs 			return -1;
72*a9bbb35dSjcs 		}
73f7dd4d03Stedu 		errx(1, "eof");
74*a9bbb35dSjcs 	}
75*a9bbb35dSjcs 	if (cmdfp)
76*a9bbb35dSjcs 		printf("%s", lbuf);
77f7dd4d03Stedu 	return parse_cmd(cmd, lbuf);
78f7dd4d03Stedu }
79f7dd4d03Stedu 
80f7dd4d03Stedu int
ask_yn(const char * str)81815b5809Sderaadt ask_yn(const char *str)
8235319decSderaadt {
83*a9bbb35dSjcs 	extern FILE *cmdfp;
8435319decSderaadt 	int ch, first;
8535319decSderaadt 
8635319decSderaadt 	printf("%s [n] ", str);
8735319decSderaadt 	fflush(stdout);
8835319decSderaadt 
89*a9bbb35dSjcs 	first = ch = getc(cmdfp ? cmdfp : stdin);
90*a9bbb35dSjcs 	if (verbose || (cmdfp && ch != EOF)) {
91c1a66a41Sderaadt 		printf("%c", ch);
92c1a66a41Sderaadt 		fflush(stdout);
93c1a66a41Sderaadt 	}
94c1a66a41Sderaadt 	while (ch != '\n' && ch != EOF) {
95*a9bbb35dSjcs 		ch = getc(cmdfp ? cmdfp : stdin);
96c1a66a41Sderaadt 		if (verbose) {
97c1a66a41Sderaadt 			printf("%c\n", ch);
98c1a66a41Sderaadt 			fflush(stdout);
99*a9bbb35dSjcs 		} else if (cmdfp)
100*a9bbb35dSjcs 			putchar('\n');
101c1a66a41Sderaadt 	}
10235319decSderaadt 	if (ch == EOF || first == EOF)
10335319decSderaadt 		errx(1, "eof");
10435319decSderaadt 
10535319decSderaadt 	return (first == 'y' || first == 'Y');
10635319decSderaadt }
107