Example #1
0
/**
 * Demonstration of the stack implementation
 */
int main() {
    Stack_Item *item1 = stack_item_create(1);
    Stack_Item *item2 = stack_item_create(3);
    printf("[DEBUG] Stack Item 2 at %p\n", item2);
     
    Stack *stack = stack_create(5);
    stack_dump(stack);

    stack_push(stack, item1);
    stack_push(stack, item2);

    printf("Stack after pushing\n");
    stack_dump(stack);
    
    Stack_Item *popped = stack_pop(stack);
    printf("[DEBUG] Popped item at %p (expected %p)\n", popped, item2);

    int popped_value = stack_item_get_val(popped);

    printf("[DEBUG] Expected to pop 3, popped %d\n", popped_value);

    printf("[DEBUG] Stack after popping\n");
    stack_dump(stack); 
    return 1;
}
Example #2
0
int stack_is_valide(stack* stk){
	
	if (stk == NULL) {
		errno = EINVAL;
		stack_errno = ERR_MEM_STK;
		stack_dump(stk, stack_errno);
		return 0;
	}

	if (!(stk -> size - 1 >= stk -> head && stk -> head >= -1)){
		errno = EINVAL;
		stack_errno = INV_HEAD;
		stack_dump(stk, stack_errno);
		return 0;
	}
	
	if ((stk -> data) == NULL) {
		
		errno = EINVAL;
		stack_errno = ERR_MEM_DATA;
		stack_dump(stk, stack_errno);
		abort();
	}
	
	return 1;
}
Example #3
0
/**
* \brief Pops some value from the stack
*
* Pops value that must be popped from stack
* @param stk_p Pointer to stack
* @return rtr_val Value popped from stack
*/
double stack_pop(struct Stack* stk_p)
{
    if (!$(stk_p))
    {
        stack_dump(stk_p);
        printf("INVALID STACK IN THE BEGINNING OF STACK_POP\n");
        assert(NULL);
    }
    stk_p->counter--;
    if (!$(stk_p))
    {
        stack_dump(stk_p);
        printf("INVALID STACK IN THE MIDDLE OF STACK_POP\n");
        assert(NULL);
    }

    double rtr_val = stk_p->data[stk_p->counter];
    if (!$(stk_p))
    {
        stack_dump(stk_p);
        printf("INVALID STACK IN THE END OF STACK_POP\n");
        assert(NULL);
    }
    return rtr_val;
}
Example #4
0
File: idt.c Project: gdarcy/scaraOS
void panic_exc(volatile struct intr_ctx ctx)
{
	cli();
	ctx_dump((struct intr_ctx *)&ctx);
	stack_dump((struct intr_ctx *)&ctx);
	idle_task_func();
}
Example #5
0
int stack_ok(stack* user_stack)
{
	if (!(user_stack)) return ERR_NULL;
	if ((user_stack -> maxsize < user_stack -> size) || (user_stack -> size < 0))
		stack_dump(stdout, user_stack);

	return ((user_stack -> maxsize < user_stack -> size) || (user_stack -> size < 0) ? ERR_WRONG_SZ : OK);
}
Example #6
0
/**
* \brief Push some value to stack
*
* Push value add_data to stack
* @param stk_p Pointer to stack
* @param add_data Pushed value
* @return 0 if function ends
* @return nothing if function will be aborted
*/
int stack_push(struct Stack* stk_p, double add_data)
{
    if (!$(stk_p))
    {
        stack_dump(stk_p);
        printf("INVALID STACK IN THE BEGINNING OF STACK_PUSH\n");
        assert(NULL);
    }
    stk_p->data[stk_p->counter] = add_data;
    stk_p->counter++;
    if (!$(stk_p))
    {
        stack_dump(stk_p);
        printf("INVALID STACK IN THE END OF STACK_PUSH\n");
        assert(NULL);
    }
    return 0;
}
Example #7
0
File: proc.c Project: olegrok/iLab
int proc_dump(mystack stk)                  //Информация о процессоре
{
    printf("Registers: \n");
    printf("\tAx %d\n", Ax);
    printf("\tBx %d\n", Bx);
    printf("\tCx %d\n", Cx);
    printf("\tDx %d\n", Dx);
    stack_dump(stk);
}
Example #8
0
void CPU_dump (cpu_t* cpu) {

    FILE* dump = fopen("dump.txt", "a");

    if (cpu == NULL) {

        fprintf(dump, "CPU_DUMP IS IMPOSSIBLE. CPU_POINTER = NULL.\n");
        return;

    }

    if (dump == NULL) {

        fprintf(logs, "CAN'T WRITE TO DUMP FILE\n");

        return;
    }

    stack_dump(&cpu->Stack, dump);

    stack_dump(&cpu->Address_stack, dump);

    fprintf(dump, "CPU DUMP:\nPROGRAM COUNTER: %d\nMAX COUNTER: %d\nREGISTERS:", \
           cpu->prgm_counter, cpu->max_counter);

    for (int i = 0; i < MAX_NUM_OF_REGISTERS; i++)
        fprintf(dump, " |%d (%lg)| ", i, cpu->reg[i]);

    fprintf(dump, "\nPM:");

    if (cpu->PM == NULL) {

        fprintf(dump, "UNKNOWN\n");
        return;

    }

    for (int i = 0; i < cpu->max_counter; i++)
        fprintf(dump, " %lg", cpu->PM[i]);

    fprintf(dump, "\n*************************************************************\n\n");

    fclose(dump);
}
Example #9
0
int push (struct stek* stek_push, char x)
{

    if (stek_push->counter >= stek_push->max_size - sizeof(double))
    {
        char* P = (char*) realloc((void*) stek_push->data, (stek_push->max_size+=100)*sizeof(*stek_push->data));
        if (P!=NULL)
            stek_push->data = P;
        else
        {
            printf("Memory is over, you can read the data.\n");
            abort();
            return 1;
        }

    }

    if (!stack_ok(stek_push))
    {
        stack_dump(stek_push);
        printf("INVALID STACK\n");
        abort();
    }

    if (!stack_ok(stek_push))
    {
        stack_dump(stek_push);
        printf("INVALID STACK\n");
        abort();
    }

    stek_push->data[stek_push->counter++] = x;

    if (!stack_ok(stek_push))
    {
        stack_dump(stek_push);
        printf("INVALID STACK\n");
        abort();
    }
    return 0;
}
Example #10
0
File: idt.c Project: gdarcy/scaraOS
void ctx_dump(struct intr_ctx *ctx)
{
	struct task *tsk = __this_task;
	printk("Task pid=%lu name=%s: CS=0x%lx tsk=%p\n",
			tsk->pid, tsk->name, ctx->cs, tsk);
	printk("  EIP=0x%.8lx EFLAGS=0x%.8lx\n", ctx->eip, ctx->eflags);
	printk("  EAX=0x%.8lx    EBX=0x%.8lx\n", ctx->eax, ctx->ebx);
	printk("  ECX=0x%.8lx    EDX=0x%.8lx\n", ctx->ecx, ctx->edx);
	printk("  ESP=0x%.8lx    EBP=0x%.8lx\n", ctx->esp, ctx->ebp);
	printk("  ESI=0x%.8lx    EDI=0x%.8lx\n", ctx->edi, ctx->esi);
	if ( ctx->esp >= PAGE_OFFSET && 0 == (ctx->cs & __CPL3) )
		stack_dump(ctx);
}
Example #11
0
byte pop(stack* stk){
 
	if (stack_is_valide(stk)) {
	
		if (stack_is_empty(stk)) { 
		
			stack_errno = EMPTY;
			stack_dump(stk, stack_errno);
			errno = EACCES;
			return -2;
		
		}
	
		(stk -> head)--;
	if (stack_is_valide(stk)) return ((stk -> data)[stk -> head + 1]);
	}
	return -1;
}
Example #12
0
int stack(lua_State* L)
{
    lua_pushboolean(L, 1);
    lua_pushnumber(L, 10);
    lua_pushnil(L);
    lua_pushstring(L, "hello");

    stack_dump(L);
    lua_pushvalue(L, -3); stack_dump(L); //get element and push top
    lua_replace(L, 3);    stack_dump(L); //pop top and replace at
    lua_settop(L, 6);     stack_dump(L); //expand the stack
    lua_remove(L, -2);    stack_dump(L); //erase element
    lua_insert(L, -3);    stack_dump(L); //pop top and insert at
    lua_settop(L, 1);     stack_dump(L); //shrink the stack

    return 0;
}
//dump exception log into Flash
void ICACHE_FLASH_ATTR
system_restart_hook(struct rst_info *info)
{
    register uint32 sp asm("a1");
    uint32 offset = 0;
    #if 0
    if (info->reason == REASON_SOFT_WDT_RST) {
        offset = 0x1c0;
    }
    else if (info->reason == REASON_EXCEPTION_RST) {
        offset = 0x1b0;
    }
	#endif
	debug_DumpBufReset();	
    uint8 InfoBuf[200];
    os_memset(InfoBuf,0,200);
    uint8* ptmp = InfoBuf;
    os_sprintf(ptmp,"reset reason:%x\n", info->reason);
    ptmp+=os_strlen(ptmp);
    if (info->reason == REASON_WDT_RST ||
        info->reason == REASON_EXCEPTION_RST ||
        info->reason == REASON_SOFT_WDT_RST) {
        if (info->reason == REASON_EXCEPTION_RST) {
            os_sprintf(ptmp,"Fatal exception (%d):\n", info->exccause);
            ptmp += os_strlen(ptmp);
        }
        ptmp+=os_strlen(ptmp);
        os_sprintf(ptmp,"epc1=0x%08x, epc2=0x%08x, epc3=0x%08x, excvaddr=0x%08x, depc=0x%08x\r\n",
                info->epc1, info->epc2, info->epc3, info->excvaddr, info->depc);
        ptmp+=os_strlen(ptmp);
    }

    int len = os_strlen(InfoBuf);
    uint8 pad_len = 0;
    if(len%4 != 0){
        pad_len = 4 - (len%4);
        os_memcpy(ptmp,"   ",pad_len);
    }
    len += pad_len;
    ESP_DBG("============\r\nSV DBUG INFO:\r\n%s\r\n==============\r\n",InfoBuf);
    debug_DumpToFlash(InfoBuf,len);
    stack_dump(info, sp + offset);
Example #14
0
double stack_pop(struct stek* stek_pop)
{
    if (!stack_ok(stek_pop))
    {
        stack_dump(stek_pop);
        printf("INVALID STACK");
        abort();
    }

    if (stek_pop->counter == 0)
    {
        printf("Stack is empty, first enter the data\n");
        return 1;
    }

    if ((stek_pop->counter+200 < stek_pop->max_size) && (stek_pop->counter >0))
        stek_pop->data = (double*) realloc(stek_pop->data, (stek_pop->max_size-=10)*sizeof(*stek_pop->data));
    assert(stek_pop->data);
    return stek_pop->data[--stek_pop->counter];
}
Example #15
0
int push(stack* stk, byte a){

	if (stack_is_valide(stk)) {
	
		if (stack_is_full(stk)){
		
			errno = EACCES;
			stack_errno = FULL;
			stack_dump(stk, stack_errno);
			
			return -2;
		
		}
	
		(stk -> data)[stk -> head + 1] = a;
		(stk -> head)++;
		
	if (stack_is_valide(stk)) return 0;
	}
	return -1;
}
Example #16
0
// coverity[+kill]
void panic(const char *fmt, ...)
{
    if(panic_reenter)
        _exit(33);

    board_panic_stop_world();

    hal_cli();
    panic_reenter++;

    // CI: this word is being watched by CI scripts. Do not change -- or change CI appropriately
    printf("Panic: ");
    va_list ap;
    va_start(ap, fmt);
    vprintf(fmt, ap);

    if (!bootflag_unattended)
    {
	// CI: this word is being watched by CI scripts. Do not change -- or change CI appropriately
	printf("\nPress any key ...\n");
	board_panic_wait_keypress();
    }

    printf("\r             \r");

    stack_dump();

    dump_thread_stacks();

    if (!bootflag_unattended)
    {
	// CI: this word is being watched by CI scripts. Do not change -- or change CI appropriately
	printf("\nPress any key to reboot ...\n");
	board_panic_wait_keypress();
    }

    exit(33);
}
Example #17
0
int execute(const cmd_t* cmd)
{
	int ret = 0;
	tsip_ssession_id_t sid;
	tsk_istr_t istr;

	if(!cmd){
		TSK_DEBUG_ERROR("Invalid parameter");
		return -1;
	}

	tsk_safeobj_lock(ctx);

	switch(cmd->type){
		case cmd_audio:
		case cmd_audiovideo:
			{
				TSK_DEBUG_INFO("command=audio/video");
				if((sid = invite_handle_cmd(cmd->type, cmd->opts)) != TSIP_SSESSION_INVALID_ID){
					if(cmd->sidparam){
						tsk_itoa(sid, &istr);
						update_param(cmd->sidparam, istr);
					}
				}
				break;
			}
		case cmd_config_session:
			{
				TSK_DEBUG_INFO("command=config-session");
				break;
			}
		case cmd_config_stack:
			{
				TSK_DEBUG_INFO("command=config-satck");
				ret = stack_config(cmd->opts);
				break;
			}
		case cmd_dtmf:
			{
				const opt_t* opt;
				TSK_DEBUG_INFO("command=dtmf");
				if(!(opt = opt_get_by_type(cmd->opts, opt_sid)) || tsk_strnullORempty(opt->value)){ /* --sid option */
					TSK_DEBUG_ERROR("++dtmf command need --sid option");
					break;
				}
				if(!(opt = opt_get_by_type(cmd->opts, opt_event)) || tsk_strnullORempty(opt->value)){ /* --event option */
					TSK_DEBUG_ERROR("++dtmf command need --event option");
					break;
				}
				invite_handle_cmd(cmd->type, cmd->opts);
				break;
			}
		case cmd_dump:
			{
				TSK_DEBUG_INFO("command=dump");
				ret = stack_dump();
				break;
			}
		case cmd_ect:
		{
			const opt_t* opt;
			TSK_DEBUG_INFO("command=ect");
			if((opt = opt_get_by_type(cmd->opts, opt_sid)) && !tsk_strnullORempty(opt->value)){
				TSK_DEBUG_ERROR("++ect command need --sid option");
				ret = -1;
				break;
			}
			if((opt = opt_get_by_type(cmd->opts, opt_to)) && !tsk_strnullORempty(opt->value)){
				TSK_DEBUG_ERROR("++ect command need --to option");
				ret = -1;
				break;
			}
			invite_handle_cmd(cmd->type, cmd->opts);
			break;
		}
		case cmd_exit:
			{
				TSK_DEBUG_INFO("command=exit");
				goto bail;
				break;
			}
		case cmd_file:
			{
				const opt_t* opt;
				TSK_DEBUG_INFO("command=file");
				if(!(opt = opt_get_by_type(cmd->opts, opt_path)) || tsk_strnullORempty(opt->value)){
					TSK_DEBUG_ERROR("++file command need --path option");
					break;
				}
				if((sid = invite_handle_cmd(cmd->type, cmd->opts)) != TSIP_SSESSION_INVALID_ID){
					if(cmd->sidparam){
						tsk_itoa(sid, &istr);
						update_param(cmd->sidparam, istr);
					}
				}
				break;
			}
		case cmd_hangup:
			{
				const opt_t* opt;
				TSK_DEBUG_INFO("command=hangup");
				if((opt = opt_get_by_type(cmd->opts, opt_sid)) && !tsk_strnullORempty(opt->value)){ /* --sid option */
					ret = session_hangup(tsk_atoll(opt->value));
				}
				else{
					TSK_DEBUG_ERROR("++hangup command need --sid option");
					ret = -1;
				}
				break;
			}
		case cmd_help:
			{
				TSK_DEBUG_INFO("command=help");
				cmd_print_help();
				break;
			}
		case cmd_hold:
			{
				const opt_t* opt;
				TSK_DEBUG_INFO("command=hold");
				if((opt = opt_get_by_type(cmd->opts, opt_sid)) && !tsk_strnullORempty(opt->value)){ /* --sid option */
					invite_handle_cmd(cmd->type, cmd->opts);
				}
				else{
					TSK_DEBUG_ERROR("++hold command need --sid option");
					ret = -1;
				}
				break;
			}
		case cmd_message:
			{
				TSK_DEBUG_INFO("command=message");
				if((sid = message_handle_cmd(cmd->type, cmd->opts)) != TSIP_SSESSION_INVALID_ID){
					if(cmd->sidparam){
						tsk_itoa(sid, &istr);
						update_param(cmd->sidparam, istr);
					}
				}
				break;
			}

		case cmd_options:
			{
				TSK_DEBUG_INFO("command=options");
				if((sid = options_handle_cmd(cmd->type, cmd->opts)) != TSIP_SSESSION_INVALID_ID){
					if(cmd->sidparam){
						tsk_itoa(sid, &istr);
						update_param(cmd->sidparam, istr);
					}
				}
				break;
			}
		case cmd_publish:
			{
				TSK_DEBUG_INFO("command=publish");
				if((sid = publish_handle_cmd(cmd->type, cmd->opts)) != TSIP_SSESSION_INVALID_ID){
					if(cmd->sidparam){
						tsk_itoa(sid, &istr);
						update_param(cmd->sidparam, istr);
					}
				}
				break;
			}
		case cmd_register:
			{
				TSK_DEBUG_INFO("command=register");
				if((sid = register_handle_cmd(cmd->type, cmd->opts)) != TSIP_SSESSION_INVALID_ID){
					if(cmd->sidparam){
						tsk_itoa(sid, &istr);
						update_param(cmd->sidparam, istr);
					}
				}
				break;
			}
		case cmd_resume:
			{
				const opt_t* opt;
				TSK_DEBUG_INFO("command=resume");
				if((opt = opt_get_by_type(cmd->opts, opt_sid)) && !tsk_strnullORempty(opt->value)){ /* --sid option */
					invite_handle_cmd(cmd->type, cmd->opts);
				}
				else{
					TSK_DEBUG_ERROR("++resume command need --sid option");
					ret = -1;
				}
				break;
			}
		case cmd_run:
			{
				TSK_DEBUG_INFO("command=run");
				ret = stack_run(cmd->opts);
				break;
			}
		case cmd_scenario:
			{
				TSK_DEBUG_INFO("command=scenario");
				break;
			}
		case cmd_sleep:
			{
				const opt_t* opt;
				double seconds;

				tsk_safeobj_unlock(ctx); /* beacuse of callback function */

				if((opt = opt_get_by_type(cmd->opts, opt_sec)) && !tsk_strnullORempty(opt->value)){ /* --sec option */
					seconds = strtod(opt->value, tsk_null); /* strtod() is better than atof() */
					if(seconds<=0){
						printf("\n==== Press ENTER to continue...\n");
						getchar();
					}
					else{
						TSK_DEBUG_INFO("Sleeping %f seconds", seconds);
						tsk_thread_sleep((uint64_t)(seconds * 1000));
					}
				}
				else{
					TSK_DEBUG_WARN("++sleep need --sec option.");
				}
				return 0; /* bail: will unlock again */
			}
		case cmd_sms:
			{
				TSK_DEBUG_INFO("command=sms");
				if((sid = message_handle_cmd(cmd->type, cmd->opts)) != TSIP_SSESSION_INVALID_ID){
					if(cmd->sidparam){
						tsk_itoa(sid, &istr);
						update_param(cmd->sidparam, istr);
					}
				}
				break;
			}
		case cmd_stop:
			{
				TSK_DEBUG_INFO("command=stop");
				tsip_stack_stop(ctx->stack);
				break;
			}
		case cmd_subscribe:
			{
				TSK_DEBUG_INFO("command=subscribe");
				if((sid = subscribe_handle_cmd(cmd->type, cmd->opts)) != TSIP_SSESSION_INVALID_ID){
					if(cmd->sidparam){
						tsk_itoa(sid, &istr);
						update_param(cmd->sidparam, istr);
					}
				}
				break;
			}
		case cmd_video:
			{
				TSK_DEBUG_INFO("command=video");
				break;
			}
		default:
			{
				TSK_DEBUG_ERROR("%d not a valid command.", cmd);
				break;
			}
	}

bail:
	tsk_safeobj_unlock(ctx);

	return ret;
}
Example #18
0
static ssize_t scan_data(char *buf, int buf_size, const char *fmt, ...)
{
	char *p = buf, *endp = &buf[buf_size], *s;
	int len;
	int32_t *i32;
	uint64_t *u64;
	va_list ap;

	va_start(ap, fmt);
	while (*fmt && *fmt != '%') fmt++;
	if (*fmt == '%') fmt++;
	while (*fmt) {
		switch (*fmt++) {
		case 's':              /* string */
			s = va_arg(ap, char *);
			if (p + 4 > endp) {
				dbg("buffer to short for string length");
				stack_dump();
				return -1;
			}
			len = be32toh(*(int32_t *)p);
			p += 4;
			if (p + len > endp) {
				dbg("buffer to short for string");
				stack_dump();
				return -1;
			}

			memcpy(s, p, len);
			s[len] = '\0';
			p += len;
			break;
		case 'i':              /* 32-bit int */
			if (p + 4 > endp) {
				dbg("buffer to short for int32_t");
				stack_dump();
				return -1;
			}
			i32 = va_arg(ap, int32_t *);

			*i32 = be32toh(*(int32_t *)p);
			p += 4;
			break;
		case 'l':              /* 64-bit unsigned int */
			if (*fmt++ != 'u') {
				dbg("invalid long format character: '%c'", *fmt);
				stack_dump();
				break;
			}
			if (p + 8 > endp) {
				dbg("buffer to short for uint64_t");
				stack_dump();
				return -1;
			}
			u64 = va_arg(ap, uint64_t *);

			*u64 = be32toh(*(uint64_t *)p);
			p += 8;
			break;
		default:
			dbg("invalid format character: '%c'", *fmt);
			stack_dump();
		}
		while (*fmt && *fmt != '%') fmt++;
		if (*fmt == '%') fmt++;
	}
	va_end(ap);

	return p - buf;
}
Example #19
0
double _parse(gchar *args, struct global_vars *gvars)
{
	gchar mul_char = '*';
	gdouble minus_one = -1.0;
	gchar null = 0;
	gint args_len = strlen(args);
	
	struct stack *arguments = stack_init(sizeof(gdouble));
	struct stack *operators = stack_init(sizeof(gchar));

	gint i = 0;
	gint j = 0;
	gint local_nest_level = 0;

	gint8 last_p = 0;  /** priority of last parsed operator */
	gboolean coef_flag = FALSE;  /** set if value might preceed a bracket and thus become a coefficient */
	gboolean func_flag = FALSE;  /** set if result of next bracket is to be passed as an argument to function <symbol> */
	gboolean nest_flag = FALSE;  /** indicates characters are being collected and not parsed */
	gboolean nest_init_flag = FALSE;  /** necessary to skip first character '(' during string collection */
	gboolean neg_flag = TRUE;  /** set if next '-' will make number signed and not be an operator */
	gboolean frac_flag = FALSE;
	
	
	//char nested_term[100] = { 0 }; /** collector string for contents of nested term */
	GString *nested_term = g_string_new(&null);
	//char symbol[100] = { 0 }; /** collector string for symbol name */
	GString *symbol = g_string_new(&null);


	for (i=0; i < args_len; i++)
	{

		if (nest_init_flag) {nest_init_flag = FALSE;}

		/** lock computing by raising nest level, substitute '*' if coefficient exists */
		if (args[i] == '(')
		{
			if (!nest_flag) /** nested interpreting is just about to be initialized */
			{
				if (coef_flag) {stack_push(operators, &mul_char); last_p = priority(mul_char);}
				coef_flag = TRUE;
				nest_flag = TRUE;
				nest_init_flag = TRUE;
				gvars->nest_level += 1;
				nested_term = g_string_new(&null);
			}
			else  /** nested interpreting is in progress */
			{
				local_nest_level += 1;
			}
		}

		else if (args[i] == ')')
		{
			if (nest_flag && local_nest_level == 0) /** nesting has reached end */
			{
				nest_flag = FALSE;
				gdouble nested_term_result = _parse(nested_term->str, gvars);
				gvars->nest_level -= 1;
				g_string_free(nested_term, TRUE);
				nested_term = g_string_new(&null);
				if (func_flag)
				{
					gdouble compute_function_results = compute_function(symbol->str, nested_term_result, gvars);
					stack_push(arguments, &compute_function_results);
					func_flag = FALSE;
					g_string_free(symbol, TRUE);
					symbol = g_string_new(&null);
				}
				else {stack_push(arguments, &nested_term_result);}
			}
			else  /** nested interpreting is in progress, passing by uninterpreted ')' */
			{
				local_nest_level -= 1;
			}
		}


		if (!nest_flag)
		{

			if (args[i] == '.' || args[i] == ',')
			{
				if (g_ascii_isdigit(char_at(args,i+1))) {frac_flag = TRUE;}
				else {gvars->error_type = 3; return 0;}
			}

			else if (g_ascii_isdigit(args[i]))  /** parse number */
			{
				if (gvars->debug)
					{for (j=0;j<gvars->nest_level;j++) {g_printf("   ");} g_printf("args[%d] is digit\n", i);}

				gint8 dig = to_d(args[i]);
				stack_push(gvars->digits, &dig);
				if (frac_flag) {gvars->frac_point -= 1;}
				/** check if there is more than one digit or fractal part */
				if (!(g_ascii_isdigit(char_at(args, i+1)) || char_at(args, i+1) == '.' || char_at(args, i+1) == ','))
				{
					if (coef_flag) {stack_push(operators, &mul_char); last_p = priority(mul_char);}
					gdouble joined_dig =  join_digits(gvars->digits, gvars->frac_point);
					stack_push(arguments, &joined_dig);
					neg_flag = FALSE; coef_flag = TRUE; frac_flag = FALSE; gvars->frac_point = 0;
				}
			}

			else if (isoperator(args[i]))  /** parse operators */
			{
				if (gvars->debug)
					{for (j=0;j<gvars->nest_level;j++) {g_printf("   ");} g_printf("args[%d] is operator\n", i);}

				if (neg_flag && args[i] == '-')  /** check if preceeding minus changes sign of next symbol */
				{
					neg_flag = FALSE;
					stack_push(arguments, &minus_one); stack_push(operators, &mul_char); last_p = priority(mul_char);
				}
				else
				{
					if (stack_length(arguments) <= stack_length(operators)) {gvars->error_type = 4; break;}
					/** check beforehand if lower priority operator is encountered */
					if (priority(args[i]) < last_p) {compute(arguments, operators, gvars);}
					last_p = priority(args[i]);
					stack_push(operators, &args[i]);
					coef_flag = FALSE;
					neg_flag = TRUE;
				}
			}

			else if (g_ascii_isalpha(args[i])) /** parse letters */
			{
				if (gvars->debug)
					{for (j=0;j<gvars->nest_level;j++) {g_printf("   ");} printf("args[%d] is letter\n", i);}

				if (coef_flag) {coef_flag = FALSE; stack_push(operators, &mul_char); last_p = priority(mul_char);}
				if (neg_flag) {neg_flag = FALSE;}
				g_string_append_c(symbol, args[i]);
				if (char_at(args,i+1) == '(')
				{
					compute_function(symbol->str, 1.337, gvars);
					if (gvars->error_type != 0)
					{
						gvars->error_type = 0;
						gdouble looked_up_c = lookup_constant(symbol->str, gvars);
						stack_push(arguments, &looked_up_c);
						//+symbol = "";
						g_string_free(symbol, TRUE);
						symbol = g_string_new(&null);
						coef_flag = TRUE;
					}
					else {func_flag = TRUE;}
				}
				else if (!g_ascii_isalpha(char_at(args,i+1)))
				{
					gdouble looked_up_c = lookup_constant(symbol->str, gvars);
					stack_push(arguments, &looked_up_c);
					g_string_free(symbol, TRUE);
					symbol = g_string_new(&null);
					coef_flag = TRUE;
				}
			}

		}

		else if (!nest_init_flag) /** this collector block needs to be skipped once so the first '(' isn't collected */
		{
			g_string_append_c(nested_term, args[i]);
		}

		if (args[i] == ' ') {coef_flag = FALSE;}

		if (char_at(args,i) == '#') {break;}  /** failsafe, in case array bounds are left */
	}
	if (gvars->debug)
		{printf("<args>\n");stack_dump(arguments, 'd');printf("<ops>\n");stack_dump(operators, 'c');printf("<>\n");}

	if (local_nest_level != 0 && gvars->error_type == 0) {gvars->error_type = 1;}
	if (neg_flag && gvars->error_type == 0) {gvars->error_type = 4;}
	if (gvars->error_type == 0) {compute(arguments, operators, gvars);}
	if (stack_length(arguments) > 1 && gvars->error_type == 0) {gvars->error_type = 4;printf("no2\n");}

	gdouble return_value = 0;
	if (gvars->error_type == 0) {stack_pop(arguments, &return_value);}
	stack_destroy(arguments);
	stack_destroy(operators);

	return return_value;
}
Example #20
0
static ssize_t __prepare_data(char *buf, const size_t size,
			      const char *fmt, va_list ap)
{
	char *p = buf, *endp = &buf[size], *s;
	int len;
	int32_t i32;
	uint64_t u64;

	while (*fmt && *fmt != '%') fmt++;
	if (*fmt == '%') fmt++;
	while (*fmt) {
		switch (*fmt++) {
		case 's':              /* string */
			s = va_arg(ap, char *);
			if (s)
				len = strlen(s);
			else
				len = 0;
			if (p + len + 4 > endp) {
				dbg("buffer to short for string");
				stack_dump();
				return -1;
			}
			i32 = htobe32(len);
			memcpy(p, &i32, 4);
			p += 4;
			if (s)
				memcpy(p, s, len);
			p += len;
			break;
		case 'p':              /* binary data with specified length */
			s = va_arg(ap, char *);
			len = va_arg(ap, ssize_t);
			if (p + len + 4 > endp) {
				dbg("buffer to short for string");
				stack_dump();
				return -1;
			}
			i32 = htobe32(len);
			memcpy(p, &i32, 4);
			p += 4;
			memcpy(p, s, len);
			p += len;
			break;
		case 'i':              /* 32-bit int */
			if (p + 4 > endp) {
				dbg("buffer to short for int32_t");
				stack_dump();
				return -1;
			}

			i32 = htobe32(va_arg(ap, int32_t));
			memcpy(p, &i32, 4);
			p += 4;
			break;
		case 'l':              /* 64-bit unsigned int */
			if (*fmt++ != 'u') {
				dbg("invalid long format character: '%c'", *fmt);
				stack_dump();
				break;
			}
			if (p + 8 > endp) {
				dbg("buffer to short for uint64_t");
				stack_dump();
				return -1;
			}
			u64 = htobe64(va_arg(ap, uint64_t));
			memcpy(p, &u64, 8);
			p += 8;
			break;
		default:
			dbg("invalid format character: '%c'", *fmt);
			stack_dump();
		}
		while (*fmt && *fmt != '%') fmt++;
		if (*fmt == '%') fmt++;
	}
	return p - buf;
}