1
0

shell_var.c 11 KB

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