@@ -76,7 +76,7 @@ finish() { # Clean up and terminate
76
76
unset -f error warning note verbose
77
77
unset -f rmcr getwslpath escapestring convertpath
78
78
unset -f check_display check_displayport cookiebaker makecookie strlenhex
79
- unset -f check_host check_dependency check_dependencies setup_cookie generate_xcommand
79
+ unset -f check_host check_dependency check_xserver setup_cookie generate_xcommand
80
80
unset -f cleanup
81
81
unset -f declare_variables parse_options main
82
82
@@ -122,6 +122,14 @@ verbose() { # Show verbose message (--verbose)
122
122
123
123
# ### general routines
124
124
125
+ check_dependency () { # Check for single command
126
+ [ " ${1:- } " ] || return 1
127
+ command -v " ${1:- } " > /dev/null || {
128
+ note " Command not found: ${1:- } "
129
+ return 1
130
+ }
131
+ return 0
132
+ }
125
133
rmcr () { # Remove carriage return to translate DOS/Windows newlines into UNIX newlines.
126
134
# Convert stdin if $1 is empty. Otherwise convert file $1.
127
135
case " ${1:- } " in
@@ -291,9 +299,6 @@ strlenhex() { # print byte length of string $1 as hex value
291
299
}
292
300
293
301
# ### setup
294
- check_displayport () { # return 0 if display number $1 is in use
295
- echo " quit" | env LC_ALL=C telnet " $Hostip " " $(( 6000 + ${1:- } )) " 2>&1 | grep -q " Connected"
296
- }
297
302
check_display () { # find unused display number
298
303
local Displaynumber
299
304
for Displaynumber in $( seq 999 | shuf) ; do
@@ -304,6 +309,9 @@ check_display() { # find unused display number
304
309
done
305
310
return 1
306
311
}
312
+ check_displayport () { # return 0 if display number $1 is in use
313
+ echo " quit" | env LC_ALL=C telnet " $Hostip " " $(( 6000 + ${1:- } )) " 2>&1 | grep -q " Connected"
314
+ }
307
315
check_host () { # Check host environment
308
316
309
317
# Check for MS Windows subsystem
@@ -313,6 +321,11 @@ check_host() { # Check host environment
313
321
}
314
322
uname -r | grep -q Microsoft && Winsubsystem=" WSL1"
315
323
uname -r | grep -q microsoft && Winsubsystem=" WSL2"
324
+ case " $Winsubsystem " in
325
+ WSL1|WSL2|CYGWIN|MSYS2) ;;
326
+ * ) error " runx is designed to run on MS Windows in WSL, Cygwin or MSYS2.
327
+ Did not detect WSL, Cygwin or MSYS2." ;;
328
+ esac
316
329
317
330
[ " $0 " = " $BASH_SOURCE " ] && Sourced=" no" || Sourced=" yes"
318
331
verbose " Script is being sourced yes/no: $Sourced "
@@ -349,23 +362,9 @@ check_host() { # Check host environment
349
362
350
363
return 0
351
364
}
352
- check_dependency () { # Check for single command
353
- [ " ${1:- } " ] || return 1
354
- command -v " ${1:- } " > /dev/null || {
355
- note " Command not found: ${1:- } "
356
- return 1
357
- }
358
- return 0
359
- }
360
- check_dependencies () { # Check all dependencies
365
+ check_xserver () { # Check all dependencies
361
366
local Line
362
367
363
- # subsystem
364
- case " $Winsubsystem " in
365
- WSL1|WSL2|CYGWIN|MSYS2) ;;
366
- * ) error " runx is designed to run on MS Windows in WSL, Cygwin or MSYS2.
367
- Did not detect WSL, Cygwin or MSYS2." ;; # ##
368
- esac
369
368
[ " $Winsubsystem " = " MSYS2" ] && [ " $Xserver " = " xwin" ] && {
370
369
note " runx in MSYS2 does not support --xwin.
371
370
Fallback: Enabling option --vcxsrv."
@@ -408,22 +407,12 @@ check_dependencies() { # Check all dependencies
408
407
https://sourceforge.net/projects/vcxsrv
409
408
or install Cygwin with packages xinit and xauth to provide XWin:
410
409
https://www.cygwin.com/" ;;
411
- MSYS2) error " No X server found for MSYS2.
410
+ MSYS2) error " No X server found for MSYS2.
412
411
Please install X server VcXsrv:
413
412
https://sourceforge.net/projects/vcxsrv" ;;
414
413
esac
415
414
}
416
-
417
- # Windows commands
418
- for Line in cmd.exe ipconfig.exe powershell.exe tasklist.exe taskkill.exe; do
419
- check_dependency " $Line " || error " Did not find Windows command: $Line "
420
- done
421
-
422
- # Linux commands
423
- for Line in telnet shuf; do
424
- check_dependency " $Line " || error " Did not find Linux command: $Line
425
- Please install $Line "
426
- done
415
+ return 0
427
416
}
428
417
cookiebaker () { # create an X cookie without xauth
429
418
# $1 DISPLAY
@@ -652,11 +641,69 @@ parse_options() {
652
641
shift
653
642
done
654
643
}
644
+ start_xserver () {
645
+ check_xserver
646
+ generate_xcommand
647
+ [ " $Xauthentication " = " yes" ] && setup_cookie
648
+
649
+ case " $Verbose " in
650
+ yes) Xcommand=" $Xcommand 1>&2" ;;
651
+ no) Xcommand=" $Xcommand >/dev/null 2>&1" ;;
652
+ esac
653
+
654
+ # Run X in background?
655
+ { [ " $Hostcommand " ] || [ " $Sourced " = " yes" ] ; } && Xcommand=" $Xcommand & Xserverpid=\$ !"
656
+
657
+ # Store Windows PID list
658
+ case $Winsubsystem in
659
+ WSL1|WSL2) Tasklistold=" $( tasklist.exe | rmcr | grep -i ${Xserver} .exe | awk ' {print $2}' ) " ;;
660
+ esac
661
+
662
+ # Run X server
663
+ eval $Xcommand
664
+
665
+ # Find Windows PID of X server
666
+ case $Winsubsystem in
667
+ WSL1|WSL2)
668
+ Tasklistnew=" $( tasklist.exe | rmcr | grep -i ${Xserver} .exe | awk ' {print $2}' ) "
669
+ Xserverwinpid=" $( echo " $Tasklistold
670
+ $Tasklistnew " | sort | uniq -u | sed ' /^$/d' ) "
671
+ verbose " Windows PID of X server: $Xserverwinpid "
672
+ ;;
673
+ esac
674
+
675
+ # Check for successfull startup
676
+ [ " $Xserverpid " ] && {
677
+ verbose " Linux PID of X server: $Xserverpid
678
+ $( ps -p " $Xserverpid " ) "
679
+ case " $Xserver " in
680
+ xwin)
681
+ for Waiting in 1 2 3 4 5 6 7 8 9 10; do
682
+ [ -e " $( dirname " $Xwinexe " ) /../../tmp/.X11-unix/X$Newdisplaynumber " ] && Xready=" yes"
683
+ [ -e " $( dirname " $Xwinexe " ) /../tmp/.X11-unix/X$Newdisplaynumber " ] && Xready=" yes"
684
+ sleep 1
685
+ [ " $Xready " ] && break
686
+ verbose " Waiting since ${Waiting} s for XWin to be ready."
687
+ done
688
+ [ " $Xready " ] && verbose " X server XWin ready after ${Waiting} s." || error " X server XWin not ready after ${Waiting} s."
689
+ ;;
690
+ vcxsrv) sleep 1 ;;
691
+ esac
692
+ }
693
+ }
655
694
main () {
656
695
local Waiting Tasklistold Tasklistnew Xalreadyrunning
657
696
658
- Xclientcookie=" $( convertpath subsystem " ~/runx_Xauthority" ) "
659
- Xservercookie=" $( convertpath subsystem " $( MSYS2_ARG_CONV_EXCL=' *' cmd.exe /C " echo %userprofile%" ) " | rmcr) /runx_Xauthority"
697
+ # Dependency checks
698
+ # # Windows commands ### check disabled for faster startup
699
+ # for Line in cmd.exe ipconfig.exe powershell.exe tasklist.exe taskkill.exe; do
700
+ # check_dependency "$Line" || error "Did not find Windows command: $Line"
701
+ # done
702
+ # Linux commands
703
+ for Line in telnet shuf; do
704
+ check_dependency " $Line " || error " Did not find Linux command: $Line
705
+ Please install $Line "
706
+ done
660
707
661
708
Xalreadyrunning=" no"
662
709
[ -n " $Newdisplaynumber " ] && {
@@ -666,7 +713,6 @@ main() {
666
713
Xalreadyrunning=" yes"
667
714
}
668
715
}
669
-
670
716
[ -z " $Newdisplaynumber " ] && {
671
717
Newdisplaynumber=" $( check_display) " || error " runx: Did not find a free display number."
672
718
}
@@ -680,6 +726,8 @@ main() {
680
726
export DISPLAY=$Hostip :$Newdisplaynumber
681
727
case " $Xauthentication " in
682
728
yes)
729
+ Xclientcookie=" $( convertpath subsystem " ~/runx_Xauthority" ) "
730
+ Xservercookie=" $( convertpath subsystem " $( MSYS2_ARG_CONV_EXCL=' *' cmd.exe /C " echo %userprofile%" ) " | rmcr) /runx_Xauthority"
683
731
export XAUTHORITY=" $Xclientcookie "
684
732
verbose " DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY "
685
733
note " If you get application error messages like 'Cannot open display'
@@ -699,57 +747,10 @@ main() {
699
747
;;
700
748
esac
701
749
702
- [ " $Xalreadyrunning " = " no" ] && {
703
- [ " $Xauthentication " = " yes" ] && setup_cookie
704
- generate_xcommand
705
-
706
- case " $Verbose " in
707
- yes) Xcommand=" $Xcommand 1>&2" ;;
708
- no) Xcommand=" $Xcommand >/dev/null 2>&1" ;;
709
- esac
710
-
711
- # Run X in background?
712
- { [ " $Hostcommand " ] || [ " $Sourced " = " yes" ] ; } && Xcommand=" $Xcommand & Xserverpid=\$ !"
713
-
714
- # Store Windows PID list
715
- case $Winsubsystem in
716
- WSL1|WSL2) Tasklistold=" $( tasklist.exe | rmcr | grep -i ${Xserver} .exe | awk ' {print $2}' ) " ;;
717
- esac
718
-
719
- # Run X server
720
- eval $Xcommand
721
-
722
- # Find Windows PID of X server
723
- case $Winsubsystem in
724
- WSL1|WSL2)
725
- Tasklistnew=" $( tasklist.exe | rmcr | grep -i ${Xserver} .exe | awk ' {print $2}' ) "
726
- Xserverwinpid=" $( echo " $Tasklistold
727
- $Tasklistnew " | sort | uniq -u | sed ' /^$/d' ) "
728
- verbose " Windows PID of X server: $Xserverwinpid "
729
- ;;
730
- esac
731
-
732
- # Check for successfull startup
733
- [ " $Xserverpid " ] && {
734
- verbose " Linux PID of X server: $Xserverpid
735
- $( ps -p " $Xserverpid " ) "
736
- case " $Xserver " in
737
- xwin)
738
- for Waiting in 1 2 3 4 5 6 7 8 9 10; do
739
- [ -e " $( dirname " $Xwinexe " ) /../../tmp/.X11-unix/X$Newdisplaynumber " ] && Xready=" yes"
740
- [ -e " $( dirname " $Xwinexe " ) /../tmp/.X11-unix/X$Newdisplaynumber " ] && Xready=" yes"
741
- sleep 1
742
- [ " $Xready " ] && break
743
- verbose " Waiting since ${Waiting} s for XWin to be ready."
744
- done
745
- [ " $Xready " ] && verbose " X server XWin ready after ${Waiting} s." || error " X server XWin not ready after ${Waiting} s."
746
- ;;
747
- vcxsrv) sleep 1 ;;
748
- esac
749
- }
750
- }
750
+ # run X
751
+ [ " $Xalreadyrunning " = " no" ] && start_xserver
751
752
752
- # Run host command
753
+ # run host command
753
754
[ -z " $Exitcode " ] && [ " $Hostcommand " ] && {
754
755
verbose " Executing command:
755
756
$Hostcommand "
@@ -761,7 +762,6 @@ trap finish EXIT
761
762
declare_variables
762
763
check_host
763
764
parse_options " $@ "
764
- check_dependencies
765
765
[ " $Cleanup " = " yes" ] && cleanup && Exitcode=" ${Exitcode:- 0} "
766
766
[ -z " $Exitcode " ] && main
767
767
finish " ${Exitcode:- 0} "
0 commit comments