A tool for adding anime to your anidb list.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

77 行
2.1KB

  1. #include <stdbool.h>
  2. #include "cmd.h"
  3. #include "error.h"
  4. #include "config.h"
  5. #include "api.h"
  6. #include "uio.h"
  7. #include "net.h"
  8. #include "cache.h"
  9. struct cmd_entry {
  10. bool need_api : 1; /* Does this command needs to connect to the api? */
  11. bool need_auth : 1; /* Does this command needs auth to the api? sets need_api */
  12. bool need_cache : 1; /* Does this cmd needs the file cache? */
  13. const char *arg_name; /* If this argument is present, execute this cmd */
  14. enum error (*fn)(void *data); /* The function for the command */
  15. };
  16. static const struct cmd_entry ents[] = {
  17. { .arg_name = "version", .fn = cmd_prog_version, },
  18. { .arg_name = "server-version", .fn = cmd_server_version, .need_api = true },
  19. { .arg_name = "uptime", .fn = cmd_server_uptime, .need_auth = true },
  20. { .arg_name = "ed2k", .fn = cmd_ed2k, },
  21. { .arg_name = "add", .fn = cmd_add, .need_auth = true, .need_cache = true, },
  22. { .arg_name = "modify", .fn = cmd_modify, .need_auth = true, .need_cache = false, },
  23. };
  24. static const int32_t ents_len = sizeof(ents)/sizeof(*ents);
  25. static enum error cmd_run_one(const struct cmd_entry *ent)
  26. {
  27. enum error err = NOERR;
  28. if (ent->need_cache) {
  29. err = cache_init();
  30. if (err != NOERR)
  31. goto end;
  32. }
  33. if (ent->need_api || ent->need_auth) {
  34. err = api_init(ent->need_auth);
  35. if (err != NOERR)
  36. return err;
  37. }
  38. void *data = NULL;
  39. err = ent->fn(data);
  40. end:
  41. if (ent->need_api || ent->need_auth)
  42. api_free();
  43. if (ent->need_cache)
  44. cache_free();
  45. return err;
  46. }
  47. enum error cmd_main()
  48. {
  49. for (int i = 0; i < ents_len; i++) {
  50. enum error err;
  51. bool *is_set;
  52. err = config_get(ents[i].arg_name, (void**)&is_set);
  53. if (err != NOERR && err != ERR_OPT_UNSET) {
  54. uio_error("Cannot get arg '%s' (%s)", ents[i].arg_name,
  55. error_to_string(err));
  56. continue;
  57. }
  58. if (*is_set) {
  59. err = cmd_run_one(&ents[i]);
  60. return err;
  61. }
  62. }
  63. return ERR_CMD_NONE;
  64. }