Exemplo n.º 1
0
void
log_set_peer_addr(const char *addr)
{

	/*
	 * XXX: Turn it into assertion?
	 */
	if (peer_addr != NULL)
		log_errx(1, "%s called twice", __func__);

	peer_addr = checked_strdup(addr);
}
Exemplo n.º 2
0
void
keys_add(struct keys *keys, const char *name, const char *value)
{
	int i;

	log_debugx("key to send: \"%s=%s\"", name, value);

	/*
	 * Note that we don't check for duplicates here, as they are perfectly
	 * fine in responses, e.g. the "TargetName" keys in discovery sesion
	 * response.
	 */
	for (i = 0; i < KEYS_MAX; i++) {
		if (keys->keys_names[i] == NULL) {
			keys->keys_names[i] = checked_strdup(name);
			keys->keys_values[i] = checked_strdup(value);
			return;
		}
	}
	log_errx(1, "too many keys");
}
Exemplo n.º 3
0
struct rchap *
rchap_new(const char *secret)
{
    struct rchap *rchap;

    rchap = calloc(1, sizeof(*rchap));
    if (rchap == NULL)
        log_err(1, "calloc");

    rchap->rchap_secret = checked_strdup(secret);

    return (rchap);
}
Exemplo n.º 4
0
void
log_set_peer_name(const char *name)
{

	/*
	 * XXX: Turn it into assertion?
	 */
	if (peer_name != NULL)
		log_errx(1, "%s called twice", __func__);
	if (peer_addr == NULL)
		log_errx(1, "%s called before log_set_peer_addr", __func__);

	peer_name = checked_strdup(name);
}
Exemplo n.º 5
0
static void
resolve_addr(const struct connection *conn, const char *address,
             struct addrinfo **ai, bool initiator_side)
{
    struct addrinfo hints;
    char *arg, *addr, *ch;
    const char *port;
    int error, colons = 0;

    arg = checked_strdup(address);

    if (arg[0] == '\0') {
        fail(conn, "empty address");
        log_errx(1, "empty address");
    }
    if (arg[0] == '[') {
        /*
         * IPv6 address in square brackets, perhaps with port.
         */
        arg++;
        addr = strsep(&arg, "]");
        if (arg == NULL) {
            fail(conn, "malformed address");
            log_errx(1, "malformed address %s", address);
        }
        if (arg[0] == '\0') {
            port = NULL;
        } else if (arg[0] == ':') {
            port = arg + 1;
        } else {
            fail(conn, "malformed address");
            log_errx(1, "malformed address %s", address);
        }
    } else {
        /*
         * Either IPv6 address without brackets - and without
         * a port - or IPv4 address.  Just count the colons.
         */
        for (ch = arg; *ch != '\0'; ch++) {
            if (*ch == ':')
                colons++;
        }
        if (colons > 1) {
            addr = arg;
            port = NULL;
        } else {
            addr = strsep(&arg, ":");
            if (arg == NULL)
                port = NULL;
            else
                port = arg;
        }
    }

    if (port == NULL && !initiator_side)
        port = "3260";

    memset(&hints, 0, sizeof(hints));
    hints.ai_family = PF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = AI_ADDRCONFIG | AI_NUMERICSERV;
    if (initiator_side)
        hints.ai_flags |= AI_PASSIVE;

