A clone of btpd with my configuration changes.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

159 lignes
3.2 KiB

  1. #include "btcli.h"
  2. const char *btpd_dir;
  3. struct ipc *ipc;
  4. void
  5. btpd_connect(void)
  6. {
  7. if ((errno = ipc_open(btpd_dir, &ipc)) != 0)
  8. err(1, "cannot open connection to btpd in %s", btpd_dir);
  9. }
  10. enum ipc_err
  11. handle_ipc_res(enum ipc_err code, const char *target)
  12. {
  13. switch (code) {
  14. case IPC_OK:
  15. break;
  16. case IPC_COMMERR:
  17. errx(1, "fatal error in communication with btpd");
  18. default:
  19. warnx("btpd response for '%s': %s", target, ipc_strerror(code));
  20. }
  21. return code;
  22. }
  23. char
  24. tstate_char(enum ipc_tstate ts)
  25. {
  26. switch (ts) {
  27. case IPC_TSTATE_INACTIVE:
  28. return 'I';
  29. case IPC_TSTATE_START:
  30. return '+';
  31. case IPC_TSTATE_STOP:
  32. return '-';
  33. case IPC_TSTATE_LEECH:
  34. return 'L';
  35. case IPC_TSTATE_SEED:
  36. return 'S';
  37. }
  38. errx(1, "bad state");
  39. }
  40. int
  41. torrent_spec(char *arg, struct ipc_torrent *tp)
  42. {
  43. char *p;
  44. tp->u.num = strtoul(arg, &p, 10);
  45. if (*p == '\0') {
  46. tp->by_hash = 0;
  47. return 1;
  48. }
  49. if ((p = mi_load(arg, NULL)) == NULL) {
  50. warnx("bad torrent '%s' (%s)", arg, strerror(errno));
  51. return 0;
  52. }
  53. tp->by_hash = 1;
  54. mi_info_hash(p, tp->u.hash);
  55. free(p);
  56. return 1;
  57. }
  58. struct {
  59. const char *name;
  60. void (*fun)(int, char **);
  61. void (*help)(void);
  62. } cmd_table[] = {
  63. { "add", cmd_add, usage_add },
  64. { "del", cmd_del, usage_del },
  65. { "kill", cmd_kill, usage_kill },
  66. { "list", cmd_list, usage_list },
  67. { "start", cmd_start, usage_start },
  68. { "stop", cmd_stop, usage_stop },
  69. { "stat", cmd_stat, usage_stat }
  70. };
  71. int ncmds = sizeof(cmd_table) / sizeof(cmd_table[0]);
  72. void
  73. usage(void)
  74. {
  75. printf(
  76. "btcli is the btpd command line interface.\n"
  77. "\n"
  78. "Usage: btcli [main options] command [command options]\n"
  79. "\n"
  80. "Main options:\n"
  81. "-d dir\n"
  82. "\tThe btpd directory.\n"
  83. "\n"
  84. "--help [command]\n"
  85. "\tShow this text or help for the specified command.\n"
  86. "\n"
  87. "Commands:\n"
  88. "add\n"
  89. "del\n"
  90. "kill\n"
  91. "list\n"
  92. "start\n"
  93. "stat\n"
  94. "stop\n"
  95. "\n");
  96. exit(1);
  97. }
  98. struct option base_opts [] = {
  99. { "help", no_argument, NULL, 'H' },
  100. {NULL, 0, NULL, 0}
  101. };
  102. int
  103. main(int argc, char **argv)
  104. {
  105. int ch, help = 0;
  106. if (argc < 2)
  107. usage();
  108. while ((ch = getopt_long(argc, argv, "+d:", base_opts, NULL)) != -1) {
  109. switch (ch) {
  110. case 'd':
  111. btpd_dir = optarg;
  112. break;
  113. case 'H':
  114. help = 1;
  115. break;
  116. default:
  117. usage();
  118. }
  119. }
  120. argc -= optind;
  121. argv += optind;
  122. if (argc == 0)
  123. usage();
  124. if (btpd_dir == NULL)
  125. if ((btpd_dir = find_btpd_dir()) == NULL)
  126. errx(1, "cannot find the btpd directory");
  127. optind = 0;
  128. int found = 0;
  129. for (int i = 0; !found && i < ncmds; i++) {
  130. if (strcmp(argv[0], cmd_table[i].name) == 0) {
  131. found = 1;
  132. if (help)
  133. cmd_table[i].help();
  134. else
  135. cmd_table[i].fun(argc, argv);
  136. }
  137. }
  138. if (!found)
  139. usage();
  140. return 0;
  141. }