Skip to content

Commit af27ba4

Browse files
author
Nima Talebi
committed
Completed preliminary reimplementation of type()
Updated test unit to match. Throw an exception instead of returning None/False in some functions.
1 parent 6b1598c commit af27ba4

8 files changed

Lines changed: 72 additions & 21 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ vpath % $(OBJ_D)
3939
ifeq (0,1)
4040
TEMP:
4141
sudo make install
42-
sudo python -c 'import dmidecode; print "-"*80; print dmidecode.slot(); print "-"*80; print dmidecode.type(1)'
42+
sudo python -c 'import dmidecode; print "-"*80; print dmidecode.slot(); print "-"*80; print dmidecode.type(9)'
4343
endif
4444

4545
###############################################################################

src/dmidecodemodule.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,8 @@ static PyObject *dmidecode_get_slot(PyObject * self, PyObject * args)
360360
static PyObject *dmidecode_get_type(PyObject * self, PyObject * args)
361361
{
362362
long unsigned int lu;
363+
char msg[8194];
364+
int e = 0;
363365

364366
if(PyArg_ParseTuple(args, (char *)"i", &lu)) {
365367
if(lu < 256) {
@@ -368,9 +370,17 @@ static PyObject *dmidecode_get_type(PyObject * self, PyObject * args)
368370
sprintf(s, "%lu", lu);
369371
return dmidecode_get(self, s);
370372
}
371-
return Py_False;
373+
e = 1;
374+
//return Py_False;
372375
}
373-
return Py_None;
376+
e = 2;
377+
//return Py_None;
378+
379+
if(e == 1) snprintf(msg, 8193, "Types are bound between 0 and 255 (inclusive)%c", 0);
380+
else snprintf(msg, 8193, "Invalid type identifier%c", 0);
381+
382+
PyErr_SetString(PyExc_SystemError, msg);
383+
return NULL;
374384
}
375385

376386
static PyObject *dmidecode_dump(PyObject * self, PyObject * null)

src/dmixml.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,36 @@ char *dmixml_GetAttrValue(xmlNode *node, const char *key) {
179179
return NULL;
180180
}
181181

182+
xmlNode *dmixml_FindNodeByAttr(xmlNode *node, const char *key, const char *val) {
183+
xmlNode *ptr_n = NULL;
184+
xmlChar *key_s = NULL;
185+
xmlChar *val_s = NULL;
186+
xmlChar *_val_s = NULL;
187+
188+
if( node->children == NULL ) {
189+
return NULL;
190+
}
191+
192+
key_s = xmlCharStrdup(key);
193+
assert( key_s != NULL );
194+
val_s = xmlCharStrdup(val);
195+
assert( val_s != NULL );
196+
197+
for( ptr_n = node->children; ptr_n != NULL; ptr_n = ptr_n->next ) {
198+
_val_s = xmlCharStrdup(dmixml_GetAttrValue(ptr_n, (const char *)key_s));
199+
if( (ptr_n->type == XML_ELEMENT_NODE)
200+
&& (xmlStrcmp(val_s, _val_s) == 0) ) {
201+
free(val_s); val_s = NULL;
202+
free(key_s); key_s = NULL;
203+
return ptr_n;
204+
}
205+
free(_val_s);
206+
}
207+
free(key_s); key_s = NULL;
208+
free(val_s); val_s = NULL;
209+
return NULL;
210+
}
211+
182212
xmlNode *dmixml_FindNode(xmlNode *node, const char *key) {
183213
xmlNode *ptr_n = NULL;
184214
xmlChar *key_s = NULL;

src/dmixml.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ xmlNode *dmixml_AddTextChild(xmlNode *node, const char *tagname, const char *fmt
3535
xmlNode *dmixml_AddTextContent(xmlNode *node, const char *fmt, ...);
3636

3737
char *dmixml_GetAttrValue(xmlNode *node, const char *key);
38+
xmlNode *dmixml_FindNodeByAttr(xmlNode *node, const char *key, const char *val);
3839
xmlNode *dmixml_FindNode(xmlNode *, const char *key);
3940
inline char *dmixml_GetContent(xmlNode *node);
4041
inline char *dmixml_GetNodeContent(xmlNode *node, const char *key);

src/util.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,12 @@ int write_dump(size_t base, size_t len, const void *data, const char *dumpfile,
190190
return -1;
191191
}
192192

193-
int is_int(const char *s)
193+
long is_int(const char *s)
194194
{
195+
long i = strtol(s, (char **)NULL, 10);
195196
char _s[3];
196-
snprintf(_s, 3, "%ld", strtol(s, (char **)NULL, 10));
197-
return !strcmp(s, _s);
197+
snprintf(_s, 3, "%ld", i);
198+
return strcmp(s, _s)==0 ? i : -1;
198199
}
199200

200201

src/util.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@
2828
int checksum(const u8 * buf, size_t len);
2929
void *mem_chunk(size_t base, size_t len, const char *devmem);
3030
int write_dump(size_t base, size_t len, const void *data, const char *dumpfile, int add);
31-
int is_int(const char *s);
31+
long is_int(const char *s);

src/xmlpythonizer.c

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -339,22 +339,29 @@ ptzMAP *dmiMAP_ParseMappingXML(xmlDoc *xmlmap, const char *mapname) {
339339
return NULL;
340340
}
341341

342-
if(!is_int(mapname)) {
343-
// Find the <Mapping> section matching our request (mapname)
344-
for( node = node->children->next; node != NULL; node = node->next ) {
345-
if( xmlStrcmp(node->name, (xmlChar *) "Mapping") == 0) {
346-
char *name = dmixml_GetAttrValue(node, "name");
347-
if( (name != NULL) && (strcmp(name, mapname) == 0) ) {
348-
break;
349-
}
342+
int type_id = is_int(mapname);
343+
if(type_id > -1) {
344+
//FIXME
345+
char *python_xml_typemap = strdup(PYTHON_XML_TYPEMAP);
346+
xmlDoc *typemappingxml = xmlReadFile(python_xml_typemap, NULL, 0);
347+
xmlNode *node = xmlDocGetRootElement(typemappingxml);
348+
xmlNode *wally;
349+
char type_id_hex[5];
350+
snprintf(type_id_hex, 5, "0x%02x", type_id);
351+
wally = dmixml_FindNodeByAttr(node, "id", type_id_hex);
352+
if(wally) {
353+
mapname = dmixml_GetAttrValue(wally, "value");
354+
}
355+
}
356+
357+
// Find the <Mapping> section matching our request (mapname)
358+
for( node = node->children->next; node != NULL; node = node->next ) {
359+
if( xmlStrcmp(node->name, (xmlChar *) "Mapping") == 0) {
360+
char *name = dmixml_GetAttrValue(node, "name");
361+
if( (name != NULL) && (strcmp(name, mapname) == 0) ) {
362+
break;
350363
}
351364
}
352-
} else {
353-
//. FIXME
354-
char msg[8194];
355-
snprintf(msg, 8193, "Not (yet) implemented%c", 0);
356-
PyErr_SetString(PyExc_SystemError, msg);
357-
return NULL;
358365
}
359366

360367
if( node == NULL ) {

unit-tests/unit

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ try:
161161
sys.stdout.write(" * %s\n"%output.keys())
162162
except IOError, e:
163163
failed(e, 2)
164+
except LookupError, e:
165+
failed(e, 2)
164166
except IOError:
165167
skipped()
166168

0 commit comments

Comments
 (0)