summaryrefslogtreecommitdiff
path: root/src/main/CommandLine.c
blob: 6fec674592ed07e7696cf21e1500b5e8f9201c37 (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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
 /*@@
   @file      CommandLine.c
   @date      Wed Feb 17 00:11:26 1999
   @author    Tom Goodale
   @desc 
   Routines to deal with command line arguments.
   @enddesc 
 @@*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "CommandLine.h"
#include "cctk_Flesh.h"
#include "cGH.h"

#include "cctk_Comm.h"
#include "cctk_WarnLevel.h"
#include "cctk_Bindings.h"
#include "cctk_Misc.h"
#include "cctki_ActiveThorns.h"
#include "cctk_ActiveThorns.h"
#include "cctki_WarnLevel.h"

#define NEED_PARAMETER_SCOPE_STRINGS
#define NEED_PARAMETER_TYPE_STRINGS

#include "cctk_Parameter.h"

static char *rcsid = "$Header$";

/*Prototypes for some functions */

char *compileTime(void);
char *compileDate(void);
char *CCTK_FullVersion(void);
int CCTK_CommandLine(char ***outargv);

static void CommandLinePrintParameter(const cParamData *properties);

static int redirectsubs;

/* The functions used to deal with each option. */

 /*@@
   @routine    CCTK_CommandLineTestThorncompiled
   @date       Wed Feb 17 10:25:30 1999
   @author     Gabrielle Allen
   @desc 
   Tests if a given thorn has been compiled. 
   At the moment the given thorn must be in the format
   <package name>/<thorn name>
   @enddesc 
   @calls      CCTK_IsThornCompiled 
   @calledby   
   @history 
 
   @endhistory 

@@*/

void CCTKi_CommandLineTestThornCompiled(const char *optarg)
{
  int retval;

  if((retval = CCTK_IsThornCompiled(optarg)))
  {
    printf("Thorn '%s' available.\n", optarg);
  }
  else
  {
    printf("Thorn '%s' unavailable.\n", optarg);
  }

  exit(retval);
}


void CCTKi_CommandLineDescribeAllParameters(const char *optarg)
{
  int first;
  int n_thorns;
  int thorn;
  const char *thornname;
  char *param;
  const cParamData *properties;

  n_thorns = CCTK_NumCompiledThorns ();

  for(thorn = 0; thorn < n_thorns; thorn++)
  {
    thornname = CCTK_CompiledThorn (thorn);
    printf("\nParameters of thorn '%s' providing implementation '%s':\n",
           thornname, CCTK_ThornImplementation(thornname));

    first = 1;
    while (CCTK_ParameterWalk (first, thornname, &param, &properties) == 0)
    {
      if(optarg)
      {
        switch(*optarg)
        {
          case 'v':
            CommandLinePrintParameter(properties);
            break;
          default :
            fprintf(stderr, "Unknown verbosity option %s\n", optarg);
            exit(2);
        }
      }
      else
      {
        printf("%s\n", param);
      }

      free(param);
      first = 0;
    }
  }

  /*  CCTKi_BindingsParameterHelp(NULL,"%s",stdout);*/
 
  exit(0);
}

void CCTKi_CommandLineDescribeParameter(const char *optarg)
{
  char *thorn;
  char *param;
  const cParamData *properties;
  const char *cthorn;

  Util_SplitString(&thorn, &param, optarg, "::");

  if(!param)
  {
    properties = CCTK_ParameterData(optarg, NULL);
  }
  else
  {
    properties = CCTK_ParameterData(param, thorn);

    if(!properties)
    {
      cthorn = CCTK_ImplementationThorn(thorn);
      properties = CCTK_ParameterData(param, cthorn);
    }

    free(thorn);
    free(param);
  }

  CommandLinePrintParameter(properties);
    
  exit(0);
}

void CCTKi_CommandLineTestParameters(const char *optarg)
{
  int nprocs;

  if(optarg == NULL)
  {
    nprocs = 1;
  }
  else
  {
    nprocs = atoi(optarg);
  }

}

/*@@
   @routine    CCTKi_CommandLineWarningLevel
   @date       Wed Feb 17 00:58:56 1999
   @author     Tom Goodale
   @desc 
   Sets the CCTK warning level from a command line argument. 
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 
@@*/

void CCTKi_CommandLineWarningLevel(const char *optarg)
{
  int warninglevel;

  warninglevel = atoi(optarg);

  CCTKi_SetWarnLevel(warninglevel);

}

 /*@@
   @routine    CCTKi_CommandLineErrorLevel
   @date       Wed Feb 17 00:58:56 1999
   @author     Tom Goodale
   @desc 
   Sets the CCTK error level from a command line argument. 
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/
void CCTKi_CommandLineErrorLevel(const char *optarg)
{
  int errorlevel;

  errorlevel = atoi(optarg);

  CCTKi_SetErrorLevel(errorlevel);

}

 /*@@
   @routine    CCTKi_CommandLineRedirectStdout
   @date       Fri Jul 23 11:32:46 1999
   @author     Tom Goodale
   @desc 
   
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/
void CCTKi_CommandLineRedirectStdout(void)
{
  /* Set the flag to say we need to redirect the stdout. */

  redirectsubs = 1;

}

