1 /*
2  * This file has been modified for the cdrkit suite.
3  *
4  * The behaviour and appearence of the program code below can differ to a major
5  * extent from the version distributed by the original author(s).
6  *
7  * For details, see Changelog file distributed with the cdrkit package. If you
8  * received this file from another source then ask the distributing person for
9  * a log of modifications.
10  *
11  */
12 
13 /* @(#)io.c	1.19 98/10/10 Copyright 1988 J. Schilling */
14 /*
15  *	Copyright (c) 1988 J. Schilling
16  */
17 /*
18  * This program is free software; you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License version 2
20  * as published by the Free Software Foundation.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License along with
28  * this program; see the file COPYING.  If not, write to the Free Software
29  * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30  */
31 
32 #include <mconfig.h>
33 #include <stdio.h>
34 #include <standard.h>
35 #include <vadefs.h>
36 #include <stdxlib.h>
37 #include <strdefs.h>
38 #include <utypes.h>
39 #include <schily.h>
40 #include <ctype.h>
41 
42 struct disk {
43 	int	dummy;
44 };
45 
46 static	char	*skipwhite(const char *);
47 static	void	prt_std(char *, long, long, long, struct disk *);
48 BOOL	cvt_std(char *, long *, long, long, struct disk *);
49 extern	BOOL	getvalue(char *, long *, long, long,
50 								void (*)(char *, long, long, long, struct disk *),
51 								BOOL (*)(char *, long *, long, long, struct disk *),
52 								struct disk *);
53 extern	BOOL	getlong(char *, long *, long, long);
54 extern	BOOL	getint(char *, int *, int, int);
55 extern	BOOL	yes(char *, ...);
56 
57 static
skipwhite(const char * s)58 char *skipwhite(const char *s)
59 {
60 	register const Uchar	*p = (const Uchar *)s;
61 
62 	while (*p) {
63 		if (!isspace(*p))
64 			break;
65 		p++;
66 	}
67 	return ((char *)p);
68 }
69 
70 /* ARGSUSED */
71 
72 BOOL
cvt_std(char * linep,long * lp,long mini,long maxi,struct disk * dp)73 cvt_std(char *linep, long *lp, long mini, long maxi, struct disk *dp)
74 {
75 	long	l	= -1L;
76 
77 /*	printf("cvt_std(\"%s\", %d, %d, %d);\n", linep, *lp, mini, maxi);*/
78 
79 	if (linep[0] == '?') {
80 		printf("Enter a number in the range from %ld to %ld\n",
81 								mini, maxi);
82 		printf("The default radix is 10\n");
83 		printf("Precede number with '0x' for hexadecimal or with '0' for octal\n");
84 		printf("Shorthands are:\n");
85 		printf("\t'^' for minimum value (%ld)\n", mini);
86 		printf("\t'$' for maximum value (%ld)\n", maxi);
87 		printf("\t'+' for incrementing value to %ld\n", *lp + 1);
88 		printf("\t'-' for decrementing value to %ld\n", *lp - 1);
89 		return (FALSE);
90 	}
91 	if (linep[0] == '^' && *skipwhite(&linep[1]) == '\0') {
92 		l = mini;
93 	} else if (linep[0] == '$' && *skipwhite(&linep[1]) == '\0') {
94 		l = maxi;
95 	} else if (linep[0] == '+' && *skipwhite(&linep[1]) == '\0') {
96 		if (*lp < maxi)
97 			l = *lp + 1;
98 	} else if (linep[0] == '-' && *skipwhite(&linep[1]) == '\0') {
99 		if (*lp > mini)
100 			l = *lp - 1;
101 	} else if (*astol(linep, &l)) {
102 		printf("Not a number: '%s'.\n", linep);
103 		return (FALSE);
104 	}
105 	if (l < mini || l > maxi) {
106 		printf("'%s' is out of range.\n", linep);
107 		return (FALSE);
108 	}
109 	*lp = l;
110 	return (TRUE);
111 }
112 
113 /* ARGSUSED */
114 static void
prt_std(char * s,long l,long mini,long maxi,struct disk * dp)115 prt_std(char *s, long l, long mini, long maxi, struct disk *dp)
116 {
117 	printf("%s %ld (%ld - %ld)/<cr>:", s, l, mini, maxi);
118 }
119 
120 
getvalue(char * s,long * lp,long mini,long maxi,void (* prt)(char *,long,long,long,struct disk *),BOOL (* cvt)(char *,long *,long,long,struct disk *),struct disk * dp)121 BOOL getvalue(char *s, long *lp, long mini, long maxi,
122 				  void  (*prt)(char *, long, long, long, struct disk *),
123 	  			  BOOL  (*cvt)(char *, long *, long, long, struct disk *),
124 				  struct disk *dp)
125 {
126 	char	line[128];
127 	char	*linep;
128 
129 	for(;;) {
130 		(*prt)(s, *lp, mini, maxi, dp);
131 		flush();
132 		line[0] = '\0';
133 		if (rols_getline(line, 80) == EOF)
134 			exit(EX_BAD);
135 
136 		linep = skipwhite(line);
137 		/*
138 		 * Nicht initialisierte Variablen
139 		 * duerfen nicht uebernommen werden
140 		 */
141 		if (linep[0] == '\0' && *lp != -1L)
142 			return (FALSE);
143 
144 		if (strlen(linep) == 0) {
145 			/* Leere Eingabe */
146 			/* EMPTY */
147 		} else if ((*cvt)(linep, lp, mini, maxi, dp))
148 			return (TRUE);
149 	}
150 	/* NOTREACHED */
151 }
152 
153 
getlong(char * s,long * lp,long mini,long maxi)154 BOOL getlong(char *s, long *lp, long mini, long maxi)
155 {
156 	return (getvalue(s, lp, mini, maxi, prt_std, cvt_std, (void *)0));
157 }
158 
159 
getint(char * s,int * ip,int mini,int maxi)160 BOOL getint(char *s, int *ip, int mini, int maxi)
161 {
162 	long	l = *ip;
163 	BOOL	ret;
164 
165 	ret = getlong(s, &l, (long)mini, (long)maxi);
166 	*ip = l;
167 	return (ret);
168 }
169 
170 /* VARARGS1 */
yes(char * form,...)171 BOOL yes(char *form, ...)
172 {
173 	va_list	args;
174 	char okbuf[10];
175 
176 again:
177 	va_start(args, form);
178 	vprintf(form, args);
179 	va_end(args);
180 	flush();
181 	if (rols_getline(okbuf, sizeof(okbuf)) == EOF)
182 		exit(EX_BAD);
183 	if (okbuf[0] == '?') {
184 		printf("Enter 'y', 'Y', 'yes' or 'YES' if you agree with the previous asked question.\n");
185 		printf("All other input will be handled as if the question has beed answered with 'no'.\n");
186 		goto again;
187 	}
188 	if(streql(okbuf, "y") || streql(okbuf, "yes") ||
189 	   streql(okbuf, "Y") || streql(okbuf, "YES"))
190 		return(TRUE);
191 	else
192 		return(FALSE);
193 }
194