cmd.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright (C) 2009 Mamadou Diop.
  3. *
  4. * Contact: Mamadou Diop <diopmamadou(at)doubango.org>
  5. *
  6. * This file is part of Open Source Doubango Framework.
  7. *
  8. * DOUBANGO is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * DOUBANGO is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with DOUBANGO.
  20. *
  21. */
  22. #include "cmd.h"
  23. #include "dssl.h"
  24. #include "tsk_memory.h"
  25. #include "tsk_string.h"
  26. #include "tsk_debug.h"
  27. //#define PAD " "
  28. static int pred_find_opt_by_type(const tsk_list_item_t *item, const void *type);
  29. /* parse a command line */
  30. cmd_t* cmd_parse(const char* buffer, tsk_size_t size, tsk_bool_t *comment, tsk_params_L_t* params)
  31. {
  32. return dssl_parse(buffer, size, comment, params);
  33. }
  34. ///* print usage */
  35. void cmd_print_help()
  36. {
  37. printf("\n\n========================= Usage =========================\n\n");
  38. printf("Please refer to the Programmer's Guide at\n http://www.doubango.org/\n\n");
  39. //
  40. // printf("Usage:\n");
  41. // printf(PAD"++[command] --[opts]\n\n");
  42. //
  43. // /* Commands */
  44. // printf("Commands:\n--\n");
  45. // printf(PAD"[++audio] or [++a"PAD"%s", "Make audio call\n--\n");
  46. // printf(PAD"[++audiovideo] or [++av]"PAD"%s", "Make audio/video call\n--\n");
  47. // printf(PAD"[++config-file] or [++cf]"PAD"%s", "Load opts from config file\n--\n");
  48. // printf(PAD"[++config-session] | [++css]"PAD"%s", "Configure an 3GPP IMS/LTE session\n--\n");
  49. // printf(PAD"[++config-stack] | [++cst]"PAD"%s", "Configure an 3GPP IMS/LTE stack\n--\n");
  50. // printf(PAD"[++exit] | [++e]"PAD"%s", "Exit the application\n--\n");
  51. // printf(PAD"[++file] | [++f]"PAD"%s", "Send a file. The stack must be running (see ++run command). To abort the File transfer, use ++hangup.\n--\n");
  52. // printf(PAD"[++hangup] | [++hp]"PAD"%s", "Hangup any SIP session (unREGISTER, unSUBSCRIBE, unPUBLISH, HangUp Call, Abort ...).\n--\n");
  53. // printf(PAD"[++help] | [++h]"PAD"%s", "Print this help screen\n--\n");
  54. // printf(PAD"[++message] | [++m]"PAD"%s", "Send Pager Mode IM. The stack must be running (see ++run command).\n\n");
  55. // printf(PAD"[++publish] | [++pub]"PAD"%s", "Send PUBLISH message. The stack must be running (see ++run command). To unPUBLISH, use ++hanggup.\n--\n");
  56. // printf(PAD"[++quit] | [++q]"PAD"%s", "Quit the application\n--\n");
  57. // printf(PAD"[++run]"PAD"%s", "Start/Run the 3GPP IMS/LTE stack. Mandatory before starting to do anything.\n--\n");
  58. // printf(PAD"[++sms]"PAD"%s", "Send Binary SMS (RP-DATA). The stack must be running (see ++run command).\n--\n");
  59. // printf(PAD"[++subscribe] | [++sub]"PAD"%s", "Send SUBSCRIBE message. The stack must be running (see ++run command). To unSUBSCRIBE, use ++hangup.\n--\n");
  60. // printf(PAD"[++video] or [++v]"PAD"%s", "Make video call\n--\n");
  61. //
  62. // printf("\n\n========================= =========================\n\n");
  63. }
  64. cmd_t* cmd_create(cmd_type_t type)
  65. {
  66. return tsk_object_new(cmd_def_t, type);
  67. }
  68. static tsk_object_t* cmd_ctor(tsk_object_t * self, va_list * app)
  69. {
  70. cmd_t *cmd = self;
  71. if(cmd) {
  72. cmd->type = va_arg(*app, cmd_type_t);
  73. cmd->opts = tsk_list_create();
  74. }
  75. return self;
  76. }
  77. static tsk_object_t* cmd_dtor(tsk_object_t * self)
  78. {
  79. cmd_t *cmd = self;
  80. if(cmd) {
  81. TSK_OBJECT_SAFE_FREE(cmd->opts);
  82. TSK_FREE(cmd->sidparam);
  83. }
  84. return self;
  85. }
  86. static const tsk_object_def_t cmd_def_s = {
  87. sizeof(cmd_t),
  88. cmd_ctor,
  89. cmd_dtor,
  90. tsk_null,
  91. };
  92. const tsk_object_def_t *cmd_def_t = &cmd_def_s;
  93. opt_t* opt_create(opt_type_t type, lv_t level, const char* value)
  94. {
  95. return tsk_object_new(opt_def_t, type, level, value);
  96. }
  97. const opt_t* opt_get_by_type(const opts_L_t* opts, opt_type_t type)
  98. {
  99. const tsk_list_item_t* item;
  100. if((item = tsk_list_find_item_by_pred(opts, pred_find_opt_by_type, &type))) {
  101. return item->data;
  102. }
  103. return tsk_null;
  104. }
  105. static int pred_find_opt_by_type(const tsk_list_item_t *item, const void *type)
  106. {
  107. if(item && item->data) {
  108. opt_t *opt = item->data;
  109. return (opt->type - *((opt_type_t*)type));
  110. }
  111. return -1;
  112. }
  113. static tsk_object_t* opt_ctor(tsk_object_t * self, va_list * app)
  114. {
  115. opt_t *opt = self;
  116. if(opt) {
  117. opt->type = va_arg(*app, opt_type_t);
  118. opt->lv = va_arg(*app, lv_t);
  119. opt->value = tsk_strdup(va_arg(*app, const char*));
  120. }
  121. return self;
  122. }
  123. static tsk_object_t* opt_dtor(tsk_object_t * self)
  124. {
  125. opt_t *opt = self;
  126. if(opt) {
  127. TSK_FREE(opt->value);
  128. }
  129. return self;
  130. }
  131. static const tsk_object_def_t opt_def_s = {
  132. sizeof(opt_t),
  133. opt_ctor,
  134. opt_dtor,
  135. tsk_null,
  136. };
  137. const tsk_object_def_t *opt_def_t = &opt_def_s;