main.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. Copyright (c) 2011, 2012, Simon Howard
  3. Permission to use, copy, modify, and/or distribute this software
  4. for any purpose with or without fee is hereby granted, provided
  5. that the above copyright notice and this permission notice appear
  6. in all copies.
  7. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
  8. WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
  9. WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
  10. AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
  11. CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  12. LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  13. NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <errno.h>
  20. #include "lib/lha_arch.h"
  21. #include "lha_reader.h"
  22. #include "config.h"
  23. #include "extract.h"
  24. #include "list.h"
  25. typedef enum {
  26. MODE_UNKNOWN,
  27. MODE_LIST,
  28. MODE_LIST_VERBOSE,
  29. MODE_CRC_CHECK,
  30. MODE_EXTRACT,
  31. MODE_PRINT
  32. } ProgramMode;
  33. static void help_page(char *progname)
  34. {
  35. printf(
  36. PACKAGE_NAME " v" PACKAGE_VERSION " command line LHA tool "
  37. "- Copyright (C) 2011,2012 Simon Howard\n"
  38. "usage: %s [-]{lvtxe[q{num}][finv]}[w=<dir>] archive_file [file...]\n"
  39. "commands: options:\n"
  40. " l,v List / Verbose List f Force overwrite (no prompt)\n"
  41. " t Test file CRC in archive i Ignore directory path\n"
  42. " x,e Extract from archive n Perform dry run\n"
  43. " p Print to stdout from archive q{num} Quiet mode\n"
  44. " v Verbose\n"
  45. " w=<dir> Specify extract directory\n"
  46. , progname);
  47. exit(-1);
  48. }
  49. static int do_command(ProgramMode mode, char *filename,
  50. LHAOptions *options,
  51. char **filters, unsigned int num_filters)
  52. {
  53. FILE *fstream;
  54. LHAInputStream *stream;
  55. LHAReader *reader;
  56. LHAFilter filter;
  57. int result;
  58. if (!strcmp(filename, "-")) {
  59. fstream = stdin;
  60. } else {
  61. fstream = fopen(filename, "rb");
  62. if (fstream == NULL) {
  63. fprintf(stderr, "LHa: Error: %s %s\n",
  64. filename, strerror(errno));
  65. exit(-1);
  66. }
  67. }
  68. stream = lha_input_stream_from_FILE(fstream);
  69. reader = lha_reader_new(stream);
  70. lha_filter_init(&filter, reader, filters, num_filters);
  71. result = 1;
  72. switch (mode) {
  73. case MODE_LIST:
  74. list_file_basic(&filter, options, fstream);
  75. break;
  76. case MODE_LIST_VERBOSE:
  77. list_file_verbose(&filter, options, fstream);
  78. break;
  79. case MODE_CRC_CHECK:
  80. result = test_file_crc(&filter, options);
  81. break;
  82. case MODE_EXTRACT:
  83. result = extract_archive(&filter, options);
  84. break;
  85. case MODE_PRINT:
  86. result = print_archive(&filter, options);
  87. break;
  88. case MODE_UNKNOWN:
  89. break;
  90. }
  91. lha_reader_free(reader);
  92. lha_input_stream_free(stream);
  93. fclose(fstream);
  94. return result;
  95. }
  96. static void init_options(LHAOptions *options)
  97. {
  98. options->overwrite_policy = LHA_OVERWRITE_PROMPT;
  99. options->quiet = 0;
  100. options->verbose = 0;
  101. options->dry_run = 0;
  102. options->extract_path = NULL;
  103. options->use_path = 1;
  104. }
  105. // Determine the program mode from the first character of the command
  106. // argument.
  107. static ProgramMode mode_for_char(char c)
  108. {
  109. switch (c) {
  110. case 'l':
  111. return MODE_LIST;
  112. case 'v':
  113. return MODE_LIST_VERBOSE;
  114. case 't':
  115. return MODE_CRC_CHECK;
  116. case 'e':
  117. case 'x':
  118. return MODE_EXTRACT;
  119. case 'p':
  120. return MODE_PRINT;
  121. default:
  122. return MODE_UNKNOWN;
  123. }
  124. }
  125. // Parse the option flags from the command argument.
  126. static int parse_options(char *arg, LHAOptions *options)
  127. {
  128. for (; *arg != '\0'; ++arg) {
  129. switch (*arg) {
  130. // Force overwrite of existing files.
  131. case 'f':
  132. options->overwrite_policy = LHA_OVERWRITE_ALL;
  133. break;
  134. // -i option turns off paths for extract.
  135. case 'i':
  136. options->use_path = 0;
  137. break;
  138. // Dry run?
  139. case 'n':
  140. options->dry_run = 1;
  141. break;
  142. // Quiet mode parsing. The quiet 'level' can be
  143. // specified - if the level is omitted, level 2
  144. // is implied. All quiet mode options imply
  145. // -f (overwrite without confirmation).
  146. case 'q':
  147. if (arg[1] >= '0' && arg[1] <= '9') {
  148. ++arg;
  149. options->quiet = *arg - '0';
  150. } else {
  151. options->quiet = 2;
  152. }
  153. options->overwrite_policy = LHA_OVERWRITE_ALL;
  154. break;
  155. // Verbose mode.
  156. case 'v':
  157. options->verbose = 1;
  158. break;
  159. // Specify extract directory: must be last option
  160. // specified. Optional '=' separator.
  161. case 'w':
  162. ++arg;
  163. if (*arg == '=') {
  164. ++arg;
  165. }
  166. options->extract_path = arg;
  167. arg += strlen(arg) - 1;
  168. break;
  169. default:
  170. return 0;
  171. }
  172. }
  173. return 1;
  174. }
  175. /**
  176. * Parse the command line options, initializing the options structure
  177. * used by the main program code.
  178. *
  179. * @param cmd The 'command' argument (first command line option).
  180. * @param mode Pointer to variable to store program mode.
  181. * @param options Pointer to options structure to initialize.
  182. * @return Non-zero if successful.
  183. */
  184. static int parse_command_line(char *cmd, ProgramMode *mode,
  185. LHAOptions *options)
  186. {
  187. // Parse program mode argument. Initial '-' is ignored.
  188. if (*cmd == '-') {
  189. ++cmd;
  190. }
  191. *mode = mode_for_char(*cmd);
  192. if (*mode == MODE_UNKNOWN) {
  193. return 0;
  194. }
  195. // Parse remaining options.
  196. if (!parse_options(cmd + 1, options)) {
  197. return 0;
  198. }
  199. return 1;
  200. }
  201. int main(int argc, char *argv[])
  202. {
  203. ProgramMode mode;
  204. LHAOptions options;
  205. #ifdef TEST_BUILD
  206. // When running tests, give output to stdout in binary mode;
  207. // on Windows, this gives the expected output (which was
  208. // constructed for Unix):
  209. lha_arch_set_binary(stdout);
  210. #endif
  211. // Parse the command line options and run command.
  212. // As a shortcut, a single argument can be provided to list the
  213. // contents of an archive ("lha foo.lzh" == "lha l foo.lzh").
  214. init_options(&options);
  215. if (argc >= 3 && parse_command_line(argv[1], &mode, &options)) {
  216. return !do_command(mode, argv[2], &options,
  217. argv + 3, argc - 3);
  218. } else if (argc == 2) {
  219. return !do_command(MODE_LIST, argv[1], &options, NULL, 0);
  220. } else {
  221. help_page(argv[0]);
  222. return 0;
  223. }
  224. }