| /* | |
| zipsh.c -- shell for interacting with zip archives in multiple ways | |
| Copyright (C) 2012-2025 Dieter Baron and Thomas Klausner | |
| This file is part of libzip, a library to manipulate ZIP archives. | |
| The authors can be contacted at <info@libzip.org> | |
| Redistribution and use in source and binary forms, with or without | |
| modification, are permitted provided that the following conditions | |
| are met: | |
| 1. Redistributions of source code must retain the above copyright | |
| notice, this list of conditions and the following disclaimer. | |
| 2. Redistributions in binary form must reproduce the above copyright | |
| notice, this list of conditions and the following disclaimer in | |
| the documentation and/or other materials provided with the | |
| distribution. | |
| 3. The names of the authors may not be used to endorse or promote | |
| products derived from this software without specific prior | |
| written permission. | |
| THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS | |
| OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
| WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY | |
| DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
| DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE | |
| GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
| INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER | |
| IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | |
| OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN | |
| IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| */ | |
| #include <errno.h> | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <string.h> | |
| #include "shell_int.h" | |
| #include "compat/src_compat.h" | |
| bool exit_implementation(invocation_t *invocation, error_message_t *error_message) { | |
| error_message_set(error_message, "internal error: exit command function should not be called"); | |
| return false; | |
| } | |
| COMMAND_NO_ARGUMENTS_OR_OPTIONS(exit, "exit the shell"); | |
| words_t *words = NULL; | |
| command_status_t execute_line(const char *line, error_message_t *error_message) { | |
| command_status_t status = COMMAND_STATUS_OK; | |
| error_message_t local_error_message = ERROR_MESSAGE_INIT; | |
| if (!words_parse(words, line, error_message)) { | |
| return COMMAND_STATUS_ERROR; | |
| } | |
| if (words->n_words > 0) { | |
| const command_t *cmd = find_command(words->words[0]); | |
| if (cmd == NULL) { | |
| error_message_set_format(error_message, "unknown command '%s'", words->words[0]); | |
| status = COMMAND_STATUS_ERROR; | |
| } | |
| else if (cmd->function == exit_implementation) { | |
| status = COMMAND_STATUS_EXIT; | |
| } | |
| else if (!execute_command(cmd, (int)words->n_words, words->words, &local_error_message)) { | |
| error_message_set_format(error_message, "%s: %s", words->words[0], error_message_get(&local_error_message)); | |
| status = COMMAND_STATUS_ERROR; | |
| } | |
| } | |
| error_message_fini(&local_error_message); | |
| return status; | |
| } | |
| bool execute_lines(const char *filename, FILE *input) { | |
| size_t line_number = 0; | |
| error_message_t error_message = ERROR_MESSAGE_INIT; | |
| char *line = NULL; | |
| size_t len = 0; | |
| bool ok = true; | |
| command_status_t status; | |
| while (getline(&line, &len, input) != -1) { | |
| line_number++; | |
| status = execute_line(line, &error_message); | |
| if (status == COMMAND_STATUS_EXIT) { | |
| break; | |
| } | |
| else if (status == COMMAND_STATUS_ERROR) { | |
| fprintf(stderr, "%s:%zu: %s\n", filename, line_number, error_message_get(&error_message)); | |
| ok = false; | |
| } | |
| } | |
| error_message_fini(&error_message); | |
| free(line); | |
| return ok; | |
| } | |
| bool shell_loop(const char *script_filename) { | |
| FILE *input = stdin; | |
| const char *file_name = "*stdin*"; | |
| bool ok = true; | |
| if ((words = words_new()) == NULL) { | |
| fprintf(stderr, "out of memory\n"); | |
| exit(1); | |
| } | |
| if (script_filename != NULL) { | |
| input = fopen(script_filename, "r"); | |
| if (input == NULL) { | |
| fprintf(stderr, "can't open script file '%s': %s\n", script_filename, strerror(errno)); | |
| exit(1); | |
| } | |
| file_name = script_filename; | |
| } | |
| if (use_readline(script_filename)) { | |
| ok = execute_lines_readline(); | |
| } | |
| else { | |
| ok = execute_lines(file_name, input); | |
| } | |
| if (input != stdin) { | |
| fclose(input); | |
| } | |
| words_free(words); | |
| return ok; | |
| } | |
| /* clang-format off */ | |
| static int early_exit_options[] = { | |
| 'h', | |
| VERSION_OPTION_ID, | |
| 0}; | |
| /* clang-format on */ | |
| int shell_main(shell_command_t *shell_command, int argc, char *argv[]) { | |
| invocation_t *invocation = NULL; | |
| error_message_t error_message = ERROR_MESSAGE_INIT; | |
| bool ok = true; | |
| opterr = 0; | |
| if ((invocation = invocation_new(&shell_command->command, early_exit_options, argc, argv, &error_message, NULL)) == NULL) { | |
| fprintf(stderr, "%s: %s\n", argv[0], error_message_get(&error_message)); | |
| error_message_fini(&error_message); | |
| return 1; | |
| } | |
| if (invocation_get_option(invocation, 'h')) { | |
| if (!print_command_help(&shell_command->command, argv[0], &error_message)) { | |
| fprintf(stderr, "%s: %s\n", argv[0], error_message_get(&error_message)); | |
| error_message_fini(&error_message); | |
| return 1; | |
| } | |
| return 0; | |
| } | |
| if (invocation_get_option(invocation, VERSION_OPTION_ID)) { | |
| printf("%s %s\n", shell_command->command.name, LIBZIP_VERSION); | |
| return 0; | |
| } | |
| if (shell_command->setup && !shell_command->setup(invocation, &error_message)) { | |
| fprintf(stderr, "%s: %s\n", argv[0], error_message_get(&error_message)); | |
| error_message_fini(&error_message); | |
| invocation_free(invocation); | |
| return 1; | |
| } | |
| if (invocation_get_option(invocation, 'c')) { | |
| int command_argc; | |
| char **command_argv; | |
| const command_t *command = find_command(invocation->arguments[shell_command->shell_argument_index].string); | |
| if (command == NULL) { | |
| fprintf(stderr, "%s: Unknown command: %s\n", argv[0], invocation->arguments[shell_command->shell_argument_index].string); | |
| return 1; | |
| } | |
| command_argc = (int)(invocation->n_arguments - (shell_command->shell_argument_index)); | |
| command_argv = malloc(command_argc * sizeof(char *)); | |
| for (int i = 0; i < command_argc; i++) { | |
| command_argv[i] = (char *)invocation->arguments[shell_command->shell_argument_index + i].string; | |
| } | |
| ok = execute_command(command, command_argc, command_argv, &error_message); | |
| if (!ok) { | |
| fprintf(stderr, "%s: %s\n", command->name, error_message_get(&error_message)); | |
| } | |
| free(command_argv); | |
| } | |
| else { | |
| ok = shell_loop(invocation->arguments[shell_command->shell_argument_index].string); | |
| } | |
| if (shell_command->cleanup && !shell_command->cleanup(ok, &error_message)) { | |
| fprintf(stderr, "%s: %s\n", argv[0], error_message_get(&error_message)); | |
| ok = false; | |
| } | |
| invocation_free(invocation); | |
| error_message_fini(&error_message); | |
| return ok ? 0 : 1; | |
| } | |
| bool is_pattern(const char *str) { | |
| return strchr(str, '*') != NULL || strchr(str, '?') != NULL || strchr(str, '[') != NULL; | |
| } |