static void maybe_save_session_output (const char *cmdfile) { char outfile[FILENAME_MAX]; printf(_("type a filename to store output (enter to quit): ")); *outfile = '\0'; if (fgets(outfile, sizeof outfile, stdin) != NULL) { top_n_tail(outfile, 0, NULL); } if (*outfile != '\0' && *outfile != '\n' && *outfile != '\r' && strcmp(outfile, "q")) { const char *udir = gretl_workdir(); char *syscmd; printf(_("writing session output to %s%s\n"), udir, outfile); #ifdef WIN32 syscmd = gretl_strdup_printf("\"%sgretlcli\" -b \"%s\" > \"%s%s\"", gretl_home(), cmdfile, udir, outfile); system(syscmd); #else syscmd = gretl_strdup_printf("gretlcli -b \"%s\" > \"%s%s\"", cmdfile, udir, outfile); gretl_spawn(syscmd); #endif printf("%s\n", syscmd); free(syscmd); } }
int main (int argc, char *argv[]) { #ifdef WIN32 char *callname = argv[0]; #endif char linecopy[MAXLINE]; DATASET *dset = NULL; MODEL *model = NULL; ExecState state; char *line = NULL; int quiet = 0; int makepkg = 0; int load_datafile = 1; char filearg[MAXLEN]; char runfile[MAXLEN]; double scriptval = NADBL; CMD cmd; PRN *prn = NULL; PRN *cmdprn = NULL; int err = 0; #ifdef G_OS_WIN32 win32_set_gretldir(callname); #endif nls_init(); #ifdef HAVE_READLINE rl_bind_key(0x18, ctrl_x); #endif dset = datainfo_new(); if (dset == NULL) { noalloc(); } if (argc < 2) { load_datafile = 0; } else { gretlopt opt = 0; err = parse_options(&argc, &argv, &opt, &scriptval, filearg); if (!err && (opt & (OPT_DBOPEN | OPT_WEBDB))) { /* catch GUI-only options */ err = E_BADOPT; } if (err) { /* bad option */ usage(1); } else if (opt & (OPT_HELP | OPT_VERSION)) { /* we'll exit in these cases */ if (opt & OPT_HELP) { usage(0); } else { logo(0); exit(EXIT_SUCCESS); } } if (opt & (OPT_BATCH | OPT_RUNIT | OPT_MAKEPKG)) { if (*filearg == '\0') { /* we're missing a filename argument */ usage(1); } else { /* record argument (not a datafile) */ strcpy(runfile, filearg); load_datafile = 0; if (opt & OPT_BATCH) { batch = 1; } else if (opt & OPT_MAKEPKG) { batch = 1; makepkg = 1; } else { runit = 1; } } } else if (*filearg == '\0') { load_datafile = 0; } if (opt & OPT_QUIET) { quiet = 1; } if (opt & OPT_ENGLISH) { force_language(LANG_C); } } libgretl_init(); logo(quiet); if (!quiet) { session_time(NULL); } prn = gretl_print_new(GRETL_PRINT_STDOUT, &err); if (err) { noalloc(); } line = malloc(MAXLINE); if (line == NULL) { noalloc(); } #ifdef WIN32 win32_cli_read_rc(callname); #else cli_read_rc(); #endif /* WIN32 */ if (!batch) { strcpy(cmdfile, gretl_workdir()); strcat(cmdfile, "session.inp"); cmdprn = gretl_print_new_with_filename(cmdfile, &err); if (err) { errmsg(err, prn); return EXIT_FAILURE; } } if (load_datafile) { handle_datafile(filearg, runfile, dset, prn, cmdprn); } /* allocate memory for model */ model = allocate_working_model(); if (model == NULL) { noalloc(); } gretl_cmd_init(&cmd); gretl_exec_state_init(&state, 0, line, &cmd, model, prn); set_debug_read_func(get_interactive_line); /* print list of variables */ if (data_status) { varlist(dset, prn); } if (!na(scriptval)) { /* define "scriptopt" */ gretl_scalar_add("scriptopt", scriptval); } /* misc. interactive-mode setup */ if (!batch) { check_help_file(); fb = stdin; push_input_file(fb); if (!runit && !data_status) { fputs(_("Type \"open filename\" to open a data set\n"), stdout); } } #ifdef HAVE_READLINE initialize_readline(); #endif if (batch || runit) { /* re-initialize: will be incremented by "run" cmd */ runit = 0; if (makepkg) { set_gretl_echo(0); } if (strchr(runfile, ' ')) { sprintf(line, "run \"%s\"", runfile); } else { sprintf(line, "run %s", runfile); } err = cli_exec_line(&state, dset, cmdprn); if (err && fb == NULL) { exit(EXIT_FAILURE); } } *linecopy = '\0'; /* main command loop */ while (cmd.ci != QUIT && fb != NULL && !xout) { if (err && gretl_error_is_fatal()) { gretl_abort(linecopy); } if (gretl_execute_loop()) { if (gretl_loop_exec(&state, dset, NULL)) { return 1; } } else { err = cli_get_input_line(&state); if (err) { errmsg(err, prn); break; } } if (!state.in_comment) { if (cmd.context == FOREIGN || cmd.context == MPI || gretl_compiling_python(line)) { tailstrip(line); } else { err = maybe_get_input_line_continuation(line); if (err) { errmsg(err, prn); break; } } } strcpy(linecopy, line); tailstrip(linecopy); err = cli_exec_line(&state, dset, cmdprn); } /* end of get commands loop */ if (!err) { err = gretl_if_state_check(0); if (err) { errmsg(err, prn); } } if (makepkg && !err) { switch_ext(filearg, runfile, "gfn"); sprintf(line, "makepkg %s\n", filearg); cli_exec_line(&state, dset, cmdprn); } /* leak check -- try explicitly freeing all memory allocated */ destroy_working_model(model); destroy_dataset(dset); if (fb != stdin && fb != NULL) { fclose(fb); } free(line); gretl_print_destroy(prn); gretl_cmd_free(&cmd); libgretl_cleanup(); return 0; }
int gretl_string_table_print (gretl_string_table *gst, DATASET *dset, const char *fname, PRN *prn) { series_table *st; const char *fshort; char stname[MAXLEN]; FILE *fp; int i, j, ncols = 0; int err = 0; if (gst == NULL) { return E_DATA; } strcpy(stname, "string_table.txt"); gretl_path_prepend(stname, gretl_workdir()); fp = gretl_fopen(stname, "w"); if (fp == NULL) { return E_FOPEN; } fshort = strrchr(fname, SLASH); if (fshort != NULL) { fprintf(fp, "%s\n", fshort + 1); } else { fprintf(fp, "%s\n", fname); } ncols = (gst->cols_list != NULL)? gst->cols_list[0] : 0; if (ncols > 0) { fputc('\n', fp); fputs(_("One or more non-numeric variables were found.\n" "These variables have been given numeric codes as follows.\n\n"), fp); if (gst->extra != NULL) { fputs(_("In addition, some mappings from numerical values to string\n" "labels were found, and are printed below.\n\n"), fp); } } for (i=0; i<ncols; i++) { int vi = gst->cols_list[i+1]; st = gst->cols[i]; if (i > 0) { fputc('\n', fp); } fprintf(fp, _("String code table for variable %d (%s):\n"), vi, dset->varname[vi]); for (j=0; j<st->n_strs; j++) { fprintf(fp, "%3d = '%s'\n", j+1, st->strs[j]); } if (dset->varinfo != NULL) { series_attach_string_table(dset, vi, st); gst->cols[i] = NULL; } } if (gst->extra != NULL) { fputs(gst->extra, fp); } pprintf(prn, _("String code table written to\n %s\n"), stname); fclose(fp); set_string_table_written(); return err; }