xref: /haiku/src/bin/package/package.cpp (revision 9760dcae2038d47442f4658c2575844c6cf92c40)
1 /*
2  * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include "package.h"
8 
9 #include <errno.h>
10 #include <getopt.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 
15 #include "PackageWriter.h"
16 
17 
18 extern const char* __progname;
19 const char* kCommandName = __progname;
20 
21 
22 static const char* kUsage =
23 	"Usage: %s <command> <command args>\n"
24 	"Creates, inspects, or extracts a Haiku package.\n"
25 	"\n"
26 	"Commands:\n"
27 	"  create [ <options> ] <package> <file> ...\n"
28 	"    Creates package file <package> from a list of files.\n"
29 	"\n"
30 	"    -C <dir>    - Change to directory <dir> before interpreting the file\n"
31 	"                  names <file>.\n"
32 	"\n"
33 	"  dump [ <options> ] <package>\n"
34 	"    Dumps the TOC section of package file <package>. For debugging only.\n"
35 	"\n"
36 	"  extract [ <options> ] <package>\n"
37 	"    Extracts the contents of package file <package>.\n"
38 	"\n"
39 	"    -C <dir>    - Change to directory <dir> before extracting the "
40 		"contents\n"
41 	"                  of the archive.\n"
42 	"\n"
43 	"  list [ <options> ] <package>\n"
44 	"    Lists the contents of package file <package>.\n"
45 	"\n"
46 	"    -a         - Also list the file attributes.\n"
47 	"\n"
48 	"Common Options:\n"
49 	"  -h, --help   - Print this usage info.\n"
50 ;
51 
52 
53 void
54 print_usage_and_exit(bool error)
55 {
56     fprintf(error ? stderr : stdout, kUsage, kCommandName);
57     exit(error ? 1 : 0);
58 }
59 
60 
61 int
62 main(int argc, const char* const* argv)
63 {
64 	if (argc < 2)
65 		print_usage_and_exit(true);
66 
67 	const char* command = argv[1];
68 	if (strcmp(command, "create") == 0)
69 		return command_create(argc - 1, argv + 1);
70 
71 	if (strcmp(command, "dump") == 0)
72 		return command_dump(argc - 1, argv + 1);
73 
74 	if (strcmp(command, "extract") == 0)
75 		return command_extract(argc - 1, argv + 1);
76 
77 	if (strcmp(command, "list") == 0)
78 		return command_list(argc - 1, argv + 1);
79 
80 	if (strcmp(command, "help") == 0)
81 		print_usage_and_exit(false);
82 	else
83 		print_usage_and_exit(true);
84 
85 	// never gets here
86 	return 0;
87 }
88