variable_expand.c 8.3 KB

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