commands.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #include "../include/commands.hpp"
  2. #include <cstdio>
  3. CMD commands[]{
  4. {CMD_HELP, "help", "show help"},
  5. {CMD_STATUS, "status", "query status of IP"},
  6. {CMD_DISCONNECT, "disconnect", "disconnect from IP"},
  7. {CMD_PUT , "put", "upload file to IP and add to queue"},
  8. {CMD_GET , "get", "retrieve file from deamon"},
  9. {CMD_LIST , "list", "list files that are downloadable from deamon"}
  10. /* TODO
  11. {CMD_REMOVE , "remove", "remove file from IP and queue (stops xfer
  12. if required)"},
  13. {CMD_SETUP , "setup", "configure server at IP"},
  14. {CMD_LOG , "log", "show log from IP"}
  15. */
  16. };
  17. COMMANDID getCmdIdFromString(const char *str) {
  18. COMMANDID ret = CMD_UNKNOWN;
  19. char temp[COMMANDLEN + 1] = {0};
  20. int len = COMMANDLEN;
  21. if (std::strlen(str) > COMMANDLEN) {
  22. return ret;
  23. }
  24. else len = std::strlen(str);
  25. for (int i = 0; i < len; i++)
  26. temp[i] = std::tolower(str[i]);
  27. for (int i = 0; i < sizeofarr(commands); i++) {
  28. if (!std::strncmp(temp, commands[i].name, COMMANDLEN))
  29. ret = commands[i].cmd;
  30. }
  31. return ret;
  32. }
  33. const char *getCmdStringFromId(COMMANDID id) {
  34. const char *ret = NULL;
  35. for (int i = 0; i < sizeofarr(commands); i++) {
  36. if (commands[i].cmd == id)
  37. ret = commands[i].name;
  38. }
  39. return ret;
  40. }
  41. void printCmds(void) {
  42. for(CMD command : commands) {
  43. printf("%-12s - %s\n", command.name, command.desc);
  44. }
  45. }