variable_expand.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 = lub_string_dup(clish_command__get_name(this->cmd));
  69. } else if (!lub_string_nocasecmp(name, "__line")) {
  70. if (this->cmd && this->pargv)
  71. result = clish_variable__get_line(this->cmd, this->pargv);
  72. } else if (!lub_string_nocasecmp(name, "__params")) {
  73. if (this->cmd && this->pargv)
  74. result = clish_variable__get_params(this->cmd, this->pargv);
  75. }
  76. return result;
  77. }
  78. /*----------------------------------------------------------- */
  79. /*
  80. * This needs to escape any dangerous characters within the command line
  81. * to prevent gaining access to the underlying system shell.
  82. */
  83. static char *escape_special_chars(const char *string, const char *escape_chars)
  84. {
  85. char *result = NULL;
  86. const char *p;
  87. if (NULL == escape_chars) {
  88. escape_chars = default_escape_chars;
  89. }
  90. for (p = string; p && *p; p++)
  91. {
  92. /* find any special characters and prefix them with '\' */
  93. size_t len = strcspn(p, escape_chars);
  94. lub_string_catn(&result, p, len);
  95. p += len;
  96. if (*p) {
  97. lub_string_catn(&result, "\\", 1);
  98. lub_string_catn(&result, p, 1);
  99. } else {
  100. break;
  101. }
  102. }
  103. return result;
  104. }
  105. /*--------------------------------------------------------- */
  106. static char *context_retrieve(const context_t * this, const char *name)
  107. {
  108. char *result = NULL;
  109. const char *tmp = NULL;
  110. const char *escape_chars = NULL;
  111. char *string = NULL;
  112. assert(name);
  113. /* try and substitute a parameter value */
  114. if (this && this->pargv) {
  115. const clish_parg_t *parg =
  116. clish_pargv_find_arg(this->pargv, name);
  117. if (NULL != parg) {
  118. /* substitute the command line value */
  119. tmp = clish_parg__get_value(parg);
  120. }
  121. }
  122. if (NULL == tmp) {
  123. /* try and substitute a viewId variable */
  124. if (this && this->viewid) {
  125. tmp = string = find_viewid_var(this->viewid, name);
  126. }
  127. }
  128. if (NULL == tmp) {
  129. /* try and substitute context fixed variable */
  130. tmp = string = find_context_var(this, name);
  131. }
  132. /* get the contents of an environment variable */
  133. if (NULL == tmp) {
  134. tmp = getenv(name);
  135. }
  136. if (this && this->cmd) {
  137. /* override the escape characters */
  138. escape_chars = clish_command__get_escape_chars(this->cmd);
  139. }
  140. result = escape_special_chars(tmp, escape_chars);
  141. if (NULL != string) {
  142. /* free the dynamic memory */
  143. lub_string_free(string);
  144. }
  145. return result;
  146. }
  147. /*--------------------------------------------------------- */
  148. /*
  149. * return the next segment of text from the provided string
  150. * segments are delimited by variables within the string.
  151. */
  152. static char *context_nextsegment(const context_t * this, const char **string)
  153. {
  154. const char *p = *string;
  155. char *result = NULL;
  156. size_t len = 0;
  157. if (p) {
  158. if (*p && (p[0] == '$') && (p[1] == '{')) {
  159. /* start of a variable */
  160. const char *tmp;
  161. p += 2;
  162. tmp = p;
  163. /*
  164. * find the end of the variable
  165. */
  166. while (*p && p++[0] != '}') {
  167. len++;
  168. }
  169. /* ignore non-terminated variables */
  170. if (p[-1] == '}') {
  171. bool_t valid = BOOL_FALSE;
  172. char *text, *q;
  173. /* get the variable text */
  174. text = lub_string_dupn(tmp, len);
  175. /*
  176. * tokenise this INTO ':' separated words
  177. * and either expand or duplicate into the result string.
  178. * Only return a result if at least
  179. * of the words is an expandable variable
  180. */
  181. for (q = strtok(text, ":");
  182. q; q = strtok(NULL, ":")) {
  183. char *var = context_retrieve(this, q);
  184. /* copy the expansion or the raw word */
  185. lub_string_cat(&result, var ? var : q);
  186. /* record any expansions */
  187. if (NULL != var) {
  188. valid = BOOL_TRUE;
  189. }
  190. lub_string_free(var);
  191. }
  192. if (BOOL_FALSE == valid) {
  193. /* not a valid variable expansion */
  194. lub_string_free(result);
  195. result = lub_string_dup("");
  196. }
  197. /* finished with the variable text */
  198. lub_string_free(text);
  199. }
  200. } else {
  201. /* find the start of a variable */
  202. while (*p) {
  203. if ((p[0] == '$') && (p[1] == '{')) {
  204. break;
  205. }
  206. len++;
  207. p++;
  208. }
  209. if (len > 0) {
  210. result = lub_string_dupn(*string, len);
  211. }
  212. }
  213. /* move the string pointer on for next time... */
  214. *string = p;
  215. }
  216. return result;
  217. }
  218. /*--------------------------------------------------------- */
  219. /*
  220. * This function builds a dynamic string based on that provided
  221. * subtituting each occurance of a "${FRED}" type variable sub-string
  222. * with the appropriate value.
  223. */
  224. char *clish_variable_expand(const char *string,
  225. const char *viewid,
  226. const clish_command_t * cmd, clish_pargv_t * pargv)
  227. {
  228. char *seg, *result = NULL;
  229. context_t context;
  230. /* setup the context */
  231. context.viewid = viewid;
  232. context.cmd = cmd;
  233. context.pargv = pargv;
  234. /* read each segment and extend the result */
  235. while (NULL != (seg = context_nextsegment(&context, &string))) {
  236. lub_string_cat(&result, seg);
  237. lub_string_free(seg);
  238. }
  239. return result;
  240. }
  241. /*--------------------------------------------------------- */
  242. char *clish_variable__get_params(const clish_command_t * cmd, clish_pargv_t * pargv)
  243. {
  244. char *line = NULL;
  245. unsigned i, cnt;
  246. const clish_param_t *param;
  247. const clish_parg_t *parg;
  248. if (!pargv)
  249. return NULL;
  250. cnt = clish_pargv__get_count(pargv);
  251. for (i = 0; i < cnt; i++) {
  252. param = clish_pargv__get_param(pargv, i);
  253. if (CLISH_PARAM_SWITCH == clish_param__get_mode(param))
  254. continue;
  255. if (CLISH_PARAM_NORMAL != clish_param__get_visibility(param))
  256. continue;
  257. parg = clish_pargv__get_parg(pargv, i);
  258. if (NULL != line)
  259. lub_string_cat(&line, " ");
  260. lub_string_cat(&line, clish_parg__get_value(parg));
  261. }
  262. return line;
  263. }
  264. /*--------------------------------------------------------- */
  265. char *clish_variable__get_line(const clish_command_t * cmd, clish_pargv_t * pargv)
  266. {
  267. char *line = NULL;
  268. char *params = NULL;
  269. lub_string_cat(&line, clish_command__get_name(cmd));
  270. if (!pargv)
  271. return line;
  272. params = clish_variable__get_params(cmd, pargv);
  273. if (params) {
  274. lub_string_cat(&line, " ");
  275. lub_string_cat(&line, params);
  276. }
  277. lub_string_free(params);
  278. return line;
  279. }
  280. /*--------------------------------------------------------- */