OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [host/] [tools/] [configtool/] [standalone/] [common/] [ecosconfig.cxx] - Blame information for rev 786

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
// ####ECOSHOSTGPLCOPYRIGHTBEGIN####                                        
2
// -------------------------------------------                              
3
// This file is part of the eCos host tools.                                
4
// Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2010 Free Software Foundation, Inc.
5
//
6
// This program is free software; you can redistribute it and/or modify     
7
// it under the terms of the GNU General Public License as published by     
8
// the Free Software Foundation; either version 2 or (at your option) any   
9
// later version.                                                           
10
//
11
// This program is distributed in the hope that it will be useful, but      
12
// WITHOUT ANY WARRANTY; without even the implied warranty of               
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU        
14
// General Public License for more details.                                 
15
//
16
// You should have received a copy of the GNU General Public License        
17
// along with this program; if not, write to the                            
18
// Free Software Foundation, Inc., 51 Franklin Street,                      
19
// Fifth Floor, Boston, MA  02110-1301, USA.                                
20
// -------------------------------------------                              
21
// ####ECOSHOSTGPLCOPYRIGHTEND####                                          
22
//==========================================================================
23
//
24
//      ecosconfig.cxx
25
//
26
//      The implementation of ecosconfig command line processing
27
//
28
//==========================================================================
29
//==========================================================================
30
//#####DESCRIPTIONBEGIN####                                             
31
//
32
// Author(s):           jld
33
// Date:                1999-11-08
34
//
35
//####DESCRIPTIONEND####
36
//==========================================================================
37
 
38
#ifndef _MSC_VER
39
#include <cstring>
40
#include <sys/param.h>
41
#include <unistd.h> /* for realpath() */
42
#endif
43
#ifdef __CYGWIN__
44
// suppress min/max macro definitions in windef.h
45
#define NOMINMAX
46
#include <windows.h>
47
#include <sys/cygwin.h> /* for cygwin_conv_to_win32_path() */
48
#endif
49
#include "cdl_exec.hxx"
50
#include "ecosconfig.hxx"
51
 
52
#define TOOL_VERSION "3.net"
53
#define TOOL_COPYRIGHT "Copyright (c) 2002-2010 Free Software Foundation, Inc."
54
#define DEFAULT_SAVE_FILE "ecos.ecc"
55
static const char* tool = "ecosconfig";
56
 
