From: Bruce Korb Date: Wed, 12 Apr 2006 01:05:00 +0000 (-0700) Subject: AutoOpt the clktest program. X-Git-Tag: NTP_4_2_3~10 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=06d633b091dfec24ab28811b34d9fc639c57fc3b;p=thirdparty%2Fntp.git AutoOpt the clktest program. Move copyright, version and config file stuff into a common file. bk: 443c523cCitlkE1q1NBD-TtUc8cZnQ --- diff --git a/clockstuff/clktest-opts.def b/clockstuff/clktest-opts.def new file mode 100644 index 000000000..9b9f99dd0 --- /dev/null +++ b/clockstuff/clktest-opts.def @@ -0,0 +1,228 @@ +/* -*- Mode: Text -*- */ + +autogen definitions options; + +/* + * $Id: opts.def,v 3.34 2004/10/15 01:48:34 bkorb Exp $ + * + * NTP copyright 1970-2004 Someone + * + * NTP is free software. + * You may redistribute it and/or modify it under the terms of the + * GNU General Public License, as published by the Free Software + * Foundation; either version 2, or (at your option) any later version. + * + * AutoGen is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with AutoGen. See the file "COPYING". If not, + * write to: The Free Software Foundation, Inc., + * 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +prog-name = "clktest"; +prog-title = "test the clock line discipline"; +argument = ''; + +#include copyright.def + +detail = <<- _EODetail_ + Clktest can be used to test the clock line discipline (CLKLDISC, + it must be available), and to take a look at radio clocks attached + to a serial port. + _EODetail_; + +test-main; + +export = <<- _EOExport_ + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + + #include "../include/ntp_fp.h" + #include "../include/ntp.h" + #include "../include/ntp_unixtime.h" + + #ifdef CLKLDISC + # define DEFMAGIC '\r' + #endif + + #ifdef CLKLDISC + # ifdef STREAM + # include + # ifdef HAVE_SYS_CLKDEFS_H + # include + # endif + # undef DEFMAGIC + # define DEFMAGIC "\r" + # endif + #endif + _EOExport_; + +flag = { + name = magic2; + value = a; + arg-type = number; + arg-name = kill-code; + arg-range = '0->255'; + descrip = "The 'kill' character (default: '\\r')"; + ifdef = CLKLDISC; + doc = <<- _EndOfDoc_ + "CLKLDISC" is actually never defined, or you will have compile issues. + _EndOfDoc_; +}; + +flag = { + name = magic1; + value = c; + arg-type = number; + arg-name = erase-code; + arg-range = '0->255'; + descrip = "The 'erase' character (default: '\\r')"; + ifdef = CLKLDISC; + doc = <<- _EndOfDoc_ + Or maybe the kill or maybe something else. + _EndOfDoc_; +}; + +extern = <<- _EOExtern_ + #ifdef STREAM + extern char magic[32]; + #endif + _EOExtern_; + +include = <<- _EOInclude_ + #ifdef STREAM + char magic[32] = ""; + #endif + _EOInclude_; + +flag = { + name = magic1; + value = c; + arg-type = string; + arg-name = erase-code; + descrip = "Magic characters for streams"; + ifdef = STREAM; + doc = <<- _EndOfDoc_ + Or maybe the kill or maybe something else. + _EndOfDoc_; + + flag-code = <<- _EOCode_ + unsigned long tmp; + + if (!atouint(pOptDesc->pzLastArg, &tmp)) { + fprintf(stderr, "%s error: not a number: ``%s''\n", + pOptions->pzProgName, pOptDesc->pzLastArg ); + USAGE( EXIT_FAILURE ); + } + _EOCode_; +}; + +include = 'extern int speed;'; +flag = { + name = speed; + value = b; + arg-type = string; + arg-name = bps; + descrip = "Set the rate according to the speed"; + doc = <<- _EndOfDoc_ + Be careful with this option. It will cause an infinite loop, unless + all speeds are unsupported. + _EndOfDoc_; + + flag-code = <<- _EOCode_ + /* + * speed table + */ + static struct speeds { + const unsigned int bps; + const unsigned int rate; + } *spd, speedtab[] = { + { 300, B300 }, + { 1200, B1200 }, + { 2400, B2400 }, + { 4800, B4800 }, + { 9600, B9600 }, + { 19200, EXTA }, + { 38400, EXTB }, + { 0, 0 } + }; + unsigned long tmp; + + if (!atouint(pOptDesc->pzLastArg, &tmp)) { + fprintf(stderr, "%s error: not a number: ``%s''\n", + pOptions->pzProgName, pOptDesc->pzLastArg ); + USAGE( EXIT_FAILURE ); + } + + for (spd = speedtab;;) { + if (tmp == spd->bps) + break; + spd++; + if (spd->bps == 0) { + fprintf(stderr, "%s: speed %lu is unsupported\n", + pOptions->pzProgName, tmp); + USAGE( EXIT_FAILURE ); + } + } + + speed = spd->rate; + _EOCode_; +}; + + +flag = { + name = debug; + value = d; + descrip = "Turn on debugging output"; + doc = <<- _EndOfDoc_ + This option is not really used. + _EndOfDoc_; +}; + +include = 'extern int ttflags;'; +flag = { + name = crmod; + value = f; + descrip = "Turn on CRMOD flag"; + flag-code = ' ttflags |= CRMOD;'; + doc = <<- _EndOfDoc_ + + _EndOfDoc_; +}; + +flag = { + name = command; + value = s; + arg-type = string; + arg-name = cmd-name; + descrip = ""; + flags-must= timeout; + doc = <<- _EndOfDoc_ + + _EndOfDoc_; +}; + +flag = { + name = timeout; + value = t; + arg-type = number; + arg-name = seconds; + arg-range = '1->600'; /* one second to ten minutes */ + descrip = "Limit wait for command completion"; + doc = <<- _EndOfDoc_ + + _EndOfDoc_; +}; diff --git a/clockstuff/clktest.c b/clockstuff/clktest.c index 86edf59d5..04df4b9bb 100644 --- a/clockstuff/clktest.c +++ b/clockstuff/clktest.c @@ -4,20 +4,7 @@ * usage: clktest -b bps -f -t timeo -s cmd -c char1 -a char2 /dev/whatever */ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "../include/ntp_fp.h" -#include "../include/ntp.h" -#include "../include/ntp_unixtime.h" +#include "clktest-opts.h" #define STREQ(a, b) (*(a) == *(b) && strcmp((a), (b)) == 0) @@ -28,9 +15,13 @@ #endif #ifndef STREAM -#ifndef CLKLDISC -CLOCK_LINE_DISCIPLINE_NEEDED_BY_THIS_PROGRAM; -#endif +# ifndef CLKLDISC + CLOCK_LINE_DISCIPLINE_NEEDED_BY_THIS_PROGRAM; +# endif +#else +# ifdef CLKLDISC + ONLY_ONE_CLOCK_LINE_DISCIPLINE_FOR_THIS_PROGRAM; +# endif #endif /* @@ -38,51 +29,17 @@ CLOCK_LINE_DISCIPLINE_NEEDED_BY_THIS_PROGRAM; */ #define BLOCKSIGMASK (sigmask(SIGIO)|sigmask(SIGALRM)) -/* - * speed table - */ -struct speeds { - int bps; - int rate; -} speedtab[] = { - { 300, B300 }, - { 1200, B1200 }, - { 2400, B2400 }, - { 4800, B4800 }, - { 9600, B9600 }, - { 19200, EXTA }, - { 38400, EXTB }, - { 0, 0 } -}; - -char *progname; -int debug; - -#ifdef CLKLDISC -#define DEFMAGIC '\r' -#endif +#define progname clktestOptions.pzProgName -#ifdef CLKLDISC -# ifdef STREAM -# include -# ifdef HAVE_SYS_CLKDEFS_H -# include -# endif -# define DEFMAGIC "\r" -# endif -#endif - struct timeval timeout = { 0 }; char *cmd = NULL; int cmdlen; -int docmd = 0; + #ifdef CLKLDISC u_long magic1 = DEFMAGIC; u_long magic2 = DEFMAGIC; #endif -#ifdef STREAM -char magic[32]; -#endif + int speed = B9600; int ttflags = RAW|EVENP|ODDP; @@ -95,6 +52,9 @@ extern u_long ustotslo[]; extern u_long ustotsmid[]; extern u_long ustotshi[]; +int alarming(); +int ioready(); + /* * main - parse arguments and handle options */ @@ -104,113 +64,35 @@ main( char *argv[] ) { - int c; - int errflg = 0; - struct speeds *spd; - u_long tmp; int fd; struct sgttyb ttyb; struct itimerval itimer; - extern int ntp_optind; - extern char *ntp_optarg; - int alarming(); - int ioready(); - progname = argv[0]; #ifdef STREAM magic[0] = 0; #endif - while ((c = ntp_getopt(argc, argv, "a:b:c:dfs:t:")) != EOF) - switch (c) { -#ifdef CLKLDISC - case 'a': -#endif - case 'c': - if (!atouint(ntp_optarg, &tmp)) { - (void) fprintf(stderr, - "%s: argument for -%c must be integer\n", - progname, c); - errflg++; - break; - } -#ifdef CLKLDISC - if (c == 'c') - magic1 = tmp; - else - magic2 = tmp; -#endif -#ifdef STREAM - magic[strlen(magic)+1] = '\0'; - magic[strlen(magic)] = tmp; -#endif - break; - case 'b': - if (!atouint(ntp_optarg, &tmp)) { - errflg++; - break; - } - spd = speedtab; - while (spd->bps != 0) - if ((int)tmp == spd->bps) - break; - if (spd->bps == 0) { - (void) fprintf(stderr, - "%s: speed %lu is unsupported\n", - progname, tmp); - errflg++; - } else { - speed = spd->rate; - } - break; - case 'd': - ++debug; - break; - case 'f': - ttflags |= CRMOD; - break; - case 's': - cmdlen = strlen(ntp_optarg); - if (cmdlen == 0) - errflg++; - else - cmd = ntp_optarg; - break; - case 't': - if (!atouint(ntp_optarg, &tmp)) - errflg++; - else { - timeout.tv_sec = (long)tmp; - docmd = 1; - } - break; - default: - errflg++; - break; + + { + int ct = optionProcess( &clktestOptions, argc, argv ); + if (HAVE_OPT(COMMAND) && (strlen(OPT_ARG(COMMAND)) == 0)) { + fputs( "The command option string must not be empty\n", stderr ); + USAGE( EXIT_FAILURE ); } - if (errflg || ntp_optind+1 != argc) { - (void) fprintf(stderr, -#ifdef CLKLDISC - "usage: %s [-b bps] [-c magic1] [-a magic2] [-f] [-s cmd] [-t timeo] tty_device\n", -#endif -#ifdef STREAM - "usage: %s [-b bps] [-c magic1] [-c magic2]... [-f] [-s cmd] [-t timeo] tty_device\n", -#endif - progname); - exit(2); - } + if ((argc -= ct) != 1) { + fputs( "Missing tty device name\n", stderr ); + USAGE( EXIT_FAILURE ); + } + argv += ct; + } #ifdef STREAM if (!strlen(magic)) strcpy(magic,DEFMAGIC); #endif - if (docmd) - fd = open(argv[ntp_optind], O_RDWR, 0777); - else - fd = open(argv[ntp_optind], O_RDONLY, 0777); + fd = open(*argv, HAVE_OPT(TIMEOUT) ? O_RDWR : O_RDONLY, 0777); if (fd == -1) { - (void) fprintf(stderr, "%s: open(%s): ", progname, - argv[ntp_optind]); + fprintf(stderr, "%s: open(%s): ", progname, *argv); perror(""); exit(1); } @@ -270,7 +152,7 @@ main( (void) gettimeofday(&lasttv, (struct timezone *)0); - if (docmd) { + if (HAVE_OPT(TIMEOUT)) { /* * set non-blocking, async I/O on the descriptor */ @@ -288,6 +170,7 @@ main( */ wasalarmed = 0; (void) signal(SIGALRM, alarming); + timeout.tv_sec = OPT_VALUE_TIMEOUT; itimer.it_interval = itimer.it_value = timeout; setitimer(ITIMER_REAL, &itimer, (struct itimerval *)0); doboth(fd); @@ -495,7 +378,7 @@ doalarm( { int n; - if (cmd == NULL || cmdlen <= 0) + if (! HAVE_OPT(COMMAND)) return; n = write(fd, cmd, cmdlen); diff --git a/include/copyright.def b/include/copyright.def new file mode 100644 index 000000000..cac5f53b2 --- /dev/null +++ b/include/copyright.def @@ -0,0 +1,24 @@ +/* -*- Mode: Text -*- */ + +copyright = { + date = "1970-2004"; + owner = "ntp.org"; + eaddr = "http://bugs.ntp.isc.org, bugs@ntp.org"; + type = bsd; +}; + +#ifndef __windows__ +rcfile = ".ntprc"; +#else +rcfile = "ntp.ini"; +#endif + +homerc = $HOME, "."; +long-opts; +config-header = config.h; +environrc; + +version = ` +eval VERSION=\`sed -e 's/.*,\\[//' -e 's/\\].*//' < ../version.m4\` +[ -z "${VERSION}" ] && echo "Cannot determine VERSION" && kill -TERM $AG_pid +echo $VERSION`; diff --git a/ntpdc/ntpdc-opts.def b/ntpdc/ntpdc-opts.def index 88b7065fd..49985c380 100644 --- a/ntpdc/ntpdc-opts.def +++ b/ntpdc/ntpdc-opts.def @@ -24,33 +24,10 @@ autogen definitions options; * Boston, MA 02111-1307, USA. */ -copyright = { - date = "1970-2004"; - owner = "ntp.org"; - eaddr = "http://bugs.ntp.isc.org, bugs@ntp.org"; - type = bsd; -}; - - prog-name = "ntpdc"; prog-title = "vendor-specific NTP query program"; -homerc = $HOME, "."; -long-opts; -config-header = config.h; -argument = '[ host ...]'; - -#ifndef __windows__ -rcfile = ".ntprc"; -#else -rcfile = "ntp.ini"; -#endif - -environrc; - -version = ` -eval VERSION=\`sed -e 's/.*,\\[//' -e 's/\\].*//' < ../version.m4\` -[ -z "${VERSION}" ] && echo "Cannot determine VERSION" && kill -TERM $AG_pid -echo $VERSION`; + +#include copyright.def test-main;