From 6d57fb0daafc06ac07971aa77f5d1c603609e74b Mon Sep 17 00:00:00 2001
From: Robert Izzard <r.izzard@surrey.ac.uk>
Date: Mon, 16 Dec 2019 21:01:04 +0000
Subject: [PATCH] add new help_all function to the API, and cleaned the
 previous help API function so it works with the old batchmode

cleaned up the stellar magnitudes code a bit (to annoy Gio mostly ;)
---
 doc/tbse.help                               | 11 ++-
 src/API/binary_c_API_prototypes.h           |  2 +-
 src/API/binary_c_help.c                     |  5 +-
 src/logging/log_every_timestep.c            |  4 +-
 src/main.c                                  |  9 +--
 src/setup/argument_setting_functions.c      | 81 ++++++++++++++-------
 src/setup/cmd_line_args.h                   |  2 +-
 src/setup/parse_arguments.c                 |  6 +-
 src/setup/setup_prototypes.h                |  2 +-
 src/stellar_colours/gaia_magnitudes.c       |  4 +-
 src/stellar_colours/stellar_colour_macros.h | 28 ++++---
 src/stellar_structure/make_data_objects.sh  |  6 +-
 12 files changed, 102 insertions(+), 58 deletions(-)

diff --git a/doc/tbse.help b/doc/tbse.help
index d69c3292e..5999937e7 100644
--- a/doc/tbse.help
+++ b/doc/tbse.help
@@ -32,7 +32,7 @@ All other parameters will be as in the tbse script.
 
 ------------------------------------------------------------
 
-option flags for running binary_c 
+option flags for running tbse 
 
 echo
         Outputs the arguments that would be sent to binary_c,
@@ -117,3 +117,12 @@ Return value
        tbse, when run normally, returns the return value of binary_c
 
 ------------------------------------------------------------
