File: cmdline.c

package info (click to toggle)
crash 3.3-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 16,220 kB
  • ctags: 5,827
  • sloc: ansic: 62,500; makefile: 1,827
file content (1848 lines) | stat: -rwxr-xr-x 43,870 bytes parent folder | download
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
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
/* cmdline.c - core analysis suite
 *
 * Copyright (C) 1999, 2000, 2001, 2002 Mission Critical Linux, Inc.
 *
 * This program 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 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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.
 *
 * 11/09/99, 1.0    Initial Release
 * 11/12/99, 1.0-1  Bug fixes
 * 12/10/99, 1.1    Fixes, new commands, support for v1 SGI dumps
 * 01/18/00, 2.0    Initial gdb merger, support for Alpha
 * 02/01/00, 2.1    Bug fixes, new commands, options, support for v2 SGI dumps
 * 02/29/00, 2.2    Bug fixes, new commands, options
 * 04/11/00, 2.3    Bug fixes, new command, options, initial PowerPC framework
 * 04/12/00  ---    Transition to BitKeeper version control
 * 
 * BitKeeper ID: @(#)cmdline.c 1.16
 *
 * 09/28/00  ---    Transition to CVS version control
 *
 * CVS: $Revision: 1.47 $ $Date: 2002/01/17 16:11:12 $
 */

#include "defs.h"

static void restore_sanity(void);
static void restore_ifile_sanity(void);
static int pseudo_command(char *);
static void check_special_handling(char *);
static int is_shell_script(char *);
static void list_aliases(char *);
static int allocate_alias(int);
static int alias_exists(char *);
static void resolve_aliases(void);
static int setup_redirect(int);
static int output_command_to_pid(void);
static void set_my_tty(void);
static char *signame(int);
static int setup_stdpipe(void);
static void wait_for_children(ulong);
#define ZOMBIES_ONLY (1)
#define ALL_CHILDREN (2)

#define READLINE_LIBRARY

#include <readline.h>
#include <rldefs.h>
#include <history.h>

static void readline_init(void);

static struct alias_data alias_head = { 0 }; 

void
get_command_line(void)
{
	/*
	 *  Restore normal environment, clearing out any excess baggage
	 *  piled up by the previous command.
	 */
	restore_sanity();
	fp = stdout;
	BZERO(pc->command_line, BUFSIZE);

	if (!(pc->flags & 
	    (READLINE|SILENT|CMDLINE_IFILE|RCHOME_IFILE|RCLOCAL_IFILE))) 
		fprintf(fp, pc->prompt);
	fflush(fp);

	/*
	 *  Input can come from five possible sources:
	 *
	 *    1. an .rc file located in the user's HOME directory.
         *    2. an .rc file located in the current directory.
	 *    3. an input file that was designated by the -i flag at 
	 *       program invocation.
	 *    4. from a terminal.
	 *    5. from a pipe, if stdin is a pipe rather than a terminal.
	 */
	if (pc->flags & RCHOME_IFILE)  
                sprintf(pc->command_line, "< %s/.%src", 
			pc->home, pc->program_name);
	else if (pc->flags & RCLOCAL_IFILE) 
                sprintf(pc->command_line, "< .%src", pc->program_name);
	else if (pc->flags & CMDLINE_IFILE) 
		sprintf(pc->command_line, "< %s", pc->input_file);
	else if (pc->flags & TTY) {
		if (!(pc->readline = readline(pc->prompt))) {
			args[0] = NULL;
			fprintf(fp, "\n");
			return;
		} 

		strcpy(pc->command_line, pc->readline);
		free(pc->readline); 

		clean_line(pc->command_line);
		pseudo_command(pc->command_line);
		strcpy(pc->orig_line, pc->command_line);

		if (strlen(pc->command_line) && !iscntrl(pc->command_line[0])) 
			add_history(pc->command_line);
		
		check_special_handling(pc->command_line);
        } else {
                fflush(fp);
        	if (fgets(pc->command_line, BUFSIZE-1, stdin) == NULL)
			exit(1);
		strcpy(pc->orig_line, pc->command_line);
        }

	/*
	 *  First clean out all linefeeds and leading/trailing spaces.
	 *  Then substitute aliases for the real thing they represent.
	 */
	clean_line(pc->command_line);
	resolve_aliases();

	/*
	 *  Setup output redirection based upon the command line itself or
	 *  based upon the default scrolling behavior, if any.
	 */

	switch (setup_redirect(FROM_COMMAND_LINE))
	{
	case REDIRECT_NOT_DONE:
	case REDIRECT_TO_STDPIPE:
	case REDIRECT_TO_PIPE:
	case REDIRECT_TO_FILE:
		break;

	case REDIRECT_SHELL_ESCAPE:
	case REDIRECT_SHELL_COMMAND:
	case REDIRECT_FAILURE:  
		RESTART();
		break;
	}

	/*
	 *  Setup the global argcnt and args[] array for use by everybody
	 *  during the life of this command.
	 */
	argcnt = parse_line(pc->command_line, args);
}


/*
 *  Allow input file redirection without having to put a space between 
 *  the < and the filename.  Allow the "pointer-to" asterisk to "touch"
 *  the structure/union name.
 */
static void 
check_special_handling(char *s)
{
	char local[BUFSIZE];

	strcpy(local, s);

	if ((local[0] == '*') && (!whitespace(local[1]))) {
		sprintf(s, "* %s", &local[1]);
		return;
	}

        if ((local[0] == '<') && (!whitespace(local[1]))) {
                sprintf(s, "< %s", &local[1]);
                return;
        }
}


/*
 *  At this point the only pseudo commands are the "r" (repeat) and 
 *  the "h" (history) command:
 *
 *    1. an "r" alone, or "!!" along, just means repeat the last command.
 *    2. an "r" followed by a number, means repeat that command from the
 *       history table.
 *    3. an "r" followed by one or more non-decimal characters means to
 *       seek back until a line-beginning match is found. 
 *    4. an "h" alone, or a string beginning with "hi", means history.
 */
