shell_var.c 7.9 KB

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