Skip to contentSkip to conversation

Cannot use list of strings

Qemu-devel New Lluís Vilanova 6 participants 15 messages Apr 15, 2013 – Apr 26, 2013

lore
Share & tools
lore
#0 lore raw

Tried using a list of strings as an argument to a command, but the generated code references the 'strList' type, which does not exist.

Is a specialized version for "['str']" missing, or should I define my own type with a single field of 'str' type?

Thanks,
Lluis

Signature · 4 lines
 "And it's much the same thing with knowledge, for whenever you learn
 something new, the whole world becomes that much richer."
 -- The Princess of Pure Reason, as told by Norton Juster in The Phantom
 Tollbooth
#1 lore raw
Quoted 5 lines · Lluís Vilanova

Tried using a list of strings as an argument to a command, but the generated code references the 'strList' type, which does not exist.

Is a specialized version for "['str']" missing, or should I define my own type with a single field of 'str' type?

I could also not find some handy code in QEMU to parse the arguments for the instrumentation library. For example:

    qemu -instr file=/blah.so,arg=a1,arg=a2

Or just:

qemu -instr file=/blah.so,args=a1,a2

As it seems, QEMU's option parsing has no routine that I could find to perform this (it also looks like it's not possible to escape commas).

Thanks,
Lluis

Signature · 4 lines
 "And it's much the same thing with knowledge, for whenever you learn
 something new, the whole world becomes that much richer."
 -- The Princess of Pure Reason, as told by Norton Juster in The Phantom
 Tollbooth
#2 lore raw
In reply to Lluís Vilanova
Quoted 5 lines · Lluís Vilanova

Tried using a list of strings as an argument to a command, but the generated code references the 'strList' type, which does not exist.

Is a specialized version for "['str']" missing, or should I define my own type with a single field of 'str' type?

akong just hit this too.

I think it's a question for aliguori, luiz, or mdroth.

Stefan

#3 lore raw

Il 16/04/2013 10:49, Stefan Hajnoczi ha scritto:

Quoted 8 lines
Quoted 5 lines
Quoted 5 lines

Tried using a list of strings as an argument to a command, but the generated code references the 'strList' type, which does not exist.

Is a specialized version for "['str']" missing, or should I define my own type with a single field of 'str' type?

akong just hit this too.

I think it's a question for aliguori, luiz, or mdroth.

Laszlo defined and used String for this purpose:

##
# @String
#
# A fat type wrapping 'str', to be embedded in lists. #
# Since 1.2
##
{ 'type': 'String',

  'data': {
    'str': 'str' } }

Paolo

#4 lore raw
Quoted 7 lines · Paolo Bonzini

Il 16/04/2013 10:49, Stefan Hajnoczi ha scritto:

Quoted 6 lines
Quoted 5 lines
Quoted 5 lines

Tried using a list of strings as an argument to a command, but the generated code references the 'strList' type, which does not exist.

Is a specialized version for "['str']" missing, or should I define my own type with a single field of 'str' type?

akong just hit this too.

That thread: http://marc.info/?l=qemu-devel&m=136572276530702&w=3

Quoted 3 lines
Quoted 1 line

I think it's a question for aliguori, luiz, or mdroth.

Laszlo defined and used String for this purpose:

Eric said String list contains additional JSON structure. At least, it works.

===================== using StringList

    '*unicast':        ['String'],
    '*multicast':      ['String']
{
    "return": [
        {
            "name": "virtio-net-pci.0", 
            "multicast": [
                {
                    "str": "01:80:c2:00:00:21"
                }, 
                {
                    "str": "00:00:00:00:00:00"
                }
            ]
        }, 
        ....
    ]
}
======================== using strList
    '*unicast':        ['str'],
    '*multicast':      ['str']

Eric, is it expected format?

{
    "return": [
        {
            "name": "virtio-net-pci.0", 
            "multicast": [
                    "str": "01:80:c2:00:00:21",
                    "str": "00:00:00:00:00:00"
            ]
        },
        ....
    ]
}

========================

I changed qapi scripts to define struct, struct list, visit/free functions for 'str'. But it conflicts with existed str functions. I hadn't find a solution.

Do we need to defind strList & visit functions in qapi-core files? not generated by scripts.

