예제 #1
0
/*--------------------*/ void DOcmd(void *tag, void *msg, int *size)  {
/* msg: string finished by "\n\0" */
//printf("DOcmd: tag:%d size:%d msg:%s<-endofmsg\n", *tag, *size,msg);
char mymsg[400];
int stdoutyes=1;
strncpy(mymsg, (char *)msg, 400); 
mymsg[398]='\n'; mymsg[399]='\0';   // force \n (if not given)
if((strncmp(mymsg,"pcfg ",5)==0) || (strncmp(mymsg,"Ncfg ",5)==0) ||
    (strncmp(mymsg,"Acfg ",5)==0) ) {
/* pcfg RUNNUMBER partname    -try ACT download (ECS INIT)
   Ncfg RUNNUMBER partname    -NO ACT, change: Ncfg -> pcfg
   Acfg RUNNUMBER partname    - abort
   \n               -stop this server
   rcfgdel useDAQLOGBOOK
   rcfgdel ignoreDAQLOGBOOK
   rcfgdel ALL 0xc606       -ctpproxy restart
   rcfgdel PARTNAME RUNN    -process in .py, (ECS STOP)
   rcfgdel reload           -just reload parted
   csupdate
   aliasesupdate
   intupdate
   clockshift
   rcfg  -OBSOLETE!
   resetclock   -just write out
*/
  char c; unsigned int rundec=0;
  int rc, infoerr, ix=0, runnactive=0;
  char instname[100]="";
  char version[100]="";
  char pname[60]="";
  char runc[16]="";
  char emsg[500];
  //rc= getname_rn(mymsg, pname, rundec);   moved to DOrcfg()
  while(1) {
    int cix;
    c=(mymsg)[5+ix];
    if((c==' ') || (c=='\n') || (c=='\0')) {
      if(runnactive==0) {
        if(c==' ') {
          ix++; runnactive=1; cix=0;
          continue;
        } else { break; };
      } else {
        if(runc[0]!='\0') {
          rundec=atoi(runc); break;
        };
      };
    };
    if(runnactive==1) {
      runc[cix]=c; runc[cix+1]='\0'; cix++;
    } else {
      pname[ix]= c; pname[ix+1]='\0';
    };
    ix++;
    if(ix>=60) break;
  };
  infolog_SetStream(pname, rundec);
  if(mymsg[0]=='p') {
    char filter[20000]="";
    //actdb_open(); 
    rc=actdb_getPartition(pname,filter, instname, version);
    //actdb_close();
    if(rc==0) {
      sprintf(emsg,"INFO %s (run:%d inst:%s ver:%s) downloaded from ACT.", 
        pname, rundec, instname, version); 
      infoerr=LOG_INFO;
    } else if(rc==1) {
      sprintf(emsg,"ERROR %s (run:%d) not found in ACT, might be OK if shift leader disabled it in ACT (i.e. is in 'Local File' mode)", pname, rundec); 
      infoerr=LOG_ERROR;
    } else {
      sprintf(emsg,"actdb_getPartition(%s) run:%d rc:%d (-2: partition not available in ACT)", pname,rundec,rc); 
      infoerr=LOG_ERROR;
    };
  } else if(mymsg[0]=='A') {
    //printf("INFO Acfg %s %d\n", pname, rundec);
    del_insver(rundec);
  } else {   // Ncfg runnumber partname
    sprintf(emsg,"INFO %s (run:%d) not searched in ACT (ECS:ACT_CONFIG=NO)", 
      pname, rundec); 
    infoerr=LOG_INFO;
    mymsg[0]= 'p';
  };
  if(mymsg[0]=='p') {
    //prtLog(emsg);
    //myprtLog(emsg);
    infolog_trgboth(infoerr, emsg);
    rc= add_insver(rundec, pname, instname, version);
    if(rc==-1) {
      sprintf(emsg,"run:%d, instance/ver will not be stored in ACT", rundec);
      infolog_trg(LOG_FATAL, emsg);
      printf("ERROR %s", emsg);
    };
  };
  infolog_SetStream("",0);
/*---- moved to .rcfg time
  if(ignoreDAQLOGBOOK==1) {
    rcdaq=0;
  } else {
    rcdaq= daqlogbook_open(); //rcdaq=0;
    if(rcdaq!=0) {
      printf("ERROR update_cs: DAQlogbook_open failed rc:%d",rcdaq); rc=4;
      //prtError("DAQlogbook_open failed");
    }else{
      char itemname[200];
      rc= daqlogbook_update_cs(rundec, CSString);
      do_partitionCtpConfigItem(pname, itemname);
      rc_insert= daqlogbook_update_ACTConfig(rundec, itemname,instname,version);
      rcdaq= daqlogbook_close();
    };
    if((rc==0) && (rc_insert==0)) {
      infoerr=LOG_INFO;
    } else {
      infoerr=LOG_FATAL;
    };
    sprintf(emsg,"daglogbook_update_cs rc:%d _update_ACTConfig rc:%d", rc, rc_insert);
    infolog_trg(infoerr, emsg);
  };
-------- moved to .rcfg */
//                        1...,...10....,....20..3
} else if((strncmp(mymsg,"rcfgdel ignoreDAQLOGBOOK",23)==0)) {
  int rcdaq;
  ignoreDAQLOGBOOK=1;
  rcdaq= daqlogbook_close();
  if(rcdaq==-1) {
    printf("ERROR DAQlogbook_close failed\n");
  } else {
    printf("INFO DAQlogbook closed:rc:%d (ignoreDAQLOGBOOK from ctpproxy received)\n",rcdaq);
  };
  stdoutyes=0;
  //cshmSetGlobFlag(FLGignoreDAQLOGBOOK);
} else if((strncmp(mymsg,"rcfgdel useDAQLOGBOOK",20)==0)) {
  int rcdaq;
  rcdaq= daqlogbook_open(); //rcdaq=0;
  if(rcdaq!=0) {
    printf("ERROR DAQlogbook_open failed rc:%d",rcdaq);
    ignoreDAQLOGBOOK=1;
    cshmSetGlobFlag(FLGignoreDAQLOGBOOK);
  } else {
    ignoreDAQLOGBOOK=0;
    cshmClearGlobFlag(FLGignoreDAQLOGBOOK);
  };
  stdoutyes=0;
} else if((strncmp(mymsg,"rcfgdel ALL 0x...",11)==0)) {   //ctpproxy [re]start
  //int irc;
  printf("INFO rcfgdel ALL 0x... (redis, readTables...)\n");
  red_clear_detsinrun(0);
  reset_insver();
  readTables();
  ctpc_clear(); updateCNAMES();
  /*irc=*/ check_xcounters();
} else if((strncmp(mymsg,"rcfgdel reload",14)==0)) {   //reload parted
  // perhaps it is reasonable to clean (even if ctpproxy did not restart but is 
  // without active partitions)
  //reset_insver();
  //readTables();
  //ctpc_clear(); updateCNAMES();
  //printf("INFO rcfgdel reload\n");
  ;
} else if((strncmp(mymsg,"rcfgdel ",8)==0)) {   // rcfgdel partname runn
  enum Ttokentype t1; int ixl, runn; char pname[16]; char intval[16];;
  char emsg[200];
  emsg[0]='\0';
  ixl=8; t1= nxtoken(mymsg, pname, &ixl);   // runNumber
  if(t1==tSYMNAME) {
    t1= nxtoken(mymsg, intval, &ixl);   // runNumber
    if(t1==tINTNUM) {
      runn= str2int(intval);
      // from 26.2. maybe not needed here, but seems ok when INIT brings ctpproxy to LOAD_FAILURE
      del_insver(runn);  
    } else {
      sprintf(emsg,"pydimserver: bad run number in rcfgdel %s cmd", pname);
    };
  } else {
    sprintf(emsg,"pydimserver: bad part. name in %s cmd", mymsg);
  };
  if(emsg[0]=='\0') {
    red_clear_detsinrun(runn);
    ctpc_delrun(runn); updateCNAMES();
  } else {
    infolog_trg(LOG_ERROR, emsg); printf("ERROR %s\n",emsg);
  };
} else if((strncmp(mymsg,"csupdate",8)==0)) {
  int csclients; // char *cs;
  /*cs=*/ readCS();
  csclients= dis_update_service(CSid);
  printf("INFO CS update for %d clients\n", csclients);
  stdoutyes=0;
} else if((strncmp(mymsg,"aliasesupdate",13)==0)) {
  int rc;
  rc= readAliases(); 
  if(rc==-1) {
    char emsg[200];
    strcpy(emsg,"aliasesupdate: info from aliases.txt not updated correctly");
    infolog_trg(LOG_INFO, emsg); printf("ERROR %s\n",emsg);
  };
  stdoutyes=0;
} else if((strncmp(mymsg,"intupdate",9)==0)) {
  int rc1, rc2;
  rc1= getINT12fromcfg(INT1String, INT2String, MAXINT12LINE);
  printf("INFO rc:%d INT1:%s INT2:%s\n", rc1, INT1String, INT2String);
  rc1= dis_update_service(INT1id);
  rc2= dis_update_service(INT2id);
  printf("INFO INT1/INT2 update for %d/%d clients\n", rc1, rc2);
  stdoutyes=0;
} else if((strncmp(mymsg,"clockshift ",11)==0)) {
  char halfns[20]; int ix,rc;
  unsigned int hns,cordeval,last_applied;
  /* char c; halfns[0]='\0';
  for(ix=0; ix<40; ix++) {
    c= mymsg[11+ix]; 
    if((c=='\0') || (c=='\n')) {
      halfns[ix]= '\0'; break;
    };
    halfns[ix]= c;
  }; 
  halfns[20]= '\0';*/
  ix= sscanf(&mymsg[11], "%d %d %d\n", &hns, &cordeval, &last_applied);
  if( (ix<2) || (hns>63) || (cordeval>1023) ) {
    printf("ERROR set clockshift %d %d incorrect, not updated.\n",hns, cordeval);
  } else {
    //int csclients;
    sprintf(halfns, "%d %d %d", hns, cordeval, last_applied);
    rc= writedbfile("clockshift", halfns);   // 2ints: halfns cordereg
    //csclients= dis_update_service(CSid);  is not here
    printf("INFO set clockshift %s. rc(=chars):%d \n", halfns, rc);
  };
  stdoutyes=0;
} else if((strncmp(mymsg,"rcfg ",5)==0)) {   // moved to DOrcfg()
  printf("ERROR rcfg cmd ignored (processed by CTPRCFG cmd\n");
  stdoutyes=0;
} else if((strncmp(mymsg,"resetclock",9)==0)) {
  ;
};
if(strcmp(mymsg,"\n")==0) {
  //stopserving();
  myprtLog("Quitting server...");
  printf("stop\n"); fflush(stdout);
  //sleep(1);
  //exit(0);
};
if(stdoutyes==1) {   /*
  pcfg 
    -prepare .pcfg (parted.py)
    - scp .pcfg
  rcfg partName NNN clu1 clu2 ... clu6 cl1 ... clNCLASS NewLine
    -prepare .rcfg (parted.py)
    -
  smaqmv
  */
  printf("%s",mymsg); fflush(stdout);
};
}
예제 #2
0
/*--------------------------------------------------- main */
int main(int argc, char **argv) {
int rc,grc=0;
//char *cs; 
char command[40];
char servername[40];
char cmd[80];

setlinebuf(stdout);
if(argc!=3) {
  printf("ERROR: Usage: linux/server servername command\n"); return(8);
};
infolog_SetFacility("CTP");
infolog_SetStream("",0);
strcpy(servername, argv[1]);
strcpy(command, argv[2]);
sprintf(cmd, "%s/%s", servername, command);   // CTPRCFG/RCFG
// following line has to be 1st line printed!
printf("DIM server:%s cmd:%s\n", servername, command); // should be 1st line

reset_insver();
/*cs=*/ readCS();
rc= readAliases(); 
if(rc==-1) {
  char emsg[200];
  strcpy(emsg,"aliases info from aliases.txt not updated correctly");
  infolog_trg(LOG_ERROR, emsg); printf("ERROR %s\n",emsg);
};
cshmInit();
readTables();   // + when ctpprxy restarted, i.e. in time of rcfgdel 0 ALL
printf("INFO ctpshmbase1:%p\n", ctpshmbase);
ctpc_clear(); ctpc_print(CNAMESString);
//updateCNAMES();
rc= getINT12fromcfg(INT1String, INT2String, MAXINT12LINE);
printf("INFO rc:%d INT1:%s INT2:%s\n", rc, INT1String, INT2String);

rc= mydbConnect();

dis_add_error_handler(error_handler);
dis_add_exit_handler(exit_handler);
dis_add_client_exit_handler (client_exit_handler);
// commands:
CTPRCFGRCFGid= dis_add_cmnd(cmd,"C", DOcmd, 88);
printf("INFO DIM cmd:%s id:%d\n", cmd, CTPRCFGRCFGid);
sprintf(cmd, "%s", servername);   // CTPRCFG binary (after LS1) RCFG request
CTPRCFGid= dis_add_cmnd(cmd,NULL, DOrcfg, 89);
printf("INFO DIM cmd:%s id:%d\n", cmd, CTPRCFGid);

sprintf(cmd, "%s/LTUCFG", servername);   // LTUCFG binary (after LS1)
LTUCFGid= dis_add_cmnd(cmd,NULL, DOltucfg, 90);
printf("INFO DIM cmd:%s id:%d\n", cmd, LTUCFGid);

sprintf(cmd, "%s/COM2DAQ", servername);   // CTPRCFG/COM2DAQ
C2Did= dis_add_cmnd(cmd,"C", DOcom2daq, 91);
printf("INFO DIM cmd:%s id:%d\n", cmd, C2Did);
sprintf(cmd, "%s/SETBM", servername);   // CTPRCFG/SETBM
SETBMid= dis_add_cmnd(cmd,"C", DOsetbm, 92);
printf("INFO DIM cmd:%s id:%d\n", cmd, SETBMid);
/*
sprintf(cmd, "%s/FSUPDATE", servername);   // CTPRCFG/FSUPDATE
FSUid= dis_add_cmnd(cmd,NULL, DOfsupdate, 92);
printf("INFO DIM cmd:%s id:%d\n", cmd, FSUid);
*/
// services:
sprintf(cmd, "%s/INT1", servername);   // CTPRCFG/INT1
INT1id=dis_add_service(cmd,"C:99", INT1String, MAXINT12LINE, NULL, 4567);
printf("INFO DIM service:%s id:%d\n", cmd, INT1id);
sprintf(cmd, "%s/INT2", servername);   // CTPRCFG/INT2
INT2id=dis_add_service(cmd,"C:99", INT2String, MAXINT12LINE, NULL, 4568);
printf("INFO DIM service:%s id:%d\n", cmd, INT2id);
sprintf(cmd, "%s/CS", servername);   // CTPRCFG/CS
CSid=dis_add_service(cmd,"C", CSString, MAXCSString, CScaba, 4569);
printf("INFO DIM service:%s id:%d\n", cmd, CSid);
sprintf(cmd, "%s/CNAMES", servername);   // CTPRCFG/CNAMES
CNAMESid=dis_add_service(cmd,"C", CNAMESString, MAXCNAMESString, CNAMEScaba, 4570);
printf("INFO DIM service:%s id:%d\n", cmd, CNAMESid);

dis_start_serving(servername);  
while(1) {
  char *frc;
#define MAXLINECS 8000
  char line[MAXLINECS];   //80 chars per class for NCLASS classes
  //sleep(10);
  frc= fgets(line, MAXLINECS, stdin);
  if(frc==NULL) break;
  if(strncmp(line,"quit",4)==0) {
    break;
  } else if(strncmp(line,"class ",6)==0) {
    int rcdaq;
    printf("INFO igDAQLOGBOOK:%d line:%s",ignoreDAQLOGBOOK, line);
    /* from 28.5.2013: DAQlogbook opened/closed when ctp_proxy restarted
    rcdaq= daqlogbook_open();
    if(rcdaq==-1) {
      printf("ERROR DAQlogbook_open failed\n");
    } else {
      if(ignoreDAQLOGBOOK==0) {
        rcdaq= updateDAQDB(line);
        if(rcdaq!=0) {
          printf("ERROR updateDAQLOGBOOK failed. rc=%d\n", rcdaq);
        };
        rcdaq= daqlogbook_close();
        if(rcdaq==-1) {
          printf("ERROR DAQlogbook_close failed\n");
        };
      };
    };*/
    rcdaq= updateDAQDB(line);
    if(rcdaq!=0) {
      printf("ERROR updateDAQLOGBOOK failed. rc=%d\n", rcdaq);
    };
  } else if(strncmp(line,"inpupd ",7)==0) {
    update_ctpins(line);
  } else if(strncmp(line,"indets ",7)==0) {
    int ix, ix1, runnumb; char *efstart;
    ix1= sscanf(&line[7], "%d ", &runnumb);
    efstart= strstr(&line[7], "0x");   // indets runN 0xeffiout 0xindets
    ix= sscanf(efstart, "0x%x 0x%x\n", &effiout, &indets);
    printf("INFO ix1:%d runnumb:%d ix:%d effiout:0x%x indets:0x%x\n",
      ix1, runnumb, ix, effiout, indets); fflush(stdout);
    //rcupd= updeff_insver(runnumb, effiout);
  } else if(strncmp(line,"cmd ",4)==0) {
    int unsigned ix,rcsystem;
    for(ix=0; ix<strlen(line); ix++) {
      if(line[ix]=='\n'){ line[ix]='\0'; break; };
    };
    rcsystem= system(&line[4]);
    printf("INFO rc:%d cmd:%s\n", rcsystem, &line[4]);   
    continue;
  } else {
    printf("ERROR %s", line); fflush(stdout);
  };
};
rc= daqlogbook_close();
if(rc==-1) {
  printf("ERROR DAQlogbook_close failed\n");
};
mydbDisconnect();
stopserving();
cshmDetach(); printf("INFO shm detached.\n");
return(grc);
}
예제 #3
0
/* check/configure/print FPGAs versions for all the ctp boards */
void checkCTP() {
int ix; w32 code, vmever,sernum,boardver,adshift;
readBICfile();
readTables();
for(ix=0; ix<NCTPBOARDS; ix++) {
/*  if(notInCrate(ix)) continue;  -not here*/
  if(ctpboards[ix].vmever!=NICRATE) continue; 
  adshift=BSP*ctpboards[ix].dial;
#ifdef SIMVME
  ctpboards[ix].boardver= ctpboards[ix].lastboardver;
  ctpboards[ix].vmever= 0xa3; 
  code=ctpboards[ix].code;
  continue; 
#else
  code= 0xff&vmer32(CODE_ADD+adshift);
#endif
  /*printf("code:%x adshift:%x\n",code,adshift); fflush(stdout);  */
  if(code==ctpboards[ix].code) {
    vmever= 0xff&vmer32(VERSION_ADD+adshift);
    sernum= 0xff&vmer32(SERIAL_NUMBER+adshift);
    boardver= 0xff&vmer32(FPGAVERSION_ADD+adshift);
    ctpboards[ix].vmever= vmever;
    /*
      printf("--->%s (code:0x%x base:0x82%1x000) vmeFPGA:0x%x boardFPGA:0x%x SN:0x%x\n",
        ctpboards[ix].name,ctpboards[ix].code, ctpboards[ix].dial, 
	vmever, boardver, sernum);
      fflush(stdout);
    */
    if(boardver==0xff) {
      boardver= loadFPGA(BSP*ctpboards[ix].dial);
    };
    if(boardver>0x100) {
      printf("Board %s (base:0x82%1x000) not configured. Error:0x%x\n",
        ctpboards[ix].name,ctpboards[ix].dial, boardver);
    } else {
      ctpboards[ix].boardver= boardver;
      ctpboards[ix].serial= sernum;
    };
  } else {
    printf("Board %s (base:0x82%1x000) missing\n",
      ctpboards[ix].name,ix);
    ctpboards[ix].vmever= 0;
  };
};
printf("CTP boards in the crate:\n");
printf("   board code ser# base     vmeV boardV BCstatus\n");
for(ix=0; ix<NCTPBOARDS; ix++) {
  w32 bcst;
  char errnote[80];
  if(notInCrate(ix)) continue;
  adshift=BSP*ctpboards[ix].dial;
  bcst= vmer32(adshift+BC_STATUS)&0x7;
  errnote[0]='\0';
  if(ctpboards[ix].boardver != ctpboards[ix].lastboardver) {
    sprintf(errnote,"Bad boardV (%x expected).", ctpboards[ix].lastboardver);
  };
  if(bcst!= 2) {
    sprintf(errnote,"%s Bad BCstatus (0x2 expected)", errnote);
  };
  /*
  printf("%d:%s \t(code:0x%x serial:0x%x base:0x82%1x000) vmeFPGA:0x%x boardFPGA:0x%x\n",
    ix, ctpboards[ix].name,ctpboards[ix].code, ctpboards[ix].serial, 
    ctpboards[ix].dial, ctpboards[ix].vmever, ctpboards[ix].boardver);
    */
  printf("%2d:%5s 0x%x %4d 0x82%1x000 0x%x 0x%x   %x     %s\n",
    ix, ctpboards[ix].name,ctpboards[ix].code, ctpboards[ix].serial, 
    ctpboards[ix].dial, ctpboards[ix].vmever, ctpboards[ix].boardver,bcst,
    errnote);
};
}
예제 #4
0
파일: main.c 프로젝트: sora-chan/bklg
int main(int argc, char **argv)
{
	atexit(cleanup);
	options = calloc(sizeof(struct opts_t), 1);
	tableList = calloc(sizeof(*tableList), 1);
	options->mode = -1;
	options->rating = RATING_UNDEFINED;

	if (argc < 2)
		exitmsg(1, "Try -help for usage information\n");

	parseArgs(argv + 1, options);

	if (!options->backlog_path[0])
		snprintf(options->backlog_path, 256, "%s/.backlog", getenv("HOME"));

	if (!modified && readTables(tableList, options->backlog_path))
		printf("Couldn't open '%s': %s\nYou can force the creation of this file by passing -force\n\n", options->backlog_path, strerror(errno));

	struct entry_t *entry = 0;
	struct table_t *table = 0;

	if (options->mode == RATING_UNDEFINED && !options->searchMode && !modified)
		exitmsg(1, "Must specify a mode\n");

	if (options->rating != RATING_UNDEFINED && (options->rating < -1 || options->rating > 10))
		exitmsg(1, "Rating must be in the range of -1 to 10\n");

	char *entrystr = "Entry not found\n";
	char *tablestr = "Table not found\n";
	char *err_dupe = "An entry with that name already exists in that table\n";
	char *err_dupe_table = "A table with that name already exists\n";

	char help_msg[] =
		"Available options:\n"
		" -t\n"
		"\tSwitch to table mode\n"
		" -help\n"
		"\tPrint this message\n"
		" -insert\n"
		"\tAdd entry/table\n"
		" -rm\n"
		"\tRemove entry/table\n"
		" -mv\n"
		"\tMove entry between tables\n"
		" -ls\n"
		"\tList tables, can be used with -name to print specific table\n"
		" -find\n"
		"\tSearch for entry, can be used with -name to search within table\n"
		" -rate <INT>\n"
		"\tSet rating on entry\n"
		" -rename\n"
		"\tRename entry/table\n"
		" -name <STRING>\n"
		"\tSet name\n"
		" -new <STRING>\n"
		"\tSet new name\n"
		" -src <STRING>\n"
		"\tSet source name\n"
		" -dest <STRING>\n"
		"\tSet destination name\n"
		" -roll\n"
		"\tReturn random entry\n"
		" -mal\n"
		"\tSearch MAL for entry\n"
		" -nyaa\n"
		"\tSearch Nyaa for entry\n"
		" -baka\n"
		"\tSearch BakaBT for entry (through Google)\n"
		" -anidb\n"
		"\tSearch AniDB for entry\n"
		" -force\n"
		"\tForce write-out to backlog file\n"
		" -path\n"
		"\tOverride the default backlog path (~/.backlog)\n"
		" -nobak\n"
		"\tDon't backup backlog file";

	if (options->mode == MODE_LIST) {	//List tables.
		if (options->sourceName) {
			table = bklgGetTable(tableList, options->sourceName);
			if (!table)
				exitmsg(1, tablestr);
		}
		if (table) {
			bklgPrintTable(table, stdout);
		} else {
			char buffer[32];
			struct tm *tm;
			time_t utime = time(0);

			tm = localtime(&utime);
			strftime(buffer, 32, "%d/%m/%Y\n", tm);
			puts(buffer);
			bklgPrintAllTables(tableList, stdout);
		}
	}

	else if (options->mode == MODE_ENTRY_ADD && options->tableMode) {	//Add table.
		if (!options->entryName)
			exitmsg(1, "Name required\n");

		if (bklgGetTableExact(tableList, options->entryName))
			exitmsg(1, err_dupe_table);

		puts("Added table");
		bklgTableAdd(tableList, options->entryName);
		modified = 1;
	}

	else if (options->mode == MODE_ENTRY_REMOVE && options->tableMode) {	//Remove table.
		if (!options->entryName)
			exitmsg(1, "Name required\n");
		struct table_t *table = bklgGetTable(tableList, options->entryName);

		if (!table)
			exitmsg(1, tablestr);
		printf("Remove \"%s\"? [Y,n]: ", table->name);
		if (confirm()) {
			puts("Table removed");
			bklgLnodeDestroy(tableList, table->parent);
			bklgTableDestroy(table);
			modified = 1;
		}
	}

	else if (options->mode == MODE_ENTRY_ADD) {	//Add entry.
		if (!options->entryName || !options->destinationName)
			exitmsg(1, "Name and destination required, rating optional\n");

		table = bklgGetTable(tableList, options->destinationName);
		if (!table)
			exitmsg(1, tablestr);

		if (bklgGetEntryExact(table->entries, options->entryName))
			exitmsg(1, err_dupe);

		printf("Add entry to \"%s\"? [Y,n]: ", table->name);
		if (confirm()) {
			puts("Entry added");
			entry = bklgEntryAdd(table->entries, options->entryName, options->rating == -2 ? -1 : options->rating);
			modified = 1;
		}
	}

	else if (options->mode == MODE_ENTRY_REMOVE) {	//Remove entry.
		if (!options->entryName)
			exitmsg(1, "Name required, source optional\n");
		struct entry_t *entry = 0;

		if (!options->sourceName) {
			entry = bklgGetEntryAllTables(tableList, options->entryName, &table);
		} else {
			table = bklgGetTable(tableList, options->sourceName);
			if (!table)
				exitmsg(1, tablestr);
			entry = bklgGetEntry(table->entries, options->entryName);
		}
		if (!entry)
			exitmsg(1, entrystr);
		printf("Remove \"%s\" from \"%s\"? [Y,n]: ", entry->name, table->name);
		if (confirm()) {
			puts("Entry removed");
			bklgLnodeDestroy(table->entries, entry->parent);
			free(entry);
			modified = 1;
		}
	}

	else if (options->mode == MODE_ENTRY_MOVE) {	//Move entry to another table.
		if (!options->entryName || !options->destinationName)
			exitmsg(1, "Name and destination required, source optional\n");

		struct entry_t *oldEntry = 0;
		struct table_t *sourceTable = 0;

		if (!options->sourceName) {
			oldEntry = bklgGetEntryAllTables(tableList, options->entryName, &sourceTable);

		} else {
			sourceTable = bklgGetTable(tableList, options->sourceName);
			if (!sourceTable)
				exitmsg(1, "Source table not found\n");
			oldEntry = bklgGetEntry(sourceTable->entries, options->entryName);
		}
		if (!oldEntry)
			exitmsg(1, entrystr);

		struct table_t *destTable = bklgGetTable(tableList, options->destinationName);

		if (!destTable)
			exitmsg(1, "Destination table not found\n");

		if (bklgGetEntryExact(destTable->entries, oldEntry->name))
			exitmsg(1, err_dupe);

		printf("Move \"%s\" from \"%s\" to \"%s\"? [Y,n]: ", oldEntry->name, sourceTable->name, destTable->name);
		if (confirm()) {
			puts("Entry moved");
			entry = bklgEntryAdd(destTable->entries, oldEntry->name, (options->rating < -1 ? oldEntry->rating : options->rating));
			bklgLnodeDestroy(sourceTable->entries, oldEntry->parent);
			free(oldEntry);
			modified = 1;
		}
	}

	else if (options->mode == MODE_FIND) {	//Find entry.
		if (!options->entryName)
			exitmsg(1, "Name required, source optional\n");
		if (!options->sourceName) {
			entry = bklgGetEntryAllTables(tableList, options->entryName, &table);
		} else {
			table = bklgGetTable(tableList, options->sourceName);
			if (!table)
				exitmsg(1, tablestr);
			entry = bklgGetEntry(table->entries, options->entryName);
		}
		if (entry) {
			char rating[8];

			snprintf(rating, 8, "%i/10", entry->rating);
			printf("Best match:\n\tName: %s\n\tRating: %s\n\tParent table: %s\n", entry->name, entry->rating == -1 ? "none" : rating, table->name);
		} else {
			exitmsg(1, entrystr);
		}
	}

	else if (options->rating != RATING_UNDEFINED) {	//Set a rating.
		if (!options->entryName)
			exitmsg(1, "Name required, rating and source optional\n");
		if (!options->sourceName) {
			entry = bklgGetEntryAllTables(tableList, options->entryName, &table);
		} else {
			table = bklgGetTable(tableList, options->sourceName);
			if (!table)
				exitmsg(1, tablestr);
			entry = bklgGetEntry(table->entries, options->entryName);
		}
		if (!entry)
			exitmsg(1, entrystr);
		printf("Change rating of \"%s\"? [Y,n]: ", entry->name);
		if (confirm()) {
			puts("Changed rating");
			entry->rating = options->rating;
			modified = 1;
		}
	}

	else if (options->mode == MODE_ENTRY_RENAME && options->tableMode) {	//Rename table.
		if (!options->entryName || !options->entryNewName)
			exitmsg(1, "Name and new name required\n");

		table = bklgGetTable(tableList, options->entryName);
		if (!table)
			exitmsg(1, tablestr);

		if (bklgGetTableExact(tableList, options->entryNewName))
			exitmsg(1, err_dupe_table);

		printf("Rename \"%s\"? [Y,n]: ", table->name);
		if (confirm()) {
			puts("Renamed table");
			char tmp[64];
			char *tmpp = 0;

			strncpy(tmp, options->entryNewName, 64);
			tmpp = stripWhiteSpace(tmp);
			strncpy(table->name, tmpp, 64);
			modified = 1;
		}
	}

	else if (options->mode == MODE_ENTRY_RENAME) {	//Rename entry.
		if (!options->entryName || !options->entryNewName)
			exitmsg(1, "Name and new name required, source optional\n");

		if (!options->sourceName) {
			entry = bklgGetEntryAllTables(tableList, options->entryName, &table);
		} else {
			table = bklgGetTable(tableList, options->sourceName);
			if (!table)
				exitmsg(1, tablestr);
			entry = bklgGetEntry(table->entries, options->entryName);
		}

		if (!entry)
			exitmsg(1, entrystr);

		if (bklgGetEntryExact(table->entries, options->entryNewName))
			exitmsg(1, err_dupe);

		printf("Rename \"%s\"? [Y,n]: ", entry->name);
		if (confirm()) {
			puts("Renamed entry");
			char tmp[128];
			char *tmpp = 0;

			strncpy(tmp, options->entryNewName, 128);
			tmpp = stripWhiteSpace(tmp);
			strncpy(entry->name, tmpp, 128);
			modified = 1;
		}
	}

	else if (options->mode == MODE_RANDOM) {	//Choose random entry.
		if (!options->sourceName)
			exitmsg(1, "Source required\n");
		struct table_t *table = bklgGetTable(tableList, options->sourceName);

		if (!table)
			exitmsg(1, tablestr);
		srand(time(0) * getpid());
		size_t len = listlen(table->entries);
		int randomindex = rand() % len;
		struct lnode_t *current = table->entries->tail;

		for (int i = 0; i < randomindex; i++) {
			current = current->next;
		}
		entry = current->data;
		char rating[8];

		snprintf(rating, 8, "%i/10", entry->rating);
		printf("Random entry at index %i out of %zu:\n\tName: %s\n\tRating: %s\n\tParent table: %s\n", randomindex + 1, len, entry->name, entry->rating == -1 ? "none" : rating, table->name);
	}

	if (options->searchMode) {	//Search various sites for entry.
		if (!entry && !options->entryName) {
			exitmsg(1, "Name required, source optional\n");
		} else if (!entry) {
			struct table_t *table = 0;

			if (!options->sourceName) {
				entry = bklgGetEntryAllTables(tableList, options->entryName, &table);

			} else {
				if (!table) {
					table = bklgGetTable(tableList, options->sourceName);
					if (!table)
						exitmsg(1, tablestr);
				}
				entry = bklgGetEntry(table->entries, options->entryName);
			}
			if (!entry)
				exitmsg(1, entrystr);
		}
		printf("Searching web for \"%s\"\n", entry->name);
		webSearch(entry, options->searchMode);
	}

	else if (options->mode == MODE_HELP) {	//Print usage stuff.
		puts(help_msg);
	}

	if (modified) {
		if (backup) {
			char bakpath[256];

			snprintf(bakpath, 256, "%s.bak", options->backlog_path);
			if (rename(options->backlog_path, bakpath) == -1)
				exitmsg(1, "Failed to rename to '%s': %s\n", bakpath, strerror(errno));
		}
		if (writeTables(tableList, options->backlog_path))
			exitmsg(1, "Failed to open '%s' for writing: %s\n", options->backlog_path, strerror(errno));
	}
	exit(0);
}
예제 #5
0
int main(int argc, char **argv) {
TRBIF *grbif=NULL;
int rc=0;
FILE *cfgfile; char *env;
char fnpath[MAXNAMELENGTH+40];
char pname[MAXNAMELENGTH];
char errmsg[300]="";

if(argc<=1) {
  printf("./main_TRBIF part_name\n");
  return 1;
} else {
  strcpy(pname, argv[1]);
};
infolog_SetFacility("CTP"); infolog_SetStream("",0);
cshmInit();
//setglobalflags(argc, argv);
//if((rc=ctp_Initproxy())!=0) exit(8);
rc= vmeopen("0x820000", "0xd000");
if(rc!=0) {
  printf("vmeopen CTP vme:%d\n", rc); exit(8);
};
printf("main_TRBIF...\n");
checkCTP();   /* check which boards are in the crate - ctpboards */
readTables(); // enough only in ctp_proxy

env= getenv("dbctp"); 
/*strcpy(fnpath, env); strcat(fnpath, "../pardefs/");
strcat(fnpath, pname); strcat(fnpath, ".pcfg"); */
sprintf(fnpath, "%s/../pardefs/%s.pcfg", env, pname);
printf(":opening:%s:\n",fnpath);
cfgfile=fopen(fnpath,"r");
while(1) {
  TRBIF *rcgrbif=NULL;
  char *okstr; char line[MAXLINECFG];
  okstr= fgets(line, MAXLINECFG,cfgfile);
  if(okstr==NULL) break;
  if(strncmp("RBIF",line,4) == 0){
   grbif=RBIF2Partition(line,grbif);
   if(grbif == NULL) {
     sprintf(errmsg,"ParseFile: RBIF2Partition error. line:%s",line);
     rc= 1;
   };
  } else if(strncmp("L0F34",line, 5) == 0){
     //rcgrbif= L0342Partition(line, grbif);
     if(rcgrbif == NULL) {
       sprintf(errmsg,"ParseFile: L0342Partition error."); 
       rc= 1;
     };

  };
};
fclose(cfgfile); vmeclose();
if(errmsg[0]!='\0') {
  infolog_trg(LOG_ERROR, errmsg);
} else {
  //char m4[LEN_l0f34+1];
  //printTRBIF(grbif);
  //combine34(grbif->lut34, m4);
};
return(rc);
}