Rev 1318 | Rev 1322 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1321 | vana | 1 | /* |
2 | * Copyright (C) 2006 Jakub Vana |
||
3 | * All rights reserved. |
||
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 | * |
||
9 | * - Redistributions of source code must retain the above copyright |
||
10 | * notice, this list of conditions and the following disclaimer. |
||
11 | * - Redistributions in binary form must reproduce the above copyright |
||
12 | * notice, this list of conditions and the following disclaimer in the |
||
13 | * documentation and/or other materials provided with the distribution. |
||
14 | * - The name of the author may not be used to endorse or promote products |
||
15 | * derived from this software without specific prior written permission. |
||
16 | * |
||
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
||
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
||
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
||
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
||
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
27 | */ |
||
28 | |||
1317 | vana | 29 | #include <sysinfo/sysinfo.h> |
30 | #include <mm/slab.h> |
||
31 | #include <print.h> |
||
1318 | vana | 32 | #include <syscall/copy.h> |
1317 | vana | 33 | |
34 | sysinfo_item_t *_root=NULL; |
||
35 | |||
36 | |||
37 | static sysinfo_item_t* sysinfo_find_item(const char *name,sysinfo_item_t *subtree) |
||
38 | { |
||
39 | if(subtree==NULL) return NULL; |
||
40 | while(subtree!=NULL) |
||
41 | { |
||
42 | int i; |
||
43 | char *a,*b; |
||
44 | a=(char *)name; |
||
45 | b=subtree->name; |
||
46 | while((a[i]==b[i])&&(b[i])) i++; |
||
47 | if((!a[i]) && (!b[i])) return subtree; /*Last name in path matches*/ |
||
48 | if((a[i]=='.') && (!b[i])) /*Middle name in path matches*/ |
||
49 | { |
||
50 | if(subtree->subinfo_type==SYSINFO_SUBINFO_TABLE) return sysinfo_find_item(a+i+1,subtree->subinfo.table); |
||
51 | //if(subtree->subinfo_type==SYSINFO_SUBINFO_FUNCTION) return NULL; /* Subinfo managed by subsystem*/ |
||
52 | return NULL; /*No subinfo*/ |
||
53 | } |
||
54 | /* No matches try next*/ |
||
55 | subtree=subtree->next; |
||
56 | i=0; |
||
57 | } |
||
58 | return NULL; |
||
59 | } |
||
60 | |||
61 | static sysinfo_item_t* sysinfo_create_path(const char *name,sysinfo_item_t **psubtree) |
||
62 | { |
||
63 | sysinfo_item_t *subtree; |
||
64 | subtree = *psubtree; |
||
65 | |||
66 | if(subtree==NULL) |
||
67 | { |
||
68 | sysinfo_item_t *item; |
||
69 | int i=0,j; |
||
70 | |||
71 | item = malloc(sizeof(sysinfo_item_t),0); |
||
72 | ASSERT(item); |
||
73 | *psubtree = item; |
||
74 | item -> next = NULL; |
||
75 | item -> val_type = SYSINFO_VAL_UNDEFINED; |
||
76 | item -> subinfo.table = NULL; |
||
77 | |||
78 | while(name[i]&&(name[i]!='.')) i++; |
||
79 | |||
80 | item -> name = malloc(i,0); |
||
81 | ASSERT(item -> name); |
||
82 | |||
83 | for(j=0;j<i;j++) item->name[j]=name[j]; |
||
84 | item->name[j]=0; |
||
85 | |||
86 | if(name[i]/*=='.'*/) |
||
87 | { |
||
88 | item -> subinfo_type = SYSINFO_SUBINFO_TABLE; |
||
89 | return sysinfo_create_path(name+i+1,&(item->subinfo.table)); |
||
90 | } |
||
91 | item -> subinfo_type = SYSINFO_SUBINFO_NONE; |
||
92 | return item; |
||
93 | } |
||
94 | |||
95 | while(subtree!=NULL) |
||
96 | { |
||
97 | int i=0,j; |
||
98 | char *a,*b; |
||
99 | a=(char *)name; |
||
100 | b=subtree->name; |
||
101 | while((a[i]==b[i])&&(b[i])) i++; |
||
102 | if((!a[i]) && (!b[i])) return subtree; /*Last name in path matches*/ |
||
103 | if((a[i]=='.') && (!b[i])) /*Middle name in path matches*/ |
||
104 | { |
||
105 | if(subtree->subinfo_type==SYSINFO_SUBINFO_TABLE) return sysinfo_create_path(a+i+1,&(subtree->subinfo.table)); |
||
106 | if(subtree->subinfo_type==SYSINFO_SUBINFO_NONE) |
||
107 | { |
||
108 | subtree->subinfo_type=SYSINFO_SUBINFO_TABLE; |
||
109 | return sysinfo_create_path(a+i+1,&(subtree->subinfo.table)); |
||
110 | } |
||
111 | //if(subtree->subinfo_type==SYSINFO_SUBINFO_FUNCTION) return NULL; /* Subinfo managed by subsystem*/ |
||
112 | return NULL; |
||
113 | } |
||
114 | /* No matches try next or create new*/ |
||
115 | if(subtree->next==NULL) |
||
116 | { |
||
117 | sysinfo_item_t *item; |
||
118 | |||
119 | item = malloc(sizeof(sysinfo_item_t),0); |
||
120 | ASSERT(item); |
||
121 | subtree -> next = item; |
||
122 | item -> next = NULL; |
||
123 | item -> val_type = SYSINFO_VAL_UNDEFINED; |
||
124 | item -> subinfo.table = NULL; |
||
125 | |||
126 | i=0; |
||
127 | while(name[i]&&(name[i]!='.')) i++; |
||
128 | |||
129 | item -> name = malloc(i,0); |
||
130 | ASSERT(item -> name); |
||
131 | for(j=0;j<i;j++) item->name[j]=name[j]; |
||
132 | item->name[j]=0; |
||
133 | |||
134 | if(name[i]/*=='.'*/) |
||
135 | { |
||
136 | item -> subinfo_type = SYSINFO_SUBINFO_TABLE; |
||
137 | return sysinfo_create_path(name+i+1,&(item->subinfo.table)); |
||
138 | } |
||
139 | item -> subinfo_type = SYSINFO_SUBINFO_NONE; |
||
140 | return item; |
||
141 | |||
142 | } |
||
143 | else |
||
144 | { |
||
145 | subtree=subtree->next; |
||
146 | i=0; |
||
147 | } |
||
148 | } |
||
149 | panic("Not reached\n"); |
||
150 | return NULL; |
||
151 | } |
||
152 | |||
153 | void sysinfo_set_item_val(const char *name,sysinfo_item_t **root,__native val) |
||
154 | { |
||
155 | if(root==NULL) root=&_root; |
||
156 | sysinfo_item_t *item; |
||
157 | item = sysinfo_create_path(name,root); /* If already created create only returns pointer |
||
158 | If ! , create it */ |
||
159 | if(item!=NULL) /* If in subsystem, unable to create or return so unable to set */ |
||
160 | { |
||
161 | item->val.val=val; |
||
162 | item -> val_type = SYSINFO_VAL_VAL; |
||
163 | } |
||
164 | } |
||
165 | |||
166 | void sysinfo_set_item_function(const char *name,sysinfo_item_t **root,sysinfo_val_fn_t fn) |
||
167 | { |
||
168 | if(root==NULL) root=&_root; |
||
169 | sysinfo_item_t *item; |
||
170 | item = sysinfo_create_path(name,root); /* If already created create only returns pointer |
||
171 | If ! , create it */ |
||
172 | if(item!=NULL) /* If in subsystem, unable to create or return so unable to set */ |
||
173 | { |
||
174 | item->val.fn=fn; |
||
175 | item -> val_type = SYSINFO_VAL_FUNCTION; |
||
176 | } |
||
177 | } |
||
178 | |||
179 | |||
180 | void sysinfo_set_item_undefined(const char *name,sysinfo_item_t **root) |
||
181 | { |
||
182 | if(root==NULL) root=&_root; |
||
183 | sysinfo_item_t *item; |
||
184 | item = sysinfo_find_item(name,*root); |
||
185 | if(item!=NULL) item -> val_type = SYSINFO_VAL_UNDEFINED; |
||
186 | } |
||
187 | |||
188 | |||
189 | void sysinfo_dump(sysinfo_item_t **proot,int depth) |
||
190 | { |
||
191 | sysinfo_item_t *root; |
||
192 | if(proot==NULL) proot=&_root; |
||
193 | root = *proot; |
||
194 | |||
195 | while(root!=NULL) |
||
196 | { |
||
197 | |||
198 | int i; |
||
199 | __native val=0; |
||
200 | char *vtype=NULL; |
||
201 | |||
202 | |||
203 | for(i=0;i<depth;i++) printf(" "); |
||
204 | |||
205 | switch (root->val_type) |
||
206 | { |
||
207 | case (SYSINFO_VAL_UNDEFINED): |
||
208 | val=0; |
||
209 | vtype="UND"; |
||
210 | break; |
||
211 | case (SYSINFO_VAL_VAL): |
||
212 | val=root->val.val; |
||
213 | vtype="VAL"; |
||
214 | break; |
||
215 | case (SYSINFO_VAL_FUNCTION): |
||
216 | val=((sysinfo_val_fn_t)(root->val.fn))(root); |
||
217 | vtype="FUN"; |
||
218 | break; |
||
219 | } |
||
220 | printf("%s %s val:%d(%X) sub:%s\n",root->name,vtype,val,val, |
||
221 | (root->subinfo_type==SYSINFO_SUBINFO_NONE)?"NON":((root->subinfo_type==SYSINFO_SUBINFO_TABLE)?"TAB":"FUN")); |
||
222 | |||
223 | |||
224 | if(root -> subinfo_type == SYSINFO_SUBINFO_TABLE) |
||
225 | sysinfo_dump(&(root->subinfo.table),depth+1); |
||
226 | root=root->next; |
||
227 | } |
||
228 | } |
||
229 | |||
230 | sysinfo_rettype_t sysinfo_get_val(const char *name,sysinfo_item_t **root) |
||
231 | { |
||
232 | /*TODO: |
||
233 | Implement Subsystem subinfo (by function implemented subinfo) |
||
234 | */ |
||
235 | |||
236 | sysinfo_rettype_t ret={0,false}; |
||
237 | |||
238 | if(root==NULL) root=&_root; |
||
239 | sysinfo_item_t *item; |
||
240 | item = sysinfo_find_item(name,*root); |
||
241 | if(item!=NULL) |
||
242 | { |
||
243 | if (item -> val_type == SYSINFO_VAL_UNDEFINED) |
||
244 | return ret; |
||
245 | else ret.valid=true; |
||
246 | if (item -> val_type == SYSINFO_VAL_VAL) |
||
247 | ret.val=item -> val.val; |
||
248 | else ret.val=((sysinfo_val_fn_t)(item->val.fn))(item); |
||
249 | } |
||
250 | return ret; |
||
251 | } |
||
1318 | vana | 252 | |
253 | __native sys_sysinfo_valid(__native ptr,__native len) |
||
254 | { |
||
255 | char *str; |
||
256 | sysinfo_rettype_t ret; |
||
257 | str=malloc(len+1,0); |
||
258 | ASSERT(str); |
||
1321 | vana | 259 | if(copy_from_uspace(str,(void *)ptr,len+1)) return 0; |
1318 | vana | 260 | if(str[len]) return 0; /*This is not len lenght C string*/ |
261 | ret=sysinfo_get_val(str,NULL); |
||
262 | free(str); |
||
263 | return ret.valid; |
||
264 | } |
||
265 | |||
266 | __native sys_sysinfo_value(__native ptr,__native len) |
||
267 | { |
||
268 | char *str; |
||
269 | sysinfo_rettype_t ret; |
||
270 | str=malloc(len+1,0); |
||
271 | ASSERT(str); |
||
1321 | vana | 272 | if(copy_from_uspace(str,(void *)ptr,len+1)) return 0; |
1318 | vana | 273 | if(str[len]) return 0; /*This is not len lenght C string*/ |
274 | ret=sysinfo_get_val(str,NULL); |
||
275 | free(str); |
||
276 | return ret.val; |
||
277 | } |
||
278 | |||
279 |