|
21 | 21 | #ifndef WIN32 |
22 | 22 | #include <regex.h> |
23 | 23 | #endif |
| 24 | +#if defined(LIBCLI_USE_POLL) && !defined(WIN32) |
| 25 | +#include <poll.h> |
| 26 | +#define CLI_SOCKET_WAIT_PERROR "poll" |
| 27 | +#else |
| 28 | +#define CLI_SOCKET_WAIT_PERROR "select" |
| 29 | +#endif |
24 | 30 | #include "libcli.h" |
25 | 31 |
|
26 | 32 | #ifdef __GNUC__ |
@@ -170,6 +176,7 @@ inline void cli_int_show_pipeline(struct cli_def *cli, struct cli_pipeline *pipe |
170 | 176 | static void cli_int_free_pipeline(struct cli_pipeline *pipeline); |
171 | 177 | static struct cli_command *cli_register_command_core(struct cli_def *cli, struct cli_command *parent, struct cli_command *c); |
172 | 178 | static void cli_int_wrap_help_line(char *nameptr, char *helpptr, struct cli_comphelp *comphelp); |
| 179 | +static int cli_socket_wait(int sockfd, struct timeval *tm); |
173 | 180 |
|
174 | 181 | static char DELIM_OPT_START[] = "["; |
175 | 182 | static char DELIM_OPT_END[] = "]"; |
@@ -1127,7 +1134,6 @@ int cli_loop(struct cli_def *cli, int sockfd) { |
1127 | 1134 |
|
1128 | 1135 | while (1) { |
1129 | 1136 | int sr; |
1130 | | - fd_set r; |
1131 | 1137 |
|
1132 | 1138 | /* |
1133 | 1139 | * Ensure our transient mode is reset to the starting mode on *each* loop traversal transient mode is valid only |
@@ -1168,12 +1174,9 @@ int cli_loop(struct cli_def *cli, int sockfd) { |
1168 | 1174 | cli->showprompt = 0; |
1169 | 1175 | } |
1170 | 1176 |
|
1171 | | - FD_ZERO(&r); |
1172 | | - FD_SET(sockfd, &r); |
1173 | | - |
1174 | | - if ((sr = select(sockfd + 1, &r, NULL, NULL, &tm)) < 0) { |
| 1177 | + if ((sr = cli_socket_wait(sockfd, &tm)) < 0) { |
1175 | 1178 | if (errno == EINTR) continue; |
1176 | | - perror("select"); |
| 1179 | + perror(CLI_SOCKET_WAIT_PERROR); |
1177 | 1180 | l = -1; |
1178 | 1181 | break; |
1179 | 1182 | } |
@@ -3585,3 +3588,19 @@ void cli_dump_optargs_and_args(struct cli_def *cli, const char *text, char *argv |
3585 | 3588 | cli_print(cli, "Extra args"); |
3586 | 3589 | for (i = 0; i < argc; i++) cli_print(cli, "%2d %s", i, argv[i]); |
3587 | 3590 | } |
| 3591 | + |
| 3592 | +static int cli_socket_wait(int sockfd, struct timeval *tm) { |
| 3593 | +#if defined(LIBCLI_USE_POLL) && !defined(WIN32) |
| 3594 | + struct pollfd pfd = { |
| 3595 | + .fd = sockfd, |
| 3596 | + .events = POLLIN, |
| 3597 | + }; |
| 3598 | + |
| 3599 | + return poll(&pfd, 1, (tm->tv_sec * 1000) + (tm->tv_usec / 1000)); |
| 3600 | +#else |
| 3601 | + fd_set r; |
| 3602 | + FD_ZERO(&r); |
| 3603 | + FD_SET(sockfd, &r); |
| 3604 | + return select(sockfd + 1, &r, NULL, NULL, tm); |
| 3605 | +#endif |
| 3606 | +} |
0 commit comments