static int
pseudo_command(char *input)
{
        int i;
	HIST_ENTRY *entry;
	int idx, found;
	char *p;

        clean_line(input);

        /*  
         *  Just dump all commands that have been entered to date.
         */
        if (STREQ(input, "h") || STRNEQ(input, "hi")) {
                dump_history();
                pc->command_line[0] = NULLCHAR;
                return TRUE;
        }

        if (STREQ(input, "r") || STREQ(input, "!!")) {
                if (!history_offset)
                        error(FATAL, "no commands entered!\n");
                entry = history_get(history_offset);
                strcpy(input, entry->line);
                fprintf(fp, "%s%s\n", pc->prompt, input);
                return TRUE;
        }

        if ((input[0] == 'r') && decimal(&input[1], 0)) {
                if (!history_offset)
                        error(FATAL, "no commands entered!\n");
                p = &input[1];
                goto rerun;
        }


	if (STRNEQ(input, "r ")) {
                if (!history_offset)
                        error(FATAL, "no commands entered!\n");

		p = first_nonspace(&input[1]);
rerun:
		if (decimal(p, 0)) {
			idx = atoi(p);
			if (idx == 0)
				goto invalid_repeat_request;
			if (idx > history_offset) 
				error(FATAL, "command %d not entered yet!\n",
					idx);	
                	entry = history_get(idx);
               		strcpy(input, entry->line);
                	fprintf(fp, "%s%s\n", pc->prompt, input);
                	return TRUE;
		} 

		idx = -1;
		found = FALSE;

        	for (i = history_offset; i > 0; i--) {
                	entry = history_get(i);
			if (STRNEQ(entry->line, p)) {
				found = TRUE;
				break;
			}
        	}

		if (found) {
			strcpy(input, entry->line);
			fprintf(fp, "%s%s\n", pc->prompt, input);
			return TRUE;
		}

invalid_repeat_request:
		fprintf(fp, "invalid repeat request: %s\n", input);
		strcpy(input, "");
		return TRUE;
	}

	return FALSE;
}

/*
 *  Dump the history table in first-to-last chronological order.
 */
void
dump_history(void)
{
        int i;
        HIST_ENTRY **the_history;
        HIST_ENTRY *entry;

        if (!history_offset)
                error(FATAL, "no commands entered!\n");

        the_history = history_list();

        for (i = 0; i < history_offset; i++) {
                entry = the_history[i];
                fprintf(fp, "[%d] %s\n", i+1, entry->line);
        }
}

/*
 *  Parse the command line for pipe or redirect characters:  
 *
 *   1. if a "|" character is found, popen() what comes after it, and 
 *      modify the contents of the global "fp" FILE pointer.
 *   2. if one or two ">" characters are found, fopen() the filename that
 *      follows, and modify the contents of the global "fp" FILE pointer.
 * 
 *  Care is taken to segregate:
 *
 *   1. expressions encompassed by parentheses, or
 *   2. strings encompassed by apostrophes. 
 *
 *  When either of the above are in affect, no redirection is done.
 *
 *  Lastly, if no redirection is requested by the user on the command line,
 *  output is passed to the default scrolling command, which is popen()'d
 *  and again, the contents of the global "fp" FILE pointer is modified.
 *  This default behavior is not performed if the command is coming from
 *  an input file, nor if scrolling has been turned off.
 */
static int
setup_redirect(int origin)
{
	char *p, which;
	int append;
	int expression;
	int string;
	FILE *pipe;
	FILE *ofile;

	pc->redirect = origin;
	pc->eoc_index = 0;

	p = pc->command_line;

        if (STREQ(p, "|") || STREQ(p, "!")) {
        	system("/bin/sh");
		pc->redirect |= REDIRECT_SHELL_ESCAPE;
		return REDIRECT_SHELL_ESCAPE;
	}

	if (FIRSTCHAR(p) == '|' || FIRSTCHAR(p) == '!')
		pc->redirect |= REDIRECT_SHELL_COMMAND;

	expression = string = FALSE;

	while (*p) {
		if (*p == '(')
			expression = TRUE;
		if (*p == ')')
			expression = FALSE;

		if (*p == '"')
			string = !string;

		if (!(expression || string) && 
		    ((*p == '|') || (*p == '!'))) {
			which = *p;
			*p = NULLCHAR;
			pc->eoc_index = p - pc->command_line;
			p++;
			p = strip_beginning_whitespace(p);

			if (!strlen(p)) {
				error(INFO, "no shell command after '%c'\n",
					which);
				pc->redirect |= REDIRECT_FAILURE;
				return REDIRECT_FAILURE;
			}
		
			if (LASTCHAR(p) == '|')
				error(FATAL_RESTART, "pipe to nowhere?\n");

			if (pc->redirect & REDIRECT_SHELL_COMMAND) {
				system(p);
				return REDIRECT_SHELL_COMMAND;
			} 

                        if ((pipe = popen(p, "w")) == NULL) {
                                error(INFO, "cannot open pipe\n");
				pc->redirect |= REDIRECT_FAILURE;
				return REDIRECT_FAILURE;
                        }
                        setbuf(pipe, NULL);

			switch (origin)
			{
			case FROM_COMMAND_LINE:
				fp = pc->pipe = pipe;
				break;

			case FROM_INPUT_FILE:
				fp = pc->ifile_pipe = pipe;
				break;
			}

			if (strstr(p, "|")) {
				p = rindex(p, '|') + 1;
				p = first_nonspace(p);
				pc->redirect |= REDIRECT_MULTI_PIPE;
			}

			strcpy(pc->pipe_command, p);
			null_first_space(pc->pipe_command);

			pc->redirect |= REDIRECT_TO_PIPE;

			if (!(pc->redirect & REDIRECT_SHELL_COMMAND)) {
				if ((pc->pipe_pid = output_command_to_pid()))
					pc->redirect |= REDIRECT_PID_KNOWN;
				else 
					error(FATAL_RESTART, 
						"pipe operation failed\n");
			}

			return REDIRECT_TO_PIPE;
		}

                if (!(expression || string) && (*p == '>') &&
		    !((p > pc->command_line) && (*(p-1) == '-'))) {
                	append = FALSE;

			*p = NULLCHAR;
			pc->eoc_index = p - pc->command_line;
                        if (*(p+1) == '>') {
                                append = TRUE;
				*p = NULLCHAR;
				p++;
			}
			p++;
			p = strip_beginning_whitespace(p);

                        if (!strlen(p)) {
                                error(INFO, "no file name after %s\n",
					append ? ">>" : ">");
				pc->redirect |= REDIRECT_FAILURE;
                                return REDIRECT_FAILURE;
                        }

        		if ((ofile = 
			    fopen(p, append ? "a+" : "w+")) == NULL) {
                		error(INFO, "unable to open %s\n", p);
				pc->redirect = REDIRECT_FAILURE;
				return REDIRECT_FAILURE;
        		}
			setbuf(ofile, NULL);

                        switch (origin)
                        {
                        case FROM_COMMAND_LINE:
                                fp = pc->ofile = ofile;
                                break;

                        case FROM_INPUT_FILE:
                                fp = pc->ifile_ofile = ofile;
                                break;
                        }

			pc->redirect |= REDIRECT_TO_FILE;
			return REDIRECT_TO_FILE;
		}

		p++;
	}

	if ((origin == FROM_COMMAND_LINE) && (pc->flags & TTY) && 
	    (pc->flags & SCROLL) && pc->scroll_command) {
		if (!strlen(pc->command_line) ||
		    STREQ(pc->command_line, "q") ||
		    STREQ(pc->command_line, "Q") ||
		    STREQ(pc->command_line, "exit") ||
		    STRNEQ(pc->command_line, "<")) {
			pc->redirect |= REDIRECT_NOT_DONE;
			return REDIRECT_NOT_DONE;
		}

                if (!setup_stdpipe()) {
                        error(INFO, "cannot open pipe\n");
			pc->redirect |= REDIRECT_FAILURE;
                        return REDIRECT_FAILURE;
                }
                fp = pc->stdpipe;

		pc->redirect |= REDIRECT_TO_STDPIPE;
	
		switch (pc->scroll_command)
		{
		case SCROLL_LESS:
			strcpy(pc->pipe_command, "/usr/bin/less");
			break;
		case SCROLL_MORE:
			strcpy(pc->pipe_command, "/bin/more");
			break;
		}

                return REDIRECT_TO_STDPIPE;
	}

	pc->redirect |= REDIRECT_NOT_DONE;

	return REDIRECT_NOT_DONE;
}

