variable_expand.c 7.7 KB

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