aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulien Dessaux2019-08-06 19:22:51 +0200
committerJulien Dessaux2019-08-06 19:22:51 +0200
commitad333fb2c7790dfcc4a5af286dfb15e8f7db0a27 (patch)
treeea9d3e35e1e5bc39b43fd388434c65ea714d88de
parentAdded a proper subscriptions listing for the direct conversations closer (diff)
downloadrocket-cli-client-ad333fb2c7790dfcc4a5af286dfb15e8f7db0a27.tar.gz
rocket-cli-client-ad333fb2c7790dfcc4a5af286dfb15e8f7db0a27.tar.bz2
rocket-cli-client-ad333fb2c7790dfcc4a5af286dfb15e8f7db0a27.zip
Migrated the reading of login and password from cli to the common lib
-rw-r--r--close_direct_conversations/main.c42
-rw-r--r--common/cli.c58
-rw-r--r--common/cli.h9
3 files changed, 73 insertions, 36 deletions
diff --git a/close_direct_conversations/main.c b/close_direct_conversations/main.c
index 0938982..82bf851 100644
--- a/close_direct_conversations/main.c
+++ b/close_direct_conversations/main.c
@@ -1,9 +1,8 @@
#include <stdlib.h>
#include <stdio.h>
-#include <termios.h>
-#include <unistd.h>
#include "common/config.h"
+#include "common/cli.h"
#include "common/util.h"
#include "restapi/auth.h"
#include "restapi/im.h"
@@ -22,40 +21,12 @@ int main(void)
}
const char* login = config_get_login();
- char* termlogin = NULL;
- if (login == NULL) {
- size_t len = 0;
- printf("Login: ");
- ssize_t read = getline(&termlogin, &len, stdin);
- if (read > 1) termlogin[read-1] = 0;
- login = termlogin;
- }
+ if (login == NULL)
+ login = common_cli_get_login();
const char* password = config_get_password();
- char* termpassword = NULL;
- if (password == NULL) {
- struct termios oflags, nflags;
- tcgetattr(fileno(stdin), &oflags);
- nflags = oflags;
- nflags.c_lflag &= ~ECHO;
- nflags.c_lflag |= ECHONL;
-
- if (tcsetattr(fileno(stdin), TCSADRAIN, &nflags) != 0) {
- perror("tcsetattr");
- return -1;
- }
-
- size_t len = 0;
- printf("Password: ");
- size_t read = getline(&termpassword, &len, stdin);
- if (read > 1) termpassword[read-1] = 0;
-
- if (tcsetattr(fileno(stdin), TCSANOW, &oflags) != 0) {
- perror("tcsetattr");
- return -1;
- }
- password = termpassword;
- }
+ if (password == NULL)
+ password = common_cli_get_password();
if (restapi_login(login, password) == 0) {
struct subscription* subscriptions = restapi_subscriptions_get();
@@ -84,8 +55,7 @@ int main(void)
restapi_logout();
config_clean();
- free(termlogin);
- free(termpassword);
+ common_cli_free();
return 0;
}
diff --git a/common/cli.c b/common/cli.c
new file mode 100644
index 0000000..fb4ac1f
--- /dev/null
+++ b/common/cli.c
@@ -0,0 +1,58 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <termios.h>
+
+#include "cli.h"
+
+char * login = NULL;
+char * password = NULL;
+
+void common_cli_free(void)
+{
+ free(login);
+ login = NULL;
+ free(password);
+ password = NULL;
+}
+
+const char* common_cli_get_login(void)
+{
+ while (1) {
+ size_t len = 0;
+ printf("Login: ");
+ ssize_t read = getline(&login, &len, stdin);
+ if (read > 1) {
+ login[read-1] = 0;
+ return login;
+ }
+ }
+}
+
+const char* common_cli_get_password(void)
+{
+ struct termios oflags, nflags;
+ tcgetattr(fileno(stdin), &oflags);
+ nflags = oflags;
+ nflags.c_lflag &= ~ECHO;
+ nflags.c_lflag |= ECHONL;
+
+ if (tcsetattr(fileno(stdin), TCSADRAIN, &nflags) != 0) {
+ perror("tcsetattr");
+ exit(998);
+ }
+
+ while (1) {
+ size_t len = 0;
+ printf("Password: ");
+ size_t read = getline(&password, &len, stdin);
+ if (read > 1) {
+ password[read-1] = 0;
+ break;
+ }
+ }
+ if (tcsetattr(fileno(stdin), TCSANOW, &oflags) != 0) {
+ perror("tcsetattr 2");
+ exit(998);
+ }
+ return password;
+}
diff --git a/common/cli.h b/common/cli.h
new file mode 100644
index 0000000..51f014b
--- /dev/null
+++ b/common/cli.h
@@ -0,0 +1,9 @@
+#ifndef COMMON_CLI_H_
+#define COMMON_CLI_H_
+
+void common_cli_free(void);
+const char* common_cli_get_login(void);
+const char* common_cli_get_password(void);
+
+#endif
+