#include <pwd.h>
#include <sys/stat.h>
#include <time.h>
#include <utime.h>
#include <fcntl.h>
#include <signal.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <curses.h>
#include <sys/wait.h> // for wait
#include "conf.h"
#include "maps.h"
#include "yank.h"
#include "cmds.h"
#include "file.h"
#include "marks.h"
#include "lex.h"
#include "format.h"
#include "interp.h"
#include "utils/string.h"
#include "utils/dictionary.h"
#include "cmds_edit.h"
#include "color.h" // for set_ucolor
#include "xmalloc.h" // for scxfree
#include "y.tab.h"
#include "xlsx.h"
#include "xls.h"
extern struct ent * freeents;
/* erase the database (tbl, etc.) */
void erasedb() {
int r, c;
char * home;
for (c = 0; c <= maxcol; c++) {
fwidth[c] = DEFWIDTH;
precision[c] = DEFPREC;
realfmt[c] = DEFREFMT;
}
for (r = 0; r <= maxrow; r++) {
register struct ent ** pp = ATBL(tbl, r, 0);
for (c = 0; c++ <= maxcol; pp++)
if (*pp != NULL) {
//(*pp)->next = freeents; /* save [struct ent] for reuse */
//freeents = *pp;
clearent(*pp);
}
}
for (c = 0; c < COLFORMATS; c++)
if (colformat[c]) {
scxfree(colformat[c]);
colformat[c] = NULL;
}
maxrow = 0;
maxcol = 0;
clean_range();
propagation = 10;
calc_order = BYROWS;
prescale = 1.0;
tbl_style = 0;
optimize = 0;
currow = curcol = 0;
// if (usecurses && has_colors())
// color_set(0, NULL);
/*
if (mdir) {
scxfree(mdir);
mdir = NULL;
}
if (autorun) {
scxfree(autorun);
autorun = NULL;
}
for (c = 0; c < FKEYS; c++)
if (fkey[c]) {
scxfree(fkey[c]);
fkey[c] = NULL;
}
*/
// Load $HOME/.scrc if present.
if ((home = getenv("HOME"))) {
strcpy(curfile, home);
strcat(curfile, "/.scimrc");
if ((c = open(curfile, O_RDONLY)) > -1) {
close(c);
(void) readfile(curfile, 0);
}
}
*curfile = '\0';
}
// function that checks if a file exists.
// returns 1 if so. returns 0 otherwise.
int file_exists(const char * fname) {
FILE * file;
if ((file = fopen(fname, "r"))) {
fclose(file);
return 1;
}
return 0;
}
// This function checks if a file suffered mods since it was open
int modcheck() {
if (modflg && ! atoi(get_conf_value("nocurses"))) {
scerror("File not saved since last change. Add '!' to force");
return(1);
}
return 0;
}
// This function handles the save file process in SC-IM format
// returns 0 if OK
// return -1 on error
int savefile() {
int force_rewrite = 0;
char name[BUFFERSIZE];
if (! curfile[0] && strlen(inputline) < 3) { // casos ":w" ":w!" ":x" ":x!"
scerror("There is no filename");
return -1;
}
if (inputline[1] == '!') force_rewrite = 1;
strcpy(name, inputline);
del_range_chars(name, 0, 1 + force_rewrite);
if (! force_rewrite && file_exists(name)) {
scerror("File already exists. Use \"!\" to force rewrite.");
return -1;
}
if (strlen(inputline) > 2) {
strcpy(curfile, name);
}
if (writefile(curfile, 0, 0, maxrow, maxcol) < 0) {
scerror("File could not be saved");
return -1;
}
return 0;
}
// Funcion que graba un archivo
// recibe un rango como parametros y un nombre de archivo a generar
int writefile(char * fname, int r0, int c0, int rn, int cn) {
register FILE *f;
char save[PATHLEN];
char tfname[PATHLEN];
//long namelen;
//char * tpp;
int pid;
/*
if (*fname == '\0') {
if (isatty(STDOUT_FILENO) || *curfile != '\0')
fname = curfile;
else {
write_fd(stdout, r0, c0, rn, cn);
return 0;
}
}
*/
/*
if ((tpp = strrchr(fname, '/')) == NULL)
namelen = pathconf(".", _PC_NAME_MAX);
else {
*tpp = '\0';
namelen = pathconf(fname, _PC_NAME_MAX);
*tpp = '/';
}
*/
(void) strcpy(tfname, fname);
/*
for (tpp = tfname; *tpp != '\0'; tpp++)
if (*tpp == '\\' && *(tpp + 1) == '"')