BRL-CAD
saveview.c
Go to the documentation of this file.
1 /* S A V E V I E W . 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/saveview.c
21  *
22  * The saveview command.
23  *
24  */
25 
26 #include "common.h"
27 
28 #include <stdlib.h>
29 #include <ctype.h>
30 #include <string.h>
31 
32 #include "bu/getopt.h"
33 
34 
35 #include "./ged_private.h"
36 
37 
38 /**
39  * Return basename of path, removing leading slashes and trailing suffix.
40  */
41 static char *
42 basename_without_suffix(const char *p1, const char *suff)
43 {
44  char *p2, *p3;
45  static char buf[128];
46 
47  /* find the basename */
48  p2 = (char *)p1;
49  while (*p1) {
50  if (*p1++ == '/')
51  p2 = (char *)p1;
52  }
53 
54  /* find the end of suffix */
55  for (p3=(char *)suff; *p3; p3++)
56  ;
57 
58  /* early out */
59  while (p1>p2 && p3>suff) {
60  if (*--p3 != *--p1)
61  return p2;
62  }
63 
64  /* stash and return filename, sans suffix */
65  bu_strlcpy(buf, p2, p1-p2+1);
66  return buf;
67 }
68 
69 
70 int
71 ged_saveview(struct ged *gedp, int argc, const char *argv[])
72 {
73  struct display_list *gdlp;
74  struct display_list *next_gdlp;
75  int i;
76  FILE *fp;
77  char *base;
78  int c;
79  char rtcmd[255] = {'r', 't', 0};
80  char outlog[255] = {0};
81  char outpix[255] = {0};
82  char inputg[255] = {0};
83  static const char *usage = "[-e] [-i] [-l] [-o] filename [args]";
84 
87  GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);
88 
89  /* initialize result */
90  bu_vls_trunc(gedp->ged_result_str, 0);
91 
92  /* must be wanting help */
93  if (argc == 1) {
94  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
95  return GED_HELP;
96  }
97 
98  bu_optind = 1;
99  while ((c = bu_getopt(argc, (char * const *)argv, "e:i:l:o:")) != -1) {
100  switch (c) {
101  case 'e':
102  snprintf(rtcmd, 255, "%s", bu_optarg);
103  break;
104  case 'l':
105  snprintf(outlog, 255, "%s", bu_optarg);
106  break;
107  case 'o':
108  snprintf(outpix, 255, "%s", bu_optarg);
109  break;
110  case 'i':
111  snprintf(inputg, 255, "%s", bu_optarg);
112  break;
113  default: {
114  bu_vls_printf(gedp->ged_result_str, "Option '%c' unknown\n", c);
115  bu_vls_printf(gedp->ged_result_str, "help saveview");
116  return GED_ERROR;
117  }
118  }
119  }
120  argc -= bu_optind-1;
121  argv += bu_optind-1;
122 
123  if (argc < 2) {
124  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
125  return GED_ERROR;
126  }
127 
128  if ((fp = fopen(argv[1], "a")) == NULL) {
129  perror(argv[1]);
130  return GED_ERROR;
131  }
132  (void)bu_fchmod(fileno(fp), 0755); /* executable */
133 
134  if (!gedp->ged_wdbp->dbip->dbi_filename) {
135  bu_log("Error: geometry file is not specified\n");
136  fclose(fp);
137  return GED_ERROR;
138  }
139 
140  if (!bu_file_exists(gedp->ged_wdbp->dbip->dbi_filename, NULL)) {
141  bu_log("Error: %s does not exist\n", gedp->ged_wdbp->dbip->dbi_filename);
142  fclose(fp);
143  return GED_ERROR;
144  }
145 
146  base = basename_without_suffix(argv[1], ".sh");
147  if (outpix[0] == '\0') {
148  snprintf(outpix, 255, "%s.pix", base);
149  }
150  if (outlog[0] == '\0') {
151  snprintf(outlog, 255, "%s.log", base);
152  }
153 
154  /* Do not specify -v option to rt; batch jobs must print everything. -Mike */
155  fprintf(fp, "#!/bin/sh\n%s -M ", rtcmd);
156  if (gedp->ged_gvp->gv_perspective > 0)
157  fprintf(fp, "-p%g ", gedp->ged_gvp->gv_perspective);
158  for (i = 2; i < argc; i++)
159  fprintf(fp, "%s ", argv[i]);
160 
161  if (bu_strncmp(rtcmd, "nirt", 4) != 0)
162  fprintf(fp, "\\\n -o %s\\\n $*\\\n", outpix);
163 
164  if (inputg[0] == '\0') {
165  snprintf(inputg, 255, "%s", gedp->ged_wdbp->dbip->dbi_filename);
166  }
167  fprintf(fp, " '%s'\\\n ", inputg);
168 
170  while (BU_LIST_NOT_HEAD(gdlp, gedp->ged_gdp->gd_headDisplay)) {
171  next_gdlp = BU_LIST_PNEXT(display_list, gdlp);
172  fprintf(fp, "'%s' ", bu_vls_addr(&gdlp->dl_path));
173  gdlp = next_gdlp;
174  }
175 
176  fprintf(fp, "\\\n 2>> %s\\\n", outlog);
177  fprintf(fp, " <<EOF\n");
178 
179  {
180  vect_t eye_model;
181 
182  _ged_rt_set_eye_model(gedp, eye_model);
183  _ged_rt_write(gedp, fp, eye_model);
184  }
185 
186  fprintf(fp, "\nEOF\n");
187  (void)fclose(fp);
188 
189  return GED_OK;
190 }
191 
192 
193 /*
194  * Local Variables:
195  * tab-width: 8
196  * mode: C
197  * indent-tabs-mode: t
198  * c-file-style: "stroustrup"
199  * End:
200  * ex: shiftwidth=4 tabstop=8
201  */
void usage(struct ged *gedp)
Definition: coil.c:315
#define GED_OK
Definition: ged.h:55
void bu_log(const char *,...) _BU_ATTR_PRINTF12
Definition: log.c:176
int bu_fchmod(int fd, unsigned long pmode)
Definition: fchmod.c:131
Definition: ged.h:338
struct db_i * dbip
Definition: raytrace.h:1266
void _ged_rt_set_eye_model(struct ged *gedp, vect_t eye_model)
Definition: rt.c:88
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 rt_wdb * ged_wdbp
Definition: ged.h:340
char * bu_optarg
Definition: globals.c:91
Header file for the BRL-CAD common definitions.
fastf_t gv_perspective
perspective angle
Definition: bview.h:214
int bu_optind
Definition: globals.c:89
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
void _ged_rt_write(struct ged *gedp, FILE *fp, vect_t eye_model)
Definition: rt.c:54
struct bview * ged_gvp
Definition: ged.h:361
int bu_strncmp(const char *string1, const char *string2, size_t n)
Definition: str.c:191
#define GED_CHECK_VIEW(_gedp, _flags)
Definition: ged.h:140
#define GED_CHECK_DATABASE_OPEN(_gedp, _flags)
Definition: ged.h:114
#define bu_strlcpy(dst, src, size)
Definition: str.h:60
#define BU_LIST_PNEXT(structure, p)
Definition: list.h:422
struct bu_vls dl_path
Definition: bview.h:48
char * bu_vls_addr(const struct bu_vls *vp)
Definition: vls.c:111
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 GED_HELP
Definition: ged.h:62
char * dbi_filename
file name
Definition: raytrace.h:805
int bu_file_exists(const char *path, int *fd)
Definition: file.c:57
#define BU_LIST_NEXT(structure, hp)
Definition: list.h:316
#define BU_LIST_NOT_HEAD(p, hp)
Definition: list.h:324
int ged_saveview(struct ged *gedp, int argc, const char *argv[])
Definition: saveview.c:71