home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d6xx / d658 / view.lha / View / Source / Main.c < prev    next >
C/C++ Source or Header  |  1992-05-15  |  32KB  |  1,078 lines

  1. /*-- AutoRev header do NOT      edit!
  2. *
  3. *   Program         :   Main.c
  4. *   Copyright       :   Copyright © 1991-92 Jaba Development
  5. *   Author          :   Jan van den Baard
  6. *   Creation Date   :   04-Apr-92
  7. *   Current version :   2.0
  8. *   Translator      :   Dice v2.06.40
  9. *
  10. *   REVISION HISTORY
  11. *
  12. *   Date          Version         Comment
  13. *   ---------     -------         ------------------------------------------
  14. *   04-Apr-92     2.0             Main program. (rewrite)
  15. *
  16. *-- REV_END --*/
  17.  
  18. #include "View.h"
  19.  
  20. struct WBStartup                *WBenchMsg      = NULL;
  21.  
  22. UBYTE                           *CX_PRIORITY        = "CX_PRIORITY";
  23. UBYTE                           *CX_POPWINDOW       = "CX_POPUP";
  24. UBYTE                           *CX_VIEW            = "VIEW";
  25. UBYTE                           *CX_FLUSH           = "FLUSH";
  26. UBYTE                           *CX_QUIT            = "QUIT";
  27.  
  28. WORD                             CX_DEF_PRIORITY    =   0;
  29. UBYTE                           *CX_DEF_POPWINDOW   =   "YES";
  30. UBYTE                           *CX_DEF_VIEW        =   "lalt lcommand v";
  31. UBYTE                           *CX_DEF_FLUSH       =   "lalt lcommand f";
  32. UBYTE                           *CX_DEF_QUIT        =   "lalt lcommand q";
  33.  
  34. UBYTE                           *vwVersion          =   "\0$VER: " CX_NAME " 37.45 (16.4.92)";
  35.  
  36. struct MsgPort                  *vwCxPort           =   NULL;
  37. struct MsgPort                  *vwIdPort           =   NULL;
  38.  
  39. ULONG                            vwCxMask           =   NULL;
  40. ULONG                            vwIdMask           =   NULL;
  41.  
  42. UBYTE                            vwDopen            =   FALSE;
  43. UBYTE                            vwIsOpen           =   FALSE;
  44. UBYTE                            vwFsu              =   TRUE;
  45. UBYTE                            vwCustom           =   FALSE;
  46.  
  47. UWORD                            vwTabSize;
  48.  
  49. CxObj                           *vwBroker           =   NULL;
  50.  
  51. UBYTE                           *vwQuitStr, *vwFlushStr, *vwViewStr;
  52.  
  53. struct NewBroker                 vwNewBroker = {
  54.     NB_VERSION, CX_NAME, CX_TITLE, CX_DESCRIPTION,
  55.     NBU_NOTIFY | NBU_UNIQUE, COF_SHOW_HIDE, NULL, 0
  56. };
  57.  
  58. struct Window                   *vwWindow           =   NULL;
  59. struct Screen                   *vwScreen           =   NULL;
  60. struct AsciiText                *vwText             =   NULL;
  61. struct RastPort                 *vwRPort;
  62. struct TextFont                 *vwFont;
  63. struct Line                     *vwFirst;
  64. struct Line                     *vwLast;
  65. struct Process                  *vwProc;
  66. struct StringScan                vwSearch;
  67. struct TextBlock                 vwBlocks[ 10 ];
  68. APTR                             vwWdPtr;
  69.  
  70. #ifdef __DETACH
  71. ULONG                           *vwArgs;
  72. ULONG                            vwWBArgs[14]       =   { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  73. UBYTE                           *_procname          =   "ViewD2.0";
  74. UBYTE                           *_template          =   "Name,CX_PRIORITY/K/N,CX_POPUP/K,VIEW/K,FLUSH/K,QUIT/K,LACE/S,SUPER/S,PRODUCTIVITY/S,A2024TENHZ/S,A2024FIFTEENHZ/S,NOPOINTER/S,SCROLL/S,TABS/K/N";
  75. UBYTE                           *_exthelp           =   NULL;
  76. LONG                             _stack             =   4096L;
  77. LONG                             _priority          =   NULL;
  78. LONG                             _BackGroundIO      =   NULL;
  79. #else
  80. ULONG                            vwArgs[14]         =   { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  81. struct RDArgs                    vwIArgs            =   { { 0, 0, 0}, 0, 0, 0, 0, 0 };
  82. struct RDArgs                   *vwFArgs            =   NULL;
  83. UBYTE                           *Template           =   "Name,CX_PRIORITY/K/N,CX_POPUP/K,VIEW/K,FLUSH/K,QUIT/K,LACE/S,SUPER/S,PRODUCTIVITY/S,A2024TENHZ/S,A2024FIFTEENHZ/S,NOPOINTER/S,SCROLL/S,TABS/K/N";
  84. #endif
  85.  
  86. UBYTE                            vwFound            =   FALSE;
  87. UBYTE                            vwPrinting         =   FALSE;
  88. UBYTE                            vwErr              =   FALSE;
  89.  
  90. ULONG                            vwSignal, vwShown, vwNum, vwId;
  91. UWORD                            vwYMax, vwMode, vwPMode, vwMaxLin, vwPMark;
  92.  
  93. UWORD                            vwDriPens          =   ~0;
  94.  
  95. UBYTE                            vwPath[ 256 ];
  96. UBYTE                            vwPatt[ 32  ];
  97. UBYTE                            vwFile[ 32  ];
  98. UBYTE                            vwName[ 512 ];
  99. UBYTE                            vwFBuf[ 256 ];
  100.  
  101. extern struct IntuitionBase     *IntuitionBase;
  102. extern struct GfxBase           *GfxBase;
  103. extern struct GadToolsBase      *GadToolsBase;
  104. extern struct UtilityBase       *UtilityBase;
  105.  
  106. extern UBYTE                     StdStr[];
  107. extern UBYTE                     ClearStr[];
  108. extern UBYTE                     MoveStr[];
  109. extern UBYTE                     ScrUpStr[];
  110. extern UBYTE                     ScrDnStr[];
  111. extern UBYTE                     ResStr[];
  112.  
  113. extern ULONG                     vwClass;
  114. extern UWORD                     vwCode, vwQualifier;
  115.  
  116. extern __far struct Custom       custom;
  117.  
  118. struct CxBase                   *CxBase             =   NULL;
  119. struct IconBase                 *IconBase           =   NULL;
  120. struct NoFragBase               *NoFragBase         =   NULL;
  121. struct PPBase                   *PPBase             =   NULL;
  122. struct ReqToolsBase             *ReqToolsBase       =   NULL;
  123.  
  124. Prototype void OpenLibraries( void );
  125. Prototype void CloseLibraries( void );
  126. Prototype void Quit( long );
  127. Prototype void OpenDisplay( void );
  128. Prototype void CloseDisplay( void );
  129. Prototype void QuitCommodity( void );
  130. Prototype void myMain( long );
  131. Prototype void MainLoop( void );
  132. Prototype void SetupCustom( void );
  133. Prototype void QuitCommodity( void );
  134. Prototype void SetupCommodity( void );
  135. Prototype void CheckDirExtension( UBYTE * );
  136. Prototype void LoadFile( long );
  137. Prototype void DisplayText( void );
  138. Prototype void MoveTo( UBYTE c, UBYTE r );
  139. Prototype void LineDown( void );
  140. Prototype void LineUp( void );
  141. Prototype void Top( void );
  142. Prototype void Bottom( void );
  143. Prototype void PageDown( void );
  144. Prototype void PageUp( void );
  145. Prototype void Percentage( void );
  146. Prototype void BytesShown( void );
  147. Prototype void InfoLine( void );
  148. Prototype void ErrorLine( UBYTE * );
  149. Prototype void Error( UBYTE * );
  150. Prototype long IsAsciiChar( UBYTE );
  151. Prototype long CheckAscii( UBYTE * );
  152. Prototype void PrintFound( UWORD );
  153. Prototype long CheckPP( void );
  154. Prototype long CountEsc( struct Line *, long );
  155. Prototype long ParseName( void );
  156. Prototype long IsItADir( void );
  157. Prototype void _waitwbmsg( void );
  158.  
  159. void OpenLibraries( void )
  160. {
  161.     NoFragBase   = ( struct NoFragBase * )   OpenLibrary( "nofrag.library", NOFRAG_VERSION );
  162.     CxBase       = ( struct CxBase * )       OpenLibrary( "commodities.library", 37L );
  163.     IconBase     = ( struct IconBase * )     OpenLibrary( "icon.library", 36L );
  164.     PPBase       = ( struct PPBase * )       OpenLibrary( "powerpacker.library", 34L );
  165.     ReqToolsBase = ( struct ReqToolsBase * ) OpenLibrary( REQTOOLSNAME, REQTOOLSVERSION );
  166.  
  167.     if ( ! NoFragBase || ! CxBase || ! IconBase || ! ReqToolsBase )
  168.         Quit( 20L );
  169. }
  170.  
  171. void CloseLibraries( void )
  172. {
  173.     if ( ReqToolsBase )         CloseLibrary(( struct Library * )ReqToolsBase );
  174.     if ( PPBase )               CloseLibrary(( struct Library * )PPBase );
  175.     if ( IconBase )             CloseLibrary( IconBase );
  176.     if ( CxBase )               CloseLibrary( CxBase );
  177.     if ( NoFragBase )           CloseLibrary( NoFragBase );
  178. }
  179.  
  180. void OpenDisplay( void )
  181. {
  182.     struct DimensionInfo         disp;
  183.     struct Screen               *lock;
  184.     ULONG                        modes, id;
  185.     UWORD                        width, height, depth = 2;
  186.  
  187.     if ( ! vwCustom ) {
  188.         if ( lock = LockPubScreen( "Workbench" )) {
  189.  
  190.             width       = lock->Width;
  191.             height      = lock->Height;
  192.             modes       = lock->ViewPort.Modes;
  193.             depth       = lock->BitMap.Depth;
  194.             id          = GetVPModeID( &lock->ViewPort );
  195.  
  196.             if ( vwArgs[ 6 ] )
  197.                 id     |= LORESLACE_KEY;
  198.  
  199.             UnlockPubScreen( NULL, lock );
  200.         } else
  201.             Quit( 20L );
  202.     } else
  203.         id = vwId;
  204.  
  205.     GetDisplayInfoData( NULL, ( UBYTE * )&disp, sizeof( struct DimensionInfo ), DTAG_DIMS, id );
  206.  
  207.     width  = disp.TxtOScan.MaxX + 1;
  208.     height = disp.TxtOScan.MaxY + 1;
  209.  
  210.     if ( ! ( vwScreen = OpenScreenTags( NULL, SA_Pens,          &vwDriPens,
  211.                                               SA_DisplayID,     id,
  212.                                               SA_Overscan,      OSCAN_TEXT,
  213.                                               SA_Width,         width,
  214.                                               SA_Height,        height,
  215.                                               SA_Depth,         depth,
  216.                                               TAG_DONE )))
  217.         Quit( 20L );
  218.  
  219.     if ( ! ( vwWindow = OpenWindowTags( NULL, WA_RptQueue,      1,
  220.                                               WA_Width,         width,
  221.                                               WA_Height,        height,
  222.                                               WA_IDCMP,         IDCMP_RAWKEY | IDCMP_ACTIVEWINDOW | IDCMP_INACTIVEWINDOW,
  223.                                               WA_Flags,         WFLG_ACTIVATE | WFLG_RMBTRAP | WFLG_SMART_REFRESH | WFLG_BORDERLESS | WFLG_BACKDROP,
  224.                                               WA_CustomScreen,  vwScreen,
  225.                                               TAG_DONE )))
  226.         Quit( 20L );
  227.  
  228.     ShowTitle( vwScreen, FALSE );
  229.  
  230.     if ( vwArgs[ 11 ] ) OFF_SPRITE;
  231.  
  232.     vwIdPort                = vwWindow->UserPort;
  233.     vwIdMask                = ( 1L << vwIdPort->mp_SigBit );
  234.     vwWdPtr                 = vwProc->pr_WindowPtr;
  235.     vwProc->pr_WindowPtr    = (APTR)vwWindow;
  236.     vwRPort                 = vwWindow->RPort;
  237.     vwFont                  = vwRPort->Font;
  238.     vwMaxLin                = ( height / vwFont->tf_YSize ) - 1;
  239.  
  240.     sprintf(( char * )&StdStr[ 7 ], "%02.2ldy", vwFont->tf_YSize );
  241.  
  242.     if ( ! OpenConsole())
  243.         Quit( 20L );
  244.  
  245.     vwIsOpen                = TRUE;
  246.  
  247.     if ( ! vwText ) {
  248.         if ( vwFsu ) {
  249.             if ( strlen( vwName )) {
  250.                 if ( ! ParseName()) LoadFile( TRUE );
  251.                 else                LoadFile( FALSE );
  252.             } else
  253.                 LoadFile( TRUE );
  254.         } else
  255.             LoadFile( TRUE );
  256.     }
  257.  
  258.     if ( vwText ) {
  259.         DisplayText();
  260.         BytesShown();
  261.         InfoLine();
  262.     } else
  263.         InfoLine();
  264.  
  265.     vwFsu = FALSE;
  266. }
  267.  
  268. long IsItADir( void )
  269. {
  270.     struct FileInfoBlock    *fib;
  271.     BPTR                     lock;
  272.     long                     ret = -1L;
  273.  
  274.     if( fib = ( struct FileInfoBlock * )AllocDosObjectTags( DOS_FIB, TAG_DONE )) {
  275.         if( lock = Lock(( char * )vwName, ACCESS_READ )) {
  276.             Examine( lock, fib );
  277.             ret = fib->fib_DirEntryType;
  278.             UnLock( lock );
  279.         }
  280.         FreeDosObject( DOS_FIB, fib );
  281.     }
  282.     return(ret);
  283. }
  284.  
  285.  
  286. long ParseName( void )
  287. {
  288.     char *str;
  289.     long  len;
  290.  
  291.     if( strlen( vwName )) {
  292.         if( str = PathPart( vwName )) {
  293.             if ( IsItADir() < 0 ) {
  294.                 if ( *str == '/' ) str++;
  295.                 strcpy( vwFile, str );
  296.                 len = strlen( vwName ) - strlen( vwFile );
  297.                 strcpy( vwPath, vwName );
  298.                 vwPath[ len ] = 0;
  299.             } else
  300.                 strcpy( vwPath, vwName );
  301.         } else
  302.             strcpy( vwFile, vwName );
  303.     }
  304.     return( strlen( vwFile ));
  305. }
  306.  
  307. void CloseDisplay( void )
  308. {
  309.     CloseConsole();
  310.  
  311.     if ( vwArgs[ 11 ] ) ON_SPRITE;
  312.  
  313.     if ( vwWindow ) {
  314.         vwProc->pr_WindowPtr = vwWdPtr;
  315.         ClearMsgPort( vwIdPort );
  316.         CloseWindow( vwWindow );
  317.         vwWindow = NULL;
  318.         vwIdPort = NULL;
  319.         vwIdMask = NULL;
  320.     }
  321.  
  322.     if ( vwScreen ) {
  323.         CloseScreen( vwScreen );
  324.         vwScreen = NULL;
  325.     }
  326.  
  327.     vwIsOpen = FALSE;
  328. }
  329.  
  330. void Quit( long code )
  331. {
  332.     ON_SPRITE;
  333.  
  334.     CloseDisplay();
  335.     QuitCommodity();
  336.  
  337.     if ( vwText )       FreeAscii( vwText );
  338.  
  339.     CloseLibraries();
  340.  
  341. #ifndef __DETACH
  342.     if ( vwFArgs )      FreeArgs( vwFArgs );
  343.     _exit( code );
  344. #else
  345.     exit( code );
  346. #endif
  347. }
  348.  
  349. void SetupCommodity( void )
  350. {
  351.     CxObj               *tmp;
  352.     struct WBArg        *wba;
  353.     char                *pw, **ttypes;
  354.     WORD                 pri;
  355.  
  356.     if ( WBenchMsg ) {
  357.         ttypes = ArgArrayInit( NULL, ( UBYTE ** )WBenchMsg );
  358.  
  359.         pri         = ArgInt( ttypes, CX_PRIORITY, CX_DEF_PRIORITY );
  360.         pw          = ArgString( ttypes, CX_POPWINDOW, CX_DEF_POPWINDOW );
  361.         vwViewStr   = ArgString( ttypes, CX_VIEW, CX_DEF_VIEW );
  362.         vwFlushStr  = ArgString( ttypes, CX_FLUSH, CX_DEF_FLUSH );
  363.         vwQuitStr   = ArgString( ttypes, CX_QUIT, CX_DEF_QUIT );
  364.         vwArgs[ 6 ] = ArgString( ttypes, "LACE", NULL );
  365.         vwArgs[ 7 ] = ArgString( ttypes, "SUPER", NULL );
  366.         vwArgs[ 8 ] = ArgString( ttypes, "PRODUCTIVITY", NULL );
  367.         vwArgs[ 9 ] = ArgString( ttypes, "A2024TENHZ", NULL );
  368.         vwArgs[ 10] = ArgString( ttypes, "A2024FIFTEENHZ", NULL );
  369.         vwArgs[ 11] = ArgString( ttypes, "NOPOINTER", NULL );
  370.         vwArgs[ 12] = ArgString( ttypes, "SCROLL", NULL );
  371.         vwTabSize   = ArgInt( ttypes, "TABS", ANSITAB );
  372.  
  373.         wba = WBenchMsg->sm_ArgList;
  374.  
  375.         if ( WBenchMsg->sm_NumArgs > 1 ) {
  376.             wba++;
  377.             CurrentDir( wba->wa_Lock );
  378.             if ( wba->wa_Name ) strcpy( vwName, ( char * )wba->wa_Name );
  379.         }
  380.     } else {
  381.         if ( vwArgs[ 0 ] ) strcpy( vwName, (char *)vwArgs[ 0 ] );
  382.  
  383.         if ( vwArgs[ 1  ] )  pri        = vwArgs[ 1 ];
  384.         else                 pri        = CX_DEF_PRIORITY;
  385.         if ( vwArgs[ 2  ] )  pw         = ( char * )vwArgs[ 2 ];
  386.         else                 pw         = CX_DEF_POPWINDOW;
  387.         if ( vwArgs[ 3  ] )  vwViewStr  = ( char * )vwArgs[ 3 ];
  388.         else                 vwViewStr  = CX_DEF_VIEW;
  389.         if ( vwArgs[ 4  ] )  vwFlushStr = ( char * )vwArgs[ 4 ];
  390.         else                 vwFlushStr = CX_DEF_FLUSH;
  391.         if ( vwArgs[ 5  ] )  vwQuitStr  = ( char * )vwArgs[ 5 ];
  392.         else                 vwQuitStr  = CX_DEF_QUIT;
  393.         if ( vwArgs[ 13 ])   vwTabSize  = (UWORD)(*((ULONG *)vwArgs[ 13 ]));
  394.         else                 vwTabSize  = ANSITAB;
  395.     }
  396.  
  397.     SetupCustom();
  398.  
  399.     if ( ! Stricmp( pw, CX_DEF_POPWINDOW )) vwDopen = TRUE;
  400.     else                                    vwDopen = FALSE;
  401.  
  402.     if ( ! ( vwCxPort = CreateMsgPort()))
  403.         Quit( RETURN_FAIL );
  404.  
  405.     if ( vwTabSize < MINTAB )       vwTabSize = MINTAB;
  406.     else if ( vwTabSize > MAXTAB )  vwTabSize = MAXTAB;
  407.  
  408.     vwNewBroker.nb_Pri  = pri;
  409.     vwNewBroker.nb_Port = vwCxPort;
  410.  
  411.     vwCxMask            = ( 1L <<  vwCxPort->mp_SigBit );
  412.  
  413.     if ( ! ( vwBroker = CxBroker( &vwNewBroker, NULL )))
  414.         Quit( RETURN_FAIL );
  415.  
  416.     if ( ! ( tmp = HotKey( vwQuitStr, vwCxPort, CXC_QUIT )))
  417.         Quit( RETURN_FAIL );
  418.     AttachCxObj( vwBroker, tmp );
  419.  
  420.     if ( ! ( tmp = HotKey( vwFlushStr, vwCxPort, CXC_FLUSH )))
  421.         Quit( RETURN_FAIL );
  422.     AttachCxObj( vwBroker, tmp );
  423.  
  424.     if ( ! ( tmp = HotKey( vwViewStr, vwCxPort, CXC_VIEW )))
  425.         Quit( RETURN_FAIL );
  426.     AttachCxObj( vwBroker, tmp );
  427.  
  428.     if( CxObjError( vwBroker ))
  429.         Quit( RETURN_FAIL );
  430.  
  431.     if(WBenchMsg)
  432.         ArgArrayDone();
  433.  
  434.     CxOn( vwBroker );
  435. }
  436.  
  437. void SetupCustom( void )
  438. {
  439.     if ( vwArgs[ 7 ] && ! ModeNotAvailable( DEFAULT_MONITOR_ID | SUPER_KEY )) {
  440.         vwId = DEFAULT_MONITOR_ID | SUPER_KEY;
  441.         vwCustom = TRUE;
  442.     } else if ( vwArgs[ 8 ] && ! ModeNotAvailable( DEFAULT_MONITOR_ID | VGAPRODUCT_KEY )) {
  443.         vwId = DEFAULT_MONITOR_ID | VGAPRODUCT_KEY;
  444.         vwCustom = TRUE;
  445.     } else if ( vwArgs[ 9 ] && ! ModeNotAvailable( DEFAULT_MONITOR_ID | A2024TENHERTZ_KEY )) {
  446.         vwId = DEFAULT_MONITOR_ID | A2024TENHERTZ_KEY;
  447.         vwCustom = TRUE;
  448.     } else if ( vwArgs[ 10 ] && ! ModeNotAvailable( DEFAULT_MONITOR_ID | A2024FIFTEENHERTZ_KEY )) {
  449.         vwId = DEFAULT_MONITOR_ID | A2024FIFTEENHERTZ_KEY;
  450.         vwCustom = TRUE;
  451.     } else
  452.         vwCustom = FALSE;
  453.  
  454.     if ( vwArgs[ 6 ] ) vwId |= LORESLACE_KEY;
  455. }
  456.  
  457. void QuitCommodity( void )
  458. {
  459.     if ( vwBroker )     DeleteCxObjAll( vwBroker );
  460.  
  461.     if( vwCxPort ) {
  462.         ClearMsgPort( vwCxPort );
  463.         DeleteMsgPort( vwCxPort );
  464.     }
  465. }
  466.  
  467. void MainLoop( void )
  468. {
  469.     struct Message      *msg;
  470.     struct IntuiMessage *imsg;
  471.     ULONG                sig, type, id, class;
  472.     BOOL                 running = TRUE;
  473.  
  474.     if ( vwDopen )    OpenDisplay();
  475.  
  476.     do {
  477.         if ( vwIsOpen)   Percentage();
  478.  
  479.         sig = Wait( SIGBREAKF_CTRL_C | vwCxMask | vwIdMask );
  480.  
  481.         if (( sig & SIGBREAKF_CTRL_C ) == SIGBREAKF_CTRL_C ) {
  482.             if ( ! vwPrinting ) running = FALSE;
  483.         } else if (( sig & vwCxMask ) == vwCxMask ) {
  484.  
  485.             while (( msg = (struct Message *)GetMsg( vwCxPort ))) {
  486.  
  487.                 id   = CxMsgID(( CxMsg * )msg );
  488.                 type = CxMsgType(( CxMsg * )msg );
  489.                 ReplyMsg( msg );
  490.  
  491.                 switch( type ) {
  492.  
  493.                     case    CXM_IEVENT:
  494.  
  495.                         switch( id ) {
  496.  
  497.                             case    CXC_VIEW:
  498.                                     if ( ! vwIsOpen )
  499.                                         OpenDisplay();
  500.                                     else {
  501.                                         ScreenToFront( vwScreen );
  502.                                         if( vwArgs[ 11 ] ) OFF_SPRITE;
  503.                                     }
  504.                                     break;
  505.  
  506.                             case    CXC_FLUSH:
  507.                                     if ( ! vwIsOpen ) {
  508.                                         if ( vwText ) FreeAscii( vwText );
  509.                                         vwText = NULL;
  510.                                     }
  511.                                     break;
  512.  
  513.                             case    CXC_QUIT:
  514.                                     if( ! vwIsOpen ) {
  515.                                         if ( ! vwPrinting ) running = FALSE;
  516.                                     }
  517.                                     break;
  518.                         }
  519.                         break;
  520.  
  521.                     case    CXM_COMMAND:
  522.                         switch( id ) {
  523.  
  524.                             case    CXCMD_KILL:
  525.                                 if ( ! vwPrinting ) running = FALSE;
  526.                                 break;
  527.  
  528.                             case    CXCMD_DISABLE:
  529.                                 CxOff( vwBroker );
  530.                                 break;
  531.  
  532.                             case    CXCMD_ENABLE:
  533.                                 CxOn( vwBroker );
  534.                                 break;
  535.  
  536.                             case    CXCMD_UNIQUE:
  537.                             case    CXCMD_APPEAR:
  538.                                 if ( ! vwIsOpen )
  539.                                     OpenDisplay();
  540.                                 else {
  541.                                     ScreenToFront( vwScreen );
  542.                                     if( vwArgs[ 11 ] ) OFF_SPRITE;
  543.                                 }
  544.                                 break;
  545.  
  546.                             case    CXCMD_DISAPPEAR:
  547.                                 if ( vwIsOpen )
  548.                                     CloseDisplay();
  549.                                 break;
  550.                         }
  551.                         break;
  552.                 }
  553.             }
  554.         } else if ( vwIsOpen ) {
  555.             if (( sig & vwIdMask ) == vwIdMask ) {
  556.  
  557.                 while ( ReadMsgPort( vwWindow->UserPort )) {
  558.                     Percentage();
  559.  
  560.                     switch( vwClass ) {
  561.                         case IDCMP_RAWKEY:
  562.                             HandleKeyboard( vwCode, vwQualifier );
  563.                             break;
  564.  
  565.                         case IDCMP_ACTIVEWINDOW:
  566.                             if ( vwArgs[ 11 ] ) OFF_SPRITE;
  567.                             break;
  568.  
  569.                         case IDCMP_INACTIVEWINDOW:
  570.                             if ( vwArgs[ 11 ] ) ON_SPRITE;
  571.                             break;
  572.                     }
  573.                     if ( ! vwIsOpen ) break;
  574.                 }
  575.             }
  576.         }
  577.     } while( running == TRUE );
  578.  
  579.     if( vwIsOpen )
  580.         CloseDisplay();
  581.     CxOff( vwBroker );
  582.     Quit(NULL);
  583. }
  584.  
  585. void CheckDirExtension( UBYTE *name )
  586. {
  587.     UWORD   len = strlen( name );
  588.  
  589.     if ( len ) {
  590.         if ( name[ len - 1 ] != ':' && name[ len - 1 ] != '/' )
  591.             strcat( name, "/" );
  592.     }
  593. }
  594.  
  595. void LoadFile( long request )
  596. {
  597.     UBYTE           *ppdata, *fname;
  598.     ULONG            pplen;
  599.     BPTR             ppout;
  600.     BOOL             pptemp = FALSE, ret = TRUE;
  601.     LONG             pperr;
  602.     struct BuffIO   *file = NULL;
  603.     struct Line     *line;
  604.  
  605.     ErrorLine( CX_NAME " v" CX_VERSION " © 1991-92 Jaba Development" );
  606.  
  607.     if ( request ) {
  608.         if ( vwArgs[ 11 ] ) ON_SPRITE;
  609.  
  610.         ret = FileRequest( vwWindow, "Select file to load...", vwFile, vwPath, vwPatt, FR_LOAD );
  611.  
  612.         if ( vwArgs[ 11 ] ) OFF_SPRITE;
  613.     }
  614.  
  615.     if ( ret ) {
  616.         if ( vwText ) {
  617.             FreeAscii( vwText );
  618.             vwText = NULL;
  619.             ClrBlk();
  620.         }
  621.  
  622.         Inform( ClearStr );
  623.         Inform( StdStr );
  624.  
  625.         Busy();
  626.  
  627.         if ( CheckPP()) {
  628.             if ( PPBase ) {
  629.                 ErrorLine( "Loading and decrunching file..." );
  630.                 if ( vwArgs[ 11 ] ) ON_SPRITE;
  631.                 if (( pperr = ppLoadData( vwName, DECR_POINTER, MEMF_PUBLIC, &ppdata, &pplen, NULL )) < 0 ) {
  632.                     switch( pperr ) {
  633.                         case    PP_OPENERR:
  634.                             Error( "PPLib: Can't open the file." );
  635.                             break;
  636.                         case    PP_READERR:
  637.                             Error("PPLIB: Read error.");
  638.                             break;
  639.                         case    PP_NOMEMORY:
  640.                             Error("PPLIB: Out of memory.");
  641.                             break;
  642.                         case    PP_PASSERR:
  643.                             Error("PPLIB: Incorrect password.");
  644.                             break;
  645.                         case    PP_UNKNOWNPP:
  646.                             Error("PPLIB: Unknown PowerPacker version.");
  647.                             break;
  648.                     }
  649.                     return;
  650.                 }
  651.  
  652.                 if ( vwArgs[ 11 ] ) OFF_SPRITE;
  653.                 fname = "T:view.pp.tmp.v" CX_VERSION ".decrunched";
  654.                 if ( ppout = Open( fname, MODE_NEWFILE )) {
  655.                     if ( Write( ppout, ppdata, pplen ) != pplen )
  656.                         Error( "Error writing temporary file." );
  657.                     else
  658.                         pptemp = TRUE;
  659.                     Close( ppout );
  660.                 } else
  661.                     Error( "Can't open temporary file." );
  662.  
  663.                 if ( ppdata ) FreeMem( ppdata, pplen );
  664.  
  665.                 if ( ! pptemp ) goto ppCleanUp;
  666.             } else
  667.                 Error( "Can't load powerpacker files\nwithout the powerpacker.library." );
  668.         } else {
  669.             ErrorLine( "Loading file..." );
  670.             fname = vwName;
  671.         }
  672.  
  673.         if ( CheckAscii( fname )) {
  674.             if ( vwText = AllocAscii( vwTabSize, MaxCol() + 1, ATF_SkipEsc | ATF_TabConvert )) {
  675.                 if ( file = BOpen( fname, MODE_OLDFILE )) {
  676.                     while( line = BGetS( file, vwText ))
  677.                         AddTail(( struct List * )vwText, ( struct Node * )line );
  678.                     switch( BIoErr( file )) {
  679.                         case    ASE_READ:
  680.                             Error( "Read error." );
  681.                             goto CleanExit;
  682.                         case    ASE_NOMEM:
  683.                             Error( "Out of memory." );
  684.                             goto CleanExit;
  685.                     }
  686.  
  687.                     BClose( file );
  688.                     vwFirst = vwText->First;
  689.                     vwFound = FALSE;
  690.                     DisplayText();
  691.                     BytesShown();
  692.                     InfoLine();
  693.                     goto ppCleanUp;
  694.                 } else {
  695.                     Error( "Could not open the file." );
  696.                     goto CleanExit;
  697.                 }
  698.             } else {
  699.                 Error( "Out of memory." );
  700.                 goto CleanExit;
  701.             }
  702.         } else {
  703.             Error( "File contains binary." );
  704.             goto CleanExit;
  705.         }
  706.     } else goto ppCleanUp;
  707.  
  708. CleanExit:
  709.     if ( vwText ) {
  710.         FreeAscii( vwText );
  711.         vwText = NULL;
  712.     }
  713.  
  714.     if ( file ) BClose( file );
  715.  
  716. ppCleanUp:
  717.     ClearMsgPort( vwIdPort );
  718.     Ready();
  719.     if ( pptemp )   DeleteFile( fname );
  720. }
  721.  
  722. void DisplayText( void )
  723. {
  724.     UWORD   y = 1, i = 0;
  725.  
  726.     if ( ! vwText )    return;
  727.  
  728.     for(  vwLast = vwFirst; i < vwMaxLin; i++, vwLast = vwLast->Next, y++ ) {
  729.         if ( vwLast == vwText->Last->Next )  break;
  730.         MoveTo( 1 , y );
  731.         Display( vwLast );
  732.         if ( vwFound && vwLast == vwSearch.Line ) PrintFound( y );
  733.     }
  734.     vwYMax = y - 1;
  735. }
  736.  
  737. void MoveTo( UBYTE c, UBYTE r )
  738. {
  739.     sprintf((char *)&MoveStr[ 1 ], "%02ld;%02ldH", r, c );
  740.     Inform( MoveStr );
  741. }
  742.  
  743. void LineDown( void )
  744. {
  745.     if ( ! vwText )    return;
  746.     if ( vwLast == vwText->Last->Next ) return;
  747.  
  748.     vwFirst = vwFirst->Next;
  749.     Inform( ScrUpStr );
  750.     MoveTo( 1, vwYMax  );
  751.     Display( vwLast );
  752.  
  753.     vwShown += vwLast->Size;
  754.  
  755.     if ( vwFound && vwLast == vwSearch.Line ) PrintFound( vwYMax );
  756.  
  757.     vwLast = vwLast->Next;
  758. }
  759.  
  760. void LineUp( void )
  761. {
  762.     if ( ! vwText )    return;
  763.     if ( vwFirst == vwText->First ) return;
  764.  
  765.     vwLast   = vwLast->Prev;
  766.     vwShown -= vwLast->Size;
  767.     vwFirst  = vwFirst->Prev;
  768.  
  769.     Inform( ScrDnStr );
  770.     MoveTo( 1, 1 );
  771.     Display( vwFirst );
  772.  
  773.     if ( vwFound && vwFirst == vwSearch.Line ) PrintFound( 1 );
  774. }
  775.  
  776. void Top( void )
  777. {
  778.     if ( ! vwText ) return;
  779.     if ( vwFirst == vwText->First ) return;
  780.  
  781.     vwFirst = vwText->First;
  782.  
  783.     Inform( ClearStr );
  784.     DisplayText();
  785.     BytesShown();
  786. }
  787.  
  788. void Bottom( void )
  789. {
  790.     UWORD   i;
  791.  
  792.     if ( ! vwText )    return;
  793.     if ( vwLast == vwText->Last->Next ) return;
  794.  
  795.     vwLast = vwFirst = vwText->Last;
  796.  
  797.     for( i = 1; i < vwMaxLin; i++ )
  798.         vwFirst = vwFirst->Prev;
  799.  
  800.     vwShown = vwText->NumBytes;
  801.  
  802.     Inform( ClearStr );
  803.     DisplayText();
  804. }
  805.  
  806. void PageDown( void )
  807. {
  808.     UWORD   i;
  809.  
  810.     if ( ! vwText ) return;
  811.     if ( vwLast == vwText->Last->Next ) return;
  812.  
  813.     if ( ! vwArgs[ 12 ] ) {
  814.         for( i = 1; i < vwMaxLin; i++ ) {
  815.             vwFirst  = vwFirst->Next;
  816.             vwShown += vwLast->Size;
  817.             if (( vwLast = vwLast->Next ) == vwText->Last->Next ) break;
  818.         }
  819.         Inform( ClearStr );
  820.         DisplayText();
  821.     } else {
  822.         for ( i = 1; i < vwMaxLin; i++ ) {
  823.             LineDown();
  824.             Percentage();
  825.         }
  826.     }
  827. }
  828.  
  829. void PageUp( void )
  830. {
  831.     UWORD   i;
  832.  
  833.     if ( ! vwText ) return;
  834.     if ( vwFirst == vwText->First ) return;
  835.  
  836.     if ( ! vwArgs[ 12 ] ) {
  837.         for( i = 1; i < vwMaxLin; i++ ) {
  838.             vwLast   = vwLast->Prev;
  839.             vwShown -= vwLast->Size;
  840.             if (( vwFirst = vwFirst->Prev ) == vwText->First ) break;
  841.         }
  842.         Inform( ClearStr );
  843.         DisplayText();
  844.     } else {
  845.         for ( i = 1; i < vwMaxLin; i++ ) {
  846.             LineUp();
  847.             Percentage();
  848.         }
  849.     }
  850. }
  851.  
  852. void Percentage( void )
  853. {
  854.     ULONG   perc;
  855.     char    pcbuf[5];
  856.  
  857.     if ( ! vwText )    return;
  858.  
  859.     perc = ( ULONG )( vwShown * 100 / vwText->NumBytes );
  860.  
  861.     if ( perc == 100 && ( vwLast != vwText->Last->Next )) perc = 99;
  862.  
  863.     vwNum = perc;
  864.  
  865.     sprintf(pcbuf,"%3ld%%",perc);
  866.  
  867.     SetAPen( vwRPort, 3 );
  868.     SetBPen( vwRPort, 2 );
  869.     SetDrMd( vwRPort, JAM2 );
  870.     Move( vwRPort, 544, vwFont->tf_Baseline );
  871.     Text( vwRPort, pcbuf, strlen( pcbuf ));
  872.     SetAPen( vwRPort, 1 );
  873.     Move(  vwRPort, 584, vwFont->tf_Baseline );
  874.     Text( vwRPort, "Shown.", 6);
  875. }
  876.  
  877. void BytesShown( void )
  878. {
  879.     struct Line *line;
  880.  
  881.     vwShown = 0L;
  882.  
  883.     for ( line = vwText->First; line != vwLast; line = line->Next )
  884.         vwShown += line->Size;
  885. }
  886.  
  887. void InfoLine( void )
  888. {
  889.     UBYTE   ibuf[100];
  890.  
  891.     ClrLin();
  892.  
  893.     vwErr = FALSE;
  894.  
  895.     SetAPen( vwRPort, 1 );
  896.     SetDrMd( vwRPort, JAM1 );
  897.  
  898.     if ( vwText )
  899.         sprintf( ibuf, "File : %-31.31ls %-6.6ld Lines, %-6.6ld Bytes.", &vwFile[0], vwText->NumLines - vwText->NumSplit, vwText->NumBytes );
  900.     else
  901.         strcpy( ibuf, "No text in memory (press 'l' to load a file)" );
  902.  
  903.     Move( vwRPort, 1, vwFont->tf_Baseline );
  904.     Text( vwRPort, ibuf, strlen( ibuf ));
  905. }
  906.  
  907. void ErrorLine( UBYTE *text )
  908. {
  909.     ClrLin();
  910.  
  911.     vwErr = TRUE;
  912.  
  913.     SetAPen( vwRPort, 1 );
  914.     SetDrMd( vwRPort, JAM1 );
  915.     Move( vwRPort, 1, vwFont->tf_Baseline );
  916.     Text( vwRPort, text, strlen( text ));
  917. }
  918.  
  919. void Error( UBYTE *error )
  920. {
  921.     rtEZRequestTags( error, "Continue", NULL, NULL,
  922.                      RT_ReqPos,         REQPOS_CENTERSCR,
  923.                      RT_WaitPointer,    TRUE,
  924.                      RTEZ_ReqTitle,     CX_NAME " " CX_VERSION,
  925.                      RTEZ_Flags,        EZREQF_CENTERTEXT,
  926.                      TAG_DONE );
  927.  
  928.     ClearMsgPort( vwIdPort );
  929. }
  930.  
  931. long IsAsciiChar( UBYTE chr )
  932. {
  933.     if ( chr > 0x00 && chr < 0x09 ) return( FALSE );
  934.     if ( chr > 0x0d && chr < 0x1b ) return( FALSE );
  935.     if ( chr > 0x1b && chr < 0x20 ) return( FALSE );
  936.     if ( chr > 0x7f && chr < 0x9b ) return( FALSE );
  937.     return( TRUE );
  938. }
  939.  
  940. long CheckAscii( UBYTE *name )
  941. {
  942.     BPTR        file;
  943.     UBYTE       memory[ 100 ];
  944.     ULONG       cnt, ret = TRUE, len;
  945.  
  946.     if ( file = Open( name, MODE_OLDFILE )) {
  947.         len = Read( file, memory, 100L );
  948.         Close( file );
  949.         if ( len ) {
  950.             for ( cnt = 0; cnt < len; cnt++ ) {
  951.                 if ( ! IsAsciiChar( memory[ cnt ] )) {
  952.                     ret = FALSE;
  953.                     break;
  954.                 }
  955.             }
  956.         }
  957.     }
  958.     return( ret );
  959. }
  960.  
  961. long CheckPP( void )
  962. {
  963.     BPTR    file;
  964.     ULONG   ident;
  965.  
  966.     if ( ! ( file = Open( vwName, MODE_OLDFILE ))) {
  967.         strcat( vwName, ".pp" );
  968.         if ( ! ( file = Open( vwName, MODE_OLDFILE ))) {
  969.             vwName[ strlen( vwName ) - 3 ] = 0;
  970.             return FALSE;
  971.         }
  972.     }
  973.     Read( file, &ident, 4L );
  974.     Close( file );
  975.  
  976.     if( ident == 'PP20' || ident == 'PX20' ) return TRUE;
  977.     return FALSE;
  978. }
  979.  
  980. long CountEsc( struct Line *line, long off )
  981. {
  982.     UWORD   i;
  983.     LONG    c = 0L;
  984.     char   *ptr = line->Text, chr;
  985.  
  986.     for( i = 0; i <= off; i++ ) {
  987.         if( *ptr == ESC || *ptr == CSI ) {
  988.             while( 1 ) {
  989.                c++;
  990.                i++;
  991.                chr = ( *ptr++ ) - ' ';
  992.                if(( chr >= '?' && chr <= 'Z' ) || !chr ) break;
  993.             }
  994.         }
  995.         else ptr++;
  996.     }
  997.  
  998.     return(c);
  999. }
  1000.  
  1001. void PrintFound( UWORD y )
  1002. {
  1003.     char wrd[256];
  1004.  
  1005.     setmem( &wrd[ 0 ], 256, 0 );
  1006.  
  1007.     Inform( "\033[32;43m" );
  1008.     strncpy( wrd, (char *)&vwSearch.Line->Text[ vwSearch.TextOffset ], vwSearch.StringSize );
  1009.     MoveTo( vwSearch.TextOffset - CountEsc( vwSearch.Line, vwSearch.TextOffset ) + 1, y );
  1010.     Inform( wrd );
  1011.     Inform( ResStr );
  1012. }
  1013.  
  1014. void myMain( long cli )
  1015. {
  1016.     OpenLibraries();
  1017.  
  1018.     vwFile[ 0 ] = vwName[ 0 ] = vwPath[ 0 ] = 0;
  1019.  
  1020.     strcpy( vwPatt, "~(#?.info)" );
  1021.  
  1022. #ifndef __DETACH
  1023.     if ( ! cli ) {
  1024.         if ( WBenchMsg->sm_ArgList->wa_Lock )
  1025.             CurrentDir( WBenchMsg->sm_ArgList->wa_Lock );
  1026.     }
  1027. #endif
  1028.  
  1029.     SetupCommodity();
  1030.     MainLoop();
  1031.     return;
  1032. }
  1033.  
  1034. #ifdef __DETACH
  1035. void main( long numargs, long *argarray )
  1036. {
  1037.     vwProc = ( struct Process * )FindTask( NULL );
  1038.     vwArgs = argarray;
  1039.  
  1040.     myMain( TRUE );
  1041.     return;
  1042.  
  1043.     atoi("");
  1044. }
  1045.  
  1046. void wbmain( struct WBStartup *su )
  1047. {
  1048.     vwArgs    = &vwWBArgs[ 0 ];
  1049.     WBenchMsg = su;
  1050.     myMain( FALSE );
  1051.     return;
  1052. }
  1053. #else
  1054. extern struct Library       *SysBase;
  1055. extern struct WBStartup     *_WBMsg;
  1056.  
  1057. void _main( void )
  1058. {
  1059.     vwProc = ( struct Process * )FindTask( NULL );
  1060.  
  1061.     if ( SysBase->lib_Version < 36 )
  1062.         _exit( RETURN_FAIL );
  1063.  
  1064.     if ( vwProc->pr_CLI ) {
  1065.         if ( vwFArgs = ReadArgs( Template, &vwArgs[ 0 ], &vwIArgs ))
  1066.             myMain( TRUE );
  1067.         return;
  1068.     } else {
  1069.         WBenchMsg = _WBMsg;
  1070.         myMain( FALSE );
  1071.         return;
  1072.     }
  1073.  
  1074.     _waitwbmsg();
  1075.     atoi("");
  1076. }
  1077. #endif
  1078.