コード例 #1
0
ファイル: myshell.c プロジェクト: joeynorkin/cis3207
void shell_loop() {
    char user_in[INPUT_BUFF_LEN];
    char **argv;
    int loop = 1;

    /* variables if bg_exec == 1 */
    pid_t cpid;
    int status = 0;

    do {
        flag_io = 0;
        redir_in = 0;
        redir_out = 0;
        redir_in_index = 0;
        redir_out_index = 0;
        append = 0;
        pipe_child_index = 0;
        bg_exec = 0;

        /* variales if batch == 1 */
        
        if(batch) {
            fgets(user_in, INPUT_BUFF_LEN, batchfp);
            if(feof(batchfp))
                break;
        } else /* read from command line interpreter */ {
            fputs(prompt, stdout);
            fgets(user_in, INPUT_BUFF_LEN, stdin);

            if(feof(stdin)) {
                printf("\n");
                break;
            }
        }

        argv = parse_string(user_in);
        evaluate_flags(argv);

        /* finish shell in child process if bg_exec == 1 */
        if(bg_exec) {
            cpid = fork();
            if (cpid == -1) {
                perror("bg_exec fork");
                free_arg(argv);
            }
            if (cpid == 0) /* child */ {
                loop = shell_execute(argv);
                free_arg(argv);
                exit(1);
            }
        }

        else {
            loop = shell_execute(argv);
            free_arg(argv);
        }
    } while(loop);

}
コード例 #2
0
ファイル: copy.c プロジェクト: alreimer/httpd_nh221
/*if file is *.inc - then parse it by handle_get, otherwise put out without parsing*/
int copy_file_include(char *file, FILE *out)
{
	char *ptr = file;
	int i = 0, ret = 0;
	
	while(*ptr){
	    if(*ptr == '?'){
		*ptr = '\0';
		i = 1;
		break;
	    }
	    ptr++;
	}
	
	if(i){
//here must be arg_handle for ptr+1
	handle_arg(1, ptr+1);
	}

//        if(parsestr1_(file, "/l.linc/") || parsestr1_(file, "/l.lhtm/")){ // 
	if(parsestr1_(file, "/l.l/Binc\\0/\\htm\\0/\\")){		//like: include.inc or incude.htm
	    FILE *infile;
	    char *data;
	    struct stat stbuf;

	    if(stat(file, &stbuf) || (stbuf.st_size>1024*64) || !(infile = fopen(file,"r"))) {
		fprintf(out, "Unable to open HTML file %s, %d\n", file, errno);
		if(i){
		    *ptr = '?';
		    free_arg(1);
		}
		return 1;
	    }

	    data = (char *)malloc(stbuf.st_size+1);
	    if(data){
		fread(data, stbuf.st_size, 1, infile);
		data[stbuf.st_size] = '\0';
		handle_get(data, out);
		free(data);
	    } else {
		fprintf(out, "ERR: Unable allocate memory.\n");
		//ret = 1;//error alocate
	    }
	    fclose(infile);
        } else ret = copy_file(file, out);		//all others will only copied
  if(i){
    *ptr = '?';
    free_arg(1);
  }
  return ret;
}
コード例 #3
0
ファイル: main.c プロジェクト: Morefaya/ft_ls
int			main(int ac, char **av)
{
	t_opt	opt;
	int		index;
	t_list	*lst_f;
	t_list	*lst_d;
	t_sort	pt_arg;

	lst_f = NULL;
	lst_d = NULL;
	index = check_opt(ac, av, &opt);
	if (index == ac)
		ft_ls(".", opt);
	else
	{
		split_arg(&lst_f, &lst_d, av + index, ac - index);
		pt_arg = select_sort(opt);
		sort(&lst_f, pt_arg, opt);
		sort(&lst_d, pt_arg, opt);
		print(lst_f, opt);
		if (lst_f && lst_d)
			ft_putchar('\n');
		opt.lf_dir = 1;
		deal_dir(lst_d, opt, ac, index);
		free_arg(&lst_f, &lst_d);
	}
	return (0);
}
コード例 #4
0
static int filter_event(struct event_filter *filter,
			struct event_format *event,
			const char *filter_str, char **error_str)
{
	struct filter_type *filter_type;
	struct filter_arg *arg;
	int ret;

	if (filter_str) {
		ret = process_event(event, filter_str, &arg, error_str);
		if (ret < 0)
			return ret;

	} else {
		/* just add a TRUE arg */
		arg = allocate_arg();
		arg->type = FILTER_ARG_BOOLEAN;
		arg->boolean.value = FILTER_TRUE;
	}

	filter_type = add_filter_type(filter, event->id);
	if (filter_type->filter)
		free_arg(filter_type->filter);
	filter_type->filter = arg;

	return 0;
}
コード例 #5
0
ファイル: message.c プロジェクト: Xolgrim/server
void msg_free(message * msg)
{
    int i;
    assert(msg->refcount == 0);
    for (i = 0; i != msg->type->nparameters; ++i) {
        free_arg(msg->type->types[i], msg->parameters[i]);
    }
    free((void *)msg->parameters);
    free(msg);
}
コード例 #6
0
ファイル: argument.c プロジェクト: antoinerollin/pc-opencl
/**
 * Free a list of Arguments
 */
