Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

   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
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
/*	$NetBSD: lex.c,v 1.45 2018/02/04 09:01:12 mrg Exp $	*/

/*
 * Copyright (c) 1980, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)lex.c	8.2 (Berkeley) 4/20/95";
#else
__RCSID("$NetBSD: lex.c,v 1.45 2018/02/04 09:01:12 mrg Exp $");
#endif
#endif /* not lint */

#include <assert.h>
#include <util.h>

#include "rcv.h"
#include "extern.h"
#ifdef USE_EDITLINE
#include "complete.h"
#endif
#include "format.h"
#include "sig.h"
#include "thread.h"

/*
 * Mail -- a mail program
 *
 * Lexical processing of commands.
 */

static const char *prompt = DEFAULT_PROMPT;
static int	*msgvec;
static int	inithdr;		/* Am printing startup headers. */
static jmp_buf	jmpbuf;			/* The reset jmpbuf */
static int	reset_on_stop;		/* To do job control longjmp. */

#ifdef DEBUG_FILE_LEAK
struct glue {
	struct  glue *next;
	int     niobs;
	FILE    *iobs;
};
extern struct glue __sglue;

static int open_fd_cnt;
static int open_fp_cnt;

static int
file_count(void)
{
	struct glue *gp;
	FILE *fp;
	int n;
	int cnt;

	cnt = 0;
	for (gp = &__sglue; gp; gp = gp->next) {
		for (fp = gp->iobs, n = gp->niobs; --n >= 0; fp++)
			if (fp->_flags)
				cnt++;
	}
	return cnt;
}

static int
fds_count(void)
{
	int maxfd;
	int cnt;
	int fd;

	maxfd = fcntl(0, F_MAXFD);
	if (maxfd == -1) {
		warn("fcntl");
		return -1;
	}

	cnt = 0;
	for (fd = 0; fd <= maxfd; fd++) {
		struct stat sb;

		if (fstat(fd, &sb) != -1)
			cnt++;
		else if (errno != EBADF
#ifdef BROKEN_CLONE_STAT /* see PRs 37878 and 37550 */
		    && errno != EOPNOTSUPP
#endif
			)
			warn("fstat(%d): errno=%d", fd, errno);
	}
	return cnt;
}

static void
file_leak_init(void)
{
	open_fd_cnt = fds_count();
	open_fp_cnt = file_count();
}

static void
file_leak_check(void)
{
	if (open_fp_cnt != file_count() ||
	    open_fd_cnt != fds_count()) {
		(void)printf("FILE LEAK WARNING: "
		    "fp-count: %d (%d)  "
		    "fd-count: %d (%d)  max-fd: %d\n",
		    file_count(), open_fp_cnt,
		    fds_count(), open_fd_cnt,
		    fcntl(0, F_MAXFD));
	}
}
#endif /* DEBUG_FILE_LEAK */

static void
update_mailname(const char *name)
{
	char tbuf[PATHSIZE];
	size_t l;

	/* Don't realpath(3) if it's only an update request */
	if (name != NULL && realpath(name, mailname) == NULL) {
		warn("Can't canonicalize `%s'", name);
		return;
	}

	if (getfold(tbuf, sizeof(tbuf)) >= 0) {
		l = strlen(tbuf);
		if (l < sizeof(tbuf) - 1)
			tbuf[l++] = '/';
		if (strncmp(tbuf, mailname, l) == 0) {
			char const *sep = "", *cp = mailname + l;

			l = strlen(cp);
			if (l >= sizeof(displayname)) {
				cp += l;
				cp -= sizeof(displayname) - 5;
				sep = "...";
			}
			(void)snprintf(displayname, sizeof(displayname),
			    "+%s%s", sep, cp);
			return;
		}
	}

	l = strlen(mailname);
	if (l < sizeof(displayname))
		strcpy(displayname, mailname);
	else {
		l -= sizeof(displayname) - 4 - sizeof(displayname) / 3;
		(void)snprintf(displayname, sizeof(displayname), "%.*s...%s",
			(int)sizeof(displayname) / 3, mailname, mailname + l);
	}
}

