summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeo Tenenbaum <pommicket@gmail.com>2021-01-25 19:34:30 -0500
committerLeo Tenenbaum <pommicket@gmail.com>2021-01-25 19:34:30 -0500
commit9f8e74337832533a806ac0c46ee993a927f0c057 (patch)
treebbe437b4c3c68ea5c905ad957bb4307af2031354
parent795262f69900af674156bed2bcd0fdb57dbbb55e (diff)
error log, fix memory leak, moved globals to Ted
-rw-r--r--Makefile1
-rw-r--r--Untitled2
-rw-r--r--main.c38
-rw-r--r--ted-base.c14
-rw-r--r--ted.h21
-rw-r--r--ui.c25
6 files changed, 59 insertions, 42 deletions
diff --git a/Makefile b/Makefile
index babddc3..41bd8a5 100644
--- a/Makefile
+++ b/Makefile
@@ -19,6 +19,7 @@ install: release
@[ -w `dirname $(INSTALL_BIN_DIR)` ] || { echo "You need permission to write to $(INSTALL_BIN_DIR). Try running with sudo/as root." && exit 1; }
mkdir -p $(GLOBAL_DATA_DIR) $(LOCAL_DATA_DIR)
+ chown `logname`:`logname` $(LOCAL_DATA_DIR)
cp -r assets $(GLOBAL_DATA_DIR)
install -m 644 ted.cfg $(GLOBAL_DATA_DIR)
[ ! -e $(LOCAL_DATA_DIR)/ted.cfg ] && install -o `logname` -g `logname` -m 644 ted.cfg $(LOCAL_DATA_DIR) || :
diff --git a/Untitled b/Untitled
index d222a6a..b375b10 100644
--- a/Untitled
+++ b/Untitled
@@ -1,2 +1,2 @@
1 2 3 4
-56 78 910 1112 \ No newline at end of file
+56 78 910 1112
diff --git a/main.c b/main.c
index 4bf0a92..aed49c9 100644
--- a/main.c
+++ b/main.c
@@ -122,11 +122,22 @@ int main(int argc, char **argv) {
strbuf_printf(ted_home, "%ls", home);
CoTaskMemFree(home);
}
+ strbuf_printf(ted->global_data_dir, "C:\\Program Files\\ted");
#else
char *home = getenv("HOME");
- strbuf_printf(ted_home, "%s", home);
- strbuf_printf(ted_local_data_dir, "%s/.local/share/ted", home);
+ strbuf_printf(ted->home, "%s", home);
+ strbuf_printf(ted->local_data_dir, "%s/.local/share/ted", home);
+ strbuf_printf(ted->global_data_dir, "/usr/share/ted");
#endif
+
+ }
+
+ FILE *log = NULL;
+ {
+ // open log file
+ char log_filename[TED_PATH_MAX];
+ strbuf_printf(log_filename, "%s/log.txt", ted->local_data_dir);
+ log = fopen(log_filename, "w");
}
{ // get current working directory
@@ -153,7 +164,7 @@ int main(int argc, char **argv) {
char *last_slash = strrchr(executable_path, '/');
if (last_slash) {
*last_slash = '\0';
- ted_search_cwd = streq(cwd, executable_path);
+ ted->search_cwd = streq(cwd, executable_path);
}
}
#endif
@@ -164,14 +175,14 @@ int main(int argc, char **argv) {
{
// read global configuration file first to establish defaults
char global_config_filename[TED_PATH_MAX];
- strbuf_printf(global_config_filename, "%s" PATH_SEPARATOR_STR "ted.cfg", ted_global_data_dir);
+ strbuf_printf(global_config_filename, "%s" PATH_SEPARATOR_STR "ted.cfg", ted->global_data_dir);
if (fs_file_exists(global_config_filename))
config_read(ted, global_config_filename);
}
{
// read local configuration file
char config_filename[TED_PATH_MAX];
- if (ted_get_file("ted.cfg", config_filename, sizeof config_filename))
+ if (ted_get_file(ted, "ted.cfg", config_filename, sizeof config_filename))
config_read(ted, config_filename);
else
ted_seterr(ted, "Couldn't find config file (ted.cfg), not even the backup one that should have come with ted.");
@@ -187,7 +198,7 @@ int main(int argc, char **argv) {
{ // set icon
char icon_filename[TED_PATH_MAX];
- if (ted_get_file("assets/icon.bmp", icon_filename, sizeof icon_filename)) {
+ if (ted_get_file(ted, "assets/icon.bmp", icon_filename, sizeof icon_filename)) {
SDL_Surface *icon = SDL_LoadBMP(icon_filename);
SDL_SetWindowIcon(window, icon);
SDL_FreeSurface(icon);
@@ -447,6 +458,17 @@ int main(int argc, char **argv) {
if (ted_haserr(ted)) {
ted->error_time = time_get_seconds();
str_cpy(ted->error_shown, sizeof ted->error_shown, ted->error);
+
+ { // output error to log file
+ char tstr[256];
+ time_t t = time(NULL);
+ struct tm *tm = localtime(&t);
+ strftime(tstr, sizeof tstr, "%Y-%m-%d %H:%M:%S", tm);
+ if (log) {
+ fprintf(log, "[ERROR %s] %s\n", tstr, ted->error);
+ fflush(log);
+ }
+ }
ted_clearerr(ted);
}
@@ -458,7 +480,6 @@ int main(int argc, char **argv) {
// stop showing error
ted->error_shown[0] = '\0';
} else {
- // @TODO(eventually): output a log
// @TODO: middle click to dismiss
float padding = settings->padding;
float char_width = text_font_char_width(font);
@@ -508,9 +529,12 @@ int main(int argc, char **argv) {
SDL_GL_SwapWindow(window);
}
+
if (ted->menu)
menu_close(ted, false); // free any memory used by the current menu
+ fclose(log);
+
SDL_GL_DeleteContext(glctx);
SDL_DestroyWindow(window);
SDL_Quit();
diff --git a/ted-base.c b/ted-base.c
index 83c8208..b3a593e 100644
--- a/ted-base.c
+++ b/ted-base.c
@@ -43,19 +43,19 @@ static void *ted_realloc(Ted *ted, void *p, size_t new_size) {
}
// Check the various places a file could be, and return the full path.
-static Status ted_get_file(char const *name, char *out, size_t outsz) {
- if (ted_search_cwd && fs_file_exists(name)) {
+static Status ted_get_file(Ted const *ted, char const *name, char *out, size_t outsz) {
+ if (ted->search_cwd && fs_file_exists(name)) {
// check in current working directory
str_cpy(out, outsz, name);
return true;
}
- if (*ted_local_data_dir) {
- str_printf(out, outsz, "%s" PATH_SEPARATOR_STR "%s", ted_local_data_dir, name);
+ if (*ted->local_data_dir) {
+ str_printf(out, outsz, "%s" PATH_SEPARATOR_STR "%s", ted->local_data_dir, name);
if (fs_file_exists(out))
return true;
}
- if (*ted_global_data_dir) {
- str_printf(out, outsz, "%s" PATH_SEPARATOR_STR "%s", ted_global_data_dir, name);
+ if (*ted->global_data_dir) {
+ str_printf(out, outsz, "%s" PATH_SEPARATOR_STR "%s", ted->global_data_dir, name);
if (fs_file_exists(out))
return true;
}
@@ -66,7 +66,7 @@ static Status ted_get_file(char const *name, char *out, size_t outsz) {
// *out is left unchanged on failure.
static void ted_load_font(Ted *ted, char const *filename, Font **out) {
char font_filename[TED_PATH_MAX];
- if (ted_get_file(filename, font_filename, sizeof font_filename)) {
+ if (ted_get_file(ted, filename, font_filename, sizeof font_filename)) {
Font *font = text_font_load(font_filename, ted->settings.text_size);
if (font) {
if (*out) {
diff --git a/ted.h b/ted.h
index 15c13c6..bdc2f60 100644
--- a/ted.h
+++ b/ted.h
@@ -118,20 +118,11 @@ typedef struct Ted {
TextBuffer main_buffer;
double error_time; // time error box was opened (in seconds -- see time_get_seconds)
KeyAction key_actions[KEY_COMBO_COUNT];
+ bool search_cwd; // should the working directory be searched for files? set to true if the executable isn't "installed"
+ char local_data_dir[TED_PATH_MAX];
+ char global_data_dir[TED_PATH_MAX];
+ char home[TED_PATH_MAX];
char cwd[TED_PATH_MAX]; // current working directory
- char error[256];
- char error_shown[256]; // error display in box on screen
+ char error[512];
+ char error_shown[512]; // error display in box on screen
} Ted;
-
-// should the working directory be searched for files? set to true if the executable isn't "installed"
-static bool ted_search_cwd = false;
-static char const ted_global_data_dir[] =
-#if _WIN32
- "C:\\Program Files\\ted";
-#else
- "/usr/share/ted";
-#endif
-
-// filled out in main()
-static char ted_local_data_dir[TED_PATH_MAX];
-static char ted_home[TED_PATH_MAX]; // home directory -- this is what ~ expands to
diff --git a/ui.c b/ui.c
index e9fea11..7cabdc6 100644
--- a/ui.c
+++ b/ui.c
@@ -101,10 +101,10 @@ static int qsort_file_entry_cmp(void const *av, void const *bv, void *search_ter
return strcmp_case_insensitive(a->name, b->name);
}
-static Status file_selector_cd_(FileSelector *fs, char const *path, int symlink_depth);
+static Status file_selector_cd_(Ted const *ted, FileSelector *fs, char const *path, int symlink_depth);
// cd to the directory `name`. `name` cannot include any path separators.
-static Status file_selector_cd1(FileSelector *fs, char const *name, size_t name_len, int symlink_depth) {
+static Status file_selector_cd1(Ted const *ted, FileSelector *fs, char const *name, size_t name_len, int symlink_depth) {
char *const cwd = fs->cwd;
if (name_len == 0 || (name_len == 1 && name[0] == '.')) {
@@ -116,7 +116,7 @@ static Status file_selector_cd1(FileSelector *fs, char const *name, size_t name_
// just in case the user's HOME happens to be accidentally set to, e.g. '/foo/~', make
// sure we don't recurse infinitely
if (symlink_depth < 32) {
- return file_selector_cd_(fs, ted_home, symlink_depth + 1);
+ return file_selector_cd_(ted, fs, ted->home, symlink_depth + 1);
} else {
return false;
}
@@ -155,7 +155,7 @@ static Status file_selector_cd1(FileSelector *fs, char const *name, size_t name_
if (bytes != -1) {
// this is a symlink
link_to[bytes] = '\0';
- return file_selector_cd_(fs, link_to, symlink_depth + 1);
+ return file_selector_cd_(ted, fs, link_to, symlink_depth + 1);
}
} else {
return false;
@@ -174,7 +174,7 @@ static Status file_selector_cd1(FileSelector *fs, char const *name, size_t name_
}
-static Status file_selector_cd_(FileSelector *fs, char const *path, int symlink_depth) {
+static Status file_selector_cd_(Ted const *ted, FileSelector *fs, char const *path, int symlink_depth) {
char *const cwd = fs->cwd;
if (path[0] == '\0') return true;
@@ -202,7 +202,7 @@ static Status file_selector_cd_(FileSelector *fs, char const *path, int symlink_
while (*p) {
size_t len = strcspn(p, PATH_SEPARATOR_STR);
- if (!file_selector_cd1(fs, p, len, symlink_depth))
+ if (!file_selector_cd1(ted, fs, p, len, symlink_depth))
return false;
p += len;
p += strspn(p, PATH_SEPARATOR_STR);
@@ -213,9 +213,9 @@ static Status file_selector_cd_(FileSelector *fs, char const *path, int symlink_
// go to the directory `path`. make sure `path` only contains path separators like PATH_SEPARATOR, not any
// other members of ALL_PATH_SEPARATORS
// returns false if this path doesn't exist or isn't a directory
-static bool file_selector_cd(FileSelector *fs, char const *path) {
+static bool file_selector_cd(Ted const *ted, FileSelector *fs, char const *path) {
fs->selected = 0;
- return file_selector_cd_(fs, path, 0);
+ return file_selector_cd_(ted, fs, path, 0);
}
// returns the name of the selected file, or NULL
@@ -254,7 +254,7 @@ static char *file_selector_update(Ted *ted, FileSelector *fs) {
if (strchr(ALL_PATH_SEPARATORS, *p))
*p = PATH_SEPARATOR;
- if (file_selector_cd(fs, dir_name)) {
+ if (file_selector_cd(ted, fs, dir_name)) {
buffer_delete_chars_at_pos(line_buffer, buffer_start_of_file(line_buffer), last_path_sep + 1); // delete up to and including the last path separator
buffer_clear_undo_redo(line_buffer);
} else {
@@ -262,6 +262,7 @@ static char *file_selector_update(Ted *ted, FileSelector *fs) {
size_t nchars = search_term32.len - first_path_sep;
buffer_delete_chars_at_pos(line_buffer, pos, (i64)nchars);
}
+ free(dir_name);
}
}
@@ -288,7 +289,7 @@ static char *file_selector_update(Ted *ted, FileSelector *fs) {
if (path) return str_dup(path);
break;
case FS_DIRECTORY:
- file_selector_cd(fs, name);
+ file_selector_cd(ted, fs, name);
buffer_clear(line_buffer); // clear search term
break;
default: break;
@@ -308,7 +309,7 @@ static char *file_selector_update(Ted *ted, FileSelector *fs) {
if (entry->path) return str_dup(entry->path);
break;
case FS_DIRECTORY:
- file_selector_cd(fs, entry->name);
+ file_selector_cd(ted, fs, entry->name);
buffer_clear(line_buffer); // clear search term
break;
default: break;
@@ -329,7 +330,7 @@ static char *file_selector_update(Ted *ted, FileSelector *fs) {
else
ted_seterr(ted, "Can't list directory %s.", cwd);
}
- file_selector_cd(fs, "..");
+ file_selector_cd(ted, fs, "..");
}
if (files) {