Exemplo n.º 1
0
static void
cal(void)
{
	bool printing;
	FILE *fp, *in = NULL;
	char *line;

	if ((fp = opencal(&in)) == NULL || in == NULL)
		return;
	printing = false;
	while ((line = fparseln(in,
		    NULL, NULL, NULL, FPARSELN_UNESCCOMM)) != NULL) {
		if (line[0] == '\0')
			continue;
		if (line[0] != '\t')
			printing = isnow(line);
		if (printing)
			(void)fprintf(fp, "%s\n", line);
		free(line);

	}
	closecal(fp);
}
Exemplo n.º 2
0
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);
}
Exemplo n.º 3
0
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);
}