home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 632.lha / KCommodity_v1.63 / source.LZH / source / misc / BIGSTARTUP.i next >
Encoding:
Text File  |  1992-02-01  |  7.5 KB  |  286 lines

  1.  
  2. **********************************************************************
  3. *   This is my startup-routine, which passes in _ArgV a pointer to   *
  4. *  a ToolTypes-Array and in _ArgC there`s the amount of arguments.   *
  5. *   If _ArgC=0 you can bet, that the prg was launched from the Cli   *
  6. *    or by accessing the _OurTask variable. The routine opens the    *
  7. *     dos-/icon-libraries and stores the pointers to their bases     *
  8. *                     in _DOSBase and _IconBase.                     *
  9. *  You can access the Arguments passed to your program from the CLI  *
  10. *  by using FindToolType, since the arguments are handled the same   *
  11. *                 as in the DiskObject-Structure :-)                 *
  12. *In _stdout you can find the handle to output, _stdin for input if   *
  13. *the prg was launched from cli. This routine works on Kick 1.3 and on*
  14. *                                 OS 2.x                             *
  15. * Written by Kai Iske in 1991. You can use it whereever you want, it *
  16. *      is Public Domain......If you like it let me know....:-)       *
  17. **********************************************************************
  18.  
  19.  
  20.  
  21.         IFND    LIBRARIES_DOSEXTENS_I
  22.         INCLUDE    Libraries/DosExtens.i
  23.         ENDC
  24.  
  25.         IFND    WORKBENCH_STARTUP_I
  26.         INCLUDE    WorkBench/StartUp.i
  27.         ENDC
  28.  
  29.         IFND    EXEC_MEMORY_I
  30.         INCLUDE    Exec/Memory.i
  31.         ENDC
  32.  
  33.         IFND    WORKBENCH_WORKBENCH_I
  34.         INCLUDE    WorkBench/WorkBench.i
  35.         ENDC
  36.  
  37.         IFND    MISC_MYMACROS_I
  38.         INCLUDE    Misc/MyMacros.i
  39.         ENDC
  40.  
  41.  
  42. HardStartUp20    movem.l    d0/a0,-(sp)            ; Store valuable registers
  43.  
  44.         lea    .SUpDosName(pc),a1
  45.         moveq    #0,d0
  46.         CALLSYS    OpenLibrary
  47.         tst.l    d0
  48.         beq    .ErrorStartUp
  49.         move.l    d0,_DOSBase
  50.  
  51.         lea    .SUpIconName(pc),a1
  52.         moveq    #0,d0
  53.         DOLIB    OpenLibrary
  54.         tst.l    d0
  55.         beq    .ErrorStartUp
  56.         move.l    d0,_IconBase
  57.  
  58.         sub.l    a1,a1
  59.         DOLIB     FindTask
  60.         move.l     d0,_OurTask            ; Get address of task
  61.         move.l    d0,a0
  62.  
  63.         movem.l    (sp)+,d7/a4            ; Get registers
  64.  
  65.         tst.l    pr_CLI(a0)            ; Launched from CLI
  66.         beq    .FromWorkBench
  67.  
  68.         movem.l    d7/a4,-(sp)
  69.  
  70.         LNKLIB    Output,_DOSBase            ; Get Output handle
  71.         move.l    d0,_stdout
  72.  
  73.         LNKLIB    Input,_DOSBase            ; Get Input handle
  74.         move.l    d0,_stdin
  75.  
  76.         movem.l    (sp)+,d7/a4
  77.  
  78.         move.l    _OurTask(pc),a0            ; Get address of command-name
  79.         move.l    pr_CLI(a0),a0
  80.         add.l    a0,a0
  81.         add.l    a0,a0
  82.         move.l    cli_CommandName(a0),a0
  83.         add.l    a0,a0
  84.         add.l    a0,a0
  85.         move.l    a0,.ComBuff            ; Store address
  86.         moveq    #0,d0
  87.         move.b    0(a0),d0            ; Length of Command-Name
  88.         add.l    d7,d0                ; + Length of command-line
  89.         addq.l    #2,d0                ; + two NULL-Bytes
  90.         move.l    d0,.ArgLen            ; Store length
  91.         move.l    #MEMF_CLEAR|MEMF_CHIP,d1
  92.         CALLSYS    AllocMem            ; Get memory for command-line
  93.         tst.l    d0
  94.         beq    .ErrorStartUp
  95.         move.l    d0,.ArgLine            ; Store address
  96.  
  97.         move.l    .ComBuff(pc),a0
  98.         move.l    .ArgLine(pc),a1
  99.  
  100.         moveq    #0,d0
  101.         move.b    (a0)+,d0            ; Get len and skip length byte
  102.         move.l    d0,d6
  103.         subq.l    #1,d6
  104. .CpyCommand    move.b    (a0)+,(a1)+            ; Copy Command-Name
  105.         dbf    d6,.CpyCommand
  106.         move.b    #0,(a1)+            ; Mark end of string
  107.         move.l    a4,a0                ; Append command-line
  108.         move.l    d7,d6
  109.         subq.l    #1,d6
  110. .CpyCommandLine    move.b    (a0)+,(a1)+
  111.         dbf    d6,.CpyCommandLine
  112.  
  113.         move.l    #1,_ArgC            ; We already have on argument
  114.         addq.l    #1,d0                ; One more byte
  115.         move.l    .ArgLine(pc),a0
  116.         add.l    d0,a0                ; a0 = Start of command(s)
  117.         move.l    a0,a1
  118.  
  119. .FindArgs    moveq    #0,d0
  120.         move.b    (a0)+,d0            ; Get char
  121. .FindArgStart    cmp.b    #' ',d0                ; as long as it is....
  122.         beq    .FindArgs            ; a Space
  123.         cmp.b    #9,d0
  124.         beq    .FindArgs            ; a Tab
  125.         cmp.b    #12,d0
  126.         beq    .FindArgs            ; a FormFeed
  127.         cmp.b    #13,d0
  128.         beq    .FindArgs            ; a Carrier Return
  129.         cmp.b    #10,d0
  130.         beq    .FindArgs            ; or a return ist
  131.         subq.l    #1,a0                ; Reget char
  132.         move.b    (a0),d0
  133.         cmp.b    #' ',d0                ; Smaller than a SPACE ???
  134.         blt.s    .ArgsFound            ; All args done
  135.  
  136.         cmp.b    #'"',d0                ; Is it a quote ?
  137.         bne.s    .NoQuote            ; no -> normal argument
  138.         addq.l    #1,a0                ; skip quote
  139. .DoQuotes    move.b    (a0)+,d0            ; Get new char
  140.         move.b    d0,(a1)+            ; Copy it to new location
  141.         cmp.b    #'"',d0                ; as long as it ain`t no quote
  142.         bne.s    .DoQuotes
  143.         move.b    (a0),d0                ; Next char
  144.         cmp.b    #'"',d0                ; again a quote
  145.         bne.s    .EndQuoteArg            ; nope -> argument parsed
  146.         addq.l    #1,a0                ; skip and go on
  147.         bra    .DoQuotes
  148. .EndQuoteArg    move.b    #0,-1(a1)            ; End of string
  149.         bra.s    .ArgDone
  150. .NoQuote    move.b    (a0)+,d0            ; Get char
  151.         cmp.b    #' ',d0                ; End it, if it is (see above)
  152.         beq.s    .EndNQuoteArg
  153.         cmp.b    #9,d0
  154.         beq.s    .EndNQuoteArg
  155.         cmp.b    #12,d0
  156.         beq.s    .EndNQuoteArg
  157.         cmp.b    #13,d0
  158.         beq.s    .EndNQuoteArg
  159.         cmp.b    #10,d0
  160.         beq.s    .EndNQuoteArg
  161.         move.b    d0,(a1)+            ; Copy char to new location
  162.         bra    .NoQuote            ; Here we go again
  163. .EndNQuoteArg    move.b    #0,(a1)+            ; end of string
  164. .ArgDone    addq.l    #1,_ArgC            ; one more argument
  165.         tst.b    d0                ; Is it an end of string ??
  166.         bne    .FindArgs            ; No -> next arg
  167.         subq.l    #1,a0                ; one char backwards
  168.         bra    .FindArgs            ; -> go on with next arg
  169. .ArgsFound    move.b    #0,(a1)                ; Last end of string
  170.         move.l    _ArgC(pc),d0            ; Amount of args
  171.         addq.l    #1,d0                ; One more for the zero-pointer
  172.         asl.l    #2,d0                ; * 4 (for Pointer)
  173.         move.l    #MEMF_CLEAR|MEMF_CHIP,d1
  174.         DOLIB    AllocMem            ; Get memory for ArgV
  175.         tst.l    d0
  176.         beq    .ErrorStartUp
  177.         move.l    d0,_ArgV
  178.  
  179.         move.l    .ArgLine(pc),a0            ; Get address of parsed string
  180.         move.l    d0,a1
  181.         move.l    _ArgC(pc),d7
  182.         subq.l    #1,d7
  183. .MakeArgVs    move.l    a0,(a1)+            ; Store address of argument
  184.         move.l    a0,a2
  185.         moveq    #1,d0
  186. .GetArgLen    tst.b    (a2)+                ; Get length of argument
  187.         beq.s    .GotArgLen
  188.         addq.l    #1,d0
  189.         bra    .GetArgLen
  190. .GotArgLen    add.l    d0,a0                ; Get address of next arg
  191.         dbf    d7,.MakeArgVs            ; Loop
  192.         move.l    #0,(a1)                ; End of array
  193.  
  194.         bra.s    .EndHardSUp20            ; finally start the prg
  195.  
  196. .FromWorkBench    move.l    a0,a4
  197.         lea    pr_MsgPort(a4),a0        ; Wait for StartUp-Msg
  198.         move.l    a0,_WBMsgPort            ; Store pointer to port
  199.         CALLSYS WaitPort
  200.         lea    pr_MsgPort(a4),a0
  201.         DOLIB    GetMsg                ; Get msg
  202.         move.l    d0,_WBStartUp
  203.  
  204.         move.l    _WBStartUp(pc),a4
  205.         move.l    sm_ArgList(a4),a4        ; Get pointer to arglist
  206.         cmp.l    #0,a4                ; if NULL -> go on
  207.         beq.s    .EndHardSUp20
  208.         move.l    wa_Lock(a4),d1            ; Get lock to Prg-Dir
  209.         LNKLIB    CurrentDir,_DOSBase
  210.         move.l    wa_Name(a4),a0            ; Read in .info-File
  211.         LNKLIB    GetDiskObject,_IconBase
  212.         tst.l    d0                ; Error -> quit
  213.         beq.s    .ErrorStartUp
  214.         move.l    d0,.SUpDiskObj            ; Store address
  215.         move.l    d0,a0
  216.         move.l    do_ToolTypes(a0),_ArgV        ; Store pointer to array (like CLI)
  217.         move.l    #0,_ArgC            ; No arguments (for identify)
  218.  
  219. .EndHardSUp20    jsr    _main                ; Start main routine
  220.         bra.s    .NoErrorStartUp
  221.  
  222. .ErrorStartUp    move.l    #20,d0                ; Prg-Failed
  223. .NoErrorStartUp    move.l    d0,-(sp)            ; Store Error-Code
  224.  
  225.         tst.l    _WBStartUp            ; Did we come from WB ?
  226.         beq.s    .ExitFromDOS            ; No -> go on
  227.  
  228.         tst.l    .SUpDiskObj            ; Do we have a Disk-Object ?
  229.         beq.s    .EndStartUp
  230.         move.l    .SUpDiskObj(pc),a0
  231.         LNKLIB    FreeDiskObject,_IconBase    ; Free the object
  232.         bra.s    .EndStartUp
  233.  
  234. .ExitFromDOS    tst.l    _ArgV                ; Do we have ArgV ?
  235.         beq.s    .NoArgV
  236.         move.l    _ArgV(pc),a1            ; Free memory
  237.         move.l    _ArgC(pc),d0
  238.         addq.l    #1,d0
  239.         asl.l    #2,d0
  240.         CALLSYS    FreeMem
  241.  
  242. .NoArgV        tst.l    .ArgLine            ; Command-buffer ?
  243.         beq.s    .EndStartUp
  244.         move.l    .ArgLine(pc),a1            ; Free memory
  245.         move.l    .ArgLen(pc),d0
  246.         DOLIB    FreeMem
  247.  
  248. .EndStartUp    tst.l    _IconBase
  249.         beq.s    .NoSIBase
  250.         move.l    _IconBase(pc),a1
  251.         CALLSYS    CloseLibrary
  252.  
  253. .NoSIBase    tst.l    _DOSBase
  254.         beq.s    .NoSDBase
  255.         move.l    _DOSBase(pc),a1
  256.         DOLIB    CloseLibrary
  257.  
  258. .NoSDBase    tst.l    _WBStartUp
  259.         beq.s    .NoWB
  260.         CALLSYS    Forbid
  261.         move.l    _WBStartUp(pc),a1        ; Finally reply the Msg
  262.         DOLIB    ReplyMsg
  263.  
  264. .NoWB        move.l    (sp)+,d0            ; restore error-code
  265.         rts
  266. .SUpIconName    dc.b    "icon.library",0
  267.         EVEN
  268. .SUpDosName    dc.b    "dos.library",0
  269.         EVEN
  270. .ArgLine    dc.l    0
  271. .ArgLen        dc.l    0
  272. .ComBuff    dc.l    0
  273. .SUpDiskObj    dc.l    0
  274. _ArgC        dc.l    0
  275. _ArgV        dc.l    0
  276. _stdout        dc.l    0
  277. _stdin        dc.l    0
  278. _OurTask    dc.l    0
  279. _WBMsgPort    dc.l    0
  280. _WBStartUp    dc.l    0
  281. _DOSBase    dc.l    0
  282. _IconBase    dc.l    0
  283.  
  284. _main
  285.  
  286.