Esempio n. 1
0
// This function frees its cons cells. It can only be used on basic types from tokenizer.
void cleanup(Value* head){
  if (head && (head->type == cellType)){
    Value *second;

    while (head->cons->cdr){
      if (head->type != cellType) {
	freeValue(head);
	return;
      }
      if (head->cons->cdr->type != cellType) {
	freeValue(head);
	return;
      }
      second = (head->cons)->cdr;
      if (head->cons->car->type == stringType){
	free(head->cons->car->stringValue);
	free(head->cons->car);
      }else if (head->cons->car->type == symbolType){
	free(head->cons->car->symbolValue);
	free(head->cons->car);
      }else if (head->cons->car->type == cellType){
	cleanup(head->cons->car);
      }else{
	free(head->cons->car);
      }
      free(head->cons);
      free(head);
      head = second;
    }
    if (head && head->type == cellType) {
      freeValue(head);
    }
  }    
}
Esempio n. 2
0
/*
 * Convert rule-value to datum using namefield information
 */
datum *
ruleValueToDatum(__nis_table_mapping_t *t, __nis_rule_value_t *rv, int *statP) {
	__nis_value_t	*val;
	datum		*value;
	char		*str, *cstr, commentSep[3] = {' ', 0, 0};
	char		*myself = "ruleValueToDatum";

	/* No error yet */
	*statP = 0;

	/* Return empty datum if no namefield information available */
	if (t->e == 0) {
		if ((value = am(myself, sizeof (*value))) == 0)
			*statP = MAP_NO_MEMORY;
		return (value);
	}

	val = getMappingFormatArray(t->e->element.match.fmt, rv,
				fa_item, t->e->element.match.numItems,
				t->e->element.match.item);

	if (val && val->val && val->val->value) {
		if ((value = am(myself, sizeof (*value))) == 0) {
			*statP = MAP_NO_MEMORY;
			freeValue(val, 1);
			return (0);
		}

		/* Strip trailing whitespaces */
		cstr = (char *)val->val->value + val->val->length;
		for (; cstr >= (char *)val->val->value &&
			(*cstr == ' ' || *cstr == '\t'); *cstr-- = '\0');

		if (t->commentChar != '\0' &&
		    (str = findVal(N2LCOMMENT, rv, mit_nisplus)) != 0 &&
		    *str != '\0') {
			commentSep[1] = t->commentChar;
			cstr = scat(myself, F, commentSep, str);
			if (cstr) {
				value->dptr = scat(myself, F,
						val->val->value, cstr);
				sfree(cstr);
			}
		} else {
			value->dptr = sdup(myself, T, val->val->value);
		}
		freeValue(val, 1);
		if (value->dptr) {
			value->dsize = strlen(value->dptr);
			return (value);
		} else {
			*statP = MAP_NO_MEMORY;
			sfree(value);
			return (0);
		}
	}

	*statP = MAP_NAMEFIELD_MATCH_ERROR;
	return (0);
}
Esempio n. 3
0
// allows us to evaluate user defined functions
Value evalApply(Value expr, Frame *env) {
    // evaluate function
    Value f = eval(expr.consValue->car, env);
    if( (f.type != closureType) && (f.type != primitiveType) ){
        return evaluationError("first argument of a function applcation did not evaluate to a proceedure");
    }

    // evaluate arguments
    Vector args;
    initVector(&args,8);
    int i = 1;
    Value curr = getNth(expr.consValue, i); 
    while(curr.type != openType){
        Value *storedVal = (Value *)malloc(sizeof(Value));
        (*storedVal) = valdup(eval(curr,env));
        if((*storedVal).type == openType){ //This is an error
            void *pointerToAVal = NULL;
            for(int j = 0; j < i-2; j++){
                getVectorItem(&args, j, &pointerToAVal);
                freeValue(*((Value *)pointerToAVal));
                free((Value *)pointerToAVal);
            }
            free(storedVal);
            return evaluationError(NULL);
        }
        insertVectorItem(&(args), i-1, (void *)storedVal);
        i++;
        curr = getNth(expr.consValue, i);
    }
    
    // apply the function
    Value ret = apply(f, args);
    cleanupVector(&args);
    return ret;
}
Esempio n. 4
0
File: Value.c Progetto: zurl/MUA
void dfsFreeList(ListNode *x) {
	if (x != NULL) {
		dfsFreeList(x->next);
		freeValue(x->data);
		free(x);
	}
}
Esempio n. 5
0
//
// Assignment operator
//
Variant & Variant::operator= (const std::wstring & value)
{
	freeValue();
	m_Type = WideString;
	assignString(value.data(), value.length());
	return *this;
}
Esempio n. 6
0
/*
  Insert key = id, payload = value into the hash table.
  Auto doubling the hash table if the size of the hash table reaches 2/3 of the capacity, 
   where 2/3 is the load factor.
*/
int insertItem(HashTable* table, char* id, Value* value){
  if (table){
    if ((table->size) >= ((table->capacity)*2/3)){
      autoDouble(table);
    }
    
    int key = hash(table, id);
    if (key == -1){
      return 0;
    }
    int length = 0;
    while (id[length]!='\0'){
      length++;
    }
    length++;
    char* copiedID = (char*)malloc(sizeof(char)*length);
    strncpy(copiedID,id,length-1);
    copiedID[length-1]='\0';
    Value* keyVal = (Value*)malloc(sizeof(Value));
    keyVal->type = symbolType;
    keyVal->symbolValue = copiedID;
    
    if ((table->entries)[key].cdr){
      freeValue((table->entries)[key].cdr);
    }
    (table->entries)[key].car = keyVal;
    (table->entries)[key].cdr = deepCopy(value);
    (table->size)++;
    return 1;
  }
  else{
    return 0;
  }
}
Esempio n. 7
0
//
// Assignment operator
//
Variant & Variant::operator= (const char * value)
{
	freeValue();
	m_Type = String;
	assignString(value ? value : "", value ? strlen(value) : 0);
	return *this;
}
Esempio n. 8
0
//
// Assignment operator
//
Variant & Variant::operator= (const wchar_t * value)
{
	freeValue();
	m_Type = WideString;
	assignString(value ? value : L"", value ? wcslen(value) : 0);
	return *this;
}
Esempio n. 9
0
/** Odstrani ze zasobniku \a i polozek (EOF se odstranit nesmi!) */
void removeFromExpStack(ExpStack* stack, int i){
    assert( i >= 1 && i <= stack->count );
    
    // uvolneni odstranovanych prvku
    for( int ii = stack->count - i - 1 ; ii < stack->count ; ii++ )
    {
        if(stack->array[ii].type==term)
        {
            if( stack->array[ii].val.term.type == tokId )
            {
                deleteRCString( &stack->array[ii].val.term.data.id );
            }
            if( stack->array[ii].val.term.type == tokLiteral )
            {
                freeValue( &stack->array[ii].val.term.data.val );
            }
        }
    }
    // snizeni poctu prvku
    stack->count -= i;
    
    // nalezeni noveho nejvrcholovejsiho terminalu
    for( i=stack->count-1; stack->array[i].type!=term; i-- );
    stack->termIndex = i;
}
Esempio n. 10
0
void NetValue::reallocValue(unsigned char type) {
    freeValue();

    if (m_type != type) {
        m_type = type;
    }

    allocValue();
}
Esempio n. 11
0
// cleanup frees the contents of the closure, but does not free closure itself.
void cleanupClosure(Closure *closure){
  if (closure && closure->args && closure->body){
    cleanup(closure->args->head);
    if (closure->identifier){
      free(closure->identifier);
    }
    free(closure->args); 
    freeValue(closure->body);
  }
}
Esempio n. 12
0
double valueToDouble(Value* v) {
  double d, n;
  BoolExpr* be;
  Value* u;
  List* node;
  n = 0.0;
  if (v->type == 'n')
    return *(double*)v->data;
  if (v->type == 's')
    return atof(((String*)v->data)->val);
  if (v->type == 'b') {
    be = evaluateBoolExpr((BoolExpr*)v);
    if (be == NULL) return NAN;
    return (double)(be->lasteval);
  }
  if (v->type == 'c') {
    u = (Value*)evaluateFuncVal((FuncVal*)v);
    if (u == NULL) return NAN;
    d = valueToDouble(u);
    freeValue(u);
    return d;
  }
  if (v->type == 'd')
    return valueToDouble(evaluateStatements(v->data));
  if (v->type == 'l') {
    if (!v->data)
      return 0.0;
    for (node = ((List*)v->data)->next;node != NULL;node = node->next) {
      u = evaluateValue(node->data);
      d = valueToDouble(u);
      freeValue(u);
      if (d < 0) return d;
      n += d;
    }
    return n;
  }
  if (v->type == 'v') {
    u = evaluateValue(v);
    if (u == NULL) return NAN;
    n = valueToDouble(u);
  }
  return n;
}
Esempio n. 13
0
void BBT_free(BalancedBinaryTree b, void (*freeValue)(void*)) {
	BalancedBinaryTree lb,rb;
	if(b!=NULL) {
		lb = BBT_getLeftBranch(b);
		rb = BBT_getRightBranch(b);
		freeValue(BBT_getValue(b));
		free(b);
		BBT_free(lb, freeValue);
		BBT_free(rb, freeValue);
	}
}
Esempio n. 14
0
void destroy(LinkedList *list) {
   Node *current = list->head;
   while(current) {
      Node *next = current->next;
      freeValue(current->value);
      free(current);
      current = next;
   }
   // also want to free the values -- want to loop through value list and free each value
   list->head = NULL; // think about memory
   list->tail = NULL;
}
Esempio n. 15
0
void freeValue(Value *value){
  if (!value){
    return;
  }

  if (value->type == stringType){
    free(value->stringValue);
    free(value);
    
  }else if (value->type == symbolType){
    free(value->symbolValue);
    free(value);
  
  }else if (value->type == envType){
    //destroyFrame(value->envValue);
    //free(value);
  }else if (value->type == cellType){
    assert(value->cons!=NULL);
    assert(value->cons->car!=NULL);
    freeValue(value->cons->car);
    freeValue(value->cons->cdr);
    free(value->cons);
    free(value);

  }else if (value->type == primitiveType){
    
    free(value);
  }else if (value->type == closureType){
    destroyClosure(value->closureValue);
    free(value);
  }else if (value->type == tableType){
    destroyTable(value->tableValue);
    free(value);
  }else{
    free(value);
  }
   
}
Esempio n. 16
0
void
printMappingFormat(__nis_mapping_format_t *f) {
	__nis_value_t	*val = getMappingFormat(f, 0, fa_any, 0, 0);
	int		i;
	char		*myself = "printMappingFormat";

	if (val == 0)
		return;

	for (i = 0; i < val->numVals; i++) {
		c2buf(myself, val->val[i].value, val->val[i].length);
	}
	freeValue(val, 1);
}
Esempio n. 17
0
/*
  Delete an item with key = id.
  This function does not free the payload since we did not malloc memory 
  inside the insertItem function for payload.
*/
int deleteItem(HashTable* table, char* id){
  ConsCell* entry = lookupEntry(table, id);
  if (entry){
    if (entry->car && entry->car->type == symbolType){
      free(entry->car->symbolValue); 
      free(entry->car);
      freeValue(entry->cdr);
     }
     entry->car = NULL;
     //entry->cdr = NULL;
     (table->size)--;
     
    return 1;
  }
  return 0;
}
Esempio n. 18
0
// cleanupTable only cleans the cons cells and keys.
void cleanupTable(HashTable* table){
  if (table){
    int i;
    for (i=0;i<(table->capacity);i++){
      if (table->entries[i].car){
	if (table->entries[i].car->type==symbolType){
	  free(table->entries[i].car->symbolValue);
	}
	free(table->entries[i].car);
	freeValue(table->entries[i].cdr);
      }
    }
    table->size = 0;
    free(table->entries);
  }
}
Esempio n. 19
0
void
printMappingItem(__nis_mapping_item_t *i, __nis_mapping_item_type_t native) {
	__nis_value_t	*val = getMappingItem(i, native, 0, 0, NULL);
	int		j;
	char		*myself = "printMappingItem";

	if (val == 0)
		return;

	if (i->repeat)
		p2buf(myself, "(");
	for (j = 0; j < val->numVals; j++) {
		c2buf(myself, val->val[j].value, val->val[j].length);
	}
	if (i->repeat)
		p2buf(myself, ")");
	freeValue(val, 1);
}
Esempio n. 20
0
/* Removes the Vectors and frees the Frame. */
void cleanupFrame(Frame *cleanMe){
    if(!cleanMe) return;
    void *pointerToAValue = NULL;
    int vectorSize = cleanMe->values.size;
    for(int i = 0; i < vectorSize; i++){
        getVectorItem(&(cleanMe->values),i,&pointerToAValue);
        freeValue(*((Value *)pointerToAValue));
        free((Value *)pointerToAValue);
    }

    void *pointerToAChar = NULL;
    vectorSize = cleanMe->names.size;
    for(int i = 0; i < vectorSize; i++){
        getVectorItem(&(cleanMe->names),i,&pointerToAChar);
        free((char *)pointerToAChar);
    }

    cleanupVector(&(cleanMe->names));
    cleanupVector(&(cleanMe->values));
    free(cleanMe);
}
Esempio n. 21
0
/*
 * Convert string to __nis_value_t
 */
