summaryrefslogtreecommitdiffstats
path: root/usr.bin/ncftp/set.c
blob: 833c7af2ce3bc9eab7cd7bc76e7e8d975bbf0d15 (plain)
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
/* Set.c */

/*  $RCSfile: set.c,v $
 *  $Revision: 14020.12 $
 *  $Date: 93/07/09 11:45:48 $
 */

#include "sys.h"

#include <ctype.h>

#include "util.h"
#include "cmds.h"
#include "main.h"
#include "set.h"
#include "defaults.h"
#include "copyright.h"

#ifdef TERM_FTP
extern int compress_toggle;
#endif

/* Set.c globals: */
char *verbose_msgs[4] = {
	"Not printing anything.\n",
	"Only printing necessary error messages.\n",
	"Printing error messages and announcements from the remote host.\n",
	"Printing all messages, errors, acknowledgments, and announcements.\n"
};

char *short_verbose_msgs[4] = {
	"Quiet (-1)",
	"Errors Only (0)",
	"Terse (1)",
	"Verbose (2)"
};

string						vstr;

/* Set.c externs: */
extern int					progress_meter, connected;
extern int					parsing_rc, keep_recent;
extern string				pager, anon_password, prompt;
extern str32				curtypename;
extern long					logsize;
extern FILE					*logf;
extern longstring			rcname, logfname, lcwd;
extern int					auto_binary, ansi_escapes, debug;
extern int					mprompt, remote_is_unix, verbose;
extern int					startup_msg, anon_open, passivemode;
extern int					restricted_data_ports;
#ifndef NO_TIPS
extern int					tips;
#endif
#ifdef GATEWAY
extern string				gateway, gate_login;
#endif

/* The variables must be sorted in alphabetical order, or else
 * match_var() will choke.
 */
struct var vars[] = {
	VARENTRY("anon-open",		BOOL, 0, &anon_open,	NULL),
	VARENTRY("anon-password",	STR,  0, anon_password,	NULL),
	VARENTRY("ansi-escapes",	BOOL, 0, &ansi_escapes,	NULL),
	VARENTRY("auto-binary",		BOOL, 0, &auto_binary,	NULL),
#ifdef TERM_FTP
	VARENTRY("compress",		INT,  0,
		 &compress_toggle,NULL),
#endif
	VARENTRY("debug",			INT,  0, &debug,		NULL),
#ifdef GATEWAY
	VARENTRY("gateway-login",	STR,  0, gate_login,	set_gatelogin),
	VARENTRY("gateway-host",	STR,  0, gateway,		NULL),
#endif
	VARENTRY("local-dir",		STR,  0, lcwd,			set_ldir),
	VARENTRY("logfile",			STR,  0, logfname,		set_log),
	VARENTRY("logsize",			LONG, 0, &logsize,		NULL),
	VARENTRY("mprompt",			BOOL, 0, &mprompt,		NULL),
	VARENTRY("netrc",			-STR, 0, rcname,		NULL),
	VARENTRY("passive",			BOOL, 0, &passivemode,	NULL),
	VARENTRY("pager",			STR,  0, pager + 1,		set_pager),
	VARENTRY("prompt",			STR,  0, prompt,		set_prompt),
	VARENTRY("progress-reports",INT,  0, &progress_meter,NULL),
	VARENTRY("recent-list",		BOOL, 0, &keep_recent,	NULL),
	VARENTRY("remote-is-unix",	BOOL, 1, &remote_is_unix,NULL),
	VARENTRY("restricted-data-ports",BOOL, 0, &restricted_data_ports, NULL),
	VARENTRY("startup-msg",		BOOL, 0, &startup_msg,	NULL),  /* TAR */
#ifndef NO_TIPS
	VARENTRY("tips",			BOOL, 0, &tips,			NULL),
#endif
	VARENTRY("type",			STR,  1, curtypename,	set_type),
	VARENTRY("verbose",			STR,  0, vstr,			set_verbose),
};


void set_verbose(char *new, int unset)
{
	int i, c;

	if (unset == -1) verbose = !verbose;
	else if (unset || !new) verbose = V_ERRS;
	else {
		if (isalpha(*new)) {
			c = islower(*new) ? toupper(*new) : *new;	
			for (i=0; i<(int)(sizeof(short_verbose_msgs)/sizeof(char *)); i++) {
				if (short_verbose_msgs[i][0] == c)
					verbose = i - 1;
			}
		} else {
			i = atoi(new);
			if (i < V_QUIET) i = V_QUIET;
			else if (i > V_VERBOSE) i = V_VERBOSE;
			verbose = i;
		}
	}
	(void) Strncpy(vstr, short_verbose_msgs[verbose+1]);
	if (!parsing_rc && NOT_VQUIET) 
		(void) fputs(verbose_msgs[verbose+1], stdout);
}	/* set_verbose */




void set_prompt(char *new, int unset)
{
	(void) Strncpy(prompt, (unset || !new) ? dPROMPT : new);
	init_prompt();
}	/* set_prompt */




void set_log(char *fname, int unset)
{
	if (logf) {
		(void) fclose(logf);
		logf = NULL;
	}
	if (!unset && fname) {
		(void) Strncpy(logfname, fname);
		logf = fopen (LocalDotPath(logfname), "a");
	}
}	/* set_log */