void
debug_redirect(char *s)
{
	int others;
	int alive;

        others = 0;
        console("%s: (", s);
        if (pc->redirect & FROM_COMMAND_LINE)
                console("%sFROM_COMMAND_LINE", others++ ? "|" : "");
        if (pc->redirect & FROM_INPUT_FILE)
                console("%sFROM_INPUT_FILE", others++ ? "|" : "");
        if (pc->redirect & REDIRECT_NOT_DONE)
                console("%sREDIRECT_NOT_DONE", others++ ? "|" : "");
        if (pc->redirect & REDIRECT_TO_PIPE)
                console("%sREDIRECT_TO_PIPE", others++ ? "|" : "");
        if (pc->redirect & REDIRECT_TO_STDPIPE)
                console("%sREDIRECT_TO_STDPIPE", others++ ? "|" : "");
        if (pc->redirect & REDIRECT_TO_FILE)
                console("%sREDIRECT_TO_FILE", others++ ? "|" : "");
        if (pc->redirect & REDIRECT_FAILURE)
                console("%sREDIRECT_FAILURE", others++ ? "|" : "");
        if (pc->redirect & REDIRECT_SHELL_ESCAPE)
                console("%sREDIRECT_SHELL_ESCAPE", others++ ? "|" : "");
        if (pc->redirect & REDIRECT_SHELL_COMMAND)
                console("%sREDIRECT_SHELL_COMMAND", others++ ? "|" : "");
        if (pc->redirect & REDIRECT_PID_KNOWN)
                console("%sREDIRECT_PID_KNOWN", others++ ? "|" : "");
        if (pc->redirect & REDIRECT_MULTI_PIPE)
                console("%sREDIRECT_MULTI_PIPE", others++ ? "|" : "");
        console(")\n");

	if (pc->pipe_pid || strlen(pc->pipe_command)) {
		if (pc->pipe_pid && PID_ALIVE(pc->pipe_pid))
			alive = TRUE;
		else
			alive = FALSE;
        	console("pipe_pid: %d (%s) pipe_command: %s\n", 
			pc->pipe_pid, 
			alive ? "alive" : "dead",
			pc->pipe_command);
	}
}

/*
 *  Determine whether the pid receiving the current piped output is still
 *  alive. 
 *
 *  NOTE: This routine returns TRUE by default, and only returns FALSE if
 *        the pipe_pid exists *and* it's known to have died.  Therefore the
 *        caller must be cognizant of pc->pipe_pid or pc->stdpipe_pid.
 */ 
int
output_open(void)
{
	int waitstatus, waitret;

	if (!(pc->flags & TTY)) 
		return TRUE;

	switch (pc->redirect & PIPE_OPTIONS)
	{
	case (REDIRECT_TO_STDPIPE|FROM_COMMAND_LINE):
		waitret = waitpid(pc->stdpipe_pid, &waitstatus, WNOHANG);
		if ((waitret == pc->stdpipe_pid) || (waitret == -1))
               		return FALSE;
		break;

	case (REDIRECT_TO_PIPE|FROM_COMMAND_LINE):
	case (REDIRECT_TO_PIPE|FROM_INPUT_FILE):
		switch (pc->redirect & (REDIRECT_MULTI_PIPE)) 
		{
		case REDIRECT_MULTI_PIPE:
			if (!PID_ALIVE(pc->pipe_pid))
				return FALSE;
			break;

		default:
               		waitret = waitpid(pc->pipe_pid, &waitstatus, WNOHANG);
                	if (waitret == pc->pipe_pid) 
                        	return FALSE;
			if (waitret == -1) {  /* intervening sh */
				if (!PID_ALIVE(pc->pipe_pid))
					return FALSE;
			}
			break;
		}
		break;

	default:
		break;
	}

	return TRUE;
}


/*
 *  Get the pid of the current output command.  This is typically used to 
 *  determine whether it's been killed prematurely.  There's probably a
 *  cleaner way to find out the pid of a popen'd child... 
 *  (like checking /proc/pid/stat files maybe?)
 */
static int
output_command_to_pid(void)
{
	char buf[BUFSIZE];
	char *arglist[MAXARGS];
	int argc;
	int scmd;
	FILE *pipe;

	sprintf(buf, "ps -ef | grep -e '%s'", pc->pipe_command);

	if ((pipe = popen(buf, "r")) == NULL) {
        	error(INFO, "cannot determine scroll pid\n");
		return 0;
	}

	scmd = 0;
	while (fgets(buf, BUFSIZE, pipe)) {
		argc = parse_line(buf, arglist);
		if ((argc >= 8) && 
		    STREQ(arglist[7], pc->pipe_command) &&
		    STRNEQ(pc->my_tty, arglist[5])) {
			scmd = atoi(arglist[1]);
			break;
		}
	}
	pclose(pipe);
	return scmd;
}

/*
 *  Close straggling, piped-to, output commands.
 */
