From f533c6c1622438f738348c22ff754d9952fb3f01 Mon Sep 17 00:00:00 2001 From: James Booth Date: Fri, 9 Mar 2012 01:06:55 +0000 Subject: [PATCH] Usage of const --- command.c | 21 +++++++++--------- contact_list.c | 9 ++++---- contact_list.h | 4 ++-- history.c | 3 ++- history.h | 2 +- input_win.c | 12 +++++----- jabber.c | 14 ++++++------ jabber.h | 6 ++--- profanity.c | 2 +- profanity.h | 2 +- status_bar.c | 6 ++--- title_bar.c | 4 ++-- windows.c | 60 +++++++++++++++++++++++++++----------------------- windows.h | 33 ++++++++++++++------------- 14 files changed, 94 insertions(+), 84 deletions(-) diff --git a/command.c b/command.c index 7517ec55..d062138e 100644 --- a/command.c +++ b/command.c @@ -30,15 +30,15 @@ #include "windows.h" #include "util.h" -static int _handle_command(char *command, char *inp); +static int _handle_command(const char * const command, const char * const inp); static int _cmd_quit(void); static int _cmd_help(void); static int _cmd_who(void); static int _cmd_ros(void); -static int _cmd_connect(char *inp); -static int _cmd_msg(char *inp); -static int _cmd_close(char *inp); -static int _cmd_default(char *inp); +static int _cmd_connect(const char * const inp); +static int _cmd_msg(const char * const inp); +static int _cmd_close(const char * const inp); +static int _cmd_default(const char * const inp); int process_input(char *inp) { @@ -64,7 +64,7 @@ int process_input(char *inp) return result; } -static int _handle_command(char *command, char *inp) +static int _handle_command(const char * const command, const char * const inp) { int result = FALSE; @@ -89,7 +89,7 @@ static int _handle_command(char *command, char *inp) return result; } -static int _cmd_connect(char *inp) +static int _cmd_connect(const char * const inp) { int result = FALSE; jabber_status_t conn_status = jabber_connection_status(); @@ -160,7 +160,7 @@ static int _cmd_who(void) return TRUE; } -static int _cmd_msg(char *inp) +static int _cmd_msg(const char * const inp) { char *usr = NULL; char *msg = NULL; @@ -178,6 +178,7 @@ static int _cmd_msg(char *inp) strtok(inp_cpy, " "); usr = strtok(NULL, " "); if ((usr != NULL) && (strlen(inp) > (5 + strlen(usr) + 1))) { + // get message msg = strndup(inp+5+strlen(usr)+1, strlen(inp)-(5+strlen(usr)+1)); if (msg != NULL) { jabber_send(msg, usr); @@ -193,7 +194,7 @@ static int _cmd_msg(char *inp) return TRUE; } -static int _cmd_close(char *inp) +static int _cmd_close(const char * const inp) { if (!win_close_win()) cons_bad_command(inp); @@ -201,7 +202,7 @@ static int _cmd_close(char *inp) return TRUE; } -static int _cmd_default(char *inp) +static int _cmd_default(const char * const inp) { if (win_in_chat()) { char *recipient = win_get_recipient(); diff --git a/contact_list.c b/contact_list.c index 9ce5f843..82bbe243 100644 --- a/contact_list.c +++ b/contact_list.c @@ -20,7 +20,6 @@ * */ -#include #include #include @@ -35,7 +34,7 @@ struct _contact_t { // the contact list static struct _contact_t *_contact_list = NULL; -static struct _contact_t * _make_contact(char *contact); +static struct _contact_t * _make_contact(const char * const contact); void contact_list_clear(void) { @@ -52,7 +51,7 @@ void contact_list_clear(void) } } -int contact_list_remove(char *contact) +int contact_list_remove(const char * const contact) { if (!_contact_list) { return 0; @@ -81,7 +80,7 @@ int contact_list_remove(char *contact) } } -int contact_list_add(char *contact) +int contact_list_add(const char * const contact) { if (!_contact_list) { _contact_list = _make_contact(contact); @@ -136,7 +135,7 @@ struct contact_list *get_contact_list(void) return list; } -static struct _contact_t * _make_contact(char *contact) +static struct _contact_t * _make_contact(const char * const contact) { struct _contact_t *new = (struct _contact_t *) malloc(sizeof(struct _contact_t)); new->contact = (char *) malloc((strlen(contact) + 1) * sizeof(char)); diff --git a/contact_list.h b/contact_list.h index 99d276fe..f751f2ab 100644 --- a/contact_list.h +++ b/contact_list.h @@ -29,8 +29,8 @@ struct contact_list { }; void contact_list_clear(void); -int contact_list_add(char *contact); -int contact_list_remove(char *contact); +int contact_list_add(const char * const contact); +int contact_list_remove(const char * const contact); struct contact_list *get_contact_list(void); #endif diff --git a/history.c b/history.c index 9053b54f..0f4dfae6 100644 --- a/history.c +++ b/history.c @@ -39,7 +39,8 @@ void history_init(void) _pos = -1; } -void history_append(char *inp) +// FIXME: Roll history when full +void history_append(const char * const inp) { if (_size < MAX_HISTORY) { _history[_size] = (char*) malloc(strlen(inp) * sizeof(char)); diff --git a/history.h b/history.h index b96a728e..7afca5e1 100644 --- a/history.h +++ b/history.h @@ -25,7 +25,7 @@ #define HISTORY_H void history_init(void); -void history_append(char *inp); +void history_append(const char * const inp); char *history_previous(void); char *history_next(void); diff --git a/input_win.c b/input_win.c index 1a895ebd..fc242c99 100644 --- a/input_win.c +++ b/input_win.c @@ -43,9 +43,9 @@ static WINDOW *inp_win; -static int _handle_edit(int ch, char *input, int *size); -static int _printable(int ch); -static void _replace_input(char *input, char *new_input, int *size); +static int _handle_edit(const int ch, char *input, int *size); +static int _printable(const int ch); +static void _replace_input(char *input, const char * const new_input, int *size); void create_input_window(void) { @@ -133,7 +133,7 @@ void inp_put_back(void) * key press: up, down, left, right or backspace * return 0 if it wasnt */ -static int _handle_edit(int ch, char *input, int *size) +static int _handle_edit(const int ch, char *input, int *size) { int i; char *prev = NULL; @@ -196,7 +196,7 @@ static int _handle_edit(int ch, char *input, int *size) } } -static int _printable(int ch) +static int _printable(const int ch) { return (ch != ERR && ch != '\n' && ch != KEY_PPAGE && ch != KEY_NPAGE && ch != KEY_F(1) && ch != KEY_F(2) && ch != KEY_F(3) && @@ -205,7 +205,7 @@ static int _printable(int ch) ch != KEY_F(10)); } -static void _replace_input(char *input, char *new_input, int *size) +static void _replace_input(char *input, const char * const new_input, int *size) { int i; diff --git a/jabber.c b/jabber.c index 500838a8..b6d54fbb 100644 --- a/jabber.c +++ b/jabber.c @@ -65,15 +65,15 @@ static void _jabber_conn_handler(xmpp_conn_t * const conn, static int _jabber_message_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata); -static int _roster_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, - void * const userdata); +static int _roster_handler(xmpp_conn_t * const conn, + xmpp_stanza_t * const stanza, void * const userdata); static int _jabber_presence_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata); static int _ping_timed_handler(xmpp_conn_t * const conn, void * const userdata); -void jabber_init(int disable_tls) +void jabber_init(const int disable_tls) { jabber_conn.conn_status = JABBER_STARTED; jabber_conn.tls_disabled = disable_tls; @@ -84,7 +84,7 @@ jabber_status_t jabber_connection_status(void) return (jabber_conn.conn_status); } -jabber_status_t jabber_connect(char *user, char *passwd) +jabber_status_t jabber_connect(const char * const user, const char * const passwd) { xmpp_initialize(); @@ -126,7 +126,7 @@ void jabber_process_events(void) xmpp_run_once(jabber_conn.ctx, 10); } -void jabber_send(char *msg, char *recipient) +void jabber_send(const char * const msg, const char * const recipient) { xmpp_stanza_t *reply, *body, *text; @@ -224,8 +224,8 @@ static void _jabber_conn_handler(xmpp_conn_t * const conn, } } -static int _roster_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, - void * const userdata) +static int _roster_handler(xmpp_conn_t * const conn, + xmpp_stanza_t * const stanza, void * const userdata) { xmpp_stanza_t *query, *item; char *type, *name, *jid; diff --git a/jabber.h b/jabber.h index 4bffc56a..2a88613b 100644 --- a/jabber.h +++ b/jabber.h @@ -30,12 +30,12 @@ typedef enum { JABBER_DISCONNECTED } jabber_status_t; -void jabber_init(int disable_tls); +void jabber_init(const int disable_tls); jabber_status_t jabber_connection_status(void); -jabber_status_t jabber_connect(char *user, char *passwd); +jabber_status_t jabber_connect(const char * const user, const char * const passwd); void jabber_disconnect(void); void jabber_roster_request(void); void jabber_process_events(void); -void jabber_send(char *msg, char *recipient); +void jabber_send(const char * const msg, const char * const recipient); #endif diff --git a/profanity.c b/profanity.c index 9b7d7674..3470e7e1 100644 --- a/profanity.c +++ b/profanity.c @@ -58,7 +58,7 @@ void profanity_run(void) } -void profanity_init(int disable_tls) +void profanity_init(const int disable_tls) { log_init(); gui_init(); diff --git a/profanity.h b/profanity.h index 3013ff5b..92316ff6 100644 --- a/profanity.h +++ b/profanity.h @@ -23,7 +23,7 @@ #ifndef PROFANITY_H #define PROFANITY_H -void profanity_init(int disable_tls); +void profanity_init(const int disable_tls); void profanity_run(void); void profanity_shutdown(void); diff --git a/status_bar.c b/status_bar.c index 56cae9ce..d15997b4 100644 --- a/status_bar.c +++ b/status_bar.c @@ -67,7 +67,7 @@ void status_bar_refresh(void) } } -void status_bar_inactive(int win) +void status_bar_inactive(const int win) { int active_pos = 1 + ((win -1) * 3); @@ -81,7 +81,7 @@ void status_bar_inactive(int win) dirty = TRUE; } -void status_bar_active(int win) +void status_bar_active(const int win) { int active_pos = 1 + ((win -1) * 3); @@ -103,7 +103,7 @@ void status_bar_get_password(void) dirty = TRUE; } -void status_bar_print_message(const char *msg) +void status_bar_print_message(const char * const msg) { mvwprintw(status_bar, 0, 9, msg); diff --git a/title_bar.c b/title_bar.c index 17e2df2a..08ac44f5 100644 --- a/title_bar.c +++ b/title_bar.c @@ -38,7 +38,7 @@ void create_title_bar(void) dirty = TRUE; } -void title_bar_title() +void title_bar_title(void) { char *title = "Profanity. Type /help for help information."; title_bar_show(title); @@ -90,7 +90,7 @@ void title_bar_refresh(void) } } -void title_bar_show(char *title) +void title_bar_show(const char * const title) { wmove(title_bar, 0, 0); int i; diff --git a/windows.c b/windows.c index 6e7b50f0..939e5d74 100644 --- a/windows.c +++ b/windows.c @@ -43,16 +43,18 @@ static WINDOW * _cons_win = NULL; static int dirty; static void _create_windows(void); -static int _find_prof_win_index(char *contact); -static int _new_prof_win(char *contact); -static void _current_window_refresh(); -static void _win_switch_if_active(int i); +static int _find_prof_win_index(const char * const contact); +static int _new_prof_win(const char * const contact); +static void _current_window_refresh(void); +static void _win_switch_if_active(const int i); static void _win_show_time(WINDOW *win); -static void _win_show_user(WINDOW *win, char *user, int colour); -static void _win_show_message(WINDOW *win, char *message); -static void _show_status_string(WINDOW *win, char *from, char *show, char *status, - char *pre, char *default_show); -static void _cons_show_incoming_message(char *short_from, int win_index); +static void _win_show_user(WINDOW *win, const char * const user, const int colour); +static void _win_show_message(WINDOW *win, const char * const message); +static void _show_status_string(WINDOW *win, const char * const from, + const char * const show, const char * const status, const char * const pre, + const char * const default_show); +static void _cons_show_incoming_message(const char * const short_from, + const int win_index); void gui_init(void) { @@ -139,7 +141,7 @@ char *win_get_recipient(void) return recipient; } -void win_show_incomming_msg(char *from, char *message) +void win_show_incomming_msg(const char * const from, const char * const message) { char from_cpy[strlen(from) + 1]; strcpy(from_cpy, from); @@ -161,7 +163,8 @@ void win_show_incomming_msg(char *from, char *message) dirty = TRUE; } -void win_show_outgoing_msg(char *from, char *to, char *message) +void win_show_outgoing_msg(const char * const from, const char * const to, + const char * const message) { int win_index = _find_prof_win_index(to); if (win_index == NUM_WINS) @@ -178,7 +181,8 @@ void win_show_outgoing_msg(char *from, char *to, char *message) dirty = TRUE; } -void win_contact_online(char *from, char *show, char *status) +void win_contact_online(const char * const from, const char * const show, + const char * const status) { _show_status_string(_cons_win, from, show, status, "++", "online"); @@ -192,7 +196,8 @@ void win_contact_online(char *from, char *show, char *status) dirty = TRUE; } -void win_contact_offline(char *from, char *show, char *status) +void win_contact_offline(const char * const from, const char * const show, + const char * const status) { _show_status_string(_cons_win, from, show, status, "--", "offline"); @@ -228,7 +233,7 @@ void cons_help(void) dirty = TRUE; } -void cons_show_online_contacts(struct contact_list *list) +void cons_show_online_contacts(const struct contact_list * const list) { _win_show_time(_cons_win); wprintw(_cons_win, "Online contacts:\n"); @@ -244,7 +249,7 @@ void cons_show_online_contacts(struct contact_list *list) } -void cons_bad_show(char *msg) +void cons_bad_show(const char * const msg) { _win_show_time(_cons_win); wattron(_cons_win, COLOR_PAIR(6)); @@ -255,7 +260,7 @@ void cons_bad_show(char *msg) dirty = TRUE; } -void cons_show(char *msg) +void cons_show(const char * const msg) { _win_show_time(_cons_win); wprintw(_cons_win, "%s\n", msg); @@ -264,7 +269,7 @@ void cons_show(char *msg) dirty = TRUE; } -void cons_bad_command(char *cmd) +void cons_bad_command(const char * const cmd) { _win_show_time(_cons_win); wprintw(_cons_win, "Unknown command: %s\n", cmd); @@ -293,7 +298,7 @@ void cons_bad_message(void) cons_show("Usage: /msg user@host message"); } -void win_handle_switch(int *ch) +void win_handle_switch(const int * const ch) { if (*ch == KEY_F(1)) { _win_switch_if_active(0); @@ -336,7 +341,7 @@ void win_page_off(void) dirty = TRUE; } -void win_handle_page(int *ch) +void win_handle_page(const int * const ch) { int rows, cols, y, x; getmaxyx(stdscr, rows, cols); @@ -410,7 +415,7 @@ static void _create_windows(void) } } -static int _find_prof_win_index(char *contact) +static int _find_prof_win_index(const char * const contact) { // find the chat window for recipient int i; @@ -421,7 +426,7 @@ static int _find_prof_win_index(char *contact) return i; } -static int _new_prof_win(char *contact) +static int _new_prof_win(const char * const contact) { int i; // find the first unused one @@ -435,7 +440,7 @@ static int _new_prof_win(char *contact) return i; } -static void _win_switch_if_active(int i) +static void _win_switch_if_active(const int i) { win_page_off(); if (strcmp(_wins[i].from, "") != 0) { @@ -458,7 +463,7 @@ static void _win_show_time(WINDOW *win) wprintw(win, "%s - ", tstmp); } -static void _win_show_user(WINDOW *win, char *user, int colour) +static void _win_show_user(WINDOW *win, const char * const user, const int colour) { if (colour) wattron(win, COLOR_PAIR(2)); @@ -467,7 +472,7 @@ static void _win_show_user(WINDOW *win, char *user, int colour) wattroff(win, COLOR_PAIR(2)); } -static void _win_show_message(WINDOW *win, char *message) +static void _win_show_message(WINDOW *win, const char * const message) { wattroff(win, A_BOLD); wprintw(win, "%s\n", message); @@ -483,8 +488,9 @@ static void _current_window_refresh() prefresh(current, _wins[_curr_prof_win].y_pos, 0, 1, 0, rows-3, cols-1); } -static void _show_status_string(WINDOW *win, char *from, char *show, char *status, - char *pre, char *default_show) +static void _show_status_string(WINDOW *win, const char * const from, + const char * const show, const char * const status, const char * const pre, + const char * const default_show) { _win_show_time(win); if (strcmp(default_show, "online") == 0) { @@ -515,7 +521,7 @@ static void _show_status_string(WINDOW *win, char *from, char *show, char *statu } -static void _cons_show_incoming_message(char *short_from, int win_index) +static void _cons_show_incoming_message(const char * const short_from, const int win_index) { _win_show_time(_cons_win); wattron(_cons_win, COLOR_PAIR(8)); diff --git a/windows.h b/windows.h index 6b42bb68..b7c565fd 100644 --- a/windows.h +++ b/windows.h @@ -45,7 +45,7 @@ void create_input_window(void); // title bar actions void title_bar_refresh(void); -void title_bar_show(char *title); +void title_bar_show(const char * const title); void title_bar_title(void); void title_bar_connected(void); void title_bar_disconnected(void); @@ -54,33 +54,36 @@ void title_bar_disconnected(void); int win_close_win(void); int win_in_chat(void); char *win_get_recipient(void); -void win_show_incomming_msg(char *from, char *message); -void win_show_outgoing_msg(char *from, char *to, char *message); -void win_handle_switch(int *ch); -void win_handle_page(int *ch); +void win_show_incomming_msg(const char * const from, const char * const message); +void win_show_outgoing_msg(const char * const from, const char * const to, + const char * const message); +void win_handle_switch(const int * const ch); +void win_handle_page(const int * const ch); void win_page_off(void); -void win_contact_online(char *from, char *show, char *status); -void win_contact_offline(char *from, char *show, char *status); +void win_contact_online(const char * const from, const char * const show, + const char * const status); +void win_contact_offline(const char * const from, const char * const show, + const char * const status); // console window actions void cons_help(void); -void cons_bad_command(char *cmd); +void cons_bad_command(const char * const cmd); void cons_bad_connect(void); void cons_not_disconnected(void); void cons_not_connected(void); void cons_bad_message(void); -void cons_show(char *cmd); -void cons_bad_show(char *cmd); -void cons_highlight_show(char *cmd); -void cons_show_online_contacts(struct contact_list *list); +void cons_show(const char * const cmd); +void cons_bad_show(const char * const cmd); +void cons_highlight_show(const char * const cmd); +void cons_show_online_contacts(const struct contact_list * const list); // status bar actions void status_bar_refresh(void); void status_bar_clear(void); void status_bar_get_password(void); -void status_bar_print_message(const char *msg); -void status_bar_inactive(int win); -void status_bar_active(int win); +void status_bar_print_message(const char * const msg); +void status_bar_inactive(const int win); +void status_bar_active(const int win); void status_bar_update_time(void); // input window actions