void settimes(time_t now, int before, int after, int friday, struct tm *tp1, struct tm *tp2) { char *oldl, *lbufp; struct tm tp; localtime_r(&now, &tp); /* Friday displays Monday's events */ if (after == 0 && before == 0 && friday != -1) after = tp.tm_wday == friday ? 3 : 1; time1 = now - SECSPERDAY * before; localtime_r(&time1, tp1); year1 = 1900 + tp1->tm_year; time2 = now + SECSPERDAY * after; localtime_r(&time2, tp2); year2 = 1900 + tp2->tm_year; strftime(dayname, sizeof(dayname) - 1, "%A, %d %B %Y", tp1); oldl = NULL; lbufp = setlocale(LC_TIME, NULL); if (lbufp != NULL && (oldl = strdup(lbufp)) == NULL) errx(1, "cannot allocate memory"); (void)setlocale(LC_TIME, "C"); (void)setlocale(LC_TIME, (oldl != NULL ? oldl : "")); if (oldl != NULL) free(oldl); setnnames(); }
void cal(void) { int printing; char *p; FILE *fp; int ch, l; int month; int day; int var = 0; /* avoid gcc warnings */ static int d_first = -1; char buf[2048 + 1]; struct event *events = NULL; if ((fp = opencal()) == NULL) return; for (printing = 0; fgets(buf, sizeof(buf), stdin) != NULL;) { if ((p = strchr(buf, '\n')) != NULL) *p = '\0'; else while ((ch = getchar()) != '\n' && ch != EOF); for (l = strlen(buf); l > 0 && isspace((unsigned char)buf[l - 1]); l--) ; buf[l] = '\0'; if (buf[0] == '\0') continue; if (strncmp(buf, "LANG=", 5) == 0) { setlocale(LC_ALL, buf + 5); d_first = (*nl_langinfo(D_MD_ORDER) == 'd'); setnnames(); continue; } if (strncasecmp(buf, "Easter=", 7) == 0 && buf[7]) { if (neaster.name != NULL) free(neaster.name); if ((neaster.name = strdup(buf + 7)) == NULL) errx(EXIT_FAILURE, "cannot allocate memory"); neaster.len = strlen(buf + 7); continue; } if (strncasecmp(buf, "Paskha=", 7) == 0 && buf[7]) { if (npaskha.name != NULL) free(npaskha.name); if ((npaskha.name = strdup(buf + 7)) == NULL) errx(EXIT_FAILURE, "cannot allocate memory"); npaskha.len = strlen(buf + 7); continue; } if (buf[0] != '\t') { printing = isnow(buf, &month, &day, &var) ? 1 : 0; if ((p = strchr(buf, '\t')) == NULL) continue; if (p > buf && p[-1] == '*') var = 1; if (printing) { struct tm tm; char dbuf[80]; if (d_first < 0) d_first = (*nl_langinfo(D_MD_ORDER) == 'd'); tm.tm_sec = 0; /* unused */ tm.tm_min = 0; /* unused */ tm.tm_hour = 0; /* unused */ tm.tm_wday = 0; /* unused */ tm.tm_mon = month - 1; tm.tm_mday = day; tm.tm_year = tp->tm_year; /* unused */ strftime(dbuf, sizeof(dbuf), d_first ? "%e %b" : "%b %e", &tm); events = event_add(events, month, day, dbuf, var, p); } } else if (printing) event_continue(events, buf); } event_print_all(fp, events); closecal(fp); }
void cal(void) { char *pp, p; FILE *fp; int ch, l; int count, i; int month[MAXCOUNT]; int day[MAXCOUNT]; int year[MAXCOUNT]; char **extradata; /* strings of 20 length */ int flags; static int d_first = -1; char buf[2048 + 1]; struct event *events[MAXCOUNT]; struct tm tm; char dbuf[80]; extradata = (char **)calloc(MAXCOUNT, sizeof(char *)); for (i = 0; i < MAXCOUNT; i++) { extradata[i] = (char *)calloc(1, 20); } /* Unused */ tm.tm_sec = 0; tm.tm_min = 0; tm.tm_hour = 0; tm.tm_wday = 0; count = 0; if ((fp = opencal()) == NULL) { free(extradata); return; } while (fgets(buf, sizeof(buf), stdin) != NULL) { if ((pp = strchr(buf, '\n')) != NULL) *pp = '\0'; else /* Flush this line */ while ((ch = getchar()) != '\n' && ch != EOF); for (l = strlen(buf); l > 0 && isspace((unsigned char)buf[l - 1]); l--) ; buf[l] = '\0'; if (buf[0] == '\0') continue; /* Parse special definitions: LANG, Easter, Paskha etc */ if (strncmp(buf, "LANG=", 5) == 0) { (void)setlocale(LC_ALL, buf + 5); d_first = (*nl_langinfo(D_MD_ORDER) == 'd'); setnnames(); continue; } REPLACE("Easter=", 7, neaster); REPLACE("Paskha=", 7, npaskha); REPLACE("ChineseNewYear=", 15, ncny); REPLACE("NewMoon=", 8, nnewmoon); REPLACE("FullMoon=", 9, nfullmoon); REPLACE("MarEquinox=", 11, nmarequinox); REPLACE("SepEquinox=", 11, nsepequinox); REPLACE("JunSolstice=", 12, njunsolstice); REPLACE("DecSolstice=", 12, ndecsolstice); if (strncmp(buf, "SEQUENCE=", 9) == 0) { setnsequences(buf + 9); continue; } /* * If the line starts with a tab, the data has to be * added to the previous line */ if (buf[0] == '\t') { for (i = 0; i < count; i++) event_continue(events[i], buf); continue; } /* Get rid of leading spaces (non-standard) */ while (isspace((unsigned char)buf[0])) memcpy(buf, buf + 1, strlen(buf)); /* No tab in the line, then not a valid line */ if ((pp = strchr(buf, '\t')) == NULL) continue; /* Trim spaces in front of the tab */ while (isspace((unsigned char)pp[-1])) pp--; p = *pp; *pp = '\0'; if ((count = parsedaymonth(buf, year, month, day, &flags, extradata)) == 0) continue; *pp = p; if (count < 0) { /* Show error status based on return value */ fprintf(stderr, "Ignored: %s\n", buf); if (count == -1) continue; count = -count + 1; } /* Find the last tab */ while (pp[1] == '\t') pp++; if (d_first < 0) d_first = (*nl_langinfo(D_MD_ORDER) == 'd'); for (i = 0; i < count; i++) { tm.tm_mon = month[i] - 1; tm.tm_mday = day[i]; tm.tm_year = year[i] - 1900; (void)strftime(dbuf, sizeof(dbuf), d_first ? "%e %b" : "%b %e", &tm); if (debug) fprintf(stderr, "got %s\n", pp); events[i] = event_add(year[i], month[i], day[i], dbuf, ((flags &= F_VARIABLE) != 0) ? 1 : 0, pp, extradata[i]); } } event_print_all(fp); closecal(fp); free(extradata); }
void cal(void) { int ch, l, i, bodun = 0, bodun_maybe = 0, var, printing; struct event *events, *cur_evt, *ev1, *tmp; char buf[2048 + 1], *prefix = NULL, *p; struct match *m; FILE *fp; events = NULL; cur_evt = NULL; if ((fp = opencal()) == NULL) return; for (printing = 0; fgets(buf, sizeof(buf), stdin) != NULL;) { if ((p = strchr(buf, '\n')) != NULL) *p = '\0'; else while ((ch = getchar()) != '\n' && ch != EOF); for (l = strlen(buf); l > 0 && isspace(buf[l - 1]); l--) ; buf[l] = '\0'; if (buf[0] == '\0') continue; if (strncmp(buf, "LANG=", 5) == 0) { (void) setlocale(LC_ALL, buf + 5); setnnames(); if (!strcmp(buf + 5, "ru_RU.UTF-8") || !strcmp(buf + 5, "uk_UA.UTF-8") || !strcmp(buf + 5, "by_BY.UTF-8")) { bodun_maybe++; bodun = 0; free(prefix); prefix = NULL; } else bodun_maybe = 0; continue; } else if (strncmp(buf, "CALENDAR=", 9) == 0) { char *ep; if (buf[9] == '\0') calendar = 0; else if (!strcasecmp(buf + 9, "julian")) { calendar = JULIAN; errno = 0; julian = strtoul(buf + 14, &ep, 10); if (buf[0] == '\0' || *ep != '\0') julian = 13; if ((errno == ERANGE && julian == ULONG_MAX) || julian > 14) errx(1, "Julian calendar offset is too large"); } else if (!strcasecmp(buf + 9, "gregorian")) calendar = GREGORIAN; else if (!strcasecmp(buf + 9, "lunar")) calendar = LUNAR; } else if (bodun_maybe && strncmp(buf, "BODUN=", 6) == 0) { bodun++; free(prefix); if ((prefix = strdup(buf + 6)) == NULL) err(1, NULL); continue; } /* User defined names for special events */ if ((p = strchr(buf, '='))) { for (i = 0; i < NUMEV; i++) { if (strncasecmp(buf, spev[i].name, spev[i].nlen) == 0 && (p - buf == spev[i].nlen) && buf[spev[i].nlen + 1]) { p++; free(spev[i].uname); if ((spev[i].uname = strdup(p)) == NULL) err(1, NULL); spev[i].ulen = strlen(p); i = NUMEV + 1; } } if (i > NUMEV) continue; } if (buf[0] != '\t') { printing = (m = isnow(buf, bodun)) ? 1 : 0; if ((p = strchr(buf, '\t')) == NULL) { printing = 0; continue; } /* Need the following to catch hardwired "variable" * dates */ if (p > buf && p[-1] == '*') var = 1; else var = 0; if (printing) { struct match *foo; ev1 = NULL; while (m) { cur_evt = malloc(sizeof(struct event)); if (cur_evt == NULL) err(1, NULL); cur_evt->when = m->when; snprintf(cur_evt->print_date, sizeof(cur_evt->print_date), "%s%c", m->print_date, (var + m->var) ? '*' : ' '); if (ev1) { cur_evt->desc = ev1->desc; cur_evt->ldesc = NULL; } else { if (m->bodun && prefix) { if (asprintf(&cur_evt->ldesc, "\t%s %s", prefix, p + 1) == -1) err(1, NULL); } else if ((cur_evt->ldesc = strdup(p)) == NULL) err(1, NULL); cur_evt->desc = &(cur_evt->ldesc); ev1 = cur_evt; } insert(&events, cur_evt); foo = m; m = m->next; free(foo); } } } else if (printing) { if (asprintf(&p, "%s\n%s", ev1->ldesc, buf) == -1) err(1, NULL); free(ev1->ldesc); ev1->ldesc = p; } } tmp = events; while (tmp) { (void)fprintf(fp, "%s%s\n", tmp->print_date, *(tmp->desc)); tmp = tmp->next; } tmp = events; while (tmp) { events = tmp; free(tmp->ldesc); tmp = tmp->next; free(events); } closecal(fp); }
static int cal_parse(FILE *in, FILE *out) { char *line = NULL; char *buf; size_t linecap = 0; ssize_t linelen; ssize_t l; static int d_first = -1; static int count = 0; int i; int month[MAXCOUNT]; int day[MAXCOUNT]; int year[MAXCOUNT]; bool skip = false; char dbuf[80]; char *pp, p; struct tm tm; int flags; /* Unused */ tm.tm_sec = 0; tm.tm_min = 0; tm.tm_hour = 0; tm.tm_wday = 0; if (in == NULL) return (1); while ((linelen = getline(&line, &linecap, in)) > 0) { if (*line == '#') { switch (token(line+1, out, &skip)) { case T_ERR: free(line); return (1); case T_OK: continue; case T_PROCESS: break; default: break; } } if (skip) continue; buf = line; for (l = linelen; l > 0 && isspace((unsigned char)buf[l - 1]); l--) ; buf[l] = '\0'; if (buf[0] == '\0') continue; /* Parse special definitions: LANG, Easter, Paskha etc */ if (strncmp(buf, "LANG=", 5) == 0) { (void)setlocale(LC_ALL, buf + 5); d_first = (*nl_langinfo(D_MD_ORDER) == 'd'); setnnames(); continue; } REPLACE("Easter=", 7, neaster); REPLACE("Paskha=", 7, npaskha); REPLACE("ChineseNewYear=", 15, ncny); REPLACE("NewMoon=", 8, nnewmoon); REPLACE("FullMoon=", 9, nfullmoon); REPLACE("MarEquinox=", 11, nmarequinox); REPLACE("SepEquinox=", 11, nsepequinox); REPLACE("JunSolstice=", 12, njunsolstice); REPLACE("DecSolstice=", 12, ndecsolstice); if (strncmp(buf, "SEQUENCE=", 9) == 0) { setnsequences(buf + 9); continue; } /* * If the line starts with a tab, the data has to be * added to the previous line */ if (buf[0] == '\t') { for (i = 0; i < count; i++) event_continue(events[i], buf); continue; } /* Get rid of leading spaces (non-standard) */ while (isspace((unsigned char)buf[0])) memcpy(buf, buf + 1, strlen(buf)); /* No tab in the line, then not a valid line */ if ((pp = strchr(buf, '\t')) == NULL) continue; /* Trim spaces in front of the tab */ while (isspace((unsigned char)pp[-1])) pp--; p = *pp; *pp = '\0'; if ((count = parsedaymonth(buf, year, month, day, &flags, extradata)) == 0) continue; *pp = p; if (count < 0) { /* Show error status based on return value */ if (debug) fprintf(stderr, "Ignored: %s\n", buf); if (count == -1) continue; count = -count + 1; } /* Find the last tab */ while (pp[1] == '\t') pp++; if (d_first < 0) d_first = (*nl_langinfo(D_MD_ORDER) == 'd'); for (i = 0; i < count; i++) { tm.tm_mon = month[i] - 1; tm.tm_mday = day[i]; tm.tm_year = year[i] - 1900; (void)strftime(dbuf, sizeof(dbuf), d_first ? "%e %b" : "%b %e", &tm); if (debug) fprintf(stderr, "got %s\n", pp); events[i] = event_add(year[i], month[i], day[i], dbuf, ((flags &= F_VARIABLE) != 0) ? 1 : 0, pp, extradata[i]); } } free(line); fclose(in); return (0); }