BRL-CAD
list.c
Go to the documentation of this file.
1 /* L I S T . C
2  * BRL-CAD
3  *
4  * Copyright (c) 2008-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 /** @file libged/list.c
21  *
22  * The l command.
23  *
24  */
25 
26 #include "common.h"
27 
28 #include <stdlib.h>
29 #include <string.h>
30 
31 #include "bu/cmd.h"
32 #include "bu/getopt.h"
33 #include "bu/units.h"
34 
35 #include "./ged_private.h"
36 
37 
38 void
39 _ged_do_list(struct ged *gedp, struct directory *dp, int verbose)
40 {
41  int id;
42  struct rt_db_internal intern;
43 
44  RT_CK_DBI(gedp->ged_wdbp->dbip);
45 
46  if (dp->d_major_type == DB5_MAJORTYPE_ATTRIBUTE_ONLY) {
47  /* this is the _GLOBAL object */
48  struct bu_attribute_value_set avs;
49  struct bu_attribute_value_pair *avp;
50 
52  bu_vls_strcat(gedp->ged_result_str, ": global attributes object\n");
53  bu_avs_init_empty(&avs);
54  if (db5_get_attributes(gedp->ged_wdbp->dbip, &avs, dp)) {
55  bu_vls_printf(gedp->ged_result_str, "Cannot get attributes for %s\n", dp->d_namep);
56  return;
57  }
58 /* !!! left off here*/
59  for (BU_AVS_FOR(avp, &avs)) {
60  if (BU_STR_EQUAL(avp->name, "units")) {
61  double conv;
62  const char *str;
63 
64  conv = atof(avp->value);
65  bu_vls_strcat(gedp->ged_result_str, "\tunits: ");
66  if ((str=bu_units_string(conv)) == NULL) {
67  bu_vls_strcat(gedp->ged_result_str, "Unrecognized units\n");
68  } else {
69  bu_vls_strcat(gedp->ged_result_str, str);
70  bu_vls_putc(gedp->ged_result_str, '\n');
71  }
72  } else {
73  bu_vls_putc(gedp->ged_result_str, '\t');
74  bu_vls_strcat(gedp->ged_result_str, avp->name);
75  bu_vls_strcat(gedp->ged_result_str, ": ");
76  bu_vls_strcat(gedp->ged_result_str, avp->value);
77  bu_vls_putc(gedp->ged_result_str, '\n');
78  }
79  }
80  } else {
81 
82  if ((id = rt_db_get_internal(&intern, dp, gedp->ged_wdbp->dbip,
83  (fastf_t *)NULL, &rt_uniresource)) < 0) {
84  bu_vls_printf(gedp->ged_result_str, "rt_db_get_internal(%s) failure\n", dp->d_namep);
85  rt_db_free_internal(&intern);
86  return;
87  }
88 
89  bu_vls_printf(gedp->ged_result_str, "%s: ", dp->d_namep);
90 
91  if (!OBJ[id].ft_describe ||
92  OBJ[id].ft_describe(gedp->ged_result_str,
93  &intern,
94  verbose,
97  gedp->ged_wdbp->dbip) < 0)
98  bu_vls_printf(gedp->ged_result_str, "%s: describe error\n", dp->d_namep);
99  rt_db_free_internal(&intern);
100  }
101 }
102 
103 
104 int
105 ged_list(struct ged *gedp, int argc, const char *argv[])
106 {
107  struct directory *dp;
108  int arg;
109  int id;
110  int c;
111  int recurse = 0;
112  int verbose = 99;
113  char *terse_parm = "-t";
114  char *listeval = "listeval";
115  struct rt_db_internal intern;
116  static const char *usage = "[-r] [-t] <objects>";
117 
119  GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);
120 
121  /* initialize result */
122  bu_vls_trunc(gedp->ged_result_str, 0);
123 
124  /* must be wanting help */
125  if (argc == 1) {
126  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
127  return GED_HELP;
128  }
129 
130  bu_optind = 1; /* re-init bu_getopt() */
131  while ((c = bu_getopt(argc, (char * const *)argv, "rt")) != -1) {
132  switch (c) {
133  case 'r':
134  recurse = 1;
135  break;
136  case 't': /* terse */
137  verbose = 0;
138  break;
139  default:
140  bu_vls_printf(gedp->ged_result_str, "Unrecognized option - %c", c);
141  return GED_ERROR;
142  }
143  }
144 
145  /* skip options processed plus command name */
146  argc -= bu_optind;
147  argv += bu_optind;
148 
149  for (arg = 0; arg < argc; arg++) {
150  if (recurse) {
151  char *tmp_argv[3] = {NULL, NULL, NULL};
152 
153  tmp_argv[0] = listeval;
154  if (verbose) {
155  tmp_argv[1] = (char *)argv[arg];
156  ged_pathsum(gedp, 2, (const char **)tmp_argv);
157  } else {
158  tmp_argv[1] = terse_parm;
159  tmp_argv[2] = (char *)argv[arg];
160  ged_pathsum(gedp, 3, (const char **)tmp_argv);
161  }
162  } else if (strchr(argv[arg], '/')) {
163  struct db_tree_state ts;
164  struct db_full_path path;
165 
166  db_full_path_init(&path);
167  ts = gedp->ged_wdbp->wdb_initial_tree_state; /* struct copy */
168  ts.ts_dbip = gedp->ged_wdbp->dbip;
169  ts.ts_resp = &rt_uniresource;
170  MAT_IDN(ts.ts_mat);
171 
172  if (db_follow_path_for_state(&ts, &path, argv[arg], 1))
173  continue;
174 
175  dp = DB_FULL_PATH_CUR_DIR(&path);
176 
177  if ((id = rt_db_get_internal(&intern, dp, gedp->ged_wdbp->dbip, ts.ts_mat, &rt_uniresource)) < 0) {
178  bu_vls_printf(gedp->ged_result_str, "rt_db_get_internal(%s) failure", dp->d_namep);
179  continue;
180  }
181 
182  db_free_full_path(&path);
183 
184  bu_vls_printf(gedp->ged_result_str, "%s: ", argv[arg]);
185 
186  if (!OBJ[id].ft_describe || OBJ[id].ft_describe(gedp->ged_result_str, &intern, verbose, gedp->ged_wdbp->dbip->dbi_base2local, &rt_uniresource, gedp->ged_wdbp->dbip) < 0)
187  bu_vls_printf(gedp->ged_result_str, "%s: describe error", dp->d_namep);
188 
189  rt_db_free_internal(&intern);
190  } else {
191  if ((dp = db_lookup(gedp->ged_wdbp->dbip, argv[arg], LOOKUP_NOISY)) == RT_DIR_NULL)
192  continue;
193 
194  _ged_do_list(gedp, dp, verbose); /* very verbose */
195  }
196  }
197 
198  return GED_OK;
199 }
200 
201 
202 /*
203  * Local Variables:
204  * tab-width: 8
205  * mode: C
206  * indent-tabs-mode: t
207  * c-file-style: "stroustrup"
208  * End:
209  * ex: shiftwidth=4 tabstop=8
210  */
void usage(struct ged *gedp)
Definition: coil.c:315
#define GED_OK
Definition: ged.h:55
char * d_namep
pointer to name string
Definition: raytrace.h:859
unsigned char d_major_type
object major type
Definition: raytrace.h:870
int rt_db_get_internal(struct rt_db_internal *ip, const struct directory *dp, const struct db_i *dbip, const mat_t mat, struct resource *resp)
Definition: dir.c:76
void bu_avs_init_empty(struct bu_attribute_value_set *avp)
Definition: avs.c:36
Definition: ged.h:338
struct db_i * dbip
Definition: raytrace.h:1266
void bu_vls_strcat(struct bu_vls *vp, const char *s)
Definition: vls.c:368
void bu_vls_trunc(struct bu_vls *vp, int len)
Definition: vls.c:198
#define GED_CHECK_ARGC_GT_0(_gedp, _argc, _flags)
Definition: ged.h:202
struct directory * db_lookup(const struct db_i *, const char *name, int noisy)
Definition: db_lookup.c:153
int ged_list(struct ged *gedp, int argc, const char *argv[])
Definition: list.c:105
int ged_pathsum(struct ged *gedp, int argc, const char *argv[])
Definition: pathsum.c:36
struct rt_wdb * ged_wdbp
Definition: ged.h:340
Header file for the BRL-CAD common definitions.
int bu_optind
Definition: globals.c:89
#define DB_FULL_PATH_CUR_DIR(_pp)
Definition: db_fullpath.h:51
const char * value
Definition: avs.h:62
int bu_getopt(int nargc, char *const nargv[], const char *ostr)
Definition: getopt.c:43
void db_full_path_init(struct db_full_path *pathp)
Definition: db_fullpath.c:40
#define GED_ERROR
Definition: ged.h:61
char * strchr(const char *sp, int c)
struct resource rt_uniresource
default. Defined in librt/globals.c
Definition: globals.c:41
#define GED_CHECK_DATABASE_OPEN(_gedp, _flags)
Definition: ged.h:114
struct db_i * ts_dbip
Definition: raytrace.h:1040
void db_free_full_path(struct db_full_path *pp)
Definition: db_fullpath.c:473
mat_t ts_mat
transform matrix
Definition: raytrace.h:1050
#define BU_AVS_FOR(_pp, _avp)
Definition: avs.h:141
struct bu_vls * ged_result_str
Definition: ged.h:357
struct resource * ts_resp
Per-CPU data.
Definition: raytrace.h:1074
double dbi_base2local
unit conversion factors
Definition: raytrace.h:808
#define RT_CK_DBI(_p)
Definition: raytrace.h:829
const char * bu_units_string(const double mm)
const struct rt_functab OBJ[]
Definition: table.c:159
struct db_tree_state wdb_initial_tree_state
Definition: raytrace.h:1267
void bu_vls_printf(struct bu_vls *vls, const char *fmt,...) _BU_ATTR_PRINTF23
Definition: vls.c:694
#define RT_DIR_NULL
Definition: raytrace.h:875
#define LOOKUP_NOISY
Definition: raytrace.h:892
#define GED_HELP
Definition: ged.h:62
void _ged_do_list(struct ged *gedp, struct directory *dp, int verbose)
Definition: list.c:39
const char * name
Definition: avs.h:61
HIDDEN void verbose(struct human_data_t *dude)
Definition: human.c:2008
int db5_get_attributes(const struct db_i *dbip, struct bu_attribute_value_set *avs, const struct directory *dp)
Definition: db5_io.c:1027
int db_follow_path_for_state(struct db_tree_state *tsp, struct db_full_path *pathp, const char *orig_str, int noisy)
Definition: db_tree.c:839
double fastf_t
Definition: defines.h:300
void bu_vls_putc(struct bu_vls *vp, int c)
Definition: vls.c:666
void rt_db_free_internal(struct rt_db_internal *ip)
Definition: dir.c:216
#define BU_STR_EQUAL(s1, s2)
Definition: str.h:126