mybin

my ~/bin
git clone https://a3nm.net/git/mybin/
Log | Files | Refs | README

cgitaboutfilter.c (1789B)


      1 // by p4bl0 <http://pablo.rauzy.name/>, public domain
      2 
      3 #include <stdio.h>
      4 #include <string.h>
      5 
      6 void
      7 print_html_c (int c)
      8 {
      9   switch (c) {
     10   case '<':
     11     fputs("&lt;", stdout);
     12     break;
     13   case '>':
     14     fputs("&gt;", stdout);
     15     break;
     16   case '&':
     17     fputs("&amp;", stdout);
     18     break;
     19   case '"':
     20     fputs("&quot;", stdout);
     21     break;
     22   default:
     23     putchar(c);
     24   }
     25 }
     26 
     27 void
     28 print_html_s (char *s)
     29 {
     30   while (*s) print_html_c(*s++);
     31 }
     32 
     33 int
     34 main (int argc, char *argv[])
     35 {
     36   signed char c;
     37   signed char urlflag;
     38   char urlbuf[256];
     39   char *ext = strrchr(argv[1], '.');
     40 
     41   if (NULL == ext) { // plain text
     42     fputs("<pre>\n", stdout);
     43     urlflag = -1;
     44     while (EOF != (c = getchar())) {
     45       if (urlflag > -1) {
     46         if ((0 == urlflag && 'h' == c) ||
     47             (1 == urlflag && 't' == c) ||
     48             (2 == urlflag && 't' == c) ||
     49             (3 == urlflag && 'p' == c)) {
     50           urlbuf[urlflag++] = c;
     51         } else if (urlflag < 4) { // not an url
     52           urlbuf[urlflag++] = c;
     53           urlbuf[urlflag] = 0;
     54           print_html_s(urlbuf);
     55           urlflag = -1;
     56         } else { // indeed an url
     57           urlbuf[urlflag++] = c;
     58           while ('>' != (c = getchar())) {
     59             if ('\n' == c) {
     60               fputs("BROKEN README\n</pre>", stdout);
     61               return 1;
     62             } else {
     63               urlbuf[urlflag++] = c;
     64             }
     65           }
     66           urlbuf[urlflag] = 0;
     67           printf("<a href=\"%s\">%s</a>&gt;", urlbuf, urlbuf);
     68           urlflag = -1;
     69         }
     70       } else {
     71         if ('<' == c) {
     72           urlflag++;
     73         }
     74         print_html_c(c);
     75       }
     76     }
     77     fputs("\n</pre>", stdout);
     78   } else if (0 == strcmp(".html", ext)) { // html
     79     while (EOF != (c = getchar())) {
     80       putchar(c);
     81     }
     82   }
     83 
     84   return 0;
     85 }