shell_var.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. * shell_var.c
  3. */
  4. #include <stdlib.h>
  5. #include <assert.h>
  6. #include <string.h>
  7. #include "lub/string.h"
  8. #include "private.h"
  9. /*--------------------------------------------------------- */
  10. void clish_shell_insert_var(clish_shell_t *this, clish_var_t *var)
  11. {
  12. (void)lub_bintree_insert(&this->var_tree, var);
  13. }
  14. /*--------------------------------------------------------- */
  15. clish_var_t *clish_shell_find_var(clish_shell_t *this, const char *name)
  16. {
  17. return lub_bintree_find(&this->var_tree, name);
  18. }
  19. /*--------------------------------------------------------- */
  20. const char *clish_shell__get_viewid(const clish_shell_t *this)
  21. {
  22. assert(this);
  23. return this->viewid;
  24. }
  25. /*----------------------------------------------------------- */
  26. /*
  27. * search the current viewid string for a variable
  28. */
  29. static char *find_viewid_var(const char *name, const char *viewid)
  30. {
  31. char *result = NULL;
  32. regex_t regex;
  33. int status;
  34. char *pattern = NULL;
  35. regmatch_t pmatches[2];
  36. /* build up the pattern to match */
  37. lub_string_cat(&pattern, name);
  38. lub_string_cat(&pattern, "[ ]*=([^;]*)");
  39. /* compile the regular expression to find this variable */
  40. status = regcomp(&regex, pattern, REG_EXTENDED);
  41. assert(0 == status);
  42. lub_string_free(pattern);
  43. /* now perform the matching */
  44. /*lint -e64 Type mismatch (arg. no. 4) */
  45. /*
  46. * lint seems to equate regmatch_t[] as being of type regmatch_t !!!
  47. */
  48. status = regexec(&regex, viewid, 2, pmatches, 0);
  49. /*lint +e64 */
  50. if (0 == status) {
  51. regoff_t len = pmatches[1].rm_eo - pmatches[1].rm_so;
  52. const char *value = &viewid[pmatches[1].rm_so];
  53. /* found a match */
  54. result = lub_string_dupn(value, (unsigned)len);
  55. }
  56. /* release the regular expression */
  57. regfree(&regex);
  58. return result;
  59. }
  60. /*----------------------------------------------------------- */
  61. /*
  62. * expand context dependent fixed-name variables
  63. */
  64. static char *find_context_var(const char *name, clish_context_t *this)
  65. {
  66. char *result = NULL;
  67. if (!this->cmd)
  68. return NULL;
  69. if (!lub_string_nocasecmp(name, "__full_cmd")) {
  70. result = lub_string_dup(clish_command__get_name(this->cmd));
  71. } else if (!lub_string_nocasecmp(name, "__cmd")) {
  72. result = lub_string_dup(clish_command__get_name(
  73. clish_command__get_cmd(this->cmd)));
  74. } else if (!lub_string_nocasecmp(name, "__orig_cmd")) {
  75. result = lub_string_dup(clish_command__get_name(
  76. clish_command__get_orig(this->cmd)));
  77. } else if (!lub_string_nocasecmp(name, "__line")) {
  78. if (this->pargv)
  79. result = clish_shell__get_line(this->cmd, this->pargv);
  80. } else if (!lub_string_nocasecmp(name, "__params")) {
  81. if (this->pargv)
  82. result = clish_shell__get_params(this->cmd, this->pargv);
  83. } else if (lub_string_nocasestr(name, "__prefix") == name) {
  84. int idx = 0;
  85. int pnum = 0;
  86. pnum = lub_argv_wordcount(clish_command__get_name(this->cmd)) -
  87. lub_argv_wordcount(clish_command__get_name(
  88. clish_command__get_cmd(this->cmd)));
  89. idx = atoi(name + strlen("__prefix"));
  90. if (idx < pnum) {
  91. lub_argv_t *argv = lub_argv_new(
  92. clish_command__get_name(this->cmd), 0);
  93. result = lub_string_dup(lub_argv__get_arg(argv, idx));
  94. lub_argv_delete(argv);
  95. }
  96. }
  97. return result;
  98. }
  99. /*--------------------------------------------------------- */
  100. static char *find_global_var(const char *name, clish_context_t *context)
  101. {
  102. clish_shell_t *this = context->shell;
  103. clish_var_t *var = clish_shell_find_var(this, name);
  104. clish_action_t *action;
  105. char *value;
  106. char *script;
  107. bool_t dynamic;
  108. char *res = NULL;
  109. if (!var)
  110. return NULL;
  111. /* Try to get saved value for static var */
  112. dynamic = clish_var__get_dynamic(var);
  113. if (!dynamic) {
  114. char *saved = clish_var__get_saved(var);
  115. if (saved)
  116. return lub_string_dup(saved);
  117. }
  118. /* Try to expand value field */
  119. value = clish_var__get_value(var);
  120. if (value)
  121. res = clish_shell_expand(value, context);
  122. /* Try to execute ACTION */
  123. if (!res) {
  124. action = clish_var__get_action(var);
  125. script = clish_action__get_script(action);
  126. if (script) {
  127. char *out = NULL;
  128. if (!clish_shell_exec_action(action, context, &out)) {
  129. lub_string_free(out);
  130. return NULL;
  131. }
  132. res = out;
  133. }
  134. }
  135. /* Save value for static var */
  136. if (!dynamic && res)
  137. clish_var__set_saved(var, res);
  138. return res;
  139. }
  140. /*--------------------------------------------------------- */
  141. /*
  142. * return the next segment of text from the provided string
  143. * segments are delimited by variables within the string.
  144. */
  145. static char *expand_nextsegment(const char **string, clish_context_t *this)
  146. {
  147. const char *p = *string;
  148. char *result = NULL;
  149. size_t len = 0;
  150. if (p) {
  151. if (*p && (p[0] == '$') && (p[1] == '{')) {
  152. /* start of a variable */
  153. const char *tmp;
  154. p += 2;
  155. tmp = p;
  156. /*
  157. * find the end of the variable
  158. */
  159. while (*p && p++[0] != '}') {
  160. len++;
  161. }
  162. /* ignore non-terminated variables */
  163. if (p[-1] == '}') {
  164. bool_t valid = BOOL_FALSE;
  165. char *text, *q;
  166. char *saveptr;
  167. /* get the variable text */
  168. text = lub_string_dupn(tmp, len);
  169. /*
  170. * tokenise this INTO ':' separated words
  171. * and either expand or duplicate into the result string.
  172. * Only return a result if at least
  173. * of the words is an expandable variable
  174. */
  175. for (q = strtok_r(text, ":", &saveptr);
  176. q; q = strtok_r(NULL, ":", &saveptr)) {
  177. char *var = clish_shell_expand_var(q, this);
  178. /* copy the expansion or the raw word */
  179. lub_string_cat(&result, var ? var : q);
  180. /* record any expansions */
  181. if (var)
  182. valid = BOOL_TRUE;
  183. lub_string_free(var);
  184. }
  185. if (!valid) {
  186. /* not a valid variable expansion */
  187. lub_string_free(result);
  188. result = lub_string_dup("");
  189. }
  190. /* finished with the variable text */
  191. lub_string_free(text);
  192. }
  193. } else {
  194. /* find the start of a variable */
  195. while (*p) {
  196. if ((p[0] == '$') && (p[1] == '{'))
  197. break;
  198. len++;
  199. p++;
  200. }
  201. if (len > 0)
  202. result = lub_string_dupn(*string, len);
  203. }
  204. /* move the string pointer on for next time... */
  205. *string = p;
  206. }
  207. return result;
  208. }
  209. /*--------------------------------------------------------- */
  210. /*
  211. * This function builds a dynamic string based on that provided
  212. * subtituting each occurance of a "${FRED}" type variable sub-string
  213. * with the appropriate value.
  214. */
  215. char *clish_shell_expand(const char *str, void *context)
  216. {
  217. char *seg, *result = NULL;
  218. /* read each segment and extend the result */
  219. while ((seg = expand_nextsegment(&str, context))) {
  220. lub_string_cat(&result, seg);
  221. lub_string_free(seg);
  222. }
  223. return result;
  224. }
  225. /*--------------------------------------------------------- */
  226. char *clish_shell__get_params(const clish_command_t *cmd, clish_pargv_t *pargv)
  227. {
  228. char *line = NULL;
  229. unsigned i, cnt;
  230. const clish_param_t *param;
  231. const clish_parg_t *parg;
  232. if (!pargv)
  233. return NULL;
  234. cnt = clish_pargv__get_count(pargv);
  235. for (i = 0; i < cnt; i++) {
  236. const char *tmp;
  237. char *space = NULL;
  238. param = clish_pargv__get_param(pargv, i);
  239. if (clish_param__get_hidden(param))
  240. continue;
  241. parg = clish_pargv__get_parg(pargv, i);
  242. tmp = clish_parg__get_value(parg);
  243. space = strchr(tmp, ' ');
  244. if (line)
  245. lub_string_cat(&line, " ");
  246. if (space)
  247. lub_string_cat(&line, "\\\"");
  248. lub_string_cat(&line, tmp);
  249. if (space)
  250. lub_string_cat(&line, "\\\"");
  251. }
  252. return line;
  253. }
  254. /*--------------------------------------------------------- */
  255. char *clish_shell__get_line(const clish_command_t *cmd, clish_pargv_t *pargv)
  256. {
  257. char *line = NULL;
  258. char *params = NULL;
  259. lub_string_cat(&line, clish_command__get_name(
  260. clish_command__get_cmd(cmd)));
  261. if (!pargv)
  262. return line;
  263. params = clish_shell__get_params(cmd, pargv);
  264. if (params) {
  265. lub_string_cat(&line, " ");
  266. lub_string_cat(&line, params);
  267. }
  268. lub_string_free(params);
  269. return line;
  270. }
  271. /*--------------------------------------------------------- */
  272. char *clish_shell_expand_var(const char *name, void *context)
  273. {
  274. clish_context_t *con = (clish_context_t *)context;
  275. clish_shell_t *this;
  276. const clish_command_t *cmd;
  277. clish_pargv_t *pargv;
  278. char *result = NULL;
  279. const char *tmp = NULL;
  280. const char *escape_chars = NULL;
  281. char *string = NULL;
  282. assert(name);
  283. if (!context)
  284. return lub_string_dup(name);
  285. this = con->shell;
  286. cmd = con->cmd;
  287. pargv = con->pargv;
  288. /* try and substitute a parameter value */
  289. if (pargv) {
  290. const clish_parg_t *parg = clish_pargv_find_arg(pargv, name);
  291. /* substitute the command line value */
  292. if (parg)
  293. tmp = clish_parg__get_value(parg);
  294. }
  295. /* try and substitute the param's default */
  296. if (!tmp && cmd)
  297. tmp = clish_paramv_find_default(
  298. clish_command__get_paramv(cmd), name);
  299. /* try and substitute a viewId variable */
  300. if (!tmp && this && this->viewid)
  301. tmp = string = find_viewid_var(name, this->viewid);
  302. /* try and substitute context fixed variable */
  303. if (!tmp)
  304. tmp = string = find_context_var(name, context);
  305. /* try and substitute a global var value */
  306. if (!tmp && this)
  307. tmp = string = find_global_var(name, context);
  308. /* get the contents of an environment variable */
  309. if (!tmp)
  310. tmp = getenv(name);
  311. /* override the escape characters */
  312. if (cmd)
  313. escape_chars = clish_command__get_escape_chars(cmd);
  314. result = lub_string_encode(tmp, escape_chars);
  315. /* free the dynamic memory */
  316. if (string)
  317. lub_string_free(string);
  318. return result;
  319. }
  320. /*----------------------------------------------------------- */