shell_var.c 9.8 KB

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