#include #include #include #include #include #include "common.h" #include "compat.h" static int showsidebar = 1; /* show sidebar ? */ 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 *line = NULL, *fields[FieldLast]; unsigned long totalfeeds = 0, totalnew = 0; int islink, isnew; struct feed *feedcurrent = NULL, *feeds = NULL; /* start of feeds linked-list. */ time_t parsedtime, comparetime; size_t size = 0; comparetime = time(NULL) - (3600 * 24); /* 1 day is old news */ fputs( "\n" "\n" " \n" " \n" " \n" " \n" " \n", stdout); 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. */ if(!totalfeeds || strcmp(feedcurrent->name, fields[FieldFeedName])) { if(totalfeeds) { /* end previous one. */ fputs("\n", stdout); if(!(feedcurrent->next = calloc(1, sizeof(struct feed)))) die("can't allocate enough memory"); feedcurrent = feedcurrent->next; } else { if(!(feedcurrent = calloc(1, sizeof(struct feed)))) die("can't allocate enough memory"); feeds = feedcurrent; /* 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); } if(!(feedcurrent->name = xstrdup(fields[FieldFeedName]))) die("can't allocate enough memory"); if(fields[FieldFeedName][0] != '\0') { fputs("

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

\n", stdout); } fputs("\n", stdout); totalfeeds++; } totalnew += isnew; feedcurrent->totalnew += isnew; feedcurrent->total++; if(isnew) fputs("\n", stdout); } if(totalfeeds) { fputs("
", stdout); else 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", stdout); fputs("\t\t
\n", stdout); /* div items */ } if(showsidebar) { fputs("\t
\n\t\t\n\t
\n", stdout); } fputs( " \n" " Newsfeed (", stdout); fprintf(stdout, "%lu", totalnew); fputs(")\n", stdout); free(line); /* free line */ feedsfree(feeds); /* free feeds linked-list */ return EXIT_SUCCESS; }