shell_var.c 14 KB

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