A tool for adding anime to your anidb list.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
2.4KB

  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 (*argcheck)(void); /* Function to check argument correctness before calling fn */
  15. enum error (*fn)(void *data); /* The function for the command */
  16. };
  17. static const struct cmd_entry ents[] = {
  18. { .arg_name = "version", .fn = cmd_prog_version, },
  19. { .arg_name = "server-version", .fn = cmd_server_version, .need_api = true },
  20. { .arg_name = "uptime", .fn = cmd_server_uptime, .need_auth = true },
  21. { .arg_name = "ed2k", .fn = cmd_ed2k, },
  22. { .arg_name = "add", .fn = cmd_add, .argcheck = cmd_add_argcheck, .need_auth = true, .need_cache = true, },
  23. { .arg_name = "modify", .fn = cmd_modify, .argcheck = cmd_modify_argcheck, .need_auth = true, .need_cache = true, },
  24. { .arg_name = "stats", .fn = cmd_stats, .need_auth = true, .need_cache = false, },
  25. };
  26. static const int32_t ents_len = sizeof(ents)/sizeof(*ents);
  27. static enum error cmd_run_one(const struct cmd_entry *ent)
  28. {
  29. enum error err = NOERR;
  30. if (ent->argcheck) {
  31. err = ent->argcheck();
  32. if (err != NOERR)
  33. goto end;
  34. }
  35. if (ent->need_cache) {
  36. err = cache_init();
  37. if (err != NOERR)
  38. goto end;
  39. }
  40. if (ent->need_api || ent->need_auth) {
  41. err = api_init(ent->need_auth);
  42. if (err != NOERR)
  43. return err;
  44. }
  45. void *data = NULL;
  46. err = ent->fn(data);
  47. end:
  48. if (ent->need_api || ent->need_auth)
  49. api_free();
  50. if (ent->need_cache)
  51. cache_free();
  52. return err;
  53. }
  54. enum error cmd_main()
  55. {
  56. for (int i = 0; i < ents_len; i++) {
  57. enum error err;
  58. bool *is_set;
  59. err = config_get(ents[i].arg_name, (void**)&is_set);
  60. if (err != NOERR && err != ERR_OPT_UNSET) {
  61. uio_error("Cannot get arg '%s' (%s)", ents[i].arg_name,
  62. error_to_string(err));
  63. continue;
  64. }
  65. if (*is_set) {
  66. err = cmd_run_one(&ents[i]);
  67. return err;
  68. }
  69. }
  70. return ERR_CMD_NONE;
  71. }