-
Notifications
You must be signed in to change notification settings - Fork 0
/
isolate_value.c
100 lines (73 loc) · 1.36 KB
/
isolate_value.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
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#include"cgiaudit.h"
static const char*err_str="While parsing value from tag attribute";
/*
* Isolate values from hash pairs in HTML tags.
*
* @param porig pointer to the beginning of the value
* @return c-style representation of the value
*/
/*
* Discard HTML tokens which function as hash pair seperators.
*/
/* void discard_tokens_1(char**pp)
{
while(**pp&&isspace(**pp))
(*pp)++;
if(!**pp||**pp!='=')
syntax_error(err_str,1);
(*pp)++;
if(**pp=='"')
(*pp)++;
if(!**pp)
syntax_error(err_str,1);
return;
} */
/* #define STR "NAME=BLAH heh"
int main(void){
char*p=malloc(strlen(STR)+1);
strcpy(p,STR);
puts(p+=4);
puts(isolate_value(&p));
puts(p);
} */
char*isolate_value(char**porig)
{
register char*p=*porig;
char c=0,*val,*oldp;
while(*p&&isspace(*p))
p++;
if(!*p||*p!='=')
syntax_error(err_str,1);
p++;
while(*p&&isspace(*p))
p++;
if(*p=='"')
p+=c=1;
if(!*p)
syntax_error(err_str,1);
oldp=p;
if(c)
while(*p&&*p!='"')
p++;
else
while(*p&&!isspace(*p)&&*p!='>')
p++;
if(!*p)
syntax_error(err_str,1);
c=*p;
*p=0;
val=malloc(p-oldp+1);
if(!val)
vexit("malloc");
strcpy(val,oldp);
*p=c; /* overwrite null for strcpy() with original character */
*porig=p;
if(*p=='>')
return val;
do p++; while(*p&&isspace(*p));
*porig=p;
return val;
}