/*
 * Set the size of the message vector used to construct argument
 * lists to message list functions.
 */
static void
setmsize(int sz)
{
	if (msgvec != 0)
		free(msgvec);
	msgvec = ecalloc((size_t) (sz + 1), sizeof(*msgvec));
}

/*
 * Set up editing on the given file name.
 * If the first character of name is %, we are considered to be
 * editing the file, otherwise we are reading our mail which has
 * signficance for mbox and so forth.
 */
PUBLIC int
setfile(const char *name)
{
	FILE *ibuf;
	int i, fd;
	struct stat stb;
	char isedit = *name != '%' || getuserid(myname) != (int)getuid();
	const char *who = name[1] ? name + 1 : myname;
	static int shudclob;
	char tempname[PATHSIZE];

	if ((name = expand(name)) == NULL)
		return -1;

	if ((ibuf = Fopen(name, "ref")) == NULL) {
		if (!isedit && errno == ENOENT)
			goto nomail;
		warn("Can't open `%s'", name);
		return -1;
	}

	if (fstat(fileno(ibuf), &stb) < 0) {
		warn("fstat");
		(void)Fclose(ibuf);
		return -1;
	}

	switch (stb.st_mode & S_IFMT) {
	case S_IFDIR:
		(void)Fclose(ibuf);
		errno = EISDIR;
		warn("%s", name);
		return -1;

	case S_IFREG:
		break;

	default:
		(void)Fclose(ibuf);
		errno = EINVAL;
		warn("%s", name);
		return -1;
	}

	/*
	 * Looks like all will be well.  We must now relinquish our
	 * hold on the current set of stuff.  Must hold signals
	 * while we are reading the new file, else we will ruin
	 * the message[] data structure.
	 */

	sig_check();
	sig_hold();
	if (shudclob)
		quit(jmpbuf);

	/*
	 * Copy the messages into /tmp
	 * and set pointers.
	 */

	readonly = 0;
	if ((i = open(name, O_WRONLY)) < 0)
		readonly++;
	else
		(void)close(i);
	if (shudclob) {
		(void)fclose(itf);
		(void)fclose(otf);
	}
	shudclob = 1;
	edit = isedit;
	(void)strcpy(prevfile, mailname);
	update_mailname(name != mailname ? name : NULL);
	mailsize = fsize(ibuf);
	(void)snprintf(tempname, sizeof(tempname),
	    "%s/mail.RxXXXXXXXXXX", tmpdir);
	if ((fd = mkstemp(tempname)) == -1 ||
	    (otf = fdopen(fd, "wef")) == NULL)
		err(EXIT_FAILURE, "Can't create tmp file `%s'", tempname);
	if ((itf = fopen(tempname, "ref")) == NULL)
		err(EXIT_FAILURE, "Can't create tmp file `%s'", tempname);
	(void)rm(tempname);
	setptr(ibuf, (off_t)0);
	setmsize(get_abs_msgCount());
	/*
	 * New mail may have arrived while we were reading
	 * the mail file, so reset mailsize to be where
	 * we really are in the file...
	 */
	mailsize = ftell(ibuf);
	(void)Fclose(ibuf);
	sig_release();
	sig_check();
	sawcom = 0;
	if (!edit && get_abs_msgCount() == 0) {
nomail:
		(void)fprintf(stderr, "No mail for %s\n", who);
		return -1;
	}
	return 0;
}

/*
 * Incorporate any new mail that has arrived since we first
 * started reading mail.
 */
PUBLIC int
incfile(void)
{
	off_t newsize;
	int omsgCount;
	FILE *ibuf;
	int rval;

	omsgCount = get_abs_msgCount();

	ibuf = Fopen(mailname, "ref");
	if (ibuf == NULL)
		return -1;
	sig_check();
	sig_hold();
	newsize = fsize(ibuf);
	if (newsize == 0 ||		/* mail box is now empty??? */
	    newsize < mailsize) {	/* mail box has shrunk??? */
		rval = -1;
		goto done;
	}
	if (newsize == mailsize) {
		rval = 0;               /* no new mail */
		goto done;
	}
	setptr(ibuf, mailsize);		/* read in new mail */
	setmsize(get_abs_msgCount());	/* get the new message count */
	mailsize = ftell(ibuf);
	rval = get_abs_msgCount() - omsgCount;
 done:
	(void)Fclose(ibuf);
	sig_release();
	sig_check();
	return rval;
}

