1 /*--- variable.c -------------------------------------------------------------
2 Copyright (C) 2004, 2005 Sylvain Fourmanoit <syfou@users.sourceforge.net>
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to
6 deal in the Software without restriction, including without limitation the
7 rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8 sell copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
11 The above copyright notice and this permission notice shall be included in
12 all copies of the Software and its documentation and acknowledgment shall be
13 given in the documentation and software packages that this Software was
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 ------------------------------------------------------------------------------*/
23 /* Textual variables related functions
26 /*----------------------------------------------------------------------------*/
27 #include "variable.h" /* Variable header */
29 /*----------------------------------------------------------------------------*/
30 extern char * dupstr(const char *s
); /* Defined in command.c */
32 /*----------------------------------------------------------------------------*/
34 variable_create(const char * name
, const char * value
)
37 if ((item
=malloc(sizeof(var_item
)))) {
38 item
->name
=dupstr(name
);
39 item
->value
=dupstr(value
);
44 /*----------------------------------------------------------------------------*/
46 variable_search_func(void* item
, void* name
)
48 return strcmp(((var_item
*)item
)->name
,(char*)name
)==0;
51 /*----------------------------------------------------------------------------*/
52 /* This is the big ugly variable expansion routine. It takes each first group
53 from matching /$([^$[:space:]]*)/ and expands it to its found value
54 in the *vars vertor. C is definitely not the best tool
55 for string manipulation.
58 variable_expansion(vector
* vars
, char ** old
)
67 if ((new=malloc(sizeof(char)*size
))) {
68 for(i
=0,j
=0;(*old
)[i
];++i
) {
72 if ((*old
)[i
+1]!='$') {
75 (*old
)[k
]!=' ' && (*old
)[k
]!='\t' && (*old
)[k
]!='$';
77 if((var
=malloc(sizeof(char)*(k
-i
)))) {
78 strncpy(var
,(*old
)+i
+1,(k
-i
-1));
80 if ((v_item
=vector_find(vars
,
82 variable_search_func
,var
))) {
83 size
+=strlen(v_item
->value
)+1;
84 new=realloc(new,sizeof(char)*size
);
85 for(l
=0;v_item
->value
[l
];++l
)
86 new[j
+l
]=v_item
->value
[l
];
105 /*----------------------------------------------------------------------------*/
107 variable_vector_free_item(void* item
)
110 TRY(free(((var_item
*)item
)->name
));
112 TRY(free(((var_item
*)item
)->value
));
118 debug("Ouch! Memory leak while freeing variable.");
123 /*----------------------------------------------------------------------------*/