parse.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <mpv/client.h>
  5. #include "libmpv.h"
  6. #include "parse.h"
  7. int
  8. parse(char *buf)
  9. {
  10. char *token = NULL;
  11. if (!buf) {
  12. fprintf(stderr, "parse: argument is NULL.\n");
  13. return -1;
  14. }
  15. token = strtok(buf, " \n");
  16. do {
  17. if (!strcmp("play", token)) {
  18. token = strtok(NULL, "\n");
  19. if (!token) {
  20. fprintf(stderr, "mpvd play: missing filename…\n");
  21. continue;
  22. }
  23. fprintf(stderr, "mpvd play %s\n", token);
  24. libmpv_play(token);
  25. /* printf("%s\n", token);
  26. */
  27. } else if (!strcmp("queue", token)) {
  28. token = strtok(NULL, "\n");
  29. if (!token) {
  30. fprintf(stderr, "mpvd queue: missing filename…\n");
  31. continue;
  32. }
  33. fprintf(stderr, "mpvd queue %s\n", token);
  34. libmpv_queue(token);
  35. /* printf("mpvd queue add %s\n", token);
  36. */
  37. } else if (!strcmp("next", token)) {
  38. fprintf(stderr, "mpvd next\n");
  39. libmpv_next();
  40. } else if (!strcmp("prev", token)) {
  41. fprintf(stderr, "mpvd prev\n");
  42. libmpv_prev();
  43. } else if (!strcmp("stop", token)) {
  44. fprintf(stderr, "mpvd stop\n");
  45. libmpv_stop();
  46. } else if (!strcmp("keypress", token)) {
  47. token = strtok(NULL, "\n");
  48. if (!token) {
  49. fprintf(stderr, "mpvd keypress: missing argument…\n");
  50. continue;
  51. }
  52. fprintf(stderr, "mpvd keypress %s\n", token);
  53. libmpv_keypress(token);
  54. } else if (!strcmp("quit", token)) {
  55. fprintf(stderr, "mpvd quit\n");
  56. exit(EXIT_SUCCESS);
  57. } else {
  58. fprintf(stderr, "invalid command: \"%s\"\n", token);
  59. }
  60. } while ((token = strtok(NULL, " \n")));
  61. fflush(stdout);
  62. return 0;
  63. }