summaryrefslogtreecommitdiff
path: root/usr/src/cmd/calendar/calprog.c
blob: 7fadc9204c6df61d094520d7094733877a050aa4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
/*	  All Rights Reserved  	*/


#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*
 *	/usr/lib/calprog produces an egrep -f file
 *	that will select today's and tomorrow's
 *	calendar entries, with special weekend provisions
 *	used by calendar command
 */


#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <ctype.h>
#include <sys/types.h>
#include <time.h>
#include <sys/stat.h>
#include <locale.h>
#include <errno.h>


#define	DAY	(3600*24L)

extern  char	*getenv(), *malloc();

static char	*file;
static int	old_behavior;
static int	linenum = 1;
static time_t	t;
static char	errmsg[128];
static char	*errlst[] = {
/*	0	*/ "error on open of \"%s\", errno = %d",
/*	1	*/ "could not malloc enough memory",
/*	2	*/ "error on stat of \"%s\", errno = %d",
/*	3	*/ "file \"%s\" is not a regular file",
/*	4	*/ "error in reading the file \"%s\"",
/*	5	*/ "\"%s\" file: error on line %d",
/*	6	*/ "\"%s\" file: format descriptions are missing"
};

static
char *month[] = {
	"[Jj]an",
	"[Ff]eb",
	"[Mm]ar",
	"[Aa]pr",
	"[Mm]ay",
	"[Jj]un",
	"[Jj]ul",
	"[Aa]ug",
	"[Ss]ep",
	"[Oo]ct",
	"[Nn]ov",
	"[Dd]ec"
};

static void read_tmpl(void);
static void error(const char *fmt, ...);
static void generate(char *);

static void
tprint(time_t t)
{
	struct tm *tm;
	tm = localtime(&t);
	(void) printf
		("(^|[ \t(,;])((%s[^ ]* *|0*%d/|\\*/)0*%d)([^0123456789]|$)\n",
		month[tm->tm_mon], tm->tm_mon + 1, tm->tm_mday);
}

int
main(int argc, char *argv[])
{

	(void) setlocale(LC_ALL, "");
	(void) time(&t);
	if (((file = getenv("DATEMSK")) == 0) || file[0] == '\0')
		old_behavior = 1;
	if (old_behavior)
		tprint(t);
	else
		read_tmpl();
	switch (localtime(&t)->tm_wday) {
	case 5:
		t += DAY;
		if (old_behavior)
			tprint(t);
		else
			read_tmpl();
	case 6:
		t += DAY;
		if (old_behavior)
			tprint(t);
		else
			read_tmpl();
	default:
		t += DAY;
		if (old_behavior)
			tprint(t);
		else
			read_tmpl();
	}
	return (0);
}


static void
read_tmpl(void)
{
	char	*clean_line();
	FILE  *fp;
	char *bp, *start;
	struct stat sb;
	int	no_empty = 0;

	if ((start = (char *)malloc(512)) == NULL)
		error(errlst[1]);
	if ((fp = fopen(file, "r")) == NULL)
		error(errlst[0], file, errno);
	if (fstat(fileno(fp), &sb) < 0)
		error(errlst[2], file, errno);
	if ((sb.st_mode & S_IFMT) != S_IFREG)
		error(errlst[3], file);
	for (;;) {
		bp = start;
		if (!fgets(bp, 512, fp)) {
			if (!feof(fp)) {
				free(start);
				fclose(fp);
				error(errlst[4], file);
				}
			break;
		}
		if (*(bp+strlen(bp)-1) != '\n')   /* terminating newline? */
			{
			free(start);
			fclose(fp);
			error(errlst[5], file, linenum);
			}
		bp = clean_line(bp);
		if (strlen(bp))  /*  anything left?  */
			{
			no_empty++;
			generate(bp);
			}
	linenum++;
	}
	free(start);
	fclose(fp);
	if (!no_empty)
		error(errlst[6], file);
}


char  *
clean_line(char *s)
{
	char  *ns;

	*(s + strlen(s) -1) = (char)0; /* delete newline */
	if (!strlen(s))
		return (s);
	ns = s + strlen(s) - 1; /* s->start; ns->end */
	while ((ns != s) && (isspace(*ns))) {
		*ns = (char)0;	/* delete terminating spaces */
		--ns;
		}
	while (*s)		/* delete beginning white spaces */
		if (isspace(*s))
			++s;
		else
			break;
	return (s);
}

static void
error(const char *fmt, ...)
{
	va_list	args;

	va_start(args, fmt);
	(void) vsnprintf(errmsg, sizeof (errmsg), fmt, args);
	fprintf(stderr, "%s\n", errmsg);
	va_end(args);
	exit(1);
}

static void
generate(char *fmt)
{
	char	timebuf[1024];
	char	outbuf[2 * 1024];
	char	*tb, *ob;
	int	space = 0;

	strftime(timebuf, sizeof (timebuf), fmt, localtime(&t));
	tb = timebuf;
	ob = outbuf;
	while (*tb)
		if (isspace(*tb)) {
			++tb;
			space++;
		}
		else
			{
			if (space) {
				*ob++ = '[';
				*ob++ = ' ';
				*ob++ = '\t';
				*ob++ = ']';
				*ob++ = '*';
				space = 0;
				continue;
			}
			if (isalpha(*tb)) {
				*ob++ = '[';
				*ob++ = toupper(*tb);
				*ob++ = tolower(*tb++);
				*ob++ = ']';
				continue;
			}
			else
				*ob++ = *tb++;
				if (*(tb - 1) == '0')
					*ob++ = '*';
			}
	*ob = '\0';
	printf("%s\n", outbuf);
}