read_config_header.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #include <ccan/err/err.h>
  2. #include <ccan/str/str.h>
  3. #include <ccan/tal/path/path.h>
  4. #include <ccan/tal/grab_file/grab_file.h>
  5. #include "read_config_header.h"
  6. #include "tools.h"
  7. #include <string.h>
  8. /* Get an identifier token. */
  9. char *get_symbol_token(void *ctx, const char **line)
  10. {
  11. unsigned int toklen;
  12. char *ret;
  13. *line += strspn(*line, " \t");
  14. toklen = strspn(*line, IDENT_CHARS);
  15. if (!toklen)
  16. return NULL;
  17. ret = tal_strndup(ctx, *line, toklen);
  18. *line += toklen;
  19. return ret;
  20. }
  21. /* Get token if it's equal to token. */
  22. bool get_token(const char **line, const char *token)
  23. {
  24. unsigned int toklen;
  25. *line += strspn(*line, " \t");
  26. if (cisalnum(token[0]) || token[0] == '_')
  27. toklen = strspn(*line, IDENT_CHARS);
  28. else {
  29. /* FIXME: real tokenizer handles ++ and other multi-chars. */
  30. toklen = strlen(token);
  31. }
  32. if (toklen == strlen(token) && !strncmp(*line, token, toklen)) {
  33. *line += toklen;
  34. return true;
  35. }
  36. return false;
  37. }
  38. static char *demangle_string(char *string)
  39. {
  40. unsigned int i;
  41. const char mapfrom[] = "abfnrtv";
  42. const char mapto[] = "\a\b\f\n\r\t\v";
  43. if (!strchr(string, '"'))
  44. return NULL;
  45. string = strchr(string, '"') + 1;
  46. if (!strrchr(string, '"'))
  47. return NULL;
  48. *strrchr(string, '"') = '\0';
  49. for (i = 0; i < strlen(string); i++) {
  50. if (string[i] == '\\') {
  51. char repl;
  52. unsigned len = 0;
  53. const char *p = strchr(mapfrom, string[i+1]);
  54. if (p) {
  55. repl = mapto[p - mapfrom];
  56. len = 1;
  57. } else if (strlen(string+i+1) >= 3) {
  58. if (string[i+1] == 'x') {
  59. repl = (string[i+2]-'0')*16
  60. + string[i+3]-'0';
  61. len = 3;
  62. } else if (cisdigit(string[i+1])) {
  63. repl = (string[i+2]-'0')*8*8
  64. + (string[i+3]-'0')*8
  65. + (string[i+4]-'0');
  66. len = 3;
  67. }
  68. }
  69. if (len == 0) {
  70. repl = string[i+1];
  71. len = 1;
  72. }
  73. string[i] = repl;
  74. memmove(string + i + 1, string + i + len + 1,
  75. strlen(string + i + len + 1) + 1);
  76. }
  77. }
  78. return string;
  79. }
  80. char *read_config_header(const char *ccan_dir, bool verbose)
  81. {
  82. char *fname = path_join(NULL, ccan_dir, "config.h");
  83. char **lines;
  84. unsigned int i;
  85. char *config_header;
  86. config_header = grab_file(NULL, fname);
  87. tal_free(fname);
  88. if (!config_header)
  89. return NULL;
  90. lines = tal_strsplit(config_header, config_header, "\n", STR_EMPTY_OK);
  91. for (i = 0; i < tal_count(lines) - 1; i++) {
  92. char *sym;
  93. const char **line = (const char **)&lines[i];
  94. if (!get_token(line, "#"))
  95. continue;
  96. if (!get_token(line, "define"))
  97. continue;
  98. sym = get_symbol_token(lines, line);
  99. if (streq(sym, "CCAN_COMPILER")) {
  100. compiler = demangle_string(lines[i]);
  101. if (!compiler)
  102. errx(1, "%s:%u:could not parse CCAN_COMPILER",
  103. fname, i+1);
  104. if (verbose)
  105. printf("%s: compiler set to '%s'\n",
  106. fname, compiler);
  107. } else if (streq(sym, "CCAN_CFLAGS")) {
  108. cflags = demangle_string(lines[i]);
  109. if (!cflags)
  110. errx(1, "%s:%u:could not parse CCAN_CFLAGS",
  111. fname, i+1);
  112. if (verbose)
  113. printf("%s: compiler flags set to '%s'\n",
  114. fname, cflags);
  115. }
  116. }
  117. return config_header;
  118. }