#include #include #include #include #include #include #include #include #include #include #include #include #include #include "util.h" static struct feed **feeds; static char *line; static size_t linesize; static time_t comparetime; static unsigned long totalnew; static void printfeed(FILE *fpitems, FILE *fpin, struct feed *f) { char *fields[FieldLast]; ssize_t linelen; unsigned int isnew; struct tm *tm; time_t parsedtime; /* menu if not unnamed */ if (f->name[0]) { fputs("

name, fpitems); fputs("\">name, fpitems); fputs("\">", fpitems); xmlencode(f->name, fpitems); fputs("

\n", fpitems); } while ((linelen = getline(&line, &linesize, fpin)) > 0) { if (line[linelen - 1] == '\n') line[--linelen] = '\0'; if (!parseline(line, fields)) break; parsedtime = 0; if (strtotime(fields[FieldUnixTimestamp], &parsedtime)) continue; if (!(tm = localtime(&parsedtime))) err(1, "localtime"); isnew = (parsedtime >= comparetime) ? 1 : 0; totalnew += isnew; f->totalnew += isnew; f->total++; fprintf(fpitems, "%04d-%02d-%02d %02d:%02d ", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min); if (isnew) fputs("", fpitems); if (fields[FieldLink][0]) { fputs("", fpitems); xmlencode(fields[FieldTitle], fpitems); fputs("", fpitems); } else { xmlencode(fields[FieldTitle], fpitems); } if (isnew) fputs("", fpitems); fputs("\n", fpitems); } } int main(int argc, char *argv[]) { FILE *fpindex, *fpitems, *fpmenu = NULL, *fp; char *name; int i, showsidebar = (argc > 1); struct feed *f; if (pledge("stdio rpath wpath cpath", NULL) == -1) err(1, "pledge"); if (!(feeds = calloc(argc, sizeof(struct feed *)))) err(1, "calloc"); if ((comparetime = time(NULL)) == -1) err(1, "time"); /* 1 day is old news */ comparetime -= 86400; /* write main index page */ if (!(fpindex = fopen("index.html", "wb"))) err(1, "fopen: index.html"); if (!(fpitems = fopen("items.html", "wb"))) err(1, "fopen: items.html"); if (showsidebar && !(fpmenu = fopen("menu.html", "wb"))) err(1, "fopen: menu.html"); if (pledge("stdio rpath", NULL) == -1) err(1, "pledge"); fputs("" "" "
", fpitems);

	if (argc == 1) {
		if (!(feeds[0] = calloc(1, sizeof(struct feed))))
			err(1, "calloc");
		feeds[0]->name = "";
		printfeed(fpitems, stdin, feeds[0]);
	} else {
		for (i = 1; i < argc; i++) {
			if (!(feeds[i - 1] = calloc(1, sizeof(struct feed))))
				err(1, "calloc");
			name = ((name = strrchr(argv[i], '/'))) ? name + 1 : argv[i];
			feeds[i - 1]->name = name;

			if (!(fp = fopen(argv[i], "r")))
				err(1, "fopen: %s", argv[i]);
			printfeed(fpitems, fp, feeds[i - 1]);
			if (ferror(fp))
				err(1, "ferror: %s", argv[i]);
			fclose(fp);
		}
	}
	fputs("
\n
\n\n", fpitems); /* div items */ if (showsidebar) { fputs("" "\n" "\n" "\n", fpmenu); } fputs("\n\tNewsfeed (", fpindex); fprintf(fpindex, "%lu", totalnew); fputs(")\n\t\n" "\n" "\n", fpindex); if (showsidebar) { fputs("\n" "\t\n", fpindex); } else { fputs("\n", fpindex); } fputs( "\t\n" "\n" "\n", fpindex); fclose(fpindex); fclose(fpitems); if (fpmenu) fclose(fpmenu); return 0; }