bool OpenStringDestination( Environment *theEnv, const char *name, char *str, size_t maximumPosition) { struct stringRouter *newStringRouter; char *theName; if (FindStringRouter(theEnv,name) != NULL) return false; newStringRouter = get_struct(theEnv,stringRouter); theName = (char *) gm1(theEnv,strlen(name) + 1); genstrcpy(theName,name); newStringRouter->name = theName; newStringRouter->readString = NULL; newStringRouter->writeString = str; newStringRouter->currentPosition = 0; newStringRouter->readWriteType = WRITE_STRING; newStringRouter->maximumPosition = maximumPosition; newStringRouter->next = StringRouterData(theEnv)->ListOfStringRouters; StringRouterData(theEnv)->ListOfStringRouters = newStringRouter; return true; }
globle intBool AddRouter( char *routerName, int priority, int (*queryFunction)(char *), int (*printFunction)(char *,char *), int (*getcFunction)(char *), int (*ungetcFunction)(int,char *), int (*exitFunction)(int)) { struct router *newPtr, *lastPtr, *currentPtr; void *theEnv; char *nameCopy; theEnv = GetCurrentEnvironment(); newPtr = get_struct(theEnv,router); nameCopy = (char *) genalloc(theEnv,strlen(routerName) + 1); genstrcpy(nameCopy,routerName); newPtr->name = nameCopy; newPtr->active = TRUE; newPtr->environmentAware = FALSE; newPtr->priority = priority; newPtr->context = NULL; newPtr->query = (int (*)(void *,char *)) queryFunction; newPtr->printer = (int (*)(void *,char *,char *)) printFunction; newPtr->exiter = (int (*)(void *,int)) exitFunction; newPtr->charget = (int (*)(void *,char *)) getcFunction; newPtr->charunget = (int (*)(void *,int,char *)) ungetcFunction; newPtr->next = NULL; if (RouterData(theEnv)->ListOfRouters == NULL) { RouterData(theEnv)->ListOfRouters = newPtr; return(1); } lastPtr = NULL; currentPtr = RouterData(theEnv)->ListOfRouters; while ((currentPtr != NULL) ? (priority < currentPtr->priority) : FALSE) { lastPtr = currentPtr; currentPtr = currentPtr->next; } if (lastPtr == NULL) { newPtr->next = RouterData(theEnv)->ListOfRouters; RouterData(theEnv)->ListOfRouters = newPtr; } else { newPtr->next = currentPtr; lastPtr->next = newPtr; } return(1); }
globle intBool EnvAddRouterWithContext( void *theEnv, EXEC_STATUS, char *routerName, int priority, int (*queryFunction)(void *,EXEC_STATUS,char *), int (*printFunction)(void *,EXEC_STATUS,char *,char *), int (*getcFunction)(void *,EXEC_STATUS,char *), int (*ungetcFunction)(void *,EXEC_STATUS,int,char *), int (*exitFunction)(void *,EXEC_STATUS,int), void *context) { struct router *newPtr, *lastPtr, *currentPtr; char *nameCopy; newPtr = get_struct(theEnv,execStatus,router); nameCopy = (char *) genalloc(theEnv,execStatus,strlen(routerName) + 1); genstrcpy(nameCopy,routerName); newPtr->name = nameCopy; newPtr->active = TRUE; newPtr->environmentAware = TRUE; newPtr->context = context; newPtr->priority = priority; newPtr->query = queryFunction; newPtr->printer = printFunction; newPtr->exiter = exitFunction; newPtr->charget = getcFunction; newPtr->charunget = ungetcFunction; newPtr->next = NULL; if (RouterData(theEnv,execStatus)->ListOfRouters == NULL) { RouterData(theEnv,execStatus)->ListOfRouters = newPtr; return(1); } lastPtr = NULL; currentPtr = RouterData(theEnv,execStatus)->ListOfRouters; while ((currentPtr != NULL) ? (priority < currentPtr->priority) : FALSE) { lastPtr = currentPtr; currentPtr = currentPtr->next; } if (lastPtr == NULL) { newPtr->next = RouterData(theEnv,execStatus)->ListOfRouters; RouterData(theEnv,execStatus)->ListOfRouters = newPtr; } else { newPtr->next = currentPtr; lastPtr->next = newPtr; } return(1); }
globle char *CopyPPBuffer( void *theEnv, EXEC_STATUS) { size_t length; char *newString; length = (1 + strlen(PrettyPrintData(theEnv,execStatus)->PrettyPrintBuffer)) * (int) sizeof (char); newString = (char *) gm2(theEnv,execStatus,length); genstrcpy(newString,PrettyPrintData(theEnv,execStatus)->PrettyPrintBuffer); return(newString); }
void SetCommandString( void *theEnv, const char *str) { size_t length; FlushCommandString(theEnv); length = strlen(str); CommandLineData(theEnv)->CommandString = (char *) genrealloc(theEnv,CommandLineData(theEnv)->CommandString,(unsigned) CommandLineData(theEnv)->MaximumCharacters, (unsigned) CommandLineData(theEnv)->MaximumCharacters + length + 1); genstrcpy(CommandLineData(theEnv)->CommandString,str); CommandLineData(theEnv)->MaximumCharacters += (length + 1); RouterData(theEnv)->CommandBufferInputCount += (int) length; }
SaveCallFunctionItem *AddSaveFunctionToCallList( Environment *theEnv, const char *name, int priority, SaveCallFunction *func, struct saveCallFunctionItem *head, void *context) { struct saveCallFunctionItem *newPtr, *currentPtr, *lastPtr = NULL; char *nameCopy; newPtr = get_struct(theEnv,saveCallFunctionItem); nameCopy = (char *) genalloc(theEnv,strlen(name) + 1); genstrcpy(nameCopy,name); newPtr->name = nameCopy; newPtr->func = func; newPtr->priority = priority; newPtr->context = context; if (head == NULL) { newPtr->next = NULL; return(newPtr); } currentPtr = head; while ((currentPtr != NULL) ? (priority < currentPtr->priority) : false) { lastPtr = currentPtr; currentPtr = currentPtr->next; } if (lastPtr == NULL) { newPtr->next = head; head = newPtr; } else { newPtr->next = currentPtr; lastPtr->next = newPtr; } return(head); }
bool OpenStringBuilderDestination( Environment *theEnv, const char *name, StringBuilder *theSB) { StringBuilderRouter *newStringRouter; char *theName; if (FindStringBuilderRouter(theEnv,name) != NULL) return false; newStringRouter = get_struct(theEnv,stringBuilderRouter); theName = (char *) gm1(theEnv,strlen(name) + 1); genstrcpy(theName,name); newStringRouter->name = theName; newStringRouter->SBR = theSB; newStringRouter->next = StringRouterData(theEnv)->ListOfStringBuilderRouters; StringRouterData(theEnv)->ListOfStringBuilderRouters = newStringRouter; return true; }
globle int OpenAFile( void *theEnv, char *fileName, char *accessMode, char *logicalName) { FILE *newstream; struct fileRouter *newRouter; /*==================================*/ /* Make sure the file can be opened */ /* with the specified access mode. */ /*==================================*/ if ((newstream = GenOpen(theEnv,fileName,accessMode)) == NULL) { return(FALSE); } /*===========================*/ /* Create a new file router. */ /*===========================*/ newRouter = get_struct(theEnv,fileRouter); newRouter->logicalName = (char *) gm2(theEnv,strlen(logicalName) + 1); genstrcpy(newRouter->logicalName,logicalName); newRouter->stream = newstream; /*==========================================*/ /* Add the newly opened file to the list of */ /* files associated with logical names. */ /*==========================================*/ newRouter->next = FileRouterData(theEnv)->ListOfFileRouters; FileRouterData(theEnv)->ListOfFileRouters = newRouter; /*==================================*/ /* Return TRUE to indicate the file */ /* was opened successfully. */ /*==================================*/ return(TRUE); }
globle int OpenStringDestination( void *theEnv, char *name, char *str, size_t maximumPosition) { struct stringRouter *newStringRouter; if (FindStringRouter(theEnv,name) != NULL) return(0); newStringRouter = get_struct(theEnv,stringRouter); newStringRouter->name = (char *) gm1(theEnv,(int) strlen(name) + 1); genstrcpy(newStringRouter->name,name); newStringRouter->str = str; newStringRouter->currentPosition = 0; newStringRouter->readWriteType = WRITE_STRING; newStringRouter->maximumPosition = maximumPosition; newStringRouter->next = StringRouterData(theEnv)->ListOfStringRouters; StringRouterData(theEnv)->ListOfStringRouters = newStringRouter; return(1); }
static int CreateReadStringSource( void *theEnv, char *name, char *str, size_t currentPosition, size_t maximumPosition) { struct stringRouter *newStringRouter; if (FindStringRouter(theEnv,name) != NULL) return(0); newStringRouter = get_struct(theEnv,stringRouter); newStringRouter->name = (char *) gm1(theEnv,strlen(name) + 1); genstrcpy(newStringRouter->name,name); newStringRouter->str = str; newStringRouter->currentPosition = currentPosition; newStringRouter->readWriteType = READ_STRING; newStringRouter->maximumPosition = maximumPosition; newStringRouter->next = StringRouterData(theEnv)->ListOfStringRouters; StringRouterData(theEnv)->ListOfStringRouters = newStringRouter; return(1); }
static void PerformEditCommand( void *theEnv) { register int c; register int f; register int n; register int mflag; register int rtn_flag; char bname[NBUFN]; int num_a; char *fileName = NULL; DATA_OBJECT arg_ptr; /*====================*/ /* Get the file name. */ /*====================*/ if ((num_a = EnvArgCountCheck(theEnv,"edit",NO_MORE_THAN,1)) == -1) return; if (num_a == 1) { if (EnvArgTypeCheck(theEnv,"edit",1,SYMBOL_OR_STRING,&arg_ptr) == FALSE) return; fileName = DOToString(arg_ptr); } if(bheadp == NULL) { /**********************************************/ /* Initial entry, set up buffers and pointers */ /**********************************************/ genstrcpy(bname, "main"); /* Work out the name of */ if (num_a > 0) /* the default buffer. */ makename(bname,fileName); edinit(theEnv,bname); /* Buffers, windows. */ vtinit(theEnv); /* Displays. */ if (num_a > 0) { update(); /* You have to update */ readin(theEnv,fileName); /* in case "[New file]" */ } init_cmp_router(theEnv); /* Prepare the compile */ EnvDeactivateRouter(theEnv,"cmp_router"); /* router. */ } else { /**********************************************************/ /* Return from temporary exit, reset necessary stuff only */ /**********************************************************/ (*term.t_open)(); if (num_a > 0) { filevisit_guts(theEnv,fileName); } } sgarbf = TRUE; /* Force screen update */ lastbufn[0] = '\0'; /* Make sure last name */ /* is cleared out */ lastflag = 0; /* Fake last flags. */ loop: update(); /* Fix up the screen */ c = getkey(); if (mpresf != FALSE) { mlerase(); update(); if (c == ' ') /* ITS EMACS does this */ goto loop; } f = FALSE; n = 1; if (c == (COTL|'U')) { /* ^U, start argument */ f = TRUE; n = 4; /* with argument of 4 */ mflag = 0; /* that can be discarded. */ mlwrite("Arg: 4"); while ((((c=getkey()) >='0') && (c<='9')) || (c==(COTL|'U')) || (c=='-')){ if (c == (COTL|'U')) n = n*4; /* * If dash, and start of argument string, set arg. * to -1. Otherwise, insert it. */ else if (c == '-') { if (mflag) break; n = 0; mflag = -1; } /* * If first digit entered, replace previous argument * with digit and set sign. Otherwise, append to arg. */ else { if (!mflag) { n = 0; mflag = 1; } n = 10*n + c - '0'; } mlwrite("Arg: %d", (mflag >=0) ? n : (n ? -n : -1)); } /* * Make arguments preceded by a minus sign negative and change * the special argument "^U -" to an effective "^U -1". */ if (mflag == -1) { if (n == 0) n++; n = -n; } } if (c == (COTL|'X')) /* ^X is a prefix */ c = CTLX | getctl(); if (kbdmip != NULL) { /* Save macro strokes. */ if (c!=(CTLX|')') && kbdmip>&kbdm[NKBDM-6]) { ctrlg(theEnv,FALSE, 0); goto loop; } if (f != FALSE) { *kbdmip++ = (COTL|'U'); *kbdmip++ = n; } *kbdmip++ = c; } rtn_flag = execute(theEnv,c, f, n); /* Do it. */ if(rtn_flag == EXIT) return; else goto loop; }
int main (int argc, char *argv[]) { int arg, i, ct, *inv, old_ndiff, maxneweqns, numeqns, ngens, maxwdiffs; fsa diff2, genmult; char gpname[100], cosgpname[100], inf1[100], inf2[100], inf3[100], outf[100], outfwg[100], outfec[100], fsaname[100]; fsa *wd_fsa; /* This is for doing word-reductions in the case that we * correct the diff2 machine */ fsa wa; /* The word-acceptor in the wtlex case */ int weight[MAXGEN+1]; /* The weights of the generators in the wtlex case */ gen testword[MAXREDUCELEN]; /* for word reduction */ char **names; /* generator names in case we need to output words */ reduction_equation *eqnptr; reduction_struct rs_wd; storage_type ip_store = DENSE; int dr = 0; boolean seengpname, seencosname; boolean cosets=FALSE; boolean wtlex=FALSE; rewriting_system rws; boolean outputwords = FALSE; int separator=0; setbuf(stdout,(char*)0); setbuf(stderr,(char*)0); rws.maxeqns = MAXRWSEQNS; maxneweqns = MAXNEWEQNS; maxwdiffs = MAXWDIFFS; inf1[0] = '\0'; inf2[0] = '\0'; outf[0] = '\0'; arg = 1; seengpname=seencosname=FALSE; while (argc > arg) { if (strcmp(argv[arg],"-ip")==0) { arg++; if (arg >= argc) badusage_gpcheckmult(); if (strcmp(argv[arg],"d")==0) ip_store = DENSE; else if (argv[arg][0] == 's') { ip_store = SPARSE; if (stringlen(argv[arg]) > 1) dr = atoi(argv[arg]+1); } else badusage_gpcheckmult(); } else if (strcmp(argv[arg],"-silent")==0) kbm_print_level = 0; else if (strcmp(argv[arg],"-v")==0) kbm_print_level = 2; else if (strcmp(argv[arg],"-vv")==0) kbm_print_level = 3; else if (strcmp(argv[arg],"-l")==0) kbm_large = TRUE; else if (strcmp(argv[arg],"-h")==0) kbm_huge = TRUE; else if (strcmp(argv[arg],"-ow")==0) outputwords = TRUE; else if (strncmp(argv[arg],"-cos",4)==0) cosets = TRUE; else if (strcmp(argv[arg],"-m")==0) { arg++; if (arg >= argc) badusage_gpcheckmult(); maxneweqns = atoi(argv[arg]); } else if (strcmp(argv[arg],"-mwd")==0) { arg++; if (arg >= argc) badusage_gpcheckmult(); maxwdiffs = atoi(argv[arg]); } else if (strcmp(argv[arg],"-wtlex")==0) wtlex = TRUE; else if (argv[arg][0] == '-') badusage_gpcheckmult(); else if (!seengpname) { seengpname=TRUE; strcpy(gpname,argv[arg]); } else if (!seencosname) { seencosname=TRUE; sprintf(cosgpname,"%s.%s",gpname,argv[arg]); } else badusage_gpcheckmult(); arg++; } if (!seengpname) badusage_gpcheckmult(); if (cosets && wtlex) { fprintf(stderr, "Sorry: -cos and -wtlex options cannot be used together.\n"); badusage_gpcheckmult(); } if (cosets && !seencosname) sprintf(cosgpname,"%s.cos",gpname); if (cosets) strcpy(inf1,cosgpname); else strcpy(inf1,gpname); strcpy(inf2,inf1); strcat(inf1,".gm"); if (cosets) sprintf(outfec,"%s.cm.ec",cosgpname); else sprintf(outfec,"%s.cm.ec",gpname); if ((rfile = fopen(inf1,"r")) == 0) { fprintf(stderr,"Cannot open file %s.\n",inf1); exit(1); } fsa_read(rfile,&genmult,ip_store,dr,0,TRUE,fsaname); fclose(rfile); tmalloc(eqnptr,reduction_equation,maxneweqns) if (cosets) separator=rs_wd.separator=genmult.alphabet->base->size+1; if ((numeqns = fsa_checkmult(&genmult,eqnptr,maxneweqns,cosets,separator)) > 0) { /* A multiplier was not valid, so groupname.(mi)diff2 will need updating. */ if (outputwords) { /* We do not update gpname.diff2, but output the offending words. */ if (cosets) strcpy(outfwg,cosgpname); else strcpy(outfwg,gpname); strcat(outfwg,".wg"); wfile=fopen(outfwg,"w"); base_prefix(fsaname); fprintf(wfile,"%s.wg := [\n",fsaname); names=genmult.alphabet->base->names; for (i=0;i<numeqns;i++) { strcpy(kbm_buffer," ["); if (cosets) add_word_to_buffer(wfile,eqnptr[i].lhs+1,names); else add_word_to_buffer(wfile,eqnptr[i].lhs,names); strcat(kbm_buffer,","); add_word_to_buffer(wfile,eqnptr[i].rhs,names); if (i<numeqns-1) strcat(kbm_buffer,"],"); else strcat(kbm_buffer,"]"); fprintf(wfile,"%s\n",kbm_buffer); } fprintf(wfile,"];\n"); fclose(wfile); fsa_clear(&genmult); for (i=0;i<numeqns;i++) { tfree(eqnptr[i].lhs); tfree(eqnptr[i].rhs); } tfree(eqnptr); wfile=fopen(outfec,"w"); fprintf(wfile,"_ExitCode := 2;\n"); fclose(wfile); exit(2); } fsa_clear(&genmult); if (cosets) strcat(inf2,".midiff2"); else strcat(inf2,".diff2"); strcpy(outf,inf2); if (kbm_print_level>1) printf(" #Altering wd-machine to make it accept new equations.\n"); if ((rfile = fopen(inf2,"r")) == 0) { fprintf(stderr,"Cannot open file %s.\n",inf2); exit(1); } /* We read groupname.(mi)diff2 into diff2, and then copy it into * wd_fsa. The copy is used for reducing words - Not surprisingly, * we get problems if we try to alter it while using it at * the same time! */ fsa_read(rfile,&diff2,DENSE,0,maxwdiffs,TRUE,fsaname); fclose(rfile); tmalloc(wd_fsa,fsa,1); fsa_copy(wd_fsa,&diff2); if (wtlex) { /* we have to read in the weights from the group file */ if ((rfile = fopen(gpname,"r")) == 0) { fprintf(stderr,"Cannot open file %s.\n",gpname); exit(1); } read_kbinput_simple(rfile,TRUE,&rws); fclose(rfile); /* we only need the weights, which we can simply copy */ for (i=1;i<=rws.num_gens;i++) weight[i] = rws.weight[i]; weight[rws.num_gens+1]=0; /* padding symbol */ rws_clear(&rws); strcpy(inf3,gpname); strcat(inf3,".wa"); if ((rfile = fopen(inf3,"r")) == 0) { fprintf(stderr,"Cannot open file %s.\n",inf3); exit(1); } fsa_read(rfile,&wa,DENSE,0,0,TRUE,fsaname); fclose(rfile); } rs_wd.wd_fsa = wd_fsa; reduce_word = cosets ? diff_reduce_cos : wtlex ? diff_reduce_wl : diff_reduce; if (fsa_table_dptr_init(wd_fsa)==-1) return -1; if (fsa_table_dptr_init(&diff2)== -1) return -1; if (cosets){ tmalloc(diff2.is_initial,boolean,maxwdiffs+1); for (i=1;i<=maxwdiffs;i++) diff2.is_initial[i]=FALSE; for (i=1;i<=diff2.num_initial;i++) diff2.is_initial[diff2.initial[i]]=TRUE; } else if (wtlex){ rs_wd.wa = &wa; rs_wd.weight = weight; rs_wd.maxreducelen = MAXREDUCELEN; } /* We need to know the inverses of generators - let's just work them out! */ ngens = diff2.alphabet->base->size; if (calculate_inverses(&inv,ngens,&rs_wd)==-1) return -1; old_ndiff = diff2.states->size; /* Now add the new equations * The right hand side of the equation to be added will be the reduction of * the lhs times the generator which is currently in the rhs. */ for (i=0;i<numeqns;i++) { genstrcat(eqnptr[i].lhs,eqnptr[i].rhs); tfree(eqnptr[i].rhs); genstrcpy(testword,eqnptr[i].lhs); reduce_word(testword,&rs_wd); tmalloc(eqnptr[i].rhs,gen,genstrlen(testword)+1); genstrcpy(eqnptr[i].rhs,testword); if (cosets) { if (add_wd_fsa_cos(&diff2,eqnptr+i,inv,TRUE,&rs_wd)== -1) exit(1); } else if (add_wd_fsa(&diff2,eqnptr+i,inv,TRUE,&rs_wd)== -1) exit(1); } if (cosets) { tfree(diff2.initial); tmalloc(diff2.initial,int,diff2.num_initial+1); ct=0; for (i=1;i<=diff2.states->size;i++) if (diff2.is_initial[i]) diff2.initial[++ct]=i; tfree(diff2.is_initial); make_full_wd_fsa_cos(&diff2,inv,old_ndiff+1,&rs_wd); } else make_full_wd_fsa(&diff2,inv,old_ndiff+1,&rs_wd); if (kbm_print_level>1) printf(" #Word-difference machine now has %d states.\n", diff2.states->size); wfile = fopen(inf2,"w"); fsa_print(wfile,&diff2,fsaname); fclose(wfile); tfree(inv); fsa_clear(wd_fsa); fsa_clear(&diff2); tfree(wd_fsa); if (wtlex) fsa_clear(&wa); for (i=0;i<numeqns;i++) { tfree(eqnptr[i].lhs); tfree(eqnptr[i].rhs); } tfree(eqnptr); wfile=fopen(outfec,"w"); fprintf(wfile,"_ExitCode := 2;\n"); fclose(wfile); exit(2); }
bool EnvAddRouterWithContext( void *theEnv, const char *routerName, int priority, bool (*queryFunction)(void *,const char *), int (*printFunction)(void *,const char *,const char *), int (*getcFunction)(void *,const char *), int (*ungetcFunction)(void *,int,const char *), int (*exitFunction)(void *,int), void *context) { struct router *newPtr, *lastPtr, *currentPtr; char *nameCopy; /*==================================================*/ /* Reject the router if the name is already in use. */ /*==================================================*/ for (currentPtr = RouterData(theEnv)->ListOfRouters; currentPtr != NULL; currentPtr = currentPtr->next) { if (strcmp(currentPtr->name,routerName) == 0) { return 0; } } newPtr = get_struct(theEnv,router); nameCopy = (char *) genalloc(theEnv,strlen(routerName) + 1); genstrcpy(nameCopy,routerName); newPtr->name = nameCopy; newPtr->active = true; newPtr->context = context; newPtr->priority = priority; newPtr->query = queryFunction; newPtr->printer = printFunction; newPtr->exiter = exitFunction; newPtr->charget = getcFunction; newPtr->charunget = ungetcFunction; newPtr->next = NULL; if (RouterData(theEnv)->ListOfRouters == NULL) { RouterData(theEnv)->ListOfRouters = newPtr; return(1); } lastPtr = NULL; currentPtr = RouterData(theEnv)->ListOfRouters; while ((currentPtr != NULL) ? (priority < currentPtr->priority) : false) { lastPtr = currentPtr; currentPtr = currentPtr->next; } if (lastPtr == NULL) { newPtr->next = RouterData(theEnv)->ListOfRouters; RouterData(theEnv)->ListOfRouters = newPtr; } else { newPtr->next = currentPtr; lastPtr->next = newPtr; } return(1); }