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