57
int main (int argc, char * argv []) {
58
 
59
    // process command qualifiers
60
    std::string repository;     // --srcdir=
61
    std::string savefile;       // --config=
62
    std::string install_prefix; // --prefix=
63
    bool version = false;       // --version
64
    bool no_resolve = false;    // --no-resolve
65
    bool quiet = false;         // -q, --quiet
66
    bool verbose = false;       // -v, --verbose
67
    bool ignore_errors = false; // -i, --ignore-errors
68
    bool no_updates = false;    // -n, --no-updates,
69
    bool help = false;          // --help
70
    bool enable_debug_set = false;  // --enable-debug or --disable-debug
71
    int  debug_level = 0;       // --enable-debug=[0|1|2]
72
 
73
#ifdef __CYGWIN__
74
    try {
75
        throw int(42);
76
    } catch(...) { };
77
#endif
78
 
79
    Tcl_FindExecutable(argv[0]);
80
 
81
    // getopt() cannot easily be used here since this code has to
82
    // build with VC++ as well.
83
    bool args_ok = true;
84
    int command_index;
85
    for (command_index = 1; command_index < argc; command_index++) { // for each command line argument
86
        char* arg = argv[command_index];
87
        if (0 == strcmp(arg, "--help")) {
88
            help = true;
89
        } else if ((0 == strcmp(arg, "-q")) || (0 == strcmp(arg, "--quiet"))) {
90
            // Allow repeated use of -q and -v to override each other.
91
            // This is useful in conjunction with shell aliases.
92
            quiet = true;
93
            verbose = false;
94
        } else if ((0 == strcmp(arg, "-v")) || (0 == strcmp(arg, "--verbose"))) {
95
            verbose = true;
96
            quiet = false;
97
        } else if ((0 == strcmp(arg, "-i")) || (0 == strcmp(arg, "--ignore-errors"))) {
98
            // Duplicate use of -i and the other flags is harmless.
99
            ignore_errors = true;
100
        } else if ((0 == strcmp(arg, "-n")) || (0 == strcmp(arg, "--no-updates"))) {
101
            no_updates = true;
102
        } else if (0 == strcmp(arg, "--version")) {
103
            version = true;
104
        } else if (0 == strcmp(arg, "--no-resolve")) {
105
            no_resolve = true;
106
        } else if (0 == strcmp(arg, "--enable-debug")) {
107
            enable_debug_set = true;
108
            debug_level = 1;
109
        } else if (0 == strcmp(arg, "--disable-debug")) {
110
            enable_debug_set = true;
111
            debug_level = 0;
112
        } else if (0 == strncmp(arg, "--srcdir", 8)) {
113
            // Duplicate use of --srcdir and other data-containing options should
114
            // be marked as an error.
115
            if ("" != repository) {
116
                fprintf(stderr, "%s: the `--srcdir' option should be used only once.\n", tool);
117
                args_ok = false;
118
            } else {
119
                if ('=' == arg[8]) {
120
                    repository = std::string(arg + 9);
121
                    if ("" == repository) {
122
                        fprintf(stderr, "%s: missing component repository after `--srcdir='\n", tool);
123
                        args_ok = false;
124
                    }
125
                } else if ('\0' == arg[8]) {
126
                    command_index++;
127
                    if (command_index == argc) {
128
                        fprintf(stderr, "%s: missing component repository after `--srcdir'\n", tool);
129
                        args_ok = false;
130
                    } else {
131
                        repository = argv[command_index];
132
                    }
133
                } else {
134
                    fprintf(stderr, "%s: invalid option `%s'\n", tool, arg);
135
                    args_ok = false;
136
                }
137
            }
138
        } else if (0 == strncmp(arg, "--config", 8)) {
139
            if ("" != savefile) {
140
                fprintf(stderr, "%s: the `--config' option should be used only once.\n", tool);
141
                args_ok = false;
142
            } else {
143
                if ('=' == arg[8]) {
144
                    savefile = std::string(arg + 9);
145
                    if ("" == savefile) {
146
                        fprintf(stderr, "%s: missing configuration savefile after `--config='\n", tool);
147
                        args_ok = false;
148
                    }
149
                } else if ('\0' == arg[8]) {
150
                    command_index++;
151
                    if (command_index == argc) {
152
                        fprintf(stderr, "%s: missing configuration savefile after `--config'\n", tool);
153
                        args_ok = false;
154
                    } else {
155
                        savefile = argv[command_index];
156
                    }
157
                } else {
158
                    fprintf(stderr, "%s: invalid option `%s'\n", tool, arg);
159
                    args_ok = false;
160
                }
161
            }
162
        } else if (0 == strncmp(arg, "--prefix", 8)) {
163
            if ("" != install_prefix) {
164
                fprintf(stderr, "%s: the `--prefix' option should be used only once.\n", tool);
165
                args_ok = false;
166
            } else {
167
                if ('=' == arg[8]) {
168
                    install_prefix = std::string(arg + 9);
169
                    if ("" == install_prefix) {
170
                        fprintf(stderr, "%s: missing install prefix after `--prefix='\n", tool);
171
                        args_ok = false;
172
                    }
173
                } else if ('\0' == arg[8]) {
174
                    command_index++;
175
                    if (command_index == argc) {
176
                        fprintf(stderr, "%s: missing install prefix after `--prefix'\n", tool);
177
                        args_ok = false;
178
                    } else {
179
                        install_prefix = argv[command_index];
180
                    }
181
                } else {
182
                    fprintf(stderr, "%s: invalid option `%s'\n", tool, arg);
183
                    args_ok = false;
184
                }
185
            }
186
        } else {
187
            // The argument is not a qualifier
188
            // However, none of the sub-commands begin with a -
189
            if ('-' == arg[0]) {
190
                fprintf(stderr, "%s: unknown option `%s'\n", tool, arg);
191
                args_ok = false;
192
            }
193
            break; // end of qualifiers
194
        }
195
    }
196
 
197
#if 0
198
    printf("args_ok is %d\n", args_ok);
199
    printf("help is %d\n", help);
200
    printf("version is %d\n", version);
201
    printf("no_resolve is %d\n", no_resolve);
202
    printf("quiet is %d\n", quiet);
203
    printf("verbose is %d\n", verbose);
204
    printf("no-updates is %d\n", no_updates);
205
    printf("ignore_errors is %d\n", ignore_errors);
206
    printf("repository is %s\n", repository.c_str());
207
    printf("savefile is %s\n", savefile.c_str());
208
    printf("install_prefix is %s\n", install_prefix.c_str());
209
    exit(EXIT_SUCCESS);
210
#endif
211
 
212
    // Usually argv[command_index] will be a sub-command, unless
213
    // --help or --version has been used.
214
 
215
    // Always output the version number, irrespective of subsequent
216
    // commands or any problems. This can be useful in batch jobs.
217
    if (version) {
218
        printf ("ecosconfig %s (%s %s)\n%s\n", TOOL_VERSION, __DATE__, __TIME__, TOOL_COPYRIGHT);
219
        if (command_index == argc) {
220
            return EXIT_SUCCESS;
221
        }
222
    }
223
    // Cope with --help and any user errors. If --help is used then
224
    // subsequent arguments should be ignored, as should any problems
225
    // with the arguments. This allows the user to type a partial
226
    // command, then switch to --help, and use shell history editing
227
    // to complete/correct the command.
228
    if (help || !args_ok || (command_index == argc)) {
229
        usage_message();
230
        return help ? EXIT_SUCCESS : EXIT_FAILURE;
231
    }
232
 
233
    // set the default save file
234
    if (savefile.empty ()) { // if the save file was not specified on the command line
235
        savefile = DEFAULT_SAVE_FILE; // use the default save file
236
    }
237
 
238
    // find the repository
239
    if (repository.empty ()) { // if the repository was not specified on the command line
240
        const char * env_var = getenv ("ECOS_REPOSITORY");
241
        if (env_var) { // if the ECOS_REPOSITORY environment variable is defined
242
            repository = env_var;
243
        } else { // the ECOS_REPOSITORY environment variable is not defined
244
            // assume that the tool is located in the root of the repository
245
#ifdef _MSC_VER
246
            char toolpath [_MAX_PATH + 1];
247
            _fullpath (toolpath, argv [0], sizeof (toolpath)); // get the absolute path to the tool
248
#else
249
            // NOTE: portability problem. realpath() is not a POSIX function.
250
            // Alternative code may be needed on some platforms.
251
            char toolpath [MAXPATHLEN + 1];
252
            realpath (argv [0], toolpath); // get the absolute path to the tool
253
#endif
254
            repository = toolpath;
255
            for (unsigned int n = repository.size () - 1; n > 0; n--) { // for each char starting at the tail
256
                if (('\\' == repository [n]) || ('/' == repository [n])) { // if the char is a directory separator
257
                    repository.resize (n); // remove the filename from the filepath
258
                    break;
259
                }
260
            }
261
        }
262
    }
263
 
264
    // convert a DOS format repository directory to POSIX format for backward compatibility (Cygwin only)
265
#ifdef __CYGWIN__
266
    if (1 < repository.size() && ':' == repository [1]) {
267
        char buffer [MAX_PATH + 1];
268
        cygwin_conv_to_posix_path (repository.c_str (), buffer);
269
        repository = buffer;
270
    }
271
#endif
272
 
273
    // Initialize the cdl_exec code (not quite sure why this needs a
274
    // separate object rather than just a bunch of statics). 
275
    cdl_exec exec (trim_path (repository), savefile, trim_path (install_prefix), no_resolve);
276
    cdl_exec::set_quiet_mode(quiet);
277
    cdl_exec::set_verbose_mode(verbose);
278
    cdl_exec::set_ignore_errors_mode(ignore_errors);
279
    cdl_exec::set_no_updates_mode(no_updates);
280
    if (enable_debug_set) {
281
        cdl_exec::set_debug_level(debug_level);
282
    }
283
 
284
    // Now identify and process the sub-command.
285
    const std::string command = argv [command_index];
286
    command_index++;
287
    bool status = false;
288
 
289
    if ("new" == command) {
290
        // Usage: ecosconfig new <target> [template [version]]
291
        if ((command_index == argc) || ((command_index + 3) < argc)) {
292
            usage_message();
293
        } else {
294
            // The default values for template and template_version
295
            // are part of the cdl_exec class, so cdl_exec::cmd_new() has
296
            // to be invoked with the right number of arguments.
297
            if ((command_index + 1) == argc) {
298
                status = exec.cmd_new(argv[command_index]);
299
            } else if ((command_index + 2) == argc) {
300
                status = exec.cmd_new(argv[command_index], argv[command_index + 1]);
301
            } else {
302
                status = exec.cmd_new(argv[command_index], argv[command_index + 1], argv[command_index + 2]);
303
            }
304
        }
305
    } else if ("tree" == command) {
306
        // Usage: ecosconfig tree
307
        if (command_index == argc) {
308
            status = exec.cmd_tree ();
309
        } else {
310
            usage_message ();
311
        }
312
    } else if ("list" == command) {
313
        // Usage: ecosconfig list
314
        if (command_index == argc) {
315
            status = exec.cmd_list ();
316
        } else {
317
            usage_message ();
318
        }
319
    } else if ("check" == command) {
320
        // Usage: ecosconfig check
321
        if (command_index == argc) {
322
            status = exec.cmd_check ();
323
        } else {
324
            usage_message ();
325
        }
326
    } else if ("resolve" == command) {
327
        // Usage: ecosconfig resolve
328
        if (command_index == argc) {
329
            status = exec.cmd_resolve ();
330
        } else {
331
            usage_message ();
332
        }
333
    } else if ("add" == command) {
334
        // Usage: ecosconfig add <package> [<package2> ...]
335
        if (command_index < argc) {
336
            std::vector<std::string> packages;
337
            for (int n = command_index; n < argc; n++) {
338
                packages.push_back (argv [n]);
339
            }
340
            status = exec.cmd_add (packages);
341
        } else {
342
            usage_message ();
343
        }
344
    } else if ("remove" == command) {
345
        // Usage: ecosconfig remove <package> [<package2> ...]
346
        if (command_index < argc) {
347
            std::vector<std::string> packages;
348
            for (int n = command_index; n < argc; n++) {
349
                packages.push_back (argv [n]);
350
            }
351
            status = exec.cmd_remove (packages);
352
        } else {
353
            usage_message ();
354
        }
355
    } else if ("version" == command) {
356
        // Usage: ecosconfig version <version> <package> [<package2> ...]
357
        // Note that it is not possible to change several packages to different versions. 
358
        if (command_index + 1 < argc) {
359
            std::vector<std::string> packages;
360
            for (int n = command_index + 1; n < argc; n++) {
361
                packages.push_back (argv [n]);
362
            }
363
            status = exec.cmd_version (argv [command_index], packages);
364
        } else {
365
            usage_message ();
366
        }
367
 
368
    } else if ("target" == command) {
369
        // Usage: ecosconfig target <target>
370
        if (command_index + 1 == argc) {
371
            status = exec.cmd_target (argv [command_index]);
372
        } else {
373
            usage_message ();
374
        }
375
 
376
    } else if ("template" == command) {
377
        // Usage: ecosconfig template <template> [<version>]
378
        if (command_index + 1 == argc) {
379
            status = exec.cmd_template (argv [command_index]);
380
        } else if (command_index + 2 == argc) {
381
            status = exec.cmd_template (argv [command_index], argv [command_index + 1]);
382
        } else {
383
            usage_message ();
384
        }
385
 
386
    } else if ("export" == command) {
387
        // Usage: ecosconfige export <filename>
388
        if (command_index + 1 == argc) {
389
            std::string filename = std::string(argv[command_index]);
390
            status = exec.cmd_export(filename);
391
        } else {
392
            usage_message ();
393
        }
394
 
395
    } else if ("import" == command) {
396
        // Usage: ecosconfig import <filename>
397
        if (command_index + 1 == argc) {
398
            std::string filename = std::string(argv[command_index]);
399
            status = exec.cmd_import(filename);
400
        } else {
401
            usage_message ();
402
        }
403
 
404
    } else {
405
        usage_message ();
406
    }
407
 
408
    return status ? EXIT_SUCCESS : EXIT_FAILURE;
409
}
410
 
