home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 113 / EnigmaAmiga113CD.iso / software / utilities / viewer / source / startup.easy < prev    next >
Encoding:
Text File  |  1993-05-21  |  10.3 KB  |  423 lines

  1. *
  2. ** Startup.Easy v1.1
  3. *
  4. ** Author: Jörg van de Loo
  5. **         Hövel 15
  6. **         47559 Kranenburg
  7. **         Germany
  8. ** ...this material was written using HiSoft's "Devpac Assembler 3".
  9. *
  10. ** This  file  is  FreeWare (I would say public domain but this gives criminal
  11. ** people the right to add "their" copyright remark, so to protect myself from
  12. ** excluding  of  the  use  of  this  startup-code,  it's FreeWare and I'm the
  13. ** copyright holder).
  14. ** HiSoft  (UK) is it allowed to use/spread/include this file if they like to,
  15. ** also in modified form, for free.
  16. *
  17. ** Date: 5.21.1993
  18. *
  19. ** I  cannot  be  held  for  any  failures may happen using this startup-code.
  20. ** Unnecessary to tell that you use it at your own risk.
  21. *
  22.  
  23. *
  24. ** This  startup-code  is  reentrant,  so  if your code is it too, you may use
  25. ** Shell command "Resident".
  26. *
  27.  
  28. *
  29. ** The  BaseTable is only readable!  Don't go messing with 'em or weird things
  30. ** may happen.
  31. *
  32.  
  33. *
  34. ** The BaseTable used by this startup-code is 68 bytes large.  If you define a
  35. ** label  called  "_TableSize"  before  you  include  the  startup-code  (it's
  36. ** important!)  then  you  can  specify the bytesize of the table on your own,
  37. ** e.g.  1024 bytes.  Up from offset 68 you can now store your own datas,
  38. ** e.g. _GfxBase.
  39. *
  40. ** Note:   This  startup-code  offers only 32KB of datas that can be reached -
  41. **       BSS relative to A5.
  42. *
  43.  
  44. *
  45. ** Example:
  46. **
  47. *    STRUCTURE    mytable,68    ; begin at offset 68
  48. *    APTR        _GfxBase    ; offset 68
  49. *    APTR        _IntuitionBase    ; offset 72
  50. *    UBYTE        _myFlag        ; offset 76
  51. *    LABEL        _TableSize
  52. *        ; size for "BaseTable" and! "mytable" (here 77 bytes) -
  53. *        ; automatically allocated when startup-code is executed.
  54. *
  55. *    include        include/startup.easy    ; insert startup-code
  56. *
  57. * _main        ; this label is the entry point for your code!
  58. *    ....
  59. *    ....
  60. *
  61. **
  62. ** The  label  "_main" will be called out of the startup-code.  There your own
  63. ** code has to start.
  64. ** When  your code returns to the exit-routine, all things allocated/opened by
  65. ** the startup-code are freed/closed.
  66. ** The  address  register  where  the  BaseTable  is  stored, is the processor
  67. ** register A5.  Now you can store/read constants out of it, e.g.
  68. *
  69. *    movea.l    _DOSBase(A5),A6            ; get base of dos-lib
  70. *
  71.  
  72. *
  73. ** To exit your code there are different ways.
  74. *
  75. **  1. By "RTS"                stack must be ok!
  76. **  2. By calling "bsr _exit"         no need for corrected stack
  77. **  3. By calling "bra _exit"         ditto
  78. **  4. By calling "jsr _exit"         ditto
  79. **  5. By calling "jmp _exit"         ditto
  80. *
  81. ** It's not necessary to restore the stack nor to restore the A5 register when
  82. ** using exits 2,3,4,5.
  83. *
  84.  
  85. *
  86. ** This  startup-code  is  a  thined  version  of  a  startup-code which I use
  87. ** normally.   Because  I  wrote a few programs for the PD and I don't want to
  88. ** spread  my  hard  worked made startup-code (it's a lot time better than the
  89. ** original of Commodore), I wrote this one.
  90. *
  91.  
  92.  
  93.     IFND    STARTUP_EASY
  94. STARTUP_EASY    SET    1
  95.  
  96. *    incdir    :include/
  97.     IFND    EXEC_TYPES_I
  98.     include    exec/types.i
  99.     ENDC
  100.  
  101.     IFND    EXEC_MEMORY_I
  102.     include    exec/memory.i
  103.     ENDC
  104.  
  105.     IFND    EXEC_TASKS_I
  106.     include    exec/tasks.i
  107.     ENDC
  108.  
  109.     IFND    EXEC_LIBRARIES_I
  110.     include    exec/libraries.i
  111.     ENDC
  112.  
  113.     IFND    CALLEXEC
  114.     include    exec/exec_lib.i
  115.     ENDC
  116.  
  117. *    include    exec/execbase.i
  118.  
  119.     IFND    CALLDOS
  120.     include    dos/dos_lib.i
  121.     ENDC
  122.  
  123.     IFND    DOS_DOSEXTENS_I
  124.     include    dos/dosextens.i
  125.     ENDC
  126.  
  127.     IFND    WORKBENCH_STARTUP_I
  128.     include    workbench/startup.i
  129.     ENDC
  130.  
  131. *    include    workbench/icon_lib.i
  132.  
  133.     IFND    WORKBENCH_WORKBENCH_I
  134.     include    workbench/workbench.i
  135.     ENDC
  136.  
  137.  
  138.     IFND    AbsExecBase
  139. AbsExecBase    EQU    4
  140.     ENDC
  141.  
  142.     IFND    ThisTask
  143. ThisTask        EQU    276
  144.     ENDC
  145.  
  146.     IFND    _LVOGetDiskObject
  147. _LVOGetDiskObject    EQU    -78
  148.     ENDC
  149.  
  150.     IFND    _LVOFreeDiskObject
  151. _LVOFreeDiskObject    EQU    -90
  152.     ENDC
  153.  
  154.  
  155.  
  156. StoreTableSize    MACRO
  157.     IFND     _TableSize
  158.     move.l    #256,D0        ; Default, if nothing is specified
  159.  
  160.     ELSE
  161.  
  162.     IFLE    _TableSize-127    ; If _TableSize < 128 use moveq
  163.     moveq    #_TableSize,D0
  164.  
  165.     ELSE
  166.  
  167.     IFLE    _TableSize-32767 ; _TableSize not larger than 32KB
  168.     move.l    #_TableSize,D0
  169.  
  170.     ELSE
  171.     FAIL    <BSS-table exeeds 32KB!> ; else tell user
  172.     ENDC
  173.  
  174.     ENDC
  175.     ENDC
  176.  
  177.     addq.l    #7,D0        ; ...make size divisible
  178.     andi.l    #-8,D0        ; through eight -> for exec's AllocMem()...
  179.     ENDM
  180.  
  181.  
  182.  
  183. *
  184. ** This is the BaseTable....
  185. *
  186.    STRUCTURE    BaseTable,0    ; begin at offset NULL
  187.     APTR    bt_Reserved0    ; currently unused - do not use!
  188.     APTR    _SysBase    ; pointer to exec-base (readable)
  189.                 ; NOTE: « movea.l _SysBase.w,A6 » still work
  190.     APTR    bt_Reserved1    ; currently unused - do not use!
  191.     APTR    bt_Reserved2    ; currently unused - do not use!
  192.     APTR    _DOSBase    ; pointer to dos-base (opened/readable)
  193.     APTR    _IconBase    ; pointer to icon-base (readable, may not opened)
  194.     APTR    _argv        ; pointer to dos/WB-arguments (readable, may NULL)
  195.     ULONG    _argc        ; numbers of dos/WB-arguments (readable, may NULL)
  196.     APTR    _OwnTask    ; pointer to own-process-structure (readable)
  197.     APTR    _OwnName    ; pointer to own-program-name
  198.                 ; (readable, Shell: with path, WB: name only)
  199.     ULONG    _CmdNameSize    ; PRIVATE!!!
  200.     APTR    _WBenchMsg    ; pointer to Workbench-message (readable, may NULL)
  201.     APTR    _ToolTypesArray    ; pointer to tooltypes (readable, may NULL)
  202.     APTR    _OwnDiskObject    ; pointer to own-program-icon-structure (readable,
  203. *                  may NULL)
  204.     APTR    _stdin        ; pointer to standard output-handle (readable,
  205. *                  may NULL)
  206.     APTR    _stdout        ; pointer to standard input-handle (readable,
  207. *                  may NULL)
  208.     ULONG    _errno        ; place into this constant the returncode of
  209. *                  your program
  210.     LABEL    bt_SIZEOF    ; size of this table: 68 bytes - next free offset: 68!
  211.  
  212. *
  213. ** Where  "may NULL" stands it's possible that this values are not initialized
  214. ** if running from Workbench or when running from Shell.
  215. *
  216. * Examples: The WBenchMsg will be NULL if running under Shell.
  217. *        The stdin or stdout will be NULL when running from Workbench.
  218. *        So watch out!
  219.  
  220.  
  221. _startup
  222.     movem.l    D0/A0,-(sp)
  223.  
  224.     move.l    AbsExecBase.w,A6
  225.     StoreTableSize            ; MACRO - bytesize of "_TableSize"
  226.     move.l    #MEMF_PUBLIC|MEMF_CLEAR,D1
  227.     jsr    _LVOAllocMem(A6)    ; Allocate BaseTable and user's BSS
  228.     tst.l    D0
  229.     beq.w    __noTable
  230.     movea.l    D0,A5            ; Pointer to BaseTable into A5
  231.     move.l    A6,_SysBase(A5)
  232.  
  233.     move.l    (sp)+,_argc(A5)        ; Number of chars in commandline
  234.     move.l    (sp)+,_argv(A5)        ; Address commandline (if Shell)
  235.  
  236.     move.l    A5,-(sp)        ; Save BaseTable right in front
  237. *                      of ReturnAddr of task
  238.  
  239.     cmpi.w    #32,LIB_VERSION(A6)    ; Say: I need at least kickstart 1.2
  240.     bls.w    __fastout
  241.  
  242.     lea    _DOSName(pc),A1
  243.     moveq    #0,D0            ; Say: any version up from  Kickstart 1.2
  244.     jsr    _LVOOpenLibrary(A6)
  245.     move.l    D0,_DOSBase(A5)
  246.     beq.w    __fastout        ; ...only for savety...
  247.  
  248.     sub.l    A1,A1
  249.     jsr    _LVOFindTask(A6)
  250.     move.l    D0,_OwnTask(A5)        ; Pointer to process-structure
  251.  
  252.     movea.l    D0,A2
  253.     tst.l    pr_CLI(A2)        ; Are we called from CLI or WBench?
  254.     bne.s    .fromDOS
  255.  
  256. * ...it was Workbench
  257.     lea    pr_MsgPort(A2),A0
  258.     jsr    _LVOWaitPort(A6)    ; Wait for WB-message
  259.  
  260.     lea    pr_MsgPort(A2),A0
  261.     jsr    _LVOGetMsg(A6)        ; Get message
  262.     move.l    D0,_WBenchMsg(A5)    ; and save message for later
  263.  
  264.     movea.l    D0,A0            ; Message
  265.     move.l    sm_NumArgs(A0),_argc(A5) ; Number of files
  266.     move.l    sm_ArgList(A0),_argv(A5) ; Array of locks and files
  267.  
  268.     lea    _IconName(pc),A1
  269.     moveq    #33,D0
  270.     jsr    _LVOOpenLibrary(A6)    ; Open Icon-library
  271.     move.l    D0,_IconBase(A5)
  272.     beq.s    .skipToolTypes
  273.  
  274.     tst.l    _argv(A5)        ; Array empty?
  275.     beq.s    .skipToolTypes
  276.     movea.l    _argv(A5),A0        ; Array of:
  277. *    lock[0],file[0], lock[1],file[1], lock[2],file[2] and so on
  278.     move.l    (A0),D1            ; wa_Lock(A0),D1 (home-lock)
  279.     beq.s    .skipLock
  280.     movea.l    _DOSBase(A5),A6
  281.     jsr    _LVOCurrentDir(A6)    ; Change directory to PROGDIR
  282.     tst.l    D0
  283.     bmi.s    .skipToolTypes
  284. .skipLock
  285.     movea.l    _IconBase(A5),A6
  286.     movea.l    _argv(A5),A0
  287.     movea.l    wa_Name(A0),A0        ; Our name...
  288.     move.l    A0,_OwnName(A5)        ; Remember name of prg
  289.     jsr    _LVOGetDiskObject(A6)    ; Read program-icon
  290.     tst.l    D0
  291.     beq.s    .skipToolTypes
  292.     move.l    D0,_OwnDiskObject(A5)    ; Remember icon-structure of our prg
  293.     movea.l    D0,A0
  294.     move.l    do_ToolTypes(A0),_ToolTypesArray(A5)
  295. .skipToolTypes
  296.     bra.s    __main
  297.  
  298. * ...Shell start...
  299. .fromDOS
  300.     movea.l    _DOSBase(A5),A6
  301.     jsr    _LVOInput(A6)
  302.     move.l    D0,_stdin(A5)
  303.     jsr    _LVOOutput(A6)
  304.     move.l    D0,_stdout(A5)
  305.  
  306.     movea.l    _OwnTask(A5),A0
  307.     movea.l    pr_CLI(A0),A0
  308.     adda.l    A0,A0
  309.     adda.l    A0,A0
  310.     movea.l    cli_CommandName(A0),A2    ; BSTR to our prg-name
  311.     adda.l    A2,A2
  312.     adda.l    A2,A2            ; APTR
  313.     moveq    #0,D2
  314.     move.b    (A2)+,D2        ; Get length of our name
  315.  
  316.     move.l    #103,_errno(A5)        ; Setup possible error
  317.  
  318.     move.l    D2,D0            ; Length
  319.     addq.w    #8,D0            ; plus 1 byte (NULL-byte) plus 7 &
  320.     andi.w    #-8,D0            ; "and" with -8 =  make length divisible through 8
  321.     move.l    D0,_CmdNameSize(A5)    ; Remember size for later
  322.     move.l    #MEMF_PUBLIC|MEMF_CLEAR,D1
  323.     movea.l    _SysBase(A5),A6
  324.     jsr    _LVOAllocMem(A6)    ; Allocate name-buffer
  325.     move.l    D0,_OwnName(A5)        ; Save pointer to name-buffer
  326.     beq.s    __closeAndFree
  327.  
  328.     clr.l    _errno(A5)        ; Tell: no error occurred
  329.  
  330.     movea.l    D0,A0            ; Name-buffer
  331.     bra.s    .doIt            ; Because of DBF
  332. .StoreName
  333.     move.b    (A2)+,(A0)+        ; Copy CMD-name into own buffer
  334. .doIt
  335.     dbf    D2,.StoreName
  336.     clr.b    (A0)            ; Not needed....
  337. __main
  338.     move.l    _SysBase(A5),A6
  339.     bsr.w    _main            ; Call user's code with:
  340. *                      ·A5 BaseTable, A6 SysBase·
  341.  
  342. _exit
  343.     movea.l    AbsExecBase.w,A6    ; Execbase
  344.     suba.l    A1,A1            ; 0 = own task (process)
  345.     jsr    _LVOFindTask(A6)    ; Find our process
  346.     movea.l    D0,A0            ; Pointer to A0
  347.     movea.l    pr_ReturnAddr(A0),A0    ; Program's exit address
  348.     subq.l    #8,A0
  349.     move.l    A0,sp            ; Correct Stackpointer
  350.     move.l    (sp)+,A5        ; Place basetable pointer in A5
  351. *                      and make a "RTS" using right address
  352.  
  353.     tst.l    _WBenchMsg(A5)
  354.     beq.s    .fromDOS
  355.  
  356.     tst.l    _IconBase(A5)
  357.     beq.s    .ReplyMsg
  358.  
  359.     tst.l    _OwnDiskObject(A5)
  360.     beq.s    .ReplyMsg
  361.     movea.l    _OwnDiskObject(A5),A0
  362.     movea.l    _IconBase(A5),A6
  363.     jsr    _LVOFreeDiskObject(A6)    
  364.  
  365. .ReplyMsg
  366.     movea.l    _SysBase(A5),A6
  367.     tst.l    _IconBase(A5)
  368.     beq.s    .skipIt
  369.     movea.l    _IconBase(A5),A1
  370.     jsr    _LVOCloseLibrary(A6)
  371. .skipIt
  372.     jsr    _LVOForbid(A6)
  373.  
  374.     movea.l    _WBenchMsg(A5),A1
  375.     jsr    _LVOReplyMsg(A6)
  376.  
  377.     bra.s    __closeAndFree
  378.  
  379. .fromDOS
  380.     movea.l    _SysBase(A5),A6
  381.     move.l    _CmdNameSize(A5),D0
  382.     movea.l    _OwnName(A5),A1
  383.     jsr    _LVOFreeMem(A6)
  384.  
  385. __closeAndFree
  386.     move.l    _errno(A5),D2
  387.     movea.l    _DOSBase(A5),A1
  388.     movea.l    _SysBase(A5),A6
  389.     jsr    _LVOCloseLibrary(A6)
  390.     movea.l    _OwnTask(A5),A2
  391.     movea.l    A5,A1
  392.     StoreTableSize
  393.     jsr    _LVOFreeMem(A6)    
  394.     move.l    D2,D0
  395.     move.l    D0,pr_Result2(A2)    ; Back to  Shell/WB with returncode in D0
  396.     rts                ; and a valid Result2 constant...
  397.  
  398. * ------------------------------------- *
  399. __noTable
  400.     addq.w    #8,sp
  401.     moveq    #103,D0
  402.     move.l    ThisTask(A6),A0
  403.     move.l    D0,pr_Result2(A0)
  404.     rts
  405. __fastout
  406.     movea.l    A5,A1
  407.     StoreTableSize
  408.     jsr    _LVOFreeMem(A6)    
  409.     moveq    #121,D0
  410.     move.l    ThisTask(A6),A0
  411.     move.l    D0,pr_Result2(A0)
  412.     rts
  413. _DOSName
  414.     dc.b    'dos.library',0
  415. _IconName
  416.     dc.b    'icon.library',0
  417.  
  418.     CNOP    0,4
  419.  
  420.     ENDC
  421. * ------------------------------------- *
  422.