home *** CD-ROM | disk | FTP | other *** search
/ World of A1200 / World_Of_A1200.iso / programs / commodity / newedit18b / newedit_source.lha / NewEdit.c < prev    next >
C/C++ Source or Header  |  1994-05-30  |  5KB  |  218 lines

  1. #include <stdlib.h>
  2.  
  3. #include <exec/execbase.h>
  4. #include <exec/memory.h>
  5. #include <exec/semaphores.h>
  6. #include <dos/dosextens.h>
  7. #include <intuition/sghooks.h>
  8. #include <libraries/commodities.h>
  9. #include <libraries/locale.h>
  10.  
  11. #include <clib/alib_protos.h>
  12. #include <clib/commodities_protos.h>
  13. #include <clib/dos_protos.h>
  14. #include <clib/exec_protos.h>
  15. #include <clib/intuition_protos.h>
  16. #include <clib/locale_protos.h>
  17.  
  18. #include<pragmas/commodities_pragmas.h>
  19. #include<pragmas/dos_pragmas.h>
  20. #include<pragmas/exec_pragmas.h>
  21. #include<pragmas/intuition_pragmas.h>
  22. #include<pragmas/locale_pragmas.h>
  23.  
  24. #include "NewEdit.h"
  25.  
  26. #define CATCOMP_NUMBERS
  27. #define CATCOMP_BLOCK
  28. #define CATCOMP_CODE
  29. #include "Catalog.h"
  30.  
  31. /* Disable SAS/C control c handling */
  32. int _CXBRK   (void)  { return 0; }
  33. int __chkabort(void) { return 0; }
  34.  
  35. void disable_hook( void );
  36. void process_messages( struct MsgPort *port );
  37.  
  38. static char verstag[] = VERSTAG;
  39. char Version[] = VERS" ("DATE") ";
  40.  
  41. extern ULONG __saveds __asm  string_hook( register __a0 struct Hook   *hook,
  42.                                           register __a2 struct SGWork *sgw,
  43.                                           register __a1 unsigned long *msg  );
  44. extern BOOL init_iffparse( void );
  45. extern void close_iffparse( void );
  46.  
  47. #define TITLESTRING VERS" by Uwe Röhm"
  48.  
  49. extern struct ExecBase *SysBase, *DOSBase;
  50. struct Library *IntuitionBase = NULL, *UtilityBase = NULL, *CxBase = NULL;
  51.  
  52. BOOL Disabled;
  53. CxObj *Broker = NULL;
  54. struct Hook Edh;
  55. struct Hook *OldHook;
  56. struct SignalSemaphore *Sem;
  57. struct NewBroker MyBroker =
  58. {
  59.     NB_VERSION, "NewEdit", TITLESTRING,
  60.     0, NBU_NOTIFY | NBU_UNIQUE,
  61.     0, 0, 0L, 0
  62. };
  63.  
  64. struct LocaleInfo LI;
  65. #define LocaleBase LI.li_LocaleBase
  66. #define catalog LI.li_Catalog
  67.  
  68. LONG Max, Num, Pos, Len;
  69.  
  70. void __stdargs __main( char *stream )
  71. {
  72.     LONG error;
  73.     int retcode;
  74.     struct Task *mytask;
  75.     struct TagItem cattags[] =
  76.     {
  77.         OC_BuiltInLanguage, (ULONG)"english",
  78.         TAG_DONE
  79.     };
  80.  
  81.     if ( SysBase->LibNode.lib_Version > 36 )
  82.     {
  83.         SetProgramName( "NewEdit" );
  84.  
  85.         if ( LocaleBase = OpenLibrary( "locale.library", 38 ) )
  86.             catalog = OpenCatalogA( NULL, "NewEdit.catalog", cattags );
  87.  
  88.         if ( IntuitionBase = OpenLibrary( "intuition.library", 37 ) )
  89.         {
  90.             if ( UtilityBase = OpenLibrary( "utility.library", 37 ) )
  91.             {
  92.                 if ( CxBase = OpenLibrary( "commodities.library", 37 ) )
  93.                 {
  94.                     mytask = FindTask( NULL );
  95.  
  96.                     MyBroker.nb_Port = &( ( struct Process * )mytask )->pr_MsgPort;
  97.                     MyBroker.nb_Descr = GetString( &LI, MSG_CX_DESCRIPTION );
  98.  
  99.                     if ( Broker = CxBroker( &MyBroker, &error ) )
  100.                     {
  101.                         if ( Sem = (struct SignalSemaphore *)AllocMem( sizeof( struct SignalSemaphore ), MEMF_CLEAR ) )
  102.                         {
  103.                             Sem->ss_Link.ln_Pri  = 0;
  104.                             Sem->ss_Link.ln_Name = "NewEdit.Semaphore";
  105.                             AddSemaphore( Sem );
  106.  
  107.                             Edh.h_Entry = ( ULONG (*)() )string_hook;
  108.  
  109.                             /* Initialize clipboard unit 0 */
  110.                             if ( init_iffparse() )
  111.                             {
  112.                                 Disabled = FALSE;
  113.                                 OldHook = SetEditHook( &Edh );
  114.                                 PutStr( GetString( &LI, MSG_HAILSTRING ) );
  115.                                 ActivateCxObj( Broker, 1l );
  116.  
  117.                                 process_messages( MyBroker.nb_Port );
  118.  
  119.                                 disable_hook();
  120.                                 close_iffparse();
  121.                             }
  122.                             else
  123.                             {
  124.                                 PutStr( GetString( &LI, MSG_ERROR_NOCLIPBOARD ) );
  125.                                 retcode = RETURN_FAIL;
  126.                             }
  127.  
  128.                             RemSemaphore( Sem );
  129.                             FreeMem( Sem, sizeof( struct SignalSemaphore ) );
  130.                             PutStr( GetString( &LI, MSG_BYEBYE ) );
  131.                         }
  132.                         else
  133.                         {
  134.                             Permit(); // Why ???????
  135.                             PutStr( GetString ( &LI, MSG_ERROR_MEMORY ) );
  136.                             retcode = RETURN_FAIL;
  137.                         }
  138.  
  139.                         DeleteCxObj ( Broker );
  140.  
  141.                     }
  142.                     else retcode = RETURN_WARN;
  143.  
  144.                     CloseLibrary( CxBase );
  145.                 }
  146.                 CloseLibrary( UtilityBase );
  147.             }
  148.             CloseLibrary( IntuitionBase );
  149.         }
  150.  
  151.         if ( LocaleBase )
  152.         {
  153.             CloseCatalog( catalog );
  154.             CloseLibrary( LocaleBase );
  155.         }
  156.     }
  157.    __exit( retcode );
  158. }
  159.  
  160.  
  161. void disable_hook()
  162. {
  163.     ObtainSemaphore( Sem );
  164.  
  165.     Disable();
  166.  
  167.     SetEditHook( OldHook );
  168.  
  169.     Enable();
  170.  
  171.     ReleaseSemaphore( Sem );
  172. }
  173.  
  174. void process_messages ( struct MsgPort *port )
  175. {
  176.     short sigs, tasksig;
  177.     CxMsg *cxm;
  178.  
  179.     tasksig = 1L << port->mp_SigBit;
  180.  
  181.     do
  182.     {
  183.         sigs = Wait( tasksig | SIGBREAKF_CTRL_C | SIGBREAKF_CTRL_F );
  184.  
  185.         if ( sigs & tasksig )
  186.         {
  187.             while ( cxm = ( CxMsg * )GetMsg( MyBroker.nb_Port ) )
  188.             {
  189.                 if ( CxMsgType( cxm ) == CXCMD_UNIQUE ) sigs |= SIGBREAKF_CTRL_C;
  190.                 else
  191.                 switch ( CxMsgID( cxm ) )
  192.                 {
  193.                     case CXCMD_DISABLE:
  194.                         Disabled = TRUE;
  195.                         ActivateCxObj( Broker, 0l );
  196.                     break;
  197.  
  198.                     case CXCMD_ENABLE:
  199.                         Disabled = FALSE;
  200.                         ActivateCxObj( Broker, 1l );
  201.                     break;
  202.  
  203.                     case CXCMD_UNIQUE:
  204.                     case CXCMD_KILL:
  205.                         sigs |= SIGBREAKF_CTRL_C;
  206.                     break;
  207.  
  208.                     default:
  209.                     break;
  210.                 }
  211.  
  212.                 ReplyMsg( (struct Message*)cxm );
  213.  
  214.             }
  215.         }
  216.     } while ( !( sigs & ( SIGBREAKF_CTRL_C | SIGBREAKF_CTRL_F ) ) );
  217. }
  218.