411
// remove the trailing directory separator from a file path if present
412
std::string trim_path (const std::string input) {
413
    std::string output = input;
414
    if (! output.empty ()) {
415
        const char last_char = output [output.size () - 1];
416
        if (('\\' == last_char) || ('/' == last_char)) { // if the last char is a directory separator
417
            output.resize (output.size () - 1); // remove the last char
418
        }
419
    }
420
    return output;
421
}
422
 
423
// print a usage message
424
void usage_message () {
425
    printf ("Usage: ecosconfig [ qualifier ... ] [ command ]\n");
426
    printf ("  commands are:\n");
427
    printf ("    list                                       : list repository contents\n");
428
    printf ("    new TARGET [ TEMPLATE [ VERSION ] ]        : create a configuration\n");
429
    printf ("    target TARGET                              : change the target hardware\n");
430
    printf ("    template TEMPLATE [ VERSION ]              : change the template\n");
431
    printf ("    add PACKAGE [ PACKAGE ... ]                : add package(s)\n");
432
    printf ("    remove PACKAGE [ PACKAGE ... ]             : remove package(s)\n");
433
    printf ("    version VERSION PACKAGE [ PACKAGE ... ]    : change version of package(s)\n");
434
    printf ("    export FILE                                : export minimal config info\n");
435
    printf ("    import FILE                                : import additional config info\n");
436
    printf ("    check                                      : check the configuration\n");
437
    printf ("    resolve                                    : resolve conflicts\n");
438
    printf ("    tree                                       : create a build tree\n");
439
    printf ("  qualifiers are:\n");
440
    printf ("    --config=FILE                              : the configuration file\n");
441
    printf ("    --prefix=DIRECTORY                         : the install prefix\n");
442
    printf ("    --srcdir=DIRECTORY                         : the source repository\n");
443
    printf ("    --no-resolve                               : disable conflict resolution\n");
444
    printf ("    --version                                  : show version and copyright\n");
445
    printf ("    -q, --quiet                                : reduce verbosity\n");
446
    printf ("    -v, --verbose                              : increase verbosity\n");
447
    printf ("    -i, --ignore-errors                        : ignore unresolved conflicts\n");
448
    printf ("    -n, --no-updates                           : read-only mode, do not modify the file system\n");
449
    printf ("    --enable-debug                             : enable debugging in this configuration\n");
450
    printf ("    --disable-debug                            : disable debugging in this configuration\n");
451
    printf ("    --help                                     : display this message\n");
452
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.