    error = getaddrinfo(addr, port, &hints, ai);
    if (error != 0) {
        fail(conn, gai_strerror(error));
        log_errx(1, "getaddrinfo for %s failed: %s",
                 address, gai_strerror(error));
    }
}
Exemplo n.º 6
0
static void
process_init_file(const char *filename)
{
    /*
    ** The strings that are supposed to be followed by other information
    ** (INIT, REQUIRED_INIT, and REQUIRED_FINAL) should end with
    ** the space that separates the keyword from the following data.
    ** The string that is not supposed to be following by other information
    ** (ENDINIT) should not have a following space, since llds_out.m and
    ** mlds_to_c.m do not add that space.
    */

    const char * const  init_str = "INIT ";
    const char * const  reqinit_str = "REQUIRED_INIT ";
    const char * const  reqfinal_str = "REQUIRED_FINAL ";
    const char * const  envvar_str = "ENVVAR ";
    const char * const  endinit_str = "ENDINIT";
    const int           init_strlen = strlen(init_str);
    const int           reqinit_strlen = strlen(reqinit_str);
    const int           reqfinal_strlen = strlen(reqfinal_str);
    const int           envvar_strlen = strlen(envvar_str);
    const int           endinit_strlen = strlen(endinit_str);
    char                line[MAXLINE];
    FILE                *cfile;

    cfile = fopen(filename, "r");
    if (cfile == NULL) {
        fprintf(stderr, "%s: error opening file `%s': %s\n",
            MR_progname, filename, strerror(errno));
        num_errors++;
        return;
    }

    while (get_line(cfile, line, MAXLINE) > 0) {
        if (strncmp(line, init_str, init_strlen) == 0) {
            char    *func_name;
            int     func_name_len;
            int     j;
            MR_bool special;

            for (j = init_strlen; MR_isalnumunder(line[j]); j++) {
                /* VOID */
            }
            line[j] = '\0';

            func_name = line + init_strlen;
            func_name_len = strlen(func_name);
            if (MR_strneq(&func_name[func_name_len - 4], "init", 4)) {
                func_name[func_name_len - 4] = '\0';
                MR_ensure_room_for_next(std_module, const char *,
                    MR_INIT_STD_MODULE_SIZE);
                std_modules[std_module_next] = checked_strdup(func_name);
                std_module_next++;
            } else {
                MR_ensure_room_for_next(special_module, const char *,
                    MR_INIT_SPECIAL_MODULE_SIZE);
                special_modules[special_module_next] =
                    checked_strdupcat(func_name, "_");
                special_module_next++;
            }
        } else if (strncmp(line, reqinit_str, reqinit_strlen) == 0) {
            char    *func_name;
            int     j;

            for (j = reqinit_strlen; MR_isalnumunder(line[j]); j++) {
                /* VOID */
            }
            line[j] = '\0';

            func_name = line + reqinit_strlen;
            MR_ensure_room_for_next(req_init_module, const char *,
                MR_INIT_REQ_MODULE_SIZE);
            req_init_modules[req_init_module_next] = checked_strdup(func_name);
            req_init_module_next++;
        } else if (strncmp(line, reqfinal_str, reqfinal_strlen) == 0) {
            char    *func_name;
            int     j;

            for (j = reqfinal_strlen; MR_isalnumunder(line[j]); j++) {
                /* VOID */
            }
            line[j] = '\0';

            func_name = line + reqfinal_strlen;
            MR_ensure_room_for_next(req_final_module, const char *,
                MR_FINAL_REQ_MODULE_SIZE);
            req_final_modules[req_final_module_next] =
                checked_strdup(func_name);
            req_final_module_next++;
        } else if (strncmp(line, envvar_str, envvar_strlen) == 0) {
            char    *envvar_name;
            int     i;
            int     j;
            MR_bool found;

            /*
            ** Check that all characters in the name of the environment
            ** variable are acceptable as components of a C variable name.
            ** Note that the variable name doesn't have to start with a letter
            ** because the variable name has a prefix.
            */
            for (j = envvar_strlen; MR_isalnumunder(line[j]); j++) {
                /* VOID */
            }

            if (line[j] != '\n') {
                printf("mkinit: error: bad environment variable name %s\n",
                    line);
            }

            line[j] = '\0';     /* overwrite the newline */

            envvar_name = line + envvar_strlen;

            /*
            ** Since the number of distinct environment variables used by
            ** a program is likely to be in the single digits, linear search
            ** should be efficient enough.
            */
            found = MR_FALSE;
            for (i = 0; i < mercury_env_var_next; i++) {
                if (strcmp(envvar_name, mercury_env_vars[i]) == 0) {
                    found = MR_TRUE;
                    break;
                }
            }

            if (!found) {
                MR_ensure_room_for_next(mercury_env_var, const char *,
                    MR_ENV_VAR_LIST_SIZE);
                mercury_env_vars[mercury_env_var_next] =
                    checked_strdup(envvar_name);
                mercury_env_var_next++;
            }
        } else if (strncmp(line, endinit_str, endinit_strlen) == 0) {
            break;
        }
    }

    fclose(cfile);
}
Exemplo n.º 7
0
int
main(int argc, char **argv)
{
	int ch, error;
	bool Vflag = false, vflag = false;
	const char *certpath = NULL, *keypath = NULL, *outpath = NULL, *inpath = NULL;
	FILE *certfp = NULL, *keyfp = NULL;
	X509 *cert = NULL;
	EVP_PKEY *key = NULL;
	pid_t pid;
	int pipefds[2];

	while ((ch = getopt(argc, argv, "Vc:k:o:v")) != -1) {
		switch (ch) {
		case 'V':
			Vflag = true;
			break;
		case 'c':
			certpath = checked_strdup(optarg);
			break;
		case 'k':
			keypath = checked_strdup(optarg);
			break;
		case 'o':
			outpath = checked_strdup(optarg);
			break;
		case 'v':
			vflag = true;
			break;
		default:
			usage();
		}
	}

	argc -= optind;
	argv += optind;
	if (argc != 1)
		usage();

	if (Vflag) {
		if (certpath != NULL)
			errx(1, "-V and -c are mutually exclusive");
		if (keypath != NULL)
			errx(1, "-V and -k are mutually exclusive");
		if (outpath != NULL)
			errx(1, "-V and -o are mutually exclusive");
	} else {
		if (certpath == NULL)
			errx(1, "-c option is mandatory");
		if (keypath == NULL)
			errx(1, "-k option is mandatory");
		if (outpath == NULL)
			errx(1, "-o option is mandatory");
	}

	inpath = argv[0];

	OPENSSL_config(NULL);
	ERR_load_crypto_strings();
	OpenSSL_add_all_algorithms();

	error = pipe(pipefds);
	if (error != 0)
		err(1, "pipe");

	pid = fork();
	if (pid < 0)
		err(1, "fork");

	if (pid == 0)
		return (child(inpath, outpath, pipefds[1], Vflag, vflag));

	if (!Vflag) {
		certfp = checked_fopen(certpath, "r");
		cert = PEM_read_X509(certfp, NULL, NULL, NULL);
		if (cert == NULL) {
			ERR_print_errors_fp(stderr);
			errx(1, "failed to load certificate from %s", certpath);
		}

		keyfp = checked_fopen(keypath, "r");
		key = PEM_read_PrivateKey(keyfp, NULL, NULL, NULL);
		if (key == NULL) {
			ERR_print_errors_fp(stderr);
			errx(1, "failed to load private key from %s", keypath);
		}

		sign(cert, key, pipefds[0]);
	}

	return (wait_for_child(pid));
}
Exemplo n.º 8
0
static void
process_init_file(const char *filename, const char *prefix_str)
{
    /*
    ** The strings that are supposed to be followed by other information
    ** (INIT, REQUIRED_INIT, and REQUIRED_FINAL) should end with
    ** the space that separates the keyword from the following data.
    ** The string that is not supposed to be following by other information
    ** (ENDINIT) should not have a following space, since elds_to_erlang.m
    ** does not add that space.
    */

    const char * const  init_str = "INIT ";
    const char * const  reqinit_str = "REQUIRED_INIT ";
    const char * const  reqfinal_str = "REQUIRED_FINAL ";
    const char * const  envvar_str = "ENVVAR ";
    const char * const  endinit_str = "ENDINIT";
    const size_t        prefix_strlen = strlen(prefix_str);
    const size_t        init_strlen = strlen(init_str);
    const size_t        reqinit_strlen = strlen(reqinit_str);
    const size_t        reqfinal_strlen = strlen(reqfinal_str);
    const size_t        envvar_strlen = strlen(envvar_str);
    const size_t        endinit_strlen = strlen(endinit_str);
    char                line0[MAXLINE];
    char *              line;
    size_t              len;
    FILE                *erl_file;

    erl_file = fopen(filename, "r");
    if (erl_file == NULL) {
        fprintf(stderr, "%s: error opening file `%s': %s\n",
            MR_progname, filename, strerror(errno));
        num_errors++;
        return;
    }

    while (get_line(erl_file, line0, MAXLINE) > 0) {
        if (strncmp(line0, prefix_str, prefix_strlen) != 0) {
            continue;
        }
        line = line0 + prefix_strlen;

        /* Remove trailing whitespace. */
        len = strlen(line);
        while (len > 0 && isspace((int)line[len - 1])) {
            line[len - 1] = '\0';
            len--;
        }

        if (strncmp(line, init_str, init_strlen) == 0) {
            char    *func_name;
            size_t  func_name_len;

            func_name = line + init_strlen;
            func_name_len = strlen(func_name);

            func_name[func_name_len - 4] = '\0';
            MR_ensure_room_for_next(std_module, const char *,
                MR_INIT_STD_MODULE_SIZE);
            std_modules[std_module_next] = checked_strdup(func_name);
            std_module_next++;
        } else if (strncmp(line, reqinit_str, reqinit_strlen) == 0) {
            char    *func_name;

            func_name = line + reqinit_strlen;
            MR_ensure_room_for_next(req_init_module, const char *,
                MR_INIT_REQ_MODULE_SIZE);
            req_init_modules[req_init_module_next] = checked_strdup(func_name);
            req_init_module_next++;
        } else if (strncmp(line, reqfinal_str, reqfinal_strlen) == 0) {
            char    *func_name;

            func_name = line + reqfinal_strlen;
            MR_ensure_room_for_next(req_final_module, const char *,
                MR_FINAL_REQ_MODULE_SIZE);
            req_final_modules[req_final_module_next] =
                checked_strdup(func_name);
            req_final_module_next++;
        } else if (strncmp(line, envvar_str, envvar_strlen) == 0) {
            char    *envvar_name;
            int     i;
            MR_bool found;

            envvar_name = line + envvar_strlen;

            /*
            ** Since the number of distinct environment variables used by
            ** a program is likely to be in the single digits, linear search
            ** should be efficient enough.
            */
            found = MR_FALSE;
            for (i = 0; i < mercury_env_var_next; i++) {
                if (strcmp(envvar_name, mercury_env_vars[i]) == 0) {
                    found = MR_TRUE;
                    break;
                }
            }

            if (!found) {
                MR_ensure_room_for_next(mercury_env_var, const char *,
                    MR_ENV_VAR_LIST_SIZE);
                mercury_env_vars[mercury_env_var_next] =
                    checked_strdup(envvar_name);
                mercury_env_var_next++;
            }
        } else if (strncmp(line, endinit_str, endinit_strlen) == 0) {
            break;
        }
    }

    fclose(erl_file);
}
Exemplo n.º 9
0
void syntax(string filename) {
	S_token_stack t_stack = checked_malloc( sizeof *t_stack );
	t_stack->size = 0;
	for(int i=0; i<STACK_SIZE; i++) t_stack->contents[i] = NULL;
	S_state_stack s_stack = checked_malloc( sizeof *s_stack );
	s_stack->size = 0;
	for(int i=0; i<STACK_SIZE; i++) s_stack->contents[i] = -1;
	//stack_push(t_stack, getToken(token_find_id("$")));
	stack_push(s_stack, 0);

	assert(start(filename));

	int state = -1;
	S_token token = NULL;
	act_type act = -1;
	int act_num = -1;
    token = getToken(token_lex());
	while( TRUE ) {
		state = stack_top(s_stack);
		act_num = action_table[state]->actions[token->id];
		act = action_table[state]->type[token->id];
		if( SHIFT == act ) {
			stack_push(s_stack, act_num);
			stack_push(t_stack, token);
            token = getToken(token_lex());
//        	printf("New TOKEN: %s\n", tokens_text[token->id]);
//        	if(token->type == S_IDENTIFIER) {
//        		printf("NAME: %s\n\n", token->val->s);
//        	}
//        	else {
//        		printf("\n");
//        	}
		}
		else if( REDUCE == act ) {
			S_token head_token = getToken(rules[act_num]->head_token_nr);  // new token, the target

			/* do semantic action here */
//			printf("Token is :%s\n", tokens_text[token->id]);
			printf("Reduce by rule %d : %s\n", act_num, rules_text[act_num]);

			//		"primary_expression: IDENTIFIER ", symref
			if( 1 == act_num ) {
				//head_token->ast = (E_ast)E_newstr(E_SYM_REF, val->s );
				head_token->ast = E_newast(E_SYM_REF, NULL, NULL);
				head_token->ast->s = checked_strdup(val->s);
			}

			//		"primary_expression: CONSTANT ", numval
			if( 2 == act_num ) {
//				head_token->ast = (E_ast)E_newnum(E_NUM, val->s, val->num_t);
				head_token->ast = E_newast(E_NUM, NULL, NULL);
				head_token->ast->s = checked_strdup( val->s );
				head_token->ast->num_t = val->num_t;
			}

			//		"primary_expression: STRING_LITERAL ", strval
			if( 3 == act_num ) {
//				head_token->ast = (E_ast)E_newstr(E_STRING, val->s);
				head_token->ast = E_newast(E_STRING, NULL, NULL);
				head_token->ast->s = checked_strdup( val->s );
			}

			//		"postfix_expression: primary_expression ",
			if( 5 == act_num ) {
				head_token->ast = $(1)->ast;
			}

			//		"postfix_expression: postfix_expression [ expression ] ", E_ARRAY_REF
			if( 6 == act_num ) {
				head_token->ast = E_newast(E_ARRAY_REF, $(4)->ast, $(2)->ast);
			}

			//		"postfix_expression: postfix_expression ( argument_expression_list ) ", E_FUNC_REF
			if( 8 == act_num ) {
				head_token->ast = E_newast(E_FUNC_REF, $(4)->ast, $(2)->ast);
			}

			//		"postfix_expression: postfix_expression INC_OP ",
			if( 11 == act_num ) {
				assert($(2)->ast != NULL);
				head_token->ast = E_newast(E_INC_OP, $(2)->ast, NULL);
			}

			//		"argument_expression_list: assignment_expression ",
			if( 15 == act_num ) {
				head_token->ast = E_newast(E_ARG_END, $(1)->ast, NULL);

			}

			//		"argument_expression_list: argument_expression_list , assignment_expression ",
			if( 16 == act_num ) {
				printf("list.\n");
				head_token->ast = E_newast(E_ARG_LIST, $(3)->ast, $(1)->ast); // TODO is this ok? direction?
				//head_token->ast = E_newast();
			}

			//		"unary_expression: postfix_expression ",
			if( 17 == act_num ) {
				//TODO make new node for processing
				head_token->ast = $(1)->ast;
			}

			//		"unary_expression: unary_operator cast_expression ",
			if( 20 == act_num ) {
				head_token->ast = E_newast(E_UNARY_EXP, $(2)->ast, $(1)->ast);
			}

			//		"unary_operator: & ",
			if( 21 == act_num ) {
				head_token->ast = E_newast(E_TAKE_ADDR, NULL, NULL);
			}

			//		"cast_expression: unary_expression ",
			if( 27 == act_num ) {
				head_token->ast = $(1)->ast;
			}

			//		"multiplicative_expression: cast_expression ",
			if( 29 == act_num ) {
				head_token->ast = $(1)->ast;
			}

			//		"multiplicative_expression: multiplicative_expression * cast_expression ",
			if( 30 == act_num ) {
				head_token->ast = E_newast(E_MUL, $(3)->ast, $(1)->ast);
			}

			//		"multiplicative_expression: multiplicative_expression / cast_expression ",
			if( 31 == act_num ) {
				head_token->ast = E_newast(E_DIV, $(3)->ast, $(1)->ast);
			}

			//		"additive_expression: multiplicative_expression ",
			if( 33 == act_num ) {
				head_token->ast = $(1)->ast;
			}

			//		"additive_expression: additive_expression + multiplicative_expression ",
			if( 34 == act_num ) {
				head_token->ast = E_newast(E_ADD, $(3)->ast, $(1)->ast);
//				head_token->ast->pending = TRUE;
			}

			//		"additive_expression: additive_expression - multiplicative_expression ",
			if( 35 == act_num ) {
				head_token->ast = E_newast(E_MINUS, $(3)->ast, $(1)->ast);
			}

			//		"shift_expression: additive_expression ",
			if( 36 == act_num ) {
				head_token->ast = $(1)->ast;
			}

			//		"relational_expression: shift_expression ",
			if( 39 == act_num ) {
				head_token->ast = $(1)->ast;
			}

			//		"relational_expression: relational_expression < shift_expression ",
			if( 40 == act_num ) {
				head_token->ast = E_newast(E_REL_L, $(3)->ast, $(1)->ast);
			}

			//		"relational_expression: relational_expression GE_OP shift_expression ",
			if( 43 == act_num ) {
				head_token->ast = E_newast(E_REL_GE, $(3)->ast, $(1)->ast);
			}

			//		"equality_expression: relational_expression ",
			if( 44 == act_num ) {
				head_token->ast = $(1)->ast;
			}

			//		"and_expression: equality_expression ",
			if( 47 == act_num ) {
				head_token->ast = $(1)->ast;
			}

			//		"exclusive_or_expression: and_expression ",
			if( 49 == act_num ) {
				head_token->ast = $(1)->ast;
			}

			//		"inclusive_or_expression: exclusive_or_expression ",
			if( 51 == act_num ) {
				head_token->ast = $(1)->ast;
			}

			//		"logical_and_expression: inclusive_or_expression ",
			if( 53 == act_num ) {
				head_token->ast = $(1)->ast;
			}

			//		"logical_or_expression: logical_and_expression ",
			if( 55 == act_num ) {
				head_token->ast = $(1)->ast;
			}

			//		"conditional_expression: logical_or_expression ",
			if( 57 == act_num ) {
				head_token->ast = $(1)->ast;
			}

			//		"assignment_expression: conditional_expression ",
			if( 59 == act_num ) {
				head_token->ast = $(1)->ast;
			}

			//		"assignment_expression: unary_expression assignment_operator assignment_expression ",
			if( 60 == act_num ) {
				head_token->ast = E_newast($(2)->ast->type, $(3)->ast, $(1)->ast);
			}

			//		"assignment_operator: = ",
			if( 61 == act_num ) {
//				head_token->ast = E_newsinglet(E_ASSIGN_EQL);
				head_token->ast = E_newast(E_ASSIGN_EQL, NULL, NULL);
			}

			//		"expression: assignment_expression ",
			if( 71 == act_num ) {
				head_token->ast = $(1)->ast;
			}

			//		"declaration_specifiers: type_specifier ",
			if( 74 == act_num ) {
				head_token->ast = $(1)->ast;
			}

			//		"declaration: declaration_specifiers init_declarator_list ; ",
			if( 77 == act_num ) {
				head_token->ast = E_newast(E_DECLARATION, $(3)->ast, $(2)->ast);

			}

			//		"init_declarator_list: init_declarator ",
			if( 78 == act_num ) {
				head_token->ast = $(1)->ast;
			}

			//		"init_declarator_list: init_declarator_list , init_declarator ",
			if( 79 == act_num ) {
			}

			//		"init_declarator: declarator ",
			if( 80 == act_num ) {
				//TODO make new node for processing
				head_token->ast = $(1)->ast;
			}

			//		"init_declarator: declarator = initializer ",
			if( 81 == act_num ) {
				head_token->ast = E_newast(E_INIT_DECL, $(3)->ast, $(1)->ast);
			}

			//		"type_specifier: INT ",
			if( 82 == act_num ) {
				head_token->ast = E_newast(E_SPEC, NULL, NULL);
				head_token->ast->m.ch = 0;
				head_token->ast->m.bit.b_int = 1;
//				head_token->ast = E_newspec(E_SPEC);
//				((E_spec)(head_token->ast))->m.bit.b_int = 1;
//				head_token->id = ;
//				head_token->val
			}

			//		"type_specifier: FLOAT ",
			if( 83 == act_num ) { //TODO do real float
				head_token->ast = E_newast(E_SPEC, NULL, NULL);
				head_token->ast->m.ch = 0;
				head_token->ast->m.bit.b_int = 1;
			}

			//		"declarator: direct_declarator ",
			if( 84 == act_num ) {
				head_token->ast = E_newast(E_DECLAR_STORAGE, $(1)->ast, NULL);
			}

			//		"direct_declarator: IDENTIFIER ",
			if( 85 == act_num ) {
//				head_token->ast = (E_ast)E_newstr(E_SYM, val->s);
				head_token->ast = E_newast(E_SYM, NULL, NULL);
				head_token->ast->s = checked_strdup( val->s );
				printf("new id: %s\n", val->s);
				//assert(0);
			}

			//		"direct_declarator: direct_declarator [ assignment_expression ] ", // E_ARRAY
			if( 87 == act_num ) {
				head_token->ast = E_newast(E_ARRAY_BODY, $(4)->ast, $(2)->ast);
//				head_token->ast->pending = TRUE;
			}

			//		"direct_declarator: direct_declarator ( parameter_type_list ) ",
			if( 90 == act_num ) {
				head_token->ast = E_newast(E_FUNC_BODY, $(4)->ast, $(2)->ast);
//				$(4)->ast->isfunc = TRUE;
//				head_token->ast->pending = TRUE;
			}

			//		"direct_declarator: direct_declarator ( ) ",
			if( 92 == act_num ) {
				head_token->ast = $(3)->ast;
//				head_token->ast = E_newast(E_DECLAR_LIST, $(3)->ast, NULL);
			}

			//		"parameter_type_list: parameter_list ",
			if( 93 == act_num ) {
				head_token->ast = $(1)->ast;
			}

			//		"parameter_list: parameter_declaration ",
			if( 95 == act_num ) {
				head_token->ast = $(1)->ast;
			}

			//		"parameter_list: parameter_list , parameter_declaration ",
			if( 96 == act_num ) {
				assert(0);
			}

			//		"parameter_declaration: declaration_specifiers declarator ",
			if( 97 == act_num ) {
				assert(0);
			}

			//		"initializer: assignment_expression ",
			if( 114 == act_num ) {
				head_token->ast = $(1)->ast;
			}

			//		"initializer: { initializer_list } ",
			if( 115 == act_num ) {
				head_token->ast = $(2)->ast;
			}

			//		"initializer_list: initializer ",
			if( 117 == act_num ) {
				head_token->ast = E_newast(E_INIT_VAL_LIST, NULL, $(1)->ast);
			}

			//		"initializer_list: initializer_list , initializer ",
			if( 119 == act_num ) {
				head_token->ast = E_newast(E_INIT_VAL_LIST, $(3)->ast, $(1)->ast);
			}

			//		"statement: compound_statement ",
			if( 126 == act_num ) {
				head_token->ast = $(1)->ast;
			}

			//		"statement: expression_statement ",
			if( 127 == act_num ) {
				head_token->ast = $(1)->ast;
			}

			//		"statement: selection_statement ",
			if( 128 == act_num ) {
				head_token->ast = $(1)->ast;
			}

			//		"statement: iteration_statement ",
			if( 129 == act_num ) {
				head_token->ast = $(1)->ast;
			}

			//		"statement: jump_statement ",
			if( 130 == act_num ) {
				head_token->ast = $(1)->ast;
			}

			//		"compound_statement: { block_item_list } ",
			if( 132 == act_num ) {
				head_token->ast = $(2)->ast;
			}

			//		"block_item_list: block_item ",
			if( 133 == act_num ) {
				head_token->ast = $(1)->ast;
			}

			//		"block_item_list: block_item_list block_item ",
			if( 134 == act_num ) {
				head_token->ast = (E_ast)E_newast(E_BLK_ITM_LIST, $(2)->ast, $(1)->ast);
			}

			//		"block_item: declaration ",
			if( 135 == act_num ) {
				head_token->ast = $(1)->ast;
			}

			//		"block_item: statement ",
			if( 136 == act_num ) {
				head_token->ast = $(1)->ast;
			}

			//		"expression_statement: expression ; ",
			if( 138 == act_num ) {
				head_token->ast = $(2)->ast;
			}

			//		"selection_statement: IF ( expression ) statement ELSE statement ",
			if( 140 == act_num ) {
				head_token->ast = E_newast(E_CONT_IF, NULL, NULL);
				head_token->ast->bool_exp = $(5)->ast;
				head_token->ast->true_stat = $(3)->ast;
				head_token->ast->false_stat = $(1)->ast;
			}

			//		"iteration_statement: FOR ( expression_statement expression_statement expression ) statement ",
			if( 144 == act_num ) {
				head_token->ast = E_newast(E_CONT_FOR, NULL, NULL);
				head_token->ast->init_stat = $(5)->ast;
				head_token->ast->bool_exp = $(4)->ast;
				head_token->ast->step_state = $(3)->ast;
				head_token->ast->ast = $(1)->ast;
			}

			//		"jump_statement: RETURN expression ; ",
			if( 149 == act_num ) {
				head_token->ast = E_newast(E_RET, $(3)->ast, NULL);
			}

			//		"translation_unit: external_declaration ",
			if( 150 == act_num ) {
//				assert($(1));
				head_token->ast = $(1)->ast;
			}

			//		"translation_unit: translation_unit external_declaration ",
			if( 151 == act_num ) {
				assert(0);
			}

			//		"external_declaration: function_definition ",
			if( 152 == act_num ) {
				assert( $(1) );
				head_token->ast = $(1)->ast;
			}

			//		"function_definition: declaration_specifiers declarator compound_statement ",
			if( 155 == act_num ) {
//				head_token->ast = E_newast(E_FUNC, $(3)->ast, $(2)->ast, $(1)->ast);
				head_token->ast = E_newast(E_FUNC, NULL, NULL);
				head_token->ast->spcifiers = $(3)->ast;
				head_token->ast->declarator = $(2)->ast;
				head_token->ast->ast = $(1)->ast;
			}
			/* end */

			/*** clear up ***/
			C_token_list head = NULL;
			for( head = rules[act_num]->list; head; head = head->next ) {
				stack_pop(t_stack);
				stack_pop(s_stack);
			}
			state = stack_top(s_stack);
			act_num = action_table[state]->actions[head_token->id];
			act = action_table[state]->type[head_token->id];
			assert( GOTO == act );

			assert(head_token->ast);
			stack_push(t_stack, head_token);
			stack_push(s_stack, act_num);
		}
		else if( ACC == act ) {
			assert( $(1) );
			S_token s = checked_malloc( sizeof *s );
			s->ast = $(1)->ast;

			/*** translate begins ***/
		    assert( NULL == E_lookup("printf") && NULL == E_lookup("scanf") );
			E_symbol sym = E_install("printf");
			sym->addr = sym->name;
			sym = E_install("scanf");
			sym->addr = sym->name;
			translate(s->ast);
//			printf("body is here: \n%s", s->ast->code);
			string result = checked_malloc( strlen($(1)->ast->code) + 500);
			sprintf( result, 	"\t.file	\"simple1.c\"\n"
								"\t.text\n"
								"%s"
					            "\t.ident	\"GCC: (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3\"\n"
	                            "\t.section\t.note.GNU-stack,\"\",@progbits\n" , s->ast->code);
			printf("LAST !!!\n");
			printf("%s", result);
			FILE * file_out = fopen("result.s", "w");
			fprintf(file_out, "%s", result);
			fclose(file_out);
			printf("Yahoo!!!\n");
			return;
		}
		else {
			if( GOTO == act ) assert(0); // strange
			if( ERROR == act ) {
				printf("STATE: %d\n", stack_top(s_stack));
				printf("Token is: %s\n", tokens_text[stack_top(t_stack)->id]);
				assert(0); // Ooops
			}
			assert(0); // WTF?
		}
	}
	/**
	 *lex的值出来了之后,建立一个相应的S_token,这个作为堆栈里的符号
	 *状态栈里面压的是数字, 然后根据状态栈里的东西,选择action表的项目
	 */
}
Exemplo n.º 10
0
void process_arguments(int argc, char** argv, struct program_options* options)
{
    /*
     * Usage: progname [-h hostname] [-p port] [-t port] ssh_hostname ssh_port
     *
     * Options:
     * -h hostname
     *    sets proxy_host : hostname of both the SOCKS5 proxy *and* the ssh-tunneld process
     * -p port
     *    sets proxy_port : port for the SOCKS5 proxy
     * -t port
     *    sets tun_port : port for the ssh-tunneld process
     *
     * ssh_hostname and ssh_port are set from the remaining values of argv after option
     * processing has completed. These must always be present.
     */
    int opt;

    int set_proxy_host = 0;
    int set_proxy_port = 0;
    int set_tun_port = 0;

    while ((opt = getopt(argc, argv, "h:p:t:")) != -1)
    {
        switch (opt)
        {
            case 'h':
                /* Set proxy host */
                if (! set_proxy_host)
                {
                    options->proxy_host = optarg;
                    set_proxy_host = 1;
                }
                break;
            case 'p':
                if (! set_proxy_port)
                {
                    options->proxy_port = optarg;
                    set_proxy_port = 1;
                }
                break;
            case 't':
                if (! set_tun_port)
                {
                    options->tunnel_port = optarg;
                    set_tun_port = 1;
                }
                break;
            default:
                print_usage(argv[0]);
                exit(EXIT_FAILURE);
                break;
        }
    }
    
    if (optind + 2 > argc)
    {
        print_usage(argv[0]);
        exit(EXIT_FAILURE);
    }
    /* The remaining options should now be the ssh host and port */
    options->remote_host = argv[optind];
    options->remote_port = argv[optind+1];

    /* And set default values if we didn't receive them from the options */
    if (! set_proxy_host)
    {
        char* default_proxy_host = "127.0.0.1";
        options->proxy_host = checked_strdup(default_proxy_host);
    }
    if (! set_proxy_port)
    {
        char* default_proxy_port = "1080";
        options->proxy_port = checked_strdup(default_proxy_port);
    }
    if (! set_tun_port)
    {
        char* default_tun_port = "1081";
        options->tunnel_port = checked_strdup(default_tun_port);
    }
}
Exemplo n.º 11
0
int
main(int argc, char **argv)
{
	int i;
	int opt;
	int threads = -1;
	int cpu;

	char *needle = NULL;
	char *dir = NULL;

	struct kmp_table table;
	struct queue q;
	struct produce_arg parg;
	struct consume_arg carg;

	pthread_t producer;
	pthread_t *consumers;

	while ((opt = getopt(argc, argv, "n:s:d:")) != -1) {
		switch (opt) {
			case 'n':
				threads = atoi(optarg);
				break;
			case 's':
				needle = checked_strdup(optarg);
				break;
			case 'd':
				dir = checked_strdup(optarg);
				break;
			case '?':
			default:
				usage(argv[0]);
				return (1);
		}
	}

	if (!needle || strlen(needle) <= 0) {
		usage(argv[0]);
		return (1);
	}

	if (!dir) {
		dir = checked_malloc(sizeof (char) * 2);
		dir[0] = '.';
		dir[1] = 0;
	}

	if (threads <= 0) {
		cpu = sysconf(_SC_NPROCESSORS_ONLN);
		if (cpu <= 1) {
			threads = 1;
		} else if (cpu >= 20) {
			threads = 20;
		} else {
			threads = cpu;
		}
	}

	alloc_table(&table, needle);
	fill_table(&table);

	alloc_queue(&q, INITIAL_CAPACITY);

	parg.q = &q;
	parg.path = dir;
	parg.consumer_count = threads;

	carg.q = &q;
	carg.t = &table;

	consumers = checked_malloc(sizeof (pthread_t) * threads);

	checked_thread_create(&producer, NULL, &produce, &parg);
	for (i = 0; i < threads; i++) {
		checked_thread_create(consumers + i, NULL, &consume, &carg);
	}

	checked_thread_join(producer, NULL);
	for (i = 0; i < threads; i++) {
		checked_thread_join(consumers[i], NULL);
	}

	free_queue(&q);
	free_table(&table);
	free(consumers);
	free(needle);
	free(dir);

	return (0);
}