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.

192 lignes
4.0 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 *cmd, const char *target)
  12. {
  13. switch (code) {
  14. case IPC_OK:
  15. break;
  16. case IPC_COMMERR:
  17. errx(1, "%s", ipc_strerror(code));
  18. default:
  19. warnx("%s '%s': %s", cmd, target, ipc_strerror(code));
  20. }
  21. return code;
  22. }
  23. void
  24. print_percent(long long part, long long whole)
  25. {
  26. printf("%5.1f%% ", floor(1000.0 * part / whole) / 10);
  27. }
  28. void
  29. print_rate(long long rate)
  30. {
  31. if (rate >= 999.995 * (1 << 10))
  32. printf("%6.2fMB/s ", (double)rate / (1 << 20));
  33. else
  34. printf("%6.2fkB/s ", (double)rate / (1 << 10));
  35. }
  36. void
  37. print_size(long long size)
  38. {
  39. if (size >= 999.995 * (1 << 20))
  40. printf("%6.2fG ", (double)size / (1 << 30));
  41. else
  42. printf("%6.2fM ", (double)size / (1 << 20));
  43. }
  44. void
  45. print_ratio(long long part, long long whole)
  46. {
  47. printf("%7.2f ", (double)part / whole);
  48. }
  49. char
  50. tstate_char(enum ipc_tstate ts)
  51. {
  52. switch (ts) {
  53. case IPC_TSTATE_INACTIVE:
  54. return 'I';
  55. case IPC_TSTATE_START:
  56. return '+';
  57. case IPC_TSTATE_STOP:
  58. return '-';
  59. case IPC_TSTATE_LEECH:
  60. return 'L';
  61. case IPC_TSTATE_SEED:
  62. return 'S';
  63. }
  64. errx(1, "bad state");
  65. }
  66. int
  67. torrent_spec(char *arg, struct ipc_torrent *tp)
  68. {
  69. char *p;
  70. tp->u.num = strtoul(arg, &p, 10);
  71. if (*p == '\0') {
  72. tp->by_hash = 0;
  73. return 1;
  74. }
  75. if ((p = mi_load(arg, NULL)) == NULL) {
  76. warnx("bad torrent '%s' (%s)", arg, strerror(errno));
  77. return 0;
  78. }
  79. tp->by_hash = 1;
  80. mi_info_hash(p, tp->u.hash);
  81. free(p);
  82. return 1;
  83. }
  84. static struct {
  85. const char *name;
  86. void (*fun)(int, char **);
  87. void (*help)(void);
  88. } cmd_table[] = {
  89. { "add", cmd_add, usage_add },
  90. { "del", cmd_del, usage_del },
  91. { "kill", cmd_kill, usage_kill },
  92. { "list", cmd_list, usage_list },
  93. { "start", cmd_start, usage_start },
  94. { "stop", cmd_stop, usage_stop },
  95. { "stat", cmd_stat, usage_stat }
  96. };
  97. int ncmds = sizeof(cmd_table) / sizeof(cmd_table[0]);
  98. static void
  99. usage(void)
  100. {
  101. printf(
  102. "btcli is the btpd command line interface.\n"
  103. "\n"
  104. "Usage: btcli [main options] command [command options]\n"
  105. "\n"
  106. "Main options:\n"
  107. "-d dir\n"
  108. "\tThe btpd directory.\n"
  109. "\n"
  110. "--help [command]\n"
  111. "\tShow this text or help for the specified command.\n"
  112. "\n"
  113. "Commands:\n"
  114. "add\t- Add torrents to btpd.\n"
  115. "del\t- Remove torrents from btpd.\n"
  116. "kill\t- Shut down btpd.\n"
  117. "list\t- List torrents.\n"
  118. "start\t- Activate torrents.\n"
  119. "stat\t- Display stats for active torrents.\n"
  120. "stop\t- Deactivate torrents.\n"
  121. "\n"
  122. "Note:\n"
  123. "Torrents can be specified either with its number or its file.\n"
  124. "\n"
  125. );
  126. exit(1);
  127. }
  128. static struct option base_opts [] = {
  129. { "help", no_argument, NULL, 'H' },
  130. {NULL, 0, NULL, 0}
  131. };
  132. int
  133. main(int argc, char **argv)
  134. {
  135. int ch, help = 0;
  136. if (argc < 2)
  137. usage();
  138. while ((ch = getopt_long(argc, argv, "+d:", base_opts, NULL)) != -1) {
  139. switch (ch) {
  140. case 'd':
  141. btpd_dir = optarg;
  142. break;
  143. case 'H':
  144. help = 1;
  145. break;
  146. default:
  147. usage();
  148. }
  149. }
  150. argc -= optind;
  151. argv += optind;
  152. if (argc == 0)
  153. usage();
  154. if (btpd_dir == NULL)
  155. if ((btpd_dir = find_btpd_dir()) == NULL)
  156. errx(1, "cannot find the btpd directory");
  157. optind = 0;
  158. int found = 0;
  159. for (int i = 0; !found && i < ncmds; i++) {
  160. if (strcmp(argv[0], cmd_table[i].name) == 0) {
  161. found = 1;
  162. if (help)
  163. cmd_table[i].help();
  164. else
  165. cmd_table[i].fun(argc, argv);
  166. }
  167. }
  168. if (!found)
  169. usage();
  170. return 0;
  171. }