From 408d03be285010d852fa4234ea03f80e7ee5403a Mon Sep 17 00:00:00 2001 From: James Maki Date: Fri, 7 May 2010 10:52:16 -0500 Subject: clean up some memory --- src/atcmd.c | 5 +++++ src/global.h | 38 +------------------------------------- src/sms_config.c | 40 +++++++++++++++++++++++++++------------- src/sms_main.c | 48 +++++++++++++++++++++++++++++++++++++++++++----- 4 files changed, 76 insertions(+), 55 deletions(-) diff --git a/src/atcmd.c b/src/atcmd.c index e153d39..95d86dc 100644 --- a/src/atcmd.c +++ b/src/atcmd.c @@ -1139,6 +1139,11 @@ int sms_device_open(void) int fd = -1; int tmp; + if (!Global.core.device) { + log_error("device is not set"); + return -1; + } + lock_path = device_lock(Global.core.device); if (!lock_path) { sms_device_close(fd); diff --git a/src/global.h b/src/global.h index 4a73d9e..fe8f5c2 100644 --- a/src/global.h +++ b/src/global.h @@ -53,42 +53,6 @@ struct global_config { struct global_send_email send_email; }; -#if __MAIN_FILE_C -struct global_config Global = { - .user = { - .name = NULL, - .email = NULL, - }, - .core = { - .verbose = false, - .interactive = true, - .sms_init = true, - .baud_rate = B115200, - .read_timeout = 5000, - .device = DEFAULT_DEVICE, - .msg_store_read = "MT", - .msg_store_send = "MT", - .msg_store_new = "MT", - .pb_store = "ME", - .editor = "vi", - .edit_file = "${HOME}/.smsmsg", - }, - .smtp = { - .server = NULL, - .port = 0, - .user = NULL, - .passwd = NULL, - .encryption = NULL, - }, - .send_email = { - .domain = NULL, - }, -}; -#else -extern struct global_config Global; -#endif - - +GLOBAL_EXTERN struct global_config Global; #endif /* ~__GLOBAL_H */ - diff --git a/src/sms_config.c b/src/sms_config.c index 9386ff7..2d44e18 100644 --- a/src/sms_config.c +++ b/src/sms_config.c @@ -40,7 +40,19 @@ static int boolean_value(const char *str) return !strcmp(str, "true") ? true : false; } -static int config_open(struct config_info *config, char *filename) +static int config_close(struct config_info *config) +{ + yaml_parser_delete(&config->parser); + fclose(config->file); + free(config->filename); + free(config->section); + free(config->key); + free(config->value); + + return 0; +} + +static int config_open(struct config_info *config, const char *filename) { int err; @@ -71,18 +83,6 @@ static int config_open(struct config_info *config, char *filename) return 0; } -static int config_close(struct config_info *config) -{ - yaml_parser_delete(&config->parser); - fclose(config->file); - free(config->filename); - free(config->section); - free(config->key); - free(config->value); - - return 0; -} - static int config_next_event(struct config_info *config) { int err; @@ -117,18 +117,25 @@ static int config_set_core_value(const char *key, const char *value) } else if (!strcmp("read-timeout", key)) { Global.core.read_timeout = atoi(value); } else if (!strcmp("device", key)) { + free(Global.core.device); Global.core.device = strdup(value); } else if (!strcmp("msg-store-read", key)) { + free(Global.core.msg_store_read); Global.core.msg_store_read = strdup(value); } else if (!strcmp("msg-store-send", key)) { + free(Global.core.msg_store_send); Global.core.msg_store_send = strdup(value); } else if (!strcmp("msg-store-new", key)) { + free(Global.core.msg_store_new); Global.core.msg_store_new = strdup(value); } else if (!strcmp("pb-store", key)) { + free(Global.core.pb_store); Global.core.pb_store = strdup(value); } else if (!strcmp("editor", key)) { + free(Global.core.editor); Global.core.editor = strdup(value); } else if (!strcmp("edit-file", key)) { + free(Global.core.edit_file); Global.core.edit_file = strdup(value); } @@ -140,14 +147,18 @@ static int config_set_smtp_value(const char *key, const char *value) log_debug("try setting smtp.%s to '%s'", key, value); if (!strcmp("server", key)) { + free(Global.smtp.server); Global.smtp.server = strdup(value); } else if (!strcmp("port", key)) { Global.smtp.port = atoi(value); } else if (!strcmp("user", key)) { + free(Global.smtp.user); Global.smtp.user = strdup(value); } else if (!strcmp("passwd", key)) { + free(Global.smtp.passwd); Global.smtp.passwd = strdup(value); } else if (!strcmp("encryption", key)) { + free(Global.smtp.encryption); Global.smtp.encryption = strdup(value); } @@ -159,6 +170,7 @@ static int config_set_send_email_value(const char *key, const char *value) log_debug("try setting send-email.%s to %s", key, value); if (!strcmp("domain", key)) { + free(Global.send_email.domain); Global.send_email.domain = strdup(value); } @@ -170,8 +182,10 @@ static int config_set_user_value(const char *key, const char *value) log_debug("try setting user.%s to %s", key, value); if (!strcmp("name", key)) { + free(Global.user.name); Global.user.name = strdup(value); } else if (!strcmp("email", key)) { + free(Global.user.email); Global.user.email = strdup(value); } diff --git a/src/sms_main.c b/src/sms_main.c index d1155b8..4a955b8 100644 --- a/src/sms_main.c +++ b/src/sms_main.c @@ -42,6 +42,37 @@ #include "atcmd.h" #include "sms_config.h" +static int global_init(void) +{ + memset(&Global, 0, sizeof(Global)); + + Global.user.name = NULL; + Global.user.email = NULL; + + Global.core.verbose = false; + Global.core.interactive = true; + Global.core.sms_init = true; + Global.core.baud_rate = B115200; + Global.core.read_timeout = 5000; + Global.core.device = strdup(DEFAULT_DEVICE); + Global.core.msg_store_read = strdup("MT"); + Global.core.msg_store_send = strdup("MT"); + Global.core.msg_store_new = strdup("MT"); + Global.core.pb_store = strdup("ME"); + Global.core.editor = strdup("vi"); + Global.core.edit_file = strdup("${HOME}/.smsmsg"); + + Global.smtp.server = NULL; + Global.smtp.port = 25; + Global.smtp.user = NULL; + Global.smtp.passwd = NULL; + Global.smtp.encryption = NULL; + + Global.send_email.domain = NULL; + + return 0; +} + struct sms_cmd { const char *object; int (*call)(int argc, char **argv); @@ -213,6 +244,8 @@ int main(int argc, char *argv[]) xprintf_init(); + global_init(); + err = sms_config_load(); if (err < 0) { log_error("sms config load error"); @@ -237,7 +270,8 @@ int main(int argc, char *argv[]) break; case CMD_OPT_DEVICE: - Global.core.device = optarg; + free(Global.core.device); + Global.core.device = strdup(optarg); break; case CMD_OPT_BAUD_RATE: @@ -254,19 +288,23 @@ int main(int argc, char *argv[]) break; case CMD_OPT_MSG_STORE_READ: - Global.core.msg_store_read = optarg; + free(Global.core.msg_store_read); + Global.core.msg_store_read = strdup(optarg); break; case CMD_OPT_MSG_STORE_SEND: - Global.core.msg_store_send = optarg; + free(Global.core.msg_store_send); + Global.core.msg_store_send = strdup(optarg); break; case CMD_OPT_MSG_STORE_NEW: - Global.core.msg_store_new = optarg; + free(Global.core.msg_store_new); + Global.core.msg_store_new = strdup(optarg); break; case CMD_OPT_PHONEBOOK_STORE: - Global.core.pb_store = optarg; + free(Global.core.pb_store); + Global.core.pb_store = strdup(optarg); break; case CMD_OPT_VERSION: -- cgit v1.2.3