1
0

shell_var.c 13 KB

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