sfeed_frames.c 5.2 KB

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