modfiles.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* List files in a module, or modules. */
  2. #include <ccan/opt/opt.h>
  3. #include <ccan/foreach/foreach.h>
  4. #include <ccan/tal/str/str.h>
  5. #include "manifest.h"
  6. #include <stdio.h>
  7. static void add_file(const struct ccan_file *f,
  8. bool fullpath, bool nul_term, bool gitonly)
  9. {
  10. /* Hacky way of seeing if git knows about this. */
  11. if (gitonly) {
  12. char *cmd = tal_fmt(f, "git status --porcelain --ignored %s | grep -q '^ *[!?]'", f->fullname);
  13. if (system(cmd) == 0)
  14. return;
  15. }
  16. printf("%s%c", fullpath ? f->fullname : f->name,
  17. nul_term ? '\0' : '\n');
  18. }
  19. int main(int argc, char *argv[])
  20. {
  21. bool code = true;
  22. bool license = true;
  23. bool tests = true;
  24. bool other = true;
  25. bool gitonly = false;
  26. bool nul_term = false;
  27. bool fullpath = false;
  28. int i;
  29. opt_register_noarg("--no-code", opt_set_invbool, &code,
  30. "Don't list .c and .h files");
  31. opt_register_noarg("--no-license", opt_set_invbool, &license,
  32. "Don't list license file");
  33. opt_register_noarg("--no-tests", opt_set_invbool, &tests,
  34. "Don't list test files");
  35. opt_register_noarg("--no-other", opt_set_invbool, &other,
  36. "Don't list other files");
  37. opt_register_noarg("--git-only", opt_set_bool, &gitonly,
  38. "Only include files in git");
  39. opt_register_noarg("--fullpath", opt_set_bool, &fullpath,
  40. "Print full path names instead of module-relative ones");
  41. opt_register_noarg("--null|-0", opt_set_bool, &nul_term,
  42. "Separate files with nul character instead of \\n");
  43. opt_register_noarg("-h|--help", opt_usage_and_exit,
  44. "<moduledir>...",
  45. "Show usage");
  46. opt_parse(&argc, argv, opt_log_stderr_exit);
  47. if (argc < 2)
  48. opt_usage_exit_fail("Expected one or more module directories");
  49. for (i = 1; i < argc; i++) {
  50. struct manifest *m = get_manifest(NULL, argv[i]);
  51. struct list_head *list;
  52. struct ccan_file *f;
  53. if (code) {
  54. foreach_ptr(list, &m->c_files, &m->h_files) {
  55. list_for_each(list, f, list)
  56. add_file(f, fullpath, nul_term, gitonly);
  57. }
  58. }
  59. if (license) {
  60. list_for_each(&m->other_files, f, list) {
  61. if (streq(f->name, "LICENSE"))
  62. add_file(f, fullpath, nul_term, gitonly);
  63. }
  64. }
  65. if (tests) {
  66. foreach_ptr(list, &m->run_tests, &m->api_tests,
  67. &m->compile_ok_tests, &m->compile_fail_tests,
  68. &m->other_test_c_files,
  69. &m->other_test_files) {
  70. list_for_each(list, f, list)
  71. add_file(f, fullpath, nul_term, gitonly);
  72. }
  73. }
  74. if (other) {
  75. list_for_each(&m->other_files, f, list) {
  76. if (!streq(f->name, "LICENSE"))
  77. add_file(f, fullpath, nul_term, gitonly);
  78. }
  79. }
  80. }
  81. return 0;
  82. }