/* 
* Return memory used by a trigger
* The command list is free'd when changed and when
* shutting down.
*/
void free_trigger(trig_data *trig) {
	if (trig->name) {
		free(trig->name);
		trig->name = NULL;
	}

	if (trig->arglist) {
		free(trig->arglist);
		trig->arglist = NULL;
	}
	if (trig->var_list) {
		free_varlist(trig->var_list);
		trig->var_list = NULL;
	}
	if (GET_TRIG_WAIT(trig))
		event_cancel(GET_TRIG_WAIT(trig));

	free(trig);
}
Exemplo n.º 2
0
/* save the zone's triggers to internal memory and to disk */
void trigedit_save(struct descriptor_data *d)
{
	char *name=NULL;
	char *trgarglist=NULL;
	char *code=NULL;
	int trig_rnum, i;
	int found = 0;
	char *s;
	trig_data *proto;
	trig_data *trig = OLC_TRIG(d);
	trig_data *live_trig;
	struct cmdlist_element *cmd, *next_cmd;
	struct index_data **new_index;
	struct descriptor_data *dsc;
	int zone, top;
	char buf[MAX_CMD_LENGTH];
	char bitBuf[MAX_INPUT_LENGTH];

	char *trg_replace = "REPLACE INTO %s ("
		"znum, "
		"vnum, "
		"name, "
		"attach_type, "
		"flags, "
		"numarg, "
		"arglist, "
		"code) "
		"VALUES (%d, %d, '%s', %d, '%s', %d, '%s', '%s');";

	if ((trig_rnum = real_trigger(OLC_NUM(d))) != -1) {
		proto = trig_index[trig_rnum]->proto;
		for (cmd = proto->cmdlist; cmd; cmd = next_cmd) { 
			next_cmd = cmd->next;
			if (cmd->cmd)
				free(cmd->cmd);
			free(cmd);
		}

		if (proto->arglist)
			free(proto->arglist);
		if (proto->name)
			free(proto->name);

		/* Recompile the command list from the new script */
		s = OLC_STORAGE(d);

		CREATE(trig->cmdlist, struct cmdlist_element, 1);
		trig->cmdlist->cmd = str_dup(strtok(s, "\n\r"));
		cmd = trig->cmdlist;

		while ((s = strtok(NULL, "\n\r"))) {
			CREATE(cmd->next, struct cmdlist_element, 1);
			cmd = cmd->next;
			cmd->cmd = str_dup(s);
		}

		/* make the prorotype look like what we have */
		trig_data_copy(proto, trig);

		/* go through the mud and replace existing triggers         */
		live_trig = trigger_list;
		while (live_trig)
		{
			if (GET_TRIG_RNUM(live_trig) == trig_rnum) {
				if (live_trig->arglist) {
					free(live_trig->arglist);
					live_trig->arglist = NULL;
				}
				if (live_trig->name) {
					free(live_trig->name);
					live_trig->name = NULL;
				}

				if (proto->arglist)
					live_trig->arglist = str_dup(proto->arglist);
				if (proto->name)
					live_trig->name = str_dup(proto->name);

				live_trig->cmdlist = proto->cmdlist;
				live_trig->curr_state = live_trig->cmdlist;
				live_trig->trigger_type = proto->trigger_type;
				live_trig->attach_type = proto->attach_type;
				live_trig->narg = proto->narg;
				live_trig->data_type = proto->data_type;
				live_trig->depth = 0;
				live_trig->wait_event = NULL;
				if (GET_TRIG_WAIT(live_trig))
					event_cancel(GET_TRIG_WAIT(live_trig));
				free_varlist(live_trig->var_list);
			}

			live_trig = live_trig->next_in_world;
		}
	} else {