home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 272.lha / xprquickb / xprquickb.c < prev    next >
C/C++ Source or Header  |  1989-08-07  |  9KB  |  353 lines

  1. /*
  2.  * xprQuickB.library
  3.  * XPR implementation of CompuServe Quick-B protocol, written by Marc Boucher.
  4.  
  5. Files in this release:
  6.  
  7.  aztec.h    - includes usual aztec stuff, function decls etc..
  8.  protolib.c    - An exec library Based on "elib", an example library,
  9.  protolib.h    - created by Jim Mackraz using mylib.asm by Neil Katin.
  10.  timeout.c    - timer.device routines for a simple dos Delay() style function
  11.  protocol.c    - quick-b routines
  12.  xprquickb.c    - main module
  13.  Makefile
  14.  libface.asm    - other library code, from examples by J.Mackraz etc.. Adapted for XPR by Willy Langeveld
  15.  xprquickb.library - your compiled library
  16.  funky.asm    - initial Manx C .begin stuff, (C) by Manx but modified by J.Mackraz
  17.  xproto.h    - include file of most xpr structures by XPR author.
  18.  rtag.asm    - RomTag structure for an exec lib, written by J. Mackraz
  19.  
  20.     Placed in the public domain, no copyrights apply on this stuff; but
  21. please  do  not  claim  credit  for  code that isn't yours.  Distribute the
  22. complete package, PLEASE!  not just the .library file..
  23.  
  24.     Please   note   however   that  "protocol.c"  is  based  on  freely
  25. distributable  source  code  placed  on CompuServe.  Some copyrights and/or
  26. restrictions  on  these  may  be  in  effect  -  Read  the notice on top of
  27. "protocol.c".
  28.  
  29.     I  am  not  yet  on  the  CompuServe  network.  This was written to
  30. satisfy  interest in XPR and to finish a previous attempt which was written
  31. for a friend.
  32.  
  33.      Thanks  to  all who contributed to the XPR definition and who have
  34. written  software  to  support  it..   Willy  Langeveld,  Marco  Papa, Rick
  35. Huebner,  and  others  that  I  forget,  don't know, or that will do in the
  36. future.
  37.  
  38. --
  39. Send bug reports, suggestions, ting ting jahe candy to:
  40.  
  41. -Marc Boucher------------------------------------------Phone: 514/466+8932-
  42.   AmiNet    postmaster@marcami        | Bitnet    UUQRL0 at POLYTECA
  43.   UUCP      ...!killer!jolnet!boucher | Internet  boucher@jolnet.Orpk.IL.US
  44.        CompuServe (EasyPlex): ">INTERNET:boucher@jolnet.Orpk.IL.US"
  45.  
  46.  
  47. Money, flames, trouble, to Gunnar Nordmark's NULL: device.
  48.  
  49.  *
  50.  *
  51.  */
  52.  
  53. #include <exec/types.h>
  54.  
  55. #include "aztec.h"
  56. #include "xproto.h"
  57.  
  58. /*#define DEBUG*/
  59.  
  60. #define    DLE        16
  61. #define    ENQ        05
  62. #define    ESC        27
  63.  
  64. void DosEntry()    /* in case someone runs us from AmigaDOS! */
  65. {
  66. }
  67.  
  68. char  XPRname[]   = "xprquickb.library";
  69. char  XPRid[]     = "xprQuickB library 0.9 (Jul 89)\r\n";
  70. UWORD XPRrevision = 1;
  71.  
  72. extern long calla(), callaa(), callad(), calladd(), calladda(), calld();
  73. extern long bp_DLE();
  74.  
  75. long (*xupdate)(), (*xswrite)(), (*xfopen)(), (*xfclose)(), \
  76.     (*xfread)(), (*xsread)(),  (*xchkabort)(), (*xfwrite)(), \
  77.     (*xchkmisc)(), (*xsetserial)(),(*xsflush)();
  78.  
  79. long ctimeouts , cbytes, cnaks, cblocks, gbufsize;    /* counts for status */
  80.  
  81. short cgetc(timeout)
  82. long timeout;
  83. {
  84.     short result;
  85.     UBYTE dat[2];
  86.  
  87.     if(calladd(xsread, dat, 1L, ((long)timeout)*60000L)==-1L) {
  88.         result=-1;
  89.     }
  90.     else {
  91.         result=dat[0];
  92.     } 
  93.     return(result);
  94. }
  95.  
  96. void
  97. cputc(bytetowrite)
  98. char bytetowrite;
  99. {
  100.     UBYTE dat[2];
  101.  
  102.     dat[0]=bytetowrite;
  103.     callad(xswrite, dat, 1L);
  104. }
  105.  
  106. static struct XPR_UPDATE xpru;
  107.  
  108. long XPR_QuickB(IO)
  109. struct XPR_IO *IO;
  110. {
  111.     static long i, NewStatus, OldSerStatus, finished, active;
  112.  
  113.     if ((xupdate=IO->xpr_update) == NULL) return(0L);
  114.     if ((xswrite=IO->xpr_swrite) == NULL) {
  115.         Error("No xpr_swrite() function available!");
  116.         return(0L);
  117.     }
  118.     if ((xfopen=IO->xpr_fopen) == NULL) {
  119.         Error("No xpr_fopen() function!");
  120.         return(0L);
  121.     }
  122.     if ((xfclose=IO->xpr_fclose) == NULL) {
  123.         Error("No xpr_fclose() function!");
  124.         return(0L);
  125.     }
  126.     if ((xfread=IO->xpr_fread) == NULL) {
  127.         Error("No xpr_fread() function!");
  128.         return(0L);
  129.     }
  130.     if ((xfwrite=IO->xpr_fwrite) == NULL) {
  131.         Error("No xpr_fwrite() function!");
  132.         return(0L);
  133.     }
  134.     if ((xsread=IO->xpr_sread) == NULL) {
  135.         Error("No xpr_sread() function!");
  136.         return(0L);
  137.     }
  138.     xsflush=IO->xpr_sflush;
  139.     xchkabort=IO->xpr_chkabort;
  140.     xsetserial=IO->xpr_setserial;
  141.     xchkmisc=IO->xpr_chkmisc;
  142.  
  143.     finished=FALSE;
  144.     active=FALSE;
  145.  
  146.     if(xsetserial) {
  147.         OldSerStatus = calld(xsetserial,-1L);
  148.         NewStatus = OldSerStatus & 0xFFFFE0BC;
  149.         if (NewStatus != OldSerStatus) calld(xsetserial,NewStatus);
  150.      }
  151.  
  152.     xpru.xpru_updatemask = XPRU_PROTOCOL;
  153.     xpru.xpru_protocol = "xprQuickB library 0.9 (Jul 89) by Marc Boucher";
  154.     calla(xupdate, &xpru);
  155.     TimeOut(50L);
  156.  
  157.     for (i = 0; i < (long)strlen(IO->xpr_filename); i++)    /* send filename */
  158.         cputc(*(IO->xpr_filename + i));
  159.     cputc(0x0d);    /* carriage return */
  160.  
  161.     do {
  162.         switch(cgetc(100L)) {
  163.             case DLE: if (cgetc (10L) != 'B') break;
  164.                 active=TRUE;
  165.                 finished=bp_DLE();
  166.                 break;
  167.             case ENQ: bp_ENQ();
  168.                 break;
  169.         }
  170.         if(xchkmisc) xchkmisc();
  171.         if(xchkabort) if((xchkabort())&&(!active)) finished=TRUE;
  172.     } while (!finished);
  173.     return(TRUE);
  174. }
  175.  
  176. long user_abort()
  177. {
  178.     if(xchkmisc) xchkmisc();
  179.  
  180.     if(xchkabort) return(xchkabort());
  181.     else return(FALSE);
  182. }
  183.  
  184. long XProtocolSend(IO)
  185. struct XPR_IO *IO;
  186. {
  187.     return(XPR_QuickB(IO));
  188. }
  189.  
  190. long XProtocolReceive(IO)
  191. struct XPR_IO *IO;
  192. {
  193.     return(XPR_QuickB(IO));
  194. }
  195.  
  196. long XProtocolSetup(IO)
  197. struct XPR_IO *IO;
  198. {
  199.     return(TRUE);
  200. }
  201.  
  202. long XProtocolCleanup(IO)
  203. struct XPR_IO *IO;
  204. {
  205.     return(TRUE);
  206. }
  207.  
  208. Status(text)
  209. char *text;
  210. {
  211.     xpru.xpru_updatemask = XPRU_MSG;
  212.     xpru.xpru_msg   = text;
  213.     calla(xupdate, &xpru);
  214. }
  215.  
  216. OtherStats()
  217. {
  218.     xpru.xpru_blocks=cblocks;
  219.     xpru.xpru_errors=cnaks;
  220.     xpru.xpru_timeouts=ctimeouts;
  221.     xpru.xpru_blocksize=gbufsize;
  222.     xpru.xpru_updatemask = XPRU_BLOCKS|XPRU_ERRORS|XPRU_TIMEOUTS|XPRU_BLOCKSIZE;
  223.     calla(xupdate, &xpru);
  224. }
  225.  
  226. Error(text)
  227. char *text;
  228. {
  229.     xpru.xpru_updatemask = XPRU_ERRORMSG;
  230.     xpru.xpru_errormsg   = text;
  231.     calla(xupdate, &xpru);
  232. }
  233.  
  234. LastError(text)
  235. char *text;
  236. {
  237.     cnaks++;
  238.  
  239.     xpru.xpru_updatemask = XPRU_ERRORMSG;
  240.     xpru.xpru_errormsg   = text;
  241.     calla(xupdate, &xpru);
  242.     OtherStats();
  243. }
  244.  
  245. Bytes(bytesnum)
  246. long bytesnum;
  247. {
  248.     xpru.xpru_bytes=bytesnum;
  249.     xpru.xpru_updatemask = XPRU_BYTES;
  250.     calla(xupdate, &xpru);
  251. }
  252.  
  253. Filename(text)
  254. char *text;
  255. {
  256.         xpru.xpru_updatemask = XPRU_FILENAME;
  257.         xpru.xpru_filename = text;
  258.         calla(xupdate, &xpru);
  259. }
  260.  
  261. /* The following can be used to debug. uncomment #define DEBUG at line 15 */
  262.  
  263. #ifdef DEBUG
  264. static long deblog = 0L;
  265.  
  266. DebugMsg(text)
  267. char *text;
  268. {
  269.     xpru.xpru_updatemask = XPRU_ERRORMSG;
  270.     xpru.xpru_errormsg   = text;
  271.     calla(xupdate, &xpru);
  272.  
  273.     if (!deblog) deblog = callaa(xfopen,"ram:qb.debug","a");
  274.     calladda(xfwrite,text,1L,(long)strlen(text),deblog);
  275.     calladda(xfwrite,"\n",1L,1L,deblog);
  276.     calla(xfclose,deblog);
  277.     deblog=0L;
  278. }
  279.  
  280. static char msg[200];
  281.  
  282. DebugVal(val)
  283. long val;
  284. {
  285.     sprintf(msg,"val (hex)=%lx (dec)=%ld",val,val);
  286.     DebugMsg(msg);
  287. }
  288. #endif
  289.  
  290. /* (m.b. note) the following routines were written by W.G.J. Langeveld
  291.     Copyrights may be in effect.
  292.  */
  293.  
  294. /**
  295. *
  296. *   The following functions setup the proper registers for the call-back 
  297. *   functions.
  298. *
  299. **/
  300. #asm
  301.         public _callad
  302. _callad:
  303.         movea.l 8(sp),a0                ; Second argument goes in a0
  304.         move.l  12(sp),d0               ; Third  argument goes in d0
  305. /*
  306. *   Now this is a trick to avoid using another register.
  307. *   Charlie taught me this...
  308. */
  309.         move.l  4(sp),-(sp)             ; First  argument is function
  310.         rts
  311.  
  312.         public  _calladda
  313. _calladda:
  314.         movea.l 8(sp),a0                ; Second argument goes in a0
  315.         move.l  12(sp),d0               ; Third  argument goes in d0
  316.         move.l  16(sp),d1               ; Fourth argument goes in d1
  317.         movea.l 20(sp),a1               ; Fifth  argument goes in a1
  318.         move.l  4(sp),-(sp)             ; First  argument is function
  319.         rts
  320.  
  321.         public  _calla
  322. _calla:
  323.         movea.l 8(sp),a0                ; Second argument goes in a0
  324.         move.l  4(sp),-(sp)             ; First  argument is function
  325.         rts
  326.  
  327.         public  _callaa
  328. _callaa:
  329.         movea.l 8(sp),a0                ; Second argument goes in a0
  330.         movea.l 12(sp),a1               ; Third  argument goes in a1
  331.         move.l  4(sp),-(sp)             ; First  argument is function
  332.         rts
  333.  
  334.         public  _calladd
  335. _calladd:
  336.         move.l  8(sp),a0                ; Second argument goes in a0
  337.         move.l  12(sp),d0               ; Third  argument goes in d0
  338.         move.l  16(sp),d1               ; Fourth argument goes in d1
  339.         move.l  4(sp),-(sp)             ; First  argument is function
  340.         rts
  341.  
  342.         public  _calld
  343. _calld:
  344.         move.l  8(sp),d0                ; Second argument goes in d0
  345.         move.l  4(sp),-(sp)             ; First  argument is function
  346.         rts
  347.  
  348. #endasm
  349. /*
  350. *   Could have added any other functions needed for other call-backs.
  351. *   Could have written a fancier single one... Could've...
  352. */
  353.