forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand.cpp
144 lines (121 loc) · 4.15 KB
/
command.cpp
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
134
135
136
137
138
139
140
141
142
143
144
//===-- runtime/command.cpp -----------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "flang/Runtime/command.h"
#include "environment.h"
#include "stat.h"
#include "terminator.h"
#include "flang/Runtime/descriptor.h"
#include <cstdlib>
#include <limits>
namespace Fortran::runtime {
std::int32_t RTNAME(ArgumentCount)() {
int argc{executionEnvironment.argc};
if (argc > 1) {
// C counts the command name as one of the arguments, but Fortran doesn't.
return argc - 1;
}
return 0;
}
// Returns the length of the \p string. Assumes \p string is valid.
static std::int64_t StringLength(const char *string) {
std::size_t length{std::strlen(string)};
if constexpr (sizeof(std::size_t) <= sizeof(std::int64_t)) {
return static_cast<std::int64_t>(length);
} else {
std::size_t max{std::numeric_limits<std::int64_t>::max()};
return length > max ? 0 // Just fail.
: static_cast<std::int64_t>(length);
}
}
std::int64_t RTNAME(ArgumentLength)(std::int32_t n) {
if (n < 0 || n >= executionEnvironment.argc ||
!executionEnvironment.argv[n]) {
return 0;
}
return StringLength(executionEnvironment.argv[n]);
}
static bool IsValidCharDescriptor(const Descriptor *value) {
return value && value->IsAllocated() &&
value->type() == TypeCode(TypeCategory::Character, 1) &&
value->rank() == 0;
}
static void FillWithSpaces(const Descriptor *value) {
std::memset(value->OffsetElement(), ' ', value->ElementBytes());
}
static std::int32_t CopyToDescriptor(const Descriptor &value,
const char *rawValue, std::int64_t rawValueLength,
const Descriptor *errmsg) {
std::int64_t toCopy{std::min(
rawValueLength, static_cast<std::int64_t>(value.ElementBytes()))};
std::memcpy(value.OffsetElement(), rawValue, toCopy);
if (rawValueLength > toCopy) {
return ToErrmsg(errmsg, StatValueTooShort);
}
return StatOk;
}
std::int32_t RTNAME(ArgumentValue)(
std::int32_t n, const Descriptor *value, const Descriptor *errmsg) {
if (IsValidCharDescriptor(value)) {
FillWithSpaces(value);
}
if (n < 0 || n >= executionEnvironment.argc) {
return ToErrmsg(errmsg, StatInvalidArgumentNumber);
}
if (IsValidCharDescriptor(value)) {
const char *arg{executionEnvironment.argv[n]};
std::int64_t argLen{StringLength(arg)};
if (argLen <= 0) {
return ToErrmsg(errmsg, StatMissingArgument);
}
return CopyToDescriptor(*value, arg, argLen, errmsg);
}
return StatOk;
}
static std::size_t LengthWithoutTrailingSpaces(const Descriptor &d) {
std::size_t s{d.ElementBytes() - 1};
while (*d.OffsetElement(s) == ' ') {
--s;
}
return s + 1;
}
static const char *GetEnvVariableValue(
const Descriptor &name, bool trim_name, const char *sourceFile, int line) {
std::size_t nameLength{
trim_name ? LengthWithoutTrailingSpaces(name) : name.ElementBytes()};
if (nameLength == 0) {
return nullptr;
}
Terminator terminator{sourceFile, line};
const char *value{executionEnvironment.GetEnv(
name.OffsetElement(), nameLength, terminator)};
return value;
}
std::int32_t RTNAME(EnvVariableValue)(const Descriptor &name,
const Descriptor *value, bool trim_name, const Descriptor *errmsg,
const char *sourceFile, int line) {
if (IsValidCharDescriptor(value)) {
FillWithSpaces(value);
}
const char *rawValue{GetEnvVariableValue(name, trim_name, sourceFile, line)};
if (!rawValue) {
return ToErrmsg(errmsg, StatMissingEnvVariable);
}
if (IsValidCharDescriptor(value)) {
return CopyToDescriptor(*value, rawValue, StringLength(rawValue), errmsg);
}
return StatOk;
}
std::int64_t RTNAME(EnvVariableLength)(
const Descriptor &name, bool trim_name, const char *sourceFile, int line) {
const char *value{GetEnvVariableValue(name, trim_name, sourceFile, line)};
if (!value) {
return 0;
}
return StringLength(value);
}
} // namespace Fortran::runtime