variable_expand.c 7.9 KB

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