~ubuntu-branches/ubuntu/karmic/libgsf/karmic

« back to all changes in this revision

Viewing changes to gsf/gsf.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastian Dröge
  • Date: 2006-06-30 15:46:51 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20060630154651-3wyy7y8c2zj7l5e0
Tags: 1.14.1-2ubuntu1
* Sync with Debian:
  Remaining Ubuntu Changes
  + Call intltool-update -p at build time to get a .pot file 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <gsf-infile-msole.h>
 
2
#include <gsf-infile-zip.h>
 
3
#include <gsf-infile.h>
 
4
#include <gsf-input-stdio.h>
 
5
#include <gsf-utils.h>
 
6
#include <glib/gi18n.h>
 
7
#include <string.h>
 
8
 
 
9
#define GETTEXT_PACKAGE NULL /* FIXME */
 
10
 
 
11
static gboolean show_version;
 
12
 
 
13
static const GOptionEntry gsf_options [] = { 
 
14
        {
 
15
                "version", 'v',
 
16
                0, G_OPTION_ARG_NONE, &show_version,
 
17
                N_("Display program version"),
 
18
                NULL
 
19
        },
 
20
 
 
21
        /* ---------------------------------------- */
 
22
 
 
23
        { NULL, 0, 0, 0, NULL, NULL, NULL} 
 
24
};
 
25
 
 
26
/* ------------------------------------------------------------------------- */
 
27
 
 
28
static GsfInfile *
 
29
open_archive (const char *filename)
 
30
{
 
31
        GsfInfile *infile;
 
32
        GError *error = NULL;   
 
33
        GsfInput *src;
 
34
        char *display_name;
 
35
 
 
36
        src = gsf_input_stdio_new (filename, &error);
 
37
        if (error) {
 
38
                display_name = g_filename_display_name (filename);
 
39
                g_printerr (_("%s: Failed to open %s: %s\n"),
 
40
                            g_get_prgname (),
 
41
                            display_name,
 
42
                            error->message);
 
43
                g_free (display_name);
 
44
                return NULL;
 
45
        }
 
46
 
 
47
        infile = gsf_infile_zip_new (src, NULL);
 
48
        if (infile)
 
49
                return infile;
 
50
 
 
51
        infile = gsf_infile_msole_new (src, NULL);
 
52
        if (infile)
 
53
                return infile;
 
54
 
 
55
        display_name = g_filename_display_name (filename);
 
56
        g_printerr (_("%s: Failed to recognize %s as an archive\n"),
 
57
                    g_get_prgname (),
 
58
                    display_name);
 
59
        g_free (display_name);
 
60
        return NULL;
 
61
}
 
62
 
 
63
/* ------------------------------------------------------------------------- */
 
64
 
 
65
static GsfInput *
 
66
find_member (GsfInfile *arch, const char *name)
 
67
{
 
68
        const char *slash = strchr (name, '/');
 
69
 
 
70
        if (slash) {
 
71
                char *dirname = g_strndup (name, slash - name);
 
72
                GsfInput *member;
 
73
                GsfInfile *dir;
 
74
 
 
75
                member = gsf_infile_child_by_name (arch, dirname);
 
76
                g_free (dirname);
 
77
                if (!member)
 
78
                        return NULL;
 
79
                dir = GSF_INFILE (member);
 
80
                member = find_member (dir, slash + 1);
 
81
                g_object_unref (dir);
 
82
                return member;
 
83
        } else {
 
84
                return gsf_infile_child_by_name (arch, name);
 
85
        }
 
86
}
 
87
 
 
88
/* ------------------------------------------------------------------------- */
 
89
 
 
90
static int
 
91
gsf_help (G_GNUC_UNUSED int argc, G_GNUC_UNUSED char **argv)
 
92
{
 
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"));
 
98
        return 0;
 
99
}
 
100
 
 
101
/* ------------------------------------------------------------------------- */
 
102
 
 
103
static void
 
104
ls_R (GsfInput *input, const char *prefix)
 
105
{
 
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;
 
109
        char *full_name;
 
110
        char *new_prefix;
 
111
 
 
112
        if (prefix) {
 
113
                char *display_name = name ?
 
114
                        g_filename_display_name (name)
 
115
                        : g_strdup ("?");
 
116
                full_name = g_strconcat (prefix,
 
117
                                         display_name,
 
118
                                         NULL);
 
119
                new_prefix = g_strconcat (full_name, "/", NULL);
 
120
                g_free (display_name);
 
121
        } else {
 
122
                full_name = g_strdup ("*root*");
 
123
                new_prefix = g_strdup ("");
 
124
        }
 
125
 
 
126
        g_print ("%c %10" GSF_OFF_T_FORMAT " %s\n",
 
127
                 (is_dir ? 'd' : 'f'),
 
128
                 gsf_input_size (input),
 
129
                 full_name);
 
130
 
 
131
        if (is_dir) {
 
132
                int i;
 
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);
 
137
                }
 
