File: main.c

package info (click to toggle)
yafc 1.1.1.dfsg.1-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 3,244 kB
  • ctags: 1,679
  • sloc: ansic: 19,338; sh: 10,365; makefile: 155; perl: 38; ruby: 33
file content (463 lines) | stat: -rw-r--r-- 11,192 bytes parent folder | download | duplicates (4)
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/* $Id: main.c,v 1.23 2003/07/12 10:22:45 mhe Exp $
 *
 * main.c -- parses command line options and starts Yafc
 *
 * Yet Another FTP Client
 * Copyright (C) 1998-2001, Martin Hedenfalk <mhe@stacken.kth.se>
 *
 * 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. See COPYING for more details.
 */

#include "syshdr.h"
#include "ftp.h"
#include "gvars.h"
#include "help.h"
#include "alias.h"
#include "input.h"
#include "cmd.h"
#if 0
# include "setproctitle.h"
#endif
#include "completion.h"
#include "login.h"
#include "strq.h"
#include "utils.h"
#include "rc.h"
#include "ltag.h"

#ifdef HAVE_LOCALE_H
# include <locale.h>
#endif

#include "yafcrc.h"

void print_syntax_and_exit(char *argv0)
{
	printf(COPYLINE "\n");
	printf(_("This is yet another FTP client.  Usage:\n"
			 " %s [options] [[proto://][user[:password]@]hostname[:port][/directory] ...]\n"
			 " proto can be either 'ftp' or 'ssh'\n"
			 "Options:\n"
			 "  -a, --anon        (*) anonymous login\n"
			 "  -d, --debug           print all commands sent to/from server\n"
			 "  -D, --dump-rc         prints the default config file to stdout\n"
			 "  -m, --mechanism=MECH\n"
			 "                    (*) try MECH as security mechanism(s)\n"
			 "  -n, --norc            don't parse config file\n"
			 "  -p, --noproxy     (*) don't connect via proxy\n"
			 "  -q, --quiet           don't print the yafc welcome message\n"
			 "  -r, --rcfile=FILE     use other config file instead of ~/.yafc/yafcrc\n"
			 "  -t, --trace[=FILE]    use a trace file (mainly for debugging)\n"
			 "               if FILE specified, use it instead of ~/.yafc/trace/trace.<pid>\n"
			 "  -u, --noauto      (*) disable autologin\n"
			 "  -U, --noalias     (*) disable bookmark alias lookup and abbreviation\n"
			 "  -v, --verbose         print all replies from server\n"
			 "  -w, --wait=TIME       use a different wait time for reconnecting\n"
			 "  -W, --workdir=DIR     use a different working directory (instead of ~/.yafc)\n"
			 "  -V, --version         print version information and quit\n"
			 "  -h, --help            print this help and quit\n"
			 "\n"
			 "(*) only applies for login to host specified on the command line\n"
			 "\n"
			 "Report bugs to <yafc-devel@lists.sourceforge.net>\n\n"), argv0);
	exit(0);
}

RETSIGTYPE tstp_sighandler(int signum)
{
	if(signum == SIGTSTP) {
		reset_xterm_title();
		raise(SIGSTOP);
	} else if(signum == SIGCONT) {
		if(readline_running)
			input_redisplay_prompt();
		print_xterm_title();
	}
	ftp_set_signal(signum, tstp_sighandler);
}

void init_ftp(void)
{
	ftp->getuser_hook = getuser_hook;
	ftp->getpass_hook = getpass_hook;

	ftp->open_timeout = gvConnectionTimeout;
	ftp->reply_timeout = gvCommandTimeout;

	ftp_set_verbosity(vbError); /* default */
	if(gvVerbose)
		ftp_set_verbosity(vbCommand);
	if(gvDebug)
		ftp_set_verbosity(vbDebug);
}

