On Sat, Jul 18, 2015 at 07:14:27PM +0200, Martin Pieuchot wrote:
> This applies phessler@'s logic to *all* the commands supporting the
> "-priority" switch. Plus some KNF.
>
> ok?
OK claudio@
> Index: route.c
> ===================================================================
> RCS file: /cvs/src/sbin/route/route.c,v
> retrieving revision 1.175
> diff -u -p -r1.175 route.c
> --- route.c 18 Jul 2015 00:05:02 -0000 1.175
> +++ route.c 18 Jul 2015 17:12:39 -0000
> @@ -85,7 +85,8 @@ void monitor(int, char *[]);
> int prefixlen(char *);
> void sockaddr(char *, struct sockaddr *);
> void sodump(sup, char *);
> -char *priorityname(u_int8_t);
> +char *priorityname(uint8_t);
> +uint8_t getpriority(char *);
> void print_getmsg(struct rt_msghdr *, int);
> const char *get_linkstate(int, int);
> void print_rtmsg(struct rt_msghdr *, int);
> @@ -96,7 +97,7 @@ void mask_addr(union sockunion *, union
> int inet6_makenetandmask(struct sockaddr_in6 *, char *);
> int getaddr(int, char *, struct hostent **);
> void getmplslabel(char *, int);
> -int rtmsg(int, int, int, u_char);
> +int rtmsg(int, int, int, uint8_t);
> __dead void usage(char *);
> void set_metric(char *, int);
> void inet_makenetandmask(u_int32_t, struct sockaddr_in *, int);
> @@ -230,13 +231,12 @@ main(int argc, char **argv)
> void
> flushroutes(int argc, char **argv)
> {
> - const char *errstr;
> size_t needed;
> int mib[7], rlen, seqno;
> char *buf = NULL, *next, *lim = NULL;
> struct rt_msghdr *rtm;
> struct sockaddr *sa;
> - u_char prio = 0;
> + uint8_t prio = 0;
> unsigned int ifindex = 0;
>
> if (uid)
> @@ -268,10 +268,7 @@ flushroutes(int argc, char **argv)
> case K_PRIORITY:
> if (!--argc)
> usage(1+*argv);
> - prio = strtonum(*++argv, 0, RTP_MAX, &errstr);
> - if (errstr)
> - errx(1, "priority is %s: %s", errstr,
> - *argv);
> + prio = getpriority(*++argv);
> break;
> default:
> usage(*argv);
> @@ -403,12 +400,11 @@ set_metric(char *value, int key)
> int
> newroute(int argc, char **argv)
> {
> - const char *errstr;
> char *cmd, *dest = "", *gateway = "", *error;
> int ishost = 0, ret = 0, attempts, oerrno, flags = RTF_STATIC;
> int fmask = 0;
> int key;
> - u_char prio = 0;
> + uint8_t prio = 0;
> struct hostent *hp = NULL;
>
> if (uid)
> @@ -581,10 +577,7 @@ newroute(int argc, char **argv)
> case K_PRIORITY:
> if (!--argc)
> usage(1+*argv);
> - prio = strtonum(*++argv, 0, RTP_MAX, &errstr);
> - if (errstr)
> - errx(1, "priority is %s: %s", errstr,
> - *argv);
> + prio = getpriority(*++argv);
> break;
> default:
> usage(1+*argv);
> @@ -663,8 +656,6 @@ show(int argc, char *argv[])
> {
> int af = 0;
> char prio = 0;
> - char *priostr;
> - const char *errstr;
>
> while (--argc > 0) {
> if (**(++argv)== '-')
> @@ -692,33 +683,7 @@ show(int argc, char *argv[])
> case K_PRIORITY:
> if (!--argc)
> usage(1+*argv);
> - priostr = *++argv;
> - switch (keyword(priostr)) {
> - case K_LOCAL:
> - prio = RTP_LOCAL;
> - break;
> - case K_CONNECTED:
> - prio = RTP_CONNECTED;
> - break;
> - case K_STATIC:
> - prio = RTP_STATIC;
> - break;
> - case K_OSPF:
> - prio = RTP_OSPF;
> - break;
> - case K_RIP:
> - prio = RTP_RIP;
> - break;
> - case K_BGP:
> - prio = RTP_BGP;
> - break;
> - default:
> - prio = strtonum(priostr, -RTP_MAX,
> RTP_MAX,
> - &errstr);
> - if (errstr)
> - errx(1, "priority is %s: %s",
> - errstr, *argv);
> - }
> + prio = getpriority(*++argv);
> break;
> default:
> usage(*argv);
> @@ -1146,7 +1111,7 @@ struct {
> } m_rtmsg;
>
> int
> -rtmsg(int cmd, int flags, int fmask, u_char prio)
> +rtmsg(int cmd, int flags, int fmask, uint8_t prio)
> {
> static int seq;
> char *cp = m_rtmsg.m_space;
> @@ -1379,7 +1344,7 @@ print_rtmsg(struct rt_msghdr *rtm, int m
> }
>
> char *
> -priorityname(u_int8_t prio)
> +priorityname(uint8_t prio)
> {
> switch (prio) {
> case RTP_NONE:
> @@ -1403,6 +1368,40 @@ priorityname(u_int8_t prio)
> default:
> return ("");
> }
> +}
> +
> +uint8_t
> +getpriority(char *priostr)
> +{
> + const char *errstr;
> + uint8_t prio;
> +
> + switch (keyword(priostr)) {
> + case K_LOCAL:
> + prio = RTP_LOCAL;
> + break;
> + case K_CONNECTED:
> + prio = RTP_CONNECTED;
> + break;
> + case K_STATIC:
> + prio = RTP_STATIC;
> + break;
> + case K_OSPF:
> + prio = RTP_OSPF;
> + break;
> + case K_RIP:
> + prio = RTP_RIP;
> + break;
> + case K_BGP:
> + prio = RTP_BGP;
> + break;
> + default:
> + prio = strtonum(priostr, -RTP_MAX, RTP_MAX, &errstr);
> + if (errstr)
> + errx(1, "priority is %s: %s", errstr, priostr);
> + }
> +
> + return (prio);
> }
>
> void
>
--
:wq Claudio