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 / setcfg.c < prev    next >
C/C++ Source or Header  |  1996-11-07  |  15KB  |  523 lines

  1. void setcfg()
  2. {
  3. /*
  4.         this subroutine opens the configuration file named
  5.         nistime.cfg and sets the various global parameters
  6.         based on the contents.
  7.  
  8.  
  9.         if the file does not exist
  10.         the globals are not altered and the default configuration
  11.         is used.  the global parameters are defined in the main
  12.         program preamble.
  13.     the configuration file is slight different for the different
  14.     versions of the program.
  15.     the first line always contains the telephone number.
  16.     the second line contains the port specifier at the start.
  17.     for the ibm pc version, this is a single digit or h (for
  18.     a hardware address to be specified on line 6). for the
  19.     UNIX version, if the first two character are letters or
  20.     numbers (nn), they define the port address as /dev/tty<nn>.
  21.     if the first is a letter or number (n) and  the second is - then
  22.     the port address is /dev/tty<n>. if both positions are -, then
  23.     the port is specified by its full path on the last line.
  24. */
  25. #include "nistime.h"
  26. #include <stdio.h>
  27. #ifdef SUN
  28. #include <sys/file.h>
  29. #include <fcntl.h>        /*this include not needed in some systems */
  30. #endif
  31. #include <ctype.h>
  32. FILE *fopen();
  33. FILE *iop;
  34. int getlst();
  35. extern FILE *jop;
  36. extern int debug;       /* turn on debug mode if = 1 */
  37. #ifdef SUN
  38. char dv[20]; /*device path name*/
  39. #endif
  40. int j,eol,icol;
  41. char c;
  42. char *peof,*perr;
  43. /*
  44.     the IBMPC version can convert the received time
  45.     from UTC to local time using the following two flags.
  46.     the SUN version always sets the time directly in
  47.     UTC seconds since 1970 since that is the UNIX standard
  48. */
  49. #ifdef IBMPC
  50. extern int utcdif;         /* local time - utc in hours */
  51. extern int dsflag;         /* daylight savings time? 1=yes, 0=no */
  52. #endif
  53. /*
  54.     for the IBMPC version, cmport specifies the comport, 0=com1,
  55.     1=com2, etc.
  56.     for the SUN version, cmport is the file handle returned when
  57.     the comport is opened at the end of this routine.
  58. */
  59. extern int cmport;
  60. #ifdef IBMPC
  61. extern int atflag;         /* 1=AT-type machine with CMOS clock, 0= not */
  62. #endif
  63. extern char number[];      /* number to dial with trailing <cr> and 0 */
  64. extern int echo;           /* expect echoes from modem? 1=yes, 0=no */
  65. extern int hs;             /* 1 = use 1200 baud, 0=use 300 baud     */
  66. #ifdef IBMPC
  67. extern int lpt;             /* 1=  pulse lp on time, 0= do not pulse */
  68. #endif
  69. extern int setclk;         /*  1= set computer clock, 0= do not set */
  70. /*
  71.     wrtdif = 1      compute current time difference and write to file
  72.     wrtdif = 0      do not write to file
  73.     wrtdif = 2      write current difference to file and estimate rate
  74.               using second difference divided by elapsed time
  75. */
  76. extern int wrtdif;
  77. #ifdef IBMPC
  78. #ifndef BIOS
  79. extern int cmadr;          /* hardware address of port for _d version*/
  80. #endif
  81. #endif
  82. extern int retry;       /* number of times to retry if BUSY or NO ANSWER*/
  83.         perr="\n Syntax error in config. file ";
  84.         peof=" -- premature EOF \n ";
  85.         iop=fopen("nistime.cfg","rt");
  86.         if(iop == 0)
  87.         {
  88.         printf("\n can't open file nistime.cfg, use default configuration.");
  89.         return;
  90.         }
  91. /*
  92.         first line of file is full telephone number. If this line
  93.         begins with either T or P then we have an old-style command
  94.         file.  read the characters and store then with a leading
  95.         ATD which starts any command to the modem.
  96.         if this line begins with A then it is a more complex
  97.         string of modem commands which we may not understand.
  98.         read the string in this case without interpretation --
  99.         it will be passed by the dial subroutine directly to
  100.         the modem.  If there is a problem here, we may not be
  101.         able to detect it until the modem returns some kind of
  102.         error status.
  103. */
  104.         eol=0;
  105.         for(j=3; (j<256) && (eol==0); j++)
  106.           {
  107.           c=getc(iop);
  108.           switch(c)
  109.            {
  110.            case EOF:
  111.               printf("%s %s",perr,peof);
  112.               abort();
  113.               break;
  114.            case '\n':
  115.               number[j]='\r';        /*terminate with carriage return*/
  116.               number[j+1]=0;        /* and then a null */
  117.               eol=1;
  118.               break;
  119.            default:
  120.               number[j]=c;
  121.               break;
  122.           }
  123.         }
  124.         if(eol == 0)
  125.           {
  126.           printf("%s -- telephone # too long. \n",perr);
  127.           abort();
  128.         }
  129. /*
  130.     for ibm version:
  131.     next line contains comport, 1 = com1, 2 = com2 in col 1, etc.
  132.         if first line contains h, then comport hardware address will be
  133.         specified on sixth line of file.  if comport is specified by
  134.     number, sixth line of file is not used.
  135.         second char sets echo: B=0, anything else =1,
  136.         third character is baud rate, h=1200, l=300;
  137.         fourth character is line printer y=yes, n=no.
  138.         fifth character is set clocks, s=set, d=disable set.
  139.         rest of line is ignored and may be used for a comment
  140.  
  141.     for sun version:
  142.     if first two characters are alphanumeric, they specify port.
  143.     if first character is alphanumeric and second is -, first is port.
  144.     if first is - then port is specified by its full address.
  145.     note that length of port specifier may be 1 or 2 chars so that
  146.     the word next below depends on how the port was specified
  147.     next character is echo as above.
  148.     next is baud rate as above
  149.     next is set/ no set as above.
  150. */
  151.     icol=0;            /* keep track of what col. we are in*/
  152.         c=getc(iop);
  153.     icol++;
  154.         switch (c)
  155.         {
  156.         case EOF:
  157.            printf("%s %s",perr,peof);
  158.            abort();
  159.            break;
  160. #ifdef IBMPC
  161.         case '1':
  162.            cmport=0;
  163.            break;
  164.         case '2':
  165.            cmport=1;
  166.            break;
  167.         #ifndef BIOS
  168.         case '3':
  169.            cmport=2;
  170.            break;
  171.         case '4':
  172.            cmport=3;
  173.            break;
  174.         case 'h':
  175.         case 'H':
  176.            cmport= -1;
  177.            break;
  178.         #endif
  179.         default:
  180.            #ifdef BIOS
  181.            printf("%s -- comport not 1 or 2 \n",perr);
  182.        #endif
  183.        #ifndef BIOS
  184.            printf("%s -- comport must be 1,2 3,4 or h \n",perr);
  185.            #endif
  186.            abort();
  187.            break;
  188. #endif
  189. #ifdef SUN
  190.     case '-':           /* port to be specified by path name */
  191.        cmport= -1;
  192.        break;
  193.     default:
  194.        if( (isalpha(c) == 0) && (isdigit(c) == 0) )
  195.         {
  196.         printf("%s -- port address must be alphanumeric \n",perr);
  197.         abort();
  198.         break;
  199.         }
  200. /*
  201.     name begins with /dev/tty then add chars just read
  202. */
  203.        sprintf(&dv[0],"%s","/dev/tty");
  204.        dv[8]=c;
  205.        c=getc(iop);
  206.        icol++;
  207.        if(c == '-')             /* comport is only 1 char long*/
  208.         {
  209.         dv[9]='\0';         /*terminate string with null*/
  210.         cmport=1;           /*show port specified ok.   */
  211.         break;
  212.         }
  213.        if( (isalpha(c) == 0) && (isdigit(c) == 0) )
  214.         {
  215.         printf("%s -- port address must be alphanumeric \n",perr);
  216.         abort();
  217.         break;
  218.         }
  219.        dv[9]=c;            /* get second character of comport*/
  220.        dv[10]='\0';        /* and terminate with null byte */
  221.        cmport= 1;          /* show port specified ok.      */
  222.        break;
  223. #endif
  224.         }
  225.         c=getc(iop);
  226.     icol++;
  227.         switch(c)
  228.         {
  229.         case EOF:
  230.            printf("%s %s", perr,peof);
  231.            abort();
  232.            break;
  233.         case 'b':
  234.         case 'B':
  235.            echo=0;
  236.            break;
  237.         case 'e':
  238.         case 'E':
  239.            echo=1;
  240.            break;
  241.         default:
  242.            printf("\nWarning, Line 2 Char %d not e or b -- e assumed.",icol);
  243.            echo=1;
  244.            break;
  245.         }
  246.         c=getc(iop);
  247.     icol++;
  248.         switch (c)
  249.         {
  250.         case EOF:
  251.            printf("%s %s",perr,peof);
  252.            abort();
  253.         case 'h':
  254.         case 'H':
  255.            hs=1;
  256.            break;
  257.         case 'l':
  258.         case 'L':
  259.            hs=0;
  260.            break;
  261.         default:
  262.            printf("\n Warning, Line 2 Char %d not h or l -- h assumed.",icol);
  263.            hs=1;
  264.            break;
  265.         }
  266. #ifdef IBMPC
  267.         c=getc(iop);
  268.     icol++;
  269.         switch (c)
  270.         {
  271.         case EOF:
  272.            printf("%s %s",perr,peof);
  273.            abort();
  274.         case 'y':
  275.         case 'Y':
  276.            lpt=1;
  277.            break;
  278.         case 'n':
  279.         case 'N':
  280.            lpt=0;
  281.            break;
  282.         default:
  283.            printf("\n Warning, Line 2 Char %d not y or n -- n assumed.",icol);
  284.            lpt=0;
  285.            break;
  286.         }
  287. #endif
  288.         c=getc(iop);
  289.     icol++;
  290.         switch (c)
  291.         {
  292.         case EOF:
  293.            printf("%s %s",perr,peof);
  294.            abort();
  295.         case 's':
  296.         case 'S':
  297.            setclk=1;
  298.            break;
  299.         case 'd':
  300.         case 'D':
  301.            setclk=0;
  302.            break;
  303.         default:
  304.            printf("\n Warning, Line 2 Char %d not s or d -- d assumed.",icol);
  305.            setclk=0;
  306.            break;
  307.         }
  308.         c=getc(iop);
  309.     icol++;
  310.         switch (c)
  311.         {
  312.         case EOF:
  313.            printf("%s %s",perr,peof);
  314.            abort();
  315. /*
  316.     the next character specifies if the time difference is to be
  317.     stored in the archive file.  a response of "a" means store difference
  318.     and a response of "n" means do not store it.  any other response is
  319.     taken to be "n" and a diagnostic is printed.  In addition, an upper
  320.     case response "A" also means estimate the rate offset of the clock
  321.     by comparing the last difference with the current difference and
  322.     dividing the second difference by the elapsed time.  this value is
  323.     the rate offset with respect to UTC(NIST).
  324. */
  325.     case 'A':
  326.        if( getlst() == 0)
  327.           {
  328.           printf("\n Can't read previous difference for rate estimate.");
  329.           wrtdif=0;
  330.           }
  331.        else
  332.           {
  333.           wrtdif=2;
  334.           break;
  335.           }
  336.         case 'a':
  337.            wrtdif=1;
  338.            if(  (jop=fopen("nistime.dif","at")) == 0)
  339.               {
  340.               printf("\n Cannot open file for writing time difference");
  341.               wrtdif=0;
  342.               }
  343.            break;
  344.         case 'n':
  345.         case 'N':
  346.            wrtdif=0;
  347.            break;
  348.         default:
  349.            printf("\n Warning, Line 2 Char %d not a or n -- n assumed.",icol);
  350.            wrtdif=0;
  351.            break;
  352.         }
  353. /*
  354.     the next character specifies how to deal with BUSY or NO ANSWER
  355.     from the modem.  If the next character is R, then parameter retry
  356.     is set to 6, which will retry to dialing sequence up to 6
  357.     additional times; r specifies up to 3 additional times; m is
  358.     manual mode -- ask the operator before each retry and do as many
  359.     as we are told and anything else means no retries at all. Note
  360.     anything else includes a newline, so that older versions of the
  361.     configuration file that did not support retries will result in
  362.     the retry feature being disabled which is consistent with the
  363.     old action.
  364. */
  365.     c=getc(iop);     /*read next character and advance col.  counter*/
  366.     icol++;
  367.     switch (c)
  368.     {
  369.     case EOF:    /* Error, premature EOF */
  370.       printf("%s %s",perr,peof);
  371.       abort();
  372.       break;
  373.     case 'R':
  374.       retry=6;
  375.       break;
  376.     case 'r':
  377.       retry=3;
  378.       break;
  379.     case 'm':
  380.     case 'M':
  381.       retry= -1;    /*signal manual mode */
  382.       break;
  383.     case '\n':
  384.       retry=0;    /*newline means no retries */
  385.       goto ss;    /*bypass skip past newline below */
  386.     default:
  387.       retry=0;    /*anything else also means no retries */
  388.       break;
  389.     }    /* end of switch statement */
  390.         while( ( (c=getc(iop)) != '\n') && (c != EOF) );/*skip rest of line*/
  391.         if(c == EOF)
  392.         {
  393.         printf("%s %s",perr,peof);
  394.         abort();
  395.         }
  396. ss:        /* continue reading next line */
  397. #ifdef IBMPC
  398. /*
  399.         next line contains utcdif as number for hours or symbol for zone
  400.     note that this is not used for SUN since conversion is to UTC
  401.     directly.
  402. */
  403.         c=getc(iop);
  404.         switch (c)
  405.         {
  406.         case 'p':
  407.         case 'P':
  408.            utcdif= -8;
  409.            break;
  410.         case 'm':
  411.         case 'M':
  412.            utcdif= -7;
  413.            break;
  414.         case 'c':
  415.         case 'C':
  416.            utcdif= -6;
  417.            break;
  418.         case 'e':
  419.         case 'E':
  420.            utcdif= -5;
  421.            break;
  422.         case 'z':
  423.         case 'Z':
  424.            utcdif=0;
  425.            break;
  426.         case '-':
  427.         case '+':
  428.         case '0':
  429.         case '1':
  430.         case '2':
  431.         case '3':
  432.         case '4':
  433.         case '5':
  434.         case '6':
  435.         case '7':
  436.         case '8':
  437.         case '9':
  438.            ungetc(c,iop);
  439.            fscanf(iop,"%d",&utcdif);
  440.            break;
  441.         default:
  442.            printf("\n %s -- syntax error in Line 3 -- utc dif.\n",perr);
  443.            abort();
  444.            break;
  445.         }
  446.         while( ( (c=getc(iop) ) != '\n') && (c != EOF));/*skip to next line*/
  447.         if(c == EOF)
  448.         {
  449.         printf("%s %s",perr,peof);
  450.         abort();
  451.         }
  452.         j=fscanf(iop,"%d \n%d",&dsflag,&atflag);
  453.         if(j == EOF)
  454.         {
  455.         printf("%s %s",perr,peof);
  456.         abort();
  457.         }
  458.         if(j != 2)
  459.         {
  460.         printf("%s -- syntax error in AT flag or ds flag \n",perr);
  461.         abort();
  462.         }
  463. #endif
  464. /*
  465.         if the comport is to be specified by its hardware address,
  466.         then the first character on line 2 was 'h' for the IBMPC
  467.     version and '-' for the SUN version -- comport is now -1.
  468.         if so, read the hardware address from the next line.  if comport
  469.         is positive or zero, then next line is ignored.
  470.  
  471.     note that line is read differently depending on which version
  472.     is being generated.  for IBMPC is is read as a hex address
  473.     while for  the SUN it is read as a full path name.
  474.     
  475.     also for the IBMPC the last read was for the AT and ds flags
  476.     and the newline must be skipped at the end.  for the sun
  477.     the last read was for line 2 and the newline at the end
  478.     of line 2 has already been skipped there
  479.  
  480.     also note that the IBMPC version of the code is only compiled
  481.     if the non-BIOS version is being generated.  The bios version
  482.     does not used the hardware address of the port
  483. */
  484.         if(cmport < 0)
  485.         {
  486. #ifdef IBMPC
  487. #ifndef BIOS
  488.         while( ( (c=getc(iop)) != '\n') && (c != EOF) ) ;/*skip to next line*/
  489.         if(c == EOF)
  490.            {
  491.            printf("%s %s",perr,peof);
  492.            printf("\n while trying to read comport hardware address.");
  493.            abort();
  494.            }
  495.         j=fscanf(iop,"\n%x",&cmadr);
  496. #endif
  497. #endif
  498. #ifdef SUN
  499.     j=fscanf(iop,"%s",&dv[0]);
  500. #endif
  501.         if(j < 1)
  502.            {
  503.            printf("\n Syntax error reading comport hardware address.");
  504.            abort();
  505.            }
  506.     }
  507. #ifdef SUN
  508. /*
  509.     now try to open port using path name either read above
  510.     or constructed from template
  511. */
  512.     if(debug != 0) printf("\n port= %s",dv);
  513.     cmport=open(dv, O_RDWR | O_NDELAY);
  514.     if(debug != 0) printf("\n port file descriptor=%d",cmport);
  515.     if(cmport < 0 )
  516.        {
  517.        printf("\n error opening port %s",dv);
  518.        abort();
  519.        }
  520. #endif
  521.         fclose(iop);
  522. }
  523.