void
close_output(void)
{
        if ((pc->flags & TTY) &&
	    (pc->pipe_pid || strlen(pc->pipe_command)) && 
            output_open()) 
                kill(pc->pipe_pid, 9);
}

/*
 *  Initialize what's needed for the command line:
 *
 *   1. termios structures for raw and cooked terminal mode.
 *   2. set up SIGINT and SIGPIPE handlers for aborted commands. 
 *   3. set up the command history table.
 *   4. create the prompt string.
 */
void
cmdline_init(void)
{
	int fd;

	/*
	 *  Stash a copy of the original termios setup. 
         *  Build a raw version for quick use for each command entry.
	 */ 
	if (isatty(fileno(stdin))) {
        	if ((fd = open("/dev/tty", O_RDONLY)) < 0) 
			error(FATAL, "/dev/tty: %s\n", strerror(errno));
                
		if (tcgetattr(fd, &pc->termios_orig) == -1) 
			error(FATAL, "tcgetattr /dev/tty: %s\n", 
				strerror(errno));

                if (tcgetattr(fd, &pc->termios_raw) == -1) 
			error(FATAL, "tcgetattr /dev/tty: %s\n", 
				strerror(errno));
                 
                close(fd);

		pc->termios_raw.c_lflag &= ~ECHO & ~ICANON;
        	pc->termios_raw.c_cc[VMIN] = (char)1;
        	pc->termios_raw.c_cc[VTIME] = (char)0;

		restore_sanity();

		pc->flags |= TTY;
		set_my_tty();

		SIGACTION(SIGINT, restart, &pc->sigaction, NULL);
		readline_init();
        }
        else {
        	fprintf(fp, pc->flags & SILENT ? 
			"" : "    NOTE: stdin: not a tty\n");
                fflush(fp);
		pc->flags &= ~TTY;
        }

	SIGACTION(SIGPIPE, SIG_IGN, &pc->sigaction, NULL);

	if ((pc->prompt = (char *)malloc(strlen(pc->program_name)+3)) == NULL)
		pc->prompt = "> ";
	else
		sprintf(pc->prompt, "%s> ", pc->program_name);

}

/*
 *  SIGINT, SIGPIPE, and SIGSEGV handler.
 *  Signal number 0 is sent for a generic restart.
 */
#define MAX_RECURSIVE_SIGNALS (10)
#define MAX_SIGINTS_ACCEPTED  (3)

void
restart(int sig)
{
	static int in_restart = 0;

	console("restart (%s) %s\n", signame(sig), 
		pc->flags & IN_GDB ? "(in gdb)" : "(in crash)");

        if (pc->flags & IN_RESTART) {
                fprintf(stderr, 
		   "\nembedded signal received (%s): recursive restart call\n",
			signame(sig));
		if (++in_restart < MAX_RECURSIVE_SIGNALS) 
			return;
		fprintf(stderr, "bailing out...\n");
               	exit(1);
        } else {
		pc->flags |= IN_RESTART;
		in_restart = 0;
	}

	switch (sig) 
	{
        case SIGSEGV:
		fflush(fp);
                fprintf(stderr, "   <segmentation violation%s>\n",
                        pc->flags & IN_GDB ? " in gdb" : "");
        case 0:
	case SIGPIPE:
                restore_sanity();
                break;

	case SIGINT:
		SIGACTION(SIGINT, restart, &pc->sigaction, NULL);
		pc->flags |= _SIGINT_;
		pc->sigint_cnt++;
		pc->flags &= ~IN_RESTART;
		if (pc->sigint_cnt == MAX_SIGINTS_ACCEPTED) {
			restore_sanity();
			break;
		}
		return;

	default:
		fprintf(stderr, "unexpected signal received: %s\n", 
			signame(sig));
		restore_sanity();
		close_output();
		break;
	}

	fprintf(fp, "\n");

	pc->flags &= ~(IN_FOREACH|IN_GDB|IN_RESTART);
	longjmp(pc->main_loop_env, 1);
}

/*
 *  Return a signal name string, or a number if the signal is not listed.
 */
static char *
signame(int sig)
{
	static char sigbuf[20];

	switch (sig)
	{
	case SIGINT:
		sprintf(sigbuf, "SIGINT-%d", pc->sigint_cnt+1);
		return sigbuf;
	case SIGPIPE:
		return "SIGPIPE";
	case SIGSEGV:
		return "SIGSEGV";
	default:
		sprintf(sigbuf, "%d", sig);
		return sigbuf;
	}
}

/*
 *  Restore the program environment to the state it was in before the
 *  last command was executed:  
 *
 *   1. close all temporarily opened pipes and output files.
 *   2. set the terminal back to normal cooked mode.
 *   3. free all temporary buffers.
 *   4. restore the last known output radix.
 */
static void
restore_sanity(void)
{
	int fd, waitstatus;

        if (pc->stdpipe) {
		close(fileno(pc->stdpipe));
                pc->stdpipe = NULL;
		if (pc->stdpipe_pid && PID_ALIVE(pc->stdpipe_pid)) {
			while (!waitpid(pc->stdpipe_pid, &waitstatus, WNOHANG))
				;
		}
		pc->stdpipe_pid = 0;
        }
	if (pc->pipe) {
		close(fileno(pc->pipe));
	 	pc->pipe = NULL;
		if (pc->pipe_pid && PID_ALIVE(pc->pipe_pid)) { 
			while (!waitpid(pc->pipe_pid, &waitstatus, WNOHANG))
				;
		}
		pc->pipe_pid = 0;
	}
	if (pc->ifile_pipe) {
		fflush(pc->ifile_pipe);
		close(fileno(pc->ifile_pipe));
		pc->ifile_pipe = NULL;
        	if (pc->pipe_pid &&
            	    ((pc->redirect & (PIPE_OPTIONS|REDIRECT_PID_KNOWN)) ==
                    (FROM_INPUT_FILE|REDIRECT_TO_PIPE|REDIRECT_PID_KNOWN))) {
			console("wait for redirect %d to finish...\n",
				pc->pipe_pid);
                	while (PID_ALIVE(pc->pipe_pid))
				waitpid(pc->pipe_pid, &waitstatus, WNOHANG);
			if (pc->redirect & (REDIRECT_MULTI_PIPE))
				wait_for_children(ALL_CHILDREN);
		}
	}

	if (pc->ofile) {
		fclose(pc->ofile);
		pc->ofile = NULL;
	}
	if (pc->ifile_ofile) {
		fclose(pc->ifile_ofile);
		pc->ifile_ofile = NULL;
	}

	if (pc->ifile) {
		fclose(pc->ifile);
		pc->ifile = NULL;
	}

	if (pc->tmpfile) {
		close_tmpfile();
	}

	if (pc->tmpfile2) {
		close_tmpfile2();
	}

	if (pc->flags & TTY) {
		if ((fd = open("/dev/tty", O_RDONLY)) < 0) 
			error(FATAL, "/dev/tty: %s\n", strerror(errno));
	        
	        if (tcsetattr(fd, TCSANOW, &pc->termios_orig) == -1) 
                        error(FATAL, "tcsetattr /dev/tty: %s\n",
                                strerror(errno));
	        
		close(fd);
	}

	wait_for_children(ZOMBIES_ONLY);

	pc->flags &= ~(RUNTIME_IFILE|_SIGINT_);
	pc->sigint_cnt = 0;
	pc->redirect = 0;
	pc->pipe_command[0] = NULLCHAR;
	pc->pipe_pid = 0;
	pc->sbrk = sbrk(0);

	restore_gdb_sanity();

	free_all_bufs();

	/*
	 *  Clear the structure cache references -- no-ops if DUMPFILE().
	 */
	clear_task_cache();
	clear_machdep_cache();
	clear_swap_info_cache();
	clear_file_cache();
	clear_dentry_cache();
	clear_inode_cache();
	clear_vma_cache();

	if (MCLXDEBUG(2)) {
                dump_filesys_table(0);
		dump_vma_cache(0);
		dump_text_value_cache(0);
	}
	
	if (REMOTE())
		remote_clear_pipeline();
}

