// return: 1 if escaped (forced to be symbol) static int read_token(char c, int digits) { int i=0, ch, escaped=0, issym=0, first=1; while (1) { if (!first) { ch = ios_getc(F); if (ch == IOS_EOF) goto terminate; c = (char)ch; } first = 0; if (c == '|') { issym = 1; escaped = !escaped; } else if (c == '\\') { issym = 1; ch = ios_getc(F); if (ch == IOS_EOF) goto terminate; accumchar((char)ch, &i); } else if (!escaped && !(symchar(c) && (!digits || isdigit(c)))) { break; } else { accumchar(c, &i); } } ios_ungetc(c, F); terminate: buf[i++] = '\0'; return issym; }
void readsym(char *isymbol) { char *p; Rune r; p = isymbol; do { if (p < &isymbol[MAXSYM-UTFmax-1]){ r = lastc; p += runetochar(p, &r); } readchar(); } while (symchar(1)); *p = 0; }
static void print_symbol_name(ios_t *f, char *name) { int i, escape=0, charescape=0; if ((name[0] == '\0') || (name[0] == '.' && name[1] == '\0') || (name[0] == '#') || isnumtok(name, NULL)) escape = 1; i=0; while (name[i]) { if (!symchar(name[i])) { escape = 1; if (name[i]=='|' || name[i]=='\\') { charescape = 1; break; } } i++; } if (escape) { if (charescape) { outc('|', f); i=0; while (name[i]) { if (name[i]=='|' || name[i]=='\\') outc('\\', f); outc(name[i], f); i++; } outc('|', f); } else { outc('|', f); outs(name, f); outc('|', f); } } else { outs(name, f); } }
static u_int32_t peek(void) { char c, *end; fixnum_t x; int ch, base; if (toktype != TOK_NONE) return toktype; c = nextchar(); if (ios_eof(F)) return TOK_NONE; if (c == '(') { toktype = TOK_OPEN; } else if (c == ')') { toktype = TOK_CLOSE; } else if (c == '[') { toktype = TOK_OPENB; } else if (c == ']') { toktype = TOK_CLOSEB; } else if (c == '\'') { toktype = TOK_QUOTE; } else if (c == '`') { toktype = TOK_BQ; } else if (c == '"') { toktype = TOK_DOUBLEQUOTE; } else if (c == '#') { ch = ios_getc(F); c = (char)ch; if (ch == IOS_EOF) lerror(ParseError, "read: invalid read macro"); if (c == '.') { toktype = TOK_SHARPDOT; } else if (c == '\'') { toktype = TOK_SHARPQUOTE; } else if (c == '\\') { uint32_t cval; if (ios_getutf8(F, &cval) == IOS_EOF) lerror(ParseError, "read: end of input in character constant"); if (cval == (uint32_t)'u' || cval == (uint32_t)'U' || cval == (uint32_t)'x') { read_token('u', 0); if (buf[1] != '\0') { // not a solitary 'u','U','x' if (!read_numtok(&buf[1], &tokval, 16)) lerror(ParseError, "read: invalid hex character constant"); cval = numval(tokval); } } else if (cval >= 'a' && cval <= 'z') { read_token((char)cval, 0); tokval = symbol(buf); if (buf[1] == '\0') /* one character */; else if (tokval == nulsym) cval = 0x00; else if (tokval == alarmsym) cval = 0x07; else if (tokval == backspacesym) cval = 0x08; else if (tokval == tabsym) cval = 0x09; else if (tokval == linefeedsym) cval = 0x0A; else if (tokval == newlinesym) cval = 0x0A; else if (tokval == vtabsym) cval = 0x0B; else if (tokval == pagesym) cval = 0x0C; else if (tokval == returnsym) cval = 0x0D; else if (tokval == escsym) cval = 0x1B; else if (tokval == spacesym) cval = 0x20; else if (tokval == deletesym) cval = 0x7F; else lerrorf(ParseError, "read: unknown character #\\%s", buf); } toktype = TOK_NUM; tokval = mk_wchar(cval); } else if (c == '(') { toktype = TOK_SHARPOPEN; } else if (c == '<') { lerror(ParseError, "read: unreadable object"); } else if (isdigit(c)) { read_token(c, 1); c = (char)ios_getc(F); if (c == '#') toktype = TOK_BACKREF; else if (c == '=') toktype = TOK_LABEL; else lerror(ParseError, "read: invalid label"); errno = 0; x = strtol(buf, &end, 10); if (*end != '\0' || errno) lerror(ParseError, "read: invalid label"); tokval = fixnum(x); } else if (c == '!') { // #! single line comment for shbang script support do { ch = ios_getc(F); } while (ch != IOS_EOF && (char)ch != '\n'); return peek(); } else if (c == '|') { // multiline comment int commentlevel=1; while (1) { ch = ios_getc(F); hashpipe_gotc: if (ch == IOS_EOF) lerror(ParseError, "read: eof within comment"); if ((char)ch == '|') { ch = ios_getc(F); if ((char)ch == '#') { commentlevel--; if (commentlevel == 0) break; else continue; } goto hashpipe_gotc; } else if ((char)ch == '#') { ch = ios_getc(F); if ((char)ch == '|') commentlevel++; else goto hashpipe_gotc; } } // this was whitespace, so keep peeking return peek(); } else if (c == ';') { // datum comment (void)do_read_sexpr(UNBOUND); // skip return peek(); } else if (c == ':') { // gensym ch = ios_getc(F); if ((char)ch == 'g') ch = ios_getc(F); read_token((char)ch, 0); errno = 0; x = strtol(buf, &end, 10); if (*end != '\0' || buf[0] == '\0' || errno) lerror(ParseError, "read: invalid gensym label"); toktype = TOK_GENSYM; tokval = fixnum(x); } else if (symchar(c)) { read_token(ch, 0); if (((c == 'b' && (base= 2)) || (c == 'o' && (base= 8)) || (c == 'd' && (base=10)) || (c == 'x' && (base=16))) && (isdigit_base(buf[1],base) || buf[1]=='-')) { if (!read_numtok(&buf[1], &tokval, base)) lerrorf(ParseError, "read: invalid base %d constant", base); return (toktype=TOK_NUM); } toktype = TOK_SHARPSYM; tokval = symbol(buf); } else { lerror(ParseError, "read: unknown read macro"); } } else if (c == ',') { toktype = TOK_COMMA; ch = ios_getc(F); if (ch == IOS_EOF) return toktype; if ((char)ch == '@') toktype = TOK_COMMAAT; else if ((char)ch == '.') toktype = TOK_COMMADOT; else ios_ungetc((char)ch, F); } else { if (!read_token(c, 0)) { if (buf[0]=='.' && buf[1]=='\0') { return (toktype=TOK_DOT); } else { if (read_numtok(buf, &tokval, 0)) return (toktype=TOK_NUM); } } toktype = TOK_SYM; tokval = symbol(buf); } return toktype; }
item(int a) { /* name [ . local ] | number | . | ^ | <register | 'x | | */ char *base; char savc; uvlong e; Symbol s; char gsym[MAXSYM], lsym[MAXSYM]; readchar(); if (isfileref()) { readfname(gsym); rdc(); /* skip white space */ if (lastc == ':') { /* it better be */ rdc(); /* skip white space */ if (!getnum(readchar)) error("bad number"); if (expv == 0) expv = 1; /* file begins at line 1 */ expv = file2pc(gsym, expv); if (expv == -1) error("%r"); return 1; } error("bad file location"); } else if (symchar(0)) { readsym(gsym); if (lastc=='.') { readchar(); /* ugh */ if (lastc == '.') { lsym[0] = '.'; readchar(); readsym(lsym+1); } else if (symchar(0)) { readsym(lsym); } else lsym[0] = 0; if (localaddr(cormap, gsym, lsym, &e, rget) < 0) error("%r"); expv = e; } else { if (lookup(0, gsym, &s) == 0) error("symbol not found"); expv = s.value; } reread(); } else if (getnum(readchar)) { ; } else if (lastc=='.') { readchar(); if (!symchar(0) && lastc != '.') { expv = dot; } else { if (findsym(rget(cormap, mach->pc), CTEXT, &s) == 0) error("no current function"); if (lastc == '.') { lsym[0] = '.'; readchar(); readsym(lsym+1); } else readsym(lsym); if (localaddr(cormap, s.name, lsym, &e, rget) < 0) error("%r"); expv = e; } reread(); } else if (lastc=='"') { expv=ditto; } else if (lastc=='+') { expv=inkdot(dotinc); } else if (lastc=='^') { expv=inkdot(-dotinc); } else if (lastc=='<') { savc=rdc(); base = regname(savc); expv = rget(cormap, base); } else if (lastc=='\'') expv = ascval(); else if (a) error("address expected"); else { reread(); return(0); } return(1); }
int main(int argc, char *argv[]) { int i, j, l, isapi; FILE *fd; char *buf, *buf2, *buf3, *buf4; char *s, *s2, *t; char *cat, *api; buf=malloc(16384); buf2=malloc(16384); buf3=malloc(256); buf4=malloc(256); doc=0; pds=0; amod=0; prx=0; api=NULL; for(i=1; i<argc; i++) { if(argv[i][0]=='-') { if(!strcmp(argv[i], "-doc")) { doc=1; cat=argv[i+1]; i++; continue; } if(!strcmp(argv[i], "-pds")) { pds=1; cat=argv[i+1]; i++; continue; } if(!strcmp(argv[i], "-api")) { api=argv[i+1]; i++; continue; } if(!strcmp(argv[i], "-apionly")) { api=argv[i+1]; amod=1; i++; continue; } if(!strcmp(argv[i], "-noapi")) { api=argv[i+1]; amod=2; i++; continue; } if(!strcmp(argv[i], "-proxy")) { prx=1; continue; } if(!strcmp(argv[i], "-idarch")) { printf("%s", id_arch()); exit(0); } if(!strcmp(argv[i], "-idwsuf")) { printf("%s", id_wsuf()); exit(0); } fprintf(stderr, "Unknown option '%s'\n", argv[i]); exit(-1); } fd=fopen(argv[i], "rt"); if(!fd)continue; printf("//AHSRC:%s\n", argv[i]); l=0; while(!feof(fd)) { memset(buf, 0, 256); fgets(buf, 255, fd); l++; s=buf; while(*s && (*s!='\r') && (*s!='\n'))s++; if(*s=='\r')*s=0; if(*s=='\n')*s=0; s=buf; while(*s && (*s<=' '))s++; if(!strncmp(s, "//AH:", 5)) { // fprintf(stderr, "//AH tag %s %d\n", // argv[i], l); s+=5; while(*s && (*s<=' '))s++; if(!strcmp(s, "skip"))break; } if(pds)if(!strncmp(s, "//PDSCAT:", 9)) { s+=9; t=buf3; while(*s)*t++=*s++; *t++=0; cat=strdup(buf3); } if(pds)if(!strncmp(s, "//PDS:", 6)) { s+=6; while(*s && (*s<=' '))s++; t=buf3; while(*s && strncmp(s, "::", 2)) { if((s[0]=='\\') && (s[1]<=' ')) { fgets(buf, 255, fd); s=buf; while(*s && (*s<=' '))s++; if(!strncmp(s, "//", 2))s+=2; continue; } if(*s=='\r')break; if(*s=='\n')break; *t++=*s++; } *t++=0; if(!strncmp(s, "::", 2))s+=2; t=buf4; while(*s) { if((s[0]=='\\') && (s[1]<=' ')) { fgets(buf, 255, fd); s=buf; while(*s && (*s<=' '))s++; if(!strncmp(s, "//", 2))s+=2; continue; } if(*s=='\r')break; if(*s=='\n')break; *t++=*s++; } *t++=0; printf( "/*--\n" "Cat %s\n" "Text\n" "\t%s\n" "\t%s\n" "--*/\n", cat, buf3, buf4); } if(pds)continue; s=buf; isapi=0; if(api) { if(!strncmp(s, api, strlen(api))) { if(amod==2)continue; s+=strlen(api); while(*s && (*s<=' '))s++; isapi=1; }else { if(amod==1)continue; } } if(!strncmp(s, "static", strlen("static"))) continue; if(*s<=' ')continue; while(symchar(*s))s++; while(*s && (*s<=' '))s++; while(*s=='*')s++; if(!*s || (*s=='('))continue; while(*s && symchar(*s))s++; if(((*s=='=') || (*s==';')) && isapi) { //special case: API variables are exported t=s; while(*s && (*s!='/'))s++; if(!strncmp(s, "//AH:", 5)) { s+=5; while(*s && (*s<=' '))s++; if(!strcmp(s, "ignore"))continue; } s=t; *s=0; if(!doc)printf("%s;\n", buf); continue; } if(*s==' ')s++; if(*s!='(')continue; t=s; while(*s && (*s!='/'))s++; if(!strncmp(s, "//AH:", 5)) { // fprintf(stderr, "//AH tag %s %d\n", // argv[i], l); s+=5; while(*s && (*s<=' '))s++; if(!strcmp(s, "ignore"))continue; } s=t; if(*s=='(')s++; j=1; while(j) { if(!*s) { memset(buf2, 0, 256); fgets(buf2, 255, fd); l++; t=buf2; while(*t && (*t<=' '))t++; s2=t; t=s; while(*s2 && (*s2!='\n'))*t++=*s2++; *t++=' '; *t++=0; }else { if(*s=='(')j++; if(*s==')')j--; s++; } } t=s; while(*s && (*s!='/'))s++; if(!strncmp(s, "//AH:", 5)) { fprintf(stderr, "//AH tag %s %d\n", argv[i], l); s+=5; while(*s && (*s<=' '))s++; if(!strncmp(s, "ignore", 6))continue; if(prx && strncmp(s, "proxy", 5))continue; if(!strncmp(s, "proxy", 5))isapi=2; }else { if(prx)continue; } s=t; if(*s==')')s++; *s=0; if(doc) { printf( "/*--\n" "Cat %s\n" "Text\n" "\t%s;\n" "\t%s:%d\n" "--*/\n", cat, buf, argv[i], l); }else { if(prx) { prx_def(buf); continue; } if(isapi==2) printf("/*AHPRX:*/ ", buf); printf("%s;\n", buf); } } fclose(fd); } return(0); }
int main(int argc, char *argv[]) { int i, j, l; FILE *fd; char *buf, *buf2, *buf3, *buf4; char *s, *s2, *t; char *cat; buf=malloc(16384); buf2=malloc(16384); buf3=malloc(256); buf4=malloc(256); doc=0; pds=0; for(i=1; i<argc; i++) { if(argv[i][0]=='-') { if(!strcmp(argv[i], "-doc")) { doc=1; cat=argv[i+1]; i++; continue; } if(!strcmp(argv[i], "-pds")) { pds=1; cat=argv[i+1]; i++; continue; } fprintf(stderr, "Unknown option '%s'\n", argv[i]); exit(-1); } fd=fopen(argv[i], "rt"); printf("//%s\n", argv[i]); l=0; while(!feof(fd)) { memset(buf, 0, 256); fgets(buf, 255, fd); l++; s=buf; while(*s && (*s!='\r') && (*s!='\n'))s++; if(*s=='\r')*s=0; if(*s=='\n')*s=0; s=buf; while(*s && (*s<=' '))s++; if(!strncmp(s, "//AH:", 5)) { // fprintf(stderr, "//AH tag %s %d\n", // argv[i], l); s+=5; while(*s && (*s<=' '))s++; if(!strcmp(s, "skip"))break; } if(pds)if(!strncmp(s, "//PDSCAT:", 9)) { s+=9; t=buf3; while(*s)*t++=*s++; *t++=0; cat=strdup(buf3); } if(pds)if(!strncmp(s, "//PDS:", 6)) { s+=6; while(*s && (*s<=' '))s++; t=buf3; while(*s && strncmp(s, "::", 2)) { if((s[0]=='\\') && (s[1]<=' ')) { fgets(buf, 255, fd); s=buf; while(*s && (*s<=' '))s++; if(!strncmp(s, "//", 2))s+=2; continue; } if(*s=='\r')break; if(*s=='\n')break; *t++=*s++; } *t++=0; if(!strncmp(s, "::", 2))s+=2; t=buf4; while(*s) { if((s[0]=='\\') && (s[1]<=' ')) { fgets(buf, 255, fd); s=buf; while(*s && (*s<=' '))s++; if(!strncmp(s, "//", 2))s+=2; continue; } if(*s=='\r')break; if(*s=='\n')break; *t++=*s++; } *t++=0; printf( "/*--\n" "Cat %s\n" "Text\n" "\t%s\n" "\t%s\n" "--*/\n", cat, buf3, buf4); } if(pds)continue; s=buf; if(*s<=' ')continue; while(symchar(*s))s++; while(*s && (*s<=' '))s++; if(!*s || (*s=='('))continue; while(*s=='*')s++; while(*s && symchar(*s))s++; if(*s!='(')continue; t=s; while(*s && (*s!='/'))s++; if(!strncmp(s, "//AH:", 5)) { // fprintf(stderr, "//AH tag %s %d\n", // argv[i], l); s+=5; while(*s && (*s<=' '))s++; if(!strcmp(s, "ignore"))continue; } s=t; if(*s=='(')s++; j=1; while(j) { if(!*s) { memset(buf2, 0, 256); fgets(buf2, 255, fd); l++; t=buf2; while(*t && (*t<=' '))t++; s2=t; t=s; while(*s2 && (*s2!='\n'))*t++=*s2++; *t++=' '; *t++=0; }else { if(*s=='(')j++; if(*s==')')j--; s++; } } t=s; while(*s && (*s!='/'))s++; if(!strncmp(s, "//AH:", 5)) { fprintf(stderr, "//AH tag %s %d\n", argv[i], l); s+=5; while(*s && (*s<=' '))s++; if(!strncmp(s, "ignore", 6))continue; } s=t; if(*s==')')s++; *s=0; if(doc) { printf( "/*--\n" "Cat %s\n" "Text\n" "\t%s;\n" "\t%s:%d\n" "--*/\n", cat, buf, argv[i], l); }else printf("%s;\n", buf); } fclose(fd); } return(0); }
void prx_def(char *buf) { char tb[256], tbn[256], tty[256]; char *s, *t, *t1, *sa; int ind; s=buf; t=tb; #ifdef _MSC_VER sprintf(t, "__declspec(dllexport) "); t+=strlen(t); #endif t1=tty; while(symchar(*s))*t1++=*s++; *t1++=0; sprintf(t, "%s", tty); t+=strlen(t); while(*s && (*s<=' '))*t++=*s++; ind=0; while(*s=='*') { *t++=*s++; ind++; } t1=tbn; while(*s && symchar(*s))*t1++=*s++; *t1++=0; sprintf(t, "(*_iproxy_%s)", tbn); t+=strlen(t); sa=s; while(*s)*t++=*s++; *t++=0; printf("%s;\n", tb); t=tb; sprintf(tb, "%s\n\t{ ", buf); t+=strlen(t); if(strcmp(tty, "void") || ind) { sprintf(t, "return "); t+=strlen(t); } sprintf(t, "_iproxy_%s(", tbn); t+=strlen(t); s=sa; while(s && *s!=')') { while(*s && (*s!=')') && (*s!=','))s++; while(!symchar(*(s-1)))s--; while(symchar(*(s-1)))s--; if(!strncmp(s, "void)", 5))break; while(symchar(*s))*t++=*s++; while(*s && (*s!=')') && (*s!=','))s++; if(*s==',') { s++; *t++=','; *t++=' '; } } sprintf(t, "); }\n"); t+=strlen(t); printf("%s\n", tb); }