shell_var.c 15 KB

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