aboutsummaryrefslogtreecommitdiffhomepage
path: root/exec.c
blob: e15ec9b0fc2956e264c34261b85e33ca94799757 (plain)
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
/** \file exec.c
	Functions for executing a program.

	Some of the code in this file is based on code from the Glibc
	manual, though I the changes performed have been massive.
*/

#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <wchar.h>
#include <string.h>
#include <limits.h>
#include <signal.h>
#include <sys/wait.h>
#include <assert.h>
#include <dirent.h>

#include "config.h"
#include "util.h"
#include "common.h"
#include "wutil.h"
#include "proc.h"
#include "exec.h"
#include "parser.h"
#include "builtin.h"
#include "function.h"
#include "env.h"
#include "wildcard.h"
#include "sanity.h"
#include "expand.h"
#include "env_universal.h"

/**
   Prototype for the getpgid library function. The prototype for this
   function seems to be missing in glibc, at least I've not found any
   combination of #includes, macros and compiler switches that will
   include it.
*/
pid_t getpgid( pid_t pid );

/**
   file descriptor redirection error message
*/
#define FD_ERROR   L"An error occurred while redirecting file descriptor %d"
/**
   file redirection error message
*/
#define FILE_ERROR L"An error occurred while redirecting file '%ls'"
/**
   pipe redirection error message
*/
#define PIPE_ERROR L"An error occurred while setting up pipe"
/**
   fork error message
*/
#define FORK_ERROR L"Could not create child process - exiting"

/**
   Make sure the fd used by this redirection is not used by i.e. a pipe. 
*/
void free_fd( io_data_t *io, int fd )
{
	if( !io )
		return;
	
	if( io->io_mode == IO_PIPE )
	{
		int i;
		for( i=0; i<2; i++ )
		{
			if(io->pipe_fd[i] == fd )
			{
				while(1)
				{
					if( (io->pipe_fd[i] = dup(fd)) == -1)
					{
						if( errno != EINTR )
						{
							debug( 1, 
								   FD_ERROR,
								   fd );							
							wperror( L"dup" );
							exit(1);
						}
					}
					else
						break;
				}
			}
		}
	}
}


static void handle_child_io( io_data_t *io )
{

	if( env_universal_server.fd >= 0 )
		close( env_universal_server.fd );

	for( ; io; io=io->next )
	{
		int tmp;

		if( io->io_mode == IO_FD && io->fd == io->old_fd )
		{
			continue;
		}

		if( io->fd > 2 )
		{
			/*
			  Make sure the fd used by this redirection is not used by i.e. a pipe. 
			*/
			free_fd( io, io->fd );
		}
				
		
		if( close(io->fd) == -1 )
		{
/*			debug( 1, 
				   FD_ERROR,
				   io->fd );
			wperror( L"close" );
			exit(1);*/
		}
		
		switch( io->io_mode )
		{
			case IO_CLOSE:
				break;
			case IO_FILE:
				if( (tmp=wopen( io->filename, io->flags, 0666 ))==-1 )
				{
					debug( 1, 
						   FILE_ERROR,
						   io->filename );
					
					wperror( L"open" );
					exit(1);
				}				
				else if( tmp != io->fd)
				{
					if(dup2( tmp, io->fd ) == -1 )
					{
						debug( 1, 
							   FD_ERROR,
							   io->fd );
						wperror( L"dup2" );
						exit(1);
					}
					if( close( tmp ) == -1 )
					{
						debug( 1, 
							   FD_ERROR,
							   io->fd );
						wperror( L"close" );
						exit(1);
					}
				}				
				break;
			case IO_FD:
/*				debug( 3, L"Redirect fd %d in process %ls (%d) from fd %d",
  io->fd,
  p->actual_cmd,
  p->pid,
  io->old_fd );
*/			
				if( dup2( io->old_fd, io->fd ) == -1 )
				{
					debug( 1, 
						   FD_ERROR,
						   io->fd );
					wperror( L"dup2" );
					exit(1);					
				}
				break;
				
			case IO_BUFFER:
			case IO_PIPE:
			{
				
/*				debug( 3, L"Pipe fd %d in process %ls (%d) (Through fd %d)", 
  io->fd, 
  p->actual_cmd,
  p->pid,
  io->pipe_fd[io->fd] );
*/
				int fd_to_dup = io->fd;//io->io_mode==IO_BUFFER?1:(io->fd?1:0);
				
				if( dup2( io->pipe_fd[fd_to_dup], io->fd ) == -1 )
				{
					debug( 1, PIPE_ERROR );
					wperror( L"dup2" );
					exit(1);
				}

				if( fd_to_dup )
				{
					close( io->pipe_fd[0]);
					close( io->pipe_fd[1]);
				}
				else
					close( io->pipe_fd[fd_to_dup] );
				
/*				
  if( close( io[i].pipe_fd[ io->fd ] ) == -1 )
  {
  wperror( L"close" );
  exit(1);
  }
*/			
/*				if( close( io[i].pipe_fd[1] ) == -1 )
  {
  wperror( L"close" );
  exit(1);
  }
*/
		
/*		    fprintf( stderr, "fd %d points to fd %d\n", i, io[i].fd[i>0?1:0] );*/
				break;
			}
			
		}
	}
}