void init_yafc(void)
{
#ifdef HAVE_UNAME
	struct utsname unbuf;
#endif

	gvFtpList = list_new((listfunc)ftp_destroy);
	list_additem(gvFtpList, ftp_create());
	gvCurrentFtp = gvFtpList->first;
	ftp_use((Ftp *)gvCurrentFtp->data);
	ftp_initsigs();

	gvEditor = getenv("EDITOR");
	if(!gvEditor)
		gvEditor = getenv("VISUAL");
	if(gvEditor)
		gvEditor = xstrdup(gvEditor);
	else
		gvEditor = xstrdup("vi");

#ifdef HAVE_UNAME
	if(uname(&unbuf) == 0)
		gvLocalHost = xstrdup(unbuf.nodename);
#endif
	if(!gvLocalHost)
		gvLocalHost = xstrdup(getenv("HOST"));
	if(!gvLocalHost)
		gvLocalHost = xstrdup("localhost");

	{
		struct passwd *pwd;
		pwd = getpwuid(geteuid());
		gvUsername = xstrdup(pwd->pw_name);
		gvLocalHomeDir = xstrdup(pwd->pw_dir);
	}

	if(!gvLocalHomeDir)
		gvLocalHomeDir = xstrdup(getenv("HOME"));

	gvWorkingDirectory = path_absolute(
		gvWorkingDirectory ? gvWorkingDirectory : "~/.yafc",
		get_local_curdir(),
		gvLocalHomeDir);

	gvSSHProgram = xstrdup("/usr/bin/ssh");
	gvSSHOptions = xstrdup("");

	gvAnonPasswd = xstrdup("anonymous@");

	/* init colors from LS_COLORS for ls */
	init_colors();

	/* choose default security mechanism */
	gvDefaultMechanism = list_new((listfunc)free);
#ifdef HAVE_KRB4
# ifdef HAVE_KRB5
	listify_string("krb5:krb4:none", gvDefaultMechanism);
# elif defined(USE_SSL)
	listify_string("krb5:krb4:ssl:none", gvDefaultMechanism);
# else
	listify_string("krb4:none", gvDefaultMechanism);
# endif
#elif defined(HAVE_KRB5)
# ifdef USE_SSL
	listify_string("krb5:ssl:none", gvDefaultMechanism);
# else
	listify_string("krb5:none", gvDefaultMechanism);
# endif
#elif defined(USE_SSL)
	listify_string("ssl", gvDefaultMechanism);
#else
	listify_string("none", gvDefaultMechanism);
#endif

	gvPrompt1 = xstrdup("yafc> ");
	gvPrompt2 = xstrdup("yafc %h> ");
	gvPrompt3 = xstrdup("yafc %h:%42~> ");

	gvTerm = xstrdup(getenv("TERM"));
	if(!gvTerm)
		gvTerm = xstrdup("dummy");
	gvXtermTitleTerms = xstrdup("xterm xterm-debian rxvt");

	gvBookmarks = list_new((listfunc)url_destroy);
	gvAliases = list_new((listfunc)alias_destroy);
	gvAsciiMasks = list_new((listfunc)free);
	gvTransferFirstMasks = list_new((listfunc)free);
	gvLocalTagList = list_new((listfunc)free);
	gvProxyExclude = list_new((listfunc)free);

	asprintf(&gvHistoryFile, "%s/history", gvWorkingDirectory);

	gvSendmailPath = xstrdup("/usr/sbin/sendmail");

	ftp_set_signal(SIGTSTP, tstp_sighandler);
	ftp_set_signal(SIGCONT, tstp_sighandler);

	gvTransferBeginString = xstrdup("%-70R\n");
	gvTransferString = xstrdup("%5p%% [%25v] %s/%S ETA %e %B");
/*	gvTransferEndString = xstrdup("%-40R      %s in %t @ %b\n");*/

	gvTransferXtermString = xstrdup("\x1B]0;yafc - (%p%%) %r\x07");

	input_init();
}

void check_if_first_time(void)
{
	if(access(gvWorkingDirectory, X_OK) == 0)
		return;

	if(errno == ENOENT) {
		char *dir;

/*		printf(_("This seems to be the first time you run Yafc...\n"));*/

		printf(_("creating working directory %s: "), gvWorkingDirectory);
		fflush(stdout);
		if(mkdir(gvWorkingDirectory, S_IRUSR|S_IWUSR|S_IXUSR) != 0) {
			perror("");
			return;
		}
		chmod(gvWorkingDirectory, S_IRUSR|S_IWUSR|S_IXUSR);
		printf(_("done\n"));

		asprintf(&dir, "%s/trace", gvWorkingDirectory);
		printf(_("creating directory %s: "), dir);
		fflush(stdout);
		if(mkdir(dir, S_IRUSR|S_IWUSR|S_IXUSR) != 0) {
			perror("");
			return;
		}
		chmod(dir, S_IRUSR|S_IWUSR|S_IXUSR);
		printf(_("done\n"));
		free(dir);

		asprintf(&dir, "%s/nohup", gvWorkingDirectory);
		printf(_("creating directory %s: "), dir);
		fflush(stdout);
		if(mkdir(dir, S_IRUSR|S_IWUSR|S_IXUSR) != 0) {
			perror("");
			return;
		}
		chmod(dir, S_IRUSR|S_IWUSR|S_IXUSR);
		printf(_("done\n"));
		free(dir);

#if 0
		else {
			printf(_("creating default config file /yafcrc: "),
				   gvWorkingDirectory);
			fflush(stdout);
			{
				FILE *fp;
				fp = fopen(, "w");
				if(!fp) {
					perror("");
					free(rcdir);
					return;
				}
				fprintf(fp, "%s", default_yafcrc);
				fclose(fp);
				printf(_("done\n"));
			}
		}
#endif
	} else
		perror(gvWorkingDirectory);
}

