summaryrefslogtreecommitdiffstats
path: root/lldb/source/Commands/CommandObjectSource.cpp
blob: 9be4dccc8d9088a13ecb9a34b8060dcfbe6693f9 (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
//===-- CommandObjectSource.cpp ---------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "CommandObjectSource.h"

// C Includes
// C++ Includes
// Other libraries and framework includes
// Project includes
#include "lldb/Interpreter/Args.h"
#include "lldb/Core/Debugger.h"
#include "lldb/Interpreter/CommandInterpreter.h"
#include "lldb/Interpreter/CommandReturnObject.h"
#include "lldb/Target/Process.h"
#include "lldb/Target/TargetList.h"

using namespace lldb;
using namespace lldb_private;

const char *k_space_characters = "\t\n\v\f\r ";

//-------------------------------------------------------------------------
// CommandObjectSource
//-------------------------------------------------------------------------

CommandObjectSource::CommandObjectSource() :
    CommandObject ("source",
                   "Reads in debugger commands from the file <filename> and executes them.",
                   "source <filename>")
{
}

CommandObjectSource::~CommandObjectSource ()
{
}

bool
CommandObjectSource::Execute
(
    CommandInterpreter &interpreter,
    Args& args,
    CommandReturnObject &result
)
{
    const int argc = args.GetArgumentCount();
    if (argc == 1)
    {
        const char *filename = args.GetArgumentAtIndex(0);
        bool success = true;

        result.AppendMessageWithFormat ("Executing commands in '%s'.\n", filename);

        FileSpec cmd_file (filename);
        if (cmd_file.Exists())
        {
            STLStringArray commands;
            success = cmd_file.ReadFileLines (commands);

            STLStringArray::iterator pos = commands.begin();

            // Trim out any empty lines or lines that start with the comment
            // char '#'
            while (pos != commands.end())
            {
                bool remove_string = false;
                size_t non_space = pos->find_first_not_of (k_space_characters);
                if (non_space == std::string::npos)
                    remove_string = true; // Empty line
                else if ((*pos)[non_space] == '#')
                    remove_string = true; // Comment line that starts with '#'

                if (remove_string)
                    pos = commands.erase(pos);
                else
                    ++pos;
            }

            if (commands.size() > 0)
            {
                const size_t num_commands = commands.size();
                size_t i;
                for (i = 0; i<num_commands; ++i)
                {
                    result.GetOutputStream().Printf("%s %s\n", interpreter.GetPrompt(), commands[i].c_str());
                    if (!interpreter.HandleCommand(commands[i].c_str(), false, result))
                        break;
                }

                if (i < num_commands)
                {
                    result.AppendErrorWithFormat("Aborting source of '%s' after command '%s' failed.\n", filename, commands[i].c_str());
                    result.SetStatus (eReturnStatusSuccessFinishResult);
                }
                else
                {
                    success = true;
                    result.SetStatus (eReturnStatusFailed);
                }
            }
        }
        else
        {
            result.AppendErrorWithFormat ("File '%s' does not exist.\n", filename);
            result.SetStatus (eReturnStatusFailed);
            success = false;
        }

        if (success)
        {
            result.SetStatus (eReturnStatusSuccessFinishNoResult);
        }
    }
    else
    {
        result.AppendErrorWithFormat("'%s' takes exactly one executable filename argument.\n", GetCommandName());
        result.SetStatus (eReturnStatusFailed);
    }
    return result.Succeeded();

}
OpenPOWER on IntegriCloud