BRL-CAD
kill.c
Go to the documentation of this file.
1 /* K I L L . 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/kill.c
21  *
22  * The kill command.
23  *
24  */
25 
26 #include "common.h"
27 
28 #include <string.h>
29 
30 #include "bu/cmd.h"
31 #include "bu/getopt.h"
32 
33 #include "./ged_private.h"
34 
35 
36 int
37 ged_kill(struct ged *gedp, int argc, const char *argv[])
38 {
39  struct directory *dp;
40  int i;
41  int c;
42  int is_phony;
43  int verbose = LOOKUP_NOISY;
44  int force = 0;
45  int nflag = 0;
46  static const char *usage = "[-f|-n] object(s)";
47 
51  GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);
52 
53  /* initialize result */
54  bu_vls_trunc(gedp->ged_result_str, 0);
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  bu_optind = 1;
63  while ((c = bu_getopt(argc, (char * const *)argv, "fn")) != -1) {
64  switch (c) {
65  case 'f':
66  force = 1;
67  break;
68  case 'n':
69  nflag = 1;
70  break;
71  default:
72  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
73  return GED_ERROR;
74  }
75  }
76 
77  if ((force + nflag) > 1) {
78  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
79  return GED_ERROR;
80  }
81 
82  argc -= (bu_optind - 1);
83  argv += (bu_optind - 1);
84 
85  if (nflag) {
86  bu_vls_printf(gedp->ged_result_str, "{");
87  for (i = 1; i < argc; i++)
88  bu_vls_printf(gedp->ged_result_str, "%s ", argv[i]);
89  bu_vls_printf(gedp->ged_result_str, "} {}");
90 
91  return GED_OK;
92  }
93 
94  for (i = 1; i < argc; i++) {
95  if ((dp = db_lookup(gedp->ged_wdbp->dbip, argv[i], verbose)) != RT_DIR_NULL) {
96  if (!force && dp->d_major_type == DB5_MAJORTYPE_ATTRIBUTE_ONLY && dp->d_minor_type == 0) {
97  bu_vls_printf(gedp->ged_result_str, "You attempted to delete the _GLOBAL object.\n");
98  bu_vls_printf(gedp->ged_result_str, "\tIf you delete the \"_GLOBAL\" object you will be losing some important information\n");
99  bu_vls_printf(gedp->ged_result_str, "\tsuch as your preferred units and the title of the database.\n");
100  bu_vls_printf(gedp->ged_result_str, "\tUse the \"-f\" option, if you really want to do this.\n");
101  continue;
102  }
103 
104  is_phony = (dp->d_addr == RT_DIR_PHONY_ADDR);
105 
106  /* don't worry about phony objects */
107  if (is_phony)
108  continue;
109 
111 
112  if (db_delete(gedp->ged_wdbp->dbip, dp) != 0 || db_dirdelete(gedp->ged_wdbp->dbip, dp) != 0) {
113  /* Abort kill processing on first error */
114  bu_vls_printf(gedp->ged_result_str, "an error occurred while deleting %s", argv[i]);
115  return GED_ERROR;
116  }
117  }
118  }
119 
120  return GED_OK;
121 }
122 
123 
124 /*
125  * Local Variables:
126  * tab-width: 8
127  * mode: C
128  * indent-tabs-mode: t
129  * c-file-style: "stroustrup"
130  * End:
131  * ex: shiftwidth=4 tabstop=8
132  */
void usage(struct ged *gedp)
Definition: coil.c:315
#define GED_OK
Definition: ged.h:55
unsigned char d_major_type
object major type
Definition: raytrace.h:870
int db_dirdelete(struct db_i *, struct directory *dp)
Definition: db_lookup.c:262
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
struct rt_wdb * ged_wdbp
Definition: ged.h:340
Header file for the BRL-CAD common definitions.
int bu_optind
Definition: globals.c:89
void(* ged_free_vlist_callback)(unsigned int, int)
function to call after freeing a vlist
Definition: ged.h:371
int bu_getopt(int nargc, char *const nargv[], const char *ostr)
Definition: getopt.c:43
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
unsigned char d_minor_type
object minor type
Definition: raytrace.h:871
#define GED_CHECK_DATABASE_OPEN(_gedp, _flags)
Definition: ged.h:114
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 RT_DIR_PHONY_ADDR
Special marker for d_addr field.
Definition: raytrace.h:879
#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 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
int ged_kill(struct ged *gedp, int argc, const char *argv[])
Definition: kill.c:37
HIDDEN void verbose(struct human_data_t *dude)
Definition: human.c:2008
#define GED_CHECK_READ_ONLY(_gedp, _flags)
Definition: ged.h:181
int db_delete(struct db_i *, struct directory *dp)
Definition: db_alloc.c:132