File: set.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 (185 lines) | stat: -rw-r--r-- 3,927 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
/* $Id: set.c,v 1.8 2003/07/12 10:25:41 mhe Exp $
 *
 * set.c -- sets and shows variables from within 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 "strq.h"
#include "set.h"

static void set_autologin(void *val)
{
	if(val)
		gvAutologin = *(bool *)val;

	if(gvAutologin)
		puts(_("autologin is on"));
	else
		puts(_("autologin is off"));
}

static void set_verbose(void *val)
{
	bool b;

	if(val) {
		b = *(bool *)val;
		ftp_set_verbosity(b ? vbCommand : vbError);
	}

	if(ftp->verbosity == vbCommand) {
		puts(_("verbose is on"));
		gvVerbose = true;
	} else {
		puts(_("verbose is off"));
		gvVerbose = false;
	}
}

static void set_pasvmode(void *val)
{
	bool b;

	if(val) {
		gvPasvmode = *(bool *)val;
		if(ftp && ftp->url)
			ftp->url->pasvmode = gvPasvmode;
	}

	b = (ftp && ftp->url) ? ftp->url->pasvmode : gvPasvmode;
	b = (b == -1) ? gvPasvmode : b;
	if(b)
		puts(_("passive mode is on"));
	else
		puts(_("passive mode is off"));
}

static void set_debug(void *val)
{
	bool b;

	if(val) {
		b = *(bool *)val;
		ftp_set_verbosity(b ? vbDebug : vbError);
	}

	if(ftp->verbosity == vbDebug) {
		puts(_("debug is on"));
		gvDebug = true;
	} else {
		puts(_("debug is off"));
		gvDebug = false;
	}
}

static void set_type(void *val)
{
	if(val) {
		if(strcasecmp((char *)val, "binary") == 0
		   || strcasecmp((char *)val, "I") == 0)
			gvDefaultType = tmBinary;
		else if(strcasecmp((char *)val, "ascii") == 0
				|| strcasecmp((char *)val, "A") == 0)
			gvDefaultType = tmAscii;
		else {
			printf(_("Unknown type '%s'? Use 'ascii' or 'binary'\n"),
				   (char *)val);
			return;
		}
	}
	printf(_("default type is '%s'\n"),
		   gvDefaultType==tmBinary ? "binary" : "ascii");
}

/* changes password used with anonymous connections */
static void set_anonpass(void *val)
{
	if(val) {
		free(gvAnonPasswd);
		gvAnonPasswd = xstrdup((char *)val);
	}
	printf(_("anonymous password is '%s'\n"), gvAnonPasswd);
}

struct _setvar setvariables[] = {
	{"autologin", ARG_BOOL, set_autologin},
	{"debug", ARG_BOOL, set_debug},
	{"verbose", ARG_BOOL, set_verbose},
	{"passive_mode", ARG_BOOL, set_pasvmode},
	{"type", ARG_STR, set_type},
	{"anonpass", ARG_STR, set_anonpass},
	{NULL, 0, NULL}
};

static struct _setvar *get_variable(char *varname)
{
	int i;
	struct _setvar *r = 0;

	for(i=0;setvariables[i].name;i++) {
		/* compare only strlen(varname) chars, allowing variables
		 * to be shortened, as long as they're not ambiguous
		 */
		if(strncmp(setvariables[i].name, varname, strlen(varname)) == 0) {
			/* is this an exact match? */
			if(strlen(varname) == strlen(setvariables[i].name))
				return &setvariables[i];

			if(r)
				r = (struct _setvar *)-1;
			else
				r = &setvariables[i];
		}
	}
	return r;
}

void cmd_set(int argc, char **argv)
{
	struct _setvar *sp;
	int n;
	void *vp = 0;

	if(argc < 2) {
		for(n = 0; setvariables[n].name; n++)
			setvariables[n].setfunc(0);
	} else {
		sp = get_variable(argv[1]);
		if(!sp) {
			fprintf(stderr, _("No such variable '%s'\n"), argv[1]);
			return;
		} else if(sp == (struct _setvar *)-1) {
			fprintf(stderr, _("Ambiguous variable '%s'\n"), argv[1]);
			return;
		}

		if(argc == 2)
			sp->setfunc(0);
		else {
			if(sp->argtype == ARG_BOOL) {
				if((n = str2bool(argv[2])) == -1) {
					printf(_("Expected boolean value, but got '%s'\n"),
						   argv[2]);
					return;
				}
				vp = &n;
			} else if(sp->argtype == ARG_INT) {
				n = atoi(argv[2]);
				vp = &n;
			} else if(sp->argtype == ARG_STR) {
				vp = argv[2];
			}
			sp->setfunc(vp);
		}
	}
}