1
0

shell_var.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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 (!shell || !shell->tinyrl)
  50. return NULL;
  51. if (!lub_string_nocasecmp(name, "_width")) {
  52. char tmp[5];
  53. snprintf(tmp, sizeof(tmp), "%u",
  54. tinyrl__get_width(shell->tinyrl));
  55. tmp[sizeof(tmp) - 1] = '\0';
  56. result = strdup(tmp);
  57. } else if (!lub_string_nocasecmp(name, "_height")) {
  58. char tmp[5];
  59. snprintf(tmp, sizeof(tmp), "%u",
  60. tinyrl__get_height(shell->tinyrl));
  61. tmp[sizeof(tmp) - 1] = '\0';
  62. result = strdup(tmp);
  63. } else if (!lub_string_nocasecmp(name, "_watchdog_timeout")) {
  64. char tmp[5];
  65. snprintf(tmp, sizeof(tmp), "%u", shell->wdog_timeout);
  66. tmp[sizeof(tmp) - 1] = '\0';
  67. result = strdup(tmp);
  68. } else if (!this->cmd) { /* The vars dependent on command */
  69. return NULL;
  70. } else if (!lub_string_nocasecmp(name, "_full_cmd")) {
  71. result = lub_string_dup(clish_command__get_name(this->cmd));
  72. } else if (!lub_string_nocasecmp(name, "_cmd")) {
  73. result = lub_string_dup(clish_command__get_name(
  74. clish_command__get_cmd(this->cmd)));
  75. } else if (!lub_string_nocasecmp(name, "_orig_cmd")) {
  76. result = lub_string_dup(clish_command__get_name(
  77. clish_command__get_orig(this->cmd)));
  78. } else if (!lub_string_nocasecmp(name, "_line")) {
  79. result = clish_shell__get_line(this);
  80. } else if (!lub_string_nocasecmp(name, "_full_line")) {
  81. result = clish_shell__get_full_line(this);
  82. } else if (!lub_string_nocasecmp(name, "_params")) {
  83. if (this->pargv)
  84. result = clish_shell__get_params(this);
  85. } else if (!lub_string_nocasecmp(name, "_interactive")) {
  86. if (clish_shell__get_interactive(this->shell))
  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, "_pid")) {
  96. char tmp[10];
  97. snprintf(tmp, sizeof(tmp), "%u", getpid());
  98. tmp[sizeof(tmp) - 1] = '\0';
  99. result = strdup(tmp);
  100. } else if (lub_string_nocasestr(name, "_prefix") == name) {
  101. int idx = 0;
  102. int pnum = 0;
  103. pnum = lub_argv_wordcount(clish_command__get_name(this->cmd)) -
  104. lub_argv_wordcount(clish_command__get_name(
  105. clish_command__get_cmd(this->cmd)));
  106. idx = atoi(name + strlen("_prefix"));
  107. if (idx < pnum) {
  108. lub_argv_t *argv = lub_argv_new(
  109. clish_command__get_name(this->cmd), 0);
  110. result = lub_string_dup(lub_argv__get_arg(argv, idx));
  111. lub_argv_delete(argv);
  112. }
  113. } else if (!lub_string_nocasecmp(name, "_cur_depth")) {
  114. char tmp[10];
  115. int depth = clish_shell__get_depth(shell);
  116. snprintf(tmp, sizeof(tmp), "%u", depth);
  117. tmp[sizeof(tmp) - 1] = '\0';
  118. result = strdup(tmp);
  119. } else if (!lub_string_nocasecmp(name, "_cur_pwd")) {
  120. int depth = clish_shell__get_depth(shell);
  121. result = clish_shell__get_pwd_full(shell, depth);
  122. }
  123. return result;
  124. }
  125. /*--------------------------------------------------------- */
  126. static char *find_var(const char *name, lub_bintree_t *tree, clish_context_t *context)
  127. {
  128. clish_var_t *var = lub_bintree_find(tree, name);
  129. char *value;
  130. bool_t dynamic;
  131. char *res = NULL;
  132. if (!var)
  133. return NULL;
  134. /* Try to get saved value for static var */
  135. dynamic = clish_var__get_dynamic(var);
  136. if (!dynamic) {
  137. char *saved = clish_var__get_saved(var);
  138. if (saved)
  139. return lub_string_dup(saved);
  140. }
  141. /* Try to expand value field */
  142. value = clish_var__get_value(var);
  143. if (value)
  144. res = clish_shell_expand(value, SHELL_VAR_NONE, context);
  145. /* Try to execute ACTION */
  146. if (!res) {
  147. char *out = NULL;
  148. clish_action_t *action = clish_var__get_action(var);
  149. if (clish_shell_exec_action(action, context, &out)) {
  150. lub_string_free(out);
  151. return NULL;
  152. }
  153. res = out;
  154. }
  155. /* Save value for static var */
  156. if (!dynamic && res)
  157. clish_var__set_saved(var, res);
  158. return res;
  159. }
  160. /*--------------------------------------------------------- */
  161. static char *find_global_var(const char *name, clish_context_t *context)
  162. {
  163. return find_var(name, &context->shell->var_tree, context);
  164. }
  165. /*--------------------------------------------------------- */
  166. static char *find_viewid_var(const char *name, clish_context_t *context)
  167. {
  168. int depth = clish_shell__get_depth(context->shell);
  169. if (depth < 0)
  170. return NULL;
  171. return find_var(name, &context->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_t vtype, clish_context_t *context)
  328. {
  329. char *seg, *result = NULL;
  330. const char *escape_chars = NULL;
  331. const clish_command_t *cmd = context->cmd;
  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 = context->pargv;
  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 = context->cmd;
  382. clish_pargv_t *pargv = context->pargv;
  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. clish_shell_t *this;
  414. const clish_command_t *cmd;
  415. clish_pargv_t *pargv;
  416. const char *tmp = NULL;
  417. char *string = NULL;
  418. assert(name);
  419. if (!context)
  420. return NULL;
  421. this = context->shell;
  422. cmd = context->cmd;
  423. pargv = context->pargv;
  424. /* try and substitute a parameter value */
  425. if (pargv) {
  426. const clish_parg_t *parg = clish_pargv_find_arg(pargv, name);
  427. if (parg)
  428. tmp = clish_parg__get_value(parg);
  429. }
  430. /* try and substitute the param's default */
  431. if (!tmp && cmd)
  432. tmp = clish_paramv_find_default(
  433. clish_command__get_paramv(cmd), name);
  434. /* try and substitute a viewId variable */
  435. if (!tmp && this)
  436. tmp = string = find_viewid_var(name, context);
  437. /* try and substitute context fixed variable */
  438. if (!tmp)
  439. tmp = string = find_context_var(name, context);
  440. /* try and substitute a global var value */
  441. if (!tmp && this)
  442. tmp = string = find_global_var(name, context);
  443. /* get the contents of an environment variable */
  444. if (!tmp)
  445. tmp = getenv(name);
  446. if (string)
  447. return string;
  448. return lub_string_dup(tmp);
  449. }
  450. /*----------------------------------------------------------- */