home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 3 / CDPDIII.bin / pd / commodities / newedit / newedit.c < prev    next >
C/C++ Source or Header  |  1993-03-06  |  11KB  |  344 lines

  1. /*----------------------------------------------------------------------------
  2.    File   :    NewEdit.c
  3.    Projekt:    NewEdit
  4.    Inhalt :    __main()
  5.                disable_hook()
  6.                process_messages()
  7.  
  8.    Version:    1.8
  9.    Datum  :    06. März 1993
  10.  
  11.    Autor  :    Uwe Röhm
  12.                Original aus AmigaPlus 8/92: Oliver Wagner
  13.    Adresse:    Wörthstr. 18    W-8390 Passau
  14.    Bemerkung:
  15.    Globaler Edit-Hook mit folgenden Optionen:
  16.         ALT CURSOR LEFT     Cursor auf Anfang des vorherigen Wortes
  17.         ALT CURSOR RIGHT    Cursor auf Anfang des nächsten Wortes
  18.         ALT BACKSPACE       vorheriges Wort löschen
  19.         ALT DEL             nächstes Wort löschen
  20.         RALT CURSOR UP/DOWN zum nächsten/vorherigen Stringgadget springen
  21.         ESC                 Gadget verlassen (mit GADGETUP)
  22.         RCommand C          Gadgetinhalt in das Clipboard kopieren
  23.         RCommand V          Inhalt des Clipboards (unit 0) ins Gadget einfügen
  24.    Seit Version 1.6 nun als Commodity, das an- und ausgeschaltet werden kann.
  25. ----------------------------------------------------------------------------*/
  26. #include "NewEdit.h"
  27. #include <libraries/locale.h>
  28. #include <libraries/commodities.h>
  29. #include <intuition/sghooks.h>
  30. #include <dos/dosextens.h>
  31. #include <exec/semaphores.h>
  32. #include <exec/execbase.h>
  33. #include <exec/memory.h>
  34.  
  35. /* ------------------------  Prototypes & Pragmas  ------------------------ */
  36. #include <clib/commodities_protos.h>
  37. #include <clib/intuition_protos.h>
  38. #include <clib/locale_protos.h>
  39. #include <clib/exec_protos.h>
  40. #include <clib/dos_protos.h>
  41. #include <clib/alib_protos.h>
  42.  
  43. #include <stdlib.h>
  44.  
  45. #ifdef LATTICE
  46.    #include<pragmas/commodities_pragmas.h>
  47.    #include<pragmas/intuition_pragmas.h>
  48.    #include<pragmas/locale_pragmas.h>
  49.    #include<pragmas/exec_pragmas.h>
  50.    #include<pragmas/dos_pragmas.h>
  51.    int _CXBRK   (void)  { return 0; } /* Lattice CTRL-C Handling */
  52.    int __chkabort(void) { return 0; } /* vollstaendig abschalten */
  53. #endif
  54.  
  55. #define CATCOMP_NUMBERS
  56. #define CATCOMP_BLOCK
  57. #define CATCOMP_CODE
  58. #include "Catalog.h"
  59.  
  60. void disable_hook     ( void );
  61. void process_messages ( struct MsgPort *port );
  62.  
  63. /* -------------------------  externe Funktionen  ------------------------- */
  64. extern ULONG __saveds __asm  string_hook( register __a0 struct Hook   *hook,
  65.                                           register __a2 struct SGWork *sgw,
  66.                                           register __a1 unsigned long *msg  );
  67. extern BOOL init_iffparse ( void );
  68. extern void close_iffparse ( void );
  69.  
  70. /* ----------------------------  Textdefines  ----------------------------- */
  71. #define BASENAME      "NewEdit"
  72. #define SEMAPHORENAME "NewEdit.Semaphore"
  73. #define CATALOG_NAME  "NewEdit.catalog"
  74. #define TITLESTRING   VERS" by Uwe Röhm"
  75. #define INTUITIONNAME "intuition.library"
  76. #define UTILITYNAME   "utility.library"
  77. #define CXNAME        "commodities.library"
  78. #define KICKSTART     "NewEdit requires at least Kickstart V37!\n"
  79.  
  80. /* ----------------------------  globale Vars  ---------------------------- */
  81. extern struct ExecBase *SysBase;
  82. extern struct Library  *DOSBase;
  83. struct Library         *IntuitionBase= NULL;
  84. struct Library         *UtilityBase  = NULL;
  85. struct Library         *CxBase       = NULL;
  86. struct Hook             Edh;
  87. struct Hook            *OldHook;
  88. struct SignalSemaphore *Sem;
  89. struct NewBroker        MyBroker = { NB_VERSION, BASENAME, TITLESTRING, 0, NBU_NOTIFY | NBU_UNIQUE, 0, 0, 0l, 0 };
  90. BOOL   Disabled;
  91.  
  92. CxObj *Broker = NULL;
  93.  
  94. struct LocaleInfo    LI;
  95. #define LocaleBase   LI.li_LocaleBase
  96. #define catalog      LI.li_Catalog
  97.  
  98.  
  99. LONG Max, Num, Pos, Len;
  100.  
  101. /*  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --
  102.    Funktion  :  __main
  103.    Parameter :  ---
  104.    Rückgabe  :  int
  105.  
  106.    Aufruf von:  ---
  107.    UnterFunks:  init_iffparse()
  108.                 close_iffparse()
  109.                 disable_hook()
  110.                 process_messages()
  111.    Autor     :  Uwe Röhm
  112.    Datum     :  06. März 1993
  113.    Bemerkung :  Hauptprogramm zu NewEdit.
  114.                  - Test auf Kickstart >= 37
  115.                  - Libraries öffnen/schliessen
  116.                  - Semaphore einrichten/löschen
  117.                  - eigene Stringhook setzen/entfernen
  118.                  - Abbruch mit CTRL-C
  119. --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  */
  120. void __stdargs __main( char *stream )
  121. {
  122.    LONG error;
  123.    int  retcode;
  124.    struct Task *mytask;
  125.    struct TagItem  cattags[] = {
  126.       OC_BuiltInLanguage, (ULONG) "deutsch",
  127.       TAG_DONE
  128.    };
  129.  
  130.  
  131.    if ( SysBase->LibNode.lib_Version > 36 )
  132.    {
  133.       /*
  134.        *  Programmnamen ändern (Hilfreich fuer "Status")
  135.        */
  136.       SetProgramName( BASENAME );
  137.  
  138.       /*
  139.        *    Die Locale Library öffnen
  140.        */
  141.       if (LocaleBase = OpenLibrary("locale.library",38))
  142.          catalog = OpenCatalogA( NULL, CATALOG_NAME, cattags );
  143.  
  144.       if ( IntuitionBase = OpenLibrary (INTUITIONNAME, 37) )
  145.       {
  146.          if ( UtilityBase = OpenLibrary (UTILITYNAME, 37) )
  147.          {
  148.             if ( CxBase = OpenLibrary (CXNAME, 37) )
  149.             {
  150.  
  151.                /*
  152.                 * läuft NewEdit schon? (Da MBU_UNIQUE gesetzt ist, kann
  153.                 * ein zweites CXObj Namens NewEdit nicht angelegt werden)
  154.                 */
  155.                Forbid();
  156.                   mytask = FindTask( NULL );
  157.                Permit();
  158.                MyBroker.nb_Port = &((struct Process *)mytask)->pr_MsgPort;
  159.                MyBroker.nb_Descr= GetString( &LI, MSG_CX_DESCRIPTION );
  160.                if ( Broker = CxBroker( &MyBroker, &error ) )
  161.                {
  162.                   /*
  163.                    * Semaphore anlegen als Sicherung vor zu frühem Entfernen der
  164.                    * StringHook
  165.                    */
  166.                   if ( Sem = (struct SignalSemaphore *) AllocMem ( sizeof( struct SignalSemaphore ), MEMF_CLEAR ) )
  167.                   {
  168.                      Sem->ss_Link.ln_Pri  = 0;
  169.                      Sem->ss_Link.ln_Name = SEMAPHORENAME;
  170.                      AddSemaphore( Sem );
  171.  
  172.                      Edh.h_Entry = (ULONG (*)()) string_hook;
  173.  
  174.                      /*
  175.                       * Hook setzen (vorher die iif.library öffnen und auf das
  176.                       * Clipboard Unit 0 initialisieren)
  177.                       */
  178.                      if ( init_iffparse() )
  179.                      {
  180.                         Disabled = FALSE;
  181.                         OldHook = SetEditHook( &Edh );
  182.                         PutStr( GetString( &LI, MSG_HAILSTRING ) );
  183.                         ActivateCxObj( Broker, 1l );
  184.  
  185.                         process_messages( MyBroker.nb_Port );
  186.  
  187.                         disable_hook();
  188.                         close_iffparse();
  189.  
  190.                      } /* if init_iff_parse() */
  191.                      else
  192.                         retcode = RETURN_FAIL;
  193.  
  194.                      RemSemaphore( Sem );
  195.                      FreeMem( Sem, sizeof( struct SignalSemaphore ) );
  196.                      PutStr( GetString( &LI, MSG_BYEBYE ) );
  197.  
  198.                   } /* if AllocMem() */
  199.                   else
  200.                   {
  201.                      Permit();
  202.                      PutStr( GetString ( &LI, MSG_ERROR_MEMORY ) );
  203.                      retcode = RETURN_FAIL;
  204.  
  205.                   } /* if !AllocMem() */
  206.  
  207.                   DeleteCxObj ( Broker );
  208.  
  209.                } /* if CxBroker() */
  210.                else
  211.                   retcode = RETURN_WARN;
  212.  
  213.                CloseLibrary( CxBase );
  214.  
  215.             } /* if OpenLibrary( "commodities.library"...) */
  216.             else
  217.             {
  218.                retcode = RETURN_FAIL;
  219.                Printf ( GetString ( &LI, MSG_ERROR_LIBRARY ), CXNAME, 37 );
  220.  
  221.             } /* if !OpenLibrary() */
  222.  
  223.             CloseLibrary( UtilityBase );
  224.  
  225.          } /* if OpenLibrary( "utility.library"...) */
  226.          else
  227.          {
  228.             retcode = RETURN_FAIL;
  229.             Printf ( GetString ( &LI, MSG_ERROR_LIBRARY ), UTILITYNAME, 37 );
  230.  
  231.          } /* if !OpenLibrary() */
  232.  
  233.          CloseLibrary( IntuitionBase );
  234.  
  235.       } /* if OpenLibrary( "intuition.library"...) */
  236.       else
  237.       {
  238.          retcode = RETURN_FAIL;
  239.          Printf ( GetString ( &LI, MSG_ERROR_LIBRARY ), INTUITIONNAME, 37 );
  240.  
  241.       } /* if !OpenLibrary() */
  242.  
  243.  
  244.       if (LocaleBase)
  245.       {
  246.           CloseCatalog(catalog);
  247.           CloseLibrary(LocaleBase);
  248.  
  249.       } /* if LocaleBase */
  250.  
  251.  
  252.    } /* if lib_Version > 36 */
  253.    else
  254.    {
  255.       retcode = RETURN_FAIL;
  256.       Write ( Output(), KICKSTART, sizeof(KICKSTART) );
  257.  
  258.    } /* if lib_Version <= 36 */
  259.  
  260.    __exit( retcode );
  261. }
  262.  
  263.  
  264. /*  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --
  265.    Funktion  :  disable_hook
  266.    Parameter :  ---
  267.    Rückgabe  :  ---
  268.  
  269.    Aufruf von:  ---
  270.    UnterFunks:  ---
  271.    Autor     :  Uwe Röhm
  272.    Datum     :  15. September 1992
  273.    Bemerkung :  Sicherung, damit nicht gerade eine String-Hook aktiv ist,
  274.                 wenn hier das Programm verlassen wird: Einen exklusiven
  275.                 Lock auf die Semaphore nehmen.
  276. --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  */
  277. void disable_hook()
  278. {
  279.    ObtainSemaphore ( Sem );
  280.    Disable();
  281.       SetEditHook( OldHook );
  282.    Enable();
  283.    ReleaseSemaphore ( Sem );
  284. }
  285.  
  286. /*  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --
  287.    Funktion  :  process_messages
  288.    Parameter :  struct MsgPort *port (Port des eigenen Prozesses)
  289.    Rückgabe  :  ---
  290.  
  291.    Aufruf von:  ---
  292.    UnterFunks:  disable_hook (s.o.)
  293.    Autor     :  Uwe Röhm
  294.    Datum     :  15. September 1992
  295.    Bemerkung :  Den Port nach CxMessages abfragen und darauf reagieren
  296.                 (nur ENABLE, DISABLE und KILL unterstützt)
  297.                 CTRL-C und CTRL-F beenden NewEdit
  298. --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  */
  299. void process_messages ( struct MsgPort *port )
  300. {
  301.    short sigs,
  302.          tasksig;
  303.    CxMsg *cxm;
  304.  
  305.    tasksig = (1 << port->mp_SigBit);
  306.    do
  307.    {
  308.       sigs = Wait( tasksig | SIGBREAKF_CTRL_C | SIGBREAKF_CTRL_F );
  309.       if ( sigs & tasksig )
  310.       {
  311.          while ( cxm = (CxMsg *) GetMsg(MyBroker.nb_Port) )
  312.          {
  313.             if ( CxMsgType(cxm) == CXCMD_UNIQUE )
  314.                sigs |= SIGBREAKF_CTRL_C;
  315.             else
  316.                switch ( CxMsgID(cxm) )
  317.                {
  318.                   case CXCMD_DISABLE:
  319.                      Disabled = TRUE;
  320.                      ActivateCxObj( Broker, 0l );
  321.                      break;
  322.                   case CXCMD_ENABLE:
  323.                      Disabled = FALSE;
  324.                      ActivateCxObj( Broker, 1l );
  325.                      break;
  326.                   case CXCMD_UNIQUE:
  327.                   case CXCMD_KILL:
  328.                      sigs |= SIGBREAKF_CTRL_C;
  329.                      break;
  330.                   default:
  331.                      break;
  332.  
  333.                } /* switch ID */
  334.  
  335.             ReplyMsg( (struct Message*) cxm );
  336.  
  337.          } /* while cxm */
  338.  
  339.       } /* if tasksig */
  340.  
  341.    } while (!(sigs & (SIGBREAKF_CTRL_C | SIGBREAKF_CTRL_F)) );
  342. }
  343.  
  344.