1 /* 2 * Access a filesystem. 3 * 4 * Copyright (C) 2019, 2022, 2024 Paul Boddie <paul@boddie.org.uk> 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation; either version 2 of 9 * the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 * Boston, MA 02110-1301, USA 20 */ 21 22 #include <stdio.h> 23 #include <unistd.h> 24 25 #include "ops.h" 26 #include "session.h" 27 28 29 30 /* Help messages. */ 31 32 char command_help_text[] = "\ 33 Usage: %s [ <options> ] <operation> <filename>...\n\ 34 "; 35 36 char options_help_text[] = "\ 37 \n\ 38 File permission options:\n\ 39 \n\ 40 -m MASK Set mode/permissions mask for new directories\n\ 41 "; 42 43 char operations_help_text[] = "\ 44 \n\ 45 Transfer operations:\n\ 46 \n\ 47 copy-in Copy files into a directory within the image\n\ 48 \n\ 49 Image operations:\n\ 50 \n\ 51 ls List files and directories within the image\n\ 52 mkdir Make directories within the image\n\ 53 rm Remove non-directory objects from the image\n\ 54 rmdir Remove directories from the image\n\ 55 stat Show statistics for files and directories\n\ 56 \n\ 57 Script operations:\n\ 58 \n\ 59 script Read operations from a script file (or stdin)\n\ 60 \n\ 61 Execution operations:\n\ 62 \n\ 63 file Send a file to a program\n\ 64 jobs Show initiated programs\n\ 65 pipe Connect a running program to a new program\n\ 66 run Run a program from the filesystem and wait for it to finish\n\ 67 spawn Run a program in the background\n\ 68 wait Wait for a spawned program to finish\n\ 69 "; 70 71 72 73 /* Show a help message appropriate to interactive use. */ 74 75 static int show_help(int argc, char *argv[]) 76 { 77 (void) argc; (void) argv; 78 79 fprintf(stderr, operations_help_text); 80 return 0; 81 } 82 83 84 85 /* Operations exposed by the program. */ 86 87 struct operation operations[] = { 88 {"copy-in", copy_in}, 89 {"file", file_to_program}, 90 {"help", show_help}, 91 {"jobs", show_programs}, 92 {"ls", list_objects}, 93 {"mkdir", make_dirs}, 94 {"pipe", pipe_to_program}, 95 {"rm", remove_non_dirs}, 96 {"rmdir", remove_dirs}, 97 {"run", run_program}, 98 {"spawn", spawn_program}, 99 {"script", run_script}, 100 {"stat", stat_objects}, 101 {"wait", wait_program}, 102 {NULL, NULL}, 103 }; 104 105 /* Main program. */ 106 107 int main(int argc, char *argv[]) 108 { 109 /* Program argument details. */ 110 111 char **args; 112 int num_args; 113 int exitcode; 114 enum op_results op_result; 115 116 /* Parse program options and initialise the argument details. */ 117 118 if (parse_options(argc, argv)) 119 return 1; 120 121 args = &argv[optind]; 122 num_args = argc - optind; 123 124 if (num_args < 2) 125 { 126 fprintf(stderr, command_help_text, argv[0]); 127 fprintf(stderr, options_help_text); 128 fprintf(stderr, operations_help_text); 129 return 1; 130 } 131 132 /* Perform the requested operation. */ 133 134 op_result = run_operation(args[0], num_args - 1, &args[1], &exitcode); 135 return handle_op_result(args[0], op_result); 136 } 137 138 /* vim: tabstop=4 expandtab shiftwidth=4 139 */