updates for 5.1-pre1, reorganized wget list, and some minor nitpicks
[automated_linux_from_scratch.git] / nALFS / src / handlers / link.c
blobef3504eb28e3275fdcf13973eee1454408f6ad08
1 /*
2 * link.c - Handler.
3 *
4 * Copyright (C) 2001-2003
5 *
6 * Neven Has <haski@sezampro.yu>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <stdlib.h>
25 #include <string.h>
26 #include <errno.h>
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
32 #define MODULE_NAME link
33 #include <nALFS.h>
35 #include "handlers.h"
36 #include "utility.h"
37 #include "nprint.h"
38 #include "parser.h"
39 #include "backend.h"
42 #define El_link_target(el) alloc_trimmed_param_value("target", el)
43 #define El_link_name(el) alloc_trimmed_param_value("name", el)
46 #if HANDLER_SYNTAX_2_0
48 static const char *link_parameters_ver2[] =
49 { "options", "base", "target", "name", NULL };
50 // char *HANDLER_SYMBOL(attributes)[] = { "type", NULL };
52 static int link_main_ver2(element_s *el)
54 int status;
55 int force = option_exists("force", el);
56 char *type = attr_value("type", el);
57 char *base;
58 char *target;
59 char *link_name;
60 char *command = NULL;
61 char *message = NULL;
64 if ((target = El_link_target(el)) == NULL) {
65 Nprint_h_err("No source files specified.");
66 return -1;
69 link_name = El_link_name(el);
71 base = alloc_base_dir(el);
73 if (change_current_dir(base)) {
74 xfree(base);
75 xfree(target);
76 xfree(link_name);
77 return -1;
80 if (type == NULL || strcmp(type, "symbolic") == 0) {
81 append_str(&command, "ln -s");
82 append_str(&message, "Creating symbolic link in ");
84 } else if (strcmp(type, "hard") == 0) {
85 append_str(&command, "ln");
86 append_str(&message, "Creating hard link in ");
88 } else {
89 Nprint_h_warn("Unknown link type (%s), using symbolic.", type);
90 append_str(&command, "ln -s");
91 append_str(&message, "Creating symbolic link in ");
94 append_str(&message, base);
96 if (force) {
97 append_str(&command, " -f");
98 append_str(&message, " (force):");
100 } else {
101 append_str(&message, ":");
104 Nprint_h("%s", message);
106 if (link_name) {
107 Nprint_h(" %s -> %s", link_name, target);
108 } else {
109 Nprint_h(" %s", target);
112 append_str(&command, " ");
113 append_str(&command, target);
115 if (link_name) {
116 append_str(&command, " ");
117 append_str(&command, link_name);
120 if ((status = execute_command("%s", command))) {
121 Nprint_h_err("Executing \"%s\" in \"%s\" failed.",
122 command, base);
125 xfree(base);
126 xfree(target);
127 xfree(link_name);
128 xfree(command);
129 xfree(message);
131 return status;
134 #endif /* HANDLER_SYNTAX_2_0 */
137 #if HANDLER_SYNTAX_3_0 || HANDLER_SYNTAX_3_1 || HANDLER_SYNTAX_3_2
139 static const char *link_parameters_ver3[] =
140 { "option", "target", "name", NULL };
141 // char *HANDLER_SYMBOL(attributes)[] = { "type", "base", NULL };
143 static int link_main_ver3(element_s *el)
145 int options[2], force, no_dereference;
146 int status;
147 char *type;
148 char *base;
149 char *targets = NULL;
150 char *link_name;
151 char *command = NULL;
152 char *message = NULL;
153 element_s *p;
156 /* Read all <option>s. */
157 check_options(2, options, "force no-dereference", el);
158 force = options[0];
159 no_dereference = options[1];
161 link_name = alloc_trimmed_param_value("name", el);
163 base = alloc_base_dir_new(el);
164 if (change_current_dir(base)) {
165 xfree(base);
166 xfree(link_name);
167 return -1;
170 type = attr_value("type", el);
172 if (type == NULL || strcmp(type, "symbolic") == 0) {
173 append_str(&message, "Creating symbolic link in ");
174 append_str(&command, "ln -s");
176 } else if (strcmp(type, "hard") == 0) {
177 append_str(&message, "Creating hard link in ");
178 append_str(&command, "ln");
180 } else {
181 Nprint_h_warn("Unknown link type (%s), using symbolic.", type);
182 append_str(&message, "Creating symbolic link in ");
183 append_str(&command, "ln -s");
186 append_str(&message, base);
188 if (force) {
189 append_str(&message, " (force)");
190 append_str(&command, " -f");
193 if (no_dereference) {
194 append_str(&message, " (no_dereference)");
195 append_str(&command, " -n");
198 append_str(&message, ": ");
200 if (link_name) {
201 append_str(&message, link_name);
204 /* Concatenate all targets in "targets". */
205 for (p = first_param("target", el); p; p = next_param(p)) {
206 char *target;
209 if ((target = alloc_trimmed_str(p->content)) == NULL) {
210 Nprint_h_warn("No target specified.");
211 continue;
214 if (targets != NULL) {
215 append_str(&targets, " ");
218 append_str(&targets, target);
220 xfree(target);
223 if (targets) {
224 append_str(&command, " ");
225 append_str(&command, targets);
227 if (link_name) {
228 append_str(&command, " ");
229 append_str(&command, link_name);
232 Nprint_h("%s", message);
233 Nprint_h(" %s", targets);
235 if ((status = execute_command("%s", command))) {
236 Nprint_h_err("Executing \"%s\" in %s failed.",
237 command, base);
240 } else {
241 Nprint_h_err("No target for link specified.");
242 status = -1;
245 xfree(base);
246 xfree(link_name);
247 xfree(command);
248 xfree(message);
250 return status;
253 #endif /* HANDLER_SYNTAX_3_0 || HANDLER_SYNTAX_3_1 || HANDLER_SYNTAX_3_2 */
257 * Handlers' information.
260 handler_info_s HANDLER_SYMBOL(info)[] = {
261 #if HANDLER_SYNTAX_2_0
263 .name = "link",
264 .description = "Link",
265 .syntax_version = "2.0",
266 .parameters = link_parameters_ver2,
267 .main = link_main_ver2,
268 .type = 0,
269 .alloc_data = NULL,
270 .is_action = 1,
271 .priority = 0
273 #endif
274 #if HANDLER_SYNTAX_3_0
276 .name = "link",
277 .description = "Link",
278 .syntax_version = "3.0",
279 .parameters = link_parameters_ver3,
280 .main = link_main_ver3,
281 .type = 0,
282 .alloc_data = NULL,
283 .is_action = 1,
284 .priority = 0
286 #endif
287 #if HANDLER_SYNTAX_3_1
289 .name = "link",
290 .description = "Link",
291 .syntax_version = "3.1",
292 .parameters = link_parameters_ver3,
293 .main = link_main_ver3,
294 .type = 0,
295 .alloc_data = NULL,
296 .is_action = 1,
297 .priority = 0
299 #endif
300 #if HANDLER_SYNTAX_3_2
302 .name = "link",
303 .description = "Link",
304 .syntax_version = "3.2",
305 .parameters = link_parameters_ver3,
306 .main = link_main_ver3,
307 .type = 0,
308 .alloc_data = NULL,
309 .is_action = 1,
310 .priority = 0
312 #endif
314 NULL, NULL, NULL, NULL, NULL, 0, NULL, 0, 0