home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / amiga / comms / misc / xprcl014.lzh / xprclock.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-26  |  10.0 KB  |  358 lines

  1. /*
  2.     $Header: Welmat:src/RCS/lotek.c,v 1.20 92/09/12 20:43:18 rwm Exp Locker: rwm $
  3.  
  4.     Perform a Lotek/DietIFNA session
  5.  
  6.     Copyright (C) 1988,1989,1990 Michael Richardson
  7.     Copyright (C) 1992 Russell McOrmond
  8.  
  9.     This program is free software; you can redistribute it and/or modify
  10.     it under the terms of the GNU General Public License as published by
  11.     the Free Software Foundation; either version 2 of the License, or
  12.     (at your option) any later version.
  13.  
  14.     This program is distributed in the hope that it will be useful,
  15.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.     GNU General Public License for more details.
  18.  
  19.     You should have received a copy of the GNU General Public License
  20.     along with this program; if not, write to the Free Software
  21.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  
  23. */
  24.  
  25.  
  26. #ifdef RCSID
  27. static char RCSid[]="$Id: lotek.c,v 1.20 92/09/12 20:43:18 rwm Exp Locker: rwm $\n";
  28. #endif
  29.  
  30. #include <proto/all.h>
  31. #include <exec/types.h>
  32. #include <exec/memory.h>
  33. #include <time.h>
  34. #include <ctype.h>
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include "xproto.h"
  39. #include "xprclock.h"
  40.  
  41. struct ExecBase *SysBase=(struct ExecBase *)4l;
  42. extern struct DosLibrary *DOSBase;
  43.  
  44.  
  45. static char *months[]={"Jan", "Feb", "Mar", "Apr",
  46.                        "May", "Jun", "Jul", "Aug",
  47.                        "Sep", "Oct", "Nov", "Dec" };
  48.  
  49.  
  50. long  __saveds XProtocolSend(struct XPR_IO *io) {
  51.   struct Vars *v;
  52.   unsigned int i,state,dtime, ltime = 120;
  53.   long timeout, dummy, tstart, time(),gotit=FALSE;
  54.   int hours, minutes, seconds;
  55.   unsigned char buffer[256];
  56.   int tday, day, month;
  57.  
  58.   if(!(DOSBase=(struct DosLibrary *)OpenLibrary("dos.library",0)))
  59.     return XPRS_FAILURE;
  60.  
  61.   /* Perform common setup and initializations */
  62.   if (!(v = setup(io)))  return XPRS_FAILURE;
  63.  
  64.   SPrintF(v->scratch,"Year: %ld, Adjust: %ld",v->year, v->adjust);
  65.   updmsg(v,v->scratch);
  66.   updmsg(v,"Waiting for Atomic Clock time signal...");
  67.   state=i=xpr_sread(&v->io,buffer,9,10000000); /* wait a maximum of 10 seconds */
  68.   if(state==NODCD) {
  69.     upderr(v,"Carrier Dropped");
  70.     goto exit;
  71.   }
  72.   if(i==9) {
  73.     updmsg(v,"Getting there.");
  74.     timeout = time( &dummy) + 60l;
  75.     while(((dtime = (timeout - time(&dummy))) > 0) && (i<255)) {
  76.       if (ltime != dtime) {
  77.         SPrintF(v->scratch,"\r%02ld", dtime);
  78.         updmsg(v,v->scratch);
  79.         ltime = dtime;
  80.       }
  81.       state=xpr_sread(&v->io,&buffer[i],1,10000000);
  82.       if(state==NODCD) {
  83.         upderr(v,"Carrier Dropped");
  84.         goto exit;
  85.       }
  86.       if (((buffer[i-4]&0xf) == 6) &&
  87.         (buffer[i] != buffer[i-10]) &&
  88.         (buffer[i] == buffer[i-5]) &&
  89.         (buffer[i-1] == buffer[i-6]) &&
  90.         (buffer[i-2] == buffer[i-7]) &&
  91.         (buffer[i-3] == buffer[i-8]) &&
  92.         (buffer[i-4] == buffer[i-9])) {
  93.  
  94.         updmsg(v,"Getting Interesting.");
  95.         tday = (buffer[i-4]>>4) * 100 + (buffer[i-3]&0xf) * 10 +
  96.           (buffer[i-3]>>4);
  97.         hours = (buffer[i-2]&0xf) * 10 + (buffer[i-2]>>4) + (v->adjust);
  98.         minutes = (buffer[i-1]&0xf) * 10 + (buffer[i-1]>>4);
  99.         seconds = (buffer[i]&0xf) * 10 + (buffer[i]>>4) +1;
  100.  
  101.         if (seconds == 60) {
  102.           seconds = 0;
  103.           minutes++;
  104.           if (minutes == 60) {
  105.             minutes = 0;
  106.             hours++;
  107.           }
  108.         }
  109.     if (hours > 23) {
  110.           tday++;
  111.           hours -= 24;
  112.         }
  113.         else if (hours < 0) {
  114.           tday--;
  115.           hours+= 24;
  116.         }
  117.         time(&tstart);
  118.  
  119.         {
  120.           int feb;
  121.  
  122.           if ((v->year)/4*4 == (v->year) && (v->year)/100*100 != (v->year)) feb = 29;
  123.           else feb = 28;
  124.  
  125.           if (tday > (306 + feb)) {
  126.             month = 12;
  127.             day = tday - 306 - feb;
  128.           }
  129.           else if (tday > (276 + feb)) {
  130.             month = 11;
  131.             day = tday - 276 - feb;
  132.           }
  133.           else if (tday > (245 + feb)) {
  134.             month = 10;
  135.             day = tday - 245 - feb;
  136.           }
  137.           else if (tday > (215 + feb)) {
  138.             month = 9;
  139.             day = tday - 215 - feb;
  140.           }
  141.           else if (tday > (184 + feb)) {
  142.             month = 8;
  143.             day = tday - 184 - feb;
  144.           }
  145.           else if (tday > (153 + feb)) {
  146.             month = 7;
  147.             day = tday - 153 - feb;
  148.           }
  149.           else if (tday > (123 + feb)) {
  150.             month = 6;
  151.             day = tday - 123 - feb;
  152.           }
  153.           else if (tday > (92 + feb)) {
  154.             month = 5;
  155.             day = tday - 92 - feb;
  156.           }
  157.           else if (tday > (62 + feb)) {
  158.             month = 4;
  159.             day = tday - 62 - feb;
  160.           }
  161.           else if (tday > (31 + feb)) {
  162.             month = 3;
  163.             day = tday - 31 - feb;
  164.           }
  165.           else if (tday > 31) {
  166.             month = 2;
  167.             day = tday - 31;
  168.           }
  169.           else {
  170.             month = 1;
  171.             day = tday;
  172.           }
  173.         }
  174.         SPrintF(v->scratch,"Date %ld-%ld-%ld %02ld:%02ld:%02ld",
  175.                (v->year)-1900,months[month-1],day,hours,minutes,seconds);
  176.  
  177.         updmsg(v,v->scratch);
  178.         gotit=TRUE;
  179.         Execute(v->scratch,NULL,NULL);
  180.         break;
  181.       }
  182.       i=i+state;
  183.     }
  184.  
  185.   } else
  186.     updmsg(v,"No characters received...");
  187.  
  188. exit:
  189.  
  190.   if (!gotit) {
  191.     updmsg(v,"\nUNABLE TO CONNECT TO Atomic Clock TIMEBASE\n");
  192.  
  193.     KPrintF("\n\n");
  194.     for(state=0;state<i;state++) {
  195.       KPrintF(" %02lx",buffer[state]&0xff);
  196.     }
  197.     KPrintF("\n\n");
  198.     state=TRUE;
  199.   } else
  200.     state=OK;
  201.  
  202.   /* Clean up and return */
  203.   FreeMem(v,(long)sizeof(struct Vars));
  204.   return ((state) ? XPRS_FAILURE : XPRS_SUCCESS );
  205. }
  206.  
  207.  
  208. /* Main protocol for inbound calls */
  209. long __saveds XProtocolReceive(struct XPR_IO *io) {
  210.   /* just stub this out */
  211. return(XProtocolSend(io));
  212. }
  213.  
  214. /* Transfer options to use if XProtocolSetup not called */
  215. struct SetupVars Default_Config = {
  216.   NULL, NULL, 0,
  217.   { "1992" }, { "-5" }
  218. };
  219.  
  220.  
  221. /* Called by comm program to set transfer options */
  222. long __saveds XProtocolSetup(struct XPR_IO *io) {
  223.   struct SetupVars *sv;
  224.   UBYTE buf[512], *p;
  225.  
  226.   /* Allocate memory for transfer options string */
  227.   if (!(sv = (void *)io->xpr_data)) {
  228.     io->xpr_data = AllocMem((long)sizeof(struct SetupVars),MEMF_CLEAR);
  229.     if (!(sv = (void *)io->xpr_data)) {
  230.       ioerr(io,"Not enough memory");
  231.       return XPRS_FAILURE;
  232.     }
  233.     /* Start out with default options; merge user changes into defaults */
  234.     *sv = Default_Config;
  235.   }
  236.  
  237.   /* If options string passed by comm prog, use it; else use defaults */
  238.   if (io->xpr_filename)
  239.     strcpy(buf,io->xpr_filename);
  240.   else
  241.     return XPRS_SUCCESS;
  242.  
  243.   /* Upshift options string for easier parsing */
  244.   strupr(buf);
  245.  
  246.   if (p = find_option(buf,'Y')) {
  247.     int year=atoi(p);
  248.     if ((year > 1900) && (year < 9999)) SPrintF(sv->year,"%ld",year);
  249.     else ioerr(io,"Invalid Y Flag Ignored; Should Be 1900-9999");
  250.   }
  251.  
  252.   if (p = find_option(buf,'O')) {
  253.     SPrintF(sv->adjust,"%ld",atoi(p));    
  254.   }
  255.   return ( XPRS_SUCCESS );
  256. }
  257.  
  258.  
  259. /* Called by comm program to give us a chance to clean up before program ends */
  260. long __saveds XProtocolCleanup(struct XPR_IO *io) {
  261.   /* Release option memory, if any */
  262.   if (io->xpr_data) {
  263.     FreeMem(io->xpr_data,(long)sizeof(struct SetupVars));
  264.     io->xpr_data = NULL;
  265.   }
  266.   return XPRS_SUCCESS;
  267. }
  268.  
  269.  
  270. /* Called by comm program to let us monitor user's inputs; we never ask for
  271.    this to be called, but it's better to recover gracefully than guru the machine */
  272. long __saveds XProtocolHostMon(struct XPR_IO *io,UBYTE *serbuff,long actual,long maxsize) {
  273.   return actual;
  274. }
  275.  
  276.  
  277. /* Called by comm program to let us monitor user's inputs; we never ask for
  278.    this to be called, but it's better to recover gracefully than guru the machine */
  279. long __saveds XProtocolUserMon(struct XPR_IO *io,UBYTE *serbuff,long actual,long maxsize) {
  280.   return actual;
  281. }
  282.  
  283. /* Perform setup and initializations common to both Send and Receive routines */
  284. struct Vars *setup(struct XPR_IO *io) {
  285.   struct SetupVars *sv;
  286.   struct Vars *v;
  287.  
  288.   /* Make sure comm program supports the required call-back functions */
  289.   if (!io->xpr_update) return NULL;
  290.   if (!io->xpr_fopen || !io->xpr_fclose || !io->xpr_fread || !io->xpr_fwrite ||
  291.       !io->xpr_fseek || !io->xpr_sread || !io->xpr_swrite ||
  292.       !io->xpr_sflush) {
  293.     ioerr(io,"Comm Prog Missing Required Function(s); See Docs");
  294.     return NULL;
  295.   }
  296.  
  297.   /* Hook in default transfer options if XProtocolSetup wasn't called */
  298.   if (!(sv = (void *)io->xpr_data)) {
  299.     io->xpr_data = AllocMem((long)sizeof(struct SetupVars),MEMF_CLEAR);
  300.     if (!(sv = (void *)io->xpr_data)) {
  301.       ioerr(io,"Not Enough Memory");
  302.       return NULL;
  303.     }
  304.     *sv = Default_Config;
  305.   }
  306.  
  307.   /* Allocate memory for our unshared variables, to provide reentrancy */
  308.   if (!(v = AllocMem((long)sizeof(struct Vars),MEMF_CLEAR))) {
  309.     ioerr(io,"Not Enough Memory");
  310.     return NULL;
  311.   }
  312.  
  313.   /* Copy caller's io struct into our Vars for easier passing */
  314.   v->io = *io;
  315.  
  316.   v->year=atoi(sv->year);
  317.   v->adjust=atoi(sv->adjust);
  318.   return v;
  319. }
  320.  
  321. /* Search for specified option setting in string */
  322. UBYTE *find_option(UBYTE *buf,UBYTE option) {
  323.   while (*buf) {
  324.     buf += strspn(buf," ,\t\r\n");
  325.     if (*buf == option) return ++buf;
  326.     buf += strcspn(buf," ,\t\r\n");
  327.   }
  328.   return NULL;
  329. }
  330.  
  331. /* Have the comm program display an error message for us, using a
  332.    temporary XPR_UPDATE structure; used to display errors before Vars
  333.    gets allocated */
  334. void ioerr(struct XPR_IO *io,char *msg) {
  335.   struct XPR_UPDATE xpru;
  336.  
  337.   if (io->xpr_update) {
  338.     xpru.xpru_updatemask = XPRU_ERRORMSG;
  339.     xpru.xpru_errormsg = msg;
  340.     xpr_update(io,&xpru);
  341.   }
  342. }
  343.  
  344. /* Have the comm program display an error message for us, using the
  345.    normal XPR_IO structure allocated in Vars */
  346. void upderr(struct Vars *v,char *msg) {
  347.   v->xpru.xpru_updatemask = XPRU_ERRORMSG;
  348.   v->xpru.xpru_errormsg = msg;
  349.   xpr_update(&v->io,&v->xpru);
  350. }
  351.  
  352. /* Have the comm program display a normal message for us */
  353. void updmsg(struct Vars *v,char *msg) {
  354.   v->xpru.xpru_updatemask = XPRU_MSG;
  355.   v->xpru.xpru_msg = msg;
  356.   xpr_update(&v->io,&v->xpru);
  357. }
  358.