"Fossies" - the Fresh Open Source Software Archive

Member "gnuastro-0.8/bin/buildprog/ui.c" (4 Nov 2018, 10114 Bytes) of package /linux/privat/gnuastro-0.8.tar.lz:


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 "ui.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 0.7_vs_0.8.

    1 /*********************************************************************
    2 BuildProgram: Compile and run programs using Gnuastro's library
    3 BuildProgram is part of GNU Astronomy Utilities (Gnuastro) package.
    4 
    5 Original author:
    6      Mohammad Akhlaghi <mohammad@akhlaghi.org>
    7 Contributing author(s):
    8 Copyright (C) 2017-2018, Free Software Foundation, Inc.
    9 
   10 Gnuastro is free software: you can redistribute it and/or modify it
   11 under the terms of the GNU General Public License as published by the
   12 Free Software Foundation, either version 3 of the License, or (at your
   13 option) any later version.
   14 
   15 Gnuastro is distributed in the hope that it will be useful, but
   16 WITHOUT ANY WARRANTY; without even the implied warranty of
   17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   18 General Public License for more details.
   19 
   20 You should have received a copy of the GNU General Public License
   21 along with Gnuastro. If not, see <http://www.gnu.org/licenses/>.
   22 **********************************************************************/
   23 #include <config.h>
   24 
   25 #include <argp.h>
   26 #include <errno.h>
   27 #include <error.h>
   28 #include <stdio.h>
   29 #include <string.h>
   30 
   31 #include <gnuastro/fits.h>
   32 
   33 #include <gnuastro-internal/timing.h>
   34 #include <gnuastro-internal/options.h>
   35 #include <gnuastro-internal/checkset.h>
   36 #include <gnuastro-internal/fixedstringmacros.h>
   37 
   38 #include "main.h"
   39 
   40 #include "ui.h"
   41 #include "authors-cite.h"
   42 
   43 
   44 
   45 
   46 
   47 /**************************************************************/
   48 /*********      Argp necessary global entities     ************/
   49 /**************************************************************/
   50 /* Definition parameters for the Argp: */
   51 const char *
   52 argp_program_version = PROGRAM_STRING "\n"
   53                        GAL_STRINGS_COPYRIGHT
   54                        "\n\nWritten/developed by "PROGRAM_AUTHORS;
   55 
   56 const char *
   57 argp_program_bug_address = PACKAGE_BUGREPORT;
   58 
   59 static char
   60 args_doc[] = "C-source [ARGUMENTS TO RUN]";
   61 
   62 const char
   63 doc[] = GAL_STRINGS_TOP_HELP_INFO PROGRAM_NAME" will compile and run a "
   64   "C program, while automatically linking with libraries that Gnuastro "
   65   "depends on. Hence you do not have to worry about explicitly linking "
   66   "with CFITSIO for example if you want to work on a FITS file, or with "
   67   "GSL if you want to use GNU Scientific Library's functions. The standard "
   68   "compiler options of `-I', `-L', and `-l' are also available for further "
   69   "customization of the build.\n"
   70   GAL_STRINGS_MORE_HELP_INFO
   71   /* After the list of options: */
   72   "\v"
   73   PACKAGE_NAME" home page: "PACKAGE_URL;
   74 
   75 
   76 
   77 
   78 
   79 
   80 
   81 
   82 
   83 
   84 
   85 
   86 
   87 
   88 
   89 
   90 
   91 
   92 
   93 
   94 /**************************************************************/
   95 /*********    Initialize & Parse command-line    **************/
   96 /**************************************************************/
   97 static void
   98 ui_initialize_options(struct buildprogparams *p,
   99                       struct argp_option *program_options,
  100                       struct argp_option *gal_commonopts_options)
  101 {
  102   size_t i;
  103   struct gal_options_common_params *cp=&p->cp;
  104 
  105 
  106   /* Set the necessary common parameters structure. */
  107   cp->poptions           = program_options;
  108   cp->program_name       = PROGRAM_NAME;
  109   cp->program_exec       = PROGRAM_EXEC;
  110   cp->program_bibtex     = PROGRAM_BIBTEX;
  111   cp->program_authors    = PROGRAM_AUTHORS;
  112   cp->coptions           = gal_commonopts_options;
  113 
  114 
  115   /* Modify common options. */
  116   for(i=0; !gal_options_is_last(&cp->coptions[i]); ++i)
  117     {
  118       /* Select individually. */
  119       switch(cp->coptions[i].key)
  120         {
  121         case GAL_OPTIONS_KEY_LOG:
  122         case GAL_OPTIONS_KEY_HDU:
  123         case GAL_OPTIONS_KEY_TYPE:
  124         case GAL_OPTIONS_KEY_SEARCHIN:
  125         case GAL_OPTIONS_KEY_NUMTHREADS:
  126         case GAL_OPTIONS_KEY_MINMAPSIZE:
  127         case GAL_OPTIONS_KEY_TABLEFORMAT:
  128         case GAL_OPTIONS_KEY_STDINTIMEOUT:
  129           cp->coptions[i].flags=OPTION_HIDDEN;
  130           cp->coptions[i].mandatory=GAL_OPTIONS_NOT_MANDATORY;
  131           break;
  132 
  133         /* `--ignorecase's default short format is `I', but here we want to
  134            follow the compiler format, hence we need `I' for
  135            `include'. Therefore, here, we'll change the key for `include'
  136            to some large number just to avoid confusion.*/
  137         case GAL_OPTIONS_KEY_IGNORECASE:
  138           cp->coptions[i].key=20000;
  139           cp->coptions[i].flags=OPTION_HIDDEN;
  140           cp->coptions[i].mandatory=GAL_OPTIONS_NOT_MANDATORY;
  141         }
  142 
  143 
  144 
  145       /* Select by group. */
  146       switch(cp->coptions[i].group)
  147         {
  148         case GAL_OPTIONS_GROUP_TESSELLATION:
  149           cp->coptions[i].doc=NULL; /* Necessary to remove title. */
  150           cp->coptions[i].flags=OPTION_HIDDEN;
  151           break;
  152         }
  153     }
  154 }
  155 
  156 
  157 
  158 
  159 
  160 /* Parse a single option: */
  161 error_t
  162 parse_opt(int key, char *arg, struct argp_state *state)
  163 {
  164   struct buildprogparams *p = state->input;
  165 
  166   /* Pass `gal_options_common_params' into the child parser.  */
  167   state->child_inputs[0] = &p->cp;
  168 
  169   /* In case the user incorrectly uses the equal sign (for example
  170      with a short format or with space in the long format, then `arg`
  171      start with (if the short version was called) or be (if the long
  172      version was called with a space) the equal sign. So, here we
  173      check if the first character of arg is the equal sign, then the
  174      user is warned and the program is stopped: */
  175   if(arg && arg[0]=='=')
  176     argp_error(state, "incorrect use of the equal sign (`=`). For short "
  177                "options, `=` should not be used and for long options, "
  178                "there should be no space between the option, equal sign "
  179                "and value");
  180 
  181   /* Set the key to this option. */
  182   switch(key)
  183     {
  184 
  185     /* Read the non-option tokens (arguments): */
  186     case ARGP_KEY_ARG:
  187       gal_list_str_add(&p->sourceargs, arg, 0);
  188       break;
  189 
  190 
  191     /* This is an option, set its value. */
  192     default:
  193       return gal_options_set_from_key(key, arg, p->cp.poptions, &p->cp);
  194     }
  195 
  196   return 0;
  197 }
  198 
  199 
  200 
  201 
  202 
  203 
  204 
  205 
  206 
  207 
  208 
  209 
  210 
  211 
  212 
  213 
  214 
  215 
  216 
  217 
  218 /**************************************************************/
  219 /***************       Sanity Check         *******************/
  220 /**************************************************************/
  221 /* Read and check ONLY the options. When arguments are involved, do the
  222    check in `ui_check_options_and_arguments'. */
  223 static void
  224 ui_read_check_only_options(struct buildprogparams *p)
  225 {
  226   size_t len;
  227 
  228   /* If an `.la' file is given, make sure it has the correct suffix. */
  229   if(p->la)
  230     {
  231       len=strlen(p->la);
  232       if(len>=4)
  233         if(strcmp(&p->la[len-3], ".la"))
  234           error(EXIT_FAILURE, 0, "`%s' is not a Libtool control file name "
  235                 "(with a `.la' suffix). The file name given to the `--la' "
  236                 "(`-a') option must be a Libtool control file", p->la);
  237     }
  238 }
  239 
  240 
  241 
  242 
  243 
  244 /* Check the options and arguments. */
  245 static void
  246 ui_check_options_and_arguments(struct buildprogparams *p)
  247 {
  248   if(p->sourceargs==NULL)
  249     error(EXIT_FAILURE, 0, "no input (C source file) given");
  250 }
  251 
  252 
  253 
  254 
  255 
  256 
  257 
  258 
  259 
  260 
  261 
  262 
  263 
  264 
  265 
  266 
  267 
  268 
  269 
  270 
  271 /**************************************************************/
  272 /***************       Preparations         *******************/
  273 /**************************************************************/
  274 
  275 void
  276 ui_preparations(struct buildprogparams *p)
  277 {
  278   /* Reverse the sourceargs list (note that the options are reversed in
  279      options.c). */
  280   gal_list_str_reverse(&p->sourceargs);
  281 
  282   /* Set the final output name. `EXEEXT' comes from the configuration
  283      script (given by BuildProgram's `Makefile.am'). */
  284   if(p->cp.output==NULL)
  285     p->cp.output=gal_checkset_automatic_output(&p->cp, p->sourceargs->v,
  286                                                EXEEXT);
  287 }
  288 
  289 
  290 
  291 
  292 
  293 
  294 
  295 
  296 
  297 
  298 
  299 
  300 
  301 
  302 
  303 
  304 
  305 
  306 
  307 /**************************************************************/
  308 /************         Set the parameters          *************/
  309 /**************************************************************/
  310 
  311 void
  312 ui_read_check_inputs_setup(int argc, char *argv[], struct buildprogparams *p)
  313 {
  314   struct gal_options_common_params *cp=&p->cp;
  315 
  316 
  317   /* Include the parameters necessary for argp from this program (`args.h')
  318      and for the common options to all Gnuastro (`commonopts.h'). We want
  319      to directly put the pointers to the fields in `p' and `cp', so we are
  320      simply including the header here to not have to use long macros in
  321      those headers which make them hard to read and modify. This also helps
  322      in having a clean environment: everything in those headers is only
  323      available within the scope of this function. */
  324 #include <gnuastro-internal/commonopts.h>
  325 #include "args.h"
  326 
  327 
  328   /* Initialize the options and necessary information.  */
  329   ui_initialize_options(p, program_options, gal_commonopts_options);
  330 
  331 
  332   /* Read the command-line options and arguments. */
  333   errno=0;
  334   if(argp_parse(&thisargp, argc, argv, 0, 0, p))
  335     error(EXIT_FAILURE, errno, "parsing arguments");
  336 
  337 
  338   /* Read the configuration files and set the common values. */
  339   gal_options_read_config_set(&p->cp);
  340 
  341 
  342   /* Read the options into the program's structure, and check them and
  343      their relations prior to printing. */
  344   ui_read_check_only_options(p);
  345 
  346 
  347   /* Print the option values if asked. Note that this needs to be done
  348      after the option checks so un-sane values are not printed in the
  349      output state. */
  350   gal_options_print_state(&p->cp);
  351 
  352 
  353   /* Check that the options and arguments fit well with each other. Note
  354      that arguments don't go in a configuration file. So this test should
  355      be done after (possibly) printing the option values. */
  356   ui_check_options_and_arguments(p);
  357 
  358 
  359   /* Read/allocate all the necessary starting arrays. */
  360   ui_preparations(p);
  361 }
  362 
  363 
  364 
  365 
  366 
  367 
  368 
  369 
  370 
  371 
  372 
  373 
  374 
  375 
  376 
  377 
  378 
  379 
  380 
  381 
  382 /**************************************************************/
  383 /************      Free allocated, report         *************/
  384 /**************************************************************/
  385 void
  386 ui_free_report(struct buildprogparams *p)
  387 {
  388   /* Free the allocated arrays: */
  389   free(p->cp.hdu);
  390   free(p->cp.output);
  391   gal_list_str_free(p->include,    1);
  392   gal_list_str_free(p->linkdir,    1);
  393   gal_list_str_free(p->linklib,    1);
  394   gal_list_str_free(p->sourceargs, 0);
  395 }