variable_expand.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. /* 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(text, ":");
  163. q; q = strtok(NULL, ":")) {
  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_orig(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. /*--------------------------------------------------------- */