1
0

variable_expand.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 the param's default */
  102. if (!tmp) {
  103. if (this && this->cmd)
  104. tmp = clish_paramv_find_default(
  105. clish_command__get_paramv(this->cmd), name);
  106. }
  107. /* try and substitute a viewId variable */
  108. if (!tmp) {
  109. if (this && this->viewid)
  110. tmp = string = find_viewid_var(this->viewid, name);
  111. }
  112. /* try and substitute context fixed variable */
  113. if (!tmp)
  114. tmp = string = find_context_var(this, name);
  115. /* get the contents of an environment variable */
  116. if (!tmp)
  117. tmp = getenv(name);
  118. /* override the escape characters */
  119. if (this && this->cmd)
  120. escape_chars = clish_command__get_escape_chars(this->cmd);
  121. result = lub_string_encode(tmp, escape_chars);
  122. /* free the dynamic memory */
  123. if (string)
  124. lub_string_free(string);
  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 (var)
  169. valid = BOOL_TRUE;
  170. lub_string_free(var);
  171. }
  172. if (!valid) {
  173. /* not a valid variable expansion */
  174. lub_string_free(result);
  175. result = lub_string_dup("");
  176. }
  177. /* finished with the variable text */
  178. lub_string_free(text);
  179. }
  180. } else {
  181. /* find the start of a variable */
  182. while (*p) {
  183. if ((p[0] == '$') && (p[1] == '{'))
  184. break;
  185. len++;
  186. p++;
  187. }
  188. if (len > 0)
  189. result = lub_string_dupn(*string, len);
  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, const char *viewid,
  203. const clish_command_t * cmd, clish_pargv_t * pargv)
  204. {
  205. char *seg, *result = NULL;
  206. context_t context;
  207. /* setup the context */
  208. context.viewid = viewid;
  209. context.cmd = cmd;
  210. context.pargv = pargv;
  211. /* read each segment and extend the result */
  212. while (NULL != (seg = context_nextsegment(&context, &string))) {
  213. lub_string_cat(&result, seg);
  214. lub_string_free(seg);
  215. }
  216. return result;
  217. }
  218. /*--------------------------------------------------------- */
  219. char *clish_variable__get_params(const clish_command_t * cmd, clish_pargv_t * pargv)
  220. {
  221. char *line = NULL;
  222. unsigned i, cnt;
  223. const clish_param_t *param;
  224. const clish_parg_t *parg;
  225. if (!pargv)
  226. return NULL;
  227. cnt = clish_pargv__get_count(pargv);
  228. for (i = 0; i < cnt; i++) {
  229. const char *tmp;
  230. char *space = NULL;
  231. param = clish_pargv__get_param(pargv, i);
  232. if (clish_param__get_hidden(param))
  233. continue;
  234. parg = clish_pargv__get_parg(pargv, i);
  235. tmp = clish_parg__get_value(parg);
  236. space = strchr(tmp, ' ');
  237. if (line)
  238. lub_string_cat(&line, " ");
  239. if (space)
  240. lub_string_cat(&line, "\\\"");
  241. lub_string_cat(&line, tmp);
  242. if (space)
  243. lub_string_cat(&line, "\\\"");
  244. }
  245. return line;
  246. }
  247. /*--------------------------------------------------------- */
  248. char *clish_variable__get_line(const clish_command_t * cmd, clish_pargv_t * pargv)
  249. {
  250. char *line = NULL;
  251. char *params = NULL;
  252. lub_string_cat(&line, clish_command__get_name(
  253. clish_command__get_cmd(cmd)));
  254. if (!pargv)
  255. return line;
  256. params = clish_variable__get_params(cmd, pargv);
  257. if (params) {
  258. lub_string_cat(&line, " ");
  259. lub_string_cat(&line, params);
  260. }
  261. lub_string_free(params);
  262. return line;
  263. }
  264. /*--------------------------------------------------------- */
  265. char *clish_variable__get_value(const char *name, const char *viewid,
  266. const clish_command_t * cmd, clish_pargv_t * pargv)
  267. {
  268. context_t context;
  269. /* setup the context */
  270. context.viewid = viewid;
  271. context.cmd = cmd;
  272. context.pargv = pargv;
  273. return context_retrieve(&context, name);
  274. }
  275. /*--------------------------------------------------------- */