/*
 *  Similar to above, but only called in between each command that is
 *  read from an input file.
 */
static void
restore_ifile_sanity(void)
{
        int fd;

        if (pc->ifile_pipe) {
		close(fileno(pc->ifile_pipe));
                pc->ifile_pipe = NULL;
        }

        if (pc->ifile_ofile) {
                fclose(pc->ifile_ofile);
                pc->ifile_ofile = NULL;
        }

        if (pc->flags & TTY) {
                if ((fd = open("/dev/tty", O_RDONLY)) < 0) 
			error(FATAL, "/dev/tty: %s\n", strerror(errno));
                
                if (tcsetattr(fd, TCSANOW, &pc->termios_orig) == -1) 
			error(FATAL, "tcsetattr /dev/tty: %s\n",
                                strerror(errno));
                
                close(fd);
        }

	if (pc->tmpfile2) {
		close_tmpfile2();
	}

	restore_gdb_sanity();

	free_all_bufs();
}

/*
 *  Check whether a SIGINT was received during the execution of a command,
 *  clearing the flag if it was set.  This allows individual commands or
 *  entities to do whatever is appropriate to handle CTRL-C.
 */
int
received_SIGINT(void)
{
	if (pc->flags & _SIGINT_) {
		pc->flags &= ~_SIGINT_;
		pc->sigint_cnt = 0;
		return TRUE;
	} else 
		return FALSE;
}


/*
 *  Look for an executable file that begins with #!
 */
static int
is_shell_script(char *s)
{
        int fd;
        char interp[2];
        struct stat sbuf;

        if ((fd = open(s, O_RDONLY)) < 0) 
                return FALSE;
        
        if (isatty(fd)) 
                return FALSE;
        
        if (read(fd, interp, 2) != 2) {
                close(fd);
                return FALSE;
        }

        if (!STRNEQ(interp, "#!")) {
                close(fd);
                return FALSE;
        }

        close(fd);

        if (stat(s, &sbuf) == -1) 
		return FALSE;

        if (!(sbuf.st_mode & (S_IXUSR|S_IXGRP|S_IXOTH))) 
		return FALSE;
        
        return TRUE;
}

/*
 *  After verifying the user's input file, loop through each line, executing
 *  one command at a time.  This command pretty much does the same as
 *  get_command_line(), but also kicks off the command execution as well.  
 *  It's kept self-contained, as indicated by the RUNTIME_IFILE flag, and 
 *  keeps its own internal sanity by calling restore_ifile_sanity() between 
 *  each line.
 */ 
void
exec_input_file(void)
{
        char *file;
	FILE *incoming_fp;
        char buf[BUFSIZE];
	ulong this;

	/*
	 *  Do start-up .rc or input files in the proper order.
	 */
	if (pc->flags & RCHOME_IFILE) {
		this = RCHOME_IFILE;
		pc->flags &= ~RCHOME_IFILE;
	} else if (pc->flags & RCLOCAL_IFILE) {
		this = RCLOCAL_IFILE;
		pc->flags &= ~RCLOCAL_IFILE;
	} else if (pc->flags & CMDLINE_IFILE) {
		this = CMDLINE_IFILE;
		pc->flags &= ~CMDLINE_IFILE;
	} else
		this = 0;


        if (pc->flags & RUNTIME_IFILE) {
                error(INFO, "embedded input files not allowed!\n");
                return;
        }

        if (argcnt < 2) {
                error(INFO, "no input file entered!\n");
                return;
        } else
                file = args[1];

        if (!file_exists(file, NULL)) {
                error(INFO, "%s: %s\n", file, strerror(ENOENT));
                return;
        }

        if (is_elf_file(file)) {
                error(INFO, "input from executable files not supported yet!\n");
                return;
        }

        if (is_shell_script(file)) {
                error(INFO, "input from shell scripts not supported yet!\n");
                return;
        }

        if ((pc->ifile = fopen(file, "r")) == NULL) {
                error(INFO, "%s: %s\n", file, strerror(errno));
                return;
        }

        pc->flags |= RUNTIME_IFILE;
	incoming_fp = fp;

        while (fgets(buf, BUFSIZE-1, pc->ifile)) {
                /*
                 *  Restore normal environment.
                 */
                fp = incoming_fp;
		restore_ifile_sanity();
        	BZERO(pc->command_line, BUFSIZE);
        	BZERO(pc->orig_line, BUFSIZE);

		if (STRNEQ(buf, "#") || STREQ(buf, "\n"))
			continue;

                check_special_handling(buf);
                strcpy(pc->command_line, buf);
                clean_line(pc->command_line);
                strcpy(pc->orig_line, pc->command_line);
		strip_linefeeds(pc->orig_line);
		resolve_aliases();

	        switch (setup_redirect(FROM_INPUT_FILE))
	        {
	        case REDIRECT_NOT_DONE:
	        case REDIRECT_TO_PIPE:
	        case REDIRECT_TO_FILE:
	                break;
	
		case REDIRECT_SHELL_ESCAPE:
		case REDIRECT_SHELL_COMMAND:
			continue;

	        case REDIRECT_FAILURE:
	                goto done_input;
	        }

		if (MCLXDEBUG(1))
			console(buf);

		if (!(argcnt = parse_line(pc->command_line, args)))
			continue;

		if ((this & (RCHOME_IFILE|RCLOCAL_IFILE)) &&
		    (STREQ(args[0], "set") || STREQ(args[0], "alias")))
			continue;

                if (!(pc->flags & SILENT)) {
                        fprintf(fp, "%s%s", pc->prompt, buf);
                        fflush(fp);
                }

                exec_command();
        }

done_input:

        fclose(pc->ifile);
        pc->ifile = NULL;
        pc->flags &= ~RUNTIME_IFILE;
}

