diff options
author | Gregor Beck <gbeck@sernet.de> | 2011-08-18 14:13:28 +0200 |
---|---|---|
committer | Michael Adam <obnox@samba.org> | 2011-10-12 22:45:53 +0200 |
commit | e430c75d4c275323f5a3ac894647f8ebf2a58a7a (patch) | |
tree | 6bc334f2fff6e57ea0ba03306022feff6c2efa68 | |
parent | 6b28a517e3a7d128ae6018dbbe6545d4f33cfdb7 (diff) | |
download | samba-e430c75d4c275323f5a3ac894647f8ebf2a58a7a.tar.gz samba-e430c75d4c275323f5a3ac894647f8ebf2a58a7a.tar.bz2 samba-e430c75d4c275323f5a3ac894647f8ebf2a58a7a.zip |
s3:net: factor out interact_edit()
Signed-off-by: Michael Adam <obnox@samba.org>
-rw-r--r-- | source3/Makefile.in | 3 | ||||
-rw-r--r-- | source3/utils/interact.c | 126 | ||||
-rw-r--r-- | source3/utils/interact.h | 36 | ||||
-rw-r--r-- | source3/utils/net_idmap_check.c | 94 |
4 files changed, 165 insertions, 94 deletions
diff --git a/source3/Makefile.in b/source3/Makefile.in index 24567e27ef..8298520a6c 100644 --- a/source3/Makefile.in +++ b/source3/Makefile.in @@ -1187,7 +1187,8 @@ LIBNET_SAMSYNC_OBJ = libnet/libnet_samsync.o \ NET_OBJ1 = utils/net.o utils/net_ads.o utils/net_help.o \ utils/net_rap.o utils/net_rpc.o utils/net_rpc_samsync.o \ utils/net_rpc_join.o utils/net_time.o utils/net_lookup.o \ - utils/net_cache.o utils/net_groupmap.o utils/net_idmap.o utils/net_idmap_check.o\ + utils/net_cache.o utils/net_groupmap.o utils/net_idmap.o \ + utils/net_idmap_check.o utils/interact.o \ utils/net_status.o utils/net_rpc_printer.o utils/net_rpc_rights.o \ utils/net_rpc_service.o utils/net_rpc_registry.o utils/net_usershare.o \ utils/netlookup.o utils/net_sam.o utils/net_rpc_shell.o \ diff --git a/source3/utils/interact.c b/source3/utils/interact.c new file mode 100644 index 0000000000..a18e56c3c9 --- /dev/null +++ b/source3/utils/interact.c @@ -0,0 +1,126 @@ +/* + * Samba Unix/Linux SMB client library + * + * Copyright (C) Gregor Beck 2011 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +/** + * @brief Functions to interact with an user. + * @author Gregor Beck <gb@sernet.de> + * @date Aug 2011 + * + */ + +#include "interact.h" + +#include "includes.h" +#include <termios.h> + +static const char* get_editor(void) { + static const char* editor = NULL; + if (editor == NULL) { + editor = getenv("VISUAL"); + if (editor == NULL) { + editor = getenv("EDITOR"); + } + if (editor == NULL) { + editor = "vi"; + } + } + return editor; +} + +int interact_prompt(const char* msg, const char* acc, char def) { + struct termios old_tio, new_tio; + int c; + + tcgetattr(STDIN_FILENO, &old_tio); + new_tio=old_tio; + new_tio.c_lflag &=(~ICANON & ~ECHO); + tcsetattr(STDIN_FILENO, TCSANOW, &new_tio); + + do { + d_printf("%s? [%c]\n", msg, def); + fflush(stdout); + c = getchar(); + if (c == '\n') { + c = def; + break; + } + else if (strchr(acc, tolower(c)) != NULL) { + break; + } + d_printf("Invalid input '%c'\n", c); + } while(c != EOF); + tcsetattr(STDIN_FILENO, TCSANOW, &old_tio); + return c; +} + + +char* interact_edit(TALLOC_CTX* mem_ctx, const char* str) { + char fname[] = "/tmp/net_idmap_check.XXXXXX"; + char buf[128]; + char* ret = NULL; + FILE* file; + + int fd = mkstemp(fname); + if (fd == -1) { + DEBUG(0, ("failed to mkstemp %s: %s\n", fname, + strerror(errno))); + return NULL; + } + + file = fdopen(fd, "w"); + if (!file) { + DEBUG(0, ("failed to open %s for writing: %s\n", fname, + strerror(errno))); + close(fd); + unlink(fname); + return NULL; + } + + fprintf(file, "%s", str); + fclose(file); + + snprintf(buf, sizeof(buf), "%s %s\n", get_editor(), fname); + if (system(buf) != 0) { + DEBUG(0, ("failed to start editor %s: %s\n", buf, + strerror(errno))); + unlink(fname); + return NULL; + } + + file = fopen(fname, "r"); + if (!file) { + DEBUG(0, ("failed to open %s for reading: %s\n", fname, + strerror(errno))); + unlink(fname); + return NULL; + } + while ( fgets(buf, sizeof(buf), file) ) { + ret = talloc_strdup_append(ret, buf); + } + fclose(file); + unlink(fname); + + return talloc_steal(mem_ctx, ret); +} + + + +/*Local Variables:*/ +/*mode: c*/ +/*End:*/ diff --git a/source3/utils/interact.h b/source3/utils/interact.h new file mode 100644 index 0000000000..c719d09900 --- /dev/null +++ b/source3/utils/interact.h @@ -0,0 +1,36 @@ +/* * Samba Unix/Linux SMB client library + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +/** + * @brief Functions to interact with an user. + * @author Gregor Beck <gb@sernet.de> + * @date Aug 2011 + */ + +#ifndef __INTERACT_H +#define __INTERACT_H +#include <talloc.h> + +char* interact_edit(TALLOC_CTX* mem_ctx, const char* str); +int interact_prompt(const char* msg, const char* accept, char def); + + + +#endif /* __INTERACT_H */ + +/*Local Variables:*/ +/*mode: c++*/ +/*End:*/ diff --git a/source3/utils/net_idmap_check.c b/source3/utils/net_idmap_check.c index 960a597073..de654066ec 100644 --- a/source3/utils/net_idmap_check.c +++ b/source3/utils/net_idmap_check.c @@ -33,15 +33,12 @@ #include "../libcli/security/dom_sid.h" #include "cbuf.h" #include "srprs.h" -#include <termios.h> #include "util_tdb.h" +#include "interact.h" static int traverse_commit(struct db_record *diff_rec, void* data); static int traverse_check(struct db_record *rec, void* data); -static char* interact_edit(TALLOC_CTX* mem_ctx, const char* str); -static int interact_prompt(const char* msg, const char* accept, char def); - /* TDB_DATA *******************************************************************/ static char* print_data(TALLOC_CTX* mem_ctx, TDB_DATA d); static TDB_DATA parse_data(TALLOC_CTX* mem_ctx, const char** ptr); @@ -670,31 +667,6 @@ struct record* reverse_record(struct record* in) /******************************************************************************/ -int interact_prompt(const char* msg, const char* acc, char def) { - struct termios old_tio, new_tio; - int c; - - tcgetattr(STDIN_FILENO, &old_tio); - new_tio=old_tio; - new_tio.c_lflag &=(~ICANON & ~ECHO); - tcsetattr(STDIN_FILENO, TCSANOW, &new_tio); - - do { - d_printf("%s? [%c]\n", msg, def); - fflush(stdout); - c = getchar(); - if (c == '\n') { - c = def; - break; - } - else if (strchr(acc, tolower(c)) != NULL) { - break; - } - d_printf("Invalid input '%c'\n", c); - } while(c != EOF); - tcsetattr(STDIN_FILENO, TCSANOW, &old_tio); - return c; -} char* print_data(TALLOC_CTX* mem_ctx, TDB_DATA d) { @@ -725,70 +697,6 @@ TDB_DATA parse_data(TALLOC_CTX* mem_ctx, const char** ptr) { return ret; } -static const char* get_editor(void) { - static const char* editor = NULL; - if (editor == NULL) { - editor = getenv("VISUAL"); - if (editor == NULL) { - editor = getenv("EDITOR"); - } - if (editor == NULL) { - editor = "vi"; - } - } - return editor; -} - -char* interact_edit(TALLOC_CTX* mem_ctx, const char* str) { - char fname[] = "/tmp/net_idmap_check.XXXXXX"; - char buf[128]; - char* ret = NULL; - FILE* file; - - int fd = mkstemp(fname); - if (fd == -1) { - DEBUG(0, ("failed to mkstemp %s: %s\n", fname, - strerror(errno))); - return NULL; - } - - file = fdopen(fd, "w"); - if (!file) { - DEBUG(0, ("failed to open %s for writing: %s\n", fname, - strerror(errno))); - close(fd); - unlink(fname); - return NULL; - } - - fprintf(file, "%s", str); - fclose(file); - - snprintf(buf, sizeof(buf), "%s %s\n", get_editor(), fname); - if (system(buf) != 0) { - DEBUG(0, ("failed to start editor %s: %s\n", buf, - strerror(errno))); - unlink(fname); - return NULL; - } - - file = fopen(fname, "r"); - if (!file) { - DEBUG(0, ("failed to open %s for reading: %s\n", fname, - strerror(errno))); - unlink(fname); - return NULL; - } - while ( fgets(buf, sizeof(buf), file) ) { - ret = talloc_strdup_append(ret, buf); - } - fclose(file); - unlink(fname); - - return talloc_steal(mem_ctx, ret); -} - - static int traverse_print_diff(struct db_record *rec, void* data) { struct check_ctx* ctx = (struct check_ctx*)data; TDB_DATA key; |