namespacize.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. /* Code to move a ccan module into the ccan_ namespace. */
  2. #include <errno.h>
  3. #include <string.h>
  4. #include <stdbool.h>
  5. #include <ctype.h>
  6. #include <sys/types.h>
  7. #include <dirent.h>
  8. #include <unistd.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <fcntl.h>
  12. #include "ccan/str/str.h"
  13. #include "ccan/take/take.h"
  14. #include "ccan/rbuf/rbuf.h"
  15. #include "ccan/err/err.h"
  16. #include "tools.h"
  17. static bool verbose = false;
  18. static int indent = 0;
  19. #define verbose(args...) \
  20. do { if (verbose) { \
  21. unsigned int _i; \
  22. for (_i = 0; _i < indent; _i++) printf(" "); \
  23. printf(args); \
  24. } \
  25. } while(0)
  26. #define verbose_indent() (indent += 2)
  27. #define verbose_unindent() (indent -= 2)
  28. static int unlink_no_errno(const char *filename)
  29. {
  30. int ret = 0, serrno = errno;
  31. if (unlink(filename) < 0)
  32. ret = errno;
  33. errno = serrno;
  34. return ret;
  35. }
  36. static char **get_dir(const char *dir)
  37. {
  38. DIR *d;
  39. struct dirent *ent;
  40. char **names = tal_arr(NULL, char *, 0), *n;
  41. d = opendir(dir);
  42. if (!d)
  43. return NULL;
  44. while ((ent = readdir(d)) != NULL) {
  45. n = tal_fmt(names, "%s/%s", dir, ent->d_name);
  46. tal_expand(&names, &n, 1);
  47. }
  48. n = NULL;
  49. tal_expand(&names, &n, 1);
  50. closedir(d);
  51. return names;
  52. }
  53. struct replace
  54. {
  55. struct replace *next;
  56. char *string;
  57. };
  58. static void __attribute__((noreturn)) usage(void)
  59. {
  60. errx(1, "Usage:\n"
  61. "namespacize [--verbose] <dir>\n"
  62. "namespacize [--verbose] --adjust <dir>...\n"
  63. "The first form converts dir/ to insert 'ccan_' prefixes, and\n"
  64. "then adjusts any other ccan directories at the same level which\n"
  65. "are effected.\n"
  66. "--adjust does an adjustment for each directory, in case a\n"
  67. "dependency has been namespacized\n");
  68. }
  69. static void add_replace(struct replace **repl, const char *str)
  70. {
  71. struct replace *new, *i;
  72. /* Avoid duplicates. */
  73. for (i = *repl; i; i = i->next)
  74. if (streq(i->string, str))
  75. return;
  76. new = tal(*repl, struct replace);
  77. new->next = *repl;
  78. new->string = tal_strdup(new, str);
  79. *repl = new;
  80. }
  81. static void add_replace_tok(struct replace **repl, const char *s)
  82. {
  83. struct replace *new;
  84. unsigned int len = strspn(s, IDENT_CHARS);
  85. new = tal(*repl, struct replace);
  86. new->next = *repl;
  87. new->string = tal_strndup(new, s, len);
  88. *repl = new;
  89. }
  90. static void look_for_macros(char *contents, struct replace **repl)
  91. {
  92. char *p;
  93. enum { LINESTART, HASH, DEFINE, NONE } state = LINESTART;
  94. /* Look for lines of form #define X */
  95. for (p = contents; *p; p++) {
  96. if (*p == '\n')
  97. state = LINESTART;
  98. else if (!cisspace(*p)) {
  99. if (state == LINESTART && *p == '#')
  100. state = HASH;
  101. else if (state==HASH && !strncmp(p, "define", 6)) {
  102. state = DEFINE;
  103. p += 5;
  104. } else if (state == DEFINE) {
  105. unsigned int len;
  106. len = strspn(p, IDENT_CHARS);
  107. if (len) {
  108. char *s;
  109. s = tal_strndup(contents, p, len);
  110. /* Don't wrap idempotent wrappers */
  111. if (!strstarts(s, "CCAN_")) {
  112. verbose("Found %s\n", s);
  113. add_replace(repl, s);
  114. }
  115. }
  116. state = NONE;
  117. } else
  118. state = NONE;
  119. }
  120. }
  121. }
  122. /* Blank out preprocessor lines, and eliminate \ */
  123. static void preprocess(char *p)
  124. {
  125. char *s;
  126. /* We assume backslashes are only used for macros. */
  127. while ((s = strstr(p, "\\\n")) != NULL)
  128. s[0] = s[1] = ' ';
  129. /* Now eliminate # lines. */
  130. if (p[0] == '#') {
  131. unsigned int i;
  132. for (i = 0; p[i] != '\n'; i++)
  133. p[i] = ' ';
  134. }
  135. while ((s = strstr(p, "\n#")) != NULL) {
  136. unsigned int i;
  137. for (i = 1; s[i] != '\n'; i++)
  138. s[i] = ' ';
  139. }
  140. }
  141. static char *get_statement(const void *ctx, char **p)
  142. {
  143. unsigned brackets = 0;
  144. bool seen_brackets = false;
  145. char *answer = tal_strdup(ctx, "");
  146. for (;;) {
  147. if ((*p)[0] == '/' && (*p)[1] == '/')
  148. *p += strcspn(*p, "\n");
  149. else if ((*p)[0] == '/' && (*p)[1] == '*')
  150. *p = strstr(*p, "*/") + 1;
  151. else {
  152. char c = **p;
  153. if (c == ';' && !brackets) {
  154. (*p)++;
  155. return answer;
  156. }
  157. /* Compress whitespace into a single ' ' */
  158. if (cisspace(c)) {
  159. c = ' ';
  160. while (cisspace((*p)[1]))
  161. (*p)++;
  162. } else if (c == '{' || c == '(' || c == '[') {
  163. if (c == '(')
  164. seen_brackets = true;
  165. brackets++;
  166. } else if (c == '}' || c == ')' || c == ']')
  167. brackets--;
  168. if (answer[0] != '\0' || c != ' ') {
  169. tal_append_fmt(&answer, "%c", c);
  170. }
  171. if (c == '}' && seen_brackets && brackets == 0) {
  172. (*p)++;
  173. return answer;
  174. }
  175. }
  176. (*p)++;
  177. if (**p == '\0')
  178. return NULL;
  179. }
  180. }
  181. /* This hack should handle well-formatted code. */
  182. static void look_for_definitions(char *contents, struct replace **repl)
  183. {
  184. char *stmt, *p = contents;
  185. preprocess(contents);
  186. while ((stmt = get_statement(contents, &p)) != NULL) {
  187. int i, len;
  188. /* Definition of struct/union? */
  189. if ((strncmp(stmt, "struct", 5) == 0
  190. || strncmp(stmt, "union", 5) == 0)
  191. && strchr(stmt, '{') && stmt[7] != '{')
  192. add_replace_tok(repl, stmt+7);
  193. /* Definition of var or typedef? */
  194. for (i = strlen(stmt)-1; i >= 0; i--)
  195. if (strspn(stmt+i, IDENT_CHARS) == 0)
  196. break;
  197. if (i != strlen(stmt)-1) {
  198. add_replace_tok(repl, stmt+i+1);
  199. continue;
  200. }
  201. /* function or array declaration? */
  202. len = strspn(stmt, IDENT_CHARS "* ");
  203. if (len > 0 && (stmt[len] == '(' || stmt[len] == '[')) {
  204. if (strspn(stmt + len + 1, IDENT_CHARS) != 0) {
  205. for (i = len-1; i >= 0; i--)
  206. if (strspn(stmt+i, IDENT_CHARS) == 0)
  207. break;
  208. if (i != len-1) {
  209. add_replace_tok(repl, stmt+i+1);
  210. continue;
  211. }
  212. } else {
  213. /* Pointer to function? */
  214. len++;
  215. len += strspn(stmt + len, " *");
  216. i = strspn(stmt + len, IDENT_CHARS);
  217. if (i > 0 && stmt[len + i] == ')')
  218. add_replace_tok(repl, stmt+len);
  219. }
  220. }
  221. }
  222. }
  223. /* FIXME: Only does main header, should chase local includes. */
  224. static void analyze_headers(const char *dir, struct replace **repl)
  225. {
  226. char *hdr, *contents;
  227. /* Get hold of header, assume that's it. */
  228. hdr = tal_fmt(dir, "%s/%s.h", dir, tal_basename(dir, dir));
  229. contents = tal_grab_file(dir, hdr, NULL);
  230. if (!contents)
  231. err(1, "Reading %s", hdr);
  232. verbose("Looking in %s for macros\n", hdr);
  233. verbose_indent();
  234. look_for_macros(contents, repl);
  235. verbose_unindent();
  236. verbose("Looking in %s for symbols\n", hdr);
  237. verbose_indent();
  238. look_for_definitions(contents, repl);
  239. verbose_unindent();
  240. }
  241. static void write_replacement_file(const char *dir, struct replace **repl)
  242. {
  243. char *replname = tal_fmt(dir, "%s/.namespacize", dir);
  244. int fd;
  245. struct replace *r;
  246. fd = open(replname, O_WRONLY|O_CREAT|O_EXCL, 0644);
  247. if (fd < 0) {
  248. if (errno == EEXIST)
  249. errx(1, "%s already exists: can't namespacize twice",
  250. replname);
  251. err(1, "Opening %s", replname);
  252. }
  253. for (r = *repl; r; r = r->next) {
  254. if (write(fd,r->string,strlen(r->string)) != strlen(r->string)
  255. || write(fd, "\n", 1) != 1) {
  256. unlink_no_errno(replname);
  257. if (errno == 0)
  258. errx(1, "Short write to %s: disk full?",
  259. replname);
  260. errx(1, "Writing to %s", replname);
  261. }
  262. }
  263. close(fd);
  264. }
  265. static void unlink_destroy(char *name)
  266. {
  267. unlink(name);
  268. }
  269. static char *find_word(char *f, const char *str)
  270. {
  271. char *p = f;
  272. while ((p = strstr(p, str)) != NULL) {
  273. /* Check it's not in the middle of a word. */
  274. if (p > f && (cisalnum(p[-1]) || p[-1] == '_')) {
  275. p++;
  276. continue;
  277. }
  278. if (cisalnum(p[strlen(str)]) || p[strlen(str)] == '_') {
  279. p++;
  280. continue;
  281. }
  282. return p;
  283. }
  284. return NULL;
  285. }
  286. /* This is horribly inefficient but simple. */
  287. static const char *rewrite_file(const char *filename,
  288. const struct replace *repl)
  289. {
  290. char *newname, *file;
  291. int fd;
  292. verbose("Rewriting %s\n", filename);
  293. file = tal_grab_file(filename, filename, NULL);
  294. if (!file)
  295. err(1, "Reading file %s", filename);
  296. for (; repl; repl = repl->next) {
  297. char *p;
  298. while ((p = find_word(file, repl->string)) != NULL) {
  299. unsigned int off;
  300. char *new = tal_arr(file, char, strlen(file)+6);
  301. off = p - file;
  302. memcpy(new, file, off);
  303. if (cisupper(repl->string[0]))
  304. memcpy(new + off, "CCAN_", 5);
  305. else
  306. memcpy(new + off, "ccan_", 5);
  307. strcpy(new + off + 5, file + off);
  308. file = new;
  309. }
  310. }
  311. /* If we exit for some reason, we want this erased. */
  312. newname = tal_fmt(autofree(), "%s.tmp", filename);
  313. fd = open(newname, O_WRONLY|O_CREAT|O_EXCL, 0644);
  314. if (fd < 0)
  315. err(1, "Creating %s", newname);
  316. tal_add_destructor(newname, unlink_destroy);
  317. if (write(fd, file, strlen(file)) != strlen(file)) {
  318. if (errno == 0)
  319. errx(1, "Short write to %s: disk full?", newname);
  320. errx(1, "Writing to %s", newname);
  321. }
  322. close(fd);
  323. return newname;
  324. }
  325. struct adjusted
  326. {
  327. struct adjusted *next;
  328. const char *file;
  329. const char *tmpfile;
  330. };
  331. static void setup_adjust_files(const char *dir,
  332. const struct replace *repl,
  333. struct adjusted **adj)
  334. {
  335. char **files;
  336. for (files = get_dir(dir); *files; files++) {
  337. if (strends(*files, "/test"))
  338. setup_adjust_files(*files, repl, adj);
  339. else if (strends(*files, ".c") || strends(*files, ".h")) {
  340. struct adjusted *a = tal(dir, struct adjusted);
  341. a->next = *adj;
  342. a->file = *files;
  343. a->tmpfile = rewrite_file(a->file, repl);
  344. *adj = a;
  345. }
  346. }
  347. }
  348. /* This is the "commit" stage, so we hope it won't fail. */
  349. static void rename_files(const struct adjusted *adj)
  350. {
  351. while (adj) {
  352. if (!move_file(adj->tmpfile, adj->file))
  353. warn("Could not rename over '%s', we're in trouble",
  354. adj->file);
  355. adj = adj->next;
  356. }
  357. }
  358. static void convert_dir(const char *dir)
  359. {
  360. char *name;
  361. struct replace *replace = NULL;
  362. struct adjusted *adj = NULL;
  363. /* Remove any ugly trailing slashes. */
  364. name = tal_strdup(NULL, dir);
  365. while (strends(name, "/"))
  366. name[strlen(name)-1] = '\0';
  367. analyze_headers(name, &replace);
  368. write_replacement_file(name, &replace);
  369. setup_adjust_files(name, replace, &adj);
  370. rename_files(adj);
  371. tal_free(name);
  372. tal_free(replace);
  373. }
  374. static struct replace *read_replacement_file(const char *depdir)
  375. {
  376. struct replace *repl = NULL;
  377. char *replname = tal_fmt(depdir, "%s/.namespacize", depdir);
  378. char *file, **line;
  379. file = tal_grab_file(replname, replname, NULL);
  380. if (!file) {
  381. if (errno != ENOENT)
  382. err(1, "Opening %s", replname);
  383. return NULL;
  384. }
  385. for (line = tal_strsplit(file, file, "\n", STR_EMPTY_OK); *line; line++)
  386. add_replace(&repl, *line);
  387. return repl;
  388. }
  389. static void adjust_dir(const char *dir)
  390. {
  391. char *parent = tal_dirname(autofree(), dir);
  392. char **deps;
  393. verbose("Adjusting %s\n", dir);
  394. verbose_indent();
  395. for (deps = get_deps(parent, dir, "depends", false, compile_info);
  396. *deps;
  397. deps++) {
  398. char *depdir;
  399. struct adjusted *adj = NULL;
  400. struct replace *repl;
  401. depdir = tal_fmt(parent, "%s/%s", parent, *deps);
  402. repl = read_replacement_file(depdir);
  403. if (repl) {
  404. verbose("%s has been namespacized\n", depdir);
  405. setup_adjust_files(parent, repl, &adj);
  406. rename_files(adj);
  407. } else
  408. verbose("%s has not been namespacized\n", depdir);
  409. tal_free(depdir);
  410. }
  411. verbose_unindent();
  412. tal_free(parent);
  413. }
  414. static void adjust_dependents(const char *dir)
  415. {
  416. char *parent = tal_dirname(NULL, dir);
  417. char *base = tal_basename(parent, dir);
  418. char **file;
  419. verbose("Looking for dependents in %s\n", parent);
  420. verbose_indent();
  421. for (file = get_dir(parent); *file; file++) {
  422. char *info, **deps;
  423. bool isdep = false;
  424. if (tal_basename(*file, *file)[0] == '.')
  425. continue;
  426. info = tal_fmt(*file, "%s/_info", *file);
  427. if (access(info, R_OK) != 0)
  428. continue;
  429. for (deps = get_deps(*file, *file, "depends", false,
  430. compile_info);
  431. *deps; deps++) {
  432. if (!strstarts(*deps, "ccan/"))
  433. continue;
  434. if (streq(*deps + strlen("ccan/"), base))
  435. isdep = true;
  436. }
  437. if (isdep)
  438. adjust_dir(*file);
  439. else
  440. verbose("%s is not dependent\n", *file);
  441. }
  442. verbose_unindent();
  443. }
  444. int main(int argc, char *argv[])
  445. {
  446. if (argv[1] && streq(argv[1], "--verbose")) {
  447. verbose = true;
  448. argv++;
  449. argc--;
  450. }
  451. if (argc == 2) {
  452. verbose("Namespacizing %s\n", argv[1]);
  453. verbose_indent();
  454. convert_dir(argv[1]);
  455. adjust_dependents(argv[1]);
  456. verbose_unindent();
  457. return 0;
  458. }
  459. if (argc > 2 && streq(argv[1], "--adjust")) {
  460. unsigned int i;
  461. for (i = 2; i < argc; i++)
  462. adjust_dir(argv[i]);
  463. return 0;
  464. }
  465. usage();
  466. }