@ -49,13 +49,6 @@ static void write_prefix(FILE *f, int level)
fputc('\t', f);
fputc('\t', f);
}
}
enum proptype {
PROP_EMPTY,
PROP_STRING,
PROP_CELLS,
PROP_BYTES,
};
int isstring(char c)
int isstring(char c)
{
{
return (isprint(c)
return (isprint(c)
@ -63,31 +56,6 @@ int isstring(char c)
|| strchr("\a\b\t\n\v\f\r", c));
|| strchr("\a\b\t\n\v\f\r", c));
}
}
static enum proptype guess_type(struct property *prop)
{
int len = prop->val.len;
char *p = prop->val.val;
int nnotstring = 0, nnul = 0;
int i;
if (len == 0)
return PROP_EMPTY;
for (i = 0; i < len; i++) {
if (! isstring(p[i]))
nnotstring++;
if (p[i] == '\0')
nnul++;
}
if ((p[len-1] == '\0') && (nnotstring == 0) && (nnul < (len-nnul)))
return PROP_STRING;
else if ((len % sizeof(cell_t)) == 0)
return PROP_CELLS;
else
return PROP_BYTES;
}
static void write_propval_string(FILE *f, struct data val)
static void write_propval_string(FILE *f, struct data val)
{
{
char *str = val.val;
char *str = val.val;
@ -95,7 +63,7 @@ static void write_propval_string(FILE *f, struct data val)
assert(str[val.len-1] == '\0');
assert(str[val.len-1] == '\0');
fprintf(f, " = \"");
fprintf(f, "\"");
for (i = 0; i < (val.len-1); i++) {
for (i = 0; i < (val.len-1); i++) {
char c = str[i];
char c = str[i];
@ -137,7 +105,7 @@ static void write_propval_string(FILE *f, struct data val)
fprintf(f, "\\x%02hhx", c);
fprintf(f, "\\x%02hhx", c);
}
}
}
}
fprintf(f, "\";\n");
fprintf(f, "\"");
}
}
static void write_propval_cells(FILE *f, struct data val)
static void write_propval_cells(FILE *f, struct data val)
@ -145,14 +113,14 @@ static void write_propval_cells(FILE *f, struct data val)
void *propend = val.val + val.len;
void *propend = val.val + val.len;
cell_t *cp = (cell_t *)val.val;
cell_t *cp = (cell_t *)val.val;
fprintf(f, " = <");
fprintf(f, "<");
for (;;) {
for (;;) {
fprintf(f, "%x", be32_to_cpu(*cp++));
fprintf(f, "%x", be32_to_cpu(*cp++));
if ((void *)cp >= propend)
if ((void *)cp >= propend)
break;
break;
fprintf(f, " ");
fprintf(f, " ");
}
}
fprintf(f, ">;\n");
fprintf(f, ">");
}
}
static void write_propval_bytes(FILE *f, struct data val)
static void write_propval_bytes(FILE *f, struct data val)
@ -160,14 +128,45 @@ static void write_propval_bytes(FILE *f, struct data val)
void *propend = val.val + val.len;
void *propend = val.val + val.len;
char *bp = val.val;
char *bp = val.val;
fprintf(f, " = [");
fprintf(f, "[");
for (;;) {
for (;;) {
fprintf(f, "%02hhx", *bp++);
fprintf(f, "%02hhx", *bp++);
if ((void *)bp >= propend)
if ((void *)bp >= propend)
break;
break;
fprintf(f, " ");
fprintf(f, " ");
}
}
fprintf(f, "];\n");
fprintf(f, "]");
}
static void write_propval(FILE *f, struct property *prop)
{
int len = prop->val.len;
char *p = prop->val.val;
int nnotstring = 0, nnul = 0;
int i;
if (len == 0) {
fprintf(f, ";\n");
return;
}
for (i = 0; i < len; i++) {
if (! isstring(p[i]))
nnotstring++;
if (p[i] == '\0')
nnul++;
}
fprintf(f, " = ");
if ((p[len-1] == '\0') && (nnotstring == 0) && (nnul < (len-nnul))) {
write_propval_string(f, prop->val);
} else if (((len % sizeof(cell_t)) == 0)) {
write_propval_cells(f, prop->val);
} else {
write_propval_bytes(f, prop->val);
}
fprintf(f, ";\n");
}
}
static void write_tree_source_node(FILE *f, struct node *tree, int level)
static void write_tree_source_node(FILE *f, struct node *tree, int level)
@ -184,31 +183,11 @@ static void write_tree_source_node(FILE *f, struct node *tree, int level)
fprintf(f, "/ {\n");
fprintf(f, "/ {\n");
for_each_property(tree, prop) {
for_each_property(tree, prop) {
enum proptype type;
write_prefix(f, level+1);
write_prefix(f, level+1);
if (prop->label)
if (prop->label)
fprintf(f, "%s: ", prop->label);
fprintf(f, "%s: ", prop->label);
fprintf(f, "%s", prop->name);
fprintf(f, "%s", prop->name);
type = guess_type(prop);
write_propval(f, prop);
switch (type) {
case PROP_EMPTY:
fprintf(f, ";\n");
break;
case PROP_STRING:
write_propval_string(f, prop->val);
break;
case PROP_CELLS:
write_propval_cells(f, prop->val);
break;
case PROP_BYTES:
write_propval_bytes(f, prop->val);
break;
}
}
}
for_each_child(tree, child) {
for_each_child(tree, child) {
fprintf(f, "\n");
fprintf(f, "\n");