int ff_ctl_open2(char *ip, unsigned short port) { int fd; struct sockaddr_in addr; memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = htons(port); if(inet_aton(ip, &addr.sin_addr) == 0){ printf("bad ip '%s'\n", ip); return -1; } fd = socket(AF_INET, SOCK_STREAM, 0); if (fd < 0) { perror ("socket"); return -1; } if(connect(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0){ printf("cant connect to ip '%s'\n", ip); return -1; } if(fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK) < 0){ printf("set non block failed\n"); } ctrl_fd = fd; sb_in = sb_init(8096); sb_out = sb_init(8096); return fd; }
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; }
/** 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" ); }
/** Returns the interpreter for the specified script. Returns 0 if file is not a script with a shebang. This function leaks memory on every call. Only use it in the execve error handler which calls exit right afterwards, anyway. */ static wchar_t *get_interpreter( wchar_t *file ) { string_buffer_t sb; FILE *fp = wfopen( file, "r" ); sb_init( &sb ); wchar_t *res = 0; if( fp ) { while( 1 ) { wint_t ch = getwc( fp ); if( ch == WEOF ) break; if( ch == L'\n' ) break; sb_append_char( &sb, (wchar_t)ch ); } } res = (wchar_t *)sb.buff; if( !wcsncmp( L"#! /", res, 4 ) ) return res+3; if( !wcsncmp( L"#!/", res, 3 ) ) return res+2; return 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; }
static void Memtest_run(Memtest *memtest, MemtestSharedInfo *shared_info) { int idx; unsigned int nthread = 0; int gpu_max = 0; unsigned long long gpu_iter, gpu_flag; gpu_flag = shared_info->gpu_flag; for (gpu_iter = gpu_flag; gpu_iter; gpu_iter >>= 1) { gpu_max++; if (gpu_iter & 1) nthread++; } SpinningBarrier shared_barrier; sb_init(&shared_barrier, nthread); MemtestContext contexts[nthread]; int cnt = 0; for (idx = 0; idx < gpu_max; idx++) { if (gpu_flag & (1u << idx)) { contexts[cnt] = (MemtestContext) { .memtest = memtest, .shared_info = shared_info, .id = idx, .barrier = &shared_barrier }; CPU_ZERO(&contexts[cnt].affinity_mask); CPU_SET(cnt, &contexts[cnt].affinity_mask); cnt++; } }
static void test_myers(const char *old, const char *new_, const char *expected_difference) { SB patch; BDELTAcode rc; const char *verify_msg; if (sb_init(&patch) != 0) { fail("Out of memory"); return; } rc = diff_myers(old, strlen(old), new_, strlen(new_), &patch); if (rc != BDELTA_OK) { fail("test_myers(%s, %s, %s): diff_myers failed: %s", old, new_, expected_difference, bdelta_strerror(rc)); sb_discard(&patch, NULL, NULL); return; } verify_msg = verify_csi32(patch.start, patch.cur, expected_difference); sb_discard(&patch, NULL, NULL); if (verify_msg != NULL) { fail("test_myers(%s, %s, %s): %s", old, new_, expected_difference, verify_msg); return; } pass("test_myers(%s, %s, %s)", old, new_, expected_difference); }
char* sb_give_control_of_str(string_builder* sb) { char* str_p = sb->_str_p; str_p[sb->count] = '\0'; sb_init(sb); return str_p; }
static void buffer_enable(struct remote *self, int id) { sh_socket_subscribe(id, true); if (buffer_find(self, id)) { return; } struct buffer *b = sh_array_push(&self->buffers); b->id = id; sb_init(&b->sb); }
// ---------------------------------------------------------------- lrec_reader_t* lrec_reader_csvex_alloc(byte_reader_t* pbr, char irs, char ifs) { lrec_reader_t* plrec_reader = mlr_malloc_or_die(sizeof(lrec_reader_t)); lrec_reader_csvex_state_t* pstate = mlr_malloc_or_die(sizeof(lrec_reader_csvex_state_t)); pstate->ilno = 0LL; pstate->irs = "\r\n"; // xxx multi-byte the cli irs/ifs/etc, and integrate here pstate->ifs = ","; // xxx multi-byte the cli irs/ifs/etc, and integrate here pstate->dquote_irs = mlr_paste_2_strings("\"", pstate->irs); pstate->dquote_ifs = mlr_paste_2_strings("\"", pstate->ifs); pstate->dquote_eof = "\"\xff"; pstate->dquote = "\""; pstate->dquote_dquote = "\"\""; pstate->ifs_eof = mlr_paste_2_strings(pstate->ifs, "\xff"); pstate->irs_len = strlen(pstate->irs); pstate->ifs_len = strlen(pstate->ifs); pstate->dquote_irs_len = strlen(pstate->dquote_irs); pstate->dquote_ifs_len = strlen(pstate->dquote_ifs); pstate->dquote_eof_len = strlen(pstate->dquote_eof); pstate->dquote_len = strlen(pstate->dquote); pstate->dquote_dquote_len = strlen(pstate->dquote_dquote); pstate->ifs_eof_len = strlen(pstate->ifs_eof); pstate->peek_buf_len = pstate->irs_len; pstate->peek_buf_len = mlr_imax2(pstate->peek_buf_len, pstate->ifs_len); pstate->peek_buf_len = mlr_imax2(pstate->peek_buf_len, pstate->dquote_irs_len); pstate->peek_buf_len = mlr_imax2(pstate->peek_buf_len, pstate->dquote_ifs_len); pstate->peek_buf_len = mlr_imax2(pstate->peek_buf_len, pstate->dquote_eof_len); pstate->peek_buf_len = mlr_imax2(pstate->peek_buf_len, pstate->dquote_len); pstate->peek_buf_len = mlr_imax2(pstate->peek_buf_len, pstate->dquote_dquote_len); pstate->peek_buf_len = mlr_imax2(pstate->peek_buf_len, pstate->ifs_eof_len); pstate->peek_buf_len += 2; sb_init(&pstate->sb, STRING_BUILDER_INIT_SIZE); pstate->psb = &pstate->sb; pstate->pbr = pbr; pstate->pfr = NULL; // xxx allocate the parse-tries here -- one for dquote only, // the second for non-dquote-after-that, the third for dquoted-after-that. pstate->expect_header_line_next = TRUE; pstate->pheader_keeper = NULL; pstate->pheader_keepers = lhmslv_alloc(); plrec_reader->pvstate = (void*)pstate; plrec_reader->popen_func = &file_reader_stdio_vopen; plrec_reader->pclose_func = &file_reader_stdio_vclose; plrec_reader->pprocess_func = &lrec_reader_csvex_process; plrec_reader->psof_func = &lrec_reader_csvex_sof; plrec_reader->pfree_func = &lrec_reader_csvex_free; return plrec_reader; }
static void init(void) { options_init(); comm_init(); tl_init(); ts_init(); ti_init(); sb_init(); tl_show(); // others... }
int ff_ctl_open(unsigned short port) { int fd, tmp; struct sockaddr_in addr; memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = htons(port); fd = socket(AF_INET, SOCK_STREAM, 0); if (fd < 0) { perror ("socket"); return -1; } tmp = 1; if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &tmp, sizeof(tmp))) printf("setsockopt SO_REUSEADDR failed\n"); if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0){ printf("cant bind\n"); close(fd); return -1; } if(listen(fd, 5) < 0){ perror ("listen"); close(fd); return -1; } if(fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK) < 0){ printf("set non block failed\n"); } serv_fd = fd; sb_in = sb_init(8096); sb_out = sb_init(8096); return fd; }
static int build_test(struct TestFile *tf, struct Config *conf) { struct StringBuffer sb; sb_init(&sb, 128); make_build_command(&sb, tf, conf); int ret = checked_system(sb.s); sb_free(&sb); return ret; }
/** For wgettext: Internal init function. Automatically called when a translation is first requested. */ static void wgettext_init() { int i; wgettext_is_init = 1; for( i=0; i<BUFF_COUNT; i++ ) { sb_init( &buff[i] ); } halloc_register_function_void( global_context, &wgettext_destroy ); bindtextdomain( PACKAGE_NAME, LOCALEDIR ); textdomain( PACKAGE_NAME ); }
int output_init_http(struct output_s *o) { o->buffer=sb_init(HTTP_MAX_TS, TS_PACKET_SIZE, 0); if (o->buffer == NULL) return 0; o->hurl=calloc(2, sizeof(struct http_url)); o->hurl->url=o->url; o->hurl->cb=output_cb_http; o->hurl->arg=(void *) o; http_register_url(hserver, o->hurl); return 0; }
// Find the file in bin independent on where the // program was called from. void get_path_to_file(char* dest, const char* path_from_bin, const char* called_from) { string_builder sb; sb_init(&sb); sb_append(&sb, called_from); int dash_i = sb_last_index_of(&sb, "/"); sb_cut(&sb, 0, dash_i + 1); char* path_to_dir = sb_give_control_of_str(&sb); strcpy(dest, path_to_dir); dest = strcat(dest, path_from_bin); free(path_to_dir); sb_free(&sb); }
int luaamf_save(lua_State * L) { int result = LUAAMF_EFAILURE; luaamf_SaveBuffer sb; { void * alloc_ud = NULL; lua_Alloc alloc_fn = lua_getallocf(L, &alloc_ud); sb_init(&sb, alloc_fn, alloc_ud); } result = save_value(&sb, L, 1, 1); if (result != LUAAMF_ESUCCESS) { switch (result) { case LUAAMF_EBADTYPE: lua_pushliteral(L, "can't save: unsupported type detected"); break; case LUAAMF_ETOODEEP: lua_pushliteral(L, "can't save: nesting is too deep"); break; case LUAAMF_ETOOLONG: lua_pushliteral(L, "can't save: not enough memory"); break; default: /* Should not happen */ lua_pushliteral(L, "save failed"); break; } sb_destroy(&sb); return result; } { size_t len = 0UL; const unsigned char * buf = sb_buffer(&sb, &len); lua_pushlstring(L, (const char *)buf, len); sb_destroy(&sb); } return LUAAMF_ESUCCESS; }
/** Replace/append/insert the selection with/at/after the specified string. \param begin beginning of selection \param end end of selection \param insert the string to insert \param append_mode can be one of REPLACE_MODE, INSERT_MODE or APPEND_MODE, affects the way the test update is performed */ static void replace_part( const wchar_t *begin, const wchar_t *end, wchar_t *insert, int append_mode ) { const wchar_t *buff = get_buffer(); string_buffer_t out; int out_pos=get_cursor_pos(); sb_init( &out ); sb_append_substring( &out, buff, begin-buff ); switch( append_mode) { case REPLACE_MODE: { sb_append( &out, insert ); out_pos = wcslen( insert ) + (begin-buff); break; } case APPEND_MODE: { sb_append_substring( &out, begin, end-begin ); sb_append( &out, insert ); break; } case INSERT_MODE: { int cursor = get_cursor_pos() -(begin-buff); sb_append_substring( &out, begin, cursor ); sb_append( &out, insert ); sb_append_substring( &out, begin+cursor, end-begin-cursor ); out_pos += wcslen( insert ); break; } } sb_append( &out, end ); reader_set_buffer( (wchar_t *)out.buff, out_pos ); sb_destroy( &out ); }
/** 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 ); } }
static int read_file_mmap_psb(char* filename) { file_reader_mmap_state_t* ph = file_reader_mmap_open(filename); char* irs = "\n"; int irs_len = strlen(irs); string_builder_t sb; string_builder_t* psb = &sb; sb_init(&sb, STRING_BUILDER_INIT_SIZE); int bc = 0; while (TRUE) { char* line = read_line_mmap_psb(ph, psb, irs, irs_len); if (line == NULL) break; bc += strlen(line); } file_reader_mmap_close(ph); return bc; }
static int read_file_fgetc_psb(char* filename) { FILE* fp = fopen_or_die(filename); char* irs = "\n"; int irs_len = strlen(irs); string_builder_t sb; string_builder_t* psb = &sb; sb_init(&sb, STRING_BUILDER_INIT_SIZE); int bc = 0; while (TRUE) { char* line = read_line_fgetc_psb(fp, psb, irs, irs_len); if (line == NULL) break; bc += strlen(line); } fclose(fp); return bc; }
int main(int argc, char **argv) { int i; UpClient *upclient; IxpClient *client; signals_setup(&quit_handler); client = ixp_nsmount("wmii"); if(client == NULL) { printf("ixp_nsmount: %s\n", ixp_errbuf()); abort(); } mainloop = g_main_loop_new(NULL, FALSE); upclient = up_client_new(); sb_init(&sb, client); sb_add(&sb, &sbe_ac); for(i = 0; i < MAX_BATTERIES; i++) { sb_add(&sb, &sbe_batteries[i]); } up_client_enumerate_devices_sync(upclient, NULL, NULL); g_signal_connect(upclient, "device-added", G_CALLBACK(device_added_cb), NULL); g_signal_connect(upclient, "device-removed", G_CALLBACK(device_removed_cb), NULL); g_signal_connect(upclient, "device-changed", G_CALLBACK(device_changed_cb), NULL); g_signal_connect(upclient, "changed", G_CALLBACK(changed_cb), NULL); update_sb(upclient); g_main_loop_run(mainloop); sb_finish(&sb); ixp_unmount(client); }
void sb_replace(string_builder *sb, const char* old_val, const char* new_val) { if (old_val == NULL || sb->count == 0) return; string_builder new_sb; sb_init(&new_sb); int old_val_len = strlen(old_val); for (size_t i = 0; i < sb->count; i++) { if (strncmp(sb->_str_p + i, old_val, old_val_len) == 0 && i <= sb->count - old_val_len) { sb_append(&new_sb, new_val); i += old_val_len - 1; } else { sb_append_char(&new_sb, sb->_str_p[i]); } } sb_free(sb); *sb = new_sb; }
void proc_init() { dbg_uart_str("Proc init\n"); _proc_id = 0; sb_init(&proc_tree); list_init(&proc_zero_queue); list_init(&proc_normal_queue); list_init(&proc_normal_noticks_queue); list_init(&proc_realtime_queue); list_init(&init_proc_queue); Process *init_proc = _proc_construct_init(); current_process = init_proc; proc_schedule(); // update states ProcScene *scene = proc_current_scene(init_proc); scene->regs[28] = (size_t)mm_do_mmap_empty(PAGE_SIZE, USER_SPACE_SIZE - PAGE_SIZE) + PAGE_SIZE; }
/* * JSON serializes a collection of dSets. * * sets -- A collection of dSet objects. * count -- The number of dSets in sets. * */ char* json_serialize(dSet** sets, int count) { SB sb; sb_init(&sb); sb_putc(&sb, '['); int i; for (i = 0; i < count; i++) { sb_put_dset(&sb, sets[i]); if (i != count - 1) { sb_putc(&sb, ','); } } sb_putc(&sb, ']'); sb_finish(&sb); return sb.start; }
/** 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; }
char *ett_unique_type_name(EagleComplexType *t) { switch(t->type) { NAME_BASIC(None); NAME_BASIC(Any); NAME_BASIC(Nil); NAME_BASIC(Int1); NAME_BASIC(UInt8); NAME_BASIC(Int8); NAME_BASIC(UInt16); NAME_BASIC(Int16); NAME_BASIC(UInt32); NAME_BASIC(Int32); NAME_BASIC(UInt64); NAME_BASIC(Int64); NAME_BASIC(Float); NAME_BASIC(Double); NAME_BASIC(CString); case ETPointer: { EaglePointerType *pt = (EaglePointerType *)t; char *sub = ett_unique_type_name(pt->to); char *out = malloc(strlen(sub) + 100); sprintf(out, "__%s_%sptr__", sub, pt->counted ? "c" : ""); return out; } case ETArray: { EagleArrayType *at = (EagleArrayType *)t; char *sub = ett_unique_type_name(at->of); char *out = malloc(strlen(sub) + 100); sprintf(out, "__%s_arr__", sub); return out; } case ETGenerator: { EagleGenType *gt = (EagleGenType *)t; char *sub = ett_unique_type_name(gt->ytype); char *out = malloc(strlen(sub) + 100); sprintf(out, "__%s_gen__", sub); return out; } case ETClass: case ETStruct: { EagleStructType *st = (EagleStructType *)t; return strdup(st->name); } case ETInterface: { EagleInterfaceType *it = (EagleInterfaceType *)t; Strbuilder sb; sb_init(&sb); for(int i = 0; i < it->names.count; i++) { sb_append(&sb, it->names.items[i]); sb_append(&sb, "_"); } return sb.buffer; } case ETFunction: { EagleFunctionType *ft = (EagleFunctionType *)t; Strbuilder sb; sb_init(&sb); sb_append(&sb, "fn_"); for(int i = 0; i < ft->pct; i++) { char *p = ett_unique_type_name(ft->params[i]); sb_append(&sb, p); free(p); } if(ft->retType->type != ETVoid) { char *p = ett_unique_type_name(ft->retType); sb_append(&sb, p); free(p); } else { sb_append(&sb, "void"); } return sb.buffer; } default: return NULL; } }
int main(int argc, char *argv[]) { unsigned int exposure, gain, mode, nzwo, bin, lm; ASI_CAMERA_INFO prop; unsigned char *buff; int bsize, n=0, max = 0; float sum = 0.0; int width, height; if (argc != 9) { printf("Usage: %s [camera idx] [mode] [bin] [exposure in ms] [gain] [long mode] [w if >0] [h if>0]\n", argv[0]); exit(1); } cidx = strtoul(argv[1], NULL, 0); mode = strtoul(argv[2], NULL, 0); bin = strtoul(argv[3], NULL, 0); exposure = strtoul(argv[4], NULL, 0); gain = strtoul(argv[5], NULL, 0); lm = strtoul(argv[6], NULL, 0); width = strtol(argv[7], NULL, 0); height = strtol(argv[8], NULL, 0); nzwo = ASIGetNumOfConnectedCameras(); if (cidx >= nzwo) { fprintf(stderr, "Camera out of range %d/%d\n", cidx, nzwo); exit(1); } CHECK(ASIGetCameraProperty(&prop, cidx)); CHECK(ASIOpenCamera(cidx)); CHECK(ASIInitCamera(cidx)); if (width <= 0) width = prop.MaxWidth/bin; if (height <= 0) height = prop.MaxHeight/bin; fprintf(stderr, "%d x %d\n", width, height); bsize = width * height; if (mode == 1) bsize *= 3; else if (mode == 2) bsize *= 2; buff = malloc(bsize); CHECK(ASISetROIFormat(cidx, width, height, bin, mode)); set_cap(ASI_EXPOSURE, exposure * 1000); set_cap(ASI_GAIN, gain); set_cap(ASI_HARDWARE_BIN, ASI_TRUE); #ifdef SELF_BULK sb_init(bsize); #endif if (lm == 0) CHECK(ASIStartVideoCapture(cidx)); while (1) { int r; struct timespec start, stop; long delta; clock_gettime(CLOCK_MONOTONIC, &start); if (lm == 1) { ASI_EXPOSURE_STATUS s; CHECK1(ASIGetExpStatus(cidx, &s)); if (s != ASI_EXP_IDLE && s != ASI_EXP_FAILED) { fprintf(stderr, "Not idle or failed: %d\n", s); exit(1); } CHECK(ASIStartExposure(cidx, 0)); fprintf(stderr, "START\n"); r = 99; /* timeout */ do { usleep(10*1000); clock_gettime(CLOCK_MONOTONIC, &stop); delta = (stop.tv_sec - start.tv_sec) * 1000 + (stop.tv_nsec - start.tv_nsec) / 1000000; CHECK1(ASIGetExpStatus(cidx, &s)); if (s == ASI_EXP_IDLE) { fprintf(stderr, "start\n"); CHECK(ASIStartExposure(cidx, 0)); } else if (s == ASI_EXP_SUCCESS) { r = 0; fprintf(stderr, "FINISHED\n"); CHECK(ASIGetDataAfterExp(cidx, buff, bsize)); break; } else if (s == ASI_EXP_FAILED) { r = 98; break; } else if (s != ASI_EXP_WORKING) { fprintf(stderr, "Unexpected status\n"); exit(1); } } while (delta < 20 * exposure + 500); } else { fprintf(stderr, "ASIGetVideoData\n"); r = ASIGetVideoData(cidx, buff, bsize, 20 * exposure + 500); clock_gettime(CLOCK_MONOTONIC, &stop); delta = (stop.tv_sec - start.tv_sec) * 1000 + (stop.tv_nsec - start.tv_nsec) / 1000000; } n++; if (delta > max) max = delta; sum += delta; fprintf(stderr, ":%2d %6.0f(%6d) %ld\n", r, sum / n, max, delta); } return 0; }
/** This function is executed by the child process created by a call to fork(). It should be called after \c setup_child_process. It calls execve to replace the fish process image with the command specified in \c p. It never returns. */ static void launch_process( process_t *p ) { FILE* f; int err; // debug( 1, L"exec '%ls'", p->argv[0] ); char **argv = wcsv2strv( (const wchar_t **) p->argv); char **envv = env_export_arr( 0 ); execve ( wcs2str(p->actual_cmd), argv, envv ); err = errno; /* Something went wrong with execve, check for a ":", and run /bin/sh if encountered. This is a weird predecessor to the shebang that is still sometimes used since it is supported on Windows. */ f = wfopen(p->actual_cmd, "r"); if( f ) { char begin[1] = {0}; size_t read; read = fread(begin, 1, 1, f); fclose( f ); if( (read==1) && (begin[0] == ':') ) { int count = 0; int i = 1; wchar_t **res; char **res_real; while( p->argv[count] != 0 ) count++; res = malloc( sizeof(wchar_t*)*(count+2)); res[0] = L"/bin/sh"; res[1] = p->actual_cmd; for( i=1; p->argv[i]; i++ ){ res[i+1] = p->argv[i]; } res[i+1] = 0; p->argv = res; p->actual_cmd = L"/bin/sh"; res_real = wcsv2strv( (const wchar_t **) res); execve ( wcs2str(p->actual_cmd), res_real, envv ); } } errno = err; debug( 0, _( L"Failed to execute process '%ls'. Reason:" ), p->actual_cmd ); switch( errno ) { case E2BIG: { size_t sz = 0; char **p; string_buffer_t sz1; string_buffer_t sz2; long arg_max = -1; sb_init( &sz1 ); sb_init( &sz2 ); for(p=argv; *p; p++) { sz += strlen(*p)+1; } for(p=envv; *p; p++) { sz += strlen(*p)+1; } sb_format_size( &sz1, sz ); arg_max = sysconf( _SC_ARG_MAX ); if( arg_max > 0 ) { sb_format_size( &sz2, arg_max ); debug( 0, L"The total size of the argument and environment lists (%ls) exceeds the operating system limit of %ls.", (wchar_t *)sz1.buff, (wchar_t *)sz2.buff); } else { debug( 0, L"The total size of the argument and environment lists (%ls) exceeds the operating system limit.", (wchar_t *)sz1.buff); } debug( 0, L"Try running the command again with fewer arguments."); sb_destroy( &sz1 ); sb_destroy( &sz2 ); exit(STATUS_EXEC_FAIL); break; } case ENOEXEC: { wperror(L"exec"); debug(0, L"The file '%ls' is marked as an executable but could not be run by the operating system.", p->actual_cmd); exit(STATUS_EXEC_FAIL); } case ENOENT: { wchar_t *interpreter = get_interpreter( p->actual_cmd ); if( interpreter && waccess( interpreter, X_OK ) ) { debug(0, L"The file '%ls' specified the interpreter '%ls', which is not an executable command.", p->actual_cmd, interpreter ); } else { debug(0, L"The file '%ls' or a script or ELF interpreter does not exist, or a shared library needed for file or interpreter cannot be found.", p->actual_cmd); } exit(STATUS_EXEC_FAIL); } case ENOMEM: { debug(0, L"Out of memory"); exit(STATUS_EXEC_FAIL); } default: { wperror(L"exec"); // debug(0, L"The file '%ls' is marked as an executable but could not be run by the operating system.", p->actual_cmd); exit(STATUS_EXEC_FAIL); } } }
int main(int argc, char **argv) { int n, nfds, res; struct itimerspec timerits; struct epoll_event events[MAX_EVENTS]; struct epoll_event timerevent; IxpClient* client; struct sb sb; signals_setup(&quit_handler); struct sb_entry sbe_sda = { .sbe_path = "/rbar/60_sda", .sbe_private = "sda", .sbe_init = &init_block, .sbe_update = &update_block, .sbe_foreground = 0xbbbbbb, .sbe_background = 0x444444, .sbe_border = 0x555555, }; struct sb_entry sbe_sdb = { .sbe_path = "/rbar/61_sdb", .sbe_private = "sdb", .sbe_init = &init_block, .sbe_update = &update_block, .sbe_foreground = 0xbbbbbb, .sbe_background = 0x444444, .sbe_border = 0x555555, }; struct sb_entry sbe_sdc = { .sbe_path = "/rbar/62_sdc", .sbe_private = "sdc", .sbe_init = &init_block, .sbe_update = &update_block, .sbe_foreground = 0xbbbbbb, .sbe_background = 0x444444, .sbe_border = 0x555555, }; int epollfd = epoll_create1(EPOLL_CLOEXEC); if(epollfd == -1) { perror("epoll_create"); abort(); } int timerfd = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK|TFD_CLOEXEC); if(timerfd == -1) { perror("timerfd_create"); abort(); } timerevent.events = EPOLLIN; timerevent.data.fd = timerfd; timerits.it_interval.tv_sec = 0; timerits.it_interval.tv_nsec = 250 * 1000 * 1000; timerits.it_value.tv_sec = timerits.it_interval.tv_sec; timerits.it_value.tv_nsec = timerits.it_interval.tv_nsec; client = ixp_nsmount("wmii"); if(client == NULL) { printf("ixp_nsmount: %s\n", ixp_errbuf()); abort(); } res = epoll_ctl(epollfd, EPOLL_CTL_ADD, timerfd, &timerevent); if(res == -1) { perror("epoll_ctl"); abort(); } res = timerfd_settime(timerfd, 0, &timerits, NULL); if(res == -1) { perror("timerfd_settime"); abort(); } sb_init(&sb, client); sb_add(&sb, &sbe_sda); sb_add(&sb, &sbe_sdb); sb_add(&sb, &sbe_sdc); while(1) { nfds = epoll_wait(epollfd, events, MAX_EVENTS, -1); if(nfds == -1) { if(errno != EINTR) { perror("epoll_wait"); abort(); } } if(should_quit) { break; } for (n = 0; n < nfds; n++) { if(events[n].data.fd == timerfd) { uint64_t x; read(timerfd, &x, sizeof(x)); sb_update(&sb); } } } sb_finish(&sb); ixp_unmount(client); return 0; }