application/VDBStartup.st
changeset 43 c98aa29401f7
parent 37 f417fe8685c5
child 47 25d82943a3cf
equal deleted inserted replaced
42:a6f5f470a947 43:c98aa29401f7
    48     ^ true
    48     ^ true
    49 
    49 
    50     "Created: / 08-09-2014 / 19:30:40 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    50     "Created: / 08-09-2014 / 19:30:40 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    51 ! !
    51 ! !
    52 
    52 
       
    53 !VDBStartup class methodsFor:'private'!
       
    54 
       
    55 loadPreferenceFile: file
       
    56 
       
    57     "Created: / 07-06-2017 / 09:49:58 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
    58 ! !
       
    59 
       
    60 !VDBStartup class methodsFor:'queries'!
       
    61 
       
    62 applicationName
       
    63     ^ 'vdb'
       
    64 
       
    65     "Created: / 06-06-2017 / 22:50:44 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
    66 ! !
       
    67 
    53 !VDBStartup class methodsFor:'startup'!
    68 !VDBStartup class methodsFor:'startup'!
    54 
    69 
    55 main:argv
    70 main:argv
    56     | gdb |
    71     "Application entry point. `argv` is the array of command arguments (as Array of Strings)"
    57 
    72 
       
    73     | optparser positional settingsFile settingsSuppressed programExecutable programArgs programPid attach debugger |
       
    74 
       
    75     settingsSuppressed := false.
       
    76 
       
    77     "/ Parse options...
       
    78     optparser := CmdLineParser new
       
    79                     ignoreUnknownOptions: true;
       
    80                     on: #('--help') do:[ 
       
    81                         self usage 
       
    82                     ];
       
    83                     on: #('--preferences') do:[:filename | 
       
    84                         | file |
       
    85 
       
    86                         file := filename asFilename.
       
    87                         file isReadable ifFalse:[ 
       
    88                             self error: 'preference file does not exists or not readable: ' , filename.
       
    89                         ].
       
    90                         file isRegularFile ifFalse:[ 
       
    91                             self error: 'preference file is not a regular file: ' , filename.
       
    92                         ].
       
    93                         settingsFile := Array with: file 
       
    94                     ];
       
    95                     on: #('--no-preferences') do:[ 
       
    96                         settingsSuppressed := #() 
       
    97                     ];
       
    98                     yourself.
       
    99     [
       
   100         positional := optparser parse:argv.
       
   101     ] on: CmdLineOptionError do:[:ex |
       
   102         self error: ex description.
       
   103     ].
       
   104 
       
   105     "/ Parse positional arguments - there are two forms:
       
   106     "/
       
   107     "/   vdb [OPTIONS] PROGRAM [ARGS] 
       
   108     "/   vdb [OPTIONS] PID
       
   109     "/ 
       
   110     "/ [OPTIONS] have already been processed, the rest is in `positional`
       
   111     "/ variable
       
   112 
       
   113     positional isEmpty ifTrue:[ 
       
   114         self error: 'no executable (or PID to attach) given'
       
   115     ].
       
   116     programExecutable := positional first.
       
   117     programExecutable asFilename exists ifFalse:[ 
       
   118         programExecutable := OperatingSystem pathOfCommand: programExecutable.
       
   119     ].
       
   120     programPid := Integer fromString: positional first onError: [ nil ].
       
   121     programArgs := positional copyFrom: 2.
       
   122 
       
   123     "/ Now validate and process options
       
   124     settingsSuppressed ifFalse:[ 
       
   125         | settings |
       
   126 
       
   127         settingsFile notNil ifTrue:[ 
       
   128             settingsFile exists ifFalse:[
       
   129                 self error: 'preference file does not exist: ', settingsFile pathName
       
   130             ].
       
   131             settingsFile isDirectory ifTrue:[
       
   132                 self error: 'preference file is not a regular file: ', settingsFile pathName
       
   133             ].
       
   134             settingsFile isReadable ifFalse:[
       
   135                 self error: 'preference file is not a readable (check permissions): ', settingsFile pathName
       
   136             ].    
       
   137             settings := UserPreferences loadSettingsFrom: settingsFile.  
       
   138         ] ifFalse:[ 
       
   139             settings := UserPreferences loadSettings.
       
   140         ].
       
   141         UserPreferences setCurrent: settings.
       
   142     ].
       
   143 
       
   144     "/ If * programExecutable does not exists 
       
   145     "/    * AND programPid is not nil (i.e., first positional argument can be converted to an integer)
       
   146     "/    * AND programArguments are empty 
       
   147     "/ then interpret positional argument as PID and attach to it.
       
   148     "/ Otherwise, interpret positional arguments
       
   149     (programExecutable asFilename exists not and: [ programPid notNil and: [ programArgs isEmpty ]]) ifTrue:[ 
       
   150         attach := true.
       
   151     ] ifFalse:[ 
       
   152         attach := false.
       
   153         programExecutable asFilename exists ifFalse:[ 
       
   154             self error: 'cannot find program executable: ', programExecutable.
       
   155         ].
       
   156     ].
       
   157     Debugger := DebugView ? MiniDebugger.
       
   158     Inspector := InspectorView ? MiniInspector. 
       
   159 
       
   160     debugger := GDBDebugger new.
       
   161     attach ifTrue:[ 
       
   162         debugger attach: programPid
       
   163     ] ifFalse:[ 
       
   164         debugger executable: programExecutable arguments: programArgs.
       
   165     ].
    58     Smalltalk openDisplay.
   166     Smalltalk openDisplay.
    59     gdb := GDBDebugger new.
   167     VDBDebuggerApplication openFor: debugger.
    60     VDBDebuggerApplication openFor: gdb.
   168 
    61 
   169 
    62     "Modified: / 21-09-2014 / 01:31:41 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   170     "
       
   171         VDBStartup main: #('ls')
       
   172         VDBStartup main: #('/bin/ls' '/tmp')               
       
   173     "
       
   174 
       
   175     "Modified: / 08-06-2017 / 13:09:33 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    63 !
   176 !
    64 
   177 
    65 usage
   178 usage
    66     Stderr nextPutLine:'usage: ',self applicationName,' [options...]'.
   179     Stdout nextPutAll:'usage: '; nextPutAll: self applicationName; nextPutAll: ' [OPTIONS] PROGRAM [ARGS] '; cr.
    67     Stderr nextPutLine:'  -h .................. output this message'.
   180     Stdout nextPutAll:'       '; nextPutAll: self applicationName; nextPutAll: ' [OPTIONS] PID'; cr.
    68 
   181                                                                           "|"
    69     Smalltalk isStandAloneApp ifTrue:[ Smalltalk exit:1 ].
   182     Stdout nextPutLine:'                                             
       
   183 options:
       
   184  --settings FILE .............. read user settings from FILE
       
   185  --no-settings ................ do not read user settings at all
       
   186  --help ....................... output this message
       
   187 '.
       
   188 
       
   189     Smalltalk exitIfStandalone: 0.
       
   190 
       
   191     "Modified: / 07-06-2017 / 09:04:20 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    70 ! !
   192 ! !
    71 
   193 
    72 !VDBStartup class methodsFor:'documentation'!
   194 !VDBStartup class methodsFor:'documentation'!
    73 
   195 
    74 version_HG
   196 version_HG