示例#1
0
文件: addmult.c 项目: Tlf/tlf
/* get best matching lenght of of name or aliaslist of mult 'n' in 'str' */
unsigned int get_matching_length(char *str, unsigned int n) {
    unsigned len = 0;

    if (strstr(str, get_mult(n)) != NULL) {
	len = strlen(get_mult(n));
    }

    for (int i = 0; i < g_slist_length(get_aliases(n)); i++) {
	char *tmp =g_slist_nth_data(get_aliases(n), i);
	if (strstr(str, tmp) != NULL) {
	    if (strlen(tmp) >= len)
		len = strlen(tmp);
	}
    }
    return len;
}
	//returns true if the command exists.
	bool help_command(const std::string& acmd)
	{
		std::string cmd = get_actual_cmd(acmd);
		const command* c = get_command(cmd);
		if (c) {
			std::stringstream ss;
			ss << cmd_prefix_ << cmd;
			if (c->help.empty() && c->usage.empty()) {
				ss << _(" No help available.");
			}
			else {
				ss << " - " << c->help;
			}
			if (!c->usage.empty()) {
				ss << " " << _("Usage:") << " " << cmd_prefix_ << cmd << " " << c->usage;
			}
			ss << get_command_flags_description(*c);
			const std::vector<std::string> l = get_aliases(cmd);
			if (!l.empty()) {
				ss << " (" << _("aliases:") << " " << utils::join(l, " ") << ")";
			}
			print(_("help"), ss.str());
		}
		return c != 0;
	}
示例#3
0
int
rotz_rem_alias(rotz_t ctx, const char *alias)
{
	size_t aliaz = strlen(alias);
	const_buf_t al;
	const char *ap;
	rtz_vtxkey_t akey;
	rtz_vtx_t aid;

	/* first check if V is actually there */
	if (!(aid = get_vertex(ctx, alias, aliaz))) {
		/* nothing to be done */
		return 0;
	}
	/* now remove that alias from the alias list */
	if ((al = get_aliases(ctx, akey = rtz_vtxkey(aid))).d == NULL ||
	    UNLIKELY((ap = find_in_buf(al, alias, aliaz)) == NULL)) {
		/* alias is already removed innit? */
		;
	} else if (UNLIKELY((al = rem_from_buf(al, ap, aliaz + 1)).d == NULL)) {
		/* huh? */
		return -1;
	} else {
		/* just reassing the list with the tag removed */
		add_akalst(ctx, akey, al);
	}
	unput_vertex(ctx, alias, aliaz);
	return 0;
}
示例#4
0
int
rotz_add_alias(rotz_t ctx, rtz_vtx_t v, const char *alias)
{
	size_t aliaz = strlen(alias);
	const_buf_t al;
	rtz_vtxkey_t akey;
	rtz_vtx_t tmp;

	/* first check if V is already there, if not get an id and add that */
	if ((tmp = get_vertex(ctx, alias, aliaz)) && tmp != v) {
		/* alias points to a different vertex already
		 * we return -2 here to indicate this, so that callers that
		 * meant to combine 2 tags can use rotz_combine() */
		return -2;
	} else if (UNLIKELY(tmp)) {
		/* ah, tmp == v, don't bother putting it in again */
		;
	} else if (UNLIKELY(put_vertex(ctx, alias, aliaz, v) < 0)) {
		return -1;
	}
	/* check aliases */
	if ((al = get_aliases(ctx, akey = rtz_vtxkey(v))).d != NULL &&
	    UNLIKELY(find_in_buf(al, alias, aliaz) != NULL)) {
		/* alias is already in the list */
		return 0;
	} else if (UNLIKELY(add_alias(ctx, akey, alias, aliaz) < 0)) {
		return -1;
	}
	return 1;
}
示例#5
0
文件: alias.c 项目: wfp5p/elm
/* FIXME nothing actually pay attention to these return values */
int open_alias_files(int are_in_aliases)
{
	if(open_system_aliases() || open_user_aliases()) {
	    dprint(5, (debugfile,
		      "Reading alias data files...\n"));
	    get_aliases(are_in_aliases);
	    return 0;
	}
	return 1;
}
示例#6
0
const char*
rotz_get_name(rotz_t ctx, rtz_vtx_t v)
{
	static char *nmspc;
	static size_t nmspcz;
	rtz_vtxkey_t vkey = rtz_vtxkey(v);
	const_buf_t buf;

	if (UNLIKELY((buf = get_aliases(ctx, vkey)).d == NULL)) {
		return 0;
	}
	/* we're interested in the first name only */
	buf.z = strlen(buf.d);
	if (UNLIKELY(buf.z >= nmspcz)) {
		nmspcz = ((buf.z / 64U) + 1U) * 64U;
		nmspc = realloc(nmspc, nmspcz);
	}
	memcpy(nmspc, buf.d, buf.z);
	nmspc[buf.z] = '\0';
	return nmspc;
}
示例#7
0
static const_buf_t
rem_from_buf(const_buf_t b, const char *s, size_t z)
{
	static char *akaspc;
	static size_t akaspz;
	char *ap;

	if (UNLIKELY(s < b.d || s + z > b.d + b.z)) {
		/* definitely not in our array */
		return b;
	} else if (UNLIKELY(b.z > akaspz)) {
		akaspz = ((b.z - 1) / 64U + 1U) * 64U;
		akaspc = realloc(akaspc, akaspz);
	}
	/* S points to the element to delete */
	ap = akaspc;
	if (s > b.d) {
		/* cpy the stuff before S */
		memcpy(ap, b.d, (s - b.d));
		ap += s - b.d;
	}
	if (s - b.d + z < b.z) {
		/* cpy the stuff after S */
		b.z -= s - b.d + z;
		memcpy(ap, s + z, b.z);
		ap += b.z;
	}
	return (const_buf_t){.z = ap - akaspc, .d = akaspc};
}

static rtz_buf_t
get_name_r(rotz_t cp, rtz_vtxkey_t svtx)
{
	const_buf_t cb;

	if (UNLIKELY((cb = get_aliases(cp, svtx)).d == NULL)) {
		return (rtz_buf_t){0U};
	}
	/* we're interested in the first name only */
	cb.z = strlen(cb.d);
	return (rtz_buf_t){.z = cb.z, .d = strndup(cb.d, cb.z)};
}

static int
add_vertex(rotz_t cp, const char *v, size_t z, rtz_vtx_t i)
{
	if (UNLIKELY(put_vertex(cp, v, z, i) < 0)) {
		return -1;
	}
	/* act as though we're renaming the vertex */
	return rnm_vertex(cp, rtz_vtxkey(i), v, z);
}

static int
rem_vertex(rotz_t cp, rtz_vtx_t i, const char *v, size_t z)
{
	rtz_vtxkey_t vkey = rtz_vtxkey(i);
	const_buf_t al;
	int res = 0;

	/* get all them aliases */
	if (LIKELY((al = get_aliases(cp, vkey)).d != NULL)) {
		/* go through all names in the alias list */
		for (const char *x = al.d, *const ex = al.d + al.z;
		     x < ex; x += z + 1) {
			z = strlen(x);
			res += unput_vertex(cp, x, z);
		}
	} else {
		/* just to be sure */
		res += unput_vertex(cp, v, z);
	}
	res += unrnm_vertex(cp, rtz_vtxkey(i));
	return res;
}