17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 227c478bd9Sstevel@tonic-gate /* 23*78eb75caSchin * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 287c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 297c478bd9Sstevel@tonic-gate 307c478bd9Sstevel@tonic-gate 317c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 327c478bd9Sstevel@tonic-gate 337c478bd9Sstevel@tonic-gate /* 347c478bd9Sstevel@tonic-gate * /usr/lib/calprog produces an egrep -f file 357c478bd9Sstevel@tonic-gate * that will select today's and tomorrow's 367c478bd9Sstevel@tonic-gate * calendar entries, with special weekend provisions 377c478bd9Sstevel@tonic-gate * used by calendar command 387c478bd9Sstevel@tonic-gate */ 397c478bd9Sstevel@tonic-gate 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate #include <fcntl.h> 427c478bd9Sstevel@tonic-gate #include <stdarg.h> 437c478bd9Sstevel@tonic-gate #include <stdio.h> 447c478bd9Sstevel@tonic-gate #include <ctype.h> 457c478bd9Sstevel@tonic-gate #include <sys/types.h> 467c478bd9Sstevel@tonic-gate #include <time.h> 477c478bd9Sstevel@tonic-gate #include <sys/stat.h> 487c478bd9Sstevel@tonic-gate #include <locale.h> 497c478bd9Sstevel@tonic-gate #include <errno.h> 507c478bd9Sstevel@tonic-gate 517c478bd9Sstevel@tonic-gate 527c478bd9Sstevel@tonic-gate #define DAY (3600*24L) 537c478bd9Sstevel@tonic-gate 547c478bd9Sstevel@tonic-gate extern char *getenv(), *malloc(); 557c478bd9Sstevel@tonic-gate 567c478bd9Sstevel@tonic-gate static char *file; 577c478bd9Sstevel@tonic-gate static int old_behavior; 587c478bd9Sstevel@tonic-gate static int linenum = 1; 597c478bd9Sstevel@tonic-gate static time_t t; 607c478bd9Sstevel@tonic-gate static char errmsg[128]; 617c478bd9Sstevel@tonic-gate static char *errlst[] = { 627c478bd9Sstevel@tonic-gate /* 0 */ "error on open of \"%s\", errno = %d", 637c478bd9Sstevel@tonic-gate /* 1 */ "could not malloc enough memory", 647c478bd9Sstevel@tonic-gate /* 2 */ "error on stat of \"%s\", errno = %d", 657c478bd9Sstevel@tonic-gate /* 3 */ "file \"%s\" is not a regular file", 667c478bd9Sstevel@tonic-gate /* 4 */ "error in reading the file \"%s\"", 677c478bd9Sstevel@tonic-gate /* 5 */ "\"%s\" file: error on line %d", 687c478bd9Sstevel@tonic-gate /* 6 */ "\"%s\" file: format descriptions are missing" 697c478bd9Sstevel@tonic-gate }; 707c478bd9Sstevel@tonic-gate 717c478bd9Sstevel@tonic-gate static 727c478bd9Sstevel@tonic-gate char *month[] = { 737c478bd9Sstevel@tonic-gate "[Jj]an", 747c478bd9Sstevel@tonic-gate "[Ff]eb", 757c478bd9Sstevel@tonic-gate "[Mm]ar", 767c478bd9Sstevel@tonic-gate "[Aa]pr", 777c478bd9Sstevel@tonic-gate "[Mm]ay", 787c478bd9Sstevel@tonic-gate "[Jj]un", 797c478bd9Sstevel@tonic-gate "[Jj]ul", 807c478bd9Sstevel@tonic-gate "[Aa]ug", 817c478bd9Sstevel@tonic-gate "[Ss]ep", 827c478bd9Sstevel@tonic-gate "[Oo]ct", 837c478bd9Sstevel@tonic-gate "[Nn]ov", 847c478bd9Sstevel@tonic-gate "[Dd]ec" 857c478bd9Sstevel@tonic-gate }; 867c478bd9Sstevel@tonic-gate 87*78eb75caSchin static void read_tmpl(void); 887c478bd9Sstevel@tonic-gate static void error(const char *fmt, ...); 89*78eb75caSchin static void generate(char *); 907c478bd9Sstevel@tonic-gate 91*78eb75caSchin static void 92*78eb75caSchin tprint(time_t t) 937c478bd9Sstevel@tonic-gate { 947c478bd9Sstevel@tonic-gate struct tm *tm; 957c478bd9Sstevel@tonic-gate tm = localtime(&t); 967c478bd9Sstevel@tonic-gate (void) printf 977c478bd9Sstevel@tonic-gate ("(^|[ \t(,;])((%s[^ ]* *|0*%d/|\\*/)0*%d)([^0123456789]|$)\n", 987c478bd9Sstevel@tonic-gate month[tm->tm_mon], tm->tm_mon + 1, tm->tm_mday); 997c478bd9Sstevel@tonic-gate } 1007c478bd9Sstevel@tonic-gate 101*78eb75caSchin int 102*78eb75caSchin main(int argc, char *argv[]) 1037c478bd9Sstevel@tonic-gate { 1047c478bd9Sstevel@tonic-gate 1057c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 1067c478bd9Sstevel@tonic-gate (void) time(&t); 1077c478bd9Sstevel@tonic-gate if (((file = getenv("DATEMSK")) == 0) || file[0] == '\0') 1087c478bd9Sstevel@tonic-gate old_behavior = 1; 1097c478bd9Sstevel@tonic-gate if (old_behavior) 1107c478bd9Sstevel@tonic-gate tprint(t); 1117c478bd9Sstevel@tonic-gate else 1127c478bd9Sstevel@tonic-gate read_tmpl(); 1137c478bd9Sstevel@tonic-gate switch (localtime(&t)->tm_wday) { 1147c478bd9Sstevel@tonic-gate case 5: 1157c478bd9Sstevel@tonic-gate t += DAY; 1167c478bd9Sstevel@tonic-gate if (old_behavior) 1177c478bd9Sstevel@tonic-gate tprint(t); 1187c478bd9Sstevel@tonic-gate else 1197c478bd9Sstevel@tonic-gate read_tmpl(); 1207c478bd9Sstevel@tonic-gate case 6: 1217c478bd9Sstevel@tonic-gate t += DAY; 1227c478bd9Sstevel@tonic-gate if (old_behavior) 1237c478bd9Sstevel@tonic-gate tprint(t); 1247c478bd9Sstevel@tonic-gate else 1257c478bd9Sstevel@tonic-gate read_tmpl(); 1267c478bd9Sstevel@tonic-gate default: 1277c478bd9Sstevel@tonic-gate t += DAY; 1287c478bd9Sstevel@tonic-gate if (old_behavior) 1297c478bd9Sstevel@tonic-gate tprint(t); 1307c478bd9Sstevel@tonic-gate else 1317c478bd9Sstevel@tonic-gate read_tmpl(); 1327c478bd9Sstevel@tonic-gate } 133*78eb75caSchin return (0); 1347c478bd9Sstevel@tonic-gate } 1357c478bd9Sstevel@tonic-gate 1367c478bd9Sstevel@tonic-gate 137*78eb75caSchin static void 138*78eb75caSchin read_tmpl(void) 1397c478bd9Sstevel@tonic-gate { 1407c478bd9Sstevel@tonic-gate char *clean_line(); 1417c478bd9Sstevel@tonic-gate FILE *fp; 1427c478bd9Sstevel@tonic-gate char *bp, *start; 1437c478bd9Sstevel@tonic-gate struct stat sb; 1447c478bd9Sstevel@tonic-gate int no_empty = 0; 1457c478bd9Sstevel@tonic-gate 1467c478bd9Sstevel@tonic-gate if ((start = (char *)malloc(512)) == NULL) 1477c478bd9Sstevel@tonic-gate error(errlst[1]); 1487c478bd9Sstevel@tonic-gate if ((fp = fopen(file, "r")) == NULL) 1497c478bd9Sstevel@tonic-gate error(errlst[0], file, errno); 1507c478bd9Sstevel@tonic-gate if (fstat(fileno(fp), &sb) < 0) 1517c478bd9Sstevel@tonic-gate error(errlst[2], file, errno); 1527c478bd9Sstevel@tonic-gate if ((sb.st_mode & S_IFMT) != S_IFREG) 1537c478bd9Sstevel@tonic-gate error(errlst[3], file); 1547c478bd9Sstevel@tonic-gate for (;;) { 1557c478bd9Sstevel@tonic-gate bp = start; 1567c478bd9Sstevel@tonic-gate if (!fgets(bp, 512, fp)) { 1577c478bd9Sstevel@tonic-gate if (!feof(fp)) { 1587c478bd9Sstevel@tonic-gate free(start); 1597c478bd9Sstevel@tonic-gate fclose(fp); 1607c478bd9Sstevel@tonic-gate error(errlst[4], file); 1617c478bd9Sstevel@tonic-gate } 1627c478bd9Sstevel@tonic-gate break; 1637c478bd9Sstevel@tonic-gate } 1647c478bd9Sstevel@tonic-gate if (*(bp+strlen(bp)-1) != '\n') /* terminating newline? */ 1657c478bd9Sstevel@tonic-gate { 1667c478bd9Sstevel@tonic-gate free(start); 1677c478bd9Sstevel@tonic-gate fclose(fp); 1687c478bd9Sstevel@tonic-gate error(errlst[5], file, linenum); 1697c478bd9Sstevel@tonic-gate } 1707c478bd9Sstevel@tonic-gate bp = clean_line(bp); 1717c478bd9Sstevel@tonic-gate if (strlen(bp)) /* anything left? */ 1727c478bd9Sstevel@tonic-gate { 1737c478bd9Sstevel@tonic-gate no_empty++; 1747c478bd9Sstevel@tonic-gate generate(bp); 1757c478bd9Sstevel@tonic-gate } 1767c478bd9Sstevel@tonic-gate linenum++; 1777c478bd9Sstevel@tonic-gate } 1787c478bd9Sstevel@tonic-gate free(start); 1797c478bd9Sstevel@tonic-gate fclose(fp); 1807c478bd9Sstevel@tonic-gate if (!no_empty) 1817c478bd9Sstevel@tonic-gate error(errlst[6], file); 1827c478bd9Sstevel@tonic-gate } 1837c478bd9Sstevel@tonic-gate 1847c478bd9Sstevel@tonic-gate 1857c478bd9Sstevel@tonic-gate char * 186*78eb75caSchin clean_line(char *s) 1877c478bd9Sstevel@tonic-gate { 1887c478bd9Sstevel@tonic-gate char *ns; 1897c478bd9Sstevel@tonic-gate 1907c478bd9Sstevel@tonic-gate *(s + strlen(s) -1) = (char)0; /* delete newline */ 1917c478bd9Sstevel@tonic-gate if (!strlen(s)) 1927c478bd9Sstevel@tonic-gate return (s); 1937c478bd9Sstevel@tonic-gate ns = s + strlen(s) - 1; /* s->start; ns->end */ 1947c478bd9Sstevel@tonic-gate while ((ns != s) && (isspace(*ns))) { 1957c478bd9Sstevel@tonic-gate *ns = (char)0; /* delete terminating spaces */ 1967c478bd9Sstevel@tonic-gate --ns; 1977c478bd9Sstevel@tonic-gate } 1987c478bd9Sstevel@tonic-gate while (*s) /* delete beginning white spaces */ 1997c478bd9Sstevel@tonic-gate if (isspace(*s)) 2007c478bd9Sstevel@tonic-gate ++s; 2017c478bd9Sstevel@tonic-gate else 2027c478bd9Sstevel@tonic-gate break; 2037c478bd9Sstevel@tonic-gate return (s); 2047c478bd9Sstevel@tonic-gate } 2057c478bd9Sstevel@tonic-gate 2067c478bd9Sstevel@tonic-gate static void 2077c478bd9Sstevel@tonic-gate error(const char *fmt, ...) 2087c478bd9Sstevel@tonic-gate { 2097c478bd9Sstevel@tonic-gate va_list args; 2107c478bd9Sstevel@tonic-gate 2117c478bd9Sstevel@tonic-gate va_start(args, fmt); 2127c478bd9Sstevel@tonic-gate (void) vsnprintf(errmsg, sizeof (errmsg), fmt, args); 2137c478bd9Sstevel@tonic-gate fprintf(stderr, "%s\n", errmsg); 2147c478bd9Sstevel@tonic-gate va_end(args); 2157c478bd9Sstevel@tonic-gate exit(1); 2167c478bd9Sstevel@tonic-gate } 2177c478bd9Sstevel@tonic-gate 218*78eb75caSchin static void 219*78eb75caSchin generate(char *fmt) 2207c478bd9Sstevel@tonic-gate { 2217c478bd9Sstevel@tonic-gate char timebuf[1024]; 2227c478bd9Sstevel@tonic-gate char outbuf[2 * 1024]; 2237c478bd9Sstevel@tonic-gate char *tb, *ob; 2247c478bd9Sstevel@tonic-gate int space = 0; 2257c478bd9Sstevel@tonic-gate 2267c478bd9Sstevel@tonic-gate strftime(timebuf, sizeof (timebuf), fmt, localtime(&t)); 2277c478bd9Sstevel@tonic-gate tb = timebuf; 2287c478bd9Sstevel@tonic-gate ob = outbuf; 2297c478bd9Sstevel@tonic-gate while (*tb) 230*78eb75caSchin if (isspace(*tb)) { 2317c478bd9Sstevel@tonic-gate ++tb; 2327c478bd9Sstevel@tonic-gate space++; 2337c478bd9Sstevel@tonic-gate } 2347c478bd9Sstevel@tonic-gate else 2357c478bd9Sstevel@tonic-gate { 236*78eb75caSchin if (space) { 2377c478bd9Sstevel@tonic-gate *ob++ = '['; 2387c478bd9Sstevel@tonic-gate *ob++ = ' '; 2397c478bd9Sstevel@tonic-gate *ob++ = '\t'; 2407c478bd9Sstevel@tonic-gate *ob++ = ']'; 2417c478bd9Sstevel@tonic-gate *ob++ = '*'; 2427c478bd9Sstevel@tonic-gate space = 0; 2437c478bd9Sstevel@tonic-gate continue; 2447c478bd9Sstevel@tonic-gate } 245*78eb75caSchin if (isalpha(*tb)) { 2467c478bd9Sstevel@tonic-gate *ob++ = '['; 2477c478bd9Sstevel@tonic-gate *ob++ = toupper(*tb); 2487c478bd9Sstevel@tonic-gate *ob++ = tolower(*tb++); 2497c478bd9Sstevel@tonic-gate *ob++ = ']'; 2507c478bd9Sstevel@tonic-gate continue; 2517c478bd9Sstevel@tonic-gate } 2527c478bd9Sstevel@tonic-gate else 2537c478bd9Sstevel@tonic-gate *ob++ = *tb++; 2547c478bd9Sstevel@tonic-gate if (*(tb - 1) == '0') 2557c478bd9Sstevel@tonic-gate *ob++ = '*'; 2567c478bd9Sstevel@tonic-gate } 2577c478bd9Sstevel@tonic-gate *ob = '\0'; 2587c478bd9Sstevel@tonic-gate printf("%s\n", outbuf); 2597c478bd9Sstevel@tonic-gate } 260