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