void free_arg_list(Argument ** args)
{
    int i = 0;
    while (args[i] != '\0') {
        free_arg(args[i]);
        i++;
    }

    free(args);
}
コード例 #7
0
void reparent_op_arg(struct filter_arg *parent, struct filter_arg *old_child,
		  struct filter_arg *arg)
{
	struct filter_arg *other_child;
	struct filter_arg **ptr;

	if (parent->type != FILTER_ARG_OP &&
	    arg->type != FILTER_ARG_OP)
		die("can not reparent other than OP");

	/* Get the sibling */
	if (old_child->op.right == arg) {
		ptr = &old_child->op.right;
		other_child = old_child->op.left;
	} else if (old_child->op.left == arg) {
		ptr = &old_child->op.left;
		other_child = old_child->op.right;
	} else
		die("Error in reparent op, find other child");

	/* Detach arg from old_child */
	*ptr = NULL;

	/* Check for root */
	if (parent == old_child) {
		free_arg(other_child);
		*parent = *arg;
		/* Free arg without recussion */
		free(arg);
		return;
	}

	if (parent->op.right == old_child)
		ptr = &parent->op.right;
	else if (parent->op.left == old_child)
		ptr = &parent->op.left;
	else
		die("Error in reparent op");
	*ptr = arg;

	free_arg(old_child);
}
コード例 #8
0
static struct filter_arg *
create_arg_item(struct event_format *event, const char *token,
		enum event_type type, char **error_str)
{
	struct format_field *field;
	struct filter_arg *arg;

	arg = allocate_arg();

	switch (type) {

	case EVENT_SQUOTE:
	case EVENT_DQUOTE:
		arg->type = FILTER_ARG_VALUE;
		arg->value.type =
			type == EVENT_DQUOTE ? FILTER_STRING : FILTER_CHAR;
		arg->value.str = strdup(token);
		if (!arg->value.str)
			die("malloc string");
		break;
	case EVENT_ITEM:
		/* if it is a number, then convert it */
		if (isdigit(token[0])) {
			arg->type = FILTER_ARG_VALUE;
			arg->value.type = FILTER_NUMBER;
			arg->value.val = strtoull(token, NULL, 0);
			break;
		}
		/* Consider this a field */
		field = pevent_find_any_field(event, token);
		if (!field) {
			if (strcmp(token, COMM) != 0) {
				/* not a field, Make it false */
				arg->type = FILTER_ARG_BOOLEAN;
				arg->boolean.value = FILTER_FALSE;
				break;
			}
			/* If token is 'COMM' then it is special */
			field = &comm;
		}
		arg->type = FILTER_ARG_FIELD;
		arg->field.field = field;
		break;
	default:
		free_arg(arg);
		show_error(error_str, "expected a value but found %s",
			   token);
		return NULL;
	}
	return arg;
}
コード例 #9
0
ファイル: scanner.c プロジェクト: ybakos/wayland
static void
free_message(struct message *message)
{
	struct arg *a, *a_next;

	free(message->name);
	free(message->uppercase_name);
	free_description(message->description);

	wl_list_for_each_safe(a, a_next, &message->arg_list, link)
		free_arg(a);

	free(message);
}
コード例 #10
0
ファイル: func.c プロジェクト: AlfredArouna/illumos-gate
static void
freereg(struct regnod *regp)
{
	struct regnod 	*sav;

	while (regp)
	{
		free_arg(regp->regptr);
		freetree(regp->regcom);
		sav = regp->regnxt;
		free(regp);
		regp = sav;
	}
}
コード例 #11
0
ファイル: langmon.c プロジェクト: Magister/bjcups-2.50
int main(int ac, char **arv)
{
	int error;

#ifdef DEBUG
	/* logfile=/var/log/log.txt */
	create_log();
	fprintf(log_path, "ac = %d\n", ac);
	fflush(log_path);
#endif

	/* setup signal handlers */
	init_signals();

	/* printer DEVICE ID and command line option check */
	if((error = get_printer_devid()) < 0 || check_arg(ac,arv) < 0){

		/*
		   Not Canon printer
			or
		   "--gui" option not found
		*/
#ifdef DEBUG
		write_log("Now normal printing ......\n");
#endif
		print_normal();
		exit(0);
	}

	/* create semapho and other setup */
	if((error = lm_init(PRNT_PATH))){
		exit(0);
	}

	/* monitor_process/print_process/status_process start */
	lm_main_fork();

	/* delete semapho */
	remove_sem(sem_id);
	/* free memory (status monitor argv string) */
	free_arg();

#ifdef DEBUG
	write_log("LM end \n");
#endif
	exit(0);
}
コード例 #12
0
/* Remove any unknown event fields */
static struct filter_arg *collapse_tree(struct filter_arg *arg)
{
	enum filter_vals ret;

