shell_var.c 11 KB

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