1
0
mirror of https://github.com/Tha14/toxic.git synced 2025-12-19 13:53:21 +01:00

Update astylerc to match toxcore & reformat all source files

This commit is contained in:
jfreegman
2018-07-18 11:33:16 -04:00
parent cb524dcbc3
commit a37bf300f9
39 changed files with 1924 additions and 1123 deletions

View File

@@ -80,108 +80,118 @@ static char prev_note [TOX_MAX_STATUS_MESSAGE_LENGTH] = "";
static pthread_mutex_t status_lock;
static pthread_t mplex_tid;
void lock_status ()
void lock_status()
{
pthread_mutex_lock (&status_lock);
pthread_mutex_lock(&status_lock);
}
void unlock_status ()
void unlock_status()
{
pthread_mutex_unlock (&status_lock);
pthread_mutex_unlock(&status_lock);
}
static char *read_into_dyn_buffer (FILE *stream)
static char *read_into_dyn_buffer(FILE *stream)
{
const char *input_ptr = NULL;
char *dyn_buffer = NULL;
int dyn_buffer_size = 1; /* account for the \0 */
while ((input_ptr = fgets (buffer, BUFFER_SIZE, stream)) != NULL) {
int length = dyn_buffer_size + strlen (input_ptr);
while ((input_ptr = fgets(buffer, BUFFER_SIZE, stream)) != NULL) {
int length = dyn_buffer_size + strlen(input_ptr);
if (dyn_buffer)
dyn_buffer = (char *) realloc (dyn_buffer, length);
else
dyn_buffer = (char *) malloc (length);
if (dyn_buffer) {
dyn_buffer = (char *) realloc(dyn_buffer, length);
} else {
dyn_buffer = (char *) malloc(length);
}
strcpy (dyn_buffer + dyn_buffer_size - 1, input_ptr);
strcpy(dyn_buffer + dyn_buffer_size - 1, input_ptr);
dyn_buffer_size = length;
}
return dyn_buffer;
}
static char *extract_socket_path (const char *info)
static char *extract_socket_path(const char *info)
{
const char *search_str = " Socket";
const char *pos = strstr (info, search_str);
const char *pos = strstr(info, search_str);
char *end = NULL;
char *path = NULL;
if (!pos)
if (!pos) {
return NULL;
}
pos += strlen (search_str);
pos = strchr (pos, PATH_SEP_C);
pos += strlen(search_str);
pos = strchr(pos, PATH_SEP_C);
if (!pos)
if (!pos) {
return NULL;
}
end = strchr (pos, '\n');
end = strchr(pos, '\n');
if (!end)
if (!end) {
return NULL;
}
*end = '\0';
end = strrchr (pos, '.');
end = strrchr(pos, '.');
if (!end)
if (!end) {
return NULL;
}
path = (char *) malloc (end - pos + 1);
path = (char *) malloc(end - pos + 1);
*end = '\0';
return strcpy (path, pos);
return strcpy(path, pos);
}
static int detect_gnu_screen ()
static int detect_gnu_screen()
{
FILE *session_info_stream = NULL;
char *socket_name = NULL, *socket_path = NULL;
char *dyn_buffer = NULL;
socket_name = getenv ("STY");
socket_name = getenv("STY");
if (!socket_name)
if (!socket_name) {
goto nomplex;
}
session_info_stream = popen ("env LC_ALL=C screen -ls", "r");
session_info_stream = popen("env LC_ALL=C screen -ls", "r");
if (!session_info_stream)
if (!session_info_stream) {
goto nomplex;
}
dyn_buffer = read_into_dyn_buffer (session_info_stream);
dyn_buffer = read_into_dyn_buffer(session_info_stream);
if (!dyn_buffer)
if (!dyn_buffer) {
goto nomplex;
}
pclose (session_info_stream);
pclose(session_info_stream);
session_info_stream = NULL;
socket_path = extract_socket_path (dyn_buffer);
socket_path = extract_socket_path(dyn_buffer);
if (!socket_path)
if (!socket_path) {
goto nomplex;
}
free (dyn_buffer);
free(dyn_buffer);
dyn_buffer = NULL;
if (strlen(socket_path) + strlen(PATH_SEP_S) + strlen(socket_name) >= sizeof(mplex_data))
if (strlen(socket_path) + strlen(PATH_SEP_S) + strlen(socket_name) >= sizeof(mplex_data)) {
goto nomplex;
}
strcpy (mplex_data, socket_path);
strcat (mplex_data, PATH_SEP_S);
strcat (mplex_data, socket_name);
free (socket_path);
strcpy(mplex_data, socket_path);
strcat(mplex_data, PATH_SEP_S);
strcat(mplex_data, socket_name);
free(socket_path);
socket_path = NULL;
mplex = MPLEX_SCREEN;
@@ -189,33 +199,38 @@ static int detect_gnu_screen ()
nomplex:
if (session_info_stream)
pclose (session_info_stream);
if (session_info_stream) {
pclose(session_info_stream);
}
if (dyn_buffer)
free (dyn_buffer);
if (dyn_buffer) {
free(dyn_buffer);
}
if (socket_path)
if (socket_path) {
free(socket_path);
}
return 0;
}
static int detect_tmux ()
static int detect_tmux()
{
char *tmux_env = getenv ("TMUX"), *pos;
char *tmux_env = getenv("TMUX"), *pos;
if (!tmux_env)
if (!tmux_env) {
return 0;
}
/* find second separator */
pos = strrchr (tmux_env, ',');
pos = strrchr(tmux_env, ',');
if (!pos)
if (!pos) {
return 0;
}
/* store the session id for later use */
snprintf (mplex_data, sizeof(mplex_data), "$%s", pos + 1);
snprintf(mplex_data, sizeof(mplex_data), "$%s", pos + 1);
mplex = MPLEX_TMUX;
return 1;
}
@@ -228,92 +243,102 @@ static int detect_tmux ()
Returns 1 if present, 0 otherwise. This value can be used to determine
whether an auto-away detection timer is needed.
*/
static int detect_mplex ()
static int detect_mplex()
{
/* try screen, and if fails try tmux */
return detect_gnu_screen () || detect_tmux ();
return detect_gnu_screen() || detect_tmux();
}
/* Detects gnu screen session attached/detached by examining permissions of
the session's unix socket.
*/
static int gnu_screen_is_detached ()
static int gnu_screen_is_detached()
{
if (mplex != MPLEX_SCREEN)
if (mplex != MPLEX_SCREEN) {
return 0;
}
struct stat sb;
if (stat (mplex_data, &sb) != 0)
if (stat(mplex_data, &sb) != 0) {
return 0;
}
/* execution permission (x) means attached */
return ! (sb.st_mode & S_IXUSR);
return !(sb.st_mode & S_IXUSR);
}
/* Detects tmux attached/detached by getting session data and finding the
current session's entry.
*/
static int tmux_is_detached ()
static int tmux_is_detached()
{
if (mplex != MPLEX_TMUX)
if (mplex != MPLEX_TMUX) {
return 0;
}
FILE *session_info_stream = NULL;
char *dyn_buffer = NULL, *search_str = NULL;
char *entry_pos;
int detached;
const int numstr_len = strlen (mplex_data);
const int numstr_len = strlen(mplex_data);
/* get the number of attached clients for each session */
session_info_stream = popen ("tmux list-sessions -F \"#{session_id} #{session_attached}\"", "r");
session_info_stream = popen("tmux list-sessions -F \"#{session_id} #{session_attached}\"", "r");
if (!session_info_stream)
if (!session_info_stream) {
goto fail;
}
dyn_buffer = read_into_dyn_buffer (session_info_stream);
dyn_buffer = read_into_dyn_buffer(session_info_stream);
if (!dyn_buffer)
if (!dyn_buffer) {
goto fail;
}
pclose (session_info_stream);
pclose(session_info_stream);
session_info_stream = NULL;
/* prepare search string, for finding the current session's entry */
search_str = (char *) malloc (numstr_len + 2);
search_str = (char *) malloc(numstr_len + 2);
search_str[0] = '\n';
strcpy (search_str + 1, mplex_data);
strcpy(search_str + 1, mplex_data);
/* do the search */
if (strncmp (dyn_buffer, search_str + 1, numstr_len) == 0)
if (strncmp(dyn_buffer, search_str + 1, numstr_len) == 0) {
entry_pos = dyn_buffer;
else
entry_pos = strstr (dyn_buffer, search_str);
} else {
entry_pos = strstr(dyn_buffer, search_str);
}
if (! entry_pos)
if (! entry_pos) {
goto fail;
}
entry_pos = strchr (entry_pos, ' ') + 1;
detached = strncmp (entry_pos, "0\n", 2) == 0;
entry_pos = strchr(entry_pos, ' ') + 1;
detached = strncmp(entry_pos, "0\n", 2) == 0;
free (search_str);
free(search_str);
search_str = NULL;
free (dyn_buffer);
free(dyn_buffer);
dyn_buffer = NULL;
return detached;
fail:
if (session_info_stream)
pclose (session_info_stream);
if (session_info_stream) {
pclose(session_info_stream);
}
if (dyn_buffer)
free (dyn_buffer);
if (dyn_buffer) {
free(dyn_buffer);
}
if (search_str)
free (search_str);
if (search_str) {
free(search_str);
}
return 0;
}
@@ -327,24 +352,25 @@ fail:
sample its state and update away status according to attached/detached state
of the mplex.
*/
static int mplex_is_detached ()
static int mplex_is_detached()
{
return gnu_screen_is_detached () || tmux_is_detached ();
return gnu_screen_is_detached() || tmux_is_detached();
}
static void mplex_timer_handler (Tox *m)
static void mplex_timer_handler(Tox *m)
{
TOX_USER_STATUS current_status, new_status;
const char *new_note;
if (mplex == MPLEX_NONE)
if (mplex == MPLEX_NONE) {
return;
}
int detached = mplex_is_detached ();
int detached = mplex_is_detached();
pthread_mutex_lock (&Winthread.lock);
current_status = tox_self_get_status (m);
pthread_mutex_unlock (&Winthread.lock);
pthread_mutex_lock(&Winthread.lock);
current_status = tox_self_get_status(m);
pthread_mutex_unlock(&Winthread.lock);
if (auto_away_active && current_status == TOX_USER_STATUS_AWAY && !detached) {
auto_away_active = false;
@@ -354,25 +380,26 @@ static void mplex_timer_handler (Tox *m)
auto_away_active = true;
prev_status = current_status;
new_status = TOX_USER_STATUS_AWAY;
pthread_mutex_lock (&Winthread.lock);
pthread_mutex_lock(&Winthread.lock);
size_t slen = tox_self_get_status_message_size(m);
tox_self_get_status_message (m, (uint8_t *) prev_note);
tox_self_get_status_message(m, (uint8_t *) prev_note);
prev_note[slen] = '\0';
pthread_mutex_unlock (&Winthread.lock);
pthread_mutex_unlock(&Winthread.lock);
new_note = user_settings->mplex_away_note;
} else
} else {
return;
}
char argv[3][MAX_STR_SIZE];
strcpy (argv[0], "/status");
strcpy (argv[1], (new_status == TOX_USER_STATUS_AWAY ? "away" :
new_status == TOX_USER_STATUS_BUSY ? "busy" : "online"));
strcpy(argv[0], "/status");
strcpy(argv[1], (new_status == TOX_USER_STATUS_AWAY ? "away" :
new_status == TOX_USER_STATUS_BUSY ? "busy" : "online"));
argv[2][0] = '\"';
strcpy (argv[2] + 1, new_note);
strcat (argv[2], "\"");
pthread_mutex_lock (&Winthread.lock);
cmd_status (prompt->chatwin->history, prompt, m, 2, argv);
pthread_mutex_unlock (&Winthread.lock);
strcpy(argv[2] + 1, new_note);
strcat(argv[2], "\"");
pthread_mutex_lock(&Winthread.lock);
cmd_status(prompt->chatwin->history, prompt, m, 2, argv);
pthread_mutex_unlock(&Winthread.lock);
}
/* Time in seconds between calls to mplex_timer_handler */
@@ -388,20 +415,24 @@ void *mplex_timer_thread(void *data)
}
}
int init_mplex_away_timer (Tox *m)
int init_mplex_away_timer(Tox *m)
{
if (! detect_mplex ())
if (! detect_mplex()) {
return 0;
}
if (! user_settings->mplex_away)
if (! user_settings->mplex_away) {
return 0;
}
/* status access mutex */
if (pthread_mutex_init (&status_lock, NULL) != 0)
if (pthread_mutex_init(&status_lock, NULL) != 0) {
return -1;
}
if (pthread_create(&mplex_tid, NULL, mplex_timer_thread, (void *) m) != 0)
if (pthread_create(&mplex_tid, NULL, mplex_timer_thread, (void *) m) != 0) {
return -1;
}
return 0;
}