static void setup_child_process( job_t *j )
{
	if( is_interactive && !is_subshell && !is_block)
    {
		pid_t pid;
		/* 
		   Put the process into the process group and give the process group
		   the terminal, if appropriate.
		   This has to be done both by the shell and in the individual
		   child processes because of potential race conditions.  
		*/
		pid = getpid ();
		if (j->pgid == 0)
			j->pgid = pid;

		/* Wait till shell puts os in our own group */
		while( getpgrp() != j->pgid )
			sleep(0);
		
		/* Wait till shell gives us stdin */
		if ( j->fg )
		{
			while( tcgetpgrp( 0 ) != j->pgid )
				sleep(0);
		}
	}
	
	/* Set the handling for job control signals back to the default.  */
	signal (SIGINT, SIG_DFL);
	signal (SIGQUIT, SIG_DFL);
	signal (SIGTSTP, SIG_DFL);
	signal (SIGTTIN, SIG_DFL);
	signal (SIGTTOU, SIG_DFL);
	signal (SIGCHLD, SIG_DFL);
	
	sigset_t chldset; 
	sigemptyset( &chldset );
	sigaddset( &chldset, SIGCHLD );
	sigprocmask(SIG_UNBLOCK, &chldset, 0);
	
	handle_child_io( j->io );
}

								 
								 
/**
   This function is executed by the child process created by a call to
   fork().  This function begins by updating FDs as specified by the io
   parameter.  If the command requested for this process is not a
   builtin, execv is then called with the appropriate parameters. If
   the command is a builtin, the contents of out and err are printed.
*/
static void launch_process( process_t *p )
{
	
	/* Set the standard input/output channels of the new process.  */
		
	execve (wcs2str(p->actual_cmd), wcsv2strv( (const wchar_t **) p->argv), env_export_arr() );
	debug( 0, 
		   L"Failed to execute process %ls",
		   p->actual_cmd );
	wperror( L"execve" );
	exit(1);
}


/**
   Check if the IO redirection chains contains redirections for the specified file descriptor
*/

static int has_fd( io_data_t *d, int fd )
{
	return io_get( d, fd ) != 0;
}


/**
   Read from descriptors until they are empty. 
*/
static void read_all( io_data_t *d )
{
	
//io_data_t *d, *prev=0;
	
/*for( d = io; d; d=d->next )
  {*/
	
	if( d->io_mode == IO_BUFFER )
	{
		
		if( fcntl( d->pipe_fd[0], F_SETFL, 0 ) )
		{
			wperror( L"fcntl" );
			return;
		}

		debug( 3, L"read_all: blocking read on fd %d", d->pipe_fd[0] );
		
		while(1)
		{
			char b[4096];
			int l;
			l=read_blocked( d->pipe_fd[0], b, 4096 );			
			if( l==0 )
			{
				
/*if( prev )
  {
  prev->next = d->next;
  }
  else
  {
  j->io=d->next;
  }
  removed = true;*/
				break;
				
				
			}
			else if( l<0 )
			{
				debug( 1, 
					   L"An error occured while reading output from code block on fd %d", d->pipe_fd[0] );
				wperror( L"read_all" );				
				break;				
			}
			else
			{				
				b_append( d->out_buffer, b, l );				
			}
		}
	}
	
//if( !removed )
//prev=d;
//}
	
//if( current_block->io )
//fwprintf( stderr, L"read_all ended %ls\n", j->command );
}
io_data_t *exec_make_io_buffer()
{
	io_data_t *buffer_redirect = malloc( sizeof( io_data_t ));
	
	buffer_redirect->io_mode=IO_BUFFER;
	buffer_redirect->next=0;
	buffer_redirect->out_buffer= malloc( sizeof(buffer_t));
	b_init( buffer_redirect->out_buffer );
	buffer_redirect->fd=1;


	if( pipe( buffer_redirect->pipe_fd ) == -1 )
	{
		debug( 1, PIPE_ERROR );
		wperror (L"pipe");
		free(  buffer_redirect->out_buffer );
		free( buffer_redirect );
		return 0;
	}
	else if( fcntl( buffer_redirect->pipe_fd[0], F_SETFL, O_NONBLOCK ) )
	{
		debug( 1, PIPE_ERROR );
		wperror( L"fcntl" );
		free(  buffer_redirect->out_buffer );
		free( buffer_redirect );
		return 0;
	}
	return buffer_redirect;
}