138
        }
 
139
 
 
140
        g_free (full_name);
 
141
        g_free (new_prefix);
 
142
}
 
143
 
 
144
static int
 
145
gsf_list (int argc, char **argv)
 
146
{
 
147
        int i;
 
148
 
 
149
        for (i = 0; i < argc; i++) {
 
150
                const char *filename = argv[i];
 
151
                char *display_name;
 
152
                GsfInfile *infile = open_archive (filename);
 
153
                if (!infile)
 
154
                        return 1;
 
155
 
 
156
                if (i > 0)
 
157
                        g_print ("\n");
 
158
 
 
159
                display_name = g_filename_display_name (filename);
 
160
                g_print ("%s:\n", display_name);
 
161
                g_free (display_name);
 
162
 
 
163
                ls_R (GSF_INPUT (infile), NULL);
 
164
                g_object_unref (infile);
 
165
        }
 
166
 
 
167
        return 0;
 
168
}
 
169
 
 
170
/* ------------------------------------------------------------------------- */
 
171
 
 
172
static int
 
173
gsf_dump (int argc, char **argv, gboolean hex)
 
174
{
 
175
        const char *filename;
 
176
        GsfInfile *infile;
 
177
        int i;
 
178
        int res = 0;
 
179
 
 
180
        if (argc < 2)
 
181
                return 1;
 
182
 
 
183
        filename = argv[0];
 
184
        infile = open_archive (filename);
 
185
        if (!infile)
 
186
                return 1;
 
187
 
 
188
        for (i = 1; i < argc; i++) {
 
189
                const char *name = argv[i];
 
190
                GsfInput *member = find_member (infile, name);
 
191
                if (!member) {
 
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);
 
196
                        res = 1;
 
197
                        break;
 
198
                }
 
199
 
 
200
                if (hex) {
 
201
                        char *display_name = g_filename_display_name (name);
 
202
                        g_print ("%s:\n", display_name);
 
203
                        g_free (display_name);
 
204
                }
 
205
                gsf_input_dump (member, hex);
 
206
                g_object_unref (member);
 
207
        }
 
208
 
 
209
        g_object_unref (infile);
 
210
        return res;
 
211
}
 
212
 
 
213
/* ------------------------------------------------------------------------- */
 
214
 
 
215
int
 
216
main (int argc, char **argv)
 
217
{
 
218
        GOptionContext *ocontext;
 
219
        GError *error = NULL;   
 
220
        const char *usage;
 
221
        const char *cmd;
 
222
 
 
223
        g_set_prgname (argv[0]);
 
224
        gsf_init ();
 
225
 
 
226
#if 0
 
227
        bindtextdomain (GETTEXT_PACKAGE, gnm_locale_dir ());
 
228
        textdomain (GETTEXT_PACKAGE);
 
229
        setlocale (LC_ALL, "");
 
230
#endif
 
231
 
 
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);
 
237
 
 
238
        if (error) {
 
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);
 
242
                return 1;
 
243
        }
 
244
 
 
245
        if (show_version) {
 
246
                g_print (_("gsf version %d.%d.%d\n"),
 
247
                         libgsf_major_version, libgsf_minor_version, libgsf_micro_version);
 
248
                return 0;
 
249
        }
 
250
 
 
251
        if (argc <= 1) {
 
252
                g_printerr (_("Usage: %s %s\n"), (argv[0] ? argv[0] : "gsf"), usage);
 
253
                return 1;
 
254
        }
 
255
 
 
256
        cmd = argv[1];
 
257
 
 
258
        if (strcmp (cmd, "help") == 0)
 
259
                return gsf_help (argc - 2, argv + 2);
 
260
 
 
261
        if (strcmp (cmd, "list") == 0 || strcmp (cmd, "l") == 0)
 
262
                return gsf_list (argc - 2, argv + 2);
 
263
 
 
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);
 
268
 
 
269
        g_printerr (_("Run '%s help' to see a list subcommands.\n"), argv[0]);
 
270
        return 1;
 
271
}