/*
 *  Prime the alias list with a few built-in's.
 */
void
alias_init(void)
{
	char buf[BUFSIZE];

	strcpy(buf, "alias man help");
	argcnt = parse_line(buf, args);
	allocate_alias(ALIAS_BUILTIN);

        strcpy(buf, "alias ? help");
        argcnt = parse_line(buf, args);
        allocate_alias(ALIAS_BUILTIN);

        strcpy(buf, "alias quit q");
        argcnt = parse_line(buf, args);
        allocate_alias(ALIAS_BUILTIN);

	strcpy(buf, "alias sf set scroll off");
        argcnt = parse_line(buf, args);
        allocate_alias(ALIAS_BUILTIN);

	strcpy(buf, "alias sn set scroll on");
        argcnt = parse_line(buf, args);
        allocate_alias(ALIAS_BUILTIN);

	strcpy(buf, "alias hex set radix 16");
        argcnt = parse_line(buf, args);
        allocate_alias(ALIAS_BUILTIN);

        strcpy(buf, "alias dec set radix 10");
        argcnt = parse_line(buf, args);
        allocate_alias(ALIAS_BUILTIN);

        strcpy(buf, "alias g gdb");
        argcnt = parse_line(buf, args);
        allocate_alias(ALIAS_BUILTIN);

        strcpy(buf, "alias px p -x");
        argcnt = parse_line(buf, args);
        allocate_alias(ALIAS_BUILTIN);

        strcpy(buf, "alias pd p -d");
        argcnt = parse_line(buf, args);
        allocate_alias(ALIAS_BUILTIN);

	strcpy(buf, "alias for foreach");
        argcnt = parse_line(buf, args);
        allocate_alias(ALIAS_BUILTIN);

	strcpy(buf, "alias size *");
        argcnt = parse_line(buf, args);
        allocate_alias(ALIAS_BUILTIN);

        strcpy(buf, "alias dmesg log");
        argcnt = parse_line(buf, args);
        allocate_alias(ALIAS_BUILTIN);

}

/*
 *  Before the command line is parsed, take a snapshot and parse the snapshot.
 *  If args[0] is an known alias, recreate the pc->command_line string with 
 *  the alias substitution.
 */
static void
resolve_aliases(void)
{
	int i;
	struct alias_data *ad;
	int found;
	char *p1, *remainder;
	char buf1[BUFSIZE];
	char buf2[BUFSIZE];

	if (!strlen(pc->command_line))
		return;

	strcpy(buf1, pc->command_line);
	argcnt = parse_line(buf1, args);

	if (argcnt > 1) {
		strcpy(buf2, &pc->command_line[args[1] - buf1]);
		remainder = buf2;
	} else
		remainder = NULL;

	found = FALSE;
	for (ad = alias_head.next; ad; ad = ad->next) {
		if (STREQ(ad->alias, args[0])) {
                        for (i = 0; i < ad->argcnt; i++)
                                args[i] = ad->args[i];
			found = TRUE;
			break;
		}
	}

	if (!found)
		return;

	BZERO(pc->command_line, BUFSIZE);
	p1 = pc->command_line;

	for (i = 0; i < ad->argcnt; i++) {
		sprintf(p1, "%s ", args[i]);
		while (*p1)
			p1++;
	}
	if (remainder) 
		strcat(pc->command_line, remainder);

	clean_line(pc->command_line);
}

/*
 *  If input string is an alias, return a pointer to the alias_data struct.
 */
struct alias_data *
is_alias(char *s)
{
        struct alias_data *ad;

        for (ad = alias_head.next; ad; ad = ad->next) {
		if (STREQ(ad->alias, s)) 
			return(ad);
	}
	return NULL;
}

/*
 *  .rc file commands that consist of either "set" or "alias" commands
 *  are performed during initialization.  Determine which is being requested, 
 *  and pass the line to either cmd_set() or allocate_alias().  If any
 *  runtime commands are found, flag them for later execution by 
 *  exec_rc_commands().
 */
void
resolve_rc_cmd(char *s, int origin)
{
	int i;
	struct alias_data *ad;

	clean_line(s);

	if (*s == '#')
		return;

	if ((argcnt = parse_line(s, args)) == 0)
		return;

	/*
	 *  If the command is a built-in alias of "set", switch the
         *  args so that it will be caught by the subsequent set-string
         *  check below.
	 */
	if ((argcnt == 1) && 
	    (ad = is_alias(args[0])) &&
	    STREQ(ad->args[0], "set")) {
		argcnt = ad->argcnt;
		for (i = 0; i < ad->argcnt; i++)
			args[i] = ad->args[i];
	}

	if (STREQ(args[0], "set")) {
		optind = 0;
		cmd_set();
	} else if (STREQ(args[0], "alias") && (argcnt > 2))
		allocate_alias(origin);
	else {
        	switch (origin)
        	{
        	case ALIAS_RCHOME:
                	pc->flags |= RCHOME_IFILE;
                	break;
        	case ALIAS_RCLOCAL:
                	pc->flags |= RCLOCAL_IFILE;
               	 	break;
        	}
	}

	return;
}


/*
 *  The "alias" command.  With no arguments, list all aliases. With one
 *  argument -- which must be an alias -- display the string it's aliased to.
 *  With two or more arguments, setup a new alias, where the first argument
 *  is the alias, and the remaining arguments make up the alias string.
 *  If the second arg is the NULL string "", delete the alias.
 */   
void
cmd_alias(void)
{
	if (argerrs)
		cmd_usage(pc->curcmd, SYNOPSIS);

	switch (argcnt)
	{
	case 1:
		list_aliases(NULL);
		break;

	case 2:
		list_aliases(args[1]);
		break;
	
	default:
		if (allocate_alias(ALIAS_RUNTIME))
			list_aliases(args[1]);
		break;
	}
}

