variable_expand.c 7.8 KB

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