• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nasa / trick / 4726419650

pending completion
4726419650

push

github

GitHub
Allow for read only S_sie.resource (#1420)

59 of 59 new or added lines in 4 files covered. (100.0%)

12532 of 21440 relevant lines covered (58.45%)

1325789.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

59.7
/trick_source/sim_services/CommandLineArguments/CommandLineArguments.cpp
1
/*
2
   PURPOSE: ( Handle the simulation command line args )
3

4
   REFERENCE: ( Trick Simulation Environment )
5

6
   ASSUMPTIONS AND LIMITATIONS: ( None )
7

8
   CLASS: ( N/A )
9

10
   LIBRARY DEPENDENCY: ( None )
11

12
   PROGRAMMERS: ( Keith Vetter LinCom 6/2003 ) */
13

14

15
#include <iostream>
16
#include <sys/stat.h>
17
#include <stdio.h>
18
#include <stdlib.h>
19
#include <string.h>
20
#include <unistd.h>
21
#include <cstring>
22
#include <cerrno>
23
#include <unistd.h>
24
#include <sys/types.h>
25
#include <pwd.h>
26

27
#include "trick/CommandLineArguments.hh"
28
#include "trick/memorymanager_c_intf.h"
29

30
Trick::CommandLineArguments * the_cmd_args ;
31

32
Trick::CommandLineArguments::CommandLineArguments() {
251✔
33
    the_cmd_args = this ;
251✔
34

35
    output_dir = std::string(".") ;
251✔
36
    default_dir = std::string(".") ;
251✔
37

38
    argc = 0;
251✔
39
    argv = NULL;
251✔
40
}
251✔
41

42
int Trick::CommandLineArguments::get_argc() {
888✔
43
    return(argc) ;
888✔
44
}
45

46
char ** Trick::CommandLineArguments::get_argv() {
888✔
47
    return(argv) ;
888✔
48
}
49

50
std::string Trick::CommandLineArguments::get_output_dir() {
×
51
    return(output_dir) ;
×
52
}
53

54
std::string & Trick::CommandLineArguments::get_output_dir_ref() {
1,750✔
55
    return(output_dir) ;
1,750✔
56
}
57

58
std::string Trick::CommandLineArguments::get_user_output_dir() {
×
59
    return(user_output_dir) ;
×
60
}
61

62
std::string & Trick::CommandLineArguments::get_user_output_dir_ref() {
1✔
63
    return(user_output_dir) ;
1✔
64
}
65

66
std::string Trick::CommandLineArguments::get_input_file() {
×
67
    return(input_file) ;
×
68
}
69

70
std::string & Trick::CommandLineArguments::get_input_file_ref() {
26,793✔
71
    return(input_file) ;
26,793✔
72
}
73

74
std::string Trick::CommandLineArguments::get_default_dir() {
×
75
    return(default_dir) ;
×
76
}
77

78
std::string & Trick::CommandLineArguments::get_default_dir_ref() {
26,265✔
79
    return(default_dir) ;
26,265✔
80
}
81

82
std::string Trick::CommandLineArguments::get_cmdline_name() {
×
83
    return(cmdline_name) ;
×
84
}
85

86
std::string & Trick::CommandLineArguments::get_cmdline_name_ref() {
26,260✔
87
    return(cmdline_name) ;
26,260✔
88
}
89

90
// Helper function - create a full path with error checking along the way
91
int Trick::CommandLineArguments::create_path(const std::string& dirname) {
7✔
92
    size_t cur_index = 0;
7✔
93

94
    std::string full_dir (dirname);
14✔
95

96
    // These syscalls don't seem to take care of home special character, so do it manually
97
    // I think the shell should handle it before it gets here, but just in case check for it
98
    if (dirname.at(0) == '~') {
7✔
99
        struct passwd *pw = getpwuid(getuid());
×
100
        full_dir = std::string(pw->pw_dir) + dirname.substr(1, dirname.size());
×
101
    }
102

103
    while (cur_index != full_dir.size()) {
31✔
104
        cur_index = full_dir.find('/', cur_index+1);
25✔
105
        if (cur_index == std::string::npos) {
25✔
106
            cur_index = full_dir.size();
6✔
107
        }
108
        std::string cur_dir = full_dir.substr(0, cur_index);
25✔
109
        
110
        struct stat info;
111
        if(stat( cur_dir.c_str(), &info ) != 0) {
25✔
112
            // does not exist - make it
113
            if (mkdir(cur_dir.c_str(), 0775) == -1) {
10✔
114
                std::cerr << "Error creating directory " << cur_dir << std::endl;
×
115
                return 1;
×
116
            }
117
        } else {
118
            // Does exist
119
            if(info.st_mode & S_IFDIR) {
15✔
120
                // Is a directory
121
                if (info.st_mode & S_IWUSR) {
14✔
122
                    // Perfect, nothing to do here
123
                } else {
124
                    // Not writeable
125
                    std::cerr << "Intermediate directory " << cur_dir << " is not writable, unable to create output directory." << std::endl;
×
126
                    return 1;
×
127
                }
128
            } else {
129
                // Does exist, but is a file
130
                std::cerr << "Intermediate directory " << cur_dir << " is not a directory, unable to create output directory." << std::endl;
1✔
131
                return 1;
1✔
132
            }
133
        }
134
    }
135
    return 0;
6✔
136
}
137

138
/**
139
@details
140
-# Save the number of command line arguments.
141
-# Save the command line arguments.
142
-# Determine the directory and simulation executable name from the first
143
   command line argument
144
   -# If no directory is specified
145
      -# Save the simulation executable name as the full first argument
146
      -# Get the current working directory
147
      -# Save the current working directory as the default input directory.
148
   -# If a relative or full directory is present in the command line arguments
149
      -# split the command line argument on the last "/" character
150
      -# Save the back half of the split as the simulation executable name
151
      -# Save the current working directory
152
      -# Change the current working directory to the first half of the split
153
         command line argument.
154
      -# Get the current working directory
155
      -# Save the current working directory as the default input directory.
156
      -# Change the current working directory to the saved working directory
157
-# If present, the second argument will be the run directory and the input file.
158
   -# Save the run input file  name as the full second argument
159
   -# Split the second argument on the last "/" character
160
   -# If no directory is specified set the default output and current output
161
      directories to the current directory
162
   -# Else set the default and the current output directories to the first
163
      half of the split
164
-# Search the remaining command line arguments for "-O" or "-OO"
165
   -# If found set the output direcory to the following argument
166
   -# Attempt to create the output directory if it does not exist.
167
      -# Exit if the simulation cannot be created.
168
*/
169
int Trick::CommandLineArguments::process_sim_args(int nargs , char **args) {
220✔
170

171
    char *buf, *buf2;
172
    size_t found ;
173

174
    argc = nargs ;
220✔
175
    argv = (char **)TMM_declare_var_1d("char *", argc) ;
220✔
176
    for (int ii = 0 ; ii < argc ; ii++ ) {
1,031✔
177
       argv[ii] = TMM_strdup(args[ii]) ;
811✔
178
    }
179

180
    buf = (char *)malloc(4096) ;
220✔
181
    buf2 = (char *)malloc(4096) ;
220✔
182

183
    default_dir = argv[0] ;
220✔
184
    found = default_dir.find_last_of("/") ;
220✔
185
    if ( found == std::string::npos ) {
220✔
186
        cmdline_name = default_dir ;
×
187
        getcwd(buf, (size_t) 256);
×
188
        default_dir = buf ;
×
189
    } else {
190
        cmdline_name = default_dir.substr(found + 1) ;
220✔
191

192
        // save the current directory
193
        getcwd(buf, (size_t) 256);
220✔
194

195
        // change to the default directory
196
        chdir(default_dir.substr(0,found).c_str());
220✔
197

198
        // get the full path default directory
199
        getcwd(buf2, (size_t) 256);
220✔
200
        default_dir = buf2 ;
220✔
201

202
        // change back to the current directory
203
        chdir(buf);
220✔
204
    }
205

206
    free(buf) ;
220✔
207
    free(buf2) ;
220✔
208

209
    if ( argc > 1 ) {
220✔
210

211
    /* First occurnance of "RUN_*" is the input file name: '<Run_dir>/<file_name>'. 
212
       If not found, defaults to first argument */
213
        
214
        input_file = argv[1];
220✔
215
        run_dir = argv[1];
220✔
216

217
        for(int ii = 1; ii < argc; ii++) {
227✔
218
            if(std::string(argv[ii]).find("RUN_") != std::string::npos) {
220✔
219
              input_file = argv[ii];
213✔
220
              run_dir = argv[ii];
213✔
221
              break;
213✔
222
            }
223
        }
224

225
        if (access(input_file.c_str(), F_OK) != 0) {
220✔
226
          input_file = "";
×
227
            if(strcmp(argv[1], "trick_version") && strcmp(argv[1], "sie") && strcmp(argv[1], "-help")  && strcmp(argv[1], "--help") &&
×
228
                strcmp(argv[1], "-h") && strcmp(argv[1], "help")) {
×
229
                std::cerr << "\nERROR: Invalid input file or command line argument." << std::endl;
×
230
                exit(1);
×
231
            }
232
        }
233

234
        found = run_dir.find_last_of("/") ;
220✔
235
        if ( found != std::string::npos ) {
220✔
236
            run_dir.erase(found) ;
220✔
237
        } else {
238
            run_dir = "." ;
×
239
        }
240
        /* check existence of run directory */
241
        if (access(run_dir.c_str(), F_OK) != 0) {
220✔
242
            std::cerr << "\nERROR: while accessing input file directory \"" << run_dir << "\" : " << std::strerror(errno) << std::endl ;
×
243
            exit(1);
×
244
        }
245

246
        output_dir = run_dir ;
220✔
247

248
        for (int ii = 1; ii < argc; ii++) {
764✔
249
            if (!strncmp("-OO", argv[ii], (size_t) 3) || !strncmp("-O", argv[ii], (size_t) 2)) {
544✔
250
                if (ii == ( argc - 1 )) {
47✔
251
                    std::cerr << "\nERROR: No directory specified after -O or -OO argument" << std::endl ;
×
252
                    exit(1) ;
×
253
                }
254
                /* Output data directory */
255
                output_dir = user_output_dir = argv[++ii];
47✔
256

257

258
            }
259
        }
260

261
        /* Create output directory if necessary. */
262
        if (access(output_dir.c_str(), F_OK) != 0) {
220✔
263
            if (create_path(output_dir) != 0) {
1✔
264
                std::cerr << "\nERROR: While trying to create output directory \"" << output_dir << "\" : " << std::strerror(errno) << std::endl ;
×
265
                exit(1) ;
×
266
            }
267
        } else {
268
            /* check permissions on output directory */
269
            if (access(output_dir.c_str(), (W_OK|X_OK) ) == -1) {
219✔
270
                std::cerr << "\nERROR: while writing to output directory \"" << output_dir << "\" : " << std::strerror(errno) << std::endl ;
×
271
                exit(2) ;
×
272
            }
273
        }
274
    }
275

276
    return(0) ;
220✔
277

278
}
279

280

281
/**
282
@design
283
-# Get the current system date and time.
284
-# Set the subdirectory within the current output directory to
285
   DATA_<year>_<month>_<day>_<hour>_<minute>_<second>
286
   -# Attempt to create the output directory if it does not exist.
287
      -# Exit if the simulation cannot be created.
288
*/
289
int Trick::CommandLineArguments::output_dir_timestamped_on() {
×
290

291
    time_t date ;
292
    struct tm *curr_time ;
293
    char temp_str[256] ;
294

295
    date = time(NULL) ;
×
296
    curr_time = localtime(&date) ;
×
297

298
    snprintf(temp_str, sizeof(temp_str), "DATA_%4d_%02d_%02d_%02d_%02d_%02d",
×
299
            curr_time->tm_year + 1900 , curr_time->tm_mon + 1 , curr_time->tm_mday,
×
300
            curr_time->tm_hour , curr_time->tm_min , curr_time->tm_sec );
301

302
    time_stamp_dir = std::string(temp_str) ;
×
303

304
    if ( ! user_output_dir.empty() ) {
×
305
        output_dir = user_output_dir + "/" + time_stamp_dir ;
×
306
    } else {
307
        output_dir = run_dir + "/" + time_stamp_dir ;
×
308
    }
309

310
    /* Create directories if necessary. */
311
    if (access(output_dir.c_str(), F_OK) != 0) {
×
312
        if (mkdir(output_dir.c_str(), 0775) == -1) {
×
313
            std::cerr << "\nERROR: While trying to create dir \"" << output_dir << "\" Exiting!" << std::endl ;
×
314
            exit(1) ;
×
315
        }
316
    }
317

318
    output_dir_timestamped = 1 ;
×
319
    return(0) ;
×
320
}
321

322
/**
323
@design
324
-# If the user had specified an output directory reset
325
   the output directory to the user specified directory
326
-# Else set the output directory to the run directory.
327
*/
328
int Trick::CommandLineArguments::output_dir_timestamped_off() {
×
329

330
    if ( ! user_output_dir.empty() ) {
×
331
        output_dir = user_output_dir ;
×
332
    } else {
333
        output_dir = run_dir ;
×
334
    }
335

336
    output_dir_timestamped = 0 ;
×
337
    return(0) ;
×
338

339
}
340

341
/**
342
@design
343
-#  Sets the value of output_dir
344
*/
345
void Trick::CommandLineArguments::set_output_dir(std::string output_directory) {
46✔
346
    output_dir = output_directory;
46✔
347
}
46✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc