init-msys1.sh (1750B)
1 #!/bin/bash 2 3 function ble-edit/io:msys1/is-msys1 { 4 local cr; cr=$'\r' 5 [[ $OSTYPE == msys && ! $cr ]] 6 } 7 function ble-edit/io:msys1/get-winpid.proc { 8 /usr/bin/ps | /usr/bin/gawk -v pid="$1" ' 9 BEGIN { 10 cygpid_len = 9; 11 winpid_len = 36; 12 } 13 NR == 1 { 14 line = $0; 15 if (!match(line, /.*\yPID\y/)) next; 16 cygpid_end = RLENGTH; 17 if (!match(line, /.*\yWINPID\y/)) next; 18 winpid_end = RLENGTH; 19 next; 20 } 21 function get_last_number(line, limit, _, head, i) { 22 head = substr(line, 1, limit); 23 if (i = match(head, /[0-9]+$/)) 24 return substr(head, i, RLENGTH); 25 return -1; 26 } 27 { 28 cygpid = get_last_number($0, cygpid_end); 29 if (cygpid != pid) next; 30 print get_last_number($0, winpid_end); 31 exit 32 } 33 ' 34 } 35 function ble-edit/io:msys1/compile-helper { 36 local helper=$1 37 [[ -x $helper && -s $helper && $helper -nt $_ble_base/lib/init-msys1.sh ]] && return 0 38 39 # /mingw/bin/gcc 40 gcc -O2 -s -o "$helper" -xc - << EOF || return 1 41 #%include init-msys1-helper.c 42 EOF 43 44 [[ -x $helper ]] 45 } 46 47 function ble-edit/io:msys1/start-background { 48 local basename=$_ble_edit_io_fname2 49 local fname_buff=$basename.buff 50 51 ble-edit/io:msys1/is-msys1 || return 1 52 53 local helper=$_ble_base_cache/init-msys1-helper.exe 54 local helper2=$_ble_base_run/$$.init-msys1-helper.exe 55 ble-edit/io:msys1/compile-helper "$helper" && 56 /usr/bin/cp "$helper" "$helper2" || return 1 57 58 local winpid 59 ble/util/assign winpid 'ble-edit/io:msys1/get-winpid.proc $$' 60 [[ $winpid ]] || return 1 61 62 : >| "$fname_buff" 63 ble/fd#alloc _ble_edit_io_fd2 '> "$fname_buff"' 64 "$helper2" "$winpid" "$fname_buff" "${fname_buff%.buff}.read" | ble-edit/io/check-ignoreeof-loop & disown 65 } &>/dev/null