BRL-CAD
edmater.c
Go to the documentation of this file.
1 /* E D M A T E R . 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/edmater.c
21  *
22  * The edmater command.
23  *
24  * Relies on: rmater, editit, wmater
25  *
26  */
27 
28 #include "common.h"
29 
30 #ifdef HAVE_UNISTD_H
31 # include <unistd.h>
32 #endif
33 
34 #include "bu/getopt.h"
35 #include "./ged_private.h"
36 
37 int
38 ged_edmater(struct ged *gedp, int argc, const char *argv[])
39 {
40  FILE *fp;
41  int i, c;
42  int status;
43  const char **av;
44  static const char *usage = "comb(s)";
45  char tmpfil[MAXPATHLEN];
46  const char *editstring = NULL;
47 
50  GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);
51 
52  bu_optind = 1;
53  /* First, grab the editstring off of the argv list */
54  while ((c = bu_getopt(argc, (char * const *)argv, "E:")) != -1) {
55  switch (c) {
56  case 'E' :
57  editstring = bu_optarg;
58  break;
59  default :
60  break;
61  }
62  }
63 
64  argc -= bu_optind - 1;
65  argv += bu_optind - 1;
66 
67 
68  /* initialize result */
69  bu_vls_trunc(gedp->ged_result_str, 0);
70 
71  /* must be wanting help */
72  if (argc == 1) {
73  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
74  return GED_HELP;
75  }
76 
77  fp = bu_temp_file(tmpfil, MAXPATHLEN);
78  if (!fp)
79  return TCL_ERROR;
80 
81  av = (const char **)bu_malloc(sizeof(char *)*(argc + 2), "f_edmater: av");
82  av[0] = "wmater";
83  av[1] = tmpfil;
84  for (i = 2; i < argc + 1; ++i)
85  av[i] = argv[i-1];
86 
87  av[i] = NULL;
88 
89  (void)fclose(fp);
90 
91  if (ged_wmater(gedp, argc, av) == TCL_ERROR) {
92  bu_file_delete(tmpfil);
93  bu_free((void *)av, "f_edmater: av");
94  return TCL_ERROR;
95  }
96 
97  if (_ged_editit(editstring, tmpfil)) {
98  av[0] = "rmater";
99  av[2] = NULL;
100  status = ged_rmater(gedp, 2, av);
101  } else {
102  status = TCL_ERROR;
103  }
104 
105  bu_file_delete(tmpfil);
106  bu_free((void *)av, "ged_edmater: av");
107 
108  return status;
109 }
110 
111 
112 /*
113  * Local Variables:
114  * tab-width: 8
115  * mode: C
116  * indent-tabs-mode: t
117  * c-file-style: "stroustrup"
118  * End:
119  * ex: shiftwidth=4 tabstop=8
120  */
void usage(struct ged *gedp)
Definition: coil.c:315
int ged_edmater(struct ged *gedp, int argc, const char *argv[])
Definition: edmater.c:38
Definition: ged.h:338
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
int ged_wmater(struct ged *gedp, int argc, const char *argv[])
Definition: wmater.c:30
char * bu_optarg
Definition: globals.c:91
Header file for the BRL-CAD common definitions.
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
void * bu_malloc(size_t siz, const char *str)
Definition: malloc.c:314
#define GED_CHECK_DATABASE_OPEN(_gedp, _flags)
Definition: ged.h:114
FILE * bu_temp_file(char *filepath, size_t len)
Definition: temp.c:180
struct bu_vls * ged_result_str
Definition: ged.h:357
int _ged_editit(char *editstring, const char *filename)
Definition: editit.c:49
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
void bu_free(void *ptr, const char *str)
Definition: malloc.c:328
int ged_rmater(struct ged *gedp, int argc, const char *argv[])
Definition: rmater.c:80
#define GED_CHECK_READ_ONLY(_gedp, _flags)
Definition: ged.h:181
int bu_file_delete(const char *path)
Definition: file.c:278
#define MAXPATHLEN
Definition: defines.h:113