__nis_value_t *stringToValue(char *dptr, int dsize) {
	char		*myself = "stringToValue";
	char		*emptystr = "";
	__nis_value_t	*val;

	if ((val = am(myself, sizeof (*val))) == 0) {
		return (0);
	}

	val->type = vt_string;
	val->repeat = 0;
	val->numVals = 1;
	if ((val->val = am(myself, sizeof (val->val[0]))) == 0) {
		sfree(val);
		return (0);
	}

	/*
	 * Null strings or strings with length 0 are treated
	 * as empty strings with length 1
	 */
	if (dptr == 0 || dsize <= 0) {
		dptr = emptystr;
		dsize = 1;
	}

	val->val->length = dsize;
	if (dptr[dsize - 1] != '\0') {
		val->val->length = dsize + 1;
	}

	val->val->value = am(myself, val->val->length);
	if (val->val->value == 0) {
		freeValue(val, 1);
		return (0);
	}
	(void) memcpy(val->val->value, dptr, dsize);

	return (val);
}
Esempio n. 22
0
double evaluateValueAsBool(Value* v) {
  double i;
  Value* u;
  if (v->type == 'v') {
    u = valueFromName(((Variable*)v)->name);
    if (u == NULL) return NAN;
    return evaluateValueAsBool(u);
  }
  if (v->type == 'n') {
    return *((double*)v->data);
  }
  if (v->type == 'c') {
    u = evaluateFuncVal((FuncVal*)v);
    if (u == NULL) return NAN;
    i = evaluateValueAsBool(u);
    freeValue(u);
    return i;
  }
  if (v->type == 's') {
    return strlen(((String*)v->data)->val);
  }
  if (v->type == 'b') {
    return ((BoolExpr*)v)->lasteval;
  }
  if (v->type == 'l') {
    if (v->data)
      return lengthOfList(((List*)v->data)->next) +
	(double)((VarTree*)((List*)((List*)v->data)->data)->data)->count;
    else
      return 0.0;
  }
  if (v->type == 'd') {
    return evaluateValueAsBool(evaluateStatements((List*)v->data));
  }
  if (v->type == '0') {
    return 0;
  }
  return 1;
}
Esempio n. 23
0
void
printMappingFormatArray(__nis_mapping_format_t *a) {
	__nis_value_t	*val = getMappingFormatArray(a, 0, fa_any, 0, 0);
	char		*myself = "printMappingFormatArray";

	if (val != 0) {
		if (val->type == vt_string) {
			int	i;

			if (a[0].type != mmt_begin)
				p2buf(myself, "\"");
			for (i = 0; i < val->numVals; i++) {
				sc2buf(myself, val->val[i].value,
					val->val[i].length);
			}
		} else {
			p2buf(myself, "<illegal>");
		}
		freeValue(val, 1);
	} else {
		p2buf(myself, "<novals>");
	}
}
Esempio n. 24
0
// We have not finished this function yet.
Value* evalEach(Value* args, Environment* env){
  Value *temp, *toClean, *tail, *previous = args;
  Value *head = args, *openParen;
  int i=0;
  //printf("before eval each: ");
  //printValue(args);
  //printf("\n");
  while (args){ 
    i++;
    assert(args->type==cellType);    
    //temp = args->cons->car;
    if (getFirst(args) && (getFirst(args))->type==cellType){
      tail = getTail(args);
      
      openParen = getFirst(getFirst(args));
  
      if (openParen && openParen->type == openType){

	temp =  deepCopy(eval(getFirst(args), env));

	printf("printing temp:");
	printValue(temp);
	printf("\n");
	//assert(getFirst(getFirst(args))!=NULL);
	//assert(getFirst(getFirst(args))->type==openType);
	
	freeValue(args->cons->car);

	args->cons->car = temp;
	if (!temp){
	  return NULL;
	}
	//printf("printing tail:");
	//printValue(tail);
	//printf("\n");
	/*
	if (!temp && tail && tail->type == cellType && tail->cons->car &&  tail->cons->car->type == closeType){
	  cleanup(tail);
	  args->cons->cdr = NULL;
	  free(args->cons);
	  free(args);
	  previous->cons->cdr = NULL;
	  return NULL;
	  break;
	}else if (!temp){
	  free(args->cons);
	  free(args);
	  previous->cons->cdr = NULL;
	  return NULL;
	  break;
	  }*/
	previous = args;
	args = tail;

      }else{
	printf("error\n");
	return NULL;
      }
      
    }else{
      
      tail = getTail(args);
      
      //printf("start to print:  ");
      //printValue(eval(args->cons->car, env));
      //printf("  done\n");
      
      temp = deepCopy(eval(args->cons->car, env));
     
      if (temp==NULL){ 	
	//printf("NOPE\n");
	cleanup(tail);
	args->cons->cdr = NULL;
	freeValue(args->cons->car);
	free(args->cons);
	free(args);

	if (args!=previous){
	  previous->cons->cdr = NULL;
	}else{
	  return NULL;
	}
	break;
      }else{
	//printValue(temp);
	//printf("\n");
	freeValue(args->cons->car);
	args->cons->car = temp;
	//printValue(temp);
	//printf("\n");

      }
      previous = args;
      args = tail;
    }
    
  }
  /*
  printf("before going back ");
  assert(head!=NULL);
  assert(head->type==cellType);
  assert(head->cons!=NULL);
  assert(head->cons->car!=NULL);
  printf("this position is: ");
  printValue(head->cons->car);
  printf("\n");
 
  printValue(head);

  printf("\n");
  */
  return head;
}
Esempio n. 25
0
/*
 * Generate name=values pairs for splitfield names
 *
 * Consider Example:
 *	nisLDAPnameFields club:
 *			("%s %s %s", name, code, members)
 *	nisLDAPsplitField members:
 *			("(%s,%s,%s)", host, user, domain),
 *			("%s", group)
 * On entry,
 * - rv is an array of numVals rule-values each containing
 * name=value pairs for names occuring in nisLDAPsplitField.
 * (i.e host, user, domain, group)
 * - trv contains name=value pairs for names occuring in
 * nisLDAPnameFields. (i.e name, code but not members)
 *
 * For every name in nisLDAPnamefields that is a splitfield,
 * this function applies the data in rv to the corresponding
 * splitfield formats (accessed thru t), to generate a single
 * string value for the corresponding splitfield (members).
 * This new name=value pair is then added to trv.
 * Besides, any uninitialized namefield names are set to empty strings.
 */