/*
 *  Dump the current set of aliases.
 */
static void
list_aliases(char *s)
{
	int i;
        struct alias_data *ad;
	int found, precision;
	char buf[BUFSIZE];

	if (!alias_head.next) {
		error(INFO, "alias list is empty\n");
		return;
	}

	BZERO(buf, BUFSIZE);
	found = FALSE;
	precision = 7;

        for (ad = alias_head.next; ad; ad = ad->next) {
                switch (ad->origin)
		{
                case ALIAS_RCLOCAL:
                        sprintf(buf, ".%src", pc->program_name);
			if (strlen(buf) > precision)
				precision = strlen(buf);
                        break;
                case ALIAS_RCHOME:
                        sprintf(buf, "$HOME/.%src", pc->program_name);
			if (strlen(buf) > precision)
				precision = strlen(buf);
			break;
		}
	}

	fprintf(fp, "ORIGIN");
	pad_line(fp, precision-6, ' ');

	BZERO(buf, BUFSIZE);
	fprintf(fp, "  ALIAS    COMMAND\n");

        for (ad = alias_head.next; ad; ad = ad->next) {
		if (s && !STREQ(s, ad->alias))
			continue;

		found = TRUE;

                switch (ad->origin)
                {
                case ALIAS_RUNTIME:
                        sprintf(buf, "runtime");
                        break;
                case ALIAS_RCLOCAL:
                        sprintf(buf, ".%src", pc->program_name);
                        break;
                case ALIAS_RCHOME:
                        sprintf(buf, "$HOME/.%src", pc->program_name);
                        break;
                case ALIAS_BUILTIN:
                        sprintf(buf, "builtin");
                        break;
                }

		fprintf(fp, "%s  ", buf);
		pad_line(fp, precision-strlen(buf), ' ');

                fprintf(fp, "%-7s  ", ad->alias);

		for (i = 0; i < ad->argcnt; i++) {
			fprintf(fp, "%s ", ad->args[i]);
		}
		fprintf(fp, "\n");
	}

	if (s && !found)
		fprintf(fp, "alias does not exist: %s\n", s);
		
}

/*
 *  Verify the alias request set up in the args[] array: 
 *
 *    1. make sure that the alias string starts with a legitimate command.
 *    2. if the already exists, deallocate its current version.
 *   
 *  Then malloc space for the alias string, and link it in to the alias list.
 */
static int
allocate_alias(int origin)
{
	int i;
	int size;
        struct alias_data *ad;
        struct alias_data *newad;
	char *p1, *enclosed_string;
	int found;

	if ((enclosed_string = strstr(args[2], " ")))
		*enclosed_string = NULLCHAR;

	found = FALSE;

	if (get_command_table_entry(args[1])) {
                error(INFO, "cannot alias existing command name: %s\n", 
			args[1]);
                return FALSE;
	}

	if (get_command_table_entry(args[2])) 
		found = TRUE;

	if (!found) {
		if (!strlen(args[2])) {
			if (alias_exists(args[1])) {
				deallocate_alias(args[1]);
				fprintf(fp, "alias deleted: %s\n", args[1]);
			}
		} else {
			error(INFO, 
		          "invalid alias attempt on non-existent command: %s\n",
				args[2]);
		}
		return FALSE;
	} 

	if (alias_exists(args[1]))
		deallocate_alias(args[1]);

	if (enclosed_string)
		*enclosed_string = ' ';

	size = sizeof(struct alias_data) + argcnt;
	for (i = 0; i < argcnt; i++) 
		size += strlen(args[i]);

        if ((newad = (struct alias_data *)malloc(size+1)) == NULL) {
                error(INFO, "alias_data malloc: %s\n", strerror(errno));
                return FALSE;
        }

	BZERO(newad, size);
	newad->next = NULL;
	newad->size = size;
	newad->origin = origin;

	p1 = newad->argbuf;
	for (i = 1; i < argcnt; i++) {
                sprintf(p1, "%s ", args[i]);
		while (*p1)
			p1++;
	}
	p1 = strstr(newad->argbuf, " ");
	*p1 = NULLCHAR;

	newad->alias = newad->argbuf;
	newad->argcnt = parse_line(p1+1, newad->args); 

	for (ad = &alias_head; ad->next; ad = ad->next) 
		;
	ad->next = newad;

	return TRUE;
}


/*
 *  Check whether the passed-in string is a currently-existing alias.
 */
static int
alias_exists(char *s)
{
        struct alias_data *ad;

        if (!alias_head.next) 
                return FALSE;

        for (ad = alias_head.next; ad; ad = ad->next) 
		if (STREQ(ad->alias, s)) 
			return TRUE;

	return FALSE;
}

/*
 *  If the passed-in string is an alias, delink it and free its memory. 
 */
void
deallocate_alias(char *s)
{
        struct alias_data *ad, *lastad;

        for (ad = alias_head.next, lastad = &alias_head; ad; ad = ad->next) {
                if (!STREQ(ad->alias, s)) { 
			lastad = ad;
                        continue;
		}

		lastad->next = ad->next;
		free(ad);
		break;
	}
}

/*
 *  "help -a" output
 */
void
dump_alias_data(void)
{
        int i;
        struct alias_data *ad;

	fprintf(fp, "alias_head.next: %lx\n\n", (ulong)alias_head.next);

        for (ad = alias_head.next; ad; ad = ad->next) {
        	fprintf(fp, "      next: %lx\n", (ulong)ad->next);
        	fprintf(fp, "     alias: %s\n", ad->alias);
        	fprintf(fp, "      size: %d\n", ad->size);
        	fprintf(fp, "    origin: ");
		switch (ad->origin)
		{
		case ALIAS_RUNTIME:
			fprintf(fp, "runtime setting \n");
			break;
		case ALIAS_RCLOCAL:
			fprintf(fp, ".%src \n", pc->program_name);
			break;
		case ALIAS_RCHOME:
			fprintf(fp, "$HOME/.%src \n", pc->program_name);
			break;
		case ALIAS_BUILTIN:
			fprintf(fp, "builtin\n");
			break;
		}
        	fprintf(fp, "    argcnt: %d\n", ad->argcnt);
        	for (i = 0; i < ad->argcnt; i++)
                	fprintf(fp, "   args[%d]: %lx: %s\n", 
				i, (ulong)ad->args[i], ad->args[i]);
                fprintf(fp, "\n");
        }
}


/*
 *  Repeat a command on a live system.
 */
