summaryrefslogtreecommitdiffstats
path: root/source/core/slang-command-line.cpp
blob: f8b5ff10ffa6764c7cf740c51480ce64c7d2ed22 (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
// slang-command-line.cpp
#include "slang-command-line.h"

#include "slang-process.h"

#include "slang-string.h"
#include "slang-string-escape-util.h"
#include "slang-string-util.h"

#include "../../slang-com-helper.h"

namespace Slang {

/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ExecutableLocation !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */

void ExecutableLocation::set(const String& dir, const String& name)
{
    if (dir.getLength() == 0)
    {
        set(name);
    }
    else
    {
        set(Path::combine(dir, name));
    }
}

void ExecutableLocation::set(const String& nameOrPath)
{
    // See if input looks like a path
    if (Path::hasPath(nameOrPath))
    {
        // If it is a path we may want to add a suffix
        const auto suffix = Process::getExecutableSuffix();

        if (suffix.getLength() == 0 || nameOrPath.endsWith(suffix))
        {
            setPath(nameOrPath);
        }
        else
        {
            // If on target that has suffix make sure name has the suffix
            StringBuilder builder;
            builder << nameOrPath;
            builder << suffix;
            setPath(builder.ProduceString());
        }
    }
    else
    {
        // If we don't have a parent, we assume it is just a naem
        setName(nameOrPath);
    }
}

void ExecutableLocation::append(StringBuilder& out) const
{
    if (m_type == Type::Unknown)
    {
        out << "(unknown)";
    }
    else
    {
        auto escapeHandler = Process::getEscapeHandler();
        StringEscapeUtil::appendMaybeQuoted(escapeHandler, m_pathOrName.getUnownedSlice(), out);
    }
}

/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! CommandLine !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */

void CommandLine::addPrefixPathArg(const char* prefix, const String& path, const char* pathPostfix)
{
    StringBuilder builder;
    builder << prefix;

    // TODO(JS): The assumption here is that quoting will be added as necessary and
    // -prefixSomething Else
    // is okay as
    // "-prefixSomething Else" rather than
    // -prefix"Something Else"

    builder << path;

    if (pathPostfix)
    {
        // Work out the path with the postfix
        builder << pathPostfix;
    }
    addArg(builder.ProduceString());
}

void CommandLine::append(StringBuilder& out) const
{
    m_executableLocation.append(out);

    if (m_args.getCount())
    {
        out << " ";
        appendArgs(out);
    }
}

void CommandLine::appendArgs(StringBuilder& out) const
{
    auto escapeHandler = Process::getEscapeHandler();

    const Int argCount = m_args.getCount();
    for (Index i = 0; i < argCount; ++i)
    {
        const auto& arg = m_args[i];
        if (i > 0)
        {
            out << " ";
        }
        StringEscapeUtil::appendMaybeQuoted(escapeHandler, arg.getUnownedSlice(), out);
    }
}

String CommandLine::toString() const
{
    StringBuilder buf;
    append(buf);
    return buf.ProduceString();
}

String CommandLine::toStringArgs() const
{
    StringBuilder buf;
    appendArgs(buf);
    return buf.ProduceString();
}

} // namespace Slang