suc_code
addSplitFieldValues(__nis_table_mapping_t *t, __nis_rule_value_t *rv,
			__nis_rule_value_t *trv, int numVals, char *domain) {
	__nis_table_mapping_t	*sf;
	__nis_value_t		*val;
	int			i, j, k, nitems, res, statP;
	char			*str, *tempstr;
	char			delim[2] = {0, 0};
	char			*emptystr = "";
	char			*myself = "addSplitFieldValues";

	if (trv == 0)
		return (MAP_INTERNAL_ERROR);

	if (t->e == 0)
		return (SUCCESS);

	nitems = t->e->element.match.numItems;

	/*
	 * Procedure:
	 * - Check each name in nisLDAPnamefield
	 * - if it's a splifield, construct its value and add it to trv
	 * - if not, check if it has a value
	 * - if not, add empty string
	 */
	for (i = 0, sf = 0; i < nitems; i++) {
		if (rv) {
			/*
			 * str will eventually contain the single string
			 * value for the corresponding  splitfield.
			 * No point initializing str if rv == 0 because
			 * splitfield cannot be constructed without rv.
			 * So, only initialized here.
			 */
			str = 0;

			/* Check if it's a splitfield name */
			sf = mappingFromMap(t->e->element.match.item[i].name,
				domain, &statP);

			/*
			 * Return only incase of memory allocation failure.
			 * The other error case (MAP_NO_MAPPING_EXISTS),
			 * indicates that the item name is not a splitfieldname
			 * i.e it's a namefieldname. This case is handled by
			 * the following if (sf == 0)
			 */
			if (statP == MAP_NO_MEMORY)
				return (statP);
		}

		if (sf == 0) {
			/*
			 * Not a splitfield name. Verify if it has a value
			 */
			if (findVal(t->e->element.match.item[i].name,
				trv, mit_nisplus) == 0) {
				/* if not, use empty string */
				res = addCol2RuleValue(vt_string,
					t->e->element.match.item[i].name,
					emptystr, 0, trv);
				if (res == -1) {
					return (MAP_INTERNAL_ERROR);
				}
			}
			/*
			 * If rv == 0 then sf == 0 so we will continue here
			 * i.e. does not matter that str is not yet set up.
			 */
			continue;
		}

		/* Code to construct a single value */

		/* Use the first separator character as the delimiter */
		delim[0] = sf->separatorStr[0];

		for (j = 0; j < numVals; j++) {
			/* sf->numSplits is zero-based */
			for (k = 0; k <= sf->numSplits; k++) {
				val = getMappingFormatArray(
					sf->e[k].element.match.fmt, &rv[j],
					fa_item,
					sf->e[k].element.match.numItems,
					sf->e[k].element.match.item);
				if (val == 0)
					continue;
				if (val->numVals > 0) {
					if (str) {
						tempstr = scat(myself,
							0, str, delim);
						sfree(str);
						if (tempstr)
							str = tempstr;
						else {
							freeValue(val, 1);
							return (MAP_NO_MEMORY);
						}
					}
					tempstr = scat(myself, 0, str,
						val->val->value);
					sfree(str);
					if (tempstr)
						str = tempstr;
					else {
						freeValue(val, 1);
						return (MAP_NO_MEMORY);
					}
				}
				freeValue(val, 1);
			}
		}
		if (str == 0)
			str = emptystr;

		res = addCol2RuleValue(vt_string,
				t->e->element.match.item[i].name,
				str, strlen(str), trv);

		if (str != emptystr)
			sfree(str);

		if (res == -1) {
			return (MAP_INTERNAL_ERROR);
		}
	}

	return (SUCCESS);
}
Esempio n. 26
0
/*
 * Convert the datum to an array of RuleValues
 */