int main(int argc, char **argv, char **envp)
{
	int c;
	char *configfile = 0;
	bool dotrace = false;
	char *tracefile = 0;
	unsigned int open_opt = 0;
	int wait_time = -1;

	bool override_yafcrc = false;
	bool override_debug = false;
	bool override_verbose = false;
	bool override_welcome = false;

	char *mech = 0;

	struct option longopts[] = {
		{"anon", no_argument, 0, 'a'},
		{"debug", no_argument, 0, 'd'},
		{"dump-rc", no_argument, 0, 'D'},
		{"norc", no_argument, 0, 'n'},
		{"quiet", no_argument, 0, 'q'},
		{"rcfile", required_argument, 0, 'r'},
		{"mechanism", required_argument, 0, 'm'},
		{"noproxy", no_argument, 0, 'p'},
		{"trace", optional_argument, 0, 't'},
		{"noauto", no_argument, 0, 'u'},
		{"verbose", no_argument, 0, 'v'},
		{"version", no_argument, 0, 'V'},
		{"wait", required_argument, 0, 'w'},
		{"workdir", required_argument, 0, 'W'},
		{"help", no_argument, 0, 'h'},
		{0, 0, 0, 0},
	};

#ifdef SOCKS
	SOCKSinit(argv[0]);
#endif

#ifdef ENABLE_NLS
# ifdef HAVE_SETLOCALE
	setlocale(LC_ALL, "");
# endif
	bindtextdomain(PACKAGE, LOCALEDIR);
	textdomain(PACKAGE);
#endif
#if 0 && (!defined(HAVE_SETPROCTITLE) && defined(linux))
	initsetproctitle(argc, argv, envp);
#endif

	while((c = getopt_long(argc, argv,
						   "qhdDgant::r:uUm:pvVw:W:", longopts, 0)) != EOF)
	{
		switch(c) {
		case 'a':
			open_opt |= OP_ANON;
			break;
		case 'n':
			override_yafcrc = true;
			break;
		case 'r':
			configfile = xstrdup(optarg);
			break;
		case 't':
			dotrace = true;
			if(optarg)
				tracefile = xstrdup(optarg);
			break;
		case 'u':
			open_opt |= OP_NOAUTO;
			break;
		case 'U':
			open_opt |= OP_NOALIAS;
			break;
		case 'm':
			mech = xstrdup(optarg);
			break;
		case 'p':
			open_opt |= OP_NOPROXY;
			break;
		case 'd':
			override_debug = true;
			break;
		case 'D':
			printf("%s", default_yafcrc);
			return 0;
		case 'q':
			override_welcome = true;
			break;
		case 'v':
			override_verbose = true;
			break;
		case 'V':
			printf(PACKAGE " " VERSION " (" HOSTTYPE ")\n");
			return 0;
		case 'w':
			wait_time = atoi(optarg);
			break;
		case 'W':
			stripslash(optarg);
			gvWorkingDirectory = xstrdup(optarg);
			break;
		case 'h':
			print_syntax_and_exit(argv[0]);
			break;
		case '?':
			return 1;
		}
	}

	if(!override_welcome)
		puts(_(FULLVER));

	init_yafc();

	if(!configfile)
		asprintf(&configfile, "%s/yafcrc", gvWorkingDirectory);

	check_if_first_time();

	if(!override_yafcrc) {
		char *tmp;
		parse_rc(SYSCONFDIR "/yafcrc", false);
		parse_rc(configfile, false);
		asprintf(&tmp, "%s/bookmarks", gvWorkingDirectory);
		parse_rc(tmp, false);
		free(tmp);
	}

	if(gvReadNetrc)
		parse_rc("~/.netrc", false);

	if(gvProxyType != 0 && gvProxyUrl == 0) {
		fprintf(stderr, _("No proxy host defined!\n\n"));
		gvProxyType = 0;
	}

	if(override_debug)
		gvDebug = true;
	if(override_verbose)
		gvVerbose = true;
	if(wait_time != -1) {
		if(wait_time < 0)
			fprintf(stderr, _("Invalid value for --wait: %d\n"), wait_time);
		else
			gvConnectWaitTime = wait_time;
	}

	init_ftp();

	if(dotrace || gvTrace) {
		if(!tracefile)
			asprintf(&tracefile, "%s/trace/trace.%u",
					 gvWorkingDirectory, getpid());
		if(ftp_set_trace(tracefile) != 0)
			fprintf(stderr, _("Couldn't open tracefile '%s': %s\n"),
					tracefile, strerror(errno));
		free(tracefile);
	}

	if(optind < argc) {
#ifdef HAVE_LIBREADLINE
		char *s;
#endif
		int i;

		if(!gvAutologin)
			open_opt |= OP_NOAUTO;

		for(i=optind; i<argc; i++) {
#ifdef HAVE_GETTIMEOFDAY
			struct timeval beg, end;
			gettimeofday(&beg, 0);
#endif
			yafc_open(argv[i], open_opt, mech, 0);
#ifdef HAVE_GETTIMEOFDAY
			gettimeofday(&end, 0);
			end.tv_sec -= beg.tv_sec;
			if(gvBeepLongCommand && end.tv_sec > gvLongCommandTime)
				fputc('\007', stderr);
#endif
#ifdef HAVE_LIBREADLINE /* add appropriate 'open' command to history */
			asprintf(&s, "open %s%s",
					 argv[i], test(open_opt, OP_ANON) ? " --anon" : "");
			add_history(s);
			free(s);
#endif
		}
	}

	load_ltaglist(false, false, 0);

	command_loop();
	/* should not return */
	list_free(gvFtpList);
	return 0;
}