Code

.gitignore: Ignore ar-lib.
[sysdb.git] / src / client / sysdb.c
index fa71eed9d064b04d86037412988e5b10a5c1c7b6..ecadb4c18462cd94e61cfe116561269efe7622c0 100644 (file)
@@ -32,6 +32,7 @@
 #include "client/sysdb.h"
 #include "client/sock.h"
 #include "utils/error.h"
+#include "utils/strbuf.h"
 
 #include <errno.h>
 
 
 #include <pwd.h>
 
+#if HAVE_EDITLINE_READLINE_H
+#      include <editline/readline.h>
+#      if HAVE_EDITLINE_HISTORY_H
+#              include <editline/history.h>
+#      endif
+#elif HAVE_READLINE_READLINE_H
+#      include <readline/readline.h>
+#      if HAVE_READLINE_HISTORY_H
+#              include <readline/history.h>
+#      endif
+#elif HAVE_READLINE_H
+#      include <readline.h>
+#      if HAVE_HISTORY_H
+#              include <history.h>
+#      endif
+#endif /* READLINEs */
+
 #ifndef DEFAULT_SOCKET
 #      define DEFAULT_SOCKET "unix:"LOCALSTATEDIR"/run/sysdbd.sock"
 #endif
@@ -117,6 +135,31 @@ get_current_user(void)
        return result->pw_name;
 } /* get_current_user */
 
+static const char *
+get_homedir(const char *username)
+{
+       struct passwd pw_entry;
+       struct passwd *result = NULL;
+
+       /* needs to be static because we return a pointer into this buffer
+        * to the caller */
+       static char buf[1024];
+
+       int status;
+
+       memset(&pw_entry, 0, sizeof(pw_entry));
+       status = getpwnam_r(username, &pw_entry, buf, sizeof(buf), &result);
+
+       if (status || (! result)) {
+               char errbuf[1024];
+               sdb_log(SDB_LOG_WARNING, "Failed to determine home directory "
+                               "for user %s: %s", username,
+                               sdb_strerror(errno, errbuf, sizeof(errbuf)));
+               return NULL;
+       }
+       return result->pw_dir;
+} /* get_homedir */
+
 int
 main(int argc, char **argv)
 {
@@ -125,6 +168,11 @@ main(int argc, char **argv)
        const char *host = NULL;
        const char *user = NULL;
 
+       const char *homedir;
+       char hist_file[1024] = "";
+
+       sdb_strbuf_t *buf;
+
        while (42) {
                int opt = getopt(argc, argv, "H:U:hV");
 
@@ -155,8 +203,11 @@ main(int argc, char **argv)
 
        if (! host)
                host = DEFAULT_SOCKET;
-       if (! user)
+       if (! user) {
                user = get_current_user();
+               if (! user)
+                       exit(1);
+       }
 
        client = sdb_client_create(host);
        if (! client) {
@@ -172,6 +223,56 @@ main(int argc, char **argv)
        sdb_log(SDB_LOG_INFO, "SysDB client "SDB_CLIENT_VERSION_STRING
                        SDB_CLIENT_VERSION_EXTRA"\n");
 
+       using_history();
+
+       if ((homedir = get_homedir(user))) {
+               snprintf(hist_file, sizeof(hist_file) - 1,
+                               "%s/.sysdb_history", homedir);
+               hist_file[sizeof(hist_file) - 1] = '\0';
+
+               errno = 0;
+               if (read_history(hist_file) && (errno != ENOENT)) {
+                       char errbuf[1024];
+                       sdb_log(SDB_LOG_WARNING, "Failed to load history (%s): %s",
+                                       hist_file, sdb_strerror(errno, errbuf, sizeof(errbuf)));
+               }
+       }
+
+       buf = sdb_strbuf_create(1024);
+
+       while (42) {
+               const char *prompt = "sysdb=> ";
+               const char *query;
+               char *input;
+
+               if (sdb_strbuf_len(buf))
+                       prompt = "sysdb-> ";
+
+               input = readline(prompt);
+
+               if (! input)
+                       break;
+
+               sdb_strbuf_append(buf, input);
+               free(input);
+
+               query = sdb_strbuf_string(buf);
+               if (! strchr(query, (int)';'))
+                       continue;
+
+               /* XXX */
+               sdb_strbuf_clear(buf);
+       }
+
+       if (hist_file[0] != '\0') {
+               errno = 0;
+               if (write_history(hist_file)) {
+                       char errbuf[1024];
+                       sdb_log(SDB_LOG_WARNING, "Failed to store history (%s): %s",
+                                       hist_file, sdb_strerror(errno, errbuf, sizeof(errbuf)));
+               }
+       }
+
        sdb_client_destroy(client);
        return 0;
 } /* main */