Beispiel #1
0
/**
   Stringbuffer test
*/
static void sb_test()
{
	string_buffer_t b;
	int res;
	
	sb_init( &b );
	
	if( (res=sb_printf( &b, L"%ls%s", L"Testing ", "string_buffer_t " )) == -1 )
	{
		err( L"Error %d while testing stringbuffers", res );
	}
	
	if( (res=sb_printf( &b, L"%ls", L"functionality" ))==-1)	
	{
		err( L"Error %d while testing stringbuffers", res );
	}

	say( (wchar_t *)b.buff );

	sb_clear( &b );

	sb_printf( &b, L"%d %u %o %x %llX", -7, 99999999, 01234567, 0xdeadbeef, 0xdeadbeefdeadbeefll );
	if( wcscmp( (wchar_t *)b.buff,  NUM_ANS) != 0 )
	{
		err( L"numerical formating is broken, '%ls' != '%ls'", (wchar_t *)b.buff, NUM_ANS );
	}
	else
		say( L"numerical formating works" );	

	
}
Beispiel #2
0
const wchar_t *input_terminfo_get_name( const wchar_t *seq )
{
	int i;	
	static string_buffer_t *buff = 0;

	CHECK( seq, 0 );
	input_init();
		
	if( !buff )
	{
		buff = sb_halloc( global_context );
	}
	
	for( i=0; i<al_get_count( terminfo_mappings ); i++ )
	{
		terminfo_mapping_t *m = (terminfo_mapping_t *)al_get( terminfo_mappings, i );
		
		if( !m->seq )
		{
			continue;
		}
		
		sb_clear( buff );
		sb_printf( buff, L"%s", m->seq );
		
		if( !wcscmp( seq, (wchar_t *)buff->buff ) )
		{
			return m->name;
		}
	}
	
	return 0;
	
}
Beispiel #3
0
/**
   Remove backslashes from all newlines. This makes a string from the
   history file better formated for on screen display. The memory for
   the return value will be reused by subsequent calls to this
   function.
*/
static wchar_t *history_unescape_newlines( wchar_t *in )
{
    static string_buffer_t *out = 0;
    if( !out )
    {
        out = sb_halloc( global_context );
        if( !out )
        {
            DIE_MEM();
        }
    }
    else
    {
        sb_clear( out );
    }
    for( ; *in; in++ )
    {
        if( *in == L'\\' )
        {
            if( *(in+1)!= L'\n')
            {
                sb_append_char( out, *in );
            }
        }
        else
        {
            sb_append_char( out, *in );
        }
    }
    return (wchar_t *)out->buff;
}
Beispiel #4
0
const wchar_t *wsetlocale(int category, const wchar_t *locale)
{

	char *lang = locale?wcs2str( locale ):0;
	char * res = setlocale(category,lang);

	free( lang );

	/*
	  Use ellipsis if on known unicode system, otherwise use $
	*/
	char *ctype = setlocale( LC_CTYPE, (void *)0 );
	ellipsis_char = (strstr( ctype, ".UTF")||strstr( ctype, ".utf") )?L'\u2026':L'$';

	if( !res )
		return 0;

	if( !setlocale_buff )
	{
		setlocale_buff = sb_halloc( global_context);
	}

	sb_clear( setlocale_buff );
	sb_printf( setlocale_buff, L"%s", res );

	return (wchar_t *)setlocale_buff->buff;
}
Beispiel #5
0
static void *
worker_thread (void *arg)
{
  workerArg *wa = (workerArg *) arg;
  int ret;
  int flags;
  workerState ws;

  // set read flag to be nonblock 
  flags = fcntl (wa->rfd, F_GETFL, 0);
  assert (flags != -1);
  ret = fcntl (wa->rfd, F_SETFL, flags | O_NONBLOCK);
  assert (ret != -1);

  init_worker_state (&ws);
  ws.arg = wa;
  ws.size = wa->size;
  ws.tps = wa->tps;
  ws.el = aeCreateEventLoop (2048);
  assert (ws.el != NULL);

  ret =
    aeCreateFileEvent (ws.el, ws.arg->rfd, AE_READABLE, command_handler, &ws);
  assert (ret != AE_ERR);

  ws.teid = aeCreateTimeEvent (ws.el, 1, worker_cron, &ws, NULL);
  assert (ws.teid != -1LL);

  aeMain (ws.el);

  sb_clear (&ws.cin);
  sb_clear (&ws.in);
  sb_clear (&ws.out);
  if (ws.teid != -1LL)
    {
      aeDeleteTimeEvent (ws.el, ws.teid);
    }
  if (ws.el != NULL)
    {
      aeDeleteEventLoop (ws.el);
    }
  return NULL;
}
Beispiel #6
0
/**
   Add backslashes to all newlines, so that the returning string is
   suitable for writing to the history file. The memory for the return
   value will be reused by subsequent calls to this function.
*/
static wchar_t *history_escape_newlines( wchar_t *in )
{
    static string_buffer_t *out = 0;
    if( !out )
    {
        out = sb_halloc( global_context );
        if( !out )
        {
            DIE_MEM();
        }
    }
    else
    {
        sb_clear( out );
    }
    for( ; *in; in++ )
    {
        if( *in == L'\\' )
        {
            sb_append_char( out, *in );
            if( *(in+1) )
            {
                in++;
                sb_append_char( out, *in );
            }
            else
            {
                /*
                  This is a weird special case. When we are trying to
                  save a string that ends with a backslash, we need to
                  handle it specially, otherwise this command would be
                  combined with the one following it. We hack around
                  this by adding an additional newline.
                */
                sb_append_char( out, L'\n' );
            }

        }
        else if( *in == L'\n' )
        {
            sb_append_char( out, L'\\' );
            sb_append_char( out, *in );
        }
        else
        {
            sb_append_char( out, *in );
        }

    }
    return (wchar_t *)out->buff;
}
Beispiel #7
0
wchar_t *wbasename( const wchar_t *path )
{
	static string_buffer_t *sb = 0;
	if( sb )
		sb_clear(sb);
	else
		sb = sb_halloc( global_context );

	char *tmp =wutil_wcs2str(path);
	char *narrow_res = basename( tmp );
	if( !narrow_res )
		return 0;

	sb_printf( sb, L"%s", narrow_res );
	return (wchar_t *)sb->buff;
}
Beispiel #8
0
wchar_t *wdirname( wchar_t *path )
{
	static string_buffer_t *sb = 0;
	if( sb )
		sb_clear(sb);
	else
		sb = sb_halloc( global_context );

	char *tmp =wutil_wcs2str(path);
	char *narrow_res = dirname( tmp );
	if( !narrow_res )
		return 0;

	sb_printf( sb, L"%s", narrow_res );
	wcscpy( path, (wchar_t *)sb->buff );
	return path;
}
Beispiel #9
0
static void rename_file(const char *old_path, const char *new_name, gboolean showBox)
{
	char *new_path;
	char *dir;
	int res;

	dir = g_dirname(old_path);
	new_path = fu_join_path(dir, new_name);
	free(dir);

	if (fu_exists(new_path)) {
		int button;
		button = message_box(w_main, _("File Exists"), 
				     _("A file with this name already exists.\nDo you want to overwrite it?"), 
				     GTK_BUTTONS_YES_NO, GTK_RESPONSE_NO);
		if (button == GTK_RESPONSE_NO) {
			free(new_path);
			return;
		}
	}

	res = rename(old_path, new_path);
	if (res != 0) {
		int save_errno = errno;
		GString *msg = g_string_sized_new(256);

		g_string_printf(msg, _("Error renaming file:\n%s (%d)"), strerror(save_errno), save_errno);
		
		if(showBox)
			message_box(w_main, _("Error Renaming File"), msg->str, GTK_BUTTONS_OK, GTK_RESPONSE_OK);
		else {
			sb_clear();
			sb_printf(msg->str);
		}

		g_string_free(msg, TRUE);
	}
	else {
		fl_refresh(TRUE);
	}

	free(new_path);
}
Beispiel #10
0
/**
   Test the escaping/unescaping code by escaping/unescaping random
   strings and verifying that the original string comes back.
*/
static void test_escape()
{
	int i;
	string_buffer_t sb;
	
	say( L"Testing escaping and unescaping" );

	sb_init( &sb );
	
	for( i=0; i<ESCAPE_TEST_COUNT; i++ )
	{
		wchar_t *o, *e, *u;
		
		sb_clear( &sb );
		while( rand() % ESCAPE_TEST_LENGTH )
		{
			sb_append_char( &sb, (rand() %ESCAPE_TEST_CHAR) +1 );
		}
		o = (wchar_t *)sb.buff;
		e = escape(o, 1);
		u = unescape( e, 0 );
		if( !o || !e || !u )
		{
			err( L"Escaping cycle of string %ls produced null pointer on %ls", o, e?L"unescaping":L"escaping" );
			
		}
		
			
		if( wcscmp(o, u) )
		{
			err( L"Escaping cycle of string %ls produced different string %ls", o, u );
			
			
		}
		free( e );
		free( u );
		
	}
	

		
}
Beispiel #11
0
const wchar_t *wgettext( const wchar_t *in )
{
	if( !in )
		return in;

	if( !wgettext_is_init )
		wgettext_init();

	char *mbs_in = wgettext_wcs2str( in );
	char *out = gettext( mbs_in );
	wchar_t *wres=0;

	sb_clear( &buff[curr_buff] );

	sb_printf( &buff[curr_buff], L"%s", out );
	wres = (wchar_t *)buff[curr_buff].buff;
	curr_buff = (curr_buff+1)%BUFF_COUNT;

	return wres;
}
Beispiel #12
0
const wchar_t *input_terminfo_get_sequence( const wchar_t *name )
{
	const char *res = 0;
	int i;	
	static string_buffer_t *buff = 0;
	int err = ENOENT;
	
	CHECK( name, 0 );
	input_init();
	
	for( i=0; i<al_get_count( terminfo_mappings ); i++ )
	{
		terminfo_mapping_t *m = (terminfo_mapping_t *)al_get( terminfo_mappings, i );
		
		if( !wcscmp( name, m->name ) )
		{
			res = m->seq;
			err = EILSEQ;
			break;
		}
	}
	
	if( !res )
	{
		errno = err;
		return 0;
	}
	
	if( !buff )
	{
		buff = sb_halloc( global_context );
	}
	
	sb_clear( buff );
	sb_printf( buff, L"%s", res );

	return (wchar_t *)buff->buff;
		
}
Beispiel #13
0
wchar_t *wgetenv( const wchar_t *name )
{
	char *name_narrow =wutil_wcs2str(name);
	char *res_narrow = getenv( name_narrow );
	static string_buffer_t *out = 0;

	if( !res_narrow )
		return 0;

	if( !out )
	{
		out = sb_halloc( global_context );
	}
	else
	{
		sb_clear( out );
	}

	sb_printf( out, L"%s", res_narrow );

	return (wchar_t *)out->buff;

}
Beispiel #14
0
/**
   Returns an item_t for the specified adress. The adress must come from the item list of the specified mode.

   Later calls to this function may erase the output of a previous call to this function.
*/
static item_t *item_get( history_mode_t *m, void *d )
{
    char *begin = (char *)d;

    if( item_is_new( m, d ) )
    {
        return (item_t *)d;
    }
    else
    {

        char *end = m->mmap_start + m->mmap_length;
        char *pos=begin;

        int was_backslash = 0;
        static string_buffer_t *out = 0;
        static item_t narrow_item;
        int first_char = 1;
        int timestamp_mode = 0;

        narrow_item.timestamp = 0;

        if( !out )
        {
            out = sb_halloc( global_context );
            if( !out )
            {
                DIE_MEM();
            }
        }
        else
        {
            sb_clear( out );
        }

        while( 1 )
        {
            wchar_t c;
            mbstate_t state;
            size_t res;

            memset( &state, 0, sizeof(state) );

            res = mbrtowc( &c, pos, end-pos, &state );

            if( res == (size_t)-1 )
            {
                pos++;
                continue;
            }
            else if( res == (size_t)-2 )
            {
                break;
            }
            else if( res == (size_t)0 )
            {
                pos++;
                continue;
            }
            pos += res;

            if( c == L'\n' )
            {
                if( timestamp_mode )
                {
                    wchar_t *time_string = (wchar_t *)out->buff;
                    while( *time_string && !iswdigit(*time_string))
                        time_string++;
                    errno=0;

                    if( *time_string )
                    {
                        time_t tm;
                        wchar_t *end;

                        errno = 0;
                        tm = (time_t)wcstol( time_string, &end, 10 );

                        if( tm && !errno && !*end )
                        {
                            narrow_item.timestamp = tm;
                        }

                    }

                    sb_clear( out );
                    timestamp_mode = 0;
                    continue;
                }
                if( !was_backslash )
                    break;
            }

            if( first_char )
            {
                if( c == L'#' )
                    timestamp_mode = 1;
            }

            first_char = 0;

            sb_append_char( out, c );

            was_backslash = ( (c == L'\\') && !was_backslash);

        }

        narrow_item.data = history_unescape_newlines((wchar_t *)out->buff);
        return &narrow_item;
    }

}
Beispiel #15
0
void sb_free(SB *sb){
	free(sb->buffer);
	sb_clear(sb);
}