void exec_free_io_buffer( io_data_t *io_buffer )
{
	if( close( io_buffer->pipe_fd[0] ) == -1)
	{
		debug( 1, PIPE_ERROR );
		wperror( L"close" );
		
	}

	/*
	  Dont free fd for writing. This should already be free'd before calling read_all on the buffer
	*/
//	close( io_buffer->pipe_fd[1] );
	
	b_destroy( io_buffer->out_buffer );
	
	free( io_buffer->out_buffer );
	free( io_buffer );
}


/**
   Make a copy of the specified io redirection chain, but change file redirection into fd redirection. 
*/

static io_data_t *io_transmogrify( io_data_t * in )
{
	io_data_t *out;

	if( !in )
		return 0;
	
	out = malloc( sizeof( io_data_t ) );
	if( !out )
		die_mem();
	
	out->fd = in->fd;
	out->io_mode = IO_FD;
	out->close_old = 1;
	
	switch( in->io_mode )
	{
		/*
		  These redirections don't need transmogrification. They can be passed through.
		*/
		case IO_FD:
		case IO_CLOSE:
		case IO_BUFFER:
		case IO_PIPE:
		{
			memcpy( out, in, sizeof(io_data_t));
			break;
		}

		/*
		  Transmogrify file redirections
		*/
		case IO_FILE:
		{
			int fd;
			
			if( (fd=wopen( in->filename, in->flags, 0666 ))==-1 )
			{
				debug( 1, 
					   FILE_ERROR,
					   in->filename );
								
				wperror( L"open" );
				exit(1);
			}	

			out->old_fd = fd;
/*
  fwprintf( stderr,
  L"Replacing call to redirect %d to file '%ls' with call to redirect to fd %d\n", 
  in->fd,
  in->filename,
  fd );			
*/		
			break;
		}
	}
	
	out->next = io_transmogrify( in->next );
	
	return out;
}

/**
   Free a transmogrified io chain.
*/
static void io_untransmogrify( io_data_t * in, io_data_t *out )
{
	if( !in )
		return;
	io_untransmogrify( in->next, out->next );
	switch( in->io_mode )
	{
		case IO_FILE:
			if( close( out->old_fd ) == -1 )
			{
				debug( 1, 
					   FILE_ERROR,
					   in->filename );				
				wperror( L"close" );				
			}
			break;
	}	
	free(out);
}


/**
   Morph an io redirection chain into redirections suitable for
   passing to eval, call eval, and clean up morphed redirections.
   

   \param def the code to evaluate
   \param block_type the type of block to push on evaluation
   \param io the io redirections to be performed on this block
*/

static int internal_exec_helper( const wchar_t *def, 
								 int block_type,
								 io_data_t *io )
{
	int res=0;
	io_data_t *io_internal = io_transmogrify( io );
	int is_block_old=is_block;
	is_block=1;
	
	eval( def, io_internal, block_type );		
/*
	io_data_t *buff = io_get( io, 1 );
	if( buff && buff->io_mode == IO_BUFFER )
	fwprintf( stderr, L"block %ls produced %d bytes of output\n", 
	def,
	buff->out_buffer->used );
*/
	io_untransmogrify( io, io_internal );
	job_do_notification();
	is_block=is_block_old;
	return res;
}

/*
static void io_print( io_data_t *io )
{
	if( !io )
	{
		fwprintf( stderr, L"\n" );
		return;
	}
	

	fwprintf( stderr, L"IO fd %d, type ",
			  io->fd );
	switch( io->io_mode )
	{
		case IO_PIPE:
			fwprintf(stderr, L"PIPE, data %d\n", io->pipe_fd[io->fd?1:0] );
			break;
		
		case IO_FD:
			fwprintf(stderr, L"FD, copy %d\n", io->old_fd );
			break;

		case IO_BUFFER:
			fwprintf( stderr, L"BUFFER\n" );
			break;
			
		default:
			fwprintf( stderr, L"OTHER\n" );
	}

	io_print( io->next );
	
}
*/

static int handle_new_child( job_t *j, process_t *p )
{
	/* 
	   If we do not output to stdout, builtin IO will not appear. 
	   I have no idea why...
	*/
	//fwprintf( stdout, L"" );
	
	if(is_interactive  && !is_subshell && !is_block)
	{
		int new_pgid=0;
		
		if (!j->pgid)
		{
			new_pgid=1;			
			j->pgid = p->pid;
		}
		
		if( setpgid (p->pid, j->pgid) )
		{						
			if( getpgid( p->pid) != j->pgid )
			{
				debug( 1, 
					   L"Could not send process %d from group %d to group %d",
					   p->pid, 
					   getpgid( p->pid),
					   j->pgid );
				wperror( L"setpgid" );
			}

		}

		if( j->fg )
		{
			while( 1)
			{
				if( tcsetpgrp (0, j->pgid) )
				{
					if( errno != EINTR )
					{
						debug( 1, L"Could not send job %d ('%ls')to foreground", 
							   j->job_id, 
							   j->command );
						wperror( L"tcsetpgrp" );
						return -1;
					}
				}
				else
					break;
			}
		}

		if( j->fg && new_pgid)
		{
			while( 1 )
			{
				if( tcsetpgrp (0, j->pgid) )
				{
					if( errno != EINTR )
					{
						debug( 1, L"Could not send job %d ('%ls')to foreground", 
							   j->job_id, 
							   j->command );
						wperror( L"tcsetpgrp" );
						return -1;
					}
				}
				else
					break;
			}
		}
		
	}
	else
	{
		j->pgid = getpid();
	}
	return 0;
	
}



