Ejemplo n.º 1
0
/* Function:  expand_ModuleFunctionList(obj,len)
 *
 * Descrip:    Really an internal function for add_ModuleFunctionList
 *
 *
 * Arg:        obj [UNKN ] Object which contains the list [ModuleFunctionList *]
 * Arg:        len [UNKN ] Length to add one [int]
 *
 * Return [UNKN ]  Undocumented return value [boolean]
 *
 */
boolean expand_ModuleFunctionList(ModuleFunctionList * obj,int len) 
{


    if( obj->maxlen > obj->len )     {  
      warn("expand_ModuleFunctionList called with no need"); 
      return TRUE;   
      }  


    if( (obj->mf = (ModuleFunction ** ) ckrealloc (obj->mf,sizeof(ModuleFunction *)*len)) == NULL)   {  
      warn("ckrealloc failed for expand_ModuleFunctionList, returning FALSE");   
      return FALSE;  
      }  
    obj->maxlen = len;   
    return TRUE; 
}    
Ejemplo n.º 2
0
/* Function:  expand_me_MethodTypeSet(obj,len)
 *
 * Descrip:    Really an internal function for add_me_MethodTypeSet
 *
 *
 * Arg:        obj [UNKN ] Object which contains the list [MethodTypeSet *]
 * Arg:        len [UNKN ] Length to add one [int]
 *
 * Return [UNKN ]  Undocumented return value [boolean]
 *
 */
boolean expand_me_MethodTypeSet(MethodTypeSet * obj,int len) 
{


    if( obj->me_maxlen > obj->me_len )   {  
      warn("expand_MethodTypeSetme_ called with no need");   
      return TRUE;   
      }  


    if( (obj->me = (Method ** ) ckrealloc (obj->me,sizeof(Method *)*len)) == NULL)   {  
      warn("ckrealloc failed for expand_MethodTypeSet, returning FALSE");    
      return FALSE;  
      }  
    obj->me_maxlen = len;    
    return TRUE; 
}    
Ejemplo n.º 3
0
/* Function:  expand_Method(obj,len)
 *
 * Descrip:    Really an internal function for add_Method
 *
 *
 * Arg:        obj [UNKN ] Object which contains the list [Method *]
 * Arg:        len [UNKN ] Length to add one [int]
 *
 * Return [UNKN ]  Undocumented return value [boolean]
 *
 */
boolean expand_Method(Method * obj,int len) 
{


    if( obj->maxlen > obj->len )     {  
      warn("expand_Method called with no need"); 
      return TRUE;   
      }  


    if( (obj->ma = (MethodArg ** ) ckrealloc (obj->ma,sizeof(MethodArg *)*len)) == NULL)     {  
      warn("ckrealloc failed for expand_Method, returning FALSE");   
      return FALSE;  
      }  
    obj->maxlen = len;   
    return TRUE; 
}    
Ejemplo n.º 4
0
/* Function:  expand_in_MethodTypeSet(obj,len)
 *
 * Descrip:    Really an internal function for add_in_MethodTypeSet
 *
 *
 * Arg:        obj [UNKN ] Object which contains the list [MethodTypeSet *]
 * Arg:        len [UNKN ] Length to add one [int]
 *
 * Return [UNKN ]  Undocumented return value [boolean]
 *
 */
boolean expand_in_MethodTypeSet(MethodTypeSet * obj,int len) 
{


    if( obj->in_maxlen > obj->in_len )   {  
      warn("expand_MethodTypeSetin_ called with no need");   
      return TRUE;   
      }  


    if( (obj->in = (Input  ** ) ckrealloc (obj->in,sizeof(Input  *)*len)) == NULL)   {  
      warn("ckrealloc failed for expand_MethodTypeSet, returning FALSE");    
      return FALSE;  
      }  
    obj->in_maxlen = len;    
    return TRUE; 
}    
Ejemplo n.º 5
0
/* Function:  expand_ty_MethodTypeSet(obj,len)
 *
 * Descrip:    Really an internal function for add_ty_MethodTypeSet
 *
 *
 * Arg:        obj [UNKN ] Object which contains the list [MethodTypeSet *]
 * Arg:        len [UNKN ] Length to add one [int]
 *
 * Return [UNKN ]  Undocumented return value [boolean]
 *
 */
boolean expand_ty_MethodTypeSet(MethodTypeSet * obj,int len) 
{


    if( obj->ty_maxlen > obj->ty_len )   {  
      warn("expand_MethodTypeSetty_ called with no need");   
      return TRUE;   
      }  


    if( (obj->ty = (Type   ** ) ckrealloc (obj->ty,sizeof(Type   *)*len)) == NULL)   {  
      warn("ckrealloc failed for expand_MethodTypeSet, returning FALSE");    
      return FALSE;  
      }  
    obj->ty_maxlen = len;    
    return TRUE; 
}    
Ejemplo n.º 6
0
/* Function:  expand_PackAln(obj,len)
 *
 * Descrip:    Really an internal function for add_PackAln
 *
 *
 * Arg:        obj [UNKN ] Object which contains the list [PackAln *]
 * Arg:        len [UNKN ] Length to add one [int]
 *
 * Return [UNKN ]  Undocumented return value [boolean]
 *
 */
boolean expand_PackAln(PackAln * obj,int len) 
{


    if( obj->maxlen > obj->len )     {  
      warn("expand_PackAln called with no need");    
      return TRUE;   
      }  


    if( (obj->pau = (PackAlnUnit ** ) ckrealloc (obj->pau,sizeof(PackAlnUnit *)*len)) == NULL)   {  
      warn("ckrealloc failed for expand_PackAln, returning FALSE");  
      return FALSE;  
      }  
    obj->maxlen = len;   
    return TRUE; 
}    
Ejemplo n.º 7
0
void
growstackblock(void)
{
	char *p;
	int newlen;
	char *oldspace;
	int oldlen;
	struct stack_block *sp;
	struct stack_block *oldstackp;
	struct stackmark *xmark;

	newlen = (stacknleft == 0) ? MINSIZE : stacknleft * 2 + 100;
	newlen = ALIGN(newlen);
	oldspace = stacknxt;
	oldlen = stacknleft;

	if (stackp != NULL && stacknxt == SPACE(stackp)) {
		INTOFF;
		oldstackp = stackp;
		stackp = oldstackp->prev;
		sp = ckrealloc((pointer)oldstackp, newlen);
		sp->prev = stackp;
		stackp = sp;
		stacknxt = SPACE(sp);
		stacknleft = newlen - (stacknxt - (char*)sp);

		/*
		 * Stack marks pointing to the start of the old block
		 * must be relocated to point to the new block
		 */
		xmark = markp;
		while (xmark != NULL && xmark->stackp == oldstackp) {
			xmark->stackp = stackp;
			xmark->stacknxt = stacknxt;
			xmark->stacknleft = stacknleft;
			xmark = xmark->marknext;
		}
		INTON;
	} else {
		p = stalloc(newlen);
		if (oldlen != 0)
			memcpy(p, oldspace, oldlen);
		stunalloc(p);
	}
}
Ejemplo n.º 8
0
void
Tcl_Preserve(
    ClientData clientData)	/* Pointer to malloc'ed block of memory. */
{
    Reference *refPtr;
    int i;

    /*
     * See if there is already a reference for this pointer. If so, just
     * increment its reference count.
     */

    Tcl_MutexLock(&preserveMutex);
    for (i=0, refPtr=refArray ; i<inUse ; i++, refPtr++) {
	if (refPtr->clientData == clientData) {
	    refPtr->refCount++;
	    Tcl_MutexUnlock(&preserveMutex);
	    return;
	}
    }

    /*
     * Make a reference array if it doesn't already exist, or make it bigger
     * if it is full.
     */

    if (inUse == spaceAvl) {
	spaceAvl = spaceAvl ? 2*spaceAvl : INITIAL_SIZE;
	refArray = (Reference *) ckrealloc((char *) refArray,
		spaceAvl * sizeof(Reference));
    }

    /*
     * Make a new entry for the new reference.
     */

    refPtr = &refArray[inUse];
    refPtr->clientData = clientData;
    refPtr->refCount = 1;
    refPtr->mustFree = 0;
    refPtr->freeProc = TCL_STATIC;
    inUse += 1;
    Tcl_MutexUnlock(&preserveMutex);
}
Ejemplo n.º 9
0
void
growstackblock(void)
{
	int newlen = SHELL_ALIGN(stacknleft * 2 + 100);

	if (stacknxt == stackp->space && stackp != &stackbase) {
		struct stack_block *oldstackp;
		struct stackmark *xmark;
		struct stack_block *sp;

		INTOFF;
		oldstackp = stackp;
		sp = stackp;
		stackp = sp->prev;
		sp = ckrealloc((pointer)sp,
		    sizeof(struct stack_block) - MINSIZE + newlen);
		sp->prev = stackp;
		stackp = sp;
		stacknxt = sp->space;
		stacknleft = newlen;

		/*
		 * Stack marks pointing to the start of the old block
		 * must be relocated to point to the new block 
		 */
		xmark = markp;
		while (xmark != NULL && xmark->stackp == oldstackp) {
			xmark->stackp = stackp;
			xmark->stacknxt = stacknxt;
			xmark->stacknleft = stacknleft;
			xmark = xmark->marknext;
		}
		INTON;
	} else {
		char *oldspace = stacknxt;
		int oldlen = stacknleft;
		char *p = stalloc(newlen);

		(void)memcpy(p, oldspace, oldlen);
		stacknxt = p;			/* free the space */
		stacknleft += newlen;		/* we just allocated */
	}
}
Ejemplo n.º 10
0
static void
growstackblock(int min)
{
	char *p;
	int newlen;
	char *oldspace;
	int oldlen;
	struct stack_block *sp;
	struct stack_block *oldstackp;

	if (min < stacknleft)
		min = stacknleft;
	if ((unsigned int)min >=
	    INT_MAX / 2 - ALIGN(sizeof(struct stack_block)))
		error("Out of space");
	min += stacknleft;
	min += ALIGN(sizeof(struct stack_block));
	newlen = 512;
	while (newlen < min)
		newlen <<= 1;
	oldspace = stacknxt;
	oldlen = stacknleft;

	if (stackp != NULL && stacknxt == SPACE(stackp)) {
		INTOFF;
		oldstackp = stackp;
		stackp = oldstackp->prev;
		sp = ckrealloc((pointer)oldstackp, newlen);
		sp->prev = stackp;
		stackp = sp;
		stacknxt = SPACE(sp);
		stacknleft = newlen - (stacknxt - (char*)sp);
		sstrend = stacknxt + stacknleft;
		INTON;
	} else {
		newlen -= ALIGN(sizeof(struct stack_block));
		p = stalloc(newlen);
		if (oldlen != 0)
			memcpy(p, oldspace, oldlen);
		stunalloc(p);
	}
}
Ejemplo n.º 11
0
int init_sfilter_input(struct sfilterp_st *sfilterp,
		       struct packet_info_st *packetinfo){

  int n;
  char *p;
  int size = sfilterp->input_size;

  n = snprintf(sfilterp->input, size, "%u %d %d %d %d %s %s",
	       (unsigned int)packetinfo->seq_number, 
	       packetinfo->psh_product_type,
	       packetinfo->psh_product_category,
	       packetinfo->psh_product_code,
	       packetinfo->np_channel_index,
	       packetinfo->fname,
	       packetinfo->fpath);
 
  /* n does not include the trailing '\0' */
  if(n >= size){
    while(n >= size){
      size *= 2;
    }

    p = ckrealloc(sfilterp->input, size);
    if(p == NULL)
      return(-1);

    sfilterp->input = p;
    sfilterp->input_size = size;

    n = snprintf(sfilterp->input, size, "%u %d %d %d %d %s %s",
		 (unsigned int)packetinfo->seq_number, 
		 packetinfo->psh_product_type,
		 packetinfo->psh_product_category,
		 packetinfo->psh_product_code,
		 packetinfo->np_channel_index,
		 packetinfo->fname,
		 packetinfo->fpath);
  }

  return(0);
}
Ejemplo n.º 12
0
/* ++ InsertSlave --
 * 	Adds slave to the list of managed windows.
 */
