1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*0Sstevel@tonic-gate 
25*0Sstevel@tonic-gate 
26*0Sstevel@tonic-gate /*
27*0Sstevel@tonic-gate  * Copyright (c) 1997,1998 by Sun Microsystems, Inc.
28*0Sstevel@tonic-gate  * All rights reserved.
29*0Sstevel@tonic-gate  */
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*0Sstevel@tonic-gate /*LINTLIBRARY*/
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate #include <stdio.h>
35*0Sstevel@tonic-gate #include <string.h>
36*0Sstevel@tonic-gate #include <ctype.h>
37*0Sstevel@tonic-gate #include <limits.h>
38*0Sstevel@tonic-gate #include <sys/types.h>
39*0Sstevel@tonic-gate #include <stdlib.h>
40*0Sstevel@tonic-gate #include "libadm.h"
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate static int	fmtcheck(char *);
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate #define	PROMPT	"Enter the time of day"
45*0Sstevel@tonic-gate #define	ERRMSG	"Please enter the time of day. Format is"
46*0Sstevel@tonic-gate #define	DEFAULT	"%H:%M"
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate #define	TLEN 3
49*0Sstevel@tonic-gate #define	LH 00
50*0Sstevel@tonic-gate #define	UH 23
51*0Sstevel@tonic-gate #define	USH 12
52*0Sstevel@tonic-gate #define	LM 00
53*0Sstevel@tonic-gate #define	UM 59
54*0Sstevel@tonic-gate #define	LS 00
55*0Sstevel@tonic-gate #define	US 59
56*0Sstevel@tonic-gate #define	DELIM1 ':'
57*0Sstevel@tonic-gate #define	BLANK ' '
58*0Sstevel@tonic-gate #define	TAB '	'
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate static void
61*0Sstevel@tonic-gate setmsg(char *msg, char *fmt)
62*0Sstevel@tonic-gate {
63*0Sstevel@tonic-gate 	if (fmt == NULL)
64*0Sstevel@tonic-gate 		fmt = DEFAULT;
65*0Sstevel@tonic-gate 	(void) sprintf(msg, "%s <%s>.", ERRMSG, fmt);
66*0Sstevel@tonic-gate }
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate static char *
69*0Sstevel@tonic-gate p_ndig(char *string, int *value)
70*0Sstevel@tonic-gate {
71*0Sstevel@tonic-gate 	char *ptr;
72*0Sstevel@tonic-gate 	int accum = 0;
73*0Sstevel@tonic-gate 	int n = 2;
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate 	if (!string)
76*0Sstevel@tonic-gate 		return (0);
77*0Sstevel@tonic-gate 	for (ptr = string; *ptr && n > 0; n--, ptr++) {
78*0Sstevel@tonic-gate 		if (! isdigit((unsigned char)*ptr))
79*0Sstevel@tonic-gate 			return (NULL);
80*0Sstevel@tonic-gate 		accum = (10 * accum) + (*ptr - '0');
81*0Sstevel@tonic-gate 		}
82*0Sstevel@tonic-gate 	if (n)
83*0Sstevel@tonic-gate 		return (NULL);
84*0Sstevel@tonic-gate 	*value = accum;
85*0Sstevel@tonic-gate 	return (ptr);
86*0Sstevel@tonic-gate }
87*0Sstevel@tonic-gate 
88*0Sstevel@tonic-gate static char *
89*0Sstevel@tonic-gate p_time(char *string, int llim, int ulim)
90*0Sstevel@tonic-gate {
91*0Sstevel@tonic-gate 	char *ptr;
92*0Sstevel@tonic-gate 	int begin = -1;
93*0Sstevel@tonic-gate 	if (!(ptr = p_ndig(string, &begin)))
94*0Sstevel@tonic-gate 		return (NULL);
95*0Sstevel@tonic-gate 	if (begin >= llim && begin <= ulim)
96*0Sstevel@tonic-gate 		return (ptr);
97*0Sstevel@tonic-gate 	else return (NULL);
98*0Sstevel@tonic-gate }
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate /* p_meridian will parse the string for the meridian - AM/PM or am/pm */
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate static char *
103*0Sstevel@tonic-gate p_meridian(char *string)
104*0Sstevel@tonic-gate {
105*0Sstevel@tonic-gate 	static char *middle[] = { "AM", "PM", "am", "pm" };
106*0Sstevel@tonic-gate 	int legit, n;
107*0Sstevel@tonic-gate 	char mid[TLEN];
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate 	legit = 0;
110*0Sstevel@tonic-gate 	n = 0;
111*0Sstevel@tonic-gate 	mid[2] = '\0';
112*0Sstevel@tonic-gate 	(void) sscanf(string, "%2s", mid);
113*0Sstevel@tonic-gate 	while (!(legit) && (n < 4)) {
114*0Sstevel@tonic-gate 		if ((strncmp(mid, middle[n], 2)) == 0)
115*0Sstevel@tonic-gate 			legit = 1;	/* found legitimate string */
116*0Sstevel@tonic-gate 		n++;
117*0Sstevel@tonic-gate 	}
118*0Sstevel@tonic-gate 	if (legit)
119*0Sstevel@tonic-gate 		return (string+2);
120*0Sstevel@tonic-gate 	return (NULL);
121*0Sstevel@tonic-gate }
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate static char *
124*0Sstevel@tonic-gate p_delim(char *string, char dchoice)
125*0Sstevel@tonic-gate {
126*0Sstevel@tonic-gate 	char dlm;
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate 	if (! string)
129*0Sstevel@tonic-gate 		return (NULL);
130*0Sstevel@tonic-gate 	(void) sscanf(string, "%1c", &dlm);
131*0Sstevel@tonic-gate 	return ((dlm == dchoice) ? string + 1 : NULL);
132*0Sstevel@tonic-gate }
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate int
135*0Sstevel@tonic-gate cktime_val(char *fmt, char *input)
136*0Sstevel@tonic-gate {
137*0Sstevel@tonic-gate 	char ltrl, dfl;
138*0Sstevel@tonic-gate 	int valid = 1; 	/* time of day string is valid for format */
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate 	if ((fmt != NULL) && (fmtcheck(fmt) == 1))
141*0Sstevel@tonic-gate 		return (4);
142*0Sstevel@tonic-gate 
143*0Sstevel@tonic-gate 	if (fmt == NULL)
144*0Sstevel@tonic-gate 		fmt = DEFAULT;
145*0Sstevel@tonic-gate 	ltrl = '\0';
146*0Sstevel@tonic-gate 	while (*fmt && valid) {
147*0Sstevel@tonic-gate 		if ((*fmt) == '%') {
148*0Sstevel@tonic-gate 			switch (*++fmt) {
149*0Sstevel@tonic-gate 			    case 'H':
150*0Sstevel@tonic-gate 				input = p_time(input, LH, UH);
151*0Sstevel@tonic-gate 				if (!input)
152*0Sstevel@tonic-gate 					valid = 0;
153*0Sstevel@tonic-gate 				break;
154*0Sstevel@tonic-gate 
155*0Sstevel@tonic-gate 			    case 'M':
156*0Sstevel@tonic-gate 				input = p_time(input, LM, UM);
157*0Sstevel@tonic-gate 				if (!input)
158*0Sstevel@tonic-gate 					valid = 0;
159*0Sstevel@tonic-gate 				break;
160*0Sstevel@tonic-gate 
161*0Sstevel@tonic-gate 			    case 'S':
162*0Sstevel@tonic-gate 				input = p_time(input, LS, US);
163*0Sstevel@tonic-gate 				if (!input)
164*0Sstevel@tonic-gate 					valid = 0;
165*0Sstevel@tonic-gate 				break;
166*0Sstevel@tonic-gate 
167*0Sstevel@tonic-gate 			    case 'T':
168*0Sstevel@tonic-gate 				input = p_time(input, LH, UH);
169*0Sstevel@tonic-gate 				if (!input) {
170*0Sstevel@tonic-gate 					valid = 0;
171*0Sstevel@tonic-gate 					break;
172*0Sstevel@tonic-gate 				}
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate 				input = p_delim(input, DELIM1);
175*0Sstevel@tonic-gate 				if (!input) {
176*0Sstevel@tonic-gate 					valid = 0;
177*0Sstevel@tonic-gate 					break;
178*0Sstevel@tonic-gate 				}
179*0Sstevel@tonic-gate 				input = p_time(input, LM, UM);
180*0Sstevel@tonic-gate 				if (!input) {
181*0Sstevel@tonic-gate 					valid = 0;
182*0Sstevel@tonic-gate 					break;
183*0Sstevel@tonic-gate 				}
184*0Sstevel@tonic-gate 				input = p_delim(input, DELIM1);
185*0Sstevel@tonic-gate 				if (!input) {
186*0Sstevel@tonic-gate 					valid = 0;
187*0Sstevel@tonic-gate 					break;
188*0Sstevel@tonic-gate 				}
189*0Sstevel@tonic-gate 				input = p_time(input, LS, US);
190*0Sstevel@tonic-gate 				if (!input)
191*0Sstevel@tonic-gate 					valid = 0;
192*0Sstevel@tonic-gate 				break;
193*0Sstevel@tonic-gate 
194*0Sstevel@tonic-gate 			    case 'R':
195*0Sstevel@tonic-gate 				input = p_time(input, LH, UH);
196*0Sstevel@tonic-gate 				if (!input) {
197*0Sstevel@tonic-gate 					valid = 0;
198*0Sstevel@tonic-gate 					break;
199*0Sstevel@tonic-gate 				}
200*0Sstevel@tonic-gate 				input = p_delim(input, DELIM1);
201*0Sstevel@tonic-gate 				if (!input) {
202*0Sstevel@tonic-gate 					valid = 0;
203*0Sstevel@tonic-gate 					break;
204*0Sstevel@tonic-gate 				}
205*0Sstevel@tonic-gate 				input = p_time(input, LM, UM);
206*0Sstevel@tonic-gate 				if (!input) {
207*0Sstevel@tonic-gate 					valid = 0;
208*0Sstevel@tonic-gate 					break;
209*0Sstevel@tonic-gate 				}
210*0Sstevel@tonic-gate 				break;
211*0Sstevel@tonic-gate 
212*0Sstevel@tonic-gate 			    case 'r':
213*0Sstevel@tonic-gate 				input = p_time(input, LH, USH);
214*0Sstevel@tonic-gate 				if (!input) {
215*0Sstevel@tonic-gate 					valid = 0;
216*0Sstevel@tonic-gate 					break;
217*0Sstevel@tonic-gate 				}
218*0Sstevel@tonic-gate 				input = p_delim(input, DELIM1);
219*0Sstevel@tonic-gate 				if (!input) {
220*0Sstevel@tonic-gate 					valid = 0;
221*0Sstevel@tonic-gate 					break;
222*0Sstevel@tonic-gate 				}
223*0Sstevel@tonic-gate 				input = p_time(input, LM, UM);
224*0Sstevel@tonic-gate 				if (!input) {
225*0Sstevel@tonic-gate 					valid = 0;
226*0Sstevel@tonic-gate 					break;
227*0Sstevel@tonic-gate 				}
228*0Sstevel@tonic-gate 				input = p_delim(input, DELIM1);
229*0Sstevel@tonic-gate 				if (!input) {
230*0Sstevel@tonic-gate 					valid = 0;
231*0Sstevel@tonic-gate 					break;
232*0Sstevel@tonic-gate 				}
233*0Sstevel@tonic-gate 				input = p_time(input, LS, US);
234*0Sstevel@tonic-gate 				if (!input) {
235*0Sstevel@tonic-gate 					valid = 0;
236*0Sstevel@tonic-gate 					break;
237*0Sstevel@tonic-gate 				}
238*0Sstevel@tonic-gate 				input = p_delim(input, BLANK);
239*0Sstevel@tonic-gate 				if (!input) {
240*0Sstevel@tonic-gate 					valid = 0;
241*0Sstevel@tonic-gate 					break;
242*0Sstevel@tonic-gate 				}
243*0Sstevel@tonic-gate 				input = p_meridian(input);
244*0Sstevel@tonic-gate 				if (!input)
245*0Sstevel@tonic-gate 					valid = 0;
246*0Sstevel@tonic-gate 				break;
247*0Sstevel@tonic-gate 
248*0Sstevel@tonic-gate 			    case 'I':
249*0Sstevel@tonic-gate 				input = p_time(input, LH, USH);
250*0Sstevel@tonic-gate 				if (!input)
251*0Sstevel@tonic-gate 					valid = 0;
252*0Sstevel@tonic-gate 				break;
253*0Sstevel@tonic-gate 
254*0Sstevel@tonic-gate 			    case 'p':
255*0Sstevel@tonic-gate 				input = p_meridian(input);
256*0Sstevel@tonic-gate 				if (!input)
257*0Sstevel@tonic-gate 					valid = 0;
258*0Sstevel@tonic-gate 				break;
259*0Sstevel@tonic-gate 
260*0Sstevel@tonic-gate 			    default:
261*0Sstevel@tonic-gate 				(void) sscanf(input++, "%1c", &ltrl);
262*0Sstevel@tonic-gate 			}
263*0Sstevel@tonic-gate 		} else {
264*0Sstevel@tonic-gate 			dfl = '\0';
265*0Sstevel@tonic-gate 			(void) sscanf(input, "%1c", &dfl);
266*0Sstevel@tonic-gate 			input++;
267*0Sstevel@tonic-gate 		}
268*0Sstevel@tonic-gate 		fmt++;
269*0Sstevel@tonic-gate 	}
270*0Sstevel@tonic-gate 
271*0Sstevel@tonic-gate 	if (!(*fmt) && (input) && (*input))
272*0Sstevel@tonic-gate 		valid = 0;
273*0Sstevel@tonic-gate 
274*0Sstevel@tonic-gate 	return ((valid == 0));
275*0Sstevel@tonic-gate }
276*0Sstevel@tonic-gate 
277*0Sstevel@tonic-gate int
278*0Sstevel@tonic-gate cktime_err(char *fmt, char *error)
279*0Sstevel@tonic-gate {
280*0Sstevel@tonic-gate 	char	defmesg[128];
281*0Sstevel@tonic-gate 
282*0Sstevel@tonic-gate 	if ((fmt != NULL) && (fmtcheck(fmt) == 1))
283*0Sstevel@tonic-gate 		return (4);
284*0Sstevel@tonic-gate 	setmsg(defmesg, fmt);
285*0Sstevel@tonic-gate 	puterror(stdout, defmesg, error);
286*0Sstevel@tonic-gate 	return (0);
287*0Sstevel@tonic-gate }
288*0Sstevel@tonic-gate 
289*0Sstevel@tonic-gate int
290*0Sstevel@tonic-gate cktime_hlp(char *fmt, char *help)
291*0Sstevel@tonic-gate {
292*0Sstevel@tonic-gate 	char	defmesg[128];
293*0Sstevel@tonic-gate 
294*0Sstevel@tonic-gate 	if ((fmt != NULL) && (fmtcheck(fmt) == 1))
295*0Sstevel@tonic-gate 		return (4);
296*0Sstevel@tonic-gate 	setmsg(defmesg, fmt);
297*0Sstevel@tonic-gate 	puthelp(stdout, defmesg, help);
298*0Sstevel@tonic-gate 	return (0);
299*0Sstevel@tonic-gate }
300*0Sstevel@tonic-gate 
301*0Sstevel@tonic-gate /*
302*0Sstevel@tonic-gate  *	A little state machine that checks out the format to
303*0Sstevel@tonic-gate  *	make sure it is acceptable.
304*0Sstevel@tonic-gate  *		return value 1: NG
305*0Sstevel@tonic-gate  *		return value 0: OK
306*0Sstevel@tonic-gate  */
307*0Sstevel@tonic-gate int
308*0Sstevel@tonic-gate fmtcheck(char *fmt)
309*0Sstevel@tonic-gate {
310*0Sstevel@tonic-gate 	int	percent = 0;
311*0Sstevel@tonic-gate 
312*0Sstevel@tonic-gate 	while (*fmt) {
313*0Sstevel@tonic-gate 		switch (*fmt++) {
314*0Sstevel@tonic-gate 			case '%': /* previous state must be start or letter */
315*0Sstevel@tonic-gate 				if (percent == 0)
316*0Sstevel@tonic-gate 					percent = 1;
317*0Sstevel@tonic-gate 				else
318*0Sstevel@tonic-gate 					return (1);
319*0Sstevel@tonic-gate 				break;
320*0Sstevel@tonic-gate 			case 'H': /* previous state must be "%" */
321*0Sstevel@tonic-gate 			case 'M':
322*0Sstevel@tonic-gate 			case 'S':
323*0Sstevel@tonic-gate 			case 'T':
324*0Sstevel@tonic-gate 			case 'R':
325*0Sstevel@tonic-gate 			case 'r':
326*0Sstevel@tonic-gate 			case 'I':
327*0Sstevel@tonic-gate 			case 'p':
328*0Sstevel@tonic-gate 				if (percent == 1)
329*0Sstevel@tonic-gate 					percent = 0;
330*0Sstevel@tonic-gate 				else
331*0Sstevel@tonic-gate 					return (1);
332*0Sstevel@tonic-gate 				break;
333*0Sstevel@tonic-gate 			case TAB: /* previous state must be start or letter */
334*0Sstevel@tonic-gate 			case BLANK:
335*0Sstevel@tonic-gate 			case DELIM1:
336*0Sstevel@tonic-gate 				if (percent == 1)
337*0Sstevel@tonic-gate 					return (1);
338*0Sstevel@tonic-gate 				break;
339*0Sstevel@tonic-gate 			default:
340*0Sstevel@tonic-gate 				return (1);
341*0Sstevel@tonic-gate 		}
342*0Sstevel@tonic-gate 	}
343*0Sstevel@tonic-gate 	return (percent);
344*0Sstevel@tonic-gate }
345*0Sstevel@tonic-gate 
346*0Sstevel@tonic-gate int
347*0Sstevel@tonic-gate cktime(char *tod, char *fmt, char *defstr, char *error, char *help,
348*0Sstevel@tonic-gate 	char *prompt)
349*0Sstevel@tonic-gate {
350*0Sstevel@tonic-gate 	char	input[MAX_INPUT],
351*0Sstevel@tonic-gate 		defmesg[128];
352*0Sstevel@tonic-gate 
353*0Sstevel@tonic-gate 	if ((fmt != NULL) && (fmtcheck(fmt) == 1))
354*0Sstevel@tonic-gate 		return (4);
355*0Sstevel@tonic-gate 
356*0Sstevel@tonic-gate 	if (fmt == NULL)
357*0Sstevel@tonic-gate 		fmt = DEFAULT;
358*0Sstevel@tonic-gate 	setmsg(defmesg, fmt);
359*0Sstevel@tonic-gate 	if (!prompt)
360*0Sstevel@tonic-gate 		prompt = "Enter a time of day";
361*0Sstevel@tonic-gate 
362*0Sstevel@tonic-gate start:
363*0Sstevel@tonic-gate 	putprmpt(stderr, prompt, NULL, defstr);
364*0Sstevel@tonic-gate 	if (getinput(input))
365*0Sstevel@tonic-gate 		return (1);
366*0Sstevel@tonic-gate 
367*0Sstevel@tonic-gate 	if (!strlen(input)) {
368*0Sstevel@tonic-gate 		if (defstr) {
369*0Sstevel@tonic-gate 			(void) strcpy(tod, defstr);
370*0Sstevel@tonic-gate 			return (0);
371*0Sstevel@tonic-gate 		}
372*0Sstevel@tonic-gate 		puterror(stderr, defmesg, error);
373*0Sstevel@tonic-gate 		goto start;
374*0Sstevel@tonic-gate 	}
375*0Sstevel@tonic-gate 	if (strcmp(input, "?") == 0) {
376*0Sstevel@tonic-gate 		puthelp(stderr, defmesg, help);
377*0Sstevel@tonic-gate 		goto start;
378*0Sstevel@tonic-gate 	}
379*0Sstevel@tonic-gate 	if (ckquit && (strcmp(input, "q") == 0))
380*0Sstevel@tonic-gate 		return (3);
381*0Sstevel@tonic-gate 
382*0Sstevel@tonic-gate 	if (cktime_val(fmt, input)) {
383*0Sstevel@tonic-gate 		puterror(stderr, defmesg, error);
384*0Sstevel@tonic-gate 		goto start;
385*0Sstevel@tonic-gate 	}
386*0Sstevel@tonic-gate 	(void) strcpy(tod, input);
387*0Sstevel@tonic-gate 	return (0);
388*0Sstevel@tonic-gate }
389