shell_var.c 13 KB

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