Site update, preparation for adesklets 0.4.11.
[adesklets.git] / src / variable.c
blob7c5a654ca5ea4217a0a47fd7407615542f413905
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
14 used.
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 /*----------------------------------------------------------------------------*/
33 var_item *
34 variable_create(const char * name, const char * value)
36 var_item * item;
37 if ((item=malloc(sizeof(var_item)))) {
38 item->name=dupstr(name);
39 item->value=dupstr(value);
41 return item;
44 /*----------------------------------------------------------------------------*/
45 int
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.
57 void
58 variable_expansion(vector * vars, char ** old)
60 int i,j,k,l,size;
61 uint dummy;
62 char * new, * var;
63 var_item * v_item;
65 if(*old) {
66 size=strlen(*old)+1;
67 if ((new=malloc(sizeof(char)*size))) {
68 for(i=0,j=0;(*old)[i];++i) {
69 if ((*old)[i]!='$')
70 new[j++]=(*old)[i];
71 else
72 if ((*old)[i+1]!='$') {
73 for(k=i+1;
74 (*old)[k] &&
75 (*old)[k]!=' ' && (*old)[k]!='\t' && (*old)[k]!='$';
76 ++k);
77 if((var=malloc(sizeof(char)*(k-i)))) {
78 strncpy(var,(*old)+i+1,(k-i-1));
79 var[k-i-1]=(char)0;
80 if ((v_item=vector_find(vars,
81 &dummy,
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];
87 j+=l;
89 free(var);var=NULL;
91 i=k-1;
92 } else {
93 new[j++]='$';
94 if ((*old)[i+1])
95 ++i;
98 new[j]=0;
99 free(*old);
100 (*old)=new;
105 /*----------------------------------------------------------------------------*/
106 void *
107 variable_vector_free_item(void* item)
109 TRY_RESET;
110 TRY(free(((var_item*)item)->name));
111 if (TRY_SUCCESS) {
112 TRY(free(((var_item*)item)->value));
113 if (TRY_SUCCESS)
114 TRY(free(item));
116 #ifdef DEBUG
117 if (!TRY_SUCCESS)
118 debug("Ouch! Memory leak while freeing variable.");
119 #endif
120 return NULL;
123 /*----------------------------------------------------------------------------*/