Deleted Added
full compact
main.c (132493) main.c (133565)
1/*
2 * Copyright (c) 2001-2003
3 * Fraunhofer Institute for Open Communication Systems (FhG Fokus).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:

--- 13 unchanged lines hidden (view full) ---

22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 * Author: Hartmut Brandt <harti@freebsd.org>
28 */
29#include <sys/cdefs.h>
1/*
2 * Copyright (c) 2001-2003
3 * Fraunhofer Institute for Open Communication Systems (FhG Fokus).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:

--- 13 unchanged lines hidden (view full) ---

22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 * Author: Hartmut Brandt <harti@freebsd.org>
28 */
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD: head/sbin/atm/atmconfig/main.c 132493 2004-07-21 09:42:56Z harti $");
30__FBSDID("$FreeBSD: head/sbin/atm/atmconfig/main.c 133565 2004-08-12 12:31:43Z harti $");
31
32#include <sys/types.h>
33#include <sys/sysctl.h>
34#include <netdb.h>
35#include <stdarg.h>
36#include <ctype.h>
37#include <limits.h>
38#include <stdint.h>
39#include <fnmatch.h>
40#include <dirent.h>
31
32#include <sys/types.h>
33#include <sys/sysctl.h>
34#include <netdb.h>
35#include <stdarg.h>
36#include <ctype.h>
37#include <limits.h>
38#include <stdint.h>
39#include <fnmatch.h>
40#include <dirent.h>
41#ifndef RESCUE
42#include <bsnmp/asn1.h>
43#include <bsnmp/snmp.h>
44#include <bsnmp/snmpclient.h>
45#endif
46
41#include "atmconfig.h"
42#include "private.h"
43
44/* verbosity level */
45int verbose;
46
47/* notitle option */
48static int notitle;
49
50/* need to put heading before next output */
51static int need_heading;
52
53/*
54 * TOP LEVEL commands
55 */
56static void help_func(int argc, char *argv[]) __dead2;
57
47#include "atmconfig.h"
48#include "private.h"
49
50/* verbosity level */
51int verbose;
52
53/* notitle option */
54static int notitle;
55
56/* need to put heading before next output */
57static int need_heading;
58
59/*
60 * TOP LEVEL commands
61 */
62static void help_func(int argc, char *argv[]) __dead2;
63
58static const struct cmdtab main_tab[] = {
64static const struct cmdtab static_main_tab[] = {
59 { "help", NULL, help_func },
60 { "options", NULL, NULL },
61 { "commands", NULL, NULL },
62 { "diag", diag_tab, NULL },
63 { "natm", natm_tab, NULL },
64 { NULL, NULL, NULL }
65};
66
65 { "help", NULL, help_func },
66 { "options", NULL, NULL },
67 { "commands", NULL, NULL },
68 { "diag", diag_tab, NULL },
69 { "natm", natm_tab, NULL },
70 { NULL, NULL, NULL }
71};
72
73static struct cmdtab *main_tab = NULL;
74static size_t main_tab_size = sizeof(static_main_tab) /
75 sizeof(static_main_tab[0]);
76
67static int
68substr(const char *s1, const char *s2)
69{
70 return (strlen(s1) <= strlen(s2) && strncmp(s1, s2, strlen(s1)) == 0);
71}
72
73/*
74 * Current help file state

--- 354 unchanged lines hidden (view full) ---

429 }
430 printf(" ].");
431 if (!has_sub_topics)
432 printf(" No sub-topics found.");
433 printf("\n");
434 exit(1);
435}
436
77static int
78substr(const char *s1, const char *s2)
79{
80 return (strlen(s1) <= strlen(s2) && strncmp(s1, s2, strlen(s1)) == 0);
81}
82
83/*
84 * Current help file state

--- 354 unchanged lines hidden (view full) ---

439 }
440 printf(" ].");
441 if (!has_sub_topics)
442 printf(" No sub-topics found.");
443 printf("\n");
444 exit(1);
445}
446
447#ifndef RESCUE
448/*
449 * Parse a server specification
450 *
451 * syntax is [trans::][community@][server][:port]
452 */
453static void
454parse_server(char *name)
455{
456 char *p, *s = name;
457
458 /* look for a double colon */
459 for (p = s; *p != '\0'; p++) {
460 if (*p == '\\' && p[1] != '\0') {
461 p++;
462 continue;
463 }
464 if (*p == ':' && p[1] == ':')
465 break;
466 }
467 if (*p != '\0') {
468 if (p > s) {
469 if (p - s == 3 && strncmp(s, "udp", 3) == 0)
470 snmp_client.trans = SNMP_TRANS_UDP;
471 else if (p - s == 6 && strncmp(s, "stream", 6) == 0)
472 snmp_client.trans = SNMP_TRANS_LOC_STREAM;
473 else if (p - s == 5 && strncmp(s, "dgram", 5) == 0)
474 snmp_client.trans = SNMP_TRANS_LOC_DGRAM;
475 else
476 errx(1, "unknown SNMP transport '%.*s'",
477 (int)(p - s), s);
478 }
479 s = p + 2;
480 }
481
482 /* look for a @ */
483 for (p = s; *p != '\0'; p++) {
484 if (*p == '\\' && p[1] != '\0') {
485 p++;
486 continue;
487 }
488 if (*p == '@')
489 break;
490 }
491
492 if (*p != '\0') {
493 if (p - s > SNMP_COMMUNITY_MAXLEN)
494 err(1, "community string too long");
495 strncpy(snmp_client.read_community, s, p - s);
496 snmp_client.read_community[p - s] = '\0';
497 strncpy(snmp_client.write_community, s, p - s);
498 snmp_client.write_community[p - s] = '\0';
499 s = p + 1;
500 }
501
502 /* look for a colon */
503 for (p = s; *p != '\0'; p++) {
504 if (*p == '\\' && p[1] != '\0') {
505 p++;
506 continue;
507 }
508 if (*p == ':')
509 break;
510 }
511
512 if (*p == ':') {
513 if (p > s) {
514 *p = '\0';
515 snmp_client_set_host(&snmp_client, s);
516 *p = ':';
517 }
518 snmp_client_set_port(&snmp_client, p + 1);
519 } else if (p > s)
520 snmp_client_set_host(&snmp_client, s);
521}
522#endif
523
437int
438main(int argc, char *argv[])
439{
440 int opt, i;
441 const struct cmdtab *match, *cc, *tab;
442
524int
525main(int argc, char *argv[])
526{
527 int opt, i;
528 const struct cmdtab *match, *cc, *tab;
529
443 while ((opt = getopt(argc, argv, "htv")) != -1)
530#ifndef RESCUE
531 snmp_client_init(&snmp_client);
532 snmp_client.trans = SNMP_TRANS_LOC_STREAM;
533 snmp_client_set_host(&snmp_client, PATH_ILMI_SOCK);
534#endif
535
536#ifdef RESCUE
537#define OPTSTR "htv"
538#else
539#define OPTSTR "htvs:"
540#endif
541
542 while ((opt = getopt(argc, argv, OPTSTR)) != -1)
444 switch (opt) {
445
446 case 'h':
447 help_func(0, argv);
448
543 switch (opt) {
544
545 case 'h':
546 help_func(0, argv);
547
548#ifndef RESCUE
549 case 's':
550 parse_server(optarg);
551 break;
552#endif
553
449 case 'v':
450 verbose++;
451 break;
452
453 case 't':
454 notitle = 1;
455 break;
456 }
457
458 if (argv[optind] == NULL)
459 help_func(0, argv);
460
461 argc -= optind;
462 argv += optind;
463
554 case 'v':
555 verbose++;
556 break;
557
558 case 't':
559 notitle = 1;
560 break;
561 }
562
563 if (argv[optind] == NULL)
564 help_func(0, argv);
565
566 argc -= optind;
567 argv += optind;
568
569 if ((main_tab = malloc(sizeof(static_main_tab))) == NULL)
570 err(1, NULL);
571 memcpy(main_tab, static_main_tab, sizeof(static_main_tab));
572
573#ifndef RESCUE
574 /* XXX while this is compiled in */
575 device_register();
576#endif
577
464 cc = main_tab;
465 i = 0;
466 for (;;) {
467 /*
468 * Scan the table for a match
469 */
470 tab = cc;
471 match = NULL;

--- 110 unchanged lines hidden (view full) ---

582 strtab++;
583 }
584 warnx("illegal value for enumerated variable '%d'", value);
585 strcpy(buf, "?");
586 return (buf);
587}
588
589/*
578 cc = main_tab;
579 i = 0;
580 for (;;) {
581 /*
582 * Scan the table for a match
583 */
584 tab = cc;
585 match = NULL;

--- 110 unchanged lines hidden (view full) ---

696 strtab++;
697 }
698 warnx("illegal value for enumerated variable '%d'", value);
699 strcpy(buf, "?");
700 return (buf);
701}
702
703/*
704 * And the other way 'round
705 */
706int
707pparse(int32_t *val, const struct penum *tab, const char *str)
708{
709
710 while (tab->str != NULL) {
711 if (strcmp(tab->str, str) == 0) {
712 *val = tab->value;
713 return (0);
714 }
715 tab++;
716 }
717 return (-1);
718}
719
720/*
590 * Parse command line options
591 */
592int
593parse_options(int *pargc, char ***pargv, const struct option *opts)
594{
595 const struct option *o, *m;
596 char *arg;
597 u_long ularg, ularg1;

--- 130 unchanged lines hidden (view full) ---

728 break;
729
730 default:
731 errx(1, "(internal) bad option type %u for '%s'",
732 m->opttype, arg);
733 }
734 return (m - opts);
735}
721 * Parse command line options
722 */
723int
724parse_options(int *pargc, char ***pargv, const struct option *opts)
725{
726 const struct option *o, *m;
727 char *arg;
728 u_long ularg, ularg1;

--- 130 unchanged lines hidden (view full) ---

859 break;
860
861 default:
862 errx(1, "(internal) bad option type %u for '%s'",
863 m->opttype, arg);
864 }
865 return (m - opts);
866}
867
868/*
869 * for compiled-in modules
870 */
871void
872register_module(const struct amodule *mod)
873{
874 main_tab_size++;
875 if ((main_tab = realloc(main_tab, main_tab_size * sizeof(main_tab[0])))
876 == NULL)
877 err(1, NULL);
878 main_tab[main_tab_size - 2] = *mod->cmd;
879 memset(&main_tab[main_tab_size - 1], 0, sizeof(main_tab[0]));
880}