chiark / gitweb /
fixes during movpos wip
[trains.git] / hostside / parseutils.c
index 3c23454bdb09bc6e7867d7db3282af3b5d2d00a0..c41bbd0bbe228ef954b68da127220eb6251b08cc 100644 (file)
@@ -1,11 +1,14 @@
-/**/
+/*
+ * common
+ * utilities for helping parsing
+ */
 
 #include <stdarg.h>
 #include <errno.h>
 #include <stdlib.h>
 #include <string.h>
 
-#include "hostside.h"
+#include "common.h"
 
 void badcmd(ParseState *ps, const char *fmt, ...) {
   va_list al;
@@ -60,16 +63,17 @@ int ps_neednoargs(ParseState *ps) {
   return 1;
 }
  
-int ps_needhextoend(ParseState *ps, Byte *d, int *remain_io) {
-  Byte *d_end;
+int ps_needhextoend(ParseState *ps, Byte *d, int *len_io) {
+  Byte *d_begin, *d_end;
   char buf[3], *ep;
 
-  d_end= d + *remain_io;
+  d_begin= d;
+  d_end= d + *len_io;
   buf[2]= 0;
   
   if (!ps->remain) { badcmd(ps,"need hex data block"); return 0; }
   for (;;) {
-    if (!ps_word(ps)) return 0;
+    if (!ps_word(ps)) break;
     while (ps->lthisword > 0) {
       if (ps->lthisword & 1) {
        badcmd(ps,"hex data block with odd number of digits in part");
@@ -85,10 +89,35 @@ int ps_needhextoend(ParseState *ps, Byte *d, int *remain_io) {
     }
   }
 
-  *remain_io= d_end - d;
+  *len_io= d - d_begin;
   return 1;
 }
 
+const void *any_lookup(ParseState *ps, const void *inf, int ninfsmax,
+                      size_t sz) {
+  const char *tname;
+  int i;
+  
+  for (i=0;
+       i<ninfsmax && (tname= *(const char *const*)inf);
+       i++, inf= (const char*)inf + sz)
+    if (!thiswordstrcmp(ps,tname))
+      return inf;
+  return 0;
+}
+
+const void *any_needword_lookup(ParseState *ps, const void *infs, int ninfsmax,
+                               size_t sz, const char *what) {
+  const void *r;
+  if (!ps_needword(ps)) return 0;
+  r= any_lookup(ps,infs,ninfsmax,sz);
+  if (!r) {
+    badcmd(ps,"unknown %s %.*s",what, ps->lthisword,ps->thisword);
+    return 0;
+  }
+  return r;
+}
+
 int lstrstrcmp(const char *a, int la, const char *b) {
   int lb, minl, r;