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
|
#include "p11-tests.h"
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static char* config_data = NULL;
static char*
trim(char* data)
{
char *t;
while(*data && isspace(*data))
++data;
t = data + strlen(data);
while(t > data && isspace(*(t - 1)))
{
t--;
*t = 0;
}
return data;
}
void
p11t_config_parse(const char* filename)
{
char* name = NULL;
char* value = NULL;
char* next;
FILE* f = NULL;
long len;
char* p;
char* t;
f = fopen(filename, "r");
if(f == NULL)
p11t_msg_exit(1, "couldn't open config file: %s", filename);
/* Figure out size */
if(fseek(f, 0, SEEK_END) == -1 || (len = ftell(f)) == -1 || fseek(f, 0, SEEK_SET) == -1)
p11t_msg_exit(1, "couldn't seek config file: %s", filename);
assert(!config_data);
config_data = malloc(len + 2);
if(!config_data)
p11t_msg_exit(1, "out of memory");
/* And read in one block */
if(fread(config_data, 1, len, f) != len)
p11t_msg_exit(1, "couldn't read config file: %s", filename);
fclose(f);
/* Null terminate the data */
config_data[len] = '\n';
config_data[len + 1] = 0;
next = config_data;
/* Go through lines and process them */
while((t = strchr(next, '\n')) != NULL)
{
*t = 0;
p = next; /* Do this before cleaning below */
next = t + 1;
t = trim(p);
name = NULL;
value = NULL;
/* Empty lines / comments at start / comments without continuation */
if(!*t || *p == '#')
continue;
/* Look for the break between name = value on the same line */
t = p + strcspn(p, ":=");
if(!*t)
{
fprintf(stderr, "p11-test: invalid config line: %s", p);
exit(1);
}
/* Null terminate and split value part */
*t = 0;
t++;
name = trim(p);
value = trim(t);
if(name && value)
{
p11t_module_config(name, value);
p11t_session_config(name, value);
}
}
}
void
p11t_config_cleanup(void)
{
free(config_data);
config_data = NULL;
}
|