1
|
1 /*****************************************************************************/
|
|
2 /* gftp-text.c - text port of gftp */
|
885
|
3 /* Copyright (C) 1998-2007 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
|
675
|
23 unsigned int
|
341
|
24 gftp_text_get_win_size (void)
|
1
|
25 {
|
341
|
26 struct winsize size;
|
675
|
27 unsigned int ret;
|
1
|
28
|
341
|
29 if (ioctl (0, TIOCGWINSZ, (char *) &size) < 0)
|
|
30 ret = 80;
|
|
31 else
|
|
32 ret = size.ws_col;
|
1
|
33
|
341
|
34 return (ret);
|
|
35 }
|
1
|
36
|
|
37
|
387
|
38 static void
|
675
|
39 gftp_text_write_string (gftp_request * request, char *string)
|
387
|
40 {
|
675
|
41 gchar *stpos, *endpos, *locale_str, savechar;
|
|
42 unsigned int sw;
|
838
|
43 size_t destlen;
|
387
|
44
|
|
45 sw = gftp_text_get_win_size ();
|
|
46
|
846
|
47 locale_str = gftp_string_from_utf8 (request, 1, string, &destlen);
|
675
|
48 if (locale_str == NULL)
|
|
49 stpos = string;
|
|
50 else
|
|
51 stpos = locale_str;
|
|
52
|
387
|
53 do
|
|
54 {
|
|
55 if ((endpos = strchr (stpos, '\n')) == NULL)
|
|
56 endpos = stpos + strlen (stpos);
|
|
57
|
|
58 savechar = *endpos;
|
|
59 *endpos = '\0';
|
|
60
|
|
61 if (strlen (stpos) <= sw)
|
|
62 {
|
|
63 printf ("%s%c", stpos, savechar);
|
|
64 *endpos = savechar;
|
|
65 if (savechar == '\0')
|
|
66 break;
|
|
67 stpos = endpos + 1;
|
|
68 }
|
|
69 else
|
|
70 {
|
|
71 *endpos = savechar;
|
|
72 for (endpos = stpos + sw - 1;
|
|
73 *endpos != ' ' && endpos > stpos;
|
|
74 endpos--);
|
|
75
|
|
76 if (endpos != stpos)
|
|
77 *endpos = '\0';
|
|
78
|
|
79 printf ("%s\n", stpos);
|
|
80 stpos = endpos + 1;
|
|
81 }
|
|
82
|
|
83 sw = sw;
|
|
84 }
|
|
85 while (stpos != endpos);
|
675
|
86
|
|
87 if (locale_str != NULL)
|
|
88 g_free (locale_str);
|
387
|
89 }
|
|
90
|
|
91
|
518
|
92 static void
|
186
|
93 gftp_text_log (gftp_logging_level level, gftp_request * request,
|
|
94 const char *string, ...)
|
1
|
95 {
|
838
|
96 char tempstr[512];
|
1
|
97 va_list argp;
|
|
98
|
|
99 g_return_if_fail (string != NULL);
|
|
100
|
|
101 switch (level)
|
|
102 {
|
|
103 case gftp_logging_send:
|
341
|
104 printf ("%s", GFTPUI_COMMON_COLOR_GREEN);
|
1
|
105 break;
|
|
106 case gftp_logging_recv:
|
341
|
107 printf ("%s", GFTPUI_COMMON_COLOR_YELLOW);
|
1
|
108 break;
|
|
109 case gftp_logging_error:
|
341
|
110 printf ("%s", GFTPUI_COMMON_COLOR_RED);
|
1
|
111 break;
|
|
112 default:
|
341
|
113 printf ("%s", GFTPUI_COMMON_COLOR_DEFAULT);
|
1
|
114 break;
|
|
115 }
|
|
116
|
|
117 va_start (argp, string);
|
|
118 g_vsnprintf (tempstr, sizeof (tempstr), string, argp);
|
|
119 va_end (argp);
|
|
120
|
354
|
121 if (gftp_logfd != NULL && level != gftp_logging_misc_nolog)
|
1
|
122 {
|
838
|
123 fwrite (tempstr, 1, strlen (tempstr), gftp_logfd);
|
122
|
124 if (ferror (gftp_logfd))
|
1
|
125 {
|
122
|
126 fclose (gftp_logfd);
|
|
127 gftp_logfd = NULL;
|
1
|
128 }
|
|
129 else
|
122
|
130 fflush (gftp_logfd);
|
1
|
131 }
|
|
132
|
387
|
133 if (level == gftp_logging_misc_nolog)
|
838
|
134 printf ("%s", tempstr);
|
387
|
135 else
|
838
|
136 gftp_text_write_string (request, tempstr);
|
1
|
137
|
341
|
138 printf ("%s", GFTPUI_COMMON_COLOR_DEFAULT);
|
1
|
139 }
|
|
140
|
|
141
|
341
|
142 char *
|
846
|
143 gftp_text_ask_question (gftp_request * request, const char *question, int echo,
|
|
144 char *buf, size_t size)
|
341
|
145 {
|
|
146 struct termios term, oldterm;
|
675
|
147 gchar *locale_question;
|
341
|
148 sigset_t sig, sigsave;
|
518
|
149 char *pos, *termname;
|
838
|
150 size_t destlen;
|
518
|
151 int singlechar;
|
341
|
152 FILE *infd;
|
|
153
|
|
154 if (!echo)
|
|
155 {
|
|
156 sigemptyset (&sig);
|
|
157 sigaddset (&sig, SIGINT);
|
|
158 sigaddset (&sig, SIGTSTP);
|
|
159 sigprocmask (SIG_BLOCK, &sig, &sigsave);
|
|
160
|
|
161 termname = ctermid (NULL);
|
|
162 if ((infd = fopen (termname, "r+")) == NULL)
|
|
163 {
|
|
164
|
|
165 gftp_text_log (gftp_logging_error, NULL,
|
|
166 _("Cannot open controlling terminal %s\n"), termname);
|
|
167 return (NULL);
|
|
168 }
|
|
169
|
|
170 tcgetattr (0, &term);
|
|
171 oldterm = term;
|
|
172 term.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
|
|
173 tcsetattr (fileno (infd), TCSAFLUSH, &term);
|
|
174 }
|
|
175 else
|
|
176 infd = stdin;
|
|
177
|
846
|
178 locale_question = gftp_string_from_utf8 (request, 1, question, &destlen);
|
675
|
179 if (locale_question != NULL)
|
|
180 {
|
|
181 printf ("%s%s%s ", GFTPUI_COMMON_COLOR_BLUE, locale_question,
|
|
182 GFTPUI_COMMON_COLOR_DEFAULT);
|
|
183 g_free (locale_question);
|
|
184 }
|
|
185 else
|
|
186 printf ("%s%s%s ", GFTPUI_COMMON_COLOR_BLUE, question,
|
|
187 GFTPUI_COMMON_COLOR_DEFAULT);
|
341
|
188
|
378
|
189 if (size == 1)
|
|
190 {
|
|
191 singlechar = fgetc (infd);
|
|
192 *buf = singlechar;
|
|
193 }
|
|
194 else
|
|
195 {
|
|
196 if (fgets (buf, size, infd) == NULL)
|
|
197 return (NULL);
|
|
198
|
|
199 if (size > 1)
|
|
200 buf[size - 1] = '\0';
|
|
201 }
|
341
|
202
|
|
203 if (!echo)
|
|
204 {
|
|
205 printf ("\n");
|
|
206 tcsetattr (fileno (infd), TCSAFLUSH, &oldterm);
|
|
207 fclose (infd);
|
|
208 sigprocmask (SIG_SETMASK, &sigsave, NULL);
|
|
209 }
|
|
210
|
378
|
211 if (size > 1)
|
|
212 {
|
|
213 for (pos = buf + strlen (buf) - 1; *pos == ' ' || *pos == '\r' ||
|
|
214 *pos == '\n'; pos--);
|
|
215 *(pos+1) = '\0';
|
|
216
|
|
217 for (pos = buf; *pos == ' '; pos++);
|
341
|
218
|
378
|
219 if (*pos == '\0')
|
|
220 return (NULL);
|
341
|
221
|
378
|
222 return (pos);
|
|
223 }
|
|
224 else
|
|
225 return (buf);
|
341
|
226 }
|
|
227
|
|
228
|
|
229 int
|
|
230 main (int argc, char **argv)
|
|
231 {
|
518
|
232 gftp_request * gftp_text_locreq, * gftp_text_remreq;
|
380
|
233 void *locuidata, *remuidata;
|
422
|
234 char *pos;
|
341
|
235 #if HAVE_LIBREADLINE
|
|
236 char *tempstr, prompt[20];
|
|
237 #else
|
|
238 char tempstr[512];
|
|
239 #endif
|
|
240
|
374
|
241 gftpui_common_init (&argc, &argv, gftp_text_log);
|
341
|
242
|
|
243 /* SSH doesn't support reading the password with askpass via the command
|
|
244 line */
|
|
245
|
|
246 gftp_text_remreq = gftp_request_new ();
|
380
|
247 remuidata = gftp_text_remreq;
|
341
|
248 gftp_text_remreq->logging_function = gftp_text_log;
|
|
249
|
|
250 gftp_text_locreq = gftp_request_new ();
|
380
|
251 locuidata = gftp_text_locreq;
|
458
|
252 gftp_text_locreq->logging_function = gftp_text_log;
|
341
|
253
|
|
254 if (gftp_protocols[GFTP_LOCAL_NUM].init (gftp_text_locreq) == 0)
|
|
255 {
|
792
|
256 gftp_setup_startup_directory (gftp_text_locreq,
|
|
257 "local_startup_directory");
|
341
|
258 gftp_connect (gftp_text_locreq);
|
|
259 }
|
|
260
|
|
261 gftpui_common_about (gftp_text_log, NULL);
|
|
262 gftp_text_log (gftp_logging_misc, NULL, "\n");
|
|
263
|
|
264 if (argc == 3 && strcmp (argv[1], "-d") == 0)
|
|
265 {
|
|
266 if ((pos = strrchr (argv[2], '/')) != NULL)
|
|
267 *pos = '\0';
|
380
|
268
|
|
269 gftpui_common_cmd_open (remuidata, gftp_text_remreq,
|
|
270 locuidata, gftp_text_locreq,
|
|
271 argv[2]);
|
341
|
272
|
|
273 if (pos != NULL)
|
|
274 *pos = '/';
|
|
275
|
380
|
276 gftpui_common_cmd_mget_file (remuidata, gftp_text_remreq,
|
|
277 locuidata, gftp_text_locreq,
|
|
278 pos + 1);
|
341
|
279 exit (0);
|
|
280 }
|
|
281 else if (argc == 2)
|
380
|
282 {
|
|
283 gftpui_common_cmd_open (remuidata, gftp_text_remreq,
|
|
284 locuidata, gftp_text_locreq,
|
|
285 argv[1]);
|
792
|
286
|
|
287 gftp_setup_startup_directory (gftp_text_remreq,
|
|
288 "remote_startup_directory");
|
380
|
289 }
|
341
|
290
|
|
291 #if HAVE_LIBREADLINE
|
|
292 g_snprintf (prompt, sizeof (prompt), "%sftp%s> ", GFTPUI_COMMON_COLOR_BLUE, GFTPUI_COMMON_COLOR_DEFAULT);
|
|
293 while ((tempstr = readline (prompt)))
|
|
294 {
|
380
|
295 if (gftpui_common_process_command (locuidata, gftp_text_locreq,
|
|
296 remuidata, gftp_text_remreq,
|
|
297 tempstr) == 0)
|
341
|
298 break;
|
|
299
|
|
300 add_history (tempstr);
|
|
301 free (tempstr);
|
|
302 }
|
|
303 #else
|
|
304 printf ("%sftp%s> ", GFTPUI_COMMON_COLOR_BLUE, GFTPUI_COMMON_COLOR_DEFAULT);
|
|
305 while (fgets (tempstr, sizeof (tempstr), stdin) != NULL)
|
|
306 {
|
399
|
307 if (gftpui_common_process_command (locuidata, gftp_text_locreq,
|
|
308 remuidata, gftp_text_remreq,
|
|
309 tempstr) == 0)
|
341
|
310 break;
|
|
311
|
|
312 printf ("%sftp%s> ", GFTPUI_COMMON_COLOR_BLUE, GFTPUI_COMMON_COLOR_DEFAULT);
|
|
313 }
|
|
314 #endif
|
|
315
|
574
|
316 gftp_shutdown ();
|
341
|
317 return (0);
|
|
318 }
|
|
319
|