__nis_rule_value_t *
datumToRuleValue(datum *key, datum *value, __nis_table_mapping_t *t,
			int *nv, char *domain, bool_t readonly, int *statP) {

	__nis_rule_value_t	*rvq, *subrvq, *newrvq;
	__nis_value_t		*val;
	__nis_value_t		**valA;
	__nis_table_mapping_t	*sf;
	int			valueLen, comLen, numVals, nr, count = 1;
	int			i, j, k, l;
	char			*ipaddr, *ipvalue;

	/*  At this point, 't' is always non NULL */

	/* Initialize rule-value */
	if ((rvq = initRuleValue(1, 0)) == 0) {
		*statP = MAP_INTERNAL_ERROR;
		return (0);
	}

	/* Add domainname to rule-value */
	if (addCol2RuleValue(vt_string, N2LDOMAIN, domain, strlen(domain),
						rvq)) {
		freeRuleValue(rvq, 1);
		*statP = MAP_INTERNAL_ERROR;
		return (0);
	}

	/* Handle key */
	if (key != 0) {
		/* Add field=value pair for N2LKEY */
		i = addCol2RuleValue(vt_string, N2LKEY, key->dptr, key->dsize,
						rvq);

		/* For readonly, add field=value pair for N2LSEARCHKEY */
		if (readonly == TRUE && i == 0) {
			i = addCol2RuleValue(vt_string, N2LSEARCHKEY, key->dptr,
						key->dsize, rvq);
		}
		if (i) {
			freeRuleValue(rvq, 1);
			*statP = MAP_INTERNAL_ERROR;
			return (0);
		}

		/* Add field=value pairs for IP addresses */
		if (checkIPaddress(key->dptr, key->dsize, &ipaddr) > 0) {
			/* If key is IPaddress, use preferred format */
			ipvalue = ipaddr;
			valueLen = strlen(ipaddr);
			i = addCol2RuleValue(vt_string, N2LIPKEY, ipvalue,
						valueLen, rvq);
		} else {
			/* If not, use original value for N2LSEARCHIPKEY */
			ipaddr = 0;
			ipvalue = key->dptr;
			valueLen = key->dsize;
			i = 0;
		}

		if (readonly == TRUE && i == 0) {
			i = addCol2RuleValue(vt_string, N2LSEARCHIPKEY, ipvalue,
								valueLen, rvq);
		}
		sfree(ipaddr);
		if (i) {
			freeRuleValue(rvq, 1);
			*statP = MAP_INTERNAL_ERROR;
			return (0);
		}
	}

	/* Handle datum value */
	if (value != 0 && t->e) {
		valueLen = value->dsize;
		/*
		 * Extract the comment, if any, and add it to
		 * the rule-value.
		 */
		if (t->commentChar != '\0') {
			/*
			 * We loop on value->dsize because value->dptr
			 * may not be NULL-terminated.
			 */
			for (i = 0; i < value->dsize; i++) {
				if (value->dptr[i] == t->commentChar) {
					valueLen = i;
					comLen = value->dsize - i - 1;
					if (comLen == 0)
						break;
					if (addCol2RuleValue(vt_string,
						N2LCOMMENT, value->dptr + i + 1,
						comLen, rvq)) {
						freeRuleValue(rvq, 1);
						*statP = MAP_INTERNAL_ERROR;
						return (0);
					}
					break;
				}
			}
		}

		/* Skip trailing whitespaces */
		for (; valueLen > 0 && (value->dptr[valueLen - 1] == ' ' ||
			value->dptr[valueLen - 1] == '\t'); valueLen--);

		/*
		 * At this point valueLen is the effective length of
		 * the data. Convert value into __nis_value_t so that
		 * we can use the matchMappingItem function to break it
		 * into fields.
		 */
		if ((val = stringToValue(value->dptr, valueLen)) == 0) {
			freeRuleValue(rvq, 1);
			*statP = MAP_NO_MEMORY;
			return (0);
		}

		/* Perform namefield match */
		valA = matchMappingItem(t->e->element.match.fmt, val,
							&numVals, 0, 0);
		if (valA == 0) {
			freeValue(val, 1);
			freeRuleValue(rvq, 1);
			*statP = MAP_NAMEFIELD_MATCH_ERROR;
			return (0);
		}

		/* We don't need val anymore, so free it */
		freeValue(val, 1);

		/*
		 * Since matchMappingItem only returns us an array of
		 * __nis_value_t's, we need to associate each value
		 * in the array with the corresponding item name.
		 * This code assumes that numVals will be less than or
		 * equal to the number of item names associated with
		 * the format.
		 * These name=value pairs are added to rvq.
		 */
		for (i = 0, *statP = SUCCESS; i < numVals; i++) {
			for (j = 0; j < count; j++) {
				if (addCol2RuleValue(vt_string,
					t->e->element.match.item[i].name,
					valA[i]->val->value,
					valA[i]->val->length, &rvq[j])) {
					*statP = MAP_INTERNAL_ERROR;
					break;
				}
			}
			if (*statP == MAP_INTERNAL_ERROR)
				break;

			/*
			 * Check if splitField exists for the field.
			 * Since splitfields are also stored as mapping
			 * structures, we need to get the hash table entry
			 * corresponding to the splitfield name
			 */
			sf = mappingFromMap(t->e->element.match.item[i].name,
					domain, statP);
			if (*statP == MAP_NO_MEMORY)
				break;
			*statP = SUCCESS;
			if (sf == 0)
				continue;

			/*
			 * Process and add splitFields to rule-value rvq
			 */
			subrvq = processSplitField(sf, valA[i], &nr, statP);

			if (subrvq == 0) {
				/* statP would have been set */
				break;
			}

			/*
			 * We merge 'count' rule-values in rvq with 'nr'
			 * rule-values from subrvq to give us a whopping
			 * 'count * nr' rule-values
			 */

			/* Initialize the new rule-value array */
			if ((newrvq = initRuleValue(count * nr, 0)) == 0) {
				*statP = MAP_INTERNAL_ERROR;
				freeRuleValue(subrvq, nr);
				break;
			}

			for (j = 0, l = 0; j < nr; j++) {
				for (k = 0; k < count; k++, l++) {
					if ((mergeRuleValue(&newrvq[l],
							&rvq[k]) == -1) ||
							(mergeRuleValue(
							&newrvq[l],
							&subrvq[j]) == -1)) {
						*statP = MAP_INTERNAL_ERROR;
						for (i = 0; i < numVals; i++)
							freeValue(valA[i], 1);
						sfree(valA);
						freeRuleValue(rvq, count);
						freeRuleValue(newrvq,
								count * nr);
						freeRuleValue(subrvq, nr);
						return (0);
					}
				}
			}

			freeRuleValue(rvq, count);
			rvq = newrvq;
			count = l;
			freeRuleValue(subrvq, nr);

		}

		/* We don't need valA anymore, so free it */
		for (i = 0; i < numVals; i++)
			freeValue(valA[i], 1);
		sfree(valA);

		if (*statP != SUCCESS) {
			freeRuleValue(rvq, count);
			return (0);
		}

	} /* if value */

	if (nv != 0)
		*nv = count;
	return (rvq);

}
Esempio n. 27
0
File: db.c Progetto: yuyang0/mdb
void dictValueDestructor(void *privdata, void *val) {
	DICT_NOTUSED(privdata);
	freeValue(val);
}
Esempio n. 28
0
/*
 * Updates 'rv' with NIS name=value pairs suitable to
 * construct datum from namefield information.
 * Some part based on createNisPlusEntry (from ldap_nisdbquery.c)
 * This code assumes that from a given LDAP entry, applying the
 * mapping rules, would give us one or more NIS entries, differing
 * only in key.
 */
