From: mdw Date: Sat, 5 Feb 2005 10:40:12 +0000 (+0000) Subject: Various fixes. X-Git-Tag: 1.2.0~19 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/misc/commitdiff_plain/ed04d555ce94ab069660f589a0ac3ef9d48ce5ae Various fixes. --- diff --git a/debian/changelog b/debian/changelog index 8e19ae5..9773dde 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,13 +1,14 @@ nsict-utils (1.1.1) experimental; urgency=low - * Add pause program. + * Add pause, buf, create and inplace programs. + * Fix splitconf documentation to be right. + * Fix gorp to produce base32 (ugh!) and safe64 output. - -- Mark Wooding Sat, 11 Sep 2004 18:13:25 +0100 + -- Mark Wooding Sat, 5 Feb 2005 10:39:52 +0000 nsict-utils (1.1.0) experimental; urgency=low * Make gorp a much more useful program. - * Get the right version number. Oops! -- Mark Wooding Tue, 18 Nov 2003 18:47:21 +0000 diff --git a/debian/control b/debian/control index 271d1d4..758281c 100644 --- a/debian/control +++ b/debian/control @@ -10,7 +10,7 @@ Package: nsict-utils Architecture: all Section: utils Depends: mdwopt-perl, nsict-cdb, locking, qmail-checkspam, nsict-mail, - if-mtu, shadowfix, zz, gorp, splitconf, xtitle, buf, create, inplace + if-mtu, shadowfix, zz, gorp, splitconf, xtitle, pause, buf, create, inplace Description: Dummy package for convenience. Package: mdwopt-perl diff --git a/gorp.1 b/gorp.1 index 66de0a1..572bee7 100644 --- a/gorp.1 +++ b/gorp.1 @@ -41,6 +41,19 @@ which may be instead of .RB ` / ', so the output is suitable for use as a filename), +.B safe64 +(Base64 encoding, with +.RB ` - ' +instead of +.RB ` + ', +and +.RB ` _ ' +instead of +.RB ` / ', +as specified in RFC3548, as an attempt to achieve the same goal but +inconveniently using up all the good separator characters), +.B base32 +(Base32 encoding, as described in RFC2938), .B hex (plain hexadecimal encoding), or .B raw diff --git a/gorp.c b/gorp.c index 398e852..81b4608 100644 --- a/gorp.c +++ b/gorp.c @@ -7,6 +7,7 @@ #include #include +#include #include #include #include @@ -23,102 +24,91 @@ struct format { #define CHUNK 1024 -static void format_base64(size_t n, unsigned line) +static void do_format(size_t n, unsigned line, void *ctx, + void (*encode)(void *, char *, size_t, dstr *), + void (*fix)(char *, size_t)) { - unsigned char buf[CHUNK]; + char buf[CHUNK]; dstr d = DSTR_INIT; - base64_ctx b; - - base64_init(&b); - if (line) { - b.indent = "\n"; - b.maxline = line; - } else { - b.indent = ""; - b.maxline = 0; - } while (n) { size_t nn = CHUNK; if (nn > n) nn = n; rand_get(RAND_GLOBAL, buf, nn); - base64_encode(&b, buf, nn, &d); + encode(ctx, buf, nn, &d); + if (fix) fix(d.buf, d.len); DWRITE(&d, stdout); DRESET(&d); n -= nn; } - base64_encode(&b, 0, 0, &d); + encode(ctx, 0, 0, &d); if (!line) { while (d.len && d.buf[d.len - 1] == '=') d.len--; } + if (fix) fix(d.buf, d.len); DPUTC(&d, '\n'); - DWRITE(&d, stdout); + DWRITE(&d, stdout); } -static void file_fix(char *p, size_t n) +static void do_base64(void *ctx, char *p, size_t sz, dstr *d) + { base64_encode(ctx, p, sz, d); } +static void do_format_base64(size_t n, unsigned line, + void (*fix)(char *, size_t)) +{ + base64_ctx b; + + base64_init(&b); + if (line) { b.indent = "\n"; b.maxline = line; } + else { b.indent = ""; b.maxline = 0; } + do_format(n, line, &b, do_base64, fix); +} +static void format_base64(size_t n, unsigned line) + { do_format_base64(n, line, 0); } + +static void do_base32(void *ctx, char *p, size_t sz, dstr *d) + { base32_encode(ctx, p, sz, d); } +static void format_base32(size_t n, unsigned line) +{ + base32_ctx b; + + base32_init(&b); + if (line) { b.indent = "\n"; b.maxline = line; } + else { b.indent = ""; b.maxline = 0; } + do_format(n, line, &b, do_base32, 0); +} + +static void fix_file64(char *p, size_t n) { while (n) { if (*p == '/') *p = '%'; p++; n--; } } - static void format_file64(size_t n, unsigned line) -{ - unsigned char buf[CHUNK]; - dstr d = DSTR_INIT; - base64_ctx b; - - base64_init(&b); - b.indent = ""; - b.maxline = 0; + { do_format_base64(n, line, fix_file64); } +static void fix_safe64(char *p, size_t n) +{ while (n) { - size_t nn = CHUNK; - if (nn > n) nn = n; - rand_get(RAND_GLOBAL, buf, nn); - base64_encode(&b, buf, nn, &d); - file_fix(d.buf, d.len); - DWRITE(&d, stdout); - DRESET(&d); - n -= nn; + if (*p == '+') *p = '-'; + else if (*p == '/') *p = '_'; + p++; n--; } - base64_encode(&b, 0, 0, &d); - file_fix(d.buf, d.len); - while (d.len && d.buf[d.len - 1] == '=') - d.len--; - DPUTC(&d, '\n'); - DWRITE(&d, stdout); } +static void format_safe64(size_t n, unsigned line) + { do_format_base64(n, line, fix_safe64); } +static void do_hex(void *ctx, char *p, size_t sz, dstr *d) + { hex_encode(ctx, p, sz, d); } static void format_hex(size_t n, unsigned line) { - unsigned char buf[CHUNK]; - dstr d = DSTR_INIT; - hex_ctx h; + hex_ctx b; - hex_init(&h); - if (line) { - h.indent = "\n"; - h.maxline = line; - } else { - h.indent = ""; - h.maxline = 0; - } - - while (n) { - size_t nn = CHUNK; - if (nn > n) nn = n; - rand_get(RAND_GLOBAL, buf, nn); - hex_encode(&h, buf, nn, &d); - DWRITE(&d, stdout); - DRESET(&d); - n -= nn; - } - hex_encode(&h, 0, 0, &d); - DPUTC(&d, '\n'); - DWRITE(&d, stdout); + hex_init(&b); + if (line) { b.indent = "\n"; b.maxline = line; } + else { b.indent = ""; b.maxline = 0; } + do_format(n, line, &b, do_hex, 0); } static void format_raw(size_t n, unsigned line) @@ -137,6 +127,8 @@ static void format_raw(size_t n, unsigned line) static const struct format fmt[] = { { "base64", format_base64 }, { "file64", format_file64 }, + { "safe64", format_safe64 }, + { "base32", format_base32 }, { "hex", format_hex }, { "raw", format_raw }, { 0, 0 } @@ -178,7 +170,8 @@ Options:\n\ \n\ -y, --bytes Output length is bytes, not bits.\n\ -l, --line=LENGTH For textual output, limit line length to LENGTH.\n\ --f, --format=FORMAT Select output format: base64, file64, hex, raw.\n\ +-f, --format=FORMAT Select output format:\n\ + base64, file64, safe64, base32, hex, raw.\n\ ", stdout); } diff --git a/locking.c b/locking.c index 2e61f0d..cf2917a 100644 --- a/locking.c +++ b/locking.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: locking.c,v 1.3 2004/04/08 01:36:26 mdw Exp $ + * $Id$ * * Lock a file, run a program * @@ -34,6 +34,7 @@ #include #include #include +#include #include #include diff --git a/splitconf b/splitconf index 0b2f672..12e63d2 100755 --- a/splitconf +++ b/splitconf @@ -18,9 +18,7 @@ proc die {msg} { proc usage {file} { global argv0 - puts $file "Usage: \n\ - $argv0 \[-s\] FILE\n - $argv0 -u OUTPUT FILE FILE ..." + puts $file "Usage: \n\t$argv0 \[-s\] FILE\n\t$argv0 -u OUTPUT FILE FILE ..." } # clear-arrays ARRAY ... @@ -229,15 +227,14 @@ switch $job { write-safe { while {[gets $c line] >= 0} { if {[regexp -- {^\[(.*)\]\s*$} $line . name]} { - if {[info exists o]} { - close $o - } elseif {!$donebefore} { - exec "sh" "-c" $opt(before) <@ stdin >@ stdout 2>@ stderr - set donebefore 1 - } + if {[info exists o]} { close $o } if {[string equal $name ""]} { catch { unset o } } else { + if {!$donebefore} { + exec "sh" "-c" $opt(before) <@ stdin >@ stdout 2>@ stderr + set donebefore 1 + } set name "$opt(prefix)$name" set o [write-safe-open $name] set new($name) 1 @@ -284,7 +281,9 @@ switch $job { } write-safe-manifest $conf [array names new] } { - exec "sh" "-c" $opt(after) <@ stdin >@ stdout 2>@ stderr + if {$donebefore} { + exec "sh" "-c" $opt(after) <@ stdin >@ stdout 2>@ stderr + } } } } diff --git a/splitconf.1 b/splitconf.1 index e8b4016..2c4875d 100644 --- a/splitconf.1 +++ b/splitconf.1 @@ -93,22 +93,32 @@ A file section begins with a line of the form .BR ] ' and is followed by lines to write to the file. In these lines, comments (lines beginning with a -.RB ` # ' +.RB ` ## ' are stripped out, and any trailing blank lines are removed. Also, any line beginning with an exclamation mark .RB ` ! ' is written out with the leading exclamation mark removed. .PP +A file section with an empty +.I name +is treated specially: its contents are scanned for more preamble +directives. +.PP Options are as follows. .TP .B prefix -A string to attach to the front of all filenames. (Default is empty.) +A string to attach to the front of all subsequent filenames, until +overridden. (Default is empty.) .TP .B before -A shell command to execute before starting to write any files. +A shell command to execute before starting to write any files. There is +only one before-command, which is the one current when the first file is +written. .TP .B after A shell command to execute after committing the changes or backing out. +There is only one after-command, which is the last one set in the +configuration file. .SH BUGS None known. .SH AUTHOR