; -------------------------------------------------------------------------------- ; @Title: Example for reading environment variables ; @Description: ; Example script for reading and displaying some environment variables. ; @Keywords: OS.NAME(), OS.ENV() ; @Author: RIC ; @Copyright: (C) 1989-2016 Lauterbach GmbH, licensed for use with TRACE32(R) only ; -------------------------------------------------------------------------------- ; $Id: my_environment.cmm 15414 2019-12-12 17:59:12Z hlohn $ LOCAL &user &home &name &cpu IF VERSION.BUILD()<65657. ( DIALOG.OK "TRACE32 Version too old." "Please update to build 65657 or later." "Click 'OK' to exit." ENDDO ) ;VAR WIN LINUX MAC ; User USERNAME USER LOGNAME ; Home HOMEPATH HOME HOME ; PC name COMPUTERNAME 'uname -n' 'uname -n' ; CPU PROCESSOR_IDENTIFIER 'uname -p' 'uname -p' ; Check which host OS is in use IF OS.NAME()=="Windows" ( ; Windows detected ; Extract relevant data from Environment Variables &user=OS.ENV("USERNAME") &home=OS.ENV("HOMEPATH") &name=OS.ENV("COMPUTERNAME") &cpu=OS.ENV("PROCESSOR_IDENTIFIER") ) ELSE IF OS.NAME()=="Linux" ( ; Linux detected ; Extract relevant data from environment variables &user=OS.ENV("USER") &home=OS.ENV("HOME") ; Some aren't available so we need to get them from a command GOSUB getname RETURNVALUES &name GOSUB getcpu RETURNVALUES &cpu ) ELSE IF OS.NAME()=="MacOSX" ( ; MacOS detected ; Extract relevant data from environment variables &user=OS.ENV("USER") &home=OS.ENV("HOME") ; Some aren't available so we need to get them from a command GOSUB getname RETURNVALUES &name GOSUB getcpu RETURNVALUES &cpu ) ELSE ( ; Something else - unsupported DIALOG.OK "Unsupported host OS." "Requires Linux, Mac OSX or Windows." ENDDO ) AREA.view PRINT PRINT " *** RESULTS ***" PRINT "User Name : &user" PRINT "Home Directory : &home" PRINT "CPU Type : &cpu" PRINT "Machine name : &name" PRINT ENDDO ;------------------------------------------------------------------------------- ; Subroutines ; ;------------------------------------------------------------------------------- ;------------------------------------------------------------------------------- ; getname ; Gets the name of the computer by calline 'uname -n' ; PARAMETERS: NONE ; RETURNS : Name of computer getname: PRIVATE &tmpfile &txtline ; Get a temporary file from the host OS &tmpfile=OS.TMPFILE() ; Log some info to the temporary file OS.Hidden uname -n > &tmpfile ; WAIT until the command has completed ; This allows for filesystem caches to be flushed before ; we try to open the file. LOCAL &cnt &cnt=0. WHILE (!OS.FILE("&tmpfile"))&&(&cnt<100) ( WAIT 200.ms &cnt=&cnt+1. ) ; Check to see if the count has been exceeded IF &cnt>=100. ( ;ERROR GOSUB ErrHandler "TMPFILE for name does not exist. Please check &tmpfile." ) OPEN #2 &tmpfile /READ READ #2 %LINE &txtline CLOSE #2 IF "&txtline"=="" ( ; Empty line - probably an error GOSUB ErrHandler "Error getting host name." ) RETURN "&txtline" ;------------------------------------------------------------------------------- ; getcpu ; Gets the CPU type of the computer by calline 'uname -p' ; PARAMETERS: NONE ; RETURNS : CPU type of computer getcpu: PRIVATE &tmpfile &txtline ; Get a temporary file from the host OS &tmpfile=OS.TMPFILE() ; Log some info to the temporary file OS.Hidden uname -p > &tmpfile ; WAIT until the command has completed LOCAL &cnt &cnt=0. WHILE (!OS.FILE("&tmpfile"))&&(&cnt<100) ( WAIT 200.ms &cnt=&cnt+1. ) ; Check to see if the count has been exceeded IF &cnt>=100. ( ;ERROR GOSUB ErrHandler "TMPFILE for CPU does not exist. Please check &tmpfile." ) OPEN #2 &tmpfile /READ READ #2 %LINE &txtline CLOSE #2 IF "&txtline"=="" ( ; Empty line - probably an error GOSUB ErrHandler "Error getting host name." ) RETURN "&txtline" ;------------------------------------------------------------------------------- ; ErrHandler ; End up here if something goes wrong. ; PARAMETERS: NONE ; RETURNS : NONE ErrHandler: PARAMETERS &txt PRINT "" PRINT %COLOR.RED "An error occurred." PRINT "&txt" PRINT "Script will now terminate." ENDDO RETURN