示例#1
0
/*
 * routine:
 *	qualify
 *
 * purpose:
 *	to fully qualify a name
 *
 * parameters:
 *	name to be qualified
 *
 * returns:
 *	either original pointer or copy to a new (malloced) buffer
 *
 * notes:
 *	someday I may conclude that I should always make a copy
 *	so that the caller can know that it is safe to free the parm
 *
 *	I thought about this and concluded that there is never a need
 *	to fully qualify a string containing variables.  If the string
 *	came from the command line, the variables were already expanded
 *	and if it came from the rules data base it is required to already
 *	be fully qualified.
 */
char *
qualify(char *name)
{
	char namebuf[ MAX_PATH ];

	/* in the simple case, the parameter is already there */
	if (*name == '/') {
		cannonize(name);
		return (name);
	}

	/* things that begin with variables get the benefit of the doubt */
	if (*name == '$') {
		cannonize(name);
		return (name);
	}

	/* start with the current working directory	*/
	if (getcwd(namebuf, sizeof (namebuf)) == 0) {
		fprintf(stderr, gettext(ERR_nocwd), name);
		exit(ERR_OTHER);
	}

	/* make sure we have room for our file name	*/
	if ((strlen(namebuf) + strlen(name) + 2) >= sizeof (namebuf)) {
		fprintf(stderr, gettext(ERR_longname), name);
		exit(ERR_OTHER);
	}

	/* append the specified file name to it	*/
	strcat(namebuf, "/");
	strcat(namebuf, name);

	/* filter out redundant dots	*/
	cannonize(namebuf);

	if (opt_debug & DBG_VARS)
		fprintf(stderr, "VARS: QUALIFY %s to %s\n", name, namebuf);

	/* and return a newly malloc'd copy	*/
	return (strdup(namebuf));
}
示例#2
0
static void
insert_key (PurpleDesktopItem *item,
	    Section *cur_section,
	    Encoding encoding,
	    const char *key,
	    const char *value,
	    gboolean old_kde,
	    gboolean no_translations)
{
	char *k;
	char *val;
	/* we always store everything in UTF-8 */
	if (cur_section == NULL &&
	    strcmp (key, PURPLE_DESKTOP_ITEM_ENCODING) == 0) {
		k = g_strdup (key);
		val = g_strdup ("UTF-8");
	} else {
		char *locale = snarf_locale_from_key (key);
		/* If we're ignoring translations */
		if (no_translations && locale != NULL) {
			g_free (locale);
			return;
		}
		val = decode_string (value, encoding, locale);

		/* Ignore this key, it's whacked */
		if (val == NULL) {
			g_free (locale);
			return;
		}
		
		g_strchomp (val);

		/* For old KDE entries, we can also split by a comma
		 * on sort order, so convert to semicolons */
		if (old_kde &&
		    cur_section == NULL &&
		    strcmp (key, PURPLE_DESKTOP_ITEM_SORT_ORDER) == 0 &&
		    strchr (val, ';') == NULL) {
			int i;
			for (i = 0; val[i] != '\0'; i++) {
				if (val[i] == ',')
					val[i] = ';';
			}
		}

		/* Check some types, not perfect, but catches a lot
		 * of things */
		if (cur_section == NULL) {
			char *cannon = cannonize (key, val);
			if (cannon != NULL) {
				g_free (val);
				val = cannon;
			}
		}

		k = g_strdup (key);

		/* Take care of the language part */
		if (locale != NULL &&
		    strcmp (locale, "C") == 0) {
			char *p;
			/* Whack C locale */
			p = strchr (k, '[');
			if(p) *p = '\0';
			g_free (locale);
		} else if (locale != NULL) {
			char *p, *brace;

			/* Whack the encoding part */
			p = strchr (locale, '.');
			if (p != NULL)
				*p = '\0';

			if (g_list_find_custom (item->languages, locale,
						(GCompareFunc)strcmp) == NULL) {
				item->languages = g_list_prepend
					(item->languages, locale);
			} else {
				g_free (locale);
			}

			/* Whack encoding from encoding in the key */
			brace = strchr (k, '[');
			if(brace != NULL) {
				p = strchr (brace, '.');
				if (p != NULL) {
					*p = ']';
					*(p+1) = '\0';
				}
			}
		}
	}


	if (cur_section == NULL) {
		/* only add to list if we haven't seen it before */
		if (g_hash_table_lookup (item->main_hash, k) == NULL) {
			item->keys = g_list_prepend (item->keys,
						     g_strdup (k));
		}
		/* later duplicates override earlier ones */
		g_hash_table_replace (item->main_hash, k, val);
	} else {
		char *full = g_strdup_printf
			("%s/%s",
			 cur_section->name, k);
		/* only add to list if we haven't seen it before */
		if (g_hash_table_lookup (item->main_hash, full) == NULL) {
			cur_section->keys =
				g_list_prepend (cur_section->keys, k);
		}
		/* later duplicates override earlier ones */
		g_hash_table_replace (item->main_hash,
				      full, val);
	}
}
示例#3
0
/*
 * routine:
 *	expand
 *
 * purpose:
 *	to expand variable names within a string
 *
 * parameters:
 *	string to be expanded.  Variable references always begin
 *	with a $ and are delimited by parens or curleys.
 *
 * returns:
 *	either original pointer or a copy to a new (malloced) buffer
 *
 * notes:
 *	someday I may conclude that I should always make a copy
 *	so that the caller can know that it is safe to free the parm
 *
 *	someday I may decide to support escape conventions for embedding
 *	$(){} in file names, but I suspec that day will never come.
 *
 *	I thought about this and concluded there was no reason to
 *	fully qualify these names, because the only names that should
 *	need qualification are src/dst lines from the command line,
 *	and the shell should have handled those for me.  Once something
 *	makes it into the database, it is expected to be fully qualified
 *	already.
 *
 *	We are limited to producing strings of length MAX_PATH or less
 *	and variable names of length MAX_NAME or less.  In practice,
 *	these limitations should not be a problem.
 */