/*
 * Return a pointer to the comment character, respecting quoting as
 * done in getrawlist().  The comment character is ignored inside
 * quotes.
 */
static char *
comment_char(char *line)
{
	char *p;
	char quotec;
	quotec = '\0';
	for (p = line; *p; p++) {
		if (quotec != '\0') {
			if (*p == quotec)
				quotec = '\0';
		}
		else if (*p == '"' || *p == '\'')
			quotec = *p;
		else if (*p == COMMENT_CHAR)
			return p;
	}
	return NULL;
}

/*
 * Signal handler is hooked by setup_piping().
 * Respond to a broken pipe signal --
 * probably caused by quitting more.
 */
static jmp_buf	pipestop;

/*ARGSUSED*/
__dead static void
lex_brokpipe(int signo)
{

	longjmp(pipestop, signo);
}

/*
 * Check the command line for any requested piping or redirection,
 * depending on the value of 'c'.  If "enable-pipes" is set, search
 * the command line (cp) for the first occurrence of the character 'c'
 * that is not in a quote or (parenthese) group.
 */
PUBLIC char *
shellpr(char *cp)
{
	int quotec;
	int level;

	if (cp == NULL || value(ENAME_ENABLE_PIPES) == NULL)
		return NULL;

	level = 0;
	quotec = 0;
	for (/*EMPTY*/; *cp != '\0'; cp++) {
		if (quotec) {
			if (*cp == quotec)
				quotec = 0;
			if (*cp == '\\' &&
			    (cp[1] == quotec || cp[1] == '\\'))
				cp++;
		}
		else {
			switch (*cp) {
			case '|':
			case '>':
				if (level == 0)
					return cp;
				break;
			case '(':
				level++;
				break;
			case ')':
				level--;
				break;
			case '"':
			case '\'':
				quotec = *cp;
				break;
			default:
				break;
			}
		}
	}
	return NULL;
}

static int
do_paging(const char *cmd, int c_pipe)
{
	char *cp, *p;

	if (value(ENAME_PAGER_OFF) != NULL)
		return 0;

	if (c_pipe & C_PIPE_PAGER)
		return 1;

	if (c_pipe & C_PIPE_CRT && value(ENAME_CRT) != NULL)
		return 1;

	if ((cp = value(ENAME_PAGE_ALSO)) == NULL)
		return 0;

	if ((p = strcasestr(cp, cmd)) == NULL)
		return 0;

	if (p != cp && p[-1] != ',' && !is_WSP(p[-1]))
		return 0;

	p += strlen(cmd);

	return (*p == '\0' || *p == ',' || is_WSP(*p));
}

/*
 * Setup any pipe or redirection that the command line indicates.
 * If none, then setup the pager unless "pager-off" is defined.
 */
static FILE *fp_stop = NULL;
static int oldfd1 = -1;
static sig_t old_sigpipe;

static int
setup_piping(const char *cmd, char *cmdline, int c_pipe)
{
	FILE *fout;
	FILE *last_file;
	char *cp;

	sig_check();

	last_file = last_registered_file(0);

	fout = NULL;
	if ((cp = shellpr(cmdline)) != NULL) {
		char c;
		c = *cp;
		*cp = '\0';
		cp++;

		if (c == '|') {
			if ((fout = Popen(cp, "we")) == NULL) {
				warn("Popen: %s", cp);
				return -1;
			}
		}
		else {
			const char *mode;
			assert(c == '>');
			mode = *cp == '>' ? "ae" : "we";
			if (*cp == '>')
				cp++;

			cp = skip_WSP(cp);
			if ((fout = Fopen(cp, mode)) == NULL) {
				warn("Fopen: %s", cp);
				return -1;
			}
		}

	}
	else if (do_paging(cmd, c_pipe)) {
		const char *pager;
		pager = value(ENAME_PAGER);
		if (pager == NULL || *pager == '\0')
			pager = _PATH_MORE;

		if ((fout = Popen(pager, "we")) == NULL) {
			warn("Popen: %s", pager);
			return -1;
		}
	}

	if (fout) {
		old_sigpipe = sig_signal(SIGPIPE, lex_brokpipe);
		(void)fflush(stdout);
		if ((oldfd1 = dup(STDOUT_FILENO)) == -1)
			err(EXIT_FAILURE, "dup failed");
		if (dup2(fileno(fout), STDOUT_FILENO) == -1)
			err(EXIT_FAILURE, "dup2 failed");
		fp_stop = last_file;
	}
	return 0;
}

