blob: e111b8ba98ad7c276621aca949ccd31ba5dc12ed [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Kumar Galaaed83342008-02-14 20:44:42 -06002/*
3 * Copyright 2008 Freescale Semiconductor, Inc.
Wolfgang Denkb04ae782013-03-23 23:50:34 +00004 * Copyright 2013 Wolfgang Denk <wd@denx.de>
Kumar Galaaed83342008-02-14 20:44:42 -06005 */
6
7/*
8 * This file provides a shell like 'expr' function to return.
9 */
10
Kumar Galaaed83342008-02-14 20:44:42 -060011#include <config.h>
12#include <command.h>
Roland Gaudig2c9e7c22021-07-23 12:29:21 +000013#include <ctype.h>
Simon Glass313112a2019-08-01 09:46:46 -060014#include <env.h>
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
Simon Glass580a7a52020-11-01 14:15:44 -070016#include <malloc.h>
Joe Hershberger9d3f8962015-05-11 13:53:13 -050017#include <mapmem.h>
Tom Rinidec7ea02024-05-20 13:35:03 -060018#include <vsprintf.h>
19#include <linux/errno.h>
Simon Glass580a7a52020-11-01 14:15:44 -070020#include <linux/sizes.h>
Roland Gaudig2c9e7c22021-07-23 12:29:21 +000021#include "printf.h"
22
23#define MAX_STR_LEN 128
Kumar Galaaed83342008-02-14 20:44:42 -060024
Simon Glass00547af2020-11-01 14:15:43 -070025/**
26 * struct expr_arg: Holds an argument to an expression
27 *
28 * @ival: Integer value (if width is not CMD_DATA_SIZE_STR)
Simon Glass580a7a52020-11-01 14:15:44 -070029 * @sval: String value (if width is CMD_DATA_SIZE_STR)
Simon Glass00547af2020-11-01 14:15:43 -070030 */
31struct expr_arg {
Simon Glass580a7a52020-11-01 14:15:44 -070032 union {
33 ulong ival;
34 char *sval;
35 };
Simon Glass00547af2020-11-01 14:15:43 -070036};
37
38static int get_arg(char *s, int w, struct expr_arg *argp)
Frans Meulenbroeks2347d4e2010-02-26 14:00:19 +010039{
Simon Glass00547af2020-11-01 14:15:43 -070040 struct expr_arg arg;
41
Wolfgang Denk9a183d22010-06-23 20:50:54 +020042 /*
Joe Hershberger9d3f8962015-05-11 13:53:13 -050043 * If the parameter starts with a '*' then assume it is a pointer to
44 * the value we want.
Frans Meulenbroeks2347d4e2010-02-26 14:00:19 +010045 */
Frans Meulenbroeks2347d4e2010-02-26 14:00:19 +010046 if (s[0] == '*') {
Joe Hershberger9d3f8962015-05-11 13:53:13 -050047 ulong *p;
48 ulong addr;
49 ulong val;
Simon Glass580a7a52020-11-01 14:15:44 -070050 int len;
51 char *str;
Joe Hershberger9d3f8962015-05-11 13:53:13 -050052
Simon Glass3ff49ec2021-07-24 09:03:29 -060053 addr = hextoul(&s[1], NULL);
Frans Meulenbroeks2347d4e2010-02-26 14:00:19 +010054 switch (w) {
Joe Hershberger9d3f8962015-05-11 13:53:13 -050055 case 1:
56 p = map_sysmem(addr, sizeof(uchar));
57 val = (ulong)*(uchar *)p;
58 unmap_sysmem(p);
Simon Glass00547af2020-11-01 14:15:43 -070059 arg.ival = val;
60 break;
Joe Hershberger9d3f8962015-05-11 13:53:13 -050061 case 2:
62 p = map_sysmem(addr, sizeof(ushort));
63 val = (ulong)*(ushort *)p;
64 unmap_sysmem(p);
Simon Glass00547af2020-11-01 14:15:43 -070065 arg.ival = val;
66 break;
Simon Glass580a7a52020-11-01 14:15:44 -070067 case CMD_DATA_SIZE_STR:
68 p = map_sysmem(addr, SZ_64K);
69
70 /* Maximum string length of 64KB plus terminator */
71 len = strnlen((char *)p, SZ_64K) + 1;
72 str = malloc(len);
73 if (!str) {
74 printf("Out of memory\n");
75 return -ENOMEM;
76 }
77 memcpy(str, p, len);
78 str[len - 1] = '\0';
79 unmap_sysmem(p);
80 arg.sval = str;
81 break;
Frans Meulenbroeks2347d4e2010-02-26 14:00:19 +010082 case 4:
Simon Glass7c19edf2020-11-01 14:15:37 -070083 p = map_sysmem(addr, sizeof(u32));
84 val = *(u32 *)p;
85 unmap_sysmem(p);
Simon Glass00547af2020-11-01 14:15:43 -070086 arg.ival = val;
87 break;
Joe Hershberger9d3f8962015-05-11 13:53:13 -050088 default:
89 p = map_sysmem(addr, sizeof(ulong));
90 val = *p;
91 unmap_sysmem(p);
Simon Glass00547af2020-11-01 14:15:43 -070092 arg.ival = val;
93 break;
Frans Meulenbroeks2347d4e2010-02-26 14:00:19 +010094 }
95 } else {
Simon Glass580a7a52020-11-01 14:15:44 -070096 if (w == CMD_DATA_SIZE_STR)
97 return -EINVAL;
Simon Glass3ff49ec2021-07-24 09:03:29 -060098 arg.ival = hextoul(s, NULL);
Frans Meulenbroeks2347d4e2010-02-26 14:00:19 +010099 }
Simon Glass00547af2020-11-01 14:15:43 -0700100 *argp = arg;
101
102 return 0;
Frans Meulenbroeks2347d4e2010-02-26 14:00:19 +0100103}
104
Wolfgang Denkb04ae782013-03-23 23:50:34 +0000105#ifdef CONFIG_REGEX
106
107#include <slre.h>
108
Wolfgang Denkb04ae782013-03-23 23:50:34 +0000109/*
110 * memstr - Find the first substring in memory
111 * @s1: The string to be searched
112 * @s2: The string to search for
113 *
114 * Similar to and based on strstr(),
115 * but strings do not need to be NUL terminated.
116 */
117static char *memstr(const char *s1, int l1, const char *s2, int l2)
118{
119 if (!l2)
120 return (char *)s1;
121
122 while (l1 >= l2) {
123 l1--;
124 if (!memcmp(s1, s2, l2))
125 return (char *)s1;
126 s1++;
127 }
128 return NULL;
129}
130
Simon Glassb8e9f2d2020-11-01 14:15:39 -0700131/**
132 * substitute() - Substitute part of one string with another
133 *
134 * This updates @string so that the first occurrence of @old is replaced with
135 * @new
136 *
137 * @string: String buffer containing string to update at the start
138 * @slen: Pointer to current string length, updated on success
139 * @ssize: Size of string buffer
140 * @old: Old string to find in the buffer (no terminator needed)
141 * @olen: Length of @old excluding terminator
142 * @new: New string to replace @old with
143 * @nlen: Length of @new excluding terminator
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100144 * Return: pointer to immediately after the copied @new in @string, or NULL if
Simon Glassb8e9f2d2020-11-01 14:15:39 -0700145 * no replacement took place
146 */
147static char *substitute(char *string, int *slen, int ssize,
148 const char *old, int olen, const char *new, int nlen)
Wolfgang Denkb04ae782013-03-23 23:50:34 +0000149{
150 char *p = memstr(string, *slen, old, olen);
151
152 if (p == NULL)
153 return NULL;
154
155 debug("## Match at pos %ld: match len %d, subst len %d\n",
156 (long)(p - string), olen, nlen);
157
158 /* make sure replacement matches */
159 if (*slen + nlen - olen > ssize) {
160 printf("## error: substitution buffer overflow\n");
161 return NULL;
162 }
163
164 /* move tail if needed */
165 if (olen != nlen) {
166 int tail, len;
167
168 len = (olen > nlen) ? olen : nlen;
169
170 tail = ssize - (p + len - string);
171
172 debug("## tail len %d\n", tail);
173
174 memmove(p + nlen, p + olen, tail);
175 }
176
Simon Glassb8e9f2d2020-11-01 14:15:39 -0700177 /* insert substitute */
Wolfgang Denkb04ae782013-03-23 23:50:34 +0000178 memcpy(p, new, nlen);
179
180 *slen += nlen - olen;
181
182 return p + nlen;
183}
184
Simon Glass00e44192020-11-01 14:15:40 -0700185int setexpr_regex_sub(char *data, uint data_size, char *nbuf, uint nbuf_size,
186 const char *r, const char *s, bool global)
Wolfgang Denkb04ae782013-03-23 23:50:34 +0000187{
188 struct slre slre;
Wolfgang Denkb04ae782013-03-23 23:50:34 +0000189 char *datap = data;
Wolfgang Denkb04ae782013-03-23 23:50:34 +0000190 int res, len, nlen, loop;
191
Wolfgang Denkb04ae782013-03-23 23:50:34 +0000192 if (slre_compile(&slre, r) == 0) {
193 printf("Error compiling regex: %s\n", slre.err_str);
194 return 1;
195 }
196
Simon Glassb8e9f2d2020-11-01 14:15:39 -0700197 len = strlen(data);
Wolfgang Denkb04ae782013-03-23 23:50:34 +0000198 for (loop = 0;; loop++) {
199 struct cap caps[slre.num_caps + 2];
Wolfgang Denkb04ae782013-03-23 23:50:34 +0000200 const char *old;
201 char *np;
202 int i, olen;
203
204 (void) memset(caps, 0, sizeof(caps));
205
Simon Glass7a114862020-11-01 14:15:42 -0700206 res = slre_match(&slre, datap, len - (datap - data), caps);
Wolfgang Denkb04ae782013-03-23 23:50:34 +0000207
208 debug("Result: %d\n", res);
209
Simon Glass7a114862020-11-01 14:15:42 -0700210 for (i = 0; i <= slre.num_caps; i++) {
Wolfgang Denkb04ae782013-03-23 23:50:34 +0000211 if (caps[i].len > 0) {
212 debug("Substring %d: [%.*s]\n", i,
213 caps[i].len, caps[i].ptr);
214 }
215 }
216
217 if (res == 0) {
218 if (loop == 0) {
Łukasz Stelmach848d73c2023-08-24 08:10:25 +0200219 debug("%s: No match\n", data);
Wolfgang Denkb04ae782013-03-23 23:50:34 +0000220 } else {
Massimiliano Minella847f69c2024-02-08 15:58:27 +0100221 debug("## MATCH ## %s\n", data);
Wolfgang Denkb04ae782013-03-23 23:50:34 +0000222 }
Massimiliano Minella847f69c2024-02-08 15:58:27 +0100223 break;
Wolfgang Denkb04ae782013-03-23 23:50:34 +0000224 }
225
Simon Glassb8e9f2d2020-11-01 14:15:39 -0700226 if (!s)
Wolfgang Denkb04ae782013-03-23 23:50:34 +0000227 return 1;
Wolfgang Denkb04ae782013-03-23 23:50:34 +0000228
229 old = caps[0].ptr;
230 olen = caps[0].len;
Simon Glass28ae25a2020-11-01 14:15:41 -0700231 nlen = strlen(s);
Wolfgang Denkb04ae782013-03-23 23:50:34 +0000232
Simon Glassb8e9f2d2020-11-01 14:15:39 -0700233 if (nlen + 1 >= nbuf_size) {
Wolfgang Denkb04ae782013-03-23 23:50:34 +0000234 printf("## error: pattern buffer overflow: have %d, need %d\n",
Simon Glassb8e9f2d2020-11-01 14:15:39 -0700235 nbuf_size, nlen + 1);
Wolfgang Denkb04ae782013-03-23 23:50:34 +0000236 return 1;
237 }
238 strcpy(nbuf, s);
239
240 debug("## SUBST(1) ## %s\n", nbuf);
241
242 /*
243 * Handle back references
244 *
245 * Support for \0 ... \9, where \0 is the
246 * whole matched pattern (similar to &).
247 *
248 * Implementation is a bit simpleminded as
249 * backrefs are substituted sequentially, one
250 * by one. This will lead to somewhat
251 * unexpected results if the replacement
252 * strings contain any \N strings then then
253 * may get substitued, too. We accept this
254 * restriction for the sake of simplicity.
255 */
256 for (i = 0; i < 10; ++i) {
257 char backref[2] = {
258 '\\',
259 '0',
260 };
261
262 if (caps[i].len == 0)
263 break;
264
265 backref[1] += i;
266
267 debug("## BACKREF %d: replace \"%.*s\" by \"%.*s\" in \"%s\"\n",
268 i,
269 2, backref,
270 caps[i].len, caps[i].ptr,
271 nbuf);
272
273 for (np = nbuf;;) {
274 char *p = memstr(np, nlen, backref, 2);
275
276 if (p == NULL)
277 break;
278
279 np = substitute(np, &nlen,
Simon Glass7a114862020-11-01 14:15:42 -0700280 nbuf_size - (np - nbuf),
Wolfgang Denkb04ae782013-03-23 23:50:34 +0000281 backref, 2,
282 caps[i].ptr, caps[i].len);
283
284 if (np == NULL)
285 return 1;
286 }
287 }
288 debug("## SUBST(2) ## %s\n", nbuf);
289
Simon Glass7a114862020-11-01 14:15:42 -0700290 datap = substitute(datap, &len, data_size - (datap - data),
291 old, olen, nbuf, nlen);
Wolfgang Denkb04ae782013-03-23 23:50:34 +0000292
293 if (datap == NULL)
294 return 1;
295
296 debug("## REMAINDER: %s\n", datap);
297
298 debug("## RESULT: %s\n", data);
299
300 if (!global)
301 break;
302 }
Simon Glass6a38e412017-08-03 12:22:09 -0600303 debug("## FINAL (now env_set()) : %s\n", data);
Wolfgang Denkb04ae782013-03-23 23:50:34 +0000304
Simon Glassb8e9f2d2020-11-01 14:15:39 -0700305 return 0;
306}
307
308#define SLRE_BUFSZ 16384
309#define SLRE_PATSZ 4096
310
311/*
312 * Perform regex operations on a environment variable
313 *
314 * Returns 0 if OK, 1 in case of errors.
315 */
316static int regex_sub_var(const char *name, const char *r, const char *s,
317 const char *t, int global)
318{
319 struct slre slre;
320 char data[SLRE_BUFSZ];
321 char nbuf[SLRE_PATSZ];
322 const char *value;
323 int len;
324 int ret;
325
326 if (!name)
327 return 1;
328
329 if (slre_compile(&slre, r) == 0) {
330 printf("Error compiling regex: %s\n", slre.err_str);
331 return 1;
332 }
333
334 if (!t) {
335 value = env_get(name);
336 if (!value) {
337 printf("## Error: variable \"%s\" not defined\n", name);
338 return 1;
339 }
340 t = value;
341 }
342
343 debug("REGEX on %s=%s\n", name, t);
344 debug("REGEX=\"%s\", SUBST=\"%s\", GLOBAL=%d\n", r, s ? s : "<NULL>",
345 global);
346
347 len = strlen(t);
348 if (len + 1 > SLRE_BUFSZ) {
349 printf("## error: subst buffer overflow: have %d, need %d\n",
350 SLRE_BUFSZ, len + 1);
351 return 1;
352 }
353
354 strcpy(data, t);
355
Simon Glass00e44192020-11-01 14:15:40 -0700356 ret = setexpr_regex_sub(data, SLRE_BUFSZ, nbuf, SLRE_PATSZ, r, s,
357 global);
Simon Glassb8e9f2d2020-11-01 14:15:39 -0700358 if (ret)
359 return 1;
360
Łukasz Stelmach848d73c2023-08-24 08:10:25 +0200361 debug("%s=%s\n", name, data);
Wolfgang Denkb04ae782013-03-23 23:50:34 +0000362
Simon Glass6a38e412017-08-03 12:22:09 -0600363 return env_set(name, data);
Wolfgang Denkb04ae782013-03-23 23:50:34 +0000364}
365#endif
366
Simon Glassed38aef2020-05-10 11:40:03 -0600367static int do_setexpr(struct cmd_tbl *cmdtp, int flag, int argc,
368 char *const argv[])
Kumar Galaaed83342008-02-14 20:44:42 -0600369{
Simon Glass00547af2020-11-01 14:15:43 -0700370 struct expr_arg aval, bval;
Simon Glass103789d2013-02-24 17:33:22 +0000371 ulong value;
Simon Glass580a7a52020-11-01 14:15:44 -0700372 int ret = 0;
Frans Meulenbroeks2347d4e2010-02-26 14:00:19 +0100373 int w;
Kumar Galaaed83342008-02-14 20:44:42 -0600374
Wolfgang Denkb04ae782013-03-23 23:50:34 +0000375 /*
Roland Gaudig2c9e7c22021-07-23 12:29:21 +0000376 * We take 3, 5, or 6 arguments, except fmt operation, which
377 * takes 4 to 8 arguments (limited by _maxargs):
Wolfgang Denkb04ae782013-03-23 23:50:34 +0000378 * 3 : setexpr name value
379 * 5 : setexpr name val1 op val2
380 * setexpr name [g]sub r s
381 * 6 : setexpr name [g]sub r s t
Roland Gaudig2c9e7c22021-07-23 12:29:21 +0000382 * setexpr name fmt format [val1] [val2] [val3] [val4]
Wolfgang Denkb04ae782013-03-23 23:50:34 +0000383 */
384
Roland Gaudig2c9e7c22021-07-23 12:29:21 +0000385 if (argc < 3)
Simon Glassa06dfc72011-12-10 08:44:01 +0000386 return CMD_RET_USAGE;
Kumar Galaaed83342008-02-14 20:44:42 -0600387
Frans Meulenbroeks2347d4e2010-02-26 14:00:19 +0100388 w = cmd_get_data_size(argv[0], 4);
389
Simon Glass00547af2020-11-01 14:15:43 -0700390 if (get_arg(argv[2], w, &aval))
391 return CMD_RET_FAILURE;
Joe Hershberger0e640e62012-11-01 16:21:14 +0000392
Roland Gaudig2c9e7c22021-07-23 12:29:21 +0000393 /* format string assignment: "setexpr name fmt %d value" */
394 if (strcmp(argv[2], "fmt") == 0 && IS_ENABLED(CONFIG_CMD_SETEXPR_FMT)) {
395 char str[MAX_STR_LEN];
396 int result;
397
398 if (argc == 3)
399 return CMD_RET_USAGE;
400
401 result = printf_setexpr(str, sizeof(str), argc - 3, &argv[3]);
402 if (result)
403 return result;
404
405 return env_set(argv[1], str);
406 }
407
408 if (argc == 4 || argc > 6)
409 return CMD_RET_USAGE;
410
Wolfgang Denkb2234d62013-03-23 23:50:33 +0000411 /* plain assignment: "setexpr name value" */
Simon Glass580a7a52020-11-01 14:15:44 -0700412 if (argc == 3) {
413 if (w == CMD_DATA_SIZE_STR) {
414 ret = env_set(argv[1], aval.sval);
415 free(aval.sval);
416 } else {
417 ret = env_set_hex(argv[1], aval.ival);
418 }
419
420 return ret;
421 }
Joe Hershberger0e640e62012-11-01 16:21:14 +0000422
Wolfgang Denkb04ae782013-03-23 23:50:34 +0000423 /* 5 or 6 args (6 args only with [g]sub) */
424#ifdef CONFIG_REGEX
425 /*
426 * rexep handling: "setexpr name [g]sub r s [t]"
427 * with 5 args, "t" will be NULL
428 */
429 if (strcmp(argv[2], "gsub") == 0)
Simon Glassb8e9f2d2020-11-01 14:15:39 -0700430 return regex_sub_var(argv[1], argv[3], argv[4], argv[5], 1);
Wolfgang Denkb04ae782013-03-23 23:50:34 +0000431
432 if (strcmp(argv[2], "sub") == 0)
Simon Glassb8e9f2d2020-11-01 14:15:39 -0700433 return regex_sub_var(argv[1], argv[3], argv[4], argv[5], 0);
Wolfgang Denkb04ae782013-03-23 23:50:34 +0000434#endif
435
Wolfgang Denkb2234d62013-03-23 23:50:33 +0000436 /* standard operators: "setexpr name val1 op val2" */
437 if (argc != 5)
438 return CMD_RET_USAGE;
439
440 if (strlen(argv[3]) != 1)
441 return CMD_RET_USAGE;
442
Simon Glass580a7a52020-11-01 14:15:44 -0700443 if (get_arg(argv[4], w, &bval)) {
444 if (w == CMD_DATA_SIZE_STR)
445 free(aval.sval);
Simon Glass00547af2020-11-01 14:15:43 -0700446 return CMD_RET_FAILURE;
Simon Glass580a7a52020-11-01 14:15:44 -0700447 }
Kumar Galaaed83342008-02-14 20:44:42 -0600448
Simon Glass580a7a52020-11-01 14:15:44 -0700449 if (w == CMD_DATA_SIZE_STR) {
450 int len;
451 char *str;
452
453 switch (argv[3][0]) {
454 case '+':
455 len = strlen(aval.sval) + strlen(bval.sval) + 1;
456 str = malloc(len);
457 if (!str) {
458 printf("Out of memory\n");
459 ret = CMD_RET_FAILURE;
460 } else {
461 /* These were copied out and checked earlier */
462 strcpy(str, aval.sval);
463 strcat(str, bval.sval);
464 ret = env_set(argv[1], str);
465 if (ret)
466 printf("Could not set var\n");
467 free(str);
468 }
469 break;
470 default:
471 printf("invalid op\n");
472 ret = 1;
473 }
474 } else {
Simon Glass00547af2020-11-01 14:15:43 -0700475 ulong a = aval.ival;
476 ulong b = bval.ival;
Kumar Galaaed83342008-02-14 20:44:42 -0600477
Simon Glass00547af2020-11-01 14:15:43 -0700478 switch (argv[3][0]) {
479 case '|':
480 value = a | b;
481 break;
482 case '&':
483 value = a & b;
484 break;
485 case '+':
486 value = a + b;
487 break;
488 case '^':
489 value = a ^ b;
490 break;
491 case '-':
492 value = a - b;
493 break;
494 case '*':
495 value = a * b;
496 break;
497 case '/':
498 value = a / b;
499 break;
500 case '%':
501 value = a % b;
502 break;
503 default:
504 printf("invalid op\n");
505 return 1;
506 }
507
508 env_set_hex(argv[1], value);
509 }
Kumar Galaaed83342008-02-14 20:44:42 -0600510
Simon Glass580a7a52020-11-01 14:15:44 -0700511 if (w == CMD_DATA_SIZE_STR) {
512 free(aval.sval);
513 free(bval.sval);
514 }
515
516 return ret;
Kumar Galaaed83342008-02-14 20:44:42 -0600517}
518
519U_BOOT_CMD(
Roland Gaudig2c9e7c22021-07-23 12:29:21 +0000520 setexpr, 8, 0, do_setexpr,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600521 "set environment variable as the result of eval expression",
Simon Glass580a7a52020-11-01 14:15:44 -0700522 "[.b, .w, .l, .s] name [*]value1 <op> [*]value2\n"
Kumar Galaaed83342008-02-14 20:44:42 -0600523 " - set environment variable 'name' to the result of the evaluated\n"
Wolfgang Denkb04ae782013-03-23 23:50:34 +0000524 " expression specified by <op>. <op> can be &, |, ^, +, -, *, /, %\n"
Simon Glass580a7a52020-11-01 14:15:44 -0700525 " (for strings only + is supported)\n"
Joe Hershberger0e640e62012-11-01 16:21:14 +0000526 " size argument is only meaningful if value1 and/or value2 are\n"
527 " memory addresses (*)\n"
Wolfgang Denkb2234d62013-03-23 23:50:33 +0000528 "setexpr[.b, .w, .l] name [*]value\n"
529 " - load a value into a variable"
Roland Gaudig2c9e7c22021-07-23 12:29:21 +0000530#ifdef CONFIG_CMD_SETEXPR_FMT
531 "\n"
532 "setexpr name fmt <format> [value1] [value2] [value3] [value4]\n"
533 " - set environment variable 'name' to the result of the bash like\n"
534 " format string evaluation of value."
535#endif
Wolfgang Denkb04ae782013-03-23 23:50:34 +0000536#ifdef CONFIG_REGEX
537 "\n"
538 "setexpr name gsub r s [t]\n"
539 " - For each substring matching the regular expression <r> in the\n"
540 " string <t>, substitute the string <s>. The result is\n"
541 " assigned to <name>. If <t> is not supplied, use the old\n"
Massimiliano Minella847f69c2024-02-08 15:58:27 +0100542 " value of <name>. If no substring matching <r> is found in <t>,\n"
543 " assign <t> to <name>.\n"
Wolfgang Denkb04ae782013-03-23 23:50:34 +0000544 "setexpr name sub r s [t]\n"
545 " - Just like gsub(), but replace only the first matching substring"
546#endif
Kumar Galaaed83342008-02-14 20:44:42 -0600547);