Esempio n. 1
0
File: utils.c Progetto: MUME/mudlle
static void vlog_message(const char *fname, const char *nname, int line,
                         bool is_warning, const char *msg, va_list va)
{
  strbuf_t sb = SBNULL;

  if (fname != NULL)
    {
      bool use_fname = !use_nicename || nname == NULL;

      sb_printf(&sb, "%s:", use_fname ? fname : nname);
      if (line > 0)
        sb_printf(&sb, "%d:", line);
      sb_addc(&sb, ' ');
    }
  if (is_warning)
    sb_addstr(&sb, "warning: ");
  if (!use_nicename && fname != NULL && nname != NULL
      && strcmp(nname, fname) != 0)
    sb_printf(&sb, "[%s] ", nname);
  sb_vprintf(&sb, msg, va);
  if (mudout) pflush(mudout);
  pprintf(muderr, "%s\n", sb_str(&sb));
  if (muderr) pflush(muderr);
  sb_free(&sb);
  if (!is_warning)
    erred = true;
}
Esempio n. 2
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" );	

	
}
Esempio n. 3
0
int searchy_prepare_stats (struct connection *c) {
  stats_buffer_t sb;
  sb_prepare (&sb, c, stats_buff, STATS_BUFF_SIZE);
  sb_memory (&sb, AM_GET_MEMORY_USAGE_SELF);
  sb_printf (&sb, "malloc_memory_used\t%lld\n", get_malloc_memory_used ());
  SB_BINLOG;
  SB_INDEX;
  sb_printf (&sb,
    "index_items\t%d\n"
    "index_words\t%d\n"
    "index_data_bytes\t%lld\n",
    idx_items, idx_words, idx_bytes);

  sb_printf (&sb, "left_subtree_size_threshold\t%d\n", Header.left_subtree_size_threshold);
  SB_PRINT_QUERIES(search_queries);
  SB_PRINT_QUERIES(increment_queries);
  SB_PRINT_QUERIES(set_rate_queries);
  SB_PRINT_QUERIES(delete_item_queries);
  SB_PRINT_QUERIES(set_item_queries);

  sb_printf (&sb,
      "items\t%d\n"
      "items_marked_as_deleted\t%d\n"
      "items_freed\t%d\n"
      "index_items_deleted\t%d\n"
      "tree_nodes_allocated\t%d\n"
      "tree_nodes_unused\t%d\n",
    tot_items,
    del_items,
    tot_freed_deleted_items,
    mod_items,
    alloc_tree_nodes,
    free_tree_nodes);

  SB_PRINT_I64(tree_positions_bytes);
  SB_PRINT_I32(decoder_positions_max_capacity);
  SB_PRINT_I32(max_search_query_memory);
  SB_PRINT_I32(use_stemmer);
  SB_PRINT_I32(universal);
  SB_PRINT_I32(hashtags_enabled);
  SB_PRINT_I32(wordfreqs_enabled);
  SB_PRINT_I32(stemmer_version);
  SB_PRINT_I32(word_split_version);
  SB_PRINT_I32(word_split_utf8);
  SB_PRINT_I32(tag_owner);
  SB_PRINT_I32(listcomp_version);
  SB_PRINT_I32(creation_date);

  sb_printf (&sb, "version\t%s\n", FullVersionStr);

  return sb.pos;
}
Esempio n. 4
0
void load_file_list()
{
	static const char *patterns[] = {
#ifdef ENABLE_MP3
		".+[mM][pP][aA23]$",
#endif
#ifdef ENABLE_VORBIS
		".+[oO][gG][gGaA]$",
#endif
		NULL
	};

	gint count;

	/* clear the form in the edit tab */ 
	et_unload_file();

	sb_printf(_("Scanning..."));
	cursor_set_wait();

	/* flush pending operations before we start */
	while (gtk_events_pending()) gtk_main_iteration();

	/* rebuild the file list */
	if (file_list) {
		g_elist_free_data(file_list);
		file_list = NULL;
	}
	scan_progress_start();
	file_list = fu_get_file_list(working_dir->str, patterns, 
				     scan_progress_callback, 
				     gtk_toggle_button_get_active(cb_recurse),
				     TRUE);
	scan_progress_stop();

	/* update the interface */
	update_tree_view(file_list);
	count = g_elist_length(file_list);
	switch (count) {
	    case 0:
		sb_printf(_("No files found."));
		break;
	    case 1:
		sb_printf(_("1 file found."));
		break;
	    default:
		sb_printf(_("%d files found."), count);
		break;
	}

	cursor_set_normal();
}
Esempio n. 5
0
int seqmap_prepare_stats (struct connection *c) {
  stats_buffer_t sb;
  sb_prepare (&sb, c, stats_buff, STATS_BUFF_SIZE);
  sb_memory (&sb, AM_GET_MEMORY_USAGE_SELF);
  sb_printf (&sb,
    "malloc_mem\t%ld\n"
    "zalloc_mem\t%ld\n",
    (long) malloc_mem,
    (long) zalloc_mem);

  SB_BINLOG;
  //data_prepare_stats (&sb);
  sb_printf (&sb, "%s\n", FullVersionStr);
  return sb.pos;
}
Esempio n. 6
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;
	
}
Esempio n. 7
0
void debug( int level, const wchar_t *msg, ... )
{
	va_list va;

	string_buffer_t sb;
	string_buffer_t sb2;

	int errno_old = errno;

	if( level > debug_level )
		return;

	CHECK( msg, );

	sb_init( &sb );
	sb_init( &sb2 );

	sb_printf( &sb, L"%ls: ", program_name );

	va_start( va, msg );
	sb_vprintf( &sb, msg, va );
	va_end( va );

	write_screen( (wchar_t *)sb.buff, &sb2 );
	fwprintf( stderr, L"%ls", sb2.buff );

	sb_destroy( &sb );
	sb_destroy( &sb2 );

	errno = errno_old;
}
Esempio n. 8
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;
}
Esempio n. 9
0
static anna_node_t *anna_macro_attribute_expand(anna_node_call_t *node, anna_node_call_t *attr)
{
    int i;
    
    for(i = attr->child_count-1; i >= 0; i--)
    {
	if(attr->child[i]->node_type == ANNA_NODE_IDENTIFIER)
	{
	    anna_node_identifier_t *nam = (anna_node_identifier_t *)attr->child[i];
	    string_buffer_t sb;
	    sb_init(&sb);
	    sb_printf(&sb, L"%lsAttribute", nam->name);
	    
	    node = anna_node_create_call2(
		&node->location,
		anna_node_create_identifier(
		    &nam->location,
		    sb_content(&sb)),
		node);
	    sb_destroy(&sb);
/*
	    anna_message(L"FADFADS\n");
	    anna_node_print(5, nam);
	    anna_message(L"\n\n");
*/
	}
	
    }
    return (anna_node_t *)node;
}
Esempio n. 10
0
static void anna_indent(string_buffer_t *sb, int indentation)
{
    int indent;
    for(indent=0; indent<indentation; indent++)
    {
	sb_printf(sb,L"    ");
    }
}
Esempio n. 11
0
int stats_prepare_stats (struct connection *c) {
  stats_buffer_t sb;
  sb_prepare (&sb, c, stats_buff, STATS_BUFF_SIZE);
  sb_memory (&sb, AM_GET_MEMORY_USAGE_SELF + AM_GET_MEMORY_USAGE_OVERALL);  
  SB_BINLOG;
  SB_INDEX;

  sb_printf (&sb,
      "snapshot_loading_average_blocking_read_bytes\t%.6f\n"
      "snapshot_loading_blocking_read_calls\t%d\n"
      "tot_user_metafiles\t%d\n"
      "tot_user_metafile_bytes\t%lld\n"
      "counters\t%d\n"
      "counters_percent\t%.6f\n"
      "counters_prime\t%d\n"
      "total_views\t%lld\n"
      "tree_nodes_allocated\t%d\n"
      "counter_instances\t%d\n"
      "counter_instances_percent\t%.6f\n"
      "allocated_counter_instances\t%d\n"
      "deleted_by_LRU\t%lld\n"
      "allocated_memory\t%lld\n"
      "tot_aio_queries\t%lld\n"
      "active_aio_queries\t%lld\n"
      "expired_aio_queries\t%lld\n"
      "avg_aio_query_time\t%.6f\n"
      "aio_bytes_loaded\t%lld\n"
      "tot_aio_queries\t%lld\n"
      "tot_aio_fails\t%lld\n"
      "memory_to_index\t%lld\n"
      "version\t%s\n",
    snapshot_loading_average_blocking_read_bytes,
    snapshot_loading_blocking_read_calls,
    tot_user_metafiles,
    tot_user_metafile_bytes,
    tot_counters,
    percent (tot_counters, max_counters),
    counters_prime,
    tot_views,
    alloc_tree_nodes,
    tot_counter_instances,
    percent (tot_counter_instances, index_size),
    tot_counters_allocated,
    deleted_by_lru,
    tot_memory_allocated,
    tot_aio_queries,
    active_aio_queries,
    expired_aio_queries,
    tot_aio_queries > 0 ? total_aio_time / tot_aio_queries : 0,
    tot_aio_loaded_bytes,
    tot_aio_queries,
    tot_aio_fails,
    memory_to_index,
    FullVersionStr
    );
  return sb.pos;
}
Esempio n. 12
0
static gboolean check_working_dir()
{
	gint res;
	struct stat stat_data;

	res = stat(working_dir->str, &stat_data);
	if (res < 0) {
		sb_printf(_("Error: Can't open directory \"%s\"."), working_dir_utf8->str);
		return FALSE;
	}
	if (!S_ISDIR(stat_data.st_mode)) {
		sb_printf(_("Error: \"%s\" is not a directory."), working_dir_utf8->str);
		return FALSE;
	}
	if (!fu_check_permission(&stat_data, "rx")) {
		sb_printf(_("Error: Permission denied for \"%s\"."), working_dir_utf8->str);
		return FALSE;
	}
	return TRUE;
}
Esempio n. 13
0
void sb_format_size( string_buffer_t *sb,
		     long long sz )
{
	wchar_t *sz_name[]=
		{
			L"kB", L"MB", L"GB", L"TB", L"PB", L"EB", L"ZB", L"YB", 0
		}
	;

	if( sz < 0 )
	{
		sb_append( sb, L"unknown" );
	}
	else if( sz < 1 )
	{
		sb_append( sb, _( L"empty" ) );
	}
	else if( sz < 1024 )
	{
		sb_printf( sb, L"%lldB", sz );
	}
	else
	{
		int i;

		for( i=0; sz_name[i]; i++ )
		{
			if( sz < (1024*1024) || !sz_name[i+1] )
			{
				int isz = sz/1024;
				if( isz > 9 )
					sb_printf( sb, L"%d%ls", isz, sz_name[i] );
				else
					sb_printf( sb, L"%.1f%ls", (double)sz/1024, sz_name[i] );
				break;
			}
			sz /= 1024;

		}
	}
}
Esempio n. 14
0
Void task1(UArg arg0, UArg arg1)
{
	sb_printf("\nWaiting for message.\n");

	for (;;);

#if 0
	/* Waiting for a ping request from the remote (host) side */
	while (!REG32(MAILBOX_STATUS(HOST_TO_SYSM3_MBX)));

	startExchange((Uint32)arg0);
#endif
}
Esempio n. 15
0
int friend_prepare_stats (struct connection *c) {
    stats_buffer_t sb;
    sb_prepare (&sb, c, stats_buff, STATS_BUFF_SIZE);
    sb_memory (&sb, AM_GET_MEMORY_USAGE_SELF);
    SB_BINLOG;
    SB_INDEX;

    sb_printf (&sb,
               "tree_nodes\t%d\n"
               "privacy_nodes\t%d\n"
               "reverse_friends_nodes\t%d\n"
               "total_privacy_len\t%d\n"
               "memory_users\t%d\n",
               alloc_tree_nodes,
               privacy_nodes,
               alloc_rev_friends_nodes,
               tot_privacy_len,
               tot_users);

    sb_printf (&sb, "version\t%s\n", FullVersionStr);
    return sb.pos;
}
Esempio n. 16
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;
}
int copyexec_results_prepare_stats (struct connection *c) {
  int log_uncommitted = compute_uncommitted_log_bytes();
  dyn_update_stats ();
  long long a[7];
  get_memory_usage (a, 7);

  stats_buffer_t sb;
  sb_prepare (&sb, c, stats_buff, STATS_BUFF_SIZE);
  sb_memory (&sb, AM_GET_MEMORY_USAGE_SELF);

  sb_printf (&sb,
      "binlog_original_size\t%lld\n"
      "binlog_loaded_bytes\t%lld\n"
      "binlog_load_time\t%.6fs\n"
      "current_binlog_size\t%lld\n"
      "binlog_uncommitted_bytes\t%d\n"
      "binlog_path\t%s\n"
      "binlog_first_timestamp\t%d\n"
      "binlog_read_timestamp\t%d\n"
      "binlog_last_timestamp\t%d\n"
      "cmd_get\t%lld\n"
      "cmd_stats\t%lld\n"
      "hosts\t%d\n"
      "tot_memory_transactions\t%d\n"
      "max_memory_transactions\t%d\n"
      "alloc_tree_nodes\t%d\n"
      "free_tree_nodes\t%d\n"
      "version\t%s\n",
    log_readto_pos,
    binlog_loaded_size,
    binlog_load_time,
    log_pos,
    log_uncommitted,
    binlogname ? (strlen(binlogname) < 250 ? binlogname : "(too long)") : "(none)",
    log_first_ts,
    log_read_until,
    log_last_ts,
    cmd_get,
    cmd_stats,
    hosts,
    tot_memory_transactions,
    max_lru_size,
    alloc_tree_nodes,
    free_tree_nodes,
    FullVersionStr);
  return sb.pos;
}
Esempio n. 18
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;
}
Esempio n. 19
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);
}
Esempio n. 20
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;
}
Esempio n. 21
0
Void main(Int argc, Char *argv[])
{
	Task_Params params;
	volatile uint32_t *addr = (uint32_t *)SAR_BANK1_VIRT;

	serial_init();

	sb_printf("Sysbios main\r\n");

	*addr = 0xAABBCCDD;


	Task_Params_init(&params);
	params.instance->name = "test_copy";
	params.priority = 3;
	params.arg0 = 50;
	Task_create(task1, &params, NULL);

	BIOS_start();
}
Esempio n. 22
0
static Void startExchange(Uint32 arg)
{
	MessageQCopy_Handle    handle;
	Char                   buffer[128];
	UInt32                 myEndpoint = 0;
	UInt32                 remoteEndpoint;
	UInt16                 len;
	UInt16				   dstProc;
	Int                    i;

	sb_printf("copyTask %d: Entered...:\n", arg);

	dstProc = MultiProc_getId("HOST");

	MessageQCopy_init(dstProc);

    /* Create the messageQ for receiving (and get our endpoint for sending). */
	handle = MessageQCopy_create(arg, &myEndpoint);

	NameMap_register("fuckit-all", arg);

	for (i = 0; i < APP_NUM_ITERATIONS; i++) {
		/* Await a character message: */
		MessageQCopy_recv(handle, (Ptr)buffer, &len, &remoteEndpoint,
							MessageQCopy_FOREVER);

		buffer[len] = '\0';

		len = 8;
		memcpy((Ptr)buffer, "response", 8);
		/* Send data back to remote endpoint: */
		MessageQCopy_send(dstProc, remoteEndpoint, myEndpoint,
							(Ptr)buffer, len);
	}

	/* Teardown our side: */
	MessageQCopy_delete(&handle);

	/* Free MessageQCopy module wide resources: */
	MessageQCopy_finalize();
}
Esempio n. 23
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;
		
}
Esempio n. 24
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;

}
Esempio n. 25
0
/**
   Print information about the specified job
*/
static void builtin_jobs_print( job_t *j, int mode, int header )
{
	process_t *p;
	switch( mode )
	{
		case JOBS_DEFAULT:
		{

			if( header )
			{
				/*
				  Print table header before first job
				*/
				sb_append( sb_out, _( L"Job\tGroup\t" ));
#ifdef HAVE__PROC_SELF_STAT
				sb_append( sb_out, _( L"CPU\t" ) );
#endif
				sb_append( sb_out, _( L"State\tCommand\n" ) );
			}

			sb_printf( sb_out, L"%d\t%d\t", j->job_id, j->pgid );

#ifdef HAVE__PROC_SELF_STAT
			sb_printf( sb_out, L"%d%%\t", cpu_use(j) );
#endif
			sb_append( sb_out,
						job_is_stopped(j)?_(L"stopped"):_(L"running"),
						L"\t",
						j->command,
						L"\n",
						(void *)0 );
			break;
		}

		case JOBS_PRINT_GROUP:
		{
			if( header )
			{
				/*
				  Print table header before first job
				*/
				sb_append( sb_out, _( L"Group\n" ));
			}
			sb_printf( sb_out, L"%d\n", j->pgid );
			break;
		}

		case JOBS_PRINT_PID:
		{
			if( header )
			{
				/*
				  Print table header before first job
				*/
				sb_append( sb_out, _( L"Procces\n" ));
			}

			for( p=j->first_process; p; p=p->next )
			{
				sb_printf( sb_out, L"%d\n", p->pid );
			}
			break;
		}

		case JOBS_PRINT_COMMAND:
		{
			if( header )
			{
				/*
				  Print table header before first job
				*/
				sb_append( sb_out, _( L"Command\n" ));
			}

			for( p=j->first_process; p; p=p->next )
			{
				sb_printf( sb_out, L"%ls\n", p->argv[0] );
			}
			break;
		}
	}

}
Esempio n. 26
0
/**
   The jobs builtin. Used fopr printing running jobs. Defined in builtin_jobs.c.
*/
static int builtin_jobs( wchar_t **argv )
{
	int argc=0;
	int found=0;
	int mode=JOBS_DEFAULT;
	int print_last = 0;
	job_t *j;

	argc = builtin_count_args( argv );
	woptind=0;

	while( 1 )
	{
		const static struct woption
			long_options[] =
			{
				{
					L"pid", no_argument, 0, 'p'
				}
				,
				{
					L"command", no_argument, 0, 'c'
				}
				,
				{
					L"group", no_argument, 0, 'g'
				}
				,
				{
					L"last", no_argument, 0, 'l'
				}
				,
				{
					L"help", no_argument, 0, 'h'
				}
				,
				{
					0, 0, 0, 0
				}
			}
		;

		int opt_index = 0;

		int opt = wgetopt_long( argc,
								argv,
								L"pclgh",
								long_options,
								&opt_index );
		if( opt == -1 )
			break;

		switch( opt )
		{
			case 0:
				if(long_options[opt_index].flag != 0)
					break;
                sb_printf( sb_err,
                           BUILTIN_ERR_UNKNOWN,
                           argv[0],
                           long_options[opt_index].name );

				builtin_print_help( argv[0], sb_err );


				return 1;


			case 'p':
				mode=JOBS_PRINT_PID;
				break;

			case 'c':
				mode=JOBS_PRINT_COMMAND;
				break;

			case 'g':
				mode=JOBS_PRINT_GROUP;
				break;

			case 'l':
			{
				print_last = 1;
				break;
			}

			case 'h':
				builtin_print_help( argv[0], sb_out );
				return 0;				

			case '?':
				builtin_unknown_option( argv[0], argv[woptind-1] );
				return 1;

		}
	}


	/*
	  Do not babble if not interactive
	*/
	if( builtin_out_redirect )
	{
		found=1;
	}

	if( print_last )
	{
		/*
		  Ignore unconstructed jobs, i.e. ourself.
		*/
		for( j=first_job; j; j=j->next )
		{
			if( (j->flags & JOB_CONSTRUCTED) && !job_is_completed(j) )
			{
				builtin_jobs_print( j, mode, !found );
				return 0;
			}
		}

	}
	else
	{
		if( woptind < argc )
		{
			int i;

			found = 1;

			for( i=woptind; i<argc; i++ )
			{
				long pid;
				wchar_t *end;
				errno=0;
				pid=wcstol( argv[i], &end, 10 );
				if( errno || *end )
				{
					sb_printf( sb_err,
							   _( L"%ls: '%ls' is not a job\n" ),
							   argv[0],
							   argv[i] );
					return 1;
				}

				j = job_get_from_pid( pid );

				if( j && !job_is_completed( j ) )
				{
					builtin_jobs_print( j, mode, !found );
				}
				else
				{
					sb_printf( sb_err,
							   _( L"%ls: No suitable job: %d\n" ),
							   argv[0],
							   pid );
					return 1;
				}
			}
		}
		else
		{
			for( j= first_job; j; j=j->next )
			{
				/*
				  Ignore unconstructed jobs, i.e. ourself.
				*/
				if( (j->flags & JOB_CONSTRUCTED) && !job_is_completed(j) )
				{
					builtin_jobs_print( j, mode, !found );
					found = 1;
				}
			}
		}
	}

	if( !found )
	{
		sb_printf( sb_out,
				   _( L"%ls: There are no jobs\n" ),
				   argv[0] );
	}

	return 0;
}
Esempio n. 27
0
/**
   The complete builtin. Used for specifying programmable
   tab-completions. Calls the functions in complete.c for any heavy
   lifting. Defined in builtin_complete.c
*/
static int builtin_complete( wchar_t **argv )
{
	int res=0;
	int argc=0;
	int result_mode=SHARED;
	int remove = 0;
	int authoritative = -1;
	int flags = COMPLETE_AUTO_SPACE;
	
	string_buffer_t short_opt;
	array_list_t gnu_opt, old_opt;
	wchar_t *comp=L"", *desc=L"", *condition=L"";

	wchar_t *do_complete = 0;
	
	array_list_t cmd;
	array_list_t path;

	static int recursion_level=0;
	
	if( !is_interactive_session )
	{
		debug( 1, _(L"%ls: Command only available in interactive sessions"), argv[0] );
	}
	
	al_init( &cmd );
	al_init( &path );
	sb_init( &short_opt );
	al_init( &gnu_opt );
	al_init( &old_opt );
	
	argc = builtin_count_args( argv );	
	
	woptind=0;
	
	while( res == 0 )
	{
		const static struct woption
			long_options[] =
			{
				{
					L"exclusive", no_argument, 0, 'x' 
				}
				,
				{
					L"no-files", no_argument, 0, 'f' 
				}
				,
				{
					L"require-parameter", no_argument, 0, 'r' 
				}
				,
				{
					L"path", required_argument, 0, 'p'
				}
				,					
				{
					L"command", required_argument, 0, 'c' 
				}
				,					
				{
					L"short-option", required_argument, 0, 's' 
				}
				,
				{
					L"long-option", required_argument, 0, 'l'
				}
				,
				{
					L"old-option", required_argument, 0, 'o' 
				}
				,
				{
					L"description", required_argument, 0, 'd'
				}
				,
				{
					L"arguments", required_argument, 0, 'a'
				}
				,
				{
					L"erase", no_argument, 0, 'e'
				}
				,
				{
					L"unauthoritative", no_argument, 0, 'u'
				}
				,
				{
					L"authoritative", no_argument, 0, 'A'
				}
				,
				{
					L"condition", required_argument, 0, 'n'
				}
				,
				{
					L"do-complete", optional_argument, 0, 'C'
				}
				,
				{
					L"help", no_argument, 0, 'h'
				}
				,
				{ 
					0, 0, 0, 0 
				}
			}
		;		
		
		int opt_index = 0;
		
		int opt = wgetopt_long( argc,
								argv, 
								L"a:c:p:s:l:o:d:frxeuAn:C::h", 
								long_options, 
								&opt_index );
		if( opt == -1 )
			break;
			
		switch( opt )
		{
			case 0:
				if(long_options[opt_index].flag != 0)
					break;
                sb_printf( sb_err,
                           BUILTIN_ERR_UNKNOWN,
                           argv[0],
                           long_options[opt_index].name );
				builtin_print_help( argv[0], sb_err );

				
				res = 1;
				break;
				
			case 'x':					
				result_mode |= EXCLUSIVE;
				break;
					
			case 'f':					
				result_mode |= NO_FILES;
				break;
				
			case 'r':					
				result_mode |= NO_COMMON;
				break;
					
			case 'p':	
			case 'c':
			{
				wchar_t *a = unescape( woptarg, 1);
				if( a )
				{
					al_push( (opt=='p'?&path:&cmd), a );
				}
				else
				{
					sb_printf( sb_err, L"%ls: Invalid token '%ls'\n", argv[0], woptarg );
					res = 1;					
				}				
				break;
			}
				
			case 'd':
				desc = woptarg;
				break;
				
			case 'u':
				authoritative=0;
				break;
				
			case 'A':
				authoritative=1;
				break;
				
			case 's':
				sb_append( &short_opt, woptarg );
				break;
					
			case 'l':
				al_push( &gnu_opt, woptarg );
				break;
				
			case 'o':
				al_push( &old_opt, woptarg );
				break;

			case 'a':
				comp = woptarg;
				break;
				
			case 'e':
				remove = 1;
				break;

			case 'n':
				condition = woptarg;
				break;
				
			case 'C':
				do_complete = woptarg?woptarg:reader_get_buffer();
				break;
				
			case 'h':
				builtin_print_help( argv[0], sb_out );
				return 0;
				
			case '?':
				builtin_unknown_option( argv[0], argv[woptind-1] );
				res = 1;
				break;
				
		}
		
	}

	if( !res )
	{
		if( condition && wcslen( condition ) )
		{
			if( parser_test( condition, 0, 0, 0 ) )
			{
				sb_printf( sb_err,
						   L"%ls: Condition '%ls' contained a syntax error\n", 
						   argv[0],
						   condition );
				
				parser_test( condition, 0, sb_err, argv[0] );
				
				res = 1;
			}
		}
	}
	
	if( !res )
	{
		if( comp && wcslen( comp ) )
		{
			if( parser_test_args( comp, 0, 0 ) )
			{
				sb_printf( sb_err,
						   L"%ls: Completion '%ls' contained a syntax error\n", 
						   argv[0],
						   comp );
				
				parser_test_args( comp, sb_err, argv[0] );
				
				res = 1;
			}
		}
	}

	if( !res )
	{
		if( do_complete )
		{
			array_list_t *comp;
			int i;

			const wchar_t *prev_temporary_buffer = temporary_buffer;

			wchar_t *token;

			parse_util_token_extent( do_complete, wcslen( do_complete ), &token, 0, 0, 0 );
						
			temporary_buffer = do_complete;		

			if( recursion_level < 1 )
			{
				recursion_level++;
			
				comp = al_halloc( 0 );
			
				complete( do_complete, comp );
			
				for( i=0; i<al_get_count( comp ); i++ )
				{
					completion_t *next = (completion_t *)al_get( comp, i );
					wchar_t *prepend;
					
					if( next->flags & COMPLETE_NO_CASE )
					{
						prepend = L"";
					}
					else
					{
						prepend = token;
					}
						

					if( next->description )
					{
						sb_printf( sb_out, L"%ls%ls\t%ls\n", prepend, next->completion, next->description );
					}
					else
					{
						sb_printf( sb_out, L"%ls%ls\n", prepend, next->completion );
					}
				}
			
				halloc_free( comp );
				recursion_level--;
			}
		
			temporary_buffer = prev_temporary_buffer;		
		
		}
		else if( woptind != argc )
		{
			sb_printf( sb_err, 
					   _( L"%ls: Too many arguments\n" ),
					   argv[0] );
			builtin_print_help( argv[0], sb_err );

			res = 1;
		}
		else if( (al_get_count( &cmd) == 0 ) && (al_get_count( &path) == 0 ) )
		{
			/* No arguments specified, meaning we print the definitions of
			 * all specified completions to stdout.*/
			complete_print( sb_out );		
		}
		else
		{
			if( remove )
			{
				builtin_complete_remove( &cmd,
										 &path,
										 (wchar_t *)short_opt.buff,
										 &gnu_opt,
										 &old_opt );									 
			}
			else
			{
				builtin_complete_add( &cmd, 
									  &path,
									  (wchar_t *)short_opt.buff,
									  &gnu_opt,
									  &old_opt, 
									  result_mode, 
									  authoritative,
									  condition,
									  comp,
									  desc,
									  flags ); 
			}

		}	
	}
	
	al_foreach( &cmd, &free );
	al_foreach( &path, &free );

	al_destroy( &cmd );
	al_destroy( &path );
	sb_destroy( &short_opt );
	al_destroy( &gnu_opt );
	al_destroy( &old_opt );

	return res;
}
Esempio n. 28
0
void write_screen( const wchar_t *msg, string_buffer_t *buff )
{
	const wchar_t *start, *pos;
	int line_width = 0;
	int tok_width = 0;
	int screen_width = common_get_width();

	CHECK( msg, );
	CHECK( buff, );

	if( screen_width )
	{
		start = pos = msg;
		while( 1 )
		{
			int overflow = 0;

			tok_width=0;

			/*
			  Tokenize on whitespace, and also calculate the width of the token
			*/
			while( *pos && ( !wcschr( L" \n\r\t", *pos ) ) )
			{

				/*
				  Check is token is wider than one line.
				  If so we mark it as an overflow and break the token.
				*/
				if((tok_width + wcwidth(*pos)) > (screen_width-1))
				{
					overflow = 1;
					break;
				}

				tok_width += wcwidth( *pos );
				pos++;
			}

			/*
			  If token is zero character long, we don't do anything
			*/
			if( pos == start )
			{
				start = pos = pos+1;
			}
			else if( overflow )
			{
				/*
				  In case of overflow, we print a newline, except if we already are at position 0
				*/
				wchar_t *token = wcsndup( start, pos-start );
				if( line_width != 0 )
					sb_append_char( buff, L'\n' );
				sb_printf( buff, L"%ls-\n", token );
				free( token );
				line_width=0;
			}
			else
			{
				/*
				  Print the token
				*/
				wchar_t *token = wcsndup( start, pos-start );
				if( (line_width + (line_width!=0?1:0) + tok_width) > screen_width )
				{
					sb_append_char( buff, L'\n' );
					line_width=0;
				}
				sb_printf( buff, L"%ls%ls", line_width?L" ":L"", token );
				free( token );
				line_width += (line_width!=0?1:0) + tok_width;
			}

			/*
			  Break on end of string
			*/
			if( !*pos )
			{
				break;
			}

			start=pos;
		}
	}
	else
	{
		sb_printf( buff, L"%ls", msg );
	}
	sb_append_char( buff, L'\n' );
}
Esempio n. 29
0
/**
   The commandline builtin. It is used for specifying a new value for
   the commandline.
*/
static int builtin_commandline( wchar_t **argv )
{

	int buffer_part=0;
	int cut_at_cursor=0;

	int argc = builtin_count_args( argv );
	int append_mode=0;

	int function_mode = 0;

	int tokenize = 0;

	int cursor_mode = 0;
	int line_mode = 0;
	int search_mode = 0;
	wchar_t *begin, *end;

	current_buffer = (wchar_t *)builtin_complete_get_temporary_buffer();
	if( current_buffer )
	{
		current_cursor_pos = wcslen( current_buffer );
	}
	else
	{
		current_buffer = reader_get_buffer();
		current_cursor_pos = reader_get_cursor_pos();
	}

	if( !get_buffer() )
	{
		if (is_interactive_session)
		{
			/*
			  Prompt change requested while we don't have
			  a prompt, most probably while reading the
			  init files. Just ignore it.
			*/
			return 1;
		}

		sb_append( sb_err,
			    argv[0],
			    L": Can not set commandline in non-interactive mode\n",
			    (void *)0 );
		builtin_print_help( argv[0], sb_err );
		return 1;
	}

	woptind=0;

	while( 1 )
	{
		static const struct woption
			long_options[] =
			{
				{
					L"append", no_argument, 0, 'a'
				}
				,
				{
					L"insert", no_argument, 0, 'i'
				}
				,
				{
					L"replace", no_argument, 0, 'r'
				}
				,
				{
					L"current-job", no_argument, 0, 'j'
				}
				,
				{
					L"current-process", no_argument, 0, 'p'
				}
				,
				{
					L"current-token", no_argument, 0, 't'
				}
				,
				{
					L"current-buffer", no_argument, 0, 'b'
				}
				,
				{
					L"cut-at-cursor", no_argument, 0, 'c'
				}
				,
				{
					L"function", no_argument, 0, 'f'
				}
				,
				{
					L"tokenize", no_argument, 0, 'o'
				}
				,
				{
					L"help", no_argument, 0, 'h'
				}
				,
				{
					L"input", required_argument, 0, 'I'
				}
				,
				{
					L"cursor", no_argument, 0, 'C'
				}
				,
				{
					L"line", no_argument, 0, 'L'
				}
				,
				{
					L"search-mode", no_argument, 0, 'S'
				}
				,
				{
					0, 0, 0, 0
				}
			}
		;

		int opt_index = 0;

		int opt = wgetopt_long( argc,
								argv,
								L"abijpctwforhI:CLS",
								long_options,
								&opt_index );
		if( opt == -1 )
			break;

		switch( opt )
		{
			case 0:
				if(long_options[opt_index].flag != 0)
					break;
                sb_printf( sb_err,
                           BUILTIN_ERR_UNKNOWN,
                           argv[0],
                           long_options[opt_index].name );
				builtin_print_help( argv[0], sb_err );

				return 1;

			case L'a':
				append_mode = APPEND_MODE;
				break;

			case L'b':
				buffer_part = STRING_MODE;
				break;


			case L'i':
				append_mode = INSERT_MODE;
				break;

			case L'r':
				append_mode = REPLACE_MODE;
				break;

			case 'c':
				cut_at_cursor=1;
				break;

			case 't':
				buffer_part = TOKEN_MODE;
				break;

			case 'j':
				buffer_part = JOB_MODE;
				break;

			case 'p':
				buffer_part = PROCESS_MODE;
				break;

			case 'f':
				function_mode=1;
				break;

			case 'o':
				tokenize=1;
				break;

			case 'I':
				current_buffer = woptarg;
				current_cursor_pos = wcslen( woptarg );
				break;

			case 'C':
				cursor_mode = 1;
				break;

			case 'L':
				line_mode = 1;
				break;

			case 'S':
				search_mode = 1;
				break;

			case 'h':
				builtin_print_help( argv[0], sb_out );
				return 0;

			case L'?':
				builtin_unknown_option( argv[0], argv[woptind-1] );
				return 1;
		}
	}

	if( function_mode )
	{
		int i;

		/*
		  Check for invalid switch combinations
		*/
		if( buffer_part || cut_at_cursor || append_mode || tokenize || cursor_mode || line_mode || search_mode )
		{
			sb_printf(sb_err,
					  BUILTIN_ERR_COMBO,
					  argv[0] );

			builtin_print_help( argv[0], sb_err );
			return 1;
		}


		if( argc == woptind )
		{
			sb_printf( sb_err,
					   BUILTIN_ERR_MISSING,
					   argv[0] );

			builtin_print_help( argv[0], sb_err );
			return 1;
 		}
		for( i=woptind; i<argc; i++ )
		{
			wint_t c = input_function_get_code( argv[i] );
			if( c != -1 )
			{
				/*
				  input_unreadch inserts the specified keypress or
				  readline function at the top of the stack of unused
				  keypresses
				*/
				input_unreadch(c);
			}
			else
			{
				sb_printf( sb_err,
					   _(L"%ls: Unknown input function '%ls'\n"),
					   argv[0],
					   argv[i] );
				builtin_print_help( argv[0], sb_err );
				return 1;
			}
		}

		return 0;
	}

	/*
	  Check for invalid switch combinations
	*/
	if( (search_mode || line_mode || cursor_mode) && (argc-woptind > 1) )
	{

		sb_append( sb_err,
					argv[0],
					L": Too many arguments\n",
					(void *)0 );
		builtin_print_help( argv[0], sb_err );
		return 1;
	}

	if( (buffer_part || tokenize || cut_at_cursor) && (cursor_mode || line_mode || search_mode) )
	{
		sb_printf( sb_err,
				   BUILTIN_ERR_COMBO,
				   argv[0] );

		builtin_print_help( argv[0], sb_err );
		return 1;
	}


	if( (tokenize || cut_at_cursor) && (argc-woptind) )
	{
		sb_printf( sb_err,
				   BUILTIN_ERR_COMBO2,
				   argv[0],
				   L"--cut-at-cursor and --tokenize can not be used when setting the commandline" );


		builtin_print_help( argv[0], sb_err );
		return 1;
	}

	if( append_mode && !(argc-woptind) )
	{
		sb_printf( sb_err,
                    BUILTIN_ERR_COMBO2,
                    argv[0],
				   L"insertion mode switches can not be used when not in insertion mode" );

        builtin_print_help( argv[0], sb_err );
        return 1;
	}

	/*
	  Set default modes
	*/
	if( !append_mode )
	{
		append_mode = REPLACE_MODE;
	}

	if( !buffer_part )
	{
		buffer_part = STRING_MODE;
	}

	if( cursor_mode )
	{
		if( argc-woptind )
		{
			wchar_t *endptr;
			int new_pos;
			errno = 0;

			new_pos = wcstol( argv[woptind], &endptr, 10 );
			if( *endptr || errno )
			{
				sb_printf( sb_err,
					   BUILTIN_ERR_NOT_NUMBER,
					   argv[0],
					   argv[woptind] );
				builtin_print_help( argv[0], sb_err );
			}

			current_buffer = reader_get_buffer();
			new_pos = maxi( 0, mini( new_pos, wcslen( current_buffer ) ) );
			reader_set_buffer( current_buffer, new_pos );
			return 0;
		}
		else
		{
			sb_printf( sb_out, L"%d\n", reader_get_cursor_pos() );
			return 0;
		}

	}

	if( line_mode )
	{
		int pos = reader_get_cursor_pos();
		wchar_t *buff = reader_get_buffer();
		sb_printf( sb_out, L"%d\n", parse_util_lineno( buff, pos ) );
		return 0;

	}

	if( search_mode )
	{
		return !reader_search_mode();
	}


	switch( buffer_part )
	{
		case STRING_MODE:
		{
			begin = get_buffer();
			end = begin+wcslen(begin);
			break;
		}

		case PROCESS_MODE:
		{
			parse_util_process_extent( get_buffer(),
									   get_cursor_pos(),
									   &begin,
									   &end );
			break;
		}

		case JOB_MODE:
		{
			parse_util_job_extent( get_buffer(),
								   get_cursor_pos(),
								   &begin,
								   &end );
			break;
		}

		case TOKEN_MODE:
		{
			parse_util_token_extent( get_buffer(),
									 get_cursor_pos(),
									 &begin,
									 &end,
									 0, 0 );
			break;
		}

	}

	switch(argc-woptind)
	{
		case 0:
		{
			write_part( begin, end, cut_at_cursor, tokenize );
			break;
		}

		case 1:
		{
			replace_part( begin, end, argv[woptind], append_mode );
			break;
		}

		default:
		{
			string_buffer_t sb;
			int i;

			sb_init( &sb );

			sb_append( &sb, argv[woptind] );

			for( i=woptind+1; i<argc; i++ )
			{
				sb_append( &sb, L"\n" );
				sb_append( &sb, argv[i] );
			}

			replace_part( begin, end, (wchar_t *)sb.buff, append_mode );
			sb_destroy( &sb );

			break;
		}
	}

	return 0;
}