shell_var.c 10 KB

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