[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[gnuastro-commits] master 77b7910 057/125: Program specific global varia
From: |
Mohammad Akhlaghi |
Subject: |
[gnuastro-commits] master 77b7910 057/125: Program specific global variables for in options library |
Date: |
Sun, 23 Apr 2017 22:36:36 -0400 (EDT) |
branch: master
commit 77b7910e49d5571986fe0fcdd216e7ef9c7d695f
Author: Mohammad Akhlaghi <address@hidden>
Commit: Mohammad Akhlaghi <address@hidden>
Program specific global variables for in options library
Thanks to global variables, constant variables can be used easily in any of
the functions of `lib/options.c'. So the program name, executable file and
bibtex record are now external variables in `lib/options.h' and global
variables in the program's `ui.c'.
With this addition, the `--onlyversion' and `--cite' options can be
immediately executed and allow the program to abort without going onto read
the rest of the options.
Also, to allow easy modification of a program's author list, a new global
variable is defined (in the renamed `authors-cite.h'). Later, we can change
this to `authors-cite.h.in' and during the making of the program, build it
to get the ordered list of contributing authors from the Git source, as we
do for the documentation.
---
bin/table/args.h | 8 +++--
bin/table/{cite.h => authors-cite.h} | 8 +++--
bin/table/main.c | 2 ++
bin/table/main.h | 7 ++--
bin/table/table.h | 2 --
bin/table/ui.c | 50 +++++++++++++++++---------
doc/gnuastro.texi | 7 ++--
lib/fixedstringmacros.h | 16 ++++-----
lib/options.c | 69 ++++++++++++++++++++----------------
lib/options.h | 13 +++----
10 files changed, 106 insertions(+), 76 deletions(-)
diff --git a/bin/table/args.h b/bin/table/args.h
index 7a69fbb..a09cc70 100644
--- a/bin/table/args.h
+++ b/bin/table/args.h
@@ -36,8 +36,10 @@ along with Gnuastro. If not, see
<http://www.gnu.org/licenses/>.
/* Definition parameters for the argp: */
-const char *argp_program_version=PROG_STRING"\n"GAL_STRINGS_COPYRIGHT
- "\n\nWritten by Mohammad Akhlaghi";
+const char *argp_program_version =
+ PROGRAM_NAME" (" PACKAGE_NAME ") " PACKAGE_VERSION "\n"
+ GAL_STRINGS_COPYRIGHT
+ "\n\nWritten/developed by Mohammad Akhlaghi";
const char *argp_program_bug_address=PACKAGE_BUGREPORT;
static char args_doc[] = "ASTRdata";
@@ -48,7 +50,7 @@ static char args_doc[] = "ASTRdata";
const char doc[] =
/* Before the list of options: */
GAL_STRINGS_TOP_HELP_INFO
- PROG_NAME" can be used to view the information, select columns, or "
+ PROGRAM_NAME" can be used to view the information, select columns, or "
"convert tables. The inputs and outputs can be plain text (with "
"whitespace or comma as delimiters), FITS ascii, or FITS binary tables. "
"The output columns can either be selected by number (counting from 1), "
diff --git a/bin/table/cite.h b/bin/table/authors-cite.h
similarity index 92%
rename from bin/table/cite.h
rename to bin/table/authors-cite.h
index 6eaee12..8896e7c 100644
--- a/bin/table/cite.h
+++ b/bin/table/authors-cite.h
@@ -20,8 +20,8 @@ General Public License for more details.
You should have received a copy of the GNU General Public License
along with Gnuastro. If not, see <http://www.gnu.org/licenses/>.
**********************************************************************/
-#ifndef CITE_H
-#define CITE_H
+#ifndef AUTHORS_CITE_H
+#define AUTHORS_CITS_H
/* When any specific citation is necessary, please add its BibTeX (from ADS
hopefully) to this variable a long with a title decribing what this
@@ -31,6 +31,8 @@ along with Gnuastro. If not, see
<http://www.gnu.org/licenses/>.
See the `gnuastro_bibtex' variable in `lib/options' (from the top
Gnuastro source code directory as an example.*/
-char *bibtex="";
+char program_bibtex[]="";
+
+char program_authors[]="Mohammad Akhlaghi";
#endif
diff --git a/bin/table/main.c b/bin/table/main.c
index 8b34cf3..b5fe161 100644
--- a/bin/table/main.c
+++ b/bin/table/main.c
@@ -32,6 +32,8 @@ along with Gnuastro. If not, see
<http://www.gnu.org/licenses/>.
#include "ui.h" /* needs main.h. */
#include "table.h" /* needs main.h. */
+
+/* Main function */
int
main (int argc, char *argv[])
{
diff --git a/bin/table/main.h b/bin/table/main.h
index 2b54865..9760c3e 100644
--- a/bin/table/main.h
+++ b/bin/table/main.h
@@ -28,10 +28,9 @@ along with Gnuastro. If not, see
<http://www.gnu.org/licenses/>.
#include <options.h>
-/* Progarm name macros: */
-#define PROG_EXEC "asttable" /* Program executable name. */
-#define PROG_NAME "Table" /* Program full name. */
-#define PROG_STRING PROG_NAME" ("PACKAGE_NAME") "PACKAGE_VERSION
+/* Progarm names. */
+#define PROGRAM_NAME "Table" /* Program full name. */
+#define PROGRAM_EXEC "asttable" /* Program executable name. */
diff --git a/bin/table/table.h b/bin/table/table.h
index 8c164ec..e28f32e 100644
--- a/bin/table/table.h
+++ b/bin/table/table.h
@@ -23,8 +23,6 @@ along with Gnuastro. If not, see
<http://www.gnu.org/licenses/>.
#ifndef TABLE_H
#define TABLE_H
-
-
void
table(struct tableparams *p);
diff --git a/bin/table/ui.c b/bin/table/ui.c
index 3e6bb13..8cad636 100644
--- a/bin/table/ui.c
+++ b/bin/table/ui.c
@@ -45,15 +45,7 @@ along with Gnuastro. If not, see
<http://www.gnu.org/licenses/>.
#include "ui.h"
#include "args.h"
-#include "cite.h"
-
-
-/* Set the file names of the places where the default parameters are
- put. */
-#define SYSCONFIG_FILE SYSCONFIG_DIR "/" CONFIG_FILE
-#define USERCONFIG_FILEEND USERCONFIG_DIR CONFIG_FILE
-#define CURDIRCONFIG_FILE CURDIRCONFIG_DIR CONFIG_FILE
-
+#include "authors-cite.h"
@@ -92,8 +84,8 @@ ui_option_is_mandatory(char *name)
After setting all the values, do other forms of sanity checks that
involve more than one option.
*/
-void
-ui_read_check_options(struct tableparams *p)
+static void
+ui_read_check_only_options(struct tableparams *p)
{
size_t i;
@@ -151,7 +143,15 @@ ui_read_check_options(struct tableparams *p)
error(EXIT_FAILURE, 0, "option key %d not recognized in "
"`fill_params_from_options'", options[i].key);
}
+}
+
+
+
+
+static void
+ui_check_options_and_arguments(struct tableparams *p)
+{
/* Make sure an input file name was given and if it was a FITS file, that
a HDU is also given. */
if(p->up.filename)
@@ -289,6 +289,19 @@ ui_preparations(struct tableparams *p)
/**************************************************************/
/************ Set the parameters *************/
/**************************************************************/
+
+/* These global variables are necessary because the `options.c' library
+ also needs to use these values in various stages, so its cleaner/easier
+ to define them as global variables here (they are declared as `extern'
+ variables in `options.h'). The macros are defined in `main.h'.*/
+char program_name[]=PROGRAM_NAME;
+char program_exec[]=PROGRAM_EXEC;
+
+
+
+
+
+/* Top level function. */
void
setparams(int argc, char *argv[], struct tableparams *p)
{
@@ -309,17 +322,20 @@ setparams(int argc, char *argv[], struct tableparams *p)
error(EXIT_FAILURE, errno, "parsing arguments");
/* Read the configuration files. */
- gal_options_config_files(PROG_EXEC, PROG_NAME, options,
- gal_commonopts_options, &p->cp);
+ gal_options_config_files(options, &p->cp);
- /* Fill the parameters from the options and check their values. */
- ui_read_check_options(p);
+ /* Read the options into the program's structure, and check them and
+ their relations prior to printing. */
+ ui_read_check_only_options(p);
/* Print the necessary information if asked. Note that this needs to be
done after the sanity check so un-sane values are not printed in the
output state. */
- gal_options_print_state(PROG_NAME, bibtex, options,
- gal_commonopts_options);
+ gal_options_print_state(options);
+
+ /* Check that the options and arguments fit well with each other (note
+ that arguments don't go in a configuration file). */
+ ui_check_options_and_arguments(p);
/* Read/allocate all the necessary starting arrays */
ui_preparations(p);
diff --git a/doc/gnuastro.texi b/doc/gnuastro.texi
index ab146d8..f2579fd 100644
--- a/doc/gnuastro.texi
+++ b/doc/gnuastro.texi
@@ -18880,10 +18880,11 @@ there is no need for return values. Under more
complicated situations
where a return value might be necessary, @code{void} will be replaced
with an @code{int} in the examples above.
address@hidden cite.h
address@hidden authors-cite.h
@cindex Citation information
-This file keeps the function to be called if the user runs any of the
-programs with @option{--cite}, see @ref{Operating modes}.
+This header file keeps the global variable for the program authors and its
+BibTeX record for citation. They are used in the outputs of the common
+options @option{--version} and @option{--cite}, see @ref{Operating modes}.
@end vtable
diff --git a/lib/fixedstringmacros.h b/lib/fixedstringmacros.h
index 5d8cf72..d613aad 100644
--- a/lib/fixedstringmacros.h
+++ b/lib/fixedstringmacros.h
@@ -40,7 +40,7 @@ along with Gnuastro. If not, see
<http://www.gnu.org/licenses/>.
#define GAL_STRINGS_TOP_HELP_INFO \
- "\n"PROG_NAME" is part of "PACKAGE_STRING".\n"
+ "\n"PROGRAM_NAME" is part of "PACKAGE_STRING".\n"
@@ -48,18 +48,18 @@ along with Gnuastro. If not, see
<http://www.gnu.org/licenses/>.
#define GAL_STRINGS_MORE_HELP_INFO \
"\nFor more information, please run any of the " \
"following commands. They will respectively show you the `Invoking " \
- PROG_NAME"' subsection, the complete `"PROG_NAME"' section, or the" \
- "full "PACKAGE_NAME" manual. In particular the first contains a very "\
- "comprehensive explanation of "PROG_NAME"'s invocation and all the " \
- "options.\n\n" \
- " info "PROG_EXEC"\n\n" \
- " info "PROG_NAME"\n\n" \
+ PROGRAM_NAME"' subsection, the complete `"PROGRAM_NAME"' section, or "\
+ "the full "PACKAGE_NAME" manual. In particular the first contains a " \
+ "very comprehensive explanation of "PROGRAM_NAME"'s invocation and " \
+ "all the options.\n\n" \
+ " info "PROGRAM_EXEC"\n\n" \
+ " info "PROGRAM_NAME"\n\n" \
" info "PACKAGE_TARNAME"\n\n" \
"If you couldn't find your answer in the manual, you can get " \
"direct help from experienced Gnuastro users and developers. " \
"For more information, please run:\n\n" \
" info help-gnuastro\n\n" \
- PROG_NAME" options:" \
+ PROGRAM_NAME" options:" \
/* This can be used in the end of error messages related to option
diff --git a/lib/options.c b/lib/options.c
index de51b05..4923d8e 100644
--- a/lib/options.c
+++ b/lib/options.c
@@ -127,7 +127,7 @@ options_check_version(char *version_string)
static void
-options_print_citation_exit(char *prog_name, char *prog_bibtex)
+options_print_citation_exit()
{
char *gnuastro_bibtex=
"Gnuastro package/infrastructure\n"
@@ -155,16 +155,18 @@ options_print_citation_exit(char *prog_name, char
*prog_bibtex)
/* Print the statements. */
- printf("\nThank you for using %s (%s) %s.\n\n", prog_name, PACKAGE_NAME,
+ printf("\nThank you for using %s (%s) %s.\n\n", program_name, PACKAGE_NAME,
PACKAGE_VERSION);
printf("Citations are vital for the continued work on Gnuastro.\n\n"
"Please cite these BibTeX record(s) in your paper(s).\n\n%s\n\n",
gnuastro_bibtex);
+
/* Only print the citation for the program if one exists. */
- if(prog_bibtex[0]!='\0') printf("%s\n\n", prog_bibtex);
+ if(program_bibtex[0]!='\0') printf("%s\n\n", program_bibtex);
+ /* Print a thank you message. */
printf(" ,\n"
" {|'--.\n"
" {{\\ \\\n"
@@ -187,6 +189,30 @@ options_print_citation_exit(char *prog_name, char
*prog_bibtex)
+/* Some options need immediate attention/action before continuing to read
+ the rest of the options. In these cases we need to (maybe) check and
+ (possibly) abort immediately. */
+void
+options_immediate(int key, char *arg)
+{
+ switch(key)
+ {
+ /* We don't want later options that were set for the given version to
+ cause errors if this option was given. */
+ case GAL_OPTIONS_ONLYVERSION_KEY:
+ options_check_version(arg);
+ break;
+
+ /* This option is completely independent of anything and doesn't need
+ out attention later. */
+ case GAL_OPTIONS_CITE_KEY:
+ options_print_citation_exit();
+ }
+}
+
+
+
+
@@ -222,12 +248,8 @@ gal_options_set_from_key(int key, char *arg, struct
argp_option *options)
/* Check if the key corresponds to this option. */
if(options[i].key==key)
{
- /* If this is the only version option, we need to check and
- (possibly) abort immediately, because the other options might
- not exist in later versions and we don't want to confuse the
- user with an unknown option error. */
- if( options[i].key == GAL_OPTIONS_ONLYVERSION_KEY )
- options_check_version(arg);
+ /* For options that need immediate attention. */
+ options_immediate(key, arg);
/* When options are read from keys, they are read from the
command-line. On the commandline, the last invokation of the
@@ -476,13 +498,8 @@ options_set_from_name(char *name, char *arg, struct
argp_option *options,
if(options[i].value && !gal_data_is_linked_list(options[i].type))
return 0;
- /* If this is the `onlyversion' option, we need to check and
- (possibly) abort immediately. The other options might not
- exist in the running version and we don't want to confuse the
- user with an unknown option error (while it is present in the
- proper version). */
- if( options[i].key == GAL_OPTIONS_ONLYVERSION_KEY )
- options_check_version(arg);
+ /* For options that need immediate attention. */
+ options_immediate(options[i].key, arg);
/* For strings, `gal_data_string_to_type' is going to return an
allocated pointer to an allocated string (`char **'). In this
@@ -775,15 +792,14 @@ options_reverse_lists(struct argp_option *options)
void
-gal_options_config_files(char *prog_exec, char *prog_name,
- struct argp_option *poptions,
- struct argp_option *coptions,
+gal_options_config_files(struct argp_option *poptions,
struct gal_options_common_params *cp)
{
size_t i;
+ struct argp_option *coptions=gal_commonopts_options;
/* Parse all the configuration files. */
- gal_options_parse_config_files(prog_exec, poptions, coptions);
+ gal_options_parse_config_files(program_exec, poptions, coptions);
/* Reverse the order of all linked list type options so the popping order
@@ -1055,11 +1071,10 @@ options_print_all(struct argp_option *poptions,
#define OPTIONS_UCHARVAL *(unsigned char *)(coptions[i].value)
void
-gal_options_print_state(char *prog_name, char *prog_bibtex,
- struct argp_option *poptions,
- struct argp_option *coptions)
+gal_options_print_state(struct argp_option *poptions)
{
size_t i;
+ struct argp_option *coptions=gal_commonopts_options;
/* Do the necessary checks (printing, saving and etc). Note that if they
were called (with a value of 1 or 0), they should be checked, so
@@ -1069,16 +1084,10 @@ gal_options_print_state(char *prog_name, char
*prog_bibtex,
if(coptions[i].value)
switch(coptions[i].key)
{
- /* Print citation. */
- case GAL_OPTIONS_CITE_KEY:
- if(OPTIONS_UCHARVAL)
- options_print_citation_exit(prog_name, prog_bibtex);
- break;
-
/* Print configuration parameters and abort. */
case GAL_OPTIONS_PRINTPARAMS_KEY:
if(OPTIONS_UCHARVAL)
options_print_all(poptions, coptions, NULL);
break;
- }
+ }
}
diff --git a/lib/options.h b/lib/options.h
index 86ad8b7..6f7aa89 100644
--- a/lib/options.h
+++ b/lib/options.h
@@ -41,6 +41,11 @@ along with Gnuastro. If not, see
<http://www.gnu.org/licenses/>.
#define GAL_OPTIONS_NO_ARG_TYPE GAL_DATA_TYPE_UCHAR
+/* External global variables that must be specified by the program using
+ this header. */
+extern char program_name[]; /* Defined in program's `main.h' */
+extern char program_exec[]; /* Defined in program's `main.h' */
+extern char program_bibtex[]; /* Defined in program's `cite.h' */
extern struct argp_option gal_commonopts_options[];
@@ -129,15 +134,11 @@ gal_options_common_argp_parse(int key, char *arg, struct
argp_state *state);
/************ Configuration files ***************/
/**********************************************************************/
void
-gal_options_config_files(char *prog_exec, char *prog_name,
- struct argp_option *poptions,
- struct argp_option *coptions,
+gal_options_config_files(struct argp_option *poptions,
struct gal_options_common_params *cp);
void
-gal_options_print_state(char *prog_name, char *prog_bibtex,
- struct argp_option *poptions,
- struct argp_option *coptions);
+gal_options_print_state(struct argp_option *poptions);
#endif
- [gnuastro-commits] master c59d66c 064/125: tmpfs-config-make now has the programs that can be built, (continued)
- [gnuastro-commits] master c59d66c 064/125: tmpfs-config-make now has the programs that can be built, Mohammad Akhlaghi, 2017/04/23
- [gnuastro-commits] master 23a541a 056/125: Single correction of string keyword values in FITS tables, Mohammad Akhlaghi, 2017/04/23
- [gnuastro-commits] master 4c28d13 042/125: Problem in reading blank FITS ASCII table fixed, Mohammad Akhlaghi, 2017/04/23
- [gnuastro-commits] master 08927b8 044/125: New Table formats section in manual, Mohammad Akhlaghi, 2017/04/23
- [gnuastro-commits] master eab25b1 059/125: Option descriptions also printed with values, Mohammad Akhlaghi, 2017/04/23
- [gnuastro-commits] master 909fa0d 050/125: Table info printing in libraries, updates to Table program, Mohammad Akhlaghi, 2017/04/23
- [gnuastro-commits] master 36bcedd 066/125: Fixed automatic output checking in Arithmetic, Mohammad Akhlaghi, 2017/04/23
- [gnuastro-commits] master d2ed7ea 060/125: Options setdirconf and setusrconf implemented, Mohammad Akhlaghi, 2017/04/23
- [gnuastro-commits] master 08147ce 062/125: All mandatory options not given are listed with error, Mohammad Akhlaghi, 2017/04/23
- [gnuastro-commits] master a9092e9 036/125: The table library can read ASCII inputs, Mohammad Akhlaghi, 2017/04/23
- [gnuastro-commits] master 77b7910 057/125: Program specific global variables for in options library,
Mohammad Akhlaghi <=
- [gnuastro-commits] master 9e553f0 022/125: All old arithmetic operators are now implemented, Mohammad Akhlaghi, 2017/04/23
- [gnuastro-commits] master b470ee4 023/125: More efficient macro implementation for binary operators, Mohammad Akhlaghi, 2017/04/23
- [gnuastro-commits] master 6b51397 077/125: Added -j8 to make examples in book, Mohammad Akhlaghi, 2017/04/23
- [gnuastro-commits] master b5c9a64 063/125: Science and its tools and ImageCrop corrections in book, Mohammad Akhlaghi, 2017/04/23
- [gnuastro-commits] master edd5612 053/125: Single function to read all configuration files, Mohammad Akhlaghi, 2017/04/23
- [gnuastro-commits] master acad34c 049/125: Sanity checks for reading txt tables, Mohammad Akhlaghi, 2017/04/23
- [gnuastro-commits] master 5b61ae3 032/125: Arithmetic functions/macros out of data.h, Mohammad Akhlaghi, 2017/04/23
- [gnuastro-commits] master 232e059 041/125: First draft of FITS table writing function complete, Mohammad Akhlaghi, 2017/04/23
- [gnuastro-commits] master 56f8c18 071/125: Cosmetic changes in option printing style, Mohammad Akhlaghi, 2017/04/23
- [gnuastro-commits] master 07a043c 026/125: Starting to use the new data structure to Table, Mohammad Akhlaghi, 2017/04/23