+
+If you want help with a specific binary_c option, run
+
+./binary_c <string>
+
+where <string> is the option about which you hold some
+curiosity.
+
+------------------------------------------------------------
diff --git a/src/API/binary_c_API_prototypes.h b/src/API/binary_c_API_prototypes.h
index c66e08cf7..282da27a6 100644
--- a/src/API/binary_c_API_prototypes.h
+++ b/src/API/binary_c_API_prototypes.h
@@ -46,8 +46,8 @@ void binary_c_API_function binary_c_buffer_empty_buffer(struct stardata_t * REST
 
 void binary_c_API_function binary_c_help(struct stardata_t * RESTRICT const stardata,
                                          char * argstring);
-                                        
 
+void binary_c_API_function binary_c_help_all(struct stardata_t * RESTRICT const stardata);
 
 /*
  * API debugging functions
diff --git a/src/API/binary_c_help.c b/src/API/binary_c_help.c
index daac0e82f..f28c68d8c 100644
--- a/src/API/binary_c_help.c
+++ b/src/API/binary_c_help.c
@@ -25,13 +25,14 @@ void binary_c_API_function binary_c_help(struct stardata_t * RESTRICT const star
                 if(tmpstore == NULL)
                 {
                     build_tmpstore_contents(tmpstore);
+                    stardata->tmpstore = tmpstore;
                 }
                 binary_c_help_from_arg(
-                    stardata->tmpstore->cmd_line_args,
+                    tmpstore->cmd_line_args,
                     stardata,
                     &c,
                     0, // dummy var
-                    stardata->tmpstore->arg_count, // arg_count
+                    tmpstore->arg_count, // arg_count
                     argv,
                     -argc // argc 
                     );
diff --git a/src/logging/log_every_timestep.c b/src/logging/log_every_timestep.c
index af24773ff..108af9f87 100644
--- a/src/logging/log_every_timestep.c
+++ b/src/logging/log_every_timestep.c
@@ -2635,8 +2635,8 @@ void log_every_timestep(struct stardata_t * RESTRICT const stardata)
                    L,
                    dtp,
                    is_single == TRUE ? 0 : 1,
-                   gaia_colour,
-                   gaia_magnitude
+                   gaia_magnitude,
+                   gaia_colour
                 );
 
         }
diff --git a/src/main.c b/src/main.c
index d73c004fc..d8c1c27f4 100644
--- a/src/main.c
+++ b/src/main.c
@@ -47,7 +47,7 @@ int main (int argc,
     Set_stack_size;
     
     struct stardata_t * stardata Aligned = NULL;
-    
+        
     const int ret = binary_c_main(argc,argv,&stardata);
     
 #if !defined SEGFAULTS
@@ -81,7 +81,7 @@ static int binary_c_main(int argc,
                      &preferences,
                      &store);
     *s = stardata;
-    
+        
 #ifdef USE_GSL
     /* set up GSL */
     setup_GSL_handlers(stardata);
@@ -99,7 +99,7 @@ static int binary_c_main(int argc,
     /* examine command line arguments */
     set_up_variables(argc,argv,NULL,stardata); 
 #endif
-
+    
 #ifdef MAKE_BSE_TABLES
     make_BSE_tables(stardata);
 #endif
@@ -107,8 +107,7 @@ static int binary_c_main(int argc,
     /* save argv and argc */
     stardata->common.argv = argv;
     stardata->common.argc = argc;
-
-
+    
 #ifdef TEST_OPACITY
 
     struct opacity_t op;
diff --git a/src/setup/argument_setting_functions.c b/src/setup/argument_setting_functions.c
index 6ee3ce087..eaebecf21 100644
--- a/src/setup/argument_setting_functions.c
+++ b/src/setup/argument_setting_functions.c
@@ -41,8 +41,8 @@ void No_return binary_c_speedtests(ARG_SUBROUTINE_DECLARATION)
     {
 	for(i=0;i<ntests;i++)
 	{
-            double x=(double)rand();
-            x*=1.0;
+            double x = (double)rand();
+            x *= 1.0;
         }
     }
     End_test("Multiply by FLOAT_TYPE rand : %g s\n",TIME_TAKEN);
@@ -152,10 +152,17 @@ void binary_c_help_from_arg(ARG_SUBROUTINE_DECLARATION)
     /*
      * Output help: either a general help statement (binary_c --help),
      * or per-argument help (binary_c --help <arg1> <arg2> ...)
+     *
+     * Note that stardata should not be NULL.
+     *
+     * If argc < 0 and batchmode is off, we then exit after
+     * showing help.
      */
-    Boolean __exit = argc >= 0 ? TRUE : FALSE;
+    const Boolean exit_help = (argc >= 0 &&
+                               stardata->preferences &&
+                               Batchmode_is_off(stardata->preferences->batchmode)) ? TRUE : FALSE;
     if(argc < 0) argc = -argc;
-    
+        
     if(*c==argc-1)
     {
 	/* no extra help required */
@@ -173,8 +180,8 @@ void binary_c_help_from_arg(ARG_SUBROUTINE_DECLARATION)
     }
     else
     {
-	char *next = NULL;
-        int head=0;
+	char * next = NULL;
+        Boolean head = FALSE;
         (*c)++;
         while(*c<argc)
 	{
@@ -220,10 +227,10 @@ void binary_c_help_from_arg(ARG_SUBROUTINE_DECLARATION)
                      */
                     if(strstr(cmd_line_args[i].name,next) != NULL)
                     {
-                        if(head==0)
+                        if(head == FALSE)
                         {
                             Printf("Did you mean :\n\n");
-                            head=1;
+                            head = TRUE;
                         }
                         Printf("  %s\n",cmd_line_args[i].name);
                     }
@@ -234,10 +241,11 @@ void binary_c_help_from_arg(ARG_SUBROUTINE_DECLARATION)
  	    if(success==FALSE) Printf("\nBinary_c help : no help available for %s\n",next);
 	    (*c)++;
 	}
-	
-	if(__exit)
+        
+	if(exit_help == TRUE)
         {
-            Exit_binary_c(BINARY_C_NORMAL_EXIT,"Exit after help\n");
+            Exit_binary_c(BINARY_C_NORMAL_EXIT,
+                          "Exit after help\n");
         }
         else
         {
@@ -247,26 +255,43 @@ void binary_c_help_from_arg(ARG_SUBROUTINE_DECLARATION)
 }
 
 
-void No_return binary_c_help_all(ARG_SUBROUTINE_DECLARATION)
+void binary_c_help_all_from_arg(ARG_SUBROUTINE_DECLARATION)
 {
-    /* show help for all arguments */
-    static const char *cc[] = ARG_SECTION_STRINGS;
-    int j;
-    for(j=0;j<NUMBER_OF_ARG_SECTIONS;j++)
+    /* Show help for all arguments (into the buffer
+     * so stardata should not be NULL) 
+     *
+     * If argc < 0 and batchmode is off, we then exit.
+     */
+    if(stardata != NULL)
     {
-	Printf("\n############################################################\n##### Section %s\n############################################################\n",cc[j]);
-	for(i=0;i<arg_count;i++)
-	{
-	    if(cmd_line_args[i].section == j)
-	    {
-		Printf("%s : %s : %s\n",
-		       cmd_line_args[i].name,
-		       cmd_line_args[i].help,
-		       cmd_line_args[i].wtts_string);
-	    }
-	}
+        const Boolean exit_help_all =
+            (argc >= 0 &&
+             stardata->preferences &&
+             Batchmode_is_off(stardata->preferences->batchmode)) ? TRUE : FALSE;
+        static const char *cc[] = ARG_SECTION_STRINGS;
+        int j;
+    
+        for(j=0;j<NUMBER_OF_ARG_SECTIONS;j++)
+        {
+            Printf("\n############################################################\n##### Section %s\n############################################################\n",cc[j]);
+
+            for(i=0;i<arg_count;i++)
+            {
+                if(cmd_line_args[i].section == j)
+                {
+                    Printf("%s : %s : %s\n",
+                           cmd_line_args[i].name,
+                           cmd_line_args[i].help,
+                           cmd_line_args[i].wtts_string);
+                }
+            }
+        }
+
+        if(exit_help_all == TRUE)
+        {
+            Exit_binary_c(BINARY_C_NORMAL_EXIT,NULL);
+        }
     }
-    Exit_binary_c(BINARY_C_NORMAL_EXIT,NULL);
 }
 
 #ifdef BATCHMODE
diff --git a/src/setup/cmd_line_args.h b/src/setup/cmd_line_args.h
index b07de95c2..6cb1e8ad8 100644
--- a/src/setup/cmd_line_args.h
+++ b/src/setup/cmd_line_args.h
@@ -3538,7 +3538,7 @@ struct cmd_line_arg_t
         "Display all help pages.",                                      \
         ARG_SUBROUTINE,                                                 \
         "Ignore",                                                       \
-        &(binary_c_help_all)                                            \
+        &(binary_c_help_all_from_arg)                                   \
         ,1.0                                                            \
         },                                                              \
 {                                                                       \
diff --git a/src/setup/parse_arguments.c b/src/setup/parse_arguments.c
index 1ce6238a3..efd7190b4 100644
--- a/src/setup/parse_arguments.c
+++ b/src/setup/parse_arguments.c
@@ -437,14 +437,14 @@ void parse_arguments(const int start,
             {
                 Dprint(
                     "Exit because given cmd line arg \"%s\" (number %d) failed to match any known argument.",
-                    arg,c);
+                    arg,c);          
                 Exit_binary_c(
                     BINARY_C_SETUP_UNKNOWN_ARGUMENT,
                     "Exit because given cmd line arg  \"%s\" (number %d) failed to match any known argument (prev args are \"%s\" and \"%s\").",
                     arg,
                     c,
-                    argv[c<=1?0:c-2],
-                    argv[c<=0?1:c-1]
+                    Batchmode_is_on(stardata) ? "N/A (in batchmode)" : argv[c<=1 ? 0 : (c-2)],
+                    Batchmode_is_on(stardata) ? "N/A (in batchmode)" : argv[c<=0 ? 1 : (c-1)]
                     );
             }
         } /* loop over cmd line args */
diff --git a/src/setup/setup_prototypes.h b/src/setup/setup_prototypes.h
index b5ea27038..765374d5d 100644
--- a/src/setup/setup_prototypes.h
+++ b/src/setup/setup_prototypes.h
@@ -36,7 +36,7 @@ void force_flushing(void);
 
 
 void binary_c_help_from_arg(ARG_SUBROUTINE_DECLARATION);
-void binary_c_help_all(ARG_SUBROUTINE_DECLARATION) No_return;
+void binary_c_help_all_from_arg(ARG_SUBROUTINE_DECLARATION);
 void binary_c_speedtests(ARG_SUBROUTINE_DECLARATION) No_return;
 void binary_c_warmup_cpu(ARG_SUBROUTINE_DECLARATION);
 void list_available_args(ARG_SUBROUTINE_DECLARATION);
diff --git a/src/stellar_colours/gaia_magnitudes.c b/src/stellar_colours/gaia_magnitudes.c
index ae54f2e5f..179c628ef 100644
--- a/src/stellar_colours/gaia_magnitudes.c
+++ b/src/stellar_colours/gaia_magnitudes.c
@@ -18,8 +18,8 @@ static double crosscubic(const double x,
                          const double h);
 
 void gaia_magnitudes(struct stardata_t * stardata,
-                  double * RESTRICT magnitudes,
-                  int method)
+                     double * RESTRICT magnitudes,
+                     int method)
 {
     /*
      * Given a populated magntiudes array,
diff --git a/src/stellar_colours/stellar_colour_macros.h b/src/stellar_colours/stellar_colour_macros.h
index bb53bf73a..e298b1e25 100644
--- a/src/stellar_colours/stellar_colour_macros.h
+++ b/src/stellar_colours/stellar_colour_macros.h
@@ -36,24 +36,32 @@
 #define STELLAR_MAGNITUDE_GAIA_GRP 22
 #define STELLAR_MAGNITUDE_GAIA_GRVS 23
 
-#define STELLAR_COLOUR_STRINGS {"U","B","V","R","I","J","H","K","u","g","r","i","z","f300w","f336w","f435w","f450w","f555w","f606w","f814w","G","GBP","GRP","GRVS"}
+/* number of the above magnitudes */
+#define NUMBER_OF_STELLAR_MAGNITUDES 24
 
-#define STELLAR_COLOUR_STRING_LENGTH ((size_t)6)
+#define STELLAR_COLOUR_STRINGS                          \
+    {                                                   \
+        "U","B","V","R","I",                            \
+        "J","H","K","u","g",                            \
+        "r","i","z","f300w","f336w",                    \
+        "f435w","f450w","f555w","f606w","f814w",        \
+        "G","GBP","GRP","GRVS"                          \
+    }
 
-#define NUMBER_OF_STELLAR_MAGNITUDES 24
+/* 
+ * STELLAR_COLOUR_STRING_LENGTH is the maximum number
+ * of characters in one of the above strings, +1
+ */ 
+#define STELLAR_COLOUR_STRING_LENGTH ((size_t)(1+5))
 
-/* Gaia conversion methods */
 
+/* Gaia conversion methods */
 #define GAIA_CONVERSION_USE_DEFAULT 0
 #define GAIA_CONVERSION_UBVRI_SINGLE 1
 #define GAIA_CONVERSION_UBVRI_DOUBLE 2
 #define GAIA_CONVERSION_ugriz_SINGLE 3
-#define GAIA_CONVERSION_ugriz_DOUBLE 3
-
-#define GAIA_CONVERSION_DEFAULT_METHOD GAIA_CONVERSION_UBVRI_DOUBLE
-
-
-
+#define GAIA_CONVERSION_ugriz_DOUBLE 4
 
+#define GAIA_CONVERSION_DEFAULT_METHOD (GAIA_CONVERSION_UBVRI_DOUBLE)
 
 #endif// COLOUR_MACROS_H
diff --git a/src/stellar_structure/make_data_objects.sh b/src/stellar_structure/make_data_objects.sh
index 441ecf29d..e61d23ada 100755
--- a/src/stellar_structure/make_data_objects.sh
+++ b/src/stellar_structure/make_data_objects.sh
@@ -4,7 +4,9 @@
 # http://gareus.org/wiki/embedding_resources_in_executables
 
 : ${CC:="gcc"}
-: ${OBJCOPY_OPTS:="-I binary -B i386:x86-64 -O elf64-x86-64"}
+: ${OBJCOPY_ARCH:=$(objdump -f /bin/bash |grep architecture | gawk "{print \$2}" | sed s/,//)}
+: ${OBJCOPY_TARGET:=$(objdump -f /bin/bash  | grep format | gawk "{print \$4}")}
+: ${OBJCOPY_OPTS:="-I binary -B $OBJCOPY_ARCH -O $OBJCOPY_TARGET"}
 
 # Miller Bertolami's post-AGB tables
 HFILE=miller_bertolami_postagb.h
@@ -21,7 +23,7 @@ if [ $HFILE -nt $OBJFILE ] ; then
     OPTS="--rename-section .data=.rodata,alloc,load,readonly,data,contents "
 
     objcopy $OBJCOPY_OPTS $OPTS $TMPFILE $OBJFILE
-    # ld -r -b binary -o $OBJFILE $TMPFILE
+    #ld -r -b binary -o $OBJFILE $TMPFILE
 
     rm $TMPFILE
     rm ./double2bin
-- 
GitLab