#include #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 */ } /* print error message to stderr */ static void die(const char *s) { fprintf(stderr, "sfeed_html: %s\n", s); 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) { errno = 0; parsedtime = (time_t)strtol(fields[FieldUnixTimestamp], NULL, 10); if(errno != 0) parsedtime = 0; isnew = (parsedtime >= comparetime) ? 1 : 0; islink = (fields[FieldLink][0] != '\0') ? 1 : 0; /* first of feed section or new feed section. */ 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"); 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; }