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/>.
21 * @brief Functions to interact with an user.
22 * @author Gregor Beck <gb@sernet.de>
33 static const char* get_editor(void) {
34 static char editor
[64] = {0};
36 if (editor
[0] == '\0') {
37 const char *tmp
= getenv("VISUAL");
39 tmp
= getenv("EDITOR");
44 snprintf(editor
, sizeof(editor
), "%s", tmp
);
50 int interact_prompt(const char* msg
, const char* acc
, char def
) {
51 struct termios old_tio
, new_tio
;
54 tcgetattr(STDIN_FILENO
, &old_tio
);
56 new_tio
.c_lflag
&=(~ICANON
& ~ECHO
);
57 tcsetattr(STDIN_FILENO
, TCSANOW
, &new_tio
);
60 d_printf("%s? [%c]\n", msg
, def
);
67 else if (strchr(acc
, tolower(c
)) != NULL
) {
70 d_printf("Invalid input '%c'\n", c
);
72 tcsetattr(STDIN_FILENO
, TCSANOW
, &old_tio
);
77 char* interact_edit(TALLOC_CTX
* mem_ctx
, const char* str
) {
78 char fname
[] = "/tmp/net_idmap_check.XXXXXX";
83 int fd
= mkstemp(fname
);
85 DEBUG(0, ("failed to mkstemp %s: %s\n", fname
,
90 file
= fdopen(fd
, "w");
92 DEBUG(0, ("failed to open %s for writing: %s\n", fname
,
99 fprintf(file
, "%s", str
);
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
,
110 file
= fopen(fname
, "r");
112 DEBUG(0, ("failed to open %s for reading: %s\n", fname
,
117 while ( fgets(buf
, sizeof(buf
), file
) ) {
118 ret
= talloc_strdup_append(ret
, buf
);
123 return talloc_steal(mem_ctx
, ret
);