g_type_init: no longer needs to be called
[quvi-tool.git] / src / main.c
bloba0a2986d7656a407bd79db49350ada07a815d5ed
1 /* quvi
2 * Copyright (C) 2012,2013 Toni Gundogdu <legatvs@gmail.com>
4 * This file is part of quvi <http://quvi.sourceforge.net/>.
6 * This program is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU Affero General Public
8 * License as published by the Free Software Foundation, either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General
17 * Public License along with this program. If not, see
18 * <http://www.gnu.org/licenses/>.
21 #include "config.h"
23 #include <stdlib.h>
24 #include <locale.h>
25 #include <glib-object.h>
26 #include <glib/gi18n.h>
27 #include <quvi.h>
29 #include "opts.h"
30 #include "cmd.h"
32 struct opts_s opts;
33 gint exit_status;
34 gchar *argv0;
35 gchar *cmd;
37 static void _setup_gettext()
39 setlocale(LC_ALL, "");
40 bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR);
41 bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
42 textdomain(GETTEXT_PACKAGE);
45 static gint _exec_man_man(const gchar *page)
47 gchar *cmdl;
48 GError *e;
49 gint r;
51 cmdl = g_strdup_printf("man %s", page);
52 r = EXIT_SUCCESS;
53 e = NULL;
55 if (g_spawn_command_line_sync(cmdl, NULL, NULL, NULL, &e) == FALSE)
57 g_printerr(_("error: while executing: `%s': %s\n"), cmdl, e->message);
58 r = EXIT_FAILURE;
59 g_error_free(e);
62 g_free(cmdl);
63 return (r);
66 static gint _cmd_version(gint, gchar**);
67 static gint _cmd_help(gint, gchar**);
69 typedef gint (*cmd_cb)(gint, gchar**);
71 struct builtin_cmd_s
73 const gchar *cmd;
74 const gchar *descr;
75 const cmd_cb cb;
78 static const struct builtin_cmd_s builtin_cmds[] =
80 {"dump", N_("Query and print the property values"), cmd_dump},
81 {"get", N_("Save media stream to a file"), cmd_get},
82 {"info", N_("Inspect the configuration and the script properties"), cmd_info},
83 {"scan", N_("Scan and print the found embedded media URLs"), cmd_scan},
84 /* version */
85 {"--version", "", _cmd_version},
86 {"-v", "", _cmd_version},
87 /* help */
88 {"--help", "", _cmd_help},
89 {"help", "", _cmd_help},
90 {"-h", "", _cmd_help},
91 /* eol */
92 {NULL, NULL}
95 typedef struct builtin_cmd_s *builtin_cmd_t;
97 static gint _show_man_page(const gchar *quvi_cmd)
99 gchar *p;
100 gint r;
102 p = g_strdup_printf("quvi-%s", quvi_cmd);
103 r = _exec_man_man(p);
104 g_free(p);
106 return (r);
109 static gint _cmd_help(gint argc, gchar **argv)
111 builtin_cmd_t p = (builtin_cmd_t) builtin_cmds;
113 if (argc >1)
115 _show_man_page(argv[1]);
116 return (EXIT_SUCCESS);
119 g_print(_("Usage: quvi [--version] [--help] COMMAND [ARGS]\n\n"));
120 g_print(_("quvi commands are:\n"));
122 while (p->cmd != NULL)
124 if (g_strcmp0(p->cmd, "help") != 0
125 && g_str_has_prefix(p->cmd, "-") == FALSE)
127 g_print(" %-6s %s\n", p->cmd, p->descr);
129 ++p;
132 g_print(_("\nSee 'quvi help COMMAND' for more information on a "
133 "specific command.\n"));
135 return (EXIT_SUCCESS);
138 static const gchar copyr[] =
139 "Copyright (C) 2012,2013 Toni Gundogdu <legatvs@gmail.com>\n"
140 "quvi comes with ABSOLUTELY NO WARRANTY. You may redistribute copies of\n"
141 "quvi under the terms of the GNU Affero General Public License version 3\n"
142 "or later. For more information, see "
143 "<http://www.gnu.org/licenses/agpl.html>.\n\n"
144 "To contact the developers, please mail to "
145 "<quvi-devel@lists.sourceforge.net>";
147 static gint _cmd_version(gint argc, gchar **argv)
149 g_print("quvi %s\n built on %s for %s\n"
150 " with %s, %s\n"
151 " configuration: %s\n"
152 "libquvi %s\n built on %s for %s\n"
153 " with %s\n"
154 " configuration: %s\n"
155 "libquvi-scripts %s\n"
156 " configuration: %s\n",
157 VN, BUILD_TIME, CANONICAL_TARGET, CC, CFLAGS, BUILD_OPTS,
158 quvi_version(QUVI_VERSION),
159 quvi_version(QUVI_VERSION_BUILD_TIME),
160 quvi_version(QUVI_VERSION_BUILD_TARGET),
161 quvi_version(QUVI_VERSION_BUILD_CC_CFLAGS),
162 quvi_version(QUVI_VERSION_CONFIGURATION),
163 quvi_version(QUVI_VERSION_SCRIPTS),
164 quvi_version(QUVI_VERSION_SCRIPTS_CONFIGURATION));
165 g_printerr("\n%s\n", copyr);
166 return (EXIT_SUCCESS);
169 static gint _run_internal_cmd(gint argc, gchar **argv)
171 builtin_cmd_t p = (builtin_cmd_t) builtin_cmds;
172 while (p->cmd != NULL)
174 if (g_strcmp0(p->cmd, cmd) == 0)
175 return (p->cb(argc, argv));
176 ++p;
178 g_printerr(_("error: `%s' is not a quvi command. See 'quvi help'.\n"), cmd);
179 return (EXIT_FAILURE);
182 static void _opts_free()
184 /* core */
186 g_free(opts.core.subtitle_export_format);
187 g_free(opts.core.subtitle_language);
188 g_free(opts.core.print_format);
189 g_free(opts.core.verbosity);
190 g_free(opts.core.stream);
192 /* exec */
194 g_strfreev(opts.exec.external);
196 /* get */
198 g_strfreev(opts.get.output_regex);
199 g_free(opts.get.output_name);
200 g_free(opts.get.output_file);
201 g_free(opts.get.output_dir);
203 /* http */
205 g_free(opts.http.user_agent);
207 /* other */
209 g_strfreev(opts.rargs);
211 memset(&opts, 0, sizeof(struct opts_s));
214 static gint _cleanup()
216 _opts_free();
218 g_free(argv0);
219 argv0 = NULL;
221 g_free(cmd);
222 cmd = NULL;
224 return (exit_status);
227 gint main(gint argc, gchar **argv)
229 exit_status = EXIT_SUCCESS;
230 argv0 = NULL;
232 /* quvi COMMAND. */
233 cmd = g_strdup(argv[1]);
234 if (cmd == NULL)
235 cmd = g_strdup("help");
237 argc--;
238 argv++;
240 _setup_gettext();
241 #if !GLIB_CHECK_VERSION(2,35,0)
242 g_type_init();
243 #endif
245 exit_status = _run_internal_cmd(argc, argv);
246 return (_cleanup());
249 /* vim: set ts=2 sw=2 tw=72 expandtab: */