chiark / gitweb /
new some_lookup
authorian <ian>
Fri, 3 Jun 2005 23:43:33 +0000 (23:43 +0000)
committerian <ian>
Fri, 3 Jun 2005 23:43:33 +0000 (23:43 +0000)
hostside/client.c
hostside/commands.c
hostside/hostside.h
hostside/parse-proto-spec
hostside/skelproto-pic.h

index 22e1c0c1432f8e723f262f01274557f7f6b9a0f3..a3f409a23f67f1f89eb98246519ee5dc84cc280b 100644 (file)
@@ -81,23 +81,25 @@ int thiswordstrcmp(ParseState *ps, const char *b) {
   return lstrstrcmp(ps->thisword, ps->lthisword, b);
 }
 
-const CmdInfo *ps_lookup(ParseState *ps, const CmdInfo *inf) {
+const void *any_lookup(ParseState *ps, const void *inf, size_t sz) {
+  const char *tname;
+  
   for (;
-       inf->name;
-       inf++)
-    if (!thiswordstrcmp(ps,inf->name))
+       (tname= *(const char *const*)inf);
+       inf= (const char*)inf + sz)
+    if (!thiswordstrcmp(ps,tname))
       return inf;
   return 0;
 }
 
-const CmdInfo *ps_needword_lookup(ParseState *ps, const CmdInfo *infs) {
+const void *any_needword_lookup(ParseState *ps, const void *infs, size_t sz) {
   if (!ps_needword(ps)) return 0;
-  return ps_lookup(ps,infs);
+  return any_lookup(ps,infs,sz);
 }
 
 void ps_callword(ParseState *ps, const CmdInfo *infs, const char *what) {
   const CmdInfo *ci;
-  ci= ps_needword_lookup(ps,infs);
+  ci= some_needword_lookup(ps,infs);
   if (!ci) { badcmd(ps,"unknown %s",what); return; }
   ci->fn(ps,ci);
 }
index 3441f9be03b7d4542db500a7c5d0d3f1892bc8ab..904e53bd323aede7d4d74c56f5c0d559708ebd09 100644 (file)
@@ -159,6 +159,7 @@ struct PicCmdInfo {
 
 static void cmd_pic(ParseState *ps, const CmdInfo *ci) {
   if (!ps_needword(ps)) return;
+  
 }
 
 const CmdInfo toplevel_cmds[]= {
index 62dd211bd36ce41a4f2227b64efc3d1f157b9b56..dc6efa393b01ea08f0212d62a5951a5c71c60d35 100644 (file)
@@ -98,8 +98,6 @@ int thiswordstrcmp(ParseState *ps, const char *b);
 int ps_word(ParseState *ps);
 int ps_needword(ParseState *ps);
 int ps_needhextoend(ParseState *ps, Byte *dbuf, int *len_io);
-const CmdInfo *ps_lookup(ParseState *ps, const CmdInfo *inf);
-const CmdInfo *ps_needword_lookup(ParseState *ps, const CmdInfo *infs);
 void ps_callword(ParseState *ps, const CmdInfo *infs, const char *what);
 
 void vbadcmd(ParseState *ps, const char *fmt, va_list al)
@@ -123,4 +121,17 @@ struct RetransmitNode {
 void retransmit_queue(RetransmitNode *rn);
 void retransmit_cancel(RetransmitNode *rn);
 
+/*---------- macro for table lookups, with help from client.c ----------*/
+
+#define some_lookup(ps, infos)                 \
+  ((const typeof(infos[0])*)                   \
+   any_lookup((ps),(infos),sizeof((infos)[0])))
+
+#define some_needword_lookup(ps, infos)                                \
+  ((const typeof(infos[0])*)                                   \
+   any_needword_lookup((ps),(infos),sizeof((infos)[0])))
+
+const void *any_lookup(ParseState *ps, const void *infos, size_t infosz);
+const void *any_needword_lookup(ParseState *ps, const void *infos, size_t);
+
 #endif /*HOSTSIDE_H*/
index 8d34ce33d3bbba8daaf56e7e823ac23c782e902a..b4847324b05ee7619d2775e6e9b481b93f6cf22e 100755 (executable)
@@ -10,6 +10,21 @@ sub begin ($) {
 
 ($spec,$templ)=@ARGV;
 
+sub expand_and_write () {
+    $templl= $templlin;
+    $templl =~ s/\@([a-z]+)\=(\w*)\@/
+       die "$1=$2 in $templl ?" unless exists $v{$1};
+        $v{$1} eq $2  ? '' : '@SKIP@'
+    /ge;
+    $templl =~ m/\@SKIP\@/
+       and next;
+    $templl =~ s/\@([a-z]+)\@/
+       die $1 unless exists $v{$1};
+        $v{$1}
+    /ge;
+    print $templl or die $!;
+}
+
 sub process_line () {
     chomp;
     $origprotoline= $_;
@@ -64,20 +79,7 @@ sub process_line () {
        $v{opcodeyn}= sprintf "0x%02x", (oct("0b$opcode") | $ybit * $yval);
        $v{arglen}= $arglen;
        $v{arglentf}= sprintf "%d", !!$arglen;
-       $templl= $templlin;
-       $templl =~ s/\@h2p\@/\@dname=host2pic\@/;
-       $templl =~ s/\@p2h\@/\@dname=pic2host\@/;
-       $templl =~ s/\@([a-z]+)\=(\w*)\@/
-           die "$1=$2 in $templl ?" unless exists $v{$1};
-           $v{$1} eq $2  ? '' : '@SKIP@'
-       /ge;
-       $templl =~ m/\@SKIP\@/
-           and next;
-       $templl =~ s/\@([a-z]+)\@/
-           die $1 unless exists $v{$1};
-           $v{$1}
-       /ge;
-       print $templl or die $!;
+       expand_and_write();
     }
 }
 
@@ -86,8 +88,14 @@ for (;;) {
     $templlin= <T>;  last unless length $templlin;
     if ($templlin !~ m/\@\w+\@/) {
        print $templlin or die $!;
+    } elsif ($templlin =~ s/\@1\@//) {
+       undef %v;
+       $v{skeleton}= 'autogenerated - do not edit';
+       expand_and_write();
     } else {
         $doyn= $templlin =~ m/\@[a-z]+yn\@/;
+       $templlin =~ s/\@h2p\@/\@dname=host2pic\@/;
+       $templlin =~ s/\@p2h\@/\@dname=pic2host\@/;
         open S, "$spec" or die "$spec $!";
         while (<S>) {
            process_line();
index c6804e6fb4394d7c96433ab8033a98eb2515fb36..dc1ca89bbed307fcbd496dfe0c59d9c0530151df 100644 (file)
@@ -1,3 +1,4 @@
+/* @skeleton@ @1@ */
 /*
  * arranges for the declarations of
  *  enco_pic_WHATEVER