home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 272.lha / XProtocol / comm-program / xprfuncs.c < prev    next >
C/C++ Source or Header  |  1989-07-24  |  10KB  |  505 lines

  1. /** xprfuncs.c
  2. *
  3. *   Call-back functions for eXternal PRotocol support
  4. *
  5. **/
  6. #include "vt100.h"
  7. #include <stat.h>
  8. /*
  9. *   xproto.h is given in Appendix B
  10. */
  11. #include "xproto.h"
  12. /*
  13. *   xfer.h is a VLT private header file containing some information for
  14. *   file transfer protocols
  15. */
  16. #include "xfer.h"
  17.  
  18. /*
  19. *   These are the C versions of the interface
  20. */
  21. long        vlt_update(),  vlt_swrite(),   vlt_fread(),     vlt_fopen(),
  22.             vlt_fclose(),  vlt_gets(),     vlt_sread(),     vlt_chkabort(),
  23.             vlt_fwrite(),  vlt_fseek(),    vlt_ffirst(),    vlt_fnext(),
  24.             vlt_sflush(),  vlt_chkmisc(),  vlt_setserial(), vlt_finfo(),
  25.             vlt_options();
  26. /*
  27. *   These are the assembly level glue functions, see vltface.asm
  28. */
  29. extern long avlt_update(), avlt_swrite(),  avlt_fread(),     avlt_fopen(),
  30.             avlt_fclose(), avlt_gets(),    avlt_sread(),     avlt_chkabort(),
  31.             avlt_fwrite(), avlt_fseek(),   avlt_ffirst(),    avlt_fnext(),
  32.             avlt_sflush(), avlt_chkmisc(), avlt_setserial(), avlt_finfo(),
  33.             avlt_options();
  34.  
  35. /**
  36. *
  37. *   This function initializes an XPR_IO structure.
  38. *
  39. **/
  40. xpr_setup(IO)
  41. struct XPR_IO *IO;
  42. {
  43. /*
  44. *   NULL out all the functions we don't do yet.
  45. *   Fill the other ones with the addresses to the assembler glue version
  46. *   of the interface routines. See vltface.asm
  47. */
  48.    IO->xpr_filename  = NULL;
  49.    IO->xpr_fopen     = avlt_fopen;
  50.    IO->xpr_fclose    = avlt_fclose;
  51.    IO->xpr_fread     = avlt_fread;
  52.    IO->xpr_fwrite    = avlt_fwrite;
  53.    IO->xpr_sread     = avlt_sread;
  54.    IO->xpr_swrite    = avlt_swrite;
  55.    IO->xpr_sflush    = avlt_sflush;
  56.    IO->xpr_update    = avlt_update;
  57.    IO->xpr_chkabort  = avlt_chkabort;
  58.    IO->xpr_chkmisc   = avlt_chkmisc;
  59.    IO->xpr_gets      = avlt_gets;
  60.    IO->xpr_setserial = avlt_setserial;
  61.    IO->xpr_ffirst    = avlt_ffirst;
  62.    IO->xpr_fnext     = avlt_fnext;
  63.    IO->xpr_finfo     = avlt_finfo;
  64.    IO->xpr_fseek     = avlt_fseek;
  65. /*
  66. *   We support 1 extension field
  67. */
  68.    IO->xpr_extension = 1L;
  69.    IO->xpr_options   = avlt_options;
  70. /*
  71. *   NULL out the XPR private data field.
  72. */
  73.    IO->xpr_data      = NULL;
  74.  
  75.    return;
  76. }
  77.  
  78. /**
  79. *
  80. *   Interface to VLT's MsgDisplay() function.
  81. *
  82. **/
  83. /*
  84. *   These are formats for VLT's requester
  85. */
  86. static char *xprnamfmt = "%s\n%s\n\n\n\n";
  87. static char *filnamfmt = "\n\n%s\n\n\n";
  88. static char *blksizfmt = "\n\n\n\nBlock:  %6ld  --  Block Size:  %6ld\n";
  89. static char *errtimfmt = "\n\n\n\n\nErrors: %6ld  --  Timeouts:    %6ld";
  90. static char *delayfmt  = "\n\n\n\n\nPacket delay %ld";
  91. /*
  92. *   Below are some VLT globals to orchestrate the display
  93. */
  94. long xpr_blocks = 0L, xpr_blocksize = 0L, xpr_errors = 0L, xpr_timeouts = 0L;
  95. /*
  96. *   The function
  97. */
  98. long vlt_update(x)
  99. struct XPR_UPDATE *x;
  100. {
  101.    extern struct Window *mywindow;
  102.    extern char *XPR_Name;
  103. /*
  104. *   First time, determine the window size (50 chars wide, 5 lines tall).
  105. */
  106.    SetMsgWindow(mywindow, 50, 6);
  107. /*
  108. *   Use VLT's PostMsg function to display all the information.
  109. */
  110.    if (x->xpru_updatemask & XPRU_PROTOCOL) {
  111.       PostMsg(mywindow, xprnamfmt, XPR_Name, x->xpru_protocol);
  112.    }
  113.    if (x->xpru_updatemask & XPRU_MSG) {
  114.       PostMsg(mywindow, xprnamfmt, XPR_Name, x->xpru_msg);
  115.    }
  116.    if (x->xpru_updatemask & XPRU_ERRORMSG) {
  117.       PostMsg(mywindow, xprnamfmt, XPR_Name, x->xpru_errormsg);
  118.    }
  119.    if (x->xpru_updatemask & XPRU_FILENAME) {
  120.       PostMsg(mywindow, filnamfmt, x->xpru_filename);
  121.    }
  122.    if (x->xpru_updatemask & XPRU_PACKETDELAY) {
  123.       PostMsg(mywindow, delayfmt, x->xpru_packetdelay);
  124.    }
  125.    if (x->xpru_updatemask & (XPRU_BLOCKS | XPRU_BLOCKSIZE)) {
  126.       if (x->xpru_updatemask & XPRU_BLOCKS)    xpr_blocks    = x->xpru_blocks;
  127.       if (x->xpru_updatemask & XPRU_BLOCKSIZE) xpr_blocksize = x->xpru_blocksize;
  128.       PostMsg(mywindow, blksizfmt, xpr_blocks, xpr_blocksize);
  129.    }
  130.    if (x->xpru_updatemask & (XPRU_ERRORS | XPRU_TIMEOUTS)) {
  131.       if (x->xpru_updatemask & XPRU_ERRORS)   xpr_errors   = x->xpru_errors;
  132.       if (x->xpru_updatemask & XPRU_TIMEOUTS) xpr_timeouts = x->xpru_timeouts;
  133.       PostMsg(mywindow, errtimfmt, xpr_errors, xpr_timeouts);
  134.    }
  135.    return(0L);
  136. }
  137.  
  138. /**
  139. *
  140. *   Prompt the user for input
  141. *
  142. **/
  143. long vlt_gets(s, t)
  144. char *s, *t;
  145. {
  146. /*
  147. *   Use VLT's DoRequest() function
  148. */
  149.    return((long) DoRequest(mywindow, t, s, NULL, " Cancel "));
  150. }
  151.  
  152. /**
  153. *
  154. *   Write a string to the serial port
  155. *
  156. **/
  157. long vlt_swrite(s, n)
  158. char *s;
  159. long n;
  160. {
  161. /*
  162. *   Use VLT's SendString() function
  163. */
  164.    SendString(s, (int) n);
  165.    return(0L);
  166. }
  167.  
  168. /**
  169. *
  170. *   Read characters from the serial port
  171. *
  172. **/
  173. long vlt_sread(buff, length, micros)
  174. unsigned char *buff;
  175. long length, micros;
  176. {
  177.    extern int timeout;
  178.    long secs = 0L;
  179.  
  180.    if (buff == NULL) return(-1L);
  181. /*
  182. *   Convert timeout to seconds and micros if necessary
  183. */
  184.    if (micros) {
  185.       if (micros > 1000000L) {
  186.          secs   = micros / 1000000L;
  187.          micros = micros % 1000000L;
  188.       }
  189.    }
  190. /*
  191. *   Cheat! Only return a single character since we have such a nice
  192. *   readchar() function in VLT. One day I'll have to modify this to 
  193. *   save the odd microsecond...
  194. */
  195.    buff[0] = (unsigned char) readchar(secs, micros);
  196. /*
  197. *   VLT has a global called timeout. This comes in xfer.h.
  198. *   If the read was successful, return having read a single character.
  199. */
  200.    if (timeout == GOODREAD) return(1L);
  201. /*
  202. *   Else return error condition
  203. */
  204.    return(-1L);
  205. }
  206.  
  207. /**
  208. *
  209. *   Flush the serial buffer.
  210. *
  211. **/
  212. long vlt_sflush()
  213. {
  214.    ClearBuffer();
  215.    return(0L);
  216. }
  217.  
  218. /**
  219. *
  220. *   Interfaces to stdio
  221. *
  222. **/
  223. long vlt_fopen(s, t)
  224. char *s, *t;
  225. {
  226.    return((long) fopen(s, t));
  227. }
  228.  
  229. long vlt_fclose(fp)
  230. FILE *fp;
  231. {
  232.    return((long) fclose(fp));
  233. }
  234.  
  235. long vlt_fread(buff, size, count, fp)
  236. char *buff;
  237. long size, count;
  238. FILE *fp;
  239. {
  240.    int res;
  241.    res = fread(buff, (int) size, (int) count, fp);
  242.    return((long) res);
  243. }
  244.  
  245. long vlt_fwrite(buff, size, count, fp)
  246. char *buff;
  247. long size, count;
  248. FILE *fp;
  249. {
  250.    int res;
  251.    res = fwrite(buff, (int) size, (int) count, fp);
  252.    return((long) res);
  253. }
  254.  
  255. long vlt_fseek(fp, offset, origin)
  256. FILE *fp;
  257. long offset;
  258. long origin;
  259. {
  260.    int res;
  261.    res = fseek(fp, offset, (int) origin);
  262.    return((long) res);
  263. }
  264.  
  265. /*
  266. *   File name match (Marco's version).
  267. */
  268. extern char *scdir();        /* MANX pattern matching function */
  269.  
  270. long vlt_ffirst(buff, pattern)
  271. char *buff;
  272. char *pattern;
  273. {
  274.    char *name;
  275.  
  276.    name = scdir(pattern);
  277.    if (name) {
  278.       strcpy(buff, name);
  279.       return(1L);
  280.    }
  281.    else return(0L);
  282. }
  283.  
  284. long vlt_fnext(oldstate, buff, pattern)
  285. long oldstate;
  286. char *buff;
  287. char *pattern;
  288. {
  289.    return(vlt_ffirst(buff, pattern));
  290. }
  291.  
  292. /**
  293. *
  294. *   Check for Abort
  295. *
  296. **/
  297. long vlt_chkabort()
  298. {
  299. /*
  300. *   VLT aborts its protocols when the escape key is pressed.
  301. *   CheckForKey loops over the UserPort messages looking for an escape.
  302. */
  303.    return((long) CheckForKey(69));
  304. }
  305.  
  306. /**
  307. *
  308. *   Check for miscellaneous items
  309. *
  310. **/
  311. long vlt_chkmisc()
  312. {
  313. /*
  314. *   VLT does nothing
  315. */
  316.    return(0L);
  317. }
  318.  
  319. /**
  320. *
  321. *   File information
  322. *
  323. **/
  324. long vlt_finfo(filename, infotype)
  325. char *filename;
  326. long infotype;
  327. {
  328.    struct stat st;
  329.  
  330.    if      (infotype == 1L) {
  331.       if (stat(filename, &st) != -1) return(st.st_size);
  332.       else return(0L);
  333.    }
  334.    else if (infotype == 2L) {
  335.       return((long) (p_xlatemode + 1));
  336.    }
  337. }
  338.  
  339. /**
  340. *
  341. *   This function set the serial port and returns the current status.
  342. *
  343. **/
  344. long vlt_setserial(newstatus)
  345. long newstatus;
  346. {
  347.    long oldstatus, getserial();
  348.  
  349. /*
  350. *   If only want to know current status return it
  351. */
  352.    if (newstatus == -1L) return(getserial());
  353. /*
  354. *   Fields we don't support
  355. */
  356.    if (   newstatus & 0xFF00E070L)               return(-1L);
  357. /*
  358. *   Baud rates we don't support
  359. */
  360.    if ( ((newstatus & 0x00FF0000L) >> 16L) > 6L) return(-1L);
  361. /*
  362. *   Otherwise get old status
  363. */
  364.    oldstatus = getserial();
  365. /*
  366. *   Set new status
  367. */
  368.    setserial(newstatus);
  369. /*
  370. *   And return old status
  371. */
  372.    return(oldstatus);
  373. }
  374.  
  375. /**
  376. *
  377. *   Get current serial status
  378. *
  379. **/
  380. static long getserial()
  381. {
  382.    static long pariarr[] = { 0x0000L, 0x0301L, 0x0101L, 0x0001L, 0x0003L,
  383.                              0x0400L, 0x1B01L, 0x1901L, 0x1801L, 0x1803L,
  384.                              0x1C00L, 0x1F01L, 0x1D01L, 0x1C01L, 0x1C03L  };
  385.    long oldstatus;
  386.  
  387.    oldstatus = pariarr[p_parity];
  388. /*
  389. *   No Xon/Xoff
  390. */
  391.    if ((p_handshake & 1) == 0) oldstatus |= 0x0080L;
  392. /*
  393. *   7-wire
  394. */
  395.    if (p_handshake & 2) oldstatus |= 0x0004L;
  396. /*
  397. *   Baud rate
  398. */
  399.    oldstatus |= ( ((long) p_baud) << 16L );
  400.  
  401.    return(oldstatus);
  402. }
  403.  
  404. /**
  405. *
  406. *   Set new serial status
  407. *
  408. **/
  409. static int setserial(newstatus)
  410. long newstatus;
  411. {
  412. /*
  413. *   Parity
  414. */
  415.    switch (newstatus & 0xFFFFL) {
  416.       case 0x0000L :
  417.          p_parity = 0;
  418.          break;
  419.       case 0x0301L :
  420.          p_parity = 1;
  421.          break;
  422.       case 0x0101L :
  423.          p_parity = 2;
  424.          break;
  425.       case 0x0001L :
  426.          p_parity = 3;
  427.          break;
  428.       case 0x0003L :
  429.          p_parity = 4;
  430.          break;
  431.       case 0x0400L :
  432.          p_parity = 5;
  433.          break;
  434.       case 0x1B01L :
  435.          p_parity = 6;
  436.          break;
  437.       case 0x1901L :
  438.          p_parity = 7;
  439.          break;
  440.       case 0x1801L :
  441.          p_parity = 8;
  442.          break;
  443.       case 0x1803L :
  444.          p_parity = 9;
  445.          break;
  446.       case 0x1C00L :
  447.          p_parity = 10;
  448.          break;
  449.       case 0x1F01L :
  450.          p_parity = 11;
  451.          break;
  452.       case 0x1D01L :
  453.          p_parity = 12;
  454.          break;
  455.       case 0x1C01L :
  456.          p_parity = 13;
  457.          break;
  458.       case 0x1C03L :
  459.          p_parity = 14;
  460.          break;
  461.    }
  462.    BaudService(4, p_parity);
  463. /*
  464. *   Protocol
  465. */
  466.    p_handshake = 0;
  467.  
  468.    if ((newstatus & 0x0080L) == 0) p_handshake  = 1;
  469.    if ( newstatus & 0x0004L      ) p_handshake |= 2;
  470.  
  471.    BaudService(3, p_handshake);
  472. /*
  473. *   Baud rate
  474. */
  475.    p_baud = (newstatus & 0x00FF0000) >> 16L;
  476.    BaudService(2, p_baud);
  477.  
  478.    return;
  479. }
  480.  
  481. /**
  482. *
  483. *   Options function
  484. *
  485. **/
  486. long vlt_options(n, opt)
  487. long n;
  488. struct xpr_option *opt[];
  489. {
  490.    char buff[256];
  491.    long changed = 0L, i;
  492. /*
  493. *   Just loop over the options until we have time to implement a single
  494. *   requester.
  495. */
  496.    for (i = 0L; i < n; i++) {
  497.       strncpy(buff, opt[i]->xpro_value, (int) opt[i]->xpro_length);
  498.       if (DoRequest(mywindow, buff, opt[i]->xpro_description, NULL, " Cancel ")) {
  499.          strncpy(opt[i]->xpro_value, buff, (int) opt[i]->xpro_length);
  500.          changed |= (1L << i);
  501.       }
  502.    }
  503.    return(changed);
  504. }
  505.