summaryrefslogtreecommitdiffstats
path: root/src/main.c
blob: 3338fccad58305f25158c7b91c5fa7482b23cd9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
 * Copyright (C) 2023  Patrick McDermott
 *
 * This file is part of opkg-opk.
 *
 * opkg-opk is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * opkg-opk is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with opkg-opk.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#ifdef ENABLE_NLS
#include <locale.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include "defs.h"
#include "i18n.h"
#include "opk.h"

#ifdef HAVE_GETOPT_LONG
#include <getopt.h>
#else
#include <unistd.h>
#endif

extern const char *PACKAGE_VERSION_GIT;

const char *_optstring = "I:fchV";
#ifdef HAVE_GETOPT_LONG
struct option _longopts[] = {
	{
		.name    = "info",
		.has_arg = 1,
		.flag    = NULL,
		.val     = 'I',
	},
	{
		.name    = "control",
		.has_arg = 0,
		.flag    = NULL,
		.val     = 'f',
	},
	{
		.name    = "contents",
		.has_arg = 0,
		.flag    = NULL,
		.val     = 'c',
	},
	{
		.name    = "help",
		.has_arg = 0,
		.flag    = NULL,
		.val     = 'h',
	},
	{
		.name    = "version",
		.has_arg = 0,
		.flag    = NULL,
		.val     = 'V',
	},
};
#endif

static void
_help(const char *program_name)
{
	printf(_("Usage: %s OPTION... PACKAGE\n"), program_name);
#ifdef HAVE_GETOPT_LONG
	puts(_("Options:\n"
"  -I, --info=CONTROL-FILE  Print the named control file.  If this option is\n"
"                           given multiple times, the named control files will "
                           "be\n"
"                           printed in the order they appear in the package.\n"
"  -f, --control            Print the control file.\n"
"  -c, --contents           List the contents of the filesystem tree archive\n"
"                           portion of the package.  It is currently produced "
                           "in\n"
"                           a format similar to that generated by GNU and "
                           "BusyBox\n"
"                           tar's verbose listing.  User and group IDs and "
                           "names\n"
"                           are not checked against those on the host system,\n"
"                           since they may differ.\n"
"  -h, --help               Show this help information and exit.\n"
"  -V, --version            Show version information and exit."));
#else
	puts(_("Options:\n"
"  -I  Print the named control file.  If this option is given multiple times, "
      "the\n"
"      named control files will be printed in the order they appear in the\n"
"      package.\n"
"  -f  Print the control file.\n"
"  -c  List the contents of the filesystem tree archive portion of the "
      "package.\n"
"      It is currently produced in a format similar to that generated by GNU "
      "and\n"
"      BusyBox tar's verbose listing.  User and group IDs and names are not\n"
"      checked against those on the host system, since they may differ.\n"
"  -h  Show this help information and exit.\n"
"  -V  Show version information and exit."));
#endif
}

static void
_version(void)
{
	printf("%s %s%s\n", PACKAGE_NAME, PACKAGE_VERSION, PACKAGE_VERSION_GIT);
	/* TRANSLATORS: The "%s" conversion specifications are the copyright
	 * year(s) and copyright holder(s), respectively. */
	printf(_("Copyright (C) %s  %s\n"), "2023", "Patrick McDermott");
	puts(_("License GPLv3+: GNU GPL version 3 or later "
			"<http://gnu.org/licenses/gpl.html>.\n"
			"This is free software: you are free to change and "
			"redistribute it.\n"
			"There is NO WARRANTY, to the extent permitted by "
			"law.\n"));
	printf(_("Please report bugs to <%s>.\n"), PACKAGE_BUGREPORT);
}

static void
_help_tip(const char *program_name)
{
	/* TRANSLATORS: The "%s" conversion specifications are the program name
	 * and help option, respectively. */
	fprintf(stderr, _("Try \"%s %s\" for more information\n"), program_name,
#ifdef HAVE_GETOPT_LONG
			"--help"
#else
			"-h"
#endif
			);
}

int
main(int argc, char *argv[])
{
	const char          *program_name;
	struct opkg_opk_opk *opk;
	int                  opt;

#ifdef ENABLE_NLS
	bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR);
#ifdef HAVE_BIND_TEXTDOMAIN_CODESET
	bind_textdomain_codeset(PACKAGE, "UTF-8");
#endif
	textdomain(PACKAGE);
	setlocale(LC_ALL, "");
#endif

	program_name = argv[0];

	/* Initialize package. */
	opk = opkg_opk_opk_init();
	if (opk == NULL) {
		return EXIT_FAILURE;
	}

	opterr        = 0;
#ifdef HAVE_GETOPT_LONG
	while ((opt = getopt_long(argc, argv, _optstring, _longopts, NULL))
			!= -1) {
#else
	while ((opt = getopt(argc, argv, _optstring)) != -1) {
#endif
		switch(opt) {
			case 'I':
				if (opkg_opk_opk_print_control(opk, optarg)
						!= OPKG_OPK_OK) {
					opkg_opk_opk_free(opk);
					return EXIT_FAILURE;
				}
				break;
			case 'f':
				if (opkg_opk_opk_print_control(opk, "control")
						!= OPKG_OPK_OK) {
					opkg_opk_opk_free(opk);
					return EXIT_FAILURE;
				}
				break;
			case 'c':
				opkg_opk_opk_list_data(opk);
				break;
			case 'h':
				_help(program_name);
				return EXIT_SUCCESS;
			case 'V':
				_version();
				return EXIT_SUCCESS;
			default:
				fprintf(stderr, _("%s: Invalid option: "
							"\"%c\"\n"),
						program_name, optopt);
				_help_tip(program_name);
				opkg_opk_opk_free(opk);
				return EXIT_FAILURE;
		}
	}
	argc -= optind;
	argv += optind;

	if (argc < 1) {
		fprintf(stderr, _("%s: Missing package file operand\n"),
				program_name);
		_help_tip(program_name);
		opkg_opk_opk_free(opk);
		return EXIT_FAILURE;
	} else if (argc > 1) {
		fprintf(stderr, _("%s: Too many package file operands\n"),
				program_name);
		_help_tip(program_name);
		opkg_opk_opk_free(opk);
		return EXIT_FAILURE;
	}

	/* Read package. */
	if (opkg_opk_opk_read(opk, argv[0]) != OPKG_OPK_OK) {
		opkg_opk_opk_free(opk);
		return EXIT_FAILURE;
	}

	opkg_opk_opk_free(opk);
	return EXIT_SUCCESS;
}