static int match_class(struct ExecCtx *ctx, const struct Op *op, const char *str, struct GMatch *gm) { int i, maxcnt = SIMPLE_MAXCNT(op); for (i = 0; (i < maxcnt); i++) { if (!class_isset(&op->cdata, str[i])) break; } return scan_next(ctx, op, str + i, gm, i, 1); }
static int match_any(struct ExecCtx *ctx, const struct Op *op, const char *str, struct GMatch *gm) { bool nl = (ctx->flags & REG_NEWLINE); int i, maxcnt = SIMPLE_MAXCNT(op); for (i = 0; (i < maxcnt) && str[i]; i++) { if (nl && str[i] == '\n') break; } return scan_next(ctx, op, str + i, gm, i, 1); }
static int match_char(struct ExecCtx *ctx, const struct Op *op, const char *str, struct GMatch *gm) { bool icase = (ctx->flags & REG_ICASE); int c, i, maxcnt = SIMPLE_MAXCNT(op); for (i = 0; (i < maxcnt) && str[i]; i++) { c = icase ? tolower((unsigned char)str[i]) : str[i]; if (c != op->lit) break; } return scan_next(ctx, op, str + i, gm, i, 1); }
int main() { char buff[128]; ScanState *ts = scan_new_from_file("subst.c"); scan_set_flags(ts,C_WSPACE | C_STRING_QUOTE); FILE *out = fopen("subst.co","w"); while (scan_next(ts)) { scan_get_tok(ts,buff,sizeof(buff)); fprintf(out,"%s",buff); } unref(ts); fclose(out); return 0; }
static int match_bref(struct ExecCtx *ctx, const struct Op *op, const char *str, struct GMatch *gm) { bool icase = ctx->flags & REG_ICASE; int i; struct GMatch *bgm = ctx->gm_stack[op->bref]; int blen = (bgm && bgm->end) ? (bgm->end - bgm->start) : -1; /* handle no-match, zero-len, zero-count */ if (blen < 0 && op->mincnt > 0) return REG_NOMATCH; if (blen <= 0 || op->maxcnt == 0) return do_match(ctx, op->next, str, gm); /* find max matches */ for (i = 0; (i < op->maxcnt) && *str; i++) { if (icase && strncasecmp(str, bgm->start, blen) != 0) break; else if (!icase && strncmp(str, bgm->start, blen) != 0) break; str += blen; } return scan_next(ctx, op, str, gm, i, blen); }
/* Scan a file and return an array of all the tokens */ static struct scanner_token *scan(struct scanner_input *I, unsigned int *n) { size_t nallocated = 16, count = 0; struct scanner_token *ret = GC_malloc(nallocated * sizeof(struct scanner_token)); struct scanner_token tok; do { tok = scan_next(I); if (count >= nallocated) { nallocated *= 2; ret = GC_realloc(ret, nallocated * sizeof(struct scanner_token)); } ret[count++] = tok; } while (tok.type != TOK_EOF); if (n) *n = (unsigned int) count; // Shrink it down to the right size ret = GC_realloc(ret, count * sizeof(struct scanner_token)); return ret; }
int main() { ScanState *ts; char buff[BUFSZ]; double values[100]; ts = scan_new_from_file("test1.dat"); scan_numbers_fun(ts,dump,NULL); // scan_numbers goes over the whole stream; // using scan_next_line can make it work over current line set (ts,scan_new_from_file("test1.dat")); while (scan_next_line(ts)) { int n = scan_numbers(ts,values,100); dumpall(values,n); } set(ts,scan_new_from_string("hello = (10,20,30)")); puts(scan_next_iden(ts,buff,BUFSZ)); printf("%c \n",scan_next(ts)); // after this, next call to scan_next will return this token again if (scan_next(ts) == '(') { scan_push_back(ts); } while (scan_next(ts)) { printf("%d \n",ts->type); } // words, strings and numbers set(ts,scan_new_from_string("here 'we go' again 10 ")); while (scan_next(ts)) { // this gives us a properly ref-counted string... char *str = scan_get_str(ts); printf("%d '%s'\n",ts->type,str); unref(str); } // extracting C strings from this file set(ts,scan_new_from_file("test-scan.c")); // set this if you want to decode C string escapes // scan_set_flags(ts,C_STRING); if (ts) { while (scan_next(ts)) { if (ts->type == T_STRING) { scan_get_tok(ts,buff,BUFSZ); printf("string '%s'\n",buff); } } } set(ts,scan_new_from_file("test.cfg")); scan_set_line_comment(ts,"#"); while (scan_next(ts)) { scan_get_tok(ts,buff,BUFSZ); printf("key %s ",buff); scan_next(ts); scan_get_line(ts,buff,BUFSZ); printf("value '%s'\n",buff); } ////// scan_scanf ////// const char *xml = "<boo a='woo' b='doll'>bonzo dog <(10,20,30),(1,2,3);"; set(ts, scan_new_from_string(xml)); //scan_set_flags(ts,C_NUMBER) is currently not consistent with scan_scanf! char *tag, *attrib, *value; scan_scanf(ts,"<%s",&tag); // %s is any iden, and %q is a _quoted_ string while (scan_scanf(ts,"%s=%q",&attrib,&value)) { printf("tag '%s' attrib '%s' value '%s'\n",tag,attrib,value); dispose(attrib,value); } dispose(tag); assert(ts->type == '>'); scan_get_upto(ts,"<",buff,BUFSZ); scan_advance(ts,-1); printf("got '%s' (%c)\n",buff,scan_getch(ts)); int i,j,k; char ch; // %d, %f and %c are what you expect... while (scan_scanf(ts,"(%d,%d,%d)%c",&i,&j,&k,&ch) && (ch == ',' || ch == ';')) printf("values %d %d %d\n",i,j,k); assert(ts->type == ';'); // %l means rest of current line... const char *config_data = "A=cool stuff\nB=necessary nonsense\nC=10,20\n"; set(ts,scan_new_from_string(config_data)); char *key, *v; while (scan_scanf(ts,"%s=%l",&key,&v)) { printf("%s='%s'\n",key,v); dispose(key,v); } // %v means read next token as a Value config_data = "alpha=1 beta=2 gamma=hello delta='frodo'"; PValue val; set(ts,scan_new_from_string(config_data)); while (scan_scanf(ts,"%s=%v",&key,&val)) { printf("%s=",key); if (value_is_string(val)) printf("'%s'\n",(char*)val); else if (value_is_int(val)) printf("%d\n",(int)*(intptr_t*)val); dispose(key,val); } unref(ts); printf("kount = %d\n",obj_kount()); }
static void rx_callback(phy_rx_data_t* res) { //log_packet(res->data); if (res == NULL) { scan_timeout(); return; } // Data Link Filtering // Subnet Matching do not parse it yet if (dll_state == DllStateScanBackgroundFrame) { uint16_t crc = crc_calculate(res->data, 4); if (memcmp((uint8_t*) &(res->data[4]), (uint8_t*) &crc, 2) != 0) { #ifdef LOG_DLL_ENABLED log_print_stack_string(LOG_DLL, "DLL CRC ERROR"); #endif scan_next(NULL); // how to reïnitiate scan on CRC Error, PHY should stay in RX return; } if (!check_subnet(0xFF, res->data[0])) // TODO: get device_subnet from datastore { #ifdef LOG_DLL_ENABLED log_print_stack_string(LOG_DLL, "DLL Subnet mismatch"); #endif scan_next(NULL); // how to reïnitiate scan on subnet mismatch, PHY should stay in RX return; } } else if (dll_state == DllStateScanForegroundFrame) { uint16_t crc = crc_calculate(res->data, res->length - 2); if (memcmp((uint8_t*) &(res->data[res->length - 2]), (uint8_t*) &crc, 2) != 0) { #ifdef LOG_DLL_ENABLED log_print_stack_string(LOG_DLL, "DLL CRC ERROR"); #endif scan_next(NULL); // how to reïnitiate scan on CRC Error, PHY should stay in RX return; } if (!check_subnet(0xFF, res->data[2])) // TODO: get device_subnet from datastore { #ifdef LOG_DLL_ENABLED log_print_stack_string(LOG_DLL, "DLL Subnet mismatch"); #endif scan_next(NULL); // how to reïnitiate scan on subnet mismatch, PHY should stay in RX return; } } else { #ifdef LOG_DLL_ENABLED log_print_stack_string(LOG_DLL, "DLL You fool, you can't be here"); #endif } // Optional Link Quality Assessment // parse packet dll_res.rssi = res->rssi; dll_res.lqi = res->lqi; dll_res.spectrum_id = current_css->values[current_scan_id].spectrum_id; if (dll_state == DllStateScanBackgroundFrame) { dll_background_frame_t* frame = (dll_background_frame_t*)frame_data; frame->subnet = res->data[0]; memcpy(frame->payload, res->data+1, 4); dll_res.frame_type = FrameTypeBackgroundFrame; dll_res.frame = frame; } else { dll_foreground_frame_t* frame = (dll_foreground_frame_t*)frame_data; frame->length = res->data[0]; frame->frame_header.tx_eirp = res->data[1] * 0.5 - 40; frame->frame_header.subnet = res->data[2]; frame->frame_header.frame_ctl = res->data[3]; uint8_t* data_pointer = res->data + 4; if (frame->frame_header.frame_ctl & FRAME_CTL_LISTEN) // Listen timeout_listen = 10; else timeout_listen = 0; if (frame->frame_header.frame_ctl & FRAME_CTL_DLLS) // DLLS present { // TODO parse DLLS Header frame->dlls_header = NULL; } else { frame->dlls_header = NULL; } if (frame->frame_header.frame_ctl & 0x20) // Enable Addressing { // Address Control Header dll_foreground_frame_address_ctl_t address_ctl;// = (dll_foreground_frame_address_ctl_t*) data_pointer; frame->address_ctl = &address_ctl; frame->address_ctl->dialogId = *data_pointer; data_pointer++; frame->address_ctl->flags = *data_pointer; data_pointer++; //data_pointer += sizeof(uint8_t*); uint8_t addressing = (frame->address_ctl->flags & 0xC0) >> 6; uint8_t vid = (frame->address_ctl->flags & 0x20) >> 5; uint8_t nls = (frame->address_ctl->flags & 0x10) >> 4; // TODO parse Source ID Header frame->address_ctl->source_id = data_pointer; if (vid) { data_pointer += 2; } else { data_pointer += 8; } if (addressing == 0 && nls == 0) { uint8_t id_target[8]; if (vid) { memcpy(data_pointer, &id_target, 2); data_pointer += 2; } else { memcpy(data_pointer, &id_target, 8); data_pointer += 8; } frame->address_ctl->target_id = (uint8_t*) &id_target; } else { frame->address_ctl->target_id = NULL; } } else {