static void InsertSlave(Ttk_Manager *mgr, Ttk_Slave *slave, int index)
{
    int endIndex = mgr->nSlaves++;
    mgr->slaves = (Ttk_Slave**)ckrealloc(
	    (ClientData)mgr->slaves, mgr->nSlaves * sizeof(Ttk_Slave *));

    while (endIndex > index) {
	mgr->slaves[endIndex] = mgr->slaves[endIndex - 1];
	--endIndex;
    }

    mgr->slaves[index] = slave;

    Tk_ManageGeometry(slave->slaveWindow,
	&mgr->managerSpec->tkGeomMgr, (ClientData)mgr);

    Tk_CreateEventHandler(slave->slaveWindow,
	SlaveEventMask, SlaveEventHandler, (ClientData)slave);

    ScheduleUpdate(mgr, MGR_RESIZE_REQUIRED);
}
Ejemplo n.º 13
0
Archivo: tkOption.c Proyecto: das/tcltk
static ElArray *
ExtendArray(
    register ElArray *arrayPtr,	/* Array to be extended. */
    register Element *elPtr)	/* Element to be copied into array. */
{
    /*
     * If the current array has filled up, make it bigger.
     */

    if (arrayPtr->numUsed >= arrayPtr->arraySize) {
	register int newSize = 2*arrayPtr->arraySize;

	arrayPtr = (ElArray *)
		ckrealloc((char *) arrayPtr, EL_ARRAY_SIZE(newSize));
	arrayPtr->arraySize = newSize;
	arrayPtr->nextToUse = &arrayPtr->els[arrayPtr->numUsed];
    }

    *arrayPtr->nextToUse = *elPtr;
    arrayPtr->nextToUse++;
    arrayPtr->numUsed++;
    return arrayPtr;
}
Ejemplo n.º 14
0
void
emptyoutbuf(struct output *dest)
{
	int offset;

	if (dest->buf == NULL) {
		INTOFF;
		dest->buf = ckmalloc(dest->bufsize);
		dest->nextc = dest->buf;
		dest->nleft = dest->bufsize;
		INTON;
	} else if (dest->fd == MEM_OUT) {
		offset = dest->bufsize;
		INTOFF;
		dest->bufsize <<= 1;
		dest->buf = ckrealloc(dest->buf, dest->bufsize);
		dest->nleft = dest->bufsize - offset;
		dest->nextc = dest->buf + offset;
		INTON;
	} else {
		flushout(dest);
	}
	dest->nleft--;
}
Ejemplo n.º 15
0
int
Tcl_ParseArgsObjv(
    Tcl_Interp *interp,		/* Place to store error message. */
    const Tcl_ArgvInfo *argTable,
				/* Array of option descriptions. */
    int *objcPtr,		/* Number of arguments in objv. Modified to
				 * hold # args left in objv at end. */
    Tcl_Obj *const *objv,	/* Array of arguments to be parsed. */
    Tcl_Obj ***remObjv)		/* Pointer to array of arguments that were not
				 * processed here. Should be NULL if no return
				 * of arguments is desired. */
{
    Tcl_Obj **leftovers;	/* Array to write back to remObjv on
				 * successful exit. Will include the name of
				 * the command. */
    int nrem;			/* Size of leftovers.*/
    register const Tcl_ArgvInfo *infoPtr;
				/* Pointer to the current entry in the table
				 * of argument descriptions. */
    const Tcl_ArgvInfo *matchPtr;
				/* Descriptor that matches current argument */
    Tcl_Obj *curArg;		/* Current argument */
    const char *str = NULL;
    register char c;		/* Second character of current arg (used for
				 * quick check for matching; use 2nd char.
				 * because first char. will almost always be
				 * '-'). */
    int srcIndex;		/* Location from which to read next argument
				 * from objv. */
    int dstIndex;		/* Used to keep track of current arguments
				 * being processed, primarily for error
				 * reporting. */
    int objc;			/* # arguments in objv still to process. */
    int length;			/* Number of characters in current argument */

    if (remObjv != NULL) {
	/*
	 * Then we should copy the name of the command (0th argument). The
	 * upper bound on the number of elements is known, and (undocumented,
	 * but historically true) there should be a NULL argument after the
	 * last result. [Bug 3413857]
	 */

	nrem = 1;
	leftovers = ckalloc((1 + *objcPtr) * sizeof(Tcl_Obj *));
	leftovers[0] = objv[0];
    } else {
	nrem = 0;
	leftovers = NULL;
    }

    /*
     * OK, now start processing from the second element (1st argument).
     */

    srcIndex = dstIndex = 1;
    objc = *objcPtr-1;

    while (objc > 0) {
	curArg = objv[srcIndex];
	srcIndex++;
	objc--;
	str = Tcl_GetStringFromObj(curArg, &length);
	if (length > 0) {
	    c = str[1];
	} else {
	    c = 0;
	}

	/*
	 * Loop throught the argument descriptors searching for one with the
	 * matching key string. If found, leave a pointer to it in matchPtr.
	 */

	matchPtr = NULL;
	infoPtr = argTable;
	for (; infoPtr != NULL && infoPtr->type != TCL_ARGV_END ; infoPtr++) {
	    if (infoPtr->keyStr == NULL) {
		continue;
	    }
	    if ((infoPtr->keyStr[1] != c)
		    || (strncmp(infoPtr->keyStr, str, length) != 0)) {
		continue;
	    }
	    if (infoPtr->keyStr[length] == 0) {
		matchPtr = infoPtr;
		goto gotMatch;
	    }
	    if (matchPtr != NULL) {
		Tcl_SetObjResult(interp, Tcl_ObjPrintf(
			"ambiguous option \"%s\"", str));
		goto error;
	    }
	    matchPtr = infoPtr;
	}
	if (matchPtr == NULL) {
	    /*
	     * Unrecognized argument. Just copy it down, unless the caller
	     * prefers an error to be registered.
	     */

	    if (remObjv == NULL) {
		Tcl_SetObjResult(interp, Tcl_ObjPrintf(
			"unrecognized argument \"%s\"", str));
		goto error;
	    }

	    dstIndex++;		/* This argument is now handled */
	    leftovers[nrem++] = curArg;
	    continue;
	}

	/*
	 * Take the appropriate action based on the option type
	 */

    gotMatch:
	infoPtr = matchPtr;
	switch (infoPtr->type) {
	case TCL_ARGV_CONSTANT:
	    *((int *) infoPtr->dstPtr) = PTR2INT(infoPtr->srcPtr);
	    break;
	case TCL_ARGV_INT:
	    if (objc == 0) {
		goto missingArg;
	    }
	    if (Tcl_GetIntFromObj(interp, objv[srcIndex],
		    (int *) infoPtr->dstPtr) == TCL_ERROR) {
		Tcl_SetObjResult(interp, Tcl_ObjPrintf(
			"expected integer argument for \"%s\" but got \"%s\"",
			infoPtr->keyStr, Tcl_GetString(objv[srcIndex])));
		goto error;
	    }
	    srcIndex++;
	    objc--;
	    break;
	case TCL_ARGV_STRING:
	    if (objc == 0) {
		goto missingArg;
	    }
	    *((const char **) infoPtr->dstPtr) =
		    Tcl_GetString(objv[srcIndex]);
	    srcIndex++;
	    objc--;
	    break;
	case TCL_ARGV_REST:
	    /*
	     * Only store the point where we got to if it's not to be written
	     * to NULL, so that TCL_ARGV_AUTO_REST works.
	     */

	    if (infoPtr->dstPtr != NULL) {
		*((int *) infoPtr->dstPtr) = dstIndex;
	    }
	    goto argsDone;
	case TCL_ARGV_FLOAT:
	    if (objc == 0) {
		goto missingArg;
	    }
	    if (Tcl_GetDoubleFromObj(interp, objv[srcIndex],
		    (double *) infoPtr->dstPtr) == TCL_ERROR) {
		Tcl_SetObjResult(interp, Tcl_ObjPrintf(
			"expected floating-point argument for \"%s\" but got \"%s\"",
			infoPtr->keyStr, Tcl_GetString(objv[srcIndex])));
		goto error;
	    }
	    srcIndex++;
	    objc--;
	    break;
	case TCL_ARGV_FUNC: {
	    Tcl_ArgvFuncProc *handlerProc = (Tcl_ArgvFuncProc *)
		    infoPtr->srcPtr;
	    Tcl_Obj *argObj;

	    if (objc == 0) {
		argObj = NULL;
	    } else {
		argObj = objv[srcIndex];
	    }
	    if (handlerProc(infoPtr->clientData, argObj, infoPtr->dstPtr)) {
		srcIndex++;
		objc--;
	    }
	    break;
	}
	case TCL_ARGV_GENFUNC: {
	    Tcl_ArgvGenFuncProc *handlerProc = (Tcl_ArgvGenFuncProc *)
		    infoPtr->srcPtr;

	    objc = handlerProc(infoPtr->clientData, interp, objc,
		    &objv[srcIndex], infoPtr->dstPtr);
	    if (objc < 0) {
		goto error;
	    }
	    break;
	}
	case TCL_ARGV_HELP:
	    PrintUsage(interp, argTable);
	    goto error;
	default:
	    Tcl_SetObjResult(interp, Tcl_ObjPrintf(
		    "bad argument type %d in Tcl_ArgvInfo", infoPtr->type));
	    goto error;
	}
    }

    /*
     * If we broke out of the loop because of an OPT_REST argument, copy the
     * remaining arguments down. Note that there is always at least one
     * argument left over - the command name - so we always have a result if
     * our caller is willing to receive it. [Bug 3413857]
     */

  argsDone:
    if (remObjv == NULL) {
	/*
	 * Nothing to do.
	 */

	return TCL_OK;
    }

    if (objc > 0) {
	memcpy(leftovers+nrem, objv+srcIndex, objc*sizeof(Tcl_Obj *));
	nrem += objc;
    }
    leftovers[nrem] = NULL;
    *objcPtr = nrem++;
    *remObjv = ckrealloc(leftovers, nrem * sizeof(Tcl_Obj *));
    return TCL_OK;

    /*
     * Make sure to handle freeing any temporary space we've allocated on the
     * way to an error.
     */

  missingArg:
    Tcl_SetObjResult(interp, Tcl_ObjPrintf(
	    "\"%s\" option requires an additional argument", str));
  error:
    if (leftovers != NULL) {
	ckfree(leftovers);
    }
    return TCL_ERROR;
}
Ejemplo n.º 16
0
void
TclUnsetEnv(
    const char *name)		/* Name of variable to remove (UTF-8). */
{
    char *oldValue;
    int length;
    int index;
#ifdef USE_PUTENV_FOR_UNSET
    Tcl_DString envString;
    char *string;
#else
    char **envPtr;
#endif /* USE_PUTENV_FOR_UNSET */

    Tcl_MutexLock(&envMutex);
    index = TclpFindVariable(name, &length);

    /*
     * First make sure that the environment variable exists to avoid doing
     * needless work and to avoid recursion on the unset.
     */

    if (index == -1) {
	Tcl_MutexUnlock(&envMutex);
	return;
    }

    /*
     * Remember the old value so we can free it if Tcl created the string.
     */

    oldValue = environ[index];

    /*
     * Update the system environment. This must be done before we update the
     * interpreters or we will recurse.
     */

#ifdef USE_PUTENV_FOR_UNSET
    /*
     * For those platforms that support putenv to unset, Linux indicates
     * that no = should be included, and Windows requires it.
     */

#if defined(__WIN32__) || defined(__CYGWIN__)
    string = ckalloc((unsigned) length+2);
    memcpy(string, name, (size_t) length);
    string[length] = '=';
    string[length+1] = '\0';
#else
    string = ckalloc((unsigned) length+1);
    memcpy(string, name, (size_t) length);
    string[length] = '\0';
#endif /* WIN32 */

    Tcl_UtfToExternalDString(NULL, string, -1, &envString);
    string = ckrealloc(string, (unsigned) Tcl_DStringLength(&envString)+1);
    strcpy(string, Tcl_DStringValue(&envString));
    Tcl_DStringFree(&envString);

    putenv(string);

    /*
     * Watch out for versions of putenv that copy the string (e.g. VC++). In
     * this case we need to free the string immediately. Otherwise update the
     * string in the cache.
     */

    if (environ[index] == string) {
	ReplaceString(oldValue, string);
#ifdef HAVE_PUTENV_THAT_COPIES
    } else {
	/*
	 * This putenv() copies instead of taking ownership.
	 */

	ckfree(string);
#endif /* HAVE_PUTENV_THAT_COPIES */
    }
#else /* !USE_PUTENV_FOR_UNSET */
    for (envPtr = environ+index+1; ; envPtr++) {
	envPtr[-1] = *envPtr;
	if (*envPtr == NULL) {
	    break;
	}
    }
    ReplaceString(oldValue, NULL);
#endif /* USE_PUTENV_FOR_UNSET */

    Tcl_MutexUnlock(&envMutex);
}
Ejemplo n.º 17
0
void
TclSetEnv(
    const char *name,		/* Name of variable whose value is to be set
				 * (UTF-8). */
    const char *value)		/* New value for variable (UTF-8). */
{
    Tcl_DString envString;
    int index, length, nameLength;
    char *p, *oldValue;
    const char *p2;

    /*
     * Figure out where the entry is going to go. If the name doesn't already
     * exist, enlarge the array if necessary to make room. If the name exists,
     * free its old entry.
     */

    Tcl_MutexLock(&envMutex);
    index = TclpFindVariable(name, &length);

    if (index == -1) {
#ifndef USE_PUTENV
	/*
	 * We need to handle the case where the environment may be changed
	 * outside our control. ourEnvironSize is only valid if the current
	 * environment is the one we allocated. [Bug 979640]
	 */

	if ((env.ourEnviron != environ) || (length+2 > env.ourEnvironSize)) {
	    char **newEnviron = (char **)
		    ckalloc(((unsigned) length + 5) * sizeof(char *));

	    memcpy(newEnviron, environ, length * sizeof(char *));
	    if ((env.ourEnvironSize != 0) && (env.ourEnviron != NULL)) {
		ckfree((char *) env.ourEnviron);
	    }
	    environ = env.ourEnviron = newEnviron;
	    env.ourEnvironSize = length + 5;
	}
	index = length;
	environ[index + 1] = NULL;
#endif /* USE_PUTENV */
	oldValue = NULL;
	nameLength = strlen(name);
    } else {
	const char *env;

	/*
	 * Compare the new value to the existing value. If they're the same
	 * then quit immediately (e.g. don't rewrite the value or propagate it
	 * to other interpreters). Otherwise, when there are N interpreters
	 * there will be N! propagations of the same value among the
	 * interpreters.
	 */

	env = Tcl_ExternalToUtfDString(NULL, environ[index], -1, &envString);
	if (strcmp(value, env + (length + 1)) == 0) {
	    Tcl_DStringFree(&envString);
	    Tcl_MutexUnlock(&envMutex);
	    return;
	}
	Tcl_DStringFree(&envString);

	oldValue = environ[index];
	nameLength = length;
    }

    /*
     * Create a new entry. Build a complete UTF string that contains a
     * "name=value" pattern. Then convert the string to the native encoding,
     * and set the environ array value.
     */

    p = ckalloc((unsigned) nameLength + strlen(value) + 2);
    strcpy(p, name);
    p[nameLength] = '=';
    strcpy(p+nameLength+1, value);
    p2 = Tcl_UtfToExternalDString(NULL, p, -1, &envString);

    /*
     * Copy the native string to heap memory.
     */

    p = ckrealloc(p, strlen(p2) + 1);
    strcpy(p, p2);
    Tcl_DStringFree(&envString);

#ifdef USE_PUTENV
    /*
     * Update the system environment.
     */

    putenv(p);
    index = TclpFindVariable(name, &length);
#else
    environ[index] = p;
#endif /* USE_PUTENV */

    /*
     * Watch out for versions of putenv that copy the string (e.g. VC++). In
     * this case we need to free the string immediately. Otherwise update the
     * string in the cache.
     */

    if ((index != -1) && (environ[index] == p)) {
	ReplaceString(oldValue, p);
#ifdef HAVE_PUTENV_THAT_COPIES
    } else {
	/*
	 * This putenv() copies instead of taking ownership.
	 */

	ckfree(p);
#endif /* HAVE_PUTENV_THAT_COPIES */
    }

    Tcl_MutexUnlock(&envMutex);

    if (!strcmp(name, "HOME")) {
	/*
	 * If the user's home directory has changed, we must invalidate the
	 * filesystem cache, because '~' expansions will now be incorrect.
	 */

	Tcl_FSMountsChanged(NULL);
    }
}
Ejemplo n.º 18
0
static int
CreateElement(
    const char *name,		/* Name of the element. */
    int create)			/* Boolean, whether the element is being
				 * created explicitly (being registered) or
				 * implicitly (by a derived element). */
{
    ThreadSpecificData *tsdPtr =
	    Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
    Tcl_HashEntry *entryPtr, *engineEntryPtr;
    Tcl_HashSearch search;
    int newEntry, elementId, genericId = -1;
    char *dot;
    StyleEngine *enginePtr;

    /*
     * Find or create the element.
     */

    entryPtr = Tcl_CreateHashEntry(&tsdPtr->elementTable, name, &newEntry);
    if (!newEntry) {
	elementId = PTR2INT(Tcl_GetHashValue(entryPtr));
	if (create) {
	    tsdPtr->elements[elementId].created = 1;
	}
	return elementId;
    }

    /*
     * The element didn't exist. If it's a derived element, find or create its
     * generic element ID.
     */

    dot = strchr(name, '.');
    if (dot) {
	genericId = CreateElement(dot+1, 0);
    }

    elementId = tsdPtr->nbElements++;
    Tcl_SetHashValue(entryPtr, INT2PTR(elementId));

    /*
     * Reallocate element table.
     */

    tsdPtr->elements = ckrealloc(tsdPtr->elements,
	    sizeof(Element) * tsdPtr->nbElements);
    InitElement(tsdPtr->elements+elementId,
	    Tcl_GetHashKey(&tsdPtr->elementTable, entryPtr), elementId,
	    genericId, create);

    /*
     * Reallocate style engines' element table.
     */

    engineEntryPtr = Tcl_FirstHashEntry(&tsdPtr->engineTable, &search);
    while (engineEntryPtr != NULL) {
	enginePtr = Tcl_GetHashValue(engineEntryPtr);

	enginePtr->elements = ckrealloc(enginePtr->elements,
		sizeof(StyledElement) * tsdPtr->nbElements);
	InitStyledElement(enginePtr->elements+elementId);

	engineEntryPtr = Tcl_NextHashEntry(&search);
    }

    return elementId;
}
Ejemplo n.º 19
0
int
class_browser_insert(ClientData clientData,
    Tcl_Interp *interp,             /* Current interpreter. */
    int argc,                       /* Number of arguments. */
    Tcl_Obj *objv[])            /* Argument strings. */
{
    Tcl_CmdInfo     infoPtr;
    char    *textwid;
    register void*textPtr = NULL;
    char image [64];
    char *protected_font, *public_font, *private_font, font[512];
    int del;
    Tcl_CmdProc *text_wdgcmd;

    char    *linebuf;
    int linebuf_pos, linebuf_size = 1024;

    char    *data;
    int data_pos, data_size = 1024;
    int len;
    char    *tag_name;
    char    *imageptr;
    unsigned int attr;
    
    Tcl_Obj *objlist, *next;
    int objlistc, oi;
    
    int wargc;
    char *wargv[12];
    int fld_cou;
    int j, fnd1, fnd2;
    char **flds;
    char *p, * base_classes_of, * sub_classes_of, * viewed_classes;
    char *browsed_class;
    int overridden;
    unsigned int filter, filter1;
    int flags_and;

    char **prev_flds=NULL, **actu_flds=NULL, **next_flds=NULL;

    if (argc < 13 || argc > 14)
    {
	Tcl_AppendResult(interp, "wrong # args:  should be ",
	    Tcl_GetString(objv[0]),
	    " ?-delete? textwidget list base_class_tree"
	    " sub_class_tree viewed_classes"
	    " overridden filter "
	    " protected_font private_font, public_font"
	    " browsed_class, and/or"
	    ,
	    NULL);

	return TCL_ERROR;
    }

    if (Tcl_GetString(objv[1])[0] == '-')
    {
	del = TRUE;
	argc--;
	objv++;
    }
    else
	del = FALSE;
	
    textwid         = Tcl_GetString(objv[1]);        /* tree pathname */
    objlist         = objv[2];        /* list of entries */

    base_classes_of = Tcl_GetString(objv[3]);        /* base classes filter */
    sub_classes_of  = Tcl_GetString(objv[4]);        /* sub classes filter */
    viewed_classes  = Tcl_GetString(objv[5]);        /* list of viewed classes */
    overridden      = atoi (Tcl_GetString(objv[6])); /* overridden flag */
    filter          = atoi (Tcl_GetString(objv[7])); /* member filter */
    filter1         = filter&(~(PAF_OVERRIDE|PAF_OVERLOADED)); /* flags without group flags */

    public_font     = Tcl_GetString(objv[8]);        /* font for public members */
    protected_font  = Tcl_GetString(objv[9]);        /* font for protected members */
    private_font    = Tcl_GetString(objv[10]);       /* font for private members */
    browsed_class   = Tcl_GetString(objv[11]);       /* browsed class in the browser */
    flags_and       = atoi(Tcl_GetString(objv[12])); /* Flag if all flags must be seted */

    if (!Tcl_GetCommandInfo(interp, textwid, &infoPtr))
    {
	Tcl_AppendResult(interp, "unknown widget \"",
	    textwid,"\"",NULL);
	return TCL_ERROR;
    }

    textPtr = (void*)infoPtr.clientData;
    text_wdgcmd = (Tcl_CmdProc *)infoPtr.proc;

    /* set widget state as normal */
    wargc = 0;
    wargv[wargc++] = textwid;
    wargv[wargc++] = "configure";
    wargv[wargc++] = "-state";
    wargv[wargc++] = "normal";
    (*text_wdgcmd)((ClientData)textPtr,interp,wargc,wargv);

    /* delete old items */
    if (del)
    {
	wargc = 0;
	wargv[wargc++] = textwid;
	wargv[wargc++] = "delete";
	wargv[wargc++] = "0";
	wargv[wargc++] = "end";

	(*text_wdgcmd)((ClientData)textPtr,interp,wargc,wargv);
    }

    if (Tcl_ListObjLength(interp, objlist, &objlistc) != TCL_OK)
    {
        return TCL_ERROR;
    }
    if (objlistc == 0)
    {
	return TCL_OK;
    }

    /* using of dynamic buffers */
    linebuf = ckalloc (linebuf_size);
    data    = ckalloc (data_size);

    /* options for inserting items */
    wargc = 0;
    wargv[wargc++] = textwid;
    wargv[wargc++] = "insert";
    wargv[wargc++] = "end";
    wargv[wargc++] = "-image";
    wargv[wargc++] = image;
    wargv[wargc++] = "-font";
    wargv[wargc++] = font;
    wargv[wargc++] = "-data";
    wargv[wargc  ] = data; data_pos = wargc++;
    wargv[wargc++] = "-text";
    wargv[wargc  ] = linebuf; linebuf_pos = wargc++;


    for (j=0, oi=0; oi<=objlistc; j++, oi++)
    {
	/* line scanning is complicated, because at least two lines are
	 * to be stored to compare for overloaded and overridden flags
	 */
	if (oi == objlistc)
	{
	    if (j > 1)
	    {
		if (prev_flds)
		{
		    ckfree ((char *) prev_flds);
		}
		prev_flds = actu_flds;
		actu_flds = next_flds;
		next_flds = NULL;
	    }
	    if (actu_flds == NULL)
	    {
		break;
	    }
	}
	else
	{
	    if (Tcl_ListObjIndex (interp, objlist, oi, &next) != TCL_OK)
	    {
		continue;
	    }
	    if (Tcl_SplitList(interp, Tcl_GetString(next), &fld_cou, &flds) != TCL_OK)
	    {
		continue;
	    }
	
	    if (fld_cou < LIST_CNT)
	    {
		ckfree((char *)flds);
		continue;
	    }
	    if (actu_flds == NULL)
	    {
		actu_flds = flds;
		continue;
	    }
	    if (next_flds == NULL)
	    {
		next_flds = flds;
	    }
	    else
	    {
		if (prev_flds)
		{
		    ckfree ((char *) prev_flds);
		}
		prev_flds = actu_flds;
		actu_flds = next_flds;
		next_flds = flds;
	    }
	}
	if (Tcl_GetInt(interp, actu_flds[ATTR_POS],(int *)&attr) != TCL_OK)
	{
	    continue;
	}

 	/* verify if the class is selected */
	p = Tcl_GetVar2 (interp, viewed_classes, CLASS(actu_flds[CLASS_POS]), TCL_LIST_ELEMENT);
	if (p != NULL && atoi (p) == 0)    /* class not selected */
	{
	    continue;
	}
	
	/* if filter enabled, view only selected member types */
	if (filter1)
	{
	    if (flags_and)
	    {
		if ((filter1&attr)!=filter1)
		{
		    continue;
		}
	    }
	    else
	    {
		int cnt = 0;
		if ((filter1&PAF_STATIC    )!=0 && (attr&PAF_STATIC    )!=0) cnt++;
		if ((filter1&PAF_STRUCT_DEF)!=0 && (attr&PAF_STRUCT_DEF)!=0) cnt++;
		if ((filter1&PAF_INLINE    )!=0 && (attr&PAF_INLINE    )!=0) cnt++;
		if ((filter1&PAF_VIRTUAL   )!=0 && (attr&PAF_VIRTUAL   )!=0) cnt++;
		if ((filter1&PAF_PUREVIRTUAL)==PAF_PUREVIRTUAL && (attr&PAF_PUREVIRTUAL)==PAF_PUREVIRTUAL) cnt ++;
		if (cnt == 0)
		{
		    continue;
		}
	    }
	}

	/* verif if overloaded flag is enabled */
	if (filter & PAF_OVERLOADED)
	{
	    if ((prev_flds && strcmp (actu_flds[MEMBER_POS], prev_flds[MEMBER_POS]) == 0) ||
	        (next_flds && strcmp (actu_flds[MEMBER_POS], next_flds[MEMBER_POS]) == 0))
	    {
	    }
	    else
	    {
		continue;
	    }
	}
	
	/* we need this to build correct image name */
	strcpy (image, "cls_br_");
     	imageptr = image+7;
	if (attr & PAF_PROTECTED)
	{
	   *imageptr++ = 'p';
	}
	if (attr & PAF_STATIC)
	{
	    *imageptr++ = 's';
	}
	if (attr & PAF_VIRTUAL)
	{
	    *imageptr++ = 'v';
	}
	
       /* verify if the member overides a member on the base method
         * or is being overridden by a sub class
         */
         fnd1 = fnd2 = 0;
	/* override flag */
	if (next_flds &&
	    strcmp (next_flds[MEMBER_POS], actu_flds[MEMBER_POS]) == 0 &&
	    strcmp (CLASS(next_flds[CLASS_POS]), CLASS(actu_flds[CLASS_POS])) != 0 && /* different classes */
	    strcmp (next_flds[PARAM_POS ], actu_flds[PARAM_POS ]) == 0)
	{
	    *imageptr++ = OVERRIDE;
	    fnd1 = 0;
	}
	
	/* overridden flag */
	if (prev_flds &&
	    strcmp (prev_flds[MEMBER_POS], actu_flds[MEMBER_POS]) == 0 &&
	    strcmp (CLASS(prev_flds[CLASS_POS]), CLASS(actu_flds[CLASS_POS])) != 0 && /* different classes */
	    strcmp (prev_flds[PARAM_POS ], actu_flds[PARAM_POS ]) == 0)
	{
	    *imageptr++ = OVERRIDDEN;
	    fnd2 = 1;
	}
	
	/* if we don't view the overridden members
	 * or when we view only override/overridden members
	 */
	if ((fnd2 && overridden == 0) ||
	     ((filter & PAF_OVERRIDE) && fnd1 == 0 && fnd2 == 0))
	{
	    continue;
	}

        /* A private member uses a special empty image */
        if (attr & PAF_PRIVATE) {
            char * pstr = "private";
            strcpy (imageptr, pstr);
            imageptr += strlen(pstr);
        }

	/* finish image name */
	strcpy (imageptr, "_image");

	/* make text */
	tag_name= strchr(actu_flds[MEMBER_POS],'(');
	/* function */
	if (tag_name && (strncmp(tag_name + 1,"md",2) == 0 ||
	    strncmp(tag_name + 1,"fr",2) == 0))
	{
	    if (tag_name[1] == 'f')      /* Friend use the private tag. */
	    {
		attr &= ~(PAF_PUBLIC|PAF_PROTECTED); 
	    }
	
	    /* using dynamic buffers */
	    len = strlen (actu_flds[MEMBER_POS]) +
		    strlen (actu_flds[CLASS_POS]) +
		    strlen (actu_flds[TYPE_POS]) +
		    strlen (actu_flds[PARAM_POS]) + 6;
	    if (len > linebuf_size)
	    {
		linebuf_size += len;
		linebuf = ckrealloc (linebuf, linebuf_size);
		wargv[linebuf_pos] = linebuf;
	    }
	    sprintf(linebuf,"%s\t%s\t%s\t(%s)",
		    actu_flds[MEMBER_POS],
		    actu_flds[CLASS_POS],
		    actu_flds[TYPE_POS],
		    actu_flds[PARAM_POS]);
	}
	/* variable */
	else
	{
	    /* using dynamic buffers */
	    len = strlen (actu_flds[MEMBER_POS]) +
		    strlen (actu_flds[CLASS_POS]) +
		    strlen (actu_flds[TYPE_POS]) + 3;
	    if (len > linebuf_size)
	    {
		linebuf_size += len;
		linebuf = ckrealloc (linebuf, linebuf_size);
		wargv[linebuf_pos] = linebuf;
	    }
	    sprintf(linebuf,"%s\t%s\t%s",
		    actu_flds[MEMBER_POS],
		    actu_flds[CLASS_POS],
		    actu_flds[TYPE_POS]);
	}
	
	/* using dynamic buffers */
	len = strlen (actu_flds[FILENAME_POS]) + strlen (actu_flds[FILEPOS_POS]) + 2;
	if (len > data_size)
	{
	    data_size += len;
	    data = ckrealloc (data, data_size);
	    wargv[data_pos] = data;
	}
	/* Add file name and position in the data section */
	sprintf (data, "%s\t%s", actu_flds[FILENAME_POS], actu_flds[FILEPOS_POS]);
	
	if (attr & PAF_PUBLIC)
	    strcpy (font, public_font);
	else if (attr & PAF_PROTECTED)
	    strcpy (font, protected_font);
	else if (attr & PAF_PRIVATE)
	    strcpy (font, private_font);
	
	/*
	 * Add line to browser list
	 */
	(*text_wdgcmd)((ClientData)textPtr,interp,wargc,wargv);	/* Insert ! */
    }

    /* free dynamic buffers */
    ckfree ((void*)linebuf);
    ckfree ((void*)data);

    if (prev_flds) ckfree ((void*)prev_flds);
    if (actu_flds) ckfree ((void*)actu_flds);
    if (next_flds) ckfree ((void*)next_flds);

    return TCL_OK;
}	
Ejemplo n.º 20
0
int
retriever_services(ClientData clientData,
		Tcl_Interp *interp, /* Current interpreter. */
		int argc,           /* Number of arguments. */
		char **argv)        /* Argument strings.    */
{
    Tcl_CmdInfo infoPtr;
    ClientData  wcdata = 0;
    Tcl_CmdProc* wcmd = NULL;
    char *wname;
    char *command, *contents;
    int pargc, i, size, itembuf_size[10], item_size[10];
    char *p, *q, *line_items[10], *pline;
    int result, ret = TCL_OK, fnd = 0;
    int length;

    wname = argv[1];
    if (wname[0])
    {
	if (!Tcl_GetCommandInfo(interp, wname, &infoPtr))
	{
	    Tcl_AppendResult(interp, "wrong # \"",
		wname, "\" does not exist", (char *) NULL);
	    return TCL_ERROR;
	}
	wcdata = infoPtr.clientData;
	wcmd = (Tcl_CmdProc *)infoPtr.proc;
    }
    pargc = 2;
    command   = argv[pargc++];
    contents  = argv[pargc++];

    if (contents[0] == '\0')
    {
	Tcl_SetResult (interp, "", TCL_STATIC);
	return ret;
    }
    Tcl_ResetResult (interp);

    for (i=0;i<10;i++)
    {
	line_items[i] = ckalloc (256);
	itembuf_size[i] = 256;
    }

    /*
     * given a list of found symbols, insert those into the
     * treetable.
     *******************************************************/
    if ((argc == 5 || argc == 4) && *command == 'i' && strcmp (command, "insert") == 0)
    {
	int txtlen = 1024, datalen = 256, newlen, newdlen;
	int rargc = 0, slen;
	char *rargv[13];
	char *text, *data, image[64], *scope;
	int fnd;
	
	text = ckalloc (txtlen);
	data = ckalloc (datalen);
	
	rargc = 0;
	rargv[rargc++] = wname;
	rargv[rargc++] = "insert";
	rargv[rargc++] = "end";
	rargv[rargc++] = "-text";
	rargv[rargc++] = text;
	rargv[rargc++] = "-data";
	rargv[rargc++] = data;
	rargv[rargc++] = "-image";
	rargv[rargc++] = image;
	if (argc == 5)
	{
	    rargv[rargc++] = "-parent";
	    rargv[rargc++] = argv[pargc++];
	}
	
	for (length = strlen(contents), q = contents; 1;)
        {
	    char    *prevlist = q;
	    result = TclFindElement(interp, q, length, &pline, &q, &size, NULL);
	    if (result != TCL_OK || size == 0)
	    {
		break;
	    }
	    length -= q - prevlist;
	
	    /* split line into fields */
	    fnd = SplitLine (pline, size, itembuf_size, line_items, item_size);
	
	    /* use dynamic buffers for data */
	    newdlen = item_size[FIL_POS]
		    + item_size[FRM_POS]
		    + item_size[TO__POS]
		    + 10 /*scope*/ + 4;
	    if (newdlen > datalen)
	    {
		datalen = newdlen + newdlen / 2;
		data = ckrealloc (data, datalen);
		rargv[6] = data;
	    }
	
	    /* we parse files, when number of columns is 1-2 or
	     * the file filed is empty */
	    if (fnd == 2 || fnd == 3 || line_items[FIL_POS][0] == 0)
	    {
		strcpy (image, "file_+_image");
		
		if (line_items[TYP_POS][0] == '1')
		{
		    continue; /* skip directories */
		}
		/* skip first character, if there is a space */
		if (line_items[TYP_POS][0] == ' ')
		{
		    strcpy (line_items[TYP_POS], line_items[TYP_POS]+1);
		}
		
		/* mark the files entries as special entries */
		strcpy (data, "1");
	    }
	    else
	    {
		/* scope name */
		scope = GetScope (line_items[NAM_POS]);
		if (scope[0] == 0)
		{
		    scope = "ud";
		}
		slen = strlen (scope);
		/* skip 'iu' and 'in': there are ref. to includes */
		if (slen == 2 && scope[0] == 'i' && (scope[1] == 'u' || scope[1] == 'n'))
		{
		    continue;
		}
		
		/* only 'fd' 'fu' 'md' 'mi' and 'fr' have parameter list */
		if (! (slen == 2 && (
		    (scope[0] == 'm' && (scope[1] == 'i' || scope[1] == 'd')) ||
		    (scope[0] == 'f' && (scope[1] == 'd' || scope[1] == 'u' || scope[1] == 'r')))))
		{
		    line_items[PRM_POS][0] = 0;
		}
		sprintf (image, "type_%s_image", scope);
		
		sprintf (data, "%s\t%s\t%s\t%s",
			    line_items[FIL_POS],
			    line_items[FRM_POS],
			    line_items[TO__POS],
			    scope);
	    }
	
	    newlen = item_size[NAM_POS]
		    + item_size[CLS_POS]
		    + item_size[TYP_POS]
		    + item_size[PRM_POS]
		    + item_size[FIL_POS]
		    + 5;
	    if (newlen > txtlen)
	    {
		txtlen = newlen + newlen / 2;
		text = ckrealloc (text, txtlen);
		rargv[4] = text;
	    }
	    if (line_items[CLS_POS][0] == '#')
	    {
		line_items[CLS_POS][0] = 0;
	    }
	    sprintf (text, "%s\t%s\t%s\t%s\t%s",
			line_items[NAM_POS],
			line_items[CLS_POS],
			line_items[TYP_POS],
			line_items[PRM_POS],
			line_items[FIL_POS]);
	    (*wcmd)(wcdata, interp, rargc, rargv);
	}
	
	ckfree (data);
	ckfree (text);
    }
    else if (argc == 10 && *command == 'f' && strcmp (command, "filter") == 0)
    {
	char *pattern, *file, *type, *parameter;
	int from, to;
	char *buf;
	int buf_size = 512;
	int length;
	
	pattern   = argv[pargc++];
	file      = argv[pargc++];
	type      = argv[pargc++];
	parameter = argv[pargc++];
	from      = atoi (argv[pargc++]);
	to        = atoi (argv[pargc++]);
	
	buf = (char*)ckalloc (buf_size);
	
	if (*contents) for (length=strlen(contents), q = contents; 1;)
        {
	    char    *prevlist = q;
	    result = TclFindElement(interp, q, length, &pline, &q, &size, NULL);
	    if (result != TCL_OK || size == 0)
	    {
		break;
	    }
	    length -= q - prevlist;
	    SplitLine (pline, size, itembuf_size, line_items, item_size);
	
	    if (*type)
	    {
		if (item_size[TYP_POS] == 0)
		{
		    continue;
		}
		if (strncmp (type, line_items[TYP_POS], item_size[TYP_POS]) != 0)
		{
		    continue;
		}
	    }
	
	    if (*parameter)
	    {
		if (item_size[PRM_POS] == 0)
		{
		    continue;
		}
		/* delete () from parameter list, if parameter doesn't
		 * begin with '(' */
		if (*parameter != '(')
		{
		    for (p=line_items[PRM_POS]; p[1] && p[2]; p++)
		    {
			p[0] = p[1];
		    }
		    p[0] = 0;
		}
		if (strcmp (parameter, line_items[PRM_POS]) != 0)
		{
		    continue;
		}
	    }
	
	    if (*file)
	    {
		if (*file != *line_items[FIL_POS] || strcmp (file, line_items[FIL_POS]) != 0)
		{
		    continue;
		}
	    }
	
	    if (from != -1)
	    {
		if (from < GetNumber (line_items[FRM_POS]))
		{
		    continue;
		}
	    }
	    if (to != -1)
	    {
		if (to > GetNumber (line_items[TO__POS]))
		{
		    continue;
		}
	    }
	
	    if (buf_size < size+1)
	    {
		buf_size += size;
		buf = (char*)ckrealloc (buf, buf_size);
	    }
	    memcpy (buf, pline, size);
	    buf[size] = 0;
	
	    Tcl_AppendElement (interp, buf);
	    fnd = 1;
	}
	
	if (!fnd)
	{
	    Tcl_SetResult (interp, "", TCL_STATIC);
	}
	
	ckfree (buf);
    }
    else
    {
	Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
	     " tree filter contents pattern file type parameter from to |\n"
	     " tree insert contents ?parent?\"",
	     (char *) NULL);
	ret = TCL_ERROR;
    }

    for(i=0;i<10;i++)
    {
	ckfree (line_items[i]);
    }
    return ret;
}
Ejemplo n.º 21
0
/* Append the string, and reallocate buffer when necessary! */
static   char *
LongStringAppend(LongString *str,char *string,int len)
{
   if (len == 0)
   {
      return str->buf;
   }

   if (len < 0)      /* Copy until '\0'! */
   {
      char  *pi;
      char  *po;
      int   s;

      if (!str->buf)
      {
         str->allocated = str->slot_size;
         str->buf = (char*)ckalloc(str->allocated);
         if (!str->buf)
         {
            str->allocated = 0;
            return NULL;
         }
      }
      for (pi = string, po = &str->buf[str->len], s = str->allocated - (po - str->buf) - 1;;)
      {
         char  *bf;

         /* Copy as much as possible! */
         while (s-- > 0 && *pi)
         {
            *po++ = *pi++;
         }

         if (*pi == '\0')  /* We have reached the end. */
            break;

         /* The buffer must be reallocated! */
         str->allocated += str->slot_size;
         bf = (char*)ckrealloc(str->buf,str->allocated);
         if (!bf)
         {
            str->free(str);

            return NULL;
         }
         po = bf + (po - str->buf);
         str->buf = bf;
         s = str->allocated - (po - str->buf) - 1;
      }
      *po = '\0';
      str->len = po - str->buf;
   }
   else
   {
      /* +1 is necessary because we will append '\0' too. */
      if (len >= (int)(str->allocated - str->len))
      {
         char  *bf;

         /* Allocate always bit more to reduce the number of reallocs! */
         if (str->buf)
         {
            str->allocated += len + str->slot_size;
            bf = (char*)ckrealloc(str->buf,str->allocated);
         }
         else
         {
            str->allocated = len + str->slot_size;
            bf = (char*)ckalloc(str->allocated);
         }
         if (!bf)
         {
            str->free(str);

            return NULL;
         }
         str->buf = bf;
      }
      /* printf("COPY: <%*.*s>,%d\n",len,len,string,len); */
      memcpy(&str->buf[str->len],string,len);
      str->len += len;
      str->buf[str->len] = '\0';
   }

   return str->buf;
}
Ejemplo n.º 22
0
void AddToHistogram(Histogram * h, float sc)
{
  int score;
  int moveby;
  int prevsize;
  int newsize;
  int i;

  /* Adding to a histogram conflicts with existing fit:
   * prohibit this.
   */
  if (h->fit_type != HISTFIT_NONE)
    fatal("AddToHistogram(): Can't add to a fitted histogram\n");
  

  /* histogram bins are defined as:  score >= bin value, < bin+1 
   * -1.9 -> -2    -0.4 -> -1    1.9 -> 1
   * -2.1 -> -3     0.4 -> 0     2.1 -> 2
   */
  score = (int) floor(sc);

  /* Check to see if we must reallocate the histogram.
   */
  if (score < h->min)
    {
      prevsize = h->max - h->min + 1;
      moveby   = (h->min - score) + h->lumpsize;
      newsize  = prevsize + moveby;
      h->min  -= moveby;

      h->histogram = (int *) ckrealloc(h->histogram, sizeof(int) * newsize);
      if( h->histogram == NULL ) {
	  fatal("Unable to extend histogram... have to crash... sorry!");
	  }          
      memmove(h->histogram+moveby, h->histogram, sizeof(int) * prevsize);
      for (i = 0; i < moveby; i++)
	h->histogram[i] = 0;
    }
  else if (score > h->max)
    {
      prevsize = h->max - h->min + 1;
      h->max   = h->lumpsize + score;
      newsize  = h->max - h->min + 1;

      h->histogram = (int *) ckrealloc(h->histogram, sizeof(int) * newsize);
      if( h->histogram == NULL ) {
          fatal("Cannot realloc histogram... going to die... sorry!");
	  }
      for (i = prevsize; i < newsize; i++)
	h->histogram[i] = 0;
    }

  /* Bump the correct bin.
   * The bin number is score - h->min
   */
  h->histogram[score - h->min]++;
  h->total++;
  if (score < h->lowscore) h->lowscore   = score;
  if (score > h->highscore) h->highscore = score;

#if DEBUG
  fprintf(stderr, "AddToHistogram(): added %.1f; rounded to %d; in bin %d (%d-%d)\n",
	  sc, score, score-h->min, h->min, h->max);
#endif
  return;
}
Ejemplo n.º 23
0
char *
TnmGetTableValues(TnmTable *table)
{
    static char *buffer = NULL;
    static size_t size = 0;
    TnmTable *elemPtr;
    size_t cnt = 8;
    char *p;

    Tcl_MutexLock(&utilMutex);

    if (buffer == NULL) {
	size = 256;
	buffer = ckalloc(size);
    }

    /*
     * First count the number of bytes that we need to build 
     * the result string and make sure that the buffer is long
     * enough to hold the result.
     */

    if (table) {
	for (elemPtr = table; elemPtr->value; elemPtr++) {
	    cnt += strlen(elemPtr->value) + 2;
	}
    }

    if (cnt > size) {
	size = cnt;
	buffer = ckrealloc(buffer, size);
    }

    /*
     * Build the result string.
     */
    
    p = buffer;
    if (table) {
	for (elemPtr = table; elemPtr->value; elemPtr++) {
	    char *s = elemPtr->value;
	    if (p != buffer) {
		TnmTable *nextPtr = elemPtr + 1;
		*p++ = ',';
		*p++ = ' ';
		if (nextPtr->value == NULL) {
		    *p++ = 'o';
		    *p++ = 'r';
		    *p++ = ' ';
		}
	    }
	    while (*s) {
		*p++ = *s++;
	    }
	}
    }
    *p = '\0';

    Tcl_MutexUnlock(&utilMutex);
    
    return buffer;
}
Ejemplo n.º 24
0
void realloc_seq(SEQ *seq,sint length)
{
	seq->data = (char *)ckrealloc(seq->data, (length+2) * sizeof (char));
	seq->mask = (char *)ckrealloc(seq->mask, (length+2) * sizeof (char));
}
Ejemplo n.º 25
0
static char *
LinkTraceProc(
    ClientData clientData,	/* Contains information about the link. */
    Tcl_Interp *interp,		/* Interpreter containing Tcl variable. */
    CONST char *name1,		/* First part of variable name. */
    CONST char *name2,		/* Second part of variable name. */
    int flags)			/* Miscellaneous additional information. */
{
    Link *linkPtr = (Link *) clientData;
    int changed, valueLength;
    CONST char *value;
    char **pp;
    Tcl_Obj *valueObj;
    int valueInt;
    Tcl_WideInt valueWide;
    double valueDouble;

    /*
     * If the variable is being unset, then just re-create it (with a trace)
     * unless the whole interpreter is going away.
     */

    if (flags & TCL_TRACE_UNSETS) {
	if (Tcl_InterpDeleted(interp)) {
	    Tcl_DecrRefCount(linkPtr->varName);
	    ckfree((char *) linkPtr);
	} else if (flags & TCL_TRACE_DESTROYED) {
	    Tcl_ObjSetVar2(interp, linkPtr->varName, NULL, ObjValue(linkPtr),
		    TCL_GLOBAL_ONLY);
	    Tcl_TraceVar(interp, Tcl_GetString(linkPtr->varName),
		    TCL_GLOBAL_ONLY|TCL_TRACE_READS|TCL_TRACE_WRITES
		    |TCL_TRACE_UNSETS, LinkTraceProc, (ClientData) linkPtr);
	}
	return NULL;
    }

    /*
     * If we were invoked because of a call to Tcl_UpdateLinkedVar, then don't
     * do anything at all. In particular, we don't want to get upset that the
     * variable is being modified, even if it is supposed to be read-only.
     */

    if (linkPtr->flags & LINK_BEING_UPDATED) {
	return NULL;
    }

    /*
     * For read accesses, update the Tcl variable if the C variable has
     * changed since the last time we updated the Tcl variable.
     */

    if (flags & TCL_TRACE_READS) {
	switch (linkPtr->type) {
	case TCL_LINK_INT:
	case TCL_LINK_BOOLEAN:
	    changed = (LinkedVar(int) != linkPtr->lastValue.i);
	    break;
	case TCL_LINK_DOUBLE:
	    changed = (LinkedVar(double) != linkPtr->lastValue.d);
	    break;
	case TCL_LINK_WIDE_INT:
	    changed = (LinkedVar(Tcl_WideInt) != linkPtr->lastValue.w);
	    break;
	case TCL_LINK_WIDE_UINT:
	    changed = (LinkedVar(Tcl_WideUInt) != linkPtr->lastValue.uw);
	    break;
	case TCL_LINK_CHAR:
	    changed = (LinkedVar(char) != linkPtr->lastValue.c);
	    break;
	case TCL_LINK_UCHAR:
	    changed = (LinkedVar(unsigned char) != linkPtr->lastValue.uc);
	    break;
	case TCL_LINK_SHORT:
	    changed = (LinkedVar(short) != linkPtr->lastValue.s);
	    break;
	case TCL_LINK_USHORT:
	    changed = (LinkedVar(unsigned short) != linkPtr->lastValue.us);
	    break;
	case TCL_LINK_UINT:
	    changed = (LinkedVar(unsigned int) != linkPtr->lastValue.ui);
	    break;
	case TCL_LINK_LONG:
	    changed = (LinkedVar(long) != linkPtr->lastValue.l);
	    break;
	case TCL_LINK_ULONG:
	    changed = (LinkedVar(unsigned long) != linkPtr->lastValue.ul);
	    break;
	case TCL_LINK_FLOAT:
	    changed = (LinkedVar(float) != linkPtr->lastValue.f);
	    break;
	case TCL_LINK_STRING:
	    changed = 1;
	    break;
	default:
	    return "internal error: bad linked variable type";
	}
	if (changed) {
	    Tcl_ObjSetVar2(interp, linkPtr->varName, NULL, ObjValue(linkPtr),
		    TCL_GLOBAL_ONLY);
	}
	return NULL;
    }

    /*
     * For writes, first make sure that the variable is writable. Then convert
     * the Tcl value to C if possible. If the variable isn't writable or can't
     * be converted, then restore the varaible's old value and return an
     * error. Another tricky thing: we have to save and restore the interp's
     * result, since the variable access could occur when the result has been
     * partially set.
     */

    if (linkPtr->flags & LINK_READ_ONLY) {
	Tcl_ObjSetVar2(interp, linkPtr->varName, NULL, ObjValue(linkPtr),
		TCL_GLOBAL_ONLY);
	return "linked variable is read-only";
    }
    valueObj = Tcl_ObjGetVar2(interp, linkPtr->varName,NULL, TCL_GLOBAL_ONLY);
    if (valueObj == NULL) {
	/*
	 * This shouldn't ever happen.
	 */

	return "internal error: linked variable couldn't be read";
    }

    switch (linkPtr->type) {
    case TCL_LINK_INT:
	if (Tcl_GetIntFromObj(NULL, valueObj, &linkPtr->lastValue.i)
		!= TCL_OK) {
	    Tcl_ObjSetVar2(interp, linkPtr->varName, NULL, ObjValue(linkPtr),
		    TCL_GLOBAL_ONLY);
	    return "variable must have integer value";
	}
	LinkedVar(int) = linkPtr->lastValue.i;
	break;

    case TCL_LINK_WIDE_INT:
	if (Tcl_GetWideIntFromObj(NULL, valueObj, &linkPtr->lastValue.w)
		!= TCL_OK) {
	    Tcl_ObjSetVar2(interp, linkPtr->varName, NULL, ObjValue(linkPtr),
		    TCL_GLOBAL_ONLY);
	    return "variable must have integer value";
	}
	LinkedVar(Tcl_WideInt) = linkPtr->lastValue.w;
	break;

    case TCL_LINK_DOUBLE:
	if (Tcl_GetDoubleFromObj(NULL, valueObj, &linkPtr->lastValue.d)
		!= TCL_OK) {
#ifdef ACCEPT_NAN
	    if (valueObj->typePtr != &tclDoubleType) {
#endif
		Tcl_ObjSetVar2(interp, linkPtr->varName, NULL,
			ObjValue(linkPtr), TCL_GLOBAL_ONLY);
		return "variable must have real value";
#ifdef ACCEPT_NAN
	    }
	    linkPtr->lastValue.d = valueObj->internalRep.doubleValue;
#endif
	}
	LinkedVar(double) = linkPtr->lastValue.d;
	break;

    case TCL_LINK_BOOLEAN:
	if (Tcl_GetBooleanFromObj(NULL, valueObj, &linkPtr->lastValue.i)
		!= TCL_OK) {
	    Tcl_ObjSetVar2(interp, linkPtr->varName, NULL, ObjValue(linkPtr),
		    TCL_GLOBAL_ONLY);
	    return "variable must have boolean value";
	}
	LinkedVar(int) = linkPtr->lastValue.i;
	break;

    case TCL_LINK_CHAR:
	if (Tcl_GetIntFromObj(interp, valueObj, &valueInt) != TCL_OK
		|| valueInt < SCHAR_MIN || valueInt > SCHAR_MAX) {
	    Tcl_ObjSetVar2(interp, linkPtr->varName, NULL, ObjValue(linkPtr),
		    TCL_GLOBAL_ONLY);
	    return "variable must have char value";
	}
	linkPtr->lastValue.c = (char)valueInt;
	LinkedVar(char) = linkPtr->lastValue.c;
	break;

    case TCL_LINK_UCHAR:
	if (Tcl_GetIntFromObj(interp, valueObj, &valueInt) != TCL_OK
		|| valueInt < 0 || valueInt > UCHAR_MAX) {
	    Tcl_ObjSetVar2(interp, linkPtr->varName, NULL, ObjValue(linkPtr),
		    TCL_GLOBAL_ONLY);
	    return "variable must have unsigned char value";
	}
	linkPtr->lastValue.uc = (unsigned char) valueInt;
	LinkedVar(unsigned char) = linkPtr->lastValue.uc;
	break;

    case TCL_LINK_SHORT:
	if (Tcl_GetIntFromObj(interp, valueObj, &valueInt) != TCL_OK
		|| valueInt < SHRT_MIN || valueInt > SHRT_MAX) {
	    Tcl_ObjSetVar2(interp, linkPtr->varName, NULL, ObjValue(linkPtr),
		    TCL_GLOBAL_ONLY);
	    return "variable must have short value";
	}
	linkPtr->lastValue.s = (short)valueInt;
	LinkedVar(short) = linkPtr->lastValue.s;
	break;

    case TCL_LINK_USHORT:
	if (Tcl_GetIntFromObj(interp, valueObj, &valueInt) != TCL_OK
		|| valueInt < 0 || valueInt > USHRT_MAX) {
	    Tcl_ObjSetVar2(interp, linkPtr->varName, NULL, ObjValue(linkPtr),
		    TCL_GLOBAL_ONLY);
	    return "variable must have unsigned short value";
	}
	linkPtr->lastValue.us = (unsigned short)valueInt;
	LinkedVar(unsigned short) = linkPtr->lastValue.us;
	break;

    case TCL_LINK_UINT:
	if (Tcl_GetWideIntFromObj(interp, valueObj, &valueWide) != TCL_OK
		|| valueWide < 0 || valueWide > UINT_MAX) {
	    Tcl_ObjSetVar2(interp, linkPtr->varName, NULL, ObjValue(linkPtr),
		    TCL_GLOBAL_ONLY);
	    return "variable must have unsigned int value";
	}
	linkPtr->lastValue.ui = (unsigned int)valueWide;
	LinkedVar(unsigned int) = linkPtr->lastValue.ui;
	break;

    case TCL_LINK_LONG:
	if (Tcl_GetWideIntFromObj(interp, valueObj, &valueWide) != TCL_OK
		|| valueWide < LONG_MIN || valueWide > LONG_MAX) {
	    Tcl_ObjSetVar2(interp, linkPtr->varName, NULL, ObjValue(linkPtr),
		    TCL_GLOBAL_ONLY);
	    return "variable must have long value";
	}
	linkPtr->lastValue.l = (long)valueWide;
	LinkedVar(long) = linkPtr->lastValue.l;
	break;

    case TCL_LINK_ULONG:
	if (Tcl_GetWideIntFromObj(interp, valueObj, &valueWide) != TCL_OK
		|| valueWide < 0 || (Tcl_WideUInt) valueWide > ULONG_MAX) {
	    Tcl_ObjSetVar2(interp, linkPtr->varName, NULL, ObjValue(linkPtr),
		    TCL_GLOBAL_ONLY);
	    return "variable must have unsigned long value";
	}
	linkPtr->lastValue.ul = (unsigned long)valueWide;
	LinkedVar(unsigned long) = linkPtr->lastValue.ul;
	break;

    case TCL_LINK_WIDE_UINT:
	/*
	 * FIXME: represent as a bignum.
	 */
	if (Tcl_GetWideIntFromObj(interp, valueObj, &valueWide) != TCL_OK) {
	    Tcl_ObjSetVar2(interp, linkPtr->varName, NULL, ObjValue(linkPtr),
		    TCL_GLOBAL_ONLY);
	    return "variable must have unsigned wide int value";
	}
	linkPtr->lastValue.uw = (Tcl_WideUInt)valueWide;
	LinkedVar(Tcl_WideUInt) = linkPtr->lastValue.uw;
	break;

    case TCL_LINK_FLOAT:
	if (Tcl_GetDoubleFromObj(interp, valueObj, &valueDouble) != TCL_OK
		|| valueDouble < -FLT_MAX || valueDouble > FLT_MAX) {
	    Tcl_ObjSetVar2(interp, linkPtr->varName, NULL, ObjValue(linkPtr),
		    TCL_GLOBAL_ONLY);
	    return "variable must have float value";
	}
	linkPtr->lastValue.f = (float)valueDouble;
	LinkedVar(float) = linkPtr->lastValue.f;
	break;

    case TCL_LINK_STRING:
	value = Tcl_GetStringFromObj(valueObj, &valueLength);
	valueLength++;
	pp = (char **) linkPtr->addr;

	*pp = ckrealloc(*pp, valueLength);
	memcpy(*pp, value, (unsigned) valueLength);
	break;

    default:
	return "internal error: bad linked variable type";
    }
    return NULL;
}
Ejemplo n.º 26
0
/* Reads the contents of a file into a dynamically allocated buffer. */
static   char *
LongStringReadFile(LongString *str,char *filename)
{
   struct   stat  statBuf;
   register unsigned char *p;
   register size_t len;
   int   om = O_RDONLY;
   int   fd;
   int   rdb;
#ifdef WIN32
   om |= O_BINARY;
#endif /* WIN32 */

   fd = open(filename,om);
   if (fd < 0)
   {
      str->free(str);

      return NULL;
   }

   if (fstat(fd, &statBuf) == -1)
   {
      str->free(str);

      close(fd);

      return NULL;
   }

   if (!str->buf)
   {
      p = (char *)ckalloc((size_t)statBuf.st_size+1);
   }
   else
   {
      p = (char *)ckrealloc(str->buf,(size_t)statBuf.st_size+1);

   }
   if (!p)
   {
      str->free(str);

      close(fd);

      return NULL;
   }

   str->buf = p;

   str->len = (int)statBuf.st_size;
   for (p = str->buf,len = (size_t)str->len, rdb = 1;
      rdb > 0 && len > 0;)
   {
      rdb = (size_t)read(fd, p, len);
      if (rdb > 0)
      {
         len -= rdb;
         p += rdb;
      }
   }

   close(fd);

   str->buf[str->len] = '\0';

   return str->buf;
}
Ejemplo n.º 27
0
int
cross_services(ClientData clientData,
		Tcl_Interp *interp, /* Current interpreter. */
		int argc,           /* Number of arguments. */
		char **argv)        /* Argument strings.    */
{
    Tcl_CmdInfo infoPtr;
    ClientData  wcdata;
    Tcl_CmdProc* wcmd;
    char *wname;
    char *command, *contents;
    int pargc, i, size;
    char *pline, *q;
    int result, ret = TCL_OK;

    wname = argv[1];
    if (wname[0])
    {
	if (!Tcl_GetCommandInfo(interp, wname, &infoPtr))
	{
	    Tcl_AppendResult(interp, "wrong # \"",
		wname, "\" does not exist", (char *) NULL);
	    return TCL_ERROR;
	}
	wcdata = infoPtr.clientData;
	wcmd = (Tcl_CmdProc *)infoPtr.proc;
    }
    Tcl_ResetResult (interp);

    pargc = 2;
    command   = argv[pargc++];
    contents  = argv[pargc++];

    if (argc == 12 && *command == 'f' && strcmp (command, "filter") == 0)
    {
	char *refartStr, *testline, *shown_scopes, *ref_access;
	char    *file = NULL;
	enum RefTypes refart;
	char **tfields, **lfields=NULL, **oldfields=NULL;
	char *tmpline;
	int tmpline_size = 512;
	int uniq, have, accept_static, accept_param, fsize, tsize;
	int AddRefArt=0;
	int length;
	char *line[line_arg_count], AddRefartStr[16] = {0};
	Tcl_DString res, erg;
	
	refartStr = argv[pargc++];
	testline  = argv[pargc++];
	uniq      = atoi (argv[pargc++]);
	have      = atoi (argv[pargc++]);
	accept_param = atoi (argv[pargc++]);
	accept_static= atoi (argv[pargc++]);
	shown_scopes = argv[pargc++];
	ref_access   = argv[pargc++];
	
	if (accept_static)
	{
	    /* Information to the actual scope */
	    if (Tcl_SplitList (interp, testline, &tsize, &tfields) != TCL_OK)
	    {
		return TCL_ERROR;
	    }
	    file = tfields[file1_pos];
	}
	if (strcmp (refartStr, "to") == 0)
	{
	    refart = REF_TO;
	}
	else
	{
	    refart = REF_BY;
	}
	
	/* init some variables */
	for (i=0; i<line_arg_count; i++)
	{
	    line[i] = "";
	}
	Tcl_DStringInit(&res);
	Tcl_DStringInit(&erg);
	tmpline = (char*)ckalloc (tmpline_size); tmpline[0] = 0;
	
	for (length=strlen(contents), q = contents; 1;)
        {
        	char    *prevlist = q;
	    result = TclFindElement(interp, q, length, &pline, &q, &size, NULL);
	    if (result != TCL_OK || size == 0)
	    {
		break;
	    }
	    length -= q - prevlist;
	    if (size > tmpline_size)
	    {
		tmpline_size += size;
		tmpline = ckrealloc (tmpline, tmpline_size);
	    }
	    memcpy (tmpline, pline, size);
	    tmpline[size] = 0;
	    if (Tcl_SplitList (interp, tmpline, &fsize, &lfields) != TCL_OK)
	    {
		continue;
	    }
	    if (fsize != DB_COUNT)
	    {
		ckfree ((char*)lfields);
		continue;
	    }
	
	    if (*shown_scopes && strstr (shown_scopes, lfields[DB_SCP2]) == NULL)
	    {
		continue;
	    }
	
	    if (*ref_access && strstr (ref_access, lfields[DB_REFA]) == NULL)
	    {
		continue;
	    }
	
	    if (uniq && oldfields)
	    {
		if (strcmp (oldfields[DB_CLS2], lfields[DB_CLS2]) == 0 &&
		    strcmp (oldfields[DB_SYM2], lfields[DB_SYM2]) == 0 &&
		    strcmp (oldfields[DB_SCP2], lfields[DB_SCP2]) == 0 &&
		    (! accept_param ||
		    (accept_param && strcmp (oldfields[DB_PRM2], lfields[DB_PRM2]) == 0)))
		{
		    if (!AddRefartStr[0] ||
			(lfields[DB_REFA][0] && strchr (AddRefartStr, lfields[DB_REFA][0]) == NULL))
		    {
			strcat (AddRefartStr, lfields[DB_REFA]);
		    }
		    ckfree ((char *) lfields);
		    continue;
		}
	    }

	    /* Static functions and variables */
	    if (accept_static && refart == REF_TO && lfields[DB_REFA][0] != 0 && ! cross_is_type_with_classes(lfields[DB_SCP2]))
	    {
		int attr;
		if (Tcl_GetInt(interp, lfields[DB_REFA], &attr) == TCL_OK &&
		    (! (attr&PAF_STATIC) || strcmp (lfields[DB_FILE], file) != 0))
		{
		    ckfree ((char*)lfields);
		    continue;
		}
	    }
	
	    if (have)
	    {
		ckfree ((char *) lfields);
		Tcl_DStringAppendElement (&erg, "yes");
		break;
	    }
	
	    if (AddRefArt)
	    {
		Tcl_DStringAppendElement(&res, AddRefartStr);
		Tcl_DStringAppendElement(&erg, Tcl_DStringValue(&res));
	    }
	    else
	    {
		AddRefArt = 1;
	    }
	    strcpy (AddRefartStr, lfields[DB_REFA]);
	
	    line[class1_pos] = lfields[DB_CLS2];
	    line[item1_pos]  = lfields[DB_SYM2];
	    line[what1_pos]  = lfields[DB_SCP2];
	    line[param1_pos] = lfields[DB_PRM2];

	    line[file_pos]   = lfields[DB_FILE];
	    line[file_line_pos] = lfields[DB_LINE];
	
	    Tcl_DStringFree (&res);
	    for (i=0; i<refart_pos; i++)
	    {
		Tcl_DStringAppendElement (&res, line[i]);
	    }

	    /* Store last line */
	    if (oldfields)
	    {
		ckfree ((char*)oldfields);
	    }
	    oldfields = lfields;
	}
	if (AddRefArt)
	{
	    Tcl_DStringAppendElement(&res, AddRefartStr);
	    Tcl_DStringAppendElement(&erg, Tcl_DStringValue(&res));
	    AddRefartStr[0] = 0;
	}
	Tcl_DStringFree (&res);
	
	if (accept_static)
	{
	    ckfree ((char*)tfields);
	}
	if (oldfields)
	{
	    ckfree ((char*)oldfields);
	}
	ckfree (tmpline);
	
	Tcl_DStringResult(interp, &erg);
	Tcl_DStringFree  (&erg);
    }
    else if (argc == 7 && *command == 'i' && strcmp (command, "insert") == 0)
    {
    }
    else
    {
	char tmp[32];
	sprintf (tmp, "%i", argc);
	Tcl_AppendResult(interp, "wrong # args(", tmp, "): should be \"", argv[0],
	     " filter \"\" contents RefArt line unique have accept_param accept_static shown_scopes ref_access |\n"
	     "insert widget contents RefArt id line\n",
	     "\"",
	     (char *) NULL);
	ret = TCL_ERROR;
    }
    return ret;
}
Ejemplo n.º 28
0
static unsigned int
LongStringSplit(LongString *str, void *string, int length, int dup_flag, int separator, int maxfields)
{
   int   cou;
   char  sep = (char)separator;
   char  *p = (char *)string;

   if (dup_flag)
   {
      if (str->buf == (char *)string)
	  {
	  	p = str->buf;
	  }
	  else
	  {
        p = str->copy(str,(char *)string,length);
	  }
      string = (void *)p;     /* We have to remember to the beginning. */
   }

   if (!str->field_value)
   {
      str->allocated_field_pointers = FIELD_POINTER_SLOT_SIZE;
      str->field_value = (char**)ckalloc(str->allocated_field_pointers * sizeof(char *));
      str->field_size = (short*)ckalloc(str->allocated_field_pointers * sizeof(unsigned short));
   }
   for (cou = 0, str->field_value[0] = p; *p; p++)
   {
      if (*p == sep)
      {
         if (dup_flag)
            *p = '\0';

         if (cou + 1 == str->allocated_field_pointers)
         {
            str->allocated_field_pointers += FIELD_POINTER_SLOT_SIZE;

            str->field_value = (char**)ckrealloc((char*)str->field_value,
               str->allocated_field_pointers * sizeof(char *));

            str->field_size = (short*)ckrealloc((char*)str->field_size,
               str->allocated_field_pointers * sizeof(unsigned short));
         }
         str->field_size[cou] = (unsigned short)(p - str->field_value[cou]);
         str->field_value[++cou] = p + 1;

         /* The rest will be taken as one field. */
         if (maxfields > 0 && cou == maxfields - 1)
         {
            /* We have to point to the end to compute correct size! */
            if (dup_flag)
            {
               p = str->buf + str->len;
            }
            else if (length > 0)
            {
               p = (char *)string + length + 1;
            }
            else
            {
               p += strlen(p) + 1;
            }
            break;
         }
      }
   }
   str->field_size[cou] = (unsigned short)(p - str->field_value[cou]);

   str->total_fields = cou + 1;
   return (unsigned int)str->total_fields;
}
Ejemplo n.º 29
0
static int *get_trace(const char *aseq, const char *bseq,Local_Overlap *O,int piece,
                      int which){
  static char *aseg=NULL, *bseg=NULL;
  static int asegspace=0,bsegspace=0;
  static int *segtrace[2], tracespace[2]={0,0};
  int alen = 0;
  int blen = 0;
  int spnt=0, *tmptrace=0L;
  int segdiff=0;
  int i=0;
  int iret=0;

  assert(which==0||which==1);

  if(segtrace[which]==NULL){
    tracespace[which]=100;
    segtrace[which]=(int*)ckalloc(sizeof(int)*tracespace[which]);
  }

  iret = safe_substr(&aseg,&asegspace,aseq,O->chain[piece].piece.abpos,
                     O->chain[piece].piece.aepos);
  if(iret == 0){
    fprintf(stderr,"EXCEPTION get_trace: For aseg: len(aseg)=%d, len(bseg)=%d, alen=%d, blen=%d\n",
            (int)strlen(aseg),(int)strlen(bseg), alen,blen);
    return NULL;
  }
  iret = safe_substr(&bseg,&bsegspace,bseq,O->chain[piece].piece.bbpos,
                     O->chain[piece].piece.bepos);
  if(iret == 0){
    fprintf(stderr,"EXCEPTION get_trace: For bseg: len(aseg)=%d, len(bseg)=%d, alen=%d, blen=%d\n",
            (int)strlen(aseg),(int)strlen(bseg), alen,blen);
    return NULL;
  }

  alen=O->chain[piece].piece.aepos-O->chain[piece].piece.abpos;
  blen=O->chain[piece].piece.bepos-O->chain[piece].piece.bbpos;

  //printf("get_trace: len(aseg)=%d, len(bseg)=%d, alen=%d, blen=%d\n",
  //       strlen(aseg),strlen(bseg), alen,blen);

  /* get trace for the segment from AS_ALN_OKNAlign */
  spnt=0; 
  /* subtract because Gene likes to point to one before string start */
  aseg--; 
  bseg--;
  segdiff=(int)((O->chain[piece].piece.aepos-O->chain[piece].piece.abpos)
		*(1.5*O->chain[piece].piece.error)    +10);
  
  tmptrace=AS_ALN_OKNAlign(aseg,alen,bseg,blen,&spnt,segdiff);

  if(spnt!=0){
    if(spnt>0){
      O->chain[piece].agap+=spnt;
      O->chain[piece].piece.abpos+=spnt;
      i=0; 
      while(tmptrace[i]!=0){
	if(tmptrace[i]<0){
	  tmptrace[i]+=spnt;
	}
	i++;
      }
    } else {
      O->chain[piece].bgap+=-spnt;
      O->chain[piece].piece.bbpos+=-spnt;
      i=0; 
      while(tmptrace[i]!=0){
	if(tmptrace[i]>0){
	  tmptrace[i]+=spnt;
	}
	i++;
      }
    }
  }      
  aseg++; /* restore because need to know where memory block is allocated,
  		 and so that next time around strncpy will work right! */
  bseg++;
  i=0;
  while(tmptrace[i]!=0){
    segtrace[which][i]=tmptrace[i];
    i++;
    if(i==tracespace[which]){
      tracespace[which]*=2;
      segtrace[which]=(int*)ckrealloc(segtrace[which],
                                      sizeof(int)*tracespace[which]);
    }
  }
  segtrace[which][i]=0;
  return(segtrace[which]);

}
Ejemplo n.º 30
0
int *AS_Local_Trace(Local_Overlap *O, const char *aseq, const char *bseq){
  static int *TraceBuffer=NULL;
  int i,j,k,segdiff,*segtrace;
  int lastgood=-1;
  static int allocatedspace=0;
  int tracespace=0;
  static char *aseg=NULL,*bseg=NULL;

  //  Not computing traces generates slight differences.  Why?
  //
  const int computeTraceFlag = 0;

  static int aseglen=0,bseglen=0;
  int abeg=0,bbeg=0; /* begining of segment; overloaded */
  int tracep=0; /* index into TraceBuffer */
  int spnt=0; /* to pass to AS_ALN_OKNAlign */


  {
    int n=O->num_pieces;
    assert(O->num_pieces>0);
    O->chain[n].piece.abpos=O->chain[n].agap+O->chain[n-1].piece.aepos;
    O->chain[n].piece.bbpos=O->chain[n].bgap+O->chain[n-1].piece.bepos;
  }

  if(computeTraceFlag){
    /*Estimate length required to store trace*/
    tracespace=0;
    tracespace+=abs(O->begpos)+abs(O->endpos);
    for(i=0;i<=O->num_pieces;i++){
      tracespace+=max(O->chain[i].agap,1);
      tracespace+=max(O->chain[i].bgap,1);
      tracespace+=(int)((O->chain[i].piece.aepos
                         -O->chain[i].piece.abpos)
                        *1.5*O->chain[i].piece.error);
      tracespace+=1000;
    }
    
    /*(Re)allocate space for the trace as necessary;
      Note that this is persistent storage so ...
      ... it doesn't need to get allocated on every call
      ... it shouldn't get freed
      ... results stored here need to be copied elsewhere if they
      are to be saved
    */
    if(allocatedspace<tracespace){
      allocatedspace=2*tracespace;
      if(TraceBuffer==NULL){
        TraceBuffer=(int*)ckalloc(sizeof(int)*allocatedspace);
      } else {
        TraceBuffer=(int*)ckrealloc(TraceBuffer,sizeof(int)*allocatedspace);
      }
    }
  }  //  computeTraceFlag
  
  /* for each Local_Overlap chain[i].piece, 
     need to handle the gap at the beginning and
     (for all but the final piece) the associated aligned segment */
  for(i=0;i<=O->num_pieces;i++){

    /* if conditions indicate the segment was deleted in previous loop,
       skip! */
    if(O->chain[i].agap==0 &&
       O->chain[i].bgap==0 &&
       O->chain[i].piece.abpos==O->chain[i].piece.aepos &&
       O->chain[i].piece.bbpos==O->chain[i].piece.bepos){
      continue;
    }

    /* guesstimate the required number of diagonals/edits to consider to
       get optimal alignment */
    segdiff=1+(int)((O->chain[i].piece.aepos     
		     -O->chain[i].piece.abpos)   
		    *1.5*O->chain[i].piece.error);



    /* Building an alignment/trace under the usual assumptions does not allow
       a given position in one sequence to simultaneously align to two or more
       positions in the other sequence.  However, the Find_Local_Overlap()
       routine can chain together local alignment segments that overlap.

       In order to make the local overlaps compatible with everything else
       we do, we need to trim back the overlaps.  
    
       Since we will "output" this segment at the end of the loop,
       we need to fix its overlap with the following segment in this
       cycle through the loop  
    */

    k=i+1;
    while(k<O->num_pieces){

      /* if conditions indicate the segment was deleted previously, skip! */
      if(O->chain[k].agap==0 &&
	 O->chain[k].bgap==0 &&
	 O->chain[k].piece.abpos==O->chain[k].piece.aepos &&
	 O->chain[k].piece.bbpos==O->chain[k].piece.bepos){
	k++;
	continue;
      }

      if(O->chain[k].piece.abpos<O->chain[i].piece.aepos||
         O->chain[k].piece.bbpos<O->chain[i].piece.bepos){

	/* handle possibility of the first segment being
           contained within the second;originally simply asserted
           against this; now, try to handle by deleting first segment */

	if(O->chain[i].piece.abpos>O->chain[k].piece.abpos||
	   O->chain[i].piece.bbpos>O->chain[k].piece.bbpos){


	  O->chain[i].agap=0;
	  O->chain[i].bgap=0;
	  if(lastgood>=0){
	    O->chain[i].piece.abpos=O->chain[lastgood].piece.aepos;
	    O->chain[i].piece.aepos=O->chain[lastgood].piece.aepos;
	    O->chain[i].piece.bbpos=O->chain[lastgood].piece.bepos;
	    O->chain[i].piece.bepos=O->chain[lastgood].piece.bepos;
	  } else {
	    O->chain[i].piece.abpos=0;
	    O->chain[i].piece.aepos=0;
	    O->chain[i].piece.bbpos=0;
	    O->chain[i].piece.bepos=0;
          }
	  O->chain[k].agap=O->chain[k].piece.abpos-
	    O->chain[i].piece.aepos;
	  O->chain[k].bgap=O->chain[k].piece.bbpos-
	    O->chain[i].piece.bepos;
	  if(lastgood<0){
            //printf("Shrinking gaps for segment %d\n",k);
	    O->chain[k].agap--;
	    O->chain[k].bgap--;
	  }

        } else 	/* otherwise, check for 2nd piece contained within first */
	  if(O->chain[i].piece.aepos>O->chain[k].piece.aepos||
             O->chain[i].piece.bepos>O->chain[k].piece.bepos){

            /* if the next piece is completely within current piece, 
               effectively remove it */

            O->chain[k].agap = 0;
            O->chain[k].bgap = 0;
            O->chain[k].piece.abpos=O->chain[i].piece.aepos;
            O->chain[k].piece.aepos=O->chain[i].piece.aepos;
            O->chain[k].piece.bbpos=O->chain[i].piece.bepos;
            O->chain[k].piece.bepos=O->chain[i].piece.bepos;
            if(k+1<=O->num_pieces){
              int l;
              l=k-1;
              while(O->chain[l].agap==0 &&
                    O->chain[l].bgap==0 &&
                    O->chain[l].piece.abpos==O->chain[l].piece.aepos &&
                    O->chain[l].piece.bbpos==O->chain[l].piece.bepos){
                l--;
                assert(l>=0);
              }
	      

              O->chain[k+1].agap=O->chain[k+1].piece.abpos-
                O->chain[l].piece.aepos;
              O->chain[k+1].bgap=O->chain[k+1].piece.bbpos-
                O->chain[l].piece.bepos;
            }

            /* else, fix the overlap */
          } else {

            fix_overlapping_pieces(aseq,
                                   bseq,
                                   O,i,k);


            // if the second piece disappeared
            if(O->chain[k].piece.abpos==O->chain[k].piece.aepos||
               O->chain[k].piece.bbpos==O->chain[k].piece.bepos){

              O->chain[k].agap = 0;
              O->chain[k].bgap = 0;
              O->chain[k].piece.abpos=O->chain[i].piece.aepos;
              O->chain[k].piece.aepos=O->chain[i].piece.aepos;
              O->chain[k].piece.bbpos=O->chain[i].piece.bepos;
              O->chain[k].piece.bepos=O->chain[i].piece.bepos;
              if(k+1<=O->num_pieces){
                int l;
                l=k-1;
                while(O->chain[l].agap==0 &&
                      O->chain[l].bgap==0 &&
                      O->chain[l].piece.abpos==O->chain[l].piece.aepos &&
                      O->chain[l].piece.bbpos==O->chain[l].piece.bepos){
                  l--;
                  assert(l>=0);
                }
	      
                O->chain[k+1].agap=O->chain[k+1].piece.abpos-
                  O->chain[l].piece.aepos;
                O->chain[k+1].bgap=O->chain[k+1].piece.bbpos-
                  O->chain[l].piece.bepos;
              }
            } else {
              // if the first piece disappeared
              if (O->chain[i].piece.abpos==O->chain[i].piece.aepos||
                  O->chain[i].piece.bbpos==O->chain[i].piece.bepos){

                  O->chain[i].agap=0;
                O->chain[i].bgap=0;
                if(lastgood>=0){
                  O->chain[i].piece.abpos=O->chain[lastgood].piece.aepos;
                  O->chain[i].piece.aepos=O->chain[lastgood].piece.aepos;
                  O->chain[i].piece.bbpos=O->chain[lastgood].piece.bepos;
                  O->chain[i].piece.bepos=O->chain[lastgood].piece.bepos;
                } else {
                  O->chain[i].piece.abpos=0;
                  O->chain[i].piece.aepos=0;
                  O->chain[i].piece.bbpos=0;
                  O->chain[i].piece.bepos=0;
                }
                O->chain[k].agap=O->chain[k].piece.abpos-
                  O->chain[i].piece.aepos;
                O->chain[k].bgap=O->chain[k].piece.bbpos-
                  O->chain[i].piece.bepos;
                if(lastgood<0){
                  //printf("Shrinking gaps for segment %d\n",k);
                  O->chain[k].agap--;
                  O->chain[k].bgap--;
                }
              }
            }
          }
      }

      k++;
    }
  
    /* if conditions indicate the segment was deleted previously, skip! */
    if(O->chain[i].agap==0 &&
       O->chain[i].bgap==0 &&
       O->chain[i].piece.abpos==O->chain[i].piece.aepos &&
       O->chain[i].piece.bbpos==O->chain[i].piece.bepos){
      continue;
    }
  
    /* set up positions before which gaps are inserted to handle
       the gap portion of a chain piece */

    /* put gaps before beginning of aligned piece (but after the portion
       of aseq in the gap); location is relative to the beginning of the
       alignment (i.e., ignores ahang worth of positions)  */

    if(i!=O->num_pieces){
      abeg=O->chain[i].piece.abpos;
    } else {
      assert(lastgood>=0&&lastgood<O->num_pieces);
      abeg=O->chain[lastgood].piece.aepos+
	O->chain[i].agap;
    }

    /*handle boundary case to prevent gaps preceding the b sequence*/
    if((i==0||lastgood<0)&&O->chain[i].bgap>0){
      assert(O->chain[i].agap>=0);

      if(O->begpos>=0){
        O->begpos=O->chain[i].piece.abpos-1;
        assert(O->begpos>=0);
        O->chain[i].agap=0;

        //  Instead of asserting, an ifdef previously printed stuff
        //  out and continued happily along.
        //
        assert( ( i==0&&	O->chain[i].bgap==O->chain[i].piece.bbpos-1)
                ||( i>0&&lastgood<0&&O->chain[i].bgap==O->chain[i].piece.bbpos-1)) ;

        if(lastgood<0){
          O->chain[i].bgap=O->chain[i].piece.bbpos-1;
        }
      } else {
        if(i==0){
          O->begpos-=O->chain[i].bgap;
        } else{
          O->begpos=-O->chain[i].bgap;
        }
        O->chain[i].bgap=0;
      }
      
    }

    /* now prevent gaps at end of A sequence */
    //    if(i==O->num_pieces&&O->endpos>=0){
    //      O->endpos+=O->chain[i].bgap;
    //      O->chain[i].bgap=0;
    //    }

    /* now make sure that end mismatches are treated by tucking
       the shorter tail into a gap before the longer tail,
       or, ifdef FORCEPOSITIVEBHANG, by tucking the A tail into
       a gap before the B tail */
    if(i==O->num_pieces){
      if(O->endpos>=0){
	O->endpos+=O->chain[i].bgap;
	O->chain[i].bgap=0;
      }else {
	O->endpos-=O->chain[i].agap;
	abeg-=O->chain[i].agap;
	O->chain[i].agap=0;
      }	
    }

    /* put gaps before the portion of bseq in the gap; for the first
       piece, this means before position 0 */
    if(i==0 || lastgood<0){
      bbeg = 1-min(O->begpos,0);
    } else {
      assert(lastgood<O->num_pieces);
      bbeg = O->chain[lastgood].piece.bepos;
    }

    /* now insert the right number of gaps! */

    if(i==O->num_pieces){

      if(O->endpos<0){
	O->chain[i].agap+=-O->endpos;
	O->endpos=0;
      } else {
	O->chain[i].bgap+=O->endpos;
	O->endpos=0;
      }

      if(O->chain[i].agap <= O->chain[i].bgap){
	O->endpos=O->chain[i].bgap;
	O->chain[i].bgap=0;
      }else{
	O->endpos=-O->chain[i].agap;
	O->chain[i].agap=0;
      }
    }


    if (computeTraceFlag) {
      if(O->chain[i].agap <= O->chain[i].bgap || ! useSizeToOrderBlocks ){
        /* start by putting len(agap) gaps before the chunk of B in the gap */
        for(j=0; j<O->chain[i].agap ;j++)
          TraceBuffer[tracep++]=bbeg;

        /* then put len(bgap) gaps before the chunk of A in the gap */
        for(j=0; j<O->chain[i].bgap ;j++)
          TraceBuffer[tracep++]=-abeg;
      } else { // if the bgap is smaller,
        abeg-=O->chain[i].agap;
        bbeg+=O->chain[i].bgap;

        /* start by putting len(bgap) gaps before the chunk of A in the gap */
        for(j=0;j<O->chain[i].bgap   ;j++)
          TraceBuffer[tracep++]=-abeg;

        /* then put len(agap) gaps before the chunk of B in the gap */
        for(j=0;j<O->chain[i].agap ;j++)
          TraceBuffer[tracep++]=bbeg;
      }
    } else {
      //  Not computing traces!
      if(O->chain[i].agap <= O->chain[i].bgap || ! useSizeToOrderBlocks ){
      } else {
        abeg-=O->chain[i].agap;
        bbeg+=O->chain[i].bgap;
      }
    }


    ///////////////////////////////////////  

    /* if last piece, there is no aligned segment */

    if(i==O->num_pieces)break;

    /* set bbeg to beginning of aligned segment for piece */

    abeg=O->chain[i].piece.abpos;
    bbeg=O->chain[i].piece.bbpos;

    /* set lengths of segments */
  
    int alen=O->chain[i].piece.aepos-abeg; /* check +1?? */
    int blen=O->chain[i].piece.bepos-bbeg; /* check +1?? */

    /* create strings for just the parts of the sequences in the
       aligned segment */

    /* make sure there is (persistant) space for the strings */
    if(aseglen<alen+1){
      aseglen=2*(alen+1);
      if(aseg==NULL){
	aseg=(char*)ckalloc(sizeof(char)*aseglen);
      } else {
	aseg=(char*)ckrealloc(aseg,sizeof(char)*aseglen);
      }
    }
    if(bseglen<blen+1){
      bseglen=2*(blen+1);
      if(bseg==NULL){
	bseg=(char*)ckalloc(sizeof(char)*bseglen);
      } else {
	bseg=(char*)ckrealloc(bseg,sizeof(char)*bseglen);
      }
    }
  
    /* CMM we do not need the trace computed */
    if (computeTraceFlag) {

      /* copy the segments */

      strncpy(aseg, aseq+abeg, alen);
      strncpy(bseg, bseq+bbeg, blen);

      aseg[alen] = 0;
      bseg[blen] = 0;

      if (((int)strlen(bseg) != blen) ||
          ((int)strlen(aseg) != alen)) {
        fprintf(stderr,"EXCEPTION strlen(aseg)=%d alen=%d abeg=%d\n", (int)strlen(aseg), alen, abeg);
        fprintf(stderr,"EXCEPTION strlen(bseg)=%d blen=%d bbeg=%d\n", (int)strlen(bseg), blen, bbeg);

        fprintf(stderr,"EXCEPTION aseg=<%s>\n", aseg);
        fprintf(stderr,"EXCEPTION bseg=<%s>\n", bseg);

        fprintf(stderr,"EXCEPTION aseq=<%s>\n", aseq + 1);
        fprintf(stderr,"EXCEPTION bseq=<%s>\n", bseq + 1);

        return NULL; // Return an exceptional value.
      }

      /* guesstimate the required number of diagonals/edits to consider to
         get optimal alignment */
      segdiff = 1 + (int)((O->chain[i].piece.aepos - O->chain[i].piece.abpos) * 1.5 * O->chain[i].piece.error);


      /* get trace for the segment from AS_ALN_OKNAlign */
      spnt=0; 
      /* subtract from aseg, bseg because Gene likes to index from 1, not 0 */
      segtrace=AS_ALN_OKNAlign(aseg-1,alen,bseg-1,blen,&spnt,segdiff);
      // This adjusts the beginning coordinates so that segment is
      // consistent with the back-trace.

      if(spnt>0){
        O->chain[i].agap+=spnt;
        O->chain[i].piece.abpos+=spnt;
      } else {
        O->chain[i].bgap-=spnt;
        O->chain[i].piece.bbpos-=spnt;
      }

      /* get trace for the segment from AS_ALN_OKNAffine */
      /* Seems like it should be a good idea, but doesn't work as well
         as we might expect! */
      //bpnt=0; 
      //epnt=0; 
      //segtrace=AS_ALN_OKNAffine(aseg,alen,bseg,blen,&bpnt,&epnt,segdiff);

      assert(segtrace!=NULL);

      /* Now copy the segment trace into master trace, adjusting positions */
      j=0;

      if(spnt<0){
        for(int ctr=0;ctr<abs(spnt);ctr++)
          TraceBuffer[tracep++]=-abeg;
      } else {
        for(int ctr=0;ctr<spnt;ctr++)
          TraceBuffer[tracep++]=bbeg;
      }

      while(segtrace[j]!=0){
        if(segtrace[j]<0){
          TraceBuffer[tracep++]=-abeg+segtrace[j++]+1 /* -max(0,spnt) ?? */;
        } else {
          TraceBuffer[tracep++]=bbeg+segtrace[j++]-1 /* +max(0,-spnt) ?? */;
        }
      }
    }  //  computeTraceFlag
    
    /* set lastgood to this segment */

    lastgood=i;

    /* and back to the top of the loop for another overlap piece */
  }  

  /* terminate the trace */
  if(TraceBuffer != NULL) {
    TraceBuffer[tracep]=0;

    if (tracep >= allocatedspace)
      fprintf(stderr,"ERROR memory is already corrupted in %s at %d.\n", __FILE__, __LINE__);
    assert(tracep < allocatedspace);
  }

  return(TraceBuffer);
}