"Fossies" - the Fresh Open Source Software Archive

Member "pkg-config-0.29.2/main.c" (19 Mar 2017, 24615 Bytes) of package /linux/misc/pkg-config-0.29.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "main.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 0.29.1_vs_0.29.2.

    1 /*
    2  * Copyright (C) 2001, 2002 Red Hat Inc.
    3  *
    4  * This program is free software; you can redistribute it and/or
    5  * modify it under the terms of the GNU General Public License as
    6  * published by the Free Software Foundation; either version 2 of the
    7  * License, or (at your option) any later version.
    8  *
    9  * This program is distributed in the hope that it will be useful, but
   10  * WITHOUT ANY WARRANTY; without even the implied warranty of
   11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   12  * General Public License for more details.
   13  *
   14  * You should have received a copy of the GNU General Public License
   15  * along with this program; if not, write to the Free Software
   16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
   17  * 02111-1307, USA.
   18  */
   19 
   20 #ifdef HAVE_CONFIG_H
   21 #include "config.h"
   22 #endif
   23 
   24 #include "pkg.h"
   25 #include "parse.h"
   26 
   27 #include <stdlib.h>
   28 #include <string.h>
   29 #include <ctype.h>
   30 #include <stdio.h>
   31 
   32 #ifdef G_OS_WIN32
   33 #define STRICT
   34 #include <windows.h>
   35 #undef STRICT
   36 #endif
   37 
   38 char *pcsysrootdir = NULL;
   39 char *pkg_config_pc_path = NULL;
   40 
   41 static gboolean want_my_version = FALSE;
   42 static gboolean want_version = FALSE;
   43 static FlagType pkg_flags = 0;
   44 static gboolean want_list = FALSE;
   45 static gboolean want_static_lib_list = ENABLE_INDIRECT_DEPS;
   46 static gboolean want_short_errors = FALSE;
   47 static gboolean want_uninstalled = FALSE;
   48 static char *variable_name = NULL;
   49 static gboolean want_exists = FALSE;
   50 static gboolean want_provides = FALSE;
   51 static gboolean want_requires = FALSE;
   52 static gboolean want_requires_private = FALSE;
   53 static gboolean want_validate = FALSE;
   54 static char *required_atleast_version = NULL;
   55 static char *required_exact_version = NULL;
   56 static char *required_max_version = NULL;
   57 static char *required_pkgconfig_version = NULL;
   58 static gboolean want_silence_errors = FALSE;
   59 static gboolean want_variable_list = FALSE;
   60 static gboolean want_debug_spew = FALSE;
   61 static gboolean want_verbose_errors = FALSE;
   62 static gboolean want_stdout_errors = FALSE;
   63 static gboolean output_opt_set = FALSE;
   64 
   65 void
   66 debug_spew (const char *format, ...)
   67 {
   68   va_list args;
   69   gchar *str;
   70   FILE* stream;
   71 
   72   g_return_if_fail (format != NULL);
   73 
   74   if (!want_debug_spew)
   75     return;
   76 
   77   va_start (args, format);
   78   str = g_strdup_vprintf (format, args);
   79   va_end (args);
   80 
   81   if (want_stdout_errors)
   82     stream = stdout;
   83   else
   84     stream = stderr;
   85 
   86   fputs (str, stream);
   87   fflush (stream);
   88 
   89   g_free (str);
   90 }
   91 
   92 void
   93 verbose_error (const char *format, ...)
   94 {
   95   va_list args;
   96   gchar *str;
   97   FILE* stream;
   98   
   99   g_return_if_fail (format != NULL);
  100 
  101   if (!want_verbose_errors)
  102     return;
  103 
  104   va_start (args, format);
  105   str = g_strdup_vprintf (format, args);
  106   va_end (args);
  107 
  108   if (want_stdout_errors)
  109     stream = stdout;
  110   else
  111     stream = stderr;
  112   
  113   fputs (str, stream);
  114   fflush (stream);
  115 
  116   g_free (str);
  117 }
  118 
  119 static gboolean
  120 define_variable_cb (const char *opt, const char *arg, gpointer data,
  121                     GError **error)
  122 {
  123   char *varname;
  124   char *varval;
  125   char *tmp;
  126 
  127   tmp = g_strdup (arg);
  128 
  129   varname = tmp;
  130   while (*varname && isspace ((guchar)*varname))
  131     ++varname;
  132 
  133   varval = varname;
  134   while (*varval && *varval != '=' && *varval != ' ')
  135     ++varval;
  136 
  137   while (*varval && (*varval == '=' || *varval == ' '))
  138     {
  139       *varval = '\0';
  140       ++varval;
  141     }
  142 
  143   if (*varval == '\0')
  144     {
  145       fprintf (stderr, "--define-variable argument does not have a value "
  146                "for the variable\n");
  147       exit (1);
  148     }
  149 
  150   define_global_variable (varname, varval);
  151 
  152   g_free (tmp);
  153   return TRUE;
  154 }
  155 
  156 static gboolean
  157 output_opt_cb (const char *opt, const char *arg, gpointer data,
  158                GError **error)
  159 {
  160   static gboolean vercmp_opt_set = FALSE;
  161 
  162   /* only allow one output mode, with a few exceptions */
  163   if (output_opt_set)
  164     {
  165       gboolean bad_opt = TRUE;
  166 
  167       /* multiple flag options (--cflags --libs-only-l) allowed */
  168       if (pkg_flags != 0 &&
  169           (strcmp (opt, "--libs") == 0 ||
  170            strcmp (opt, "--libs-only-l") == 0 ||
  171            strcmp (opt, "--libs-only-other") == 0 ||
  172            strcmp (opt, "--libs-only-L") == 0 ||
  173            strcmp (opt, "--cflags") == 0 ||
  174            strcmp (opt, "--cflags-only-I") == 0 ||
  175            strcmp (opt, "--cflags-only-other") == 0))
  176         bad_opt = FALSE;
  177 
  178       /* --print-requires and --print-requires-private allowed */
  179       if ((want_requires && strcmp (opt, "--print-requires-private") == 0) ||
  180           (want_requires_private && strcmp (opt, "--print-requires") == 0))
  181         bad_opt = FALSE;
  182 
  183       /* --exists allowed with --atleast/exact/max-version */
  184       if (want_exists && !vercmp_opt_set &&
  185           (strcmp (opt, "--atleast-version") == 0 ||
  186            strcmp (opt, "--exact-version") == 0 ||
  187            strcmp (opt, "--max-version") == 0))
  188         bad_opt = FALSE;
  189 
  190       if (bad_opt)
  191         {
  192           fprintf (stderr, "Ignoring incompatible output option \"%s\"\n",
  193                    opt);
  194           fflush (stderr);
  195           return TRUE;
  196         }
  197     }
  198 
  199   if (strcmp (opt, "--version") == 0)
  200     want_my_version = TRUE;
  201   else if (strcmp (opt, "--modversion") == 0)
  202     want_version = TRUE;
  203   else if (strcmp (opt, "--libs") == 0)
  204     pkg_flags |= LIBS_ANY;
  205   else if (strcmp (opt, "--libs-only-l") == 0)
  206     pkg_flags |= LIBS_l;
  207   else if (strcmp (opt, "--libs-only-other") == 0)
  208     pkg_flags |= LIBS_OTHER;
  209   else if (strcmp (opt, "--libs-only-L") == 0)
  210     pkg_flags |= LIBS_L;
  211   else if (strcmp (opt, "--cflags") == 0)
  212     pkg_flags |= CFLAGS_ANY;
  213   else if (strcmp (opt, "--cflags-only-I") == 0)
  214     pkg_flags |= CFLAGS_I;
  215   else if (strcmp (opt, "--cflags-only-other") == 0)
  216     pkg_flags |= CFLAGS_OTHER;
  217   else if (strcmp (opt, "--variable") == 0)
  218     variable_name = g_strdup (arg);
  219   else if (strcmp (opt, "--exists") == 0)
  220     want_exists = TRUE;
  221   else if (strcmp (opt, "--print-variables") == 0)
  222     want_variable_list = TRUE;
  223   else if (strcmp (opt, "--uninstalled") == 0)
  224     want_uninstalled = TRUE;
  225   else if (strcmp (opt, "--atleast-version") == 0)
  226     {
  227       required_atleast_version = g_strdup (arg);
  228       want_exists = TRUE;
  229       vercmp_opt_set = TRUE;
  230     }
  231   else if (strcmp (opt, "--exact-version") == 0)
  232     {
  233       required_exact_version = g_strdup (arg);
  234       want_exists = TRUE;
  235       vercmp_opt_set = TRUE;
  236     }
  237   else if (strcmp (opt, "--max-version") == 0)
  238     {
  239       required_max_version = g_strdup (arg);
  240       want_exists = TRUE;
  241       vercmp_opt_set = TRUE;
  242     }
  243   else if (strcmp (opt, "--list-all") == 0)
  244     want_list = TRUE;
  245   else if (strcmp (opt, "--print-provides") == 0)
  246     want_provides = TRUE;
  247   else if (strcmp (opt, "--print-requires") == 0)
  248     want_requires = TRUE;
  249   else if (strcmp (opt, "--print-requires-private") == 0)
  250     want_requires_private = TRUE;
  251   else if (strcmp (opt, "--validate") == 0)
  252     want_validate = TRUE;
  253   else
  254     return FALSE;
  255 
  256   output_opt_set = TRUE;
  257   return TRUE;
  258 }
  259 
  260 static gboolean
  261 pkg_uninstalled (Package *pkg)
  262 {
  263   /* See if > 0 pkgs were uninstalled */
  264   GList *tmp;
  265 
  266   if (pkg->uninstalled)
  267     return TRUE;
  268 
  269   tmp = pkg->requires;
  270   while (tmp != NULL)
  271     {
  272       Package *pkg = tmp->data;
  273 
  274       if (pkg_uninstalled (pkg))
  275         return TRUE;
  276 
  277       tmp = g_list_next (tmp);
  278     }
  279 
  280   return FALSE;
  281 }
  282 
  283 void
  284 print_list_data (gpointer data,
  285                  gpointer user_data)
  286 {
  287   g_print ("%s\n", (gchar *)data);
  288 }
  289 
  290 static void
  291 init_pc_path (void)
  292 {
  293 #ifdef G_OS_WIN32
  294   char *instdir, *lpath, *shpath;
  295 
  296   instdir = g_win32_get_package_installation_directory_of_module (NULL);
  297   if (instdir == NULL)
  298     {
  299       /* This only happens when GetModuleFilename() fails. If it does, that
  300        * failure should be investigated and fixed.
  301        */
  302       debug_spew ("g_win32_get_package_installation_directory_of_module failed\n");
  303       return;
  304     }
  305 
  306   lpath = g_build_filename (instdir, "lib", "pkgconfig", NULL);
  307   shpath = g_build_filename (instdir, "share", "pkgconfig", NULL);
  308   pkg_config_pc_path = g_strconcat (lpath, G_SEARCHPATH_SEPARATOR_S, shpath,
  309                                     NULL);
  310   g_free (instdir);
  311   g_free (lpath);
  312   g_free (shpath);
  313 #else
  314   pkg_config_pc_path = PKG_CONFIG_PC_PATH;
  315 #endif
  316 }
  317 
  318 static gboolean
  319 process_package_args (const char *cmdline, GList **packages, FILE *log)
  320 {
  321   gboolean success = TRUE;
  322   GList *reqs;
  323 
  324   reqs = parse_module_list (NULL, cmdline, "(command line arguments)");
  325   if (reqs == NULL)
  326     {
  327       fprintf (stderr, "Must specify package names on the command line\n");
  328       fflush (stderr);
  329       return FALSE;
  330     }
  331 
  332   for (; reqs != NULL; reqs = g_list_next (reqs))
  333     {
  334       Package *req;
  335       RequiredVersion *ver = reqs->data;
  336 
  337       /* override requested versions with cmdline options */
  338       if (required_exact_version)
  339         {
  340           g_free (ver->version);
  341           ver->comparison = EQUAL;
  342           ver->version = g_strdup (required_exact_version);
  343         }
  344       else if (required_atleast_version)
  345         {
  346           g_free (ver->version);
  347           ver->comparison = GREATER_THAN_EQUAL;
  348           ver->version = g_strdup (required_atleast_version);
  349         }
  350       else if (required_max_version)
  351         {
  352           g_free (ver->version);
  353           ver->comparison = LESS_THAN_EQUAL;
  354           ver->version = g_strdup (required_max_version);
  355         }
  356 
  357       if (want_short_errors)
  358         req = get_package_quiet (ver->name);
  359       else
  360         req = get_package (ver->name);
  361 
  362       if (log != NULL)
  363         {
  364           if (req == NULL)
  365             fprintf (log, "%s NOT-FOUND\n", ver->name);
  366           else
  367             fprintf (log, "%s %s %s\n", ver->name,
  368                      comparison_to_str (ver->comparison),
  369                      (ver->version == NULL) ? "(null)" : ver->version);
  370         }
  371 
  372       if (req == NULL)
  373         {
  374           success = FALSE;
  375           verbose_error ("No package '%s' found\n", ver->name);
  376           continue;
  377         }
  378 
  379       if (!version_test (ver->comparison, req->version, ver->version))
  380         {
  381           success = FALSE;
  382           verbose_error ("Requested '%s %s %s' but version of %s is %s\n",
  383                          ver->name,
  384                          comparison_to_str (ver->comparison),
  385                          ver->version,
  386                          req->name,
  387                          req->version);
  388           if (req->url)
  389             verbose_error ("You may find new versions of %s at %s\n",
  390                            req->name, req->url);
  391           continue;
  392         }
  393 
  394       *packages = g_list_prepend (*packages, req);
  395     }
  396 
  397   *packages = g_list_reverse (*packages);
  398 
  399   return success;
  400 }
  401 
  402 static const GOptionEntry options_table[] = {
  403   { "version", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
  404     &output_opt_cb, "output version of pkg-config", NULL },
  405   { "modversion", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
  406     &output_opt_cb, "output version for package", NULL },
  407   { "atleast-pkgconfig-version", 0, 0, G_OPTION_ARG_STRING,
  408     &required_pkgconfig_version,
  409     "require given version of pkg-config", "VERSION" },
  410   { "libs", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, &output_opt_cb,
  411     "output all linker flags", NULL },
  412   { "static", 0, 0, G_OPTION_ARG_NONE, &want_static_lib_list,
  413     "output linker flags for static linking", NULL },
  414   { "short-errors", 0, 0, G_OPTION_ARG_NONE, &want_short_errors,
  415     "print short errors", NULL },
  416   { "libs-only-l", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
  417     &output_opt_cb, "output -l flags", NULL },
  418   { "libs-only-other", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
  419     &output_opt_cb, "output other libs (e.g. -pthread)", NULL },
  420   { "libs-only-L", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
  421     &output_opt_cb, "output -L flags", NULL },
  422   { "cflags", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, &output_opt_cb,
  423     "output all pre-processor and compiler flags", NULL },
  424   { "cflags-only-I", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
  425     &output_opt_cb, "output -I flags", NULL },
  426   { "cflags-only-other", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
  427     &output_opt_cb, "output cflags not covered by the cflags-only-I option",
  428     NULL },
  429   { "variable", 0, 0, G_OPTION_ARG_CALLBACK, &output_opt_cb,
  430     "get the value of variable named NAME", "NAME" },
  431   { "define-variable", 0, 0, G_OPTION_ARG_CALLBACK, &define_variable_cb,
  432     "set variable NAME to VALUE", "NAME=VALUE" },
  433   { "exists", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, &output_opt_cb,
  434     "return 0 if the module(s) exist", NULL },
  435   { "print-variables", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
  436     &output_opt_cb, "output list of variables defined by the module", NULL },
  437   { "uninstalled", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
  438     &output_opt_cb, "return 0 if the uninstalled version of one or more "
  439     "module(s) or their dependencies will be used", NULL },
  440   { "atleast-version", 0, 0, G_OPTION_ARG_CALLBACK, &output_opt_cb,
  441     "return 0 if the module is at least version VERSION", "VERSION" },
  442   { "exact-version", 0, 0, G_OPTION_ARG_CALLBACK, &output_opt_cb,
  443     "return 0 if the module is at exactly version VERSION", "VERSION" },
  444   { "max-version", 0, 0, G_OPTION_ARG_CALLBACK, &output_opt_cb,
  445     "return 0 if the module is at no newer than version VERSION", "VERSION" },
  446   { "list-all", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
  447     &output_opt_cb, "list all known packages", NULL },
  448   { "debug", 0, 0, G_OPTION_ARG_NONE, &want_debug_spew,
  449     "show verbose debug information", NULL },
  450   { "print-errors", 0, 0, G_OPTION_ARG_NONE, &want_verbose_errors,
  451     "show verbose information about missing or conflicting packages "
  452     "(default unless --exists or --atleast/exact/max-version given on the "
  453     "command line)", NULL },
  454   { "silence-errors", 0, 0, G_OPTION_ARG_NONE, &want_silence_errors,
  455     "be silent about errors (default when --exists or "
  456     "--atleast/exact/max-version given on the command line)", NULL },
  457   { "errors-to-stdout", 0, 0, G_OPTION_ARG_NONE, &want_stdout_errors,
  458     "print errors from --print-errors to stdout not stderr", NULL },
  459   { "print-provides", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
  460     &output_opt_cb, "print which packages the package provides", NULL },
  461   { "print-requires", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
  462     &output_opt_cb, "print which packages the package requires", NULL },
  463   { "print-requires-private", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
  464     &output_opt_cb, "print which packages the package requires for static "
  465     "linking", NULL },
  466   { "validate", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK,
  467     &output_opt_cb, "validate a package's .pc file", NULL },
  468   { "define-prefix", 0, 0, G_OPTION_ARG_NONE, &define_prefix,
  469     "try to override the value of prefix for each .pc file found with a "
  470     "guesstimated value based on the location of the .pc file", NULL },
  471   { "dont-define-prefix", 0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE,
  472     &define_prefix, "don't try to override the value of prefix for each .pc "
  473     "file found with a guesstimated value based on the location of the .pc "
  474     "file", NULL },
  475   { "prefix-variable", 0, 0, G_OPTION_ARG_STRING, &prefix_variable,
  476     "set the name of the variable that pkg-config automatically sets",
  477     "PREFIX" },
  478 #ifdef G_OS_WIN32
  479   { "msvc-syntax", 0, 0, G_OPTION_ARG_NONE, &msvc_syntax,
  480     "output -l and -L flags for the Microsoft compiler (cl)", NULL },
  481 #endif
  482   { NULL, 0, 0, 0, NULL, NULL, NULL }
  483 };
  484 
  485 int
  486 main (int argc, char **argv)
  487 {
  488   GString *str;
  489   GList *packages = NULL;
  490   char *search_path;
  491   char *pcbuilddir;
  492   gboolean need_newline;
  493   FILE *log = NULL;
  494   GError *error = NULL;
  495   GOptionContext *opt_context;
  496 
  497   /* This is here so that we get debug spew from the start,
  498    * during arg parsing
  499    */
  500   if (getenv ("PKG_CONFIG_DEBUG_SPEW"))
  501     {
  502       want_debug_spew = TRUE;
  503       want_verbose_errors = TRUE;
  504       want_silence_errors = FALSE;
  505       debug_spew ("PKG_CONFIG_DEBUG_SPEW variable enabling debug spew\n");
  506     }
  507 
  508 
  509   /* Get the built-in search path */
  510   init_pc_path ();
  511   if (pkg_config_pc_path == NULL)
  512     {
  513       /* Even when we override the built-in search path, we still use it later
  514        * to add pc_path to the virtual pkg-config package.
  515        */
  516       verbose_error ("Failed to get default search path\n");
  517       exit (1);
  518     }
  519 
  520   search_path = getenv ("PKG_CONFIG_PATH");
  521   if (search_path) 
  522     {
  523       add_search_dirs(search_path, G_SEARCHPATH_SEPARATOR_S);
  524     }
  525   if (getenv("PKG_CONFIG_LIBDIR") != NULL) 
  526     {
  527       add_search_dirs(getenv("PKG_CONFIG_LIBDIR"), G_SEARCHPATH_SEPARATOR_S);
  528     }
  529   else
  530     {
  531       add_search_dirs(pkg_config_pc_path, G_SEARCHPATH_SEPARATOR_S);
  532     }
  533 
  534   pcsysrootdir = getenv ("PKG_CONFIG_SYSROOT_DIR");
  535   if (pcsysrootdir)
  536     {
  537       define_global_variable ("pc_sysrootdir", pcsysrootdir);
  538     }
  539   else
  540     {
  541       define_global_variable ("pc_sysrootdir", "/");
  542     }
  543 
  544   pcbuilddir = getenv ("PKG_CONFIG_TOP_BUILD_DIR");
  545   if (pcbuilddir)
  546     {
  547       define_global_variable ("pc_top_builddir", pcbuilddir);
  548     }
  549   else
  550     {
  551       /* Default appropriate for automake */
  552       define_global_variable ("pc_top_builddir", "$(top_builddir)");
  553     }
  554 
  555   if (getenv ("PKG_CONFIG_DISABLE_UNINSTALLED"))
  556     {
  557       debug_spew ("disabling auto-preference for uninstalled packages\n");
  558       disable_uninstalled = TRUE;
  559     }
  560 
  561   /* Parse options */
  562   opt_context = g_option_context_new (NULL);
  563   g_option_context_add_main_entries (opt_context, options_table, NULL);
  564   if (!g_option_context_parse(opt_context, &argc, &argv, &error))
  565     {
  566       fprintf (stderr, "%s\n", error->message);
  567       return 1;
  568     }
  569 
  570   /* If no output option was set, then --exists is the default. */
  571   if (!output_opt_set)
  572     {
  573       debug_spew ("no output option set, defaulting to --exists\n");
  574       want_exists = TRUE;
  575     }
  576 
  577   /* Error printing is determined as follows:
  578    *     - for --exists, --*-version, --list-all and no options at all,
  579    *       it's off by default and --print-errors will turn it on
  580    *     - for all other output options, it's on by default and
  581    *       --silence-errors can turn it off
  582    */
  583   if (want_exists || want_list)
  584     {
  585       debug_spew ("Error printing disabled by default due to use of output "
  586                   "options --exists, --atleast/exact/max-version, "
  587                   "--list-all or no output option at all. Value of "
  588                   "--print-errors: %d\n",
  589                   want_verbose_errors);
  590 
  591       /* Leave want_verbose_errors unchanged, reflecting --print-errors */
  592     }
  593   else
  594     {
  595       debug_spew ("Error printing enabled by default due to use of output "
  596                   "options besides --exists, --atleast/exact/max-version or "
  597                   "--list-all. Value of --silence-errors: %d\n",
  598                   want_silence_errors);
  599 
  600       if (want_silence_errors && getenv ("PKG_CONFIG_DEBUG_SPEW") == NULL)
  601         want_verbose_errors = FALSE;
  602       else
  603         want_verbose_errors = TRUE;
  604     }
  605 
  606   if (want_verbose_errors)
  607     debug_spew ("Error printing enabled\n");
  608   else
  609     debug_spew ("Error printing disabled\n");
  610 
  611   if (want_static_lib_list)
  612     enable_private_libs();
  613   else
  614     disable_private_libs();
  615 
  616   /* honor Requires.private if any Cflags are requested or any static
  617    * libs are requested */
  618 
  619   if (pkg_flags & CFLAGS_ANY || want_requires_private || want_exists ||
  620       (want_static_lib_list && (pkg_flags & LIBS_ANY)))
  621     enable_requires_private();
  622 
  623   /* ignore Requires if no Cflags or Libs are requested */
  624 
  625   if (pkg_flags == 0 && !want_requires && !want_exists)
  626     disable_requires();
  627 
  628   /* Allow errors in .pc files when listing all. */
  629   if (want_list)
  630     parse_strict = FALSE;
  631 
  632   if (want_my_version)
  633     {
  634       printf ("%s\n", VERSION);
  635       return 0;
  636     }
  637 
  638   if (required_pkgconfig_version)
  639     {
  640       if (compare_versions (VERSION, required_pkgconfig_version) >= 0)
  641         return 0;
  642       else
  643         return 1;
  644     }
  645 
  646   package_init (want_list);
  647 
  648   if (want_list)
  649     {
  650       print_package_list ();
  651       return 0;
  652     }
  653 
  654   /* Collect packages from remaining args */
  655   str = g_string_new ("");
  656   while (argc > 1)
  657     {
  658       argc--;
  659       argv++;
  660 
  661       g_string_append (str, *argv);
  662       g_string_append (str, " ");
  663     }
  664 
  665   g_option_context_free (opt_context);
  666 
  667   g_strstrip (str->str);
  668 
  669   if (getenv("PKG_CONFIG_LOG") != NULL)
  670     {
  671       log = fopen (getenv ("PKG_CONFIG_LOG"), "a");
  672       if (log == NULL)
  673     {
  674       fprintf (stderr, "Cannot open log file: %s\n",
  675            getenv ("PKG_CONFIG_LOG"));
  676       exit (1);
  677     }
  678     }
  679 
  680   /* find and parse each of the packages specified */
  681   if (!process_package_args (str->str, &packages, log))
  682     return 1;
  683 
  684   if (log != NULL)
  685     fclose (log);
  686 
  687   g_string_free (str, TRUE);
  688 
  689   /* If the user just wants to check package existence or validate its .pc
  690    * file, we're all done. */
  691   if (want_exists || want_validate)
  692     return 0;
  693 
  694   if (want_variable_list)
  695     {
  696       GList *tmp;
  697       tmp = packages;
  698       while (tmp != NULL)
  699         {
  700           Package *pkg = tmp->data;
  701           if (pkg->vars != NULL)
  702             {
  703               /* Sort variables for consistent output */
  704               GList *keys = g_hash_table_get_keys (pkg->vars);
  705               keys = g_list_sort (keys, (GCompareFunc)g_strcmp0);
  706               g_list_foreach (keys, print_list_data, NULL);
  707               g_list_free (keys);
  708             }
  709           tmp = g_list_next (tmp);
  710           if (tmp) printf ("\n");
  711         }
  712       need_newline = FALSE;
  713     }
  714 
  715   if (want_uninstalled)
  716     {
  717       /* See if > 0 pkgs (including dependencies recursively) were uninstalled */
  718       GList *tmp;
  719       tmp = packages;
  720       while (tmp != NULL)
  721         {
  722           Package *pkg = tmp->data;
  723 
  724           if (pkg_uninstalled (pkg))
  725             return 0;
  726 
  727           tmp = g_list_next (tmp);
  728         }
  729 
  730       return 1;
  731     }
  732 
  733   if (want_version)
  734     {
  735       GList *tmp;
  736       tmp = packages;
  737       while (tmp != NULL)
  738         {
  739           Package *pkg = tmp->data;
  740 
  741           printf ("%s\n", pkg->version);
  742 
  743           tmp = g_list_next (tmp);
  744         }
  745     }
  746 
  747  if (want_provides)
  748    {
  749      GList *tmp;
  750      tmp = packages;
  751      while (tmp != NULL)
  752        {
  753          Package *pkg = tmp->data;
  754          char *key;
  755          key = pkg->key;
  756          while (*key == '/')
  757            key++;
  758          if (strlen(key) > 0)
  759            printf ("%s = %s\n", key, pkg->version);
  760          tmp = g_list_next (tmp);
  761        }
  762    }
  763 
  764   if (want_requires)
  765     {
  766       GList *pkgtmp;
  767       for (pkgtmp = packages; pkgtmp != NULL; pkgtmp = g_list_next (pkgtmp))
  768         {
  769           Package *pkg = pkgtmp->data;
  770           GList *reqtmp;
  771 
  772           /* process Requires: */
  773           for (reqtmp = pkg->requires; reqtmp != NULL; reqtmp = g_list_next (reqtmp))
  774             {
  775               Package *deppkg = reqtmp->data;
  776               RequiredVersion *req;
  777               req = g_hash_table_lookup(pkg->required_versions, deppkg->key);
  778               if ((req == NULL) || (req->comparison == ALWAYS_MATCH))
  779                 printf ("%s\n", deppkg->key);
  780               else
  781                 printf ("%s %s %s\n", deppkg->key,
  782                   comparison_to_str(req->comparison),
  783                   req->version);
  784             }
  785         }
  786     }
  787   if (want_requires_private)
  788     {
  789       GList *pkgtmp;
  790       for (pkgtmp = packages; pkgtmp != NULL; pkgtmp = g_list_next (pkgtmp))
  791         {
  792           Package *pkg = pkgtmp->data;
  793           GList *reqtmp;
  794           /* process Requires.private: */
  795           for (reqtmp = pkg->requires_private; reqtmp != NULL; reqtmp = g_list_next (reqtmp))
  796             {
  797 
  798               Package *deppkg = reqtmp->data;
  799               RequiredVersion *req;
  800 
  801               if (g_list_find (pkg->requires, reqtmp->data))
  802                 continue;
  803 
  804               req = g_hash_table_lookup(pkg->required_versions, deppkg->key);
  805               if ((req == NULL) || (req->comparison == ALWAYS_MATCH))
  806                 printf ("%s\n", deppkg->key);
  807               else
  808                 printf ("%s %s %s\n", deppkg->key,
  809                   comparison_to_str(req->comparison),
  810                   req->version);
  811             }
  812         }
  813     }
  814   
  815   /* Print all flags; then print a newline at the end. */
  816   need_newline = FALSE;
  817 
  818   if (variable_name)
  819     {
  820       char *str = packages_get_var (packages, variable_name);
  821       printf ("%s", str);
  822       g_free (str);
  823       need_newline = TRUE;
  824     }
  825 
  826   if (pkg_flags != 0)
  827     {
  828       char *str = packages_get_flags (packages, pkg_flags);
  829       printf ("%s", str);
  830       g_free (str);
  831       need_newline = TRUE;
  832     }
  833 
  834   if (need_newline)
  835     printf ("\n");
  836 
  837   return 0;
  838 }