	ret = test_arg(arg, arg);
	switch (ret) {
	case FILTER_VAL_NORM:
		return arg;

	case FILTER_VAL_TRUE:
	case FILTER_VAL_FALSE:
		free_arg(arg);
		arg = allocate_arg();
		arg->type = FILTER_ARG_BOOLEAN;
		arg->boolean.value = ret == FILTER_VAL_TRUE;
	}

	return arg;
}
コード例 #13
0
ファイル: net.c プロジェクト: open-sw/vzctl
int run_net_script(envid_t veid, int op, list_head_t *ip_h, int state,
	int skip_arpdetect)
{
	char *argv[3];
	char *envp[10];
	char *script;
	int ret;
	char buf[STR_SIZE];
	int i = 0;
	char *skip_str = "SKIP_ARPDETECT=yes";

	if (list_empty(ip_h))
		return 0;
	snprintf(buf, sizeof(buf), "VEID=%d", veid);
	envp[i++] = strdup(buf);
	snprintf(buf, sizeof(buf), "VE_STATE=%s", state2str(state));
	envp[i++] = strdup(buf);
	envp[i++] = list2str("IP_ADDR", ip_h);
	envp[i++] = strdup(ENV_PATH);
	if (skip_arpdetect)
		envp[i++] = strdup(skip_str);
	envp[i] = NULL;
	switch (op) {
		case ADD:
			script = VPS_NET_ADD;
			break;
		case DEL:
			script = VPS_NET_DEL;
			break;
		default:
			return 0;
	}
	argv[0] = script;
	argv[1] = NULL;
	ret = run_script(script, argv, envp, 0);
	free_arg(envp);

	return ret;
}
コード例 #14
0
static void free_arg(struct filter_arg *arg)
{
	if (!arg)
		return;

	switch (arg->type) {
	case FILTER_ARG_NONE:
	case FILTER_ARG_BOOLEAN:
		break;

	case FILTER_ARG_NUM:
		free_arg(arg->num.left);
		free_arg(arg->num.right);
		break;

	case FILTER_ARG_EXP:
		free_arg(arg->exp.left);
		free_arg(arg->exp.right);
		break;

	case FILTER_ARG_STR:
		free(arg->str.val);
		regfree(&arg->str.reg);
		free(arg->str.buffer);
		break;

	case FILTER_ARG_VALUE:
		if (arg->value.type == FILTER_STRING ||
		    arg->value.type == FILTER_CHAR)
			free(arg->value.str);
		break;

	case FILTER_ARG_OP:
		free_arg(arg->op.left);
		free_arg(arg->op.right);
	default:
		break;
	}

	free(arg);
}
コード例 #15
0
ファイル: userfw_cmd.c プロジェクト: gelraen/userfw
int
userfw_cmd_dispatch(unsigned char *buf,
		struct socket *so,
		struct thread *td)
{
	int err = 0;
	userfw_module_id_t dst = 0;
	struct userfw_io_header *msg = (struct userfw_io_header *)buf;
	const userfw_modinfo *modinfo = NULL;
	const userfw_cmd_descr *cmdinfo = NULL;
	struct userfw_io_header *cmd, *opcode, *cookie = NULL, *mod_id;
	userfw_arg *parsed_args = NULL;
	int i, cookie_val;
	userfw_cmd_access_check access_checker = NULL;

	if (msg->type != T_CONTAINER || (msg->subtype != ST_MESSAGE && msg->subtype != ST_CMDCALL))
		return EOPNOTSUPP;
	if (msg->length < sizeof(*msg) + sizeof(*cmd))
		return EINVAL;

	if (msg->subtype == ST_CMDCALL)
		cmd = msg;
	else
	{
		cmd = userfw_io_find_block(buf + sizeof(*msg), msg->length - sizeof(*msg), T_CONTAINER, ST_CMDCALL);
		cookie = userfw_io_find_block(buf + sizeof(*msg), msg->length - sizeof(*msg), T_UINT32, ST_COOKIE);
	}

	if (cmd == NULL || (cmd != msg && !BLOCK_FITS_INTO_OUTER(cmd, msg)))
		return EINVAL;
	if (cookie != NULL && (!BLOCK_FITS_INTO_OUTER(cookie, msg) || cookie->length != sizeof(*cookie) + sizeof(uint32_t)))
		return EINVAL;

	mod_id = userfw_io_find_block((char*)cmd + sizeof(*cmd), cmd->length - sizeof(*cmd), T_UINT32, ST_MOD_ID);
	if (mod_id == NULL || ! BLOCK_FITS_INTO_OUTER(mod_id, cmd) ||
		mod_id->length != sizeof(*mod_id) + sizeof(uint32_t))
		return EINVAL;

