aboutsummaryrefslogtreecommitdiff
path: root/samples/test-event.c
blob: b25da1e1701909f2886fc79f446622fe5d825159 (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
// SPDX-License-Identifier: LGPL-2.1
/*
 * Copyright (C) 2022 Google Inc, Steven Rostedt <rostedt@goodmis.org>
 */
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <getopt.h>
#include <event-parse.h>

static char *argv0;

static char *get_this_name(void)
{
	static char *this_name;
	char *arg;
	char *p;

	if (this_name)
		return this_name;

	arg = argv0;
	p = arg+strlen(arg);

	while (p >= arg && *p != '/')
		p--;
	p++;

	this_name = p;
	return p;
}

static void usage(void)
{
	char *p = get_this_name();

	printf("usage: %s [options]\n"
	       " -h : this message\n"
	       " -s system : the system for the event\n"
	       " -e format : the event format file\n"
	       " -f file : file to read the event from\n"
	       "      otherwise, reads from stdin\n"
	       "\n",p);
	exit(-1);
}

static void __vdie(const char *fmt, va_list ap, int err)
{
	int ret = errno;
	char *p = get_this_name();

	if (err && errno)
		perror(p);
	else
		ret = -1;

	fprintf(stderr, "  ");
	vfprintf(stderr, fmt, ap);

	fprintf(stderr, "\n");
	exit(ret);
}

void die(const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	__vdie(fmt, ap, 0);
	va_end(ap);
}

void pdie(const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	__vdie(fmt, ap, 1);
	va_end(ap);
}

/* Must be a power of two */
#define BUFALLOC	1024
#define BUFMASK		(~(BUFALLOC - 1))

int main(int argc, char **argv)
{
	struct tep_handle *tep;
	struct tep_event *event;
	FILE *file = stdin;
	FILE *fp = NULL;
	char *system = NULL;
	char *event_buf = NULL;
	int esize = 0;
	int c;

	argv0 = argv[0];

	while ((c = getopt(argc, argv, "hs:e:f:")) >= 0) {
		switch (c) {
		case 's':
			system = optarg;
			break;
		case 'e':
			event_buf = optarg;
			file = NULL;
			break;
		case 'f':
			fp = fopen(optarg, "r");
			if (!fp)
				pdie("%s", optarg);
			file = fp;
			break;
		case 'h':
			usage();
		}
	}
	if (file) {
		char *line = NULL;
		size_t n = 0;
		int len;

		while (getline(&line, &n, file) > 0) {
			len = strlen(line) + 1;

			if (((esize - 1) & BUFMASK) < ((esize + len) & BUFMASK)) {
				int a;

				a = (esize + len + BUFALLOC - 1) & BUFMASK;
				event_buf = realloc(event_buf, a);
				if (!event_buf)
					pdie("allocating event");
			}
			strcpy(event_buf + esize, line);
			esize += len - 1;
		}
		free(line);
	}

	tep = tep_alloc();
	if (!tep)
		pdie("Allocating tep handle");

	tep_set_loglevel(TEP_LOG_ALL);

	if (!system)
		system = "test";

	if (tep_parse_format(tep, &event, event_buf, esize, system))
		die("Failed to parse event");

}