void exec( job_t *j )
{
	process_t *p;
	pid_t pid;
	int mypipe[2];
	sigset_t chldset; 
	sigemptyset( &chldset );
	sigaddset( &chldset, SIGCHLD );
	int skip_fork;
	
	/* This call is used so the global environment variable array is regenerated, if needed, before the fork. That way, we avoid a lot of duplicate work where EVERY child would need to generate it */
	env_export_arr();

	io_data_t pipe_read, pipe_write;
	io_data_t *tmp;

	io_data_t *io_buffer =0;
	
//	if( j->job_id > 3 )
	
	debug( 2, L"Exec job %ls with id %d", j->command, j->job_id );	
	
	if( j->first_process->type==INTERNAL_EXEC )
	{
		/*
		  Do a regular launch -  but without forking first...
		*/
		setup_child_process( j );
		launch_process( j->first_process );
		/*
		  launch_process _never_ returns...
		*/
	}
	

	pipe_read.fd=0;
	pipe_write.fd=1;
	pipe_read.io_mode=IO_PIPE;
	pipe_read.pipe_fd[0] = -1;
	pipe_write.io_mode=IO_PIPE;
	pipe_read.next=0;
	pipe_write.next=0;

	//fwprintf( stderr, L"Run command %ls\n", j->command );
	
	if( block_io )
	{
//		fwprintf( stderr, L"Before\n" );		
//		io_print( j->io );		

		if( j->io )
			j->io = io_add( io_duplicate(block_io), j->io );
		else
			j->io=io_duplicate(block_io);				

//		fwprintf( stderr, L"After\n" );
//		io_print( j->io );		

	}
	
/*
  if true;
  read foo; read bar; read baz;
  end <foo.txt
*/

	j->io = io_add( j->io, &pipe_write );
	
	for (p = j->first_process; p; p = p->next)
	{
		mypipe[1]=-1;
		skip_fork=0;
		
		/*
		  Set up fd:s that will be used in the pipe 
		*/
//		fwprintf( stderr, L"Create process %ls\n", p->actual_cmd );
		
		if( p == j->first_process->next )
		{
			j->io = io_add( j->io, &pipe_read );
		}
		
		if (p->next)
		{
			if (pipe( mypipe ) == -1)
			{
				debug( 1, PIPE_ERROR );
				wperror (L"pipe");
				exit (1);
			}

/*			fwprintf( stderr,
  L"Make pipe from %ls to %ls using fds %d and %d\n", 
  p->actual_cmd,
  p->next->actual_cmd,
  mypipe[0],
  mypipe[1] );
*/
			memcpy( pipe_write.pipe_fd, mypipe, sizeof(int)*2);
		}
		else
		{
			/*
			  This is the last element of the pipeline.
			*/

			/*
			  Remove the io redirection for pipe output.
			*/
			j->io = io_remove( j->io, &pipe_write );
			
		}

		switch( p->type )
		{
			case INTERNAL_FUNCTION:
			{
				wchar_t **arg;
				int i;
				string_buffer_t sb;
			
				const wchar_t * def = function_get_definition( p->argv[0] );
//			fwprintf( stderr, L"run function %ls\n", argv[0] );
				if( def == 0 )
				{
					debug( 0, L"Unknown function %ls", p->argv[0] );
					break;
				}
				parser_push_block( FUNCTION_CALL );
				
				if( builtin_count_args(p->argv)>1 )
				{
					sb_init( &sb );
				
					for( i=1,arg = p->argv+1; *arg; i++, arg++ )
					{
						if( i != 1 )
							sb_append( &sb, ARRAY_SEP_STR );
						sb_append( &sb, *arg );
					}

					env_set( L"argv", (wchar_t *)sb.buff, ENV_LOCAL );
					sb_destroy( &sb );
				}
				parser_forbid_function( p->argv[0] );

				if( p->next )
				{
//					fwprintf( stderr, L"Function %ls\n", def );
					io_buffer = exec_make_io_buffer();					
					j->io = io_add( j->io, io_buffer );
				}
				
				internal_exec_helper( def, TOP, j->io );
				
				parser_allow_function();
				parser_pop_block();
				
				break;				
			}
			
			case INTERNAL_BLOCK:
			{
				if( p->next )
				{
//					fwprintf( stderr, L"Block %ls\n", p->argv[0] );
					io_buffer = exec_make_io_buffer();					
					j->io = io_add( j->io, io_buffer );
				}
				
				internal_exec_helper( p->argv[0], TOP, j->io );			
				break;
				
			}
			
			case INTERNAL_BUILTIN:
			{
				int builtin_stdin=0;
				int fg;
				int close_stdin=0;
				
				if( p == j->first_process )
				{
					io_data_t *in = io_get( j->io, 0 );
					if( in )
					{
						switch( in->io_mode )
						{
							
							case IO_FD:
							{
								builtin_stdin = in->old_fd;
/*								fwprintf( stderr, 
  L"Input redirection for builtin '%ls' from fd %d\n", 
  p->argv[0],
  in->old_fd );
*/
								break;
							}
							case IO_PIPE:
							{
								builtin_stdin = in->pipe_fd[0];
								break;
							}
							
							case IO_FILE:
							{
								int new_fd;
/*								
  fwprintf( stderr, 
  L"Input redirection for builtin from file %ls\n",
  in->filename);
*/
								new_fd=wopen( in->filename, in->flags, 0666 );
								if( new_fd == -1 )
								{
									wperror( L"wopen" );
								}
								else
								{
									builtin_stdin = new_fd;
									
									close_stdin = 1;
								}
								
								break;
							}

							default:
							{
								debug( 1, 
									   L"Unknown input redirection type %d",
									   in->io_mode);
								break;
							}
						
						}
					}
				}
				else
				{
					builtin_stdin = pipe_read.pipe_fd[0];
				}

				builtin_push_io( builtin_stdin );
				
				/* 
				   Since this may be the foreground job, and since a
				   builtin may execute another foreground job, we need to
				   pretend to suspend this job while running the builtin.
				*/

				builtin_out_redirect = has_fd( j->io, 1 );
				builtin_err_redirect = has_fd( j->io, 2 );		
				fg = j->fg;
				j->fg = 0;
				p->status = builtin_run( p->argv );

				/*
				  Restore the fg flag, which is temporarily set to
				  false during builtin execution so as not to confuse
				  some job-handling builtins.
				*/
				j->fg = fg;


/*			if( is_interactive )
  fwprintf( stderr, L"Builtin '%ls' finished\n", j->command );
*/
				if( close_stdin )
				{
//					fwprintf( stderr, L"Close builtin_stdin\n" );					
					close( builtin_stdin );
				}				
				break;				
			}
		}


		switch( p->type )
		{
			case INTERNAL_BLOCK:
			case INTERNAL_FUNCTION:
			{
				int status = proc_get_last_status();
				
				/*
				  Handle output from a block or function. This usually
				  means do nothing, but in the case of pipes, we have
				  to buffer such io, since otherwisethe internal pipe
				  buffer might overflow.
				*/
				if( !io_buffer)
				{
					p->completed = 1;
					break;
				}

				j->io = io_remove( j->io, io_buffer );
				
				if( close( io_buffer->pipe_fd[1] ) == -1 )
				{
					debug( 1, PIPE_ERROR );
					wperror( L"close" );
					
				}
				
				debug( 3, L"read_all on block '%ls'", p->argv[0] );
				
				read_all( io_buffer );
				
				if( io_buffer->out_buffer->used != 0 )
				{
					
					/*Temporary signal block for SIGCHLD */
					sigprocmask(SIG_BLOCK, &chldset, 0);
					pid = fork ();
					if (pid == 0)
					{
						/*
						  This is the child process. Write out the contents of the pipeline.
						*/
						p->pid = getpid();
						setup_child_process( j );
						write( io_buffer->fd, 
							   io_buffer->out_buffer->buff, 
							   io_buffer->out_buffer->used );
						exit( status );
					}
					else if (pid < 0)
					{
						/* The fork failed. */
						debug( 0, FORK_ERROR );
						wperror (L"fork");
						exit (1);
					}
					else
					{
						/* 
						   This is the parent process. Store away
						   information on the child, and possibly fice
						   it control over the terminal.
						*/
						p->pid = pid;						
						if( handle_new_child( j, p ) )
							return;										
					}					
					
				}
				
				exec_free_io_buffer( io_buffer );
				
				io_buffer=0;
				break;
				
			}
			
			case INTERNAL_BUILTIN:
			{
				int skip_fork=0;
				
				/*
				  If a builtin didn't produce any output, and it is not inside a pipeline, there is no need to fork 
				*/
				skip_fork =
					( !sb_out->used ) &&
					( !sb_err->used ) &&
					( !p->next );
				
				/*
				  If the output of a builtin is to be sent to an internal
				  buffer, there is no need to fork. This helps out the
				  performance quite a bit in complex completion code.
				*/

				io_data_t *io = io_get( j->io, 1 );		
				int buffer_stdout = io && io->io_mode == IO_BUFFER;

				if( ( !sb_err->used ) && 
					( !p->next ) &&
					( sb_out->used ) && 
					( buffer_stdout ) )
				{
//				fwprintf( stderr, L"Skip fork of %ls\n", j->command );				
					char *res = wcs2str( (wchar_t *)sb_out->buff );				
					b_append( io->out_buffer, res, strlen( res ) );
					skip_fork = 1;
					free( res );				
				}

				if( skip_fork )
				{
					p->completed=1;
					if( p->next == 0 )
					{
						debug( 3, L"Set status of %ls to %d using short circut", j->command, p->status );
						
						proc_set_last_status( p->status );
						proc_set_last_status( j->negate?(p->status?0:1):p->status );
					}
					break;
					
				}

				/*Temporary signal block for SIGCHLD */
				sigprocmask(SIG_BLOCK, &chldset, 0);
				pid = fork ();
				if (pid == 0)
				{
					/*
					  This is the child process. 
					*/
					p->pid = getpid();
					setup_child_process( j );
					if( sb_out->used )
						fwprintf( stdout, L"%ls", sb_out->buff );
					if( sb_err->used )
						fwprintf( stderr, L"%ls", sb_err->buff );
					
					exit( p->status );
						
				}
				else if (pid < 0)
				{
					/* The fork failed. */
					debug( 0, FORK_ERROR );
					wperror (L"fork");
					exit (1);
				}
				else
				{
					/* 
					   This is the parent process. Store away
					   information on the child, and possibly fice
					   it control over the terminal.
					*/
					p->pid = pid;
						
					if( handle_new_child( j, p ) )
						return;										
				}					
				
				break;
			}
			
			case EXTERNAL:
			{
				/*Temporary signal block for SIGCHLD */
				sigprocmask(SIG_BLOCK, &chldset, 0);
		
//			fwprintf( stderr, 
//					  L"fork on %ls\n", j->command );
				pid = fork ();
				if (pid == 0)
				{
					/*
					  This is the child process. 
					*/
					p->pid = getpid();
					setup_child_process( j );
					launch_process( p );

					/*
					  launch_process _never_ returns...
					*/
				}
				else if (pid < 0)
				{
					/* The fork failed. */
					debug( 0, FORK_ERROR );
					wperror (L"fork");
					exit (1);
				}
				else
				{
					/* 
					   This is the parent process. Store away
					   information on the child, and possibly fice
					   it control over the terminal.
					*/
					p->pid = pid;

					if( handle_new_child( j, p ) )
						return;
										
				}
				break;
			}
			
		}


		if(p->type == INTERNAL_BUILTIN)
			builtin_pop_io();			
		
		sigprocmask(SIG_UNBLOCK, &chldset, 0);
		
		/* 
		   Close the pipe the current process uses to read from the previous process_t 
		*/
		if( pipe_read.pipe_fd[0] >= 0 )
			close( pipe_read.pipe_fd[0] );
		/* 
		   Set up the pipe the next process uses to read from the current process_t 
		*/
		if( p->next )
			pipe_read.pipe_fd[0] = mypipe[0];
		
		/* 
		   If there is a next process, close the output end of the
		   pipe (the child subprocess already has a copy of the pipe).
		*/
		if( p->next )
		{
			if( close(mypipe[1]) != 0 )
			{
				debug( 1, PIPE_ERROR );
				wperror( L"close" );
			}
		}		
	}

//	fwprintf( stderr, L"Job is constructedk\n" );

	j->io = io_remove( j->io, &pipe_read );
	j->io = io_remove( j->io, &pipe_write );

	for( tmp = block_io; tmp; tmp=tmp->next )
		j->io = io_remove( j->io, tmp );
	

//	assert( !job_is_stopped(j));
	j->constructed = 1;
//	ggg( j->command, j->io );

	if( !j->fg )
	{
		proc_last_bg_pid = j->pgid;
	}
	
	job_continue (j, 0);

	debug( 3, L"End of exec()" );
	
}

