#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 <wchar.h>
#include <ncurses.h>
#include <sys/wait.h>
#include <wordexp.h>
#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"
#include "xmalloc.h"
#include "y.tab.h"
#include "xlsx.h"
#include "xls.h"
#include "screen.h"
extern struct ent * freeents;
extern int yyparse(void);
/* erase the database (tbl, etc.) */
void erasedb() {
int r, c;
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] != NULL)
scxfree(colformat[c]);
colformat[c] = NULL;
}
maxrow = 0;
maxcol = 0;
clean_range();
calc_order = BYROWS;
prescale = 1.0;
tbl_style = 0;
optimize = 0;
currow = curcol = 0;
loadrc();
*curfile = '\0';
}
void loadrc(void) {
char rcpath[PATHLEN];
char * home;
if ((home = getenv("HOME"))) {
memset(rcpath, 0, sizeof(rcpath));
strncpy(rcpath, home, sizeof(rcpath) - (sizeof("/.scimrc") + 1));
strcat(rcpath, "/.scimrc");
(void) readfile(rcpath, 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"))) {
sc_error("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];
wordexp_t p;
if (! curfile[0] && wcslen(inputline) < 3) { // casos ":w" ":w!" ":x" ":x!"
sc_error("There is no filename");
return -1;
}
if (inputline[1] == L'!') force_rewrite = 1;
wcstombs(name, inputline, BUFFERSIZE);
del_range_chars(name, 0, 1 + force_rewrite);
wordexp(name, &p, 0);
if (! force_rewrite && file_exists(p.we_wordv[0])) {
sc_error("File already exists. Use \"!\" to force rewrite.");
wordfree(&p);
return -1;
}
if (wcslen(inputline) > 2) {
strcpy(curfile, p.we_wordv[0]);
}
if (wcslen(inputline) > 2 && str_in_str(curfile, ".") == -1)
sprintf(curfile