From Hadrien Kaplan via https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=8357
authorEvan Huus <eapache@gmail.com>
Wed, 20 Feb 2013 23:40:24 +0000 (23:40 -0000)
committerEvan Huus <eapache@gmail.com>
Wed, 20 Feb 2013 23:40:24 +0000 (23:40 -0000)
Fix the Lua tostring() method on FieldInfo objects such that it always returns
a string, although the string might be '(unknown)', '(none)', or '(n/a)'. This
is more conformant to Lua's API style.

Also create a new 'FieldInfo.display' accessor table member, which Lua
scripts can use instead of tostring() to get what the GUI displays.

From me:
Misc indentation fixes, remove redundant 'return' statement.

svn path=/trunk/; revision=47783

epan/wslua/wslua_field.c

index 3981232fa28ab8a213034452fd51ebf827837383..baf9a6bc0a07884409ef5631887270f42d87dee5 100644 (file)
@@ -163,19 +163,52 @@ WSLUA_METAMETHOD FieldInfo__call(lua_State* L) {
 }
 
 WSLUA_METAMETHOD FieldInfo__tostring(lua_State* L) {
-       /* The string representation of the field */
-       FieldInfo fi = checkFieldInfo(L,1);
-       if (fi) {
-               if (fi->value.ftype->val_to_string_repr) {
-                       gchar* repr = fvalue_to_string_repr(&fi->value,FTREPR_DISPLAY,NULL);
-                       if (repr)
-                               lua_pushstring(L,repr);
-                       else
-                               luaL_error(L,"field cannot be represented as string because it may contain invalid characters");
-               } else
-                       luaL_error(L,"field has no string representation");
-       }
-       return 1;
+    /* The string representation of the field */
+    FieldInfo fi = checkFieldInfo(L,1);
+    if (fi) {
+        if (fi->value.ftype->val_to_string_repr) {
+            gchar* repr = fvalue_to_string_repr(&fi->value,FTREPR_DISPLAY,NULL);
+            if (repr)
+                lua_pushstring(L,repr);
+            else
+                lua_pushstring(L,"(unknown)");
+        } else if (fi->hfinfo->type == FT_NONE) {
+            lua_pushstring(L, "(none)");
+        } else {
+            lua_pushstring(L,"(n/a)");
+        }
+    }
+
+    return luaL_error(L,"Missing FieldInfo object");
+}
+
+static int FieldInfo_display(lua_State* L) {
+    /* The display string of this field as seen in GUI */
+    FieldInfo fi = checkFieldInfo(L,1);
+    gchar         label_str[ITEM_LABEL_LENGTH+1];
+    gchar        *label_ptr;
+    gchar        *value_ptr;
+
+    if (!fi) return 0;
+
+    if (!fi->rep) {
+        label_ptr = label_str;
+        proto_item_fill_label(fi, label_str);
+    } else 
+        label_ptr = fi->rep->representation;
+
+    if (!label_ptr) return 0;
+
+    value_ptr = strstr(label_ptr, ": ");
+    if (!value_ptr) {
+        /* just use whatever's there */
+        lua_pushstring(L, label_ptr);
+    } else {
+        value_ptr += 2;  /* get past the ': ' */
+        lua_pushstring(L, value_ptr);
+    }
+
+    return 1;
 }
 
 static int FieldInfo_get_range(lua_State* L) {
@@ -222,6 +255,8 @@ static const luaL_Reg FieldInfo_get[] = {
     {"len", FieldInfo__len},
        /* WSLUA_ATTRIBUTE FieldInfo_offset RO The offset of this field */
     {"offset", FieldInfo__unm},
+    /* WSLUA_ATTRIBUTE FieldInfo_display RO The string display of this field as seen in GUI */
+    {"display", FieldInfo_display},
     { NULL, NULL }
 };