1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
|
/* Test shell framework for GNU Mailutils.
Copyright (C) 2003-2025 Free Software Foundation, Inc.
GNU Mailutils is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GNU Mailutils is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */
#include <config.h>
#include "mailutils/mailutils.h"
#include "tesh.h"
static struct mu_tesh_command *
find_command (struct mu_tesh_command *cmd, char const *name)
{
while (cmd->verb)
{
if (strcmp (cmd->verb, name) == 0)
return cmd;
cmd++;
}
return NULL;
}
static int
cmdspecial (char const *special, struct mu_tesh_command *cmdtab,
int argc, char **argv, mu_assoc_t opt, void *env, int defval)
{
struct mu_tesh_command *cmd = find_command (cmdtab, special);
if (cmd)
return cmd->func (argc, argv, opt, env);
return defval;
}
static int
is_reserved (char const *str)
{
return mu_string_prefix (str, "__") && mu_string_suffix (str, "__");
}
#define VARIADIC (-1)
static int
interpret (int argc, char **xargv, struct mu_tesh_command *cmdtab, void *env)
{
int rc;
struct mu_tesh_command *cmd;
mu_assoc_t options = NULL;
char **argv = xargv;
if (strcmp (argv[0], "help") == 0)
{
mu_tesh_help (cmdtab, env);
return 0;
}
if (is_reserved (argv[0]))
cmd = NULL;
else
cmd = find_command (cmdtab, argv[0]);
if (!cmd)
{
if (cmdspecial("__NOCMD__", cmdtab, argc, argv, NULL, env, MU_ERR_NOENT))
{
mu_error ("%s: no such command", argv[0]);
return MU_ERR_NOENT;
}
return 0;
}
if (cmd->param_min == 0)
{
struct mu_wordsplit ws;
/* See the description of the args field in tesh.h (Note [2]) */
if (mu_wordsplit (cmd->args, &ws,
MU_WRDSF_NOVAR
| MU_WRDSF_NOCMD
| MU_WRDSF_ALLOC_DIE
| MU_WRDSF_SHOWERR))
return MU_ERR_PARSE;
if (ws.ws_wordc == 0)
{
cmd->param_min = cmd->param_max = 1;
cmd->options = NULL;
}
else
{
int i;
int variadic = 0;
cmd->param_min = cmd->param_max = 1;
for (i = 0; i < ws.ws_wordc; i++)
{
char *argdef = ws.ws_wordv[i];
if (mu_string_suffix (argdef, "..."))
{
variadic = 1;
argdef[strlen (argdef) - 3] = 0;
if (argdef[0] == 0)
{
if (i != ws.ws_wordc - 1)
{
mu_error ("%s: ellipsis must be last (found at #%d)",
cmd->args, i);
abort ();
}
break;
}
}
if (mu_string_prefix (argdef, "[-")
&& mu_string_suffix (argdef, "]"))
{
int *type;
char *p;
type = mu_alloc (sizeof (*type));
argdef += 2;
argdef[strlen (argdef) - 1] = 0;
if (!cmd->options)
{
MU_ASSERT (mu_assoc_create (&cmd->options, 0));
}
p = strchr (argdef, '=');
if (p)
{
*p = 0;
if (p[-1] == '[' && mu_string_suffix (argdef, "]"))
{
*type = mu_tesh_arg_optional;
p[-1] = 0;
}
else
*type = mu_tesh_arg_required;
}
else
*type = mu_tesh_noarg;
MU_ASSERT (mu_assoc_install (cmd->options, argdef, type));
}
else if (mu_string_prefix (argdef, "["))
{
int j;
int lev = 0;
for (j = i; j < ws.ws_wordc; )
{
if (mu_string_prefix (ws.ws_wordv[j], "["))
lev++;
if (mu_string_suffix (ws.ws_wordv[j], "]"))
lev--;
j++;
if (lev == 0)
break;
}
if (lev == 0)
{
cmd->param_max += j - i;
i = j;
}
}
else
{
cmd->param_min++;
cmd->param_max++;
}
}
if (!variadic
&& mu_string_prefix (ws.ws_wordv[ws.ws_wordc-1], "[")
&& mu_string_suffix (ws.ws_wordv[ws.ws_wordc-1], "...]"))
{
variadic = 1;
}
if (variadic)
cmd->param_max = VARIADIC;
mu_wordsplit_free (&ws);
}
}
if (cmd->options)
{
int i;
/* Extract options */
MU_ASSERT (mu_assoc_create (&options, 0));
for (i = 1; i < argc; i++)
{
if (strcmp (argv[i], "--") == 0)
{
i++;
break;
}
if (argv[i][0] == '-')
{
int *type;
char *opt = argv[i];
char *arg;
arg = strchr (opt, '=');
if (arg)
*arg++ = 0;
rc = mu_assoc_lookup (cmd->options, opt + 1, &type);
if (rc == MU_ERR_NOENT)
{
mu_error ("%s: no such option %s", argv[0], opt);
mu_assoc_destroy (&options);
return MU_ERR_NOENT;
}
if (arg)
{
if (*type == mu_tesh_noarg)
{
mu_error ("%s: option %s doesn't take argument",
argv[0], opt);
mu_assoc_destroy (&options);
return MU_ERR_PARSE;
}
}
else if (*type == mu_tesh_arg_required)
{
if (i + 1 < argc)
arg = argv[++i];
else
{
mu_error ("%s: option %s requires argument",
argv[0], opt);
mu_assoc_destroy (&options);
return MU_ERR_PARSE;
}
}
if (arg)
arg = mu_strdup (arg);
MU_ASSERT (mu_assoc_install (options, opt + 1, arg));
}
else
break;
}
if (i > 1)
{
char *t;
--i;
t = argv[i];
argv[i] = argv[0];
argv[0] = t;
argc -= i;
argv += i;
}
}
if (argc < cmd->param_min)
{
mu_error ("%s: not enough arguments", argv[0]);
return MU_ERR_PARSE;
}
if (cmd->param_max != VARIADIC && argc > cmd->param_max)
{
mu_error ("%s: too many arguments", argv[0]);
return MU_ERR_PARSE;
}
rc = cmdspecial ("__ENVINIT__", cmdtab, argc, argv, options, env, 0);
if (rc == 0)
{
rc = cmd->func (argc, argv, options, env);
cmdspecial ("__ENVFINI__", cmdtab, argc, argv, options, env, 0);
}
mu_assoc_destroy (&options);
return rc;
}
static void
cleanup (struct mu_tesh_command *cmd)
{
while (cmd->verb)
{
mu_assoc_destroy (&cmd->options);
cmd++;
}
}
void
mu_tesh_read_and_eval (int argc, char **argv,
struct mu_tesh_command *cmd,
void *env)
{
if (argc)
{
while (argc)
{
int i, n = 0;
for (i = 0; i < argc; i++)
{
size_t len = strlen (argv[i]);
if (argv[i][len - 1] == ';')
{
if (len == 1)
n = 1;
else
argv[i][len - 1] = 0;
i++;
break;
}
}
interpret (i - n, argv, cmd, env);
argc -= i;
argv += i;
}
}
else
{
char *buf = NULL;
size_t size = 0, n;
struct mu_wordsplit ws;
int wsflags;
int rc;
mu_transport_t trans[2];
int interactive;
if (mu_stream_ioctl (mu_strin, MU_IOCTL_TRANSPORT, MU_IOCTL_OP_GET, trans) == 0)
interactive = isatty ((intptr_t)trans[0]);
else
interactive = 0;
wsflags = MU_WRDSF_DEFFLAGS
| MU_WRDSF_COMMENT
| MU_WRDSF_ALLOC_DIE
| MU_WRDSF_SHOWERR;
ws.ws_comment = "#";
while (1)
{
char *larg[2];
if (interactive)
{
mu_stream_printf (mu_strout, "%s", "(tesh) ");
mu_stream_flush (mu_strout);
}
if ((rc = mu_stream_getline (mu_strin, &buf, &size, &n)) != 0 ||
n == 0)
break;
mu_ltrim_class (buf, MU_CTYPE_SPACE);
mu_rtrim_class (buf, MU_CTYPE_SPACE);
larg[0] = buf;
larg[1] = NULL;
if (!cmdspecial("__LINEPROC__", cmd, 1, larg, NULL, env, MU_ERR_NOENT))
continue;
MU_ASSERT (mu_wordsplit (larg[0], &ws, wsflags));
wsflags |= MU_WRDSF_REUSE;
if (ws.ws_wordc == 0)
continue;
interpret (ws.ws_wordc, ws.ws_wordv, cmd, env);
}
if (wsflags & MU_WRDSF_REUSE)
mu_wordsplit_free (&ws);
}
cleanup (cmd);
}
void
mu_tesh_init (char const *argv0)
{
mu_set_program_name (argv0);
mu_stdstream_setup (MU_STDSTREAM_RESET_NONE);
}
void
mu_tesh_help (struct mu_tesh_command *cmd, void *env)
{
cmdspecial("__HELPINIT__", cmd, 0, NULL, NULL, env, 0);
for (; cmd->verb; cmd++)
if (!is_reserved (cmd->verb))
mu_printf (" %s %s\n", cmd->verb, cmd->args);
cmdspecial("__HELPFINI__", cmd, 0, NULL, NULL, env, 0);
}
|