static wi_process_t * _wi_process_init_with_argv(wi_process_t *process, int argc, const char **argv) { wi_array_t *array; wi_string_t *string; struct utsname name; #if defined(HAVE_NXGETLOCALARCHINFO) const NXArchInfo *archinfo; cpu_type_t cputype; size_t cputypesize; #elif defined(HAVE_SYSINFO) && defined(SI_ARCHITECTURE) char buffer[SYS_NMLN]; #endif array = wi_array_init_with_argv(wi_array_alloc(), argc, argv); string = wi_array_first_data(array); if(string) { process->path = wi_retain(string); process->name = wi_retain(wi_string_last_path_component(process->path)); } else { process->path = wi_retain(WI_STR("unknown")); process->name = wi_retain(process->path); } if(wi_array_count(array) <= 1) process->arguments = wi_array_init(wi_array_alloc()); else process->arguments = wi_retain(wi_array_subarray_with_range(array, wi_make_range(1, wi_array_count(array) - 1))); wi_release(array); uname(&name); process->os_name = wi_string_init_with_cstring(wi_string_alloc(), name.sysname); process->os_release = wi_string_init_with_cstring(wi_string_alloc(), name.release); #if defined(HAVE_NXGETLOCALARCHINFO) cputypesize = sizeof(cputype); if(sysctlbyname("sysctl.proc_cputype", &cputype, &cputypesize, NULL, 0) < 0) cputype = NXGetLocalArchInfo()->cputype; archinfo = NXGetArchInfoFromCpuType(cputype, CPU_SUBTYPE_MULTIPLE); if(archinfo) process->arch = wi_string_init_with_cstring(wi_string_alloc(), archinfo->name); #elif defined(HAVE_SYSINFO) && defined(SI_ARCHITECTURE) if(sysinfo(SI_ARCHITECTURE, buffer, sizeof(buffer)) >= 0) process->arch = wi_string_init_with_cstring(wi_string_alloc(), buffer); #endif if(!process->arch) process->arch = wi_string_init_with_cstring(wi_string_alloc(), name.machine); return process; }
wi_array_t * wi_file_directory_contents_at_path(wi_string_t *path) { wi_array_t *contents; wi_string_t *name; DIR *dir; struct dirent de, *dep; dir = opendir(wi_string_cstring(path)); if(!dir) { wi_error_set_errno(errno); return NULL; } contents = wi_array_init_with_capacity(wi_array_alloc(), 100); while(readdir_r(dir, &de, &dep) == 0 && dep) { if(strcmp(dep->d_name, ".") != 0 && strcmp(dep->d_name, "..") != 0) { name = wi_string_init_with_cstring(wi_string_alloc(), dep->d_name); wi_array_add_data(contents, name); wi_release(name); } } closedir(dir); return wi_autorelease(contents); }
wi_terminal_t * wi_terminal_init_with_type(wi_terminal_t *terminal, wi_string_t *type) { char *env; int err, co, li; err = tgetent(NULL, wi_string_cstring(type)); if(err <= 0) { if(err == 0) wi_error_set_lib_error(WI_ERROR_TERMCAP_NOSUCHENTRY); else wi_error_set_lib_error(WI_ERROR_TERMCAP_TERMINFONOTFOUND); wi_release(terminal); return NULL; } env = getenv("COLUMNS"); co = env ? strtol(env, NULL, 10) : tgetnum("co"); terminal->co = (co <= 0) ? 80 : co; env = getenv("LINES"); li = env ? strtol(env, NULL, 10) : tgetnum("li"); terminal->li = (li <= 0) ? 24 : li; terminal->ce = (char *) tgetstr("ce", NULL); terminal->cl = (char *) tgetstr("cl", NULL); terminal->cm = (char *) tgetstr("cm", NULL); terminal->cs = (char *) tgetstr("cs", NULL); terminal->scroll = wi_make_range(0, terminal->li); terminal->buffers = wi_array_init_with_capacity(wi_array_alloc(), 10); return terminal; }
wi_terminal_buffer_t * wi_terminal_buffer_init_with_terminal(wi_terminal_buffer_t *buffer, wi_terminal_t *terminal) { buffer->terminal = terminal; buffer->textbuffer = wi_string_init_with_capacity(wi_string_alloc(), _WI_TERMINAL_TEXTBUFFER_CAPACITY); buffer->linebuffer = wi_array_init_with_capacity(wi_array_alloc(), _WI_TERMINAL_LINEBUFFER_CAPACITY); return buffer; }
static wi_array_t * _wi_terminal_buffer_lines_for_string(wi_terminal_buffer_t *buffer, wi_string_t *string) { wi_array_t *array, *linearray; wi_string_t *string_copy, *line, *subline; wi_size_t size; uint32_t i, count, index; array = wi_array_init(wi_array_alloc()); size = wi_terminal_size(buffer->terminal); linearray = wi_string_components_separated_by_string(string, WI_STR("\n")); count = wi_array_count(linearray); for(i = 0; i < count; i++) { line = WI_ARRAY(linearray, i); if(wi_terminal_width_of_string(buffer->terminal, line) < size.width) { wi_array_add_data(array, line); } else { string_copy = wi_copy(line); do { index = wi_terminal_index_of_string_for_width(buffer->terminal, string_copy, size.width); subline = wi_string_substring_to_index(string_copy, index); wi_array_add_data(array, subline); wi_string_delete_characters_to_index(string_copy, wi_string_length(subline)); } while(wi_terminal_width_of_string(buffer->terminal, string_copy) >= size.width); wi_array_add_data(array, string_copy); wi_release(string_copy); } } return array; }
static wi_array_t * _wi_terminal_buffer_lines_for_string(wi_terminal_buffer_t *buffer, wi_string_t *string) { wi_enumerator_t *enumerator; wi_array_t *array; wi_string_t *string_copy, *line, *subline; wi_size_t size; wi_uinteger_t index; array = wi_array_init(wi_array_alloc()); size = wi_terminal_size(buffer->terminal); enumerator = wi_array_data_enumerator(wi_string_components_separated_by_string(string, WI_STR("\n"))); while((line = wi_enumerator_next_data(enumerator))) { if(wi_terminal_width_of_string(buffer->terminal, line) < size.width) { wi_array_add_data(array, line); } else { string_copy = wi_copy(line); do { index = wi_terminal_index_of_string_for_width(buffer->terminal, string_copy, size.width); subline = wi_string_substring_to_index(string_copy, index); wi_array_add_data(array, subline); wi_string_delete_characters_to_index(string_copy, wi_string_length(subline)); } while(wi_terminal_width_of_string(buffer->terminal, string_copy) >= size.width); wi_array_add_data(array, string_copy); wi_release(string_copy); } } return wi_autorelease(array); }
void wr_commands_parse_command(wi_string_t *buffer, wi_boolean_t chat) { wi_array_t *array = NULL; wi_string_t *command = NULL, *arguments; wi_uinteger_t index; if(chat && !wi_string_has_prefix(buffer, WI_STR("/"))) buffer = wi_string_by_inserting_string_at_index(buffer, wr_connected ? WI_STR("say ") : WI_STR("echo "), 0); wr_commands_split_command(buffer, &command, &arguments); index = wr_commands_index_for_command(command); if(index == WI_NOT_FOUND) { wi_log_error(WI_STR("%@: Command not recognized"), command); return; } if(wr_commands[index].connected && !wr_connected) { wi_log_error(WI_STR("%@: %@"), command, WI_STR("Not connected")); return; } array = wi_autorelease(wi_array_init_with_argument_string(wi_array_alloc(), arguments, wr_commands[index].optindex)); if(wi_array_count(array) < wr_commands[index].optargs) { wr_commands_print_usage_for_command(command); return; } ((*wr_commands[index].action) (array)); }
void wi_test_string_paths(void) { WI_TEST_ASSERT_EQUAL_INSTANCES( wi_string_path_components(WI_STR("/usr/local/wired")), wi_autorelease(wi_array_init_with_data(wi_array_alloc(), WI_STR("/"), WI_STR("usr"), WI_STR("local"), WI_STR("wired"), NULL)), ""); WI_TEST_ASSERT_EQUAL_INSTANCES( wi_string_by_normalizing_path(WI_STR("////usr/././local/../local/../local/wired///")), WI_STR("/usr/local/wired"), ""); WI_TEST_ASSERT_EQUAL_INSTANCES( wi_string_by_appending_path_component(WI_STR("/usr/local/"), WI_STR("/wired")), WI_STR("/usr/local/wired"), ""); WI_TEST_ASSERT_EQUAL_INSTANCES(wi_string_last_path_component(WI_STR("/usr/local/wired/")), WI_STR("wired"), ""); WI_TEST_ASSERT_EQUAL_INSTANCES(wi_string_last_path_component(WI_STR("/")), WI_STR("/"), ""); WI_TEST_ASSERT_EQUAL_INSTANCES(wi_string_last_path_component(WI_STR("/wired/")), WI_STR("wired"), ""); WI_TEST_ASSERT_EQUAL_INSTANCES(wi_string_by_deleting_last_path_component(WI_STR("/usr/local/wired")), WI_STR("/usr/local"), ""); WI_TEST_ASSERT_EQUAL_INSTANCES(wi_string_path_extension(WI_STR("wired.c")), WI_STR("c"), ""); WI_TEST_ASSERT_EQUAL_INSTANCES(wi_string_path_extension(WI_STR("wired")), WI_STR(""), ""); WI_TEST_ASSERT_EQUAL_INSTANCES(wi_string_by_deleting_path_extension(WI_STR("wired")), WI_STR("wired"), ""); }
static wi_process_t * _wi_process_init_with_argv(wi_process_t *process, int argc, const char **argv) { wi_array_t *array; wi_string_t *string; struct utsname name; #ifdef HAVE_MACH_O_ARCH_H const NXArchInfo *arch_info; #endif array = wi_array_init_with_argv(wi_array_alloc(), argc, argv); string = wi_array_first_data(array); if(string) { process->path = wi_retain(string); process->name = wi_retain(wi_string_last_path_component(process->path)); } else { process->path = wi_retain(WI_STR("unknown")); process->name = wi_retain(process->path); } if(wi_array_count(array) <= 1) process->arguments = wi_array_init(wi_array_alloc()); else process->arguments = wi_retain(wi_array_subarray_with_range(array, wi_make_range(1, wi_array_count(array) - 1))); wi_release(array); uname(&name); process->os_name = wi_string_init_with_cstring(wi_string_alloc(), name.sysname); process->os_release = wi_string_init_with_cstring(wi_string_alloc(), name.release); #ifdef HAVE_MACH_O_ARCH_H arch_info = NXGetArchInfoFromCpuType(NXGetLocalArchInfo()->cputype, CPU_SUBTYPE_MULTIPLE); process->arch = wi_string_init_with_cstring(wi_string_alloc(), arch_info->name); #else process->arch = wi_string_init_with_cstring(wi_string_alloc(), name.machine); #endif return process; }
static wi_runtime_instance_t * _wi_array_copy(wi_runtime_instance_t *instance) { wi_array_t *array = instance, *array_copy; wi_uinteger_t i; array_copy = wi_array_init_with_capacity_and_callbacks(wi_array_alloc(), array->data_count, array->callbacks); for(i = 0; i < array->data_count; i++) wi_array_add_data(array_copy, array->items[i]->data); return array_copy; }
void wr_windows_init(void) { wr_window_runtime_id = wi_runtime_register_class(&wr_window_runtime_class); wr_windows = wi_array_init(wi_array_alloc()); wr_console_window = wr_window_init_with_chat(wr_window_alloc(), wr_public_chat); wr_windows_add_window(wr_console_window); wr_windows_show_window(wr_console_window); wr_windows_set_timestamp_format(WI_STR("%H:%M")); }
wi_array_t * wi_array_subarray_with_range(wi_array_t *array, wi_range_t range) { wi_array_t *newarray; wi_uinteger_t i; _WI_ARRAY_INDEX_ASSERT(array, range.location); _WI_ARRAY_INDEX_ASSERT(array, range.location + range.length - 1); newarray = wi_array_init_with_capacity(wi_array_alloc(), range.length); for(i = range.location; i < range.location + range.length; i++) wi_array_add_data(newarray, WI_ARRAY(array, i)); return wi_autorelease(newarray); }
wi_array_t * wi_dictionary_keys_sorted_by_value(wi_dictionary_t *dictionary, wi_compare_func_t *compare) { wi_mutable_array_t *array, *buckets; _wi_dictionary_bucket_t *bucket; wi_array_callbacks_t callbacks; void **data; wi_uinteger_t i; if(dictionary->key_count == 0) return wi_autorelease(wi_array_init(wi_array_alloc())); callbacks.retain = NULL; callbacks.release = NULL; callbacks.is_equal = NULL; callbacks.description = NULL; buckets = wi_array_init_with_capacity_and_callbacks(wi_mutable_array_alloc(), dictionary->key_count, callbacks); for(i = 0; i < dictionary->buckets_count; i++) { for(bucket = dictionary->buckets[i]; bucket; bucket = bucket->next) wi_mutable_array_add_data(buckets, bucket); } data = wi_malloc(sizeof(void *) * dictionary->key_count); wi_array_get_data(buckets, data); #ifdef _WI_DICTIONARY_USE_QSORT_R qsort_r(data, dictionary->key_count, sizeof(void *), compare, _wi_dictionary_compare_buckets); #else wi_lock_lock(_wi_dictionary_sort_lock); _wi_dictionary_sort_function = compare; qsort(data, dictionary->key_count, sizeof(void *), _wi_dictionary_compare_buckets); wi_lock_unlock(_wi_dictionary_sort_lock); #endif callbacks.retain = dictionary->key_callbacks.retain; callbacks.release = dictionary->key_callbacks.release; callbacks.is_equal = dictionary->key_callbacks.is_equal; callbacks.description = dictionary->key_callbacks.description; array = wi_array_init_with_capacity_and_callbacks(wi_mutable_array_alloc(), dictionary->key_count, callbacks); for(i = 0; i < dictionary->key_count; i++) wi_mutable_array_add_data(array, ((_wi_dictionary_bucket_t *) data[i])->key); wi_free(data); wi_release(buckets); wi_runtime_make_immutable(array); return wi_autorelease(array); }
wi_array_t * wi_array_with_data(void *data0, ...) { wi_array_t *array; void *data; va_list ap; array = wi_array_init_with_capacity(wi_array_alloc(), 0); wi_array_add_data(array, data0); va_start(ap, data0); while((data = va_arg(ap, void *))) wi_array_add_data(array, data); va_end(ap); return wi_autorelease(array); }
void wd_trackers_init(void) { wd_tracker_runtime_id = wi_runtime_register_class(&wd_tracker_runtime_class); wd_trackers = wi_array_init(wi_array_alloc()); wd_trackers_register_timer = wi_timer_init_with_function(wi_timer_alloc(), wd_trackers_register_with_timer, WD_TRACKERS_REGISTER_INTERVAL, true); wd_trackers_update_timer = wi_timer_init_with_function(wi_timer_alloc(), wd_trackers_update_with_timer, WD_TRACKERS_UPDATE_INTERVAL, true); }
wi_array_t * wi_set_all_data(wi_set_t *set) { wi_array_t *array; _wi_set_bucket_t *bucket; wi_array_callbacks_t callbacks; wi_uinteger_t i; callbacks.retain = set->callbacks.retain; callbacks.release = set->callbacks.release; callbacks.is_equal = set->callbacks.is_equal; callbacks.description = set->callbacks.description; array = wi_array_init_with_capacity_and_callbacks(wi_array_alloc(), set->data_count, callbacks); for(i = 0; i < set->buckets_count; i++) { for(bucket = set->buckets[i]; bucket; bucket = bucket->next) wi_array_add_data(array, bucket->data); } return wi_autorelease(array); }
static wd_user_t * wd_user_init_with_socket(wd_user_t *user, wi_socket_t *socket) { wi_address_t *address; user->uid = wd_user_next_uid(); user->socket = wi_retain(socket); user->state = WD_USER_CONNECTED; user->login_time = wi_time_interval(); user->idle_time = user->login_time; address = wi_socket_address(socket); user->ip = wi_retain(wi_address_string(address)); user->host = wi_retain(wi_address_hostname(address)); user->user_lock = wi_recursive_lock_init(wi_recursive_lock_alloc()); user->socket_lock = wi_lock_init(wi_lock_alloc()); user->transfers_queue = wi_array_init(wi_array_alloc()); return user; }
void wi_socket_initialize(void) { #ifdef HAVE_OPENSSL_SSL_H #ifdef WI_PTHREADS wi_lock_t *lock; wi_uinteger_t i, count; #endif SSL_library_init(); #ifdef WI_PTHREADS count = CRYPTO_num_locks(); _wi_socket_ssl_locks = wi_array_init_with_capacity(wi_array_alloc(), count); for(i = 0; i < count; i++) { lock = wi_lock_init(wi_lock_alloc()); wi_array_add_data(_wi_socket_ssl_locks, lock); wi_release(lock); } CRYPTO_set_id_callback(_wi_socket_ssl_id_function); CRYPTO_set_locking_callback(_wi_socket_ssl_locking_function); #endif #endif }
void wd_server_init(void) { wi_enumerator_t *enumerator; wi_array_t *array, *addresses; wi_address_t *address; wi_socket_t *control_socket, *transfer_socket; wi_string_t *ip, *string; wi_address_family_t family; wd_control_sockets = wi_array_init(wi_array_alloc()); wd_transfer_sockets = wi_array_init(wi_array_alloc()); addresses = wi_array_init(wi_array_alloc()); if(wi_array_count(wd_settings.address) > 0) { /* listen on configured addresses */ wi_array_rdlock(wd_settings.address); enumerator = wi_array_data_enumerator(wd_settings.address); while((string = wi_enumerator_next_data(enumerator))) { array = wi_host_addresses(wi_host_with_string(string)); if(array) wi_array_add_data_from_array(addresses, array); else wi_log_err(WI_STR("Could not resolve \"%@\": %m"), string); } wi_array_unlock(wd_settings.address); } else { /* add wildcard addresses */ wi_array_add_data(addresses, wi_address_wildcard_for_family(WI_ADDRESS_IPV4)); wi_array_add_data(addresses, wi_address_wildcard_for_family(WI_ADDRESS_IPV6)); } enumerator = wi_array_data_enumerator(addresses); while((address = wi_enumerator_next_data(enumerator))) { ip = wi_address_string(address); family = wi_address_family(address); /* force address family? */ if(wd_address_family != WI_ADDRESS_NULL && family != wd_address_family) continue; /* create sockets */ wi_address_set_port(address, wd_settings.port); control_socket = wi_autorelease(wi_socket_init_with_address(wi_socket_alloc(), address, WI_SOCKET_TCP)); wi_address_set_port(address, wd_settings.port + 1); transfer_socket = wi_autorelease(wi_socket_init_with_address(wi_socket_alloc(), address, WI_SOCKET_TCP)); if(!control_socket || !transfer_socket) { wi_log_warn(WI_STR("Could not create socket for %@: %m"), ip); continue; } /* listen on sockets */ if(!wi_socket_listen(control_socket, 5)) { wi_log_warn(WI_STR("Could not listen on %@ port %u: %m"), ip, wi_address_port(wi_socket_address(control_socket))); continue; } if(!wi_socket_listen(transfer_socket, 5)) { wi_log_warn(WI_STR("Could not listen on %@ port %u: %m"), ip, wi_address_port(wi_socket_address(transfer_socket))); continue; } wi_socket_set_interactive(control_socket, true); wi_socket_set_interactive(transfer_socket, false); /* add to list of sockets */ wi_array_add_data(wd_control_sockets, control_socket); wi_array_add_data(wd_transfer_sockets, transfer_socket); wi_log_info(WI_STR("Listening on %@ ports %d-%d"), ip, wd_settings.port, wd_settings.port + 1); } if(wi_array_count(wd_control_sockets) == 0 || wi_array_count(wd_transfer_sockets) == 0) wi_log_err(WI_STR("No addresses available for listening")); wi_release(addresses); #ifdef HAVE_DNS_SD_H wd_server_register_dnssd(); #endif }
wi_array_t * wi_array_with_plist_file(wi_string_t *path) { return wi_autorelease(wi_array_init_with_plist_file(wi_array_alloc(), path)); }
static wd_chat_t * wd_chat_init(wd_chat_t *chat) { chat->users = wi_array_init(wi_array_alloc()); chat->lock = wi_recursive_lock_init(wi_recursive_lock_alloc()); return chat; }
wr_chat_t * wr_chat_init(wr_chat_t *chat) { chat->users_array = wi_array_init(wi_array_alloc()); chat->users_hash = wi_hash_init(wi_hash_alloc()); return chat; }
wi_array_t * wi_array(void) { return wi_autorelease(wi_array_init(wi_array_alloc())); }
static wi_boolean_t _wi_plist_read_node_to_instance(xmlNodePtr content_node, wi_runtime_instance_t *collection) { xmlNodePtr node; wi_string_t *name, *key = NULL; wi_runtime_instance_t *instance = NULL; wi_boolean_t dictionary; dictionary = (wi_runtime_id(collection) == wi_dictionary_runtime_id()); for(node = content_node->children; node != NULL; node = node->next) { if(node->type == XML_ELEMENT_NODE) { name = wi_xml_node_name(node); if(wi_is_equal(name, WI_STR("key"))) key = wi_xml_node_content(node); else if(wi_is_equal(name, WI_STR("string"))) instance = wi_xml_node_content(node); else if(wi_is_equal(name, WI_STR("integer"))) instance = wi_number_with_integer(wi_string_integer(wi_xml_node_content(node))); else if(wi_is_equal(name, WI_STR("real"))) instance = wi_number_with_double(wi_string_double(wi_xml_node_content(node))); else if(wi_is_equal(name, WI_STR("true"))) instance = wi_number_with_bool(true); else if(wi_is_equal(name, WI_STR("false"))) instance = wi_number_with_bool(false); else if(wi_is_equal(name, WI_STR("date"))) instance = wi_date_with_rfc3339_string(wi_xml_node_content(node)); else if(wi_is_equal(name, WI_STR("data"))) instance = wi_data_with_base64(wi_xml_node_content(node)); else if(wi_is_equal(name, WI_STR("dict"))) { instance = wi_autorelease(wi_dictionary_init(wi_dictionary_alloc())); if(!_wi_plist_read_node_to_instance(node, instance)) return false; } else if(wi_is_equal(name, WI_STR("array"))) { instance = wi_autorelease(wi_array_init(wi_array_alloc())); if(!_wi_plist_read_node_to_instance(node, instance)) return false; } else { wi_error_set_libwired_error_with_format(WI_ERROR_PLIST_READFAILED, WI_STR("Unhandled node \"%@\""), name); return false; } } if(instance) { if(dictionary) wi_dictionary_set_data_for_key(collection, instance, key); else wi_array_add_data(collection, instance); instance = NULL; key = NULL; } } return true; }
static void _wi_settings_clear_string_array(wi_settings_t *settings, wi_uinteger_t index) { wi_array_t **array = (wi_array_t **) settings->spec[index].setting; wi_release(*array); *array = wi_array_init(wi_array_alloc()); }