BRL-CAD
killrefs.c
Go to the documentation of this file.
1 /* K I L L R E F S . 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/killrefs.c
21  *
22  * The killrefs command.
23  *
24  */
25 
26 #include "common.h"
27 
28 #include <string.h>
29 
30 #include "bu/cmd.h"
31 
32 #include "./ged_private.h"
33 
34 
35 int
36 ged_killrefs(struct ged *gedp, int argc, const char *argv[])
37 {
38  int k;
39  struct directory *dp;
40  struct rt_db_internal intern;
41  struct rt_comb_internal *comb;
42  int nflag;
43  int ret;
44  static const char *usage = "[-n] object(s)";
45 
49  GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);
50 
51  if (!gedp->ged_internal_call) {
52  /* initialize result */
53  bu_vls_trunc(gedp->ged_result_str, 0);
54  }
55 
56  /* must be wanting help */
57  if (argc == 1) {
58  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
59  return GED_HELP;
60  }
61 
62  /* Process the -n option */
63  if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'n' && argv[1][2] == '\0') {
64  nflag = 1;
65  --argc;
66  ++argv;
67  } else
68  nflag = 0;
69 
70  if (!nflag && !gedp->ged_internal_call) {
71  for (k = 1; k < argc; k++)
73  }
74 
75  ret = GED_OK;
76 
78  if (!(dp->d_flags & RT_DIR_COMB))
79  continue;
80 
81  if (rt_db_get_internal(&intern, dp, gedp->ged_wdbp->dbip, (fastf_t *)NULL, &rt_uniresource) < 0) {
82  bu_vls_printf(gedp->ged_result_str, "rt_db_get_internal(%s) failure", dp->d_namep);
83  ret = GED_ERROR;
84  continue;
85  }
86  comb = (struct rt_comb_internal *)intern.idb_ptr;
87  RT_CK_COMB(comb);
88 
89  for (k = 1; k < argc; k++) {
90  int code;
91 
92  code = db_tree_del_dbleaf(&(comb->tree), argv[k], &rt_uniresource, nflag);
93  if (code == -1)
94  continue; /* not found */
95  if (code == -2)
96  continue; /* empty tree */
97  if (code < 0) {
98  bu_vls_printf(gedp->ged_result_str, "ERROR: Failure deleting %s/%s\n", dp->d_namep, argv[k]);
99  ret = GED_ERROR;
100  } else {
101  if (nflag)
102  bu_vls_printf(gedp->ged_result_str, "%s ", dp->d_namep);
103  else
104  bu_vls_printf(gedp->ged_result_str, "deleted %s/%s\n", dp->d_namep, argv[k]);
105  }
106  }
107 
108  if (rt_db_put_internal(dp, gedp->ged_wdbp->dbip, &intern, &rt_uniresource) < 0) {
109  bu_vls_printf(gedp->ged_result_str, "ERROR: Unable to write new combination into database.\n");
110  ret = GED_ERROR;
111  continue;
112  }
114 
115  return ret;
116 }
117 
118 
119 /*
120  * Local Variables:
121  * tab-width: 8
122  * mode: C
123  * indent-tabs-mode: t
124  * c-file-style: "stroustrup"
125  * End:
126  * ex: shiftwidth=4 tabstop=8
127  */
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
#define FOR_ALL_DIRECTORY_START(_dp, _dbip)
Definition: raytrace.h:895
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
int rt_db_put_internal(struct directory *dp, struct db_i *dbip, struct rt_db_internal *ip, struct resource *resp)
Definition: dir.c:136
Definition: ged.h:338
struct db_i * dbip
Definition: raytrace.h:1266
int ged_internal_call
Definition: ged.h:374
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
#define RT_CK_COMB(_p)
Definition: raytrace.h:955
#define FOR_ALL_DIRECTORY_END
Definition: raytrace.h:899
struct rt_wdb * ged_wdbp
Definition: ged.h:340
Header file for the BRL-CAD common definitions.
void(* ged_free_vlist_callback)(unsigned int, int)
function to call after freeing a vlist
Definition: ged.h:371
struct bu_list * gd_headDisplay
head of display list
Definition: ged.h:307
#define GED_ERROR
Definition: ged.h:61
struct solid * freesolid
Definition: ged.h:345
int db_tree_del_dbleaf(union tree **tp, const char *cp, struct resource *resp, int nflag)
Definition: db_tree.c:558
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
int ged_killrefs(struct ged *gedp, int argc, const char *argv[])
Definition: killrefs.c:36
void _dl_eraseAllNamesFromDisplay(struct bu_list *hdlp, struct db_i *dbip, void(*callback)(unsigned int, int), const char *name, const int skip_first, struct solid *freesolid)
Definition: display_list.c:332
#define GED_CHECK_DRAWABLE(_gedp, _flags)
Definition: ged.h:129
struct bu_vls * ged_result_str
Definition: ged.h:357
struct ged_drawable * ged_gdp
Definition: ged.h:360
void * idb_ptr
Definition: raytrace.h:195
HIDDEN int code(fastf_t x, fastf_t y)
Definition: clip.c:43
#define RT_DIR_COMB
combination
Definition: raytrace.h:884
union tree * tree
Leading to tree_db_leaf leaves.
Definition: raytrace.h:938
void bu_vls_printf(struct bu_vls *vls, const char *fmt,...) _BU_ATTR_PRINTF23
Definition: vls.c:694
#define GED_HELP
Definition: ged.h:62
#define GED_CHECK_READ_ONLY(_gedp, _flags)
Definition: ged.h:181
int d_flags
flags
Definition: raytrace.h:869
double fastf_t
Definition: defines.h:300