BRL-CAD
tree.c
Go to the documentation of this file.
1 /* T R E E . 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/tree.c
21  *
22  * The tree command.
23  *
24  */
25 
26 #include "common.h"
27 
28 #include <stdlib.h>
29 #include <errno.h>
30 #include <limits.h>
31 
32 #include "bu/cmd.h"
33 #include "bu/getopt.h"
34 
35 
36 #include "./ged_private.h"
37 
38 
39 /*
40  * Return the object hierarchy for all object(s) specified or for all currently displayed
41  *
42  * Usage:
43  * tree [-a] [-c] [-o outfile] [-i indentSize] [-d displayDepth] [object(s)]
44  *
45  */
46 int
47 ged_tree(struct ged *gedp, int argc, const char *argv[])
48 {
49  struct directory *dp;
50  int j;
51  unsigned flags = 0;
52  int indentSize = -1;
53  int displayDepth = INT_MAX;
54  int c;
55  FILE *fdout = NULL;
56  char *buffer = NULL;
57 #define WHOARGVMAX 256
58  char *whoargv[WHOARGVMAX+1] = {0};
59  static const char *usage = "[-a] [-c] [-o outfile] [-i indentSize] [-d displayDepth] [object(s)]";
60 
63  GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);
64 
65  /* initialize result */
66  bu_vls_trunc(gedp->ged_result_str, 0);
67 
68  /* Parse options */
69  bu_optind = 1; /* re-init bu_getopt() */
70  while ((c = bu_getopt(argc, (char * const *)argv, "d:i:o:ca")) != -1) {
71  switch (c) {
72  case 'i':
73  indentSize = atoi(bu_optarg);
74  break;
75  case 'a':
76  flags |= _GED_TREE_AFLAG;
77  break;
78  case 'c':
79  flags |= _GED_TREE_CFLAG;
80  break;
81  case 'o':
82  if (fdout)
83  fclose(fdout);
84  fdout = fopen(bu_optarg, "w+b");
85  if (fdout == NULL) {
86  bu_vls_printf(gedp->ged_result_str, "Failed to open output file, %d", errno);
87  return GED_ERROR;
88  }
89  break;
90  case 'd':
91  displayDepth = atoi(bu_optarg);
92  if (displayDepth < 0) {
93  bu_vls_printf(gedp->ged_result_str, "Negative number supplied as depth - unsupported.");
94  if (fdout != NULL)
95  fclose(fdout);
96  return GED_ERROR;
97  }
98  break;
99  case '?':
100  default:
101  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
102  if (fdout != NULL)
103  fclose(fdout);
104  return GED_ERROR;
105  }
106  }
107 
108  argc -= (bu_optind - 1);
109  argv += (bu_optind - 1);
110 
111  /* tree of all displayed objects */
112  if (argc == 1) {
113  char *whocmd[2] = {"who", NULL};
114  if (ged_who(gedp, 1, (const char **)whocmd) == GED_OK) {
115  buffer = bu_strdup(bu_vls_addr(gedp->ged_result_str));
116  bu_vls_trunc(gedp->ged_result_str, 0);
117 
118  argc += bu_argv_from_string(whoargv, WHOARGVMAX, buffer);
119  }
120  }
121 
122  for (j = 1; j < argc; j++) {
123  const char *next = argv[j];
124  if (buffer) {
125  next = whoargv[j-1];
126  }
127 
128  if (j > 1)
129  bu_vls_printf(gedp->ged_result_str, "\n");
130  if ((dp = db_lookup(gedp->ged_wdbp->dbip, next, LOOKUP_NOISY)) == RT_DIR_NULL)
131  continue;
132  _ged_print_node(gedp, dp, 0, indentSize, 0, flags, displayDepth, 0);
133  }
134 
135  if (buffer) {
136  bu_free(buffer, "free who buffer");
137  buffer = NULL;
138  }
139 
140  if (fdout != NULL) {
141  fprintf(fdout, "%s", bu_vls_addr(gedp->ged_result_str));
142  fclose(fdout);
143  }
144 
145  return GED_OK;
146 }
147 
148 
149 /*
150  * Local Variables:
151  * tab-width: 8
152  * mode: C
153  * indent-tabs-mode: t
154  * c-file-style: "stroustrup"
155  * End:
156  * ex: shiftwidth=4 tabstop=8
157  */
void usage(struct ged *gedp)
Definition: coil.c:315
#define GED_OK
Definition: ged.h:55
Definition: ged.h:338
struct db_i * dbip
Definition: raytrace.h:1266
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
#define _GED_TREE_AFLAG
Definition: ged_private.h:68
struct rt_wdb * ged_wdbp
Definition: ged.h:340
char * bu_optarg
Definition: globals.c:91
Header file for the BRL-CAD common definitions.
int ged_tree(struct ged *gedp, int argc, const char *argv[])
Definition: tree.c:47
int bu_optind
Definition: globals.c:89
int bu_getopt(int nargc, char *const nargv[], const char *ostr)
Definition: getopt.c:43
#define GED_ERROR
Definition: ged.h:61
#define GED_CHECK_DATABASE_OPEN(_gedp, _flags)
Definition: ged.h:114
#define WHOARGVMAX
#define GED_CHECK_DRAWABLE(_gedp, _flags)
Definition: ged.h:129
#define _GED_TREE_CFLAG
Definition: ged_private.h:69
char * bu_vls_addr(const struct bu_vls *vp)
Definition: vls.c:111
struct bu_vls * ged_result_str
Definition: ged.h:357
void _ged_print_node(struct ged *gedp, struct directory *dp, size_t pathpos, int indentSize, char prefix, unsigned flags, int displayDepth, int currdisplayDepth)
Definition: ged.c:464
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
void bu_free(void *ptr, const char *str)
Definition: malloc.c:328
size_t bu_argv_from_string(char *argv[], size_t lim, char *lp)
Definition: argv.c:32
int ged_who(struct ged *gedp, int argc, const char *argv[])
Definition: who.c:38
#define bu_strdup(s)
Definition: str.h:71