static void get_summary_info( char *buff, const char *mpec_filename) { FILE *ifile = fopen_ext( mpec_filename, "frb"); char ibuff[400], *tptr; unsigned i; memset( buff, ' ', 80); buff[80] = '\0'; while( fgets( ibuff, sizeof( ibuff), ifile)) { if( (tptr = strstr( ibuff, "Pseudo-MPEC for")) != NULL) { tptr += 16; for( i = 0; *tptr && *tptr != '<' && i < 30; i++) buff[i] = *tptr++; } else if( ibuff[0] == 'a' && ibuff[1] == ' ') extract_value( buff + 17, ibuff + 2, 3); else if( ibuff[0] == 'e' && ibuff[1] == ' ') extract_value( buff + 34, ibuff + 2, 3); if( (tptr = strstr( ibuff, "Incl.")) != NULL) extract_value( buff + 51, tptr + 5, 1); else if( (tptr = strstr( ibuff, " Ea ")) != NULL) memcpy( buff + 68, tptr + 4, 7); } fclose( ifile); }
void __init detect_soc(void) { unsigned long upr, cfg, version, revision; upr = mfspr(SPR_UPR); if (upr & SPR_UPR_UP) printk("Detecting Processor units:\n"); else { printk("Unit Present Register not avaliable\n"); return; } cfg = mfspr(SPR_VR); version=extract_value(cfg, SPR_VR_VER); revision=extract_value(cfg, SPR_VR_REV); #ifndef CONFIG_OR32_ANONYMOUS printk(" CPU\t: or32/OpenRISC-%lx, revision %lx, @%d MHz, %s\n", version, revision, CONFIG_OR32_SYS_CLK, (upr & (unsigned long)SPR_UPR_SRP) ? "with shadow registers" : "with no shadow registers"); if ((version == 0x1200) & (revision == 0)) { detect_soc_rev_0(upr); } else { detect_soc_generic(upr); } #endif /* CONFIG_OR32_ANONYMOUS */ printk(" Signed 0x%lx\n", su_asm()); }
static void list_to_array (xmmsv_t *value, void *user_data) { VALUE *args = user_data; rb_ary_push (args[0], extract_value (args[1], value)); }
static VALUE c_dict_aref (VALUE self, VALUE key) { RbDict *dict = NULL; xmmsv_dict_iter_t *it; xmmsv_t *value; const char *ckey; int s; Check_Type (key, T_SYMBOL); Data_Get_Struct (self, RbDict, dict); ckey = rb_id2name (SYM2ID (key)); xmmsv_get_dict_iter (dict->real, &it); s = xmmsv_dict_iter_find (it, ckey); if (!s) return Qnil; xmmsv_dict_iter_pair (it, NULL, &value); return extract_value (self, value); }
static void dict_each_value (const char *key, xmmsv_t *value, void *udata) { VALUE *parent = udata; rb_yield (extract_value (*parent, value)); }
struct form_t parse_form(){ struct form_t form = {{0},{0},0,{0},{0},0,-1}; const char *query_string = getenv("QUERY_STRING"); extract_query_string(&form, query_string); size_t content_len = get_content_len(); if (content_len < 1){ return form; } char *buf = malloc(content_len); size_t readb = fread_loop(buf, 1, content_len, stdin); if (readb < content_len){ fatal("problem parsing POST data (read %zu of %zu)\n", readb, content_len); } char *content_type = getenv("CONTENT_TYPE"); if (!content_type){ free(buf); return form; } if (*content_type == ' '){ content_type++; } if (!startswith(content_type, "multipart/form-data;")){ fatal("form has unknown content type %s", content_type); } char *boundary = extract_value(content_type, "boundary="); parse_multipart_form(&form, buf, content_len, boundary); free(buf); return form; }
void extract_content_disposition(char *line, struct mimeheader_t *header){ if (!startswith(line, "form-data;")){ return; } char *name = extract_value(line, "name="); if (!name){ return; } header->name = name; char *filename = extract_value(line, "filename="); if (filename){ header->type = TYPE_FILE; header->filename = filename; } else { header->type = TYPE_VALUE; } }
static void dict_each_pair (const char *key, xmmsv_t *value, void *udata) { VALUE *parent = udata; rb_yield_values (2, ID2SYM (rb_intern (key)), extract_value (*parent, value)); }
/** * extracts a parameter: value pair */ err_t extract_parameter_value(chunk_t *name, chunk_t *value, chunk_t *line) { /* extract name */ if (!extract_token(name,':', line)) { return "missing ':'"; } /* extract value */ return extract_value(value, line); }
static VALUE c_value_get (VALUE self) { RbResult *res = NULL; xmmsv_t *val; Data_Get_Struct (self, RbResult, res); val = xmmsc_result_get_value (res->real); return extract_value (self, val); }
static bool parse_line(config_file_t *conf, struct entry_list *list, char *line) { // Remove everything after comment. char *comment = strchr(line, '#'); if (comment) *comment = '\0'; // Starting line with # and include includes config files. :) if ((comment == line) && (conf->include_depth < MAX_INCLUDE_DEPTH)) { comment++; if (strstr(comment, "include ") == comment) { add_sub_conf(conf, comment + strlen("include ")); return false; } } else if (conf->include_depth >= MAX_INCLUDE_DEPTH) fprintf(stderr, "!!! #include depth exceeded for config. Might be a cycle.\n"); // Skips to first character. while (isspace(*line)) line++; char *key = (char*)malloc(9); size_t cur_size = 8; size_t index = 0; while (isgraph(*line)) { if (index == cur_size) { cur_size *= 2; key = (char*)realloc(key, cur_size + 1); } key[index++] = *line++; } key[index] = '\0'; list->key = key; list->value = extract_value(line, true); if (!list->value) { list->key = NULL; free(key); return false; } return true; }
int main (int argc, char *argv[]){ FILE *usbdev; char linebuf[LINEBUF_MAX]; char vendor[ID_MAX], product[ID_MAX]; char found_device; vendor[0] = 0; product[0] = 0; found_device = 0; if( ! (usbdev = fopen("/proc/bus/usb/devices", "r")) ){ return 1; } while( fgets(linebuf, LINEBUF_MAX, usbdev) ){ if(strstr(linebuf, "BC125AT")){ found_device = 1; } else if(!linebuf[0] || !(linebuf[0] >= 'A' && linebuf[0] <= 'Z')){ vendor[0] = 0; product[0] = 0; } if(found_device && vendor[0] && product[0]) break; extract_value( linebuf, "Vendor=", vendor ); extract_value( linebuf, "ProdID=", product ); } if (!found_device){ printf("found_device=%d\n", found_device); return 1; } printf("found_device=%d; vendor=%s; product=%s\n", found_device, vendor, product); return setup_device(vendor, product); }
int main( int argc, char **argv ){ IrrealContext context; IrrealStack code; if( argc < 2 ){ fprintf( stderr, "Usage: %s file\n\n", argv[0] ); return 1; } init_threading(); std::string text = read_file( argv[1] ); //printf( "'%s'\n", text.c_str() ); std::vector<std::string> tokens = split_string( text ); for( size_t i = 0 ; i < tokens.size() ; ++i ){ //printf( "%s\n", tokens[i].c_str() ); code.push( extract_value( tokens[i] ) ); } //code._debug_print(); context.getCodeStack()->merge( &code, true ); global_vm_queue.push_front( context.get_id() ); ++global_running_vms; pthread_t workers[ NUM_OF_THREADS ]; pthread_attr_t attr; void *status; pthread_attr_init( &attr ); pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_JOINABLE ); for( size_t i = 0 ; i < NUM_OF_THREADS ; ++i ){ pthread_create( &workers[i], &attr, worker_thread, (void *)i ); } pthread_attr_destroy( &attr ); for( size_t i = 0 ; i < NUM_OF_THREADS ; ++i ){ pthread_join( workers[i], &status ); } pthread_exit( NULL ); return 0; }
void send_direction_vector_output(struct gtk_common_data *mdata) { double values[3]; int r; extract_value(mdata, values, 3, "X", "Y", "Z"); r = sol_flow_send_direction_vector_components_packet(mdata->node, SOL_FLOW_NODE_TYPE_GTK_DIRECTION_VECTOR_EDITOR__OUT__OUT, values[0], values[1], values[2]); if (r < 0) SOL_WRN("Could not send the direction vector packet. Reason: %s", sol_util_strerrora(-r)); }
static int on_signal (xmmsv_t *val, void *data) { VALUE rbval, ret, callback = (VALUE) data; rbval = extract_value (Qnil, val); ret = rb_funcall (callback, rb_intern ("call"), 1, rbval); if (ret == Qnil || ret == Qfalse) return 0; else if (ret == Qtrue) return 1; else return NUM2INT (ret); }
void send_float_output(struct gtk_common_data *mdata) { double value; struct sol_drange drange = SOL_DRANGE_INIT(); int r; extract_value(mdata, &value, 1, "Float"); drange.val = value; r = sol_flow_send_drange_packet(mdata->node, SOL_FLOW_NODE_TYPE_GTK_FLOAT_EDITOR__OUT__OUT, &drange); if (r < 0) SOL_WRN("Could not send the location packet. Reason: %s", sol_util_strerrora(-r)); }
static void add_sub_conf(config_file_t *conf, char *line) { char real_path[PATH_MAX_LENGTH]; config_file_t *sub_conf = NULL; char *path = extract_value(line, false); if (!path) return; add_include_list(conf, path); real_path[0] = '\0'; #ifdef _WIN32 fill_pathname_resolve_relative(real_path, conf->path, path, sizeof(real_path)); #else #ifndef __CELLOS_LV2__ if (*path == '~') { const char *home = getenv("HOME"); strlcpy(real_path, home ? home : "", sizeof(real_path)); strlcat(real_path, path + 1, sizeof(real_path)); } else #endif fill_pathname_resolve_relative(real_path, conf->path, path, sizeof(real_path)); #endif sub_conf = (config_file_t*) config_file_new_internal(real_path, conf->include_depth + 1); if (!sub_conf) { free(path); return; } /* Pilfer internal list. */ add_child_list(conf, sub_conf); config_file_free(sub_conf); free(path); }
T * operator () ( PyObject * _obj ) { T * value = nullptr; if( extract_value( _obj, value, true ) == false ) { const std::type_info & tinfo = typeid(T *); const char * type_name = tinfo.name(); pybind::log( "extract_value<intrusive>: extract invalid %s:%s not cast to '%s'" , pybind::object_repr( _obj ) , pybind::object_repr_type( _obj ) , type_name ); } return value; }
typename stdex::mpl::remove_cref<T>::type operator () ( PyObject * _obj ) { typedef typename stdex::mpl::remove_cref<T>::type type_value; type_value value; if( extract_value( _obj, value, true ) == false ) { const std::type_info & tinfo = typeid(type_value); const char * type_name = tinfo.name(); pybind::log( "extract_value<T>: extract invalid %s:%s not cast to '%s'" , pybind::object_repr( _obj ) , pybind::object_repr_type( _obj ) , type_name ); } return value; }
void send_location_output(struct gtk_common_data *mdata) { struct sol_location loc; double values[3]; int r; extract_value(mdata, values, 3, "Latitude", "Longitude", "Altitude"); loc.lat = values[0]; loc.lon = values[1]; loc.alt = values[2]; r = sol_flow_send_location_packet(mdata->node, SOL_FLOW_NODE_TYPE_GTK_LOCATION_EDITOR__OUT__OUT, &loc); if (r < 0) SOL_WRN("Could not send the location packet. Reason: %s", sol_util_strerrora(-r)); }
void extract_content(t_env *env, size_t pos, char *ptr) { while (pos < env->len && env->interprete[pos] != SPACING && env->interprete[pos] != DELIMITER) { if (env->interprete[pos] == INTERPRETED) extract_normal(env, &pos, &ptr); else if (env->interprete[pos] == SIMPLE_QUOTED) extract_simple_quote(env, &pos, &ptr); else if (env->interprete[pos] == DOUBLE_QUOTED) extract_double_quote(env, &pos, &ptr); else if (env->interprete[pos] == BACK_QUOTED) extract_back_quote(env, &pos, &ptr); else if (env->interprete[pos] == BACKSLASHED) extract_backslash(env, &pos, &ptr); else if (env->interprete[pos] == START_LOCAL_VARIABLE) extract_value(env, &pos, &ptr); } *ptr = '\0'; }
int f_impl(Params const& p) { typename boost::parameter::binding<Params, tag::name>::type n = p[name]; typename boost::parameter::binding< Params, tag::value, double >::type v = extract_value(p, boost::bind(&value_default)); typename boost::parameter::binding< Params, tag::index, int >::type i = #if BOOST_WORKAROUND(__DECCXX_VER, BOOST_TESTED_AT(60590042)) p[test::index | 999]; #else p[index | 999]; #endif p[tester](n,v,i); return 1; }
int main(int argc, char *argv[]) { int ix, jx, val; glkunix_startup_t startdata; /* Test for compile-time errors. If one of these spouts off, you must edit glk.h and recompile. */ if (sizeof(glui32) != 4) { printf("Compile-time error: glui32 is not a 32-bit value. Please fix glk.h.\n"); return 1; } if ((glui32)(-1) < 0) { printf("Compile-time error: glui32 is not unsigned. Please fix glk.h.\n"); return 1; } /* Now some argument-parsing. This is probably going to hurt. */ startdata.argc = 0; startdata.argv = (char **)malloc(argc * sizeof(char *)); /* Copy in the program name. */ startdata.argv[startdata.argc] = argv[0]; startdata.argc++; for (ix=1; ix<argc && !errflag; ix++) { glkunix_argumentlist_t *argform; int inarglist = FALSE; char *cx; for (argform = glkunix_arguments; argform->argtype != glkunix_arg_End && !errflag; argform++) { if (argform->name[0] == '\0') { if (argv[ix][0] != '-') { startdata.argv[startdata.argc] = argv[ix]; startdata.argc++; inarglist = TRUE; } } else if ((argform->argtype == glkunix_arg_NumberValue) && !strncmp(argv[ix], argform->name, strlen(argform->name)) && (cx = argv[ix] + strlen(argform->name)) && (atoi(cx) != 0 || cx[0] == '0')) { startdata.argv[startdata.argc] = argv[ix]; startdata.argc++; inarglist = TRUE; } else if (!strcmp(argv[ix], argform->name)) { int numeat = 0; if (argform->argtype == glkunix_arg_ValueFollows) { if (ix+1 >= argc) { printf("%s: %s must be followed by a value\n", argv[0], argform->name); errflag = TRUE; break; } numeat = 2; } else if (argform->argtype == glkunix_arg_NoValue) { numeat = 1; } else if (argform->argtype == glkunix_arg_ValueCanFollow) { if (ix+1 < argc && argv[ix+1][0] != '-') { numeat = 2; } else { numeat = 1; } } else if (argform->argtype == glkunix_arg_NumberValue) { if (ix+1 >= argc || (atoi(argv[ix+1]) == 0 && argv[ix+1][0] != '0')) { printf("%s: %s must be followed by a number\n", argv[0], argform->name); errflag = TRUE; break; } numeat = 2; } else { errflag = TRUE; break; } for (jx=0; jx<numeat; jx++) { startdata.argv[startdata.argc] = argv[ix]; startdata.argc++; if (jx+1 < numeat) ix++; } inarglist = TRUE; break; } } if (inarglist || errflag) continue; if (argv[ix][0] != '-') { printf("%s: unwanted argument: %s\n", argv[0], argv[ix]); errflag = TRUE; break; } if (extract_value(argc, argv, "?", ex_Void, &ix, &val, FALSE)) errflag = TRUE; else if (extract_value(argc, argv, "help", ex_Void, &ix, &val, FALSE)) errflag = TRUE; else if (extract_value(argc, argv, "version", ex_Void, &ix, &val, FALSE)) pref_printversion = val; else if (extract_value(argc, argv, "v", ex_Void, &ix, &val, FALSE)) pref_printversion = val; else if (extract_value(argc, argv, "fixmetrics", ex_Bool, &ix, &val, FALSE)) pref_fixedmetrics = val; else if (extract_value(argc, argv, "fm", ex_Bool, &ix, &val, FALSE)) pref_fixedmetrics = val; else if (extract_value(argc, argv, "width", ex_Int, &ix, &val, 80)) pref_screenwidth = val; else if (extract_value(argc, argv, "w", ex_Int, &ix, &val, 80)) pref_screenwidth = val; else if (extract_value(argc, argv, "height", ex_Int, &ix, &val, 50)) pref_screenheight = val; else if (extract_value(argc, argv, "h", ex_Int, &ix, &val, 50)) pref_screenheight = val; else if (extract_value(argc, argv, "stderr", ex_Bool, &ix, &val, FALSE)) pref_stderr = val; else if (extract_value(argc, argv, "support", ex_Str, &ix, &val, FALSE)) { if (!strcmp(extracted_string, "timer") || !strcmp(extracted_string, "timers")) pref_timersupport = TRUE; else if (!strcmp(extracted_string, "hyperlink") || !strcmp(extracted_string, "hyperlinks")) pref_hyperlinksupport = TRUE; else if (!strcmp(extracted_string, "graphics")) pref_graphicssupport = TRUE; else if (!strcmp(extracted_string, "graphicswin")) pref_graphicswinsupport = TRUE; else { printf("%s: -support value not recognized: %s\n", argv[0], extracted_string); errflag = TRUE; } } else if (extract_value(argc, argv, "resourcedir", ex_Str, &ix, &val, FALSE)) pref_resourceurl = construct_resourceurl(extracted_string, TRUE); else if (extract_value(argc, argv, "rd", ex_Str, &ix, &val, FALSE)) pref_resourceurl = construct_resourceurl(extracted_string, TRUE); else if (extract_value(argc, argv, "resourceurl", ex_Str, &ix, &val, FALSE)) pref_resourceurl = construct_resourceurl(extracted_string, FALSE); else if (extract_value(argc, argv, "ru", ex_Str, &ix, &val, FALSE)) pref_resourceurl = construct_resourceurl(extracted_string, FALSE); #if GIDEBUG_LIBRARY_SUPPORT else if (extract_value(argc, argv, "D", ex_Void, &ix, &val, FALSE)) gli_debugger = val; #endif /* GIDEBUG_LIBRARY_SUPPORT */ else { printf("%s: unknown option: %s\n", argv[0], argv[ix]); errflag = TRUE; } } if (errflag) { printf("usage: %s [ options ... ]\n", argv[0]); if (glkunix_arguments[0].argtype != glkunix_arg_End) { glkunix_argumentlist_t *argform; printf("game options:\n"); for (argform = glkunix_arguments; argform->argtype != glkunix_arg_End; argform++) { if (strlen(argform->name) == 0) printf(" %s\n", argform->desc); else if (argform->argtype == glkunix_arg_ValueFollows) printf(" %s val: %s\n", argform->name, argform->desc); else if (argform->argtype == glkunix_arg_NumberValue) printf(" %s val: %s\n", argform->name, argform->desc); else if (argform->argtype == glkunix_arg_ValueCanFollow) printf(" %s [val]: %s\n", argform->name, argform->desc); else printf(" %s: %s\n", argform->name, argform->desc); } } printf("library options:\n"); printf(" -fixmetrics BOOL: define screen size manually (default 'no')\n"); printf(" -width NUM: manual screen width (default 80)\n"); printf(" -height NUM: manual screen height (default 50)\n"); printf(" -support [timer, hyperlinks, graphics, graphicswin]: declare support for various input features\n"); printf(" -resourceurl STR: URL base for image/sound files\n"); printf(" -resourcedir STR: path to image/sound files (used to create file: URLs)\n"); printf(" -stderr BOOL: send errors to stderr rather than stdout (default 'no')\n"); #if GIDEBUG_LIBRARY_SUPPORT printf(" -D: turn on debug console\n"); #endif /* GIDEBUG_LIBRARY_SUPPORT */ printf(" -version: display Glk library version\n"); printf(" -help: display this list\n"); printf("NUM values can be any number. BOOL values can be 'yes' or 'no', or no value to toggle.\n"); return 1; } if (pref_printversion) { printf("RemGlk, library version %s.\n", LIBRARY_VERSION); printf("For more information, see http://eblong.com/zarf/glk/\n"); return 1; } gli_initialize_datainput(); data_metrics_t *metrics = data_metrics_alloc(pref_screenwidth, pref_screenheight); if (!pref_fixedmetrics) { data_event_t *data = data_event_read(); if (data->dtag != dtag_Init) gli_fatal_error("First input event must be 'init'"); if (data->supportcaps) { /* Set the suppport preference flags. (Bit of a layering violation, but the flags are simple.) */ if (data->supportcaps->timer) pref_timersupport = TRUE; if (data->supportcaps->hyperlinks) pref_hyperlinksupport = TRUE; if (data->supportcaps->graphics) pref_graphicssupport = TRUE; if (data->supportcaps->graphicswin) pref_graphicswinsupport = TRUE; } /* Copy the metrics into the permanent structure */ *metrics = *data->metrics; data_event_free(data); } /* Initialize things. */ gli_initialize_misc(); gli_initialize_windows(metrics); gli_initialize_events(); data_metrics_free(metrics); inittime = TRUE; if (!glkunix_startup_code(&startdata)) { glk_exit(); } inittime = FALSE; if (gli_debugger) gidebug_announce_cycle(gidebug_cycle_Start); /* Call the program main entry point, and then exit. */ glk_main(); glk_exit(); /* glk_exit() doesn't return, but the compiler may kvetch if main() doesn't seem to return a value. */ return 0; }
static void invoke_callback(JNIEnv* env, callback *cb, ffi_cif* cif, void *resp, void **cbargs) { jobject self; void *oldresp = resp; self = (*env)->NewLocalRef(env, cb->object); // Avoid calling back to a GC'd object if ((*env)->IsSameObject(env, self, NULL)) { fprintf(stderr, "JNA: callback object has been garbage collected\n"); if (cif->rtype->type != FFI_TYPE_VOID) { memset(resp, 0, cif->rtype->size); } } else if (cb->direct) { unsigned int i; void **args = alloca((cif->nargs + 3) * sizeof(void *)); args[0] = (void *)&env; args[1] = &self; args[2] = &cb->methodID; memcpy(&args[3], cbargs, cif->nargs * sizeof(void *)); // Note that there is no support for CVT_TYPE_MAPPER here if (cb->conversion_flags) { for (i=0;i < cif->nargs;i++) { switch(cb->conversion_flags[i]) { case CVT_INTEGER_TYPE: case CVT_POINTER_TYPE: case CVT_NATIVE_MAPPED: case CVT_NATIVE_MAPPED_STRING: case CVT_NATIVE_MAPPED_WSTRING: // Make sure we have space enough for the new argument args[i+3] = alloca(sizeof(void *)); *((void **)args[i+3]) = fromNativeCallbackParam(env, cb->arg_classes[i], cif->arg_types[i], cbargs[i], JNI_FALSE, cb->encoding); break; case CVT_POINTER: *((void **)args[i+3]) = newJavaPointer(env, *(void **)cbargs[i]); break; case CVT_STRING: *((void **)args[i+3]) = newJavaString(env, *(void **)cbargs[i], cb->encoding); break; case CVT_WSTRING: *((void **)args[i+3]) = newJavaWString(env, *(void **)cbargs[i]); break; case CVT_STRUCTURE: *((void **)args[i+3]) = newJavaStructure(env, *(void **)cbargs[i], cb->arg_classes[i]); break; case CVT_STRUCTURE_BYVAL: args[i+3] = alloca(sizeof(void *)); *((void **)args[i+3]) = newJavaStructure(env, cbargs[i], cb->arg_classes[i]); break; case CVT_CALLBACK: *((void **)args[i+3]) = newJavaCallback(env, *(void **)cbargs[i], cb->arg_classes[i]); break; case CVT_FLOAT: args[i+3] = alloca(sizeof(double)); *((double *)args[i+3]) = *(float*)cbargs[i]; break; case CVT_DEFAULT: break; default: fprintf(stderr, "JNA: Unhandled arg conversion type %d\n", cb->conversion_flags[i]); break; } } } if (cb->rflag == CVT_STRUCTURE_BYVAL) { resp = alloca(sizeof(jobject)); } else if (cb->cif.rtype->size > cif->rtype->size) { resp = alloca(cb->cif.rtype->size); } #define FPTR(ENV,OFFSET) (*(void **)((char *)(*(ENV)) + OFFSET)) #define JNI_FN(X) ((void (*)(void))(X)) ffi_call(&cb->java_cif, JNI_FN(FPTR(env, cb->fptr_offset)), resp, args); if ((*env)->ExceptionCheck(env)) { jthrowable throwable = (*env)->ExceptionOccurred(env); (*env)->ExceptionClear(env); if (!handle_exception(env, self, throwable)) { fprintf(stderr, "JNA: error handling callback exception, continuing\n"); } if (cif->rtype->type != FFI_TYPE_VOID) { memset(oldresp, 0, cif->rtype->size); } } else switch(cb->rflag) { case CVT_INTEGER_TYPE: if (cb->cif.rtype->size > sizeof(ffi_arg)) { *(jlong *)oldresp = getIntegerTypeValue(env, *(void **)resp); } else { *(ffi_arg *)oldresp = (ffi_arg)getIntegerTypeValue(env, *(void **)resp); } break; case CVT_POINTER_TYPE: *(void **)resp = getPointerTypeAddress(env, *(void **)resp); break; case CVT_NATIVE_MAPPED: toNative(env, *(void **)resp, oldresp, cb->cif.rtype->size, JNI_TRUE, cb->encoding); break; case CVT_NATIVE_MAPPED_STRING: case CVT_NATIVE_MAPPED_WSTRING: // TODO: getNativeString rather than allocated memory fprintf(stderr, "JNA: Likely memory leak here\n"); toNative(env, *(void **)resp, oldresp, cb->cif.rtype->size, JNI_TRUE, cb->encoding); break; case CVT_POINTER: *(void **)resp = getNativeAddress(env, *(void **)resp); break; case CVT_STRING: *(void **)resp = getNativeString(env, *(void **)resp, JNI_FALSE); break; case CVT_WSTRING: *(void **)resp = getNativeString(env, *(void **)resp, JNI_TRUE); break; case CVT_STRUCTURE: writeStructure(env, *(void **)resp); *(void **)resp = getStructureAddress(env, *(void **)resp); break; case CVT_STRUCTURE_BYVAL: writeStructure(env, *(void **)resp); memcpy(oldresp, getStructureAddress(env, *(void **)resp), cb->cif.rtype->size); break; case CVT_CALLBACK: *(void **)resp = getCallbackAddress(env, *(void **)resp); break; case CVT_DEFAULT: break; default: fprintf(stderr, "JNA: Unhandled result conversion: %d\n", cb->rflag); break; } if (cb->conversion_flags) { for (i=0;i < cif->nargs;i++) { if (cb->conversion_flags[i] == CVT_STRUCTURE) { writeStructure(env, *(void **)cbargs[i]); } } } } else { jobject result; jobjectArray params = (*env)->NewObjectArray(env, cif->nargs, classObject, NULL); unsigned int i; for (i=0;i < cif->nargs;i++) { jobject arg = new_object(env, cb->arg_jtypes[i], cbargs[i], JNI_FALSE, cb->encoding); (*env)->SetObjectArrayElement(env, params, i, arg); } result = (*env)->CallObjectMethod(env, self, cb->methodID, params); if ((*env)->ExceptionCheck(env)) { jthrowable throwable = (*env)->ExceptionOccurred(env); (*env)->ExceptionClear(env); if (!handle_exception(env, self, throwable)) { fprintf(stderr, "JNA: error while handling callback exception, continuing\n"); } if (cif->rtype->type != FFI_TYPE_VOID) memset(resp, 0, cif->rtype->size); } else { extract_value(env, result, resp, cif->rtype->size, JNI_TRUE, cb->encoding); } } }
// Parse the compile unit. Find the function to which the instruction pointer // belongs and fill the info structure. static int parse_cu(uint8_t *p, uintptr_t rip, struct Rip_debug_info *info) { // read the compile unit header uint32_t length = extract_uint(p, 4, &p); uint8_t *e = p + length; extract_uint(p, 2, &p); // version uint32_t abbr_offset = extract_uint(p, 4, &p); extract_uint(p, 1, &p); // address size while (p < e) { uint64_t code; // skip null DIEs do { code = extract_uleb128(p, &p); } while (code == 0 && p < e); if (p >= e) break; // Search the abbreviation table for the declaration with this code uint8_t *abbr = find_abbrev_decl(pa2kva(abbr_offset), code); uint64_t tag = extract_uleb128(abbr, &abbr); extract_uint(abbr, 1, &abbr); // skip the has_children tag // read the attributes // we're interested only in several tags and attributes uintptr_t fn_lo = 0, fn_hi = 0; char *fn_name = NULL; for (;;) { uint64_t name = extract_uleb128(abbr, &abbr); uint64_t form = extract_uleb128(abbr, &abbr); union Attr_val val; if (name == 0 && form == 0) break; // extract the attribute value if (extract_value(p, form, &val, &p) == -1) return -1; if (tag == DW_TAG_compile_unit && name == DW_AT_name) { info->file_name = val.string; } else if (tag == DW_TAG_subprogram) { if (name == DW_AT_name) fn_name = val.string; else if (name == DW_AT_low_pc) fn_lo = val.number; else if (name == DW_AT_high_pc) fn_hi = val.number; } } if (tag == DW_TAG_subprogram) { // if the recently parsed tag was a function tag... if (rip >= fn_lo && rip <= fn_hi) { // ... and the instruction pointer belongs to this function if (fn_name) info->fn_name = fn_name; info->fn_offset = rip - fn_lo; } } } return 0; }
static void callback_invoke(JNIEnv* env, callback *cb, ffi_cif* cif, void *resp, void **cbargs) { jobject self; void *oldresp = resp; self = (*env)->NewLocalRef(env, cb->object); // Avoid calling back to a GC'd object if ((*env)->IsSameObject(env, self, NULL)) { fprintf(stderr, "JNA: callback object has been garbage collected\n"); if (cif->rtype->type != FFI_TYPE_VOID) { memset(resp, 0, cif->rtype->size); } } else if (cb->direct) { unsigned int i; void **args = alloca((cif->nargs + 3) * sizeof(void *)); args[0] = (void *)&env; args[1] = &self; args[2] = &cb->methodID; memcpy(&args[3], cbargs, cif->nargs * sizeof(void *)); if (cb->flags) { for (i=0;i < cif->nargs;i++) { switch(cb->flags[i]) { case CVT_INTEGER_TYPE: case CVT_POINTER_TYPE: case CVT_NATIVE_MAPPED: *((void **)args[i+3]) = fromNative(env, cb->arg_classes[i], cif->arg_types[i], args[i+3], JNI_FALSE); break; case CVT_POINTER: *((void **)args[i+3]) = newJavaPointer(env, *(void **)args[i+3]); break; case CVT_STRING: *((void **)args[i+3]) = newJavaString(env, *(void **)args[i+3], JNI_FALSE); break; case CVT_WSTRING: *((void **)args[i+3]) = newJavaWString(env, *(void **)args[i+3]); break; case CVT_STRUCTURE: *((void **)args[i+3]) = newJavaStructure(env, *(void **)args[i+3], cb->arg_classes[i], JNI_FALSE); break; case CVT_STRUCTURE_BYVAL: { void *ptr = args[i+3]; args[i+3] = alloca(sizeof(void *)); *((void **)args[i+3]) = newJavaStructure(env, ptr, cb->arg_classes[i], JNI_TRUE); } break; case CVT_CALLBACK: *((void **)args[i+3]) = newJavaCallback(env, *(void **)args[i+3], cb->arg_classes[i]); break; case CVT_FLOAT: { void *ptr = alloca(sizeof(double)); *(double *)ptr = *(float*)args[i+3]; args[i+3] = ptr; } break; } } } if (cb->rflag == CVT_STRUCTURE_BYVAL) { resp = alloca(sizeof(jobject)); } else if (cb->cif.rtype->size > cif->rtype->size) { resp = alloca(cb->cif.rtype->size); } ffi_call(&cb->java_cif, FFI_FN(cb->fptr), resp, args); if ((*env)->ExceptionCheck(env)) { jthrowable throwable = (*env)->ExceptionOccurred(env); (*env)->ExceptionClear(env); if (!handle_exception(env, self, throwable)) { fprintf(stderr, "JNA: error handling callback exception, continuing\n"); } if (cif->rtype->type != FFI_TYPE_VOID) memset(oldresp, 0, cif->rtype->size); } else switch(cb->rflag) { case CVT_INTEGER_TYPE: if (cb->cif.rtype->size > sizeof(ffi_arg)) { *(jlong *)oldresp = getIntegerTypeValue(env, *(void **)resp); } else { *(ffi_arg *)oldresp = (ffi_arg)getIntegerTypeValue(env, *(void **)resp); } break; case CVT_POINTER_TYPE: *(void **)resp = getPointerTypeAddress(env, *(void **)resp); break; case CVT_NATIVE_MAPPED: toNative(env, *(void **)resp, oldresp, cb->cif.rtype->size, JNI_TRUE); break; case CVT_POINTER: *(void **)resp = getNativeAddress(env, *(void **)resp); break; case CVT_STRING: *(void **)resp = getNativeString(env, *(void **)resp, JNI_FALSE); break; case CVT_WSTRING: *(void **)resp = getNativeString(env, *(void **)resp, JNI_TRUE); break; case CVT_STRUCTURE: writeStructure(env, *(void **)resp); *(void **)resp = getStructureAddress(env, *(void **)resp); break; case CVT_STRUCTURE_BYVAL: writeStructure(env, *(void **)resp); memcpy(oldresp, getStructureAddress(env, *(void **)resp), cb->cif.rtype->size); break; case CVT_CALLBACK: *(void **)resp = getCallbackAddress(env, *(void **)resp); break; default: break; } if (cb->flags) { for (i=0;i < cif->nargs;i++) { if (cb->flags[i] == CVT_STRUCTURE) { writeStructure(env, *(void **)args[i+3]); } } } } else { jobject result; jobjectArray array = (*env)->NewObjectArray(env, cif->nargs, classObject, NULL); unsigned int i; for (i=0;i < cif->nargs;i++) { jobject arg = new_object(env, cb->arg_jtypes[i], cbargs[i], JNI_FALSE); (*env)->SetObjectArrayElement(env, array, i, arg); } result = (*env)->CallObjectMethod(env, self, cb->methodID, array); if ((*env)->ExceptionCheck(env)) { jthrowable throwable = (*env)->ExceptionOccurred(env); (*env)->ExceptionClear(env); if (!handle_exception(env, self, throwable)) { fprintf(stderr, "JNA: error handling callback exception, continuing\n"); } if (cif->rtype->type != FFI_TYPE_VOID) memset(resp, 0, cif->rtype->size); } else { extract_value(env, result, resp, cif->rtype->size, JNI_TRUE); } } }
// Extract the attribute value of the given form static int extract_value(uint8_t *p, uint64_t form, union Attr_val *value, uint8_t **e) { switch (form) { case DW_FORM_data1: case DW_FORM_ref1: case DW_FORM_flag: value->number = extract_uint(p, 1, &p); break; case DW_FORM_data2: case DW_FORM_ref2: value->number = extract_uint(p, 2, &p); break; case DW_FORM_data4: case DW_FORM_ref4: value->number = extract_uint(p, 4, &p); break; case DW_FORM_data8: case DW_FORM_ref8: case DW_FORM_addr: case DW_FORM_ref_addr: value->number = extract_uint(p, 8, &p); break; case DW_FORM_sdata: value->number = extract_sleb128(p, &p); break; case DW_FORM_udata: case DW_FORM_ref_udata: value->number = extract_uleb128(p, &p); break; case DW_FORM_string: value->string = (char *) p; while (*p++) ; break; case DW_FORM_strp: value->string = (char *) pa2kva(extract_uint(p, 4, &p)); break; case DW_FORM_block1: value->block.length = extract_uint(p, 1, &p); value->block.data = (char *) p; p += value->block.length; break; case DW_FORM_block2: value->block.length = extract_uint(p, 2, &p); value->block.data = (char *) p; p += value->block.length; break; case DW_FORM_block4: value->block.length = extract_uint(p, 4, &p); value->block.data = (char *) p; p += value->block.length; break; case DW_FORM_block: value->block.length = extract_uleb128(p, &p); value->block.data = (char *) p; p += value->block.length; break; case DW_FORM_indirect: form = extract_uleb128(p, &p); return extract_value(p, form, value, &p); default: kprintf("uknown attribute form: %x\n", form); return -1; } if (e) *e = p; return 0; }
static bool parse_line(config_file_t *conf, struct config_entry_list *list, char *line) { char *comment = NULL; char *key = (char*)malloc(9); char *key_tmp = NULL; size_t cur_size = 8; size_t idx = 0; if (!key) return false; if (!line || !*line) { free(key); return false; } comment = strip_comment(line); /* Starting line with # and include includes config files. */ if ((comment == line) && (conf->include_depth < MAX_INCLUDE_DEPTH)) { comment++; if (strstr(comment, "include ") == comment) { add_sub_conf(conf, comment + strlen("include ")); free(key); return false; } } else if (conf->include_depth >= MAX_INCLUDE_DEPTH) { fprintf(stderr, "!!! #include depth exceeded for config. Might be a cycle.\n"); } /* Skips to first character. */ while (isspace((int)*line)) line++; while (isgraph((int)*line)) { if (idx == cur_size) { cur_size *= 2; key_tmp = (char*)realloc(key, cur_size + 1); if (!key_tmp) { free(key); return false; } key = key_tmp; } key[idx++] = *line++; } key[idx] = '\0'; list->key = key; list->key_hash = djb2_calculate(key); list->value = extract_value(line, true); if (!list->value) { list->key = NULL; free(key); return false; } return true; }
static bool parse_line(config_file_t *conf, struct config_entry_list *list, char *line, config_file_cb_t *cb) { char *comment = NULL; char *key_tmp = NULL; size_t cur_size = 8; size_t idx = 0; char *key = (char*)malloc(9); if (!key) return false; comment = strip_comment(line); /* Starting line with #include includes config files. */ if (comment == line) { comment++; if (strstr(comment, "include ") == comment) { char *line = comment + strlen("include "); char *path = extract_value(line, false); if (path) { if (conf->include_depth >= MAX_INCLUDE_DEPTH) fprintf(stderr, "!!! #include depth exceeded for config. Might be a cycle.\n"); else add_sub_conf(conf, path, cb); free(path); } goto error; } } /* Skips to first character. */ while (isspace((int)*line)) line++; while (isgraph((int)*line)) { if (idx == cur_size) { cur_size *= 2; key_tmp = (char*)realloc(key, cur_size + 1); if (!key_tmp) goto error; key = key_tmp; } key[idx++] = *line++; } key[idx] = '\0'; list->key = key; list->value = extract_value(line, true); if (!list->value) { list->key = NULL; goto error; } return true; error: free(key); return false; }
int main(int argc, char *argv[]) { int ix, jx, val; glkunix_startup_t startdata; setlocale (LC_CTYPE, ""); /* Test for compile-time errors. If one of these spouts off, you must edit glk.h and recompile. */ if (sizeof(glui32) != 4) { printf("Compile-time error: glui32 is not a 32-bit value. Please fix glk.h.\n"); return 1; } if ((glui32)(-1) < 0) { printf("Compile-time error: glui32 is not unsigned. Please fix glk.h.\n"); return 1; } /* Now some argument-parsing. This is probably going to hurt. */ startdata.argc = 0; startdata.argv = (char **)malloc(argc * sizeof(char *)); /* Copy in the program name. */ startdata.argv[startdata.argc] = argv[0]; startdata.argc++; for (ix=1; ix<argc && !errflag; ix++) { glkunix_argumentlist_t *argform; int inarglist = FALSE; char *cx; for (argform = glkunix_arguments; argform->argtype != glkunix_arg_End && !errflag; argform++) { if (argform->name[0] == '\0') { if (argv[ix][0] != '-') { startdata.argv[startdata.argc] = argv[ix]; startdata.argc++; inarglist = TRUE; } } else if ((argform->argtype == glkunix_arg_NumberValue) && !strncmp(argv[ix], argform->name, strlen(argform->name)) && (cx = argv[ix] + strlen(argform->name)) && (atoi(cx) != 0 || cx[0] == '0')) { startdata.argv[startdata.argc] = argv[ix]; startdata.argc++; inarglist = TRUE; } else if (!strcmp(argv[ix], argform->name)) { int numeat = 0; if (argform->argtype == glkunix_arg_ValueFollows) { if (ix+1 >= argc) { printf("%s: %s must be followed by a value\n", argv[0], argform->name); errflag = TRUE; break; } numeat = 2; } else if (argform->argtype == glkunix_arg_NoValue) { numeat = 1; } else if (argform->argtype == glkunix_arg_ValueCanFollow) { if (ix+1 < argc && argv[ix+1][0] != '-') { numeat = 2; } else { numeat = 1; } } else if (argform->argtype == glkunix_arg_NumberValue) { if (ix+1 >= argc || (atoi(argv[ix+1]) == 0 && argv[ix+1][0] != '0')) { printf("%s: %s must be followed by a number\n", argv[0], argform->name); errflag = TRUE; break; } numeat = 2; } else { errflag = TRUE; break; } for (jx=0; jx<numeat; jx++) { startdata.argv[startdata.argc] = argv[ix]; startdata.argc++; if (jx+1 < numeat) ix++; } inarglist = TRUE; break; } } if (inarglist || errflag) continue; if (argv[ix][0] != '-') { printf("%s: unwanted argument: %s\n", argv[0], argv[ix]); errflag = TRUE; break; } if (extract_value(argc, argv, "?", ex_Void, &ix, &val, FALSE)) errflag = TRUE; else if (extract_value(argc, argv, "help", ex_Void, &ix, &val, FALSE)) errflag = TRUE; else if (extract_value(argc, argv, "version", ex_Void, &ix, &val, FALSE)) pref_printversion = val; else if (extract_value(argc, argv, "v", ex_Void, &ix, &val, FALSE)) pref_printversion = val; else if (extract_value(argc, argv, "historylen", ex_Int, &ix, &val, 20)) pref_historylen = val; else if (extract_value(argc, argv, "hl", ex_Int, &ix, &val, 20)) pref_historylen = val; else if (extract_value(argc, argv, "width", ex_Int, &ix, &val, 80)) pref_screenwidth = val; else if (extract_value(argc, argv, "w", ex_Int, &ix, &val, 80)) pref_screenwidth = val; else if (extract_value(argc, argv, "height", ex_Int, &ix, &val, 24)) pref_screenheight = val; else if (extract_value(argc, argv, "h", ex_Int, &ix, &val, 24)) pref_screenheight = val; else if (extract_value(argc, argv, "ml", ex_Bool, &ix, &val, pref_messageline)) pref_messageline = val; else if (extract_value(argc, argv, "revgrid", ex_Bool, &ix, &val, pref_reverse_textgrids)) pref_reverse_textgrids = val; else if (extract_value(argc, argv, "border", ex_Bool, &ix, &val, pref_window_borders)) pref_window_borders = val; else if (extract_value(argc, argv, "defprompt", ex_Bool, &ix, &val, pref_prompt_defaults)) pref_prompt_defaults = val; #ifdef OPT_TIMED_INPUT else if (extract_value(argc, argv, "precise", ex_Bool, &ix, &val, pref_precise_timing)) pref_precise_timing = val; #endif /* !OPT_TIMED_INPUT */ else { printf("%s: unknown option: %s\n", argv[0], argv[ix]); errflag = TRUE; } } if (errflag) { printf("usage: %s [ options ... ]\n", argv[0]); if (glkunix_arguments[0].argtype != glkunix_arg_End) { glkunix_argumentlist_t *argform; printf("game options:\n"); for (argform = glkunix_arguments; argform->argtype != glkunix_arg_End; argform++) { printf(" %s\n", argform->desc); } } printf("library options:\n"); printf(" -width NUM: manual screen width (if not specified, will try to measure)\n"); printf(" -height NUM: manual screen height (ditto)\n"); printf(" -ml BOOL: use message line (default 'yes')\n"); printf(" -historylen NUM: length of command history (default 20)\n"); printf(" -revgrid BOOL: reverse text in grid (status) windows (default 'no')\n"); printf(" -border BOOL: draw borders between windows (default 'yes')\n"); printf(" -defprompt BOOL: provide defaults for file prompts (default 'yes')\n"); #ifdef OPT_TIMED_INPUT printf(" -precise BOOL: more precise timing for timed input (burns more CPU time) (default 'no')\n"); #endif /* !OPT_TIMED_INPUT */ printf(" -version: display Glk library version\n"); printf(" -help: display this list\n"); printf("NUM values can be any number. BOOL values can be 'yes' or 'no', or no value to toggle.\n"); return 1; } if (pref_printversion) { printf("GlkTerm, library version %s (%s).\n", LIBRARY_VERSION, LIBRARY_PORT); printf("For more information, see http://eblong.com/zarf/glk/index.html\n"); return 1; } /* We now start up curses. From now on, the program must exit through glk_exit(), so that endwin() is called. */ gli_setup_curses(); /* Initialize things. */ gli_initialize_misc(); gli_initialize_windows(); gli_initialize_events(); inittime = TRUE; if (!glkunix_startup_code(&startdata)) { glk_exit(); } inittime = FALSE; /* Call the program main entry point, and then exit. */ glk_main(); glk_exit(); /* glk_exit() doesn't return, but the compiler may kvetch if main() doesn't seem to return a value. */ return 0; }