1
0

clish_config_callback.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * clish_config_callback.c
  3. *
  4. *
  5. * Callback hook to execute config operations.
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <sys/types.h>
  10. #include <assert.h>
  11. #include <sys/socket.h>
  12. #include <sys/un.h>
  13. #include <limits.h>
  14. #include "private.h"
  15. #include "konf/net.h"
  16. #include "konf/buf.h"
  17. #include "konf/query.h"
  18. #include "clish/variable.h"
  19. static int send_request(konf_client_t * client, char *command);
  20. /*--------------------------------------------------------- */
  21. bool_t clish_config_callback(const clish_shell_t * this,
  22. const clish_command_t * cmd, clish_pargv_t * pargv)
  23. {
  24. unsigned i;
  25. char *command = NULL;
  26. konf_client_t *client;
  27. konf_buf_t *buf = NULL;
  28. const char *viewid = NULL;
  29. char *str = NULL;
  30. char tmp[PATH_MAX + 100];
  31. clish_config_operation_t op;
  32. if (!this)
  33. return BOOL_TRUE;
  34. client = clish_shell__get_client(this);
  35. if (!client)
  36. return BOOL_TRUE;
  37. viewid = clish_shell__get_viewid(this);
  38. op = clish_command__get_cfg_op(cmd);
  39. switch (op) {
  40. case CLISH_CONFIG_NONE:
  41. return BOOL_TRUE;
  42. case CLISH_CONFIG_SET:
  43. /* Add set operation */
  44. lub_string_cat(&command, "-s");
  45. /* Add entered line */
  46. str = clish_variable__get_line(cmd, pargv);
  47. lub_string_cat(&command, " -l \"");
  48. lub_string_cat(&command, str);
  49. lub_string_cat(&command, "\"");
  50. lub_string_free(str);
  51. /* Add splitter */
  52. if (clish_command__get_splitter(cmd) == BOOL_FALSE)
  53. lub_string_cat(&command, " -i");
  54. /* Add unique */
  55. if (clish_command__get_unique(cmd) == BOOL_FALSE)
  56. lub_string_cat(&command, " -n");
  57. break;
  58. case CLISH_CONFIG_UNSET:
  59. /* Add unset operation */
  60. lub_string_cat(&command, "-u");
  61. break;
  62. case CLISH_CONFIG_DUMP:
  63. /* Add dump operation */
  64. lub_string_cat(&command, "-d");
  65. /* Add filename */
  66. str = clish_command__get_file(cmd, viewid, pargv);
  67. if (str) {
  68. lub_string_cat(&command, " -f \"");
  69. if (str[0] != '\0')
  70. lub_string_cat(&command, str);
  71. else
  72. lub_string_cat(&command, "/tmp/running-config");
  73. lub_string_cat(&command, "\"");
  74. lub_string_free(str);
  75. }
  76. break;
  77. default:
  78. return BOOL_FALSE;
  79. };
  80. /* Add pattern */
  81. if ((CLISH_CONFIG_SET == op) || (CLISH_CONFIG_UNSET == op)) {
  82. str = clish_command__get_pattern(cmd, viewid, pargv);
  83. if (!str) {
  84. lub_string_free(command);
  85. return BOOL_FALSE;
  86. }
  87. lub_string_cat(&command, " -r \"");
  88. lub_string_cat(&command, str);
  89. lub_string_cat(&command, "\"");
  90. lub_string_free(str);
  91. }
  92. /* Add priority */
  93. if (clish_command__get_priority(cmd) != 0) {
  94. snprintf(tmp, sizeof(tmp) - 1, " -p 0x%x",
  95. clish_command__get_priority(cmd));
  96. tmp[sizeof(tmp) - 1] = '\0';
  97. lub_string_cat(&command, tmp);
  98. }
  99. /* Add sequence */
  100. if (clish_command__is_seq(cmd)) {
  101. snprintf(tmp, sizeof(tmp) - 1, " -q %u",
  102. clish_command__get_seq(cmd,
  103. viewid, pargv));
  104. tmp[sizeof(tmp) - 1] = '\0';
  105. lub_string_cat(&command, tmp);
  106. }
  107. /* Add pwd */
  108. str = clish_shell__get_pwd_full(this,
  109. clish_command__get_cfg_depth(cmd, viewid, pargv));
  110. if (str) {
  111. lub_string_cat(&command, " ");
  112. lub_string_cat(&command, str);
  113. lub_string_free(str);
  114. }
  115. #ifdef DEBUG
  116. fprintf(stderr, "CONFIG request: %s\n", command);
  117. #endif
  118. if (send_request(client, command) < 0) {
  119. fprintf(stderr, "Cannot write to the running-config.\n");
  120. }
  121. if (konf_client_recv_answer(client, &buf) < 0) {
  122. fprintf(stderr, "The error while request to the config daemon.\n");
  123. }
  124. lub_string_free(command);
  125. /* Postprocessing. Get data from daemon etc. */
  126. switch (op) {
  127. case CLISH_CONFIG_DUMP:
  128. if (buf) {
  129. konf_buf_lseek(buf, 0);
  130. while ((str = konf_buf_preparse(buf))) {
  131. if (strlen(str) == 0) {
  132. lub_string_free(str);
  133. break;
  134. }
  135. fprintf(clish_shell__get_ostream(this),
  136. "%s\n", str);
  137. lub_string_free(str);
  138. }
  139. konf_buf_delete(buf);
  140. }
  141. break;
  142. default:
  143. break;
  144. };
  145. return BOOL_TRUE;
  146. }
  147. /*--------------------------------------------------------- */
  148. static int send_request(konf_client_t * client, char *command)
  149. {
  150. if ((konf_client_connect(client) < 0))
  151. return -1;
  152. if (konf_client_send(client, command) < 0) {
  153. if (konf_client_reconnect(client) < 0)
  154. return -1;
  155. if (konf_client_send(client, command) < 0)
  156. return -1;
  157. }
  158. return 0;
  159. }