variable_expand.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * shell_variable_expand.c
  3. */
  4. #include "private.h"
  5. #include "lub/string.h"
  6. #include <stdlib.h>
  7. #include <assert.h>
  8. #include <string.h>
  9. #include <sys/types.h>
  10. #include <regex.h>
  11. /*
  12. * These are the escape characters which are used by default when
  13. * expanding variables. These characters will be backslash escaped
  14. * to prevent them from being interpreted in a script.
  15. *
  16. * This is a security feature to prevent users from arbitarily setting
  17. * parameters to contain special sequences.
  18. */
  19. static const char *default_escape_chars = "`|$<>&()#";
  20. /*----------------------------------------------------------- */
  21. /*
  22. * search the current viewid string for a variable
  23. */
  24. static char *find_viewid_var(const char *viewid, const char *name)
  25. {
  26. char *result = NULL;
  27. regex_t regex;
  28. int status;
  29. char *pattern = NULL;
  30. regmatch_t pmatches[2];
  31. /* build up the pattern to match */
  32. lub_string_cat(&pattern, name);
  33. lub_string_cat(&pattern, "[ ]*=([^;]*)");
  34. /* compile the regular expression to find this variable */
  35. status = regcomp(&regex, pattern, REG_EXTENDED);
  36. assert(0 == status);
  37. lub_string_free(pattern);
  38. /* now perform the matching */
  39. /*lint -e64 Type mismatch (arg. no. 4) */
  40. /*
  41. * lint seems to equate regmatch_t[] as being of type regmatch_t !!!
  42. */
  43. status = regexec(&regex, viewid, 2, pmatches, 0);
  44. /*lint +e64 */
  45. if (0 == status) {
  46. regoff_t len = pmatches[1].rm_eo - pmatches[1].rm_so;
  47. const char *value = &viewid[pmatches[1].rm_so];
  48. /* found a match */
  49. result = lub_string_dupn(value, (unsigned)len);
  50. }
  51. /* release the regular expression */
  52. regfree(&regex);
  53. return result;
  54. }
  55. /*----------------------------------------------------------- */
  56. /*
  57. * expand context dependent fixed-name variables
  58. */
  59. static char *find_context_var(const context_t * this, const char *name)
  60. {
  61. char *result = NULL;
  62. regex_t regex;
  63. int status;
  64. char *pattern = NULL;
  65. regmatch_t pmatches[2];
  66. if (!lub_string_nocasecmp(name, "cmd")) {
  67. if (this->cmd)
  68. result =
  69. lub_string_dup(clish_command__get_name(this->cmd));
  70. } else if (!lub_string_nocasecmp(name, "line")) {
  71. if (this->cmd && this->pargv)
  72. result = clish_variable__get_line(this->cmd, this->pargv);
  73. }
  74. return result;
  75. }
  76. /*----------------------------------------------------------- */
  77. /*
  78. * This needs to escape any dangerous characters within the command line
  79. * to prevent gaining access to the underlying system shell.
  80. */
  81. static char *escape_special_chars(const char *string, const char *escape_chars)
  82. {
  83. char *result = NULL;
  84. const char *p;
  85. if (NULL == escape_chars) {
  86. escape_chars = default_escape_chars;
  87. }
  88. for (p = string; p && *p; p++)
  89. {
  90. /* find any special characters and prefix them with '\' */
  91. size_t len = strcspn(p, escape_chars);
  92. lub_string_catn(&result, p, len);
  93. p += len;
  94. if (*p) {
  95. lub_string_catn(&result, "\\", 1);
  96. lub_string_catn(&result, p, 1);
  97. } else {
  98. break;
  99. }
  100. }
  101. return result;
  102. }
  103. /*--------------------------------------------------------- */
  104. static char *context_retrieve(const context_t * this, const char *name)
  105. {
  106. char *result = NULL;
  107. const char *tmp = NULL;
  108. const char *escape_chars = NULL;
  109. char *string = NULL;
  110. assert(name);
  111. /* try and substitute a parameter value */
  112. if (this && this->pargv) {
  113. const clish_parg_t *parg =
  114. clish_pargv_find_arg(this->pargv, name);
  115. if (NULL != parg) {
  116. /* substitute the command line value */
  117. tmp = clish_parg__get_value(parg);
  118. }
  119. }
  120. if (NULL == tmp) {
  121. /* try and substitute a viewId variable */
  122. if (this && this->viewid) {
  123. tmp = string = find_viewid_var(this->viewid, name);
  124. }
  125. }
  126. if (NULL == tmp) {
  127. /* try and substitute context fixed variable */
  128. tmp = string = find_context_var(this, name);
  129. }
  130. /* get the contents of an environment variable */
  131. if (NULL == tmp) {
  132. tmp = getenv(name);
  133. }
  134. if (this && this->cmd) {
  135. /* override the escape characters */
  136. escape_chars = clish_command__get_escape_chars(this->cmd);
  137. }
  138. result = escape_special_chars(tmp, escape_chars);
  139. if (NULL != string) {
  140. /* free the dynamic memory */
  141. lub_string_free(string);
  142. }
  143. return result;
  144. }
  145. /*--------------------------------------------------------- */
  146. /*
  147. * return the next segment of text from the provided string
  148. * segments are delimited by variables within the string.
  149. */
  150. static char *context_nextsegment(const context_t * this, const char **string)
  151. {
  152. const char *p = *string;
  153. char *result = NULL;
  154. size_t len = 0;
  155. if (p) {
  156. if (*p && (p[0] == '$') && (p[1] == '{')) {
  157. /* start of a variable */
  158. const char *tmp;
  159. p += 2;
  160. tmp = p;
  161. /*
  162. * find the end of the variable
  163. */
  164. while (*p && p++[0] != '}') {
  165. len++;
  166. }
  167. /* ignore non-terminated variables */
  168. if (p[-1] == '}') {
  169. bool_t valid = BOOL_FALSE;
  170. char *text, *q;
  171. /* get the variable text */
  172. text = lub_string_dupn(tmp, len);
  173. /*
  174. * tokenise this INTO ':' separated words
  175. * and either expand or duplicate into the result string.
  176. * Only return a result if at least
  177. * of the words is an expandable variable
  178. */
  179. for (q = strtok(text, ":");
  180. q; q = strtok(NULL, ":")) {
  181. char *var = context_retrieve(this, q);
  182. /* copy the expansion or the raw word */
  183. lub_string_cat(&result, var ? var : q);
  184. /* record any expansions */
  185. if (NULL != var) {
  186. valid = BOOL_TRUE;
  187. }
  188. lub_string_free(var);
  189. }
  190. if (BOOL_FALSE == valid) {
  191. /* not a valid variable expansion */
  192. lub_string_free(result);
  193. result = lub_string_dup("");
  194. }
  195. /* finished with the variable text */
  196. lub_string_free(text);
  197. }
  198. } else {
  199. /* find the start of a variable */
  200. while (*p) {
  201. if ((p[0] == '$') && (p[1] == '{')) {
  202. break;
  203. }
  204. len++;
  205. p++;
  206. }
  207. if (len > 0) {
  208. result = lub_string_dupn(*string, len);
  209. }
  210. }
  211. /* move the string pointer on for next time... */
  212. *string = p;
  213. }
  214. return result;
  215. }
  216. /*--------------------------------------------------------- */
  217. /*
  218. * This function builds a dynamic string based on that provided
  219. * subtituting each occurance of a "${FRED}" type variable sub-string
  220. * with the appropriate value.
  221. */
  222. char *clish_variable_expand(const char *string,
  223. const char *viewid,
  224. const clish_command_t * cmd, clish_pargv_t * pargv)
  225. {
  226. char *seg, *result = NULL;
  227. context_t context;
  228. /* setup the context */
  229. context.viewid = viewid;
  230. context.cmd = cmd;
  231. context.pargv = pargv;
  232. /* read each segment and extend the result */
  233. while (NULL != (seg = context_nextsegment(&context, &string))) {
  234. lub_string_cat(&result, seg);
  235. lub_string_free(seg);
  236. }
  237. return result;
  238. }
  239. /*--------------------------------------------------------- */
  240. char *clish_variable__get_line(const clish_command_t * cmd, clish_pargv_t * pargv)
  241. {
  242. char *line = NULL;
  243. unsigned i, cnt;
  244. const clish_param_t *param;
  245. const clish_parg_t *parg;
  246. lub_string_cat(&line, clish_command__get_name(cmd));
  247. if (!pargv)
  248. return line;
  249. cnt = clish_pargv__get_count(pargv);
  250. for (i = 0; i < cnt; i++) {
  251. param = clish_pargv__get_param(pargv, i);
  252. if (CLISH_PARAM_SWITCH == clish_param__get_mode(param))
  253. continue;
  254. parg = clish_pargv__get_parg(pargv, i);
  255. lub_string_cat(&line, " ");
  256. lub_string_cat(&line, clish_parg__get_value(parg));
  257. }
  258. return line;
  259. }
  260. /*--------------------------------------------------------- */