-/**/
+/*
+ * 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;
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");
}
}
- *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;