Mercurial > gftp.yaz
annotate lib/config_file.c @ 988:63555c9744c2
remote charset should be specified by each bookmark entry.
author | Yoshiki Yazawa <yaz@honeyplanet.jp> |
---|---|
date | Fri, 14 Aug 2009 07:54:55 +0900 |
parents | 85cf59eafce2 |
children |
rev | line source |
---|---|
1 | 1 /*****************************************************************************/ |
2 /* config_file.c - config file routines */ | |
885 | 3 /* Copyright (C) 1998-2007 Brian Masney <masneyb@gftp.org> */ |
1 | 4 /* */ |
5 /* This program is free software; you can redistribute it and/or modify */ | |
6 /* it under the terms of the GNU General Public License as published by */ | |
7 /* the Free Software Foundation; either version 2 of the License, or */ | |
8 /* (at your option) any later version. */ | |
9 /* */ | |
10 /* This program is distributed in the hope that it will be useful, */ | |
11 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ | |
12 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ | |
13 /* GNU General Public License for more details. */ | |
14 /* */ | |
15 /* You should have received a copy of the GNU General Public License */ | |
16 /* along with this program; if not, write to the Free Software */ | |
17 /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA */ | |
18 /*****************************************************************************/ | |
19 | |
20 #include "gftp.h" | |
33 | 21 static const char cvsid[] = "$Id$"; |
1 | 22 |
122 | 23 void |
24 gftp_add_bookmark (gftp_bookmarks_var * newentry) | |
1 | 25 { |
122 | 26 gftp_bookmarks_var * preventry, * folderentry, * endentry; |
27 char *curpos; | |
1 | 28 |
122 | 29 if (!newentry->protocol) |
124 | 30 newentry->protocol = g_strdup ("FTP"); |
1 | 31 |
122 | 32 /* We have to create the folders. For example, if we have |
33 Debian Sites/Debian, we have to create a Debian Sites entry */ | |
34 preventry = gftp_bookmarks; | |
35 if (preventry->children != NULL) | |
1 | 36 { |
122 | 37 endentry = preventry->children; |
38 while (endentry->next != NULL) | |
39 endentry = endentry->next; | |
40 } | |
41 else | |
42 endentry = NULL; | |
43 curpos = newentry->path; | |
44 while ((curpos = strchr (curpos, '/')) != NULL) | |
45 { | |
46 *curpos = '\0'; | |
47 /* See if we already made this folder */ | |
48 if ((folderentry = (gftp_bookmarks_var *) | |
49 g_hash_table_lookup (gftp_bookmarks_htable, newentry->path)) == NULL) | |
1 | 50 { |
122 | 51 /* Allocate the individual folder. We have to do this for the edit |
52 bookmarks feature */ | |
53 folderentry = g_malloc0 (sizeof (*folderentry)); | |
124 | 54 folderentry->path = g_strdup (newentry->path); |
122 | 55 folderentry->prev = preventry; |
56 folderentry->isfolder = 1; | |
57 g_hash_table_insert (gftp_bookmarks_htable, folderentry->path, | |
58 folderentry); | |
59 if (preventry->children == NULL) | |
60 preventry->children = folderentry; | |
1 | 61 else |
122 | 62 endentry->next = folderentry; |
63 preventry = folderentry; | |
64 endentry = NULL; | |
1 | 65 } |
66 else | |
67 { | |
122 | 68 preventry = folderentry; |
69 if (preventry->children != NULL) | |
70 { | |
71 endentry = preventry->children; | |
72 while (endentry->next != NULL) | |
73 endentry = endentry->next; | |
74 } | |
75 else | |
76 endentry = NULL; | |
1 | 77 } |
122 | 78 *curpos = '/'; |
79 curpos++; | |
1 | 80 } |
81 | |
605 | 82 if (newentry->path[strlen (newentry->path) - 1] == '/') |
83 { | |
84 newentry->path[strlen (newentry->path) - 1] = '\0'; | |
85 newentry->isfolder = 1; | |
86 } | |
122 | 87 else |
1 | 88 { |
605 | 89 /* Get the parent node */ |
90 if ((curpos = strrchr (newentry->path, '/')) == NULL) | |
91 preventry = gftp_bookmarks; | |
92 else | |
93 { | |
94 *curpos = '\0'; | |
95 preventry = (gftp_bookmarks_var *) | |
96 g_hash_table_lookup (gftp_bookmarks_htable, newentry->path); | |
97 *curpos = '/'; | |
98 } | |
99 | |
100 if (preventry->children != NULL) | |
101 { | |
102 endentry = preventry->children; | |
103 while (endentry->next != NULL) | |
104 endentry = endentry->next; | |
105 endentry->next = newentry; | |
106 } | |
107 else | |
108 preventry->children = newentry; | |
1 | 109 |
605 | 110 newentry->prev = preventry; |
111 newentry->next = NULL; | |
112 g_hash_table_insert (gftp_bookmarks_htable, newentry->path, newentry); | |
1 | 113 } |
114 } | |
115 | |
116 | |
320 | 117 static int |
118 copyfile (char *source, char *dest) | |
119 { | |
120 int srcfd, destfd; | |
121 char buf[8192]; | |
122 ssize_t n; | |
123 | |
124 if ((srcfd = gftp_fd_open (NULL, source, O_RDONLY, 0)) == -1) | |
125 { | |
126 printf (_("Error: Cannot open local file %s: %s\n"), | |
127 source, g_strerror (errno)); | |
765 | 128 exit (EXIT_FAILURE); |
320 | 129 } |
130 | |
131 if ((destfd = gftp_fd_open (NULL, dest, O_WRONLY | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR)) == -1) | |
132 { | |
133 printf (_("Error: Cannot open local file %s: %s\n"), | |
134 dest, g_strerror (errno)); | |
135 close (srcfd); | |
765 | 136 exit (EXIT_FAILURE); |
320 | 137 } |
138 | |
139 while ((n = read (srcfd, buf, sizeof (buf))) > 0) | |
140 { | |
141 if (write (destfd, buf, n) == -1) | |
142 { | |
143 printf (_("Error: Could not write to socket: %s\n"), | |
144 g_strerror (errno)); | |
765 | 145 exit (EXIT_FAILURE); |
320 | 146 } |
147 } | |
148 | |
149 if (n == -1) | |
150 { | |
151 printf (_("Error: Could not read from socket: %s\n"), g_strerror (errno)); | |
765 | 152 exit (EXIT_FAILURE); |
320 | 153 } |
154 | |
155 if (close (srcfd) == -1) | |
156 { | |
157 printf (_("Error closing file descriptor: %s\n"), g_strerror (errno)); | |
765 | 158 exit (EXIT_FAILURE); |
320 | 159 } |
160 | |
161 if (close (destfd) == -1) | |
162 { | |
163 printf (_("Error closing file descriptor: %s\n"), g_strerror (errno)); | |
765 | 164 exit (EXIT_FAILURE); |
320 | 165 } |
166 | |
167 return (1); | |
168 } | |
169 | |
170 | |
122 | 171 static void |
124 | 172 gftp_read_bookmarks (char *global_data_path) |
1 | 173 { |
174 char *tempstr, *temp1str, buf[255], *curpos; | |
199 | 175 gftp_config_vars * global_entry; |
122 | 176 gftp_bookmarks_var * newentry; |
1 | 177 FILE *bmfile; |
178 size_t len; | |
179 int line; | |
180 | |
555 | 181 if ((tempstr = gftp_expand_path (NULL, BOOKMARKS_FILE)) == NULL) |
1 | 182 { |
183 printf (_("gFTP Error: Bad bookmarks file name %s\n"), BOOKMARKS_FILE); | |
765 | 184 exit (EXIT_FAILURE); |
1 | 185 } |
186 | |
187 if (access (tempstr, F_OK) == -1) | |
188 { | |
124 | 189 temp1str = g_strdup_printf ("%s/bookmarks", global_data_path); |
1 | 190 if (access (temp1str, F_OK) == -1) |
191 { | |
192 printf (_("Warning: Cannot find master bookmark file %s\n"), | |
193 temp1str); | |
194 g_free (temp1str); | |
195 return; | |
196 } | |
197 copyfile (temp1str, tempstr); | |
198 g_free (temp1str); | |
199 } | |
58 | 200 |
1 | 201 if ((bmfile = fopen (tempstr, "r")) == NULL) |
202 { | |
203 printf (_("gFTP Error: Cannot open bookmarks file %s: %s\n"), tempstr, | |
204 g_strerror (errno)); | |
765 | 205 exit (EXIT_FAILURE); |
1 | 206 } |
207 g_free (tempstr); | |
208 | |
209 line = 0; | |
210 newentry = NULL; | |
211 while (fgets (buf, sizeof (buf), bmfile)) | |
212 { | |
460 | 213 len = strlen (buf); |
107 | 214 if (len > 0 && buf[len - 1] == '\n') |
516 | 215 buf[--len] = '\0'; |
107 | 216 if (len > 0 && buf[len - 1] == '\r') |
516 | 217 buf[--len] = '\0'; |
1 | 218 line++; |
219 | |
220 if (*buf == '[') | |
221 { | |
222 newentry = g_malloc0 (sizeof (*newentry)); | |
516 | 223 for (; buf[len - 1] == ' ' || buf[len - 1] == ']'; buf[--len] = '\0'); |
124 | 224 newentry->path = g_strdup (buf + 1); |
1 | 225 newentry->isfolder = 0; |
122 | 226 gftp_add_bookmark (newentry); |
1 | 227 } |
228 else if (strncmp (buf, "hostname", 8) == 0 && newentry) | |
229 { | |
230 curpos = buf + 9; | |
231 if (newentry->hostname) | |
232 g_free (newentry->hostname); | |
124 | 233 newentry->hostname = g_strdup (curpos); |
1 | 234 } |
235 else if (strncmp (buf, "port", 4) == 0 && newentry) | |
236 newentry->port = strtol (buf + 5, NULL, 10); | |
237 else if (strncmp (buf, "protocol", 8) == 0 && newentry) | |
238 { | |
239 curpos = buf + 9; | |
240 if (newentry->protocol) | |
241 g_free (newentry->protocol); | |
124 | 242 newentry->protocol = g_strdup (curpos); |
1 | 243 } |
244 else if (strncmp (buf, "remote directory", 16) == 0 && newentry) | |
245 { | |
246 curpos = buf + 17; | |
247 if (newentry->remote_dir) | |
248 g_free (newentry->remote_dir); | |
124 | 249 newentry->remote_dir = g_strdup (curpos); |
1 | 250 } |
251 else if (strncmp (buf, "local directory", 15) == 0 && newentry) | |
252 { | |
253 curpos = buf + 16; | |
254 if (newentry->local_dir) | |
255 g_free (newentry->local_dir); | |
124 | 256 newentry->local_dir = g_strdup (curpos); |
1 | 257 } |
988
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
942
diff
changeset
|
258 else if (strncmp (buf, "remote_charset", 14) == 0 && newentry) |
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
942
diff
changeset
|
259 { |
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
942
diff
changeset
|
260 curpos = buf + 15; |
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
942
diff
changeset
|
261 if (newentry->remote_charset) |
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
942
diff
changeset
|
262 g_free (newentry->remote_charset); |
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
942
diff
changeset
|
263 newentry->remote_charset = g_strdup (curpos); |
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
942
diff
changeset
|
264 } |
1 | 265 else if (strncmp (buf, "username", 8) == 0 && newentry) |
266 { | |
267 curpos = buf + 9; | |
268 if (newentry->user) | |
269 g_free (newentry->user); | |
124 | 270 newentry->user = g_strdup (curpos); |
1 | 271 } |
272 else if (strncmp (buf, "password", 8) == 0 && newentry) | |
273 { | |
274 curpos = buf + 9; | |
275 if (newentry->pass) | |
276 g_free (newentry->pass); | |
330 | 277 |
278 /* Always try to descramble passords. If the password is not | |
279 scrambled, descramble_password returns the string unchanged */ | |
280 newentry->pass = gftp_descramble_password (curpos); | |
1 | 281 newentry->save_password = *newentry->pass != '\0'; |
282 } | |
283 else if (strncmp (buf, "account", 7) == 0 && newentry) | |
284 { | |
285 curpos = buf + 8; | |
286 if (newentry->acct) | |
287 g_free (newentry->acct); | |
124 | 288 newentry->acct = g_strdup (curpos); |
1 | 289 } |
199 | 290 else if (*buf == '#' || *buf == '\0') |
291 continue; | |
292 else | |
1 | 293 { |
199 | 294 if ((curpos = strchr (buf, '=')) == NULL) |
295 continue; | |
296 *curpos = '\0'; | |
297 | |
298 if ((global_entry = g_hash_table_lookup (gftp_global_options_htable, | |
299 buf)) == NULL || | |
300 gftp_option_types[global_entry->otype].read_function == NULL) | |
301 { | |
302 printf (_("gFTP Warning: Skipping line %d in bookmarks file: %s\n"), | |
303 line, buf); | |
304 continue; | |
305 } | |
306 | |
307 if (newentry->local_options_hash == NULL) | |
308 newentry->local_options_hash = g_hash_table_new (string_hash_function, | |
309 string_hash_compare); | |
310 | |
311 newentry->num_local_options_vars++; | |
312 newentry->local_options_vars = g_realloc (newentry->local_options_vars, | |
765 | 313 (gulong) sizeof (gftp_config_vars) * newentry->num_local_options_vars); |
199 | 314 |
315 memcpy (&newentry->local_options_vars[newentry->num_local_options_vars - 1], global_entry, | |
316 sizeof (newentry->local_options_vars[newentry->num_local_options_vars - 1])); | |
317 | |
318 newentry->local_options_vars[newentry->num_local_options_vars - 1].flags &= ~GFTP_CVARS_FLAGS_DYNMEM; | |
319 newentry->local_options_vars[newentry->num_local_options_vars - 1].value = NULL; | |
320 | |
321 if (gftp_option_types[global_entry->otype].read_function (curpos + 1, | |
322 &newentry->local_options_vars[newentry->num_local_options_vars - 1], line) != 0) | |
323 { | |
324 printf (_("gFTP Warning: Skipping line %d in bookmarks file: %s\n"), | |
325 line, buf); | |
326 continue; | |
327 } | |
328 | |
329 g_hash_table_insert (newentry->local_options_hash, newentry->local_options_vars[newentry->num_local_options_vars - 1].key, &newentry->local_options_vars[newentry->num_local_options_vars - 1]); | |
1 | 330 } |
331 } | |
332 } | |
333 | |
334 | |
198 | 335 int |
336 gftp_config_parse_args (char *str, int numargs, int lineno, char **first, ...) | |
1 | 337 { |
338 char *curpos, *endpos, *pos, **dest, tempchar; | |
339 int ret, has_colon; | |
340 va_list argp; | |
341 | |
342 ret = 1; | |
343 va_start (argp, first); | |
344 curpos = str; | |
345 dest = first; | |
346 *dest = NULL; | |
347 while (numargs > 0) | |
348 { | |
349 has_colon = 0; | |
350 if (numargs > 1) | |
351 { | |
352 if ((endpos = strchr (curpos, ':')) == NULL) | |
353 { | |
354 printf (_("gFTP Warning: Line %d doesn't have enough arguments\n"), | |
355 lineno); | |
356 ret = 0; | |
357 endpos = curpos + strlen (curpos); | |
358 } | |
359 else | |
360 { | |
361 /* Allow colons inside the fields. If you want a colon inside a | |
362 field, just put 2 colons in there */ | |
363 while (endpos != NULL && *(endpos - 1) == '\\') | |
364 { | |
365 endpos = strchr (endpos + 1, ':'); | |
366 has_colon = 1; | |
367 } | |
368 } | |
369 } | |
370 else | |
371 endpos = curpos + strlen (curpos); | |
372 | |
942 | 373 *dest = g_malloc0 ((gulong) (endpos - curpos + 1)); |
1 | 374 tempchar = *endpos; |
375 *endpos = '\0'; | |
376 strcpy (*dest, curpos); | |
377 *endpos = tempchar; | |
378 if (has_colon) | |
379 { | |
380 pos = *dest; | |
381 curpos = *dest; | |
382 while (*pos != '\0') | |
383 { | |
384 if (*pos != '\\' && *(pos + 1) != ':') | |
385 *curpos++ = *pos++; | |
386 else | |
387 pos++; | |
388 } | |
389 *curpos = '\0'; | |
390 } | |
391 if (*endpos == '\0') | |
392 break; | |
393 curpos = endpos + 1; | |
394 if (numargs > 1) | |
395 { | |
396 dest = va_arg (argp, char **); | |
397 *dest = NULL; | |
398 } | |
399 numargs--; | |
400 } | |
401 | |
402 while (numargs > 1) | |
403 { | |
404 dest = va_arg (argp, char **); | |
942 | 405 *dest = g_malloc0 (1); |
1 | 406 numargs--; |
407 } | |
408 va_end (argp); | |
516 | 409 return (ret); |
1 | 410 } |
411 | |
412 | |
122 | 413 static void * |
414 gftp_config_read_str (char *buf, int line) | |
415 { | |
516 | 416 return (g_strdup (buf)); |
122 | 417 } |
418 | |
419 | |
420 static void | |
421 gftp_config_write_str (FILE *fd, void *data) | |
422 { | |
423 fprintf (fd, "%s", (char *) data); | |
424 } | |
425 | |
426 | |
427 static void * | |
428 gftp_config_read_proxy (char *buf, int line) | |
429 { | |
430 gftp_proxy_hosts * host; | |
431 unsigned int nums[4]; | |
432 char *pos; | |
433 | |
434 host = g_malloc0 (sizeof (*host)); | |
435 if ((pos = strchr (buf, '/')) == NULL) | |
436 host->domain = g_strdup (buf); | |
437 else | |
438 { | |
439 *pos = '\0'; | |
440 sscanf (buf, "%u.%u.%u.%u", &nums[0], &nums[1], &nums[2], &nums[3]); | |
441 host->ipv4_network_address = | |
442 nums[0] << 24 | nums[1] << 16 | nums[2] << 8 | nums[3]; | |
443 | |
444 if (strchr (pos + 1, '.') == NULL) | |
445 host->ipv4_netmask = 0xffffffff << (32 - strtol (pos + 1, NULL, 10)); | |
446 else | |
447 { | |
448 sscanf (pos + 1, "%u.%u.%u.%u", &nums[0], &nums[1], &nums[2], | |
449 &nums[3]); | |
450 host->ipv4_netmask = | |
451 nums[0] << 24 | nums[1] << 16 | nums[2] << 8 | nums[3]; | |
452 } | |
453 } | |
454 | |
455 return (host); | |
456 } | |
457 | |
458 | |
459 static void | |
460 gftp_config_write_proxy (FILE *fd, void *data) | |
461 { | |
462 gftp_proxy_hosts * host; | |
463 | |
464 host = data; | |
465 | |
466 if (host->domain) | |
467 fprintf (fd, "%s", host->domain); | |
468 else | |
469 fprintf (fd, "%d.%d.%d.%d/%d.%d.%d.%d", | |
470 host->ipv4_network_address >> 24 & 0xff, | |
471 host->ipv4_network_address >> 16 & 0xff, | |
472 host->ipv4_network_address >> 8 & 0xff, | |
473 host->ipv4_network_address & 0xff, | |
474 host->ipv4_netmask >> 24 & 0xff, | |
475 host->ipv4_netmask >> 16 & 0xff, | |
476 host->ipv4_netmask >> 8 & 0xff, | |
477 host->ipv4_netmask & 0xff); | |
478 } | |
479 | |
480 | |
481 static void * | |
482 gftp_config_read_ext (char *buf, int line) | |
483 { | |
484 gftp_file_extensions * tempext; | |
485 | |
942 | 486 tempext = g_malloc0 (sizeof (*tempext)); |
198 | 487 gftp_config_parse_args (buf, 4, line, &tempext->ext, &tempext->filename, |
488 &tempext->ascii_binary, &tempext->view_program); | |
122 | 489 |
490 tempext->stlen = strlen (tempext->ext); | |
491 | |
492 return (tempext); | |
493 } | |
494 | |
495 | |
496 static void | |
497 gftp_config_write_ext (FILE *fd, void *data) | |
498 { | |
499 gftp_file_extensions * tempext; | |
500 | |
501 tempext = data; | |
502 fprintf (fd, "%s:%s:%c:%s", tempext->ext, tempext->filename, | |
503 *tempext->ascii_binary == '\0' ? ' ' : *tempext->ascii_binary, | |
504 tempext->view_program); | |
505 } | |
506 | |
507 | |
516 | 508 static gftp_config_list_vars gftp_config_list[] = { |
122 | 509 {"dont_use_proxy", gftp_config_read_proxy, gftp_config_write_proxy, |
510 NULL, 0, | |
511 N_("This section specifies which hosts are on the local subnet and won't need to go out the proxy server (if available). Syntax: dont_use_proxy=.domain or dont_use_proxy=network number/netmask")}, | |
512 {"ext", gftp_config_read_ext, gftp_config_write_ext, | |
513 NULL, 0, | |
514 N_("ext=file extenstion:XPM file:Ascii or Binary (A or B):viewer program. Note: All arguments except the file extension are optional")}, | |
515 {"localhistory", gftp_config_read_str, gftp_config_write_str, | |
516 NULL, 0, NULL}, | |
517 {"remotehistory", gftp_config_read_str, gftp_config_write_str, | |
518 NULL, 0, NULL}, | |
519 {"hosthistory", gftp_config_read_str, gftp_config_write_str, | |
520 NULL, 0, NULL}, | |
521 {"porthistory", gftp_config_read_str, gftp_config_write_str, | |
522 NULL, 0, NULL}, | |
523 {"userhistory", gftp_config_read_str, gftp_config_write_str, | |
524 NULL, 0, NULL}, | |
525 {NULL, NULL, NULL, | |
526 NULL, 0, NULL} | |
527 }; | |
528 | |
529 | |
530 static void | |
531 gftp_setup_global_options (gftp_config_vars * cvars) | |
532 { | |
533 int i; | |
534 | |
535 for (i=0; cvars[i].key != NULL; i++) | |
536 { | |
136 | 537 if (cvars[i].key != NULL && *cvars[i].key != '\0') |
122 | 538 g_hash_table_insert (gftp_global_options_htable, |
539 cvars[i].key, &cvars[i]); | |
540 } | |
541 } | |
542 | |
543 | |
544 void | |
124 | 545 gftp_read_config_file (char *global_data_path) |
122 | 546 { |
547 char *tempstr, *temp1str, *curpos, buf[255]; | |
548 gftp_config_list_vars * tmplistvar; | |
549 gftp_config_vars * tmpconfigvar; | |
125 | 550 char **protocol_list; |
122 | 551 FILE *conffile; |
600 | 552 int line, i, j; |
122 | 553 size_t len; |
554 | |
555 gftp_global_options_htable = g_hash_table_new (string_hash_function, | |
556 string_hash_compare); | |
557 | |
558 gftp_register_config_vars (gftp_global_config_vars); | |
559 | |
125 | 560 protocol_list = NULL; |
600 | 561 for (i=0, j=0; gftp_protocols[i].register_options != NULL; i++) |
122 | 562 { |
125 | 563 if (gftp_protocols[i].shown) |
564 { | |
765 | 565 protocol_list = g_realloc (protocol_list, |
566 (gulong) sizeof (char *) * (j + 2)); | |
600 | 567 protocol_list[j] = gftp_protocols[i].name; |
568 protocol_list[j + 1] = NULL; | |
569 j++; | |
125 | 570 } |
571 | |
122 | 572 if (gftp_protocols[i].register_options != NULL) |
573 gftp_protocols[i].register_options (); | |
574 } | |
575 | |
125 | 576 if ((tmpconfigvar = g_hash_table_lookup (gftp_global_options_htable, |
577 "default_protocol")) != NULL) | |
201 | 578 { |
579 tmpconfigvar->listdata = protocol_list; | |
580 tmpconfigvar->flags |= GFTP_CVARS_FLAGS_DYNLISTMEM; | |
581 } | |
125 | 582 |
122 | 583 gftp_config_list_htable = g_hash_table_new (string_hash_function, |
584 string_hash_compare); | |
585 | |
586 for (i=0; gftp_config_list[i].key != NULL; i++) | |
587 { | |
588 g_hash_table_insert (gftp_config_list_htable, gftp_config_list[i].key, | |
589 &gftp_config_list[i]); | |
590 } | |
591 | |
555 | 592 if ((tempstr = gftp_expand_path (NULL, CONFIG_FILE)) == NULL) |
122 | 593 { |
594 printf (_("gFTP Error: Bad config file name %s\n"), CONFIG_FILE); | |
765 | 595 exit (EXIT_FAILURE); |
122 | 596 } |
597 | |
598 if (access (tempstr, F_OK) == -1) | |
599 { | |
555 | 600 temp1str = gftp_expand_path (NULL, BASE_CONF_DIR); |
122 | 601 if (access (temp1str, F_OK) == -1) |
602 { | |
603 if (mkdir (temp1str, S_IRUSR | S_IWUSR | S_IXUSR) != 0) | |
604 { | |
605 printf (_("gFTP Error: Could not make directory %s: %s\n"), | |
606 temp1str, g_strerror (errno)); | |
765 | 607 exit (EXIT_FAILURE); |
122 | 608 } |
609 } | |
610 g_free (temp1str); | |
611 | |
124 | 612 temp1str = g_strdup_printf ("%s/gftprc", global_data_path); |
122 | 613 if (access (temp1str, F_OK) == -1) |
614 { | |
615 printf (_("gFTP Error: Cannot find master config file %s\n"), | |
616 temp1str); | |
617 printf (_("Did you do a make install?\n")); | |
765 | 618 exit (EXIT_FAILURE); |
122 | 619 } |
620 copyfile (temp1str, tempstr); | |
621 g_free (temp1str); | |
622 } | |
623 | |
624 if ((conffile = fopen (tempstr, "r")) == NULL) | |
625 { | |
626 printf (_("gFTP Error: Cannot open config file %s: %s\n"), CONFIG_FILE, | |
627 g_strerror (errno)); | |
765 | 628 exit (EXIT_FAILURE); |
122 | 629 } |
630 g_free (tempstr); | |
631 | |
632 line = 0; | |
633 while (fgets (buf, sizeof (buf), conffile)) | |
634 { | |
635 len = strlen (buf); | |
636 if (len > 0 && buf[len - 1] == '\n') | |
637 buf[--len] = '\0'; | |
638 if (len > 0 && buf[len - 1] == '\r') | |
639 buf[--len] = '\0'; | |
640 line++; | |
641 | |
642 if (*buf == '#' || *buf == '\0') | |
643 continue; | |
644 | |
645 if ((curpos = strchr (buf, '=')) == NULL) | |
646 continue; | |
647 | |
648 *curpos = '\0'; | |
649 | |
650 if ((tmplistvar = g_hash_table_lookup (gftp_config_list_htable, | |
651 buf)) != NULL) | |
652 { | |
653 tmplistvar->list = g_list_append (tmplistvar->list, | |
654 tmplistvar->read_func (curpos + 1, | |
655 line)); | |
656 tmplistvar->num_items++; | |
657 } | |
658 else if ((tmpconfigvar = g_hash_table_lookup (gftp_global_options_htable, | |
659 buf)) != NULL && | |
660 gftp_option_types[tmpconfigvar->otype].read_function != NULL) | |
661 { | |
662 if (gftp_option_types[tmpconfigvar->otype].read_function (curpos + 1, | |
663 tmpconfigvar, line) != 0) | |
664 { | |
665 printf (_("Terminating due to parse errors at line %d in the config file\n"), line); | |
765 | 666 exit (EXIT_FAILURE); |
122 | 667 } |
668 } | |
669 else | |
670 { | |
671 printf (_("gFTP Warning: Skipping line %d in config file: %s\n"), | |
672 line, buf); | |
673 } | |
674 } | |
675 | |
555 | 676 if ((tempstr = gftp_expand_path (NULL, LOG_FILE)) == NULL) |
122 | 677 { |
678 printf (_("gFTP Error: Bad log file name %s\n"), LOG_FILE); | |
765 | 679 exit (EXIT_FAILURE); |
122 | 680 } |
681 | |
682 if ((gftp_logfd = fopen (tempstr, "w")) == NULL) | |
683 { | |
684 printf (_("gFTP Warning: Cannot open %s for writing: %s\n"), | |
685 tempstr, g_strerror (errno)); | |
686 } | |
687 g_free (tempstr); | |
688 | |
689 gftp_bookmarks = g_malloc0 (sizeof (*gftp_bookmarks)); | |
690 gftp_bookmarks->isfolder = 1; | |
691 gftp_bookmarks->path = g_malloc0 (1); | |
692 gftp_bookmarks_htable = g_hash_table_new (string_hash_function, string_hash_compare); | |
693 | |
124 | 694 gftp_read_bookmarks (global_data_path); |
122 | 695 } |
696 | |
697 | |
698 static void | |
699 write_comment (FILE * fd, const char *comment) | |
700 { | |
701 const char *pos, *endpos; | |
702 | |
703 fwrite ("# ", 1, 2, fd); | |
704 pos = comment; | |
705 while (strlen (pos) > 76) | |
706 { | |
707 for (endpos = pos + 76; *endpos != ' ' && endpos > pos; endpos--); | |
708 if (endpos == pos) | |
709 { | |
710 for (endpos = pos + 76; *endpos != ' ' && *endpos != '\0'; | |
711 endpos++); | |
712 } | |
713 fwrite (pos, 1, endpos - pos, fd); | |
854 | 714 fwrite ("\n", 1, 1, fd); |
122 | 715 if (*endpos == '\0') |
716 { | |
717 pos = endpos; | |
718 break; | |
719 } | |
854 | 720 pos = endpos + 1; |
721 fwrite ("# ", 1, 2, fd); | |
122 | 722 } |
723 if (strlen (pos) > 1) | |
724 { | |
725 fwrite (pos, 1, strlen (pos), fd); | |
726 fwrite ("\n", 1, 1, fd); | |
727 } | |
728 } | |
729 | |
730 | |
731 void | |
732 gftp_write_bookmarks_file (void) | |
733 { | |
387 | 734 char *bmhdr, *pwhdr, *tempstr, *password, buf[256]; |
122 | 735 gftp_bookmarks_var * tempentry; |
736 FILE * bmfile; | |
199 | 737 int i; |
122 | 738 |
885 | 739 bmhdr = N_("Bookmarks file for gFTP. Copyright (C) 1998-2007 Brian Masney <masneyb@gftp.org>. Warning: Any comments that you add to this file WILL be overwritten"); |
332 | 740 pwhdr = N_("Note: The passwords contained inside this file are scrambled. This algorithm is not secure. This is to avoid your password being easily remembered by someone standing over your shoulder while you're editing this file. Prior to this, all passwords were stored in plaintext."); |
122 | 741 |
555 | 742 if ((tempstr = gftp_expand_path (NULL, BOOKMARKS_FILE)) == NULL) |
122 | 743 { |
744 printf (_("gFTP Error: Bad bookmarks file name %s\n"), CONFIG_FILE); | |
765 | 745 exit (EXIT_FAILURE); |
122 | 746 } |
747 | |
748 if ((bmfile = fopen (tempstr, "w+")) == NULL) | |
749 { | |
750 printf (_("gFTP Error: Cannot open bookmarks file %s: %s\n"), | |
751 CONFIG_FILE, g_strerror (errno)); | |
765 | 752 exit (EXIT_FAILURE); |
122 | 753 } |
754 | |
755 g_free (tempstr); | |
756 | |
757 write_comment (bmfile, _(bmhdr)); | |
332 | 758 write_comment (bmfile, _(pwhdr)); |
122 | 759 fwrite ("\n", 1, 1, bmfile); |
760 | |
761 tempentry = gftp_bookmarks->children; | |
762 while (tempentry != NULL) | |
763 { | |
764 if (tempentry->children != NULL) | |
765 { | |
766 tempentry = tempentry->children; | |
767 continue; | |
768 } | |
330 | 769 |
122 | 770 tempstr = tempentry->path; |
771 while (*tempstr == '/') | |
772 tempstr++; | |
330 | 773 |
607 | 774 if (tempentry->isfolder) |
775 { | |
776 fprintf (bmfile, "[%s/]\n", tempstr); | |
777 } | |
330 | 778 else |
607 | 779 { |
780 if (tempentry->save_password && tempentry->pass != NULL) | |
781 password = gftp_scramble_password (tempentry->pass); | |
782 else | |
783 password = NULL; | |
330 | 784 |
607 | 785 fprintf (bmfile, |
988
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
942
diff
changeset
|
786 "[%s]\nhostname=%s\nport=%u\nprotocol=%s\nremote directory=%s\nlocal directory=%s\nremote_charset=%s\nusername=%s\npassword=%s\naccount=%s\n", |
607 | 787 tempstr, tempentry->hostname == NULL ? "" : tempentry->hostname, |
788 tempentry->port, tempentry->protocol == NULL | |
789 || *tempentry->protocol == | |
790 '\0' ? gftp_protocols[0].name : tempentry->protocol, | |
791 tempentry->remote_dir == NULL ? "" : tempentry->remote_dir, | |
792 tempentry->local_dir == NULL ? "" : tempentry->local_dir, | |
988
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
942
diff
changeset
|
793 tempentry->remote_charset == NULL ? "" : tempentry->remote_charset, |
607 | 794 tempentry->user == NULL ? "" : tempentry->user, |
795 password == NULL ? "" : password, | |
796 tempentry->acct == NULL ? "" : tempentry->acct); | |
797 if (password != NULL) | |
798 g_free(password); | |
122 | 799 |
607 | 800 if (tempentry->local_options_vars != NULL) |
199 | 801 { |
607 | 802 for (i=0; i<tempentry->num_local_options_vars; i++) |
803 { | |
804 gftp_option_types[tempentry->local_options_vars[i].otype].write_function (&tempentry->local_options_vars[i], buf, sizeof (buf), 1); | |
805 | |
806 fprintf (bmfile, "%s=%s\n", tempentry->local_options_vars[i].key, | |
807 buf); | |
808 } | |
199 | 809 } |
810 } | |
122 | 811 |
812 fprintf (bmfile, "\n"); | |
813 | |
814 if (tempentry->next == NULL) | |
815 { | |
816 tempentry = tempentry->prev; | |
817 while (tempentry->next == NULL && tempentry->prev != NULL) | |
818 tempentry = tempentry->prev; | |
819 tempentry = tempentry->next; | |
820 } | |
821 else | |
822 tempentry = tempentry->next; | |
823 } | |
824 | |
825 fclose (bmfile); | |
826 } | |
827 | |
828 | |
829 void | |
830 gftp_write_config_file (void) | |
831 { | |
387 | 832 char *tempstr, buf[256]; |
122 | 833 gftp_config_vars * cv; |
834 GList *templist; | |
835 FILE *conffile; | |
836 int i; | |
837 | |
555 | 838 if ((tempstr = gftp_expand_path (NULL, CONFIG_FILE)) == NULL) |
122 | 839 { |
840 printf (_("gFTP Error: Bad config file name %s\n"), CONFIG_FILE); | |
765 | 841 exit (EXIT_FAILURE); |
122 | 842 } |
843 | |
844 if ((conffile = fopen (tempstr, "w+")) == NULL) | |
845 { | |
846 printf (_("gFTP Error: Cannot open config file %s: %s\n"), CONFIG_FILE, | |
847 g_strerror (errno)); | |
765 | 848 exit (EXIT_FAILURE); |
122 | 849 } |
850 | |
851 g_free (tempstr); | |
852 | |
885 | 853 write_comment (conffile, _("Config file for gFTP. Copyright (C) 1998-2007 Brian Masney <masneyb@gftp.org>. Warning: Any comments that you add to this file WILL be overwritten. If a entry has a (*) in it's comment, you can't change it inside gFTP")); |
122 | 854 |
855 for (templist = gftp_options_list; | |
856 templist != NULL; | |
857 templist = templist->next) | |
858 { | |
859 cv = templist->data; | |
860 | |
861 for (i=0; cv[i].key != NULL; i++) | |
862 { | |
863 if (gftp_option_types[cv[i].otype].write_function == NULL || | |
864 *cv[i].key == '\0') | |
865 continue; | |
866 | |
867 fprintf (conffile, "\n"); | |
868 if (cv[i].comment != NULL) | |
869 write_comment (conffile, _(cv[i].comment)); | |
870 | |
387 | 871 gftp_option_types[cv[i].otype].write_function (&cv[i], buf, |
872 sizeof (buf), 1); | |
873 fprintf (conffile, "%s=%s\n", cv[i].key, buf); | |
122 | 874 } |
875 } | |
876 | |
218 | 877 for (i=0; gftp_config_list[i].key != NULL; i++) |
122 | 878 { |
218 | 879 if (gftp_config_list[i].header == NULL && |
880 gftp_config_list[i].list == NULL) | |
881 continue; | |
882 | |
122 | 883 fprintf (conffile, "\n"); |
884 if (gftp_config_list[i].header != NULL) | |
885 write_comment (conffile, _(gftp_config_list[i].header)); | |
886 | |
218 | 887 for (templist = gftp_config_list[i].list; |
122 | 888 templist != NULL; |
889 templist = templist->next) | |
890 { | |
891 fprintf (conffile, "%s=", gftp_config_list[i].key); | |
892 gftp_config_list[i].write_func (conffile, templist->data); | |
893 fprintf (conffile, "\n"); | |
894 } | |
895 } | |
896 | |
897 fclose (conffile); | |
898 } | |
899 | |
900 | |
1 | 901 GHashTable * |
122 | 902 build_bookmarks_hash_table (gftp_bookmarks_var * entry) |
1 | 903 { |
122 | 904 gftp_bookmarks_var * tempentry; |
1 | 905 GHashTable * htable; |
906 | |
907 htable = g_hash_table_new (string_hash_function, string_hash_compare); | |
908 tempentry = entry; | |
909 while (tempentry != NULL) | |
910 { | |
911 g_hash_table_insert (htable, tempentry->path, tempentry); | |
912 if (tempentry->children != NULL) | |
913 { | |
914 tempentry = tempentry->children; | |
915 continue; | |
916 } | |
917 while (tempentry->next == NULL && tempentry->prev != NULL) | |
918 tempentry = tempentry->prev; | |
919 tempentry = tempentry->next; | |
920 } | |
921 return (htable); | |
922 } | |
923 | |
924 | |
925 void | |
122 | 926 print_bookmarks (gftp_bookmarks_var * bookmarks) |
1 | 927 { |
122 | 928 gftp_bookmarks_var * tempentry; |
1 | 929 |
930 tempentry = bookmarks->children; | |
931 while (tempentry != NULL) | |
932 { | |
933 printf ("Path: %s (%d)\n", tempentry->path, tempentry->children != NULL); | |
934 if (tempentry->children != NULL) | |
935 { | |
936 tempentry = tempentry->children; | |
937 continue; | |
938 } | |
939 | |
940 if (tempentry->next == NULL) | |
941 { | |
942 while (tempentry->next == NULL && tempentry->prev != NULL) | |
943 tempentry = tempentry->prev; | |
944 tempentry = tempentry->next; | |
945 } | |
946 else | |
947 tempentry = tempentry->next; | |
948 } | |
949 } | |
950 | |
122 | 951 |
952 static int | |
953 gftp_config_file_read_text (char *str, gftp_config_vars * cv, int line) | |
954 { | |
136 | 955 if (cv->flags & GFTP_CVARS_FLAGS_DYNMEM && cv->value != NULL) |
956 g_free (cv->value); | |
957 | |
122 | 958 if (str != NULL) |
959 { | |
960 cv->value = g_strdup (str); | |
136 | 961 cv->flags |= GFTP_CVARS_FLAGS_DYNMEM; |
122 | 962 return (0); |
963 } | |
964 else | |
136 | 965 { |
966 cv->value = NULL; | |
967 cv->flags &= ~GFTP_CVARS_FLAGS_DYNMEM; | |
968 return (-1); | |
969 } | |
122 | 970 } |
971 | |
972 | |
973 static int | |
387 | 974 gftp_config_file_write_text (gftp_config_vars * cv, char *buf, size_t buflen, |
975 int to_config_file) | |
122 | 976 { |
977 char *outstr; | |
978 | |
979 if (cv->value != NULL) | |
980 { | |
981 outstr = cv->value; | |
387 | 982 g_snprintf (buf, buflen, "%s", outstr); |
122 | 983 return (0); |
984 } | |
985 else | |
986 return (-1); | |
987 } | |
988 | |
989 | |
990 static int | |
387 | 991 gftp_config_file_write_hidetext (gftp_config_vars * cv, char *buf, |
992 size_t buflen, int to_config_file) | |
122 | 993 { |
994 char *outstr; | |
995 | |
996 if (cv->value != NULL) | |
997 { | |
998 outstr = cv->value; | |
999 if (*outstr != '\0') | |
1000 { | |
1001 if (to_config_file) | |
387 | 1002 g_snprintf (buf, buflen, "%s", outstr); |
122 | 1003 else |
387 | 1004 g_snprintf (buf, buflen, "*****"); |
122 | 1005 } |
1006 return (0); | |
1007 } | |
1008 else | |
1009 return (-1); | |
1010 } | |
1011 | |
1012 | |
207 | 1013 static void |
1014 gftp_config_file_copy_text (gftp_config_vars * cv, gftp_config_vars * dest_cv) | |
1015 { | |
1016 if (dest_cv->flags & GFTP_CVARS_FLAGS_DYNMEM && dest_cv->value != NULL) | |
1017 g_free (dest_cv->value); | |
1018 | |
1019 if (cv->value != NULL) | |
1020 { | |
1021 dest_cv->value = g_strdup ((char *) cv->value); | |
1022 dest_cv->flags |= GFTP_CVARS_FLAGS_DYNMEM; | |
1023 } | |
1024 else | |
1025 dest_cv->value = NULL; | |
1026 } | |
1027 | |
1028 | |
227 | 1029 static int |
1030 gftp_config_file_compare_text (gftp_config_vars * cv1, gftp_config_vars * cv2) | |
1031 { | |
1032 char *str1, *str2; | |
1033 | |
1034 str1 = cv1->value; | |
1035 str2 = cv2->value; | |
1036 | |
1037 if (cv1->value == NULL && cv2->value == NULL) | |
1038 return (0); | |
1039 | |
1040 if ((cv1->value == NULL && cv2->value != NULL) || | |
1041 (cv1->value != NULL && cv2->value == NULL)) | |
1042 return (-1); | |
1043 | |
1044 return (strcmp (str1, str2)); | |
1045 } | |
1046 | |
1047 | |
207 | 1048 static void |
1049 gftp_config_file_copy_ptr_contents (gftp_config_vars * cv, gftp_config_vars * dest_cv) | |
1050 { | |
1051 memcpy (&dest_cv->value, &cv->value, sizeof (dest_cv->value)); | |
1052 } | |
1053 | |
122 | 1054 static int |
1055 gftp_config_file_read_int (char *str, gftp_config_vars * cv, int line) | |
1056 { | |
1057 cv->value = GINT_TO_POINTER(strtol (str, NULL, 10)); | |
1058 return (0); | |
1059 } | |
1060 | |
1061 | |
1062 static int | |
387 | 1063 gftp_config_file_write_int (gftp_config_vars * cv, char *buf, size_t buflen, |
1064 int to_config_file) | |
122 | 1065 { |
387 | 1066 g_snprintf (buf, buflen, "%d", GPOINTER_TO_INT (cv->value)); |
122 | 1067 return (0); |
1068 } | |
1069 | |
1070 | |
1071 static int | |
227 | 1072 gftp_config_file_compare_int (gftp_config_vars * cv1, gftp_config_vars * cv2) |
1073 { | |
1074 return (GPOINTER_TO_INT(cv1->value) == GPOINTER_TO_INT(cv2->value) ? 0 : -1); | |
1075 } | |
1076 | |
1077 | |
1078 static int | |
122 | 1079 gftp_config_file_read_checkbox (char *str, gftp_config_vars * cv, int line) |
1080 { | |
1081 cv->value = GINT_TO_POINTER(strtol (str, NULL, 10) ? 1 : 0); | |
1082 return (0); | |
1083 } | |
1084 | |
1085 | |
1086 static int | |
1087 gftp_config_file_read_float (char *str, gftp_config_vars * cv, int line) | |
1088 { | |
325 | 1089 union { intptr_t i; float f; } r; |
131 | 1090 |
325 | 1091 r.f = strtod (str, NULL); |
1092 memcpy (&cv->value, &r.i, sizeof (cv->value)); | |
122 | 1093 return (0); |
1094 } | |
1095 | |
1096 | |
1097 static int | |
387 | 1098 gftp_config_file_write_float (gftp_config_vars * cv, char *buf, size_t buflen, |
1099 int to_config_file) | |
122 | 1100 { |
131 | 1101 float f; |
1102 | |
1103 memcpy (&f, &cv->value, sizeof (f)); | |
387 | 1104 g_snprintf (buf, buflen, "%.2f", f); |
122 | 1105 return (0); |
1106 } | |
1107 | |
1108 | |
1109 static int | |
227 | 1110 gftp_config_file_compare_float (gftp_config_vars * cv1, gftp_config_vars * cv2) |
1111 { | |
1112 float f1, f2; | |
1113 | |
1114 memcpy (&f1, &cv1->value, sizeof (f1)); | |
1115 memcpy (&f2, &cv2->value, sizeof (f2)); | |
1116 return (f1 == f2 ? 0 : -1); | |
1117 } | |
1118 | |
1119 | |
1120 static int | |
122 | 1121 gftp_config_file_read_color (char *str, gftp_config_vars * cv, int line) |
1122 { | |
1123 char *red, *green, *blue; | |
1124 gftp_color * color; | |
1125 | |
136 | 1126 if (cv->flags & GFTP_CVARS_FLAGS_DYNMEM && cv->value != NULL) |
1127 g_free (cv->value); | |
1128 | |
198 | 1129 gftp_config_parse_args (str, 3, line, &red, &green, &blue); |
122 | 1130 |
942 | 1131 color = g_malloc0 (sizeof (*color)); |
122 | 1132 color->red = strtol (red, NULL, 16); |
1133 color->green = strtol (green, NULL, 16); | |
1134 color->blue = strtol (blue, NULL, 16); | |
1135 g_free (red); | |
1136 g_free (green); | |
1137 g_free (blue); | |
1138 | |
1139 cv->value = color; | |
136 | 1140 cv->flags |= GFTP_CVARS_FLAGS_DYNMEM; |
122 | 1141 |
1142 return (0); | |
1143 } | |
1144 | |
1145 | |
1146 static int | |
387 | 1147 gftp_config_file_write_color (gftp_config_vars * cv, char *buf, size_t buflen, |
1148 int to_config_file) | |
122 | 1149 { |
1150 gftp_color * color; | |
1151 | |
1152 color = cv->value; | |
387 | 1153 g_snprintf (buf, buflen, "%x:%x:%x", color->red, color->green, color->blue); |
122 | 1154 return (0); |
1155 } | |
1156 | |
1157 | |
207 | 1158 static void |
1159 gftp_config_file_copy_color (gftp_config_vars * cv, gftp_config_vars * dest_cv) | |
1160 { | |
1161 if (dest_cv->flags & GFTP_CVARS_FLAGS_DYNMEM && dest_cv->value != NULL) | |
1162 g_free (dest_cv->value); | |
1163 | |
942 | 1164 dest_cv->value = g_malloc0 (sizeof (gftp_color)); |
207 | 1165 memcpy (dest_cv->value, cv->value, sizeof (gftp_color)); |
1166 dest_cv->flags |= GFTP_CVARS_FLAGS_DYNMEM; | |
1167 } | |
1168 | |
1169 | |
122 | 1170 static int |
227 | 1171 gftp_config_file_compare_color (gftp_config_vars * cv1, gftp_config_vars * cv2) |
1172 { | |
1173 gftp_color * color1, * color2; | |
1174 | |
1175 color1 = cv1->value; | |
1176 color2 = cv2->value; | |
1177 | |
1178 return (color1->red == color2->red && color1->green == color2->green && | |
1179 color1->blue == color2->blue ? 0 : -1); | |
1180 } | |
1181 | |
1182 | |
1183 static int | |
122 | 1184 gftp_config_file_read_intcombo (char *str, gftp_config_vars * cv, int line) |
1185 { | |
1186 char **clist; | |
1187 int i; | |
1188 | |
1189 cv->value = 0; | |
1190 if (cv->listdata != NULL) | |
1191 { | |
1192 clist = cv->listdata; | |
1193 for (i=0; clist[i] != NULL; i++) | |
1194 { | |
125 | 1195 if (strcasecmp (clist[i], str) == 0) |
122 | 1196 { |
1197 cv->value = GINT_TO_POINTER(i); | |
1198 break; | |
1199 } | |
1200 } | |
1201 } | |
1202 | |
1203 return (0); | |
1204 } | |
1205 | |
1206 | |
1207 static int | |
387 | 1208 gftp_config_file_write_intcombo (gftp_config_vars * cv, char *buf, |
1209 size_t buflen, int to_config_file) | |
122 | 1210 { |
1211 char **clist; | |
1212 | |
1213 clist = cv->listdata; | |
1214 if (clist != NULL) | |
387 | 1215 g_snprintf (buf, buflen, "%s", clist[GPOINTER_TO_INT(cv->value)]); |
122 | 1216 else |
387 | 1217 g_snprintf (buf, buflen, _("<unknown>")); |
122 | 1218 |
1219 return (0); | |
1220 } | |
1221 | |
1222 | |
125 | 1223 static int |
1224 gftp_config_file_read_textcombo (char *str, gftp_config_vars * cv, int line) | |
1225 { | |
1226 char **clist; | |
1227 int i; | |
1228 | |
1229 cv->value = NULL; | |
1230 if (cv->listdata != NULL) | |
1231 { | |
1232 clist = cv->listdata; | |
1233 for (i=0; clist[i] != NULL; i++) | |
1234 { | |
1235 if (strcasecmp (clist[i], str) == 0) | |
1236 { | |
1237 cv->value = clist[i]; | |
1238 break; | |
1239 } | |
1240 } | |
1241 } | |
1242 | |
1243 return (0); | |
1244 } | |
1245 | |
1246 | |
1247 /* Note, the index numbers of this array must match up to the numbers in | |
122 | 1248 gftp_option_type_enum in gftp.h */ |
1249 gftp_option_type_var gftp_option_types[] = { | |
227 | 1250 {gftp_config_file_read_text, gftp_config_file_write_text, |
526 | 1251 gftp_config_file_copy_text, gftp_config_file_compare_text, NULL, NULL, NULL, |
1252 NULL}, | |
227 | 1253 {gftp_config_file_read_textcombo, gftp_config_file_write_text, |
526 | 1254 gftp_config_file_copy_text, gftp_config_file_compare_text, NULL, NULL, NULL, |
1255 NULL}, | |
227 | 1256 {gftp_config_file_read_text, gftp_config_file_write_text, |
526 | 1257 gftp_config_file_copy_text, gftp_config_file_compare_text, NULL, NULL, NULL, |
1258 NULL}, | |
227 | 1259 {gftp_config_file_read_text, gftp_config_file_write_hidetext, |
526 | 1260 gftp_config_file_copy_text, gftp_config_file_compare_text, NULL, NULL, NULL, |
1261 NULL}, | |
227 | 1262 {gftp_config_file_read_int, gftp_config_file_write_int, |
1263 gftp_config_file_copy_ptr_contents, gftp_config_file_compare_int, | |
526 | 1264 NULL, NULL, NULL, NULL}, |
227 | 1265 {gftp_config_file_read_checkbox, gftp_config_file_write_int, |
1266 gftp_config_file_copy_ptr_contents, gftp_config_file_compare_int, | |
526 | 1267 NULL, NULL, NULL, NULL}, |
227 | 1268 {gftp_config_file_read_intcombo, gftp_config_file_write_intcombo, |
1269 gftp_config_file_copy_ptr_contents, gftp_config_file_compare_int, | |
526 | 1270 NULL, NULL, NULL, NULL}, |
227 | 1271 {gftp_config_file_read_float, gftp_config_file_write_float, |
1272 gftp_config_file_copy_ptr_contents, gftp_config_file_compare_float, | |
526 | 1273 NULL, NULL, NULL, NULL}, |
227 | 1274 {gftp_config_file_read_color, gftp_config_file_write_color, |
1275 gftp_config_file_copy_color, gftp_config_file_compare_color, | |
526 | 1276 NULL, NULL, NULL, NULL}, |
1277 {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}, | |
1278 {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL} | |
122 | 1279 }; |
1280 | |
1281 | |
1282 void | |
374 | 1283 gftp_lookup_global_option (const char * key, void *value) |
122 | 1284 { |
1285 gftp_config_list_vars * tmplistvar; | |
1286 gftp_config_vars * tmpconfigvar; | |
1287 | |
1288 if (gftp_global_options_htable != NULL && | |
1289 (tmpconfigvar = g_hash_table_lookup (gftp_global_options_htable, | |
1290 key)) != NULL) | |
124 | 1291 memcpy (value, &tmpconfigvar->value, sizeof (value)); |
988
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
942
diff
changeset
|
1292 else if ((tmplistvar = g_hash_table_lookup (gftp_config_list_htable, |
122 | 1293 key)) != NULL) |
124 | 1294 *(gftp_config_list_vars **) value = tmplistvar; |
122 | 1295 else |
988
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
942
diff
changeset
|
1296 #if 0 |
122 | 1297 { |
1298 fprintf (stderr, _("FATAL gFTP Error: Config option '%s' not found in global hash table\n"), key); | |
765 | 1299 exit (EXIT_FAILURE); |
122 | 1300 } |
988
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
942
diff
changeset
|
1301 #else |
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
942
diff
changeset
|
1302 value = NULL; |
63555c9744c2
remote charset should be specified by each bookmark entry.
Yoshiki Yazawa <yaz@honeyplanet.jp>
parents:
942
diff
changeset
|
1303 #endif |
122 | 1304 } |
1305 | |
1306 | |
1307 void | |
374 | 1308 gftp_lookup_request_option (gftp_request * request, const char * key, |
1309 void *value) | |
122 | 1310 { |
143 | 1311 gftp_config_vars * tmpconfigvar; |
1312 | |
1313 if (request != NULL && request->local_options_hash != NULL && | |
1314 (tmpconfigvar = g_hash_table_lookup (request->local_options_hash, | |
1315 key)) != NULL) | |
1316 memcpy (value, &tmpconfigvar->value, sizeof (value)); | |
1317 else | |
1318 gftp_lookup_global_option (key, value); | |
122 | 1319 } |
1320 | |
1321 | |
1322 void | |
374 | 1323 gftp_lookup_bookmark_option (gftp_bookmarks_var * bm, const char * key, |
1324 void *value) | |
229 | 1325 { |
1326 gftp_config_vars * tmpconfigvar; | |
1327 | |
1328 if (bm != NULL && bm->local_options_hash != NULL && | |
1329 (tmpconfigvar = g_hash_table_lookup (bm->local_options_hash, | |
1330 key)) != NULL) | |
1331 memcpy (value, &tmpconfigvar->value, sizeof (value)); | |
1332 else | |
1333 gftp_lookup_global_option (key, value); | |
1334 } | |
1335 | |
1336 | |
1337 void | |
374 | 1338 gftp_set_global_option (const char * key, const void *value) |
122 | 1339 { |
227 | 1340 gftp_config_vars * tmpconfigvar, newconfigvar; |
1341 void *nc_ptr; | |
1342 int ret; | |
129 | 1343 |
1344 if (gftp_global_options_htable != NULL && | |
1345 (tmpconfigvar = g_hash_table_lookup (gftp_global_options_htable, | |
1346 key)) != NULL) | |
201 | 1347 { |
227 | 1348 memcpy (&newconfigvar, tmpconfigvar, sizeof (newconfigvar)); |
1349 | |
1350 /* Cheap warning fix for const pointer... */ | |
1351 memcpy (&nc_ptr, &value, sizeof (nc_ptr)); | |
1352 newconfigvar.value = nc_ptr; | |
1353 newconfigvar.flags &= ~GFTP_CVARS_FLAGS_DYNMEM; | |
201 | 1354 |
516 | 1355 ret = gftp_option_types[tmpconfigvar->otype].compare_function (&newconfigvar, tmpconfigvar); |
227 | 1356 if (ret != 0) |
1357 { | |
516 | 1358 gftp_option_types[tmpconfigvar->otype].copy_function (&newconfigvar, tmpconfigvar); |
227 | 1359 gftp_configuration_changed = 1; |
1360 } | |
201 | 1361 } |
129 | 1362 else |
1363 { | |
1364 fprintf (stderr, _("FATAL gFTP Error: Config option '%s' not found in global hash table\n"), key); | |
765 | 1365 exit (EXIT_FAILURE); |
129 | 1366 } |
122 | 1367 } |
1368 | |
1369 | |
229 | 1370 static void |
1371 _gftp_set_option_value (gftp_config_vars * cv, const void * newval) | |
1372 { | |
1373 gftp_config_vars newconfigvar; | |
1374 void *nc_ptr; | |
1375 | |
1376 memcpy (&newconfigvar, cv, sizeof (newconfigvar)); | |
1377 | |
1378 /* Cheap warning fix for const pointer... */ | |
1379 memcpy (&nc_ptr, &newval, sizeof (nc_ptr)); | |
1380 newconfigvar.value = nc_ptr; | |
1381 newconfigvar.flags &= ~GFTP_CVARS_FLAGS_DYNMEM; | |
1382 | |
516 | 1383 gftp_option_types[cv->otype].copy_function (&newconfigvar, cv); |
229 | 1384 } |
1385 | |
1386 | |
122 | 1387 void |
374 | 1388 gftp_set_request_option (gftp_request * request, const char * key, |
1389 const void *value) | |
122 | 1390 { |
136 | 1391 gftp_config_vars * tmpconfigvar; |
1392 | |
1393 if (request->local_options_hash == NULL) | |
1394 request->local_options_hash = g_hash_table_new (string_hash_function, | |
1395 string_hash_compare); | |
1396 | |
1397 if ((tmpconfigvar = g_hash_table_lookup (request->local_options_hash, | |
1398 key)) != NULL) | |
229 | 1399 _gftp_set_option_value (tmpconfigvar, value); |
136 | 1400 else |
1401 { | |
143 | 1402 if (gftp_global_options_htable == NULL || |
136 | 1403 (tmpconfigvar = g_hash_table_lookup (gftp_global_options_htable, |
1404 key)) == NULL) | |
1405 { | |
1406 fprintf (stderr, _("FATAL gFTP Error: Config option '%s' not found in global hash table\n"), key); | |
765 | 1407 exit (EXIT_FAILURE); |
136 | 1408 } |
1409 | |
1410 request->num_local_options_vars++; | |
1411 request->local_options_vars = g_realloc (request->local_options_vars, | |
765 | 1412 (gulong) sizeof (gftp_config_vars) * request->num_local_options_vars); |
136 | 1413 |
229 | 1414 memcpy (&request->local_options_vars[request->num_local_options_vars - 1], tmpconfigvar, sizeof (*tmpconfigvar)); |
1415 _gftp_set_option_value (&request->local_options_vars[request->num_local_options_vars - 1], value); | |
143 | 1416 |
1417 g_hash_table_insert (request->local_options_hash, request->local_options_vars[request->num_local_options_vars - 1].key, &request->local_options_vars[request->num_local_options_vars - 1]); | |
136 | 1418 } |
122 | 1419 } |
1420 | |
1421 | |
1422 void | |
374 | 1423 gftp_set_bookmark_option (gftp_bookmarks_var * bm, const char * key, |
1424 const void *value) | |
229 | 1425 { |
1426 gftp_config_vars * tmpconfigvar, newconfigvar; | |
1427 int ret; | |
1428 | |
1429 if (bm->local_options_hash != NULL && | |
1430 (tmpconfigvar = g_hash_table_lookup (bm->local_options_hash, | |
1431 key)) != NULL) | |
1432 _gftp_set_option_value (tmpconfigvar, value); | |
1433 else | |
1434 { | |
1435 if (gftp_global_options_htable == NULL || | |
1436 (tmpconfigvar = g_hash_table_lookup (gftp_global_options_htable, | |
1437 key)) == NULL) | |
1438 { | |
1439 fprintf (stderr, _("FATAL gFTP Error: Config option '%s' not found in global hash table\n"), key); | |
765 | 1440 exit (EXIT_FAILURE); |
229 | 1441 } |
1442 | |
1443 /* Check to see if this is set to the same value as the global option. | |
1444 If so, don't add it to the bookmark preferences */ | |
1445 memcpy (&newconfigvar, tmpconfigvar, sizeof (newconfigvar)); | |
1446 memcpy (&newconfigvar.value, &value, sizeof (newconfigvar.value)); | |
1447 newconfigvar.flags &= ~GFTP_CVARS_FLAGS_DYNMEM; | |
1448 | |
516 | 1449 ret = gftp_option_types[tmpconfigvar->otype].compare_function (&newconfigvar, tmpconfigvar); |
229 | 1450 if (ret == 0) |
1451 return; | |
1452 | |
1453 if (bm->local_options_hash == NULL) | |
1454 bm->local_options_hash = g_hash_table_new (string_hash_function, | |
1455 string_hash_compare); | |
1456 | |
1457 bm->num_local_options_vars++; | |
1458 bm->local_options_vars = g_realloc (bm->local_options_vars, | |
765 | 1459 (gulong) sizeof (gftp_config_vars) * bm->num_local_options_vars); |
229 | 1460 |
1461 memcpy (&bm->local_options_vars[bm->num_local_options_vars - 1], tmpconfigvar, sizeof (*tmpconfigvar)); | |
1462 _gftp_set_option_value (&bm->local_options_vars[bm->num_local_options_vars - 1], value); | |
1463 | |
1464 g_hash_table_insert (bm->local_options_hash, bm->local_options_vars[bm->num_local_options_vars - 1].key, &bm->local_options_vars[bm->num_local_options_vars - 1]); | |
1465 } | |
1466 } | |
1467 | |
1468 | |
1469 void | |
122 | 1470 gftp_register_config_vars (gftp_config_vars * config_vars) |
1471 { | |
1472 gftp_options_list = g_list_append (gftp_options_list, config_vars); | |
1473 gftp_setup_global_options (config_vars); | |
1474 } | |
1475 | |
199 | 1476 |
1477 void | |
1478 gftp_copy_local_options (gftp_config_vars ** new_options_vars, | |
1479 GHashTable ** new_options_hash, | |
429 | 1480 int *new_num_local_options_vars, |
199 | 1481 gftp_config_vars * orig_options, |
1482 int num_local_options_vars) | |
1483 { | |
1484 int i; | |
1485 | |
429 | 1486 *new_num_local_options_vars = num_local_options_vars; |
199 | 1487 if (orig_options == NULL || num_local_options_vars == 0) |
1488 { | |
1489 *new_options_vars = NULL; | |
1490 *new_options_hash = NULL; | |
1491 return; | |
1492 } | |
1493 | |
1494 *new_options_hash = g_hash_table_new (string_hash_function, | |
1495 string_hash_compare); | |
1496 | |
942 | 1497 *new_options_vars = g_malloc0 ((gulong) sizeof (gftp_config_vars) * num_local_options_vars); |
199 | 1498 memcpy (*new_options_vars, orig_options, |
1499 sizeof (gftp_config_vars) * num_local_options_vars); | |
1500 | |
1501 for (i=0; i<num_local_options_vars; i++) | |
1502 { | |
1503 g_hash_table_insert (*new_options_hash, (*new_options_vars)[i].key, | |
1504 &(*new_options_vars)[i]); | |
207 | 1505 |
1506 (*new_options_vars)[i].value = NULL; | |
1507 (*new_options_vars)[i].flags &= ~GFTP_CVARS_FLAGS_DYNMEM; | |
199 | 1508 |
207 | 1509 gftp_option_types[(*new_options_vars)[i].otype].copy_function (&(orig_options)[i], &(*new_options_vars)[i]); |
199 | 1510 } |
1511 } | |
1512 | |
201 | 1513 |
1514 void | |
1515 gftp_config_free_options (gftp_config_vars * options_vars, | |
1516 GHashTable * options_hash, | |
1517 int num_options_vars) | |
1518 { | |
1519 int i; | |
1520 | |
1521 if (num_options_vars == 0) | |
1522 return; | |
1523 | |
1524 if (num_options_vars > 0) | |
1525 { | |
1526 /* If num_options_vars is 0, then the options was allocated with malloc */ | |
1527 | |
1528 for (i=0; i<num_options_vars; i++) | |
1529 { | |
1530 if (options_vars[i].flags & GFTP_CVARS_FLAGS_DYNMEM && | |
1531 options_vars[i].value != NULL) | |
1532 g_free (options_vars[i].value); | |
1533 } | |
1534 | |
1535 g_free (options_vars); | |
1536 } | |
1537 else if (num_options_vars < 0) | |
1538 { | |
1539 /* Otherwise we are freeing the global options */ | |
1540 | |
1541 for (i=0; options_vars[i].key != NULL; i++) | |
1542 { | |
1543 if (options_vars[i].flags & GFTP_CVARS_FLAGS_DYNMEM && | |
1544 options_vars[i].value != NULL) | |
1545 g_free (options_vars[i].value); | |
1546 | |
1547 if (options_vars[i].flags & GFTP_CVARS_FLAGS_DYNLISTMEM && | |
1548 options_vars[i].listdata != NULL) | |
1549 g_free (options_vars[i].listdata); | |
1550 } | |
1551 } | |
1552 | |
1553 if (options_hash != NULL) | |
1554 g_hash_table_destroy (options_hash); | |
1555 } | |
1556 | |
1557 | |
1558 void | |
1559 gftp_bookmarks_destroy (gftp_bookmarks_var * bookmarks) | |
1560 { | |
1561 gftp_bookmarks_var * tempentry, * delentry; | |
1562 | |
1563 if (bookmarks == NULL) | |
1564 return; | |
1565 | |
1566 tempentry = bookmarks; | |
1567 while (tempentry != NULL) | |
1568 { | |
1569 if (tempentry->children != NULL) | |
609 | 1570 tempentry = tempentry->children; |
1571 else | |
201 | 1572 { |
609 | 1573 while (tempentry->next == NULL && tempentry->prev != NULL) |
1574 { | |
1575 delentry = tempentry; | |
1576 tempentry = tempentry->prev; | |
1577 gftp_free_bookmark (delentry, 1); | |
1578 } | |
1579 | |
1580 delentry = tempentry; | |
1581 tempentry = tempentry->next; | |
1582 gftp_free_bookmark (delentry, 1); | |
201 | 1583 } |
1584 } | |
1585 } | |
1586 | |
218 | 1587 |
1588 void | |
1589 gftp_free_proxy_hosts (GList * proxy_hosts) | |
1590 { | |
1591 gftp_proxy_hosts * hosts; | |
1592 GList * templist; | |
1593 | |
1594 for (templist = proxy_hosts; | |
1595 templist != NULL; | |
1596 templist = templist->next) | |
1597 { | |
1598 hosts = templist->data; | |
1599 | |
1600 if (hosts->domain) | |
1601 g_free (hosts->domain); | |
1602 g_free (hosts); | |
1603 } | |
1604 | |
1605 g_list_free (proxy_hosts); | |
1606 } | |
1607 | |
1608 | |
1609 GList * | |
1610 gftp_copy_proxy_hosts (GList * proxy_hosts) | |
1611 { | |
1612 gftp_proxy_hosts * oldhosts, * newhosts; | |
1613 GList * templist, * new_proxy_hosts; | |
1614 | |
1615 new_proxy_hosts = NULL; | |
1616 | |
1617 if (proxy_hosts != NULL) | |
1618 { | |
1619 for (templist = proxy_hosts; | |
1620 templist != NULL; | |
1621 templist = templist->next) | |
1622 { | |
1623 oldhosts = templist->data; | |
1624 | |
1625 newhosts = g_malloc0 (sizeof (*newhosts)); | |
1626 memcpy (newhosts, oldhosts, sizeof (*newhosts)); | |
1627 | |
1628 if (oldhosts->domain) | |
1629 newhosts->domain = g_strdup (oldhosts->domain); | |
1630 | |
1631 new_proxy_hosts = g_list_append (new_proxy_hosts, newhosts); | |
1632 } | |
1633 } | |
1634 | |
1635 return (new_proxy_hosts); | |
1636 } | |
1637 |