summaryrefslogtreecommitdiff
path: root/src/main/ProcessCommandLine.c
blob: 0fcd8efb1da5bcec3c9c6ea1dbde02ce03d91e7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
 /*@@
   @file      ProcessCommandLine.c
   @date      Thu Sep 24 10:32:28 1998
   @author    Tom Goodale
   @desc 
   Routines to deal with the command line arguments.
   @enddesc 
   @version $Header$
 @@*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "cctk_Flesh.h"
#include "cctk_GNU.h"
#include "cctk_Misc.h"
#include "cctk_CommandLine.h"

#include "CommandLine.h"

static const char *rcsid = "$Header$";

CCTK_FILEVERSION(main_ProcessCommandLine_c);

/********************************************************************
 *********************     Local Data Types   ***********************
 ********************************************************************/

/********************************************************************
 ********************* Local Routine Prototypes *********************
 ********************************************************************/

/********************************************************************
 ********************* Other Routine Prototypes *********************
 ********************************************************************/

void CCTK_FCALL CCTK_FNAME(CCTK_ParameterFilename)
     (int *retval, int *len, char *name);

/********************************************************************
 *********************     Local Data   *****************************
 ********************************************************************/

static int exit_after_param_check = 0;

static char *parameter_file_name=NULL;

static int argc = 0;

static char **argv = NULL;

