aboutsummaryrefslogtreecommitdiffstats
path: root/util
diff options
context:
space:
mode:
Diffstat (limited to 'util')
-rw-r--r--util/ChangeLog15
-rw-r--r--util/iobuf.c56
-rw-r--r--util/miscutil.c13
-rw-r--r--util/strgutil.c29
-rw-r--r--util/ttyio.c2
-rw-r--r--util/w32reg.c69
6 files changed, 141 insertions, 43 deletions
diff --git a/util/ChangeLog b/util/ChangeLog
index 76a7ced53..fd6ce4146 100644
--- a/util/ChangeLog
+++ b/util/ChangeLog
@@ -1,3 +1,18 @@
+2001-09-19 Werner Koch <[email protected]>
+
+ * w32reg.c (get_root_key): New.
+ (read_w32_registry_string): Use it here.
+ (write_w32_registry_string): New. Contributed by Timo.
+
+ * iobuf.c (iobuf_ioctl): New command to disable fd
+ caching. Implemented no_cache flag where needed.
+ (iobuf_sockopen): Always set no_cache flag.
+
+ * strgutil.c (utf8_to_native): Add a delim arg and changed all
+ callers. Make sure that quoting is done when translation is
+ disabled.
+ * miscutil.c (print_utf8_string2): New.
+
2001-09-17 Werner Koch <[email protected]>
* miscutil.c (print_string): Use explicit ranges and not iscntrl().
diff --git a/util/iobuf.c b/util/iobuf.c
index db0a31568..60ae6c866 100644
--- a/util/iobuf.c
+++ b/util/iobuf.c
@@ -58,6 +58,7 @@
typedef struct {
FILE *fp; /* open file handle */
int keep_open;
+ int no_cache;
int print_only_name; /* flags indicating that fname is not a real file*/
char fname[1]; /* name of the file */
} file_filter_ctx_t ;
@@ -80,6 +81,7 @@
typedef struct {
FILEP_OR_FD fp; /* open file handle */
int keep_open;
+ int no_cache;
int eof_seen;
int print_only_name; /* flags indicating that fname is not a real file*/
char fname[1]; /* name of the file */
@@ -98,6 +100,7 @@
typedef struct {
int sock;
int keep_open;
+ int no_cache;
int eof_seen;
int print_only_name; /* flags indicating that fname is not a real file*/
char fname[1]; /* name of the file */
@@ -142,6 +145,8 @@ fd_cache_invalidate (const char *fname)
for (cc=close_cache; cc; cc = cc->next ) {
if ( cc->fp != INVALID_FP && !strcmp (cc->fname, fname) ) {
+ if( DBG_IOBUF )
+ log_debug (" did (%s)\n", cc->fname);
#ifdef HAVE_DOSISH_SYSTEM
CloseHandle (cc->fp);
#else
@@ -225,19 +230,21 @@ fd_cache_close (const char *fname, FILEP_OR_FD fp)
close(fp);
#endif
if( DBG_IOBUF )
- log_debug ("fd_cache_close (%s) immediately\n", fname);
+ log_debug ("fd_cache_close (%p) real\n", fp);
return;
}
/* try to reuse a slot */
for (cc=close_cache; cc; cc = cc->next ) {
if ( cc->fp == INVALID_FP && !strcmp (cc->fname, fname) ) {
cc->fp = fp;
+ if( DBG_IOBUF )
+ log_debug ("fd_cache_close (%s) used existing slot\n", fname);
return;
}
}
/* add a new one */
if( DBG_IOBUF )
- log_debug ("fd_cache_close (%s) new\n", fname);
+ log_debug ("fd_cache_close (%s) new slot created\n", fname);
cc = m_alloc_clear (sizeof *cc + strlen (fname));
strcpy (cc->fname, fname);
cc->fp = fp;
@@ -259,7 +266,7 @@ fd_cache_open (const char *fname, const char *mode)
FILEP_OR_FD fp = cc->fp;
cc->fp = INVALID_FP;
if( DBG_IOBUF )
- log_debug ("fd_cache_open (%s) hit\n", fname);
+ log_debug ("fd_cache_open (%s) using cached fp\n", fname);
#ifdef HAVE_DOSISH_SYSTEM
if (SetFilePointer (fp, 0, NULL, FILE_BEGIN) == 0xffffffff ) {
log_error ("rewind file failed on handle %p: ec=%d\n",
@@ -276,7 +283,7 @@ fd_cache_open (const char *fname, const char *mode)
}
}
if( DBG_IOBUF )
- log_debug ("fd_cache_open (%s) miss\n", fname);
+ log_debug ("fd_cache_open (%s) not cached\n", fname);
return direct_open (fname, mode);
}
@@ -350,7 +357,7 @@ file_filter(void *opaque, int control, IOBUF chain, byte *buf, size_t *ret_len)
*ret_len = nbytes;
}
else if( control == IOBUFCTRL_INIT ) {
- a->keep_open = 0;
+ a->keep_open = a->no_cache = 0;
}
else if( control == IOBUFCTRL_DESC ) {
*(char**)buf = "file_filter";
@@ -463,6 +470,7 @@ file_filter(void *opaque, int control, IOBUF chain, byte *buf, size_t *ret_len)
else if ( control == IOBUFCTRL_INIT ) {
a->eof_seen = 0;
a->keep_open = 0;
+ a->no_cache = 0;
}
else if ( control == IOBUFCTRL_DESC ) {
*(char**)buf = "file_filter(fd)";
@@ -473,14 +481,14 @@ file_filter(void *opaque, int control, IOBUF chain, byte *buf, size_t *ret_len)
if( DBG_IOBUF )
log_debug("%s: close handle %p\n", a->fname, f );
if (!a->keep_open)
- fd_cache_close (a->fname, f);
+ fd_cache_close (a->no_cache?NULL:a->fname, f);
}
#else
if ( (int)f != 0 && (int)f != 1 ) {
if( DBG_IOBUF )
log_debug("%s: close fd %d\n", a->fname, f );
if (!a->keep_open)
- fd_cache_close (a->fname, f);
+ fd_cache_close (a->no_cache?NULL:a->fname, f);
}
f = INVALID_FP;
#endif
@@ -550,6 +558,7 @@ sock_filter (void *opaque, int control, IOBUF chain, byte *buf, size_t *ret_len)
else if ( control == IOBUFCTRL_INIT ) {
a->eof_seen = 0;
a->keep_open = 0;
+ a->no_cache = 0;
}
else if ( control == IOBUFCTRL_DESC ) {
*(char**)buf = "sock_filter";
@@ -1096,8 +1105,8 @@ iobuf_fdopen( int fd, const char *mode )
IOBUF
iobuf_sockopen ( int fd, const char *mode )
{
-#ifdef __MINGW32__
IOBUF a;
+#ifdef __MINGW32__
sock_filter_ctx_t *scx;
size_t len;
@@ -1112,10 +1121,12 @@ iobuf_sockopen ( int fd, const char *mode )
sock_filter( scx, IOBUFCTRL_INIT, NULL, NULL, &len );
if( DBG_IOBUF )
log_debug("iobuf-%d.%d: sockopen `%s'\n", a->no, a->subno, scx->fname);
- return a;
#else
- return iobuf_fdopen (fd, mode);
+ a = iobuf_fdopen (fd, mode);
#endif
+ if (a)
+ iobuf_ioctl (a,3,1,NULL); /* disable fd caching */
+ return a;
}
/****************
@@ -1226,6 +1237,9 @@ int
iobuf_ioctl ( IOBUF a, int cmd, int intval, void *ptrval )
{
if ( cmd == 1 ) { /* keep system filepointer/descriptor open */
+ if( DBG_IOBUF )
+ log_debug("iobuf-%d.%d: ioctl `%s' keep=%d\n",
+ a? a->no:-1, a?a->subno:-1, a?a->desc:"?", intval );
for( ; a; a = a->chain )
if( !a->chain && a->filter == file_filter ) {
file_filter_ctx_t *b = a->filter_ov;
@@ -1241,6 +1255,9 @@ iobuf_ioctl ( IOBUF a, int cmd, int intval, void *ptrval )
#endif
}
else if ( cmd == 2 ) { /* invalidate cache */
+ if( DBG_IOBUF )
+ log_debug("iobuf-*.*: ioctl `%s' invalidate\n",
+ ptrval? (char*)ptrval:"?");
if ( !a && !intval && ptrval ) {
#ifndef FILE_FILTER_USES_STDIO
fd_cache_invalidate (ptrval);
@@ -1248,6 +1265,24 @@ iobuf_ioctl ( IOBUF a, int cmd, int intval, void *ptrval )
return 0;
}
}
+ else if ( cmd == 3 ) { /* disallow/allow caching */
+ if( DBG_IOBUF )
+ log_debug("iobuf-%d.%d: ioctl `%s' no_cache=%d\n",
+ a? a->no:-1, a?a->subno:-1, a?a->desc:"?", intval );
+ for( ; a; a = a->chain )
+ if( !a->chain && a->filter == file_filter ) {
+ file_filter_ctx_t *b = a->filter_ov;
+ b->no_cache = intval;
+ return 0;
+ }
+ #ifdef __MINGW32__
+ else if( !a->chain && a->filter == sock_filter ) {
+ sock_filter_ctx_t *b = a->filter_ov;
+ b->no_cache = intval;
+ return 0;
+ }
+ #endif
+ }
return -1;
}
@@ -1536,7 +1571,6 @@ iobuf_flush(IOBUF a)
if( a->directfp )
return 0;
- /*log_debug("iobuf-%d.%d: flush\n", a->no, a->subno );*/
if( a->use == 3 ) { /* increase the temp buffer */
char *newbuf;
size_t newsize = a->d.size + 8192;
diff --git a/util/miscutil.c b/util/miscutil.c
index 05beab0d6..3bff721c1 100644
--- a/util/miscutil.c
+++ b/util/miscutil.c
@@ -207,7 +207,7 @@ print_string( FILE *fp, const byte *p, size_t n, int delim )
* Print an UTF8 string to FP and filter all control characters out.
*/
void
-print_utf8_string( FILE *fp, const byte *p, size_t n )
+print_utf8_string2 ( FILE *fp, const byte *p, size_t n, int delim )
{
size_t i;
char *buf;
@@ -218,12 +218,19 @@ print_utf8_string( FILE *fp, const byte *p, size_t n )
break;
}
if( i < n ) {
- buf = utf8_to_native( p, n );
+ buf = utf8_to_native ( p, n, delim );
+ /*(utf8 conversion already does the control character quoting)*/
fputs( buf, fp );
m_free( buf );
}
else
- print_string( fp, p, n, 0 );
+ print_string( fp, p, n, delim );
+}
+
+void
+print_utf8_string( FILE *fp, const byte *p, size_t n )
+{
+ print_utf8_string2 (fp, p, n, 0);
}
/****************
diff --git a/util/strgutil.c b/util/strgutil.c
index 5e978eb04..7b72bbbe3 100644
--- a/util/strgutil.c
+++ b/util/strgutil.c
@@ -439,11 +439,13 @@ native_to_utf8( const char *string )
/****************
- * Convert string, which is in UTF8 to native encoding.
- * illegal encodings by some "\xnn" and quote all control characters
- */
+ * Convert string, which is in UTF8 to native encoding. illegal
+ * encodings by some "\xnn" and quote all control characters. A
+ * character with value DELIM will always be quoted, it must be a
+ * vanilla ASCII character.
+ */
char *
-utf8_to_native( const char *string, size_t length )
+utf8_to_native( const char *string, size_t length, int delim )
{
int nleft;
int i;
@@ -456,13 +458,6 @@ utf8_to_native( const char *string, size_t length )
size_t slen;
int resync = 0;
- if (no_translation) {
- buffer = m_alloc (length+1);
- memcpy (buffer, string, length);
- buffer[length] = 0; /* make sure it is a string */
- return buffer;
- }
-
/* 1. pass (p==NULL): count the extended utf-8 characters */
/* 2. pass (p!=NULL): create string */
for( ;; ) {
@@ -481,7 +476,7 @@ utf8_to_native( const char *string, size_t length )
}
if( !nleft ) {
if( !(*s & 0x80) ) { /* plain ascii */
- if( iscntrl( *s ) ) {
+ if( *s < 0x20 || *s == 0x7f || *s == delim) {
n++;
if( p )
*p++ = '\\';
@@ -564,7 +559,15 @@ utf8_to_native( const char *string, size_t length )
val <<= 6;
val |= *s & 0x3f;
if( !--nleft ) { /* ready */
- if( active_charset ) { /* table lookup */
+ if (no_translation) {
+ if( p ) {
+ for(i=0; i < encidx; i++ )
+ *p++ = encbuf[i];
+ }
+ n += encidx;
+ encidx = 0;
+ }
+ else if( active_charset ) { /* table lookup */
for(i=0; i < 128; i++ ) {
if( active_charset[i] == val )
break;
diff --git a/util/ttyio.c b/util/ttyio.c
index 513026d43..c5f875314 100644
--- a/util/ttyio.c
+++ b/util/ttyio.c
@@ -257,7 +257,7 @@ tty_print_utf8_string2( byte *p, size_t n, size_t max_n )
break;
}
if( i < n ) {
- buf = utf8_to_native( p, n );
+ buf = utf8_to_native( p, n, 0 );
if( strlen( buf ) > max_n ) {
buf[max_n] = 0;
}
diff --git a/util/w32reg.c b/util/w32reg.c
index 0cf38090f..a0da975ef 100644
--- a/util/w32reg.c
+++ b/util/w32reg.c
@@ -29,23 +29,13 @@
#include "util.h"
#include "memory.h"
-
-/****************
- * Return a string from the Win32 Registry or NULL in case of
- * error. Caller must release the return value. A NUKK for root
- * is an alias fro HKEY_CURRENT_USER
- * NOTE: The value is allocated with a plain malloc() - use free() and not
- * the usual m_free()!!!
- */
-char *
-read_w32_registry_string( const char *root, const char *dir, const char *name )
+static HKEY
+get_root_key(const char *root)
{
- HKEY root_key, key_handle;
- DWORD n1, nbytes;
- char *result = NULL;
-
+ HKEY root_key;
+
if( !root )
- root_key = HKEY_CURRENT_USER;
+ root_key = HKEY_CURRENT_USER;
else if( !strcmp( root, "HKEY_CLASSES_ROOT" ) )
root_key = HKEY_CLASSES_ROOT;
else if( !strcmp( root, "HKEY_CURRENT_USER" ) )
@@ -59,6 +49,27 @@ read_w32_registry_string( const char *root, const char *dir, const char *name )
else if( !strcmp( root, "HKEY_CURRENT_CONFIG" ) )
root_key = HKEY_CURRENT_CONFIG;
else
+ return NULL;
+
+ return root_key;
+}
+
+
+/****************
+ * Return a string from the Win32 Registry or NULL in case of
+ * error. Caller must release the return value. A NUKK for root
+ * is an alias fro HKEY_CURRENT_USER
+ * NOTE: The value is allocated with a plain malloc() - use free() and not
+ * the usual m_free()!!!
+ */
+char *
+read_w32_registry_string( const char *root, const char *dir, const char *name )
+{
+ HKEY root_key, key_handle;
+ DWORD n1, nbytes;
+ char *result = NULL;
+
+ if ( !(root_key = get_root_key(root) ) )
return NULL;
if( RegOpenKeyEx( root_key, dir, 0, KEY_READ, &key_handle ) )
@@ -82,7 +93,35 @@ read_w32_registry_string( const char *root, const char *dir, const char *name )
}
+int
+write_w32_registry_string(const char *root, const char *dir,
+ const char *name, const char *value)
+{
+ HKEY root_key, reg_key;
+
+ if ( !(root_key = get_root_key(root) ) )
+ return -1;
+ if ( RegOpenKeyEx( root_key, dir, 0, KEY_WRITE, &reg_key )
+ != ERROR_SUCCESS )
+ return -1;
+
+ if ( RegSetValueEx( reg_key, name, 0, REG_SZ, (BYTE *)value,
+ strlen( value ) ) != ERROR_SUCCESS ) {
+ if ( RegCreateKey( root_key, name, &reg_key ) != ERROR_SUCCESS ) {
+ RegCloseKey(reg_key);
+ return -1;
+ }
+ if ( RegSetValueEx( reg_key, name, 0, REG_SZ, (BYTE *)value,
+ strlen( value ) ) != ERROR_SUCCESS ) {
+ RegCloseKey(reg_key);
+ return -1;
+ }
+ }
+ RegCloseKey( reg_key );
+
+ return 0;
+}
#endif /* __MINGW32__ */