File: tag.c

package info (click to toggle)
yafc 1.1.1.dfsg.1-4
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 3,248 kB
  • ctags: 1,679
  • sloc: ansic: 19,338; sh: 10,365; makefile: 155; perl: 38; ruby: 33
file content (263 lines) | stat: -rw-r--r-- 5,759 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
/* $Id: tag.c,v 1.7 2004/05/20 11:10:52 mhe Exp $
 *
 * tag.c -- tag files
 *
 * 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 "rglob.h"
#include "gvars.h"
#include "strq.h"
#include "commands.h"
#include "input.h"
#include "utils.h"

static void print_tag_syntax(void)
{
	printf(_("Tag file(s) for later transferring.  Usage:\n"
			 "  tag [option(s)] [file(s)]\n"
			 "Options:\n"
			 "  -c, --clear             clear the taglist\n"
			 "  -i, --info              show info of tagged files\n"
			 "  -l, --list              list tagged files\n"
			 "  -L, --load[=FILE]       load saved taglist file for this host\n"
			 "  -s, --save[=FILE]       save taglist\n"
			 "  -h, --help              show this help\n"));
}

void save_taglist(const char *alt_filename)
{
	FILE *fp;
	char *f, *e = 0;
	listitem *li;

	if(!ftp_connected())
		return;

	if(list_numitem(ftp->taglist) == 0 || gvLoadTaglist == 0)
		return;

	if(alt_filename == 0)
		asprintf(&e, "%s/taglist.%s",
				 gvWorkingDirectory, ftp->host->ohostname);
	f = tilde_expand_home(alt_filename ? alt_filename : e, gvLocalHomeDir);
	fp = fopen(f, "w");
	if(!fp) {
		perror(_("Unable to save taglist file"));
		free(f);
		free(e);
		return;
	}

	fprintf(fp, "[yafc taglist]\n");
	for(li=ftp->taglist->first; li; li=li->next)
		fprintf(fp, "%s\n", ((rfile *)li->data)->path);
	fclose(fp);
	fprintf(stderr, _("Saved taglist to %s\n"), f);
	free(f);
	free(e);
}

void load_taglist(bool showerr, bool always_autoload,
				  const char *alt_filename)
{
	FILE *fp;
	char *f, *e = 0;
	unsigned n = 0;
	int c;
	char tmp[4096];

	if(gvLoadTaglist == 0 && !always_autoload)
		return;

	need_connected();
	need_loggedin();

	if(alt_filename == 0)
		asprintf(&e, "%s/taglist.%s",
				 gvWorkingDirectory, ftp->host->ohostname);
	f = tilde_expand_home(alt_filename ? alt_filename : e, gvLocalHomeDir);
	fp = fopen(f, "r");
	if(!fp) {
		if(showerr) {
			if(alt_filename)
				perror(alt_filename);
			else
				fprintf(stderr, _("No saved taglist for %s\n"),
						ftp->host->ohostname);
		}
		free(e);
		free(f);
		return;
	}

	if(gvLoadTaglist == 2 && !always_autoload) {
		c = ask(ASKYES|ASKNO, ASKYES, _("Found saved taglist, load it now?"));
		if(c == ASKNO)
			return;
	} /* else gvLoadTaglist == 1 == yes */

	if(fgets(tmp, 4096, fp) != 0) {
		strip_trailing_chars(tmp, "\n\r");
		if(strcasecmp(tmp, "[yafc taglist]") != 0) {
			fprintf(stderr, "Not a Yafc taglist file: %s\n", f);
			fclose(fp);
			return;
		}
	}

	while(true) {
		rfile *rf;

		if(fgets(tmp, 4096, fp) == 0)
			break;
		strip_trailing_chars(tmp, "\r\n");
		if(tmp[0] == 0)
			continue;
		rf = ftp_get_file(tmp);
		if(rf == 0) {
			rf = rfile_create();
			rfile_fake(rf, tmp);
		}
		list_additem(ftp->taglist, rfile_clone(rf));
		n++;
	}
	if(n)
		fprintf(stderr, "Loaded %u files from saved taglist %s\n", n, f);
	free(e);
	fclose(fp);
	if(alt_filename == 0)
		unlink(f);
	free(f);
}


static void show_taglist(void)
{
	listitem *li;

	if(list_numitem(ftp->taglist) == 0) {
		fprintf(stderr, _("nothing tagged -- use 'tag' to tag files\n"));
		return;
	}

	for(li=ftp->taglist->first; li; li=li->next)
		printf("%s\n", ((rfile *)li->data)->path);
}


static void show_taglist_info(void)
{
	unsigned long tlsize;

	if(list_numitem(ftp->taglist) == 0) {
		fprintf(stderr, _("nothing tagged -- use 'tag' to tag files\n"));
		return;
	}
	tlsize = rglob_size(ftp->taglist);
	printf(_("%lu bytes (%s) in %u files tagged\n"),
		   tlsize, human_size(tlsize), list_numitem(ftp->taglist));
	printf(_("%llu directories tagged\n"), rglob_numdirs(ftp->taglist));
}

void cmd_tag(int argc, char **argv)
{
	int i;
	int c;
	struct option longopts[] = {
		{"clear", no_argument, 0, 'c'},
		{"info", no_argument, 0, 'i'},
		{"list", no_argument, 0, 'l'},
		{"load", optional_argument, 0, 'L'},
		{"save", optional_argument, 0, 's'},
		{"help", no_argument, 0, 'h'},
		{0, 0, 0, 0}
	};

	optind = 0;
	while((c = getopt_long(argc, argv, "ciL::lhs::", longopts, 0)) != EOF) {
		switch(c) {
		  case 'c':
			list_clear(ftp->taglist);
			return;
		  case 's':
			save_taglist(optarg);
			return;
		  case 'l':
			show_taglist();
			return;
		  case 'L':
			load_taglist(true, true, optarg);
			return;
		  case 'i':
			show_taglist_info();
			return;
		  case 'h':
			print_tag_syntax();
			/* fall through */
		  case '?':
			return;
		}
	}

	need_connected();
	need_loggedin();

	if(argc == 1) {
#if defined(HAVE_LIBNEWT) && 0
		newt_tag();
#else
		minargs(1);
#endif
	} else {
		for(i=1;i<argc;i++) {
			stripslash(argv[i]);
			rglob_glob(ftp->taglist, argv[i], true, true, 0);
		}
	}
}

void cmd_untag(int argc, char **argv)
{
	int i, num;
	listitem *li, *pli;
	rfile *f;

	OPT_HELP("Remove files from the tag list.  Usage:\n"
			 "  untag [options] <filemask>...\n"
			 "Options:\n"
			 "  -h, --help    show this help\n");

	minargs(optind);
	need_connected();
	need_loggedin();

	if(list_numitem(ftp->taglist)==0) {
		puts(_("nothing tagged -- use 'tag' to tag files"));
		return;
	}

	for(i=1;i<argc;i++) {
		num = list_numitem(ftp->taglist);
		li = ftp->taglist->first;
		while(li) {
			f = (rfile *)li->data;
			pli = li;
			li = li->next;
			if(fnmatch(argv[i], f->path,
					   FNM_PATHNAME|FNM_PERIOD) != FNM_NOMATCH) {
				printf("%s\n", f->path);
				list_delitem(ftp->taglist, pli);
			}
		}
		if(list_numitem(ftp->taglist) == num)
			printf(_("%s: no matches found\n"), argv[i]);
	}
}