alias.c 1.4 KB

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