[Asterisk-code-review] core: Minor cleanup of ast el read char. (asterisk[13])

Corey Farrell asteriskteam at digium.com
Sat Mar 17 17:47:54 CDT 2018


Corey Farrell has uploaded this change for review. ( https://gerrit.asterisk.org/8568


Change subject: core: Minor cleanup of ast_el_read_char.
......................................................................

core: Minor cleanup of ast_el_read_char.

* Define CHAR_T_LIBEDIT and CHAR_TO_LIBEDIT based on
  HAVE_LIBEDIT_IS_UNICODE.  This avoids needing to repeatedly use
  conditional blocks, eliminates having multiple function prototypes.
* Remove parenthesis from return values.
* Add missing code block brackets {}.
* Reduce use of 'else' conditional statements where possible.

Change-Id: I4315328ebea2f62641faf6881de2ac20a9f9d08e
---
M main/asterisk.c
1 file changed, 26 insertions(+), 27 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/68/8568/1

diff --git a/main/asterisk.c b/main/asterisk.c
index ee17c82..0e4eb11 100644
--- a/main/asterisk.c
+++ b/main/asterisk.c
@@ -2862,10 +2862,14 @@
 }
 
 #ifdef HAVE_LIBEDIT_IS_UNICODE
-static int ast_el_read_char(EditLine *editline, wchar_t *cp)
+#define CHAR_T_LIBEDIT wchar_t
+#define CHAR_TO_LIBEDIT(c) btowc(c)
 #else
-static int ast_el_read_char(EditLine *editline, char *cp)
+#define CHAR_T_LIBEDIT char
+#define CHAR_TO_LIBEDIT(c) c
 #endif
+
+static int ast_el_read_char(EditLine *editline, CHAR_T_LIBEDIT *cp)
 {
 	int num_read = 0;
 	int lastpos = 0;
@@ -2886,28 +2890,29 @@
 		}
 		res = ast_poll(fds, max, -1);
 		if (res < 0) {
-			if (sig_flags.need_quit || sig_flags.need_quit_handler)
+			if (sig_flags.need_quit || sig_flags.need_quit_handler) {
 				break;
-			if (errno == EINTR)
+			}
+			if (errno == EINTR) {
 				continue;
+			}
 			fprintf(stderr, "poll failed: %s\n", strerror(errno));
 			break;
 		}
 
 		if (!ast_opt_exec && fds[1].revents) {
 			char c = '\0';
+
 			num_read = read(STDIN_FILENO, &c, 1);
 			if (num_read < 1) {
 				break;
-			} else {
-#ifdef 	HAVE_LIBEDIT_IS_UNICODE
-				*cp = btowc(c);
-#else
-				*cp = c;
-#endif
-				return (num_read);
 			}
+
+			*cp = CHAR_TO_LIBEDIT(c);
+
+			return num_read;
 		}
+
 		if (fds[0].revents) {
 			res = read(ast_consock, buf, sizeof(buf) - 1);
 			/* if the remote side disappears exit */
@@ -2918,6 +2923,7 @@
 				} else {
 					int tries;
 					int reconnects_per_second = 20;
+
 					fprintf(stderr, "Attempting to reconnect for 30 seconds\n");
 					for (tries = 0; tries < 30 * reconnects_per_second; tries++) {
 						if (ast_tryconnect()) {
@@ -2926,8 +2932,9 @@
 							WELCOME_MESSAGE;
 							send_rasterisk_connect_commands();
 							break;
-						} else
-							usleep(1000000 / reconnects_per_second);
+						}
+
+						usleep(1000000 / reconnects_per_second);
 					}
 					if (tries >= 30 * reconnects_per_second) {
 						fprintf(stderr, "Failed to reconnect for 30 seconds.  Quitting.\n");
@@ -2948,25 +2955,17 @@
 			console_print(buf, 0);
 
 			if ((res < EL_BUF_SIZE - 1) && ((buf[res-1] == '\n') || (res >= 2 && buf[res-2] == '\n'))) {
-#ifdef 	HAVE_LIBEDIT_IS_UNICODE
-				*cp = btowc(CC_REFRESH);
-#else
-				*cp = CC_REFRESH;
-#endif
-				return(1);
-			} else {
-				lastpos = 1;
+				*cp = CHAR_TO_LIBEDIT(CC_REFRESH);
+
+				return 1;
 			}
+			lastpos = 1;
 		}
 	}
 
-#ifdef 	HAVE_LIBEDIT_IS_UNICODE
-	*cp = btowc('\0');
-#else
-	*cp = '\0';
-#endif
+	*cp = CHAR_TO_LIBEDIT('\0');
 
-	return (0);
+	return 0;
 }
 
 static struct ast_str *prompt = NULL;

-- 
To view, visit https://gerrit.asterisk.org/8568
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: 13
Gerrit-MessageType: newchange
Gerrit-Change-Id: I4315328ebea2f62641faf6881de2ac20a9f9d08e
Gerrit-Change-Number: 8568
Gerrit-PatchSet: 1
Gerrit-Owner: Corey Farrell <git at cfware.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20180317/d98c7622/attachment-0001.html>


More information about the asterisk-code-review mailing list