aboutsummaryrefslogtreecommitdiffstats
path: root/main.c
blob: 92ced6926c3d68078f52dceae86fe143bcaf364f (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
/**
 * Copyright (c) 2016 Intel Corporation
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * AUTHORS
 * 	Aníbal Limón <anibal.limon@intel.com>
 */

#include <ctype.h>
#include <limits.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>

#ifdef MEMCHECK
#ifdef RELEASE
#error "You can't use MEMCHECK when RELEASE is active."
#endif

#include <mcheck.h>
#endif

#include "utils.h"
#include "flags.h"

#define DEFAULT_DIRECTORY "/usr/lib"
#define DEFAULT_TIMEOUT 300

static inline void
print_usage(FILE *stream, char *progname)
{
	fprintf(stream, "Usage: %s [-d directory] [-e exclude] [-l list] [-t timeout]"
			" [-x xml-filename] [-h] [ptest1 ptest2 ...]\n", progname);
}

int
main(int argc, char *argv[])
{
	int opt;
	int ptest_num = 0;
	int i;
	int rc;
	int ptest_exclude_num = 0;
	char *c, *tok;

#ifdef MEMCHECK
	mtrace();
#endif

	struct ptest_list *head, *run;
	struct ptest_options opts;

	opts.directory = strdup(DEFAULT_DIRECTORY);
	opts.exclude = NULL;
	opts.list = 0;
	opts.timeout = DEFAULT_TIMEOUT;
	opts.ptests = NULL;
	opts.xml_filename = NULL;
	opts.flags = 0;

	while ((opt = getopt(argc, argv, "d:e:lt:x:Lh")) != -1) {
		switch (opt) {
			case 'd':
				free(opts.directory);
				opts.directory = realpath(optarg, NULL);
				CHECK_ALLOCATION(opts.directory, 1, 1);
			break;
			case 'e':
				c = optarg;
				ptest_exclude_num = 1;

				while (*c) {
					if (isspace(*c))
						ptest_exclude_num++;
					c++;
				}


				opts.exclude = malloc(ptest_exclude_num * sizeof(char));
				CHECK_ALLOCATION(opts.exclude, 1, 1);

				i = 0;
				tok = strtok_r(optarg, " ", &c);
				opts.exclude[i] = strdup(tok);
				CHECK_ALLOCATION(opts.exclude[i], 1, 1);
				i++;
				while ((tok = strtok_r(NULL, " ", &c)) != NULL) {
					opts.exclude[i] = strdup(tok);
					CHECK_ALLOCATION(opts.exclude[i], 1, 1);
					i++;
				}
			break;
			case 'l':
				opts.list = 1;
			break;
			case 't':
				opts.timeout = atoi(optarg);
			break;
			case 'h':
				print_usage(stdout, argv[0]);
				exit(0);
			break;
			case 'x':
				free(opts.xml_filename);
				opts.xml_filename = strdup(optarg);
				CHECK_ALLOCATION(opts.xml_filename, 1, 1);
			break;
			case 'L':
				// set LAVA signal mode
				opts.flags |= LAVA_SIGNAL_ENABLE;
				fprintf(stdout, "LAVA_SIGNAL_ENABLE == %d\n", opts.flags);
			break;
			default:
				print_usage(stdout, argv[0]);
				exit(1);
			break;
		}
	}

	ptest_num = argc - optind;
	if (ptest_num > 0) {
		size_t size = ptest_num * sizeof(char *);
		opts.ptests = calloc(1, size);
		CHECK_ALLOCATION(opts.ptests, size, 1);

		for (i = 0; i < ptest_num; i++) {
			opts.ptests[i] = strdup(argv[argc - ptest_num + i]);
			CHECK_ALLOCATION(opts.ptests[i], 1, 1);
		}
	}

	head = get_available_ptests(opts.directory);
	if (head == NULL || ptest_list_length(head) == 0) {
		fprintf(stderr, PRINT_PTESTS_NOT_FOUND);
		return 1;
	}

	if (opts.list) {
		print_ptests(head, stdout);
		return 0;
	}

	run = head;
	if (ptest_num > 0) {
		for (i = 0; i < ptest_num; i++) {
			if (ptest_list_search(head, opts.ptests[i]) == NULL) {
				fprintf(stderr, "%s ptest isn't available.\n",
					opts.ptests[i]);
				return 1;
			}
		}

		run = filter_ptests(head, opts.ptests, ptest_num);
		CHECK_ALLOCATION(run, ptest_num, 1);
		ptest_list_free_all(head);
	}

	for (i = 0; i < ptest_exclude_num; i++)
		ptest_list_remove(run, opts.exclude[i], 1);

	rc = run_ptests(run, opts, argv[0], stdout, stderr);

	ptest_list_free_all(run);

	return rc;
}