xref: /onnv-gate/usr/src/cmd/calendar/calprog.c (revision 0:68f95e015346)
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 /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
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 
33*0Sstevel@tonic-gate /*
34*0Sstevel@tonic-gate  *	/usr/lib/calprog produces an egrep -f file
35*0Sstevel@tonic-gate  *	that will select today's and tomorrow's
36*0Sstevel@tonic-gate  *	calendar entries, with special weekend provisions
37*0Sstevel@tonic-gate  *	used by calendar command
38*0Sstevel@tonic-gate  */
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate #include <fcntl.h>
42*0Sstevel@tonic-gate #include <stdarg.h>
43*0Sstevel@tonic-gate #include <stdio.h>
44*0Sstevel@tonic-gate #include <ctype.h>
45*0Sstevel@tonic-gate #include <sys/types.h>
46*0Sstevel@tonic-gate #include <time.h>
47*0Sstevel@tonic-gate #include <sys/stat.h>
48*0Sstevel@tonic-gate #include <locale.h>
49*0Sstevel@tonic-gate #include <errno.h>
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate #define	DAY	(3600*24L)
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate extern  char	*getenv(), *malloc();
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate static char	*file;
57*0Sstevel@tonic-gate static int	old_behavior;
58*0Sstevel@tonic-gate static int	linenum = 1;
59*0Sstevel@tonic-gate static time_t	t;
60*0Sstevel@tonic-gate static char	errmsg[128];
61*0Sstevel@tonic-gate static char	*errlst[] = {
62*0Sstevel@tonic-gate /*	0	*/ "error on open of \"%s\", errno = %d",
63*0Sstevel@tonic-gate /*	1	*/ "could not malloc enough memory",
64*0Sstevel@tonic-gate /*	2	*/ "error on stat of \"%s\", errno = %d",
65*0Sstevel@tonic-gate /*	3	*/ "file \"%s\" is not a regular file",
66*0Sstevel@tonic-gate /*	4	*/ "error in reading the file \"%s\"",
67*0Sstevel@tonic-gate /*	5	*/ "\"%s\" file: error on line %d",
68*0Sstevel@tonic-gate /*	6	*/ "\"%s\" file: format descriptions are missing"
69*0Sstevel@tonic-gate };
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate static
72*0Sstevel@tonic-gate char *month[] = {
73*0Sstevel@tonic-gate 	"[Jj]an",
74*0Sstevel@tonic-gate 	"[Ff]eb",
75*0Sstevel@tonic-gate 	"[Mm]ar",
76*0Sstevel@tonic-gate 	"[Aa]pr",
77*0Sstevel@tonic-gate 	"[Mm]ay",
78*0Sstevel@tonic-gate 	"[Jj]un",
79*0Sstevel@tonic-gate 	"[Jj]ul",
80*0Sstevel@tonic-gate 	"[Aa]ug",
81*0Sstevel@tonic-gate 	"[Ss]ep",
82*0Sstevel@tonic-gate 	"[Oo]ct",
83*0Sstevel@tonic-gate 	"[Nn]ov",
84*0Sstevel@tonic-gate 	"[Dd]ec"
85*0Sstevel@tonic-gate };
86*0Sstevel@tonic-gate 
87*0Sstevel@tonic-gate static void error(const char *fmt, ...);
88*0Sstevel@tonic-gate 
89*0Sstevel@tonic-gate static
90*0Sstevel@tonic-gate tprint(t)
91*0Sstevel@tonic-gate time_t t;
92*0Sstevel@tonic-gate {
93*0Sstevel@tonic-gate 	struct tm *tm;
94*0Sstevel@tonic-gate 	tm = localtime(&t);
95*0Sstevel@tonic-gate 	(void) printf
96*0Sstevel@tonic-gate 		("(^|[ \t(,;])((%s[^ ]* *|0*%d/|\\*/)0*%d)([^0123456789]|$)\n",
97*0Sstevel@tonic-gate 		month[tm->tm_mon], tm->tm_mon + 1, tm->tm_mday);
98*0Sstevel@tonic-gate }
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate main()
101*0Sstevel@tonic-gate {
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
104*0Sstevel@tonic-gate 	(void) time(&t);
105*0Sstevel@tonic-gate 	if (((file = getenv("DATEMSK")) == 0) || file[0] == '\0')
106*0Sstevel@tonic-gate 		old_behavior = 1;
107*0Sstevel@tonic-gate 	if (old_behavior)
108*0Sstevel@tonic-gate 		tprint(t);
109*0Sstevel@tonic-gate 	else
110*0Sstevel@tonic-gate 		read_tmpl();
111*0Sstevel@tonic-gate 	switch (localtime(&t)->tm_wday) {
112*0Sstevel@tonic-gate 	case 5:
113*0Sstevel@tonic-gate 		t += DAY;
114*0Sstevel@tonic-gate 		if (old_behavior)
115*0Sstevel@tonic-gate 			tprint(t);
116*0Sstevel@tonic-gate 		else
117*0Sstevel@tonic-gate 			read_tmpl();
118*0Sstevel@tonic-gate 	case 6:
119*0Sstevel@tonic-gate 		t += DAY;
120*0Sstevel@tonic-gate 		if (old_behavior)
121*0Sstevel@tonic-gate 			tprint(t);
122*0Sstevel@tonic-gate 		else
123*0Sstevel@tonic-gate 			read_tmpl();
124*0Sstevel@tonic-gate 	default:
125*0Sstevel@tonic-gate 		t += DAY;
126*0Sstevel@tonic-gate 		if (old_behavior)
127*0Sstevel@tonic-gate 			tprint(t);
128*0Sstevel@tonic-gate 		else
129*0Sstevel@tonic-gate 			read_tmpl();
130*0Sstevel@tonic-gate 	}
131*0Sstevel@tonic-gate 	exit(0);
132*0Sstevel@tonic-gate }
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate 
135*0Sstevel@tonic-gate static
136*0Sstevel@tonic-gate read_tmpl()
137*0Sstevel@tonic-gate {
138*0Sstevel@tonic-gate 	char	*clean_line();
139*0Sstevel@tonic-gate 	FILE  *fp;
140*0Sstevel@tonic-gate 	char *bp, *start;
141*0Sstevel@tonic-gate 	struct stat sb;
142*0Sstevel@tonic-gate 	int	no_empty = 0;
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate 	if ((start = (char *)malloc(512)) == NULL)
145*0Sstevel@tonic-gate 		error(errlst[1]);
146*0Sstevel@tonic-gate 	if ((fp = fopen(file, "r")) == NULL)
147*0Sstevel@tonic-gate 		error(errlst[0], file, errno);
148*0Sstevel@tonic-gate 	if (fstat(fileno(fp), &sb) < 0)
149*0Sstevel@tonic-gate 		error(errlst[2], file, errno);
150*0Sstevel@tonic-gate 	if ((sb.st_mode & S_IFMT) != S_IFREG)
151*0Sstevel@tonic-gate 		error(errlst[3], file);
152*0Sstevel@tonic-gate 	for (;;) {
153*0Sstevel@tonic-gate 		bp = start;
154*0Sstevel@tonic-gate 		if (!fgets(bp, 512, fp)) {
155*0Sstevel@tonic-gate 			if (!feof(fp)) {
156*0Sstevel@tonic-gate 				free(start);
157*0Sstevel@tonic-gate 				fclose(fp);
158*0Sstevel@tonic-gate 				error(errlst[4], file);
159*0Sstevel@tonic-gate 				}
160*0Sstevel@tonic-gate 			break;
161*0Sstevel@tonic-gate 		}
162*0Sstevel@tonic-gate 		if (*(bp+strlen(bp)-1) != '\n')   /* terminating newline? */
163*0Sstevel@tonic-gate 			{
164*0Sstevel@tonic-gate 			free(start);
165*0Sstevel@tonic-gate 			fclose(fp);
166*0Sstevel@tonic-gate 			error(errlst[5], file, linenum);
167*0Sstevel@tonic-gate 			}
168*0Sstevel@tonic-gate 		bp = clean_line(bp);
169*0Sstevel@tonic-gate 		if (strlen(bp))  /*  anything left?  */
170*0Sstevel@tonic-gate 			{
171*0Sstevel@tonic-gate 			no_empty++;
172*0Sstevel@tonic-gate 			generate(bp);
173*0Sstevel@tonic-gate 			}
174*0Sstevel@tonic-gate 	linenum++;
175*0Sstevel@tonic-gate 	}
176*0Sstevel@tonic-gate 	free(start);
177*0Sstevel@tonic-gate 	fclose(fp);
178*0Sstevel@tonic-gate 	if (!no_empty)
179*0Sstevel@tonic-gate 		error(errlst[6], file);
180*0Sstevel@tonic-gate }
181*0Sstevel@tonic-gate 
182*0Sstevel@tonic-gate 
183*0Sstevel@tonic-gate char  *
184*0Sstevel@tonic-gate clean_line(s)
185*0Sstevel@tonic-gate char *s;
186*0Sstevel@tonic-gate {
187*0Sstevel@tonic-gate 	char  *ns;
188*0Sstevel@tonic-gate 
189*0Sstevel@tonic-gate 	*(s + strlen(s) -1) = (char)0; /* delete newline */
190*0Sstevel@tonic-gate 	if (!strlen(s))
191*0Sstevel@tonic-gate 		return (s);
192*0Sstevel@tonic-gate 	ns = s + strlen(s) - 1; /* s->start; ns->end */
193*0Sstevel@tonic-gate 	while ((ns != s) && (isspace(*ns))) {
194*0Sstevel@tonic-gate 		*ns = (char)0;	/* delete terminating spaces */
195*0Sstevel@tonic-gate 		--ns;
196*0Sstevel@tonic-gate 		}
197*0Sstevel@tonic-gate 	while (*s)		/* delete beginning white spaces */
198*0Sstevel@tonic-gate 		if (isspace(*s))
199*0Sstevel@tonic-gate 			++s;
200*0Sstevel@tonic-gate 		else
201*0Sstevel@tonic-gate 			break;
202*0Sstevel@tonic-gate 	return (s);
203*0Sstevel@tonic-gate }
204*0Sstevel@tonic-gate 
205*0Sstevel@tonic-gate static void
206*0Sstevel@tonic-gate error(const char *fmt, ...)
207*0Sstevel@tonic-gate {
208*0Sstevel@tonic-gate 	va_list	args;
209*0Sstevel@tonic-gate 
210*0Sstevel@tonic-gate 	va_start(args, fmt);
211*0Sstevel@tonic-gate 	(void) vsnprintf(errmsg, sizeof (errmsg), fmt, args);
212*0Sstevel@tonic-gate 	fprintf(stderr, "%s\n", errmsg);
213*0Sstevel@tonic-gate 	va_end(args);
214*0Sstevel@tonic-gate 	exit(1);
215*0Sstevel@tonic-gate }
216*0Sstevel@tonic-gate 
217*0Sstevel@tonic-gate static
218*0Sstevel@tonic-gate generate(fmt)
219*0Sstevel@tonic-gate char *fmt;
220*0Sstevel@tonic-gate {
221*0Sstevel@tonic-gate 	char	timebuf[1024];
222*0Sstevel@tonic-gate 	char	outbuf[2 * 1024];
223*0Sstevel@tonic-gate 	char	*tb, *ob;
224*0Sstevel@tonic-gate 	int	space = 0;
225*0Sstevel@tonic-gate 
226*0Sstevel@tonic-gate 	strftime(timebuf, sizeof (timebuf), fmt, localtime(&t));
227*0Sstevel@tonic-gate 	tb = timebuf;
228*0Sstevel@tonic-gate 	ob = outbuf;
229*0Sstevel@tonic-gate 	while (*tb)
230*0Sstevel@tonic-gate 		if (isspace(*tb))
231*0Sstevel@tonic-gate 			{
232*0Sstevel@tonic-gate 			++tb;
233*0Sstevel@tonic-gate 			space++;
234*0Sstevel@tonic-gate 			}
235*0Sstevel@tonic-gate 		else
236*0Sstevel@tonic-gate 			{
237*0Sstevel@tonic-gate 			if (space)
238*0Sstevel@tonic-gate 				{
239*0Sstevel@tonic-gate 				*ob++ = '[';
240*0Sstevel@tonic-gate 				*ob++ = ' ';
241*0Sstevel@tonic-gate 				*ob++ = '\t';
242*0Sstevel@tonic-gate 				*ob++ = ']';
243*0Sstevel@tonic-gate 				*ob++ = '*';
244*0Sstevel@tonic-gate 				space = 0;
245*0Sstevel@tonic-gate 				continue;
246*0Sstevel@tonic-gate 				}
247*0Sstevel@tonic-gate 			if (isalpha(*tb))
248*0Sstevel@tonic-gate 				{
249*0Sstevel@tonic-gate 				*ob++ = '[';
250*0Sstevel@tonic-gate 				*ob++ = toupper(*tb);
251*0Sstevel@tonic-gate 				*ob++ = tolower(*tb++);
252*0Sstevel@tonic-gate 				*ob++ = ']';
253*0Sstevel@tonic-gate 				continue;
254*0Sstevel@tonic-gate 				}
255*0Sstevel@tonic-gate 			else
256*0Sstevel@tonic-gate 				*ob++ = *tb++;
257*0Sstevel@tonic-gate 				if (*(tb - 1) == '0')
258*0Sstevel@tonic-gate 					*ob++ = '*';
259*0Sstevel@tonic-gate 			}
260*0Sstevel@tonic-gate 	*ob = '\0';
261*0Sstevel@tonic-gate 	printf("%s\n", outbuf);
262*0Sstevel@tonic-gate }
263