Repository navigation
Expand file tree
/
Copy pathqueryparam.c
More file actions
150 lines (132 loc) · 3.09 KB
/
Copy pathqueryparam.c
File metadata and controls
150 lines (132 loc) · 3.09 KB
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
/* queryparam() - allow program parameters to be queried
* Author: Kees J. Bot
* 21 Apr 1994
*/
#define nil 0
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include "queryparam.h"
#if EXAMPLE
struct stat st[2];
struct export_param_list ex_st_list[]= {
QP_VARIABLE(st),
QP_ARRAY(st),
QP_FIELD(st_dev, struct stat),
QP_FIELD(st_ino, struct stat),
...
QP_END()
};
struct buf { block_t b_blocknr; ... } *buf;
size_t nr_bufs;
struct export_param_list ex_buf_list[]=
QP_VECTOR(buf, buf, nr_bufs),
QP_FIELD(b_blocknr),
...
QP_END()
};
struct export_params ex_st= { ex_st_list, 0 };
struct export_params ex_buf= { ex_buf_list, 0 };
#endif
#define between(a, c, z) ((unsigned) ((c) - (a)) <= (unsigned) ((z) - (a)))
static int isvar(int c)
{
return between('a', c, 'z') || between('A', c, 'Z')
|| between('0', c, '9') || c == '_';
}
static struct export_params *params;
void qp_export(struct export_params *ex_params)
{
/* Add a set of exported parameters. */
if (ex_params->next == nil) {
ex_params->next= params;
params= ex_params;
}
}
int queryparam(int qgetc(void), void **poffset, size_t *psize)
{
char *prefix;
struct export_params *ep;
struct export_param_list *epl;
size_t offset= 0;
size_t size= -1;
size_t n;
static size_t retval;
int c, firstc;
firstc= c= (*qgetc)();
if (c == '&' || c == '$') c= (*qgetc)();
if (!isvar(c)) goto fail;
if ((ep= params) == nil) g
791F
oto fail;
epl= ep->list;
while (c != 0 && c != ',') {
prefix= (char *) "x";
n= 0;
for (;;) {
while (epl->name == nil) {
if ((ep= ep->next) == nil) goto fail;
epl= ep->list;
}
if (strncmp(prefix, epl->name, n) == 0) {
prefix= epl->name;
while (prefix[n] != 0 && c == prefix[n]) {
n++;
c= (*qgetc)();
}
}
if (prefix[n] == 0 && (!isvar(c) || prefix[0] == '[')) {
/* Got a match. */
break;
}
epl++;
}
if (prefix[0] == '[') {
/* Array reference. */
size_t idx= 0, cnt= 1, max= size / epl->size;
while (between('0', c, '9')) {
idx= idx * 10 + (c - '0');
if (idx > max) goto fail;
c= (*qgetc)();
}
if (c == ':') {
cnt= 0;
while (between('0', (c= (*qgetc)()), '9')) {
cnt= cnt * 10 + (c - '0');
}
}
if (c != ']') goto fail;
if (idx + cnt > max) cnt= max - idx;
offset+= idx * epl->size;
size= cnt * epl->size;
c= (*qgetc)();
} else
if (epl->size == -1) {
/* Vector. */
offset= (size_t) * (void **) epl->offset;
size= (* (size_t *) epl[1].offset) * epl[1].size;
} else {
/* Variable or struct field. */
offset+= (size_t) epl->offset;
if ((size_t) epl->offset > size) goto fail;
size-= (size_t) epl->offset;
if (size < epl->size) goto fail;
size= epl->size;
}
}
if (firstc == '&' || firstc == '$') {
retval= firstc == '&' ? offset : size;
offset= (size_t) &retval;
size= sizeof(retval);
}
if (c != 0 && c != ',') goto fail;
*poffset= (void *) offset;
*psize= size;
return c != 0;
fail:
while (c != 0 && c != ',') c= (*qgetc)();
*poffset= nil;
*psize= 0;
return c != 0;
}
/*
* $PchId: queryparam.c,v 1.1 2005/06/28 14:30:56 philip Exp $
*/