2 * Copyright (C) 2004 Thomas Vander Stichele <thomas@apestaart.org>
4 * gst-run.c: tool to launch GStreamer tools with correct major/minor
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library 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 GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
32 /* global statics for option parsing */
33 static gboolean _print = FALSE;
34 static gchar *_arg_mm = NULL;
35 static gboolean _arg_list_mm = FALSE;
37 /* popt options table for the wrapper */
38 static GOptionEntry wrapper_options[] = {
39 {"print", 'p', 0, G_OPTION_ARG_NONE, &_print,
40 "print wrapped command line options", NULL},
41 {"gst-mm", 0, 0, G_OPTION_ARG_STRING, &_arg_mm,
42 "Force major/minor version", "VERSION"},
43 {"gst-list-mm", 0, 0, G_OPTION_ARG_NONE, &_arg_list_mm,
44 "List found major/minor versions", NULL},
48 /* print out the major/minor, which is the hash key */
50 hash_print_key (gchar * key, gchar * value)
52 g_print ("%s\n", (gchar *) key);
55 /* return value like strcmp, but compares major/minor numerically */
57 compare_major_minor (const gchar * first, const gchar * second)
59 gchar **firsts, **seconds;
60 gint fmaj, fmin, smaj, smin;
63 firsts = g_strsplit (first, ".", 0);
64 seconds = g_strsplit (second, ".", 0);
66 if (firsts[0] == NULL || firsts[1] == NULL) {
70 if (seconds[0] == NULL || seconds[1] == NULL) {
75 fmaj = atoi (firsts[0]);
76 fmin = atoi (firsts[1]);
77 smaj = atoi (seconds[0]);
78 smin = atoi (seconds[1]);
102 g_strfreev (seconds);
107 find_highest_version (gchar * key, gchar * value, gchar ** highest)
109 if (*highest == NULL) {
110 /* first value, so just set it */
113 if (compare_major_minor (key, *highest) > 0)
117 /* Libtool creates shell scripts named "base" that calls actual binaries as
118 * .libs/lt-base. If we detect this is a libtool script, unmangle so we
119 * find the right binaries */
121 unmangle_libtool (gchar ** dir, gchar ** base)
123 gchar *new_dir, *new_base;
130 /* we assume libtool when base starts with lt- and dir ends with .libs */
131 if (!g_str_has_prefix (*base, "lt-"))
133 if (!g_str_has_suffix (*dir, ".libs"))
136 new_base = g_strdup (&((*base)[3]));
137 new_dir = g_path_get_dirname (*dir);
144 /* Returns a directory path that contains the binary given as an argument.
145 * If the binary given contains a path, it gets looked for in that path.
146 * If it doesn't contain a path, it gets looked for in the standard path.
148 * The returned string is newly allocated.
151 get_dir_of_binary (const gchar * binary)
156 base = g_path_get_basename (binary);
157 dir = g_path_get_dirname (binary);
159 /* if putting these two together yields the same as binary,
160 * then we have the right breakup. If not, it's because no path was
161 * specified which caused get_basename to return "." */
162 full = g_build_filename (dir, base, NULL);
164 if (strcmp (full, binary) != 0) {
165 if (strcmp (dir, ".") != 0) {
166 g_warning ("This should not happen, g_path_get_dirname () has changed.");
173 /* we know no path was specified, so search standard path for binary */
175 full = g_find_program_in_path (base);
177 g_warning ("This should not happen, %s not in standard path.", base);
186 dir = g_path_get_dirname (full);
192 /* Search the given directory for candidate binaries matching the base binary.
193 * Return a GHashTable of major/minor -> directory pairs
196 get_candidates (const gchar * dir, const gchar * base)
199 GError *error = NULL;
202 gchar *suffix, *copy;
205 GPatternSpec *spec, *specexe;
210 GHashTable *candidates = NULL;
212 candidates = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
214 /* compile our pattern specs */
215 pattern = g_strdup_printf ("%s-*.*", base);
216 spec = g_pattern_spec_new (pattern);
218 pattern = g_strdup_printf ("%s-*.*.exe", base);
219 specexe = g_pattern_spec_new (pattern);
222 /* get all dirs from the path and prepend with given dir */
223 path = g_strdup_printf ("%s%c%s",
224 dir, G_SEARCHPATH_SEPARATOR, g_getenv ("PATH"));
225 dirs = g_strsplit (path, G_SEARCHPATH_SEPARATOR_S, 0);
228 /* check all of these in reverse order by winding to bottom and going up */
233 while (cur != &dirs[0]) {
235 if (!g_file_test (*cur, G_FILE_TEST_EXISTS) ||
236 !g_file_test (*cur, G_FILE_TEST_IS_DIR)) {
240 gdir = g_dir_open (*cur, 0, &error);
242 g_warning ("Could not open dir %s: %s", *cur, error->message);
243 g_error_free (error);
246 while ((entry = g_dir_read_name (gdir))) {
247 if (g_pattern_match_string (spec, entry)
248 || g_pattern_match_string (specexe, entry)) {
251 /* is it executable ? */
252 full = g_build_filename (*cur, entry, NULL);
253 if (!g_file_test (full, G_FILE_TEST_IS_EXECUTABLE)) {
259 /* strip base and dash from it */
260 suffix = g_strdup (&(entry[strlen (base) + 1]));
261 copy = g_strdup (suffix);
263 /* strip possible .exe from copy */
264 if (g_strrstr (copy, ".exe"))
265 g_strrstr (copy, ".exe")[0] = '\0';
267 /* stricter pattern check: check if it only contains digits or dots */
268 g_strcanon (copy, "0123456789.", 'X');
269 if (strstr (copy, "X")) {
275 g_hash_table_insert (candidates, suffix, g_strdup (*cur));
281 g_pattern_spec_free (spec);
286 main (int argc, char **argv)
288 GHashTable *candidates;
291 gchar *highest = NULL;
292 gchar *binary; /* actual binary we're going to run */
293 gchar *path = NULL; /* and its path */
299 dir = get_dir_of_binary (argv[0]);
300 base = g_path_get_basename (argv[0]);
302 /* parse command line options */
303 desc = g_strdup_printf ("wrapper to call versioned %s", base);
304 ctx = g_option_context_new (desc);
306 g_option_context_set_ignore_unknown_options (ctx, TRUE);
307 g_option_context_add_main_entries (ctx, wrapper_options, GETTEXT_PACKAGE);
308 if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
309 g_print ("Error initializing: %s\n", err->message);
312 g_option_context_free (ctx);
314 /* unmangle libtool if necessary */
315 unmangle_libtool (&dir, &base);
317 /* get all candidate binaries */
318 candidates = get_candidates (dir, base);
322 /* if a version was forced, look it up in the hash table */
323 dir = g_hash_table_lookup (candidates, _arg_mm);
325 g_print ("ERROR: Major/minor %s of tool %s not found.\n", _arg_mm, base);
328 binary = g_strdup_printf ("%s-%s", base, _arg_mm);
332 /* otherwise, just look up the highest version */
334 g_hash_table_foreach (candidates, (GHFunc) find_highest_version,
338 if (highest == NULL) {
339 g_print ("ERROR: No version of tool %s found.\n", base);
342 dir = g_hash_table_lookup (candidates, highest);
343 binary = g_strdup_printf ("%s-%s", base, highest);
348 path = g_build_filename (dir, binary, NULL);
351 /* print out list of major/minors we found if asked for */
352 /* FIXME: do them in order by creating a GList of keys and sort them */
354 g_hash_table_foreach (candidates, (GHFunc) hash_print_key, NULL);
355 g_hash_table_destroy (candidates);
359 /* print out command line if asked for */
364 for (i = 0; i < argc; ++i) {
365 g_print ("%s", argv[i]);
373 if (execv (path, argv) == -1) {
374 g_warning ("Error executing %s: %s (%d)", path, g_strerror (errno), errno);