Home   Archive   Permalink



Identifying what you are running under

I am trying to write a run-anywhere script that identifies what version of a Rebol-like language it is running under.
    
This is what I've got so far:
        
The code below returns the following strings with the versions of the interpreters I used:
    
     R2/view: 'REBOL/View 2.7.6.3.1 View 14-Mar-2008'
     R2/view+r2-forward: 'REBOL/View 2.7.6.3.1 R2F View 14-Mar-2008'
     r2/core: 'REBOL/Core 2.7.8.3.1 Core 1-Jan-2011'
     r2/core: 'REBOL/Core 2.5.0.3.1 23-Mar-2001'    ;; old version that pre-dates 'ATTEMPT
     Red: 'Red 0.6.1 7-Jul-2016 Windows'
     R3: 'REBOL 3.0 core 2.100.67.3.1 6-Jul-2009'
    
I'd be fascinated to see better code from someone who really knows what they are doing.
    
Bonus points for:
     -- no global variable
     -- also work with Boron, World and Topaz :)
    
If you give it a try, you'll learn a lot about niggling little differences -- and go google-eyed at having so many interpreter windows open.
    
Sunanda
    
***
    
         info-string: none     ;; needs (?) to be a global variable
    
    
         get-env-info: func [
     ;; ============================
         /local
         about-func
         sigs
     ][
         ;; Work with old CORE that pre-dates 'ATTEMPT
    
         if not value? 'attempt [attempt: func [x][error? try [x]]]
    
         ;; Exfiltrate the first two words from 'ABOUT:
     info-string: copy ''
     about-func: mold get in system/words 'about
     replace/all about-func 'print' {append info-string ' ' append info-string form reduce}
     about-func: first reduce load/all about-func
     about-func
     replace/all info-string '*' ' '
     trim/lines info-string
     attempt [info-string: split info-string ' ']
     if string? info-string [info-string: parse info-string ' ']
     sigs: copy/part info-string 2
    
         ;; Grab what we can from the various SYSTEM product/version fields:
     attempt [if function? get 'latin1? [append sigs 'R2F']]
     attempt [rebcode! rebcode? rebcode [] []
                append sigs 'REBCODE'
                ]
     attempt [append sigs form system/product]
     attempt [append sigs form system/version]
     if error? try [append sigs form system/build/date][
         attempt [append sigs form first split form system/build '/']
         ]
     attempt [append sigs form system/platform]
    
         ;; Remove any duplication
     sigs: unique sigs
        
     return form sigs
     ]
    
     print get-env-info


posted by:   Sunanda     27-Sep-2016/4:31:34-7:00



{Hi! This should work with most REBOL and Red versions (tested from View 0.99/Core 2.3 to View/Core 2.7.8, Base and Face too, with some REBOL 3 build).
    
World crashes with that code (append sigs -> core dump) but the code should be correct.
    
Topaz has an usable structure (system/name and system/version) but isn't stable enough for now to manipulate strings, and Boron doesn't expose the version at the language level.
    
They should be any global var or leak (like the redefinition of 'attempt).}
    
get-env-info: func [
     ;; ============================
     /local
     info-string
     date
     do-try
     about-func
     sigs
][
     ;; Work with old CORE that pre-dates 'ATTEMPT
     do-try: func [x][error? try x]
    
     sigs: copy []
    
     ;; REBOL/Core/View/Pro/Command & Red, exfiltrate the first two words from 'ABOUT:
     either all [value? 'about function? get 'about][
         info-string: copy ""
    
         ;; Build a modified 'ABOUT
         about-func: mold get 'about
         replace/all about-func "print" {append info-string " " append info-string form reduce}
    
         ;; LOAD/ALL isn't available with old CORE versions
         if error? try [about-func: load/all about-func][about-func: load about-func]
    
         ;; Bind the modified 'ABOUT to this function context
         bind third about-func 'info-string
    
         about-func: first reduce about-func
    
         about-func
    
         ;; Process info-string
         replace/all info-string "*" " "
         trim/lines info-string
         if error? try [info-string: split info-string " "][info-string: parse info-string " "]
    sigs: copy/part info-string 2
     ][
         ;; no 'ABOUT: REBOL/Base or /Face (View) & World
         either all [value? 'about none? get 'about][
             ;; REBOL/Base or Face (View)
             append sigs join "REBOL/" system/product
             append sigs system/version
         ][
             if find [Cortex] system/product [
                 append sigs join "World/" system/product
                 append sigs system/version/version
             ]
         ]
     ]
    
     ;; Specific capabilities
    
     ;; R2-Forward?
     do-try [if function? get 'latin1? [append sigs "R2F"]]
     ;; Rebcode?
     do-try [rebcode! rebcode? rebcode [] []
         append sigs "REBCODE"
     ]
    
     ;; Grab what we can from the various SYSTEM product/version fields:
    
     ;; Product & Version
     all [
         ;; World product & version
         do-try [append sigs join "(" [system/product " " system/version/version ")"]]
         ;; REBOL product & version
         do-try [append sigs join "(" [system/product " " system/version ")"]]
         ;; Red version
         do-try [append sigs append copy "(" reduce [system/version ")"]]
     ]
    
     ;; Internal version
     all [
         ;; World variation
         do-try [append sigs join "(Variation " [system/version/variation ")"]]
         ;; REBOL core version
         do-try [append sigs join "(Core " [system/core ")"]]
     ]
    
     ;; Build date
     all [
         ;; REBOL
         do-try [append sigs form system/build/date]
         ;; World
         do-try [
             date: parse/all form system/build " "
             append sigs form first load join date/2 ["-" date/1 "-" date/3]
         ]
         ;; Red build date
         do-try [append sigs first split form system/build "/"]
    
     ]
    
     ;; Platform
     all [
         ;; World
         do-try [append sigs form system/version/platform]
         ;; REBOL & Red
         do-try [append sigs form system/platform]
     ]
                
     form sigs
]
print get-env-info


posted by:   Scureuil     10-Oct-2016/10:17:33-7:00



Thanks for that!
    
Looks like you had a lot of fun, and got a lot further than I did in exploring Rebol variants.
    
Give me a day or so to have a play, and I'll get back with any comments.
    
I had to tweak my version (or the code that generates it - can't remember off-hand) to work with Atronix's R3 - they remap the system object. Not sure if your version needs a similar tweak.
    
R3 from Atronix:
http://www.atronixengineering.com/downloads/

posted by:   Sunanda     11-Oct-2016/2:29:48-7:00



Sorry for taking too long in getting back on this - been travelling.
    
Your improvements work very smoothly in R2, R3, red and R3-atronix. World isn't working at all on my machine (nothing to do with this script -it crashes on start-up) so can't try that.
    
Thanks again!

posted by:   Sunanda     18-Oct-2016/12:25:38-7:00