AS3_Val setup_callback(lua_State * L, int index)
{
  LCALL(L, stack);

  SPAM(("setup_callback() : begin"));

  AS3_Val as3Function;
  int ref = 0;
  LuaFunctionCallbackData ** pUserdata = NULL;

  /*
    NOTE: Can't do lua_newuserdata() immediately
          since it would be deleted by Lua on lua_close()
  */
  /* TODO: Free this somewhere! When freeing, remove pData->ref from the state */
  LuaFunctionCallbackData * pData = malloc(sizeof(LuaFunctionCallbackData));

  index = LABSIDX(L, stack, index); /* Normalize index */

  /* TODO: Cache that with lua_ref, it is faster */
  lua_getfield(L, LUA_REGISTRYINDEX, AS3LUA_CALLBACKS);
  /* TODO: Assert this is a table */

  lua_newtable(L);

  LCHECK(L, stack, 2);

  lua_pushvalue(L, index); /* Store function */
  lua_rawseti(L, -2, AS3LUA_CBFNINDEX);

  LCHECK(L, stack, 2);

  pUserdata = lua_newuserdata(L, sizeof(LuaFunctionCallbackData *));
  *pUserdata = pData;

  luaL_getmetatable(L, AS3LUA_CALLBACKMT);
  lua_setmetatable(L, -2);
  lua_rawseti(L, -2, AS3LUA_CBSDATAINDEX);

  LCHECK(L, stack, 2);

  ref = luaL_ref(L, -2);

  LCHECK(L, stack, 1);

  lua_pop(L, 1); /* Pop AS3LUA_CALLBACKS */

  as3Function = AS3_Function(pData, as3_lua_callback);

  pData->ref = ref;
  pData->L = L;
  pData->as3Function = as3Function;

  LCHECK(L, stack, 0);

  SPAM(("setup_callback() : end"));

  return as3Function;
}
Exemple #2
0
/*
* Create an ActionScript value from the lua stack starting
* at index start and ending at index end.  If collapse_array == 1,
* an empty return will be transformed into AS3_Undefined() and a
* return of length 1 will just return the specific value.
* Otherwise an array is returned.
*/
AS3_Val create_as3_value_from_lua_stack(
    lua_State * L,
    int start,
    int end,
    BOOL collapse_array
  )
{
  /* WARNING: Panic alert! Use L*_FN checkers here! */

  LCALL(L, stack);
  AS3_Val ret;

  SPAM(("create_as3_value_from_lua_stack(): begin"));

  if (collapse_array == TRUE && start > end)
  {
    ret = AS3_Null();
  }
  else if (collapse_array == TRUE && start == end)
  {
    ret = get_as3_value_from_lua_stack(L, start);
  }
  else
  {
    int i;

    ret = AS3_Array("");
    for (i = start; i <= end; ++i)
    {
      AS3_Val value;
      AS3_Val r;

      /*SPAM(("create_as3_value_from_lua_stack() + 1 begin"));*/

      value = get_as3_value_from_lua_stack(L, i);
      r = AS3_CallTS("push", ret, "AS3ValType", value);
      SAFE_RELEASE(r); /* Ignoring result */

      SAFE_RELEASE(value);

      /*SPAM(("create_as3_value_from_lua_stack() + 1 end"));*/
    }
  }

#ifdef DO_SPAM
  SPAM(("create_as3_value_from_lua_stack(): end"));
  AS3_Trace(
      AS3_Call(
          getQualifiedClassName_method, NULL, AS3_Array("AS3ValType", ret)
        )
    );
#endif /* DO_SPAM */

  LCHECK_FN(L, stack, 0, fatal_error);

  return ret;
}
Exemple #3
0
int do_pcall_with_traceback(
    lua_State * L,
    int narg,
    int nresults,
    BOOL is_async /* Hackish. This may be too specific to lua-alchemy. */
  )
{
  /* WARNING: Panic alert! Use L*_FN checkers here! */

  SPAM((
      "do_pcall_with_traceback(): begin (%s)", (is_async) ? "async" : "sync"
    ));

  LCALL_ARGS(L, stack, 1 + narg); /* The function itself with arguments */
  int status = 0;

  BOOL old_async = set_async_state(L, is_async);

  lua_pushcfunction(L, db_errorfb);  /* push traceback function */
  lua_insert(L, LBASE(L, stack));  /* put it under chunk and args */

  LCHECK_FN(L, stack, 1 + narg + 1, fatal_error);

  SPAM(("do_pcall_with_traceback(): before call"));

  status = lua_pcall(L, narg, nresults, LBASE(L, stack));

  lua_remove(L, LBASE(L, stack));  /* remove traceback function */

  set_async_state(L, old_async);

  if (status != 0)
  {
    SPAM(("do_pcall_with_traceback(): after call ERROR"));

    /* force a complete garbage collection in case of errors */
    lua_gc(L, LUA_GCCOLLECT, 0);

    LCHECK_FN(L, stack, 1, fatal_error);
  }
  else
  {
    SPAM(("do_pcall_with_traceback(): after call OK"));

    if (nresults != LUA_MULTRET)
    {
      LCHECK_FN(L, stack, nresults, fatal_error);
    }
  }

  SPAM(("do_pcall_with_traceback(): end"));

  return status;
}
Exemple #4
0
/*
* Given an ActionScript object, push it onto the Lua stack as a Lua native
* type if a primitive class (String, Number, Boolean, int, null).
* If object is not convertible to native Lua value, do not push anything
* (and return 0).
*
* WARNING: It important that this function does not touch
*          non-primitive values (like Arrays). If this will be changed,
*          optional primitive autoconversion logic will break.
*/
int push_as3_to_lua_stack_if_convertible(lua_State * L, AS3_Val val)
{
  LCALL(L, stack);

#ifdef DO_SPAM
  SPAM(("push_as3_to_lua_stack_if_convertible(): begin: value, type"));
  AS3_Trace(val);
  AS3_Trace(
      AS3_Call(
          getQualifiedClassName_method, NULL, AS3_Array("AS3ValType", val)
        )
    );
#endif /* DO_SPAM */

  if (AS3_InstanceOf(val, Number_class))
  {
    lua_pushnumber(L, AS3_NumberValue(val));
  }
  else if (AS3_InstanceOf(val, int_class))
  {
    lua_pushinteger(L, AS3_IntValue(val));
  }
  else if (AS3_InstanceOf(val, String_class))
  {
    size_t length = 0;
    AS3_Malloced_Str str = get_string_bytes(val, &length);
    lua_pushlstring(L, str, length);
    free(str);
  }
  else if (AS3_InstanceOf(val, Boolean_class))
  {
    lua_pushboolean(L, AS3_IntValue(val));
  }
  else if (val == AS3_Undefined())
  {
    lua_pushnil(L);
  }
  else if (is_null(val))
  {
    lua_pushnil(L);
  }
  else
  {
    SPAM(("push_as3_to_lua_stack_if_convertible(): not convertible"));
    LRETURN(L, stack, 0);
  }

  SPAM(("push_as3_to_lua_stack_if_convertible(): end"));

  LRETURN(L, stack, 1);
}
Exemple #5
0
void for_chapter(
	void* udata,
	identifier chapter,
	git_time_t chapter_timestamp) {
	GDERP;
	SPAM("chapter %.*s %lu timestamp %d",
			 g->location.l,g->location.s,
			 chapter,chapter_timestamp - g->timestamp);
	g->any_chapter = true;
	//SPAM("chap %d:%d\n",chapter,chapter_timestamp);
	if(chapter_timestamp > g->max_timestamp)
		g->max_timestamp = chapter_timestamp;
	// this should be moved later...
	char srcname[0x100];
	struct stat srcinfo;
	int src;

	bool setupsrc(void) {
		snprintf(srcname,0x100,"chapter%ld.hish",chapter);
		src = openat(g->srcloc, srcname, O_RDONLY, 0755);
		ensure_ge(src,0);
		// for adjusting dest timestamp
		ensure0(fstatat(g->srcloc,srcname,&srcinfo,0));
		if(srcinfo.st_mtime > chapter_timestamp) {
			// git ruins file modification times... we probably cloned this, and lost
			// all timestamps. Just set the source file to have changed with the commit then.
			srcinfo.st_mtime = chapter_timestamp;
			return true;
		}
		return false;
	}
Exemple #6
0
/*
* Given an ActionScript object, push it onto the Lua stack as a Lua native
* type if a primitive class (String, Number, Boolean, int, null)
* or push as userdata
*/
int push_as3_to_lua_stack(lua_State * L, AS3_Val val)
{
  LCALL(L, stack);

  if (push_as3_to_lua_stack_if_convertible(L, val) == 0)
  {
    SPAM(("push_as3_to_lua_stack(): defaulting to userdata"));
    push_as3_lua_userdata(L, val);
  }

  LRETURN(L, stack, 1);
}
Exemple #7
0
static int lbsLS_readbytes(
    lbs_LoadState * ls,
    unsigned char * buf,
    size_t len
  )
{
  const unsigned char * pos = lbsLS_eat(ls, len);
  if (pos != NULL)
  {
    memcpy(buf, pos, len);
    return LUABINS_ESUCCESS;
  }
  SPAM(("load: Failed to read %lu bytes\n", (unsigned long)len));
  return LUABINS_EBADDATA;
}
int release_callback(lua_State * L)
{
  SPAM(("release_callback() : begin"));

  LuaFunctionCallbackData ** pUserdata = (LuaFunctionCallbackData **)luaL_checkudata(
      L, 1, AS3LUA_CALLBACKMT
    );
  if (pUserdata == NULL)
  {
    SPAM(("release_callback() : bad userdata"));
    return 0;
  }

  LuaFunctionCallbackData * pCallback = *pUserdata;
  if (pCallback == NULL)
  {
    SPAM(("release_callback() : bad callback"));
    return 0;
  }

  if (pCallback->L != NULL)
  {
    if (pCallback->as3Function != AS3_Undefined())
    {
      SPAM(("release_callback() : before release"));
      SAFE_RELEASE(pCallback->as3Function);
      SPAM(("release_callback() : after release"));
      pCallback->as3Function = AS3_Undefined();
    }

    pCallback->L = NULL;
  }

  *pUserdata = NULL;

  SPAM(("release_callback() : end"));

  return 0;
}
Exemple #9
0
//////////////////// main////////////////////////////////////////////////////
int getseqblock(char * argv[],int nproc,int iproc)
{
	clock_t start = clock();
	//printf("let's begin\n");
	//printf("sizeof(long unsinged int)=%ld,sizeof(int)=%ld\n",sizeof(long unsigned int),sizeof(int));
	char FileName[400];//name of output swig file

	strcpy(FileName, argv[1]);
	
	if (BlockSize >=8 && BlockSize <=32){
		SPAM(("BlockSize=%d\n",BlockSize));
	}
	else{
		printf("Wrong range of block size or search size! Should be from 8 to 32!\n");
		exit(1);
	}
	
	//for parallel
	int First; //first chrm to read
	int Last;
	int pflag; //indicate whether should start read
	if (nproc<=0){
		First = 0;
		Last = 32;//assume no more than 32 chrm
		pflag=1;
	}
	else{
		First = (24/nproc)*iproc; 
		if (iproc < nproc-1)
			Last = (24/nproc)*(iproc+1); 
		else
			Last = 32;
		pflag=0; 
	}
	
	
	//int N = 16;
	int Nd = 18;
	//int SetMaxLength = exp2(Nd-1); //set max contig length
	int SetMaxDiff = exp2(Nd); //set max value difference 
	int SetBlockSize = exp2(BlockSize); //number of diff that are encoded together
	//int SearchInd[(int)(1)<<IndexSize]; //auxilary table indicating how many times a code block is used for search, assume average contig length is 2^4, has the same number of elements as OffSet
	//memset(SearchInd, 0, ((int)(1)<<IndexSize)*sizeof(int));
	char ChrmNames[8*32]; //every chrm has a name less than 8 characters, assume no more than 32 chrm 
	memset(ChrmNames,0,8*32*sizeof(char));
	char *tmpChrmName;
	unsigned int DiffCount[SetMaxDiff]; //frequncy of each difference value of contigs
	memset(DiffCount,0,SetMaxDiff*sizeof(unsigned int));
	unsigned int LenDiffCount[SetMaxDiff]; //frequncy of each difference length of contigs
	memset(LenDiffCount,0,SetMaxDiff*sizeof(unsigned int));
	double DiffTable[SetMaxDiff]; //difference alphabet table
	memset(DiffTable,0,SetMaxDiff*sizeof(double));
	double LenDiffTable[SetMaxDiff]; //len diff alphabet table
	memset(LenDiffTable,0,SetMaxDiff*sizeof(double));
	int DiffSize=2, LenDiffSize=2; //Diff and LenDiff alphabet sizes, Talbe[0]=end_marker, Table[1]=exceed_marker
	DiffTable[0] = -(1<<30); //end marker
	LenDiffTable[0] = -(1<<30);
	DiffTable[1] = -(1<<29);//exceed marker
	LenDiffTable[1] = -(1<<29);

	
	double PreValue=-1;
	double Value=0;
	double Diff=1;//difference of values of contigs
	//int PreDiff=1;
	double NextDiff=1;
	double PreLocation=-1;
	double Location=-1;
	long unsigned int MaxLocation =10000;//the largest location. Not necessarily the last one, because the chromozones are not in order 
	double Contig = 1; //length of current contig
	double PreContig = 1; //length of previous contig
	double LenDiff=1; //length difference of two consecutive contigs
	//long int PreLenDiff=1; //length diff of previous two contigs
	int bcount=0; //counter inside each block
	int BlockCount=0; //counter for number of blocks
	long unsigned int BlockStart; //starting chrm location of a encode block
	long unsigned int NextBlockStart; // previous BlockStart
	int SearchInd; //number of search blocks in each code block
	//long unsigned int ChrmStart[100]; //assume at most 100 chromosomes, ChrmStart[i] stores the starting location of chrm i
	int ChrmCount=-1; //temp chrm count
 	//int ChrmNum=0; //number of chrms
	char tmp;
	char str[1000];  //annotation line
	char Prestr[1000]; //previous annotation line
    char tmpFileName[400];	
    fpos_t position;
	int i,j=0;
	int k=0;
	int tmpcount=0;
	int tmpint=0;
	int tmpflag=0;
	double tmpdouble=0;
	int warnflag=0; //if warning of float size has been printed
	
	//statistics of each block
	double min=1<<30, max=-(1<<30), mean=0; 
	double coverage=0, totalloc=0; //min and max value, sum of non-NaN basis, number of covered base pairs, number of total base pairs
	double std=0; //sum of squre of non-NaN basis
	double smry[32*6];//stats of every chrm
	for (i=0; i<32*6; i++)
		smry[i] = 0; //all set of 0
	for (i=0; i<32; i++){
		smry[6*i] = 1<<30; //min is a large number
		smry[6*i+1]= -(1<<30); //max is set to be small
	}

	
	
	//printf("let's begin\n");
	//printf("test Count[0~4] %lu %lu %lu %lu %lu", Count[0],Count[1],Count[2],Count[3],Count[4]);
	//printf("sizeof Location= %lu\n", sizeof(Location));

	//input
	strcpy(tmpFileName,FileName);
	FILE *fp;
	fp = fopen(tmpFileName, "r");
	if (fp == NULL) {
		fprintf(stderr, "Can't open input file %s!\n",tmpFileName);
		exit(1);
	}
	//output diffseq
	strcpy(tmpFileName,FileName);
	strcat(tmpFileName,"DiffSeq");
	if (nproc>0){
		tmpint = sprintf(str,"%02d",iproc);
		strcat(tmpFileName,str);
	}
	FILE *fpDiffSeq = fopen(tmpFileName, "w+");
	if (fpDiffSeq == NULL) {
		fprintf(stderr, "Can't open file %s!\n",tmpFileName);
		exit(1);
	}
	//output lendiffseq
	strcpy(tmpFileName,FileName);
	strcat(tmpFileName,"LenDiffSeq");
	if (nproc>0){
		tmpint = sprintf(str,"%02d",iproc);
		strcat(tmpFileName,str);
	}
	FILE *fpLenDiffSeq = fopen(tmpFileName, "w+");
	if (fpLenDiffSeq == NULL) {
		fprintf(stderr, "Can't open file %s!\n",tmpFileName);
		exit(1);
	}

	
	//output start, for preloc, 
	strcpy(tmpFileName,FileName);
	strcat(tmpFileName,"Start");
	if (nproc>0){
		tmpint = sprintf(str,"%02d",iproc);
		strcat(tmpFileName,str);
	}
	FILE *fpStart = fopen(tmpFileName, "wb");
	if (fpStart  == NULL) {
		fprintf(stderr, "Can't open file %s!\n",tmpFileName);
		exit(1);
	}
	//output prevalue, (Pre)contig
	strcpy(tmpFileName,FileName);
	strcat(tmpFileName,"Pre");
	if (nproc>0){
		tmpint = sprintf(str,"%02d",iproc);
		strcat(tmpFileName,str);
	}
	FILE *fpPre = fopen(tmpFileName, "wb");
	if (fpPre == NULL) {
		fprintf(stderr, "Can't open file %s!\n",tmpFileName);
		exit(1);
	}
	//output summary info: min, max, mean, std, coverage, totalloc
	strcpy(tmpFileName,FileName);
	strcat(tmpFileName,"Smry");
	if (nproc>0){
		tmpint = sprintf(str,"%02d",iproc);
		strcat(tmpFileName,str);
	}
	FILE *fpSmry = fopen(tmpFileName, "wb");
	if (fpSmry  == NULL) {
		fprintf(stderr, "Can't open file %s!\n",tmpFileName);
		exit(1);
	}
	//output blockaux
	strcpy(tmpFileName,FileName);
	strcat(tmpFileName,"BlockAux");
	if (nproc>0){
		tmpint = sprintf(str,"%02d",iproc);
		strcat(tmpFileName,str);
	}
	FILE *fpBlockAux = fopen(tmpFileName, "w");
	if (fpBlockAux  == NULL) {
		fprintf(stderr, "Can't open file %s!\n",tmpFileName);
		exit(1);
	}
	//put a dummy 0 at the beginning
	fprintf(fpBlockAux,"0 ");
	
	
	//output result
	FILE *fpResult=fopen("result","a");
	if (fpResult == NULL) {
		fprintf(stderr,"Can't open output result file!\n");
		exit(1);
	}
	SPAMR((fpResult,"\n//combine/getseqblock.c\n//%s\n",FileName));
	
	
	
	
	SPAM(("===============================\ngetseqblock.c\n"));
	
	//start processing input file
	strcpy(Prestr, "empty");
	while (!feof(fp)){		
		tmp = fgetc(fp);
		if  (tmp >= '0' && tmp <= '9'){
			if (!pflag)
				fgets(str,sizeof(str),fp);
			else{
				ungetc(tmp,fp);
				fgetpos (fp, &position);
				fscanf(fp,"%lf %lf\n", &Location,&Value);
				//Value = (round)(tmpdouble*SetFloatSize); //in case Value is stored in a floating point manner
				/*if (Value - tmpdouble*SetFloatSize > (double)1/SetfloatSize){
					if (warnflag == 0){
						fprintf(stderr,"WARNING: THE FILE CONTAINS VALUE=%g WITH PRECISION HIGHER THAN FLOATSIZE=%d! INCREASE FLOATSIZE! diff=%g\n",tmpdouble,(int)(log10(SetFloatSize)),Value - tmpdouble*SetFloatSize);
						warnflag=1;
					}
				}*/
				
				if (Location > MaxLocation)
					MaxLocation = Location;
				if (Location != PreLocation+1){//for contig before 0s
					Value = 0;
				}
				if (Value != PreValue){
					//stats
					if (PreValue != 0 && PreLocation > 0){
						if (PreValue < min)
							min = PreValue;
						if (PreValue > max)
							max = PreValue;
						mean += PreValue*Contig;
						std += (double)PreValue*PreValue*Contig;
						coverage += Contig;
					}
					totalloc += Contig;  
					/*if (k<5){
						printf("line %d: Value %d, PreLocation %d, Location %d, Contig %d min1 %d, max1 %d, mean1 %d, std1 %ld, coverage1 %d, totoalloc1 %d\n",
						        k,       Value, PreLocation, Location, Contig, min,max,mean,std,coverage,totalloc);
					}
					k++;*/
					//record
					record(Value, PreValue, PreLocation, Location, SetMaxDiff, SetBlockSize, 
						&Diff, &NextDiff, &Contig, &PreContig, &LenDiff, 
						&bcount, &BlockCount, DiffCount, LenDiffCount, DiffTable, LenDiffTable, &DiffSize, &LenDiffSize, fpLenDiffSeq, fpDiffSeq, 
						fpStart, fpPre, fpSmry, &BlockStart, &NextBlockStart, 
						&min, &max, &mean, &std, &coverage, &totalloc, smry, ChrmCount);
				}
				else{//Diff == 0
						Contig++;
				}
				
				if (Location != PreLocation+1){
					//record skipped 0s, push back the line
					fsetpos (fp, &position);
					PreValue = 0;
					if (Location<PreLocation){
						printf("Location %lg < PreLocation %lg!\n",Location, PreLocation);
						exit(1);
					}
					Contig = Location - PreLocation-1;
					PreLocation = Location-1;		
				}				
				else{
					PreValue = Value;
					PreLocation = Location;			
				}		
			}
		}
		else{ //get a line of annotations
			ungetc(tmp,fp);
			fgets(str,sizeof(str),fp); 
			if (strcmp(str, Prestr)){ //different chrm
				//old chrm
				if (ChrmCount>=0 && pflag){ 
					//insert a virtual data point and then record
					PreValue = Value;
					Value++;
					PreLocation = Location;
					Location++;
					record(Value, PreValue, PreLocation, Location, SetMaxDiff, SetBlockSize, 
						&Diff, &NextDiff, &Contig, &PreContig, &LenDiff, 
						&bcount, &BlockCount, DiffCount, LenDiffCount, DiffTable, LenDiffTable, &DiffSize, &LenDiffSize, fpLenDiffSeq, fpDiffSeq, 
						fpStart, fpPre, fpSmry, &BlockStart, &NextBlockStart, 
						&min, &max, &mean, &std, &coverage, &totalloc, smry, ChrmCount);
					//end marker of a chrom
					//fprintf(fpLenDiffSeq,"%d ", -(1<<30));
					//fprintf(fpDiffSeq,"%d ",-(1<<30));
					tmpdouble = -(1<<30);
					fwrite(&tmpdouble,sizeof(double),1,fpLenDiffSeq);
					fwrite(&tmpdouble,sizeof(double),1,fpDiffSeq);
	
					bcount++;
					if (bcount==1){//beginning of a block
						BlockCount++;
					}
					//pad 0s to fill a code block
					tmpint=(SetBlockSize-bcount)%SetBlockSize; //number of padded 0s
					for (j=0; j<tmpint; j++){
						//fprintf(fpDiffSeq,"1 ");
						//fprintf(fpLenDiffSeq,"0 ");
						tmpdouble=0;
						fwrite(&tmpdouble,sizeof(double),1,fpLenDiffSeq);
						tmpdouble = 1;
						fwrite(&tmpdouble,sizeof(double),1,fpDiffSeq);
					}
					DiffCount[mydiff(1)]+=tmpint;
					LenDiffCount[mydiff(0)]+=tmpint;
					if(tmpint){//search index of the last block
						BlockStart=NextBlockStart;
						NextBlockStart=Location;
					}
					//number of blocks so far
					fprintf(fpBlockAux,"%d ",BlockCount);
					//stats of the last block of the chrm
					summary(&min,&max,&mean,&std,&coverage,&totalloc,smry, ChrmCount, fpSmry);
				}	
				//new chrm
				//printf("chrm %d, %s", ChrmCount, str);
				strcpy(Prestr, str);
				ChrmCount++;
				if (ChrmCount>=32){
					printf("Too many chromosomes!\n");
					exit(1);
				}
				if (ChrmCount == Last)
					break;
				if (ChrmCount == First)
					pflag = 1;	
				if (pflag){
					tmpChrmName = strstr(str, "chrom=");
					if (tmpChrmName == NULL){
						printf("Wrong annotation line: %s, must contain chromosome name!\n", str);
						exit(1);
					}
					j=6;
					while (tmpChrmName[j]!=' ' && j<14){
						ChrmNames[8*ChrmCount+j-6]=tmpChrmName[j];
						j++;
					}
					
					//first (PreValue,PreContig,PreLocation) for each chrm
					
					tmpdouble=-2;//prevalue
					fwrite(&tmpdouble, sizeof(double),1,fpPre);
					tmpdouble=1;//precontig
					fwrite(&tmpdouble, sizeof(double),1,fpPre);
					tmpdouble=-1;//prelocation
					fwrite(&tmpdouble, sizeof(double),1,fpStart);
					//printf("fpstart first elt %d\n",tmpstart);
					
					
					//starting index
					//reset parameters
					PreValue=-1;
					Value=0;
					Diff=1;
					NextDiff=1;
					PreLocation=-1;
					Location=-1;
					Contig = 1; 
					PreContig = 1; 
					LenDiff=1;
					bcount=0;
					BlockStart=0;
					NextBlockStart=0; 
				}
			}
		}
	}
	//ending part of last chrm, only process it if nonparelle, or if it is the last processor
	if (Location>=0 && (nproc<=0 || (nproc>0 && iproc==nproc-1))){ 
		//printf("ending of chrm %d\n",ChrmCount);
		//insert a virtual data point and then record
		PreValue = Value;
		Value++;
		PreLocation = Location;
		Location++;
		record(Value, PreValue, PreLocation, Location, SetMaxDiff, SetBlockSize, 
			&Diff, &NextDiff, &Contig, &PreContig, &LenDiff, 
			&bcount, &BlockCount, DiffCount, LenDiffCount, DiffTable, LenDiffTable, &DiffSize, &LenDiffSize, fpLenDiffSeq, fpDiffSeq, 
			fpStart, fpPre, fpSmry, &BlockStart, &NextBlockStart, 
			&min, &max, &mean, &std, &coverage, &totalloc, smry, ChrmCount);
		//end marker of a chrom, Table[0]=marker=-(1<<30);
		//fprintf(fpLenDiffSeq,"%d ", -(1<<30));
		//fprintf(fpDiffSeq,"%d ",-(1<<30));
		tmpdouble = -(1<<30);
		fwrite(&tmpdouble,sizeof(double),1,fpLenDiffSeq);
		fwrite(&tmpdouble,sizeof(double),1,fpDiffSeq);
		bcount++;
		if (bcount==1){//beginning of a block
			BlockCount++;
		}
		//pad 0s to fill a code block
		tmpint=(SetBlockSize-bcount)%SetBlockSize; //number of padded 0s
		for (j=0; j<tmpint; j++){
			//fprintf(fpDiffSeq,"1 ");
			//fprintf(fpLenDiffSeq,"0 ");
			tmpdouble=0;
			fwrite(&tmpdouble,sizeof(double),1,fpLenDiffSeq);
			tmpdouble = 1;
			fwrite(&tmpdouble,sizeof(double),1,fpDiffSeq);
		}
		DiffCount[mydiff(1)]+=tmpint;
		LenDiffCount[mydiff(0)]+=tmpint;
		if(tmpint){//search index of the last block
			BlockStart=NextBlockStart;
			NextBlockStart=Location;
		}
		//number of blocks so far
		fprintf(fpBlockAux,"%d ",BlockCount);
		//stats of the last block of the chrm
		summary(&min,&max,&mean,&std,&coverage,&totalloc,smry, ChrmCount, fpSmry);
	}
	ChrmCount++;
	if (Last>ChrmCount)
		Last = ChrmCount;
	
	//open output files 1
	strcpy(tmpFileName,FileName);
	strcat(tmpFileName,"Diff");
	if (nproc>0){
		tmpint = sprintf(str,"%02d",iproc);
		strcat(tmpFileName,str);
	}
	FILE *fpDiff = fopen(tmpFileName,"w");
	if (fpDiff == NULL) {
		fprintf(stderr,"Can't open output Count file!\n");
		exit(1);
	}
	strcpy(tmpFileName,FileName);
	strcat(tmpFileName,"LenDiff");
	if (nproc>0){
		tmpint = sprintf(str,"%02d",iproc);
		strcat(tmpFileName,str);
	}
	FILE *fpLenDiff = fopen(tmpFileName,"w");
	if (fpLenDiff == NULL) {
		fprintf(stderr,"Can't open output Count file!\n");
		exit(1);
	}
	//write to output files 1
	if (nproc<0 || iproc==0){//if non parallel or if first processor
		DiffCount[1] = 1;//exceed marker
		LenDiffCount[1] = 1; 
		DiffCount[0] = 1;//end marker of a chrm
		LenDiffCount[0] = 1; 
	}
	else{
		DiffCount[1] = 0;//exceed marker
		LenDiffCount[1] = 0; 
		DiffCount[0] = 0;//end marker of a chrm
		LenDiffCount[0] = 0; 
	}

	
	fprintf(fpDiff,"#Difference\tCount\n");
	fprintf(fpLenDiff,"#Difference of Length\tCount\n");
	for (i=0; i<DiffSize; i++){
		//fprintf(fpDiff,"%d\t%u\n", DiffTable[i],DiffCount[i]);
		fwrite(&DiffTable[i],sizeof(double),1,fpDiff);
		fwrite(&DiffCount[i],sizeof(int),1,fpDiff);
	}
	for (i=0; i<LenDiffSize; i++){
		//fprintf(fpLenDiff,"%d\t%u\n", LenDiffTable[i],LenDiffCount[i]);
		fwrite(&LenDiffTable[i],sizeof(double),1,fpLenDiff);
		fwrite(&LenDiffCount[i],sizeof(int),1,fpLenDiff);
	}
	
	//output file chrmname
	strcpy(tmpFileName,FileName);
	strcat(tmpFileName,"ChrmName");
	if (nproc>0){
		tmpint = sprintf(str,"%02d",iproc);
		strcat(tmpFileName,str);
	}
	FILE *fpName = fopen(tmpFileName,"w");
	if (fpName == NULL) {
		fprintf(stderr,"Can't open output file %s!\n",tmpFileName);
		exit(1);
	}
	//for (i=0; i<ChrmCount; i++){
	for (i=First; i<Last; i++){
		fprintf(fpName,"%s ",&ChrmNames[8*i]);
	}

	fclose(fpName);
	
	
	//output file SmryAll
	strcpy(tmpFileName,FileName);
	strcat(tmpFileName,"SmryAll");
	if (nproc>0){
		tmpint = sprintf(str,"%02d",iproc);
		strcat(tmpFileName,str);
	}
	FILE *fpSmryAll = fopen(tmpFileName,"wb");
	if (fpSmryAll == NULL) {
		fprintf(stderr,"Can't open output file %s!\n",tmpFileName);
		exit(1);
	}
	fwrite(&smry[First*6],sizeof(double), (Last-First)*6,fpSmryAll);
	fclose(fpSmryAll);	
	

	
	

	
	clock_t end = clock();
	SPAMR((fpResult,"Time for processing file in seconds\n%2.3f\nProcess index iproc=%d, nproc=%d\n", (double)(end-start)/CLOCKS_PER_SEC,iproc,nproc));
	//printf("Time for processing file %2.3f sec.\n", (double)(end-start)/CLOCKS_PER_SEC);
	//fprintf(fpResult,"NoExceedValue=%d, NoExceededLength=%d\n",ExceededValue,ExceededLength);
	//fprintf(fpResult,"BlockSize=%d, SearchSize=%d\n",BlockSize, SearchSize);
	
	
	
	
	fclose(fp);
	fclose(fpDiffSeq);
	fclose(fpLenDiffSeq);
	fclose(fpDiff);
	fclose(fpLenDiff);
	fclose(fpResult);
	fclose(fpBlockAux);
	fclose(fpStart);
	fclose(fpSmry);
	fclose(fpPre);
	
	return 0;


}
Exemple #10
0
int fremove(int opt, char * argv[], int nproc) {
	
	char FileName[1000];//name of processed Wig file
	char tmpchar[1000], str[2];
	
	clock_t start = clock();
	SPAM(("=================\nfremove start! opt=%d\n",opt));
	
	strcpy(FileName, argv[1]);
	
	
	
	
	char Type[20][40];
	char c[20][10000];
	int i,j;
	
	
	for (i=0; i<20 && list[opt][i][0]; i++){
		for (j=0; j<40; j++){
			Type[i][j] = list[opt][i][j];
		}
	}
	int N=i;
	//extra files to delete
	char dlist[2][40]={"DiffSeqMatlabDecode","LenDiffSeqMatlabDecode"};
	for (i=0; i<2; i++){
		for (j=0; j<40; j++){
			Type[i+N][j] = dlist[i][j];
		}
		//printf("type %d is %s\n",i+N,Type[i+N]);
	}

	
	i=1;
	while (i<20 && Type[i][0]){
		strcpy(c[i],FileName);
		strcat(c[i],Type[i]);
		if (opt==3 && i>=N){
			for (j=0;j<nproc;j++){
				strcpy(tmpchar,c[i]);
				sprintf(str,"%02d",j);
				strcat(tmpchar,str);
				remove(tmpchar);
			}
		}
		else		
			remove(c[i]);
		//printf("remove file %s\n",Type[i]);
		i++;
	}
	
	i=0;
	if (opt==3){ //mergesub of the output wig file
		strcpy(c[i],argv[2]);
		fmergesub(nproc,c[i]);
	}
	
	
	clock_t end = clock();
	SPAM(("fremove takes %2.3f sec.\n", (double)(end - start)/CLOCKS_PER_SEC));
		
	
	return 1;
	
}
Exemple #11
0
int luabins_load(
    lua_State * L,
    const unsigned char * data,
    size_t len,
    int * count
  )
{
  lbs_LoadState ls;
  int result = LUABINS_ESUCCESS;
  unsigned char num_items = 0;
  int base = 0;
  int i = 0;

  base = lua_gettop(L);

  lbsLS_init(&ls, data, len);
  num_items = lbsLS_readbyte(&ls);
  if (!lbsLS_good(&ls))
  {
    SPAM(("load: failed to read num_items byte\n"));
    result = LUABINS_EBADDATA;
  }
  else if (num_items > LUABINS_MAXTUPLE)
  {
    SPAM(("load: tuple too large: %d\n", (int)num_items));
    result = LUABINS_EBADSIZE;
  }
  else
  {
    XSPAM(("* load: tuple size %d\n", (int)num_items));
    for (
        i = 0;
        i < num_items && result == LUABINS_ESUCCESS;
        ++i
      )
    {
      XSPAM(("* load: loading tuple item %d\n", i));
      result = load_value(L, &ls);
    }
  }

  if (result == LUABINS_ESUCCESS && lbsLS_unread(&ls) > 0)
  {
    SPAM(("load: %lu chars left at tail\n", lbsLS_unread(&ls)));
    result = LUABINS_ETAILEFT;
  }

  if (result == LUABINS_ESUCCESS)
  {
    *count = num_items;
  }
  else
  {
    lua_settop(L, base); /* Discard intermediate results */
    switch (result)
    {
    case LUABINS_EBADDATA:
      lua_pushliteral(L, "can't load: corrupt data");
      break;

    case LUABINS_EBADSIZE:
      lua_pushliteral(L, "can't load: corrupt data, bad size");
      break;

    case LUABINS_ETAILEFT:
      lua_pushliteral(L, "can't load: extra data at end");
      break;

    default: /* Should not happen */
      lua_pushliteral(L, "load failed");
      break;
    }
  }

  return result;
}
Exemple #12
0
static int load_value(lua_State * L, lbs_LoadState * ls)
{
  int result = LUABINS_ESUCCESS;
  unsigned char type = lbsLS_readbyte(ls);
  if (!lbsLS_good(ls))
  {
    SPAM(("load: Failed to read value type byte\n"));
    return LUABINS_EBADDATA;
  }

  XSPAM(("* load: begin load_value\n"));

  luaL_checkstack(L, 1, "load_value");

  switch (type)
  {
  case LUABINS_CNIL:
    XSPAM(("* load: nil\n"));
    lua_pushnil(L);
    break;

  case LUABINS_CFALSE:
    XSPAM(("* load: false\n"));
    lua_pushboolean(L, 0);
    break;

  case LUABINS_CTRUE:
    XSPAM(("* load: true\n"));
    lua_pushboolean(L, 1);
    break;

  case LUABINS_CNUMBER:
    {
      lua_Number value;

      XSPAM(("* load: number\n"));

      result = lbsLS_readbytes(ls, (unsigned char *)&value, LUABINS_LNUMBER);
      if (result == LUABINS_ESUCCESS)
      {
        lua_pushnumber(L, value);
      }
    }
    break;

  case LUABINS_CSTRING:
    {
      size_t len = 0;

      XSPAM(("* load: string\n"));

      result = lbsLS_readbytes(ls, (unsigned char *)&len, LUABINS_LSIZET);
      if (result == LUABINS_ESUCCESS)
      {
        const unsigned char * pos = lbsLS_eat(ls, len);

        XSPAM(("* load: string size %u\n", (int)len));

        if (pos != NULL)
        {
          lua_pushlstring(L, (const char *)pos, len);
        }
        else
        {
          result = LUABINS_EBADSIZE;
        }
      }
    }
    break;

  case LUABINS_CTABLE:
    XSPAM(("* load: table\n"));
    result = load_table(L, ls);
    break;

  default:
    SPAM(("load: Unknown type char 0x%02X found\n", type));
    result = LUABINS_EBADDATA;
    break;
  }

  XSPAM(("* load: end load_value\n"));

  return result;
}
Exemple #13
0
static int load_table(lua_State * L, lbs_LoadState * ls)
{
  int array_size = 0;
  int hash_size = 0;
  unsigned int total_size = 0;

  int result = lbsLS_readbytes(ls, (unsigned char *)&array_size, LUABINS_LINT);
  if (result == LUABINS_ESUCCESS)
  {
    result = lbsLS_readbytes(ls, (unsigned char *)&hash_size, LUABINS_LINT);
  }

  if (result == LUABINS_ESUCCESS)
  {
    total_size = array_size + hash_size;
/*
    SPAM((
        "LT SIZE CHECK\n"
        "* array_size %d limit 0 .. %d\n"
        "* hash_size %d limit >0\n"
        "* hash_size bytes %d, limit %d\n"
        "* unread %u limit >min_size %u (total_size %u)\n",
        array_size, MAXASIZE,
        hash_size,
        ceillog2((unsigned int)hash_size), MAXBITS,
        (unsigned int)lbsLS_unread(ls),
        (unsigned int)luabins_min_table_data_size(total_size),
        (unsigned int)total_size
      ));
*/
    if (
        array_size < 0 || array_size > MAXASIZE ||
        hash_size < 0  ||
        (hash_size > 0 && ceillog2((unsigned int)hash_size) > MAXBITS) ||
        lbsLS_unread(ls) < luabins_min_table_data_size(total_size)
      )
    {
      result = LUABINS_EBADSIZE;
    }
  }

  if (result == LUABINS_ESUCCESS)
  {
    unsigned int i = 0;

    XSPAM((
        "* load: creating table a:%d + h:%d = %d\n",
        array_size, hash_size, total_size
      ));

    lua_createtable(L, array_size, hash_size);

    for (i = 0; i < total_size; ++i)
    {
      int key_type = LUA_TNONE;

      result = load_value(L, ls); /* Load key. */
      if (result != LUABINS_ESUCCESS)
      {
        break;
      }

      /* Table key can't be nil or NaN */
      key_type = lua_type(L, -1);
      if (key_type == LUA_TNIL)
      {
        /* Corrupt data? */
        SPAM(("load: nil as key detected\n"));
        result = LUABINS_EBADDATA;
        break;
      }

      if (key_type == LUA_TNUMBER)
      {
        lua_Number key = lua_tonumber(L, -1);
        if (luai_numisnan(key))
        {
          /* Corrupt data? */
          SPAM(("load: NaN as key detected\n"));
          result = LUABINS_EBADDATA;
          break;
        }
      }

      result = load_value(L, ls); /* Load value. */
      if (result != LUABINS_ESUCCESS)
      {
        break;
      }

      lua_rawset(L, -3);
    }
  }

  return result;
}
Exemple #14
0
/*
* Take the Lua stack item at index i and convert it into an
* ActionScript value.
*/
AS3_Val get_as3_value_from_lua_stack_type(lua_State * L, int i, int type)
{
  /* WARNING: Panic alert! Use L*_FN checkers here! */

  LCALL(L, stack);

  AS3_Val value;
  switch (type)
  {
    case LUA_TSTRING:  /* strings */
      {
        size_t length = 0;
        const char * str = lua_tolstring(L, i, &length);
        if (str == NULL)  /* NOTE: This is unreachable. Assert instead */
        {
          length = 6;
          str = "(null)";
        }
        /* NOTE: Alchemy .5a truncates embedded zeroes in string
        * regardless to the passed length
        */
        value = AS3_StringN(str, length);
      }
      break;

    case LUA_TBOOLEAN:  /* booleans */
      value = lua_toboolean(L, i) ? AS3_True() : AS3_False();
      break;

    case LUA_TNUMBER:  /* numbers */
      value = AS3_Number(lua_tonumber(L, i));
      break;

    case LUA_TNONE: /* fall through */
    case LUA_TNIL:  /* nil */
      value = AS3_Null();
      break;

    case LUA_TUSERDATA:  /* userdata */
      {
        void * userdata = lua_touserdata(L, i);
        lua_getfield(L, LUA_REGISTRYINDEX, AS3LUA_METATABLE);
        if (userdata == NULL || !lua_getmetatable(L, i))
        {
          lua_pop(L, 1); /* Pop AS3LUA_METATABLE */
          value = as3_value_from_foreign_userdata(L, i);
        }
        else if (!lua_rawequal(L, -2, -1))
        {
          lua_pop(L, 2); /* Pop AS3LUA_METATABLE and userdata metatable */
          value = as3_value_from_foreign_userdata(L, i);
        }
        else
        {
          lua_pop(L, 2); /* Pop AS3LUA_METATABLE and userdata metatable */
          AS3LuaUserData * userdata = (AS3LuaUserData *)lua_touserdata(L, i);
          value = userdata->value;
          /*
          * We just created one more reference to the AS3 value,
          * as it still lives inside Lua.
          * (And will probably be collected by GC.)
          */
          AS3_Acquire(value);
        }
      }
      break;

    case LUA_TFUNCTION: /* function */
      value = setup_callback(L, i);
      break;

    case LUA_TLIGHTUSERDATA: /* TODO: blackbox this type */
    case LUA_TTABLE: /* TODO: deal with this type */
    case LUA_TTHREAD: /* TODO: blackbox this type */
      value = AS3_String(lua_typename(L, type));
      break;

    default:  /* unreachable */
      fatal_error("unknown Lua type");
      break;
  }

#ifdef DO_SPAM
  SPAM(("get_as3_value_from_lua_stack(): end"));
  AS3_Trace(
      AS3_Call(
          getQualifiedClassName_method, NULL, AS3_Array("AS3ValType", value)
        )
    );
#endif /* DO_SPAM */

  LCHECK_FN(L, stack, 0, fatal_error);

  return value;
}
Exemple #15
0
bool parseSocket(_CLIENT* client, sqlite3* db, int announceInterval){
	char buf[1024];
	char* buffer=buf;
	int size,c;
	bool handled=false;
	ANNOUNCE_REQUEST ar;
	socklen_t socklen;
	struct sockaddr_storage conn;
	
	size=recv(client->socket,buf,sizeof(buf)-1,MSG_PEEK);
	if(size==0||size==sizeof(buf)-1){
		ERROR(printf("Erroneous link\n"));
		handled=true;
	}
	else{
		buf[size]=0;
		SPAM(printf("%d bytes of data received\n",size));
		if(!strncmp(buffer,"GET /",5)){
			buffer+=5;
			//http client
			if(!strncmp(buffer,"announce",8)){
				buffer+=8;
				//announce request, parse
				memset(&ar,0,sizeof(ANNOUNCE_REQUEST));
				socklen=sizeof(struct sockaddr_storage);
				if(getpeername(client->socket,(struct sockaddr*)&conn,&socklen)!=-1){
					//check mandatory arguments
					c=charIndex((unsigned char*)buffer,' ');
					if(c!=-1){
						buffer[c]=0;//terminate buffer after request string
						char* hash=textAfter(buffer,"info_hash=");
						char* peerid=textAfter(buffer,"peer_id=");
						char* port=textAfter(buffer,"port=");
						char* event=textAfter(buffer,"event=");
						char* numwant=textAfter(buffer,"numwant=");
						char* left=textAfter(buffer,"left=");
						
						if(hash&&peerid&&port){
							//get compact flag
							ar.compact=false;
							if(strstr(buffer,"compact=1")){
								ar.compact=true;
							}
							
							//get no_peer_id flag
							ar.no_peer_id=false;
							if(strstr(buffer,"no_peer_id=1")){
								ar.no_peer_id=true;
							}
							
							//get event data
							ar.event=NONE;
							if(event&&(!strncmp(event,"st",2)||!strncmp(event,"co",2))){
								switch(event[2]){
									case 'a':
										ar.event=STARTED;
										break;
									case 'o':
										ar.event=STOPPED;
										break;
									case 'm':
										ar.event=COMPLETED;
										break;
								}
							}
							
							//calculate parameter lengths
							int hash_len=httpParamLength(hash);
							int peerid_len=httpParamLength(peerid);
							
							if(decodedStrlen(hash,hash_len)==MAX_HASH_LEN&&decodedStrlen(peerid,peerid_len)==MAX_HASH_LEN){
								strncpy(ar.info_hash,hash,hash_len);//FIXME TODO ensure string termination
								strncpy(ar.peer_id,peerid,peerid_len);
								ar.port=strtoul(port,NULL,10);
								
								if(numwant){
									ar.numwant=strtoul(numwant,NULL,10);
								}
								if(ar.numwant>MAXPEERS_SENT||ar.numwant<=0){//there are clients that actually send 0 here?
									ar.numwant=MAXPEERS_SENT;
								}
								
								ar.left=0;
								if(left){
									ar.left=strtoul(left,NULL,10);
								}
								
								if(conn.ss_family==AF_INET6){
									inet_ntop(conn.ss_family,&(((struct sockaddr_in6*)&conn)->sin6_addr),ar.ip,INET6_ADDRSTRLEN);
									ar.protover=6;
								}
								else{
									inet_ntop(conn.ss_family,&(((struct sockaddr_in*)&conn)->sin_addr),ar.ip,INET6_ADDRSTRLEN);
									ar.protover=4;
								}
								
								//DEBUG(printf("info_hash: %s, peer_id: %s, addr %s, port %d\n",ar.info_hash,ar.peer_id,ar.ip,ar.port));
								
								track(client->socket, db, &ar, announceInterval);
								handled=true;
							}
							else{
								//vital parameters out of bounds
								if(client->recv_iter>=MAX_RECV_ITER){
									sendHttpHeaders(client->socket,"400 Bad Syntax","X-Failure-Reason: Failed Parameter Validation\r\n");
								}
							}
						}
						else{
							//request is missing vital data
							if(client->recv_iter>=MAX_RECV_ITER){
								sendHttpHeaders(client->socket,"408 Timed out","X-Failure-Reason: Missing parameters\r\n");
							}
						}
					}
					else{
						//request was not properly terminated
						if(client->recv_iter>=MAX_RECV_ITER){
							sendHttpHeaders(client->socket,"400 Bad Syntax","X-Failure-Reason: Thats just plain wrong\r\n");
						}
					}
				}
				else{
					ERROR(printf("Call to getpeername failed\n"));
					handled=true;
				}
			}
			else if(!strncmp(buffer, "scrape", 6)){
				buffer+=6;
				//scrape request
				c=charIndex((unsigned char*)buffer, ' ');
				if(c!=-1){
					buffer[c]=0;
					char* hash=textAfter(buffer,"info_hash=");
					if(!hash){
						scrape(client->socket, db, NULL);
						handled=true;
					}
					else{
						//get hash
						int hash_len=httpParamLength(hash);
						if(decodedStrlen(hash,hash_len)==MAX_HASH_LEN){
							unsigned char hashbuf[MAX_URLENC_HASH_LEN+1];
							memcpy(hashbuf,hash,hash_len);
							hashbuf[hash_len]=0;
							destructiveURLDecode(hashbuf);
							hashEncodeHex(hashbuf,(sizeof(hashbuf)/sizeof(unsigned char))-1);
							scrape(client->socket, db, hashbuf);
							handled=true;
						}
					}
				}
				else{
					//request was not properly terminated
					if(client->recv_iter>=MAX_RECV_ITER){
						sendHttpHeaders(client->socket,"400 Bad Syntax","X-Failure-Reason: Thats just plain wrong\r\n");
					}
				}
			}
			else{
				//some other http
				sendHttpHeaders(client->socket,"200 OK",STANDARD_HEADER);
				for(c=0;c<10;c++){
					sendString(client->socket,"Making Milhouse cry is not a science project\r\n");
				}
				handled=true;
			}
		}
		else{
			//non-http client or wrong operation
			sendHttpHeaders(client->socket,"405 Not supported",STANDARD_HEADER);
			handled=true;
		}
	}
	
	if(handled||client->recv_iter>=MAX_RECV_ITER){
		handled=true;
		//flush kernel buffers
		recv(client->socket,buf,sizeof(buf)-1,0);
	}
	return handled;
}
Exemple #16
0
/*
* Function used as a callback for all Lua functions passed through
* get_as3_value_from_lua_stack()
*/
AS3_Val as3_lua_callback(void * data, AS3_Val args)
{
  /* WARNING: Panic alert! Use L*_FN checkers here! */

  SPAM(("as3_lua_callback(): begin"));

  AS3_Val res;
  LuaFunctionCallbackData * func_data = (LuaFunctionCallbackData *) data;
  int nargs = 0;
  int status = 0;
  int results_base = 0;
  lua_State * L = func_data->L;
  if (L == NULL)
  {
    /* TODO: Should we crash here?
    fatal_error("state expired"); / * Does not return * /
    */
    sztrace("as3_lua_callback: state expired");
    return AS3_Undefined();
  }

  { /* A new scope for LCALL to work (C89 conformance) */
    LCALL(L, stack);

    /* TODO: Cache that with lua_ref, it is faster */
    lua_getfield(L, LUA_REGISTRYINDEX, AS3LUA_CALLBACKS);

    /* TODO: Assert we have a table here */

    lua_rawgeti(L, -1, func_data->ref); /* push stored function */

    if (lua_istable(L, -1) == 0) /* Probably nil */
    {
      lua_pop(L, 1); /* Pop bad callback table */
      LCHECK_FN(L, stack, 0, fatal_error);

      fatal_error("function callback not found"); /* Does not return */
    }

    lua_rawgeti(L, -1, AS3LUA_CBFNINDEX); /* push stored callback function */

 #ifdef DO_SPAM
  {
    SPAM(("as3_lua_callback(): AS3 arguments"));
    AS3_Val a = AS3_CallS("join", args, AS3_Undefined());
    AS3_Trace(a);
    SAFE_RELEASE(a);
  }
#endif /* DO_SPAM */


    /* TODO: Assert we have Lua function (or other callable object) on the top of the stack */

    LCHECK_FN(L, stack, 2 + 1, fatal_error);

    nargs = push_as3_array_to_lua_stack(L, args); /* push arguments */

#ifdef DO_SPAM
    /* TODO: Remove */
    lua_pushcfunction(L, as3_trace);
    dump_lua_stack(L, LBASE(L, stack) + 2 + 1);
    lua_pushliteral(L, "ARGUMENTS");
    lua_pushnumber(L, nargs);
    lua_call(L, 3, 0);
#endif /* DO_SPAM */

    LCHECK_FN(L, stack, 2 + 1 + nargs, fatal_error);

    results_base = LBASE(L, stack) + 2;
    status = do_pcall_with_traceback(L, nargs, LUA_MULTRET);
    if (status != 0)
    {
      const char * msg = NULL;

      LCHECK_FN(L, stack, 2 + 1, fatal_error); /* Tables and error message */
      lua_remove(L, -2); /* Remove AS3LUA_CALLBACKS table */
      lua_remove(L, -2); /* Remove holder table */
      LCHECK_FN(L, stack, 1, fatal_error); /* Only error message */

      /* Error message is on stack */
      /* NOTE: It is not necessary string! If we want to preserve its type, see lua_DoString. */

      if (lua_tostring(L, -1) == NULL)
      {
        lua_pop(L, 1);
        lua_pushliteral(L, "(non-string)");
      }

      LCHECK_FN(L, stack, 1, fatal_error);

      lua_pushliteral(L, "Error in Lua callback:\n");
      lua_insert(L, -2);

      LCHECK_FN(L, stack, 2, fatal_error);

      lua_concat(L, 2);

      LCHECK_FN(L, stack, 1, fatal_error);

      sztrace((char *)lua_tostring(L, -1));

      /* TODO: ?! */
      /* lua_error(L); */

      msg = lua_tostring(L, -1);
      lua_pop(L, 1);

/*
      fatal_error(msg); / * Does not return * /
*/
    }

    /* Process results */

#ifdef DO_SPAM
    /* TODO: Remove */
    /*
    lua_pushcfunction(L, as3_trace);
    lua_pushliteral(L, "STACK");
    dump_lua_stack(L, results_base);
    lua_call(L, 2, 0);
    */
#endif /* DO_SPAM */

    res = create_as3_value_from_lua_stack(L, results_base + 1, LTOP(L, stack), TRUE);

#ifdef DO_SPAM
    SPAM(("as3_lua_callback() result type"));
    AS3_Trace(AS3_Call(getQualifiedClassName_method, NULL, AS3_Array("AS3ValType", res)));
#endif /* DO_SPAM */

    lua_settop(L, LBASE(L, stack)); /* Cleanup results and two holder tables */

    SPAM(("as3_lua_callback(): end"));

    return res;
  }

  /* Unreachable */
}
Exemple #17
0
int to_wig_by_blocks(char * argv[], int nproc, int iproc){
	
	clock_t start = clock();
	SPAM(("================\nto_wig_by_blocks.c start!\n"));
	
	char FileName[400];//name of processed Wig file
	strcpy(FileName, argv[1]);
	
	
	//for parallel
	int First; //first chrm to read
	//int Last;
	//int pflag; //indicate whether should start read
	if (nproc<=0){
		First = 0;
		//Last = 32;//assume no more than 32 chrm
		//pflag=1;
	}
	else{
		First = (24/nproc)*iproc; 
		/*if (iproc < nproc-1)
			Last = (24/nproc)*(iproc+1); 
		else
			Last = 32;*/
		//pflag=0; 
	}
	
	int Nd = 18;
	int SetMaxDiff = exp2(Nd); //set max alphabet size
	double Map[SetMaxDiff]; //maps each consecutive integer to the actual alphabet
	memset(Map,0,SetMaxDiff*sizeof(double)); 
	double LenMap[SetMaxDiff]; 
	memset(LenMap,0,SetMaxDiff*sizeof(double));  
	char ChrmNames[8*32]; //every chrm has a name less than 8 characters, assume no more than 32 chrm 
	memset(ChrmNames,0,8*32*sizeof(char));
	int ChrmCount=0;
	
	//int type;
	//char Type[40];
	
	double PreValue=-2;
	double Value=0;
	double Diff;//difference of values of contigs
	//int NextDiff=1;
	double PreLocation=-1;
	double Location=-1;
	double Contig = 1; //length of current contig
	double PreContig = 1; //length of previous contig
	double LenDiff; //length difference of two consecutive contigs
	char str[200];
	char tmpFileName[400];
	int i,j;
	int tmpint,tmp;
	int pcount=0;
	long long lcount=0; //count number of lines in wig file
	
	
	//for blocks use
	int SetBlockSize = exp2(BlockSize); //number of diff that are encoded together
	int IndexAux[32]; //(the last location of a chrm)>>SearchBlock, assume no more than 32 chrm
	memset(IndexAux,0,32*sizeof(unsigned int));
	int bcount=0; //counter inside each block
	//int BlockCount=0; //counter for number of blocks
	
	
	//read from count files
	strcpy(tmpFileName,FileName);
	strcat(tmpFileName,"Diff");
	FILE *fpDiff = fopen(tmpFileName,"r");
	if (fpDiff == NULL) {
		fprintf(stderr,"Can't open output Count file!\n");
		exit(1);
	}
	strcpy(tmpFileName,FileName);
	strcat(tmpFileName,"LenDiff");
	FILE *fpLenDiff = fopen(tmpFileName,"r");
	if (fpLenDiff == NULL) {
		fprintf(stderr,"Can't open output LenDiff file!\n");
		exit(1);
	}
	//skip annotation
	fgets(str,sizeof(str),fpDiff);
	fgets(str,sizeof(str),fpLenDiff);
	//change consecutive alphabet to the actual alphabet
	i=0;
	while (fread(&Map[i],sizeof(double),1,fpDiff)==1){
		//fscanf(fpDiff,"%d\t%*u\n", &Map[i]);
		fread(&tmpint,sizeof(int),1,fpDiff);
		i ++;
	}
	i=0;
	while (fread(&LenMap[i],sizeof(double),1,fpLenDiff)==1){
		//fscanf(fpLenDiff,"%d\t%*u\n", &LenMap[i]);
		fread(&tmpint,sizeof(int),1,fpLenDiff);
		i ++;
	}
			
		
	fclose(fpDiff);
	fclose(fpLenDiff);
	/*printf("map done!\n");
	for (j=0; j<80; j++)
		printf("j=%d, %d %d\n",j, Map[j],LenMap[j]);*/
	
	
	///////////////matlab seq to seq	
	//open matlab seq
	strcpy(tmpFileName,FileName);
	strcat(tmpFileName,"DiffSeqMatlabDecode");
	if (nproc>0){
		tmpint = sprintf(str,"%02d",iproc);
		strcat(tmpFileName,str);
	}
	FILE *fpDiffSeqMatlab = fopen(tmpFileName, "r");
	if (fpDiffSeqMatlab  == NULL) {
		fprintf(stderr, "Can't open file %s!\n",tmpFileName);
		exit(1);
	}
	//open matlab len seq
	strcpy(tmpFileName,FileName);
	strcat(tmpFileName,"LenDiffSeqMatlabDecode");
	if (nproc>0){
		tmpint = sprintf(str,"%02d",iproc);
		strcat(tmpFileName,str);
	}
	FILE *fpLenDiffSeqMatlab = fopen(tmpFileName, "r");
	if (fpLenDiffSeqMatlab  == NULL) {
		fprintf(stderr, "Can't open file %s!\n",tmpFileName);
		exit(1);
	}
	//output wig
	strcpy(tmpFileName,argv[2]);
	if (nproc>0){
		tmpint = sprintf(str,"%02d",iproc);
		strcat(tmpFileName,str);
	}
	FILE *fp;
	fp = fopen(tmpFileName, "w");
	if (fp == NULL) {
		fprintf(stderr, "Can't open output file %s!\n",tmpFileName);
		exit(1);
	}
	
	//chrm names
	strcpy(tmpFileName,FileName);
	strcat(tmpFileName,"ChrmName");
	FILE *fpName = fopen(tmpFileName, "r");
	if (fpName == NULL) {
		fprintf(stderr, "Can't open input file %s!\n",tmpFileName);
		exit(1);
	}
    while(!feof(fpName)){
        fscanf(fpName,"%8s",&ChrmNames[8*ChrmCount]);
        ChrmCount++;
    }
	
	
	int StartBlockIndex=0;
	//int EndBlockIndex=0;
	
	//fpBlockAux
	strcpy(tmpFileName,FileName);
	strcat(tmpFileName,"BlockAux");
	FILE *fpBlockAux = fopen(tmpFileName, "r");
	if (fpBlockAux == NULL) {
		fprintf(stderr, "Can't open file %s!\n",tmpFileName);
		exit(1);
	}
	for(i=0; i<First+1 && !feof(fpBlockAux); i++)
		fscanf(fpBlockAux,"%d ",&tmp);
	StartBlockIndex = tmp ;
	/*for (i; i<Last+1 && !feof(fpBlockAux); i++)
		fscanf(fpBlockAux,"%d ",&tmp);
	EndBlockIndex = tmp + 1;*/
	fclose(fpBlockAux);
	
	
	j=0;
	i = First; //Chrm count
	pcount = 1024; //count to print annotation
	bcount = 0;
	Diff=1;
	LenDiff=1;
	while(!feof(fpDiffSeqMatlab) && !feof(fpLenDiffSeqMatlab)){
			
		/*if (Location==3839085){ //(j<100){ 
			printf("PreLocation=%lu Location=%lu Contig=%ld LenDiff=%ld Value=%d Diff=%d\n", PreLocation,Location, Contig, LenDiff,Value,Diff);
		    printf("bcount=%d\n",bcount);
		}*/
		
		if (bcount==SetBlockSize)
			bcount = 0;
		bcount++;
		
		if (pcount==1024){
			fprintf(fp,"variableStep chrom=%s span=1\n", &ChrmNames[i*8]);
			pcount = 0;
			lcount++;
		}
		fscanf(fpDiffSeqMatlab,"%d ",&tmpint);
		Diff = Map[tmpint];
		fscanf(fpLenDiffSeqMatlab,"%d ",&tmpint);
		LenDiff = LenMap[tmpint];
	 
		
						
		if (Diff == -(1<<30)){//end of a chrm
			i++;
			pcount = 1024;
			PreValue = -2;
			PreContig = 1;
			PreLocation = -1;
			//printf("chrm %d finished\n",i);
			//remove padded 0s
			tmpint=(SetBlockSize-bcount)%SetBlockSize; //number of padded 0s
			for (j=0; j<tmpint; j++){
				fscanf(fpDiffSeqMatlab,"%d ",&tmp);
				fscanf(fpLenDiffSeqMatlab,"%d ",&tmp);
			}
			bcount=0;
			continue;
		}
				
		//write to wig	
		Value = PreValue + Diff;
		Contig = PreContig + LenDiff;
		
		
		if (Contig < 0){
			printf("Contig=%lg out of range! Check input file please!\n", Contig);
			printf("Location=%lg PreLocation=%lg Value=%lg PreValue=%lg ChrmName=%s\n",Location,PreLocation,Value,PreValue,&ChrmNames[i*8]);
			printf("Line # in wig is %lld\n",lcount);
			exit(1);
		}
			
		if ((Value>1e-8 || Value<-1e-8) && PreLocation!=-1){
			for (Location=PreLocation; Location < PreLocation + Contig; Location++ ){
				//fprintf(fp,"%ld\t%lg\n", Location,(double)(Value)/SetFloatSize);
				fprintf(fp,"%ld\t%lg\n", (long int)Location,Value);
				lcount ++;
				pcount ++;
			}
		}
		else
			Location = PreLocation + Contig;
		
		//reset
		PreValue = Value;
		PreContig = Contig;
		PreLocation = Location;
		//Diff = NextDiff;
		
	}
	clock_t end = clock();

    FILE *fpResult = fopen("result","a");
	if (fpResult == NULL) {
		fprintf(stderr,"Can't open output Count file!\n");
		exit(1);
	}
    SPAMR((fpResult,"//%s to_wig\n",FileName));
	SPAMR((fpResult,"Time for processing file in decode %2.3f sec.\n", (double)(end-start)/CLOCKS_PER_SEC));
	
	if (!feof(fpDiffSeqMatlab) || !feof(fpLenDiffSeqMatlab)){
		printf("two matlab seq files not same sizes!\n");
		exit(1);
	}
	
	fclose(fp);
	fclose(fpDiffSeqMatlab);
	fclose(fpLenDiffSeqMatlab);
	fclose(fpName);
	fclose(fpResult);

	return 1;
}
void introduce_user(const char *user)
{
#ifdef IRC_UNDERNET_P10
    char *modos=NULL;
#endif    
    
    /* Watch out for infinite loops... */
#define LTSIZE 20
    static int lasttimes[LTSIZE];
    if (lasttimes[0] >= time(NULL)-3)
	fatal("introduce_user() loop detected");
    memmove(lasttimes, lasttimes+1, sizeof(lasttimes)-sizeof(int));
    lasttimes[LTSIZE-1] = time(NULL);
#undef LTSIZE

    
#ifdef IRC_UNDERNET_P10
    if (!user || stricmp(user, s_NickServ) == 0 || stricmp(user, s_NickServP10) == 0) {
        s_NickServP10[0]=convert2y[ServerNumerico];
        s_NickServP10[1]='\0';
        strcat(s_NickServP10, "AA");
        modos="+okdrh";
        NICK(s_NickServ, modos, s_NickServP10, desc_NickServ);
        send_cmd(s_NickServ, "J #%s", CanalOpers);
        send_cmd(ServerName, "M #%s +o %s", CanalOpers, s_NickServP10);
	send_cmd(s_NickServ, "J #%s", CanalAdmins);
        send_cmd(ServerName, "M #%s +o %s", CanalAdmins, s_NickServP10);
    }    
    if (!user || stricmp(user, s_ChanServ) == 0 || stricmp(user, s_ChanServP10) == 0) {
        s_ChanServP10[0]=convert2y[ServerNumerico];
        s_ChanServP10[1]='\0';
        strcat(s_ChanServP10, "AB");
        modos="+Bkrhd";
        NICK(s_ChanServ, modos, s_ChanServP10, desc_ChanServ);
        send_cmd(s_ChanServ, "J #%s", CanalOpers);
        send_cmd(ServerName, "M #%s +o %s", CanalOpers, s_ChanServP10);
	 send_cmd(s_ChanServ, "J #%s", CanalAdmins);
        send_cmd(ServerName, "M #%s +o %s", CanalAdmins, s_ChanServP10);
    }    
    if (!user || stricmp(user, s_MemoServ) == 0 || stricmp(user, s_MemoServP10) == 0) {    
        s_MemoServP10[0]=convert2y[ServerNumerico];
        s_MemoServP10[1]='\0';
        strcat(s_MemoServP10, "AE");
        modos="+krhd";
        NICK(s_MemoServ, modos, s_MemoServP10, desc_MemoServ);
        send_cmd(s_MemoServ, "J #%s", CanalOpers);
        send_cmd(ServerName, "M #%s +o %s", CanalOpers, s_MemoServP10);
	send_cmd(s_MemoServ, "J #%s", CanalAdmins);
        send_cmd(ServerName, "M #%s +o %s", CanalAdmins, s_MemoServP10);
    }                                                                                                                                                                                                                                                                                                                
    if (!user || stricmp(user, s_OperServ) == 0 || stricmp(user, s_OperServP10) == 0) {    
        s_OperServP10[0]=convert2y[ServerNumerico];
        s_OperServP10[1]='\0';
        strcat(s_OperServP10, "AF");
        modos="+Bkorhdi";
        NICK(s_OperServ, modos, s_OperServP10, desc_OperServ);
        send_cmd(s_OperServ, "J #%s", CanalAdmins);
        send_cmd(ServerName, "M #%s +o %s", CanalAdmins, s_OperServP10);
        send_cmd(s_OperServ, "J #%s", CanalOpers);
        send_cmd(ServerName, "M #%s +o %s", CanalOpers, s_OperServP10);
    }
    if (!user || stricmp(user, s_NewsServ) == 0 || stricmp(user, s_NewsServP10) == 0) {
        s_NewsServP10[0]=convert2y[ServerNumerico];
        s_NewsServP10[1]='\0';
        strcat(s_NewsServP10, "AG");
        modos="+krd";
        NICK(s_NewsServ, modos, s_NewsServP10, desc_NewsServ);
        send_cmd(s_NewsServ, "J #%s", CanalOpers);
        send_cmd(ServerName, "M #%s +o %s", CanalOpers, s_NewsServP10);
	send_cmd(s_NewsServ, "J #%s", CanalAdmins);
        send_cmd(ServerName, "M #%s +o %s", CanalAdmins, s_NewsServP10);
    }   
      if (!user || stricmp(user, s_SpamServ) == 0 || stricmp(user, s_SpamServP10) == 0) {
        s_SpamServP10[0]=convert2y[ServerNumerico];
        s_SpamServP10[1]='\0';
        strcat(s_SpamServP10, "AH");
        modos="+kro";
        NICK(s_SpamServ, modos, s_SpamServP10, desc_SpamServ);
        send_cmd(s_SpamServ, "J #%s", CanalOpers);
        send_cmd(ServerName, "M #%s +o %s", CanalOpers, s_SpamServP10);
	send_cmd(s_SpamServ, "J #%s", CanalAdmins);
        send_cmd(ServerName, "M #%s +o %s", CanalAdmins, s_SpamServP10);

    }
    if (!user || stricmp(user, s_GlobalNoticer) == 0 || stricmp(user, s_GlobalNoticerP10) == 0) {
        s_GlobalNoticerP10[0]=convert2y[ServerNumerico];
        s_GlobalNoticerP10[1]='\0';
        strcat(s_GlobalNoticerP10, "AI");
        modos="+krhod";
        NICK(s_GlobalNoticer, modos, s_GlobalNoticerP10, desc_GlobalNoticer);
        send_cmd(s_GlobalNoticer, "J #%s", CanalOpers);
        send_cmd(ServerName, "M #%s +o %s", CanalOpers, s_GlobalNoticerP10);
	 send_cmd(s_GlobalNoticer, "J #%s", CanalAdmins);
        send_cmd(ServerName, "M #%s +o %s", CanalAdmins, s_GlobalNoticerP10);
    }    
    if (!user || stricmp(user, s_HelpServ) == 0 || stricmp(user, s_HelpServP10) == 0) {
        s_HelpServP10[0]=convert2y[ServerNumerico];
        s_HelpServP10[1]='\0';
        strcat(s_HelpServP10, "AJ");
        modos="+krd";
        NICK(s_HelpServ, modos, s_HelpServP10, desc_HelpServ);
        send_cmd(s_HelpServ, "J #%s", CanalOpers);
        send_cmd(ServerName, "M #%s +o %s", CanalOpers, s_HelpServP10);
	send_cmd(s_HelpServ, "J #%s", CanalAdmins);
        send_cmd(ServerName, "M #%s +o %s", CanalAdmins, s_HelpServP10);
    }
    if (s_IrcIIHelp && (!user || stricmp(user, s_IrcIIHelp) == 0 || stricmp(user, s_IrcIIHelpP10) == 0)) {
        s_IrcIIHelpP10[0]=convert2y[ServerNumerico];
        s_IrcIIHelpP10[1]='\0';
        strcat(s_IrcIIHelpP10, "AK");
        modos="+krd";
        NICK(s_IrcIIHelp, modos, s_IrcIIHelpP10, desc_IrcIIHelp);
        send_cmd(s_IrcIIHelp, "J #%s", CanalOpers);
        send_cmd(ServerName, "M #%s +o %s", CanalOpers, s_IrcIIHelpP10);
	send_cmd(s_IrcIIHelp, "J #%s", CanalAdmins);
        send_cmd(ServerName, "M #%s +o %s", CanalAdmins, s_IrcIIHelpP10);
    }    
	if (s_mIRCHelp && (!user || stricmp(user, s_mIRCHelp) == 0 || stricmp(user, s_mIRCHelpP10) == 0)) {
        s_IrcIIHelpP10[0]=convert2y[ServerNumerico];
        s_IrcIIHelpP10[1]='\0';
        strcat(s_IrcIIHelpP10, "AK");
        modos="+krd";
        NICK(s_mIRCHelp, modos, s_mIRCHelpP10, desc_mIRCHelp);
        send_cmd(s_IrcIIHelp, "J #%s", CanalOpers);
        send_cmd(ServerName, "M #%s +o %s", CanalOpers, s_mIRCHelpP10);
	send_cmd(s_mIRCHelp, "J #%s", CanalAdmins);
        send_cmd(ServerName, "M #%s +o %s", CanalAdmins, s_mIRCHelpP10);
    }    
   
   if (!user || stricmp(user, s_EuskalIRCServ) == 0 || stricmp(user, s_EuskalIRCServP10) == 0) {
        s_EuskalIRCServP10[0]=convert2y[ServerNumerico];
        s_EuskalIRCServP10[1]='\0';
        strcat(s_EuskalIRCServP10, "AH");
        modos="+kro";
        NICK(s_EuskalIRCServ, modos, s_EuskalIRCServP10, desc_EuskalIRCServ);
        send_cmd(s_EuskalIRCServ, "J #%s", CanalOpers);
        send_cmd(ServerName, "M #%s +o %s", CanalOpers, s_EuskalIRCServP10);
	send_cmd(s_EuskalIRCServ, "J #%s", CanalAdmins);
        send_cmd(ServerName, "M #%s +o %s", CanalAdmins, s_EuskalIRCServP10);

    }
if (!user || stricmp(user, s_CregServ) == 0 || stricmp(user, s_CregServP10) == 0) {
        s_CregServP10[0]=convert2y[ServerNumerico];
        s_CregServP10[1]='\0';
        strcat(s_CregServP10, "AJ");
        modos="+krd";
        NICK(s_CregServ, modos, s_CregServP10, desc_CregServ);
        send_cmd(s_CregServ, "J #%s", CanalOpers);
        send_cmd(ServerName, "M #%s +o %s", CanalOpers, s_CregServP10);
	send_cmd(s_CregServ, "J #%s", CanalAdmins);
        send_cmd(ServerName, "M #%s +o %s", CanalAdmins, s_CregServP10);
    }
#else
    if (!user || stricmp(user, s_ChanServ) == 0) {
	NICK(s_ChanServ, desc_ChanServ);
        send_cmd(s_ChanServ, "MODE %s +Bbikdor", s_ChanServ);
        send_cmd(s_ChanServ, "JOIN #%s", CanalOpers);
	send_cmd(s_ChanServ, "JOIN #%s", CanalAdmins);
	send_cmd(s_ChanServ, "MODE #%s +o %s", CanalAdmins, s_ChanServ);
        send_cmd(s_ChanServ, "MODE #%s +o %s", CanalOpers, s_ChanServ);
       	send_cmd(s_ChanServ, "MODE #%s +ntsil 1", CanalOpers);
    }
    if (!user || stricmp(user, s_NickServ) == 0) {
            NICK(s_NickServ, desc_NickServ);
        send_cmd(s_NickServ, "MODE %s +kdbBr", s_NickServ);
        send_cmd(s_NickServ, "JOIN #%s", CanalOpers);
	send_cmd(s_NickServ, "JOIN #%s", CanalAdmins);
	send_cmd(s_ChanServ, "MODE #%s +o %s", CanalAdmins, s_NickServ);
        send_cmd(s_ChanServ, "MODE #%s +o %s", CanalOpers, s_NickServ);
          }

    if (!user || stricmp(user, s_HelpServ) == 0) {
	NICK(s_HelpServ, desc_HelpServ);
        send_cmd(s_HelpServ, "MODE %s +dbBrk", s_HelpServ);
	send_cmd(s_HelpServ, "JOIN #%s", CanalAdmins);
	send_cmd(s_HelpServ, "JOIN #%s", CanalAyuda);
	send_cmd(s_ChanServ, "MODE #%s +o %s", CanalAyuda, s_HelpServ);
	send_cmd(s_ChanServ, "MODE #%s +o %s", CanalAdmins, s_HelpServ);
       

    }
    
     if (s_IrcIIHelp && (!user || stricmp(user, s_IrcIIHelp) == 0)) {
	NICK(s_IrcIIHelp, desc_IrcIIHelp);
         send_cmd(s_IrcIIHelp, "JOIN #%s", CanalAdmins);
	  send_cmd(s_IrcIIHelp, "JOIN #%s", CanalAyuda);
        send_cmd(s_IrcIIHelp, "MODE %s +dbBrk", s_IrcIIHelp);
	send_cmd(s_ChanServ, "MODE #%s +o %s", CanalAdmins, s_IrcIIHelp);
	send_cmd(s_ChanServ, "MODE #%s +o %s", CanalAyuda, s_IrcIIHelp);
              
    }
    if (s_mIRCHelp && (!user || stricmp(user, s_mIRCHelp) == 0)) {
	NICK(s_mIRCHelp, desc_mIRCHelp);
         send_cmd(s_mIRCHelp, "JOIN #%s", CanalAdmins);
	send_cmd(s_mIRCHelp, "JOIN #%s", CanalAyuda);
        send_cmd(s_mIRCHelp, "MODE %s +dbBrk", s_mIRCHelp);
	send_cmd(s_ChanServ, "MODE #%s +o %s", CanalAdmins, s_mIRCHelp);
	send_cmd(s_ChanServ, "MODE #%s +o %s", CanalAyuda, s_mIRCHelp);
              
    }

    if (!user || stricmp(user, s_MemoServ) == 0) {
	NICK(s_MemoServ, desc_MemoServ);
	send_cmd(s_MemoServ, "MODE %s +krbdB", s_MemoServ);
	send_cmd(s_MemoServ, "JOIN #%s", CanalAdmins);
	send_cmd(s_ChanServ, "MODE #%s +o %s", CanalAdmins, s_MemoServ);
       
    }
    if (!user || stricmp(user, s_OperServ) == 0) {
	NICK(s_OperServ, desc_OperServ);
        send_cmd(s_OperServ, "MODE %s +Bbikdor", s_OperServ);
        send_cmd(s_OperServ, "JOIN #%s", CanalAdmins);
        send_cmd(s_ChanServ, "MODE #%s +o %s", CanalAdmins, s_OperServ);
        send_cmd(s_OperServ, "JOIN #%s", CanalOpers);
        send_cmd(s_ChanServ, "MODE #%s +o %s", CanalOpers, s_OperServ);
	send_cmd(s_OperServ, "MODE #%s +ntsil 1", CanalAdmins);
    }
    if (!user || stricmp(user, s_CregServ) == 0) {
	NICK(s_CregServ, desc_CregServ);
        send_cmd(s_CregServ, "MODE %s +Bbikdor", s_CregServ);
        send_cmd(s_CregServ, "JOIN #%s", CanalAdmins);
        send_cmd(s_ChanServ, "MODE #%s +o %s", CanalAdmins, s_CregServ);
        
    }
     if (!user || stricmp(user, s_EuskalIRCServ) == 0) {
	euskalirc(s_EuskalIRCServ, desc_EuskalIRCServ);
        send_cmd(s_EuskalIRCServ, "MODE %s +Bbikor", s_EuskalIRCServ);
        send_cmd(s_EuskalIRCServ, "JOIN #%s", CanalAdmins);
	send_cmd(s_EuskalIRCServ, "JOIN #%s", CanalAyuda);
	send_cmd(s_ChanServ, "MODE #%s +o %s", CanalAyuda, s_EuskalIRCServ);
        send_cmd(s_ChanServ, "MODE #%s +o %s", CanalAdmins, s_EuskalIRCServ);
        
    }
    if (!user || stricmp(user, s_BddServ) == 0) {
        CNICK(s_BddServ, desc_BddServ, "-", "-base.de.datos-");
	send_cmd(s_BddServ, "MODE %s +iXkoBrd", s_BddServ);
	send_cmd(s_BddServ, "JOIN #%s", CanalAdmins);
	send_cmd(s_ChanServ, "MODE #%s +o %s", CanalAdmins, s_BddServ);
	
    }
    
    if (!user || stricmp(user, s_GlobalNoticer) == 0) {
	NICK(s_GlobalNoticer, desc_GlobalNoticer);
	send_cmd(s_GlobalNoticer, "MODE %s +ikorBd", s_GlobalNoticer);
	send_cmd(s_GlobalNoticer, "JOIN #%s", CanalAdmins);
	send_cmd(s_ChanServ, "MODE #%s +o %s", CanalAdmins, s_GlobalNoticer);
    }
    if (!user || stricmp(user, s_ShadowServ) == 0) {
        CNICK(s_ShadowServ, desc_ShadowServ, "-", "-");
	send_cmd(s_ShadowServ, "MODE %s +rokbXBd", s_ShadowServ);
	send_cmd(s_ShadowServ, "JOIN #%s", CanalAdmins);
	send_cmd(s_ChanServ, "MODE #%s +o %s", CanalAdmins, s_ShadowServ);
                              
    }
/* Esto, algún día funcionará... o eso espero ;) */
    if (!user || stricmp(user, s_IpVirtual) == 0) {
    	CNICK(s_IpVirtual, desc_IpVirtual, "ipvirtual", ServerName);
 	send_cmd(s_IpVirtual, "MODE %s +rokhBX", s_IpVirtual);
	send_cmd(s_IpVirtual, "JOIN #%s", CanalAdmins);
	send_cmd(s_IpVirtual, "MODE #%s +o %s", CanalAdmins, s_IpVirtual);
    }
 
    if (!user || stricmp(user, s_NewsServ) == 0) {
        NICK(s_NewsServ, desc_NewsServ);
        send_cmd(s_NewsServ, "MODE %s +kBbord", s_NewsServ);
	send_cmd(s_NewsServ, "JOIN #%s", CanalAdmins);
	send_cmd(s_ChanServ, "MODE #%s +o %s", CanalAdmins, s_NewsServ);
    	                        
    }
    if (!user || stricmp(user,  s_SpamServ) == 0) {
      SPAM(s_SpamServ, desc_SpamServ);
        send_cmd( s_SpamServ, "MODE %s +kBbor",  s_SpamServ);
	send_cmd( s_SpamServ, "JOIN #%s", CanalAdmins);
	send_cmd(s_ChanServ, "MODE #%s +o %s", CanalAdmins, s_SpamServ);
    	send_cmd(NULL, "STATS b");
                        
    }
   
#endif
}