chiark / gitweb /
Better integration with tclreadline. Less recompilation of serpent.[ch].
authorian <ian>
Fri, 6 Sep 2002 19:25:57 +0000 (19:25 +0000)
committerian <ian>
Fri, 6 Sep 2002 19:25:57 +0000 (19:25 +0000)
base/chiark-tcl.h
base/enum.c
base/hook.c
base/tables-examples.tct
base/tcmdifgen
base/troglodyte-Makefile
hbytes/hbytes.c
hbytes/hbytes.h
hbytes/hook.c

index 15abca2544407f8e5c7194888b7ec27787d36e98..86b2dba6dec3c3b550b6252258f45eef4c6f69dc 100644 (file)
@@ -78,7 +78,7 @@ void hbytes_empty(HBytes_Value *returns);
 void hbytes_sentinel(HBytes_Value *returns);
 void hbytes_array(HBytes_Value *returns, const Byte *array, int l);
 Byte *hbytes_arrayspace(HBytes_Value *returns, int l);
-void hbytes_free(HBytes_Value *frees);
+void hbytes_free(const HBytes_Value *frees);
   /* _empty, _sentinel and _array do not free or read the old value;
    * _free it first if needed.  _free leaves it garbage, so you
    * have to call _empty to reuse it.  _arrayspace doesn't fill
index a4e9de7ff9ad0f0d6d686f97f1c049023db2ccec..8041cd378a5b05f1c486f2f4049700299f91c631 100644 (file)
@@ -28,6 +28,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 +80,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 +103,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;
     }
     
index 68df783561431b14b411c22fa274111b5e16fa99..b6c0c9e2128e54ad06b137cb3f1d8928a87b2329 100644 (file)
@@ -147,6 +147,25 @@ int do_hbytes_length(ClientData cd, Tcl_Interp *ip,
   return TCL_OK;
 }
 
+int do_hbytes_random(ClientData cd, Tcl_Interp *ip,
+                    int length, HBytes_Value *result) {
+  Byte *space;
+  int rc;
+  
+  space= hbytes_arrayspace(result, length);
+  rc= get_urandom(ip, space, length);
+  if (rc) { hbytes_free(result); return rc; }
+  return TCL_OK;
+}  
+  
+int do_hbytes_zeroes(ClientData cd, Tcl_Interp *ip,
+                    int length, HBytes_Value *result) {
+  Byte *space;
+  space= hbytes_arrayspace(result, length);
+  memset(space,0,length);
+  return TCL_OK;
+}
+
 int do__hbytes(ClientData cd, Tcl_Interp *ip,
               const HBytes_SubCommand *subcmd,
               int objc, Tcl_Obj *const *objv) {
index 5115813ad127f13c2ea6d43ecad882f81d800049..b40198f0d90380843078318c843cee7b36cced2f 100644 (file)
@@ -68,3 +68,13 @@ Table hbytes HBytes_SubCommand
                key     obj
                ?maclen obj
                =>      hb
+       zeroes
+               length  int
+               =>      hb
+       random
+               length  int
+               =>      hb
+
+#Table udp UDP_SubCommand
+#      create
+#
index 514dc019a113f50bc27db466e4b76bcc2b51830f..90f0818588b8cda4ab3b7e8ed0b861d88b3fd7f1 100755 (executable)
@@ -162,6 +162,10 @@ foreach $c_table (sort keys %tables) {
        $any_mand= 0;
        $any_optl= 0;
        $any_eerr= 0;
+       $any_eargc= 0;
+       $pa_hint= '';
+       $pa_hint .= "$c_table " if length $c_table;
+       $pa_hint.= $c_entry;
        foreach $arg (@{ $r_entry->{A} }) {
            $n= $arg->{N};
            $t= $arg->{T};
@@ -169,18 +173,19 @@ foreach $c_table (sort keys %tables) {
            push @do_al, make_decl($n, $t, $arg->{A});
            $pa_vars .= make_decl_init("a_$n", $t, $a, \$pa_init);
            if ($arg->{O}) {
+               $pa_hint .= " ?$n?";
                if ($any_mand) {
-                   $pa_argc .= "  if (objc < $any_mand) {".
-                       " e=\"too few args\"; goto e_err; }\n";
-                   $pa_body .= "  objc -= $any_mand;\n";
                    $any_mand= 0;
                    $any_eerr= 1;
                }
                $pa_body .= "  if (!objc--) goto end_optional;\n";
                $any_optl= 1;
            } else {
-               die if $any_optl;
+               $pa_hint .= " $n";
+               $pa_body .= "  if (!objc--) goto wrong_count_args;\n";
                $any_mand++;
+               $any_eargc= 1;
+               die if $any_optl;
            }
            $paarg= "&a_$n";
            $pafin= '';
@@ -202,18 +207,15 @@ foreach $c_table (sort keys %tables) {
            push @do_aa, "a_$n";
        }
        if (exists $r_entry->{V}) {
-           if ($any_mand) {
-               $pa_body .= "  objc -= $any_mand;\n";
-           }
+           $pa_hint .= " ...";
            $va= $r_entry->{V};
            push @do_al, subst_in_decl("${va}c", 'int @');
            push @do_al, subst_in_decl("${va}v", 'Tcl_Obj *const *@');
            push @do_aa, "objc+1", "objv-1";
        } else {
            if (!$any_optl) {
-               $pa_argc .= "  if (objc != $any_mand) {".
-                   " e=\"wrong number of args\"; goto e_err; }\n";
-               $any_eerr= 1;
+               $pa_body .= "  if (objc) goto wrong_count_args;\n";
+               $any_eargc= 1;
            }
        }
        if ($any_optl) {
@@ -236,6 +238,12 @@ foreach $c_table (sort keys %tables) {
        $pa_rslt .= "rc_err:\n";
        
        $pa_fini .= "  return rc;\n";
+       if ($any_eargc) {
+           $pa_fini .= "\nwrong_count_args:\n";
+           $pa_fini .= "  e=\"wrong # args: should be \\\"$pa_hint\\\"\";\n";
+           $pa_fini .= "  goto e_err;";
+           $any_eerr= 1;
+       }
        if ($any_eerr) {
            $pa_vars .= "  const char *e;\n";
            $pa_fini .= "\n";
index c557d80992685b09610fa276b4bf78d8fd2a605e..33cace8d755930e8ef15457743b2354c390549b9 100644 (file)
@@ -37,5 +37,8 @@ hbytes.so:    $(OBJS)
 %.o:           %.c $(HDRS)
                $(CC) $(CFLAGS) $(CPPFLAGS) -o $@ -c $<
 
+serpent.o:     serpent.c serpent.h serpentsboxes.h
+               $(CC) $(CFLAGS) $(CPPFLAGS) -O3 -o $@ -c $<
+
 clean:
                rm -f $(OBJS) $(TARGETS) *~ ./#*#
index 41fc12a943de4f78359c7bf68ec1b92fbeb78a57..6bb86c31c9524b32d3e1a066dbfbb6aa881769c8 100644 (file)
@@ -52,7 +52,7 @@ void hbytes_array(HBytes_Value *returns, const Byte *array, int l) {
 
 /* destructor */
 
