shell_var.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. /*
  2. * shell_var.c
  3. */
  4. #include <stdlib.h>
  5. #include <assert.h>
  6. #include <string.h>
  7. #include <ctype.h>
  8. #include <sys/types.h>
  9. #include <unistd.h>
  10. #include "lub/string.h"
  11. #include "private.h"
  12. /*----------------------------------------------------------- */
  13. /*
  14. * search the current viewid string for a variable
  15. */
  16. void clish_shell__expand_viewid(const char *viewid, lub_bintree_t *tree,
  17. clish_context_t *context)
  18. {
  19. char *expanded;
  20. char *q;
  21. char *saveptr = NULL;
  22. expanded = clish_shell_expand(viewid, SHELL_VAR_NONE, context);
  23. if (!expanded)
  24. return;
  25. for (q = strtok_r(expanded, ";", &saveptr);
  26. q; q = strtok_r(NULL, ";", &saveptr)) {
  27. char *value;
  28. clish_var_t *var;
  29. value = strchr(q, '=');
  30. if (!value)
  31. continue;
  32. *value = '\0';
  33. value++;
  34. /* Create var instance */
  35. var = clish_var_new(q);
  36. lub_bintree_insert(tree, var);
  37. clish_var__set_value(var, value);
  38. }
  39. lub_string_free(expanded);
  40. }
  41. /*----------------------------------------------------------- */
  42. /*
  43. * expand context dependent fixed-name variables
  44. */
  45. static char *find_context_var(const char *name, clish_context_t *this)
  46. {
  47. char *result = NULL;
  48. clish_shell_t *shell = this->shell;
  49. if (!lub_string_nocasecmp(name, "_width")) {
  50. char tmp[5];
  51. snprintf(tmp, sizeof(tmp), "%u",
  52. tinyrl__get_width(shell->tinyrl));
  53. tmp[sizeof(tmp) - 1] = '\0';
  54. result = strdup(tmp);
  55. } else if (!lub_string_nocasecmp(name, "_height")) {
  56. char tmp[5];
  57. snprintf(tmp, sizeof(tmp), "%u",
  58. tinyrl__get_height(shell->tinyrl));
  59. tmp[sizeof(tmp) - 1] = '\0';
  60. result = strdup(tmp);
  61. } else if (!lub_string_nocasecmp(name, "_watchdog_timeout")) {
  62. char tmp[5];
  63. snprintf(tmp, sizeof(tmp), "%u", shell->wdog_timeout);
  64. tmp[sizeof(tmp) - 1] = '\0';
  65. result = strdup(tmp);
  66. } else if (!this->cmd) { /* The vars dependent on command */
  67. return NULL;
  68. } else if (!lub_string_nocasecmp(name, "_full_cmd")) {
  69. result = lub_string_dup(clish_command__get_name(this->cmd));
  70. } else if (!lub_string_nocasecmp(name, "_cmd")) {
  71. result = lub_string_dup(clish_command__get_name(
  72. clish_command__get_cmd(this->cmd)));
  73. } else if (!lub_string_nocasecmp(name, "_orig_cmd")) {
  74. result = lub_string_dup(clish_command__get_name(
  75. clish_command__get_orig(this->cmd)));
  76. } else if (!lub_string_nocasecmp(name, "_line")) {
  77. result = clish_shell__get_line(this);
  78. } else if (!lub_string_nocasecmp(name, "_full_line")) {
  79. result = clish_shell__get_full_line(this);
  80. } else if (!lub_string_nocasecmp(name, "_params")) {
  81. if (this->pargv)
  82. result = clish_shell__get_params(this);
  83. } else if (!lub_string_nocasecmp(name, "_interactive")) {
  84. if (clish_shell__get_interactive(this->shell))
  85. result = strdup("1");
  86. else
  87. result = strdup("0");
  88. } else if (!lub_string_nocasecmp(name, "_isatty")) {
  89. if (tinyrl__get_isatty(this->shell->tinyrl))
  90. result = strdup("1");
  91. else
  92. result = strdup("0");
  93. } else if (!lub_string_nocasecmp(name, "_pid")) {
  94. char tmp[10];
  95. snprintf(tmp, sizeof(tmp), "%u", getpid());
  96. tmp[sizeof(tmp) - 1] = '\0';
  97. result = strdup(tmp);
  98. } else if (lub_string_nocasestr(name, "_prefix") == name) {
  99. int idx = 0;
  100. int pnum = 0;
  101. pnum = lub_string_wordcount(clish_command__get_name(this->cmd)) -
  102. lub_string_wordcount(clish_command__get_name(
  103. clish_command__get_cmd(this->cmd)));
  104. idx = atoi(name + strlen("_prefix"));
  105. if (idx < pnum) {
  106. lub_argv_t *argv = lub_argv_new(
  107. clish_command__get_name(this->cmd), 0);
  108. result = lub_string_dup(lub_argv__get_arg(argv, idx));
  109. lub_argv_delete(argv);
  110. }
  111. } else if (!lub_string_nocasecmp(name, "_cur_depth")) {
  112. char tmp[10];
  113. int depth = clish_shell__get_depth(shell);
  114. snprintf(tmp, sizeof(tmp), "%u", depth);
  115. tmp[sizeof(tmp) - 1] = '\0';
  116. result = strdup(tmp);
  117. } else if (!lub_string_nocasecmp(name, "_cur_pwd")) {
  118. int depth = clish_shell__get_depth(shell);
  119. result = clish_shell__get_pwd_full(shell, depth);
  120. }
  121. return result;
  122. }
  123. /*--------------------------------------------------------- */
  124. static char *find_var(const char *name, lub_bintree_t *tree, clish_context_t *context)
  125. {
  126. clish_var_t *var = lub_bintree_find(tree, name);
  127. char *value;
  128. bool_t dynamic;
  129. char *res = NULL;
  130. if (!var)
  131. return NULL;
  132. /* Try to get saved value for static var */
  133. dynamic = clish_var__get_dynamic(var);
  134. if (!dynamic) {
  135. char *saved = clish_var__get_saved(var);
  136. if (saved)
  137. return lub_string_dup(saved);
  138. }
  139. /* Try to expand value field */
  140. value = clish_var__get_value(var);
  141. if (value)
  142. res = clish_shell_expand(value, SHELL_VAR_NONE, context);
  143. /* Try to execute ACTION */
  144. if (!res) {
  145. char *out = NULL;
  146. clish_context__set_action(context, clish_var__get_action(var));
  147. if (clish_shell_exec_action(context, &out)) {
  148. lub_string_free(out);
  149. return NULL;
  150. }
  151. res = out;
  152. }
  153. /* Save value for static var */
  154. if (!dynamic && res)
  155. clish_var__set_saved(var, res);
  156. return res;
  157. }
  158. /*--------------------------------------------------------- */
  159. static char *find_global_var(const char *name, clish_context_t *context)
  160. {
  161. clish_shell_t *shell = clish_context__get_shell(context);
  162. return find_var(name, &shell->var_tree, context);
  163. }
  164. /*--------------------------------------------------------- */
  165. static char *find_viewid_var(const char *name, clish_context_t *context)
  166. {
  167. clish_shell_t *shell = clish_context__get_shell(context);
  168. int depth = clish_shell__get_depth(shell);
  169. if (depth < 0)
  170. return NULL;
  171. return find_var(name, &shell->pwdv[depth]->viewid, context);
  172. }
  173. static char * chardiff(const char *syms, const char *minus)
  174. {
  175. char *dst = malloc(strlen(syms) + 1);
  176. char *p = dst;
  177. const char *src;
  178. for (src = syms; *src; src++) {
  179. if (!strchr(minus, *src))
  180. *(p++) = *src;
  181. }
  182. *p = '\0';
  183. return dst;
  184. }
  185. /*--------------------------------------------------------- */
  186. /*
  187. * return the next segment of text from the provided string
  188. * segments are delimited by variables within the string.
  189. */
  190. static char *expand_nextsegment(const char **string, const char *escape_chars,
  191. clish_context_t *this)
  192. {
  193. const char *p = *string;
  194. char *result = NULL;
  195. size_t len = 0;
  196. if (!p)
  197. return NULL;
  198. if (*p && (p[0] == '$') && (p[1] == '{')) {
  199. /* start of a variable */
  200. const char *tmp;
  201. p += 2;
  202. tmp = p;
  203. /*
  204. * find the end of the variable
  205. */
  206. while (*p && p++[0] != '}')
  207. len++;
  208. /* ignore non-terminated variables */
  209. if (p[-1] == '}') {
  210. bool_t valid = BOOL_FALSE;
  211. char *text, *q;
  212. char *saveptr = NULL;
  213. /* get the variable text */
  214. text = lub_string_dupn(tmp, len);
  215. /*
  216. * tokenise this INTO ':' separated words
  217. * and either expand or duplicate into the result string.
  218. * Only return a result if at least
  219. * of the words is an expandable variable
  220. */
  221. for (q = strtok_r(text, ":", &saveptr);
  222. q; q = strtok_r(NULL, ":", &saveptr)) {
  223. char *var;
  224. int mod_quote = 0; /* quote modifier */
  225. int mod_esc = 0; /* internal escape modifier */
  226. int mod_esc_chars = 1; /* escaping */
  227. int mod_esc_dec = 0; /* remove internal chars from escaping */
  228. char *space;
  229. char *all_esc = NULL;
  230. /* Search for modifiers */
  231. while (*q && !isalpha(*q)) {
  232. if ('#' == *q) {
  233. mod_quote = 1;
  234. mod_esc = 1;
  235. } else if ('\\' == *q) {
  236. mod_esc = 1;
  237. } else if ('!' == *q) {
  238. mod_quote = 1;
  239. mod_esc = 1;
  240. mod_esc_chars = 0;
  241. } else if ('~' == *q) {
  242. mod_esc = 1;
  243. mod_esc_chars = 0;
  244. /* Internal automatic variable like ${__line} */
  245. } else if (('_' == *q) && ('_' == *(q+1))) {
  246. mod_esc_dec = 1;
  247. q++;
  248. break;
  249. /* No escaping at all. Usefull for macros VAR */
  250. } else if ('^' == *q) {
  251. mod_quote = 0;
  252. mod_esc = 0;
  253. mod_esc_chars = 0;
  254. } else
  255. break;
  256. q++;
  257. }
  258. /* Get clean variable value */
  259. var = clish_shell_expand_var(q, this);
  260. if (!var) {
  261. lub_string_cat(&result, q);
  262. continue;
  263. }
  264. valid = BOOL_TRUE;
  265. /* Quoting */
  266. if (mod_quote)
  267. space = strchr(var, ' ');
  268. if (mod_quote && space)
  269. lub_string_cat(&result, "\"");
  270. /* Escape special chars */
  271. if (escape_chars && mod_esc_chars) {
  272. /* Remove internal esc from escape chars */
  273. if (mod_esc_dec)
  274. all_esc = chardiff(escape_chars,
  275. lub_string_esc_quoted);
  276. else
  277. all_esc = lub_string_dup(escape_chars);
  278. }
  279. /* Internal escaping */
  280. if (mod_esc)
  281. lub_string_cat(&all_esc,
  282. lub_string_esc_quoted);
  283. /* Real escaping */
  284. if (all_esc) {
  285. char *tstr = lub_string_encode(var,
  286. all_esc);
  287. lub_string_free(var);
  288. var = tstr;
  289. lub_string_free(all_esc);
  290. }
  291. /* copy the expansion or the raw word */
  292. lub_string_cat(&result, var);
  293. /* Quoting */
  294. if (mod_quote && space)
  295. lub_string_cat(&result, "\"");
  296. lub_string_free(var);
  297. }
  298. if (!valid) {
  299. /* not a valid variable expansion */
  300. lub_string_free(result);
  301. result = lub_string_dup("");
  302. }
  303. /* finished with the variable text */
  304. lub_string_free(text);
  305. }
  306. } else {
  307. /* find the start of a variable */
  308. while (*p) {
  309. if ((p[0] == '$') && (p[1] == '{'))
  310. break;
  311. len++;
  312. p++;
  313. }
  314. if (len > 0)
  315. result = lub_string_dupn(*string, len);
  316. }
  317. /* move the string pointer on for next time... */
  318. *string = p;
  319. return result;
  320. }
  321. /*--------------------------------------------------------- */
  322. /*
  323. * This function builds a dynamic string based on that provided
  324. * subtituting each occurance of a "${FRED}" type variable sub-string
  325. * with the appropriate value.
  326. */
  327. char *clish_shell_expand(const char *str, clish_shell_var_e vtype, clish_context_t *context)
  328. {
  329. char *seg, *result = NULL;
  330. const char *escape_chars = NULL;
  331. const clish_command_t *cmd = clish_context__get_cmd(context);
  332. /* Escape special characters */
  333. if (SHELL_VAR_REGEX == vtype) {
  334. if (cmd)
  335. escape_chars = clish_command__get_regex_chars(cmd);
  336. if (!escape_chars)
  337. escape_chars = lub_string_esc_regex;
  338. } else if (SHELL_VAR_ACTION == vtype) {
  339. if (cmd)
  340. escape_chars = clish_command__get_escape_chars(cmd);
  341. if (!escape_chars)
  342. escape_chars = lub_string_esc_default;
  343. }
  344. /* read each segment and extend the result */
  345. while ((seg = expand_nextsegment(&str, escape_chars, context))) {
  346. lub_string_cat(&result, seg);
  347. lub_string_free(seg);
  348. }
  349. return result;
  350. }
  351. /*--------------------------------------------------------- */
  352. char *clish_shell__get_params(clish_context_t *context)
  353. {
  354. clish_pargv_t *pargv = clish_context__get_pargv(context);
  355. char *line = NULL;
  356. unsigned i, cnt;
  357. const clish_param_t *param;
  358. const clish_parg_t *parg;
  359. char *request = NULL;
  360. if (!pargv)
  361. return NULL;
  362. cnt = clish_pargv__get_count(pargv);
  363. for (i = 0; i < cnt; i++) {
  364. param = clish_pargv__get_param(pargv, i);
  365. if (clish_param__get_hidden(param))
  366. continue;
  367. parg = clish_pargv__get_parg(pargv, i);
  368. if (request)
  369. lub_string_cat(&request, " ");
  370. lub_string_cat(&request, "${!");
  371. lub_string_cat(&request, clish_parg__get_name(parg));
  372. lub_string_cat(&request, "}");
  373. }
  374. line = clish_shell_expand(request, SHELL_VAR_NONE, context);
  375. lub_string_free(request);
  376. return line;
  377. }
  378. /*--------------------------------------------------------- */
  379. static char *internal_get_line(clish_context_t *context, int cmd_type)
  380. {
  381. const clish_command_t *cmd = clish_context__get_cmd(context);
  382. clish_pargv_t *pargv = clish_context__get_pargv(context);
  383. char *line = NULL;
  384. char *params = NULL;
  385. if (0 == cmd_type) /* __cmd */
  386. lub_string_cat(&line, clish_command__get_name(
  387. clish_command__get_cmd(cmd)));
  388. else /* __full_cmd */
  389. lub_string_cat(&line, clish_command__get_name(cmd));
  390. if (!pargv)
  391. return line;
  392. params = clish_shell__get_params(context);
  393. if (params) {
  394. lub_string_cat(&line, " ");
  395. lub_string_cat(&line, params);
  396. }
  397. lub_string_free(params);
  398. return line;
  399. }
  400. /*--------------------------------------------------------- */
  401. char *clish_shell__get_line(clish_context_t *context)
  402. {
  403. return internal_get_line(context, 0); /* __cmd */
  404. }
  405. /*--------------------------------------------------------- */
  406. char *clish_shell__get_full_line(clish_context_t *context)
  407. {
  408. return internal_get_line(context, 1); /* __full_cmd */
  409. }
  410. /*--------------------------------------------------------- */
  411. char *clish_shell_expand_var(const char *name, clish_context_t *context)
  412. {
  413. return clish_shell_expand_var_ex(name, context, SHELL_EXPAND_ALL);
  414. }
  415. /*----------------------------------------------------------- */
  416. char *clish_shell_expand_var_ex(const char *name, clish_context_t *context, clish_shell_expand_e flags)
  417. {
  418. clish_shell_t *this;
  419. const clish_command_t *cmd;
  420. clish_pargv_t *pargv;
  421. const char *tmp = NULL;
  422. char *string = NULL;
  423. assert(name);
  424. if (!context)
  425. return NULL;
  426. this = clish_context__get_shell(context);
  427. cmd = clish_context__get_cmd(context);
  428. pargv = clish_context__get_pargv(context);
  429. /* try and substitute a parameter value */
  430. if (pargv && (flags & SHELL_EXPAND_PARAM)) {
  431. const clish_parg_t *parg = clish_pargv_find_arg(pargv, name);
  432. if (parg)
  433. tmp = clish_parg__get_value(parg);
  434. }
  435. /* try and substitute the param's default */
  436. if (!tmp && cmd && (flags & SHELL_EXPAND_PARAM))
  437. tmp = clish_paramv_find_default(
  438. clish_command__get_paramv(cmd), name);
  439. /* try and substitute a viewId variable */
  440. if (!tmp && this && (flags & SHELL_EXPAND_VIEW))
  441. tmp = string = find_viewid_var(name, context);
  442. /* try and substitute context fixed variable */
  443. if (!tmp && (flags & SHELL_EXPAND_CONTEXT))
  444. tmp = string = find_context_var(name, context);
  445. /* try and substitute a global var value */
  446. if (!tmp && this && (flags & SHELL_EXPAND_VAR))
  447. tmp = string = find_global_var(name, context);
  448. /* get the contents of an environment variable */
  449. if (!tmp && (flags & SHELL_EXPAND_ENV))
  450. tmp = getenv(name);
  451. if (string)
  452. return string;
  453. return lub_string_dup(tmp);
  454. }
  455. /*----------------------------------------------------------- */