int exec_subshell( const wchar_t *cmd, 
				   array_list_t *l )
{
	char *begin, *end;
	char z=0;
	int prev_subshell = is_subshell;
	int status, prev_status;
	io_data_t *io_buffer;

	if( !cmd )
	{
		debug( 1, L"Sent null command to subshell" );		
		return 0;		
	}

	is_subshell=1;	
	io_buffer= exec_make_io_buffer();
	
	prev_status = proc_get_last_status();
	
	eval( cmd, io_buffer, SUBST );
	close( io_buffer->pipe_fd[1] );
	
	debug( 4, L"read_all on cmdsub '%ls'", cmd );
	read_all( io_buffer );
	
	status = proc_get_last_status();
	proc_set_last_status( prev_status );
	
	is_subshell = prev_subshell;
	
	b_append( io_buffer->out_buffer, &z, 1 );
	
	begin=end=io_buffer->out_buffer->buff;	
	
	if( l )
	{
		while( 1 )
		{
			switch( *end )
			{
				case 0:
				
					if( begin != end )
					{
						wchar_t *el = str2wcs( begin );
						if( el )
							al_push( l, el );
					}				
					exec_free_io_buffer( io_buffer );
				
					return status;
				
				case '\n':
				{
					wchar_t *el;
					*end=0;
					el = str2wcs( begin );				
					al_push( l, el );
					begin = end+1;
					break;
				}
			}
			end++;
		}
	}
	
	exec_free_io_buffer( io_buffer );
	return status;	
}