summaryrefslogtreecommitdiff
path: root/src/main/ProcessParameterDatabase.c
blob: 75e2f605776a2377f07a6bac817a223da421f40c (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
 /*@@
   @file      ProcessParameterDatabase.c
   @date      Thu Sep 24 10:34:46 1998
   @author    Tom Goodale
   @desc 
              Routines to determine the parameters and store them.
   @enddesc 
   @version   $Id$
 @@*/

#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>

#include "cctk_Flesh.h"
#include "cctk_Parameter.h"
#include "cctk_WarnLevel.h"

#include "cctki_Parameter.h"

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

CCTK_FILEVERSION(main_ProcessParameterDatabase_c);

/********************************************************************
 *********************  Macro Definitions  **************************
 ********************************************************************/

/* some systems (eg. Windows NT) don't define this macro */
#ifndef S_ISDIR
#define S_ISDIR(mode)   (((mode) & S_IFMT) == S_IFDIR)
#endif

/********************************************************************
 ********************* Other Routine Prototypes *********************
 ********************************************************************/
int ParseFile (FILE *ifp, 
               int (*set_function) (const char *, const char *, int),
               tFleshConfig *ConfigData);
void CCTKi_SetParameterSetMask (int mask);


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

 /*@@
   @routine    CCTKi_ProcessParameterDatabase
   @date       Thu Sep 24 10:37:07 1998
   @author     Tom Goodale
   @desc 
   
   @enddesc 
   @calls      CCTKi_SetParameterSetMask
               CCTKi_NumParameterFileErrors
               ParseFile
 
   @var        ConfigData
   @vdesc      Flesh configuration data
   @vtype      tFleshConfig *
   @vio        inout
   @endvar 

   @returntype int
   @returndesc
                0 - success, or<BR>
               -1 - unable to open parameter file
   @endreturndesc
@@*/
int CCTKi_ProcessParameterDatabase (tFleshConfig *ConfigData)
{
  int parse_errors;
  int major, minor;
  FILE *parameter_file;
  struct stat statbuf;


  CCTKi_SetParameterSetMask (PARAMETER_RECOVERY_PRE);

  if (! strcmp (ConfigData->parameter_file_name, "-"))
  {
    parameter_file = stdin;
  }
  else if (!stat(ConfigData->parameter_file_name, &statbuf))
  {
    if(S_ISDIR(statbuf.st_mode))
    {
      parameter_file = NULL;
      CCTK_VWarn (0, __LINE__, __FILE__, "Cactus",
                  "Cannot open parameter file '%s': it is a directory", 
                  ConfigData->parameter_file_name);
    }
    else
    {
      parameter_file = fopen (ConfigData->parameter_file_name, "r");
    }
  }
  else
  {
    /* Stat failed */
    parameter_file = NULL;

    switch(errno)
    {
      case ENOENT:
        CCTK_VWarn (0, __LINE__, __FILE__, "Cactus",
                    "Cannot open parameter file '%s': file doesn't exist", 
                    ConfigData->parameter_file_name);
        break;
      case ENOTDIR:
        CCTK_VWarn (0, __LINE__, __FILE__, "Cactus",
                    "Cannot open parameter file '%s': the path is invalid", 
                    ConfigData->parameter_file_name);
        break;
#ifdef ELOOP
      case ELOOP:
        CCTK_VWarn (0, __LINE__, __FILE__, "Cactus",
                    "Cannot open parameter file '%s': too many symbolic links", 
                    ConfigData->parameter_file_name);
        break;
#endif /* ELOOP */
      case EACCES:
        CCTK_VWarn (0, __LINE__, __FILE__, "Cactus",
                    "Cannot open parameter file '%s': permission denied",
                    ConfigData->parameter_file_name);
        break;
      case ENAMETOOLONG:
        CCTK_VWarn (0, __LINE__, __FILE__, "Cactus",
                    "Cannot open parameter file '%s': filename too long",
                    ConfigData->parameter_file_name);
        break;
      case ENOMEM:
        CCTK_VWarn (0, __LINE__, __FILE__, "Cactus",
                    "Cannot open parameter file '%s': out of system memory",
                    ConfigData->parameter_file_name);
        break;
      default:
        CCTK_VWarn (0, __LINE__, __FILE__, "Cactus",
                    "Cannot open parameter file '%s'",
                    ConfigData->parameter_file_name);
    }
  }
  
  if (parameter_file == NULL)
  {
    CCTK_VWarn (0, __LINE__, __FILE__, "Cactus",
                "Cannot open parameter file '%s'.  "
                "(This can also be an MPI problem; "
                "e.g. you may be using the wrong version of 'mpirun', "
                "or may have forgotten to call 'lamboot'.)",
                ConfigData->parameter_file_name);
  }

  if (parameter_file)
  {
    parse_errors = ParseFile (parameter_file, CCTKi_SetParameter, ConfigData);

    if (strcmp (ConfigData->parameter_file_name, "-"))
    {
      fclose (parameter_file);
    }

    if (parse_errors)
    {
      CCTK_VWarn (0, __LINE__, __FILE__, "Cactus",
                  "CCTKi_SetParameterSetMask: %d parsing errors in "
                  "parameter file", parse_errors);
    }      

    minor = CCTKi_NumParameterFileErrors(1);
    if (minor)
    {
      if (minor > 1) 
      {
        CCTK_VWarn (1, __LINE__, __FILE__, "Cactus",
                    "CCTKi_SetParameterSetMask: %d minor errors in "
                    "parameter file", CCTKi_NumParameterFileErrors (1));
      }
      else
      {
        CCTK_VWarn (1, __LINE__, __FILE__, "Cactus",
                    "CCTKi_SetParameterSetMask: %d minor error in "
                    "parameter file", CCTKi_NumParameterFileErrors (1));
      }

    }

    major = CCTKi_NumParameterFileErrors (0);
    if (major)
    {
      if (major > 1) 
      {
        CCTK_VWarn (0, __LINE__, __FILE__, "Cactus",
                    "CCTKi_SetParameterSetMask: %d major errors in "
                    "parameter file", CCTKi_NumParameterFileErrors (0));
      }
      else
      {
        CCTK_VWarn (0, __LINE__, __FILE__, "Cactus",
                    "CCTKi_SetParameterSetMask: %d major error in "
                    "parameter file", CCTKi_NumParameterFileErrors (0));
      }

    }
  }
  else
  {
    CCTK_VWarn(0, __LINE__, __FILE__, "Cactus", 
               "Unable to open parameter file '%s'\n", 
               ConfigData->parameter_file_name);
  }
      
  return (parameter_file ? 0 : -1);
}