#include #include #include #include #include #include "util.h" static int showsidebar = 1; /* show sidebar ? */ static struct feed *feeds = NULL; /* start of feeds linked-list. */ static char *line = NULL; static void cleanup(void) { free(line); /* free line */ line = NULL; feedsfree(feeds); /* free feeds linked-list */ } static void /* print error message to stderr */ die(const char *s) { fputs("sfeed_html: ", stderr); fputs(s, stderr); fputc('\n', stderr); exit(EXIT_FAILURE); } int main(void) { char *fields[FieldLast]; unsigned long totalfeeds = 0, totalnew = 0; unsigned int islink, isnew; struct feed *f, *fcur = NULL; time_t parsedtime, comparetime; size_t size = 0; atexit(cleanup); comparetime = time(NULL) - (3600 * 24); /* 1 day is old news */ fputs( "\n" "\n" "\t\n" "\t\t\n" "\t\t\n" "\t\n" "\t\n", stdout); if(!(fcur = calloc(1, sizeof(struct feed)))) die("can't allocate enough memory"); feeds = fcur; while(parseline(&line, &size, fields, FieldLast, '\t', stdin) > 0) { parsedtime = (time_t)strtol(fields[FieldUnixTimestamp], NULL, 10); isnew = (parsedtime >= comparetime); islink = (fields[FieldLink][0] != '\0'); /* first of feed section or new feed section. */ /* TODO: allocate fcur before here, fcur can be NULL */ if(!totalfeeds || (fcur && strcmp(fcur->name, fields[FieldFeedName]))) { if(!(f = calloc(1, sizeof(struct feed)))) die("can't allocate enough memory"); /*f->next = NULL;*/ if(totalfeeds) { /* end previous one. */ fputs("\n", stdout); fcur->next = f; fcur = f; } else { fcur = f; feeds = fcur; /* first item. */ if(fields[FieldFeedName][0] == '\0' || !showsidebar) { /* set nosidebar class on div for styling */ fputs("\t\t
\n", stdout); showsidebar = 0; } else fputs("\t\t
\n", stdout); } /* TODO: memcpy and make fcur->name static? */ if(!(fcur->name = strdup(fields[FieldFeedName]))) die("can't allocate enough memory"); /* fcur->totalnew = 0; fcur->total = 0; fcur->next = NULL;*/ if(fields[FieldFeedName][0] != '\0') { fputs("

name, stdout); fputs("\">name, stdout); fputs("\">", stdout); fputs(fcur->name, stdout); fputs("

\n", stdout); } fputs("\n", stdout); totalfeeds++; } totalnew += isnew; fcur->totalnew += isnew; fcur->total++; if(isnew) fputs("", stdout); else fputs("", stdout); fputs("\n", stdout); } if(totalfeeds) fputs("
", stdout); fputs(fields[FieldTimeFormatted], stdout); fputs("", stdout); if(isnew) fputs("", stdout); if(islink) { fputs("", stdout); } printhtmlencoded(fields[FieldTitle], stdout); if(islink) fputs("", stdout); if(isnew) fputs("", stdout); fputs("
\n\t\t
\n", stdout); /* div items */ if(showsidebar) { fputs("\t
\n\t\t\n\t
\n", stdout); } fputs( "\t\n" "\tNewsfeed (", stdout); fprintf(stdout, "%lu", totalnew); fputs(")\n", stdout); return EXIT_SUCCESS; }