shell_var.c 10 KB

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