shell_var.c 14 KB

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