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