diff options
author | Dan Nicholson <dbn.lists@gmail.com> | 2012-10-31 16:30:45 -0700 |
---|---|---|
committer | Dan Nicholson <dbn.lists@gmail.com> | 2012-12-11 11:59:40 -0800 |
commit | 30e7f3131875b71dcd056d82bbade2e8f00d1e70 (patch) | |
tree | 4183a67fba3d082d2f1cbfa91369aa62b966e8e7 | |
parent | a15bb1e72e03a1917464fe809aa98bb2fceae9a3 (diff) | |
download | pkg-config-30e7f3131875b71dcd056d82bbade2e8f00d1e70.tar.gz |
Split out package processing to separate function to cleanup main
The code handling processing of the packages from the command line was
in an awkward block in main. Split it out to a separate function to keep
main mostly about output.
-rw-r--r-- | main.c | 188 |
1 files changed, 89 insertions, 99 deletions
@@ -216,6 +216,89 @@ init_pc_path (void) #endif } +static gboolean +process_package_args (const char *cmdline, GList **packages, FILE *log) +{ + gboolean success = TRUE; + GList *reqs; + + reqs = parse_module_list (NULL, cmdline, "(command line arguments)"); + if (reqs == NULL) + { + fprintf (stderr, "Must specify package names on the command line\n"); + return FALSE; + } + + for (; reqs != NULL; reqs = g_list_next (reqs)) + { + Package *req; + RequiredVersion *ver = reqs->data; + + /* override requested versions with cmdline options */ + if (required_exact_version) + { + g_free (ver->version); + ver->comparison = EQUAL; + ver->version = g_strdup (required_exact_version); + } + else if (required_atleast_version) + { + g_free (ver->version); + ver->comparison = GREATER_THAN_EQUAL; + ver->version = g_strdup (required_atleast_version); + } + else if (required_max_version) + { + g_free (ver->version); + ver->comparison = LESS_THAN_EQUAL; + ver->version = g_strdup (required_max_version); + } + + if (want_short_errors) + req = get_package_quiet (ver->name); + else + req = get_package (ver->name); + + if (log != NULL) + { + if (req == NULL) + fprintf (log, "%s NOT-FOUND\n", ver->name); + else + fprintf (log, "%s %s %s\n", ver->name, + comparison_to_str (ver->comparison), + (ver->version == NULL) ? "(null)" : ver->version); + } + + if (req == NULL) + { + success = FALSE; + verbose_error ("No package '%s' found\n", ver->name); + continue; + } + + if (!version_test (ver->comparison, req->version, ver->version)) + { + success = FALSE; + verbose_error ("Requested '%s %s %s' but version of %s is %s\n", + ver->name, + comparison_to_str (ver->comparison), + ver->version, + req->name, + req->version); + if (req->url) + verbose_error ("You may find new versions of %s at %s\n", + req->name, req->url); + continue; + } + + *packages = g_list_prepend (*packages, req); + } + + *packages = g_list_reverse (*packages); + + return success; +} + static const GOptionEntry options_table[] = { { "version", 0, 0, G_OPTION_ARG_NONE, &want_my_version, "output version of pkg-config", NULL }, @@ -500,107 +583,14 @@ main (int argc, char **argv) } } - { - gboolean failed = FALSE; - GList *reqs; - GList *iter; - - reqs = parse_module_list (NULL, str->str, - "(command line arguments)"); - - iter = reqs; - - while (iter != NULL) - { - Package *req; - RequiredVersion *ver = iter->data; - - /* override requested versions with cmdline options */ - if (required_exact_version) - { - g_free (ver->version); - ver->comparison = EQUAL; - ver->version = g_strdup (required_exact_version); - } - else if (required_atleast_version) - { - g_free (ver->version); - ver->comparison = GREATER_THAN_EQUAL; - ver->version = g_strdup (required_atleast_version); - } - else if (required_max_version) - { - g_free (ver->version); - ver->comparison = LESS_THAN_EQUAL; - ver->version = g_strdup (required_max_version); - } - - if (want_short_errors) - req = get_package_quiet (ver->name); - else - req = get_package (ver->name); - - if (log != NULL) - { - if (req == NULL) - fprintf (log, "%s NOT-FOUND", ver->name); - else - fprintf (log, "%s %s %s", ver->name, - comparison_to_str (ver->comparison), - (ver->version == NULL) ? "(null)" : ver->version); - fprintf (log, "\n"); - } - - if (req == NULL) - { - failed = TRUE; - verbose_error ("No package '%s' found\n", ver->name); - goto nextiter; - } - - if (!version_test (ver->comparison, req->version, ver->version)) - { - failed = TRUE; - verbose_error ("Requested '%s %s %s' but version of %s is %s\n", - ver->name, - comparison_to_str (ver->comparison), - ver->version, - req->name, - req->version); - - if (req->url) - verbose_error ("You may find new versions of %s at %s\n", - req->name, req->url); - - goto nextiter; - } - - packages = g_list_prepend (packages, req); - - nextiter: - iter = g_list_next (iter); - } - - if (log != NULL) - { - fclose (log); - } - - if (failed) { - return 1; - } - } - - g_string_free (str, TRUE); - - packages = g_list_reverse (packages); + /* find and parse each of the packages specified */ + if (!process_package_args (str->str, &packages, log)) + return 1; - if (packages == NULL) - { - fprintf (stderr, "Must specify package names on the command line\n"); + if (log != NULL) + fclose (log); - exit (1); - } + g_string_free (str, TRUE); if (want_exists) return 0; /* if we got here, all the packages existed. */ |