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; }
static wi_boolean_t _wi_file_copy_directory(wi_string_t *frompath, wi_string_t *topath) { WI_FTS *fts; WI_FTSENT *p; wi_string_t *path, *newpath; char *paths[2]; uint32_t pathlength; wi_boolean_t result = true; paths[0] = (char *) wi_string_cstring(frompath);; paths[1] = NULL; fts = wi_fts_open(paths, WI_FTS_LOGICAL | WI_FTS_NOSTAT, NULL); if(!fts) return false; pathlength = wi_string_length(frompath); while((p = wi_fts_read(fts))) { path = wi_string_init_with_cstring(wi_string_alloc(), p->fts_path); newpath = wi_string_init_with_cstring(wi_string_alloc(), p->fts_path + pathlength); wi_string_insert_string_at_index(newpath, topath, 0); switch(p->fts_info) { case WI_FTS_NS: case WI_FTS_ERR: case WI_FTS_DNR: errno = p->fts_errno; result = false; break; case WI_FTS_DC: case WI_FTS_DP: break; case WI_FTS_D: if(!wi_file_create_directory(newpath, 0777)) result = false; break; default: if(!_wi_file_copy_file(path, newpath)) result = false; break; } wi_release(newpath); wi_release(path); } wi_fts_close(fts); return result; }
wi_string_t * wi_socket_certificate_hostname(wi_socket_t *socket) { #ifdef HAVE_OPENSSL_SSL_H X509 *x509; wi_string_t *string; char hostname[MAXHOSTNAMELEN]; x509 = SSL_get_peer_certificate(socket->ssl); if(!x509) return NULL; X509_NAME_get_text_by_NID(X509_get_subject_name(x509), NID_commonName, hostname, sizeof(hostname)); string = wi_string_init_with_cstring(wi_string_alloc(), hostname); X509_free(x509); return wi_autorelease(string); #else return NULL; #endif }
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; }
wi_socket_t * wi_socket_init_with_address(wi_socket_t *_socket, wi_address_t *address, wi_socket_type_t type) { _socket->address = wi_copy(address); _socket->close = true; _socket->buffer = wi_string_init_with_capacity(wi_string_alloc(), WI_SOCKET_BUFFER_SIZE); _socket->type = type; _socket->sd = socket(wi_address_family(_socket->address), _socket->type, 0); if(_socket->sd < 0) { wi_error_set_errno(errno); wi_release(_socket); return NULL; } if(!_wi_socket_set_option_int(_socket, SOL_SOCKET, SO_REUSEADDR, 1)) { wi_release(_socket); return NULL; } #ifdef SO_REUSEPORT if(!_wi_socket_set_option_int(_socket, SOL_SOCKET, SO_REUSEPORT, 1)) { wi_release(_socket); return NULL; } #endif return _socket; }
wi_string_t * wi_file_read_to_string(wi_file_t *file, wi_string_t *separator) { wi_string_t *string, *totalstring = NULL; wi_uinteger_t index, length; _WI_FILE_ASSERT_OPEN(file); while((string = wi_file_read(file, WI_FILE_BUFFER_SIZE))) { if(!totalstring) totalstring = wi_string_init(wi_string_alloc()); index = wi_string_index_of_string(string, separator, 0); if(index == WI_NOT_FOUND) { wi_string_append_string(totalstring, string); } else { length = wi_string_length(string); wi_string_delete_characters_from_index(string, index); wi_string_append_string(totalstring, string); wi_file_seek(file, wi_file_offset(file) - length + index + 1); break; } } return wi_autorelease(totalstring); }
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); }
void wd_broadcast(wd_chat_t *chat, uint32_t n, wi_string_t *fmt, ...) { wi_enumerator_t *enumerator; wi_string_t *string; wi_array_t *users; wd_user_t *user; va_list ap; va_start(ap, fmt); string = wi_string_init_with_format_and_arguments(wi_string_alloc(), fmt, ap); va_end(ap); users = wd_chat_users(chat); wi_array_rdlock(users); enumerator = wi_array_data_enumerator(users); while((user = wi_enumerator_next_data(enumerator))) { if(wd_user_state(user) == WD_USER_LOGGED_IN) { wd_user_lock_socket(user); wi_socket_write_format(wd_user_socket(user), 0.0, WI_STR("%u %@%c"), n, string, WD_MESSAGE_SEPARATOR); wd_user_unlock_socket(user); } } wi_array_unlock(users); wi_release(string); }
wi_boolean_t _wi_settings_set_path(wi_settings_t *settings, uint32_t index, wi_string_t *name, wi_string_t *value) { wi_string_t **string = (wi_string_t **) settings->spec[index].setting; if(*string) wi_release(*string); if(wi_string_has_prefix(value, WI_STR("/"))) *string = wi_retain(value); else if(settings->chroot) *string = wi_string_init_with_format(wi_string_alloc(), WI_STR("/%@"), value); else *string = wi_string_init_with_format(wi_string_alloc(), WI_STR("%@/%@"), wi_root_path, value); wi_string_normalize_path(*string); return true; }
static void _wi_log_vlog(int priority, wi_string_t *fmt, va_list ap) { wi_string_t *string; FILE *fp; const char *cstring, *name, *path; char date[_WI_LOG_DATE_SIZE]; string = wi_string_init_with_format_and_arguments(wi_string_alloc(), fmt, ap); cstring = wi_string_cstring(string); wi_lock_lock(_wi_log_lock); name = wi_string_cstring(wi_process_name(wi_process())); _wi_log_date(date); if(wi_log_stdout || wi_log_stderr) fprintf(wi_log_stdout ? stdout : stderr, "%s %s[%d]: %s\n", date, name, getpid(), cstring); else if(wi_log_startup && priority < LOG_INFO) fprintf(stderr, "%s: %s\n", name, cstring); else if(wi_log_tool) fprintf((priority < LOG_INFO) ? stderr : stdout, "%s: %s\n", name, cstring); if(wi_log_syslog) syslog(priority, "%s", cstring); if(wi_log_file && wi_log_path) { path = wi_string_cstring(wi_full_path(wi_log_path)); fp = fopen(path, "a"); if(fp) { fprintf(fp, "%s %s[%d]: %s\n", date, name, getpid(), cstring); fclose(fp); if(_wi_log_lines > 0 && wi_log_limit > 0) { if(_wi_log_lines % (int) ((float) wi_log_limit / 10.0f) == 0) { _wi_log_truncate(path); _wi_log_lines = wi_log_limit; } } _wi_log_lines++; } else { fprintf(stderr, "%s: %s: %s\n", name, path, strerror(errno)); } } if(wi_log_callback) (*wi_log_callback)(string); if(wi_log_startup && priority == LOG_ERR) exit(1); wi_lock_unlock(_wi_log_lock); wi_release(string); }
wi_string_t * wi_socket_certificate_name(wi_socket_t *socket) { #ifdef HAVE_OPENSSL_SSL_H X509 *x509 = NULL; EVP_PKEY *pkey = NULL; wi_string_t *string = NULL; x509 = SSL_get_peer_certificate(socket->ssl); if(!x509) goto end; pkey = X509_get_pubkey(x509); if(!pkey) goto end; switch(EVP_PKEY_type(pkey->type)) { case EVP_PKEY_RSA: string = wi_string_init_with_cstring(wi_string_alloc(), "RSA"); break; case EVP_PKEY_DSA: string = wi_string_init_with_cstring(wi_string_alloc(), "DSA"); break; case EVP_PKEY_DH: string = wi_string_init_with_cstring(wi_string_alloc(), "DH"); break; default: break; } end: if(x509) X509_free(x509); if(pkey) EVP_PKEY_free(pkey); return wi_autorelease(string); #else return NULL; #endif }
wi_string_t * wi_string_with_format(wi_string_t *fmt, ...) { wi_string_t *string; va_list ap; va_start(ap, fmt); string = wi_string_init_with_format_and_arguments(wi_string_alloc(), fmt, ap); va_end(ap); return wi_autorelease(string); }
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; }
void wi_test_initialize(void) { char *env; env = getenv("wi_test_name"); if(env) { _wi_tests_name = wi_string_init_with_utf8_string(wi_string_alloc(), env); printf("*** wi_test_initialize(): wi_test_name = %s\n", env); } }
void wd_sreply(wi_socket_t *socket, uint32_t n, wi_string_t *fmt, ...) { wi_string_t *string; va_list ap; va_start(ap, fmt); string = wi_string_init_with_format_and_arguments(wi_string_alloc(), fmt, ap); va_end(ap); wi_socket_write_format(socket, 0.0, WI_STR("%u %@%c"), n, string, WD_MESSAGE_SEPARATOR); wi_release(string); }
wi_string_t * wi_file_read_to_end_of_file(wi_file_t *file) { wi_string_t *string; char buffer[WI_FILE_BUFFER_SIZE]; wi_integer_t bytes; string = wi_string_init(wi_string_alloc()); while((bytes = wi_file_read_buffer(file, buffer, sizeof(buffer)))) wi_string_append_bytes(string, buffer, bytes); return wi_autorelease(string); }
void wd_version_init(void) { wd_version_string = wi_string_init_with_format(wi_string_alloc(), WI_STR("%s (%u)"), WD_VERSION, WI_REVISION); wd_protocol_version_string = wi_string_init_with_cstring(wi_string_alloc(), WD_PROTOCOL_VERSION); #ifdef HAVE_CORESERVICES_CORESERVICES_H wd_server_version_string = wi_string_init_with_format(wi_string_alloc(), WI_STR("Wired/%@ (%@; %@; %@) (%s; CoreFoundation %.1f)"), wd_version_string, wi_process_os_name(wi_process()), wi_process_os_release(wi_process()), wi_process_os_arch(wi_process()), SSLeay_version(SSLEAY_VERSION), kCFCoreFoundationVersionNumber); #else wd_server_version_string = wi_string_init_with_format(wi_string_alloc(), WI_STR("Wired/%@ (%@; %@; %@) (%s)"), wd_version_string, wi_process_os_name(wi_process()), wi_process_os_release(wi_process()), wi_process_os_arch(wi_process()), SSLeay_version(SSLEAY_VERSION)); #endif }
void wi_terminal_printf(wi_terminal_t *terminal, wi_string_t *fmt, ...) { wi_string_t *string; va_list ap; va_start(ap, fmt); string = wi_string_init_with_format_and_arguments(wi_string_alloc(), fmt, ap); va_end(ap); _wi_terminal_puts(terminal, string); wi_release(string); }
void wr_printf_block(wi_string_t *fmt, ...) { wi_string_t *string; va_list ap; va_start(ap, fmt); string = wi_string_init_with_format_and_arguments(wi_string_alloc(), fmt, ap); va_end(ap); wr_wprintf_block(wr_current_window, WI_STR("%@"), string); wi_release(string); }
void wr_wprintf(wr_window_t *window, wi_string_t *fmt, ...) { wi_string_t *string; va_list ap; va_start(ap, fmt); string = wi_string_init_with_format_and_arguments(wi_string_alloc(), fmt, ap); va_end(ap); wr_wprint(window, string); wi_release(string); }
void wi_error_set_regex_error(regex_t *regex, int code) { wi_error_t *error; char string[256]; error = _wi_error_get_error(); error->domain = WI_ERROR_DOMAIN_REGEX; error->code = code; regerror(code, regex, string, sizeof(string)); wi_release(error->string); error->string = wi_string_init_with_cstring(wi_string_alloc(), string); }
static void wi_assert_default_handler(const char *file, unsigned int line, wi_string_t *fmt, ...) { wi_string_t *string; va_list ap; va_start(ap, fmt); string = wi_string_init_with_format_and_arguments(wi_string_alloc(), fmt, ap); va_end(ap); wi_log_warn(WI_STR("Assertion failed at %s:%u: %@"), file, line, string); wi_release(string); wi_crash(); }
wi_array_t * wi_array_init_with_argv(wi_array_t *array, int argc, const char **argv) { wi_string_t *string; int i; array = wi_array_init_with_capacity(array, argc); for(i = 0; i < argc; i++) { string = wi_string_init_with_cstring(wi_string_alloc(), argv[i]); wi_array_add_data(array, string); wi_release(string); } return array; }
void wi_error_set_libxml2_error(void) { wi_error_t *error; xmlErrorPtr xml_error; xml_error = xmlGetLastError(); error = _wi_get_error(); error->domain = WI_ERROR_DOMAIN_REGEX; error->code = xml_error->code; wi_release(error->string); error->string = wi_string_init_with_cstring(wi_string_alloc(), xml_error->message); wi_string_delete_surrounding_whitespace(error->string); }
int32_t wi_socket_sendto(wi_socket_t *socket, wi_socket_context_t *context, wi_string_t *fmt, ...) { wi_string_t *string; int bytes; va_list ap; va_start(ap, fmt); string = wi_string_init_with_format_and_arguments(wi_string_alloc(), fmt, ap); va_end(ap); bytes = wi_socket_sendto_buffer(socket, context, wi_string_cstring(string), wi_string_length(string)); wi_release(string); return bytes; }
static wi_string_t * _wi_uuid_string(wi_uuid_t *uuid) { if(!uuid->string) { uuid->string = wi_string_init_with_format(wi_string_alloc(), WI_STR("%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X"), uuid->time_low, uuid->time_mid, uuid->time_hi_and_version, uuid->clock_seq >> 8, uuid->clock_seq & 0xFF, uuid->node[0], uuid->node[1], uuid->node[2], uuid->node[3], uuid->node[4], uuid->node[5]); }
void wd_reply(uint32_t n, wi_string_t *fmt, ...) { wd_user_t *user = wd_users_user_for_thread(); wi_string_t *string; va_list ap; va_start(ap, fmt); string = wi_string_init_with_format_and_arguments(wi_string_alloc(), fmt, ap); va_end(ap); wd_user_lock_socket(user); wi_socket_write_format(wd_user_socket(user), 0.0, WI_STR("%u %@%c"), n, string, WD_MESSAGE_SEPARATOR); wd_user_unlock_socket(user); wi_release(string); }
wi_terminal_t * wi_terminal_init(wi_terminal_t *terminal) { wi_string_t *type; const char *term; term = getenv("TERM"); if(!term) term = "vt100"; type = wi_string_init_with_cstring(wi_string_alloc(), term); terminal = wi_terminal_init_with_type(terminal, type); wi_release(type); return terminal; }
wi_integer_t wi_socket_write_format(wi_socket_t *socket, wi_time_interval_t timeout, wi_string_t *fmt, ...) { wi_string_t *string; wi_integer_t bytes; va_list ap; va_start(ap, fmt); string = wi_string_init_with_format_and_arguments(wi_string_alloc(), fmt, ap); va_end(ap); bytes = wi_socket_write_buffer(socket, timeout, wi_string_cstring(string), wi_string_length(string)); wi_release(string); return bytes; }
void wr_wprintf_block(wr_window_t *window, wi_string_t *fmt, ...) { wi_enumerator_t *enumerator; wi_string_t *string, *line; va_list ap; va_start(ap, fmt); string = wi_string_init_with_format_and_arguments(wi_string_alloc(), fmt, ap); va_end(ap); enumerator = wi_array_data_enumerator(wi_string_components_separated_by_string(string, WI_STR("\n"))); while((line = wi_enumerator_next_data(enumerator))) wr_wprintf(window, WI_STR(" %@"), line); wi_release(string); }