#include #include "buffer.h" #include "cdbread.h" #include "logmsg.h" #include "stralloc.h" #define WHO "printmaillist" static void badformat() { logmsg(WHO, 100, FATAL, "bad database format"); } static void nomem() { logmsg(WHO, 111, FATAL, "out of memory"); } static void getch(char *ch) { int r; r = buffer_get(buffer_0small, ch, 1); if (r == -1) logmsg(WHO, 111, FATAL, "unable to read input"); if (r == 0) badformat(); } static void out(char *ch) { if (buffer_put(buffer_1small, ch, 1) == -1) logmsg(WHO, 111, FATAL, "unable to write output"); } static void printbuf(char *buf) { while (*buf) out(buf++); } static void printsafe(char *buf, int len) { char ch; while (len) { ch = *buf; if ((ch <= 32) || (ch == ',') || (ch == ':') || (ch == ';') || (ch == '\\') || (ch == '#')) out("\\"); out(&ch); ++buf; --len; } } stralloc key = {0}; stralloc data = {0}; int main() { uint32 eod; uint32 pos; uint32 klen; uint32 dlen; char buf[8]; char ch; int i; int j; for (i = 0; i < 4; ++i) getch(buf + i); eod = cdb_unpack(buf); for (i = 4; i < 2048; ++i) getch(&ch); pos = 2048; while (pos < eod) { if (eod - pos < 8) badformat(); pos += 8; for (i = 0; i < 8; ++i) getch(buf + i); klen = cdb_unpack(buf); dlen = cdb_unpack(buf + 4); if (!stralloc_copys(&key, "")) nomem(); if (eod - pos < klen) badformat(); pos += klen; while (klen) { --klen; getch(&ch); if (!stralloc_append(&key, &ch)) nomem(); } if (eod - pos < dlen) badformat(); pos += dlen; if (!stralloc_copys(&data, "")) nomem(); while (dlen) { --dlen; getch(&ch); if (!stralloc_append(&data, &ch)) nomem(); } if (!key.len) badformat(); if (key.s[0] == '?') { printsafe(key.s + 1, key.len - 1); printbuf(": ?"); printsafe(data.s, data.len); printbuf(";\n"); } else if (key.s[0] == ':') { printsafe(key.s + 1, key.len - 1); printbuf(":\n"); i = 0; for (j = 0; j < data.len; ++j) if (!data.s[j]) { if ((data.s[i] == '.') || (data.s[i] == '/')) { printbuf(", "); printsafe(data.s + i, j - i); printbuf("\n"); } else if ((data.s[i] == '|') || (data.s[i] == '!')) { printbuf(", "); printsafe(data.s + i, j - i); printbuf("\n"); } else if ((data.s[i] == '&') && (j - i < 900)) { printbuf(", "); printsafe(data.s + i, j - i); printbuf("\n"); } else badformat(); i = j + 1; } if (i != j) badformat(); printbuf(";\n"); } else { badformat(); } } if (buffer_flush(buffer_1small) == -1) logmsg(WHO, 111, FATAL, "unable to write output"); _exit(0); }