vfs: Make function pointer names consistent. They all end in _fn
[Samba/gebeck_regimport.git] / source3 / utils / interact.c
bloba18e56c3c9ffca449f704c1d1862e2be8798fb65
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 "interact.h"
29 #include "includes.h"
30 #include <termios.h>
32 static const char* get_editor(void) {
33 static const char* editor = NULL;
34 if (editor == NULL) {
35 editor = getenv("VISUAL");
36 if (editor == NULL) {
37 editor = getenv("EDITOR");
39 if (editor == NULL) {
40 editor = "vi";
43 return editor;
46 int interact_prompt(const char* msg, const char* acc, char def) {
47 struct termios old_tio, new_tio;
48 int c;
50 tcgetattr(STDIN_FILENO, &old_tio);
51 new_tio=old_tio;
52 new_tio.c_lflag &=(~ICANON & ~ECHO);
53 tcsetattr(STDIN_FILENO, TCSANOW, &new_tio);
55 do {
56 d_printf("%s? [%c]\n", msg, def);
57 fflush(stdout);
58 c = getchar();
59 if (c == '\n') {
60 c = def;
61 break;
63 else if (strchr(acc, tolower(c)) != NULL) {
64 break;
66 d_printf("Invalid input '%c'\n", c);
67 } while(c != EOF);
68 tcsetattr(STDIN_FILENO, TCSANOW, &old_tio);
69 return c;
73 char* interact_edit(TALLOC_CTX* mem_ctx, const char* str) {
74 char fname[] = "/tmp/net_idmap_check.XXXXXX";
75 char buf[128];
76 char* ret = NULL;
77 FILE* file;
79 int fd = mkstemp(fname);
80 if (fd == -1) {
81 DEBUG(0, ("failed to mkstemp %s: %s\n", fname,
82 strerror(errno)));
83 return NULL;
86 file = fdopen(fd, "w");
87 if (!file) {
88 DEBUG(0, ("failed to open %s for writing: %s\n", fname,
89 strerror(errno)));
90 close(fd);
91 unlink(fname);
92 return NULL;
95 fprintf(file, "%s", str);
96 fclose(file);
98 snprintf(buf, sizeof(buf), "%s %s\n", get_editor(), fname);
99 if (system(buf) != 0) {
100 DEBUG(0, ("failed to start editor %s: %s\n", buf,
101 strerror(errno)));
102 unlink(fname);
103 return NULL;
106 file = fopen(fname, "r");
107 if (!file) {
108 DEBUG(0, ("failed to open %s for reading: %s\n", fname,
109 strerror(errno)));
110 unlink(fname);
111 return NULL;
113 while ( fgets(buf, sizeof(buf), file) ) {
114 ret = talloc_strdup_append(ret, buf);
116 fclose(file);
117 unlink(fname);
119 return talloc_steal(mem_ctx, ret);
124 /*Local Variables:*/
125 /*mode: c*/
126 /*End:*/