source: mainline/generic/src/sysinfo/sysinfo.c@ 35a96cf

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 35a96cf was 35a96cf, checked in by Jakub Vana <jakub.vana@…>, 19 years ago

Sysinfo userspace interface

  • Property mode set to 100644
File size: 6.3 KB
Line 
1#include <sysinfo/sysinfo.h>
2#include <mm/slab.h>
3#include <print.h>
4#include <syscall/copy.h>
5
6sysinfo_item_t *_root=NULL;
7
8
9static sysinfo_item_t* sysinfo_find_item(const char *name,sysinfo_item_t *subtree)
10{
11 if(subtree==NULL) return NULL;
12 while(subtree!=NULL)
13 {
14 int i;
15 char *a,*b;
16 a=(char *)name;
17 b=subtree->name;
18 while((a[i]==b[i])&&(b[i])) i++;
19 if((!a[i]) && (!b[i])) return subtree; /*Last name in path matches*/
20 if((a[i]=='.') && (!b[i])) /*Middle name in path matches*/
21 {
22 if(subtree->subinfo_type==SYSINFO_SUBINFO_TABLE) return sysinfo_find_item(a+i+1,subtree->subinfo.table);
23 //if(subtree->subinfo_type==SYSINFO_SUBINFO_FUNCTION) return NULL; /* Subinfo managed by subsystem*/
24 return NULL; /*No subinfo*/
25 }
26 /* No matches try next*/
27 subtree=subtree->next;
28 i=0;
29 }
30 return NULL;
31}
32
33static sysinfo_item_t* sysinfo_create_path(const char *name,sysinfo_item_t **psubtree)
34{
35 sysinfo_item_t *subtree;
36 subtree = *psubtree;
37
38 if(subtree==NULL)
39 {
40 sysinfo_item_t *item;
41 int i=0,j;
42
43 item = malloc(sizeof(sysinfo_item_t),0);
44 ASSERT(item);
45 *psubtree = item;
46 item -> next = NULL;
47 item -> val_type = SYSINFO_VAL_UNDEFINED;
48 item -> subinfo.table = NULL;
49
50 while(name[i]&&(name[i]!='.')) i++;
51
52 item -> name = malloc(i,0);
53 ASSERT(item -> name);
54
55 for(j=0;j<i;j++) item->name[j]=name[j];
56 item->name[j]=0;
57
58 if(name[i]/*=='.'*/)
59 {
60 item -> subinfo_type = SYSINFO_SUBINFO_TABLE;
61 return sysinfo_create_path(name+i+1,&(item->subinfo.table));
62 }
63 item -> subinfo_type = SYSINFO_SUBINFO_NONE;
64 return item;
65 }
66
67 while(subtree!=NULL)
68 {
69 int i=0,j;
70 char *a,*b;
71 a=(char *)name;
72 b=subtree->name;
73 while((a[i]==b[i])&&(b[i])) i++;
74 if((!a[i]) && (!b[i])) return subtree; /*Last name in path matches*/
75 if((a[i]=='.') && (!b[i])) /*Middle name in path matches*/
76 {
77 if(subtree->subinfo_type==SYSINFO_SUBINFO_TABLE) return sysinfo_create_path(a+i+1,&(subtree->subinfo.table));
78 if(subtree->subinfo_type==SYSINFO_SUBINFO_NONE)
79 {
80 subtree->subinfo_type=SYSINFO_SUBINFO_TABLE;
81 return sysinfo_create_path(a+i+1,&(subtree->subinfo.table));
82 }
83 //if(subtree->subinfo_type==SYSINFO_SUBINFO_FUNCTION) return NULL; /* Subinfo managed by subsystem*/
84 return NULL;
85 }
86 /* No matches try next or create new*/
87 if(subtree->next==NULL)
88 {
89 sysinfo_item_t *item;
90
91 item = malloc(sizeof(sysinfo_item_t),0);
92 ASSERT(item);
93 subtree -> next = item;
94 item -> next = NULL;
95 item -> val_type = SYSINFO_VAL_UNDEFINED;
96 item -> subinfo.table = NULL;
97
98 i=0;
99 while(name[i]&&(name[i]!='.')) i++;
100
101 item -> name = malloc(i,0);
102 ASSERT(item -> name);
103 for(j=0;j<i;j++) item->name[j]=name[j];
104 item->name[j]=0;
105
106 if(name[i]/*=='.'*/)
107 {
108 item -> subinfo_type = SYSINFO_SUBINFO_TABLE;
109 return sysinfo_create_path(name+i+1,&(item->subinfo.table));
110 }
111 item -> subinfo_type = SYSINFO_SUBINFO_NONE;
112 return item;
113
114 }
115 else
116 {
117 subtree=subtree->next;
118 i=0;
119 }
120 }
121 panic("Not reached\n");
122 return NULL;
123}
124
125void sysinfo_set_item_val(const char *name,sysinfo_item_t **root,__native val)
126{
127 if(root==NULL) root=&_root;
128 sysinfo_item_t *item;
129 item = sysinfo_create_path(name,root); /* If already created create only returns pointer
130 If ! , create it */
131 if(item!=NULL) /* If in subsystem, unable to create or return so unable to set */
132 {
133 item->val.val=val;
134 item -> val_type = SYSINFO_VAL_VAL;
135 }
136}
137
138void sysinfo_set_item_function(const char *name,sysinfo_item_t **root,sysinfo_val_fn_t fn)
139{
140 if(root==NULL) root=&_root;
141 sysinfo_item_t *item;
142 item = sysinfo_create_path(name,root); /* If already created create only returns pointer
143 If ! , create it */
144 if(item!=NULL) /* If in subsystem, unable to create or return so unable to set */
145 {
146 item->val.fn=fn;
147 item -> val_type = SYSINFO_VAL_FUNCTION;
148 }
149}
150
151
152void sysinfo_set_item_undefined(const char *name,sysinfo_item_t **root)
153{
154 if(root==NULL) root=&_root;
155 sysinfo_item_t *item;
156 item = sysinfo_find_item(name,*root);
157 if(item!=NULL) item -> val_type = SYSINFO_VAL_UNDEFINED;
158}
159
160
161void sysinfo_dump(sysinfo_item_t **proot,int depth)
162{
163 sysinfo_item_t *root;
164 if(proot==NULL) proot=&_root;
165 root = *proot;
166
167 while(root!=NULL)
168 {
169
170 int i;
171 __native val=0;
172 char *vtype=NULL;
173
174
175 for(i=0;i<depth;i++) printf(" ");
176
177 switch (root->val_type)
178 {
179 case (SYSINFO_VAL_UNDEFINED):
180 val=0;
181 vtype="UND";
182 break;
183 case (SYSINFO_VAL_VAL):
184 val=root->val.val;
185 vtype="VAL";
186 break;
187 case (SYSINFO_VAL_FUNCTION):
188 val=((sysinfo_val_fn_t)(root->val.fn))(root);
189 vtype="FUN";
190 break;
191 }
192 printf("%s %s val:%d(%X) sub:%s\n",root->name,vtype,val,val,
193 (root->subinfo_type==SYSINFO_SUBINFO_NONE)?"NON":((root->subinfo_type==SYSINFO_SUBINFO_TABLE)?"TAB":"FUN"));
194
195
196 if(root -> subinfo_type == SYSINFO_SUBINFO_TABLE)
197 sysinfo_dump(&(root->subinfo.table),depth+1);
198 root=root->next;
199 }
200}
201
202sysinfo_rettype_t sysinfo_get_val(const char *name,sysinfo_item_t **root)
203{
204 /*TODO:
205 Implement Subsystem subinfo (by function implemented subinfo)
206 */
207
208 sysinfo_rettype_t ret={0,false};
209
210 if(root==NULL) root=&_root;
211 sysinfo_item_t *item;
212 item = sysinfo_find_item(name,*root);
213 if(item!=NULL)
214 {
215 if (item -> val_type == SYSINFO_VAL_UNDEFINED)
216 return ret;
217 else ret.valid=true;
218 if (item -> val_type == SYSINFO_VAL_VAL)
219 ret.val=item -> val.val;
220 else ret.val=((sysinfo_val_fn_t)(item->val.fn))(item);
221 }
222 return ret;
223}
224
225__native sys_sysinfo_valid(__native ptr,__native len)
226{
227 char *str;
228 sysinfo_rettype_t ret;
229 str=malloc(len+1,0);
230 ASSERT(str);
231 copy_from_uspace(str,(void *)ptr,len+1);
232 if(str[len]) return 0; /*This is not len lenght C string*/
233 ret=sysinfo_get_val(str,NULL);
234 free(str);
235 return ret.valid;
236}
237
238__native sys_sysinfo_value(__native ptr,__native len)
239{
240 char *str;
241 sysinfo_rettype_t ret;
242 str=malloc(len+1,0);
243 ASSERT(str);
244 copy_from_uspace(str,(void *)ptr,len+1);
245 if(str[len]) return 0; /*This is not len lenght C string*/
246 ret=sysinfo_get_val(str,NULL);
247 free(str);
248 return ret.val;
249}
250
251
Note: See TracBrowser for help on using the repository browser.