1/*
2 * Copyright (c) 2016, Dresden University of Technology (TU Dresden)
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice,
10 *    this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * 3. Neither the name of the copyright holder nor the names of its
17 *    contributors may be used to endorse or promote products derived from
18 *    this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
24 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * Authors: Christian Menard
33 */
34
35#include <iostream>
36#include <sstream>
37
38#include "cli_parser.hh"
39#include "sim/cxx_manager.hh"
40
41void
42CliParser::usage(const std::string& prog_name)
43{
44    std::cerr
45      << "Usage: " << prog_name
46      << (" <config_file.ini> [ <option> ]\n\n"
47          "OPTIONS:\n"
48
49          "    -o <offset>                  -- set memory offset\n"
50          "    -d <flag>                    -- set a gem5 debug flag\n"
51          "                                    (-<flag> clears a flag)\n"
52          "    -v                           -- verbose output\n"
53          "    -e <ticks>                   -- end of simulation after a \n"
54          "                                    given number of ticks\n"
55          "\n");
56    std::exit(EXIT_FAILURE);
57}
58
59void
60CliParser::parse(int argc, char** argv)
61{
62    std::string prog_name(argv[0]);
63
64    unsigned int arg_ptr = 1;
65
66    if (argc == 1) {
67        usage(prog_name);
68    }
69
70    configFile = std::string(argv[arg_ptr]);
71    arg_ptr++;
72
73    // default values
74    verboseFlag = false;
75    simulationEnd = 0;
76    memoryOffset = 0;
77
78    try {
79        while (arg_ptr < argc) {
80            std::string option(argv[arg_ptr]);
81            arg_ptr++;
82            unsigned num_args = argc - arg_ptr;
83
84            if (option == "-d") {
85                if (num_args < 1) {
86                    usage(prog_name);
87                }
88                std::string flag(argv[arg_ptr]);
89                arg_ptr++;
90                debugFlags.push_back(flag);
91            } else if (option == "-e") {
92                if (num_args < 1) {
93                    usage(prog_name);
94                }
95                std::istringstream(argv[arg_ptr]) >> simulationEnd;
96                arg_ptr++;
97            } else if (option == "-v") {
98                verboseFlag = true;
99            } else if (option == "-o") {
100                if (num_args < 1) {
101                    usage(prog_name);
102                }
103                std::istringstream(argv[arg_ptr]) >> memoryOffset;
104                arg_ptr++;
105                /* code */
106            } else {
107                usage(prog_name);
108            }
109        }
110    } catch (CxxConfigManager::Exception &e) {
111        std::cerr << e.name << ": " << e.message << "\n";
112        std::exit(EXIT_FAILURE);
113    }
114
115    parsed = true;
116}
117
118std::string
119CliParser::getDebugFlags()
120{
121    std::stringstream ss;
122    for (auto& flag : debugFlags) {
123        ss << flag << ' ';
124    }
125    return ss.str();
126}
127