void set_pager(char *new, int unset)
{
	if (unset)
		(void) strcpy(pager, "-");
	else {
		if (!new)
			new = dPAGER;
		if (!new[0])
			(void) Strncpy(pager, "-");
		else {
			(void) sprintf(pager, "|%s", (*new == '|' ? new + 1 : new));
			(void) LocalPath(pager + 1);
		}
	}
}	/* set_pager */




void set_type(char *newtype, int unset)
{
	int t = verbose;
	verbose = V_QUIET;
	if (!connected && t > V_QUIET)
		(void) printf("Not connected.\n");
	else if (newtype != NULL && !unset)
		(void) _settype(newtype);
	verbose = t;
}	/* set_type */




void set_ldir(char *ldir, int unset)
{
	int t = verbose;
	char *argv[2];

	if (ldir && !unset) {
		verbose = V_QUIET;
		argv[1] = ldir;
		(void) lcd(2, argv);
		verbose = t;
	}
}	/* set_ldir */




#ifdef GATEWAY
void set_gatelogin(char *glogin, int unset)
{
	if (unset || !glogin) {
		gate_login[0] = gateway[0] = 0;
	} else
		(void) strcpy(gate_login, glogin);
}	/* set_gatelogin */
#endif




struct var *match_var(char *varname)
{
	int i, ambig;
	struct var *v;
	short c;

	c = (short) strlen(varname);
	for (i=0, v=vars; i<NVARS; i++, v++) {
		if (strcmp(v->name, varname) == 0)
			return v;	/* exact match. */
		if (c < v->nmlen) {
			if (strncmp(v->name, varname, (size_t) c) == 0) {
				/* Now make sure that it only matches one var name. */
				if (c >= v[1].nmlen || (i == (NVARS - 1)))
					ambig = 0;
				else
					ambig = !strncmp(v[1].name, varname, (size_t) c);
				if (!ambig)
					return v;
				(void) fprintf(stderr, "%s: ambiguous variable name.\n", varname);
				goto xx;
			}
		}
	}
	(void) fprintf(stderr, "%s: unknown variable.\n", varname);
xx:
	return ((struct var *)0);
}	/* match_var */




void show_var(struct var *v)
{
	int c;

	if (v != (struct var *)0) {
		(void) printf("%-20s= ", v->name);
		c = v->type;
		if (c < 0) c = -c;
		if (v->conn_required && !connected)
			(void) printf("(not connected)\n");
		else switch (c) {
			case INT:
				(void) printf("%d\n", *(int *)v->var); break;
			case LONG:
				(void) printf("%ld\n", *(long *)v->var); break;
			case STR:
				(void) printf("\"%s\"\n", (char *)v->var); break;
			case BOOL:
				(void) printf("%s\n", *(int *)v->var == 0 ? "no" : "yes");
		}
	}
}	/* show_var */




void show(char *varname)
{
	int i;
	struct var *v;

	if ((varname == NULL)	/* (Denotes show all vars) */
		|| (strcmp("all", varname) == 0))
	{
		for (i=0; i<NVARS; i++)
		    show_var(&vars[i]);
	} else {
		if ((v = match_var(varname)) != (struct var *)0)
			show_var(v);
	}
}	/* show */




int do_show(int argc, char **argv)
{
	int i;

	if (argc < 2)
		show(NULL);
	else
		for (i=1; i<argc; i++)
			show(argv[i]);
	return NOERR;
}	/* do_show */




int set(int argc, char **argv)
{
	int unset;
	struct var *v;
	char *var, *val = NULL;

	if (argc < 2 || strncmp(argv[1], "all", (size_t)3) == 0) {
		show(NULL);		/* show all variables. */
	} else {
		unset = argv[0][0] == 'u';
		var = argv[1];
		if (argc > 2) {
			/* could be '= value' or just 'value.' */
			if (*argv[2] == '=') {
				if (argc > 3)
					val = argv[3];
				else return USAGE;	/* can't do 'set var =' */
			} else
				val = argv[2];
			if (val[0] == 0)
				val = NULL;
		}
		v = match_var(var);
		if (v != NULL) {
			if (v->conn_required && !connected)
				(void) fprintf(stderr, "%s: must be connected.\n", var);
			else if (v->type < 0)	
				(void) fprintf(stderr, "%s: read-only variable.\n", var);
			else if (v->proc != (setvarproc) 0) {
				(*v->proc)(val, unset);		/* a custom set proc. */
			} else if (unset) switch(v->type) {
				case BOOL:
				case INT:
					*(int *) v->var = 0; break;
				case LONG:
					*(long *) v->var = 0; break;
				case STR:
					*(char *) v->var = 0; break;
			} else {
				if (val == NULL) switch(v->type) {
					/* User just said "set varname" */
					case BOOL:
					case INT:
						*(int *) v->var = 1; break;
					case LONG:
						*(long *) v->var = 1; break;
					case STR:
						*(char *) v->var = 0; break;
				} else {
					/* User said "set varname = value" */
					switch (v->type) {
						case BOOL:
							*(int *)v->var = StrToBool(val); break;
						case INT:
							(void) sscanf(val, "%d", (int *) v->var); break;
						case LONG:
							(void) sscanf(val, "%ld", (long *) v->var); break;
						case STR:
							(void) strcpy(v->var, val); break;
					}
				}
			}
		}
	}
	return NOERR;
}	/* set */

/* eof Set.c */
OpenPOWER on IntegriCloud