diff options
Diffstat (limited to 'util')
-rw-r--r-- | util/argparse.c | 3 | ||||
-rw-r--r-- | util/iobuf.c | 49 | ||||
-rw-r--r-- | util/miscutil.c | 2 | ||||
-rw-r--r-- | util/ttyio.c | 22 |
4 files changed, 74 insertions, 2 deletions
diff --git a/util/argparse.c b/util/argparse.c index 10cfd5c1d..80b7fcd3d 100644 --- a/util/argparse.c +++ b/util/argparse.c @@ -194,6 +194,7 @@ optfile_parse( FILE *fp, const char *filename, unsigned *lineno, char keyword[100]; char *buffer = NULL; size_t buflen = 0; + int inverse=0; if( !fp ) /* same as arg_parse() in this case */ return arg_parse( arg, opts ); @@ -216,6 +217,8 @@ optfile_parse( FILE *fp, const char *filename, unsigned *lineno, break; index = i; arg->r_opt = opts[index].short_opt; + if( inverse ) + arg->r_opt = -arg->r_opt; if( !opts[index].short_opt ) arg->r_opt = -2; /* unknown option */ else if( (opts[index].flags & 8) ) /* no optional argument */ diff --git a/util/iobuf.c b/util/iobuf.c index da8fea479..1c35db6a8 100644 --- a/util/iobuf.c +++ b/util/iobuf.c @@ -371,6 +371,36 @@ iobuf_create( const char *fname ) } /**************** + * append to a iobuf if the file does not exits; create it. + * cannont be used for stdout. + */ +IOBUF +iobuf_append( const char *fname ) +{ + IOBUF a; + FILE *fp; + file_filter_ctx_t *fcx; + size_t len; + + if( !fname ) + return NULL; + else if( !(fp = fopen(fname, "ab")) ) + return NULL; + a = iobuf_alloc(2, 8192 ); + fcx = m_alloc( sizeof *fcx + strlen(fname) ); + fcx->fp = fp; + strcpy(fcx->fname, fname ); + a->filter = file_filter; + a->filter_ov = fcx; + file_filter( fcx, IOBUFCTRL_DESC, NULL, (byte*)&a->desc, &len ); + file_filter( fcx, IOBUFCTRL_INIT, NULL, NULL, &len ); + if( DBG_IOBUF ) + log_debug("iobuf-%d.%d: append '%s'\n", a->no, a->subno, a->desc ); + + return a; +} + +/**************** * Register an i/o filter. */ int @@ -709,8 +739,25 @@ iobuf_tell( IOBUF a ) int iobuf_seek( IOBUF a, ulong newpos ) { + file_filter_ctx_t *b = NULL; - return -1; + for( ; a; a = a->chain ) { + if( !a->chain && a->filter == file_filter ) { + b = a->filter_ov; + break; + } + } + if( !a ) + return -1; + + if( fseek( b->fp, newpos, SEEK_SET ) ) { + log_error("can't seek to %lu: %s\n", newpos, strerror(errno) ); + return -1; + } + + /* FIXME: flush all buffers (and remove filters?)*/ + + return 0; } diff --git a/util/miscutil.c b/util/miscutil.c index 18fff2c08..327eae860 100644 --- a/util/miscutil.c +++ b/util/miscutil.c @@ -46,7 +46,7 @@ print_string( FILE *fp, byte *p, size_t n ) else if( !*p ) putc('0', fp); else - printf("x%02x", *p ); + fprintf(fp, "x%02x", *p ); } else putc(*p, fp); diff --git a/util/ttyio.c b/util/ttyio.c index 39ad5a666..74d31d1af 100644 --- a/util/ttyio.c +++ b/util/ttyio.c @@ -60,6 +60,28 @@ tty_printf( const char *fmt, ... ) } +/**************** + * Print a string, but filter all control characters out. + */ +void +tty_print_string( byte *p, size_t n ) +{ + for( ; n; n--, p++ ) + if( iscntrl( *p ) ) { + putc('\\', stderr); + if( *p == '\n' ) + putc('n', stderr); + else if( !*p ) + putc('0', stderr); + else + fprintf(stderr, "x%02x", *p ); + } + else + putc(*p, stderr); +} + + + char * tty_get( const char *prompt ) { |