namespacize.c 13 KB

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