File: gftp-text.c

package info (click to toggle)
gftp 2.0.19-4
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 11,464 kB
  • sloc: ansic: 37,479; sh: 4,548; makefile: 579; yacc: 291; perl: 71; sed: 16
file content (319 lines) | stat: -rw-r--r-- 8,760 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*****************************************************************************/
/*  gftp-text.c - text port of gftp                                          */
/*  Copyright (C) 1998-2007 Brian Masney <masneyb@gftp.org>                  */
/*                                                                           */
/*  This program is free software; you can redistribute it and/or modify     */
/*  it under the terms of the GNU General Public License as published by     */
/*  the Free Software Foundation; either version 2 of the License, or        */
/*  (at your option) any later version.                                      */
/*                                                                           */
/*  This program is distributed in the hope that it will be useful,          */
/*  but WITHOUT ANY WARRANTY; without even the implied warranty of           */
/*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            */
/*  GNU General Public License for more details.                             */
/*                                                                           */
/*  You should have received a copy of the GNU General Public License        */
/*  along with this program; if not, write to the Free Software              */
/*  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA      */
/*****************************************************************************/

#include "gftp-text.h"
static const char cvsid[] = "$Id: gftp-text.c 893 2007-03-13 01:52:50Z masneyb $";

unsigned int
gftp_text_get_win_size (void)
{
  struct winsize size;
  unsigned int ret;

  if (ioctl (0, TIOCGWINSZ, (char *) &size) < 0)
    ret = 80;
  else
    ret = size.ws_col;

  return (ret);
}


static void
gftp_text_write_string (gftp_request * request, char *string)
{
  gchar *stpos, *endpos, *locale_str, savechar;
  unsigned int sw;
  size_t destlen;

  sw = gftp_text_get_win_size ();

  locale_str = gftp_string_from_utf8 (request, 1, string, &destlen);
  if (locale_str == NULL)
    stpos = string;
  else
    stpos = locale_str;

  do
    {
      if ((endpos = strchr (stpos, '\n')) == NULL)
        endpos = stpos + strlen (stpos);

      savechar = *endpos;
      *endpos = '\0';

      if (strlen (stpos) <= sw)
        {
          printf ("%s%c", stpos, savechar);
          *endpos = savechar;
          if (savechar == '\0')
            break;
          stpos = endpos + 1;
        }
      else
        {
          *endpos = savechar;
          for (endpos = stpos + sw - 1;
               *endpos != ' ' && endpos > stpos;
               endpos--);

          if (endpos != stpos)
            *endpos = '\0';

          printf ("%s\n", stpos);
          stpos = endpos + 1;
        }

      sw = sw;
    }
  while (stpos != endpos);

  if (locale_str != NULL)
    g_free (locale_str);
}


static void
gftp_text_log (gftp_logging_level level, gftp_request * request, 
               const char *string, ...)
{
  char tempstr[512];
  va_list argp;

  g_return_if_fail (string != NULL);

  switch (level)
    {
      case gftp_logging_send:
        printf ("%s", GFTPUI_COMMON_COLOR_GREEN);
        break;
      case gftp_logging_recv:
        printf ("%s", GFTPUI_COMMON_COLOR_YELLOW);
        break;
      case gftp_logging_error:
        printf ("%s", GFTPUI_COMMON_COLOR_RED);
        break;
      default:
        printf ("%s", GFTPUI_COMMON_COLOR_DEFAULT);
        break;
    }

  va_start (argp, string);
  g_vsnprintf (tempstr, sizeof (tempstr), string, argp);
  va_end (argp);

  if (gftp_logfd != NULL && level != gftp_logging_misc_nolog)
    {
      fwrite (tempstr, 1, strlen (tempstr), gftp_logfd);
      if (ferror (gftp_logfd))
        {
          fclose (gftp_logfd);
          gftp_logfd = NULL;
        }
      else
        fflush (gftp_logfd);
    }

  if (level == gftp_logging_misc_nolog)
    printf ("%s", tempstr);
  else
    gftp_text_write_string (request, tempstr);
  
  printf ("%s", GFTPUI_COMMON_COLOR_DEFAULT);
}


