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.

83 lines
2.3KB

  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. };
  25. static const int32_t ents_len = sizeof(ents)/sizeof(*ents);
  26. static enum error cmd_run_one(const struct cmd_entry *ent)
  27. {
  28. enum error err = NOERR;
  29. if (ent->argcheck) {
  30. err = ent->argcheck();
  31. if (err != NOERR)
  32. goto end;
  33. }
  34. if (ent->need_cache) {
  35. err = cache_init();
  36. if (err != NOERR)
  37. goto end;
  38. }
  39. if (ent->need_api || ent->need_auth) {
  40. err = api_init(ent->need_auth);
  41. if (err != NOERR)
  42. return err;
  43. }
  44. void *data = NULL;
  45. err = ent->fn(data);
  46. end:
  47. if (ent->need_api || ent->need_auth)
  48. api_free();
  49. if (ent->need_cache)
  50. cache_free();
  51. return err;
  52. }
  53. enum error cmd_main()
  54. {
  55. for (int i = 0; i < ents_len; i++) {
  56. enum error err;
  57. bool *is_set;
  58. err = config_get(ents[i].arg_name, (void**)&is_set);
  59. if (err != NOERR && err != ERR_OPT_UNSET) {
  60. uio_error("Cannot get arg '%s' (%s)", ents[i].arg_name,
  61. error_to_string(err));
  62. continue;
  63. }
  64. if (*is_set) {
  65. err = cmd_run_one(&ents[i]);
  66. return err;
  67. }
  68. }
  69. return ERR_CMD_NONE;
  70. }