Split lines with many copyright years
[alexxy/gromacs.git] / src / gromacs / commandline / viewit.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
11  *
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.
16  *
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.
21  *
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.
26  *
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.
34  *
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.
37  */
38 /* This file is completely threadsafe - keep it that way! */
39 #include "gmxpre.h"
40
41 #include "viewit.h"
42
43 #include <cstdlib>
44 #include <cstring>
45
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"
51
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" };
55
56 static int can_view(int ftp)
57 {
58     int i;
59
60     for (i = 1; i < NVIEW; i++)
61     {
62         if (ftp == can_view_ftp[i])
63         {
64             return i;
65         }
66     }
67
68     return 0;
69 }
70
71 void do_view(const gmx_output_env_t* oenv, const char* fn, const char* opts)
72 {
73     char        buf[STRLEN], env[STRLEN];
74     const char* cmd;
75     int         ftp, n;
76
77     if (output_env_get_view(oenv) && fn)
78     {
79         if (getenv("DISPLAY") == nullptr)
80         {
81             fprintf(stderr, "Can not view %s, no DISPLAY environment variable.\n", fn);
82         }
83         else
84         {
85             ftp = fn2ftp(fn);
86             sprintf(env, "GMX_VIEW_%s", ftp2ext(ftp));
87             upstring(env);
88             switch (ftp)
89             {
90                 case efXVG:
91                     if (!(cmd = getenv(env)))
92                     {
93                         if (getenv("GMX_USE_XMGR"))
94                         {
95                             cmd = "xmgr";
96                         }
97                         else
98                         {
99                             cmd = "xmgrace";
100                         }
101                     }
102                     break;
103                 default:
104                     if ((n = can_view(ftp)))
105                     {
106                         if (!(cmd = getenv(env)))
107                         {
108                             cmd = view_program[n];
109                         }
110                     }
111                     else
112                     {
113                         fprintf(stderr, "Don't know how to view file %s", fn);
114                         return;
115                     }
116             }
117             if (strlen(cmd))
118             {
119                 sprintf(buf, "%s %s %s &", cmd, opts ? opts : "", fn);
120                 fprintf(stderr, "Executing '%s'\n", buf);
121                 if (0 != system(buf))
122                 {
123                     gmx_fatal(FARGS, "Failed executing command: %s", buf);
124                 }
125             }
126         }
127     }
128 }
129
130 void view_all(const gmx_output_env_t* oenv, int nf, t_filenm fnm[])
131 {
132     int i;
133
134     for (i = 0; i < nf; i++)
135     {
136         if (can_view(fnm[i].ftp) && is_output(&(fnm[i])) && (!is_optional(&(fnm[i])) || is_set(&(fnm[i]))))
137         {
138             do_view(oenv, fnm[i].filenames[0].c_str(), nullptr);
139         }
140     }
141 }