Update instructions in containers.rst
[gromacs.git] / src / gromacs / commandline / viewit.cpp
bloba2b1ae28259aedf459f333431f01ef47aa6d89c0
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5 * Copyright (c) 2001-2004, The GROMACS development team.
6 * Copyright (c) 2013,2014,2015,2017,2018 by the GROMACS development team.
7 * Copyright (c) 2019,2020, by the GROMACS development team, led by
8 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
9 * and including many others, as listed in the AUTHORS file in the
10 * top-level source directory and at http://www.gromacs.org.
12 * GROMACS is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 2.1
15 * of the License, or (at your option) any later version.
17 * GROMACS is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with GROMACS; if not, see
24 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 * If you want to redistribute modifications to GROMACS, please
28 * consider that scientific software is very special. Version
29 * control is crucial - bugs must be traceable. We will be happy to
30 * consider code for inclusion in the official distribution, but
31 * derived work must not be called official GROMACS. Details are found
32 * in the README & COPYING files - if they are missing, get the
33 * official version at http://www.gromacs.org.
35 * To help us fund GROMACS development, we humbly ask that you cite
36 * the research papers on the package. Check out http://www.gromacs.org.
38 /* This file is completely threadsafe - keep it that way! */
39 #include "gmxpre.h"
41 #include "viewit.h"
43 #include <cstdlib>
44 #include <cstring>
46 #include "gromacs/commandline/filenm.h"
47 #include "gromacs/fileio/oenv.h"
48 #include "gromacs/utility/arraysize.h"
49 #include "gromacs/utility/cstringutil.h"
50 #include "gromacs/utility/fatalerror.h"
52 static const int can_view_ftp[] = { 0, efEPS, efXPM, efXVG, efPDB };
53 #define NVIEW asize(can_view_ftp)
54 static const char* view_program[] = { nullptr, "ghostview", "display", nullptr, "xterm -e rasmol" };
56 static int can_view(int ftp)
58 int i;
60 for (i = 1; i < NVIEW; i++)
62 if (ftp == can_view_ftp[i])
64 return i;
68 return 0;
71 void do_view(const gmx_output_env_t* oenv, const char* fn, const char* opts)
73 char buf[STRLEN], env[STRLEN];
74 const char* cmd;
75 int ftp, n;
77 if (output_env_get_view(oenv) && fn)
79 if (getenv("DISPLAY") == nullptr)
81 fprintf(stderr, "Can not view %s, no DISPLAY environment variable.\n", fn);
83 else
85 ftp = fn2ftp(fn);
86 sprintf(env, "GMX_VIEW_%s", ftp2ext(ftp));
87 upstring(env);
88 switch (ftp)
90 case efXVG:
91 if (!(cmd = getenv(env)))
93 if (getenv("GMX_USE_XMGR"))
95 cmd = "xmgr";
97 else
99 cmd = "xmgrace";
102 break;
103 default:
104 if ((n = can_view(ftp)))
106 if (!(cmd = getenv(env)))
108 cmd = view_program[n];
111 else
113 fprintf(stderr, "Don't know how to view file %s", fn);
114 return;
117 if (strlen(cmd))
119 sprintf(buf, "%s %s %s &", cmd, opts ? opts : "", fn);
120 fprintf(stderr, "Executing '%s'\n", buf);
121 if (0 != system(buf))
123 gmx_fatal(FARGS, "Failed executing command: %s", buf);
130 void view_all(const gmx_output_env_t* oenv, int nf, t_filenm fnm[])
132 int i;
134 for (i = 0; i < nf; i++)
136 if (can_view(fnm[i].ftp) && is_output(&(fnm[i])) && (!is_optional(&(fnm[i])) || is_set(&(fnm[i]))))
138 do_view(oenv, fnm[i].filenames[0].c_str(), nullptr);