From 273ed22f8e09315cb7c444ab64d0cac3d8d47efc Mon Sep 17 00:00:00 2001 From: nirenjan Date: Mon, 16 Mar 2026 23:02:15 -0700 Subject: [PATCH] refactor(x52ctl): Break out interactive mode SonarQube keeps complaining about issues with cognitive complexity and bad practices, this commit addresses those commits. --- daemon/x52ctl.c | 75 +++++++++++++++++++++++++++---------------------- po/libx52.pot | 4 +-- po/xx_PL.po | 4 +-- 3 files changed, 45 insertions(+), 38 deletions(-) diff --git a/daemon/x52ctl.c b/daemon/x52ctl.c index f200e37..a948752 100644 --- a/daemon/x52ctl.c +++ b/daemon/x52ctl.c @@ -89,15 +89,53 @@ static int send_command(int sock_fd, int argc, char **argv) return 0; } +static void interactive_mode(int sock_fd) +{ + bool keep_running = true; + char buffer[1024]; + + fputs("> ", stdout); + while (keep_running && fgets(buffer, sizeof(buffer), stdin) != NULL) { + int sargc; + char *sargv[512] = { 0 }; + int pos; + + if (strcasecmp(buffer, "quit\n") == 0) { + keep_running = false; + } else { + /* Break the buffer into argc/argv */ + sargc = 0; + pos = 0; + while (buffer[pos]) { + if (isspace(buffer[pos])) { + buffer[pos] = '\0'; + pos++; + } else { + sargv[sargc] = &buffer[pos]; + sargc++; + for (; buffer[pos] && !isspace(buffer[pos]); pos++); + } + } + + if (send_command(sock_fd, sargc, sargv)) { + keep_running = false; + } + } + + if (keep_running) { + fputs("\n> ", stdout); + } + } +} + int main(int argc, char **argv) { bool interactive = false; - char *socket_path = NULL; + const char *socket_path = NULL; int opt; int sock_fd; int rc = EXIT_SUCCESS; - char buffer[1024]; /* * Parse command line arguments @@ -142,38 +180,7 @@ int main(int argc, char **argv) _("Running in interactive mode, ignoring extra arguments\n")); } - fputs("> ", stdout); - while (fgets(buffer, sizeof(buffer), stdin) != NULL) { - int sargc; - char *sargv[512] = { 0 }; - int pos; - - if (strcasecmp(buffer, "quit\n") == 0) { - break; - } - - /* Break the buffer into argc/argv */ - sargc = 0; - pos = 0; - while (buffer[pos]) { - if (isspace(buffer[pos])) { - buffer[pos] = '\0'; - pos++; - } else { - sargv[sargc] = &buffer[pos]; - sargc++; - for (; buffer[pos] && !isspace(buffer[pos]); pos++); - } - } - - if (send_command(sock_fd, sargc, sargv)) { - rc = EXIT_FAILURE; - break; - } - - fputs("\n> ", stdout); - } - + interactive_mode(sock_fd); } else { if (send_command(sock_fd, argc - optind, &argv[optind])) { rc = EXIT_FAILURE; diff --git a/po/libx52.pot b/po/libx52.pot index 79ce0dd..38f3e73 100644 --- a/po/libx52.pot +++ b/po/libx52.pot @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: libx52 0.3.3\n" "Report-Msgid-Bugs-To: https://github.com/nirenjan/libx52/issues\n" -"POT-Creation-Date: 2026-03-12 12:28-0700\n" +"POT-Creation-Date: 2026-03-16 23:07-0700\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -951,7 +951,7 @@ msgstr "" msgid "Argument length too long\n" msgstr "" -#: daemon/x52ctl.c:142 +#: daemon/x52ctl.c:180 #, c-format msgid "Running in interactive mode, ignoring extra arguments\n" msgstr "" diff --git a/po/xx_PL.po b/po/xx_PL.po index 13e4c1e..061b2fd 100644 --- a/po/xx_PL.po +++ b/po/xx_PL.po @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: libx52 0.2.3\n" "Report-Msgid-Bugs-To: https://github.com/nirenjan/libx52/issues\n" -"POT-Creation-Date: 2026-03-12 12:28-0700\n" +"POT-Creation-Date: 2026-03-16 23:07-0700\n" "PO-Revision-Date: 2023-01-04 08:40-0800\n" "Last-Translator: Nirenjan Krishnan \n" "Language-Team: Dummy Language for testing i18n\n" @@ -1004,7 +1004,7 @@ msgstr "Usageay: %s [-i] [-s ocketsay-athpay] [ommandcay]\n" msgid "Argument length too long\n" msgstr "Argumentay engthlay ootay onglay\n" -#: daemon/x52ctl.c:142 +#: daemon/x52ctl.c:180 #, c-format msgid "Running in interactive mode, ignoring extra arguments\n" msgstr "Unningray inay interactiveay odemay, ignoringay extraay argumentsay\n"