1 |
|
|
/* $OpenBSD: varname.c,v 1.6 2012/08/25 08:12:56 espie Exp $ */ |
2 |
|
|
/* |
3 |
|
|
* Copyright (c) 2001 Marc Espie. |
4 |
|
|
* |
5 |
|
|
* Redistribution and use in source and binary forms, with or without |
6 |
|
|
* modification, are permitted provided that the following conditions |
7 |
|
|
* are met: |
8 |
|
|
* 1. Redistributions of source code must retain the above copyright |
9 |
|
|
* notice, this list of conditions and the following disclaimer. |
10 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright |
11 |
|
|
* notice, this list of conditions and the following disclaimer in the |
12 |
|
|
* documentation and/or other materials provided with the distribution. |
13 |
|
|
* |
14 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS |
15 |
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
16 |
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
17 |
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OPENBSD |
18 |
|
|
* PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
19 |
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
20 |
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
21 |
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
22 |
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
23 |
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
24 |
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
25 |
|
|
*/ |
26 |
|
|
|
27 |
|
|
#include <stdlib.h> |
28 |
|
|
#include "config.h" |
29 |
|
|
#include "defines.h" |
30 |
|
|
#include "var.h" |
31 |
|
|
#include "buf.h" |
32 |
|
|
#include "varname.h" |
33 |
|
|
|
34 |
|
|
const char * |
35 |
|
|
VarName_Get(const char *start, struct Name *name, SymTable *ctxt, bool err, |
36 |
|
|
const char *(*cont)(const char *)) |
37 |
|
|
{ |
38 |
|
|
const char *p; |
39 |
|
23714356 |
size_t len; |
40 |
|
|
|
41 |
|
23714356 |
p = cont(start); |
42 |
|
|
/* If we don't want recursive variables, we skip over '$' */ |
43 |
|
|
if (!FEATURES(FEATURE_RECVARS)) { |
44 |
|
|
while (*p == '$') |
45 |
|
|
p = cont(p+1); |
46 |
|
|
} |
47 |
✓✓ |
23714356 |
if (*p != '$') { |
48 |
|
23443006 |
name->s = start; |
49 |
|
23443006 |
name->e = p; |
50 |
|
23443006 |
name->tofree = false; |
51 |
|
23443006 |
return p; |
52 |
|
|
} else { |
53 |
|
271350 |
BUFFER buf; |
54 |
|
271350 |
Buf_Init(&buf, MAKE_BSIZE); |
55 |
|
543474 |
for (;;) { |
56 |
|
543474 |
Buf_Addi(&buf, start, p); |
57 |
✓✓ |
543474 |
if (*p != '$') { |
58 |
|
271350 |
name->s = (const char *)Buf_Retrieve(&buf); |
59 |
|
271350 |
name->e = name->s + Buf_Size(&buf); |
60 |
|
271350 |
name->tofree = true; |
61 |
|
|
return p; |
62 |
|
|
} |
63 |
|
|
start = p; |
64 |
|
272124 |
Var_ParseBuffer(&buf, start, ctxt, err, &len); |
65 |
|
272124 |
start += len; |
66 |
|
272124 |
p = cont(start); |
67 |
|
|
} |
68 |
|
271350 |
} |
69 |
|
23714356 |
} |
70 |
|
|
|
71 |
|
|
void |
72 |
|
|
VarName_Free(struct Name *name) |
73 |
|
|
{ |
74 |
✓✓ |
47641128 |
if (name->tofree) |
75 |
|
271350 |
free((char *)name->s); |
76 |
|
23820564 |
} |
77 |
|
|
|