-
Notifications
You must be signed in to change notification settings - Fork 0
/
fargs.c
218 lines (193 loc) · 4.34 KB
/
fargs.c
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
/** fargs.c - provide the -fPATH option support, i.e. read option from file */
#include <search.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include "parseargs.h" /* actually only the macros are needed */
#include "fargs.h"
/* a file cache entry */
typedef struct _fc {
const char *path;
unsigned int len;
unsigned int idx;
char *lines[];
} fc;
/* the root of the file cache tree used by this instance */
static void *root = NULL;
/* file cache entry comparator */
static int
fcmp(const void *n1, const void *n2) {
const fc *fa = (const fc *) n1;
const fc *fb = (const fc *) n2;
if (fa == NULL && fb == NULL)
return 0;
if (fa == NULL)
return -1;
else if (fb == NULL)
return 1;
if (fa->path == NULL && fb->path == NULL)
return 0;
if (fa->path == NULL)
return -1;
else if (fb->path == NULL)
return 1;
return strcmp(fa->path, fb->path);
}
static void
fc_free(fc *f) {
int i;
for (i = f->len - 1; i >= 0; i--)
free(f->lines[i]);
/* to get rid off the const compiler warning use a casting trick ;-) */
free((void *) (unsigned long) f->path);
free(f);
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
static int noop(const void *keyA, const void *keyB) { return 0; }
#pragma GCC diagnostic pop
/* Delete all file cache entries. */
void
fargs_destroy(void)
{
fc *f;
while (root != NULL) {
f = *(fc **) root;
tdelete((void *) f, &root, noop);
fc_free(f);
}
}
#ifndef LINE_MAX
#define LINE_MAX 2048
#endif
/* Create and populate a new file cache item */
static fc *
fc_create(const char *path)
{
FILE *fp;
fc *f, *t;
int capacity, i, len;
struct stat statbuf;
char line[LINE_MAX];
char *s;
if ((fp = fopen(path, "r")) == NULL)
return NULL;
/* guess a line count based on size */
if (stat(path, &statbuf) < 0)
return NULL;
capacity = statbuf.st_size / 5;
if (capacity < 4)
capacity = 4;
f = (fc *) malloc(sizeof(fc) + capacity * sizeof(char *));
if (f == NULL) {
PERROR("fargs - Unable to create file contents table")
return NULL;
}
if ((f->path = strdup(path)) == NULL) {
PERROR("fargs - Unable to populate file contents table")
fc_free(f);
return NULL;
}
/* read the lines into memory */
for (i = 0; (s = fgets(line, LINE_MAX, fp)) != NULL; i++) {
if (i >= capacity) {
capacity *= 2;
t = (fc *) realloc(f, sizeof(fc) + capacity * sizeof(char *));
if (t == NULL) {
PERROR("Unable to extent file contents table to %d lines for "
"'%s' - dropping the file contents.", capacity, path)
fclose(fp);
fc_free(f);
return NULL;
}
f = t;
}
len = strlen(line);
if (len > 0 && line[len - 1] == '\n')
line[--len] = '\0';
if (len == 0)
DWARN("empty line %d in '%s'", i, path)
f->lines[i] = strdup(line);
line[0] = '\0';
}
f->len = i;
fclose(fp);
f->idx = 0;
return f;
}
/* Find the entry for a given file. If there isn't one, create it. */
static fc *
fc_find(const char *path)
{
fc t;
fc *f;
void *p;
t.path = path;
if ((p = tfind(&t, &root, fcmp)) == NULL) {
if ((f = fc_create(path)) == NULL) {
perror(path);
return NULL;
}
if ((p = tsearch((void *) f, &root, fcmp)) == NULL) {
perror(path);
return NULL;
}
}
return *(fc **) p;
}
#ifdef TEST
static int lino;
#endif
/* Get the next line (or first line if EOF has been reached) of the file with
* the given name. Once a file has been read and therefore cached, all lookups
* get satisfied via the cache unless the cache gets completely cleared using
* the fargs_destroy() function.
*
* @fname the path of the file. This string is used AS IS to lookup cached
* file entries!
* @return NULL if the file is not readable, the line without a trailing '\n'
* otherwise.
*/
char *
fileargument(const char *fname)
{
fc *f;
char *line;
if ((f = fc_find(fname)) == NULL)
return NULL;
#ifdef TEST
lino = f->idx;
#endif
line = f->lines[f->idx++];
if (f->idx >= f->len)
f->idx = 0;
return line;
}
#ifdef TEST
int
main(int argc, char **argv)
{
char arg[LINE_MAX];
char *line;
size_t l;
while (1) {
printf("file: ");
if ((fgets(arg, LINE_MAX, stdin)) == NULL)
break;
l = strlen(arg);
if (arg[l - 1] == '\n')
arg[--l] = '\0';
if (l == 0)
break;
line = fileargument(arg);
if (line)
printf("%s %d: '%s'\n", arg, lino, line);
else
printf("not found\n");
}
fargs_destroy();
}
#endif
/* vim: ts=4 sw=4 filetype=c
*/