1
|
1 /*****************************************************************************/
|
|
2 /* gftp-text.c - text port of gftp */
|
122
|
3 /* Copyright (C) 1998-2003 Brian Masney <masneyb@gftp.org> */
|
1
|
4 /* */
|
|
5 /* This program is free software; you can redistribute it and/or modify */
|
|
6 /* it under the terms of the GNU General Public License as published by */
|
|
7 /* the Free Software Foundation; either version 2 of the License, or */
|
|
8 /* (at your option) any later version. */
|
|
9 /* */
|
|
10 /* This program is distributed in the hope that it will be useful, */
|
|
11 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
|
12 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
|
13 /* GNU General Public License for more details. */
|
|
14 /* */
|
|
15 /* You should have received a copy of the GNU General Public License */
|
|
16 /* along with this program; if not, write to the Free Software */
|
|
17 /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA */
|
|
18 /*****************************************************************************/
|
|
19
|
|
20 #include "gftp-text.h"
|
33
|
21 static const char cvsid[] = "$Id$";
|
1
|
22
|
|
23 static gftp_request * gftp_text_locreq = NULL;
|
|
24 static gftp_request * gftp_text_remreq = NULL;
|
|
25
|
|
26 int
|
341
|
27 gftp_text_get_win_size (void)
|
1
|
28 {
|
341
|
29 struct winsize size;
|
|
30 int ret;
|
1
|
31
|
341
|
32 if (ioctl (0, TIOCGWINSZ, (char *) &size) < 0)
|
|
33 ret = 80;
|
|
34 else
|
|
35 ret = size.ws_col;
|
1
|
36
|
341
|
37 return (ret);
|
|
38 }
|
1
|
39
|
|
40
|
387
|
41 static void
|
|
42 gftp_text_write_string (char *string)
|
|
43 {
|
|
44 char *stpos, *endpos, savechar;
|
|
45 int sw;
|
|
46
|
|
47 sw = gftp_text_get_win_size ();
|
|
48
|
|
49 stpos = string;
|
|
50 do
|
|
51 {
|
|
52 if ((endpos = strchr (stpos, '\n')) == NULL)
|
|
53 endpos = stpos + strlen (stpos);
|
|
54
|
|
55 savechar = *endpos;
|
|
56 *endpos = '\0';
|
|
57
|
|
58 if (strlen (stpos) <= sw)
|
|
59 {
|
|
60 printf ("%s%c", stpos, savechar);
|
|
61 *endpos = savechar;
|
|
62 if (savechar == '\0')
|
|
63 break;
|
|
64 stpos = endpos + 1;
|
|
65 }
|
|
66 else
|
|
67 {
|
|
68 *endpos = savechar;
|
|
69 for (endpos = stpos + sw - 1;
|
|
70 *endpos != ' ' && endpos > stpos;
|
|
71 endpos--);
|
|
72
|
|
73 if (endpos != stpos)
|
|
74 *endpos = '\0';
|
|
75
|
|
76 printf ("%s\n", stpos);
|
|
77 stpos = endpos + 1;
|
|
78 }
|
|
79
|
|
80 sw = sw;
|
|
81 }
|
|
82 while (stpos != endpos);
|
|
83 }
|
|
84
|
|
85
|
341
|
86 void
|
186
|
87 gftp_text_log (gftp_logging_level level, gftp_request * request,
|
|
88 const char *string, ...)
|
1
|
89 {
|
387
|
90 char tempstr[512], *utf8_str = NULL, *outstr;
|
1
|
91 va_list argp;
|
|
92
|
|
93 g_return_if_fail (string != NULL);
|
|
94
|
|
95 switch (level)
|
|
96 {
|
|
97 case gftp_logging_send:
|
341
|
98 printf ("%s", GFTPUI_COMMON_COLOR_GREEN);
|
1
|
99 break;
|
|
100 case gftp_logging_recv:
|
341
|
101 printf ("%s", GFTPUI_COMMON_COLOR_YELLOW);
|
1
|
102 break;
|
|
103 case gftp_logging_error:
|
341
|
104 printf ("%s", GFTPUI_COMMON_COLOR_RED);
|
1
|
105 break;
|
|
106 default:
|
341
|
107 printf ("%s", GFTPUI_COMMON_COLOR_DEFAULT);
|
1
|
108 break;
|
|
109 }
|
|
110
|
|
111 va_start (argp, string);
|
|
112 g_vsnprintf (tempstr, sizeof (tempstr), string, argp);
|
|
113 va_end (argp);
|
|
114
|
186
|
115 #if GLIB_MAJOR_VERSION > 1
|
|
116 if (!g_utf8_validate (tempstr, -1, NULL))
|
|
117 utf8_str = gftp_string_to_utf8 (request, tempstr);
|
|
118 #endif
|
|
119
|
|
120 if (utf8_str != NULL)
|
|
121 outstr = utf8_str;
|
|
122 else
|
|
123 outstr = tempstr;
|
|
124
|
354
|
125 if (gftp_logfd != NULL && level != gftp_logging_misc_nolog)
|
1
|
126 {
|
186
|
127 fwrite (outstr, 1, strlen (outstr), gftp_logfd);
|
122
|
128 if (ferror (gftp_logfd))
|
1
|
129 {
|
122
|
130 fclose (gftp_logfd);
|
|
131 gftp_logfd = NULL;
|
1
|
132 }
|
|
133 else
|
122
|
134 fflush (gftp_logfd);
|
1
|
135 }
|
|
136
|
387
|
137 if (level == gftp_logging_misc_nolog)
|
|
138 printf ("%s\n", outstr);
|
|
139 else
|
|
140 gftp_text_write_string (outstr);
|
1
|
141
|
341
|
142 printf ("%s", GFTPUI_COMMON_COLOR_DEFAULT);
|
186
|
143
|
|
144 if (utf8_str != NULL)
|
|
145 g_free (utf8_str);
|
1
|
146 }
|
|
147
|
|
148
|
341
|
149 char *
|
|
150 gftp_text_ask_question (const char *question, int echo, char *buf, size_t size)
|
|
151 {
|
378
|
152 char *pos, *termname, singlechar;
|
341
|
153 struct termios term, oldterm;
|
|
154 sigset_t sig, sigsave;
|
|
155 FILE *infd;
|
|
156
|
|
157 if (!echo)
|
|
158 {
|
|
159 sigemptyset (&sig);
|
|
160 sigaddset (&sig, SIGINT);
|
|
161 sigaddset (&sig, SIGTSTP);
|
|
162 sigprocmask (SIG_BLOCK, &sig, &sigsave);
|
|
163
|
|
164 termname = ctermid (NULL);
|
|
165 if ((infd = fopen (termname, "r+")) == NULL)
|
|
166 {
|
|
167
|
|
168 gftp_text_log (gftp_logging_error, NULL,
|
|
169 _("Cannot open controlling terminal %s\n"), termname);
|
|
170 return (NULL);
|
|
171 }
|
|
172
|
|
173 tcgetattr (0, &term);
|
|
174 oldterm = term;
|
|
175 term.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
|
|
176 tcsetattr (fileno (infd), TCSAFLUSH, &term);
|
|
177 }
|
|
178 else
|
|
179 infd = stdin;
|
|
180
|
356
|
181 printf ("%s%s%s ", GFTPUI_COMMON_COLOR_BLUE, question, GFTPUI_COMMON_COLOR_DEFAULT);
|
341
|
182
|
378
|
183 if (size == 1)
|
|
184 {
|
|
185 singlechar = fgetc (infd);
|
|
186 *buf = singlechar;
|
|
187 }
|
|
188 else
|
|
189 {
|
|
190 if (fgets (buf, size, infd) == NULL)
|
|
191 return (NULL);
|
|
192
|
|
193 if (size > 1)
|
|
194 buf[size - 1] = '\0';
|
|
195 }
|
341
|
196
|
|
197 if (!echo)
|
|
198 {
|
|
199 printf ("\n");
|
|
200 tcsetattr (fileno (infd), TCSAFLUSH, &oldterm);
|
|
201 fclose (infd);
|
|
202 sigprocmask (SIG_SETMASK, &sigsave, NULL);
|
|
203 }
|
|
204
|
378
|
205 if (size > 1)
|
|
206 {
|
|
207 for (pos = buf + strlen (buf) - 1; *pos == ' ' || *pos == '\r' ||
|
|
208 *pos == '\n'; pos--);
|
|
209 *(pos+1) = '\0';
|
|
210
|
|
211 for (pos = buf; *pos == ' '; pos++);
|
341
|
212
|
378
|
213 if (*pos == '\0')
|
|
214 return (NULL);
|
341
|
215
|
378
|
216 return (pos);
|
|
217 }
|
|
218 else
|
|
219 return (buf);
|
341
|
220 }
|
|
221
|
|
222
|
|
223 int
|
|
224 main (int argc, char **argv)
|
|
225 {
|
380
|
226 char *startup_directory, *pos;
|
|
227 void *locuidata, *remuidata;
|
341
|
228 #if HAVE_LIBREADLINE
|
|
229 char *tempstr, prompt[20];
|
|
230 #else
|
|
231 char tempstr[512];
|
|
232 #endif
|
|
233
|
374
|
234 gftpui_common_init (&argc, &argv, gftp_text_log);
|
341
|
235
|
|
236 /* SSH doesn't support reading the password with askpass via the command
|
|
237 line */
|
|
238
|
|
239 gftp_text_remreq = gftp_request_new ();
|
380
|
240 remuidata = gftp_text_remreq;
|
341
|
241 gftp_set_request_option (gftp_text_remreq, "ssh_use_askpass",
|
|
242 GINT_TO_POINTER(0));
|
|
243 gftp_set_request_option (gftp_text_remreq, "sshv2_use_sftp_subsys",
|
|
244 GINT_TO_POINTER(0));
|
|
245 gftp_text_remreq->logging_function = gftp_text_log;
|
|
246
|
|
247 gftp_text_locreq = gftp_request_new ();
|
380
|
248 locuidata = gftp_text_locreq;
|
341
|
249 gftp_set_request_option (gftp_text_locreq, "ssh_use_askpass",
|
|
250 GINT_TO_POINTER(0));
|
|
251 gftp_set_request_option (gftp_text_locreq, "sshv2_use_sftp_subsys",
|
|
252 GINT_TO_POINTER(0));
|
|
253
|
|
254 gftp_text_locreq->logging_function = gftp_text_log;
|
|
255 if (gftp_protocols[GFTP_LOCAL_NUM].init (gftp_text_locreq) == 0)
|
|
256 {
|
|
257 gftp_lookup_request_option (gftp_text_locreq, "startup_directory",
|
|
258 &startup_directory);
|
|
259 if (*startup_directory != '\0')
|
|
260 gftp_set_directory (gftp_text_locreq, startup_directory);
|
|
261
|
|
262 gftp_connect (gftp_text_locreq);
|
|
263 }
|
|
264
|
|
265 gftpui_common_about (gftp_text_log, NULL);
|
|
266 gftp_text_log (gftp_logging_misc, NULL, "\n");
|
|
267
|
|
268 if (argc == 3 && strcmp (argv[1], "-d") == 0)
|
|
269 {
|
|
270 if ((pos = strrchr (argv[2], '/')) != NULL)
|
|
271 *pos = '\0';
|
380
|
272
|
|
273 gftpui_common_cmd_open (remuidata, gftp_text_remreq,
|
|
274 locuidata, gftp_text_locreq,
|
|
275 argv[2]);
|
341
|
276
|
|
277 if (pos != NULL)
|
|
278 *pos = '/';
|
|
279
|
380
|
280 gftpui_common_cmd_mget_file (remuidata, gftp_text_remreq,
|
|
281 locuidata, gftp_text_locreq,
|
|
282 pos + 1);
|
341
|
283 exit (0);
|
|
284 }
|
|
285 else if (argc == 2)
|
380
|
286 {
|
|
287 gftpui_common_cmd_open (remuidata, gftp_text_remreq,
|
|
288 locuidata, gftp_text_locreq,
|
|
289 argv[1]);
|
|
290 }
|
341
|
291
|
|
292 #if HAVE_LIBREADLINE
|
|
293 g_snprintf (prompt, sizeof (prompt), "%sftp%s> ", GFTPUI_COMMON_COLOR_BLUE, GFTPUI_COMMON_COLOR_DEFAULT);
|
|
294 while ((tempstr = readline (prompt)))
|
|
295 {
|
380
|
296 if (gftpui_common_process_command (locuidata, gftp_text_locreq,
|
|
297 remuidata, gftp_text_remreq,
|
|
298 tempstr) == 0)
|
341
|
299 break;
|
|
300
|
|
301 add_history (tempstr);
|
|
302 free (tempstr);
|
|
303 }
|
|
304 #else
|
|
305 printf ("%sftp%s> ", GFTPUI_COMMON_COLOR_BLUE, GFTPUI_COMMON_COLOR_DEFAULT);
|
|
306 while (fgets (tempstr, sizeof (tempstr), stdin) != NULL)
|
|
307 {
|
399
|
308 if (gftpui_common_process_command (locuidata, gftp_text_locreq,
|
|
309 remuidata, gftp_text_remreq,
|
|
310 tempstr) == 0)
|
341
|
311 break;
|
|
312
|
|
313 printf ("%sftp%s> ", GFTPUI_COMMON_COLOR_BLUE, GFTPUI_COMMON_COLOR_DEFAULT);
|
|
314 }
|
|
315 #endif
|
|
316
|
|
317 return (0);
|
|
318 }
|
|
319
|