variable_expand.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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_orig(this->cmd)));
  60. } else if (!lub_string_nocasecmp(name, "__line")) {
  61. if (this->pargv)
  62. result = clish_variable__get_line(this->cmd, this->pargv);
  63. } else if (!lub_string_nocasecmp(name, "__params")) {
  64. if (this->pargv)
  65. result = clish_variable__get_params(this->cmd, this->pargv);
  66. } else if (lub_string_nocasestr(name, "__prefix") == name) {
  67. int idx = 0;
  68. int pnum = 0;
  69. pnum = lub_argv_wordcount(clish_command__get_name(this->cmd)) -
  70. lub_argv_wordcount(clish_command__get_name(
  71. clish_command__get_orig(this->cmd)));
  72. idx = atoi(name + strlen("__prefix"));
  73. if (idx < pnum) {
  74. lub_argv_t *argv = lub_argv_new(
  75. clish_command__get_name(this->cmd), 0);
  76. result = lub_string_dup(lub_argv__get_arg(argv, idx));
  77. lub_argv_delete(argv);
  78. }
  79. }
  80. return result;
  81. }
  82. /*--------------------------------------------------------- */
  83. static char *context_retrieve(const context_t * this, const char *name)
  84. {
  85. char *result = NULL;
  86. const char *tmp = NULL;
  87. const char *escape_chars = NULL;
  88. char *string = NULL;
  89. assert(name);
  90. /* try and substitute a parameter value */
  91. if (this && this->pargv) {
  92. const clish_parg_t *parg =
  93. clish_pargv_find_arg(this->pargv, name);
  94. if (NULL != parg) {
  95. /* substitute the command line value */
  96. tmp = clish_parg__get_value(parg);
  97. }
  98. }
  99. if (NULL == tmp) {
  100. /* try and substitute a viewId variable */
  101. if (this && this->viewid) {
  102. tmp = string = find_viewid_var(this->viewid, name);
  103. }
  104. }
  105. if (NULL == tmp) {
  106. /* try and substitute context fixed variable */
  107. tmp = string = find_context_var(this, name);
  108. }
  109. /* get the contents of an environment variable */
  110. if (NULL == tmp) {
  111. tmp = getenv(name);
  112. }
  113. if (this && this->cmd) {
  114. /* override the escape characters */
  115. escape_chars = clish_command__get_escape_chars(this->cmd);
  116. }
  117. result = lub_string_encode(tmp, escape_chars);
  118. if (NULL != string) {
  119. /* free the dynamic memory */
  120. lub_string_free(string);
  121. }
  122. return result;
  123. }
  124. /*--------------------------------------------------------- */
  125. /*
  126. * return the next segment of text from the provided string
  127. * segments are delimited by variables within the string.
  128. */
  129. static char *context_nextsegment(const context_t * this, const char **string)
  130. {
  131. const char *p = *string;
  132. char *result = NULL;
  133. size_t len = 0;
  134. if (p) {
  135. if (*p && (p[0] == '$') && (p[1] == '{')) {
  136. /* start of a variable */
  137. const char *tmp;
  138. p += 2;
  139. tmp = p;
  140. /*
  141. * find the end of the variable
  142. */
  143. while (*p && p++[0] != '}') {
  144. len++;
  145. }
  146. /* ignore non-terminated variables */
  147. if (p[-1] == '}') {
  148. bool_t valid = BOOL_FALSE;
  149. char *text, *q;
  150. char *saveptr;
  151. /* get the variable text */
  152. text = lub_string_dupn(tmp, len);
  153. /*
  154. * tokenise this INTO ':' separated words
  155. * and either expand or duplicate into the result string.
  156. * Only return a result if at least
  157. * of the words is an expandable variable
  158. */
  159. for (q = strtok_r(text, ":", &saveptr);
  160. q; q = strtok_r(NULL, ":", &saveptr)) {
  161. char *var = context_retrieve(this, q);
  162. /* copy the expansion or the raw word */
  163. lub_string_cat(&result, var ? var : q);
  164. /* record any expansions */
  165. if (NULL != var) {
  166. valid = BOOL_TRUE;
  167. }
  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. }
  184. len++;
  185. p++;
  186. }
  187. if (len > 0) {
  188. result = lub_string_dupn(*string, len);
  189. }
  190. }
  191. /* move the string pointer on for next time... */
  192. *string = p;
  193. }
  194. return result;
  195. }
  196. /*--------------------------------------------------------- */
  197. /*
  198. * This function builds a dynamic string based on that provided
  199. * subtituting each occurance of a "${FRED}" type variable sub-string
  200. * with the appropriate value.
  201. */
  202. char *clish_variable_expand(const char *string,
  203. const char *viewid,
  204. const clish_command_t * cmd, clish_pargv_t * pargv)
  205. {
  206. char *seg, *result = NULL;
  207. context_t context;
  208. /* setup the context */
  209. context.viewid = viewid;
  210. context.cmd = cmd;
  211. context.pargv = pargv;
  212. /* read each segment and extend the result */
  213. while (NULL != (seg = context_nextsegment(&context, &string))) {
  214. lub_string_cat(&result, seg);
  215. lub_string_free(seg);
  216. }
  217. return result;
  218. }
  219. /*--------------------------------------------------------- */
  220. char *clish_variable__get_params(const clish_command_t * cmd, clish_pargv_t * pargv)
  221. {
  222. char *line = NULL;
  223. unsigned i, cnt;
  224. const clish_param_t *param;
  225. const clish_parg_t *parg;
  226. if (!pargv)
  227. return NULL;
  228. cnt = clish_pargv__get_count(pargv);
  229. for (i = 0; i < cnt; i++) {
  230. const char *tmp;
  231. char *space = NULL;
  232. param = clish_pargv__get_param(pargv, i);
  233. if (clish_param__get_hidden(param))
  234. continue;
  235. parg = clish_pargv__get_parg(pargv, i);
  236. tmp = clish_parg__get_value(parg);
  237. space = strchr(tmp, ' ');
  238. if (NULL != line)
  239. lub_string_cat(&line, " ");
  240. if (space)
  241. lub_string_cat(&line, "\\\"");
  242. lub_string_cat(&line, tmp);
  243. if (space)
  244. lub_string_cat(&line, "\\\"");
  245. }
  246. return line;
  247. }
  248. /*--------------------------------------------------------- */
  249. char *clish_variable__get_line(const clish_command_t * cmd, clish_pargv_t * pargv)
  250. {
  251. char *line = NULL;
  252. char *params = NULL;
  253. lub_string_cat(&line, clish_command__get_name(
  254. clish_command__get_orig(cmd)));
  255. if (!pargv)
  256. return line;
  257. params = clish_variable__get_params(cmd, pargv);
  258. if (params) {
  259. lub_string_cat(&line, " ");
  260. lub_string_cat(&line, params);
  261. }
  262. lub_string_free(params);
  263. return line;
  264. }
  265. /*--------------------------------------------------------- */
  266. char *clish_variable__get_value(const char *name, const char *viewid,
  267. const clish_command_t * cmd, clish_pargv_t * pargv)
  268. {
  269. context_t context;
  270. /* setup the context */
  271. context.viewid = viewid;
  272. context.cmd = cmd;
  273. context.pargv = pargv;
  274. return context_retrieve(&context, name);
  275. }
  276. /*--------------------------------------------------------- */