suc_code
buildNISRuleValue(__nis_table_mapping_t *t, __nis_rule_value_t *rv,
					char *domain) {
	int			r, i, j, k, l, nrq, res, len;
	int			numItems, splitname, count, statP;
	__nis_value_t		*rval;
	__nis_mapping_item_t	*litem;
	__nis_mapping_rule_t	*rl;
	__nis_rule_value_t	*rvq;
	char			*value, *emptystr = "";

	statP = SUCCESS;

	/* Initialize default base */
	__nisdb_get_tsd()->searchBase = t->objectDN->read.base;

	/* Initialize rule-value rvq */
	rvq = 0;
	count = 0;

	/* Add domainname to rule-value */
	if (addCol2RuleValue(vt_string, N2LDOMAIN, domain, strlen(domain),
						rv)) {
		return (MAP_INTERNAL_ERROR);
	}

	for (r = 0; r < t->numRulesFromLDAP; r++) {
		rl = t->ruleFromLDAP[r];

		/* Set escapeFlag if RHS is "dn" to remove escape chars */
		if (rl->rhs.numElements == 1 &&
			rl->rhs.element->type == me_item &&
			rl->rhs.element->element.item.type == mit_ldap &&
			strcasecmp(rl->rhs.element->element.item.name, "dn")
									== 0) {
				__nisdb_get_tsd()->escapeFlag = '2';
		}

		rval = buildRvalue(&rl->rhs, mit_ldap, rv, NULL);

		/* Reset escapeFlag */
		__nisdb_get_tsd()->escapeFlag = '\0';

		if (rval == 0) {
			continue;
		}

		if (rval->numVals <= 0) {
			/* Treat as invalid */
			freeValue(rval, 1);
			continue;
		}

		litem = buildLvalue(&rl->lhs, &rval, &numItems);
		if (litem == 0) {
			/* This will take care of numItems == 0 */
			freeValue(rval, 1);
			continue;
		}

		if (rval->numVals > 1) {
			if (numItems == 1 && litem->repeat)
				nrq = rval->numVals;
			else if (numItems > 1 && rval->repeat)
				nrq = 1 + ((rval->numVals-1)/numItems);
			else
				nrq = 1;
		} else
			nrq = 1;

		/* Set splitname if splitfield names are specified */
		for (i = 0; i < numItems; i++) {
			if (strcasecmp(litem[i].name, N2LKEY) == 0 ||
				strcasecmp(litem[i].name, N2LIPKEY) == 0 ||
				strcasecmp(litem[i].name, N2LCOMMENT) == 0)
				continue;
			for (j = 0; j < t->numColumns; j++) {
				if (strcmp(litem[i].name, t->column[j]) == 0)
					break;
			}
			if (j == t->numColumns)
				break;
		}

		splitname = (i < numItems)?1:0;

		for (j = 0; j < nrq; j++) {
			if (splitname == 1) {
				/*
				 * Put every value of splitfieldname in a new
				 * rule-value. Helps generating splitfields.
				 */
				rvq = growRuleValue(count, count + 1, rvq, 0);
				if (rvq == 0) {
					freeRuleValue(rvq, count);
					freeValue(rval, 1);
					freeMappingItem(litem, numItems);
					return (MAP_INTERNAL_ERROR);
				}
				count++;
			}

			for (k = j % nrq, l = 0; l < numItems; k += nrq, l++) {
				/* If we run out of values, use empty strings */
				if (k >= rval->numVals) {
					value = emptystr;
					len = 0;
				} else {
					value = rval->val[k].value;
					len = rval->val[k].length;
				}
				res = (splitname == 1)?addCol2RuleValue(
					vt_string, litem[l].name, value,
					len, &rvq[count - 1]):0;
				if (res != -1)
					res = addCol2RuleValue(vt_string,
						litem[l].name, value, len, rv);
				if (res == -1) {
					freeRuleValue(rvq, count);
					freeValue(rval, 1);
					freeMappingItem(litem, numItems);
					return (MAP_INTERNAL_ERROR);
				}
			}
		}
		freeValue(rval, 1);
		rval = 0;
		freeMappingItem(litem, numItems);
		litem = 0;
		numItems = 0;
	} /* for r < t->numRulesFromLDAP */

	statP = addSplitFieldValues(t, rvq, rv, count, domain);

	if (rvq)
		freeRuleValue(rvq, count);

	if (verifyIndexMatch(t, 0, rv, 0, 0) == 0)
		return (MAP_INDEXLIST_ERROR);
	return (statP);

} /* end of buildNISRuleValue */
Esempio n. 29
0
File: repl.c Progetto: uiri/fffll
char* valueToString(Value* v) {
  char* s, *t;
  int i, j, l, freet;
  double a, b, c;
  BoolExpr* be;
  Value* u;
  List* node;
  freet = 0;
  l = 0;
  t = NULL;
  if (v->type == 'v') {
    u = evaluateValue(v);
    if (u == NULL) return NULL;
    s = valueToString(u);
    if (s == NULL) return NULL;
    return s;
  }
  if (v->type == 'i') {
    u = evaluateValue(v);
    if (u == NULL) return NULL;
    s = valueToString(u);
    return s;
  }
  if (v->type == 'n') {
    l = 32;
    i = l;
    s = malloc(l);
    l = snprintf(s, l, "%g", *(double*)v->data);
    if (i<l) {
      s = realloc(s, l);
      snprintf(s, l, "%g", *(double*)v->data);
    }
    return s;
  }
  if (v->type == '0' || v->type == 'b') {
    if (v->type == 'b') {
      be = evaluateBoolExpr((BoolExpr*)v);
      if (be == NULL) return NULL;
      l = be->lasteval;
    }
    if (l) {
      s = malloc(5);
      s[0] = 't';
      s[1] = 'r';
      s[2] = 'u';
      s[3] = 'e';
      s[4] = '\0';
    } else {
      s = malloc(6);
      s[0] = 'f';
      s[1] = 'a';
      s[2] = 'l';
      s[3] = 's';
      s[4] = 'e';
      s[5] = '\0';
    }
    return s;
  }
  if (v->type == 'r') {
    a = valueToDouble(((Range*)v)->start);
    c = valueToDouble(((Range*)v)->end);
    if (((Range*)v)->increment == falsevalue) {
      b = 1.0;
      if (c<a)
	b = -1.0;
    } else {
      b = valueToDouble(((Range*)v)->increment);
    }
    s = malloc(1);
    if ((c-a)/b < 0) {
      s[0] = '\0';
      return s;
    }
    l = 1;
    if (a<c) {
      for (;a<c;a += b) {
	l += snprintf(s, 0, "%d, ", (int)a);
      }
    } else if (c<a) {
      for (;c<a;a += b) {
	l += snprintf(s, 0, "%d, ", (int)a);
      }
    }
    free(s);
    s = malloc(l);
    a = valueToDouble(((Range*)v)->start);
    i = 0;
    if (a<c) {
      for (;a<c;a += b) {
	  i += snprintf(s+i, l, "%d, ", (int)a);
      }
    } else if (c<a) {
      for (;c<a;a += b) {
	i += snprintf(s+i, l-i, "%d, ", (int)a);
      }
    }
    i--;
    s[--i] = '\0';
    return s;
  }
  if (v->type == 'l') {
    s = malloc(4);
    s[0] = '[';
    i = 0;
    node = NULL;
    if (v->data)
      node = ((List*)v->data)->next;
    for (;node != NULL;node = node->next) {
      if (i) s[i] = ' ';
      i++;
      u = evaluateValue(node->data);
      if (u == NULL) {
	free(s);
	return NULL;
      }
      t = valueToString(u);
      freeValue(u);
      if (t == NULL) {
	free(s);
	return NULL;
      }
      l = strlen(t);
      s = realloc(s, i+l+2);
      for (j=0;j<l;j++) {
	s[i+j] = t[j];
      }
      s[i+++j] = ',';
      s[i+j] = '\0';
      i += l;
      free(t);
    }
    if (!i) i = 2;
    s[--i] = ']';
    s[++i] = '\0';
    return s;
  }
  if (v->type == 'c') {
    u = evaluateFuncVal((FuncVal*)v);
    if (u == NULL) return NULL;
    s = valueToString(u);
    freeValue(u);
    return s;
  }
  if (v->type == 'd') {
    u = evaluateStatements(v->data);
    if (u == NULL) return NULL;
    s = valueToString(u);
    freeValue(u);
    return s;
  }
  if (v->type == 's' || v->type == 'x') {
    t = ((String*)v->data)->val;
  }
  if (t == NULL) return NULL;
  l = strlen(t);
  s = malloc(l+1);
  for (i=0;i<l;i++) {
    s[i] = t[i];
  }
  s[i] = '\0';
  if (freet) free(t);
  return s;
}
Esempio n. 30
0
Value *multiply(Value *args){
  int intProd = 1;
  double dblProd = 1;
  int isFloat = 0;
  Value  *head;
  head = args;
  if (head){
    while (args && args->type == cellType){
      if (getFirst(args)== NULL){
	if (getTail(args)==NULL){
	  break;
	}else{
	  printf("Invalid input for <#procedure:*>\n");
	  return NULL;
	}
      }else if (getFirst(args)->type == integerType){
	if (!isFloat){
	  intProd *= getFirst(args)->intValue;
	}else{
	  dblProd *= getFirst(args)->dblValue;
	}
      }else if (getFirst(args)->type == floatType){
	if (!isFloat){
	  isFloat = 1;
	  dblProd *=intProd;
	}
	dblProd *= getFirst(args)->dblValue;	
      }else if (getFirst(args)->type == closeType){
	break;
      }else{
	printf("*: expects type <number> as arguments");
	return NULL;
      }
      args = getTail(args);
    }
    
    printf("this is the head before adding: ");
    printValue(head);
    printf("\n");
    cleanup(getTail(head));
  }
  Value *value = (Value*) malloc(sizeof(Value));
  //result = getFirst(head);
  if (isFloat){
    value->type = floatType;
    value->dblValue = dblProd; 
  }else{
    
    value->type = integerType;
    value->intValue = intProd; 
  }
  if (head){
    freeValue(head->cons->car);
    
    head->cons->car = value;
    head->cons->cdr = NULL;
    printf("this is the head after multiplying: ");
    printValue(head);
    printf("\n");
  }
  return value;
}