shell_var.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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 = NULL;
  106. char *script = NULL;
  107. if (!var)
  108. return NULL;
  109. /* Try the value field */
  110. value = clish_var__get_value(var);
  111. if (value)
  112. return clish_shell_expand(value, context);
  113. action = clish_var__get_action(var);
  114. script = clish_action__get_script(action);
  115. if (script) {
  116. char *out = NULL;
  117. if (!clish_shell_exec_action(action, context, &out)) {
  118. lub_string_free(out);
  119. return NULL;
  120. }
  121. return out;
  122. }
  123. return NULL;
  124. }
  125. /*--------------------------------------------------------- */
  126. /*
  127. * return the next segment of text from the provided string
  128. * segments are delimited by variables within the string.
  129. */
  130. static char *expand_nextsegment(const char **string, clish_context_t *this)
  131. {
  132. const char *p = *string;
  133. char *result = NULL;
  134. size_t len = 0;
  135. if (p) {
  136. if (*p && (p[0] == '$') && (p[1] == '{')) {
  137. /* start of a variable */
  138. const char *tmp;
  139. p += 2;
  140. tmp = p;
  141. /*
  142. * find the end of the variable
  143. */
  144. while (*p && p++[0] != '}') {
  145. len++;
  146. }
  147. /* ignore non-terminated variables */
  148. if (p[-1] == '}') {
  149. bool_t valid = BOOL_FALSE;
  150. char *text, *q;
  151. char *saveptr;
  152. /* get the variable text */
  153. text = lub_string_dupn(tmp, len);
  154. /*
  155. * tokenise this INTO ':' separated words
  156. * and either expand or duplicate into the result string.
  157. * Only return a result if at least
  158. * of the words is an expandable variable
  159. */
  160. for (q = strtok_r(text, ":", &saveptr);
  161. q; q = strtok_r(NULL, ":", &saveptr)) {
  162. char *var = clish_shell_expand_var(q, this);
  163. /* copy the expansion or the raw word */
  164. lub_string_cat(&result, var ? var : q);
  165. /* record any expansions */
  166. if (var)
  167. valid = BOOL_TRUE;
  168. lub_string_free(var);
  169. }
  170. if (!valid) {
  171. /* not a valid variable expansion */
  172. lub_string_free(result);
  173. result = lub_string_dup("");
  174. }
  175. /* finished with the variable text */
  176. lub_string_free(text);
  177. }
  178. } else {
  179. /* find the start of a variable */
  180. while (*p) {
  181. if ((p[0] == '$') && (p[1] == '{'))
  182. break;
  183. len++;
  184. p++;
  185. }
  186. if (len > 0)
  187. result = lub_string_dupn(*string, len);
  188. }
  189. /* move the string pointer on for next time... */
  190. *string = p;
  191. }
  192. return result;
  193. }
  194. /*--------------------------------------------------------- */
  195. /*
  196. * This function builds a dynamic string based on that provided
  197. * subtituting each occurance of a "${FRED}" type variable sub-string
  198. * with the appropriate value.
  199. */
  200. char *clish_shell_expand(const char *str, void *context)
  201. {
  202. char *seg, *result = NULL;
  203. /* read each segment and extend the result */
  204. while ((seg = expand_nextsegment(&str, context))) {
  205. lub_string_cat(&result, seg);
  206. lub_string_free(seg);
  207. }
  208. return result;
  209. }
  210. /*--------------------------------------------------------- */
  211. char *clish_shell__get_params(const clish_command_t *cmd, clish_pargv_t *pargv)
  212. {
  213. char *line = NULL;
  214. unsigned i, cnt;
  215. const clish_param_t *param;
  216. const clish_parg_t *parg;
  217. if (!pargv)
  218. return NULL;
  219. cnt = clish_pargv__get_count(pargv);
  220. for (i = 0; i < cnt; i++) {
  221. const char *tmp;
  222. char *space = NULL;
  223. param = clish_pargv__get_param(pargv, i);
  224. if (clish_param__get_hidden(param))
  225. continue;
  226. parg = clish_pargv__get_parg(pargv, i);
  227. tmp = clish_parg__get_value(parg);
  228. space = strchr(tmp, ' ');
  229. if (line)
  230. lub_string_cat(&line, " ");
  231. if (space)
  232. lub_string_cat(&line, "\\\"");
  233. lub_string_cat(&line, tmp);
  234. if (space)
  235. lub_string_cat(&line, "\\\"");
  236. }
  237. return line;
  238. }
  239. /*--------------------------------------------------------- */
  240. char *clish_shell__get_line(const clish_command_t *cmd, clish_pargv_t *pargv)
  241. {
  242. char *line = NULL;
  243. char *params = NULL;
  244. lub_string_cat(&line, clish_command__get_name(
  245. clish_command__get_cmd(cmd)));
  246. if (!pargv)
  247. return line;
  248. params = clish_shell__get_params(cmd, pargv);
  249. if (params) {
  250. lub_string_cat(&line, " ");
  251. lub_string_cat(&line, params);
  252. }
  253. lub_string_free(params);
  254. return line;
  255. }
  256. /*--------------------------------------------------------- */
  257. char *clish_shell_expand_var(const char *name, void *context)
  258. {
  259. clish_context_t *con = (clish_context_t *)context;
  260. clish_shell_t *this;
  261. const clish_command_t *cmd;
  262. clish_pargv_t *pargv;
  263. char *result = NULL;
  264. const char *tmp = NULL;
  265. const char *escape_chars = NULL;
  266. char *string = NULL;
  267. assert(name);
  268. if (!context)
  269. return lub_string_dup(name);
  270. this = con->shell;
  271. cmd = con->cmd;
  272. pargv = con->pargv;
  273. /* try and substitute a parameter value */
  274. if (pargv) {
  275. const clish_parg_t *parg = clish_pargv_find_arg(pargv, name);
  276. /* substitute the command line value */
  277. if (parg)
  278. tmp = clish_parg__get_value(parg);
  279. }
  280. /* try and substitute the param's default */
  281. if (!tmp && cmd)
  282. tmp = clish_paramv_find_default(
  283. clish_command__get_paramv(cmd), name);
  284. /* try and substitute a viewId variable */
  285. if (!tmp && this && this->viewid)
  286. tmp = string = find_viewid_var(name, this->viewid);
  287. /* try and substitute context fixed variable */
  288. if (!tmp)
  289. tmp = string = find_context_var(name, context);
  290. /* try and substitute a global var value */
  291. if (!tmp && this)
  292. tmp = string = find_global_var(name, context);
  293. /* get the contents of an environment variable */
  294. if (!tmp)
  295. tmp = getenv(name);
  296. /* override the escape characters */
  297. if (cmd)
  298. escape_chars = clish_command__get_escape_chars(cmd);
  299. result = lub_string_encode(tmp, escape_chars);
  300. /* free the dynamic memory */
  301. if (string)
  302. lub_string_free(string);
  303. return result;
  304. }
  305. /*----------------------------------------------------------- */