Line data Source code
1 : /*
2 : * Samba Unix/Linux SMB client library
3 : *
4 : * Copyright (C) Gregor Beck 2011
5 : *
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.
10 : *
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.
15 : *
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/>.
18 : */
19 :
20 : /**
21 : * @brief Functions to interact with an user.
22 : * @author Gregor Beck <gb@sernet.de>
23 : * @date Aug 2011
24 : *
25 : */
26 :
27 : #include "includes.h"
28 : #include "system/filesys.h"
29 :
30 : #include "interact.h"
31 :
32 : #include <termios.h>
33 :
34 0 : static const char* get_editor(void) {
35 0 : static char editor[64] = {0};
36 :
37 0 : if (editor[0] == '\0') {
38 0 : const char *tmp = getenv("VISUAL");
39 0 : if (tmp == NULL) {
40 0 : tmp = getenv("EDITOR");
41 : }
42 0 : if (tmp == NULL) {
43 0 : tmp = "vi";
44 : }
45 0 : snprintf(editor, sizeof(editor), "%s", tmp);
46 : }
47 :
48 0 : return editor;
49 : }
50 :
51 0 : int interact_prompt(const char* msg, const char* acc, char def) {
52 0 : struct termios old_tio, new_tio;
53 0 : int c;
54 :
55 0 : tcgetattr(STDIN_FILENO, &old_tio);
56 0 : new_tio=old_tio;
57 0 : new_tio.c_lflag &=(~ICANON & ~ECHO);
58 0 : tcsetattr(STDIN_FILENO, TCSANOW, &new_tio);
59 :
60 0 : do {
61 0 : d_printf("%s? [%c]\n", msg, def);
62 0 : fflush(stdout);
63 0 : c = getchar();
64 0 : if (c == '\n') {
65 0 : c = def;
66 0 : break;
67 : }
68 0 : else if (strchr(acc, tolower(c)) != NULL) {
69 0 : break;
70 : }
71 0 : d_printf("Invalid input '%c'\n", c);
72 0 : } while(c != EOF);
73 0 : tcsetattr(STDIN_FILENO, TCSANOW, &old_tio);
74 0 : return c;
75 : }
76 :
77 :
78 0 : char* interact_edit(TALLOC_CTX* mem_ctx, const char* str) {
79 0 : char fname[] = "/tmp/net_idmap_check.XXXXXX";
80 0 : char buf[128];
81 0 : char* ret = NULL;
82 0 : FILE* file;
83 0 : mode_t mask;
84 0 : int fd;
85 :
86 0 : mask = umask(S_IRWXO | S_IRWXG);
87 0 : fd = mkstemp(fname);
88 0 : umask(mask);
89 0 : if (fd == -1) {
90 0 : DEBUG(0, ("failed to mkstemp %s: %s\n", fname,
91 : strerror(errno)));
92 0 : return NULL;
93 : }
94 :
95 0 : file = fdopen(fd, "w");
96 0 : if (!file) {
97 0 : DEBUG(0, ("failed to open %s for writing: %s\n", fname,
98 : strerror(errno)));
99 0 : close(fd);
100 0 : unlink(fname);
101 0 : return NULL;
102 : }
103 :
104 0 : fprintf(file, "%s", str);
105 0 : fclose(file);
106 :
107 0 : snprintf(buf, sizeof(buf), "%s %s\n", get_editor(), fname);
108 0 : if (system(buf) != 0) {
109 0 : DEBUG(0, ("failed to start editor %s: %s\n", buf,
110 : strerror(errno)));
111 0 : unlink(fname);
112 0 : return NULL;
113 : }
114 :
115 0 : file = fopen(fname, "r");
116 0 : if (!file) {
117 0 : DEBUG(0, ("failed to open %s for reading: %s\n", fname,
118 : strerror(errno)));
119 0 : unlink(fname);
120 0 : return NULL;
121 : }
122 0 : while ( fgets(buf, sizeof(buf), file) ) {
123 0 : ret = talloc_strdup_append(ret, buf);
124 : }
125 0 : fclose(file);
126 0 : unlink(fname);
127 :
128 0 : return talloc_steal(mem_ctx, ret);
129 : }
130 :
131 :
132 :
133 : /*Local Variables:*/
134 : /*mode: c*/
135 : /*End:*/
|