lib/dbwrap: talloc_strdup() name in db_open_file()
[Samba/gebeck_regimport.git] / source3 / utils / interact.c
blob6d753dd012eb1d0b9c82e600a8641f1983916baa
1 /*
2 * Samba Unix/Linux SMB client library
4 * Copyright (C) Gregor Beck 2011
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 /**
21 * @brief Functions to interact with an user.
22 * @author Gregor Beck <gb@sernet.de>
23 * @date Aug 2011
27 #include "includes.h"
29 #include "interact.h"
31 #include <termios.h>
33 static const char* get_editor(void) {
34 static char editor[64] = {0};
36 if (editor[0] == '\0') {
37 const char *tmp = getenv("VISUAL");
38 if (tmp == NULL) {
39 tmp = getenv("EDITOR");
41 if (tmp == NULL) {
42 tmp = "vi";
44 snprintf(editor, sizeof(editor), "%s", tmp);
47 return editor;
50 int interact_prompt(const char* msg, const char* acc, char def) {
51 struct termios old_tio, new_tio;
52 int c;
54 tcgetattr(STDIN_FILENO, &old_tio);
55 new_tio=old_tio;
56 new_tio.c_lflag &=(~ICANON & ~ECHO);
57 tcsetattr(STDIN_FILENO, TCSANOW, &new_tio);
59 do {
60 d_printf("%s? [%c]\n", msg, def);
61 fflush(stdout);
62 c = getchar();
63 if (c == '\n') {
64 c = def;
65 break;
67 else if (strchr(acc, tolower(c)) != NULL) {
68 break;
70 d_printf("Invalid input '%c'\n", c);
71 } while(c != EOF);
72 tcsetattr(STDIN_FILENO, TCSANOW, &old_tio);
73 return c;
77 char* interact_edit(TALLOC_CTX* mem_ctx, const char* str) {
78 char fname[] = "/tmp/net_idmap_check.XXXXXX";
79 char buf[128];
80 char* ret = NULL;
81 FILE* file;
83 int fd = mkstemp(fname);
84 if (fd == -1) {
85 DEBUG(0, ("failed to mkstemp %s: %s\n", fname,
86 strerror(errno)));
87 return NULL;
90 file = fdopen(fd, "w");
91 if (!file) {
92 DEBUG(0, ("failed to open %s for writing: %s\n", fname,
93 strerror(errno)));
94 close(fd);
95 unlink(fname);
96 return NULL;
99 fprintf(file, "%s", str);
100 fclose(file);
102 snprintf(buf, sizeof(buf), "%s %s\n", get_editor(), fname);
103 if (system(buf) != 0) {
104 DEBUG(0, ("failed to start editor %s: %s\n", buf,
105 strerror(errno)));
106 unlink(fname);
107 return NULL;
110 file = fopen(fname, "r");
111 if (!file) {
112 DEBUG(0, ("failed to open %s for reading: %s\n", fname,
113 strerror(errno)));
114 unlink(fname);
115 return NULL;
117 while ( fgets(buf, sizeof(buf), file) ) {
118 ret = talloc_strdup_append(ret, buf);
120 fclose(file);
121 unlink(fname);
123 return talloc_steal(mem_ctx, ret);
128 /*Local Variables:*/
129 /*mode: c*/
130 /*End:*/