	dst = *((uint32_t*)((char*)mod_id + sizeof(*mod_id)));
	modinfo = userfw_mod_find(dst);
	if (modinfo == NULL)
		return ECONNREFUSED;

	opcode = userfw_io_find_block((char*)cmd + sizeof(*cmd), cmd->length - sizeof(*cmd), T_UINT32, ST_OPCODE);
	if (opcode == NULL || ! BLOCK_FITS_INTO_OUTER(opcode, cmd) ||
		opcode->length != sizeof(*opcode) + sizeof(uint32_t))
		return EINVAL;

	cmdinfo = userfw_mod_find_cmd(dst, *((uint32_t*)((char*)opcode + sizeof(*opcode))));
	if (cmdinfo == NULL)
		return EINVAL;

	parsed_args = malloc(sizeof(userfw_arg)*(cmdinfo->nargs),
				M_USERFW, M_WAITOK | M_ZERO);

	err = parse_arg_list((unsigned char *)cmd + sizeof(*cmd), cmd->length - sizeof(*cmd),
				parsed_args, cmdinfo->nargs, cmdinfo->arg_types);

	access_checker = cmdinfo->is_allowed;
	if (access_checker == NULL)
		access_checker = userfw_cmd_access_only_root;

	if (err == 0)
		err = (*access_checker)(dst, cmdinfo, parsed_args, so, td) ? 0 : EPERM;

	cookie_val = cookie != NULL ? (*((uint32_t*)((char*)cookie + sizeof(*cookie)))) : 0;

	if (err != 0)
		userfw_msg_reply_error(so, cookie_val, err);

	if (err == 0)
		err = cmdinfo->do_cmd(*((uint32_t*)((char*)opcode + sizeof(*opcode))),
				cookie_val, parsed_args, so, td);

	for(i = 0; i < cmdinfo->nargs; i++)
	{
		if (parsed_args[i].type != T_INVAL)
			free_arg(&(parsed_args[i]), M_USERFW);
	}

	free(parsed_args, M_USERFW);