char *
expand(char *name)
{	const char *s;
	char *p, *v;
	char delim;
	char namebuf[ MAX_PATH ];
	char varbuf[ MAX_NAME ];

	/* first see if there are no variables to be bound */
	for (s = name; *s && *s != '$'; s++);
	if (*s == 0)
		return (name);

	/* move through the string, copying and expanding	*/
	for (s = name, p = namebuf; *s; s++) {

		/* check for overflow	*/
		if (p >= &namebuf[ MAX_PATH ]) {
			fprintf(stderr, gettext(ERR_longname), name);
			exit(ERR_OTHER);
		}

		/* normal characters, we just copy		*/
		if (*s != '$') {
			*p++ = *s;
			continue;
		}

		/* figure out how the variable name is delimited */
		delim = *++s;
		if (delim == '(') {
			delim = ')';
			s++;
		} else if (delim == '{') {
			delim = '}';
			s++;
		} else
			delim = 0;

		/* copy the variable name up to the closing delimiter */
		for (v = varbuf; *s; s++) {
			if (isalnum(*s) || (*s == '_') ||
				(delim && *s != delim))
				*v++ = *s;
			else
				break;

			/* make sure we don't overflow var name buffer	*/
			if (v >= &varbuf[MAX_NAME - 1]) {
				*v = 0;
				fprintf(stderr, gettext(ERR_longname), varbuf);
				exit(ERR_OTHER);
			}
		}

		*v = 0;

		/* FIX THIS ... there must be a more elegant way */
		/* we may have to back up because s will be bumped */
		if (delim == 0 || *s != delim)
			s--;

		/* look up the variable 			*/
		v = getenv(varbuf);
		if (v == 0 || *v == 0) {
			fprintf(stderr, gettext(ERR_undef), varbuf);
			return (0);
		}

		/* copy the variable into the buffer		*/
		while (*v)
			*p++ = *v++;
	}

	/* null terminate the copy	*/
	*p = 0;

	/* compress out any redundant dots and dot-dots	*/
	cannonize(namebuf);

	if (opt_debug & DBG_VARS)
		fprintf(stderr, "VARS: EXPAND %s to %s\n", name, namebuf);

	/* and return a newly malloc'd copy	*/
	return (strdup(namebuf));
}