| 1 |
/* |
| 2 |
Copyright (C) 1995-2026 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This file is part of GNU Inetutils. |
| 5 |
|
| 6 |
GNU Inetutils is free software: you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation, either version 3 of the License, or (at |
| 9 |
your option) any later version. |
| 10 |
|
| 11 |
GNU Inetutils is distributed in the hope that it will be useful, but |
| 12 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with this program. If not, see `http://www.gnu.org/licenses/'. */ |
| 18 |
|
| 19 |
/* |
| 20 |
* Copyright (c) 1985, 1993, 1994 |
| 21 |
* The Regents of the University of California. All rights reserved. |
| 22 |
* |
| 23 |
* Redistribution and use in source and binary forms, with or without |
| 24 |
* modification, are permitted provided that the following conditions |
| 25 |
* are met: |
| 26 |
* 1. Redistributions of source code must retain the above copyright |
| 27 |
* notice, this list of conditions and the following disclaimer. |
| 28 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 29 |
* notice, this list of conditions and the following disclaimer in the |
| 30 |
* documentation and/or other materials provided with the distribution. |
| 31 |
* 3. Neither the name of the University nor the names of its contributors |
| 32 |
* may be used to endorse or promote products derived from this software |
| 33 |
* without specific prior written permission. |
| 34 |
* |
| 35 |
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 36 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 37 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 38 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 39 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 40 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 41 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 42 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 43 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 44 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 45 |
* SUCH DAMAGE. |
| 46 |
*/ |
| 47 |
|
| 48 |
#include <config.h> |
| 49 |
|
| 50 |
#include <ctype.h> |
| 51 |
#include <limits.h> |
| 52 |
#include <stdio.h> |
| 53 |
#include <stdlib.h> |
| 54 |
#include <string.h> |
| 55 |
|
| 56 |
#include "ftp_var.h" |
| 57 |
|
| 58 |
/* Increase allocated length of `*startl' with `add' |
| 59 |
* beyond `*track', should present length not suffice. |
| 60 |
* `*track' points to last character in use. All of |
| 61 |
* the values `*start', `*track', * and `*size' are |
| 62 |
* updated during a successful reallocation. |
| 63 |
* Return zero on success. |
| 64 |
*/ |
| 65 |
static int |
| 66 |
lengthen (char **start, char **track, size_t *size, size_t add) |
| 67 |
{ |
| 68 |
char *new; |
| 69 |
size_t offset = (size_t) (*track - *start); |
| 70 |
|
| 71 |
if (*track < *start) |
| 72 |
return EXIT_FAILURE; /* Sanity check. */ |
| 73 |
|
| 74 |
if (offset + add < *size) |
| 75 |
return EXIT_SUCCESS; /* Sufficient allocation. */ |
| 76 |
|
| 77 |
new = realloc (*start, *size + add); |
| 78 |
if (!new) |
| 79 |
return EXIT_FAILURE; /* Parameters are unchanged here. */ |
| 80 |
|
| 81 |
*start = new; |
| 82 |
*size += add; |
| 83 |
*track = *start + offset; |
| 84 |
|
| 85 |
return EXIT_SUCCESS; |
| 86 |
} |
| 87 |
|
| 88 |
#define MAX_MACRO_NESTING_DEPTH 1000 |
| 89 |
|
| 90 |
void |
| 91 |
domacro (int argc, char *argv[]) |
| 92 |
{ |
| 93 |
static int nesting_depth = 0; |
| 94 |
int i, j, count = 2, loopflg = 0, allocflg = 0; |
| 95 |
char *cp1, *cp2; |
| 96 |
char *line2; /* Saved original of `line'. */ |
| 97 |
size_t line2len; /* Its allocated length. */ |
| 98 |
struct cmd *c; |
| 99 |
|
| 100 |
if (argc < 2 && !another (&argc, &argv, "macro name")) |
| 101 |
{ |
| 102 |
printf ("Usage: %s macro_name.\n", argv[0]); |
| 103 |
code = -1; |
| 104 |
return; |
| 105 |
} |
| 106 |
for (i = 0; i < macnum; ++i) |
| 107 |
{ |
| 108 |
if (!strncmp (argv[1], macros[i].mac_name, sizeof (macros[i].mac_name))) |
| 109 |
{ |
| 110 |
break; |
| 111 |
} |
| 112 |
} |
| 113 |
if (i == macnum) |
| 114 |
{ |
| 115 |
printf ("'%s' macro not found.\n", argv[1]); |
| 116 |
code = -1; |
| 117 |
return; |
| 118 |
} |
| 119 |
if (nesting_depth < 0) |
| 120 |
{ |
| 121 |
printf ("?Corrupted macro execution state.\n"); |
| 122 |
code = -1; |
| 123 |
return; |
| 124 |
} |
| 125 |
|
| 126 |
line2 = line; |
| 127 |
line2len = linelen; |
| 128 |
|
| 129 |
/* Generate a replacement for `line' to be used during |
| 130 |
* macro evaluation. There might appear some intr(), |
| 131 |
* so care must be taken before changing `line'. |
| 132 |
* The original is available as LINE2. |
| 133 |
* |
| 134 |
* Initially allocate an amount sufficient for |
| 135 |
* storing a copy of the original `line', which |
| 136 |
* is repeatedly reused once for each text line |
| 137 |
* of the stored macro definition. |
| 138 |
*/ |
| 139 |
cp2 = malloc (strlen (line2) + 2); |
| 140 |
if (!cp2) |
| 141 |
{ |
| 142 |
printf ("System refused resources for macro '%s'.\n", argv[1]); |
| 143 |
line = line2; |
| 144 |
linelen = line2len; |
| 145 |
code = -1; |
| 146 |
return; |
| 147 |
} |
| 148 |
|
| 149 |
linelen = strlen (line2) + 2; |
| 150 |
line = cp2; |
| 151 |
*line = '\0'; |
| 152 |
|
| 153 |
if (++nesting_depth > MAX_MACRO_NESTING_DEPTH) |
| 154 |
{ |
| 155 |
printf ("?Maximum macro nesting depth reached, aborting macro.\n"); |
| 156 |
goto end_exec; |
| 157 |
} |
| 158 |
|
| 159 |
do |
| 160 |
{ |
| 161 |
cp1 = macros[i].mac_start; |
| 162 |
while (cp1 != macros[i].mac_end) |
| 163 |
{ |
| 164 |
/* Skip initial white space on each line of input. */ |
| 165 |
while (isspace (*cp1)) |
| 166 |
{ |
| 167 |
cp1++; |
| 168 |
} |
| 169 |
/* Translate a line of text from macro definition |
| 170 |
* and put it in `line'. This global variable is |
| 171 |
* referenced by some parsing functions, so the |
| 172 |
* translation target cannot be changed easily. |
| 173 |
*/ |
| 174 |
cp2 = line; |
| 175 |
while (*cp1 != '\0') |
| 176 |
{ |
| 177 |
/* Usually two characters suffice. |
| 178 |
* This covers the default case below. |
| 179 |
*/ |
| 180 |
if (lengthen (&line, &cp2, &linelen, 2)) |
| 181 |
{ |
| 182 |
allocflg = 1; |
| 183 |
goto end_exec; |
| 184 |
} |
| 185 |
switch (*cp1) |
| 186 |
{ |
| 187 |
case '\\': /* Escaping character. */ |
| 188 |
*cp2++ = *++cp1; |
| 189 |
break; |
| 190 |
case '$': /* Substitution. */ |
| 191 |
if (isdigit (*(cp1 + 1))) |
| 192 |
{ |
| 193 |
/* Argument expansion. */ |
| 194 |
j = 0; |
| 195 |
while (isdigit (*++cp1)) |
| 196 |
{ |
| 197 |
if (j <= (INT_MAX - 9) / 10) |
| 198 |
j = 10 * j + *cp1 - '0'; |
| 199 |
} |
| 200 |
cp1--; |
| 201 |
if (argc - 2 >= j && j >= 0) |
| 202 |
{ |
| 203 |
if (lengthen (&line, &cp2, &linelen, |
| 204 |
strlen (argv[j + 1]) + 2)) |
| 205 |
{ |
| 206 |
allocflg = 1; |
| 207 |
goto end_exec; |
| 208 |
} |
| 209 |
strcpy (cp2, argv[j + 1]); |
| 210 |
cp2 += strlen (argv[j + 1]); |
| 211 |
} |
| 212 |
break; |
| 213 |
} |
| 214 |
if (*(cp1 + 1) == 'i') |
| 215 |
{ |
| 216 |
/* The loop counter "$i" was detected. */ |
| 217 |
loopflg = 1; |
| 218 |
cp1++; /* Back to last used char. */ |
| 219 |
if (count < argc) |
| 220 |
{ |
| 221 |
if (lengthen (&line, &cp2, &linelen, |
| 222 |
strlen (argv[count]) + 2)) |
| 223 |
{ |
| 224 |
allocflg = 1; |
| 225 |
goto end_exec; |
| 226 |
} |
| 227 |
strcpy (cp2, argv[count]); |
| 228 |
cp2 += strlen (argv[count]); |
| 229 |
} |
| 230 |
break; |
| 231 |
} |
| 232 |
/* Intentional fall through, since no acceptable |
| 233 |
* use of '$' was detected. Present input is the |
| 234 |
* dollar sign. |
| 235 |
*/ |
| 236 |
/* FALLTHROUGH */ |
| 237 |
default: |
| 238 |
*cp2++ = *cp1; /* Copy present character. */ |
| 239 |
break; |
| 240 |
} |
| 241 |
/* Advance to next usable character. */ |
| 242 |
if (*cp1 != '\0') |
| 243 |
cp1++; |
| 244 |
} |
| 245 |
*cp2 = '\0'; |
| 246 |
makeargv (); |
| 247 |
if (margv[0] == NULL) |
| 248 |
{ |
| 249 |
nesting_depth--; |
| 250 |
return; |
| 251 |
} |
| 252 |
c = getcmd (margv[0]); |
| 253 |
|
| 254 |
if (c == (struct cmd *) -1) |
| 255 |
{ |
| 256 |
printf ("?Ambiguous command: '%s'.\n", margv[0]); |
| 257 |
code = -1; |
| 258 |
loopflg = 0; |
| 259 |
break; |
| 260 |
} |
| 261 |
else if (c == 0) |
| 262 |
{ |
| 263 |
printf ("?Invalid command: '%s'.\n", margv[0]); |
| 264 |
code = -1; |
| 265 |
loopflg = 0; |
| 266 |
break; |
| 267 |
} |
| 268 |
else if (c->c_conn && !connected) |
| 269 |
{ |
| 270 |
printf ("Not connected, needed for '%s'.\n", margv[0]); |
| 271 |
code = -1; |
| 272 |
loopflg = 0; |
| 273 |
break; |
| 274 |
} |
| 275 |
else |
| 276 |
{ |
| 277 |
if (verbose) |
| 278 |
printf ("%s\n", line); |
| 279 |
(*c->c_handler) (margc, margv); |
| 280 |
if (bell && c->c_bell) |
| 281 |
putchar ('\007'); |
| 282 |
|
| 283 |
/* The arguments set at the time of invoking |
| 284 |
* the macro must be recovered, to be used |
| 285 |
* in parsing next line of macro definition. |
| 286 |
* |
| 287 |
* Executing a macro line can change "line" |
| 288 |
* to no longer provide sufficient space for |
| 289 |
* the saved line2 contents. |
| 290 |
*/ |
| 291 |
if (strlen (line2) >= linelen) |
| 292 |
{ |
| 293 |
char *tmp = realloc (line, strlen (line2) + 1); |
| 294 |
if (tmp == NULL) |
| 295 |
{ |
| 296 |
allocflg = 1; |
| 297 |
goto end_exec; |
| 298 |
} |
| 299 |
line = tmp; |
| 300 |
linelen = strlen (line2) + 1; |
| 301 |
} |
| 302 |
strcpy (line, line2); |
| 303 |
makeargv (); /* Get the arguments. */ |
| 304 |
argc = margc; |
| 305 |
argv = margv; |
| 306 |
} |
| 307 |
if (cp1 != macros[i].mac_end) |
| 308 |
cp1++; |
| 309 |
} |
| 310 |
} |
| 311 |
while (loopflg && ++count < argc); |
| 312 |
|
| 313 |
end_exec: |
| 314 |
if (allocflg) |
| 315 |
{ |
| 316 |
printf ("Memory allocation failed for macro '%s'.\n", argv[1]); |
| 317 |
code = -1; |
| 318 |
} |
| 319 |
free (line); |
| 320 |
line = line2; |
| 321 |
linelen = line2len; |
| 322 |
nesting_depth--; |
| 323 |
} |