--- ../orig/pkg-config-0.20/main.c 2005-10-16 20:04:50.000000000 +0200 +++ main.c 2005-12-15 18:17:21.000000000 +0100 @@ -167,10 +167,133 @@ } return FALSE; } +typedef struct +{ + Package *pkg; + RequiredVersion *req; +} FailedPackage; + +typedef struct +{ + gboolean failed, make_suggestion; + GList *missing; + GList *failures; +} ErrorSummary; + +static char * +stringify_pkg_names (GList *list, char *extra) +{ + /* Ought to be enough... */ + static char buf[128000]; + buf[0] = '\0'; + GList *iter = list; + + while (iter != NULL) + { + FailedPackage *failure = iter->data; + sprintf(buf, "%s'%s%s'", buf, failure->req->name, extra); + if (iter->next) + { + if (iter->next->next) + strcat(buf, ", "); + else + strcat(buf, " and "); + } + iter = iter->next; + } + return buf; +} + +static ErrorSummary * +ErrorSummary_init (gboolean make_suggestion) +{ + ErrorSummary *summary = g_new0 (ErrorSummary, 1); + summary->failed = FALSE; + summary->make_suggestion = make_suggestion; + summary->missing = NULL; + summary->failures = NULL; + return summary; +} + +static void +ErrorSummary_add_error (ErrorSummary *summary, + Package *pkg, + RequiredVersion *req, + gboolean is_missing) +{ + summary->failed = TRUE; + FailedPackage *failure = g_new0 (FailedPackage, 1); + failure->pkg = pkg; + failure->req = req; + if (is_missing) + summary->missing = g_list_prepend (summary->missing, failure); + else + summary->failures = g_list_prepend (summary->failures, failure); +} + +static void +ErrorSummary_summarize_missing_packages (GList *missing) +{ + verbose_error ("%s %s not found\n", + g_list_length(missing) == 1 ? "Package" : "Packages", + stringify_pkg_names(missing, "")); +} + +static void +ErrorSummary_summarize_failed_packages (GList *failures) +{ + GList *iter = failures; + while (iter != NULL) + { + FailedPackage *fpkg = iter->data; + Package *pkg = fpkg->pkg; + RequiredVersion *req = fpkg->req; + verbose_error ("Requested '%s %s %s' but installed version" + " of %s is %s\n", + req->name, + comparison_to_str (req->comparison), + req->version, + pkg->name, + pkg->version); + + if (pkg->url) + verbose_error ("You may find new versions of %s at %s\n", + pkg->name, pkg->url); + iter = iter->next; + } +} + +static void +ErrorSummary_do_suggest (GList *missing) +{ + gboolean single = g_list_length(missing) == 1; + + verbose_error + ("%s %s was not found in the pkg-config search path.\n", + single ? "Package" : "Packages", + stringify_pkg_names(missing, "")); + verbose_error + ("Perhaps you should add the %s containing the %s:\n" + " %s\n" + "to the PKG_CONFIG_PATH environment variable\n", + single ? "directory" : "directories", + single ? "file" : "files", + stringify_pkg_names(missing, ".pc")); +} + +static void +ErrorSummary_summarize (ErrorSummary *summary) +{ + ErrorSummary_summarize_missing_packages (summary->missing); + ErrorSummary_summarize_failed_packages (summary->failures); + if (summary->make_suggestion && summary->missing) + ErrorSummary_do_suggest (summary->missing); +} + int main (int argc, char **argv) { int want_my_version = 0; int want_version = 0; @@ -457,11 +580,11 @@ poptFreeContext (opt_context); g_strstrip (str->str); { - gboolean failed = FALSE; + ErrorSummary *error_summary = ErrorSummary_init (!want_short_errors); GSList *reqs; GSList *iter; reqs = parse_module_list (NULL, str->str, "(command line arguments)"); @@ -471,49 +594,35 @@ while (iter != NULL) { Package *req; RequiredVersion *ver = iter->data; - if (want_short_errors) - req = get_package_quiet (ver->name); - else - req = get_package (ver->name); + req = get_package (ver->name); if (req == NULL) { - failed = TRUE; - verbose_error ("No package '%s' found\n", ver->name); + ErrorSummary_add_error (error_summary, req, ver, TRUE); 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); - + ErrorSummary_add_error (error_summary, req, ver, FALSE); goto nextiter; } packages = g_slist_prepend (packages, req); nextiter: iter = g_slist_next (iter); } - if (failed) { - return 1; - } - + if (error_summary->failed) + { + ErrorSummary_summarize (error_summary); + return 1; + } } g_string_free (str, TRUE); packages = g_slist_reverse (packages);