/*
 * This will close any piping started by setup_piping().
 */
static void
close_piping(void)
{
	sigset_t oset;
	struct sigaction osa;

	if (oldfd1 != -1) {
		(void)fflush(stdout);
		if (fileno(stdout) != oldfd1 &&
		    dup2(oldfd1, STDOUT_FILENO) == -1)
			err(EXIT_FAILURE, "dup2 failed");

		(void)sig_ignore(SIGPIPE, &osa, &oset);

		close_top_files(fp_stop);
		fp_stop = NULL;
		(void)close(oldfd1);
		oldfd1 = -1;

		(void)sig_signal(SIGPIPE, old_sigpipe);
		(void)sig_restore(SIGPIPE, &osa, &oset);
	}
	sig_check();
}

/*
 * Determine if as1 is a valid prefix of as2.
 * Return true if yep.
 */
static int
isprefix(char *as1, const char *as2)
{
	char *s1;
	const char *s2;

	s1 = as1;
	s2 = as2;
	while (*s1++ == *s2)
		if (*s2++ == '\0')
			return 1;
	return *--s1 == '\0';
}

/*
 * Find the correct command in the command table corresponding
 * to the passed command "word"
 */
PUBLIC const struct cmd *
lex(char word[])
{
	const struct cmd *cp;

	for (cp = &cmdtab[0]; cp->c_name != NULL; cp++)
		if (isprefix(word, cp->c_name))
			return cp;
	return NULL;
}

PUBLIC char *
get_cmdname(char *buf)
{
	char *cp;
	char *cmd;
	size_t len;

	for (cp = buf; *cp; cp++)
		if (strchr(" \t0123456789$^.:/-+*'\">|", *cp) != NULL)
			break;
	/* XXX - Don't miss the pipe command! */
	if (cp == buf && *cp == '|')
		cp++;
	len = cp - buf + 1;
	cmd = salloc(len);
	(void)strlcpy(cmd, buf, len);
	return cmd;
}

/*
 * Execute a single command.
 * Command functions return 0 for success, 1 for error, and -1
 * for abort.  A 1 or -1 aborts a load or source.  A -1 aborts
 * the interactive command loop.
 * execute_contxt_e is in extern.h.
 */
