1
#include <gsf-infile-msole.h>
2
#include <gsf-infile-zip.h>
3
#include <gsf-infile.h>
4
#include <gsf-input-stdio.h>
6
#include <glib/gi18n.h>
9
#define GETTEXT_PACKAGE NULL /* FIXME */
11
static gboolean show_version;
13
static const GOptionEntry gsf_options [] = {
16
0, G_OPTION_ARG_NONE, &show_version,
17
N_("Display program version"),
21
/* ---------------------------------------- */
23
{ NULL, 0, 0, 0, NULL, NULL, NULL}
26
/* ------------------------------------------------------------------------- */
29
open_archive (const char *filename)
36
src = gsf_input_stdio_new (filename, &error);
38
display_name = g_filename_display_name (filename);
39
g_printerr (_("%s: Failed to open %s: %s\n"),
43
g_free (display_name);
47
infile = gsf_infile_zip_new (src, NULL);
51
infile = gsf_infile_msole_new (src, NULL);
55
display_name = g_filename_display_name (filename);
56
g_printerr (_("%s: Failed to recognize %s as an archive\n"),
59
g_free (display_name);
63
/* ------------------------------------------------------------------------- */
66
find_member (GsfInfile *arch, const char *name)
68
const char *slash = strchr (name, '/');
71
char *dirname = g_strndup (name, slash - name);
75
member = gsf_infile_child_by_name (arch, dirname);
79
dir = GSF_INFILE (member);
80
member = find_member (dir, slash + 1);
84
return gsf_infile_child_by_name (arch, name);
88
/* ------------------------------------------------------------------------- */
91
gsf_help (G_GNUC_UNUSED int argc, G_GNUC_UNUSED char **argv)
93
g_print (_("Available subcommands are...\n"));
94
g_print (_("* cat output one or more files in archive\n"));
95
g_print (_("* dump dump one or more files in archive as hex\n"));
96
g_print (_("* help list subcommands\n"));
97
g_print (_("* list list files in archive\n"));
101
/* ------------------------------------------------------------------------- */
104
ls_R (GsfInput *input, const char *prefix)
106
char const *name = gsf_input_name (input);
107
GsfInfile *infile = GSF_IS_INFILE (input) ? GSF_INFILE (input) : NULL;
108
gboolean is_dir = infile && gsf_infile_num_children (infile) > 0;
113
char *display_name = name ?
114
g_filename_display_name (name)
116
full_name = g_strconcat (prefix,
119
new_prefix = g_strconcat (full_name, "/", NULL);
120
g_free (display_name);
122
full_name = g_strdup ("*root*");
123
new_prefix = g_strdup ("");
126
g_print ("%c %10" GSF_OFF_T_FORMAT " %s\n",
127
(is_dir ? 'd' : 'f'),
128
gsf_input_size (input),
133
for (i = 0 ; i < gsf_infile_num_children (infile) ; i++) {
134
GsfInput *child = gsf_infile_child_by_index (infile, i);
135
ls_R (child, new_prefix);
136
g_object_unref (child);
145
gsf_list (int argc, char **argv)
149
for (i = 0; i < argc; i++) {
150
const char *filename = argv[i];
152
GsfInfile *infile = open_archive (filename);
159
display_name = g_filename_display_name (filename);
160
g_print ("%s:\n", display_name);
161
g_free (display_name);
163
ls_R (GSF_INPUT (infile), NULL);
164
g_object_unref (infile);
170
/* ------------------------------------------------------------------------- */
173
gsf_dump (int argc, char **argv, gboolean hex)
175
const char *filename;
184
infile = open_archive (filename);
188
for (i = 1; i < argc; i++) {
189
const char *name = argv[i];
190
GsfInput *member = find_member (infile, name);
192
char *display_name = g_filename_display_name (name);
193
g_print ("%s: archive has no member %s\n",
194
g_get_prgname (), display_name);
195
g_free (display_name);
201
char *display_name = g_filename_display_name (name);
202
g_print ("%s:\n", display_name);
203
g_free (display_name);
205
gsf_input_dump (member, hex);
206
g_object_unref (member);
209
g_object_unref (infile);
213
/* ------------------------------------------------------------------------- */
216
main (int argc, char **argv)
218
GOptionContext *ocontext;
219
GError *error = NULL;
223
g_set_prgname (argv[0]);
227
bindtextdomain (GETTEXT_PACKAGE, gnm_locale_dir ());
228
textdomain (GETTEXT_PACKAGE);
229
setlocale (LC_ALL, "");
232
usage = _("SUBCOMMAND ARCHIVE...");
233
ocontext = g_option_context_new (usage);
234
g_option_context_add_main_entries (ocontext, gsf_options, GETTEXT_PACKAGE);
235
g_option_context_parse (ocontext, &argc, &argv, &error);
236
g_option_context_free (ocontext);
239
g_printerr (_("%s\nRun '%s --help' to see a full list of available command line options.\n"),
240
error->message, argv[0]);
241
g_error_free (error);
246
g_print (_("gsf version %d.%d.%d\n"),
247
libgsf_major_version, libgsf_minor_version, libgsf_micro_version);
252
g_printerr (_("Usage: %s %s\n"), (argv[0] ? argv[0] : "gsf"), usage);
258
if (strcmp (cmd, "help") == 0)
259
return gsf_help (argc - 2, argv + 2);
261
if (strcmp (cmd, "list") == 0 || strcmp (cmd, "l") == 0)
262
return gsf_list (argc - 2, argv + 2);
264
if (strcmp (cmd, "cat") == 0)
265
return gsf_dump (argc - 2, argv + 2, FALSE);
266
if (strcmp (cmd, "dump") == 0)
267
return gsf_dump (argc - 2, argv + 2, TRUE);
269
g_printerr (_("Run '%s help' to see a list subcommands.\n"), argv[0]);