/********************************************************************
 *********************     External Routines   **********************
 ********************************************************************/

 /*@@
   @routine    CCTKi_ProcessCommandLine
   @date       Thu Sep 24 10:33:31 1998
   @author     Tom Goodale
   @desc 
   Processes the command line arguments.
   @enddesc 
   @calls    CCTKi_CommandLineTestThornCompiled CCTKi_CommandLineDescribeAllParameters CCTKi_CommandLineDescribeParameter CCTKi_CommandLineTestParameters CCTKi_CommandLineLoggingLevel CCTKi_CommandLineWarningLevel CCTKi_CommandLineErrorLevel CCTKi_CommandLineParameterLevel CCTKi_CommandLineRedirectStdout CCTKi_CommandLineListThorns() CCTKi_CommandLineVersion() CCTKi_CommandLineHelp
   @calledby   
   @history 
 
   @endhistory 
   @var     inargc
   @vdesc   Number of runtime arguments
   @vtype   int *
   @vio     inout
   @vcomment 
 
   @endvar 
   @var     inargv
   @vdesc   Command line arguments
   @vtype   char ***
   @vio     inout
   @vcomment 
 
   @endvar 
   @var     ConfigData
   @vdesc   Flesh configuration data
   @vtype   tFleshConfig
   @vio     inout
   @vcomment 
 
   @endvar 

   @returntype int
   @returndesc
   0 -- success
   @endreturndesc

@@*/
int CCTKi_ProcessCommandLine(int *inargc, char ***inargv, tFleshConfig *ConfigData)
{
  /*
   * If you add a new command-line option, you must update (at least)
   * the following different places in the code:
   * - the definition of  long_options[]  in this function
   * - the 3rd argument in the call to  getopt_long_only()  in this function
   * - the  switch (c)  statement in this function
   * - the #define of CACTUS_COMMANDLINE_OPTIONS near the top of
   *   src/main/CommandLine.c
   * - the help message printed by CCTKi_CommandLineHelp()
   *   (also in  src/main/CommandLine.c )
   * You should also update the description of command-line options in the
   * Cactus Users' Guide, in  doc/UsersGuide/RunningCactus.tex .
   */

  int option_index = 0;
  int c;
  int ignore;
  /* constants for identifying each options by the return value
     from getopt_long_only() */
  enum
  {
    help_option                   = 'h',
    describe_all_paramters_option = 'O',
    describe_parameter_option     = 'o',
    test_parameters_option        = 'x',
    logging_level_option          = 'L',
    warning_level_option          = 'W',
    error_level_option            = 'E',
    parameter_level_option        = 256,  /* no short option */
    redirect_option               = 'r',
    logdir_option                 = 257,  /* no short option */
    buffering_option              = 'b',
    print_schedule_option         = 'S',
    list_thorns_option            = 'T',
    test_thorns_compiled_option   = 't',
    version_option                = 'v',
    exit_after_param_check_option = 'P',
    ignore_next_option            = 'i'
  };
  /* the longopts argument passed into getopt_long_only() */
  const struct option long_options[] =
  {
    {"help",                    no_argument,       NULL, help_option},
    {"describe-all-parameters", optional_argument, NULL, describe_all_paramters_option},
    {"describe-parameter",      required_argument, NULL, describe_parameter_option},
    /*{"test-parameters",         optional_argument, NULL, test_parameters_option},*/
    {"logging-level",           required_argument, NULL, logging_level_option},
    {"warning-level",           required_argument, NULL, warning_level_option},
    {"error-level",             required_argument, NULL, error_level_option},
    {"parameter-level",         required_argument, NULL, parameter_level_option},
    {"redirect",                optional_argument, NULL, redirect_option},
    {"logdir",                  required_argument, NULL, logdir_option},
    {"buffering",               required_argument, NULL, buffering_option},
    {"print-schedule",          no_argument,       NULL, print_schedule_option},
    {"list-thorns",             no_argument,       NULL, list_thorns_option},
    {"test-thorn-compiled",     required_argument, NULL, test_thorns_compiled_option},
    {"exit-after-param-check",  no_argument,       NULL, exit_after_param_check_option},
    {"version",                 no_argument,       NULL, version_option},
    {"ignore-next",             no_argument,       NULL, ignore_next_option},
    {0, 0, 0, 0}
  };


  /* Store the command line */
  argc = *inargc;
  argv = *inargv;

  ignore = 0;

  if(argc>1)
  {
    while (1)
    {
      c = getopt_long_only (argc, argv, "hO::o:x::L:W:E:r::b:STt:Pvi",
                            long_options, &option_index);
      if (c == -1)
        break;
  
      if(!ignore)
      {
        switch (c)
        {
          case describe_all_paramters_option:
            CCTKi_CommandLineDescribeAllParameters(optarg); break;
          case describe_parameter_option:
            CCTKi_CommandLineDescribeParameter(optarg); break;
          case test_parameters_option:
            CCTKi_CommandLineTestParameters(optarg); break;
          case logging_level_option:
            CCTKi_CommandLineLoggingLevel(optarg); break;
          case warning_level_option:
            CCTKi_CommandLineWarningLevel(optarg); break;
          case error_level_option:
            CCTKi_CommandLineErrorLevel(optarg); break;
          case parameter_level_option:
            CCTKi_CommandLineParameterLevel(optarg); break;
          case redirect_option:
            CCTKi_CommandLineRedirect(optarg); break;
          case logdir_option:
            CCTKi_CommandLineLogDir(optarg); break;
          case buffering_option:
            CCTKi_CommandLineSetBuffering(optarg); break;
          case print_schedule_option:
            CCTKi_CommandLinePrintSchedule(); break;
          case list_thorns_option:
            CCTKi_CommandLineListThorns(); break;
          case test_thorns_compiled_option:
            CCTKi_CommandLineTestThornCompiled(optarg); break;
          case version_option:
            CCTKi_CommandLineVersion(); break;
          case ignore_next_option:
            ignore = 1; break;
          case exit_after_param_check_option:
            exit_after_param_check = 1; break;
          case help_option: 
          case '?':
            CCTKi_CommandLineHelp(); break;
          default:
            printf ("?? getopt returned character code 0%o ??\n", c);
        }
      }
      else
      {
        printf("Ignoring option\n");
        ignore = 0;
      }
    }

    if(argc > optind)
    {
      ConfigData->parameter_file_name = argv[optind];
      parameter_file_name = ConfigData->parameter_file_name;
    }
    else
    {
      CCTKi_CommandLineUsage();
    }
  }
  else
  {
    CCTKi_CommandLineUsage();
  }

  CCTKi_CommandLineFinished();

  return 0;
}


 /*@@
   @routine    CCTK_CommandLine
   @date       Wed Feb 17 00:19:30 1999
   @author     Tom Goodale
   @desc 
   Gets the command line arguments.
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 
   @var     outargv
   @vdesc   Place to dump the command line arguments
   @vtype   char ***
   @vio     out
   @vcomment 
 
   @endvar 

   @returntype int
   @returndesc
   The number of command line arguments.
   @endreturndesc
@@*/
int CCTK_CommandLine(char ***outargv)
{
  *outargv = argv;

  return argc;
}


 /*@@
   @routine    CCTK_ParameterFilename
   @date       Tue Oct 3 2000
   @author     Gabrielle Allen
   @desc 
   Returns the parameter filename
   @enddesc 
   @calls    CCTK_Equals 
   @calledby   
   @history 
 
   @endhistory 
   @var     len
   @vdesc   The length of the incoming string
   @vtype   int
   @vio     in
   @vcomment 
 
   @endvar 
   @var     filename
   @vdesc   String to contain the filename
   @vtype   char *
   @vio     out
   @vcomment 
 
   @endvar 

   @returntype int
   @returndesc
   The length of the returned string.
   @endreturndesc

@@*/
int CCTK_ParameterFilename(int len, char *filename)
{
  int retval;
  const char *copy_string;


  if (CCTK_Equals(parameter_file_name,"-"))
  {
    copy_string = "STDIN";
  }
  else
  {
    copy_string = parameter_file_name;
  }
  retval = strlen (copy_string);
  if (retval > len - 1)
  {
    retval = len - 1;
  }
  strncpy (filename, copy_string, retval);
  filename[retval] = 0;
  return retval;
}

void CCTK_FCALL CCTK_FNAME(CCTK_ParameterFilename)
     (int *retval, int *len, char *name)
{
  *retval = CCTK_ParameterFilename(*len,name);
}

int CCTKi_ExitAfterParamCheck(void)
{
    return exit_after_param_check;
}

/********************************************************************
 *********************     Local Routines   *************************
 ********************************************************************/