Ejemplo n.º 1
0
extern char *str_urlencode(char *src)
{
    char *res = NULL;

    int off = 0, len = strlen(src);
    char *tmp = mem_alloc(3 * len + 1);
    char hex[3];

    for (int i = 0; i < len; ++i) {
        hex[1] = 0;
        int v = (int) src[i];
        if (v < 0)
            goto exit;

        str_print(hex, "%X", v);
        if (hex[1] == 0) {
            hex[1] = hex[0];
            hex[0] = '0';
        }
        off += str_print(tmp + off, "%%%s", hex);
    }

    tmp[off] = '\0';
    res = tmp;
    tmp = NULL;

exit:
    if (tmp != NULL)
        mem_free(tmp);

    return res;
}
Ejemplo n.º 2
0
static __isl_give isl_printer *str_end_line(__isl_take isl_printer *p)
{
	if (p->suffix)
		p = str_print(p, p->suffix, strlen(p->suffix));
	p = str_print(p, "\n", strlen("\n"));
	return p;
}
Ejemplo n.º 3
0
Variable* run_function(Interpretator *interpretator, Function *f, Array *args)
{
    Variable *return_var=undefined;
    int       i;
    int      *current_base;

    if(f->return_var)
    {
        return_var=interpretator->stack_head;
        return_var->type=UNDEFINED;
        return_var->shift=-1;
        return_var->name=f->return_var->name;
        return_var->is_closed=f->return_var->is_closed;
        interpretator->stack_head++;

        interpretator->stack_head=(int*)interpretator->stack_head+1;
    }

    current_base=interpretator->stack_head;
    *current_base=interpretator->stack_base;

    interpretator->stack_head=(int*)interpretator->stack_head+f->variables->length+1; //+base

    if(args)
        push_args(interpretator, args, f->args, current_base);

    interpretator->stack_base=current_base;

    if(f->return_var)
        current_base[f->return_var->shift]=return_var;//инициализация return var

    execute(interpretator, f->body);

    if(f->return_var)
        to_const(return_var, interpretator);

    interpretator->stack_head=(int*)interpretator->stack_head-f->variables->length-1;
    interpretator->stack_head-=f->args->length;
    interpretator->stack_base=*interpretator->stack_base;

    if(f->return_var)
    {
        printf("\n<function ");
        str_print(f->name);
        printf(" return ");
        str_print(f->return_var->name);
        printf(">\n");
    }

    if(f->return_var)
    {
        interpretator->stack_head=(int*)interpretator->stack_head-1;
        interpretator->stack_head--;
        return return_var;
    }
    return undefined;
}
/*
* function that prints the nodes of a list. This function
* also prints the previous nodes and next nodes based on the current node.
*/
void print_dl_list(List *list){
  List *tmp;
  tmp = list;
  while(tmp != 0){
    /*Printing the name of the node*/
    str_print(tmp->str);
    str_print("\n\t");
    /*Printing NUll if the previous element is null*/
    if (tmp->prev == 0){
      str_print("NULL");
    }else{
      /*Printing the data of the previous element*/
      str_print(tmp->prev->str);
    }
    str_print("\n\t");
    /*Printing null if the next element of the node is null*/
    if (tmp->next == 0){
      str_print("NULL");
    }else{
      /*pringing the data of the next element of the node*/
      str_print(tmp->next->str);
    }
    str_print("\n");
    /*Moving to the next node*/
    tmp = tmp->next;
  }
}
Ejemplo n.º 5
0
static __isl_give isl_printer *str_start_line(__isl_take isl_printer *p)
{
	p = str_print_indent(p, p->indent);
	if (p->prefix)
		p = str_print(p, p->prefix, strlen(p->prefix));
	return p;
}
Ejemplo n.º 6
0
type resolve(str name) {
  type ret;
  if (str_eq(name, str_from_lit("s32"))) {
    ret.kind = Ty_sint;
    ret.data.sint = Int_32;
  } else {
    printf("Error: unknown type: "); str_print(name); puts("");
    exit(1);
  }

  return ret;
}
Ejemplo n.º 7
0
int
progress(int64_t arg, int64_t completed)
{
	char s[BUFSIZE];
	uint64_t total = (uint64_t)arg;

	if (completed == -1) {
		/* Got ^C. Add 2 to progress pos to compensate for ^ and C */
		progress_pos = str_print("(flushing ...)", progress_pos+2);
		return (0);
	}
	if (total == 0) {
		if (tm != time(NULL)) {
			tm = time(NULL);
			(void) snprintf(s, BUFSIZE,
			    gettext("%d bytes written"), completed);

			progress_pos = str_print(s, progress_pos);
		}
	} else {
		total = (((uint64_t)completed) * 100)/total;
		if (total == last_total)
			return (0);
		last_total = total;
		if (total > 100) {
			/* There is clearly a miscalculation somewhere */
			if (debug)
				(void) printf("\nWrote more than 100 %% !!\n");
			return (0);
		}
		if (total == 100) {
			/* l10n_NOTE : 'done' as in "Writing track 1...done"  */
			(void) snprintf(s, BUFSIZE, gettext("done.\n"));
		} else {
			(void) snprintf(s, BUFSIZE, "%d %%", (uint_t)total);
		}
		progress_pos = str_print(s, progress_pos);
	}
	return (0);
}
Ejemplo n.º 8
0
extern int sys_exec(char *const a[])
{
    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    ZeroMemory(&pi, sizeof(pi));
    ZeroMemory(&si, sizeof(si));
    si.cb = sizeof(si);

    char c[1024] = "";
    str_print(c, "\"%s\" ", a[0]);
    for (int i = 1; a[i] != NULL; ++i)
        str_print(c, "%s %s", c, a[i]);

    if (!CreateProcess(NULL, c, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
        sys_die("sys: cannot create process\n");

    CloseHandle(pi.hProcess);
    CloseHandle(pi.hThread);

    return pi.dwProcessId;
}
Ejemplo n.º 9
0
static __isl_give isl_printer *str_print_isl_int(__isl_take isl_printer *p,
	isl_int i)
{
	char *s;
	int len;

	s = isl_int_get_str(i);
	len = strlen(s);
	if (len < p->width)
		p = str_print_indent(p, p->width - len);
	p = str_print(p, s, len);
	isl_int_free_str(s);
	return p;
}
Ejemplo n.º 10
0
static void print(struct statement *st)
{
    switch (st->type) {
    default:
    case AST_ST_NOP:
        printf("nop.\n");
        break;

    case AST_ST_V_OF_TERM:
        str_print(stdout, symbol_name(st->variable));
        fputs(" = ", stdout);
        term head;
        enif_get_list_cell(NULL, st->call.args, &head, NULL);
        pretty_print_term(stdout, &head);
        puts(".");
        assert(variable_assign(st->variable, head));
        break;

    case AST_ST_V_OF_MFA:
        str_print(stdout, symbol_name(st->variable));
        fputs(" = ", stdout);
        pretty_print_mfa(&st->call);
        puts(".");
        break;

    case AST_ST_MFA:
        pretty_print_mfa(&st->call);
        puts(".");
        break;

    case AST_ST_VAR:
        str_print(stdout, symbol_name(st->variable));
        puts(".");
        break;
    }
}
Ejemplo n.º 11
0
Variable* run_function(Interpretator *interpretator, Function *f, Array *args)
{
    //Variable *return_var=undefined;
    int       i;
    int      *prev_base;

    //if(f->return_var)
        //return_var=interpretator_add_var(interpretator, f->return_var);

    if(f->return_var)
        interpretator->stack_head++;

    if(args)
        eval_args(interpretator, args, f->args);

    prev_base=interpretator->stack_base;
    interpretator->stack_base=(int*)interpretator->stack_head;
    *interpretator->stack_base=prev_base;
    interpretator->stack_head=(int*)interpretator->stack_head+f->variables->length+1;

    execute(interpretator, f->body);

    interpretator->stack_base=*(int*)interpretator->stack_base;

    if(f->return_var)
    {
        printf("\n<function ");
        str_print(f->name);
        printf(" return ");
        str_print(f->return_var->name);
        printf(">\n");
    }

    return f->return_var;
    //return return_var;
}
Ejemplo n.º 12
0
static void *exec_thread(void *arg)
{
    Exec *e = arg;
    for (;;) {
        int p = 0, pid = -1;

        /* creating processor */
        IO *sio = sys_socket(&p);
        char port[8];
        str_print(port, "%d", p);
        char *argv[] = {e->exe, "processor", "-p", port, "-t", e->tx, NULL};

        pid = sys_exec(argv);
        if (!sys_iready(sio, PROC_WAIT_SEC)) {
            sys_log('E', "failed to fork a processor\n");
        } else {
            IO *pio = sys_accept(sio, IO_CHUNK);
            int ok = 1;
            while (ok) {
                pio->stop = 0;
                Conn *c = queue_get(e->runq);

                int pcnt = 0, ccnt = 0;
                sys_proxy(c->io, &ccnt, pio, &pcnt);

                ok = pio->stop == IO_TERM || (ccnt == 0 && !pio->stop);
                if (!ok && pcnt == 0) {
                    int status = http_500(c->io);
                    sys_log('E', "failed with status %d\n", status);
                }

                c->time = sys_millis();
                queue_put(e->waitq, c);
            }
            sys_close(pio);
        }
        sys_close(sio);

        if (pid != -1) {
            sys_kill(pid);
            sys_wait(pid);
        }
    }

    mem_free(e);

    return NULL;
}
Ejemplo n.º 13
0
static __isl_give isl_printer *str_print_isl_int(__isl_take isl_printer *p,
	isl_int i)
{
	char *s;
	int len;
	isl_int_print_gmp_free_t gmp_free;

	s = isl_int_get_str(i);
	len = strlen(s);
	if (len < p->width)
		p = str_print_indent(p, p->width - len);
	p = str_print(p, s, len);
	mp_get_memory_functions(NULL, NULL, &gmp_free);
	(*gmp_free)(s, len + 1);
	return p;
}
Ejemplo n.º 14
0
static void check_vars(Vars *v)
{
    if (v == NULL || v->len != 3)
        fail();

    if (str_cmp(v->names[0], "a1") != 0 || v->vers[0] != 1)
        fail();
    if (str_cmp(v->names[1], "a2") != 0 || v->vers[1] != 2)
        fail();
    if (str_cmp(v->names[2], "a3") != 0 || v->vers[2] != 3)
        fail();

    for (int i = 0; i < 3; ++i) {
        char exp[MAX_NAME];
        str_print(exp, "%d", i + 3);

        if (str_cmp(v->vols[i], exp) != 0)
            fail();
    }
}
Ejemplo n.º 15
0
static void test_vars()
{
    Vars *v[] = {vars_new(0), vars_new(1), vars_new(3), vars_new(7)};

    for (unsigned i = 0; i < sizeof(v) / sizeof(Vars*); ++i) {
        vars_add(v[i], "a1", 1, NULL);
        vars_add(v[i], "a2", 2, NULL);
        vars_add(v[i], "a3", 3, NULL);
        for (int j = 0; j < 3; ++j)
            str_print(v[i]->vols[j], "%d", j + 3);

        check_vars(v[i]);

        Vars *c = vars_new(0);
        vars_cpy(c, v[i]);
        check_vars(c);

        const char *file = "bin/relation_vars_test";
        IO *io = sys_open(file, CREATE | WRITE);
        if (vars_write(v[i], io) < 0)
            fail();
        sys_close(io);
        io = sys_open(file, READ);
        Vars *out = vars_read(io);
        if (out == NULL)
            fail();

        check_vars(out);

        vars_free(v[i]);
        vars_free(out);
        vars_free(c);

        sys_close(io);
    }
}
Ejemplo n.º 16
0
static __isl_give isl_printer *str_print_str(__isl_take isl_printer *p,
	const char *s)
{
	return str_print(p, s, strlen(s));
}
Ejemplo n.º 17
0
int main(){
    char* str = NULL;
    str_copy(&str, "Hello");
    str_print(&str);
    return 0;
}
Ejemplo n.º 18
0
int main(int argc, char *argv[])
{
    int port = 0;
    char *data = NULL;
    char *state = NULL;
    char *source = NULL;
    char *tx_addr = NULL;

    sys_init(1);
    if (argc < 2)
        usage(argv[0]);

    for (int i = 2; (i + 1) < argc; i += 2)
        if (str_cmp(argv[i], "-d") == 0)
            data = argv[i + 1];
        else if (str_cmp(argv[i], "-c") == 0)
            source = argv[i + 1];
        else if (str_cmp(argv[i], "-s") == 0)
            state = argv[i + 1];
        else if (str_cmp(argv[i], "-p") == 0)
            port = parse_port(argv[i + 1]);
        else if (str_cmp(argv[i], "-t") == 0) {
            tx_addr = argv[i + 1];
            if (str_len(tx_addr) >= MAX_ADDR)
                sys_die("tx address exceeds the maximum length\n");
        } else
            usage(argv[0]);

    if (str_cmp(argv[1], "start") == 0 && source != NULL && data != NULL &&
        state != NULL && port != 0 && tx_addr == NULL)
    {
        int tx_port = 0;
        tx_server(source, state, &tx_port);
        vol_init(0, data);

        char addr[MAX_ADDR];
        str_print(addr, "127.0.0.1:%d", tx_port);
        multiplex(argv[0], addr, port);

        tx_free();
    } else if (str_cmp(argv[1], "processor") == 0 && source == NULL &&
               data == NULL && state == NULL && port != 0 && tx_addr != NULL)
    {
        processor(tx_addr, port);
    } else if (str_cmp(argv[1], "tx") == 0 && source != NULL &&
               data == NULL && state != NULL && port != 0 && tx_addr == NULL)
    {
        tx_server(source, state, &port);
    } else if (str_cmp(argv[1], "vol") == 0 && source == NULL &&
               data != NULL && state == NULL && port != 0 && tx_addr != NULL)
    {
        tx_attach(tx_addr);
        vol_init(port, data);
    } else if (str_cmp(argv[1], "exec") == 0 && source == NULL &&
               data == NULL && state == NULL && port != 0 && tx_addr != NULL)
    {
        tx_attach(tx_addr);
        multiplex(argv[0], tx_addr, port);
    } else if (str_cmp(argv[1], "convert") == 0 && source == NULL &&
               data == NULL && state == NULL && port == 0 && tx_addr == NULL)
    {
        conv_parse();
    } else
        usage(argv[0]);

    return 0;
}
int main (void){
  str_print("hello");
  str_print("\n");
  return 0;
}