File: printforward.c

package info (click to toggle)
fastforward 1%3A0.51-11
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,060 kB
  • sloc: ansic: 8,033; makefile: 483; sh: 160
file content (145 lines) | stat: -rw-r--r-- 2,853 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "substdio.h"
#include "subfd.h"
#include "strerr.h"
#include "stralloc.h"
#include "cdb.h"

#define FATAL "printmaillist: fatal: "

void badformat()
{
  strerr_die2x(100,FATAL,"bad database format");
}
void nomem()
{
  strerr_die2x(111,FATAL,"out of memory");
}

void getch(ch)
char *ch;
{
  int r;
  r = substdio_get(subfdinsmall,ch,1);
  if (r == -1)
    strerr_die2sys(111,FATAL,"unable to read input: ");
  if (r == 0)
    badformat();
}

void putch(ch)
char *ch;
{
  if (substdio_put(subfdoutsmall,ch,1) == -1)
    strerr_die2x(111,FATAL,"unable to write output: ");
}

void print(buf)
char *buf;
{
  while (*buf)
    putch(buf++);
}

void printsafe(buf,len)
char *buf;
int len;
{
  char ch;

  while (len) {
    ch = *buf;
    if ((ch <= 32) || (ch == ',') || (ch == ':') || (ch == ';') || (ch == '\\') || (ch == '#'))
      putch("\\");
    putch(&ch);
    ++buf;
    --len;
  }
}

stralloc key = {0};
stralloc data = {0};

void 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);
      print(": ?");
      printsafe(data.s,data.len);
      print(";\n");
    }
    else if (key.s[0] == ':') {
      printsafe(key.s + 1,key.len - 1);
      print(":\n");

      i = 0;
      for (j = 0;j < data.len;++j)
        if (!data.s[j]) {
          if ((data.s[i] == '.') || (data.s[i] == '/')) {
            print(", ");
            printsafe(data.s + i,j - i);
            print("\n");
          }
          else if ((data.s[i] == '|') || (data.s[i] == '!')) {
            print(", ");
            printsafe(data.s + i,j - i);
            print("\n");
          }
          else if ((data.s[i] == '&') && (j - i < 900)) {
            print(", ");
            printsafe(data.s + i,j - i);
            print("\n");
          }
          else badformat();
          i = j + 1;
        }
      if (i != j) badformat();
      print(";\n");
    }
    else badformat();
  }

  if (substdio_flush(subfdoutsmall) == -1)
    strerr_die2sys(111,FATAL,"unable to write output: ");
  _exit(0);
}