	return 0;
}
コード例 #16
0
static void free_filter_type(struct filter_type *filter_type)
{
	free_arg(filter_type->filter);
}
コード例 #17
0
ファイル: func.c プロジェクト: AlfredArouna/illumos-gate
void
freetree(struct trenod *t)
{
	if (t)
	{
		int type;

		type = t->tretyp & COMMSK;

		switch (type)
		{
			case TFND: {
				struct fndnod *f = fndptr(t);

				if (f->fndref > 0) {
					f->fndref--;
					return;
				}
				free(f->fndnam);
				freetree(f->fndval);
				break;
			}

			case TCOM:
				freeio(comptr(t)->comio);
				free_arg(comptr(t)->comarg);
				free_arg(comptr(t)->comset);
				break;

			case TFORK:
				freeio(forkptr(t)->forkio);
				freetree(forkptr(t)->forktre);
				break;

			case TPAR:
				freetree(parptr(t)->partre);
				break;

			case TFIL:
			case TLST:
			case TAND:
			case TORF:
				freetree(lstptr(t)->lstlef);
				freetree(lstptr(t)->lstrit);
				break;

			case TFOR:
			{
				struct fornod *f = (struct fornod *)t;

				free(f->fornam);
				freetree(f->fortre);
				if (f->forlst)
				{
					freeio(f->forlst->comio);
					free_arg(f->forlst->comarg);
					free_arg(f->forlst->comset);
					free(f->forlst);
				}
			}
			break;

			case TWH:
			case TUN:
				freetree(whptr(t)->whtre);
				freetree(whptr(t)->dotre);
				break;

			case TIF:
				freetree(ifptr(t)->iftre);
				freetree(ifptr(t)->thtre);
				freetree(ifptr(t)->eltre);
				break;

			case TSW:
				free(swptr(t)->swarg);
				freereg(swptr(t)->swlst);
				break;
		}
		free(t);
	}
}
コード例 #18
0
static int add_right(struct filter_arg *op, struct filter_arg *arg,
		     char **error_str)
{
	struct filter_arg *left;
	char *str;
	int op_type;
	int ret;

	switch (op->type) {
	case FILTER_ARG_EXP:
		if (op->exp.right)
			goto out_fail;
		op->exp.right = arg;
		break;

	case FILTER_ARG_OP:
		if (op->op.right)
			goto out_fail;
		op->op.right = arg;
		break;

	case FILTER_ARG_NUM:
		if (op->op.right)
			goto out_fail;
		/*
		 * The arg must be num, str, or field
		 */
		switch (arg->type) {
		case FILTER_ARG_VALUE:
		case FILTER_ARG_FIELD:
			break;
		default:
			show_error(error_str,
				   "Illegal rvalue");
			return -1;
		}

		/*
		 * Depending on the type, we may need to
		 * convert this to a string or regex.
		 */
		switch (arg->value.type) {
		case FILTER_CHAR:
			/*
			 * A char should be converted to number if
			 * the string is 1 byte, and the compare
			 * is not a REGEX.
			 */
			if (strlen(arg->value.str) == 1 &&
			    op->num.type != FILTER_CMP_REGEX &&
			    op->num.type != FILTER_CMP_NOT_REGEX) {
				arg->value.type = FILTER_NUMBER;
				goto do_int;
			}
			/* fall through */
		case FILTER_STRING:

			/* convert op to a string arg */
			op_type = op->num.type;
			left = op->num.left;
			str = arg->value.str;

			/* reset the op for the new field */
			memset(op, 0, sizeof(*op));

			/*
			 * If left arg was a field not found then
			 * NULL the entire op.
			 */
			if (left->type == FILTER_ARG_BOOLEAN) {
				free_arg(left);
				free_arg(arg);
				op->type = FILTER_ARG_BOOLEAN;
				op->boolean.value = FILTER_FALSE;
				break;
			}

			/* Left arg must be a field */
			if (left->type != FILTER_ARG_FIELD) {
				show_error(error_str,
					   "Illegal lvalue for string comparison");
				return -1;
			}

			/* Make sure this is a valid string compare */
			switch (op_type) {
			case FILTER_CMP_EQ:
				op_type = FILTER_CMP_MATCH;
				break;
			case FILTER_CMP_NE:
				op_type = FILTER_CMP_NOT_MATCH;
				break;

			case FILTER_CMP_REGEX:
			case FILTER_CMP_NOT_REGEX:
				ret = regcomp(&op->str.reg, str, REG_ICASE|REG_NOSUB);
				if (ret) {
					show_error(error_str,
						   "RegEx '%s' did not compute",
						   str);
					return -1;
				}
				break;
			default:
				show_error(error_str,
					   "Illegal comparison for string");
				return -1;
			}

			op->type = FILTER_ARG_STR;
			op->str.type = op_type;
			op->str.field = left->field.field;
			op->str.val = strdup(str);
			if (!op->str.val)
				die("malloc string");
			/*
			 * Need a buffer to copy data for tests
			 */
			op->str.buffer = malloc_or_die(op->str.field->size + 1);
			/* Null terminate this buffer */
			op->str.buffer[op->str.field->size] = 0;

			/* We no longer have left or right args */
			free_arg(arg);
			free_arg(left);

			break;

		case FILTER_NUMBER:

 do_int:
			switch (op->num.type) {
			case FILTER_CMP_REGEX:
			case FILTER_CMP_NOT_REGEX:
				show_error(error_str,
					   "Op not allowed with integers");
				return -1;

			default:
				break;
			}

			/* numeric compare */
			op->num.right = arg;
			break;
		default:
			goto out_fail;
		}
		break;
	default:
		goto out_fail;
	}

	return 0;

 out_fail:
	show_error(error_str,
		   "Syntax error");
	return -1;
}
コード例 #19
0
static int
process_filter(struct event_format *event, struct filter_arg **parg,
	       char **error_str, int not)
{
	enum event_type type;
	char *token = NULL;
	struct filter_arg *current_op = NULL;
	struct filter_arg *current_exp = NULL;
	struct filter_arg *left_item = NULL;
	struct filter_arg *arg = NULL;
	enum op_type op_type;
	enum filter_op_type btype;
	enum filter_exp_type etype;
	enum filter_cmp_type ctype;
	int ret;

	*parg = NULL;

	do {
		free(token);
		type = read_token(&token);
		switch (type) {
		case EVENT_SQUOTE:
		case EVENT_DQUOTE:
		case EVENT_ITEM:
			arg = create_arg_item(event, token, type, error_str);
			if (!arg)
				goto fail;
			if (!left_item)
				left_item = arg;
			else if (current_exp) {
				ret = add_right(current_exp, arg, error_str);
				if (ret < 0)
					goto fail;
				left_item = NULL;
				/* Not's only one one expression */
				if (not) {
					arg = NULL;
					if (current_op)
						goto fail_print;
					free(token);
					*parg = current_exp;
					return 0;
				}
			} else
				goto fail_print;
			arg = NULL;
			break;

		case EVENT_DELIM:
			if (*token == ',') {
				show_error(error_str,
					   "Illegal token ','");
				goto fail;
			}

			if (*token == '(') {
				if (left_item) {
					show_error(error_str,
						   "Open paren can not come after item");
					goto fail;
				}
				if (current_exp) {
					show_error(error_str,
						   "Open paren can not come after expression");
					goto fail;
				}

				ret = process_filter(event, &arg, error_str, 0);
				if (ret != 1) {
					if (ret == 0)
						show_error(error_str,
							   "Unbalanced number of '('");
					goto fail;
				}
				ret = 0;

				/* A not wants just one expression */
				if (not) {
					if (current_op)
						goto fail_print;
					*parg = arg;
					return 0;
				}

				if (current_op)
					ret = add_right(current_op, arg, error_str);
				else
					current_exp = arg;

				if (ret < 0)
					goto fail;

			} else { /* ')' */
				if (!current_op && !current_exp)
					goto fail_print;

				/* Make sure everything is finished at this level */
				if (current_exp && !check_op_done(current_exp))
					goto fail_print;
				if (current_op && !check_op_done(current_op))
					goto fail_print;

				if (current_op)
					*parg = current_op;
				else
					*parg = current_exp;
				return 1;
			}
			break;

		case EVENT_OP:
			op_type = process_op(token, &btype, &ctype, &etype);

			/* All expect a left arg except for NOT */
			switch (op_type) {
			case OP_BOOL:
				/* Logic ops need a left expression */
				if (!current_exp && !current_op)
					goto fail_print;
				/* fall through */
			case OP_NOT:
				/* logic only processes ops and exp */
				if (left_item)
					goto fail_print;
				break;
			case OP_EXP:
			case OP_CMP:
				if (!left_item)
					goto fail_print;
				break;
			case OP_NONE:
				show_error(error_str,
					   "Unknown op token %s", token);
				goto fail;
			}

			ret = 0;
			switch (op_type) {
			case OP_BOOL:
				arg = create_arg_op(btype);
				if (current_op)
					ret = add_left(arg, current_op);
				else
					ret = add_left(arg, current_exp);
				current_op = arg;
				current_exp = NULL;
				break;

			case OP_NOT:
				arg = create_arg_op(btype);
				if (current_op)
					ret = add_right(current_op, arg, error_str);
				if (ret < 0)
					goto fail;
				current_exp = arg;
				ret = process_filter(event, &arg, error_str, 1);
				if (ret < 0)
					goto fail;
				ret = add_right(current_exp, arg, error_str);
				if (ret < 0)
					goto fail;
				break;

			case OP_EXP:
			case OP_CMP:
				if (op_type == OP_EXP)
					arg = create_arg_exp(etype);
				else
					arg = create_arg_cmp(ctype);

				if (current_op)
					ret = add_right(current_op, arg, error_str);
				if (ret < 0)
					goto fail;
				ret = add_left(arg, left_item);
				if (ret < 0) {
					arg = NULL;
					goto fail_print;
				}
				current_exp = arg;
				break;
			default:
				break;
			}
			arg = NULL;
			if (ret < 0)
				goto fail_print;
			break;
		case EVENT_NONE:
			break;
		default:
			goto fail_print;
		}
	} while (type != EVENT_NONE);

	if (!current_op && !current_exp)
		goto fail_print;

	if (!current_op)
		current_op = current_exp;

	current_op = collapse_tree(current_op);

	*parg = current_op;

	return 0;

 fail_print:
	show_error(error_str, "Syntax error");
 fail:
	free_arg(current_op);
	free_arg(current_exp);
	free_arg(arg);
	free(token);
	return -1;
}
コード例 #20
0
static void load_syntax_from_file(char *dirname,char *filename,char *whole_chars_left,char *whole_chars_right)
{
  char include_file[MAX_PATH],include_full_file[MAX_PATH];
  HANDLE f=CreateFileA(filename,GENERIC_READ,0,NULL,OPEN_EXISTING,0,NULL);
  if(f!=INVALID_HANDLE_VALUE)
  {
    char *line; int size;
    while(true)
    {
      int res=read_line(f,&line,&size);
      if(res<2)
      {
        line[size]=0;
        char **argv; int argc;
        argc=get_arg(line,&argv);
        free(line);
        if(argc>0)
        {
          if((!_stricmp(argv[0],"include"))&&(argc>1))
          {
            unsigned long res=ExpandEnvironmentStringsA(argv[1],include_file,sizeof(include_file));
            if(!(res&&(res<=sizeof(include_file))))
              strcpy(include_file,argv[1]);
            if((strlen(include_file)>2)&&(((include_file[1]==':')&&((include_file[2]=='\\')||(include_file[2]=='/')))||((include_file[0]=='\\')&&(include_file[1]=='\\'))))
              strcpy(include_full_file,include_file);
            else
            {
              strcpy(include_full_file,dirname);
              strcat(include_full_file,include_file);
            }
            load_syntax_from_file(dirname,include_full_file,whole_chars_left,whole_chars_right);
          }
          if((!_stricmp(argv[0],"file"))&&(argc>2))
          {
            Rules *new_rules=(Rules *)realloc(rules,(rules_count+1)*sizeof(Rules));
            if(!new_rules) goto line_end;
            rules=new_rules;
            rules[rules_count].name=(TCHAR*)malloc((strlen(argv[1])+1)*sizeof(TCHAR));
            if(!rules[rules_count].name) goto line_end;
            MultiByteToWideChar(CP_OEMCP,0,argv[1],-1,rules[rules_count].name,strlen(argv[1])+1);
            rules[rules_count].mask=(TCHAR*)malloc((strlen(argv[2])+1)*sizeof(TCHAR));
            if(!rules[rules_count].mask) goto line_end;
            MultiByteToWideChar(CP_OEMCP,0,argv[2],-1,rules[rules_count].mask,strlen(argv[2])+1);
            if(argc>3)
            {
              rules[rules_count].start=(TCHAR*)malloc((strlen(argv[3])+1)*sizeof(TCHAR));
              if(!rules[rules_count].start) goto line_end;
              MultiByteToWideChar(CP_OEMCP,0,argv[3],-1,rules[rules_count].start,strlen(argv[3])+1);
            }
            rules[rules_count].contexts=NULL;
            rules[rules_count].contexts_count=0;
            rules_count++;
            string_to_whole(whole_chars_left,DEFAULT_WHOLE);
            string_to_whole(whole_chars_right,DEFAULT_WHOLE);
          }
          else if((!_stricmp(argv[0],"wholechars"))&&(argc>2))
          {
            if(!_stricmp(argv[1],"left"))
              string_to_whole(whole_chars_left,argv[2]);
            else if(!_stricmp(argv[1],"right"))
              string_to_whole(whole_chars_right,argv[2]);
          }
          else if(rules_count)
          {
            if(!_stricmp(argv[0],"context"))
            {
              if(!(rules[rules_count-1].contexts_count))
              {
                if(argc==1) goto line_end;
                if(_stricmp(argv[1],"default")) goto line_end;
                rules[rules_count-1].contexts=(Context *)malloc(sizeof(Context));
                rules[rules_count-1].contexts->left=NULL;
                rules[rules_count-1].contexts->right=NULL;
                rules[rules_count-1].contexts->color.Flags=ABCF_4BIT;
                rules[rules_count-1].contexts->color.ForegroundColor=0;
                rules[rules_count-1].contexts->color.BackgroundColor=0;
                rules[rules_count-1].contexts->color.Reserved=NULL;
                rules[rules_count-1].contexts->color.ForegroundDefault=true;
                rules[rules_count-1].contexts->color.BackgroundDefault=true;
                rules[rules_count-1].contexts->exclusive=0;
                rules[rules_count-1].contexts->exclusive_left=-1;
                rules[rules_count-1].contexts->exclusive_right=-1;
                rules[rules_count-1].contexts->line_start_left=0;
                rules[rules_count-1].contexts->line_start_right=0;
                rules[rules_count-1].contexts->whole_chars_left=NULL;
                rules[rules_count-1].contexts->whole_chars_right=NULL;
                rules[rules_count-1].contexts->keywords=NULL;
                rules[rules_count-1].contexts->keywords_count=0;
                if(argc>2)
                {
                  rules[rules_count-1].contexts->color.ForegroundColor=color_by_name(argv[2]);
                  rules[rules_count-1].contexts->color.ForegroundDefault=false;
                }
                if(argc>3)
                {
                  rules[rules_count-1].contexts->color.BackgroundColor=color_by_name(argv[3]);
                  rules[rules_count-1].contexts->color.BackgroundDefault=false;
                }
                rules[rules_count-1].contexts_count=1;
              }
              else
              {
                int key_count=1,whole_left=0,whole_right=0,exclusive=0,line_start_left=0,line_start_right=0;
                char *left,*right;
                if(argc<(key_count+2)) goto line_end;
                if(!_stricmp(argv[key_count],"exclusive"))
                {
                  key_count++;
                  exclusive=1;
                }
                if(argc<(key_count+2)) goto line_end;
                if(!_stricmp(argv[key_count],"whole"))
                {
                  key_count++;
                  whole_left=whole_right=1;
                }
                else if(!_stricmp(argv[key_count],"wholeleft"))
                {
                  key_count++;
                  whole_left=1;
                }
                else if(!_stricmp(argv[key_count],"wholeright"))
                {
                  key_count++;
                  whole_right=1;
                }
                if(argc<(key_count+2)) goto line_end;
                if(!_stricmp(argv[key_count],"linestart"))
                {
                  key_count++;
                  line_start_left=1;
                }
                if(argc<(key_count+2)) goto line_end;
                left=argv[key_count++];
                if(!_stricmp(argv[key_count],"linestart"))
                {
                  key_count++;
                  line_start_right=1;
                }
                if(argc<(key_count+1)) goto line_end;
                right=argv[key_count++];
                Context *NewContexts=(Context *)realloc(rules[rules_count-1].contexts,(rules[rules_count-1].contexts_count+1)*sizeof(Context));
                if(NewContexts)
                {
                  rules[rules_count-1].contexts=NewContexts;
                  NewContexts=rules[rules_count-1].contexts+rules[rules_count-1].contexts_count;
                  NewContexts->left=(char *)malloc(strlen(left)+1);
                  if(NewContexts->left)
                    strcpy(NewContexts->left,left);
                  NewContexts->right=(char *)malloc(strlen(right)+1);
                  if(NewContexts->right)
                    strcpy(NewContexts->right,right);
                  NewContexts->color=rules[rules_count-1].contexts[0].color;
                  NewContexts->exclusive=exclusive;
                  NewContexts->exclusive_left=-1;
                  NewContexts->exclusive_right=-1;
                  NewContexts->line_start_left=line_start_left;
                  NewContexts->line_start_right=line_start_right;
                  NewContexts->whole_chars_left=NULL;
                  if(whole_left)
                  {
                    NewContexts->whole_chars_left=(char *)malloc(WHOLE_SIZE);
                    if(NewContexts->whole_chars_left)
                      memcpy(NewContexts->whole_chars_left,whole_chars_left,WHOLE_SIZE);
                  }
                  NewContexts->whole_chars_right=NULL;
                  if(whole_left)
                  {
                    NewContexts->whole_chars_right=(char *)malloc(WHOLE_SIZE);
                    if(NewContexts->whole_chars_right)
                      memcpy(NewContexts->whole_chars_right,whole_chars_right,WHOLE_SIZE);
                  }
                  NewContexts->keywords=NULL;
                  NewContexts->keywords_count=0;
                  if(argc>key_count)
                  {
                    NewContexts->color.ForegroundColor=color_by_name(argv[key_count++]);
                    NewContexts->color.ForegroundDefault=false;
                  }
                  if(argc>key_count)
                  {
                    NewContexts->color.BackgroundColor=color_by_name(argv[key_count++]);
                    NewContexts->color.BackgroundDefault=false;
                  }
                  rules[rules_count-1].contexts_count++;
                }
              }
            }
            else if(!_stricmp(argv[0],"keyword"))
            {
              if(rules[rules_count-1].contexts_count)
              {
                int key_count=1,whole_left=0,whole_right=0,line_start=0,recursive=0;
                char *keyword;
                if(argc<(key_count+1)) goto line_end;
                if(!_stricmp(argv[key_count],"whole"))
                {
                  key_count++;
                  whole_left=whole_right=1;
                }
                else if(!_stricmp(argv[key_count],"wholeleft"))
                {
                  key_count++;
                  whole_left=1;
                }
                else if(!_stricmp(argv[key_count],"wholeright"))
                {
                  key_count++;
                  whole_right=1;
                }
                if(argc<(key_count+1)) goto line_end;
                if(!_stricmp(argv[key_count],"linestart"))
                {
                  key_count++;
                  line_start=1;
                }
                if(argc<(key_count+1)) goto line_end;
                if(!_stricmp(argv[key_count],"recursive"))
                {
                  key_count++;
                  recursive=1;
                }
                if(argc<(key_count+1)) goto line_end;
                keyword=argv[key_count++];
                Keyword *NewKeyword=(Keyword *)realloc(rules[rules_count-1].contexts[rules[rules_count-1].contexts_count-1].keywords,(rules[rules_count-1].contexts[rules[rules_count-1].contexts_count-1].keywords_count+1)*sizeof(Keyword));
                if(NewKeyword)
                {
                  rules[rules_count-1].contexts[rules[rules_count-1].contexts_count-1].keywords=NewKeyword;
                  NewKeyword=rules[rules_count-1].contexts[rules[rules_count-1].contexts_count-1].keywords+rules[rules_count-1].contexts[rules[rules_count-1].contexts_count-1].keywords_count;
                  NewKeyword->keyword=(char *)malloc(strlen(keyword)+1);
                  if(NewKeyword->keyword)
                    strcpy(NewKeyword->keyword,keyword);
                  NewKeyword->line_start=line_start;
                  NewKeyword->color=rules[rules_count-1].contexts[rules[rules_count-1].contexts_count-1].color;
                  NewKeyword->exclusive=0;
                  NewKeyword->recursive=recursive;
                  NewKeyword->whole_chars_left=NULL;
                  if(whole_left)
                  {
                    NewKeyword->whole_chars_left=(char *)malloc(WHOLE_SIZE);
                    if(NewKeyword->whole_chars_left)
                      memcpy(NewKeyword->whole_chars_left,whole_chars_left,WHOLE_SIZE);
                  }
                  NewKeyword->whole_chars_right=NULL;
                  if(whole_right)
                  {
                    NewKeyword->whole_chars_right=(char *)malloc(WHOLE_SIZE);
                    if(NewKeyword->whole_chars_right)
                      memcpy(NewKeyword->whole_chars_right,whole_chars_right,WHOLE_SIZE);
                  }
                  if(argc>key_count)
                  {
                    NewKeyword->color.ForegroundColor=color_by_name(argv[key_count++]);
                    NewKeyword->color.ForegroundDefault=false;
                  }
                  if(argc>key_count)
                  {
                    NewKeyword->color.BackgroundColor=color_by_name(argv[key_count++]);
                    NewKeyword->color.BackgroundDefault=false;
                  }
                  rules[rules_count-1].contexts[rules[rules_count-1].contexts_count-1].keywords_count++;
                }
              }
            }
          }
        }
line_end:
        free_arg(argv,argc);
        if(res)
          break;
      }
      else //error
      {
        break;
      }
    }
    CloseHandle(f);
  }
}