home *** CD-ROM | disk | FTP | other *** search
/ nisttime.carsoncity.k12.mi.us / nisttime.carsoncity.k12.mi.us.tar / nisttime.carsoncity.k12.mi.us / pub / acts / rdline.c < prev    next >
Text File  |  1996-11-07  |  10KB  |  330 lines

  1. #include "nistime.h"
  2. /*
  3.         following is the block of global variables
  4.         used by all of the subroutines
  5.         these variables are defined here with their default
  6.         values which are modified by reading a configuration
  7.         file named settim.cfg.  this is done by subroutine setcfg.
  8.  
  9.     the global variables depend on which version this is.  the
  10.     ibm-pc version converts from received UTC to local time
  11.     based on the values in the configuration file, while UNIX
  12.     versions store the time in UTC
  13. */
  14. #include <stdio.h>
  15. #ifdef IBMPC
  16. int utcdif = -7;   /* local standard time - utc in hours */
  17. int dsflag = 1;    /* daylight savings time? 1 = yes, 0 = no */
  18. #endif
  19. int cmport = 0;    /* 0 for com1 port, 1 for com2 port, etc. */
  20. char number[256] = { 'A','T','D','T','9',',','4','9','4','4','7','7','4',
  21.                    '\r', 0 , 0 , 0 , 0 , 0 };  /* telephone number to dial */
  22. #ifdef IBMPC
  23. int atflag = 1;    /* AT-type machine? 1=yes, 0=no */
  24. #endif
  25. int debug  = 0;    /* list diagnostic messages? 1=yes, 0=no */
  26. int echo   = 1;    /* expect echoes from modem? 1=yes, 0=no  */
  27. int hs     = 1;    /* 1200 baud dialing? 1=yes, 0=no, use 300*/
  28. int retry  = 0;       /* number of times to retry if busy/no answer */
  29. #ifdef IBMPC
  30. int lpt    = 0;    /* pulse lpt1 on time? 1=yes, 0=no */
  31. int lpadr  = 0x37a; /* default command register for line printer */
  32. #ifndef BIOS
  33. int cmadr  = 0; /* command register for COM port, NO DEFAULT !!  */
  34. #endif
  35. #endif
  36. int setclk   = 1;     /* 1=set clock, 0=check time only          */
  37. int wrtdif   = 0;     /* 1=archive time dif, 0= do not 2=get rate too  */
  38. FILE *jop;            /* file handle for writing difference */
  39. /*
  40.     following structure is used to read
  41.     previous time difference from file if
  42.     "A" mode is selected
  43.  
  44.     structure contains time of previous
  45.     difference (year, month, day, hour,
  46.     minute and second) followed by difference
  47.     and units of difference.  note that the
  48.     IBMPC version has two such pairs -- one for
  49.     the RAM clock and one for the CMOS clock
  50. */
  51. struct tmprev
  52.     {
  53.     int yrprev;
  54.     int moprev;
  55.     int dyprev;
  56.     int hrprev;
  57.     int mnprev;
  58.     int scprev;
  59.     float dffprv;
  60.     char unprev;
  61. #ifdef IBMPC
  62.     float datprv;
  63.     char uatprv;
  64. #endif
  65. } tmpp;
  66. main(argc,argv)
  67. int argc;
  68. char *argv[];
  69. {
  70. /*
  71.         this is the main program named rdline for task nistime.
  72. */
  73. #ifdef IBMPC
  74. #include <dos.h>
  75. #endif
  76. char buf[280],ichar;
  77. void wrtbuf(),inilin(),hangup(),sndptr(),diftim(),arcdif();
  78. int rdbuf(),cmplst();
  79. void wait(),parset(),setcfg();
  80. int dial();
  81. int j,param;
  82. int len;
  83. int lim;
  84. int tmo  = 0;
  85. char ie1 = '*';
  86. char ie2 = '#';
  87. char ie3 = -120;
  88. char ie3a= '>';    /* changed to allow titles to terminate with > after OTM */
  89. int jm;
  90. /*
  91.         following constant sets timeout value for read.
  92.         this version (7 jan 92) uses a time-out value that is
  93.         specified directly in ticks and is not scaled by
  94.         the cpu speed for the PC version.  the value is
  95.         therefore independent of BIOS/non-BIOS access and
  96.         machine speed.
  97.         the new higher speed modems often take several seconds to
  98.         start transmitting after the connect message is received.
  99.         the time-out is set longer on the first line for that
  100.         reason.
  101. */
  102. #ifdef IBMPC
  103. int ttmo = -72;        /*72 ticks is about 4 seconds */
  104. int ttmoo = -360;    /*about 20 seconds -- used for first line only*/
  105. #endif
  106. #ifdef SUN
  107. int ttmo = -20;
  108. int ttmoo = -100;
  109. #endif
  110. /*
  111.         following variables are used in cmplst and are defined
  112.         here so that they remain defined across consecutive
  113.         calls.
  114.         osec is the previous value of the second
  115.         obuf holds the time string of the previous transmission
  116.         count is 0 until a line has been read
  117. */
  118. int count = 0;
  119. int osec  = 0;
  120. char obuf[25];
  121. /*
  122.         see if there is a switch on the command line
  123. */
  124.         if( (argc > 1) && (*argv[1] == '-') )
  125.         {
  126.         argv[1]++;
  127.         if(*argv[1] == 'd') debug = 1;
  128.         }
  129. #ifdef IBMPC
  130.         printf("\nNISTIME/ACTS Version of %s \n",__DATE__);
  131. #endif
  132. #ifdef SUN
  133.     printf("\n Version of 28 October 1992 for SUN system \n");
  134. #endif
  135.  
  136. /*
  137.         open configuration file and set global parameters
  138. */
  139.         setcfg();
  140. /*
  141.         following code sets number of times first loop is done.
  142.         previous versions had a different limit for 300 bits/s, but
  143.         there is no difference in this version since the time can
  144.         now be set at either speed.
  145. */
  146.         lim=12;
  147.         if(debug != 0)
  148.         {
  149.         printf("\n input configuration \n %s",number);
  150. #ifdef IBMPC
  151.         printf("\n utcdif= %d",utcdif);
  152.         printf("\n cmport= COM%1d",cmport+1);
  153.         #ifdef BIOS
  154.         printf("\n i/o via calls to BIOS routines.");
  155.     #endif
  156.         #ifndef BIOS
  157.         printf("\n i/o via direct operations on port.");
  158.         #endif
  159.         printf("\n atflag= %d",atflag);
  160.         printf("\n dsflag= %d",dsflag);
  161. #endif
  162. #ifdef SUN
  163.     printf("\n port file descriptor=%d",cmport);
  164. #endif
  165.         printf("\n echo  = %d",echo);
  166.         printf("\n hs    = %d",hs);
  167. #ifdef IBMPC
  168.         printf("\n lpt1  = %d",lpt);
  169.         printf("\n lp adr= %x",lpadr);
  170. #endif
  171.         printf("\n set clk ?, (1=y)= %d",setclk);
  172.         printf("\n wrt to file (1=y), (2=y & rate)=%d",wrtdif);
  173.     if(retry == 0)
  174.       printf("\n Program will not retry if BUSY or NO ANSWER");
  175.     if(retry < 0) printf("\n Program will ask before each re-try.");
  176.     if(retry > 0)
  177.      printf("\n Up to %d retries if BUSY or NO ANSWER", retry);
  178.         }  /* end of debug printing */
  179.         if(setclk == 0)
  180.               printf("\n Computer clock will not be set.");
  181. /*
  182.         initialize com line
  183. */
  184.     if(debug != 0) printf("\n Initializing COM port.");
  185.         inilin();
  186.     if(debug != 0) printf("\n initialization complete.");
  187. /*
  188.         if debug is turned on and if direct calls are used, print
  189.         effective address which was determined by inilin
  190. */
  191. #ifdef IBMPC
  192. #ifndef BIOS
  193.         if(debug != 0) printf("\n hex. address of port=%x",cmadr);
  194. #endif
  195. #endif
  196. /*
  197.         now dial number -- continue if dial status shows connect
  198. */
  199.         if(dial() == 1)
  200.         {
  201. /*
  202.         now start reading from line
  203.         note that 6 lines will be read followed by a time set.
  204.         note that this version does not distinguish between
  205.         300 and 1200.
  206. */
  207.         for(j=0; j<lim; j++)
  208.            {
  209.            len=rdbuf(buf,ie1,ie2,ie3a,ttmoo);
  210.            if( (len > 0) && (buf[0] > 0) )
  211.               {
  212. #ifdef IBMPC
  213.               if(lpt != 0) sndptr();  /* pulse strobe of lpt1*/
  214. #endif
  215.               wrtbuf(&buf[len-1]);    /* echo terminating character */
  216. #ifdef IBMPC
  217.               printf("\n %s",buf);
  218. #endif
  219. #ifdef SUN
  220.           printf("%s",buf);       /* SUN version uses no newline*/
  221. #endif    
  222.            }
  223.            else
  224.               {
  225.               tmo=1;     /* show exit on time out */
  226.               if(debug != 0) printf("\n time out in main loop.");
  227.               break;     /* get out of loop on a time-out*/
  228.            }
  229.          }
  230. /*
  231.         write difference to archive file if enabled
  232.         and if the previous loop did not end on a time-out.
  233.         if it did end on a time-out then skip this part since
  234.         the connection has been lost
  235. */
  236.         if( (wrtdif != 0) &&  (tmo == 0) )
  237.            {
  238.            len=rdbuf(buf,ie1,ie2,ie3,ttmo);
  239.            if( (len > 0) && (buf[0] > 0) )
  240.               {
  241. #ifdef IBMPC
  242.               if(lpt != 0) sndptr();
  243. #endif
  244.               wrtbuf(&buf[len-1]);
  245.               arcdif(buf);
  246.            }
  247.            else
  248.               {
  249.               tmo = 1;
  250.               if(debug != 0) printf("\n time out in arcdif.");
  251.            }
  252.         }
  253. /*
  254.     if clock setting has been requested and if the previous
  255.     operations did not sense a time out then read the next
  256.         two lines and be sure they are sequential in time.
  257.         If they are, parse second line and set time.
  258.         if sequential comparison fails, print message
  259.         in comparison subroutine and try again.
  260.  
  261.         inner while loop continues until two consecutive lines pass
  262.         the test or until the read fails on a time-out and tmo =1.
  263. */
  264.         if( (setclk == 1) && (tmo == 0) )
  265.           {
  266.           do
  267.            {
  268.            len=rdbuf(buf,ie1,ie2,ie3,ttmo);
  269.            if(debug != 0) printf("\n len=%d \n %s",len,buf);
  270.            if( (len > 0) && (buf[0] > 0) )
  271.               {
  272. #ifdef IBMPC
  273.               if(lpt != 0) sndptr();
  274. #endif
  275.               wrtbuf(&buf[len-1]);
  276.               jm=cmplst(buf,&count,&osec,obuf);
  277.               if(debug != 0)
  278.                  printf("\n count=%d osec=%d \n %s",count,osec,obuf);
  279.               if(jm == 1) parset(buf); /* jm=1 means times are sequential*/
  280.            }
  281.            else
  282.               {
  283.               tmo=1;
  284.               if(debug != 0) printf("\n time out after parset.");
  285.            }
  286.           } while ( (jm < 1) && (tmo == 0) );
  287.         }   /* end of if setclk == 1 && tmo == 0 */
  288.  /*
  289.         now read next line and then check the time
  290. */
  291.         if(tmo == 0)
  292.           {
  293.           len=rdbuf(buf,ie1,ie2,ie3,ttmo);
  294.           if( (len > 0) && (buf[0] > 0) )
  295.            {
  296. #ifdef IBMPC
  297.            if(lpt != 0) sndptr();
  298. #endif
  299.            wrtbuf(&buf[len-1]);
  300.            diftim(buf);
  301.           }
  302.           else
  303.            {
  304.            tmo = 1;
  305.            if(debug != 0) printf("\n timeout after diftim.");
  306.           }
  307.         }
  308. /*
  309.         if lpt is on, then continue reading lines until ended
  310.         by a timeout and if we have not had a timeout yet
  311. */
  312. #ifdef IBMPC
  313.         if( (lpt != 0) && (tmo == 0) )
  314.         {
  315.            do
  316.               {
  317.               len=rdbuf(buf,ie1,ie2,ie3,ttmo);
  318.               if( (len > 0) && (buf[0] > 0) )
  319.                  {
  320.                  sndptr();
  321.                  wrtbuf(&buf[len-1]);
  322.                  printf("\n %s",buf);
  323.                  }
  324.                } while (len > 0);     /* loop until nothing more read */
  325.         }
  326. #endif
  327.         hangup();
  328.         }
  329. }
  330.