1
0

shell_var.c 13 KB

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