From 334697c68fe9008b15ed8172ff871039ac642131 Mon Sep 17 00:00:00 2001 From: ewt Date: Tue, 20 May 1997 16:16:54 +0000 Subject: [PATCH] Added :shescape query format tag CVS patchset: 1649 CVS date: 1997/05/20 16:16:54 --- CHANGES | 2 ++ docs/queryformat | 3 +++ query.c | 33 +++++++++++++++++++++++++++++++-- 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/CHANGES b/CHANGES index 78a631c..24679c3 100644 --- a/CHANGES +++ b/CHANGES @@ -3,6 +3,8 @@ - fixed configure script to assume chown() doesn't follow symlinks if lchown() isn't available and configure is not being run as root - more header file changes for AIX + - added :shescape query format type for strings, which prints strings + which will survive a single level of shell expansion 2.3.11 -> 2.4: - changed RPMNLSDIR directories to and @datadir@ to better diff --git a/docs/queryformat b/docs/queryformat index 5fccc3d..3ad119b 100644 --- a/docs/queryformat +++ b/docs/queryformat @@ -118,3 +118,6 @@ name after the tag name. Here are some examples: rpm -q --queryformat \ "[%{REQUIRENAME} %{REQUIREFLAGS:depflags} %{REQUIREVERSION}\n]" \ vlock + +The :shescape may be used on plain strings to get a string which can pass +through a single level of shell and give the original string. diff --git a/query.c b/query.c index 60d04d5..fd75ad0 100644 --- a/query.c +++ b/query.c @@ -38,6 +38,8 @@ static void printFileInfo(char * name, unsigned int size, unsigned short mode, unsigned int mtime, unsigned short rdev, char * owner, char * group, int uid, int gid, char * linkto); +static void formatString(const char * format, const char * str, + const char * how); static int queryHeader(Header h, char * chptr) { int count = 0; @@ -127,6 +129,33 @@ static int queryPartial(Header h, char ** chptrptr, int * cntptr, return 0; } +static void formatString(const char * format, const char * str, + const char * how) { + char * dst; + const char * src, * buf; + + if (!strcmp(how, "shescape")) { + buf = dst = alloca(strlen(str) * 4 + 3); + *dst++ = '\''; + for (src = str; *src; src++) { + if (*src == '\'') { + *dst++ = '\''; + *dst++ = '\\'; + *dst++ = '\''; + *dst++ = '\''; + } else { + *dst++ = *src; + } + } + *dst++ = '\''; + *dst = '\0'; + } else { + buf = str; + } + + printf(format, buf); +} + static char * handleFormat(Header h, char * chptr, int * cntptr, int arrayNum) { const char * f = chptr; @@ -235,13 +264,13 @@ static char * handleFormat(Header h, char * chptr, int * cntptr, switch (type) { case RPM_STRING_ARRAY_TYPE: strcat(format, "s"); - printf(format, ((char **) p)[arrayNum]); + formatString(format, ((char **) p)[arrayNum], how); free(p); break; case RPM_STRING_TYPE: strcat(format, "s"); - printf(format, p); + formatString(format, p, how); break; case RPM_CHAR_TYPE: -- 2.7.4