TMGKERNL ;TMG/kst/OS Specific functions ;11/01/04 ;;1.0;TMG-LIB;**1**;04/24/09 ;"TMG KERNEL FUNCTIONS ;"I.e. functions that are OS specific. ;"Kevin Toppenberg MD ;"GNU General Public License (GPL) applies ;"7-12-2005 ;"======================================================================= ;" API -- Public Functions. ;"======================================================================= ;"$$Dos2Unix^TMGKERNL(FullNamePath) ;"$$IsDir^TMGKERNL(Path) ;"$$Move^TMGKERNL(Source,Dest) ;"$$Copy^TMGKERNL(Source,Dest) ;"$$mkdir(Dir) -- provide a shell for the Linux command 'mkdir' ;"$$rmdir(Dir) -- provide a shell for the Linux command 'rmdir' ;"$$Convert^TMGKERNL(FPathName,NewType) -- convert a graphic image to new type ;"$$XLTLANG(Phrase,langPair) -- execute a linux OS call to convert a phrase into another spoken language ;"$$GetPckList(PckInit,Array,NeedsRefresh,PckDirFName) -- launch special linux script to get patch file list from ftp.va.gov ;"$$DownloadFile^TMGKERNL(URL,DestDir) -- Interact with Linux to download a file with wget ;"$$EditHFSFile^TMGKERNL(FilePathName) -- interact with Linux to edit a file on the host file system ;"ZSAVE -- to save routine out to HFS ;"MAKEBAKF^TMGKERNL(FilePathName,NodeDiv) ;Make Backup File if original exists ;"IOCapON -- redirect IO to a HFS file, so that it can be captured. ;"IOCapOFF(pOutArray) -- restore IO channel to that prior IOCapON was called, and return captured output in OutArray ;"KillPID(JobNum) -- send message to MUPIP to kill Job ;"MJOBS(array) -- execute a linux OS call to get list of all 'mumps' jobs using: 'ps -C mumps' ;"$$GetScrnSize(ROWS,COLS) --query the OS and get the dimensions of the terminal window. ;"======================================================================= ;"Dependancies ;"======================================================================= ;"======================================================================= Dos2Unix(FullNamePath) ;"Purpose: To execute the unix command Dos2Unix on filename path ;"FullNamePath: The filename to act on. ;"Result: 0 if no error; >0 if error ;"Notice!!!! The return code here is DIFFERENT from usual new result set result=0 if $get(FullNamePath)="" goto DUDone new spec set spec(" ")="\ " set FullNamePath=$$REPLACE^XLFSTR(FullNamePath,.spec) new HookCmd set HookCmd="dos2unix -q "_FullNamePath zsystem HookCmd set result=$ZSYSTEM&255 ;"get result of execution. (low byte only) DUDone quit result FileSize(FullNamePath) ;"Purpose: To return the size of the file, in bytes. ;"Input: FullNamePath: The filename to act on. ;"Result: -1 if error, or returns size in bytes new result set result=-1 new p set p="myTerm" open p:(COMMAND="stat --format=%s "_FullNamePath:readonly)::"pipe" use p new x read x close p use $p ;"write "reply was :",x,! if x'["cannot stat" set result=+x quit result IsDir(Path,NodeDiv) ;"Purpose: To determine if Path is a path to a directory (i.e. are there sub files) ;"Input: Path to test, e.g. "/home/user" or "/home/user/" ;" NodeDiv: [OPTIONAL] -- the character that separates folders (e.g. "/") ;" if not supplied, then default value is "/" ;"Result: 1 filepath is actually a directory ;"Note: NEW! Will now return 1 if Path is a valid path to a directory, but there are no files in directory set Path=$get(Path) set NodeDiv=$get(NodeDiv,"/") if $extract(Path,$length(Path))'=NodeDiv set Path=Path_NodeDiv new p set p="myTerm" open p:(COMMAND="stat --format=%F "_Path:readonly)::"pipe" use p new x read x close p use $p quit (x="directory") ;" ==== old code/method below (slower) === ;"Old results ;"Result: 1 if there are files in path, 0 otherwise ;"Note: if Path is a valid path to a directory, but there are no files in directory, 0 returned. new TMGMask set TMGMask("*")="" new TMGFiles new result set result=0 new spec set spec(" ")="\ " set Path=$$REPLACE^XLFSTR(Path,.spec) ;"Note: I can't seem to get this to work with names containing spaces. if $$LIST^%ZISH(Path,"TMGMask","TMGFiles")=1 do . new index set index=$order(TMGFiles("")) . if index'="" set result=1 quit result Move(Source,Dest) ;"Purpose to provide a shell for the Linux command 'mv' ;" This can serve to move or rename a file ;"Note: a platform independant version of the this could be constructed later... ;"Result: 0 if no error; >0 if error ;"Notice!!!! The return code here is DIFFERENT from usual new HookCmd,result new Srch set Srch(" ")="\ " set Source=$$REPLACE^XLFSTR(Source,.Srch) set Dest=$$REPLACE^XLFSTR(Dest,.Srch) set HookCmd="mv "_Source_" "_Dest zsystem HookCmd set result=$ZSYSTEM&255 ;"get result of execution. (low byte only) quit result Copy(Source,Dest) ;"Purpose to provide a shell for the Linux command 'cp' ;" This can serve to move or rename a file ;"Note: a platform independant version of the this could be constructed later... ;"Result: 0 if no error; >0 if error ;"Notice!!!! The return code here is DIFFERENT from usual new HookCmd,result new Srch set Srch(" ")="\ " set Source=$$REPLACE^XLFSTR(Source,.Srch) set Dest=$$REPLACE^XLFSTR(Dest,.Srch) set HookCmd="cp "_Source_" "_Dest zsystem HookCmd set result=$ZSYSTEM&255 ;"get result of execution. (low byte only) quit result mkdir(Dir) ;"Purpose to provide a shell for the Linux command 'mkdir' ;"Note: a platform independant version of the this could be constructed later... ;"Result: 0 if no error; >0 if error ;"Notice!!!! The return code here is DIFFERENT from usual new HookCmd,result new Srch set Srch(" ")="\ " set Dir=$$REPLACE^XLFSTR(Dir,.Srch) set HookCmd="mkdir "_Dir zsystem HookCmd set result=$ZSYSTEM&255 ;"get result of execution. (low byte only) quit result rmdir(Dir) ;"Purpose to provide a shell for the Linux command 'rmdir' ;"Note: a platform independant version of the this could be constructed later... ;"Result: 0 if no error; >0 if error ;"Notice!!!! The return code here is DIFFERENT from usual new HookCmd,result new Srch set Srch(" ")="\ " set Dir=$$REPLACE^XLFSTR(Dir,.Srch) set HookCmd="rmdir "_Dir zsystem HookCmd set result=$ZSYSTEM&255 ;"get result of execution. (low byte only) quit result Convert(FPathName,NewType) ;"Purpose: to convert a graphic image on the linux host to new type ;" i.e. image.jpg --> image.png. This is more than a simple renaming. ;"Input: FPathName -- full path, filename and extention. E.g. "\tmp\image.jpg" ;" NewType -- the new image type (without '.'), ;" E.g. "jpg", or "JPG", or "TIFF", or "pcd" (NOT ".jpg" etc) ;"Output: New FPathName (with new extension) to new image file, or "" if problem ;" ;"Note: If the conversion is successful, then the original image will be deleted ;"Note: This function depends on the ImageMagick graphic utility "convert" to be ;" installed on the host linux system, and in the path so that it can be ;" launched from any directory. new newFPathName set newFPathName="" set NewType=$get(NewType) if NewType="" goto ConvDone new FName,FPath,FileSpec do SplitFNamePath^TMGIOUTL(FPathName,.FPath,.FName,"/") set FileSpec(FName)="" set newFPathName=$piece(FPathName,".",1)_"."_NewType ;"Setup and launch linux command to execute convert new CmdStr set CmdStr="convert "_FPathName_" "_newFPathName do . ;"new $ETRAP,$ZTRAP . ;"set $ETRAP="S $ECODE=""""" . zsystem CmdStr ;"Launch command ;"get result of execution. (low byte only) -- if wanted new CmdResult set CmdResult=$ZSYSTEM&255 if CmdResult'=0 do goto ConvDone . set newFPathName="" ;"Delete old image file ;"**** temp!!!!! REMOVE COMMENTS LATER ;"new temp set temp=$$DEL^%ZISH(FPath,"FileSpec") ConvDone quit newFPathName XLTLANG(Phrase,langPair) ;"Purpose: To execute a linux OS call to convert a phrase into another ;" spoken language ;"Input: Phrase -- The text to be translated. ;" LangPair -- a language pair (as allowed by Google translater) ;" for now, tested pairs are: ;" "en-es" -- english -> spanish ;" "en-fr" -- english --> french ;" "en-da" -- english --> ? ;"Result: The translated text, or "" if error. ;"Note: This depends on the "tw" package be installed in the host OS ;" I got this on 7/11/08 from: http://savannah.nongnu.org/projects/twandgtw/ ;"Note: This is not working for some reason..... new result set result="" set langPair=$get(langPair,"en-es") set Phrase=$get(Phrase,"?? Nothing Provided ??") new msgFName set msgFName=$$UNIQUE^%ZISUTL("/tmp/TransLang.txt") ;"Setup and launch linux command to execute tw command new CmdStr set CmdStr="tw translate.google.com."_langPair_" """_Phrase_""" > """_msgFName_"""" ;"write "About to execute zsystem command:",!,CmdStr,! zsystem CmdStr ;"Launch command in linux OS ;"write "Back from zsystem",! ;"get result of execution. (low byte only) -- if wanted new CmdResult set CmdResult=$ZSYSTEM&255 if CmdResult'=0 goto TLDone new FName,FPath do SplitFNamePath^TMGIOUTL(msgFName,.FPath,.FName,"/") new resultArray if $$FTG^%ZISH(FPath,FName,"resultArray(0)",1)=0 goto TLDone set result=$get(resultArray(0)) TLDone quit result TestTrans set langPair=$get(langPair,"en-es") set Phrase=$get(Phrase,"Hello friend") new msgFName set msgFName=$$UNIQUE^%ZISUTL("/tmp/TransLang.txt") new CmdStr new qtChar set qtChar="'" set CmdStr="sh /var/local/OpenVistA_UserData/twlang.sh "_qtChar_langPair_qtChar_" "_qtChar_Phrase_qtChar_" "_msgFName write "About to execute zsystem command:",!,CmdStr,! zsystem CmdStr ;"Launch command in linux OS write "Back from zsystem",! set qtChar="""" set CmdStr="sh /var/local/OpenVistA_UserData/twlang.sh "_qtChar_langPair_qtChar_" "_qtChar_Phrase_qtChar_" "_msgFName write "About to execute zsystem command:",!,CmdStr,! zsystem CmdStr ;"Launch command in linux OS write "Back from zsystem",! quit GetPckList(PckInit,Array,NeedsRefresh,PckDirFName) ;"Purpose: Call Linux, launching special script to get patch file list from ftp.va.gov ;" This is a support function for automating the KIDS installation of patches. ;"Input: PckInit -- this is the namespace of the package to get patches for, e.g. 'DI' for fileman ;" Array -- PASS BY REFERENCE. An OUT parameter. Format: ;" Array(0)=1st line ;" Array(1)=2nd line etc. ;" NeedsRefresh -- if 0 then no refresh needed, just set PckDirFName (but ensure file exists) ;" PckDirFName -- Optional. PASS BY REFERNCE, an OUT PARAMETER. Filled with HFS filename of file ;"Result : 1=success, 0=failure new result set result=1 ;"success kill Array if $get(PckInit)="" set result=0 goto GPLDone ;"Results will be stored in /