PUBLIC int
execute(char linebuf[], enum execute_contxt_e contxt)
{
	char *word;
	char *arglist[MAXARGC];
	const struct cmd * volatile com = NULL;
	char *volatile cp;
	int retval;
	int c;
	volatile int e = 1;

	/*
	 * Strip the white space away from the beginning
	 * of the command, then scan out a word, which
	 * consists of anything except digits and white space.
	 *
	 * Handle ! escapes differently to get the correct
	 * lexical conventions.
	 */

	cp = skip_space(linebuf);
	if (*cp == '!') {
		if (sourcing) {
			(void)printf("Can't \"!\" while sourcing\n");
			goto out;
		}
		(void)shell(cp + 1);
		return 0;
	}

	word = get_cmdname(cp);
	cp += strlen(word);

	/*
	 * Look up the command; if not found, bitch.
	 * Normally, a blank command would map to the
	 * first command in the table; while sourcing,
	 * however, we ignore blank lines to eliminate
	 * confusion.
	 */

	if (sourcing && *word == '\0')
		return 0;
	com = lex(word);
	if (com == NULL) {
		(void)printf("Unknown command: \"%s\"\n", word);
		goto out;
	}

	/*
	 * See if we should execute the command -- if a conditional
	 * we always execute it, otherwise, check the state of cond.
	 */

	if ((com->c_argtype & F) == 0 && (cond & CSKIP))
		return 0;

	/*
	 * Process the arguments to the command, depending
	 * on the type he expects.  Default to an error.
	 * If we are sourcing an interactive command, it's
	 * an error.
	 */

	if (mailmode == mm_sending && (com->c_argtype & M) == 0) {
		(void)printf("May not execute \"%s\" while sending\n",
		    com->c_name);
		goto out;
	}
	if (sourcing && com->c_argtype & I) {
		(void)printf("May not execute \"%s\" while sourcing\n",
		    com->c_name);
		goto out;
	}
	if (readonly && com->c_argtype & W) {
		(void)printf("May not execute \"%s\" -- message file is read only\n",
		   com->c_name);
		goto out;
	}
	if (contxt == ec_composing && com->c_argtype & R) {
		(void)printf("Cannot recursively invoke \"%s\"\n", com->c_name);
		goto out;
	}

	if (!sourcing && com->c_pipe && value(ENAME_INTERACTIVE) != NULL) {

		sig_check();
		if (setjmp(pipestop))
			goto out;

		if (setup_piping(com->c_name, cp, com->c_pipe) == -1)
			goto out;
	}
	switch (com->c_argtype & ARGTYPE_MASK) {
	case MSGLIST:
		/*
		 * A message list defaulting to nearest forward
		 * legal message.
		 */
		if (msgvec == 0) {
			(void)printf("Illegal use of \"message list\"\n");
			break;
		}
		if ((c = getmsglist(cp, msgvec, com->c_msgflag)) < 0)
			break;
		if (c  == 0) {
			*msgvec = first(com->c_msgflag,	com->c_msgmask);
			msgvec[1] = 0;
		}
		if (*msgvec == 0) {
			(void)printf("No applicable messages\n");
			break;
		}
		e = (*com->c_func)(msgvec);
		break;

	case NDMLIST:
		/*
		 * A message list with no defaults, but no error
		 * if none exist.
		 */
		if (msgvec == 0) {
			(void)printf("Illegal use of \"message list\"\n");
			break;
		}
		if (getmsglist(cp, msgvec, com->c_msgflag) < 0)
			break;
		e = (*com->c_func)(msgvec);
		break;

	case STRLIST:
		/*
		 * Just the straight string, with
		 * leading blanks removed.
		 */
		cp = skip_space(cp);
		e = (*com->c_func)(cp);
		break;

	case RAWLIST:
		/*
		 * A vector of strings, in shell style.
		 */
		if ((c = getrawlist(cp, arglist, (int)__arraycount(arglist))) < 0)
			break;
		if (c < com->c_minargs) {
			(void)printf("%s requires at least %d arg(s)\n",
				com->c_name, com->c_minargs);
			break;
		}
		if (c > com->c_maxargs) {
			(void)printf("%s takes no more than %d arg(s)\n",
				com->c_name, com->c_maxargs);
			break;
		}
		e = (*com->c_func)(arglist);
		break;

	case NOLIST:
		/*
		 * Just the constant zero, for exiting,
		 * eg.
		 */
		e = (*com->c_func)(0);
		break;

	default:
		errx(EXIT_FAILURE, "Unknown argtype");
	}

out:
	close_piping();

	/*
	 * Exit the current source file on
	 * error.
	 */
	retval = 0;
	if (e) {
		if (e < 0)
			retval = 1;
		else if (loading)
			retval = 1;
		else if (sourcing)
			(void)unstack();
	}
	else if (com != NULL) {
		if (contxt != ec_autoprint && com->c_argtype & P &&
		    value(ENAME_AUTOPRINT) != NULL &&
		    (dot->m_flag & MDELETED) == 0)
			(void)execute(__UNCONST("print ."), ec_autoprint);
		if (!sourcing && (com->c_argtype & T) == 0)
			sawcom = 1;
	}
	sig_check();
	return retval;
}

