char* ensure_trailing_slash(char* s) { if (str_has_suffix(s, "/") == 0) { return strdup(s); } else { return str_concat(s, "/"); } }
pkg_t * pkg_find(const char *name, unsigned int flags) { char **path = NULL; size_t count = 0, iter = 0; const char *env_path; pkg_t *pkg = NULL; FILE *f; /* name might actually be a filename. */ if (str_has_suffix(name, PKG_CONFIG_EXT)) { if ((f = fopen(name, "r")) != NULL) return pkg_new_from_file(name, f); } /* PKG_CONFIG_PATH has to take precedence */ env_path = getenv("PKG_CONFIG_PATH"); if (env_path) { count = path_split(env_path, &path); for (iter = 0; iter < count; iter++) { pkg = pkg_try_specific_path(path[iter], name, flags); if (pkg != NULL) goto out; } path_free(path, count); } env_path = get_pkgconfig_path(); if (!(flags & PKGF_ENV_ONLY)) { count = path_split(env_path, &path); for (iter = 0; iter < count; iter++) { pkg = pkg_try_specific_path(path[iter], name, flags); if (pkg != NULL) goto out; } } #ifdef _WIN32 /* support getting PKG_CONFIG_PATH from registry */ pkg = pkg_find_in_registry_key(HKEY_CURRENT_USER, name, flags); if (!pkg) pkg = pkg_find_in_registry_key(HKEY_LOCAL_MACHINE, name, flags); #endif out: path_free(path, count); return pkg; }
int main (int argc, char *argv[]) { Grf *grf; GrfError error; if (!argv[1]) usage (2); grf = grf_open (argv[1], &error); if (!grf) { fprintf (stderr, "Error: %s\n", grf_strerror (error)); return 1; } if (argv[2]) { unsigned long i; char *file = NULL; void *data; if (argv[3] && strcmp (argv[3], "--ends") == 0) { for (i = 0; i < grf->nfiles; i++) { if (str_has_suffix (grf->files[i].name, argv[2])) { file = grf->files[i].name; break; } } if (!file) { fprintf (stderr, "Error: %s\n", grf_strerror (GE_NOTFOUND)); return 1; } } else file = argv[2]; data = grf_get (grf, file, &i, &error); if (data) fwrite (data, 1, i, stdout); else { fprintf (stderr, "Error extracting %s: %s\n", file, grf_strerror (error)); return 1; } } else { unsigned long i; for (i = 0; i < grf->nfiles; i++) { printf ("%s\n", grf->files[i].name); } } grf_free (grf); return 0; }
void ScanSprites() { std::list<char *> list; elix::path::Children( oz::path, "sprite sheets/", &list, false, true, false ); for ( std::list<char *>::iterator it = list.begin(); it != list.end(); it++) { if ( str_has_suffix(*it, ".spt") ) { std::string file(*it); oz::ParseSPT(file); } } std::cout << "oz::scanSprites: " << oz::sprites.size() << std::endl; //for ( std::list<sprite>::iterator q = oz::sprites.begin(); q != oz::sprites.end(); q++) // std::cout << "sprite: " << (*q).parent << ":" << (*q).name << std::endl; }
JSValueRef function_load(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argc, const JSValueRef args[], JSValueRef *exception) { // TODO: implement fully if (argc == 1 && JSValueGetType(ctx, args[0]) == kJSTypeString) { char path[PATH_MAX]; JSStringRef path_str = JSValueToStringCopy(ctx, args[0], NULL); assert(JSStringGetLength(path_str) < PATH_MAX); JSStringGetUTF8CString(path_str, path, PATH_MAX); JSStringRelease(path_str); // debug_print_value("load", ctx, args[0]); time_t last_modified = 0; char *contents = NULL; char *loaded_path = strdup(path); bool developing = (config.num_src_paths == 1 && strcmp(config.src_paths[0].type, "src") == 0 && str_has_suffix(config.src_paths[0].path, "/planck-cljs/src/") == 0); if (!developing) { contents = bundle_get_contents(path); last_modified = 0; } // load from classpath if (contents == NULL) { for (int i = 0; i < config.num_src_paths; i++) { if (config.src_paths[i].blacklisted) { continue; } char *type = config.src_paths[i].type; char *location = config.src_paths[i].path; if (strcmp(type, "src") == 0) { char *full_path = str_concat(location, path); contents = get_contents(full_path, &last_modified); if (contents != NULL) { free(loaded_path); loaded_path = strdup(full_path); } free(full_path); } else if (strcmp(type, "jar") == 0) { struct stat file_stat; if (stat(location, &file_stat) == 0) { contents = get_contents_zip(location, path, &last_modified); } else { cljs_perror(location); config.src_paths[i].blacklisted = true; } } if (contents != NULL) { break; } } } // load from out/ if (contents == NULL) { if (config.out_path != NULL) { char *full_path = str_concat(config.out_path, path); contents = get_contents(full_path, &last_modified); free(full_path); } } if (developing && contents == NULL) { contents = bundle_get_contents(path); last_modified = 0; } if (contents != NULL) { JSStringRef contents_str = JSStringCreateWithUTF8CString(contents); free(contents); JSStringRef loaded_path_str = JSStringCreateWithUTF8CString(loaded_path); free(loaded_path); JSValueRef res[3]; res[0] = JSValueMakeString(ctx, contents_str); res[1] = JSValueMakeNumber(ctx, last_modified); res[2] = JSValueMakeString(ctx, loaded_path_str); return JSObjectMakeArray(ctx, 3, res, NULL); } } return JSValueMakeNull(ctx); }
int main(int argc, char **argv) { struct option long_options[] = { {"help", no_argument, NULL, 'h'}, {"legal", no_argument, NULL, 'l'}, {"verbose", no_argument, NULL, 'v'}, {"quiet", no_argument, NULL, 'q'}, {"repl", no_argument, NULL, 'r'}, {"static-fns", no_argument, NULL, 's'}, {"elide-asserts", no_argument, NULL, 'a'}, {"cache", required_argument, NULL, 'k'}, {"eval", required_argument, NULL, 'e'}, {"theme", required_argument, NULL, 't'}, {"classpath", required_argument, NULL, 'c'}, {"auto-cache", no_argument, NULL, 'K'}, {"init", required_argument, NULL, 'i'}, {"main", required_argument, NULL, 'm'}, // development options {"javascript", no_argument, NULL, 'j'}, {"out", required_argument, NULL, 'o'}, {0, 0, 0, 0} }; int opt, option_index; while ((opt = getopt_long(argc, argv, "h?lvrsak:je:t:c:o:Ki:qm:", long_options, &option_index)) != -1) { switch (opt) { case 'h': usage(argv[0]); exit(0); case 'l': legal(); return 0; case 'v': verbose = true; break; case 'q': quiet = true; break; case 'r': repl = true; break; case 's': static_fns = true; break; case 'a': elide_asserts = true; break; case 'k': cache_path = argv[optind - 1]; break; case 'K': cache_path = ".planck_cache"; { char *path_copy = strdup(cache_path); char *dir = dirname(path_copy); if (mkdir_p(dir) < 0) { fprintf(stderr, "Could not create %s: %s\n", cache_path, strerror(errno)); } free(path_copy); } break; case 'j': javascript = true; break; case 'e': num_scripts += 1; scripts = realloc(scripts, num_scripts * sizeof(struct script)); scripts[num_scripts - 1].type = "text"; scripts[num_scripts - 1].expression = true; scripts[num_scripts - 1].source = argv[optind - 1]; break; case 'i': num_scripts += 1; scripts = realloc(scripts, num_scripts * sizeof(struct script)); scripts[num_scripts - 1].type = "path"; scripts[num_scripts - 1].expression = false; scripts[num_scripts - 1].source = argv[optind - 1]; break; case 'm': main_ns_name = argv[optind - 1]; case 't': theme = argv[optind - 1]; break; case 'c': { char *classpath = argv[optind - 1]; char *source = strtok(classpath, ":"); while (source != NULL) { char *type = "src"; if (str_has_suffix(source, ".jar") == 0) { type = "jar"; } num_src_paths += 1; src_paths = realloc(src_paths, num_src_paths * sizeof(struct src_path)); src_paths[num_src_paths - 1].type = type; src_paths[num_src_paths - 1].path = strdup(source); source = strtok(NULL, ":"); } break; } case 'o': out_path = argv[optind - 1]; break; case '?': usage(argv[0]); exit(1); default: printf("unhandled argument: %c\n", opt); } } int num_rest_args = 0; char **rest_args = NULL; if (optind < argc) { num_rest_args = argc - optind; rest_args = malloc((argc - optind) * sizeof(char*)); int i = 0; while (optind < argc) { rest_args[i++] = argv[optind++]; } } if (num_scripts == 0 && main_ns_name == NULL && num_rest_args == 0) { repl = true; } if (main_ns_name != NULL && repl) { printf("Only one main-opt can be specified."); } JSGlobalContextRef ctx = JSGlobalContextCreate(NULL); JSStringRef nameRef = JSStringCreateWithUTF8CString("planck"); JSGlobalContextSetName(ctx, nameRef); evaluate_script(ctx, "var global = this;", "<init>"); register_global_function(ctx, "AMBLY_IMPORT_SCRIPT", function_import_script); bootstrap(ctx, out_path); register_global_function(ctx, "PLANCK_CONSOLE_LOG", function_console_log); register_global_function(ctx, "PLANCK_CONSOLE_ERROR", function_console_error); evaluate_script(ctx, "var console = {};"\ "console.log = PLANCK_CONSOLE_LOG;"\ "console.error = PLANCK_CONSOLE_ERROR;", "<init>"); evaluate_script(ctx, "var PLANCK_VERSION = \"" PLANCK_VERSION "\";", "<init>"); // require app namespaces evaluate_script(ctx, "goog.require('planck.repl');", "<init>"); // without this things won't work evaluate_script(ctx, "var window = global;", "<init>"); register_global_function(ctx, "PLANCK_READ_FILE", function_read_file); register_global_function(ctx, "PLANCK_LOAD", function_load); register_global_function(ctx, "PLANCK_LOAD_DEPS_CLJS_FILES", function_load_deps_cljs_files); register_global_function(ctx, "PLANCK_CACHE", function_cache); register_global_function(ctx, "PLANCK_EVAL", function_eval); register_global_function(ctx, "PLANCK_GET_TERM_SIZE", function_get_term_size); register_global_function(ctx, "PLANCK_PRINT_FN", function_print_fn); register_global_function(ctx, "PLANCK_PRINT_ERR_FN", function_print_err_fn); register_global_function(ctx, "PLANCK_SET_EXIT_VALUE", function_set_exit_value); is_tty = isatty(STDIN_FILENO) == 1; register_global_function(ctx, "PLANCK_RAW_READ_STDIN", function_raw_read_stdin); register_global_function(ctx, "PLANCK_RAW_WRITE_STDOUT", function_raw_write_stdout); register_global_function(ctx, "PLANCK_RAW_FLUSH_STDOUT", function_raw_flush_stdout); register_global_function(ctx, "PLANCK_RAW_WRITE_STDERR", function_raw_write_stderr); register_global_function(ctx, "PLANCK_RAW_FLUSH_STDERR", function_raw_flush_stderr); { JSValueRef arguments[num_rest_args]; for (int i = 0; i < num_rest_args; i++) { arguments[i] = c_string_to_value(ctx, rest_args[i]); } JSValueRef args_ref = JSObjectMakeArray(ctx, num_rest_args, arguments, NULL); JSValueRef global_obj = JSContextGetGlobalObject(ctx); JSStringRef prop = JSStringCreateWithUTF8CString("PLANCK_INITIAL_COMMAND_LINE_ARGS"); JSObjectSetProperty(ctx, JSValueToObject(ctx, global_obj, NULL), prop, args_ref, kJSPropertyAttributeNone, NULL); JSStringRelease(prop); } evaluate_script(ctx, "cljs.core.set_print_fn_BANG_.call(null,PLANCK_PRINT_FN);", "<init>"); evaluate_script(ctx, "cljs.core.set_print_err_fn_BANG_.call(null,PLANCK_PRINT_ERR_FN);", "<init>"); char *elide_script = str_concat("cljs.core._STAR_assert_STAR_ = ", elide_asserts ? "false" : "true"); evaluate_script(ctx, elide_script, "<init>"); free(elide_script); { JSValueRef arguments[4]; arguments[0] = JSValueMakeBoolean(ctx, repl); arguments[1] = JSValueMakeBoolean(ctx, verbose); JSValueRef cache_path_ref = NULL; if (cache_path != NULL) { JSStringRef cache_path_str = JSStringCreateWithUTF8CString(cache_path); cache_path_ref = JSValueMakeString(ctx, cache_path_str); } arguments[2] = cache_path_ref; arguments[3] = JSValueMakeBoolean(ctx, static_fns); JSValueRef ex = NULL; JSObjectCallAsFunction(ctx, get_function(ctx, "planck.repl", "init"), JSContextGetGlobalObject(ctx), 4, arguments, &ex); debug_print_value("planck.repl/init", ctx, ex); } if (repl) { evaluate_source(ctx, "text", "(require '[planck.repl :refer-macros [apropos dir find-doc doc source pst]])", true, false, "cljs.user", "dumb"); } evaluate_script(ctx, "goog.provide('cljs.user');", "<init>"); evaluate_script(ctx, "goog.require('cljs.core');", "<init>"); evaluate_script(ctx, "cljs.core._STAR_assert_STAR_ = true;", "<init>"); // Process init arguments for (int i = 0; i < num_scripts; i++) { // TODO: exit if not successfull evaluate_source(ctx, scripts[i].type, scripts[i].source, scripts[i].expression, false, NULL, theme); } // Process main arguments if (main_ns_name != NULL) { run_main_in_ns(ctx, main_ns_name, num_rest_args, rest_args); } else if (!repl && num_rest_args > 0) { char *path = rest_args[0]; struct script script; if (strcmp(path, "-") == 0) { char *source = read_all(stdin); script.type = "text"; script.source = source; script.expression = false; } else { script.type = "path"; script.source = path; script.expression = false; } evaluate_source(ctx, script.type, script.source, script.expression, false, NULL, theme); } else if (repl) { if (!quiet) { banner(); } char *home = getenv("HOME"); char *history_path = NULL; if (home != NULL) { char history_name[] = ".planck_history"; int len = strlen(home) + strlen(history_name) + 2; history_path = malloc(len * sizeof(char)); snprintf(history_path, len, "%s/%s", home, history_name); linenoiseHistoryLoad(history_path); } char *prompt = javascript ? " > " : " => "; char *line; while ((line = linenoise(prompt)) != NULL) { if (javascript) { JSValueRef res = evaluate_script(ctx, line, "<stdin>"); print_value("", ctx, res); } else { evaluate_source(ctx, "text", line, true, true, "cljs.user", theme); } linenoiseHistoryAdd(line); if (history_path != NULL) { linenoiseHistorySave(history_path); } free(line); } } return exit_value; }
void PluginRegistry::Impl::get_plugins_from_dir (PluginList& list, std::string const& dir) { list.clear (); #if defined(VISUAL_OS_WIN32) std::string pattern = dir + "/*"; WIN32_FIND_DATA file_data; HANDLE hList = FindFirstFile (pattern.c_str (), &file_data); if (hList == INVALID_HANDLE_VALUE) { FindClose (hList); return; } bool finished = false; while (!finished) { if (!(file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { std::string full_path = dir + "/" + file_data.cFileName; if (str_has_suffix (full_path, ".dll")) { PluginRef* ref = load_plugin_ref (full_path); if (ref) { list.push_back (ref); } } } if (!FindNextFile (hList, &file_data)) { if (GetLastError () == ERROR_NO_MORE_FILES) { finished = true; } } } FindClose (hList); #else // NOTE: This typecast is needed for glibc versions that define // alphasort() as taking const void * arguments typedef int (*ScandirCompareFunc) (const struct dirent **, const struct dirent **); struct dirent **namelist; int n = scandir (dir.c_str (), &namelist, NULL, ScandirCompareFunc (alphasort)); if (n < 0) return; // First two entries are '.' and '..' visual_mem_free (namelist[0]); visual_mem_free (namelist[1]); for (int i = 2; i < n; i++) { std::string full_path = dir + "/" + namelist[i]->d_name; if (str_has_suffix (full_path, ".so")) { PluginRef* ref = load_plugin_ref (full_path); if (ref) { list.push_back (ref); } } visual_mem_free (namelist[i]); } visual_mem_free (namelist); #endif }
int main(int argc, char **argv) { config.verbose = false; config.quiet = false; config.repl = false; config.javascript = false; config.static_fns = false; config.elide_asserts = false; config.cache_path = NULL; config.theme = "light"; config.dumb_terminal = false; config.out_path = NULL; config.num_src_paths = 0; config.src_paths = NULL; config.num_scripts = 0; config.scripts = NULL; config.main_ns_name = NULL; struct option long_options[] = { {"help", no_argument, NULL, 'h'}, {"legal", no_argument, NULL, 'l'}, {"verbose", no_argument, NULL, 'v'}, {"quiet", no_argument, NULL, 'q'}, {"repl", no_argument, NULL, 'r'}, {"static-fns", no_argument, NULL, 's'}, {"elide-asserts", no_argument, NULL, 'a'}, {"cache", required_argument, NULL, 'k'}, {"eval", required_argument, NULL, 'e'}, {"theme", required_argument, NULL, 't'}, {"dumb-terminal", no_argument, NULL, 'd'}, {"classpath", required_argument, NULL, 'c'}, {"auto-cache", no_argument, NULL, 'K'}, {"init", required_argument, NULL, 'i'}, {"main", required_argument, NULL, 'm'}, // development options {"javascript", no_argument, NULL, 'j'}, {"out", required_argument, NULL, 'o'}, {0, 0, 0, 0} }; int opt, option_index; while ((opt = getopt_long(argc, argv, "h?lvrsak:je:t:dc:o:Ki:qm:", long_options, &option_index)) != -1) { switch (opt) { case 'h': usage(argv[0]); exit(0); case 'l': legal(); return 0; case 'v': config.verbose = true; break; case 'q': config.quiet = true; break; case 'r': config.repl = true; break; case 's': config.static_fns = true; break; case 'a': config.elide_asserts = true; break; case 'k': config.cache_path = argv[optind - 1]; break; case 'K': config.cache_path = ".planck_cache"; { char *path_copy = strdup(config.cache_path); char *dir = dirname(path_copy); if (mkdir_p(dir) < 0) { fprintf(stderr, "Could not create %s: %s\n", config.cache_path, strerror(errno)); } free(path_copy); } break; case 'j': config.javascript = true; break; case 'e': config.num_scripts += 1; config.scripts = realloc(config.scripts, config.num_scripts * sizeof(struct script)); config.scripts[config.num_scripts - 1].type = "text"; config.scripts[config.num_scripts - 1].expression = true; config.scripts[config.num_scripts - 1].source = argv[optind - 1]; break; case 'i': config.num_scripts += 1; config.scripts = realloc(config.scripts, config.num_scripts * sizeof(struct script)); config.scripts[config.num_scripts - 1].type = "path"; config.scripts[config.num_scripts - 1].expression = false; config.scripts[config.num_scripts - 1].source = argv[optind - 1]; break; case 'm': config.main_ns_name = argv[optind - 1]; break; case 't': config.theme = argv[optind - 1]; break; case 'd': config.dumb_terminal = true; break; case 'c': { char *classpath = argv[optind - 1]; char *source = strtok(classpath, ":"); while (source != NULL) { char *type = "src"; if (str_has_suffix(source, ".jar") == 0) { type = "jar"; } config.num_src_paths += 1; config.src_paths = realloc(config.src_paths, config.num_src_paths * sizeof(struct src_path)); config.src_paths[config.num_src_paths - 1].type = type; config.src_paths[config.num_src_paths - 1].path = strcmp(type, "jar") == 0 ? strdup(source) : ensure_trailing_slash(source); source = strtok(NULL, ":"); } break; } case 'o': config.out_path = ensure_trailing_slash(argv[optind - 1]); break; case '?': usage(argv[0]); exit(1); default: printf("unhandled argument: %c\n", opt); } } if (config.dumb_terminal) { config.theme = "dumb"; } config.num_rest_args = 0; config.rest_args = NULL; if (optind < argc) { config.num_rest_args = argc - optind; config.rest_args = malloc((argc - optind) * sizeof(char*)); int i = 0; while (optind < argc) { config.rest_args[i++] = argv[optind++]; } } if (config.num_scripts == 0 && config.main_ns_name == NULL && config.num_rest_args == 0) { config.repl = true; } if (config.main_ns_name != NULL && config.repl) { printf("Only one main-opt can be specified.\n"); exit(1); } config.is_tty = isatty(STDIN_FILENO) == 1; JSGlobalContextRef ctx = JSGlobalContextCreate(NULL); global_ctx = ctx; cljs_engine_init(ctx); // Process init arguments for (int i = 0; i < config.num_scripts; i++) { // TODO: exit if not successfull struct script script = config.scripts[i]; evaluate_source(ctx, script.type, script.source, script.expression, false, NULL, config.theme, true); } // Process main arguments if (config.main_ns_name != NULL) { run_main_in_ns(ctx, config.main_ns_name, config.num_rest_args, config.rest_args); } else if (!config.repl && config.num_rest_args > 0) { char *path = config.rest_args[0]; struct script script; if (strcmp(path, "-") == 0) { char *source = read_all(stdin); script.type = "text"; script.source = source; script.expression = false; } else { script.type = "path"; script.source = path; script.expression = false; } evaluate_source(ctx, script.type, script.source, script.expression, false, NULL, config.theme, true); } else if (config.repl) { if (!config.quiet) { banner(); } run_repl(ctx); } return exit_value; }