home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 200-299 / ff247.lzh / XprLib / comm-program / xprfuncs.c < prev   
C/C++ Source or Header  |  1989-09-15  |  10KB  |  513 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. *   What follows is pseudo code. VLT's implementation is at this point
  192. *   different.
  193. */
  194.    StartTimer(secs, micros);
  195. /*
  196. *   VLT has a global called timeout. This comes in xfer.h.
  197. *   If the read was successful, add character to buffer.
  198. */
  199.    for (i = 0; (timeout == GOODREAD) && (i < size); i++)
  200.       buff[i] = (unsigned char) readchar();
  201. /*
  202. *   Either timed out or buffer is full.
  203. */
  204.    if (timeout != TIMEOUT) AbortTimer();
  205. /*
  206. *   If carrier dropped, return error condition
  207. */
  208.    if (timeout == CARRIER_DROPPED) return(-1L);
  209. /*
  210. *   Else return the number of characters read.
  211. */
  212.    return(i);
  213. }
  214.  
  215. /**
  216. *
  217. *   Flush the serial buffer.
  218. *
  219. **/
  220. long vlt_sflush()
  221. {
  222.    ClearBuffer();
  223.    return(0L);
  224. }
  225.  
  226. /**
  227. *
  228. *   Interfaces to stdio
  229. *
  230. **/
  231. long vlt_fopen(s, t)
  232. char *s, *t;
  233. {
  234.    return((long) fopen(s, t));
  235. }
  236.  
  237. long vlt_fclose(fp)
  238. FILE *fp;
  239. {
  240.    return((long) fclose(fp));
  241. }
  242.  
  243. long vlt_fread(buff, size, count, fp)
  244. char *buff;
  245. long size, count;
  246. FILE *fp;
  247. {
  248.    int res;
  249.    res = fread(buff, (int) size, (int) count, fp);
  250.    return((long) res);
  251. }
  252.  
  253. long vlt_fwrite(buff, size, count, fp)
  254. char *buff;
  255. long size, count;
  256. FILE *fp;
  257. {
  258.    int res;
  259.    res = fwrite(buff, (int) size, (int) count, fp);
  260.    return((long) res);
  261. }
  262.  
  263. long vlt_fseek(fp, offset, origin)
  264. FILE *fp;
  265. long offset;
  266. long origin;
  267. {
  268.    int res;
  269.    res = fseek(fp, offset, (int) origin);
  270.    return((long) res);
  271. }
  272.  
  273. /*
  274. *   File name match (Marco's version).
  275. */
  276. extern char *scdir();        /* MANX pattern matching function */
  277.  
  278. long vlt_ffirst(buff, pattern)
  279. char *buff;
  280. char *pattern;
  281. {
  282.    char *name;
  283.  
  284.    name = scdir(pattern);
  285.    if (name) {
  286.       strcpy(buff, name);
  287.       return(1L);
  288.    }
  289.    else return(0L);
  290. }
  291.  
  292. long vlt_fnext(oldstate, buff, pattern)
  293. long oldstate;
  294. char *buff;
  295. char *pattern;
  296. {
  297.    return(vlt_ffirst(buff, pattern));
  298. }
  299.  
  300. /**
  301. *
  302. *   Check for Abort
  303. *
  304. **/
  305. long vlt_chkabort()
  306. {
  307. /*
  308. *   VLT aborts its protocols when the escape key is pressed.
  309. *   CheckForKey loops over the UserPort messages looking for an escape.
  310. */
  311.    return((long) CheckForKey(69));
  312. }
  313.  
  314. /**
  315. *
  316. *   Check for miscellaneous items
  317. *
  318. **/
  319. long vlt_chkmisc()
  320. {
  321. /*
  322. *   VLT does nothing
  323. */
  324.    return(0L);
  325. }
  326.  
  327. /**
  328. *
  329. *   File information
  330. *
  331. **/
  332. long vlt_finfo(filename, infotype)
  333. char *filename;
  334. long infotype;
  335. {
  336.    struct stat st;
  337.  
  338.    if      (infotype == 1L) {
  339.       if (stat(filename, &st) != -1) return(st.st_size);
  340.       else return(0L);
  341.    }
  342.    else if (infotype == 2L) {
  343.       return((long) (p_xlatemode + 1));
  344.    }
  345. }
  346.  
  347. /**
  348. *
  349. *   This function set the serial port and returns the current status.
  350. *
  351. **/
  352. long vlt_setserial(newstatus)
  353. long newstatus;
  354. {
  355.    long oldstatus, getserial();
  356.  
  357. /*
  358. *   If only want to know current status return it
  359. */
  360.    if (newstatus == -1L) return(getserial());
  361. /*
  362. *   Fields we don't support
  363. */
  364.    if (   newstatus & 0xFF00E070L)               return(-1L);
  365. /*
  366. *   Baud rates we don't support
  367. */
  368.    if ( ((newstatus & 0x00FF0000L) >> 16L) > 6L) return(-1L);
  369. /*
  370. *   Otherwise get old status
  371. */
  372.    oldstatus = getserial();
  373. /*
  374. *   Set new status
  375. */
  376.    setserial(newstatus);
  377. /*
  378. *   And return old status
  379. */
  380.    return(oldstatus);
  381. }
  382.  
  383. /**
  384. *
  385. *   Get current serial status
  386. *
  387. **/
  388. static long getserial()
  389. {
  390.    static long pariarr[] = { 0x0000L, 0x0301L, 0x0101L, 0x0001L, 0x0003L,
  391.                              0x0400L, 0x1B01L, 0x1901L, 0x1801L, 0x1803L,
  392.                              0x1C00L, 0x1F01L, 0x1D01L, 0x1C01L, 0x1C03L  };
  393.    long oldstatus;
  394.  
  395.    oldstatus = pariarr[p_parity];
  396. /*
  397. *   No Xon/Xoff
  398. */
  399.    if ((p_handshake & 1) == 0) oldstatus |= 0x0080L;
  400. /*
  401. *   7-wire
  402. */
  403.    if (p_handshake & 2) oldstatus |= 0x0004L;
  404. /*
  405. *   Baud rate
  406. */
  407.    oldstatus |= ( ((long) p_baud) << 16L );
  408.  
  409.    return(oldstatus);
  410. }
  411.  
  412. /**
  413. *
  414. *   Set new serial status
  415. *
  416. **/
  417. static int setserial(newstatus)
  418. long newstatus;
  419. {
  420. /*
  421. *   Parity
  422. */
  423.    switch (newstatus & 0xFFFFL) {
  424.       case 0x0000L :
  425.          p_parity = 0;
  426.          break;
  427.       case 0x0301L :
  428.          p_parity = 1;
  429.          break;
  430.       case 0x0101L :
  431.          p_parity = 2;
  432.          break;
  433.       case 0x0001L :
  434.          p_parity = 3;
  435.          break;
  436.       case 0x0003L :
  437.          p_parity = 4;
  438.          break;
  439.       case 0x0400L :
  440.          p_parity = 5;
  441.          break;
  442.       case 0x1B01L :
  443.          p_parity = 6;
  444.          break;
  445.       case 0x1901L :
  446.          p_parity = 7;
  447.          break;
  448.       case 0x1801L :
  449.          p_parity = 8;
  450.          break;
  451.       case 0x1803L :
  452.          p_parity = 9;
  453.          break;
  454.       case 0x1C00L :
  455.          p_parity = 10;
  456.          break;
  457.       case 0x1F01L :
  458.          p_parity = 11;
  459.          break;
  460.       case 0x1D01L :
  461.          p_parity = 12;
  462.          break;
  463.       case 0x1C01L :
  464.          p_parity = 13;
  465.          break;
  466.       case 0x1C03L :
  467.          p_parity = 14;
  468.          break;
  469.    }
  470.    BaudService(4, p_parity);
  471. /*
  472. *   Protocol
  473. */
  474.    p_handshake = 0;
  475.  
  476.    if ((newstatus & 0x0080L) == 0) p_handshake  = 1;
  477.    if ( newstatus & 0x0004L      ) p_handshake |= 2;
  478.  
  479.    BaudService(3, p_handshake);
  480. /*
  481. *   Baud rate
  482. */
  483.    p_baud = (newstatus & 0x00FF0000) >> 16L;
  484.    BaudService(2, p_baud);
  485.  
  486.    return;
  487. }
  488.  
  489. /**
  490. *
  491. *   Options function
  492. *
  493. **/
  494. long vlt_options(n, opt)
  495. long n;
  496. struct xpr_option *opt[];
  497. {
  498.    char buff[256];
  499.    long changed = 0L, i;
  500. /*
  501. *   Just loop over the options until we have time to implement a single
  502. *   requester.
  503. */
  504.    for (i = 0L; i < n; i++) {
  505.       strncpy(buff, opt[i]->xpro_value, (int) opt[i]->xpro_length);
  506.       if (DoRequest(mywindow, buff, opt[i]->xpro_description, NULL, " Cancel ")) {
  507.          strncpy(opt[i]->xpro_value, buff, (int) opt[i]->xpro_length);
  508.          changed |= (1L << i);
  509.       }
  510.    }
  511.    return(changed);
  512. }
  513.