variable_expand.c 7.9 KB

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