1 /*******************************************************************************
2  * Copyright (c) 2013-2021, Andrés Martinelli <andmarti@gmail.com>             *
3  * All rights reserved.                                                        *
4  *                                                                             *
5  * This file is a part of SC-IM                                                *
6  *                                                                             *
7  * SC-IM is a spreadsheet program that is based on SC. The original authors    *
8  * of SC are James Gosling and Mark Weiser, and mods were later added by       *
9  * Chuck Martin.                                                               *
10  *                                                                             *
11  * Redistribution and use in source and binary forms, with or without          *
12  * modification, are permitted provided that the following conditions are met: *
13  * 1. Redistributions of source code must retain the above copyright           *
14  *    notice, this list of conditions and the following disclaimer.            *
15  * 2. Redistributions in binary form must reproduce the above copyright        *
16  *    notice, this list of conditions and the following disclaimer in the      *
17  *    documentation and/or other materials provided with the distribution.     *
18  * 3. All advertising materials mentioning features or use of this software    *
19  *    must display the following acknowledgement:                              *
20  *    This product includes software developed by Andrés Martinelli            *
21  *    <andmarti@gmail.com>.                                                    *
22  * 4. Neither the name of the Andrés Martinelli nor the                        *
23  *   names of other contributors may be used to endorse or promote products    *
24  *   derived from this software without specific prior written permission.     *
25  *                                                                             *
26  * THIS SOFTWARE IS PROVIDED BY ANDRES MARTINELLI ''AS IS'' AND ANY            *
27  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED   *
28  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE      *
29  * DISCLAIMED. IN NO EVENT SHALL ANDRES MARTINELLI BE LIABLE FOR ANY           *
30  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES  *
31  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;*
32  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND *
33  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT  *
34  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE       *
35  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.           *
36  *******************************************************************************/
37 
38 /**
39  * \file plot.c
40  * \author Andrés Martinelli <andmarti@gmail.com>
41  * \date 2017-07-18
42  * \brief TODO Write a tbrief file description.
43  */
44 
45 #include <string.h>
46 #include <stdlib.h>
47 #include <unistd.h>
48 #include <ncurses.h>
49 #include <errno.h>
50 
51 #include "plot.h"
52 #include "file.h"
53 #include "tui.h"
54 
55 /**
56  * \brief TODO Document plotedit()
57  *
58  * \param[in] s
59  *
60  * \return none
61  */
62 
plotedit(wchar_t * s)63 int plotedit(wchar_t * s) {
64 #ifdef GNUPLOT
65     // edit ~/.scim/plotxxxx (or /usr/local/share/scim/plotxxxx)
66     char command[BUFFERSIZE];
67 
68     if (! wcscmp(s, L"line") || ! wcscmp(s, L"scatter") ||
69         ! wcscmp(s, L"pie")  || ! wcscmp(s, L"bar")) {
70         char buffer[PATHLEN + 5];
71         char path_out[PATHLEN];
72         char type[BUFFERSIZE];
73         wcstombs(type, s, BUFFERSIZE);
74         sprintf(buffer, "plot_%s", type);
75         if (! plugin_exists(buffer, strlen(buffer), path_out)) {
76             sc_error("could not load plot template file");
77             return -1;
78         }
79         ui_pause();
80 
81         char * editor;
82         if (! (editor = getenv("EDITOR")))
83             editor = DFLT_EDITOR;
84         sprintf(command, "%.*s %.*s", 100, editor, 100, path_out);
85 
86         if (system(command) == -1) sc_error("Failed editting plot file - errno:%d", errno);
87         ui_resume();
88     } else {
89         sc_error("error: invalid plot file: %ls", s);
90         return -1;
91     }
92     return 0;
93 #else
94     sc_error("Gnuplot was not installed when building Sc-im. Please rebuild Sc-im.");
95     return -1;
96 #endif
97 }
98 
99 /**
100  * \brief TODO Document plot()
101  *
102  * \param[in] s
103  * \param[in] r
104  * \param[in] c
105  * \param[in] rf
106  * \param[in] cf
107  *
108  * \return none
109  */
110 
plot(char * s,int r,int c,int rf,int cf)111 int plot(char * s, int r, int c, int rf, int cf) {
112 #ifdef GNUPLOT
113     // create tmp file
114     char datafile[] = "/tmp/sc-im-plotdataXXXXXX";
115     int fd = mkstemp(datafile);
116     if (fd == -1) {
117         sc_error("Error while creating temp file for plot");
118         return -1;
119     }
120 
121     // export range to temp file in csv format
122     export_delim(datafile, ',', r, c, rf, cf, 0);
123 
124     // call gnuplot with  ~/.scim/plotline (or /usr/local/share/scim/plotline) and temp data file
125     char command[BUFFERSIZE];
126     char buffer[PATHLEN];
127     char buffer1[PATHLEN];
128     sprintf(command, "gnuplot -e \"filename='%s'\"", datafile);
129 
130     if (! strcmp(s, "line") || ! strcmp(s, "scatter") ||
131         ! strcmp(s, "pie")  || ! strcmp(s, "bar")) {
132         sprintf(buffer, "plot_%s", s);
133         if (! plugin_exists(buffer, strlen(buffer), buffer1)) {
134             sc_error("could not load default plotline file");
135             return -1;
136         }
137         sprintf(command + strlen(command), " %s", buffer1);
138 
139     } else {
140         sc_error("plot option not valid");
141         return -1;
142     }
143 
144     ui_pause();
145 
146     if (system(command) == -1)
147         sc_error("Failed during plot - errno:%d", errno);
148     getchar();
149     ui_resume();
150 
151     // close file descriptor
152     close(fd);
153 
154     // remove temp file
155     unlink(datafile);
156 
157     return 0;
158 #else
159     sc_error("Gnuplot was not installed when building Sc-im. Please rebuild Sc-im.");
160     return -1;
161 #endif
162 }
163