void
cmd_repeat(void)
{
	ulong delay;
	char buf[BUFSIZE]; 
	char bufsave[BUFSIZE];

	if (argcnt == 1)
		cmd_usage(pc->curcmd, SYNOPSIS);

	delay = 0;

	if (args[1][0] == '-') {
		switch (args[1][1])
		{
		default:
		case NULL:
			cmd_usage(pc->curcmd, SYNOPSIS);

		case '1':
		case '2':
		case '3':
		case '4':
		case '5':
		case '6':
		case '7':
		case '8':
		case '9':
		case '0':
			delay = dtol(&args[1][1], FAULT_ON_ERROR, NULL);
			concat_args(buf, 2, FALSE);
			break;
		}
	} else 
		concat_args(buf, 1, FALSE);

#ifdef TODO
	/* 
	 * make aliases work... 
	 */
	strcpy(pc->command_line, buf);
	resolve_aliases();
	if (!argcnt)
		return;
	concat_args(buf, 0, FALSE);
#endif

	strcpy(bufsave, buf);
	argcnt = parse_line(buf, args);
	if (!argcnt)
		return;

	while (TRUE) {
		optind = 0;
		exec_command();
		free_all_bufs();

		if (received_SIGINT())
			break;

		if (delay)
			sleep(delay);

		strcpy(buf, bufsave);
		argcnt = parse_line(buf, args);
	}
}

/*
 *  Initialize readline, set the editing mode, and then perform any 
 *  crash-specific bindings, etc.
 */
static void
readline_init(void)
{               
        rl_initialize();

	if (STREQ(pc->editing_mode, "vi")) {
		rl_editing_mode = vi_mode;

		rl_bind_key(CTRL('N'), rl_get_next_history);
		rl_bind_key(CTRL('P'), rl_get_previous_history);

		rl_bind_key_in_map(CTRL('P'), rl_get_previous_history,
			vi_insertion_keymap);
		rl_bind_key_in_map(CTRL('N'), rl_get_next_history,
			vi_insertion_keymap);

		rl_generic_bind(ISFUNC, "[A", (char *)rl_get_previous_history, 
			vi_movement_keymap);
		rl_generic_bind(ISFUNC, "[B", (char *)rl_get_next_history, 
			vi_movement_keymap);
	}

	if (STREQ(pc->editing_mode, "emacs")) {
        	rl_editing_mode = emacs_mode;
	}
}

/*
 *  Find and set the tty string of this session as seen in "ps -ef" output. 
 */
static void
set_my_tty(void)
{
        char buf[BUFSIZE];
        char *arglist[MAXARGS];
        int argc;
        FILE *pipe;

        strcpy(pc->my_tty, "?");

        sprintf(buf, "ps -ef | grep ' %d '", getpid());

        if ((pipe = popen(buf, "r")) == NULL) 
                return;

        while (fgets(buf, BUFSIZE, pipe)) {
                argc = parse_line(buf, arglist);
                if ((argc >= 8) && (atoi(arglist[1]) == getpid())) {
			if (strlen(arglist[5]) < 9)
				strcpy(pc->my_tty, arglist[5]);
			else
				strncpy(pc->my_tty, arglist[5], 9); 
                }
        }
        pclose(pipe);
}

/*
 *  Check whether SIGINT's are allowed before shipping a request off to gdb.
 */
int
interruptible(void)
{
	if (!(pc->flags & RUNTIME))
		return FALSE;

	if (!(pc->flags & TTY))
		return FALSE;

	if ((pc->redirect & (FROM_INPUT_FILE|REDIRECT_NOT_DONE)) ==
	    (FROM_INPUT_FILE|REDIRECT_NOT_DONE)) 
		return TRUE;

	if (strlen(pc->pipe_command))
		return FALSE;
		
	return TRUE;
}


/*
 *  Set up the standard output pipe using whichever was selected during init.
 */

static char *less_argv[5] = {
	"/usr/bin/less",
	"-E",
	"-X",
        "-Ps -- MORE --  forward\\: <SPACE>, <ENTER> or j  backward\\: b or k  quit\\: q",
	NULL
};

static char *more_argv[2] = {
	"/bin/more",
	NULL
};

static int
setup_stdpipe(void)
{
	char *path;

	if (pipe(pc->pipefd) < 0) {
		error(INFO, "pipe system call failed: %s", strerror(errno));
		return FALSE;
	}

	if ((pc->stdpipe_pid = fork()) < 0) {
		error(INFO, "fork system call failed: %s", strerror(errno));
		return FALSE;
	}

	if (pc->stdpipe_pid > 0) {               
		pc->redirect |= REDIRECT_PID_KNOWN;

		close(pc->pipefd[0]);    /* parent closes read end */

		if ((pc->stdpipe = fdopen(pc->pipefd[1], "w")) == NULL) {
			error(INFO, "fdopen system call failed: %s", 
				strerror(errno));
			return FALSE;
		}
		setbuf(pc->stdpipe, NULL);

                switch (pc->scroll_command)
                {
                case SCROLL_LESS:
                        strcpy(pc->pipe_command, less_argv[0]);
                        break;
                case SCROLL_MORE:
                        strcpy(pc->pipe_command, more_argv[0]);
                        break;
                }

		if (MCLXDEBUG(2))
			console("pipe: %lx\n", pc->stdpipe);
		return TRUE;;

	} else {                        
		close(pc->pipefd[1]);    /* child closes write end */

		if (dup2(pc->pipefd[0], 0) != 0) {
			perror("child dup2 failed");
			exit(1);
		}

		if (MCLXDEBUG(2))
			console("execv: %d\n", getpid());

                switch (pc->scroll_command)
		{
		case SCROLL_LESS:
			path = less_argv[0];
			execv(path, less_argv);
			break;

                case SCROLL_MORE:
			path = more_argv[0];
			execv(path, more_argv);
			break;
		}

		perror("child execv failed"); 
		exit(1);
	}
}

static void 
wait_for_children(ulong waitflag)
{
        int status, pid;

	while (TRUE) {
        	switch (pid = waitpid(-1, &status, WNOHANG))
        	{
        	case  0:
			if (MCLXDEBUG(2))
			    console("wait_for_children: child running...\n");
			if (waitflag == ZOMBIES_ONLY)
				return;
			break;

        	case -1:
			if (MCLXDEBUG(2))
			    console("wait_for_children: no children alive\n");
                	return;

        	default:
			console("wait_for_children: reaped %d\n", pid);
			if (MCLXDEBUG(2))
			    fprintf(fp, "wait_for_children: reaped %d\n", pid);
                	break;
        	}
	}
}