alias.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include "cache.h"
  2. #include "util.h"
  3. #include "config.h"
  4. static const char *alias_key;
  5. static char *alias_val;
  6. static int alias_lookup_cb(const char *k, const char *v,
  7. void *cb __maybe_unused)
  8. {
  9. if (!prefixcmp(k, "alias.") && !strcmp(k+6, alias_key)) {
  10. if (!v)
  11. return config_error_nonbool(k);
  12. alias_val = strdup(v);
  13. return 0;
  14. }
  15. return 0;
  16. }
  17. char *alias_lookup(const char *alias)
  18. {
  19. alias_key = alias;
  20. alias_val = NULL;
  21. perf_config(alias_lookup_cb, NULL);
  22. return alias_val;
  23. }
  24. int split_cmdline(char *cmdline, const char ***argv)
  25. {
  26. int src, dst, count = 0, size = 16;
  27. char quoted = 0;
  28. *argv = malloc(sizeof(char*) * size);
  29. /* split alias_string */
  30. (*argv)[count++] = cmdline;
  31. for (src = dst = 0; cmdline[src];) {
  32. char c = cmdline[src];
  33. if (!quoted && isspace(c)) {
  34. cmdline[dst++] = 0;
  35. while (cmdline[++src]
  36. && isspace(cmdline[src]))
  37. ; /* skip */
  38. if (count >= size) {
  39. size += 16;
  40. *argv = realloc(*argv, sizeof(char*) * size);
  41. }
  42. (*argv)[count++] = cmdline + dst;
  43. } else if (!quoted && (c == '\'' || c == '"')) {
  44. quoted = c;
  45. src++;
  46. } else if (c == quoted) {
  47. quoted = 0;
  48. src++;
  49. } else {
  50. if (c == '\\' && quoted != '\'') {
  51. src++;
  52. c = cmdline[src];
  53. if (!c) {
  54. zfree(argv);
  55. return error("cmdline ends with \\");
  56. }
  57. }
  58. cmdline[dst++] = c;
  59. src++;
  60. }
  61. }
  62. cmdline[dst] = 0;
  63. if (quoted) {
  64. zfree(argv);
  65. return error("unclosed quote");
  66. }
  67. return count;
  68. }