shell_var.c 14 KB

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