/*
 * The following gets called on receipt of an interrupt.  This is
 * to abort printout of a command, mainly.
 * Dispatching here when commands() is inactive crashes rcv.
 * Close all open files except 0, 1, 2, and the temporary.
 * Also, unstack all source files.
 */
__dead static void
lex_intr(int signo)
{

	noreset = 0;
	if (!inithdr)
		sawcom++;
	inithdr = 0;
	while (sourcing)
		(void)unstack();

	close_piping();
	close_all_files();

	if (image >= 0) {
		(void)close(image);
		image = -1;
	}
	(void)fprintf(stderr, "Interrupt\n");
	longjmp(jmpbuf, signo);
}

/*
 * Branch here on hangup signal and simulate "exit".
 */
/*ARGSUSED*/
__dead static void
lex_hangup(int s __unused)
{

	/* nothing to do? */
	exit(EXIT_FAILURE);
}

/*
 * When we wake up after ^Z, reprint the prompt.
 *
 * NOTE: EditLine deals with the prompt and job control, so with it
 * this does nothing, i.e., reset_on_stop == 0.
 */
static void
lex_stop(int signo)
{

	if (reset_on_stop) {
		reset_on_stop = 0;
		longjmp(jmpbuf, signo);
	}
}

/*
 * Interpret user commands one by one.  If standard input is not a tty,
 * print no prompt.
 */
PUBLIC void
commands(void)
{
	int n;
	char linebuf[LINESIZE];
	int eofloop;

#ifdef DEBUG_FILE_LEAK
	file_leak_init();
#endif

	if (!sourcing) {
		sig_check();

		sig_hold();
		(void)sig_signal(SIGINT,  lex_intr);
		(void)sig_signal(SIGHUP,  lex_hangup);
		(void)sig_signal(SIGTSTP, lex_stop);
		(void)sig_signal(SIGTTOU, lex_stop);
		(void)sig_signal(SIGTTIN, lex_stop);
		sig_release();
	}

	(void)setjmp(jmpbuf);	/* "reset" location if we got an interrupt */

	eofloop = 0;	/* initialize this after a possible longjmp */
	for (;;) {
		sig_check();
		(void)fflush(stdout);
		sreset();
		/*
		 * Print the prompt, if needed.  Clear out
		 * string space, and flush the output.
		 */
		if (!sourcing && value(ENAME_INTERACTIVE) != NULL) {
			if ((prompt = value(ENAME_PROMPT)) == NULL)
				prompt = DEFAULT_PROMPT;
			prompt = smsgprintf(prompt, dot);
			if ((value(ENAME_AUTOINC) != NULL) && (incfile() > 0))
				(void)printf("New mail has arrived.\n");

#ifndef USE_EDITLINE
			reset_on_stop = 1;	/* enable job control longjmp */
			(void)printf("%s", prompt);
#endif
		}
#ifdef DEBUG_FILE_LEAK
		file_leak_check();
#endif
		/*
		 * Read a line of commands from the current input
		 * and handle end of file specially.
		 */
		n = 0;
		for (;;) {
			sig_check();
#ifdef USE_EDITLINE
			if (!sourcing) {
				char *line;

				line = my_gets(&elm.command, prompt, NULL);
				if (line == NULL) {
					if (n == 0)
						n = -1;
					break;
				}
				(void)strlcpy(linebuf, line, sizeof(linebuf));
			}
			else {
				if (readline(input, &linebuf[n], LINESIZE - n, 0) < 0) {
					if (n == 0)
						n = -1;
					break;
				}
			}
#else /* USE_EDITLINE */
			if (readline(input, &linebuf[n], LINESIZE - n, reset_on_stop) < 0) {
				if (n == 0)
					n = -1;
				break;
			}
#endif /* USE_EDITLINE */
			if (!sourcing)
				setscreensize(); /* so we can resize window */

			if (sourcing) {  /* allow comments in source files */
				char *ptr;
				if ((ptr = comment_char(linebuf)) != NULL)
					*ptr = '\0';
			}
			if ((n = (int)strlen(linebuf)) == 0)
				break;
			n--;
			if (linebuf[n] != '\\')
				break;
			linebuf[n++] = ' ';
		}
#ifndef USE_EDITLINE
		sig_check();
		reset_on_stop = 0;	/* disable job control longjmp */
#endif
		if (n < 0) {
			char *p;

			/* eof */
			if (loading)
				break;
			if (sourcing) {
				(void)unstack();
				continue;
			}
			if (value(ENAME_INTERACTIVE) != NULL &&
			    (p = value(ENAME_IGNOREEOF)) != NULL &&
			    ++eofloop < (*p == '\0' ? 25 : atoi(p))) {
				(void)printf("Use \"quit\" to quit.\n");
				continue;
			}
			break;
		}
		eofloop = 0;
		if (execute(linebuf, ec_normal))
			break;
	}
}

