static void cork_command_run(struct cork_command *command, int argc, char **argv) { cork_command_add_breadcrumb(command); /* If the gives the --help option at this point, describe the current * command. */ if (argc >= 2 && (streq(argv[1], "--help") || streq(argv[1], "-h"))) { cork_command_show_help(command, NULL); return; } /* Otherwise let the command parse any options that occur here. */ if (command->parse_options != NULL) { int option_count = command->parse_options(argc, argv); argc -= option_count; argv += option_count; } else { argc--; argv++; } switch (command->type) { case CORK_COMMAND_SET: cork_command_set_run(command, argc, argv); return; case CORK_LEAF_COMMAND: cork_command_leaf_run(command, argc, argv); return; default: cork_unreachable(); } }
static int parse_options(int argc, char **argv) { int ch; getopt_reset(); while ((ch = getopt_long(argc, argv, SHORT_OPTS, opts, NULL)) != -1) { if (general_parse_opt(ch, &buzzy_raw_pkg)) { continue; } if (package_env_parse_opt(ch, &buzzy_raw_pkg)) { continue; } switch (ch) { case 'f': force = true; break; default: cork_command_show_help(&buzzy_raw_pkg, NULL); exit(EXIT_FAILURE); } } return optind; }
static void cork_command_set_run_help(struct cork_command *command, int argc, char **argv) { /* When we see the help command when processing a command set, we use any * remaining arguments to identifity which subcommand the user wants help * with. */ /* Skip over the name of the command set */ argc--; argv++; while (argc > 0 && command->type == CORK_COMMAND_SET) { struct cork_command *subcommand = cork_command_set_get_subcommand(command, argv[0]); if (subcommand == NULL) { printf("Unknown command \"%s\".\n" "Usage:%s <command> [<options>]\n", argv[0], cork_command_breadcrumbs()); exit(EXIT_FAILURE); } cork_command_add_breadcrumb(subcommand); command = subcommand; argc--; argv++; } cork_command_show_help(command, NULL); }
static void sub_run(int argc, char **argv) { struct cork_env *env; struct cork_exec *exec; struct cork_subprocess_group *group; struct cork_subprocess *sp; if (argc == 0) { cork_command_show_help(&sub, "Missing command"); exit(EXIT_FAILURE); } rp_check_exit(env = cork_env_clone_current()); rp_check_exit(exec = cork_exec_new_with_param_array(argv[0], argv)); cork_exec_set_env(exec, env); if (sub_cwd != NULL) { cork_exec_set_cwd(exec, sub_cwd); } rp_check_exit(group = cork_subprocess_group_new()); rp_check_exit(sp = cork_subprocess_new_exec(exec, NULL, NULL, NULL)); cork_subprocess_group_add(group, sp); ri_check_exit(cork_subprocess_group_start(group)); ri_check_exit(cork_subprocess_group_wait(group)); cork_subprocess_group_free(group); }
static void rm_run(int argc, char **argv) { struct cork_file *file; if (argc < 1) { cork_command_show_help(&rm_cmd, "Missing file"); exit(EXIT_FAILURE); } else if (argc > 1) { cork_command_show_help(&rm_cmd, "Too many directories"); exit(EXIT_FAILURE); } file = cork_file_new(argv[0]); ri_check_exit(cork_file_remove(file, rm_flags)); cork_file_free(file); exit(EXIT_SUCCESS); }
static void execute(int argc, char **argv) { struct cork_path *binary_package_dir; struct cork_path *staging_dir; struct bz_packager *packager; struct bz_value *value; if (argc == 0) { cork_command_show_help(&buzzy_raw_pkg, "Missing staging directory."); exit(EXIT_FAILURE); } else if (argc > 1) { cork_command_show_help(&buzzy_raw_pkg, NULL); exit(EXIT_FAILURE); } ri_check_error(bz_load_variable_definitions()); ri_check_error(bz_pdb_discover()); package_env_init(); rp_check_error(binary_package_dir = cork_path_new(".")); ri_check_error(cork_path_set_absolute(binary_package_dir)); rp_check_error(value = bz_path_value_new(binary_package_dir)); bz_env_add_override(package_env, "binary_package_dir", value); rp_check_error(staging_dir = cork_path_new(argv[0])); ri_check_error(cork_path_set_absolute(staging_dir)); rp_check_error(value = bz_path_value_new(staging_dir)); bz_env_add_override(package_env, "staging_dir", value); rp_check_error(value = bz_string_value_new(force? "1": "0")); bz_env_add_override(package_env, "force", value); rp_check_error(value = bz_string_value_new((verbosity > 0)? "1": "0")); bz_env_add_override(package_env, "verbose", value); rp_check_error(packager = bz_package_packager_new(package_env)); ri_check_error(bz_packager_package(packager)); bz_packager_free(packager); package_env_done(); bz_finalize_actions(); exit(EXIT_SUCCESS); }
static void find_run(int argc, char **argv) { struct cork_path_list *list; if (argc < 1) { cork_command_show_help(&find, "Missing file"); exit(EXIT_FAILURE); } else if (argc < 2) { cork_command_show_help(&find, "Missing path"); exit(EXIT_FAILURE); } else if (argc < 2) { cork_command_show_help(&find, "Too many parameters"); exit(EXIT_FAILURE); } list = cork_path_list_new(argv[1]); if (find_all) { struct cork_file_list *file_list; size_t i; size_t count; rp_check_exit(file_list = cork_path_list_find_files(list, argv[0])); count = cork_file_list_size(file_list); for (i = 0; i < count; i++) { struct cork_file *file = cork_file_list_get(file_list, i); printf("%s\n", cork_path_get(cork_file_path(file))); } cork_file_list_free(file_list); } else { struct cork_file *file; rp_check_exit(file = cork_path_list_find_file(list, argv[0])); printf("%s\n", cork_path_get(cork_file_path(file))); cork_file_free(file); } cork_path_list_free(list); exit(EXIT_SUCCESS); }
static int sub_options(int argc, char **argv) { if (argc >= 2 && (streq(argv[1], "-d") || streq(argv[1], "--cwd"))) { if (argc >= 3) { sub_cwd = argv[2]; return 3; } else { cork_command_show_help(&sub, "Missing directory for --cwd"); exit(EXIT_FAILURE); } } else { return 1; } }