tttm

git clone https://orangeshoelaces.net/git/tttm.git

/pshades.c

   1 #include <fcntl.h>
   2 #include <libgen.h>
   3 #include <stddef.h>
   4 #include <stdint.h>
   5 #include <stdio.h>
   6 #include <stdlib.h>
   7 #include <unistd.h>
   8 
   9 #include "errors.h"
  10 #include "parser.h"
  11 
  12 #define LEN(a) (sizeof(a) / sizeof(a)[0])
  13 
  14 char *prod[] = {
  15         "822BODY",
  16         "ATOM",
  17         "CAP",
  18         "CAPDATA",
  19         "CONTREQ",
  20         "EXISTS",
  21         "EXPUNGE",
  22         "FETCH",
  23         "RESPAUTH",
  24         "RESPBYE",
  25         "RESPDATA",
  26         "RESPSTATE",
  27         "RESPTXTCODE",
  28         "TAGGEDRESP",
  29 };
  30 
  31 char *literals[] = {
  32         "7BIT", "8BIT", "ALERT", "APP", "APR", "ASTERISK", "AUDIO", "AUG",
  33         "AUTHEQ", "B64", "BAD", "BADCS", "BIN", "BODY", "BSLASH", "BYE",
  34         "CAP", "CBRACE", "CBRACK", "COLON", "CPAR", "DEC", "DHDR", "DNOT",
  35         "DOT", "DQUOTE", "DTXT", "ENVELOPE", "EOL", "EXISTS", "EXPUNGE",
  36         "FEB", "FETCH", "FLAGS", "FLANSWERED", "FLANY", "FLDELETED",
  37         "FLDRAFT", "FLFLAGGED", "FLMARKED", "FLNOINFERIORS", "FLNOSEL",
  38         "FLRECENT", "FLSEEN", "FLUNMARKED", "GT", "HDR", "HDRFIELDS", "IMG",
  39         "INBOX", "INTDATE", "JAN", "JUL", "JUN", "LIST", "LSUB", "LT",
  40         "MAR", "MAY", "MIME", "MINUS", "MSG", "MSGS", "NIL", "NO", "NOV",
  41         "OBRACE", "OBRACK", "OCT", "OK", "OPAR", "PARSE", "PERMFL", "PLUS",
  42         "PREAUTH", "QPRN", "RECENT", "RFC822", "RFC822SIZE", "RO", "RW",
  43         "SEARCH", "SEP", "SP", "STATUS", "STRUCT", "TRYC", "TXT", "UID",
  44         "UIDNEXT", "UIDVAL", "UNSEEN", "VIDEO"
  45 };
  46 
  47 void ptprint(union parnode *, char *, size_t);
  48 
  49 char *prog;
  50 
  51 int
  52 main(int argc, char **argv)
  53 {
  54         int e;
  55         char *ln;
  56         size_t cap, len, parsed;
  57         union parnode pt[1024];
  58 
  59         prog = basename(argv[0]);
  60 
  61         #ifdef __OpenBSD__
  62         if(pledge("stdio proc", 0) == -1) {
  63                 err(1, "pledge");
  64         }
  65         #endif
  66 
  67         ln = 0;
  68         cap = len = parsed = 0;
  69         if ((e = par_parseln(STDIN_FILENO, &ln, &cap, &len, &parsed, pt, LEN(pt))))
  70                 errx(1, "error parsing input: %s",  errmsgs[e]);
  71 
  72         ptprint(pt, ln, 0);
  73         free(ln);
  74         return 0;
  75 }
  76 
  77 void
  78 ptprint(union parnode *n, char *ln, size_t depth)
  79 {
  80         static char *prefix = "               >";
  81         union parnode *nend;
  82 
  83         printf("%.*s", (int)depth, prefix);
  84 
  85         switch (n->type) {
  86         case PN_INTER:
  87                 printf("%s\n", prod[n->inter.prod]);
  88                 for (nend = n + n->inter.len + 1, n++; n < nend; n = par_nn(n))
  89                         ptprint(n, ln, depth + 1);
  90                 break;
  91         case PN_LIT:
  92                 printf("%s\n", literals[n->lit.val]);
  93                 break;
  94         case PN_NUM:
  95                 printf("%zu\n", (size_t)n->num.val);
  96                 break;
  97         case PN_STR:
  98                 printf("%.*s\n", (int)n->str.len, ln + n->str.off);
  99                 break;
 100         case PN_QSTR:
 101                 printf("\"%.*s\"\n", (int)n->str.len, ln + n->str.off);
 102                 break;
 103         case PN_LSTR:
 104                 printf("%.*s\n", (int)n->str.len, ln + n->str.off);
 105                 break;
 106         default:
 107                 errx(1, "cannot grok parse node of type %d\n", n->type);
 108         }
 109 }