static void buildmap(Biobuf *fp) /* map goes from char name to value to print via *string() */ { uchar *p, *line, ch[100]; int val; Rune r; curmap++; if(curmap >= NMAP) { fprint(2, "proof: out of char maps; recompile\n"); exits("charmap"); } while ((line = Brdline(fp, '\n'))!= 0) { if (line[0] == '\n') return; line[Blinelen(fp)-1] = 0; scanstr((char *) line, (char *) ch, (char **) &p); if (ch[0] == '\0') { fprint(2, "bad map file line '%s'\n", (char*)line); continue; } val = strtol((char *) p, 0, 10); dprint(2, "buildmap %s (%x %x) %s %d\n", (char*)ch, ch[0], ch[1], (char*)p, val); chartorune(&r, (char*)ch); if(utflen((char*)ch)==1 && r<QUICK) charmap[curmap].quick[r] = val; else addmap(curmap, strdup((char *) ch), val); /* put somewhere else */ } }
int teacher_form(char *database){ refresh(); teacher xteacher; noecho(); WINDOW *win; int y,x; start_color(); getmaxyx(stdscr,y,x); win = newwin(0, 0, 0, 0); init_pair(1, COLOR_RED, COLOR_BLACK); box(win, 0, 0); print_in_middle(win, y/4 + 1, 0, x, "Enter the Name of Teacher", COLOR_PAIR(1)); print_form_str(win,y,x); scanstr(win,xteacher.name, x/3 - 1); clear(); refresh(); box(win, 0, 0); print_in_middle(win, y/4 + 1, 0, x, "Enter the weekly hours for the teacher", COLOR_PAIR(1)); print_form_int(win, y, x); xteacher.week_time = scanint(win); add_teacher(database, &xteacher); refresh(); endwin(); clear(); return 0; }
static void buildtroff(char *buf) /* map troff names into bitmap filenames */ { /* e.g., R -> times/R., I -> times/I., etc. */ char *p, cmd[100], name[200], prefix[400], fallback[100]; scanstr(buf, cmd, &p); scanstr(p, name, &p); scanstr(p, prefix, &p); while(*p!=0 && isspace(*p)) p++; if(*p != 0){ scanstr(p, fallback, &p); fontmap[nfontmap].fallback = strdup(fallback); }else fontmap[nfontmap].fallback = 0; fontmap[nfontmap].troffname = strdup(name); fontmap[nfontmap].prefix = strdup(prefix); fontmap[nfontmap].map = curmap; dprint(2, "troff name %s is bitmap %s map %d in slot %d fallback %s\n", name, prefix, curmap, nfontmap, fontmap[nfontmap].fallback? fontmap[nfontmap].fallback : "<null>"); nfontmap++; }
int main(void) { plan_tests(83); #undef TRY #define TRY(tgt,pat) ok(scanstr(tgt,pat) == strstr(tgt,pat), "'%s'~'%s' -> %p:%p", tgt, pat, scanstr(tgt,pat), strstr(tgt,pat)) TRY("", "a"); TRY("hello", ""); TRY("art", "xe"); TRY("hello", "h"); TRY("hello", "e"); TRY("hello", "l"); TRY("hello", "p"); TRY("ab", "cd"); TRY("ab", "ab"); TRY("cab", "ab"); TRY("dcab", "ab"); TRY("edcab", "ab"); TRY("edcabe", "ab"); TRY("fedcabf", "ab"); char ab[] = "ab"; TRY(ab, "abc"); TRY("abc", "xbc"); TRY("abc", "axc"); TRY("abc", "abx"); TRY("abc", "abc"); TRY("abcd", "abc"); TRY("zabcd", "abc"); TRY("yzabcd", "abc"); TRY("xyzabcd", "abc"); TRY("wxyzabcd", "abc"); TRY("xabc", "abc"); TRY("", "abcd"); TRY("a", "abcd"); TRY("ab", "abcd"); TRY("abcd", "xbcd"); TRY("abcd", "axcd"); TRY("abcd", "abxd"); TRY("abcd", "abcx"); TRY("abcd", "abcd"); TRY("abcde", "abcd"); TRY("zabcd", "abcd"); TRY("yzabcd", "abcd"); TRY("xyzabcd", "abcd"); TRY("vwxyzabcd", "abcd"); TRY("uvwxyzabcd", "abcd"); TRY("tuvwxyzabcd", "abcd"); TRY("stuvwxyzabcd", "abcd"); TRY("rstuvwxyzabcd", "abcd"); TRY("qrstuvwxyzabcd", "abcd"); TRY("qrstuvwxyzabcdabcd", "abcd"); TRY("xabcde", "abcd"); TRY("", "abcde"); TRY("a", "abcde"); TRY("ab", "abcde"); TRY("abcde", "xbcd"); TRY("abcde", "axcd"); TRY("abcde", "abxd"); TRY("abcde", "abcx"); TRY("abcde", "abcde"); TRY("abcdee", "abcde"); TRY("zabcde", "abcde"); TRY("yzabcde", "abcde"); TRY("xyzabcde", "abcde"); TRY("vwxyzabcde", "abcde"); TRY("uvwxyzabcde", "abcde"); TRY("tuvwxyzabcde", "abcde"); TRY("stuvwxyzabcde", "abcde"); TRY("rstuvwxyzabcde", "abcde"); TRY("qrstuvwxyzabcde", "abcde"); TRY("qrstuvwxyzabcdeabcde", "abcde"); TRY("xabcdee", "abcde"); char y[] = "451AC87F_1271_3613_1\t2006-09-27T18:52:48|f=<*****@*****.**>|t=<*****@*****.**>|h=__FRAUD_419_REFNUM|h=__HAS_MSGID|h=__MIME_TEXT_ONLY|h=__SANE_MSGID|Size=7477|inbound|action=deliver|p=0.076|S=?q?[BUGS]_BUG_#2653:_failed_to_make|b=ok|r=200.46.204.254|tm=1.00|a=a/eom"; TRY(y,"009"); #undef TRY #define TRY(tgt,pat) ok(scanbrk(tgt,pat) == strpbrk(tgt,pat), "'%s'~[%s] -> %p:%p", tgt, pat, scanbrk(tgt,pat), strpbrk(tgt,pat)) TRY("hello, world", "wa"); TRY("hello, world", "wo"); TRY("hello, world", "xy"); TRY("hello, world", "war"); TRY("hello, world", "dar"); TRY("hello, world", "xyz"); TRY("", ""); TRY("", "abcde"); TRY("abcde", ""); TRY("hello, world", "w"); TRY("abcdefghijklmnopqrstuvwxyz","fghijklmnopqrstuvwxyz"); char x[] = "451A8BF4_1271_3531_1\t2006-09-27T14:34:30|f=<borea"; TRY(x, "ab"); #undef TRY #define TRY(x,y) ok(isign(scancmp(x,y)) == isign(strcmp(x,y)), "'%s'=='%s' -> %d:%d", x,y,scancmp(x,y), strcmp(x,y)); TRY("hello, world", "hello, world"); TRY("hellow, world", "hello, world"); union { double a; char s[99]; } d0, d1; strcpy(d0.s, "hello, world"); strcpy(d1.s, "hello, world"); TRY(d0.s, d1.s); int i, j; for (i = 1; i <= 64; ++i) { for (j = 32; j < 64; ++j) { char * pat = refdup(subref(strref(y), i, j)); if (scanstr(y, pat) != strstr(y, pat)) break; free(pat); } } ok(i == 65, "scanstr[%d,%d]", i, j); __attribute__((aligned(16))) const char edge[] = "0123456789abdef0123456789abcdef"; TRY(edge+1, "01"); return exit_status(); }
static void loadfont(int n, int s) { char file[256]; int i, fd, t, deep; static char *try[3] = {"", "times/R.", "pelm/"}; Subfont *f; Font *ff; try[0] = fname[n]; for (t = 0; t < 3; t++) { i = s * mag * charmap[fmap[n]].xheight/0.72; /* a pixel is 0.72 points */ if (i < MINSIZE) i = MINSIZE; dprint(2, "size %d, i %d, mag %g\n", s, i, mag); for(; i >= MINSIZE; i--) { /* if .font file exists, take that */ snprint(file, sizeof file, "%s/%s%d.font", libfont, try[t], i); ff = openfont(display, file); if(ff != 0) { fonttab[n][s] = ff; dprint(2, "using %s for font %d %d\n", file, n, s); return; } /* else look for a subfont file */ for (deep = log2[screen->depth]; deep >= 0; deep--) { snprint(file, sizeof file, "%s/%s%d.%d", libfont, try[t], i, deep); dprint(2, "trying %s for %d\n", file, i); if ((fd = open(file, 0)) >= 0) { f = readsubfont(display, file, fd, 0); if (f == 0) { fprint(2, "can't rdsubfontfile %s: %r\n", file); exits("rdsubfont"); } close(fd); ff = mkfont(f, 0); if(ff == 0) { fprint(2, "can't mkfont %s: %r\n", file); exits("rdsubfont"); } fonttab[n][s] = ff; dprint(2, "using %s for font %d %d\n", file, n, s); return; } } } } fprint(2, "can't find font %s.%d or substitute, quitting\n", fname[n], s); exits("no font"); } void loadfontname(int n, char *s) { int i; Font *f, *g = 0; if (strcmp(s, fname[n]) == 0) return; if(fname[n] && fname[n][0]) { if(lastload[n] && strcmp(lastload[n], fname[n]) == 0) return; strcpy(lastload[n], fname[n]); } fontlookup(n, s); for (i = 0; i < NSIZE; i++) if (f = fonttab[n][i]) { if (f != g) { freefont(f); g = f; } fonttab[n][i] = 0; } } void allfree(void) { int i; for (i=0; i<NFONT; i++) loadfontname(i, "??"); } void readmapfile(char *file) { Biobuf *fp; char *p, cmd[100]; if ((fp=Bopen(file, OREAD)) == 0) { fprint(2, "proof: can't open map file %s\n", file); exits("urk"); } while((p=Brdline(fp, '\n')) != 0) { p[Blinelen(fp)-1] = 0; scanstr(p, cmd, 0); if(p[0]=='\0' || eq(cmd, "#")) /* skip comments, empty */ continue; else if(eq(cmd, "xheight")) buildxheight(fp); else if(eq(cmd, "map")) buildmap(fp); else if(eq(cmd, "special")) buildtroff(p); else if(eq(cmd, "troff")) buildtroff(p); else fprint(2, "weird map line %s\n", p); } Bterm(fp); }
static int scanpp(void) { int c, t; if (Rejected != -1) { t = Rejected; Rejected = -1; strcpy(Text, Rejtext); Value = Rejval; return t; } for (;;) { Value = 0; c = skip(); memset(Text, 0, 4); Text[0] = c; switch (c) { case '!': if ((c = next()) == '=') { Text[1] = '='; return NOTEQ; } else { putback(c); return XMARK; } case '%': if ((c = next()) == '=') { Text[1] = '='; return ASMOD; } else { putback(c); return MOD; } case '&': if ((c = next()) == '&') { Text[1] = '&'; return LOGAND; } else if ('=' == c) { Text[1] = '='; return ASAND; } else { putback(c); return AMPER; } case '(': return LPAREN; case ')': return RPAREN; case '*': if ((c = next()) == '=') { Text[1] = '='; return ASMUL; } else { putback(c); return STAR; } case '+': if ((c = next()) == '+') { Text[1] = '+'; return INCR; } else if ('=' == c) { Text[1] = '='; return ASPLUS; } else { putback(c); return PLUS; } case ',': return COMMA; case '-': if ((c = next()) == '-') { Text[1] = '-'; return DECR; } else if ('=' == c) { Text[1] = '='; return ASMINUS; } else if ('>' == c) { Text[1] = '>'; return ARROW; } else { putback(c); return MINUS; } case '/': if ((c = next()) == '=') { Text[1] = '='; return ASDIV; } else { putback(c); return SLASH; } case ':': return COLON; case ';': return SEMI; case '<': if ((c = next()) == '<') { Text[1] = '<'; if ((c = next()) == '=') { Text[2] = '='; return ASLSHIFT; } else { putback(c); return LSHIFT; } } else if ('=' == c) { Text[1] = '='; return LTEQ; } else { putback(c); return LESS; } case '=': if ((c = next()) == '=') { Text[1] = '='; return EQUAL; } else { putback(c); return ASSIGN; } case '>': if ((c = next()) == '>') { Text[1] = '>'; if ((c = next()) == '=') { Text[1] = '='; return ASRSHIFT; } else { putback(c); return RSHIFT; } } else if ('=' == c) { Text[1] = '='; return GTEQ; } else { putback(c); return GREATER; } case '?': return QMARK; case '[': return LBRACK; case ']': return RBRACK; case '^': if ((c = next()) == '=') { Text[1] = '='; return ASXOR; } else { putback(c); return CARET; } case '{': return LBRACE; case '|': if ((c = next()) == '|') { Text[1] = '|'; return LOGOR; } else if ('=' == c) { Text[1] = '='; return ASOR; } else { putback(c); return PIPE; } case '}': return RBRACE; case '~': return TILDE; case EOF: strcpy(Text, "<EOF>"); return XEOF; case '\'': Text[1] = Value = scanch(); if ((c = next()) != '\'') error( "expected '\\'' at end of char literal", NULL); Text[2] = '\''; return INTLIT; case '"': Value = scanstr(Text); return STRLIT; case '#': Text[0] = '#'; scanident(next(), &Text[1], TEXTLEN-1); if ((t = keyword(Text)) != 0) return t; error("unknown preprocessor command: %s", Text); return IDENT; case '.': if ((c = next()) == '.') { Text[1] = Text[2] = '.'; Text[3] = 0; if ((c = next()) == '.') return ELLIPSIS; putback(c); error("incomplete '...'", NULL); return ELLIPSIS; } putback(c); return DOT; default: if (isdigit(c)) { Value = scanint(c); return INTLIT; } else if (isalpha(c) || '_' == c) { Value = scanident(c, Text, TEXTLEN); if (Expandmac && macro(Text)) break; if ((t = keyword(Text)) != 0) return t; return IDENT; } else { cerror("funny input character: %s", c); break; } } } }
// scan the next symbol void nextsym() { // continue until symbol found bool symfnd; do { symfnd = true; skipsep(); symptr = inptr; if (inLetters( ch)) // ident or reserved word { scanident(); } else { if (inDigits( ch)) // number scannum(); else { switch( ch) { case '"': strToMatch = '"'; scanstr(); break; case '\'': strToMatch = '\''; scanstr(); break; case '.': sym = per; nextchar(); if ( ch == '.') { sym = doubledot; nextchar(); } break; case ':': idassign(); break; case '<': // fall through to case '>', both call relop case '>': idrelop(); break; case '(': // check for comment nextchar(); if ( ch != '*') sym = lparen; else // now we know it's a comment { symfnd = false; nextchar(); rcomment(); } break; case EOF: sym = eof_sym; break; default: // then should be in one of the "special" chars (star, slash, etc.) sym = spsym[ ch]; nextchar(); break; } // end switch } // end if } // end do } while (!symfnd); // TODO //writesym(); } // end nextsym
lex() { int c, d; char *s; if (regexflg) return sym = scanreg(); next: while ((c = Getc()) == ' ' || c == '\t') ; while (c == '#') for (c = Getc(); c != '\n'; c = Getc()) ; switch (c) { case '\\': if ((c = Getc()) == '\n') { lineno++; goto next; } break; case '\n': lineno++; break; } switch (c) { case EOF: return sym = 0; case '+': return sym = follow2('=', '+', ADDEQ, INC, ADD); case '-': return sym = follow2('=', '-', SUBEQ, DEC, SUB); case '*': return sym = follow('=', MULTEQ, MULT); case '/': return sym = follow('=', DIVEQ, DIV); case '%': return sym = follow('=', MODEQ, MOD); case '^': return sym = follow('=', POWEQ, POWER); case '=': return sym = follow('=', EQ, ASSIGN); case '!': return sym = follow2('=', '~', NE, NOMATCH, NOT); case '&': return sym = follow('&', AND, BINAND); case '|': sym = follow('|', OR, BINOR); if (printflg && sym == BINOR) sym = R_POUT; return sym; case '<': sym = follow2('=', '<', LE, SHIFTL, LT); if (getlineflg && sym == LT) sym = R_IN; return sym; case '>': sym = follow2('=', '>', GE, SHIFTR, GT); if (printflg) { switch (sym) { case GT: sym = R_OUT; break; case SHIFTR: sym = R_APD; break; } } return sym; case '~': return sym = MATCH; break; case ';': case '\n': return sym = EOL; } if (isalpha(c) || c == '_') { for (s = text; isalnum(c) || c == '_'; ) { *s++ = c; c = Getc(); } Ungetc(c); *s = '\0'; if ((d = iskeywd(text)) == 0 && (d = isbuiltin(text, &sym1)) == 0) { if (c == '(') return sym = CALL; else if (funflg) { if ((sym1 = isarg(text)) != -1) return sym = ARG; } } return sym = d ? d : IDENT; } else if (c == '.' || (isdigit(c))) { Ungetc(c); return sym = scannum(text); /* NUMBER */ } else if (c == '"') return sym = scanstr(text); /* STRING */ return sym = c; }
/** Custom match function that filters to exact matches * * If no auxiliary data is stored, we decode the pattern, compile regular * expressions, etc. and store aux_pattern_data struct as auxiliary data for the * pattern. */ void matchFunction(sqlite3_context* pCtx, int argc, sqlite3_value** argv){ /* Validate the input */ if(argc != 2){ sqlite3_result_error(pCtx, "The MATCH operator on a trigram index takes 2 arguments!", -1); return; } trilite_cursor *pTrgCur; if(triliteCursorFromBlob(&pTrgCur, argv[1]) != SQLITE_OK){ sqlite3_result_error(pCtx, "The MATCH operator must have 'contents' as left hand side", -1); return; } /* Get prepared auxiliary data, if any */ aux_pattern_data *pAuxData = (aux_pattern_data*)sqlite3_get_auxdata(pCtx, 0); /* If we didn't get any auxiliary data, create it and store it! */ if(!pAuxData){ /* Check that we have correct datatype for the pattern */ if(sqlite3_value_type(argv[0]) != SQLITE_TEXT){ sqlite3_result_error(pCtx, "The pattern for the MATCH operator on a trigram index must be a string", -1); return; } /* Get the pattern */ const unsigned char *pattern = sqlite3_value_text(argv[0]); int nPattern = sqlite3_value_bytes(argv[0]); /* Create some auxiliary data :) */ matchCreate(&pAuxData, pattern, nPattern); if(!pAuxData){ /* Die on errors, there shouldn't be any here if match is called correctly */ sqlite3_result_error(pCtx, "The match operator needs a valid pattern", -1); return; } /* Save the auxiliary data for next time */ sqlite3_set_auxdata(pCtx, 0, pAuxData, (void(*)(void*))matchAuxDataFree); } /* Get the text from the cursor */ const unsigned char *text; int nText; triliteText(pTrgCur, &text, &nText); bool retval = false; if(pAuxData->eType & PATTERN_SUBSTR){ const unsigned char *start = scanstr(text, nText, pAuxData->pattern, pAuxData->nPattern); retval = start != NULL; /* If output extents is requested */ if(pAuxData->eType & PATTERN_EXTENTS){ while(start){ const unsigned char *end = start + pAuxData->nPattern; triliteAddExtents(pTrgCur, start - text, end - text); start = scanstr(end, nText - (end - text), pAuxData->pattern, pAuxData->nPattern); } } } else if(pAuxData->eType == PATTERN_REGEXP){ assert(pAuxData->pRegExp); retval = regexpMatch(pAuxData->pRegExp, text, nText); } else if(pAuxData->eType == (PATTERN_REGEXP | PATTERN_EXTENTS)){ const unsigned char *start = text; const unsigned char *end = text; while(regexpMatchExtents(pAuxData->pRegExp, &start, &end, end, nText - (end - text))){ retval = true; triliteAddExtents(pTrgCur, start - text, end - text); } } else { assert(false); sqlite3_result_error(pCtx, "The pattern must be either a regular expression or substring pattern", -1); return; } /* Return true (1) if pattern in a substring of text */ if(retval){ sqlite3_result_int(pCtx, 1); }else{ sqlite3_result_int(pCtx, 0); } }