📄 main.c
字号:
} if (severity<=FATAL) { program_state=FINISHED; fprintf(stderr,"---Immediate exit to system, due to a fatal error.\n"); end_it(); }#ifdef UNIX if (curinized && severity<=infolevel) reset_prog_mode();#endif}char *my_strndup(char *arg,int len) /* own version of strndup */{ char *copy; copy=my_malloc(len+1); strncpy(copy,arg,len); copy[len]='\0'; return copy;}char *my_strdup(char *arg) /* my own version of strdup, checks for failure */{ int l; if (!arg) return my_strndup("",0); l=strlen(arg); return my_strndup(arg,l);}void *my_malloc(unsigned num) /* Alloc memory and issue warning on failure */{ void *room; room=malloc(num+sizeof(int)); if (room==NULL) { sprintf(string,"Can't malloc %d bytes of memory",num); error(FATAL,string); } return room;}void my_free(void *mem) /* free memory */{ free(mem);} struct libfile_name *new_file(char *l,char *s) /* create a new structure for library names */{ struct libfile_name *new; struct libfile_name *curr; static struct libfile_name *last=NULL; int start,end; /* check, if library has already been included */ for(curr=libfile_stack[0];curr;curr=curr->next) { if (!strcmp(curr->l,l)) { if (is_bound) return curr; else return NULL; } } new=my_malloc(sizeof(struct libfile_name)); new->next=NULL; new->lineno=1; if (last) last->next=new; last=new; new->l=my_strdup(l); new->llen=strlen(new->l); if (s) { new->s=my_strdup(s); } else { /* no short name supplied get piece from l */ end=strlen(l); for(start=end;start>0;start--) { if (l[start-1]=='\\' || l[start-1]=='/') break; if (l[start]=='.') end=start; } end--; new->s=my_malloc(end-start+2); strncpy(new->s,new->l+start,end-start+1); new->s[end-start+1]='\0'; } new->slen=strlen(new->s); new->datapointer=new->firstdata=NULL; return new;}char *dotify(char *name,int addfun) /* add library name, if not already present */{ static char buff[200]; if (!strchr(name,'.')) { strcpy(buff,currlib->s); strcat(buff,"."); strcat(buff,name); } else { strcpy(buff,name); } if (addfun && !strchr(name,'@')) { strcat(buff,"@"); strcat(buff,current_function); } return buff;}char *strip(char *name) /* strip down to minimal name */{ static char buff[300]; char *at,*dot; if (infolevel>=DEBUG) return name; dot=strchr(name,'.'); if (dot) strcpy(buff,dot+1); else strcpy(buff,name); at=strchr(buff,'@'); if (at) *at='\0'; return buff;}void do_error(struct command *cmd) /* issue user defined error */{ struct stackentry *s; struct command *r; s=stackhead; while(s!=stackroot) { if (s->type==stRETADDCALL) { r=s->pointer; cmd->line=r->line; cmd->lib=r->lib; break; } s=s->prev; } error(ERROR,pop(stSTRING)->pointer);}void compile() /* create s subroutine at runtime */{ open_string(pop(stSTRING)->pointer); yyparse(); add_command(cEND,NULL);}void create_execute(int string) /* create command 'cEXECUTESUB' */{ struct command *cmd; cmd=add_command(string?cEXECUTE2:cEXECUTE,NULL); cmd->pointer=my_strdup(dotify("",FALSE));}void execute(struct command *cmd) /* execute a subroutine */{ struct stackentry *st,*ret; char *fullname,*shortname; struct command *newcurr; st=stackhead; do {st=st->prev;} while(st->type!=stFREE); st=st->next; if (st->type!=stSTRING) { error(ERROR,"need a string as a function name"); return; } shortname=st->pointer; if ((shortname[strlen(shortname)-1]=='$')!=(cmd->type==cEXECUTE2)) { if (cmd->type==cEXECUTE2) sprintf(string,"expecting the name of a string function (not '%s')",shortname); else sprintf(string,"expecting the name of a numeric function (not '%s')",shortname); error(ERROR,string); return; } fullname=my_malloc(strlen(cmd->pointer)+strlen(shortname)+2); strcpy(fullname,cmd->pointer); strcat(fullname,shortname); free(st->pointer); st->type=stFREE; newcurr=search_label(fullname,smSUB); if (!newcurr) { sprintf(string,"subroutine '%s' not defined",fullname); error(ERROR,string); return; } ret=push(); ret->pointer=current; ret->type=stRETADDCALL; reshufflestack(ret); current=newcurr; free(fullname);}void create_docu(char *doc) /* create command 'docu' */{ struct command *cmd; static struct command *previous=NULL; if (inlib) return; cmd=add_command(cDOCU,NULL); cmd->pointer=doc; if (previous) previous->nextassoc=cmd; else docuhead=cmd; previous=cmd; docucount++;}void create_docu_array(void) /* create array with documentation */{ struct array *ar; struct command *doc; int i; /* create and prepare docu-array */ ar=create_array('s',1); ar->bounds[0]=docucount+1; ar->pointer=my_malloc((docucount+1)*sizeof(char *)); ((char **)ar->pointer)[0]=my_strdup(""); doc=docuhead; i=1; while(doc) { ((char **)ar->pointer)[i]=doc->pointer; doc=doc->nextassoc; i++; } get_sym("main.docu$",syARRAY,amADD_GLOBAL)->pointer=ar;} int isbound(void) /* check if this interpreter is bound to a program */{ FILE *interpreter; int i; int c; int proglen=0; int bound=1; if (!interpreter_path || !interpreter_path[0]) { error(FATAL,"interpreter_path is not set !"); return 0; } if (!(interpreter=fopen(interpreter_path,"r"))) { sprintf(string,"Couldn't open '%s' to check, if it is bound: %s",interpreter_path,my_strerror(errno)); error(WARNING,string); return 0; } if (fseek(interpreter,0-strlen(YABMAGIC)-1,SEEK_END)) { sprintf(string,"Couldn't seek within '%s': %s",interpreter_path,my_strerror(errno)); error(WARNING,string); return 0; } for(i=0;i<strlen(YABMAGIC);i++) { c=fgetc(interpreter); if (c==EOF || c!=(YABMAGIC)[i]) bound=0; } if (!bound) { fclose(interpreter); return bound; } if (fseek(interpreter,0-strlen(YABMAGIC)-5-8-1,SEEK_END)) { sprintf(string,"Couldn't seek within '%s': %s",interpreter_path,my_strerror(errno)); error(WARNING,string); return 0; } if (!fscanf(interpreter,"%d",&proglen)) { error(WARNING,"Could not read length of embedded program"); return 0; } if (fseek(interpreter,0-strlen(YABMAGIC)-5-8-5-5-proglen,SEEK_END)) { sprintf(string,"Couldn't seek within '%s': %s",interpreter_path,my_strerror(errno)); error(WARNING,string); return 0; } if (infolevel>=NOTE) { error(NOTE,"Dumping the embedded program, that will be executed:"); fprintf(stderr," "); for(i=0;i<proglen;i++) { c=fgetc(interpreter); fprintf(stderr,"%c",c); if (c=='\n' && i<proglen-1) fprintf(stderr," "); } error(NOTE,"End of program, that will be executed"); if (fseek(interpreter,0-strlen(YABMAGIC)-5-8-5-5-proglen,SEEK_END)) { sprintf(string,"Couldn't seek within '%s': %s",interpreter_path,my_strerror(errno)); error(WARNING,string); return 0; } } bound_program=interpreter; return 1;}static int mybind(char *bound) /* bind a program to the interpreter and save it */{ FILE *fyab; FILE *fprog; FILE *fbound; FILE *flib; int c; char *pc; int i; int proglen=0; if (interactive) { error(ERROR,"cannot bind a program when called interactive"); return 0; } if (!strcmp(interpreter_path,bound)) { sprintf(string,"will not overwrite '%s' with '%s'",bound,interpreter_path); error(ERROR,string); return 0; } if (!strcmp(main_file_name,bound)) { sprintf(string,"will not overwrite '%s' with '%s'",bound,main_file_name); error(ERROR,string); return 0; } if (!(fyab=fopen(interpreter_path,"rb"))) { sprintf(string,"could not open '%s' for reading: %s",interpreter_path,my_strerror(errno)); error(ERROR,string); return 0; } if (!(fprog=fopen(main_file_name,"rb"))) { sprintf(string,"could not open '%s' for reading: %s",main_file_name,my_strerror(errno)); error(ERROR,string); fclose(fyab); return 0; } if (!(fbound=fopen(bound,"wb"))) { sprintf(string,"could not open '%s' for writing: %s",bound,my_strerror(errno)); error(ERROR,string); fclose(fyab); fclose(fprog); return 0; } if (infolevel>=DEBUG) { sprintf(string,"binding %s and %s into %s",interpreter_path,main_file_name,bound); error(NOTE,string); } while((c=fgetc(fyab))!=EOF) { fputc(c,fbound); } for (i=1;i<libfile_chain_length;i++) { if (!(flib=fopen(libfile_chain[i]->l,"rb"))) { sprintf(string,"could not open '%s' for reading: %s",libfile_chain[i]->l,my_strerror(errno)); error(ERROR,string); fclose(flib); return 0; } sprintf(string,"\nimport %s\n",libfile_chain[i]->s); for (pc=string;*pc;pc++) { fputc(*pc,fbound); proglen++; } while((c=fgetc(flib))!=EOF) { fputc(c,fbound); proglen++; } } for (pc="\nimport main\n";*pc;pc++) { fputc(*pc,fbound); proglen++; } for (pc="\nimport __END_OF_IMPORT\n";*pc;pc++) { fputc(*pc,fbound); proglen++; } while((c=fgetc(fprog))!=EOF) { fputc(c,fbound); proglen++; } fprintf(fbound,"\nend\n"); fprintf(fbound,"rem %08d\n",proglen); fprintf(fbound,"rem %s\n",YABMAGIC); fclose(fyab); fclose(fprog); fclose(fbound); return 1;}char *find_interpreter(char *name) /* find interpreter with full path */{ FILE *f; char *path=NULL;#ifdef WINDOWS path=my_malloc(1000); GetModuleFileName(NULL,path,1000); if (f=fopen(path,"r")) { fclose(f); return path; } else { my_free(path); return my_strdup(name); } #else if (f=fopen(name,"r")) { fclose(f); path=my_strdup(name); return path; } else { char *from,*to,*try; from=to=path=getenv("PATH"); try=my_malloc(strlen(path)+strlen(name)+2); to=strchr(to+1,':'); while(to) { strncpy(try,from,to-from); try[to-from]=0; if (try[to-from-1]!='/') strcat(try,"/"); strcat(try,name); if (f=fopen(try,"r")) { fclose(f); return try; } from=to+1; to=strchr(to+1,':'); } return name; }#endif} char *my_strerror(int err) { /* return description of error */#ifdef WINDOWS return strerror(err);#else#ifdef HAVE_STRERROR return strerror(err);#else char buff[100]; sprintf(buff,"errno=%d",err); return buff;#endif#endif}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -