chiark / gitweb /
get resolver destruction right
[chiark-tcl.git] / base / enum.c
index a4e9de7ff9ad0f0d6d686f97f1c049023db2ccec..e3fa041b6d621875909bb06c84a0e23011555785 100644 (file)
@@ -8,6 +8,7 @@
 
 static void enum_nt_dup(Tcl_Obj *src, Tcl_Obj *dup) {
   dup->internalRep= src->internalRep;
+  dup->typePtr= src->typePtr;
 }
 
 static void enum_nt_ustr(Tcl_Obj *o) {
@@ -28,6 +29,35 @@ Tcl_ObjType enum1_nearlytype = {
   0, enum_nt_dup, enum_nt_ustr, enum_nt_sfa
 };
 
+static void report_bad(Tcl_Interp *ip, const char *what, const char *supplied,
+                      const void *first, size_t each,
+                      int (*isvalid)(const void *entry),
+                      void (*appres)(Tcl_Interp *ip, const void *entry)) {
+  int count, i;
+  const Byte *entry;
+
+  for (entry=first; isvalid(entry); entry+=each);
+  count= (entry - (const Byte*)first) / each;
+
+  Tcl_ResetResult(ip);
+  Tcl_AppendResult(ip, "bad ",what," \"",supplied,"\": must be",(char*)0);
+
+  for (i=0, entry=first; i<count; i++, entry+=each) {
+    Tcl_AppendResult(ip,
+                    (char*)(i==0 ? " " :
+                            i+1==count ? ", or " :
+                            ", "),
+                    (char*)0);
+    appres(ip,entry);
+  }
+}
+
+static const char *enum_str(const void *p) { return *(const char*const*)p; }
+static int isvalid_enum(const void *p) { return !!enum_str(p); }
+static void appres_enum(Tcl_Interp *ip, const void *p) {
+  Tcl_AppendResult(ip, enum_str(p), (char*)0);
+}
+
 const void *enum_lookup_cached_func(Tcl_Interp *ip, Tcl_Obj *o,
                                    const void *firstentry, size_t entrysize,
                                    const char *what) {
@@ -51,15 +81,18 @@ const void *enum_lookup_cached_func(Tcl_Interp *ip, Tcl_Obj *o,
     return ep;
   }
 
-  Tcl_ResetResult(ip);
-  Tcl_AppendResult(ip, "invalid ",what," - must be one of:",(char*)0);
-  for (ep= firstentry;
-       (found= *(const char*const*)ep);
-       ep += entrysize)
-    Tcl_AppendResult(ip, " ",found,(char*)0);
+  report_bad(ip,what,supplied, firstentry,entrysize, isvalid_enum,appres_enum);
   return 0;
 }
 
+static int isvalid_enum1(const void *p) { return !!*(const char*)p; }
+static void appres_enum1(Tcl_Interp *ip, const void *p) {
+  char buf[2];
+  buf[0]= *(const char*)p;
+  buf[1]= 0;
+  Tcl_AppendResult(ip, buf, (char*)0);
+}
+
 int enum1_lookup_cached_func(Tcl_Interp *ip, Tcl_Obj *o,
                             const char *opts, const char *what) {
   const char *supplied, *fp;
@@ -71,9 +104,7 @@ int enum1_lookup_cached_func(Tcl_Interp *ip, Tcl_Obj *o,
     
     if (!(strlen(supplied) == 1 &&
          (fp= strchr(opts, supplied[0])))) {
-      Tcl_ResetResult(ip);
-      Tcl_AppendResult(ip, "invalid ",what,
-                      " - must be one character from: ", opts);
+      report_bad(ip,what,supplied, opts,1, isvalid_enum1,appres_enum1);
       return -1;
     }