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/>.
25 #include <glib-object.h>
26 #include <glib/gi18n.h>
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
)
51 cmdl
= g_strdup_printf("man %s", page
);
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
);
66 static gint
_cmd_version(gint
, gchar
**);
67 static gint
_cmd_help(gint
, gchar
**);
69 typedef gint (*cmd_cb
)(gint
, gchar
**);
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
},
85 {"--version", "", _cmd_version
},
86 {"-v", "", _cmd_version
},
88 {"--help", "", _cmd_help
},
89 {"help", "", _cmd_help
},
90 {"-h", "", _cmd_help
},
95 typedef struct builtin_cmd_s
*builtin_cmd_t
;
97 static gint
_show_man_page(const gchar
*quvi_cmd
)
102 p
= g_strdup_printf("quvi-%s", quvi_cmd
);
103 r
= _exec_man_man(p
);
109 static gint
_cmd_help(gint argc
, gchar
**argv
)
111 builtin_cmd_t p
= (builtin_cmd_t
) builtin_cmds
;
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
);
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"
151 " configuration: %s\n"
152 "libquvi %s\n built on %s for %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
));
178 g_printerr(_("error: `%s' is not a quvi command. See 'quvi help'.\n"), cmd
);
179 return (EXIT_FAILURE
);
182 static void _opts_free()
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
);
194 g_strfreev(opts
.exec
.external
);
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
);
205 g_free(opts
.http
.user_agent
);
209 g_strfreev(opts
.rargs
);
211 memset(&opts
, 0, sizeof(struct opts_s
));
214 static gint
_cleanup()
224 return (exit_status
);
227 gint
main(gint argc
, gchar
**argv
)
229 exit_status
= EXIT_SUCCESS
;
233 cmd
= g_strdup(argv
[1]);
235 cmd
= g_strdup("help");
241 #if !GLIB_CHECK_VERSION(2,35,0)
245 exit_status
= _run_internal_cmd(argc
, argv
);
249 /* vim: set ts=2 sw=2 tw=72 expandtab: */