/* * Copyright (c) by Allin Cottrell * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * */ #include "gretl.h" #include "cmdstack.h" #include "lib_private.h" #include "cmd_private.h" #include "fileselect.h" #include "session.h" #define CMD_DEBUG 0 typedef struct { int ID; int n; char **cmds; } model_stack; static model_stack *mstacks; static int n_mstacks; static char **cmd_stack; static int n_cmds; void free_command_stack (void) { int i, j; if (cmd_stack != NULL) { for (i=0; i 0 && mstacks != NULL) { for (i=0; i 0 && str[n-1] == '\n') { cmd_stack[n_cmds] = gretl_strdup(str); } else { cmd_stack[n_cmds] = g_strdup_printf("%s\n", str); } if (cmd_stack[n_cmds] == NULL) { return 1; } if (strlen(str) > 2 && strncmp(str, "help", 4) && strncmp(str, "info", 4) && strncmp(str, "list", 4) && strncmp(str, "open", 4) && strncmp(str, "quit", 4)) { mark_session_changed(); } #if CMD_DEBUG fprintf(stderr, "added to stack as cmd_stack[%d]:\n" " %s\n", n_cmds, cmd_stack[n_cmds]); #endif n_cmds++; return 0; } void delete_last_command (void) { if (n_cmds > 0) { free(cmd_stack[n_cmds - 1]); n_cmds--; } } static model_stack *add_model_stack (int model_id) { model_stack *tmp; int nm = n_mstacks; tmp = myrealloc(mstacks, (nm + 1) * sizeof *tmp); if (tmp == NULL) { return NULL; } mstacks = tmp; n_mstacks++; mstacks[nm].ID = model_id; mstacks[nm].n = 0; mstacks[nm].cmds = NULL; #if CMD_DEBUG fprintf(stderr, "add_model_stack:\n" " mstacks[%d]: ID=%d\n", nm, model_id); #endif return &mstacks[nm]; } static int mstack_delete_last_command (model_stack *mstack) { int nc = mstack->n; char **tmp; free(mstack->cmds[nc-1]); mstack->n -= 1; tmp = realloc(mstack->cmds, (nc - 1) * sizeof *tmp); if (tmp == NULL) { return 1; } mstack->cmds = tmp; return 0; } static int add_command_to_mstack (model_stack *mstack, const char *str) { int nc = mstack->n; char **tmp; tmp = realloc(mstack->cmds, (nc + 1) * sizeof *tmp); if (tmp == NULL) { return 1; } mstack->cmds = tmp; mstack->cmds[nc] = gretl_strdup(str); if (mstack->cmds[nc] == NULL) { return 1; } mstack->n += 1; #if CMD_DEBUG fprintf(stderr, "add_command_to_mstack, with ID=%d:\n" " %s\n", mstack->ID, str); #endif return 0; } static model_stack *mstack_from_model_id (int ID) { int i; for (i=0; in > 0) { mstack_delete_last_command(mstack); } } static void dump_model_cmds (const model_stack *mstack, FILE *fp) { int i; fprintf(fp, "/* commands pertaining to model %d */\n", mstack->ID); for (i=0; in; i++) { fprintf(fp, "%s", mstack->cmds[i]); } } /* get the ID number of a variable operated upon by "genr" or "setinfo" */ static int vnum_from_data_command (const char *s) { char vname[VNAMELEN]; char format[8]; int offset = 6; int v = 0; sprintf(format, "%%%ds", VNAMELEN - 1); if (!strncmp(s, "genr", 4)) { offset = 5; } if (sscanf(s + offset, format, vname)) { v = varindex(datainfo, vname); if (v > datainfo->v - 1) { v = 0; } } return v; } static int parse_store_cmd (const char *sline, CMD *scmd) { char *linecpy; int err = 0; linecpy = gretl_strdup(sline); if (linecpy == NULL) { return 1; } err = parse_command_line(linecpy, scmd, &Z, datainfo); free(linecpy); return err; } /* So far as we can tell, has a given modified variable been saved to the current datafile? */ static int var_is_stored (int v, int pos) { int i, stored = 0; for (i=pos+1; i 0 && var_is_stored(v, i)) { drop[i] = 1; } } } return drop; } /* Check: Did we open any datafile in this session? If not, the session may have involved importing data from a database, in which case the data may or may not have been saved as a gretl datafile. If we're really saving the session for future use, we'd better insert an "open" command. */ static int maybe_prepend_open_data (FILE *fp) { int i, cancel = 0; for (i=0; i 2 && strncmp(s, "run ", 4) && strncmp(s, "open", 4) && strncmp(s, "help", 4) && strncmp(s, "impo", 4) && strncmp(s, "info", 4) && strncmp(s, "labe", 4) && strncmp(s, "list", 4) && strncmp(s, "quit", 4)) { work = 1; break; } } return work; }