shell_var.c 13 KB

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