variable_expand.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. /* try and substitute a viewId variable */
  102. if (!tmp) {
  103. if (this && this->viewid)
  104. tmp = string = find_viewid_var(this->viewid, name);
  105. }
  106. /* try and substitute context fixed variable */
  107. if (!tmp)
  108. tmp = string = find_context_var(this, name);
  109. /* get the contents of an environment variable */
  110. if (!tmp)
  111. tmp = getenv(name);
  112. /* override the escape characters */
  113. if (this && this->cmd)
  114. escape_chars = clish_command__get_escape_chars(this->cmd);
  115. result = lub_string_encode(tmp, escape_chars);
  116. /* free the dynamic memory */
  117. if (string)
  118. lub_string_free(string);
  119. return result;
  120. }
  121. /*--------------------------------------------------------- */
  122. /*
  123. * return the next segment of text from the provided string
  124. * segments are delimited by variables within the string.
  125. */
  126. static char *context_nextsegment(const context_t * this, const char **string)
  127. {
  128. const char *p = *string;
  129. char *result = NULL;
  130. size_t len = 0;
  131. if (p) {
  132. if (*p && (p[0] == '$') && (p[1] == '{')) {
  133. /* start of a variable */
  134. const char *tmp;
  135. p += 2;
  136. tmp = p;
  137. /*
  138. * find the end of the variable
  139. */
  140. while (*p && p++[0] != '}') {
  141. len++;
  142. }
  143. /* ignore non-terminated variables */
  144. if (p[-1] == '}') {
  145. bool_t valid = BOOL_FALSE;
  146. char *text, *q;
  147. char *saveptr;
  148. /* get the variable text */
  149. text = lub_string_dupn(tmp, len);
  150. /*
  151. * tokenise this INTO ':' separated words
  152. * and either expand or duplicate into the result string.
  153. * Only return a result if at least
  154. * of the words is an expandable variable
  155. */
  156. for (q = strtok_r(text, ":", &saveptr);
  157. q; q = strtok_r(NULL, ":", &saveptr)) {
  158. char *var = context_retrieve(this, q);
  159. /* copy the expansion or the raw word */
  160. lub_string_cat(&result, var ? var : q);
  161. /* record any expansions */
  162. if (var)
  163. valid = BOOL_TRUE;
  164. lub_string_free(var);
  165. }
  166. if (!valid) {
  167. /* not a valid variable expansion */
  168. lub_string_free(result);
  169. result = lub_string_dup("");
  170. }
  171. /* finished with the variable text */
  172. lub_string_free(text);
  173. }
  174. } else {
  175. /* find the start of a variable */
  176. while (*p) {
  177. if ((p[0] == '$') && (p[1] == '{'))
  178. break;
  179. len++;
  180. p++;
  181. }
  182. if (len > 0)
  183. result = lub_string_dupn(*string, len);
  184. }
  185. /* move the string pointer on for next time... */
  186. *string = p;
  187. }
  188. return result;
  189. }
  190. /*--------------------------------------------------------- */
  191. /*
  192. * This function builds a dynamic string based on that provided
  193. * subtituting each occurance of a "${FRED}" type variable sub-string
  194. * with the appropriate value.
  195. */
  196. char *clish_variable_expand(const char *string, const char *viewid,
  197. const clish_command_t * cmd, clish_pargv_t * pargv)
  198. {
  199. char *seg, *result = NULL;
  200. context_t context;
  201. /* setup the context */
  202. context.viewid = viewid;
  203. context.cmd = cmd;
  204. context.pargv = pargv;
  205. /* read each segment and extend the result */
  206. while (NULL != (seg = context_nextsegment(&context, &string))) {
  207. lub_string_cat(&result, seg);
  208. lub_string_free(seg);
  209. }
  210. return result;
  211. }
  212. /*--------------------------------------------------------- */
  213. char *clish_variable__get_params(const clish_command_t * cmd, clish_pargv_t * pargv)
  214. {
  215. char *line = NULL;
  216. unsigned i, cnt;
  217. const clish_param_t *param;
  218. const clish_parg_t *parg;
  219. if (!pargv)
  220. return NULL;
  221. cnt = clish_pargv__get_count(pargv);
  222. for (i = 0; i < cnt; i++) {
  223. const char *tmp;
  224. char *space = NULL;
  225. param = clish_pargv__get_param(pargv, i);
  226. if (clish_param__get_hidden(param))
  227. continue;
  228. parg = clish_pargv__get_parg(pargv, i);
  229. tmp = clish_parg__get_value(parg);
  230. space = strchr(tmp, ' ');
  231. if (line)
  232. lub_string_cat(&line, " ");
  233. if (space)
  234. lub_string_cat(&line, "\\\"");
  235. lub_string_cat(&line, tmp);
  236. if (space)
  237. lub_string_cat(&line, "\\\"");
  238. }
  239. return line;
  240. }
  241. /*--------------------------------------------------------- */
  242. char *clish_variable__get_line(const clish_command_t * cmd, clish_pargv_t * pargv)
  243. {
  244. char *line = NULL;
  245. char *params = NULL;
  246. lub_string_cat(&line, clish_command__get_name(
  247. clish_command__get_cmd(cmd)));
  248. if (!pargv)
  249. return line;
  250. params = clish_variable__get_params(cmd, pargv);
  251. if (params) {
  252. lub_string_cat(&line, " ");
  253. lub_string_cat(&line, params);
  254. }
  255. lub_string_free(params);
  256. return line;
  257. }
  258. /*--------------------------------------------------------- */
  259. char *clish_variable__get_value(const char *name, const char *viewid,
  260. const clish_command_t * cmd, clish_pargv_t * pargv)
  261. {
  262. context_t context;
  263. /* setup the context */
  264. context.viewid = viewid;
  265. context.cmd = cmd;
  266. context.pargv = pargv;
  267. return context_retrieve(&context, name);
  268. }
  269. /*--------------------------------------------------------- */