shell_var.c 14 KB

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