/*
 * Announce information about the file we are editing.
 * Return a likely place to set dot.
 */
PUBLIC int
newfileinfo(int omsgCount)
{
	struct message *mp;
	int d, n, s, t, u, mdot;

	/*
	 * Figure out where to set the 'dot'.  Use the first new or
	 * unread message.
	 */
	for (mp = get_abs_message(omsgCount + 1); mp;
	     mp = next_abs_message(mp))
		if (mp->m_flag & MNEW)
			break;

	if (mp == NULL)
		for (mp = get_abs_message(omsgCount + 1); mp;
		     mp = next_abs_message(mp))
			if ((mp->m_flag & MREAD) == 0)
				break;
	if (mp != NULL)
		mdot = get_msgnum(mp);
	else
		mdot = omsgCount + 1;
#ifdef THREAD_SUPPORT
	/*
	 * See if the message is in the current thread.
	 */
	if (mp != NULL && get_message(1) != NULL && get_message(mdot) != mp)
		mdot = 0;
#endif
	/*
	 * Scan the message array counting the new, unread, deleted,
	 * and saved messages.
	 */
	d = n = s = t = u = 0;
	for (mp = get_abs_message(1); mp; mp = next_abs_message(mp)) {
		if (mp->m_flag & MNEW)
			n++;
		if ((mp->m_flag & MREAD) == 0)
			u++;
		if (mp->m_flag & MDELETED)
			d++;
		if (mp->m_flag & MSAVED)
			s++;
		if (mp->m_flag & MTAGGED)
			t++;
	}
	/*
	 * Display the statistics.
	 */
	update_mailname(NULL);
	(void)printf("\"%s\": ", displayname);
	{
		int cnt = get_abs_msgCount();
		(void)printf("%d message%s", cnt, cnt == 1 ? "" : "s");
	}
	if (n > 0)
		(void)printf(" %d new", n);
	if (u-n > 0)
		(void)printf(" %d unread", u);
	if (t > 0)
		(void)printf(" %d tagged", t);
	if (d > 0)
		(void)printf(" %d deleted", d);
	if (s > 0)
		(void)printf(" %d saved", s);
	if (readonly)
		(void)printf(" [Read only]");
	(void)printf("\n");

	return mdot;
}

/*
 * Announce the presence of the current Mail version,
 * give the message count, and print a header listing.
 */
PUBLIC void
announce(void)
{
	int vec[2], mdot;

	mdot = newfileinfo(0);
	vec[0] = mdot;
	vec[1] = 0;
	if ((dot = get_message(mdot)) == NULL)
		dot = get_abs_message(1); /* make sure we get something! */
	if (get_abs_msgCount() > 0 && value(ENAME_NOHEADER) == NULL) {
		inithdr++;
		(void)headers(vec);
		inithdr = 0;
	}
}

/*
 * Print the current version number.
 */

/*ARGSUSED*/
PUBLIC int
pversion(void *v __unused)
{
	(void)printf("Version %s\n", version);
	return 0;
}

/*
 * Load a file of user definitions.
 */
PUBLIC void
load(const char *name)
{
	FILE *in, *oldin;

	if ((in = Fopen(name, "ref")) == NULL)
		return;
	oldin = input;
	input = in;
	loading = 1;
	sourcing = 1;
	commands();
	loading = 0;
	sourcing = 0;
	input = oldin;
	(void)Fclose(in);
}