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