home *** CD-ROM | disk | FTP | other *** search
/ Aminet 10 / aminetcdnumber101996.iso / Aminet / util / misc / fifolib38_1.lha / handler.h < prev    next >
C/C++ Source or Header  |  1995-12-14  |  6KB  |  214 lines

  1.  
  2. /*
  3.  *  HANDLER.H
  4.  */
  5.  
  6. #include <exec/types.h>
  7. #include <exec/nodes.h>
  8. #include <exec/ports.h>
  9. #include <exec/interrupts.h>
  10. #include <exec/memory.h>
  11. #include <exec/alerts.h>
  12. #include <exec/libraries.h>
  13. #include <dos/dos.h>
  14. #include <dos/dosextens.h>
  15. #include <dos/filehandler.h>
  16. #include <devices/timer.h>
  17. #include <libraries/fifo.h>
  18. #if defined(_DCC) /*  at least the very old DICE 2.06 */
  19. #include <clib/dos_protos.h>
  20. #include <clib/exec_protos.h>
  21. #include <clib/alib_protos.h>
  22. #else /*  proto/ will include inline/ if optimization is on in GCC */
  23. #include <proto/dos.h>
  24. #include <proto/exec.h>
  25. #include <proto/alib.h>
  26. #endif
  27. #include <proto/fifo.h>
  28.  
  29. #ifdef DEBUG
  30. #include <stdio.h>        /*  vsprintf() */
  31. #include <stdarg.h>        /*  for xprintf() */
  32. #endif
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #ifdef _DCC
  36. /*#include <lib/requestfh.h>*/    /*  what was this? */
  37. #endif
  38.  
  39. #if defined(__GNUC__)
  40. #if 0
  41. #include <inline/strsup.h>    /*  inline strlen() etc. */
  42. #else
  43. /*  We can make GCC generate nice code! */
  44. extern __inline__ int strlen(const char *string)
  45. {
  46.     const char *s=string;
  47.     while(*s++) ;        /*  tstb a0@+; bne 0b */
  48.     return ~(string-s);
  49. }
  50.  
  51. /*  Do not use this inline for strcpy(foo, "...") which is optimized away */
  52. extern __inline__ char *strcpy(char *d, const char *s)
  53. {
  54.     char *d2 = d;
  55.     /*
  56.      *  GCC would generate "0: moveb a@+,d0; move d0,a@+, jne 0b" instead
  57.      */
  58.     __asm__ volatile ("0: moveb %0@+,%1@+; jne 0b"
  59.     : "=a" (s), "=a" (d) : "0" (s), "1" (d) : "cc", "memory");
  60.     return d2;
  61. }
  62.  
  63. /*
  64.  *  GCC-1.x, 2.3.3, 2.5.8, 2.6.3, 2.7.0 uses dbra
  65.  *  in such while((short|long)(--n) != -1) loops
  66.  *  Other loops forms may not lead to the same optimization with all GCCs
  67.  *  short count generates single dbra, long count dbra in the inner loop
  68.  */
  69. extern __inline__ char *strscpy(char *d, const char *s, short n)
  70. {
  71.     char *d2 = d;
  72.     if (n) {
  73.     --n;
  74.     /*  0: moveb a0@+,d0; (grr) moveb d0,a0@+; beq 1f; dbra d0,0b; 1: */
  75.     while ((*d++=*s++) && ((short)(--n) != -1)) ;
  76.     }
  77.     return d2;
  78. }
  79. /*  short is enough for fifo-handler */
  80. #define strncpy strscpy
  81.  
  82. #if 1
  83. #define memscpy(d,s,scount)  \
  84. ({  void *__memscpy_d = (d); \
  85.     void *__memscpy_s = (s); \
  86.     short __memscpy_scount = (scount); \
  87.     void *__memscpy_res = __memscpy_d; \
  88.     if (!(__memscpy_scount==0)) { \
  89.     --__memscpy_scount; \
  90.     do { *(char*)__memscpy_d++ = *(char*)__memscpy_s++; } \
  91.     while ((short)(--__memscpy_scount)!=-1); \
  92.     } \
  93.     __memscpy_res; \
  94. })
  95. #else
  96. extern __inline__ void *memscpy(void *d, void *s, short scount)
  97. {
  98.     void *d2 = d;
  99.     if (scount) {
  100.     --scount;
  101.     do { *(char *)d++ = *(char *)s++; }
  102.     while ((short)(--scount)!=-1);
  103.     }
  104.     return d2;
  105. }
  106. #endif
  107. /*
  108.  *  We know this is safe because: 1st count is small,
  109.  *  2nd this memscpy() can still copy overlapping memory to
  110.  *  lower addresses and this is exactly what fifo-handler needs
  111.  */
  112. #define memmove(d,s,count)  memscpy(d,s,count)
  113.  
  114. /*
  115.  *  Alternative:
  116.  *  defines, not inline functions, work even without optimization
  117.  *  work even better than inline function with optimization on
  118.  */
  119. /*  BUG: wrong sign for chars > 127 */
  120. #define streql(s1,s2)  \
  121. ({  char *__streql_s1 = (s1), *__streql_s2 = (s2); \
  122.     char __streql_c1, __streql_c2; \
  123.     while (!(__streql_c1=*__streql_s1++ \
  124.          -(__streql_c2=*__streql_s2++)) \
  125.        && __streql_c2) ; \
  126.     __streql_c1; \
  127. })
  128. /*  the sign bug is not important, only 0 counts in fifo-handler */
  129. #define strcmp(s1,s2)  streql(s1,s2)
  130. #endif
  131. #endif
  132.  
  133. #ifndef memmove
  134. #define memmove(to,from,count)  bcopy(from,to,count)
  135. #endif
  136.  
  137.  
  138. #define Prototype extern
  139.  
  140. #define DOS_TRUE    -1
  141. #define DOS_FALSE   0
  142.  
  143. #define CB_SIZE     1024    /*  lines buffer */
  144. #define FIFO_SIZE   2048
  145.  
  146. #define BTOC(bptr)  ((void *)((unsigned long)(bptr) << 2))
  147. #define CTOB(cptr)  ((BPTR)(((unsigned long)cptr) >> 2))
  148.  
  149. typedef struct DosPacket    DosPacket;
  150. typedef struct FileHandle   FileHandle;
  151. typedef struct DeviceNode   DeviceNode;
  152. typedef struct Process        Process;
  153. typedef struct MinNode        Node;
  154. typedef struct MinList        List;
  155. typedef struct Node        MaxNode;
  156. typedef struct List        MaxList;
  157. typedef struct MsgPort        MsgPort;
  158. typedef struct Message        Message;
  159. typedef struct Interrupt    Interrupt;
  160. typedef struct DosList        DosList;
  161. typedef struct RootNode     RootNode;
  162. typedef struct DosInfo        DosInfo;
  163.  
  164. typedef struct SharRead {
  165.     void    *sr_FifoR;
  166.     long    sr_Refs;
  167. } SharRead;
  168.  
  169. typedef struct FHan {
  170.     MaxNode    ff_Node;    /*    fifo node, ln_name holds fifo name */
  171.     short    ff_Flags;
  172.     short    ff_Refs;    /*    open refs   */
  173.     SharRead    *ff_SRead;  /*    fifo handle for reading */
  174.     void    *ff_FifoW;  /*    fifo handle for writing */
  175.     long    ff_FHBufSiz;/*    FifoW half buffer size    */
  176.     Message    ff_RdMsg;
  177.     Message    ff_WrMsg;
  178.     MsgPort    *ff_Port;   /*    unique port for messages    */
  179.     List    ff_RdWait;
  180.     List    ff_WrWait;
  181.     char    *ff_CookBuf;/*    cooked buffer handling        */
  182.     short    ff_CookIdx;
  183.     short    ff_LRet;
  184.     MsgPort    *ff_SigPort;/*    message port instead of task to signal */
  185. } FHan;
  186.  
  187. #define ff_FifoR    ff_SRead->sr_FifoR
  188.  
  189. #define FHF_COOKED    0x0001
  190. #define FHF_RPEND    0x0002
  191. #define FHF_WAVAIL    0x0004
  192. #define FHF_READ    0x0008
  193. #define FHF_WRITE    0x0010
  194. #define FHF_CLOSEEOF    0x0020
  195. #define FHF_MASTER    0x0040
  196. #define FHF_TEE     0x0080
  197. #define FHF_SHELL    0x0100
  198. #define FHF_COOKBFUL    0x0200        /*    cooked buffer is full    */
  199. #define FHF_COOKECHOBLK 0x0400        /*    blocked echoing chars!    */
  200. #define FHF_REOF    0x0800        /*    REMOTE EOF        */
  201. #define FHF_WIHOLD    0x1000        /*    write hold due to input */
  202. #define FHF_COOKCRLF    0x2000        /*    need to write CRLF    */
  203. #define FHF_RREQUIRED    0x4000
  204.  
  205. typedef struct WaitTreq {
  206.     struct timerequest    wt_timereq;
  207.     DosPacket        *wt_packet;
  208. } WaitTreq;
  209.  
  210. /* Were Protoize used
  211. #include "fifohan-protos.h"
  212. */
  213.  
  214.