static const char * sh_read (lua_State *L,void *data,size_t *size){ struct reader *r = data; if(r->current)sdsfree(r->current); r->current = NULL; while(!(r->current)){ *size = 0; if(feof(r->in)) { return NULL; } r->current = compile_line(r->in); } *size = r->current?sdslen(r->current):0; //printf("%s",r->current); return r->current; }
static int compile_dictlist_file(const char *path, const char* filename) {//===================================================================== int length; int hash; char *p; int count=0; FILE *f_in; char buf[200]; char fname[sizeof(path_home)+45]; char dict_line[128]; text_mode = 0; sprintf(fname,"%s%s",path,filename); if((f_in = fopen(fname,"r")) == NULL) return(-1); fprintf(f_log,"Compiling: '%s'\n",fname); linenum=0; while(fgets(buf,sizeof(buf),f_in) != NULL) { linenum++; length = compile_line(buf,dict_line,&hash); if(length == 0) continue; /* blank line */ hash_counts[hash]++; p = (char *)malloc(length+sizeof(char *)); if(p == NULL) { if(f_log != NULL) { fprintf(f_log,"Can't allocate memory\n"); error_count++; } break; } memcpy(p,&hash_chains[hash],sizeof(char *)); hash_chains[hash] = p; memcpy(p+sizeof(char *),dict_line,length); count++; } fprintf(f_log,"\t%d entries\n",count); fclose(f_in); return(0); } /* end of compile_dictlist_file */
main(int argc, char *argv[]) { // make sure the filename is specified if (argc < 2) { printf("Please specify the name of the file to compile\n"); return; } char *name = argv[1]; char funName[20]; char newFileName[20]; char line[110]; // create the symbol table if (create_symbol_table(name, funName) == 0) { exit(0); } // create the target file int i = 0; while(name[i] != '.'){ newFileName[i] = name[i]; i++; } newFileName[i] = '.'; newFileName[i + 1] = 'l'; newFileName[i + 2] = 'c'; newFileName[i + 3] = '3'; newFileName[i + 4] = '\0'; FILE *output = fopen(newFileName, "w"); if(output == NULL) exit(0); //compile the file FILE *infile = open_file(name); fgets(line, 110, infile); generate_prologue(funName, output); while(!feof(infile)){ fgets(line, 110, infile); compile_line(line, output); } generate_epilogue(output); }
int main(int argc,const char* const* argv){ int n; struct reader re = {NULL,NULL}; if(argc<2) { printf("usage: %s <script> args...\n",argv[0]); return 1; } lua_State *L = luaL_newstate(); luaL_openlibs(L); sh_install(L); sh_install2(L); re.in = fopen(argv[1],"r"); if(!re.in)return 1; //luaL_loadfile(L,argv[1]); lua_load(L,sh_read,&re,argv[1],"bt"); for(n=1;n<argc;++n){ lua_pushstring(L,argv[n]); } lua_pcall(L,n-1,0,0); return 0; size_t pos[2]; int count=0; for(;;){ /* if(!fgets(buffer,sizeof(buffer),stdin))return 0; pos[1]=0; while(next_token(buffer,pos)){ if(count++>10)break; sds str = sdsnewlen(buffer+pos[0],pos[1]-pos[0]); printf("%d: (%d,%d) %s\n", count, (int)(pos[0]), (int)(pos[1]) ,str); } break; */ sds str = compile_line(stdin); if(!str)continue; printf("%s\n",str); sdsfree(str); } }