s4-torture: verify #8373 and add ndr nbt tests for LOGON_PRIMARY_QUERY nbt_netlogon_r...
[Samba/wip.git] / source3 / utils / interact.c
blob39ec7071760960ee451aa5c78a49cfc3172bf810
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 const char* editor = NULL;
35 if (editor == NULL) {
36 editor = getenv("VISUAL");
37 if (editor == NULL) {
38 editor = getenv("EDITOR");
40 if (editor == NULL) {
41 editor = "vi";
44 return editor;
47 int interact_prompt(const char* msg, const char* acc, char def) {
48 struct termios old_tio, new_tio;
49 int c;
51 tcgetattr(STDIN_FILENO, &old_tio);
52 new_tio=old_tio;
53 new_tio.c_lflag &=(~ICANON & ~ECHO);
54 tcsetattr(STDIN_FILENO, TCSANOW, &new_tio);
56 do {
57 d_printf("%s? [%c]\n", msg, def);
58 fflush(stdout);
59 c = getchar();
60 if (c == '\n') {
61 c = def;
62 break;
64 else if (strchr(acc, tolower(c)) != NULL) {
65 break;
67 d_printf("Invalid input '%c'\n", c);
68 } while(c != EOF);
69 tcsetattr(STDIN_FILENO, TCSANOW, &old_tio);
70 return c;
74 char* interact_edit(TALLOC_CTX* mem_ctx, const char* str) {
75 char fname[] = "/tmp/net_idmap_check.XXXXXX";
76 char buf[128];
77 char* ret = NULL;
78 FILE* file;
80 int fd = mkstemp(fname);
81 if (fd == -1) {
82 DEBUG(0, ("failed to mkstemp %s: %s\n", fname,
83 strerror(errno)));
84 return NULL;
87 file = fdopen(fd, "w");
88 if (!file) {
89 DEBUG(0, ("failed to open %s for writing: %s\n", fname,
90 strerror(errno)));
91 close(fd);
92 unlink(fname);
93 return NULL;
96 fprintf(file, "%s", str);
97 fclose(file);
99 snprintf(buf, sizeof(buf), "%s %s\n", get_editor(), fname);
100 if (system(buf) != 0) {
101 DEBUG(0, ("failed to start editor %s: %s\n", buf,
102 strerror(errno)));
103 unlink(fname);
104 return NULL;
107 file = fopen(fname, "r");
108 if (!file) {
109 DEBUG(0, ("failed to open %s for reading: %s\n", fname,
110 strerror(errno)));
111 unlink(fname);
112 return NULL;
114 while ( fgets(buf, sizeof(buf), file) ) {
115 ret = talloc_strdup_append(ret, buf);
117 fclose(file);
118 unlink(fname);
120 return talloc_steal(mem_ctx, ret);
125 /*Local Variables:*/
126 /*mode: c*/
127 /*End:*/