summaryrefslogtreecommitdiff
path: root/qsutil.c
blob: 56153e7e75c27007d1388b08ee4036be9b890528 (plain)
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
#include "stralloc.h"
#include "readwrite.h"
#include "substdio.h"
#include "qsutil.h"

static stralloc foo = {0};

static char errbuf[1];
static struct substdio sserr = SUBSTDIO_FDBUF(write,0,errbuf,1);

void logsa(sa) stralloc *sa; {
 substdio_putflush(&sserr,sa->s,sa->len); }
void Log1(s1) char *s1; {
 substdio_putsflush(&sserr,s1); }
void Log2(s1,s2) char *s1; char *s2; {
 substdio_putsflush(&sserr,s1);
 substdio_putsflush(&sserr,s2); }
void Log3(s1,s2,s3) char *s1; char *s2; char *s3; {
 substdio_putsflush(&sserr,s1);
 substdio_putsflush(&sserr,s2);
 substdio_putsflush(&sserr,s3); }
void nomem() { Log1("alert: out of memory, sleeping...\n"); sleep(10); }

void pausedir(dir) char *dir;
{ Log3("alert: unable to opendir ",dir,", sleeping...\n"); sleep(10); }

static int issafe(ch) char ch;
{
 if (ch == '%') return 0; /* general principle: allman's code is crap */
 if (ch < 33) return 0;
 if (ch > 126) return 0;
 return 1;
}

void logsafe(s) char *s;
{
 int i;
 while (!stralloc_copys(&foo,s)) nomem();
 for (i = 0;i < foo.len;++i)
   if (foo.s[i] == '\n')
     foo.s[i] = '/';
   else
     if (!issafe(foo.s[i]))
       foo.s[i] = '_';
 logsa(&foo);
}