shell_var.c 10 KB

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