xref: /netbsd-src/external/gpl3/gdb/dist/libctf/testsuite/libctf-lookup/enum.c (revision 4b169a6ba595ae283ca507b26b15fdff40495b1c)
1*4b169a6bSchristos #include <ctf-api.h>
2*4b169a6bSchristos #include <stdio.h>
3*4b169a6bSchristos #include <stdlib.h>
4*4b169a6bSchristos 
5*4b169a6bSchristos static int
print_enum(const char * name,int val,void * unused)6*4b169a6bSchristos print_enum (const char *name, int val, void *unused)
7*4b169a6bSchristos {
8*4b169a6bSchristos   printf ("iter test: %s has value %i\n", name, val);
9*4b169a6bSchristos   return 0;
10*4b169a6bSchristos }
11*4b169a6bSchristos 
12*4b169a6bSchristos int
main(int argc,char * argv[])13*4b169a6bSchristos main (int argc, char *argv[])
14*4b169a6bSchristos {
15*4b169a6bSchristos   ctf_dict_t *fp;
16*4b169a6bSchristos   ctf_archive_t *ctf;
17*4b169a6bSchristos   ctf_id_t type;
18*4b169a6bSchristos   const char *name;
19*4b169a6bSchristos   ctf_next_t *i = NULL;
20*4b169a6bSchristos   int val;
21*4b169a6bSchristos   int err;
22*4b169a6bSchristos 
23*4b169a6bSchristos   if (argc != 2)
24*4b169a6bSchristos     {
25*4b169a6bSchristos       fprintf (stderr, "Syntax: %s PROGRAM\n", argv[0]);
26*4b169a6bSchristos       exit(1);
27*4b169a6bSchristos     }
28*4b169a6bSchristos 
29*4b169a6bSchristos   if ((ctf = ctf_open (argv[1], NULL, &err)) == NULL)
30*4b169a6bSchristos     goto open_err;
31*4b169a6bSchristos   if ((fp = ctf_dict_open (ctf, NULL, &err)) == NULL)
32*4b169a6bSchristos     goto open_err;
33*4b169a6bSchristos 
34*4b169a6bSchristos   /* Try getting some enum values by hand.  */
35*4b169a6bSchristos 
36*4b169a6bSchristos   if ((type = ctf_lookup_by_name (fp, "enum e") ) == CTF_ERR)
37*4b169a6bSchristos     goto err;
38*4b169a6bSchristos   if (ctf_enum_value (fp, type, "ENUMSAMPLE_1", &val) < 0)
39*4b169a6bSchristos     goto err;
40*4b169a6bSchristos   printf ("Enum e enumerand ENUMSAMPLE_1 has value %i\n", val);
41*4b169a6bSchristos 
42*4b169a6bSchristos   if ((name = ctf_enum_name (fp, type, 1)) == NULL)
43*4b169a6bSchristos     goto err;
44*4b169a6bSchristos   printf ("Enum e enumerand %s has value 1\n", name);
45*4b169a6bSchristos 
46*4b169a6bSchristos   /* Try getting some values using both sorts of iterator.  */
47*4b169a6bSchristos 
48*4b169a6bSchristos   if ((type = ctf_lookup_by_name (fp, "enum ie") ) == CTF_ERR)
49*4b169a6bSchristos     goto err;
50*4b169a6bSchristos 
51*4b169a6bSchristos   if ((ctf_enum_iter (fp, type, print_enum, NULL)) < 0)
52*4b169a6bSchristos     goto ierr;
53*4b169a6bSchristos 
54*4b169a6bSchristos   while ((name = ctf_enum_next (fp, type, &i, &val)) != NULL)
55*4b169a6bSchristos     {
56*4b169a6bSchristos       printf ("next test: %s has value %i\n", name, val);
57*4b169a6bSchristos     }
58*4b169a6bSchristos   if (ctf_errno (fp) != ECTF_NEXT_END)
59*4b169a6bSchristos     goto nerr;
60*4b169a6bSchristos 
61*4b169a6bSchristos   ctf_dict_close (fp);
62*4b169a6bSchristos   ctf_close (ctf);
63*4b169a6bSchristos 
64*4b169a6bSchristos   return 0;
65*4b169a6bSchristos 
66*4b169a6bSchristos  open_err:
67*4b169a6bSchristos   fprintf (stderr, "%s: cannot open: %s\n", argv[0], ctf_errmsg (err));
68*4b169a6bSchristos   return 1;
69*4b169a6bSchristos  err:
70*4b169a6bSchristos   fprintf (stderr, "Lookup failed: %s\n", ctf_errmsg (ctf_errno (fp)));
71*4b169a6bSchristos   return 1;
72*4b169a6bSchristos  ierr:
73*4b169a6bSchristos   fprintf (stderr, "_iter iteration failed: %s\n", ctf_errmsg (ctf_errno (fp)));
74*4b169a6bSchristos   return 1;
75*4b169a6bSchristos  nerr:
76*4b169a6bSchristos   fprintf (stderr, "_next iteration failed: %s\n", ctf_errmsg (ctf_errno (fp)));
77*4b169a6bSchristos   return 1;
78*4b169a6bSchristos }
79