shell_var.c 9.9 KB

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