diff options
Diffstat (limited to 'src/setforward.c')
-rw-r--r-- | src/setforward.c | 108 |
1 files changed, 51 insertions, 57 deletions
diff --git a/src/setforward.c b/src/setforward.c index fe17f74..356ec78 100644 --- a/src/setforward.c +++ b/src/setforward.c @@ -1,43 +1,43 @@ #include <unistd.h> + #include "buffer.h" -#include "logmsg.h" -#include "stralloc.h" -#include "open.h" #include "case.h" #include "cdbmake.h" #include "logmsg.h" +#include "open.h" +#include "stralloc.h" #define WHO "setforward" -int rename(const char *,const char *); // stdio.h +int rename(const char *, const char *); // stdio.h void usage() { - logmsg(WHO,100,USAGE,"setforward data.cdb data.tmp"); + logmsg(WHO, 100, USAGE, "setforward data.cdb data.tmp"); } void nomem() { - logmsg(WHO,111,FATAL,"out of memory"); + logmsg(WHO, 111, FATAL, "out of memory"); } void missingsemicolon() { - logmsg(WHO,100,FATAL,"final instruction must end with semicolon"); + logmsg(WHO, 100, FATAL, "final instruction must end with semicolon"); } void extracolon() { - logmsg(WHO,100,FATAL,"double colons are not permitted"); + logmsg(WHO, 100, FATAL, "double colons are not permitted"); } void extracomma() { - logmsg(WHO,100,FATAL,"commas are not permitted before colons"); + logmsg(WHO, 100, FATAL, "commas are not permitted before colons"); } void nulbyte() { - logmsg(WHO,100,FATAL,"NUL bytes are not permitted"); + logmsg(WHO, 100, FATAL, "NUL bytes are not permitted"); } void longaddress() { - logmsg(WHO,100,FATAL,"addresses over 800 bytes are not permitted"); + logmsg(WHO, 100, FATAL, "addresses over 800 bytes are not permitted"); } char *fncdb; @@ -46,9 +46,9 @@ int fd; struct cdb_make cdb; stralloc key = {0}; -stralloc target = {0}; /* always initialized; no NUL */ +stralloc target = {0}; /* always initialized; no NUL */ stralloc command = {0}; /* always initialized; no NUL */ -stralloc instr = {0}; /* always initialized */ +stralloc instr = {0}; /* always initialized */ int flagtarget = 0; /* 0: reading target; command is empty; instr is empty */ @@ -56,19 +56,17 @@ int flagtarget = 0; void writeerr() { - logmsg(WHO,111,FATAL,B("unable to write to: ",fntmp)); + logmsg(WHO, 111, FATAL, B("unable to write to: ", fntmp)); } -void doit(prepend,data,datalen) -char *prepend; +void doit(prepend, data, datalen) char *prepend; char *data; int datalen; { - if (!stralloc_copys(&key,prepend)) nomem(); - if (!stralloc_cat(&key,&target)) nomem(); - case_lowerb(key.s,key.len); - if (cdb_make_add(&cdb,key.s,key.len,data,datalen) == -1) - writeerr(); + if (!stralloc_copys(&key, prepend)) nomem(); + if (!stralloc_cat(&key, &target)) nomem(); + case_lowerb(key.s, key.len); + if (cdb_make_add(&cdb, key.s, key.len, data, datalen) == -1) writeerr(); } int getch(ch) @@ -76,9 +74,8 @@ char *ch; { int r; - r = buffer_get(buffer_0small,ch,1); - if (r == -1) - logmsg(WHO,111,FATAL,"unable to read input: "); + r = buffer_get(buffer_0small, ch, 1); + if (r == -1) logmsg(WHO, 111, FATAL, "unable to read input: "); return r; } @@ -86,24 +83,26 @@ int main(int argc, char **argv) { char ch; - if (!stralloc_copys(&target,"")) nomem(); - if (!stralloc_copys(&command,"")) nomem(); - if (!stralloc_copys(&instr,"")) nomem(); + if (!stralloc_copys(&target, "")) nomem(); + if (!stralloc_copys(&command, "")) nomem(); + if (!stralloc_copys(&instr, "")) nomem(); - fncdb = argv[1]; if (!fncdb) usage(); - fntmp = argv[2]; if (!fntmp) usage(); + fncdb = argv[1]; + if (!fncdb) usage(); + fntmp = argv[2]; + if (!fntmp) usage(); fd = open_trunc(fntmp); - if (fd == -1) - logmsg(WHO,111,FATAL,B("unable to create: ",fntmp)); + if (fd == -1) logmsg(WHO, 111, FATAL, B("unable to create: ", fntmp)); - if (cdb_make_start(&cdb,fd) == -1) writeerr(); + if (cdb_make_start(&cdb, fd) == -1) writeerr(); for (;;) { if (!getch(&ch)) goto EOF; if (ch == '#') { - while (ch != '\n') if (!getch(&ch)) goto EOF; + while (ch != '\n') + if (!getch(&ch)) goto EOF; continue; } @@ -121,53 +120,48 @@ int main(int argc, char **argv) if (!flagtarget) extracomma(); if (command.len) { if (command.s[0] == '?') { - doit("?",command.s + 1,command.len - 1); - } - else if ((command.s[0] == '|') || (command.s[0] == '!')) { - if (!stralloc_cat(&instr,&command)) nomem(); + doit("?", command.s + 1, command.len - 1); + } else if ((command.s[0] == '|') || (command.s[0] == '!')) { + if (!stralloc_cat(&instr, &command)) nomem(); if (!stralloc_0(&instr)) nomem(); - } - else if ((command.s[0] == '.') || (command.s[0] == '/')) { - if (!stralloc_cat(&instr,&command)) nomem(); + } else if ((command.s[0] == '.') || (command.s[0] == '/')) { + if (!stralloc_cat(&instr, &command)) nomem(); if (!stralloc_0(&instr)) nomem(); - } - else { + } else { if (command.len > 800) longaddress(); if (command.s[0] != '&') - if (!stralloc_cats(&instr,"&")) nomem(); - if (!stralloc_cat(&instr,&command)) nomem(); + if (!stralloc_cats(&instr, "&")) nomem(); + if (!stralloc_cat(&instr, &command)) nomem(); if (!stralloc_0(&instr)) nomem(); } } - if (!stralloc_copys(&command,"")) nomem(); + if (!stralloc_copys(&command, "")) nomem(); if (ch == ';') { - if (instr.len) - doit(":",instr.s,instr.len); + if (instr.len) doit(":", instr.s, instr.len); - if (!stralloc_copys(&target,"")) nomem(); - if (!stralloc_copys(&instr,"")) nomem(); + if (!stralloc_copys(&target, "")) nomem(); + if (!stralloc_copys(&instr, "")) nomem(); flagtarget = 0; } continue; } - if (ch == '\\') if (!getch(&ch)) goto EOF; + if (ch == '\\') + if (!getch(&ch)) goto EOF; if (ch == 0) nulbyte(); - if (!stralloc_append(flagtarget ? &command : &target,&ch)) nomem(); + if (!stralloc_append(flagtarget ? &command : &target, &ch)) nomem(); } - EOF: - if (flagtarget || target.len) - missingsemicolon(); +EOF: + if (flagtarget || target.len) missingsemicolon(); if (cdb_make_finish(&cdb) == -1) writeerr(); if (fsync(fd) == -1) writeerr(); if (close(fd) == -1) writeerr(); /* NFS stupidity */ - if (rename(fntmp,fncdb) == -1) - logmsg(WHO,111,FATAL,B("unable to move ",fntmp," to: ",fncdb)); - + if (rename(fntmp, fncdb) == -1) logmsg(WHO, 111, FATAL, B("unable to move ", fntmp, " to: ", fncdb)); + _exit(0); } |