aboutsummaryrefslogtreecommitdiffhomepage
path: root/env_universal_common.cpp
diff options
context:
space:
mode:
authorGravatar ridiculousfish <corydoras@ridiculousfish.com>2011-12-26 19:18:46 -0800
committerGravatar ridiculousfish <corydoras@ridiculousfish.com>2011-12-26 19:18:46 -0800
commit8d2f107d61a8b0e099ab9a59b8a32c236da5a5fc (patch)
tree89f718ab74f8400332534aee237c6f925348f05c /env_universal_common.cpp
parent3f16ace6784caab54fb054836ee93902e9701913 (diff)
Some changes to migrate towards C++ and a multithreaded model
Diffstat (limited to 'env_universal_common.cpp')
-rw-r--r--env_universal_common.cpp294
1 files changed, 147 insertions, 147 deletions
diff --git a/env_universal_common.cpp b/env_universal_common.cpp
index 3cd58c0d..296baa5c 100644
--- a/env_universal_common.cpp
+++ b/env_universal_common.cpp
@@ -92,7 +92,7 @@
*/
typedef struct var_uni_entry
{
- int export; /**< Whether the variable should be exported */
+ int exportv; /**< Whether the variable should be exported */
wchar_t val[1]; /**< The value of the variable */
}
var_uni_entry_t;
@@ -109,8 +109,8 @@ hash_table_t env_universal_var;
/**
Callback function, should be called on all events
*/
-void (*callback)( int type,
- const wchar_t *key,
+void (*callback)( int type,
+ const wchar_t *key,
const wchar_t *val );
/**
@@ -140,8 +140,8 @@ static char *iconv_utf8_names[]=
*/
static char *iconv_wide_names_unknown[]=
{
- "wchar_t", "WCHAR_T",
- "wchar", "WCHAR",
+ "wchar_t", "WCHAR_T",
+ "wchar", "WCHAR",
0
}
;
@@ -151,12 +151,12 @@ static char *iconv_wide_names_unknown[]=
*/
static char *iconv_wide_names_4[]=
{
- "wchar_t", "WCHAR_T",
- "wchar", "WCHAR",
- "ucs-4", "UCS-4",
- "ucs4", "UCS4",
- "utf-32", "UTF-32",
- "utf32", "UTF32",
+ "wchar_t", "WCHAR_T",
+ "wchar", "WCHAR",
+ "ucs-4", "UCS-4",
+ "ucs4", "UCS4",
+ "utf-32", "UTF-32",
+ "utf32", "UTF32",
0
}
;
@@ -166,12 +166,12 @@ static char *iconv_wide_names_4[]=
*/
static char *iconv_wide_names_2[]=
{
- "wchar_t", "WCHAR_T",
- "wchar", "WCHAR",
- "ucs-2", "UCS-2",
- "ucs2", "UCS2",
- "utf-16", "UTF-16",
- "utf16", "UTF16",
+ "wchar_t", "WCHAR_T",
+ "wchar", "WCHAR",
+ "ucs-2", "UCS-2",
+ "ucs2", "UCS2",
+ "utf-16", "UTF-16",
+ "utf16", "UTF16",
0
}
;
@@ -196,7 +196,7 @@ static wchar_t *utf2wcs( const char *in )
switch (sizeof (wchar_t))
{
-
+
case 2:
to_name = iconv_wide_names_2;
break;
@@ -204,15 +204,15 @@ static wchar_t *utf2wcs( const char *in )
case 4:
to_name = iconv_wide_names_4;
break;
-
+
default:
to_name = iconv_wide_names_unknown;
break;
}
-
+
/*
- The line protocol fish uses is always utf-8.
+ The line protocol fish uses is always utf-8.
*/
char **from_name = iconv_utf8_names;
@@ -220,13 +220,13 @@ static wchar_t *utf2wcs( const char *in )
size_t out_len = sizeof( wchar_t )*(in_len+2);
size_t nconv;
char *nout;
-
- out = malloc( out_len );
+
+ out = (wchar_t *)malloc( out_len );
nout = (char *)out;
if( !out )
return 0;
-
+
for( i=0; to_name[i]; i++ )
{
for( j=0; from_name[j]; j++ )
@@ -236,7 +236,7 @@ static wchar_t *utf2wcs( const char *in )
if( cd != (iconv_t) -1)
{
goto start_conversion;
-
+
}
}
}
@@ -246,23 +246,23 @@ static wchar_t *utf2wcs( const char *in )
if (cd == (iconv_t) -1)
{
/* Something went wrong. */
- debug( 0, L"Could not perform utf-8 conversion" );
+ debug( 0, L"Could not perform utf-8 conversion" );
if(errno != EINVAL)
wperror( L"iconv_open" );
-
+
/* Terminate the output string. */
free(out);
- return 0;
+ return 0;
}
-
+
nconv = iconv( cd, (char **)&in, &in_len, &nout, &out_len );
-
+
if (nconv == (size_t) -1)
{
debug( 0, L"Error while converting from utf string" );
return 0;
}
-
+
*((wchar_t *) nout) = L'\0';
/*
@@ -281,12 +281,12 @@ static wchar_t *utf2wcs( const char *in )
}
free( out_old );
}
-
-
+
+
if (iconv_close (cd) != 0)
wperror (L"iconv_close");
-
- return out;
+
+ return out;
}
/**
@@ -296,7 +296,7 @@ static char *wcs2utf( const wchar_t *in )
{
iconv_t cd=(iconv_t) -1;
int i,j;
-
+
char *char_in = (char *)in;
char *out;
@@ -310,7 +310,7 @@ static char *wcs2utf( const wchar_t *in )
switch (sizeof (wchar_t))
{
-
+
case 2:
from_name = iconv_wide_names_2;
break;
@@ -318,7 +318,7 @@ static char *wcs2utf( const wchar_t *in )
case 4:
from_name = iconv_wide_names_4;
break;
-
+
default:
from_name = iconv_wide_names_unknown;
break;
@@ -330,24 +330,24 @@ static char *wcs2utf( const wchar_t *in )
size_t out_len = sizeof( char )*( (MAX_UTF8_BYTES*in_len)+1);
size_t nconv;
char *nout;
-
- out = malloc( out_len );
+
+ out = (char *)malloc( out_len );
nout = (char *)out;
in_len *= sizeof( wchar_t );
if( !out )
return 0;
-
+
for( i=0; to_name[i]; i++ )
{
for( j=0; from_name[j]; j++ )
{
cd = iconv_open ( to_name[i], from_name[j] );
-
+
if( cd != (iconv_t) -1)
{
goto start_conversion;
-
+
}
}
}
@@ -357,17 +357,17 @@ static char *wcs2utf( const wchar_t *in )
if (cd == (iconv_t) -1)
{
/* Something went wrong. */
- debug( 0, L"Could not perform utf-8 conversion" );
+ debug( 0, L"Could not perform utf-8 conversion" );
if(errno != EINVAL)
wperror( L"iconv_open" );
-
+
/* Terminate the output string. */
free(out);
- return 0;
+ return 0;
}
-
+
nconv = iconv( cd, &char_in, &in_len, &nout, &out_len );
-
+
if (nconv == (size_t) -1)
{
@@ -375,13 +375,13 @@ static char *wcs2utf( const wchar_t *in )
debug( 0, L"Error while converting from to string" );
return 0;
}
-
+
*nout = '\0';
-
+
if (iconv_close (cd) != 0)
wperror (L"iconv_close");
-
- return out;
+
+ return out;
}
@@ -421,9 +421,9 @@ static int read_byte( connection_t *src )
int res;
res = read( src->fd, src->buffer, ENV_UNIVERSAL_BUFFER_SIZE );
-
+
// debug(4, L"Read chunk '%.*s'", res, src->buffer );
-
+
if( res < 0 )
{
@@ -432,20 +432,20 @@ static int read_byte( connection_t *src )
{
return ENV_UNIVERSAL_AGAIN;
}
-
+
return ENV_UNIVERSAL_ERROR;
}
-
+
if( res == 0 )
{
return ENV_UNIVERSAL_EOF;
}
-
+
src->buffer_consumed = 0;
src->buffer_used = res;
}
-
+
return src->buffer[src->buffer_consumed++];
}
@@ -455,10 +455,10 @@ void read_message( connection_t *src )
{
while( 1 )
{
-
+
int ib = read_byte( src );
char b;
-
+
switch( ib )
{
case ENV_UNIVERSAL_AGAIN:
@@ -483,43 +483,43 @@ void read_message( connection_t *src )
{
char c = 0;
b_append( &src->input, &c, 1 );
- debug( 1,
- L"Universal variable connection closed while reading command. Partial command recieved: '%s'",
+ debug( 1,
+ L"Universal variable connection closed while reading command. Partial command recieved: '%s'",
(wchar_t *)src->input.buff );
}
return;
}
}
-
+
b = (char)ib;
-
+
if( b == '\n' )
{
wchar_t *msg;
-
+
b = 0;
b_append( &src->input, &b, 1 );
-
+
msg = utf2wcs( src->input.buff );
-
+
/*
Before calling parse_message, we must empty reset
everything, since the callback function could
potentially call read_message.
*/
src->input.used=0;
-
+
if( msg )
{
- parse_message( msg, src );
+ parse_message( msg, src );
}
else
{
debug( 0, _(L"Could not convert message '%s' to wide character string"), src->input.buff );
}
-
+
free( msg );
-
+
}
else
{
@@ -534,7 +534,7 @@ void read_message( connection_t *src )
void env_universal_common_remove( const wchar_t *name )
{
void *k, *v;
- hash_remove( &env_universal_var,
+ hash_remove( &env_universal_var,
name,
&k,
&v );
@@ -553,34 +553,34 @@ static int match( const wchar_t *msg, const wchar_t *cmd )
if( msg[len] && msg[len]!= L' ' && msg[len] != L'\t' )
return 0;
-
+
return 1;
}
-void env_universal_common_set( const wchar_t *key, const wchar_t *val, int export )
+void env_universal_common_set( const wchar_t *key, const wchar_t *val, int exportv )
{
var_uni_entry_t *entry;
wchar_t *name;
CHECK( key, );
CHECK( val, );
-
- entry = malloc( sizeof(var_uni_entry_t) + sizeof(wchar_t)*(wcslen(val)+1) );
+
+ entry = (var_uni_entry_t *)malloc( sizeof(var_uni_entry_t) + sizeof(wchar_t)*(wcslen(val)+1) );
name = wcsdup(key);
-
+
if( !entry || !name )
DIE_MEM();
-
- entry->export=export;
-
+
+ entry->exportv=exportv;
+
wcscpy( entry->val, val );
env_universal_common_remove( name );
-
+
hash_put( &env_universal_var, name, entry );
-
+
if( callback )
{
- callback( export?SET_EXPORT:SET, name, val );
+ callback( exportv?SET_EXPORT:SET, name, val );
}
}
@@ -588,67 +588,67 @@ void env_universal_common_set( const wchar_t *key, const wchar_t *val, int expor
/**
Parse message msg
*/
-static void parse_message( wchar_t *msg,
+static void parse_message( wchar_t *msg,
connection_t *src )
{
// debug( 3, L"parse_message( %ls );", msg );
-
+
if( msg[0] == L'#' )
return;
-
+
if( match( msg, SET_STR ) || match( msg, SET_EXPORT_STR ))
{
wchar_t *name, *tmp;
- int export = match( msg, SET_EXPORT_STR );
-
- name = msg+(export?wcslen(SET_EXPORT_STR):wcslen(SET_STR));
+ int exportv = match( msg, SET_EXPORT_STR );
+
+ name = msg+(exportv?wcslen(SET_EXPORT_STR):wcslen(SET_STR));
while( wcschr( L"\t ", *name ) )
name++;
-
+
tmp = wcschr( name, L':' );
if( tmp )
{
wchar_t *key;
wchar_t *val;
-
- key = malloc( sizeof( wchar_t)*(tmp-name+1));
+
+ key = (wchar_t *)malloc( sizeof( wchar_t)*(tmp-name+1));
memcpy( key, name, sizeof( wchar_t)*(tmp-name));
key[tmp-name]=0;
-
+
val = tmp+1;
val = unescape( val, 0 );
-
- env_universal_common_set( key, val, export );
-
+
+ env_universal_common_set( key, val, exportv );
+
free( val );
free( key );
}
else
{
debug( 1, PARSE_ERR, msg );
- }
+ }
}
else if( match( msg, ERASE_STR ) )
{
wchar_t *name, *tmp;
-
+
name = msg+wcslen(ERASE_STR);
while( wcschr( L"\t ", *name ) )
name++;
-
+
tmp = name;
while( iswalnum( *tmp ) || *tmp == L'_')
tmp++;
-
+
*tmp = 0;
-
+
if( !wcslen( name ) )
{
debug( 1, PARSE_ERR, msg );
}
env_universal_common_remove( name );
-
+
if( callback )
{
callback( ERASE, name, 0 );
@@ -671,7 +671,7 @@ static void parse_message( wchar_t *msg,
else
{
debug( 1, PARSE_ERR, msg );
- }
+ }
}
/**
@@ -684,7 +684,7 @@ static int try_send( message_t *msg,
{
debug( 3,
- L"before write of %d chars to fd %d", strlen(msg->body), fd );
+ L"before write of %d chars to fd %d", strlen(msg->body), fd );
int res = write( fd, msg->body, strlen(msg->body) );
@@ -696,26 +696,26 @@ static int try_send( message_t *msg,
{
debug( 4, L"Failed to write message '%s'", msg->body );
}
-
+
if( res == -1 )
{
switch( errno )
{
case EAGAIN:
return 0;
-
+
default:
debug( 2,
L"Error while sending universal variable message to fd %d. Closing connection",
fd );
if( debug_level > 2 )
wperror( L"write" );
-
+
return -1;
- }
+ }
}
msg->count--;
-
+
if( !msg->count )
{
free( msg );
@@ -726,7 +726,7 @@ static int try_send( message_t *msg,
void try_send_all( connection_t *c )
{
/* debug( 3,
- L"Send all updates to connection on fd %d",
+ L"Send all updates to connection on fd %d",
c->fd );*/
while( !q_empty( &c->unsent) )
{
@@ -735,12 +735,12 @@ void try_send_all( connection_t *c )
case 1:
q_get( &c->unsent);
break;
-
+
case 0:
debug( 4,
- L"Socket full, send rest later" );
+ L"Socket full, send rest later" );
return;
-
+
case -1:
c->killme = 1;
return;
@@ -779,16 +779,16 @@ static wchar_t *full_escape( const wchar_t *in )
message_t *create_message( int type,
- const wchar_t *key_in,
+ const wchar_t *key_in,
const wchar_t *val_in )
{
message_t *msg=0;
-
+
char *key=0;
size_t sz;
// debug( 4, L"Crete message of type %d", type );
-
+
if( key_in )
{
if( wcsvarname( key_in ) )
@@ -796,7 +796,7 @@ message_t *create_message( int type,
debug( 0, L"Illegal variable name: '%ls'", key_in );
return 0;
}
-
+
key = wcs2utf(key_in);
if( !key )
{
@@ -806,8 +806,8 @@ message_t *create_message( int type,
return 0;
}
}
-
-
+
+
switch( type )
{
case SET:
@@ -817,40 +817,40 @@ message_t *create_message( int type,
{
val_in=L"";
}
-
+
wchar_t *esc = full_escape( val_in );
if( !esc )
break;
-
+
char *val = wcs2utf(esc );
free(esc);
-
+
sz = strlen(type==SET?SET_MBS:SET_EXPORT_MBS) + strlen(key) + strlen(val) + 4;
- msg = malloc( sizeof( message_t ) + sz );
-
+ msg = (message_t *)malloc( sizeof( message_t ) + sz );
+
if( !msg )
DIE_MEM();
-
+
strcpy( msg->body, (type==SET?SET_MBS:SET_EXPORT_MBS) );
strcat( msg->body, " " );
strcat( msg->body, key );
strcat( msg->body, ":" );
strcat( msg->body, val );
strcat( msg->body, "\n" );
-
+
free( val );
-
+
break;
}
case ERASE:
{
sz = strlen(ERASE_MBS) + strlen(key) + 3;
- msg = malloc( sizeof( message_t ) + sz );
+ msg = (message_t *)malloc( sizeof( message_t ) + sz );
if( !msg )
DIE_MEM();
-
+
strcpy( msg->body, ERASE_MBS " " );
strcat( msg->body, key );
strcat( msg->body, "\n" );
@@ -859,24 +859,24 @@ message_t *create_message( int type,
case BARRIER:
{
- msg = malloc( sizeof( message_t ) +
+ msg = (message_t *)malloc( sizeof( message_t ) +
strlen( BARRIER_MBS ) +2);
if( !msg )
DIE_MEM();
strcpy( msg->body, BARRIER_MBS "\n" );
break;
}
-
+
case BARRIER_REPLY:
{
- msg = malloc( sizeof( message_t ) +
+ msg = (message_t *)malloc( sizeof( message_t ) +
strlen( BARRIER_REPLY_MBS ) +2);
if( !msg )
DIE_MEM();
strcpy( msg->body, BARRIER_REPLY_MBS "\n" );
break;
}
-
+
default:
{
debug( 0, L"create_message: Unknown message type" );
@@ -890,20 +890,20 @@ message_t *create_message( int type,
// debug( 4, L"Message body is '%s'", msg->body );
- return msg;
+ return msg;
}
/**
Function used with hash_foreach to insert keys of one table into
another
*/
-static void add_key_to_hash( void *key,
+static void add_key_to_hash( void *key,
void *data,
void *aux )
{
var_uni_entry_t *e = (var_uni_entry_t *)data;
- if( ( e->export && get_names_show_exported) ||
- ( !e->export && get_names_show_unexported) )
+ if( ( e->exportv && get_names_show_exported) ||
+ ( !e->exportv && get_names_show_unexported) )
al_push( (array_list_t *)aux, key );
}
@@ -913,25 +913,25 @@ void env_universal_common_get_names( array_list_t *l,
{
get_names_show_exported = show_exported;
get_names_show_unexported = show_unexported;
-
- hash_foreach2( &env_universal_var,
+
+ hash_foreach2( &env_universal_var,
add_key_to_hash,
l );
}
wchar_t *env_universal_common_get( const wchar_t *name )
{
- var_uni_entry_t *e = (var_uni_entry_t *)hash_get( &env_universal_var, name );
+ var_uni_entry_t *e = (var_uni_entry_t *)hash_get( &env_universal_var, name );
if( e )
return e->val;
- return 0;
+ return 0;
}
int env_universal_common_get_export( const wchar_t *name )
{
var_uni_entry_t *e = (var_uni_entry_t *)hash_get( &env_universal_var, name );
if( e )
- return e->export;
+ return e->exportv;
return 0;
}
@@ -952,17 +952,17 @@ static void enqueue( void *k,
const wchar_t *key = (const wchar_t *)k;
const var_uni_entry_t *val = (const var_uni_entry_t *)v;
dyn_queue_t *queue = (dyn_queue_t *)q;
-
- message_t *msg = create_message( val->export?SET_EXPORT:SET, key, val->val );
+
+ message_t *msg = create_message( val->exportv?SET_EXPORT:SET, key, val->val );
msg->count=1;
-
+
q_put( queue, msg );
}
void enqueue_all( connection_t *c )
{
hash_foreach2( &env_universal_var,
- &enqueue,
+ &enqueue,
(void *)&c->unsent );
try_send_all( c );
}
@@ -974,7 +974,7 @@ void connection_init( connection_t *c, int fd )
c->fd = fd;
b_init( &c->input );
q_init( &c->unsent );
- c->buffer_consumed = c->buffer_used = 0;
+ c->buffer_consumed = c->buffer_used = 0;
}
void connection_destroy( connection_t *c)