Esempio n. 1
0
/**
 * Get the raw message name present at the start of a G2 packet.
 *
 * @param start		start of message
 * @param len		amount of consecutive bytes we have so far
 *
 * @return the message name if we can intuit it, an empty string otherwise.
 */
const char *
g2_msg_raw_name(const void *start, size_t len)
{
	const char *name;
	size_t namelen;
	char buf[G2_FRAME_NAME_LEN_MAX + 1];

	name = g2_frame_name(start, len, &namelen);
	if (NULL == name)
		return "";

	clamp_strncpy(buf, sizeof buf, name, namelen);
	return constant_str(buf);
}
Esempio n. 2
0
/**
 * Needs brief description here.
 *
 * Substitutes variables from string:
 *
 * - The leading "~" is replaced by the home directory.
 * - Variables like "$PATH" or "${PATH}" are replaced by their value, as
 *   fetched from the environment, or the empty string if not found.
 *
 * If given a NULL input, we return NULL.
 *
 * @return string constant, which is not meant to be freed until exit time.
 */
const char *
eval_subst(const char *str)
{
	char buf[MAX_STRING];
	char *end = &buf[sizeof(buf)];
	char *p;
	size_t len;
	char c;

	if (str == NULL)
		return NULL;

	len = g_strlcpy(buf, str, sizeof buf);
	if (len >= sizeof buf) {
		g_warning("%s(): string too large for substitution (%zu bytes)",
			G_STRFUNC, len);
		return constant_str(str);
	}


	if (common_dbg > 3)
		g_debug("%s: on entry: \"%s\"", G_STRFUNC, buf);

	for (p = buf, c = *p++; c; c = *p++) {
		const char *val = NULL;
		char *start = p - 1;

		switch (c) {
		case '~':
			if (start == buf && ('\0' == buf[1] || '/' == buf[1])) {
				/* Leading ~ only */
				val = gethomedir();
				g_assert(val);
				memmove(start, &start[1], len - (start - buf));
				len--;

				g_assert(size_is_non_negative(len));
			}
			break;
		case '$':
			{
				const char *after;

				val = get_variable(p, &after);
				g_assert(val);
				memmove(start, after, len + 1 - (after - buf));
				len -= after - start;		/* Also removing leading '$' */

				g_assert(size_is_non_negative(len));
			}
			break;
		}

		if (val != NULL) {
			char *next;

			next = insert_value(val, start, start - buf, len, sizeof buf - 1);
			len += next - start;
			p = next;

			g_assert(len < sizeof buf);
			g_assert(p < end);
		}

		g_assert(p <= &buf[len]);
	}

	if (common_dbg > 3)
		g_debug("%s: on exit: \"%s\"", G_STRFUNC, buf);

	g_assert(len == strlen(buf));

	return constant_str(buf);
}