void CCTKi_CommandLineListThorns(void)
{
  printf ("\n---------------Compiled Thorns-------------\n");
  CCTKi_PrintThorns(stdout, "  %s\n", 0);
  printf ("-------------------------------------------\n\n");
  exit(1);
}

 /*@@
   @routine    CCTKi_CommandLineVersion
   @date       Fri Jul 23 12:57:45 1999
   @author     Tom Goodale
   @desc 
   Prints version info
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/
void CCTKi_CommandLineVersion(void)
{
  int argc;
  char **argv;

  const char *version=NULL;

  argc = CCTK_CommandLine(&argv);

  version = (const char *)CCTK_FullVersion();

  printf("%s: Version %s.  Compiled on %s at %s\n", argv[0], version, 
          compileDate(), compileTime());

  exit(1);
}

 /*@@
   @routine    CCTKi_CommandLineHelp
   @date       Fri Jul 23 12:57:23 1999
   @author     Tom Goodale
   @desc 
   Prints a help message
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/
void CCTKi_CommandLineHelp(void)
{
  int argc;
  char **argv;

  argc = CCTK_CommandLine(&argv);

  printf("%s, compiled on %s at %s\n", argv[0], compileDate(), compileTime());
  printf("Usage: %s [-h] [-O] [-o paramname] [-x [nprocs]] [-W n] [-E n] [-r] [-T] [-t name] [-v] <parameter_file_name>\n", argv[0]);

  printf("\n");
  printf("Valid options:\n");
  printf("-h, -help                           : gets this help.\n");
  printf("-O, -describe-all-parameters        : describes all the parameters.\n");
  printf("-o, -describe-parameter <paramname> : describe the given parameter.\n");
  printf("-x, -test-parameters [nprocs]       : does a quick test of the parameter file\n"
         "                                      pretending to be on nprocs processors, \n"
         "                                      or 1 if not given.\n");
  printf("-W, -warning-level <n>              : Sets the warning level to n.\n");
  printf("-E, -error-level <n>                : Sets the error level to n.\n");
  printf("-r, -redirect-stdout                : Redirects standard output to files.\n");
  printf("-T, -list-thorns                    : Lists the compiled-in thorns.\n");
  printf("-t, -test-thorn-compiled <name>     : Tests for the presence of thorn <name>.\n");
  printf("-v, -version                        : Prints the version.\n");

  exit(1);
}

 /*@@
   @routine    CCTKi_CommandLineUsage
   @date       Fri Jul 23 12:57:04 1999
   @author     Tom Goodale
   @desc 
   Prints a usage message.
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/
void CCTKi_CommandLineUsage(void)
{
  int argc;
  char **argv;

  argc = CCTK_CommandLine(&argv);

  printf("Usage: %s [-h] [-O] [-o paramname] [-x [nprocs]] [-W n] [-E n] [-r] [-T] [-t name] [-v] <parameter_file_name>\n", argv[0]);
  exit(1);
}  

 /*@@
   @routine    CCTKi_CommandLineFinished
   @date       Fri Jul 23 12:55:39 1999
   @author     Tom Goodale
   @desc 
   Subroutine to do anything which has to be done based upon the 
   commandline, but needs to be have a default.
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/
void CCTKi_CommandLineFinished(void)
{
  int myproc;

    /* Redirect output from sub-processors ... */

  if ((myproc = CCTK_MyProc(NULL)) != 0) 
  {
    char fname[256];
    if (redirectsubs)
    {
      sprintf(fname,"CCTK_Proc%d.out",myproc);
    }
    else
    {
#ifdef WIN32
      /* hack for Windows which doesn't know about /dev/null */
      sprintf(fname,"NUL");
#else
      sprintf(fname,"/dev/null");
#endif
    }

    freopen(fname,"w",stdout);
  }
}


 /*@@
   @routine    CommandLinePrintParameter
   @date       Sun Oct 17 22:11:31 1999
   @author     Tom Goodale
   @desc 
   Prints a parameter.
   @enddesc 
   @calls     
   @calledby   
   @history 
 
   @endhistory 

@@*/
static void CommandLinePrintParameter(const cParamData *properties)
{
  t_range *range;

  if(properties)
  {
    printf("Parameter:   %s::%s", properties->thorn, properties->name);
    if(properties->scope != SCOPE_PRIVATE)
    {
      printf(", %s::%s", CCTK_ThornImplementation(properties->thorn),
                         properties->name);
    }
    printf("\n");
    printf("Description: \"%s\"\n", properties->description);
    printf("Type:        %s\n", cctk_parameter_type_names[properties->type-1]);
    printf("Default:     %s\n", properties->defval);
    printf("Scope:       %s\n", cctk_parameter_scopes[properties->scope-1]);
    
    for(range=properties->range; range; range=range->next)
    {
      printf("  Range:     %s\n", range->range);
      printf("    Origin:      %s\n", range->origin);
      printf("    Description: %s\n", range->description);
    }
  }

  return;
}