# BRCM_VERSION=3
[bcm963xx.git] / userapps / opensource / net-snmp / apps / snmptranslate.c
1 /*
2  * snmptranslate.c - report or translate info about oid from mibs
3  *
4  * Update: 1998-07-17 <jhy@gsu.edu>
5  * Added support for dumping alternate oid reports (-t and -T options).
6  * Added more detailed (useful?) usage info.
7  */
8 /************************************************************************
9         Copyright 1988, 1989, 1991, 1992 by Carnegie Mellon University
10
11                       All Rights Reserved
12
13 Permission to use, copy, modify, and distribute this software and its 
14 documentation for any purpose and without fee is hereby granted, 
15 provided that the above copyright notice appear in all copies and that
16 both that copyright notice and this permission notice appear in 
17 supporting documentation, and that the name of CMU not be
18 used in advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.  
20
21 CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
22 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
23 CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
24 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
25 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
26 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
27 SOFTWARE.
28 ******************************************************************/
29
30
31 #include <net-snmp/net-snmp-config.h>
32
33 #if HAVE_STDLIB_H
34 #include <stdlib.h>
35 #endif
36 #if HAVE_UNISTD_H
37 #include <unistd.h>
38 #endif
39 #if HAVE_STRING_H
40 #include <string.h>
41 #else
42 #include <strings.h>
43 #endif
44 #include <sys/types.h>
45 #if HAVE_SYS_SELECT_H
46 #include <sys/select.h>
47 #endif
48 #if HAVE_NETINET_IN_H
49 #include <netinet/in.h>
50 #endif
51 #include <stdio.h>
52 #include <ctype.h>
53 #include <net-snmp/utilities.h>
54
55 #if HAVE_WINSOCK_H
56 #include <winsock.h>
57 #endif
58
59 #include <net-snmp/config_api.h>
60 #include <net-snmp/output_api.h>
61 #include <net-snmp/mib_api.h>
62
63 int             show_all_matched_objects(FILE *, const char *, oid *,
64                                          size_t *, int, int);
65
66 void
67 usage(void)
68 {
69     fprintf(stderr, "USAGE: snmptranslate [OPTIONS] OID [OID]...\n\n");
70     fprintf(stderr, "  Version:  %s\n", netsnmp_get_version());
71     fprintf(stderr, "  Web:      http://www.net-snmp.org/\n");
72     fprintf(stderr,
73             "  Email:    net-snmp-coders@lists.sourceforge.net\n\nOPTIONS:\n");
74
75     fprintf(stderr, "  -h\t\t\tdisplay this help message\n");
76     fprintf(stderr, "  -V\t\t\tdisplay package version number\n");
77     fprintf(stderr,
78             "  -m MIB[:...]\t\tload given list of MIBs (ALL loads everything)\n");
79     fprintf(stderr,
80             "  -M DIR[:...]\t\tlook in given list of directories for MIBs\n");
81     fprintf(stderr,
82             "  -D TOKEN[,...]\tturn on debugging output for the specified TOKENs\n\t\t\t   (ALL gives extremely verbose debugging output)\n");
83     fprintf(stderr, "  -w WIDTH\t\tset width of tree and detail output\n");
84     fprintf(stderr,
85             "  -T TRANSOPTS\t\tSet various options controlling report produced:\n");
86     fprintf(stderr,
87             "\t\t\t  B:  print all matching objects for a regex search\n");
88     fprintf(stderr, "\t\t\t  d:  print full details of the given OID\n");
89     fprintf(stderr, "\t\t\t  p:  print tree format symbol table\n");
90     fprintf(stderr, "\t\t\t  a:  print ASCII format symbol table\n");
91     fprintf(stderr, "\t\t\t  l:  enable labeled OID report\n");
92     fprintf(stderr, "\t\t\t  o:  enable OID report\n");
93     fprintf(stderr, "\t\t\t  s:  enable dotted symbolic report\n");
94     fprintf(stderr,
95             "\t\t\t  t:  enable alternate format symbolic suffix report\n");
96     fprintf(stderr,
97             "  -P MIBOPTS\t\tToggle various defaults controlling mib parsing:\n");
98     snmp_mib_toggle_options_usage("\t\t\t  ", stderr);
99     fprintf(stderr,
100             "  -O OUTOPTS\t\tToggle various defaults controlling output display:\n");
101     snmp_out_toggle_options_usage("\t\t\t  ", stderr);
102     fprintf(stderr,
103             "  -I INOPTS\t\tToggle various defaults controlling input parsing:\n");
104     snmp_in_toggle_options_usage("\t\t\t  ", stderr);
105     exit(1);
106 }
107
108 int
109 main(int argc, char *argv[])
110 {
111     int             arg;
112     char           *current_name = NULL, *cp = NULL;
113     oid             name[MAX_OID_LEN];
114     size_t          name_length;
115     int             description = 0;
116     int             print = 0;
117     int             find_all = 0;
118     int             width = 1000000;
119
120     /*
121      * usage: snmptranslate name
122      */
123     while ((arg = getopt(argc, argv, "Vhm:M:w:D:P:T:O:I:")) != EOF) {
124         switch (arg) {
125         case 'h':
126             usage();
127             exit(1);
128
129         case 'm':
130             setenv("MIBS", optarg, 1);
131             break;
132         case 'M':
133             setenv("MIBDIRS", optarg, 1);
134             break;
135         case 'D':
136             debug_register_tokens(optarg);
137             snmp_set_do_debugging(1);
138             break;
139         case 'V':
140             fprintf(stderr, "NET-SNMP version: %s\n",
141                     netsnmp_get_version());
142             exit(0);
143             break;
144         case 'w':
145             width = atoi(optarg);
146             if (width <= 0) {
147                 fprintf(stderr, "Invalid width specification: %s\n", optarg);
148                 exit (1);
149             }
150             break;
151         case 'P':
152             cp = snmp_mib_toggle_options(optarg);
153             if (cp != NULL) {
154                 fprintf(stderr, "Unknown parser option to -P: %c.\n", *cp);
155                 usage();
156                 exit(1);
157             }
158             break;
159         case 'O':
160             cp = snmp_out_toggle_options(optarg);
161             if (cp != NULL) {
162                 fprintf(stderr, "Unknown OID option to -O: %c.\n", *cp);
163                 usage();
164                 exit(1);
165             }
166             break;
167         case 'I':
168             cp = snmp_in_toggle_options(optarg);
169             if (cp != NULL) {
170                 fprintf(stderr, "Unknown OID option to -I: %c.\n", *cp);
171                 usage();
172                 exit(1);
173             }
174             break;
175         case 'T':
176             for (cp = optarg; *cp; cp++) {
177                 switch (*cp) {
178                 case 'l':
179                     print = 3;
180                     print_oid_report_enable_labeledoid();
181                     break;
182                 case 'o':
183                     print = 3;
184                     print_oid_report_enable_oid();
185                     break;
186                 case 's':
187                     print = 3;
188                     print_oid_report_enable_symbolic();
189                     break;
190                 case 't':
191                     print = 3;
192                     print_oid_report_enable_suffix();
193                     break;
194                 case 'd':
195                     description = 1;
196                     snmp_set_save_descriptions(1);
197                     break;
198                 case 'B':
199                     find_all = 1;
200                     break;
201                 case 'p':
202                     print = 1;
203                     break;
204                 case 'a':
205                     print = 2;
206                     break;
207                 default:
208                     fprintf(stderr, "Invalid -T<lostpad> character: %c\n",
209                             *cp);
210                     usage();
211                     exit(1);
212                     break;
213                 }
214             }
215             break;
216         default:
217             fprintf(stderr, "invalid option: -%c\n", arg);
218             usage();
219             exit(1);
220             break;
221         }
222     }
223
224     init_snmp("snmpapp");
225     if (optind < argc)
226         current_name = argv[optind];
227
228     if (current_name == NULL) {
229         switch (print) {
230         default:
231             usage();
232             exit(1);
233         case 1:
234             print_mib_tree(stdout, get_tree_head(), width);
235             break;
236         case 2:
237             print_ascii_dump(stdout);
238             break;
239         case 3:
240             print_oid_report(stdout);
241             break;
242         }
243         exit(0);
244     }
245
246     do {
247         name_length = MAX_OID_LEN;
248         if (snmp_get_random_access()) {
249             if (!get_node(current_name, name, &name_length)) {
250                 fprintf(stderr, "Unknown object identifier: %s\n",
251                         current_name);
252                 exit(2);
253             }
254         } else if (find_all) {
255             if (0 == show_all_matched_objects(stdout, current_name,
256                                               name, &name_length,
257                                               description, width)) {
258                 fprintf(stderr,
259                         "Unable to find a matching object identifier for \"%s\"\n",
260                         current_name);
261                 exit(1);
262             }
263             exit(0);
264         } else if (netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, 
265                                           NETSNMP_DS_LIB_REGEX_ACCESS)) {
266             if (0 == get_wild_node(current_name, name, &name_length)) {
267                 fprintf(stderr,
268                         "Unable to find a matching object identifier for \"%s\"\n",
269                         current_name);
270                 exit(1);
271             }
272         } else {
273             if (!read_objid(current_name, name, &name_length)) {
274                 snmp_perror(current_name);
275                 exit(2);
276             }
277         }
278
279
280         if (print == 1) {
281             struct tree    *tp;
282             tp = get_tree(name, name_length, get_tree_head());
283             if (tp == NULL) {
284                 snmp_log(LOG_ERR,
285                         "Unable to find a matching object identifier for \"%s\"\n",
286                         current_name);
287                 exit(1);
288             }
289             print_mib_tree(stdout, tp, width);
290         } else {
291             print_objid(name, name_length);
292             if (description) {
293                 print_description(name, name_length, width);
294             }
295         }
296         current_name = argv[++optind];
297         if (current_name != NULL)
298             printf("\n");
299     } while (optind < argc);
300
301     return (0);
302 }
303
304 /*
305  * Show all object identifiers that match the pattern.
306  */
307
308 int
309 show_all_matched_objects(FILE * fp, const char *patmatch, oid * name, size_t * name_length, int f_desc, /* non-zero if descriptions should be shown */
310                          int width)
311 {
312     int             result, count = 0;
313     size_t          savlen = *name_length;
314     clear_tree_flags(get_tree_head());
315
316     while (1) {
317         *name_length = savlen;
318         result = get_wild_node(patmatch, name, name_length);
319         if (!result)
320             break;
321         count++;
322
323         fprint_objid(fp, name, *name_length);
324         if (f_desc)
325             fprint_description(fp, name, *name_length, width);
326     }
327
328     return (count);
329 }