-void hbytes_free(HBytes_Value *frees) {
+void hbytes_free(const HBytes_Value *frees) {
   if (HBYTES_ISCOMPLEX(frees)) {
     HBytes_ComplexValue *cx= COMPLEX(frees);
     TFREE(cx->dstart - cx->prespace);
index 15abca2544407f8e5c7194888b7ec27787d36e98..86b2dba6dec3c3b550b6252258f45eef4c6f69dc 100644 (file)
@@ -78,7 +78,7 @@ void hbytes_empty(HBytes_Value *returns);
 void hbytes_sentinel(HBytes_Value *returns);
 void hbytes_array(HBytes_Value *returns, const Byte *array, int l);
 Byte *hbytes_arrayspace(HBytes_Value *returns, int l);
-void hbytes_free(HBytes_Value *frees);
+void hbytes_free(const HBytes_Value *frees);
   /* _empty, _sentinel and _array do not free or read the old value;
    * _free it first if needed.  _free leaves it garbage, so you
    * have to call _empty to reuse it.  _arrayspace doesn't fill
index 68df783561431b14b411c22fa274111b5e16fa99..b6c0c9e2128e54ad06b137cb3f1d8928a87b2329 100644 (file)
@@ -147,6 +147,25 @@ int do_hbytes_length(ClientData cd, Tcl_Interp *ip,
   return TCL_OK;
 }
 
+int do_hbytes_random(ClientData cd, Tcl_Interp *ip,
+                    int length, HBytes_Value *result) {
+  Byte *space;
+  int rc;
+  
+  space= hbytes_arrayspace(result, length);
+  rc= get_urandom(ip, space, length);
+  if (rc) { hbytes_free(result); return rc; }
+  return TCL_OK;
+}  
+  
+int do_hbytes_zeroes(ClientData cd, Tcl_Interp *ip,
+                    int length, HBytes_Value *result) {
+  Byte *space;
+  space= hbytes_arrayspace(result, length);
+  memset(space,0,length);
+  return TCL_OK;
+}
+
 int do__hbytes(ClientData cd, Tcl_Interp *ip,
               const HBytes_SubCommand *subcmd,
               int objc, Tcl_Obj *const *objv) {