(DEFINE-FILE-INFO PACKAGE "INTERLISP" READTABLE "INTERLISP" BASE 10) (FILECREATED "13-Nov-2023 12:57:10" {DSK}frank>il>medley>gmedley>library>UNIXUTILS.;26 16663 :CHANGES-TO (FNS ShellBrowser) :PREVIOUS-DATE "11-Nov-2023 09:06:39" {DSK}frank>il>medley>gmedley>library>UNIXUTILS.;25 ) (PRETTYCOMPRINT UNIXUTILSCOMS) (RPAQQ UNIXUTILSCOMS ((DECLARE%: EVAL@COMPILE DONTCOPY (* ; "For PROCESS-COMMAND") (FILES (FROM LOADUPS) EXPORTS.ALL)) (GLOBALVARS ShellBrowser ShellOpener) (INITVARS (ShellBrowser) (ShellOpener)) (FUNCTIONS ShellCommand ShellWhich) (ADDVARS (MEDLEY-INIT-VARS (ShellBrowser) (ShellOpener))) (FNS ShellBrowser ShellBrowse ShellOpener ShellOpen PROCESS-COMMAND SLASHIT UNIX-FILE-NAME) (PROPS (UNIXUTILS FILETYPE)))) (DECLARE%: EVAL@COMPILE DONTCOPY (FILESLOAD (FROM LOADUPS) EXPORTS.ALL) ) (DECLARE%: DOEVAL@COMPILE DONTCOPY (GLOBALVARS ShellBrowser ShellOpener) ) (RPAQ? ShellBrowser ) (RPAQ? ShellOpener ) (CL:DEFUN ShellCommand (Cmd &OPTIONAL (Output T)) (CL:WITH-OPEN-STREAM (s (CREATE-PROCESS-STREAM Cmd)) (CL:TAGBODY [SETFILEINFO s 'ENDOFSTREAMOP #'(CL:LAMBDA (s) (GO OUT] (CL:LOOP (PRINTCCODE (READCCODE s) Output)) OUT)) NIL) (CL:DEFUN ShellWhich (Cmd) (* ; "Edited 18-Jan-2023 13:19 by FGH") [CL:WITH-OPEN-STREAM (S (OPENSTREAM '{NODIRCORE} 'BOTH)) (ShellCommand (CONCAT "command -v " Cmd) S) (COND ((EQ (GETEOFPTR S) 0) NIL) (T (SETFILEPTR S 0) (RSTRING S]) (ADDTOVAR MEDLEY-INIT-VARS (ShellBrowser) (ShellOpener)) (DEFINEQ (ShellBrowser [LAMBDA NIL (* ; "Edited 18-Jan-2023 20:30 by FGH") (* ;; "Figure out the browser to use for the ShellOpen/ShellBrowse functions. ") (* ;; " Ordinarily, this would be the same as the generic ShellOpener.") (* ;; " But if a generic ShellOpener is not found, then there are some additional") (* ;; " possibilities that will work for http/https URLs. If one of these exists return it.") (OR ShellBrowser (SETQ ShellBrowser (if (NOT (STREQUAL (ShellOpener) "true")) then ShellOpener else (LET (CMDPATH) (if (SETQ CMDPATH (ShellWhich "git")) then (* ;; " Systems with git installed") (CONCAT CMDPATH " web--browse") elseif (SETQ CMDPATH (ShellWhich "lynx")) then (* ;; " Systems with lynx installed") (LET (CMDPATH2) (if (SETQ CMDPATH2 (ShellWhich "xterm")) then (CONCAT CMDPATH2 " -e " CMDPATH) else (LIST CMDPATH))) else (* ;; " Out of ideas - just return a dummy function") "true"]) (ShellBrowse [LAMBDA (URL) (* ; "Edited 18-Jan-2023 20:32 by FGH") (* ;; " Open the web page specified by URL using an external browser via shell call") (* ;; " URL must start with http:// or https:// or file:/// (case ireelevant) or this function will just return NIL.") (* ;; " Returns T otherwise.") (SETQ URL (MKSTRING URL)) (if (OR (EQ (STRPOS "http://" (L-CASE URL)) 1) (EQ (STRPOS "https://" (L-CASE URL)) 1) (EQ (STRPOS "file:///" (L-CASE URL)) 1)) then (ShellOpen URL) else NIL]) (ShellOpener [LAMBDA NIL (* ;; "Find an %"opener%" that will open files (and URLs) using the appropriate/default app on this machine") (OR ShellOpener (SETQ ShellOpener (LET (CMDPATH) (if (SETQ CMDPATH (ShellWhich "wslview")) then (* ;; "windows with WSL") CMDPATH elseif (SETQ CMDPATH (ShellWhich "cygstart")) then (* ;; "windows with cygwin") CMDPATH elseif (SETQ CMDPATH (ShellWhich "xdg-open")) then (* ;; "Linux systems with xdg-utils installed ") CMDPATH elseif (SETQ CMDPATH (ShellWhich "open")) then (* ;; " MacOS open") CMDPATH else (* ;;  " Out of ideas - just return a dummy function") "true"]) (ShellOpen [LAMBDA (FilenameOrURL) (* ;; "Open the file or URL using the generic %"opener%" for this machine via a shell call.") (* ;; " If FilenameOrURL starts with %"http://%" or %"https://%" or %"file:///%", then we use (ShellBrowser) as") (* ;; " the %"opener%" (which includes some browsers on a machine without a generic opener).") (* ;;  " Otherwise FilenameOrURL is assumed to be a filename and will be opened using (ShellOpener).") (* ;; " Returns T is all goes well; returns an error string if all does not go well") (RANDSET T) (SETQ FilenameOrURL (MKSTRING FilenameOrURL)) (if (OR (EQ (STRPOS "http://" (L-CASE FilenameOrURL)) 1) (EQ (STRPOS "https://" (L-CASE FilenameOrURL)) 1) (EQ (STRPOS "file://" (L-CASE FilenameOrURL)) 1)) then (LET ((BROWSER (ShellBrowser))) (if (NOT (STREQUAL BROWSER "true")) then (if (LISTP BROWSER) then (CHAT 'SHELL NIL (CONCAT (CAR BROWSER) " '" FilenameOrURL "'")) else (ShellCommand (CONCAT BROWSER " '" FilenameOrURL "'" " >>/tmp/ShellBrowser-warnings-$$.txt")) T) else (CONCAT "Unable to find a browser to open: " FilenameOrURL))) else (LET* ((OPENER (ShellOpener)) (FULLNAME (FULLNAME FilenameOrURL))) (if (NOT FULLNAME) then (CONCAT "File not found: " FilenameOrURL) elseif (STREQUAL OPENER "true") then (CONCAT "Unable to find a file opener to open: " FilenameOrURL) else (LET* ((VERSION.SPECIFIED (FILENAMEFIELD FilenameOrURL 'VERSION)) (UNPACKED (UNPACKFILENAME.STRING FULLNAME)) (NEWNAME (CONCAT (LISTGET UNPACKED 'NAME) "~" (LISTGET UNPACKED 'VERSION) "~")) (EXTENSION (LISTGET UNPACKED 'EXTENSION)) [UNVERSIONED (LET (FN (UNPACKED (COPY UNPACKED))) (LISTPUT UNPACKED 'VERSION NIL) (LISTPUT UNPACKED 'HOST NIL) (SETQ FN (PACKFILENAME.STRING UNPACKED)) (if (STREQUAL (SUBSTRING FN -1) ".") then (SETQ FN (SUBSTRING UNIXFILE 1 -2))) (SETQ FN (SLASHIT FN] (UNVERSIONED.EXISTS (INFILEP (CONCAT "{UNIX}" UNVERSIONED))) (TMPDIR (CONCAT "/tmp/" (RAND 1000 9999))) (TARGETFILE.LISP (PACKFILENAME.STRING 'HOST "{UNIX}" 'DIRECTORY TMPDIR 'NAME NEWNAME 'EXTENSION EXTENSION)) (TARGETFILE.UNIX (SLASHIT (PACKFILENAME.STRING 'HOST NIL 'DIRECTORY TMPDIR 'NAME NEWNAME 'EXTENSION EXTENSION))) (UNIXFILE NIL)) (DECLARE (SPECVARS UNIXFILE)) (if (OR VERSION.SPECIFIED (NOT UNVERSIONED.EXISTS)) then (COPYFILE FULLNAME TARGETFILE.LISP) (SETQ UNIXFILE TARGETFILE.UNIX) else (SETQ UNIXFILE UNVERSIONED)) (CL:WITH-OPEN-STREAM (SHELLSTREAM (OPENSTREAM (CONCAT "{CORE}SHELLOUT" (RAND 1000 9999)) 'BOTH)) (ShellCommand (CONCAT OPENER " '" UNIXFILE "'" " >>/tmp/ShellOpener-warnings-$$.txt") SHELLSTREAM) (if (EQ (GETFILEPTR SHELLSTREAM) 0) then T else (LET* ((OUTSTRING (ALLOCSTRING (GETFILEPTR SHELLSTREAM) " "))) (CL:WITH-OPEN-STREAM (STRINGSTREAM (OPENSTRINGSTREAM OUTSTRING 'OUTPUT)) (SETFILEPTR SHELLSTREAM 0) (CL:TAGBODY [SETFILEINFO SHELLSTREAM 'ENDOFSTREAMOP #'(CL:LAMBDA (s) (GO OUT] (CL:LOOP (PRINTCCODE (READCCODE SHELLSTREAM) STRINGSTREAM)) OUT)) OUTSTRING]) (PROCESS-COMMAND [LAMBDA (CMD) (* ; "Edited 17-Jul-2022 08:17 by rmk") (* ;; "This sets up an asynchronous process and waits until it returns with an exit code. Typically 0 means success.") (CL:WITH-OPEN-STREAM (PS (CREATE-PROCESS-STREAM CMD)) (BIND CODE WHILE (EQ T (SETQ CODE (OR (SUBRCALL UNIX-HANDLECOMM 7 (fetch (STREAM F1) of PS)) 0))) DO (BLOCK) FINALLY (RETURN CODE]) (SLASHIT [LAMBDA (X LCASEDIRS NOHOST) (* ; "Edited 23-Sep-2023 15:27 by rmk") (* ;; "It would also be nice to use the generic unpackfilename/packfilename tools. But packfilename sticks in brackets again, and sticks a dot on when removing the version.") (* ;; "Perhaps this should be a per file-device operation that maps device names into the local file system.") (* ;; "This is a first approximation to a utility that converts a filename X on a host whose files physically reside in the local Unix file system into the strings that shell commands can use to reference that file. For now, this just involves replacing directory brackets with /, removing the host, and perhaps lower-casing the directory. It probably should be extended to deal with version number translation, for now it just keeps the ; version. ") (LET [LASTDIRPOS SLASHED (DIRPOS (ADD1 (OR (STRPOS "}" X) 0] [SETQ SLASHED (CONCATCODES (for I C from DIRPOS while (SETQ C (NTHCHARCODE X I)) collect (SELCHARQ C ((< >) (SETQ LASTDIRPOS I) (CHARCODE /)) (/ (SETQ LASTDIRPOS I) C) C] (CL:WHEN (AND LCASEDIRS LASTDIRPOS) (SETQ LASTDIRPOS (ADD1 (IDIFFERENCE LASTDIRPOS DIRPOS))) (SETQ SLASHED (CONCAT (L-CASE (SUBSTRING SLASHED 1 LASTDIRPOS)) (OR (SUBSTRING SLASHED (ADD1 LASTDIRPOS)) "")))) (CL:IF (OR (EQ DIRPOS 1) NOHOST) SLASHED (CONCAT (SUBSTRING X 1 (SUB1 DIRPOS)) SLASHED))]) (UNIX-FILE-NAME [LAMBDA (FILE ACCESS COPY) (* ; "Edited 1-Oct-2023 20:52 by rmk") (* ;; "Tries to return the string that would reference FILE in a Unix shell, for the use of PROCESS-COMMAND and ShellCommand. If VERSION is 1, it assumes that the Unix file is doesn't have the Medley version convention. If FILE does not have a corresponding Unix name, COPY is non-NIL, and ACCESS is INPUT, FILE will be copied to a unix tmp file (with COPY in its name) and that name will be returned.") (CL:WHEN (\GETSTREAM FILE ACCESS T) (SETQ FILE (OR (FULLNAME FILE) FILE))) (* ; "Might catch NODIRCORE") (CL:WHEN FILE (SETQ FILE (TRUEFILENAME FILE)) (CL:UNLESS (STREAMP FILE) [SETQ FILE (\GETFILENAME FILE (SELECTQ ACCESS (OUTPUT 'NEW) (INPUT 'OLD) (NIL (SETQ ACCESS 'INPUT) 'OLD) (\ILLEGAL.ARG ACCESS]) [SELECTQ (FILENAMEFIELD FILE 'HOST) (UNIX [SUBSTRING FILE (ADD1 (CONSTANT (NCHARS "{UNIX}"]) (DSK (LET [(VERSION (FILENAMEFIELD FILE 'VERSION] (SETQ FILE (SLASHIT (PACKFILENAME 'HOST NIL 'VERSION NIL 'BODY FILE))) (CL:IF (AND VERSION (IGREATERP VERSION 1)) (CONCAT FILE (CL:IF (FILENAMEFIELD FILE 'EXTENSION) "." "") "~" VERSION "~") FILE))) (CL:WHEN (AND COPY (EQ ACCESS 'INPUT) FILE) (RESETLST (CL:WHEN (\GETSTREAM FILE 'INPUT T) (* ; "Hope is randaccess") [RESETSAVE (GETFILEPTR FILE) `(PROGN (SETFILEPTR ,FILE OLDVALUE]) (COPYFILE FILE (CONCAT "{UNIX}/tmp/medley-" (L-CASE COPY) "-" (IDATE) "-" (RAND) (CL:IF (FILENAMEFIELD FILE 'EXTENSION) (CONCAT "." (FILENAMEFIELD FILE 'EXTENSION)) "")))))])]) ) (PUTPROPS UNIXUTILS FILETYPE CL:COMPILE-FILE) (DECLARE%: DONTCOPY (FILEMAP (NIL (1146 1519 (ShellCommand 1146 . 1519)) (1521 1918 (ShellWhich 1521 . 1918)) (2008 16585 (ShellBrowser 2018 . 3790) (ShellBrowse 3792 . 4477) (ShellOpener 4479 . 6167) (ShellOpen 6169 . 11324 ) (PROCESS-COMMAND 11326 . 11939) (SLASHIT 11941 . 13983) (UNIX-FILE-NAME 13985 . 16583))))) STOP