aboutsummaryrefslogtreecommitdiffstats
path: root/longbow/src/LongBow/longBow_Config.c
blob: f650661f4fd5f5e3480624d76b76522fa735fa96 (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 */
#include <config.h>
#include <stdio.h>
#include <inttypes.h>

#include <LongBow/private/longBow_ArrayList.h>
#include <LongBow/private/longBow_Memory.h>
#include <LongBow/private/longBow_String.h>

#include <LongBow/longBow_Properties.h>

#include <LongBow/longBow_Config.h>
#include <LongBow/Reporting/longBowReport_Runtime.h>
#include <LongBow/longBow_About.h>

/** @cond private */
struct longbow_configuration {
    LongBowReportConfig *reportConfiguration;
    LongBowProperties *properties;
};
/** @endcond */

static void
_longBowConfig_SetupEnvironment(const LongBowConfig *config)
{
}

static void
_longBowConfig_Show(const LongBowConfig *configuration)
{
    char *string = longBowProperties_ToString(configuration->properties);
    ssize_t nwritten = write(1, string, strlen(string));
    if (nwritten != strlen(string)) {
        fprintf(stderr, "Failed to write to file descriptor 1.\n");
        exit(-1);
    }
    longBowMemory_Deallocate((void **) &string);
}

static bool
_longBowConfig_Set(LongBowConfig *configuration, const char *expression)
{
    bool result = false;

    LongBowArrayList *tokens = longBowString_Tokenise(expression, "=");
    if (tokens != NULL) {
        if (longBowArrayList_Length(tokens) == 2) {
            char *name = longBowArrayList_Get(tokens, 0);
            char *value = longBowArrayList_Get(tokens, 1);
            result = longBowConfig_SetProperty(configuration, name, value);
            longBowArrayList_Destroy(&tokens);
        }
    }

    return result;
}

LongBowConfig *
longBowConfig_Create(int argc, char *argv[], const char *mainFileName)
{
    LongBowConfig *result = longBowMemory_Allocate(sizeof(LongBowConfig));

    result->properties = longBowProperties_Create();
    longBowProperties_Set(result->properties, "trace", "false");
    longBowProperties_Set(result->properties, "silent", "false");
    longBowProperties_Set(result->properties, "run-forked", "false");

    for (int i = 1; i < argc; i++) {
        if (longBowString_Equals("--help", argv[i]) || longBowString_Equals("-h", argv[i])) {
            // If the option is "--help",
            // then let all of the sub-systems that also take arguments process that option also.
            printf("LongBow %s\n", longBowAbout_Version());
            printf("%s\n", longBowAbout_MiniNotice());
            printf("Options\n");
            //printf("  --main          Print the name of the main test runner source file.\n");
            printf("  --help           Print this help message.\n");
            printf("  --run-forked     Run the tests as forked processes.\n");
            printf("  --run-nonforked  Run the tests in the same process (default).\n");
            printf("  --version        Print the version of LongBow used for this test.\n");
            printf("  --core-dump      Produce a core file upon the first failed assertion.\n");
            printf("  --set name=value Set a configuration property name to the specified value\n");
            longBowTestRunner_ConfigHelp();
            longBowTestFixture_ConfigHelp();
            longBowTestCase_ConfigHelp();
            longBowReportRuntime_Create(argc, argv);
            longBowConfig_Destroy(&result);
            printf("\n");
            return NULL;
        } else if (longBowString_Equals("--main", argv[i])) {
            printf("%s\n", mainFileName);
            longBowConfig_Destroy(&result);
            return NULL;
        } else if (longBowString_Equals("--version", argv[i])) {
            printf("%s\n", longBowAbout_Version());
            longBowConfig_Destroy(&result);
            return NULL;
        } else if (longBowString_Equals("--run-nonforked", argv[i])) {
            longBowProperties_Set(result->properties, "run-forked", "false");
        } else if (longBowString_Equals("--run-forked", argv[i])) {
            printf("?\n");
            longBowProperties_Set(result->properties, "run-forked", "true");
        } else if (longBowString_Equals("--trace", argv[i])) {
            longBowProperties_Set(result->properties, "trace", "true");
        } else if (longBowString_Equals("--silent", argv[i])) {
            longBowProperties_Set(result->properties, "silent", "true");
        } else if (longBowString_Equals("--core-dump", argv[i])) {
            longBowProperties_Set(result->properties, "core-dump", "true");
        } else if (longBowString_StartsWith(argv[i], "--set")) {
            char *parameter = argv[++i];
            if (_longBowConfig_Set(result, parameter) == false) {
                printf("Could not set parameter: %s\n", parameter);
            }
        } else if (longBowString_StartsWith(argv[i], "--show")) {
            _longBowConfig_Show(result);
        } else {
            printf("Unknown option '%s'\n", argv[i]);
        }
    }

    LongBowReportConfig *reportConfiguration = longBowReportRuntime_Create(argc, argv);

    if (reportConfiguration == NULL) {
        longBowConfig_Destroy(&result);
        result = NULL;
    } else {
        if (result == NULL) {
            // nothing to do.
        } else {
            result->reportConfiguration = reportConfiguration;
        }
    }

    if (result != NULL) {
        _longBowConfig_SetupEnvironment(result);
    }
    return result;
}

void
longBowConfig_Destroy(LongBowConfig **configPtr)
{
    LongBowConfig *config = *configPtr;

    if (config != NULL) {
        if (config->reportConfiguration != NULL) {
            longBowReportRuntime_Destroy(&config->reportConfiguration);
        }
        longBowProperties_Destroy(&config->properties);
        longBowMemory_Deallocate((void **) configPtr);
    }
}

bool
longBowConfig_IsRunForked(const LongBowConfig *config)
{
    return longBowConfig_GetBoolean(config, false, "run-forked");
}

bool
longBowConfig_IsTrace(const LongBowConfig *config)
{
    return longBowConfig_GetBoolean(config, false, "trace");
}

static const char *
_longBowConfig_GetProperty(const LongBowConfig *config, const char *format, va_list ap)
{
    const char *result = NULL;

    char *propertyName;
    if (vasprintf(&propertyName, format, ap) != -1) {
        if (config != NULL && config->properties != NULL) {
            result = longBowProperties_Get(config->properties, propertyName);
        }
        free(propertyName);
    }

    return result;
}

const char *
longBowConfig_GetProperty(const LongBowConfig *config, const char *format, ...)
{
    const char *result = NULL;

    if (config != NULL && config->properties != NULL) {
        va_list ap;
        va_start(ap, format);
        result = _longBowConfig_GetProperty(config, format, ap);
        va_end(ap);
    }

    return result;
}

bool
longBowConfig_SetProperty(const LongBowConfig *config, const char *name, const char *value)
{
    bool result = false;

    if (config != NULL && config->properties != NULL) {
        result = longBowProperties_Set(config->properties, name, value);
    }

    return result;
}

bool
longBowConfig_GetBoolean(const LongBowConfig *config, bool defaultValue, const char *format, ...)
{
    va_list ap;
    va_start(ap, format);
    const char *value = _longBowConfig_GetProperty(config, format, ap);
    va_end(ap);

    bool result = defaultValue;
    if (value != NULL) {
        result = (strcasecmp(value, "true") == 0);
    }
    return result;
}

uint32_t
longBowConfig_GetUint32(const LongBowConfig *config, uint32_t defaultValue, const char *format, ...)
{
    va_list ap;
    va_start(ap, format);
    const char *value = _longBowConfig_GetProperty(config, format, ap);
    va_end(ap);

    uint32_t result = defaultValue;
    if (value != NULL) {
        result = (uint32_t) strtoul(value, NULL, 10);
    }
    return result;
}