STATIC struct block * new_block() { register struct block *pblock = maxblocklist - 1; if (!maxblocklist || !(pblock->b_flags & PARTLY)) { /* * There is no last block, or it was filled completely, * so allocate a new blockheader. */ register int siz; pblock = blocklist; if (maxblocklist == topblocklist) { /* * No blockheaders left. Allocate new ones */ siz = topblocklist - pblock; blocklist = pblock = (struct block *) re_alloc((char *) pblock, (unsigned) (siz * sizeof(*pblock)), (unsigned) ((siz + CHUNK) * sizeof(*pblock))); pblock += siz; topblocklist = pblock + CHUNK; maxblocklist = pblock; for (; pblock < topblocklist; pblock++) { pblock->b_end = 0; pblock->b_info = 0; pblock->b_flags = 0; } if (!siz) { /* * Create dummy header cell. */ maxblocklist++; } } pblock = maxblocklist++; } nextblock(pblock); return pblock; }
void *ckmalloc(size_t size) { char *p,*ptr; char *tmp=ptr_start; char line[8]; int i; ptr=(char *)get_curr_pos(); /* add 1 byte for NULL character*/ p=ptr+size+1; BOUNDARY_4X(p); /*if out of share memory then re-allocate share memory*/ if (p>=tmp+SHARESIZE) { //printf ("out of memory...realloc memory....\n"); return ERR_NO_MEM; re_alloc(); ptr=(char *)get_curr_pos(); p=ptr+size+1; BOUNDARY_4X(p); /*if still out of share memory then return NULL*/ if (p>=tmp+SHARESIZE) { //printf ("out of memory...can't alloc memory...\n"); return NULL; } } tmp+=(strlen(MAGIC_ID)+1); i=atol(tmp); i+=(size+1); BOUNDARY_4X(i); sprintf(line,"%d-",i); line[7]=0; strcpy(tmp,line); // p=(char *)pointer; pointer=(void *)p; return (void *)ptr; }
char *get_next_line(const int fd) { int i; int size; static char buf[BUF_SIZE]; char *str; i = 0; size = 0; if ((str = malloc(sizeof(char))) == NULL) my_error("Error with an allocation"); str[0] = '\0'; while ((size = read(fd, buf, BUF_SIZE)) > 0 && buf[0] != '\n') { str = re_alloc(str); str[i] = buf[0]; i++; } if (size == 0 && i == 0) return (NULL); else return (str); }
char * newname(const char *name) { char *buf; unsigned i, j, k; struct stat st; buf = re_alloc(NULL, strlen(name)+10); i = 0; for (i=1; i; i+=i) { sprintf(buf, "%s.~%02u~", name, i-1); if (stat(buf, &st)) break; } fprintf(stderr, "newname i=%d\n", i); j = i>>1; while (j<i) { k = (i+j)>>1; sprintf(buf, "%s.~%02u~", name, k); if (stat(buf, &st)) i = k; else j = k+1; } /* uh oh .. bug found */ sprintf(buf, "%s.~%02u~", name, j); fprintf(stderr, "newname %s\n", buf); errno = 0; if (!stat(buf, &st) || errno!=ENOENT) ex("new"); return buf; }
static int readparticles(int check, int ugForce ) { char *ss,*endstr; char fullname[60], massname[60], imassname[60], p1[60], p2[60],numtxt[20]; char latex[STRSIZ], latex_[STRSIZ], s[60],c[60], chlp[40]; int itmp,i,j, errcode,np1,np2,nparticleLimit =128; linelist ln=prtcls_tab.strings; tabName=prtcls_tab.headln; if(prtclbase) { cleardecaylist(); clearLatexNames(); free(prtclbase);} prtclbase=(prtcl_base*) malloc(nparticleLimit*sizeof(prtcl_base)); prtclbase1=prtclbase-1; nparticles = 0; for(i=nparticles;i<nparticleLimit;i++) {prtclbase[i].top=NULL;prtclbase[i].latex=NULL;} nLine=1; while (ln != NULL) { ss=ln->line; if (nparticles >= nparticleLimit-16) { nparticleLimit+=128; prtclbase=re_alloc(prtclbase,nparticleLimit*sizeof(prtcl_base)); if(!prtclbase) { errorMessage(" P ","too many particles"); return 0; } prtclbase1=prtclbase-1; for(i=nparticles;i<nparticleLimit;i++) {prtclbase[i].top=NULL;prtclbase[i].latex=NULL;} } sscanf(ss,"%[^|]%*c%[^|]%*c%[^|]%*c%[^|]%*c%[^|]%*c%[^|]%*c%[^|]%*c%[^|]%*c%[^|]%*c%[^|]%*c%[^|]", fullname,p1,p2,numtxt,s,massname,imassname,c,chlp,latex,latex_); trim(p1); trim(p2); trim(latex); trim(latex_); { static char fldName[2][5]={" P "," aP"}; char * pName[2]; pName[0]=p1; pName[1]=p2; for ( i=0;i<=1;i++) { if (check && (! isPrtclName(pName[i]))) { errorMessage(fldName[i],"incorrect particle name '%s'",pName[i]); return 0; } if (check ) { locateinbase(pName[i],&j); if (j != 0) { errorMessage(fldName[i],"duplicate particle name '%s'",pName[i]); return 0; } } } } nparticles++; strcpy(prtclbase[nparticles-1].name,p1); itmp=strtol(trim(s),&endstr,10); if(check) { if (s+strlen(s) != endstr) { errorMessage("2*spin","number expected"); return 0 ; } if((itmp!=0)&&(itmp!=1)&&(itmp!=2)&&(itmp!=3)&&(itmp!=4)) { errorMessage("2*spin","value out of range"); return 0; } } prtclbase[nparticles-1].spin=itmp; if( 1!=sscanf(numtxt,"%ld",&prtclbase[nparticles-1].N)) { errorMessage("number","can't read MC numeration parameter"); return 0; } trim(massname); if(strcmp(massname,"0")==0) { if(prtclbase[nparticles-1].spin==3 || prtclbase[nparticles-1].spin==4) errorMessage("mass","spin 3/2 and spin 2 particles should be massive"); } else { int pos; errcode=findvar(massname,NULL,&pos); if (check && (errcode != 0)) { errorMessage("mass","unknown variable %s",massname); return 0; }else { if(pos>nCommonVars) nCommonVars=pos; } } strcpy(prtclbase[nparticles-1].massidnt,massname); trim(imassname); if( check && strcmp(imassname,"0")&& !strcmp(massname,"0")) { errorMessage("width","non zero width for zero mass particle '%s'", prtclbase[nparticles-1].name); return 0; } if(strcmp(imassname,"0") != 0) { errcode=findvar(imassname,NULL,NULL); if(errcode) { if(imassname[0]=='!') { imassname[0]=' '; trim(imassname); if(check && (!isVarName(imassname)) ) { errorMessage("width","incorrect name '%s'",imassname); return 0; } if(check && (!isOriginName(imassname)) ) { errorMessage("width","this identifier '%s' already was used",imassname); return 0; } { varlist mvars=modelvars+1+nmodelvar; nmodelvar++; strcpy(mvars->varname,imassname); mvars->func = NULL; mvars->varvalue = 0.; mvars->need=0; mvars->hidden=0; mvars->pwidth=nparticles; } }else { errorMessage("width","unknown variable %s",imassname); return 0; } } } strcpy(prtclbase[nparticles-1].imassidnt,imassname); itmp=strtol(trim(c),&endstr,10); if(check) { if (c+strlen(c) != endstr) { errorMessage("color","number expected"); return 0; } if (((itmp!=1)&&(itmp!=3)&&(itmp!=8))||((itmp==3)&&(strcmp(p1,p2)==0)) ) { errorMessage("color","value out of range"); return 0; } } prtclbase[nparticles-1].cdim=itmp; trim(chlp); if (strcmp(chlp,"") == 0) strcpy(chlp," "); prtclbase[nparticles-1].hlp = toupper(chlp[0]); if(check) { int ner; ner=1; switch(prtclbase[nparticles-1].hlp) { case ' ':if(prtclbase[nparticles-1].spin==2 && !strcmp(prtclbase[nparticles-1].massidnt,"0") ) { errorMessage("aux","Massless vector boson must\n" " be a gauge particle"); return 0; } break; case 'L': case 'R': if ((prtclbase[nparticles-1].spin !=1) ||((prtclbase[nparticles-1].massidnt[0])!='0') ||(strcmp(p1,p2)==0)) ner=0; break; case '*': if(prtclbase[nparticles-1].massidnt[0]=='0') ner=0; else if(prtclbase[nparticles-1].imassidnt[0]!='0') { errorMessage("aux","for aux='*' zero width is expected"); return 0;} break; case 'G': if(prtclbase[nparticles-1].spin!=2) ner=0; break; default: ner=0; } if(!ner){ errorMessage("aux","unexpeted character"); return 0;} if(prtclbase[nparticles-1].N==0 && prtclbase[nparticles-1].hlp!='*') { errorMessage("number","Zero PDG code."); return 0;} } prtclbase[nparticles-1].latex=malloc(1+strlen(latex)); strcpy(prtclbase[nparticles-1].latex,latex); np1 = ghostaddition(); if (strcmp(p1,p2) == 0) prtclbase[np1-1].anti = np1; else { ++(nparticles); prtclbase[nparticles-1] = prtclbase[np1-1]; prtclbase[nparticles-1].N *=(-1); strcpy(prtclbase[nparticles-1].name,p2); prtclbase[nparticles-1].latex=malloc(1+strlen(latex_)); strcpy(prtclbase[nparticles-1].latex,latex_); if (prtclbase[np1-1].cdim == 3) prtclbase[nparticles-1].cdim = -3; np2=ghostaddition(); prtclbase[np1-1].anti = np2; prtclbase[np2-1].anti = np1; } ln=ln->next; nLine++; } for (i = 1; i <= nparticles; i++) { prtcl_base *with1 = &prtclbase[i-1]; with1->top = NULL; if (strchr("fcCtT",with1->hlp) != NULL) { sprintf(with1->name+strlen(with1->name),".%c",with1->hlp); switch (with1->hlp) { case 'c': with1->anti = prtclbase[i-1 - 1].anti +2; break; case 'C': with1->anti = prtclbase[i-1 - 2].anti +1; break; case 'f': with1->anti = prtclbase[i-1 - 3].anti +3; break; case 't': with1->anti = prtclbase[i-1 + 1].anti -1; break; case 'T': with1->anti = prtclbase[i-1 + 2].anti -2; break; } } } if(ugForce)for(i=1; i <= nparticles; i++) if(gaugep(i) && (!zeromass(i))) prtclbase[i-1].hlp=' '; for(i=1;i<=nmodelvar;i++) if(modelvars[i].pwidth) modelvars[i].pwidth=ghostmother(modelvars[i].pwidth); return 1; }
void increaseVars( polyvars * v) { int oldsize=ALIG(v->nvar); if(++v->nvar > oldsize) v->vars=re_alloc(v->vars,ALIG(v->nvar)*sizeof(* v->vars)); }
int_fast8_t main(int argc, char *argv[]) { FILE *fp; long i, j; int quiet=0; long tmplong; // FILE *fp; const gsl_rng_type * rndgenType; double v1; char prompt[200]; int terminate = 0; char str[200]; char command[200]; int nbtk; char *cmdargstring; struct timeval rldelay; FILE *fpcmd; FILE *fpcmdout; int OKcmd; char fline[200]; int r; struct stat st; FILE *fpclififo; char buf[100]; int fifofd, c=0; int fdmax; fd_set cli_fdin_set; int n; ssize_t bytes; size_t total_bytes; char buf0[1]; char buf1[1024]; int initstartup = 0; /// becomes 1 after startup int blockCLIinput = 0; int CLIinit1 = 0; int cliwaitus=100; struct timeval tv; // sleep 100 us after reading FIFO strcpy(data.processname, argv[0]); TYPESIZE[0] = 0; TYPESIZE[1] = sizeof(char); TYPESIZE[2] = sizeof(int); TYPESIZE[3] = sizeof(float); TYPESIZE[4] = sizeof(double); TYPESIZE[5] = 2*sizeof(float); TYPESIZE[6] = 2*sizeof(double); TYPESIZE[7] = sizeof(unsigned short); TYPESIZE[8] = sizeof(long); atexit(fnExit1); data.Debug = 0; data.overwrite = 0; data.precision = 0; // float is default precision data.SHARED_DFT = 0; // do not allocate shared memory for images data.NBKEWORD_DFT = 10; // allocate memory for 10 keyword per image sprintf(data.SAVEDIR, "."); data.CLIlogON = 0; // log every command data.fifoON = 1; // signal handling data.sigact.sa_handler = sig_handler; sigemptyset(&data.sigact.sa_mask); data.sigact.sa_flags = 0; data.signal_USR1 = 0; data.signal_USR2 = 0; data.signal_TERM = 0; data.signal_INT = 0; data.signal_BUS = 0; data.signal_SEGV = 0; data.signal_ABRT = 0; data.signal_HUP = 0; data.signal_PIPE = 0; // if (signal(SIGINT, sig_handler) == SIG_ERR) // printf("\ncan't catch SIGINT\n"); if (sigaction(SIGUSR1, &data.sigact, NULL) == -1) printf("\ncan't catch SIGUSR1\n"); if (sigaction(SIGUSR2, &data.sigact, NULL) == -1) printf("\ncan't catch SIGUSR2\n"); // to take advantage of kernel priority: // owner=root mode=4755 #ifndef __MACH__ getresuid(&euid_real, &euid_called, &suid); //This sets it to the privileges of the normal user r = seteuid(euid_real); #endif // initialize readline // Tell readline to use custom completion function rl_attempted_completion_function = CLI_completion; rl_initialize (); // Get command-line options command_line( argc, argv ); // initialize fifo to process name /*if(data.fifoON==1) { sprintf(data.fifoname, "%s.fifo", data.processname); printf("fifo name : %s\n", data.fifoname); } */ // if( Verbose ) { fprintf(stdout, "%s: compiled %s %s\n",__FILE__,__DATE__,__TIME__); } CLIPID = getpid(); // sprintf(promptname, "%s", data.processname); sprintf(prompt,"%c[%d;%dm%s >%c[%dm ",0x1B, 1, 36, data.processname, 0x1B, 0); //sprintf(prompt, "%s> ", PACKAGE_NAME); printf("type \"help\" for instructions\n"); # ifdef _OPENMP printf("Running with openMP, max threads = %d (defined by environment variable OMP_NUM_THREADS)\n", omp_get_max_threads()); # endif // sprintf(DocDir,"%s",DOCDIR); // sprintf(SrcDir,"%s",SOURCEDIR); // sprintf(BuildFile,"%s",__FILE__); // sprintf(BuildDate,"%s",__DATE__); // sprintf(BuildTime,"%s",__TIME__); // Initialize random-number generator // //rndgenType = gsl_rng_ranlxs2; // best algorithm but slow //rndgenType = gsl_rng_ranlxs0; // not quite as good, slower rndgenType = gsl_rng_rand; // not as good but ~10x faster fast data.rndgen = gsl_rng_alloc (rndgenType); gsl_rng_set (data.rndgen,time(NULL)); // warm up for(i=0; i<10; i++) v1 = gsl_rng_uniform (data.rndgen); /*-------------------------------------------------- | Check command-line arguements +-------------------------------------------------*/ /* Initialize data control block */ main_init(); // initialize readline rl_callback_handler_install(prompt, (rl_vcpfunc_t*) &rl_cb); // fifo fdmax = fileno(stdin); // printf("FIFO = %d\n", data.fifoON); if(data.fifoON == 1) { printf("Creating fifo %s\n", data.fifoname); mkfifo(data.fifoname, 0666); fifofd = open(data.fifoname, O_RDWR | O_NONBLOCK); if (fifofd == -1) { perror("open"); return EXIT_FAILURE; } if(fifofd>fdmax) fdmax = fifofd; } tmplong = data.NB_MAX_VARIABLE; C_ERRNO = 0; // initialize C error variable to 0 (no error) terminate = 0; while(terminate==0) { data.CMDexecuted = 0; if( fopen( "STOPCLI","r" ) != NULL ) { fprintf(stdout, "STOPCLI FILE FOUND. Exiting...\n"); exit(1); } if(Listimfile==1) { fp = fopen("imlist.txt", "w"); list_image_ID_ofp_simple(fp); fclose(fp); } /* Keep the number of image addresses available * NB_IMAGES_BUFFER above the number of used images * * Keep the number of variables addresses available * NB_VARIABLES_BUFFER above the number of used variables */ if( re_alloc() != 0 ) { fprintf(stderr,"%c[%d;%dm ERROR [ FILE: %s FUNCTION: %s LINE: %d ] %c[%d;m\n", (char) 27, 1, 31, __FILE__, __func__, __LINE__, (char) 27, 0); fprintf(stderr,"%c[%d;%dm Memory re-allocation failed %c[%d;m\n", (char) 27, 1, 31, (char) 27, 0); exit(0); } compute_image_memory(data); compute_nb_image(data); /** If fifo is on and file CLIstatup.txt exists, load it */ if(initstartup == 0) if(data.fifoON==1) { printf("IMPORTING FILE %s\n", CLIstartupfilename); sprintf(command, "cat %s > %s 2> /dev/null", CLIstartupfilename, data.fifoname); r = system(command); } initstartup = 1; // ------------------------------------------------------------- // get user input // ------------------------------------------------------------- tv.tv_sec = 0; tv.tv_usec = cliwaitus; FD_ZERO(&cli_fdin_set); // Initializes the file descriptor set cli_fdin_set to have zero bits for all file descriptors. if(data.fifoON==1) FD_SET(fifofd, &cli_fdin_set); // Sets the bit for the file descriptor fifofd in the file descriptor set cli_fdin_set. FD_SET(fileno(stdin), &cli_fdin_set); // Sets the bit for the file descriptor fifofd in the file descriptor set cli_fdin_set. while(CLIexecuteCMDready == 0) { n = select(fdmax+1, &cli_fdin_set, NULL, NULL, &tv); if (n==0) // nothing received, need to re-init and go back to select call { tv.tv_sec = 0; tv.tv_usec = cliwaitus; FD_ZERO(&cli_fdin_set); // Initializes the file descriptor set cli_fdin_set to have zero bits for all file descriptors. if(data.fifoON==1) FD_SET(fifofd, &cli_fdin_set); // Sets the bit for the file descriptor fifofd in the file descriptor set cli_fdin_set. FD_SET(fileno(stdin), &cli_fdin_set); // Sets the bit for the file descriptor fifofd in the file descriptor set cli_fdin_set. continue; } if (n == -1) { if(errno==EINTR) // no command received { continue; } else { perror("select"); return EXIT_FAILURE; } } blockCLIinput = 0; if(data.fifoON==1) { if (FD_ISSET(fifofd, &cli_fdin_set)) { total_bytes = 0; for (;;) { bytes = read(fifofd, buf0, 1); if (bytes > 0) { buf1[total_bytes] = buf0[0]; total_bytes += (size_t)bytes; } else { if (errno == EWOULDBLOCK) { break; } else { perror("read"); return EXIT_FAILURE; } } if(buf0[0]=='\n') { buf1[total_bytes-1] = '\0'; line = buf1; CLI_execute_line(); printf("%s", prompt); fflush(stdout); break; } } blockCLIinput = 1; // keep blocking input while fifo is not empty } } if(blockCLIinput == 0) if (FD_ISSET(fileno(stdin), &cli_fdin_set)) { rl_callback_read_char(); } } CLIexecuteCMDready = 0; if(data.CMDexecuted==0) printf("Command not found, or command with no effect\n"); } return(0); }