static bool build_ios_export(export_config *conf) { char *zipname = get_export_zip_name(conf, "ios"); if (am_file_exists(zipname)) am_delete_file(zipname); char *binpath = get_bin_path(conf, "ios"); if (binpath == NULL) return true; if (!create_ios_info_plist(binpath, AM_TMP_DIR AM_PATH_SEP_STR "Info.plist", conf)) return false; if (!create_ios_pkginfo(AM_TMP_DIR AM_PATH_SEP_STR "PkgInfo")) return false; if (!create_ios_icon_files(AM_TMP_DIR, conf)) return false; if (!create_ios_launch_images(AM_TMP_DIR, conf)) return false; const char *name = conf->shortname; char *appdir = am_format("Payload/%s.app", name); bool ok = add_files_to_dist(zipname, am_opt_data_dir, "*.txt", appdir, NULL, NULL, true, false, ZIP_PLATFORM_UNIX) && add_files_to_dist(zipname, binpath, "amulet_license.txt", appdir, NULL, NULL, true, false, ZIP_PLATFORM_UNIX) && add_files_to_dist(zipname, binpath, "amulet", appdir, name, NULL, true, true, ZIP_PLATFORM_UNIX) && add_files_to_dist(zipname, ".", conf->pakfile, appdir, "data.pak", NULL, false, false, ZIP_PLATFORM_UNIX) && add_files_to_dist(zipname, AM_TMP_DIR, "Info.plist", appdir, NULL, NULL, true, false, ZIP_PLATFORM_UNIX) && add_files_to_dist(zipname, AM_TMP_DIR, "PkgInfo", appdir, NULL, NULL, true, false, ZIP_PLATFORM_UNIX) && add_files_to_dist(zipname, AM_TMP_DIR, "Icon.png", appdir, NULL, NULL, false, false, ZIP_PLATFORM_UNIX) && add_files_to_dist(zipname, AM_TMP_DIR, "*****@*****.**", appdir, NULL, NULL, false, false, ZIP_PLATFORM_UNIX) && add_files_to_dist(zipname, AM_TMP_DIR, "icon40.png", appdir, NULL, NULL, false, false, ZIP_PLATFORM_UNIX) && add_files_to_dist(zipname, AM_TMP_DIR, "icon57.png", appdir, NULL, NULL, false, false, ZIP_PLATFORM_UNIX) && add_files_to_dist(zipname, AM_TMP_DIR, "icon72.png", appdir, NULL, NULL, false, false, ZIP_PLATFORM_UNIX) && add_files_to_dist(zipname, AM_TMP_DIR, "icon76.png", appdir, NULL, NULL, false, false, ZIP_PLATFORM_UNIX) && add_files_to_dist(zipname, AM_TMP_DIR, "icon80.png", appdir, NULL, NULL, false, false, ZIP_PLATFORM_UNIX) && add_files_to_dist(zipname, AM_TMP_DIR, "icon114.png", appdir, NULL, NULL, false, false, ZIP_PLATFORM_UNIX) && add_files_to_dist(zipname, AM_TMP_DIR, "icon120.png", appdir, NULL, NULL, false, false, ZIP_PLATFORM_UNIX) && add_files_to_dist(zipname, AM_TMP_DIR, "icon144.png", appdir, NULL, NULL, false, false, ZIP_PLATFORM_UNIX) && add_files_to_dist(zipname, AM_TMP_DIR, "icon152.png", appdir, NULL, NULL, false, false, ZIP_PLATFORM_UNIX) && add_files_to_dist(zipname, AM_TMP_DIR, "icon180.png", appdir, NULL, NULL, false, false, ZIP_PLATFORM_UNIX) && add_files_to_dist(zipname, AM_TMP_DIR, "iTunesArtwork", appdir, NULL, NULL, false, false, ZIP_PLATFORM_UNIX) && add_files_to_dist(zipname, AM_TMP_DIR, "Default.png", appdir, NULL, NULL, false, false, ZIP_PLATFORM_UNIX) && add_files_to_dist(zipname, AM_TMP_DIR, "*****@*****.**", appdir, NULL, NULL, false, false, ZIP_PLATFORM_UNIX) && add_files_to_dist(zipname, AM_TMP_DIR, "*****@*****.**", appdir, NULL, NULL, false, false, ZIP_PLATFORM_UNIX) && add_files_to_dist(zipname, AM_TMP_DIR, "*****@*****.**", appdir, NULL, NULL, false, false, ZIP_PLATFORM_UNIX) && add_files_to_dist(zipname, AM_TMP_DIR, "*****@*****.**", appdir, NULL, NULL, false, false, ZIP_PLATFORM_UNIX) && add_files_to_dist(zipname, AM_TMP_DIR, "Default-Landscape@2x~ipad.png", appdir, NULL, NULL, false, false, ZIP_PLATFORM_UNIX) && add_files_to_dist(zipname, AM_TMP_DIR, "Default-Landscape~ipad.png", appdir, NULL, NULL, false, false, ZIP_PLATFORM_UNIX) && add_files_to_dist(zipname, AM_TMP_DIR, "Default-Portrait@2x~ipad.png", appdir, NULL, NULL, false, false, ZIP_PLATFORM_UNIX) && add_files_to_dist(zipname, AM_TMP_DIR, "Default-Portrait~ipad.png", appdir, NULL, NULL, false, false, ZIP_PLATFORM_UNIX) && create_ios_lproj_dirs(zipname, appdir, conf) && true; free(appdir); am_delete_file(AM_TMP_DIR AM_PATH_SEP_STR "Info.plist"); am_delete_file(AM_TMP_DIR AM_PATH_SEP_STR "PkgInfo"); printf("Generated %s\n", zipname); free(zipname); free(binpath); return ok; }
static char *get_bin_path(export_config *conf, const char *platform) { const char *builds_path = conf->basepath; if (strcmp(conf->basepath, "/usr/local/bin/") == 0) { builds_path = "/usr/local/share/amulet/"; } else if (strcmp(conf->basepath, "/usr/bin/") == 0) { builds_path = "/usr/share/amulet/"; } char *bin_path = am_format("%sbuilds/%s/%s/%s/bin", builds_path, platform, conf->luavm, conf->grade); if (!am_file_exists(bin_path)) { free(bin_path); return NULL; } return bin_path; }
static char *get_bin_path(export_config *conf, const char *platform) { const char *builds_path = conf->basepath; if (strcmp(conf->basepath, "/usr/local/bin/") == 0) { builds_path = "/usr/local/share/amulet/"; } else if (strcmp(conf->basepath, "/usr/bin/") == 0) { builds_path = "/usr/share/amulet/"; } char *bin_path = am_format("%sbuilds/%s/%s/%s/bin", builds_path, platform, conf->luavm, conf->grade); if (!am_file_exists(bin_path)) { fprintf(stderr, "Error: export configuration %s/%s/%s not available in your installation.\n", platform, conf->luavm, conf->grade); fprintf(stderr, "(the path %s does not exist)\n", bin_path); free(bin_path); return NULL; } return bin_path; }
static bool build_html_export(export_config *conf) { char *zipname = get_export_zip_name(conf, "html"); if (am_file_exists(zipname)) am_delete_file(zipname); char *binpath = get_bin_path(conf, "html"); if (binpath == NULL) return false; const char *name = conf->appshortname; bool ok = add_files_to_zip_renamed(zipname, am_opt_data_dir, "*.txt", name, NULL, NULL, true, false, ZIP_PLATFORM_UNIX) && add_files_to_zip_renamed(zipname, binpath, "amulet_license.txt", name, NULL, NULL, true, false, ZIP_PLATFORM_UNIX) && add_files_to_zip_renamed(zipname, binpath, "amulet.js", name, NULL, NULL, true, false, ZIP_PLATFORM_UNIX) && add_files_to_zip_renamed(zipname, binpath, "player.html", name, "index.html", NULL, true, false, ZIP_PLATFORM_UNIX) && add_files_to_zip_renamed(zipname, ".", conf->pakfile, name, "data.pak", NULL, false, false, ZIP_PLATFORM_UNIX) && true; printf("%s\n", zipname); free(zipname); free(binpath); return ok; }
static bool build_windows_export(export_config *conf) { char *zipname = get_export_zip_name(conf, "windows"); if (am_file_exists(zipname)) am_delete_file(zipname); char *binpath = get_bin_path(conf, "msvc32"); if (binpath == NULL) return true; const char *name = conf->shortname; bool ok = add_files_to_dist(zipname, am_opt_data_dir, "*.txt", name, NULL, NULL, true, false, ZIP_PLATFORM_DOS) && add_files_to_dist(zipname, binpath, "amulet_license.txt", name, NULL, NULL, true, false, ZIP_PLATFORM_DOS) && add_files_to_dist(zipname, binpath, "amulet.exe", name, name, ".exe", true, true, ZIP_PLATFORM_DOS) && add_files_to_dist(zipname, binpath, "*.dll", name, NULL, NULL, true, true, ZIP_PLATFORM_DOS) && add_files_to_dist(zipname, ".", conf->pakfile, name, "data.pak", NULL, false, false, ZIP_PLATFORM_DOS) && true; printf("Generated %s\n", zipname); free(zipname); free(binpath); return ok; }
static bool build_mac_export(export_config *conf) { char *zipname = get_export_zip_name(conf, "mac"); if (am_file_exists(zipname)) am_delete_file(zipname); char *binpath = get_bin_path(conf, "osx"); if (binpath == NULL) return false; if (!create_mac_info_plist(AM_TMP_DIR "/Info.plist", conf)) return false; const char *name = conf->appshortname; bool ok = add_files_to_zip_renamed(zipname, am_opt_data_dir, "*.txt", name, NULL, NULL, true, false, ZIP_PLATFORM_UNIX) && add_files_to_zip_renamed(zipname, binpath, "amulet_license.txt", name, NULL, NULL, true, false, ZIP_PLATFORM_UNIX) && add_files_to_zip_renamed(zipname, binpath, "amulet", name, name, ".app/Contents/MacOS/amulet", true, true, ZIP_PLATFORM_UNIX) && add_files_to_zip_renamed(zipname, ".", conf->pakfile, name, name, ".app/Contents/Resources/data.pak", false, false, ZIP_PLATFORM_UNIX) && add_files_to_zip_renamed(zipname, AM_TMP_DIR, "Info.plist", name, name, ".app/Contents/Info.plist", true, false, ZIP_PLATFORM_UNIX) && true; am_delete_file(AM_TMP_DIR "/Info.plist"); printf("%s\n", zipname); free(zipname); free(binpath); return ok; }
static bool build_linux_export(export_config *conf) { char *zipname = get_export_zip_name(conf, "linux"); if (am_file_exists(zipname)) am_delete_file(zipname); char *binpath64 = get_bin_path(conf, "linux64"); if (binpath64 == NULL) return false; char *binpath32 = get_bin_path(conf, "linux32"); if (binpath32 == NULL) return false; const char *name = conf->appshortname; bool ok = add_files_to_zip_renamed(zipname, am_opt_data_dir, "*.txt", name, NULL, NULL, true, false, ZIP_PLATFORM_UNIX) && add_files_to_zip_renamed(zipname, binpath64, "amulet_license.txt", name, NULL, NULL, true, false, ZIP_PLATFORM_UNIX) && add_files_to_zip_renamed(zipname, binpath64, "amulet", name, name, ".x86_64", true, true, ZIP_PLATFORM_UNIX) && add_files_to_zip_renamed(zipname, binpath32, "amulet", name, name, ".i686", true, true, ZIP_PLATFORM_UNIX) && add_files_to_zip_renamed(zipname, ".", conf->pakfile, name, "data.pak", NULL, false, false, ZIP_PLATFORM_UNIX) && true; printf("%s\n", zipname); free(zipname); free(binpath32); free(binpath64); return ok; }
static bool file_exists(const char *filename) { char *path = am_format("%s%c%s", am_opt_data_dir, AM_PATH_SEP, filename); bool exists = am_file_exists(path); free(path); return exists; }
static bool build_data_pak(export_config *conf) { if (am_file_exists(conf->pakfile)) { am_delete_file(conf->pakfile); } return build_data_pak_2(0, am_opt_data_dir, am_opt_data_dir, conf->pakfile); }
int configure_step(int height, int width) { FIELD * field[6]; FORM *form; int ch, page = 0; int error = 0; char emsg[64]; url_t u, ua; field[0] = new_field(1, width - 20, 8, 10, 0, 0); field[1] = new_field(1, width - 20, 8, 10, 0, 0); field[2] = new_field(1, (width - 20) / 2, 8, 22, 0, 0); field[3] = new_field(1, (width - 20) / 2, 10, 22, 0, 0); field[4] = new_field(1, width - 20, 8, 10, 0, 0); field[5] = NULL; set_field_back(field[0], A_UNDERLINE); field_opts_off(field[0], O_AUTOSKIP); set_field_back(field[1], A_UNDERLINE); field_opts_off(field[1], O_AUTOSKIP); set_field_back(field[2], A_UNDERLINE); field_opts_off(field[2], O_AUTOSKIP); set_field_back(field[3], A_UNDERLINE); field_opts_off(field[3], O_AUTOSKIP); field_opts_off(field[3], O_PUBLIC); //agent password field_opts_off(field[4], O_AUTOSKIP); set_field_back(field[4], A_UNDERLINE); set_new_page(field[1], TRUE); set_new_page(field[2], TRUE); set_new_page(field[4], TRUE); form = new_form(field); clear(); post_form(form); refresh(); step(0, height, width); refresh(); while ((ch = getch()) != KEY_F(4)) { menu_continue: switch (ch) { case KEY_F(2): page--; if (page < 0) { page = 0; } else { form_driver(form, REQ_PREV_PAGE); step(page, height, width); } break; case KEY_F(3): //validation error = 0; emsg[0] = '\0'; form_driver(form, REQ_VALIDATION); switch (page) { case 0://openam url validate { strcpy(openam_url, field_buffer(field[0], 0)); am_trim(openam_url); u = URL(openam_url); if (u.error == 0) { if (validate_am_host(&u) != 0) { error = 1; sprintf(emsg, "Error validating OpenAM URL"); } } else { error = 1; sprintf(emsg, "Invalid URL value"); } } break; case 1://agent url strcpy(agent_url, field_buffer(field[1], 0)); am_trim(agent_url); ua = URL(agent_url); if (ua.error != 0) { error = 1; sprintf(emsg, "Invalid URL value"); } break; case 2://agent user/pass snprintf(agent_id, sizeof (agent_id), "%s", field_buffer(field[2], 0)); snprintf(agent_pass, sizeof (agent_pass), "%s", field_buffer(field[3], 0)); am_trim(agent_id); am_trim(agent_pass); if (agent_id == NULL || agent_id[0] == '\0' || agent_pass == NULL || agent_pass[0] == '\0') { error = 1; sprintf(emsg, "Values must not be empty"); } else { if (u.error == 0) { if (validate_agent(&u, agent_id, agent_pass) != 0) { error = 1; sprintf(emsg, PROFILE_NOT_FOUND); } } else { error = 1; sprintf(emsg, "Invalid OpenAM URL value"); } } break; case 3://web container path { char *t = NULL; strcpy(web_conf_path, field_buffer(field[4], 0)); am_trim(web_conf_path); if (web_conf_path == NULL || web_conf_path[0] == '\0') { error = 1; sprintf(emsg, "Value must not be empty"); } else { asprintf(&t, "%s/libvmod_am.so", web_conf_path); if (am_whitespace(web_conf_path) > 0) { LOG("configure_step() error - path to Varnish modules directory must not contain spaces %s", web_conf_path); error = 1; sprintf(emsg, "Error reading Varnish modules directory"); } else if (am_file_writeable(web_conf_path) == 0) { LOG("configure_step() error - vmod directory is not accessible %s", web_conf_path); error = 1; sprintf(emsg, "Error opening Varnish modules directory"); } else if (am_file_exists(t) == 1) { LOG("configure_step() vmod directory already contains agent library %s", t); error = 1; sprintf(emsg, "This Varnish instance is already configured"); } am_free(t); } } break; } if (error == 0) { move(13, 10); clrtoeol(); page++; if (page > 3) { goto cfg_done; } form_driver(form, REQ_NEXT_PAGE); step(page, height, width); } else { move(13, 10); clrtoeol(); attron(A_BOLD); attron(COLOR_PAIR(1)); mvprintw(13, 10, emsg); attroff(COLOR_PAIR(1)); attroff(A_BOLD); form_driver(form, REQ_END_LINE); } break; case 0x08: case 0x7f: case KEY_DC: case KEY_BACKSPACE: form_driver(form, REQ_DEL_PREV); form_driver(form, REQ_CLR_EOF); break; case KEY_DOWN: case 10://ENTER case 9://TAB if ((ch == 9 || ch == 10) && (page == 0 || page == 1 || page == 3)) { /*allow enter to accept-validate single-line form fields*/ ch = KEY_F(3); refresh(); goto menu_continue; } form_driver(form, REQ_NEXT_FIELD); form_driver(form, REQ_END_LINE); break; case KEY_UP: form_driver(form, REQ_PREV_FIELD); form_driver(form, REQ_END_LINE); break; default: form_driver(form, ch); break; } refresh(); } cfg_done: unpost_form(form); free_form(form); free_field(field[0]); free_field(field[1]); free_field(field[2]); free_field(field[3]); clear(); refresh(); if (page > 3) { curs_set(0); create_varnish_instance(ua); step(-1, height, width); while ((ch = getch()) != KEY_F(4)) { } curs_set(1); clear(); } return ch; }
int main(int argc, char** argv) { WINDOW *lic = NULL; int x, y, mr; int opt = 0, cnt = 0; char remname[256]; char *licpath = NULL; FILE *fout = NULL; char *version = NULL; if (am_bin_path(instance_path, sizeof (instance_path)) == -1) { return 0; } else { if (am_whitespace(instance_path) > 0) { fprintf(stderr, "Whitespace in path: %s \n", instance_path); return 0; } snprintf(log_path, sizeof (log_path), "%s/../logs/install_%s.log", instance_path, TIMESTAMPLONG); snprintf(lic_path, sizeof (lic_path), "%s/../legal-notices/license.txt", instance_path); fout = fopen(log_path, "a"); if (fout != NULL) { fprintf(fout, "\n\n==================================================="); print_version(fout, NULL); fprintf(fout, "\r\n"); fclose(fout); } strcat(instance_path, "../instances"); snprintf(int_conf_path, sizeof (int_conf_path), "%s/.agents", instance_path); } net_initialize(); /* cli mode */ if (argc > 1) { while ((opt = opts(argc, argv, "e:vlxr:o:a:i:p:c:")) != -1) switch (opt) { case 'e': { char encryptpasswd[1024] = ""; char origpasswd[1024] = ""; char *keystr; char bkeystr[1024] = ""; strcpy(origpasswd, oarg); am_trim(origpasswd); keystr = am_random_key(); memset(bkeystr, 0, 1024); am_b64encode(keystr, bkeystr); encrypt_base64(origpasswd, encryptpasswd, bkeystr); fprintf(stderr, "\nEncrypted password:\n%s\n\nKey:\n%s\n\n", encryptpasswd, bkeystr); net_shutdown(); return (EXIT_SUCCESS); } break; case 'l': { fprintf(stderr, "Agent instances:\n"); int n; am_conf_p inst = NULL, temp; if ((n = am_read_instances(instance_path, &inst)) > 0) { temp = inst; while (temp != NULL) { fprintf(stderr, "%s\n", temp->name); temp = temp->next; } am_free(inst); } else fprintf(stderr, "There are no agent instances registered.\n"); net_shutdown(); return (EXIT_SUCCESS); } break; case 'v': { print_version(stdout, NULL); net_shutdown(); return (EXIT_SUCCESS); } break; case 'r': { fprintf(stderr, "Removing \"%s\" instance...\n", oarg); if (remove_instance(oarg)) { fprintf(stderr, "Instance \"%s\" removed.\n", oarg); } else fprintf(stderr, "Error removing \"%s\" instance.\n", oarg); net_shutdown(); return (EXIT_SUCCESS); } break; case 'o': sprintf(openam_url, oarg); cnt = 1; break; case 'a': sprintf(agent_url, oarg); cnt = 1; break; case 'i': sprintf(agent_id, oarg); cnt = 1; break; case 'p': sprintf(agent_pass_file, oarg); cnt = 1; break; case 'c': sprintf(web_conf_path, oarg); cnt = 1; break; case 'x': { asprintf(&licpath, "%s/.license", instance_path); if (licpath) { am_setup_conf(licpath, NULL); free(licpath); licpath = NULL; } net_shutdown(); return (EXIT_SUCCESS); } break; case '?': if (oopt == 'e' || oopt == 'r' || oopt == 'o' || oopt == 'a' || oopt == 'i' || oopt == 'p' || oopt == 'c') fprintf(stderr, "\nError: option -%c requires an argument.\n", oopt); else if (isprint(oopt)) fprintf(stderr, "\nError: unknown option `-%c'.\n", oopt); else fprintf(stderr, "\nnError: unknown option character `\\x%x'.\n", oopt); opt = -1; default: opt = -1; } if (cnt == 1) { asprintf(&licpath, "%s/.license", instance_path); if (licpath && am_file_exists(licpath) == 0) { am_free(licpath); fprintf(stderr, "\nYou have to accept ForgeRock Web Policy Agent license terms to continue.\n" "Please run agentadmin with -x option or interactively to view and accept the license.\n\n"); net_shutdown(); return (EXIT_FAILURE); } am_free(licpath); licpath = NULL; } if (cnt == 1) { url_t u, ua; am_trim(openam_url); u = URL(openam_url); if (u.error == 0) { if (validate_am_host(&u) != 0) { fprintf(stderr, "Error validating OpenAM URL\n"); net_shutdown(); return (EXIT_FAILURE); } } else { fprintf(stderr, "Invalid OpenAM URL value\n"); net_shutdown(); return (EXIT_FAILURE); } am_trim(agent_url); ua = URL(agent_url); if (ua.error != 0) { fprintf(stderr, "Invalid Agent URL value\n"); net_shutdown(); return (EXIT_FAILURE); } else { am_trim(agent_id); am_trim(agent_pass_file); if (am_read_password(agent_pass_file, agent_pass) == 0) { fprintf(stderr, "Error reading password file\n"); net_shutdown(); return (EXIT_FAILURE); } if (validate_agent(&u, agent_id, agent_pass) != 0) { fprintf(stderr, "%s\n", PROFILE_NOT_FOUND); net_shutdown(); return (EXIT_FAILURE); } } am_trim(web_conf_path); if (web_conf_path == NULL || web_conf_path[0] == '\0') { fprintf(stderr, "Varnish vmod directory must not be empty\n"); net_shutdown(); return (EXIT_FAILURE); } else { char *t = NULL; asprintf(&t, "%s/libvmod_am.so", web_conf_path); if (am_whitespace(web_conf_path) > 0) { fprintf(stderr, "Path to Varnish modules directory must not contain spaces\n"); am_free(t); net_shutdown(); return (EXIT_FAILURE); } else if (am_file_writeable(web_conf_path) == 0) { fprintf(stderr, "Error opening Varnish modules directory\n"); am_free(t); net_shutdown(); return (EXIT_FAILURE); } else if (am_file_exists(t) == 1) { fprintf(stderr, "This Varnish instance is already configured\n"); am_free(t); net_shutdown(); return (EXIT_FAILURE); } am_free(t); } create_varnish_instance(ua); fprintf(stderr, "\nVarnish and agent configuration files are here:\n%s\nCheck installation log %s for any errors.\n\n", web_conf_path_out, log_path); } else { display_usage(); } net_shutdown(); return (EXIT_SUCCESS); } print_version(NULL, &version); if (version != NULL) { snprintf(vers, sizeof (vers), "Version: %s", version); free(version); } initscr(); cbreak(); noecho(); keypad(stdscr, TRUE); start_color(); init_pair(1, COLOR_RED, COLOR_BLACK); getmaxyx(stdscr, y, x); asprintf(&licpath, "%s/.license", instance_path); if (am_file_exists(licpath) == 0) { lic = license(y, x, &mr); wrefresh(lic); delwin(lic); lic = NULL; if (mr == KEY_F(4)) { goto all_done; } else { am_setup_conf(licpath, NULL); } } ret_to_menu: refresh(); mr = mainmenu(y, x); if (mr == 0) { int cr = configure_step(y, x); if (cr == KEY_F(4)) { goto ret_to_menu; } } else if (mr >= 2 && mr < menu_size - 1) { memset(remname, 0, sizeof (remname)); if (sscanf(menu_values[mr], "Remove '%[^''']' instance", remname) == 1) { if (remove_step(remname, y, x) == KEY_F(4)) goto ret_to_menu; } goto ret_to_menu; } else if (mr == 1) { encrypt_step(y, x); goto ret_to_menu; } all_done: am_free(licpath); clean_main_menu(); move(0, 0); clrtoeol(); refresh(); endwin(); net_shutdown(); return (EXIT_SUCCESS); }
bool am_process_args(int *argc, char ***argv, int *exit_status) { // skip first arg (*argc)--; (*argv)++; #ifdef AM_OSX NSString *bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier]; bool in_osx_bundle = (bundleIdentifier != nil); if (in_osx_bundle) { // Use Resource dir of OS X bundle am_opt_data_dir = SDL_GetBasePath(); // When invoked from an OSX bundle extra arguments may be set by the OS, // so don't try processing them. return true; } #endif char *filename = NULL; while (*argc > 0) { bool foundopt = false; char *arg = (*argv)[0]; option *o = &options[0]; while (o->name != NULL) { if (strcmp(o->name, arg) == 0) { (*argc)--; (*argv)++; if (!o->handle_opt(argc, argv)) { *exit_status = EXIT_FAILURE; return false; } if (o->stop) return false; foundopt = true; break; } o++; } if (!foundopt) { if (strstr(arg, ".lua")) { (*argc)--; (*argv)++; filename = arg; } break; // pass remaining args to script } } if (filename == NULL) { if (am_file_exists("main.lua")) { am_opt_main_module = "main"; } else if (am_file_exists("./data/main.lua")) { am_opt_data_dir = "./data"; am_opt_main_module = "main"; } else if (!am_file_exists("data.pak")) { if (*argc > 0) { fprintf(stderr, "'%s' is not a lua file and no main.lua found in the current directory.\n" "Type 'amulet help' for usage information.\n", (*argv)[0]); } else { fprintf(stderr, "No main.lua found in the current directory.\n" "Type 'amulet help' for usage information.\n"); } *exit_status = EXIT_FAILURE; return false; } } else { if (!am_file_exists(filename)) { fprintf(stderr, "File '%s' not found.\nType 'amulet help' for usage information.\n", filename); *exit_status = EXIT_FAILURE; return false; } char *lua_ext = strstr(filename, ".lua"); if (lua_ext == NULL) { fprintf(stderr, "File must end with .lua.\nType 'amulet help' for usage information.\n"); *exit_status = EXIT_FAILURE; return false; } *lua_ext = '\0'; // strip extension char *last_slash = am_max(strrchr(filename, AM_PATH_SEP), strrchr(filename, '/')); // accept / on windows if (last_slash != NULL) { am_opt_data_dir = filename; *last_slash = '\0'; filename = last_slash+1; } am_opt_main_module = filename; } return true; }