variable_expand.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. * shell_variable_expand.c
  3. */
  4. #include "private.h"
  5. #include "lub/string.h"
  6. #include <stdlib.h>
  7. #include <assert.h>
  8. #include <string.h>
  9. #include <sys/types.h>
  10. #include <regex.h>
  11. /*----------------------------------------------------------- */
  12. /*
  13. * search the current viewid string for a variable
  14. */
  15. static char *find_viewid_var(const char *viewid, const char *name)
  16. {
  17. char *result = NULL;
  18. regex_t regex;
  19. int status;
  20. char *pattern = NULL;
  21. regmatch_t pmatches[2];
  22. /* build up the pattern to match */
  23. lub_string_cat(&pattern, name);
  24. lub_string_cat(&pattern, "[ ]*=([^;]*)");
  25. /* compile the regular expression to find this variable */
  26. status = regcomp(&regex, pattern, REG_EXTENDED);
  27. assert(0 == status);
  28. lub_string_free(pattern);
  29. /* now perform the matching */
  30. /*lint -e64 Type mismatch (arg. no. 4) */
  31. /*
  32. * lint seems to equate regmatch_t[] as being of type regmatch_t !!!
  33. */
  34. status = regexec(&regex, viewid, 2, pmatches, 0);
  35. /*lint +e64 */
  36. if (0 == status) {
  37. regoff_t len = pmatches[1].rm_eo - pmatches[1].rm_so;
  38. const char *value = &viewid[pmatches[1].rm_so];
  39. /* found a match */
  40. result = lub_string_dupn(value, (unsigned)len);
  41. }
  42. /* release the regular expression */
  43. regfree(&regex);
  44. return result;
  45. }
  46. /*----------------------------------------------------------- */
  47. /*
  48. * expand context dependent fixed-name variables
  49. */
  50. static char *find_context_var(const context_t * this, const char *name)
  51. {
  52. char *result = NULL;
  53. if (!this->cmd)
  54. return NULL;
  55. if (!lub_string_nocasecmp(name, "__full_cmd")) {
  56. result = lub_string_dup(clish_command__get_name(this->cmd));
  57. } else if (!lub_string_nocasecmp(name, "__cmd")) {
  58. result = lub_string_dup(clish_command__get_name(
  59. clish_command__get_cmd(this->cmd)));
  60. } else if (!lub_string_nocasecmp(name, "__orig_cmd")) {
  61. result = lub_string_dup(clish_command__get_name(
  62. clish_command__get_orig(this->cmd)));
  63. } else if (!lub_string_nocasecmp(name, "__line")) {
  64. if (this->pargv)
  65. result = clish_variable__get_line(this->cmd, this->pargv);
  66. } else if (!lub_string_nocasecmp(name, "__params")) {
  67. if (this->pargv)
  68. result = clish_variable__get_params(this->cmd, this->pargv);
  69. } else if (lub_string_nocasestr(name, "__prefix") == name) {
  70. int idx = 0;
  71. int pnum = 0;
  72. pnum = lub_argv_wordcount(clish_command__get_name(this->cmd)) -
  73. lub_argv_wordcount(clish_command__get_name(
  74. clish_command__get_cmd(this->cmd)));
  75. idx = atoi(name + strlen("__prefix"));
  76. if (idx < pnum) {
  77. lub_argv_t *argv = lub_argv_new(
  78. clish_command__get_name(this->cmd), 0);
  79. result = lub_string_dup(lub_argv__get_arg(argv, idx));
  80. lub_argv_delete(argv);
  81. }
  82. }
  83. return result;
  84. }
  85. /*--------------------------------------------------------- */
  86. static char *context_retrieve(const context_t * this, const char *name)
  87. {
  88. char *result = NULL;
  89. const char *tmp = NULL;
  90. const char *escape_chars = NULL;
  91. char *string = NULL;
  92. assert(name);
  93. /* try and substitute a parameter value */
  94. if (this && this->pargv) {
  95. const clish_parg_t *parg =
  96. clish_pargv_find_arg(this->pargv, name);
  97. /* substitute the command line value */
  98. if (parg)
  99. tmp = clish_parg__get_value(parg);
  100. }
  101. if (NULL == tmp) {
  102. /* try and substitute a viewId variable */
  103. if (this && this->viewid)
  104. tmp = string = find_viewid_var(this->viewid, name);
  105. }
  106. if (NULL == tmp) {
  107. /* try and substitute context fixed variable */
  108. tmp = string = find_context_var(this, name);
  109. }
  110. /* get the contents of an environment variable */
  111. if (NULL == tmp) {
  112. tmp = getenv(name);
  113. }
  114. if (this && this->cmd) {
  115. /* override the escape characters */
  116. escape_chars = clish_command__get_escape_chars(this->cmd);
  117. }
  118. result = lub_string_encode(tmp, escape_chars);
  119. if (string) {
  120. /* free the dynamic memory */
  121. lub_string_free(string);
  122. }
  123. return result;
  124. }
  125. /*--------------------------------------------------------- */
  126. /*
  127. * return the next segment of text from the provided string
  128. * segments are delimited by variables within the string.
  129. */
  130. static char *context_nextsegment(const context_t * this, const char **string)
  131. {
  132. const char *p = *string;
  133. char *result = NULL;
  134. size_t len = 0;
  135. if (p) {
  136. if (*p && (p[0] == '$') && (p[1] == '{')) {
  137. /* start of a variable */
  138. const char *tmp;
  139. p += 2;
  140. tmp = p;
  141. /*
  142. * find the end of the variable
  143. */
  144. while (*p && p++[0] != '}') {
  145. len++;
  146. }
  147. /* ignore non-terminated variables */
  148. if (p[-1] == '}') {
  149. bool_t valid = BOOL_FALSE;
  150. char *text, *q;
  151. char *saveptr;
  152. /* get the variable text */
  153. text = lub_string_dupn(tmp, len);
  154. /*
  155. * tokenise this INTO ':' separated words
  156. * and either expand or duplicate into the result string.
  157. * Only return a result if at least
  158. * of the words is an expandable variable
  159. */
  160. for (q = strtok_r(text, ":", &saveptr);
  161. q; q = strtok_r(NULL, ":", &saveptr)) {
  162. char *var = context_retrieve(this, q);
  163. /* copy the expansion or the raw word */
  164. lub_string_cat(&result, var ? var : q);
  165. /* record any expansions */
  166. if (var)
  167. valid = BOOL_TRUE;
  168. lub_string_free(var);
  169. }
  170. if (BOOL_FALSE == valid) {
  171. /* not a valid variable expansion */
  172. lub_string_free(result);
  173. result = lub_string_dup("");
  174. }
  175. /* finished with the variable text */
  176. lub_string_free(text);
  177. }
  178. } else {
  179. /* find the start of a variable */
  180. while (*p) {
  181. if ((p[0] == '$') && (p[1] == '{'))
  182. break;
  183. len++;
  184. p++;
  185. }
  186. if (len > 0)
  187. result = lub_string_dupn(*string, len);
  188. }
  189. /* move the string pointer on for next time... */
  190. *string = p;
  191. }
  192. return result;
  193. }
  194. /*--------------------------------------------------------- */
  195. /*
  196. * This function builds a dynamic string based on that provided
  197. * subtituting each occurance of a "${FRED}" type variable sub-string
  198. * with the appropriate value.
  199. */
  200. char *clish_variable_expand(const char *string, const char *viewid,
  201. const clish_command_t * cmd, clish_pargv_t * pargv)
  202. {
  203. char *seg, *result = NULL;
  204. context_t context;
  205. /* setup the context */
  206. context.viewid = viewid;
  207. context.cmd = cmd;
  208. context.pargv = pargv;
  209. /* read each segment and extend the result */
  210. while (NULL != (seg = context_nextsegment(&context, &string))) {
  211. lub_string_cat(&result, seg);
  212. lub_string_free(seg);
  213. }
  214. return result;
  215. }
  216. /*--------------------------------------------------------- */
  217. char *clish_variable__get_params(const clish_command_t * cmd, clish_pargv_t * pargv)
  218. {
  219. char *line = NULL;
  220. unsigned i, cnt;
  221. const clish_param_t *param;
  222. const clish_parg_t *parg;
  223. if (!pargv)
  224. return NULL;
  225. cnt = clish_pargv__get_count(pargv);
  226. for (i = 0; i < cnt; i++) {
  227. const char *tmp;
  228. char *space = NULL;
  229. param = clish_pargv__get_param(pargv, i);
  230. if (clish_param__get_hidden(param))
  231. continue;
  232. parg = clish_pargv__get_parg(pargv, i);
  233. tmp = clish_parg__get_value(parg);
  234. space = strchr(tmp, ' ');
  235. if (NULL != line)
  236. lub_string_cat(&line, " ");
  237. if (space)
  238. lub_string_cat(&line, "\\\"");
  239. lub_string_cat(&line, tmp);
  240. if (space)
  241. lub_string_cat(&line, "\\\"");
  242. }
  243. return line;
  244. }
  245. /*--------------------------------------------------------- */
  246. char *clish_variable__get_line(const clish_command_t * cmd, clish_pargv_t * pargv)
  247. {
  248. char *line = NULL;
  249. char *params = NULL;
  250. lub_string_cat(&line, clish_command__get_name(
  251. clish_command__get_cmd(cmd)));
  252. if (!pargv)
  253. return line;
  254. params = clish_variable__get_params(cmd, pargv);
  255. if (params) {
  256. lub_string_cat(&line, " ");
  257. lub_string_cat(&line, params);
  258. }
  259. lub_string_free(params);
  260. return line;
  261. }
  262. /*--------------------------------------------------------- */
  263. char *clish_variable__get_value(const char *name, const char *viewid,
  264. const clish_command_t * cmd, clish_pargv_t * pargv)
  265. {
  266. context_t context;
  267. /* setup the context */
  268. context.viewid = viewid;
  269. context.cmd = cmd;
  270. context.pargv = pargv;
  271. return context_retrieve(&context, name);
  272. }
  273. /*--------------------------------------------------------- */