| 
									
										
										
										
											2007-11-12 23:18:42 -05:00
										 |  |  | ! Copyright (C) 2007 Slava Pestov. | 
					
						
							|  |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | USING: io io.launcher io.unix.backend io.nonblocking | 
					
						
							|  |  |  | sequences kernel namespaces math system alien.c-types | 
					
						
							| 
									
										
										
										
											2007-11-24 18:09:30 -05:00
										 |  |  | debugger continuations arrays assocs combinators unix.process | 
					
						
							| 
									
										
										
										
											2007-11-24 18:32:19 -05:00
										 |  |  | parser-combinators memoize promises strings ;
 | 
					
						
							| 
									
										
										
										
											2007-11-08 22:41:36 -05:00
										 |  |  | IN: io.unix.launcher | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | ! Search unix first | 
					
						
							|  |  |  | USE: unix | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-24 18:09:30 -05:00
										 |  |  | ! Our command line parser. Supported syntax: | 
					
						
							|  |  |  | ! foo bar baz -- simple tokens | 
					
						
							|  |  |  | ! foo\ bar -- escaping the space | 
					
						
							|  |  |  | ! 'foo bar' -- quotation | 
					
						
							|  |  |  | ! "foo bar" -- quotation | 
					
						
							|  |  |  | LAZY: 'escaped-char' "\\" token any-char-parser &> ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-24 19:37:21 -05:00
										 |  |  | LAZY: 'quoted-char' ( delimiter -- parser' )
 | 
					
						
							|  |  |  |     'escaped-char' | 
					
						
							|  |  |  |     swap [ member? not ] curry satisfy | 
					
						
							|  |  |  |     <|> ; inline
 | 
					
						
							| 
									
										
										
										
											2007-11-24 18:09:30 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-24 19:37:21 -05:00
										 |  |  | LAZY: 'quoted' ( delimiter -- parser )
 | 
					
						
							| 
									
										
										
										
											2007-11-24 20:19:39 -05:00
										 |  |  |     dup 'quoted-char' <!*> swap dup surrounded-by ;
 | 
					
						
							| 
									
										
										
										
											2007-11-24 18:09:30 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-24 20:19:39 -05:00
										 |  |  | LAZY: 'unquoted' ( -- parser ) " '\"" 'quoted-char' <!+> ;
 | 
					
						
							| 
									
										
										
										
											2007-11-24 18:09:30 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-24 19:37:21 -05:00
										 |  |  | LAZY: 'argument' ( -- parser )
 | 
					
						
							|  |  |  |     "\"" 'quoted' "'" 'quoted' 'unquoted' <|> <|> | 
					
						
							| 
									
										
										
										
											2007-11-24 18:09:30 -05:00
										 |  |  |     [ >string ] <@ ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-24 20:27:09 -05:00
										 |  |  | MEMO: 'arguments' ( -- parser )
 | 
					
						
							| 
									
										
										
										
											2007-11-24 20:19:39 -05:00
										 |  |  |     'argument' " " token <!+> nonempty-list-of ;
 | 
					
						
							| 
									
										
										
										
											2007-11-24 18:09:30 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : tokenize-command ( command -- arguments )
 | 
					
						
							| 
									
										
										
										
											2007-11-24 20:19:39 -05:00
										 |  |  |     'arguments' just parse-1 ;
 | 
					
						
							| 
									
										
										
										
											2007-11-24 18:09:30 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-12 23:18:42 -05:00
										 |  |  | : get-arguments ( -- seq )
 | 
					
						
							| 
									
										
										
										
											2007-11-24 18:09:30 -05:00
										 |  |  |     +command+ get [ tokenize-command ] [ +arguments+ get ] if* ;
 | 
					
						
							| 
									
										
										
										
											2007-11-12 23:18:42 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-24 18:32:19 -05:00
										 |  |  | : assoc>env ( assoc -- env )
 | 
					
						
							|  |  |  |     [ "=" swap 3append ] { } assoc>map ;
 | 
					
						
							| 
									
										
										
										
											2007-11-12 23:18:42 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : (spawn-process) ( -- )
 | 
					
						
							|  |  |  |     [ | 
					
						
							| 
									
										
										
										
											2007-11-13 01:10:26 -05:00
										 |  |  |         pass-environment? [ | 
					
						
							| 
									
										
										
										
											2007-11-14 18:35:43 -05:00
										 |  |  | 	    get-arguments get-environment assoc>env exec-args-with-env | 
					
						
							| 
									
										
										
										
											2007-11-12 23:18:42 -05:00
										 |  |  |         ] [ | 
					
						
							| 
									
										
										
										
											2007-11-14 18:35:43 -05:00
										 |  |  | 	    get-arguments exec-args-with-path | 
					
						
							| 
									
										
										
										
											2007-11-12 23:18:42 -05:00
										 |  |  |         ] if io-error | 
					
						
							|  |  |  |     ] [ error. :c flush ] recover 1 exit ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : wait-for-process ( pid -- )
 | 
					
						
							|  |  |  |     0 <int> 0 waitpid drop ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-12 23:18:42 -05:00
										 |  |  | : spawn-process ( -- pid )
 | 
					
						
							|  |  |  |     [ (spawn-process) ] [ ] with-fork ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-12 23:18:42 -05:00
										 |  |  | : spawn-detached ( -- )
 | 
					
						
							|  |  |  |     [ spawn-process 0 exit ] [ ] with-fork wait-for-process ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-12 23:18:42 -05:00
										 |  |  | M: unix-io run-process* ( desc -- )
 | 
					
						
							|  |  |  |     [ | 
					
						
							|  |  |  |         +detached+ get [ | 
					
						
							|  |  |  |             spawn-detached | 
					
						
							|  |  |  |         ] [ | 
					
						
							|  |  |  |             spawn-process wait-for-process | 
					
						
							|  |  |  |         ] if
 | 
					
						
							|  |  |  |     ] with-descriptor ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : open-pipe ( -- pair )
 | 
					
						
							|  |  |  |     2 "int" <c-array> dup pipe zero?
 | 
					
						
							|  |  |  |     [ 2 c-int-array> ] [ drop f ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : setup-stdio-pipe ( stdin stdout -- )
 | 
					
						
							|  |  |  |     2dup first close second close | 
					
						
							|  |  |  |     >r first 0 dup2 drop r> second 1 dup2 drop ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-12 23:18:42 -05:00
										 |  |  | : spawn-process-stream ( -- in out pid )
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     open-pipe open-pipe [ | 
					
						
							|  |  |  |         setup-stdio-pipe | 
					
						
							|  |  |  |         (spawn-process) | 
					
						
							|  |  |  |     ] [ | 
					
						
							| 
									
										
										
										
											2007-11-15 18:06:40 -05:00
										 |  |  |         -rot 2dup second close first close | 
					
						
							|  |  |  |     ] with-fork first swap second rot ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | TUPLE: pipe-stream pid ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : <pipe-stream> ( in out pid -- stream )
 | 
					
						
							|  |  |  |     pipe-stream construct-boa | 
					
						
							|  |  |  |     -rot handle>duplex-stream over set-delegate ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: pipe-stream stream-close | 
					
						
							|  |  |  |     dup delegate stream-close | 
					
						
							|  |  |  |     pipe-stream-pid wait-for-process ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-12 23:18:42 -05:00
										 |  |  | M: unix-io process-stream* | 
					
						
							|  |  |  |     [ spawn-process-stream <pipe-stream> ] with-descriptor ;
 |