sfeed

simple feed reader - forked from https://git.codemadness.org/sfeed
git clone git://src.gearsix.net/sfeedsfeed.zip
Log | Files | Refs | Atom | README | LICENSE

sfeed_frames.c (raw) (5306B)


   1 #include <sys/types.h>
   2 
   3 #include <stdio.h>
   4 #include <stdlib.h>
   5 #include <string.h>
   6 #include <time.h>
   7 
   8 #include "util.h"
   9 
  10 static struct feed *feeds;
  11 static char *line;
  12 static size_t linesize;
  13 static time_t comparetime;
  14 static unsigned long totalnew, total;
  15 
  16 static void
  17 printfeed(FILE *fpitems, FILE *fpin, struct feed *f)
  18 {
  19 	char *fields[FieldLast];
  20 	ssize_t linelen;
  21 	unsigned int isnew;
  22 	struct tm rtm, *tm;
  23 	time_t parsedtime;
  24 
  25 	/* menu if not unnamed */
  26 	if (f->name[0]) {
  27 		fputs("<h2 id=\"", fpitems);
  28 		xmlencode(f->name, fpitems);
  29 		fputs("\"><a href=\"#", fpitems);
  30 		xmlencode(f->name, fpitems);
  31 		fputs("\">", fpitems);
  32 		xmlencode(f->name, fpitems);
  33 		fputs("</a></h2>\n", fpitems);
  34 	}
  35 	fputs("<pre>\n", fpitems);
  36 
  37 	while ((linelen = getline(&line, &linesize, fpin)) > 0 &&
  38 	       !ferror(fpitems)) {
  39 		if (line[linelen - 1] == '\n')
  40 			line[--linelen] = '\0';
  41 		parseline(line, fields);
  42 
  43 		parsedtime = 0;
  44 		if (!strtotime(fields[FieldUnixTimestamp], &parsedtime) &&
  45 		    (tm = localtime_r(&parsedtime, &rtm))) {
  46 			isnew = (parsedtime >= comparetime) ? 1 : 0;
  47 			totalnew += isnew;
  48 			f->totalnew += isnew;
  49 			fprintf(fpitems, "%04d-%02d-%02d&nbsp;%02d:%02d ",
  50 			        tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
  51 			        tm->tm_hour, tm->tm_min);
  52 		} else {
  53 			isnew = 0;
  54 			fputs("                 ", fpitems);
  55 		}
  56 		f->total++;
  57 		total++;
  58 
  59 		if (fields[FieldLink][0]) {
  60 			fputs("<a href=\"", fpitems);
  61 			xmlencode(fields[FieldLink], fpitems);
  62 			fputs("\">", fpitems);
  63 		}
  64 		if (isnew)
  65 			fputs("<b><u>", fpitems);
  66 		xmlencode(fields[FieldTitle], fpitems);
  67 		if (isnew)
  68 			fputs("</u></b>", fpitems);
  69 		if (fields[FieldLink][0])
  70 			fputs("</a>", fpitems);
  71 		fputs("\n", fpitems);
  72 	}
  73 	fputs("</pre>\n", fpitems);
  74 }
  75 
  76 int
  77 main(int argc, char *argv[])
  78 {
  79 	FILE *fpindex, *fpitems, *fpmenu = NULL, *fp;
  80 	char *name;
  81 	int i, showsidebar = (argc > 1);
  82 	struct feed *f;
  83 
  84 	if (pledge("stdio rpath wpath cpath", NULL) == -1)
  85 		err(1, "pledge");
  86 
  87 	if (!(feeds = calloc(argc, sizeof(struct feed))))
  88 		err(1, "calloc");
  89 
  90 	if ((comparetime = time(NULL)) == (time_t)-1)
  91 		errx(1, "time");
  92 	/* 1 day is old news */
  93 	comparetime -= 86400;
  94 
  95 	/* write main index page */
  96 	if (!(fpindex = fopen("index.html", "wb")))
  97 		err(1, "fopen: index.html");
  98 	if (!(fpitems = fopen("items.html", "wb")))
  99 		err(1, "fopen: items.html");
 100 	if (showsidebar && !(fpmenu = fopen("menu.html", "wb")))
 101 		err(1, "fopen: menu.html");
 102 
 103 	if (pledge(argc == 1 ? "stdio" : "stdio rpath", NULL) == -1)
 104 		err(1, "pledge");
 105 
 106 	fputs("<!DOCTYPE HTML>\n"
 107 	      "<html>\n"
 108 	      "\t<head>\n"
 109 	      "\t<meta name=\"referrer\" content=\"no-referrer\" />\n"
 110 	      "\t<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
 111 	      "\t<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" />\n"
 112 	      "</head>\n"
 113 	      "<body class=\"frame\"><div id=\"items\">", fpitems);
 114 
 115 	if (argc == 1) {
 116 		feeds[0].name = "";
 117 		printfeed(fpitems, stdin, &feeds[0]);
 118 		checkfileerror(stdin, "<stdin>", 'r');
 119 	} else {
 120 		for (i = 1; i < argc; i++) {
 121 			name = ((name = strrchr(argv[i], '/'))) ? name + 1 : argv[i];
 122 			feeds[i - 1].name = name;
 123 
 124 			if (!(fp = fopen(argv[i], "r")))
 125 				err(1, "fopen: %s", argv[i]);
 126 			printfeed(fpitems, fp, &feeds[i - 1]);
 127 			checkfileerror(fp, argv[i], 'r');
 128 			checkfileerror(fpitems, "items.html", 'w');
 129 			fclose(fp);
 130 		}
 131 	}
 132 	fputs("</div></body>\n</html>\n", fpitems); /* div items */
 133 
 134 	if (showsidebar) {
 135 		fputs("<!DOCTYPE HTML>\n"
 136 		      "<html>\n"
 137 		      "<head>\n"
 138 		      "\t<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
 139 		      "\t<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" />\n"
 140 		      "</head>\n"
 141 		      "<body class=\"frame\">\n<div id=\"sidebar\">\n", fpmenu);
 142 
 143 		for (i = 1; i < argc; i++) {
 144 			f = &feeds[i - 1];
 145 			if (f->totalnew)
 146 				fputs("<a class=\"n\" href=\"items.html#", fpmenu);
 147 			else
 148 				fputs("<a href=\"items.html#", fpmenu);
 149 			xmlencode(f->name, fpmenu);
 150 			fputs("\" target=\"items\">", fpmenu);
 151 			if (f->totalnew > 0)
 152 				fputs("<b><u>", fpmenu);
 153 			xmlencode(f->name, fpmenu);
 154 			fprintf(fpmenu, " (%lu)", f->totalnew);
 155 			if (f->totalnew > 0)
 156 				fputs("</u></b>", fpmenu);
 157 			fputs("</a><br/>\n", fpmenu);
 158 		}
 159 		fputs("</div></body></html>\n", fpmenu);
 160 	}
 161 	fputs("<!DOCTYPE html>\n<html>\n<head>\n"
 162 	      "\t<meta name=\"referrer\" content=\"no-referrer\" />\n"
 163 	      "\t<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
 164 	      "\t<title>(", fpindex);
 165 	fprintf(fpindex, "%lu/%lu", totalnew, total);
 166 	fputs(") - Newsfeed</title>\n\t<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" />\n"
 167 	      "</head>\n", fpindex);
 168 	if (showsidebar) {
 169 		fputs("<frameset framespacing=\"0\" cols=\"250,*\" frameborder=\"1\">\n"
 170 		      "\t<frame name=\"menu\" src=\"menu.html\" target=\"menu\">\n", fpindex);
 171 	} else {
 172 		fputs("<frameset framespacing=\"0\" cols=\"*\" frameborder=\"1\">\n", fpindex);
 173 	}
 174 	fputs(
 175 	      "\t<frame name=\"items\" src=\"items.html\" target=\"items\">\n"
 176 	      "</frameset>\n"
 177 	      "</html>\n", fpindex);
 178 
 179 	checkfileerror(fpindex, "index.html", 'w');
 180 	checkfileerror(fpitems, "items.html", 'w');
 181 
 182 	fclose(fpindex);
 183 	fclose(fpitems);
 184 	if (fpmenu) {
 185 		checkfileerror(fpmenu, "menu.html", 'w');
 186 		fclose(fpmenu);
 187 	}
 188 
 189 	return 0;
 190 }