1 /*
2 * gretl -- Gnu Regression, Econometrics and Time-series Library
3 * Copyright (C) 2001 Allin Cottrell and Riccardo "Jack" Lucchetti
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19
20 /* objectsave.c for gretl: save models estimated via CLI */
21
22 #include "gretl.h"
23 #include "session.h"
24 #include "objectsave.h"
25 #include "objstack.h"
26
gui_parse_object_request(const char * line,char * objname,char ** param,void ** pptr,GretlObjType * type,PRN * prn)27 static int gui_parse_object_request (const char *line,
28 char *objname, char **param,
29 void **pptr, GretlObjType *type,
30 PRN *prn)
31 {
32 char word[MAXSAVENAME] = {0};
33 int action;
34
35 /* get object name (if any) and dot param */
36 parse_object_command(line, word, param);
37
38 /* if no dot param, nothing doing, pass through */
39 if (*param == NULL) {
40 return OBJ_ACTION_NONE;
41 }
42
43 if (gretl_is_bundle(word)) {
44 return OBJ_ACTION_NONE;
45 } else if (!strcmp(word, "R") && strchr(*param, '(')) {
46 return OBJ_ACTION_NONE;
47 }
48
49 /* see if there's an object associated with the name */
50 *pptr = get_session_object_by_name(word, type);
51
52 if (*pptr == NULL) {
53 /* no matching object */
54 if (*param) {
55 pprintf(prn, _("%s: no such object\n"), word);
56 }
57 return OBJ_ACTION_INVALID;
58 }
59
60 action = match_object_command(*param);
61
62 if (action == OBJ_ACTION_INVALID) {
63 pprintf(prn, _("command '%s' not recognized"), *param);
64 pputc(prn, '\n');
65 } else {
66 strcpy(objname, word);
67 }
68
69 return action;
70 }
71
maybe_save_graph(const char * name,int ci,gretlopt opt,PRN * prn)72 int maybe_save_graph (const char *name, int ci, gretlopt opt, PRN *prn)
73 {
74 GretlObjType type;
75 int display = 0;
76 int add, err = 0;
77
78 if (opt & OPT_U) {
79 /* If the plotting command included "--output=display"
80 we should arrange to show the plot in a window as
81 well as adding it as an icon.
82 */
83 const char *s = get_optval_string(ci, OPT_U);
84
85 if (s != NULL && !strcmp(s, "display")) {
86 display = 1;
87 }
88 }
89
90 /* note: gretl_plotfile() below should give the name of
91 a temporary file to which gnuplot commands have
92 been written.
93 */
94
95 type = (ci == BXPLOT)? GRETL_OBJ_PLOT : GRETL_OBJ_GRAPH;
96 add = cli_add_graph_to_session(gretl_plotfile(), name, type, display);
97
98 if (add == ADD_OBJECT_FAIL) {
99 err = 1;
100 } else if (add == ADD_OBJECT_REPLACE) {
101 pprintf(prn, _("%s replaced\n"), name);
102 } else {
103 pprintf(prn, _("%s saved\n"), name);
104 }
105
106 return err;
107 }
108
save_text_buffer(const char * name,PRN * prn,int pos)109 int save_text_buffer (const char *name, PRN *prn, int pos)
110 {
111 int add, err = 0;
112
113 add = real_add_text_to_session(prn, pos, name);
114
115 if (add == ADD_OBJECT_FAIL) {
116 err = 1;
117 } else if (add == ADD_OBJECT_REPLACE) {
118 pprintf(prn, _("%s replaced\n"), name);
119 } else {
120 pprintf(prn, _("%s saved\n"), name);
121 }
122
123 return err;
124 }
125
gui_saved_object_action(const char * line,PRN * prn)126 int gui_saved_object_action (const char *line, PRN *prn)
127 {
128 char objname[MAXSAVENAME] = {0};
129 char *param = NULL;
130 void *ptr = NULL;
131 GretlObjType type;
132 int action;
133
134 if (*line == '!' || *line == '#') {
135 /* shell command or comment: NOT an object command */
136 return OBJ_ACTION_NONE;
137 }
138
139 /* special: display icon view window */
140 if (!strncmp(line, "iconview", 8)) {
141 if (data_status) {
142 view_session();
143 }
144 return OBJ_ACTION_NULL; /* handled */
145 }
146
147 action = gui_parse_object_request(line, objname, ¶m,
148 &ptr, &type, prn);
149
150 if (action == OBJ_ACTION_SHOW) {
151 if (type == GRETL_OBJ_EQN ||
152 type == GRETL_OBJ_VAR ||
153 type == GRETL_OBJ_SYS) {
154 session_model_callback(ptr, action);
155 } else if (type == GRETL_OBJ_TEXT) {
156 display_saved_text(ptr);
157 } else if (type == GRETL_OBJ_GRAPH) {
158 display_session_graph_by_data(ptr);
159 } else {
160 action = OBJ_ACTION_INVALID;
161 }
162 } else if (action == OBJ_ACTION_FREE) {
163 if (type == GRETL_OBJ_EQN ||
164 type == GRETL_OBJ_VAR ||
165 type == GRETL_OBJ_SYS) {
166 session_model_callback(ptr, action);
167 pprintf(prn, _("Freed %s\n"), objname);
168 } else {
169 action = OBJ_ACTION_INVALID;
170 }
171 }
172
173 free(param);
174
175 return action;
176 }
177