scripts/qapi-types.py +13 -0
index 9e19920..5167d85 100644
@@ -282,6 +282,10 @@ fdecl.write(mcgen('''
 exprs = parse_schema(sys.stdin)
 exprs = filter(lambda expr: not expr.has_key('gen'), exprs)
 
+ret = "\n"
+ret += generate_fwd_struct('str', {'str': 'str'})
+fdecl.write(ret)
+
 for expr in exprs:
     ret = "\n"
     if expr.has_key('type'):
@@ -319,6 +323,15 @@ for expr in exprs:
         continue
     fdecl.write(ret)
 
+ret = "\n"
+ret += generate_struct('str', "", {'str': 'str'}) + "\n"
+ret += generate_type_cleanup_decl('str' + "List")
+fdef.write(generate_type_cleanup('str' + "List") + "\n")
+ret += generate_type_cleanup_decl('str')
+fdef.write(generate_type_cleanup('str') + "\n")
+
+fdecl.write(ret)
+
 fdecl.write('''
 #endif
 ''')
scripts/qapi-visit.py +7 -0
index a276540..089dda7 100644
@@ -354,6 +354,13 @@ for expr in exprs:
         ret += generate_enum_declaration(expr['enum'], expr['data'])
         fdecl.write(ret)
 
+ret = generate_visit_struct('str', {'str': 'str'})
+ret += generate_visit_list('str', {'str': 'str'})
+fdef.write(ret)
+
+ret = generate_declaration('str', {'str': 'str'})
+fdecl.write(ret)
+
 fdecl.write('''
 #endif
 ''')
Quoted 10 lines

##
# @String
#
# A fat type wrapping 'str', to be embedded in lists. #
# Since 1.2
##
{ 'type': 'String',

  'data': {
    'str': 'str' } }
Signature · 1 line
			Amos.
#6 lore raw
In reply to Paolo Bonzini
Quoted 11 lines · Paolo Bonzini

Il 16/04/2013 10:49, Stefan Hajnoczi ha scritto:

Quoted 8 lines
Quoted 5 lines
Quoted 5 lines

Tried using a list of strings as an argument to a command, but the generated code references the 'strList' type, which does not exist.

Is a specialized version for "['str']" missing, or should I define my own type with a single field of 'str' type?

akong just hit this too.

I think it's a question for aliguori, luiz, or mdroth.

Laszlo defined and used String for this purpose:

Ok, thanks for the info. I suppose I'll go for 'StringList' in the meantime.

Lluis

Signature · 4 lines
 "And it's much the same thing with knowledge, for whenever you learn
 something new, the whole world becomes that much richer."
 -- The Princess of Pure Reason, as told by Norton Juster in The Phantom
 Tollbooth
#5 lore raw
In reply to Amos Kong
Quoted 2 lines · Amos Kong

Eric said String list contains additional JSON structure. At least, it works.

Using the fat 'String' wrapper works, but requires more effort to decode.

Quoted 5 lines

===================== using StringList

    '*unicast':        ['String'],
    '*multicast':      ['String']
Quoted 12 lines
{
    "return": [
        {
            "name": "virtio-net-pci.0", 
            "multicast": [
                {
                    "str": "01:80:c2:00:00:21"
                }, 
                {
                    "str": "00:00:00:00:00:00"
                }
            ]

Libvirt can live with this, even though it is more work, if you can't figure out how to make 'str' work. I don't know enough about the JSON generator to quickly dive in and figure out why you can't make an array of native types, though.

Quoted 10 lines
        }, 
        ....
    ]
}
======================== using strList
    '*unicast':        ['str'],
    '*multicast':      ['str']

Eric, is it expected format?

Quoted 8 lines
{
    "return": [
        {
            "name": "virtio-net-pci.0", 
            "multicast": [
                    "str": "01:80:c2:00:00:21",
                    "str": "00:00:00:00:00:00"
            ]

No. This is not valid JSON. ':' is only allowed inside {}. The point of an array is to either have an array of objects (as in [ {'name':value}, {'name':value} ]) or an array of native types (as in [ value, value ]). The ideal solution is for the output QMP to look like:

    "multicast": [
        "01:80:c2:00:00:21",
        "00:00:00:00:00:00"
    ]

which seems like it should work as '*multicast':['str'], if you can figure out how to make the generator play along.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#7 lore raw
In reply to Stefan Hajnoczi
Quoted 6 lines · Stefan Hajnoczi
Quoted 5 lines · Lluís Vilanova

Tried using a list of strings as an argument to a command, but the generated code references the 'strList' type, which does not exist.

Is a specialized version for "['str']" missing, or should I define my own type with a single field of 'str' type?

I would say just give that a shot. Stick:

typedef struct strList

{
    char *value;
    struct strList *next;
} strList;

in include/qapi/visitor.h and see what happens.

It's not immediately obvious to me why that wouldn't work, except maybe that sometimes the code generators will special case on CamelCase types, but that shouldn't be too hard to work around if it's an issue.

If that works, the same approach can probably be taken for all "native" qapi
types: str, bool, [u]int[(8|16|32|64)]

Quoted 7 lines

akong just hit this too.

I think it's a question for aliguori, luiz, or mdroth.

Stefan

#8 lore raw
Quoted 10 lines · mdroth
Quoted 6 lines · Stefan Hajnoczi
Quoted 5 lines · Lluís Vilanova

Tried using a list of strings as an argument to a command, but the generated code references the 'strList' type, which does not exist.

Is a specialized version for "['str']" missing, or should I define my own type with a single field of 'str' type?

I would say just give that a shot. Stick:

Quoted 7 lines

typedef struct strList

{
    char *value;
    struct strList *next;
} strList;

in include/qapi/visitor.h and see what happens.

Hi Michael,

| In file included from /home/devel/qemu/include/qapi/error.h:16,
|                  from /home/devel/qemu/include/qapi/visitor.h:16,
|                  from /home/devel/qemu/include/qapi/dealloc-visitor.h:17,
|                  from qapi-types.c:17:
| ./qapi-types.h:2158: error: expected specifier-qualifier-list before ‘strList’
| make: *** [qapi-types.o] Error 1

("->" means "is include to")
qapi-types.h -> include/qapi/error.h -> include/qapi/visitor.h

'strList' is used in qapi-types.h, we should not declare strList in include/qapi/visitor.h. I changed scripts/qapi-types.py to declare strList in qapi-types.h, but got a redefinition error.

| In file included from qga/qapi-generated/qga-qmp-commands.h:19,
|                  from qga/commands.c:15:
| qga/qapi-generated/qga-qapi-types.h:23: error: redefinition of ‘struct strList’
| qga/qapi-generated/qga-qapi-types.h:26: error: conflicting types for ‘strList’
| ./qapi-types.h:26: note: previous declaration of ‘strList’ was here
| make: *** [qga/commands.o] Error 1

I used an _ugly_ "#ifndef" to limit it only be defined in qapi-types.h. Michael, others do you have some suggestion?

Except strList, I also added a function: visit_type_strList(). Then it works. New output:

{'execute': 'query-mac-table'}

{
    "return": [
        {
            "name": "virtio-net-pci.0", 
            "uni_overflow": false, 
            "nobcast": false, 
            "promisc": false, 
            "multicast": [
                "01:00:5e:00:00:01", 
                "33:33:00:00:00:01", 
                "33:33:ff:12:34:57"
            ], 
            "nouni": false, 
            "nomulti": false, 
            "allmulti": false, 
            "multi_overflow": false, 
            "alluni": false
        }, 
        {
            "name": "virtio-net-pci.1", 
             ....
             ....
        }
    ]
}
# cat draft.patch
hmp.c +3 -3
index 8820224..1cf524e 100644
@@ -656,7 +656,7 @@ void hmp_info_tpm(Monitor *mon, const QDict *qdict)
 void hmp_info_mac_table(Monitor *mon, const QDict *qdict)
 {
     MacTableInfoList *table_list, *table_entry;
-    StringList *str_entry;
+    strList *str_entry;
 
     table_list = qmp_query_mac_table(NULL);
     table_entry = table_list;
@@ -698,7 +698,7 @@ void hmp_info_mac_table(Monitor *mon, const QDict *qdict)
             str_entry = table_entry->value->unicast;
             monitor_printf(mon, " \\ unicast:\n");
             while (str_entry) {
-                monitor_printf(mon, "    %s\n", str_entry->value->str);
+                monitor_printf(mon, "    %s\n", str_entry->value);
                 str_entry = str_entry->next;
             }
         }
@@ -707,7 +707,7 @@ void hmp_info_mac_table(Monitor *mon, const QDict *qdict)
             str_entry = table_entry->value->multicast;
             monitor_printf(mon, " \\ multicast:\n");
             while (str_entry) {
-                monitor_printf(mon, "    %s\n", str_entry->value->str);
+                monitor_printf(mon, "    %s\n", str_entry->value);
                 str_entry = str_entry->next;
             }
         }
hw/virtio-net.c +4 -4
index 8b15759..b72f163 100644
@@ -197,8 +197,8 @@ static MacTableInfo *virtio_net_query_mactable(NetClientState *nc)
 {
     VirtIONet *n = qemu_get_nic_opaque(nc);
     MacTableInfo *info;
-    StringList *str_list = NULL;
-    StringList *entry;
+    strList *str_list = NULL;
+    strList *entry;
     char str[12];
     int i;
 
@@ -234,7 +234,7 @@ static MacTableInfo *virtio_net_query_mactable(NetClientState *nc)
                 n->mac_table.macs[i * ETH_ALEN + 4],
                 n->mac_table.macs[i * ETH_ALEN + 5]);
         entry->value = g_malloc0(sizeof(String *));
-        entry->value->str = g_strdup(str);
+        entry->value = g_strdup(str);
         entry->next = str_list;
         str_list = entry;
     }
@@ -253,7 +253,7 @@ static MacTableInfo *virtio_net_query_mactable(NetClientState *nc)
                 n->mac_table.macs[i * ETH_ALEN + 4],
                 n->mac_table.macs[i * ETH_ALEN + 5]);
         entry->value = g_malloc0(sizeof(String *));
-        entry->value->str = g_strdup(str);
+        entry->value = g_strdup(str);
         entry->next = str_list;
         str_list = entry;
     }
include/qapi/visitor.h +1 -0
index 1fef18c..2e36edb 100644
@@ -50,6 +50,7 @@ void visit_type_int64(Visitor *v, int64_t *obj, const char *name, Error **errp);
 void visit_type_size(Visitor *v, uint64_t *obj, const char *name, Error **errp);
 void visit_type_bool(Visitor *v, bool *obj, const char *name, Error **errp);
 void visit_type_str(Visitor *v, char **obj, const char *name, Error **errp);
+void visit_type_strList(Visitor *m, strList ** obj, const char *name, Error **errp);
 void visit_type_number(Visitor *v, double *obj, const char *name, Error **errp);
 
 #endif
qapi-schema.json +2 -2
index febf62a..5c8e77e 100644
@@ -3537,8 +3537,8 @@
     '*nobcast':        'bool',
     '*multi_overflow': 'bool',
     '*uni_overflow':   'bool',
-    '*unicast':        ['String'],
-    '*multicast':      ['String'] }}
+    '*unicast':        ['str'],
+    '*multicast':      ['str'] }}
 
 ##
 # @query-mac-table:
qapi/qapi-visit-core.c +22 -0
index 401ee6e..aefe227 100644
@@ -257,6 +257,28 @@ void visit_type_str(Visitor *v, char **obj, const char *name, Error **errp)
     }
 }
 
+void visit_type_strList(Visitor *m, strList ** obj, const char *name, Error **errp)
+{
+    GenericList *i, **prev = (GenericList **)obj;
+    Error *err = NULL;
+
+    if (!error_is_set(errp)) {
+        visit_start_list(m, name, &err);
+        if (!err) {
+            for (; (i = visit_next_list(m, prev, &err)) != NULL; prev = &i) {
+                strList *native_i = (strList *)i;
+                visit_type_str(m, &native_i->value, NULL, &err);
+            }
+            error_propagate(errp, err);
+            err = NULL;
+
+            /* Always call end_list if start_list succeeded.  */
+            visit_end_list(m, &err);
+        }
+        error_propagate(errp, err);
+    }
+}
+
 void visit_type_number(Visitor *v, double *obj, const char *name, Error **errp)
 {
     if (!error_is_set(errp)) {
scripts/qapi-types.py +8 -0
index 9e19920..f2ca373 100644
@@ -276,6 +276,14 @@ fdecl.write(mcgen('''
 #include <stdbool.h>
 #include <stdint.h>
 
+#ifndef QGA_QAPI_TYPES_H
+typedef struct strList
+{
+  char *value;
+  struct strList *next;
+} strList;
+#endif
+
 ''',
                   guard=guardname(h_file)))
 
#9 lore raw

RFC PATCH 1/1 qapi: introduce strList and visit_type_strList() (was Re: [qapi] Cannot use list of strings)

Quoted 44 lines · Amos Kong
Quoted 10 lines · mdroth
Quoted 6 lines · Stefan Hajnoczi
Quoted 5 lines · Lluís Vilanova

Tried using a list of strings as an argument to a command, but the generated code references the 'strList' type, which does not exist.

Is a specialized version for "['str']" missing, or should I define my own type with a single field of 'str' type?

I would say just give that a shot. Stick:

Quoted 7 lines

typedef struct strList

{
    char *value;
    struct strList *next;
} strList;

in include/qapi/visitor.h and see what happens.

Hi Michael,

| In file included from /home/devel/qemu/include/qapi/error.h:16,
|                  from /home/devel/qemu/include/qapi/visitor.h:16,
|                  from /home/devel/qemu/include/qapi/dealloc-visitor.h:17,
|                  from qapi-types.c:17:
| ./qapi-types.h:2158: error: expected specifier-qualifier-list before ‘strList’
| make: *** [qapi-types.o] Error 1

("->" means "is include to")
qapi-types.h -> include/qapi/error.h -> include/qapi/visitor.h

'strList' is used in qapi-types.h, we should not declare strList in include/qapi/visitor.h. I changed scripts/qapi-types.py to declare strList in qapi-types.h, but got a redefinition error.

| In file included from qga/qapi-generated/qga-qmp-commands.h:19,
|                  from qga/commands.c:15:
| qga/qapi-generated/qga-qapi-types.h:23: error: redefinition of ‘struct strList’
| qga/qapi-generated/qga-qapi-types.h:26: error: conflicting types for ‘strList’
| ./qapi-types.h:26: note: previous declaration of ‘strList’ was here
| make: *** [qga/commands.o] Error 1
Quoted 5 lines

I used an _ugly_ "#ifndef" to limit it only be defined in qapi-types.h. Michael, others do you have some suggestion?

From ecbd69a8ad4b5e600ac56b1cda4c4c04e0175742 Mon Sep 17 00:00:00 2001

From: Amos Kong <[email protected]>
Date: Thu, 18 Apr 2013 10:56:24 +0800
Subject: [RFC PATCH] qapi: introduce strList and visit_type_strList()

Currently we can only use ['String'] to add string to a list, it contains some additional JSON structure.

    "multicast": [
        {
            "str": "01:80:c2:00:00:21"
        },
        {
            "str": "00:00:00:00:00:00"
        }
    ]

This patch introdued strList, we can use ['str']

    "multicast": [
        "01:00:5e:00:00:01",
        "33:33:ff:12:34:57"
    ]

---
I used an _ugly_ "#ifndef" to limit it only be defined in qapi-types.h. Michael, others do you have some suggestion?

---
 include/qapi/visitor.h |    1 +
 qapi/qapi-visit-core.c |   22 ++++++++++++++++++++++
 scripts/qapi-types.py  |    8 ++++++++
 3 files changed, 31 insertions(+), 0 deletions(-)
include/qapi/visitor.h +1 -0
index 1fef18c..2e36edb 100644
@@ -50,6 +50,7 @@ void visit_type_int64(Visitor *v, int64_t *obj, const char *name, Error **errp);
 void visit_type_size(Visitor *v, uint64_t *obj, const char *name, Error **errp);
 void visit_type_bool(Visitor *v, bool *obj, const char *name, Error **errp);
 void visit_type_str(Visitor *v, char **obj, const char *name, Error **errp);
+void visit_type_strList(Visitor *m, strList ** obj, const char *name, Error **errp);
 void visit_type_number(Visitor *v, double *obj, const char *name, Error **errp);
 
 #endif
qapi/qapi-visit-core.c +22 -0
index 401ee6e..aefe227 100644
@@ -257,6 +257,28 @@ void visit_type_str(Visitor *v, char **obj, const char *name, Error **errp)
     }
 }
 
+void visit_type_strList(Visitor *m, strList ** obj, const char *name, Error **errp)
+{
+    GenericList *i, **prev = (GenericList **)obj;
+    Error *err = NULL;
+
+    if (!error_is_set(errp)) {
+        visit_start_list(m, name, &err);
+        if (!err) {
+            for (; (i = visit_next_list(m, prev, &err)) != NULL; prev = &i) {
+                strList *native_i = (strList *)i;
+                visit_type_str(m, &native_i->value, NULL, &err);
+            }
+            error_propagate(errp, err);
+            err = NULL;
+
+            /* Always call end_list if start_list succeeded.  */
+            visit_end_list(m, &err);
+        }
+        error_propagate(errp, err);
+    }
+}
+
 void visit_type_number(Visitor *v, double *obj, const char *name, Error **errp)
 {
     if (!error_is_set(errp)) {
scripts/qapi-types.py +8 -0
index 9e19920..f2ca373 100644
@@ -276,6 +276,14 @@ fdecl.write(mcgen('''
 #include <stdbool.h>
 #include <stdint.h>
 
+#ifndef QGA_QAPI_TYPES_H
+typedef struct strList
+{
+  char *value;
+  struct strList *next;
+} strList;
+#endif
+
 ''',
                   guard=guardname(h_file)))
 
Signature · 1 line
1.7.1
#10 lore raw

PATCH v2 1/1 qapi: add struct strList and visit_type_strList()

Currently we can only use ['String'] to add string to a list, it contains some additional JSON structure.

    "multicast": [
        {
            "str": "01:80:c2:00:00:21"
        },
        {
            "str": "00:00:00:00:00:00"
        }
    ]

This patch adds struct strList and visit function, we can use ['str'] in schema file.

    "multicast": [
        "01:00:5e:00:00:01",
        "33:33:ff:12:34:57"
    ]

V2: remove ugly #ifndef, add forward declaration in qapi-types.h,
and define the struct in include/qapi/visitor.h (Paolo)

---
 include/qapi/visitor.h |    7 +++++++
 qapi/qapi-visit-core.c |   23 +++++++++++++++++++++++
 scripts/qapi-types.py  |    1 +
 scripts/qapi.py        |    4 ++--
 4 files changed, 33 insertions(+), 2 deletions(-)
include/qapi/visitor.h +7 -0
index 1fef18c..743ff92 100644
@@ -22,6 +22,11 @@ typedef struct GenericList
     struct GenericList *next;
 } GenericList;
 
+typedef struct strList {
+    char *value;
+    struct strList *next;
+} strList;
+
 typedef struct Visitor Visitor;
 
 void visit_start_handle(Visitor *v, void **obj, const char *kind,
@@ -50,6 +55,8 @@ void visit_type_int64(Visitor *v, int64_t *obj, const char *name, Error **errp);
 void visit_type_size(Visitor *v, uint64_t *obj, const char *name, Error **errp);
 void visit_type_bool(Visitor *v, bool *obj, const char *name, Error **errp);
 void visit_type_str(Visitor *v, char **obj, const char *name, Error **errp);
+void visit_type_strList(Visitor *m, strList ** obj, const char *name,
+                        Error **errp);
 void visit_type_number(Visitor *v, double *obj, const char *name, Error **errp);
 
 #endif
qapi/qapi-visit-core.c +23 -0
index 401ee6e..dc54cc8 100644
@@ -257,6 +257,29 @@ void visit_type_str(Visitor *v, char **obj, const char *name, Error **errp)
     }
 }
 
+void visit_type_strList(Visitor *m, strList ** obj, const char *name,
+                        Error **errp)
+{
+    GenericList *i, **prev = (GenericList **)obj;
+    Error *err = NULL;
+
+    if (!error_is_set(errp)) {
+        visit_start_list(m, name, &err);
+        if (!err) {
+            for (; (i = visit_next_list(m, prev, &err)) != NULL; prev = &i) {
+                strList *native_i = (strList *)i;
+                visit_type_str(m, &native_i->value, NULL, &err);
+            }
+            error_propagate(errp, err);
+            err = NULL;
+
+            /* Always call end_list if start_list succeeded.  */
+            visit_end_list(m, &err);
+        }
+        error_propagate(errp, err);
+    }
+}
+
 void visit_type_number(Visitor *v, double *obj, const char *name, Error **errp)
 {
     if (!error_is_set(errp)) {
scripts/qapi-types.py +1 -0
index 9e19920..e449a14 100644
@@ -276,6 +276,7 @@ fdecl.write(mcgen('''
 #include <stdbool.h>
 #include <stdint.h>
 
+struct strList;
 ''',
                   guard=guardname(h_file)))
 
scripts/qapi.py +2 -2
index afc5f32..14f9f4d 100644
@@ -166,11 +166,11 @@ def c_fun(name, protect=True):
     return c_var(name, protect).replace('.', '_')
 
 def c_list_type(name):
-    return '%sList' % name
+    return 'struct %sList' % name
 
 def type_name(name):
     if type(name) == list:
-        return c_list_type(name[0])
+        return '%sList' % name[0]
     return name
 
 enum_types = []
Signature · 1 line
1.7.1
Reviewed-by on v2 1/1
#11 lore raw

Re: [Qemu-devel] [PATCH v2] qapi: add struct strList and visit_type_strList()

Il 24/04/2013 18:08, Amos Kong ha scritto:

Quoted 18 lines
scripts/qapi-types.py +1 -0
index 9e19920..e449a14 100644
@@ -276,6 +276,7 @@ fdecl.write(mcgen('''
 #include <stdbool.h>
 #include <stdint.h>
 
+struct strList;
 ''',
                   guard=guardname(h_file)))
 
scripts/qapi.py +0 -0
index afc5f32..14f9f4d 100644
@@ -166,11 +166,11 @@ def c_fun(name, protect=True):
     return c_var(name, protect).replace('.', '_')

scripts/qapi.py def c_fun(name, protect=True): · line 174

Quoted 10 lines
scripts/qapi.py +2 -2
 
 def c_list_type(name):
-    return '%sList' % name
+    return 'struct %sList' % name
 
 def type_name(name):
     if type(name) == list:
-        return c_list_type(name[0])
+        return '%sList' % name[0]
     return name

This second change is needed to generate the correct function names, right? If so,

Paolo

#12 lore raw

Re: [Qemu-devel] [PATCH v2] qapi: add struct strList and visit_type_strList()

Quoted 24 lines · Paolo Bonzini

Il 24/04/2013 18:08, Amos Kong ha scritto:

Quoted 23 lines
scripts/qapi-types.py +1 -0
index 9e19920..e449a14 100644
@@ -276,6 +276,7 @@ fdecl.write(mcgen('''
 #include <stdbool.h>
 #include <stdint.h>
 
+struct strList;
 ''',
                   guard=guardname(h_file)))
 
scripts/qapi.py +1 -1
index afc5f32..14f9f4d 100644
@@ -166,11 +166,11 @@ def c_fun(name, protect=True):
     return c_var(name, protect).replace('.', '_')
 
 def c_list_type(name):
-    return '%sList' % name
+    return 'struct %sList' % name
 
Quoted 8 lines

scripts/qapi.py def c_fun(name, protect=True): · line 174

Quoted 5 lines
scripts/qapi.py +1 -1
 def type_name(name):
     if type(name) == list:
-        return c_list_type(name[0])
+        return '%sList' % name[0]
     return name

This second change is needed to generate the correct function names, right? If so,

Yes, otherwise, the visit function name will be "visit_type_struct nameList".

Quoted 3 lines

Paolo

Thanks

Signature · 1 line
			Amos.
#13 lore raw

Re: [Qemu-devel] [PATCH v2] qapi: add struct strList and visit_type_strList()

In reply to Amos Kong
Quoted 19 lines · Amos Kong

Currently we can only use ['String'] to add string to a list, it contains some additional JSON structure.

    "multicast": [
        {
            "str": "01:80:c2:00:00:21"
        },
        {
            "str": "00:00:00:00:00:00"
        }
    ]

This patch adds struct strList and visit function, we can use ['str'] in schema file.

    "multicast": [
        "01:00:5e:00:00:01",
        "33:33:ff:12:34:57"
    ]
Quoted 4 lines

V2: remove ugly #ifndef, add forward declaration in qapi-types.h,
and define the struct in include/qapi/visitor.h (Paolo)

Sorry for the delay in getting back to you, I started to respond last week and ended up hacking something up that take a different approach.

Namely: we don't hardcode visit_type_strList(), but instead teach the qapi code generators about native types we currently allow in schemas ('str', 'int', 'number', 'bool') and have them handle code generation for these like they would for any user-defined type.

This approach works too, but I think this solution is more complete. I'm still working out some bits with the unit tests but I've pushed my WIP here for reference:

https://github.com/mdroth/qemu/commits/qapi-native-lists

Please give that a shot.

Quoted 99 lines
---
 include/qapi/visitor.h |    7 +++++++
 qapi/qapi-visit-core.c |   23 +++++++++++++++++++++++
 scripts/qapi-types.py  |    1 +
 scripts/qapi.py        |    4 ++--
 4 files changed, 33 insertions(+), 2 deletions(-)
include/qapi/visitor.h +7 -0
index 1fef18c..743ff92 100644
@@ -22,6 +22,11 @@ typedef struct GenericList
     struct GenericList *next;
 } GenericList;
 
+typedef struct strList {
+    char *value;
+    struct strList *next;
+} strList;
+
 typedef struct Visitor Visitor;
 
 void visit_start_handle(Visitor *v, void **obj, const char *kind,
@@ -50,6 +55,8 @@ void visit_type_int64(Visitor *v, int64_t *obj, const char *name, Error **errp);
 void visit_type_size(Visitor *v, uint64_t *obj, const char *name, Error **errp);
 void visit_type_bool(Visitor *v, bool *obj, const char *name, Error **errp);
 void visit_type_str(Visitor *v, char **obj, const char *name, Error **errp);
+void visit_type_strList(Visitor *m, strList ** obj, const char *name,
+                        Error **errp);
 void visit_type_number(Visitor *v, double *obj, const char *name, Error **errp);
 
 #endif
qapi/qapi-visit-core.c +23 -0
index 401ee6e..dc54cc8 100644
@@ -257,6 +257,29 @@ void visit_type_str(Visitor *v, char **obj, const char *name, Error **errp)
     }
 }
 
+void visit_type_strList(Visitor *m, strList ** obj, const char *name,
+                        Error **errp)
+{
+    GenericList *i, **prev = (GenericList **)obj;
+    Error *err = NULL;
+
+    if (!error_is_set(errp)) {
+        visit_start_list(m, name, &err);
+        if (!err) {
+            for (; (i = visit_next_list(m, prev, &err)) != NULL; prev = &i) {
+                strList *native_i = (strList *)i;
+                visit_type_str(m, &native_i->value, NULL, &err);
+            }
+            error_propagate(errp, err);
+            err = NULL;
+
+            /* Always call end_list if start_list succeeded.  */
+            visit_end_list(m, &err);
+        }
+        error_propagate(errp, err);
+    }
+}
+
 void visit_type_number(Visitor *v, double *obj, const char *name, Error **errp)
 {
     if (!error_is_set(errp)) {
scripts/qapi-types.py +1 -0
index 9e19920..e449a14 100644
@@ -276,6 +276,7 @@ fdecl.write(mcgen('''
 #include <stdbool.h>
 #include <stdint.h>
 
+struct strList;
 ''',
                   guard=guardname(h_file)))
 
scripts/qapi.py +2 -2
index afc5f32..14f9f4d 100644
@@ -166,11 +166,11 @@ def c_fun(name, protect=True):
     return c_var(name, protect).replace('.', '_')
 
 def c_list_type(name):
-    return '%sList' % name
+    return 'struct %sList' % name
 
 def type_name(name):
     if type(name) == list:
-        return c_list_type(name[0])
+        return '%sList' % name[0]
     return name
 
 enum_types = []
Signature · 1 line
1.7.1
#14 lore raw

Re: [Qemu-devel] [PATCH v2] qapi: add struct strList and visit_type_strList()

Hi Michael,

Quoted 25 lines · mdroth
Quoted 19 lines · Amos Kong

Currently we can only use ['String'] to add string to a list, it contains some additional JSON structure.

    "multicast": [
        {
            "str": "01:80:c2:00:00:21"
        },
        {
            "str": "00:00:00:00:00:00"
        }
    ]

This patch adds struct strList and visit function, we can use ['str'] in schema file.

    "multicast": [
        "01:00:5e:00:00:01",
        "33:33:ff:12:34:57"
    ]
Quoted 4 lines

V2: remove ugly #ifndef, add forward declaration in qapi-types.h,
and define the struct in include/qapi/visitor.h (Paolo)

Quoted 7 lines

Sorry for the delay in getting back to you, I started to respond last week and ended up hacking something up that take a different approach.

Namely: we don't hardcode visit_type_strList(), but instead teach the qapi code generators about native types we currently allow in schemas ('str', 'int', 'number', 'bool') and have them handle code generation for these like they would for any user-defined type.

It's better.

Quoted 7 lines

This approach works too, but I think this solution is more complete. I'm still working out some bits with the unit tests but I've pushed my WIP here for reference:

https://github.com/mdroth/qemu/commits/qapi-native-lists

Please give that a shot.

Your patchset looks good to me, I only added some trivial comments on github.

Signature · 1 line
			Amos.