char *
gftp_text_ask_question (gftp_request * request, const char *question, int echo,
                        char *buf, size_t size)
{
  struct termios term, oldterm;
  gchar *locale_question;
  sigset_t sig, sigsave;
  char *pos, *termname;
  size_t destlen;
  int singlechar;
  FILE *infd;

  if (!echo)
    {
      sigemptyset (&sig);
      sigaddset (&sig, SIGINT);
      sigaddset (&sig, SIGTSTP);
      sigprocmask (SIG_BLOCK, &sig, &sigsave);

      termname = ctermid (NULL);
      if ((infd = fopen (termname, "r+")) == NULL)
        {
          
          gftp_text_log (gftp_logging_error, NULL, 
                         _("Cannot open controlling terminal %s\n"), termname);
          return (NULL);
        }

      tcgetattr (0, &term);
      oldterm = term;
      term.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL); 
      tcsetattr (fileno (infd), TCSAFLUSH, &term);
    }
  else
    infd = stdin;

  locale_question = gftp_string_from_utf8 (request, 1, question, &destlen);
  if (locale_question != NULL)
    {
      printf ("%s%s%s ", GFTPUI_COMMON_COLOR_BLUE, locale_question,
              GFTPUI_COMMON_COLOR_DEFAULT);
      g_free (locale_question);
    }
  else
    printf ("%s%s%s ", GFTPUI_COMMON_COLOR_BLUE, question,
            GFTPUI_COMMON_COLOR_DEFAULT);

  if (size == 1)
    {
      singlechar = fgetc (infd);
      *buf = singlechar;
    }
  else
    {
      if (fgets (buf, size, infd) == NULL)
        return (NULL);

      if (size > 1)
        buf[size - 1] = '\0';
    }

  if (!echo)
    {
      printf ("\n");
      tcsetattr (fileno (infd), TCSAFLUSH, &oldterm);
      fclose (infd);
      sigprocmask (SIG_SETMASK, &sigsave, NULL);
    }

  if (size > 1)
    {
      for (pos = buf + strlen (buf) - 1; *pos == ' ' || *pos == '\r' ||
                                         *pos == '\n'; pos--);
      *(pos+1) = '\0';

      for (pos = buf; *pos == ' '; pos++);  

      if (*pos == '\0')
        return (NULL);

      return (pos);
    }
  else
    return (buf);
}


int
main (int argc, char **argv)
{
  gftp_request * gftp_text_locreq, * gftp_text_remreq;
  void *locuidata, *remuidata;
  char *pos;
#if HAVE_LIBREADLINE
  char *tempstr, prompt[20];
#else
  char tempstr[512];
#endif

  gftpui_common_init (&argc, &argv, gftp_text_log);

  /* SSH doesn't support reading the password with askpass via the command 
     line */

  gftp_text_remreq = gftp_request_new ();
  remuidata = gftp_text_remreq;
  gftp_text_remreq->logging_function = gftp_text_log;

  gftp_text_locreq = gftp_request_new ();
  locuidata = gftp_text_locreq;
  gftp_text_locreq->logging_function = gftp_text_log;

  if (gftp_protocols[GFTP_LOCAL_NUM].init (gftp_text_locreq) == 0)
    {
      gftp_setup_startup_directory (gftp_text_locreq,
                                    "local_startup_directory");
      gftp_connect (gftp_text_locreq);
    }

  gftpui_common_about (gftp_text_log, NULL);
  gftp_text_log (gftp_logging_misc, NULL, "\n");

  if (argc == 3 && strcmp (argv[1], "-d") == 0)
    {
      if ((pos = strrchr (argv[2], '/')) != NULL)
        *pos = '\0';

      gftpui_common_cmd_open (remuidata, gftp_text_remreq,
                              locuidata, gftp_text_locreq,
                              argv[2]);

      if (pos != NULL)
        *pos = '/';

      gftpui_common_cmd_mget_file (remuidata, gftp_text_remreq,
                                   locuidata, gftp_text_locreq,
                                   pos + 1);
      exit (0);
    }
  else if (argc == 2)
    {
      gftpui_common_cmd_open (remuidata, gftp_text_remreq,
                              locuidata, gftp_text_locreq,
                              argv[1]);

      gftp_setup_startup_directory (gftp_text_remreq,
                                    "remote_startup_directory");
    }

#if HAVE_LIBREADLINE
  g_snprintf (prompt, sizeof (prompt), "%sftp%s> ", GFTPUI_COMMON_COLOR_BLUE, GFTPUI_COMMON_COLOR_DEFAULT);
  while ((tempstr = readline (prompt)))
    {
      if (gftpui_common_process_command (locuidata, gftp_text_locreq,
                                         remuidata, gftp_text_remreq,
                                         tempstr) == 0)
        break;
   
      add_history (tempstr);
      free (tempstr);
    }
#else
  printf ("%sftp%s> ", GFTPUI_COMMON_COLOR_BLUE, GFTPUI_COMMON_COLOR_DEFAULT);
  while (fgets (tempstr, sizeof (tempstr), stdin) != NULL)
    {
      if (gftpui_common_process_command (locuidata, gftp_text_locreq,
                                         remuidata, gftp_text_remreq,
                                         tempstr) == 0)
        break;

      printf ("%sftp%s> ", GFTPUI_COMMON_COLOR_BLUE, GFTPUI_COMMON_COLOR_DEFAULT);
    }
#endif
 
  gftp_shutdown ();
  return (0);
}