Re-indent.

This commit is contained in:
QC 2014-06-04 21:29:23 +02:00
parent 7667a88732
commit 9fbd7fc99c
8 changed files with 400 additions and 812 deletions

View File

@ -13,11 +13,11 @@ indent_brace = 0
nl_enum_brace = add # "enum {" vs "enum \n {" nl_enum_brace = add # "enum {" vs "enum \n {"
nl_union_brace = add # "union {" vs "union \n {" nl_union_brace = add # "union {" vs "union \n {"
nl_struct_brace = add # "struct {" vs "struct \n {" nl_struct_brace = add # "struct {" vs "struct \n {"
nl_do_brace = add # "do {" vs "do \n {" nl_do_brace = remove # "do {" vs "do \n {"
nl_if_brace = add # "if () {" vs "if () \n {" nl_if_brace = remove # "if () {" vs "if () \n {"
nl_for_brace = add # "for () {" vs "for () \n {" nl_for_brace = remove # "for () {" vs "for () \n {"
nl_else_brace = add # "else {" vs "else \n {" nl_else_brace = remove # "else {" vs "else \n {"
nl_while_brace = add # "while () {" vs "while () \n {" nl_while_brace = remove # "while () {" vs "while () \n {"
nl_switch_brace = add # "switch () {" vs "switch () \n {" nl_switch_brace = add # "switch () {" vs "switch () \n {"
# nl_func_var_def_blk = 1 # nl_func_var_def_blk = 1
# nl_before_case = 1 # nl_before_case = 1
@ -28,9 +28,9 @@ nl_brace_while = remove
nl_brace_else = add nl_brace_else = add
nl_squeeze_ifdef = FALSE nl_squeeze_ifdef = FALSE
# mod_paren_on_return = add # "return 1;" vs "return (1);" mod_paren_on_return = remove # "return 1;" vs "return (1);"
mod_full_brace_if = add # "if (a) a--;" vs "if (a) { a--; }" mod_full_brace_if = add # "if (a) a--;" vs "if (a) { a--; }"
mod_full_brace_for = add # "for () a--;" vs "for () { a--; }" mod_full_brace_for = add # "for () a--;" vs "for () { a--; }"
mod_full_brace_do = add # "do a--; while ();" vs "do { a--; } while ();" mod_full_brace_do = add # "do a--; while ();" vs "do { a--; } while ();"
mod_full_brace_while = add # "while (a) a--;" vs "while (a) { a--; }" mod_full_brace_while = add # "while (a) a--;" vs "while (a) { a--; }"

View File

@ -44,11 +44,9 @@ static char **get_dmenu ( void )
char **retv = NULL; char **retv = NULL;
int index = 0; int index = 0;
while ( fgets ( buffer, 1024, stdin ) != NULL ) while ( fgets ( buffer, 1024, stdin ) != NULL ) {
{
char **tr = realloc ( retv, ( index + 2 ) * sizeof ( char* ) ); char **tr = realloc ( retv, ( index + 2 ) * sizeof ( char* ) );
if ( tr == NULL ) if ( tr == NULL ) {
{
return retv; return retv;
} }
retv = tr; retv = tr;
@ -56,8 +54,7 @@ static char **get_dmenu ( void )
retv[index + 1] = NULL; retv[index + 1] = NULL;
// Filter out line-end. // Filter out line-end.
if ( retv[index][strlen ( buffer ) - 1] == '\n' ) if ( retv[index][strlen ( buffer ) - 1] == '\n' ) {
{
retv[index][strlen ( buffer ) - 1] = '\0'; retv[index][strlen ( buffer ) - 1] = '\0';
} }
@ -76,26 +73,21 @@ SwitcherMode dmenu_switcher_dialog ( char **input )
int mretv = menu ( list, input, dmenu_prompt, NULL, NULL, int mretv = menu ( list, input, dmenu_prompt, NULL, NULL,
token_match, NULL, &selected_line ); token_match, NULL, &selected_line );
if ( mretv == MENU_NEXT ) if ( mretv == MENU_NEXT ) {
{
retv = DMENU_DIALOG; retv = DMENU_DIALOG;
} }
else if ( mretv == MENU_OK && list[selected_line] != NULL ) else if ( mretv == MENU_OK && list[selected_line] != NULL ) {
{
fputs ( list[selected_line], stdout ); fputs ( list[selected_line], stdout );
} }
else if ( mretv == MENU_CUSTOM_INPUT && *input != NULL && *input[0] != '\0' ) else if ( mretv == MENU_CUSTOM_INPUT && *input != NULL && *input[0] != '\0' ) {
{
fputs ( *input, stdout ); fputs ( *input, stdout );
} }
for ( unsigned int i = 0; list != NULL && list[i] != NULL; i++ ) for ( unsigned int i = 0; list != NULL && list[i] != NULL; i++ ) {
{
free ( list[i] ); free ( list[i] );
} }
if ( list != NULL ) if ( list != NULL ) {
{
free ( list ); free ( list );
} }

View File

@ -51,8 +51,7 @@ static int __element_sort_func ( const void *ea, const void *eb )
static void __history_write_element_list ( FILE *fd, _element **list, unsigned int length ) static void __history_write_element_list ( FILE *fd, _element **list, unsigned int length )
{ {
if ( list == NULL ) if ( list == NULL ) {
{
return; return;
} }
// Sort the list before writing out. // Sort the list before writing out.
@ -65,8 +64,7 @@ static void __history_write_element_list ( FILE *fd, _element **list, unsigned i
length = ( length > HISTORY_MAX_ENTRIES ) ? HISTORY_MAX_ENTRIES : length; length = ( length > HISTORY_MAX_ENTRIES ) ? HISTORY_MAX_ENTRIES : length;
// Write out entries. // Write out entries.
for ( unsigned int iter = 0; iter < length; iter++ ) for ( unsigned int iter = 0; iter < length; iter++ ) {
{
fprintf ( fd, "%ld %s\n", list[iter]->index - min_value, list[iter]->name ); fprintf ( fd, "%ld %s\n", list[iter]->index - min_value, list[iter]->name );
} }
} }
@ -76,28 +74,23 @@ static _element ** __history_get_element_list ( FILE *fd, unsigned int *length )
char buffer[HISTORY_NAME_LENGTH + 16]; char buffer[HISTORY_NAME_LENGTH + 16];
_element **retv = NULL; _element **retv = NULL;
if ( length == NULL ) if ( length == NULL ) {
{
return NULL; return NULL;
} }
*length = 0; *length = 0;
if ( fd == NULL ) if ( fd == NULL ) {
{
return NULL; return NULL;
} }
while ( fgets ( buffer, HISTORY_NAME_LENGTH + 16, fd ) != NULL ) while ( fgets ( buffer, HISTORY_NAME_LENGTH + 16, fd ) != NULL ) {
{
char * start = NULL; char * start = NULL;
// Skip empty lines. // Skip empty lines.
if ( strlen ( buffer ) == 0 ) if ( strlen ( buffer ) == 0 ) {
{
continue; continue;
} }
// Resize and check. // Resize and check.
_element **tr = realloc ( retv, ( *length + 2 ) * sizeof ( _element* ) ); _element **tr = realloc ( retv, ( *length + 2 ) * sizeof ( _element* ) );
if ( tr == NULL ) if ( tr == NULL ) {
{
return retv; return retv;
} }
retv = tr; retv = tr;
@ -126,8 +119,7 @@ void history_set ( const char *filename, const char *entry )
_element **list = NULL; _element **list = NULL;
// Open file for reading and writing. // Open file for reading and writing.
FILE *fd = fopen ( filename, "a+" ); FILE *fd = fopen ( filename, "a+" );
if ( fd == NULL ) if ( fd == NULL ) {
{
fprintf ( stderr, "Failed to open file: %s\n", strerror ( errno ) ); fprintf ( stderr, "Failed to open file: %s\n", strerror ( errno ) );
return; return;
} }
@ -135,32 +127,26 @@ void history_set ( const char *filename, const char *entry )
list = __history_get_element_list ( fd, &length ); list = __history_get_element_list ( fd, &length );
// Look if the entry exists. // Look if the entry exists.
for ( unsigned int iter = 0; !found && iter < length; iter++ ) for ( unsigned int iter = 0; !found && iter < length; iter++ ) {
{ if ( strcmp ( list[iter]->name, entry ) == 0 ) {
if ( strcmp ( list[iter]->name, entry ) == 0 )
{
curr = iter; curr = iter;
found = 1; found = 1;
} }
} }
if ( found ) if ( found ) {
{
// If exists, increment list index number // If exists, increment list index number
list[curr]->index++; list[curr]->index++;
} }
else else{
{
// If not exists, add it. // If not exists, add it.
// Increase list by one // Increase list by one
_element **tr = realloc ( list, ( length + 2 ) * sizeof ( _element* ) ); _element **tr = realloc ( list, ( length + 2 ) * sizeof ( _element* ) );
if ( tr != NULL ) if ( tr != NULL ) {
{
list = tr; list = tr;
list[length] = malloc ( sizeof ( _element ) ); list[length] = malloc ( sizeof ( _element ) );
// Copy name // Copy name
if ( list[length] != NULL ) if ( list[length] != NULL ) {
{
strncpy ( list[length]->name, entry, HISTORY_NAME_LENGTH ); strncpy ( list[length]->name, entry, HISTORY_NAME_LENGTH );
list[length]->name[HISTORY_NAME_LENGTH - 1] = '\0'; list[length]->name[HISTORY_NAME_LENGTH - 1] = '\0';
// set # hits // set # hits
@ -175,18 +161,15 @@ void history_set ( const char *filename, const char *entry )
// Rewind. // Rewind.
fseek ( fd, 0L, SEEK_SET ); fseek ( fd, 0L, SEEK_SET );
// Clear file. // Clear file.
if ( ftruncate ( fileno ( fd ), 0 ) == 0 ) if ( ftruncate ( fileno ( fd ), 0 ) == 0 ) {
{
// Write list. // Write list.
__history_write_element_list ( fd, list, length ); __history_write_element_list ( fd, list, length );
} }
else else{
{
fprintf ( stderr, "Failed to truncate file: %s\n", strerror ( errno ) ); fprintf ( stderr, "Failed to truncate file: %s\n", strerror ( errno ) );
} }
// Free the list. // Free the list.
for ( unsigned int iter = 0; iter < length; iter++ ) for ( unsigned int iter = 0; iter < length; iter++ ) {
{
free ( list[iter] ); free ( list[iter] );
} }
free ( list ); free ( list );
@ -202,8 +185,7 @@ void history_remove ( const char *filename, const char *entry )
unsigned int length = 0; unsigned int length = 0;
// Open file for reading and writing. // Open file for reading and writing.
FILE *fd = fopen ( filename, "a+" ); FILE *fd = fopen ( filename, "a+" );
if ( fd == NULL ) if ( fd == NULL ) {
{
fprintf ( stderr, "Failed to open file: %s\n", strerror ( errno ) ); fprintf ( stderr, "Failed to open file: %s\n", strerror ( errno ) );
return; return;
} }
@ -211,18 +193,15 @@ void history_remove ( const char *filename, const char *entry )
list = __history_get_element_list ( fd, &length ); list = __history_get_element_list ( fd, &length );
// Find entry. // Find entry.
for ( unsigned int iter = 0; !found && iter < length; iter++ ) for ( unsigned int iter = 0; !found && iter < length; iter++ ) {
{ if ( strcmp ( list[iter]->name, entry ) == 0 ) {
if ( strcmp ( list[iter]->name, entry ) == 0 )
{
curr = iter; curr = iter;
found = 1; found = 1;
} }
} }
// If found, remove it and write out new file. // If found, remove it and write out new file.
if ( found ) if ( found ) {
{
// Remove the entry. // Remove the entry.
free ( list[curr] ); free ( list[curr] );
// Swap last to here (if list is size 1, we just swap empty sets). // Swap last to here (if list is size 1, we just swap empty sets).
@ -234,24 +213,20 @@ void history_remove ( const char *filename, const char *entry )
// Rewind. // Rewind.
fseek ( fd, 0L, SEEK_SET ); fseek ( fd, 0L, SEEK_SET );
// Clear list. // Clear list.
if ( ftruncate ( fileno ( fd ), 0 ) == 0 ) if ( ftruncate ( fileno ( fd ), 0 ) == 0 ) {
{
// Write list. // Write list.
__history_write_element_list ( fd, list, length ); __history_write_element_list ( fd, list, length );
} }
else else{
{
fprintf ( stderr, "Failed to open file: %s\n", strerror ( errno ) ); fprintf ( stderr, "Failed to open file: %s\n", strerror ( errno ) );
} }
} }
// Free the list. // Free the list.
for ( unsigned int iter = 0; iter < length; iter++ ) for ( unsigned int iter = 0; iter < length; iter++ ) {
{
free ( list[iter] ); free ( list[iter] );
} }
if ( list != NULL ) if ( list != NULL ) {
{
free ( list ); free ( list );
} }
// Close file. // Close file.
@ -264,12 +239,10 @@ char ** history_get_list ( const char *filename, unsigned int *length )
char **retv = NULL; char **retv = NULL;
// Open file. // Open file.
FILE *fd = fopen ( filename, "r" ); FILE *fd = fopen ( filename, "r" );
if ( fd == NULL ) if ( fd == NULL ) {
{
// File that does not exists is not an error, so ignore it. // File that does not exists is not an error, so ignore it.
// Everything else? panic. // Everything else? panic.
if ( errno != ENOENT ) if ( errno != ENOENT ) {
{
fprintf ( stderr, "Failed to open file: %s\n", strerror ( errno ) ); fprintf ( stderr, "Failed to open file: %s\n", strerror ( errno ) );
} }
return NULL; return NULL;
@ -279,11 +252,9 @@ char ** history_get_list ( const char *filename, unsigned int *length )
// Copy list in right format. // Copy list in right format.
// Lists are always short, so performance should not be an issue. // Lists are always short, so performance should not be an issue.
if ( ( *length ) > 0 ) if ( ( *length ) > 0 ) {
{
retv = malloc ( ( ( *length ) + 1 ) * sizeof ( char * ) ); retv = malloc ( ( ( *length ) + 1 ) * sizeof ( char * ) );
for ( int iter = 0; iter < ( *length ); iter++ ) for ( int iter = 0; iter < ( *length ); iter++ ) {
{
retv[iter] = strdup ( list[iter]->name ); retv[iter] = strdup ( list[iter]->name );
free ( list[iter] ); free ( list[iter] );
} }

File diff suppressed because it is too large Load Diff

View File

@ -51,8 +51,7 @@
static inline int execsh ( const char *cmd, int run_in_term ) static inline int execsh ( const char *cmd, int run_in_term )
{ {
// use sh for args parsing // use sh for args parsing
if ( run_in_term ) if ( run_in_term ) {
{
return execlp ( config.terminal_emulator, config.terminal_emulator, "-e", "sh", "-c", cmd, NULL ); return execlp ( config.terminal_emulator, config.terminal_emulator, "-e", "sh", "-c", cmd, NULL );
} }
@ -62,16 +61,14 @@ static inline int execsh ( const char *cmd, int run_in_term )
// execute sub-process // execute sub-process
static pid_t exec_cmd ( const char *cmd, int run_in_term ) static pid_t exec_cmd ( const char *cmd, int run_in_term )
{ {
if ( !cmd || !cmd[0] ) if ( !cmd || !cmd[0] ) {
{
return -1; return -1;
} }
signal ( SIGCHLD, catch_exit ); signal ( SIGCHLD, catch_exit );
pid_t pid = fork (); pid_t pid = fork ();
if ( !pid ) if ( !pid ) {
{
setsid (); setsid ();
execsh ( cmd, run_in_term ); execsh ( cmd, run_in_term );
exit ( EXIT_FAILURE ); exit ( EXIT_FAILURE );
@ -82,8 +79,7 @@ static pid_t exec_cmd ( const char *cmd, int run_in_term )
* It is allowed to be a bit slower. * It is allowed to be a bit slower.
*/ */
char *path = NULL; char *path = NULL;
if ( asprintf ( &path, "%s/%s", cache_dir, RUN_CACHE_FILE ) == -1 ) if ( asprintf ( &path, "%s/%s", cache_dir, RUN_CACHE_FILE ) == -1 ) {
{
return -1; return -1;
} }
history_set ( path, cmd ); history_set ( path, cmd );
@ -100,8 +96,7 @@ static void delete_entry ( const char *cmd )
* It is allowed to be a bit slower. * It is allowed to be a bit slower.
*/ */
char *path = NULL; char *path = NULL;
if ( asprintf ( &path, "%s/%s", cache_dir, RUN_CACHE_FILE ) == -1 ) if ( asprintf ( &path, "%s/%s", cache_dir, RUN_CACHE_FILE ) == -1 ) {
{
return; return;
} }
history_remove ( path, cmd ); history_remove ( path, cmd );
@ -125,14 +120,12 @@ static char ** get_apps ( void )
clock_gettime ( CLOCK_REALTIME, &start ); clock_gettime ( CLOCK_REALTIME, &start );
#endif #endif
if ( getenv ( "PATH" ) == NULL ) if ( getenv ( "PATH" ) == NULL ) {
{
return NULL; return NULL;
} }
if ( asprintf ( &path, "%s/%s", cache_dir, RUN_CACHE_FILE ) > 0 ) if ( asprintf ( &path, "%s/%s", cache_dir, RUN_CACHE_FILE ) > 0 ) {
{
retv = history_get_list ( path, &index ); retv = history_get_list ( path, &index );
free ( path ); free ( path );
// Keep track of how many where loaded as favorite. // Keep track of how many where loaded as favorite.
@ -144,20 +137,16 @@ static char ** get_apps ( void )
for ( const char *dirname = strtok ( path, ":" ); for ( const char *dirname = strtok ( path, ":" );
dirname != NULL; dirname != NULL;
dirname = strtok ( NULL, ":" ) ) dirname = strtok ( NULL, ":" ) ) {
{
DIR *dir = opendir ( dirname ); DIR *dir = opendir ( dirname );
if ( dir != NULL ) if ( dir != NULL ) {
{
struct dirent *dent; struct dirent *dent;
while ( ( dent = readdir ( dir ) ) != NULL ) while ( ( dent = readdir ( dir ) ) != NULL ) {
{
if ( dent->d_type != DT_REG && if ( dent->d_type != DT_REG &&
dent->d_type != DT_LNK && dent->d_type != DT_LNK &&
dent->d_type != DT_UNKNOWN ) dent->d_type != DT_UNKNOWN ) {
{
continue; continue;
} }
@ -165,22 +154,18 @@ static char ** get_apps ( void )
// This is a nice little penalty, but doable? time will tell. // This is a nice little penalty, but doable? time will tell.
// given num_favorites is max 25. // given num_favorites is max 25.
for ( unsigned int j = 0; found == 0 && j < num_favorites; j++ ) for ( unsigned int j = 0; found == 0 && j < num_favorites; j++ ) {
{ if ( strcasecmp ( dent->d_name, retv[j] ) == 0 ) {
if ( strcasecmp ( dent->d_name, retv[j] ) == 0 )
{
found = 1; found = 1;
} }
} }
if ( found == 1 ) if ( found == 1 ) {
{
continue; continue;
} }
char ** tr = realloc ( retv, ( index + 2 ) * sizeof ( char* ) ); char ** tr = realloc ( retv, ( index + 2 ) * sizeof ( char* ) );
if ( tr != NULL ) if ( tr != NULL ) {
{
retv = tr; retv = tr;
retv[index] = strdup ( dent->d_name ); retv[index] = strdup ( dent->d_name );
retv[index + 1] = NULL; retv[index + 1] = NULL;
@ -193,16 +178,14 @@ static char ** get_apps ( void )
} }
// TODO: check this is still fast enough. (takes 1ms on laptop.) // TODO: check this is still fast enough. (takes 1ms on laptop.)
if ( index > num_favorites ) if ( index > num_favorites ) {
{
qsort ( &retv[num_favorites], index - num_favorites, sizeof ( char* ), sort_func ); qsort ( &retv[num_favorites], index - num_favorites, sizeof ( char* ), sort_func );
} }
free ( path ); free ( path );
#ifdef TIMING #ifdef TIMING
clock_gettime ( CLOCK_REALTIME, &stop ); clock_gettime ( CLOCK_REALTIME, &stop );
if ( stop.tv_sec != start.tv_sec ) if ( stop.tv_sec != start.tv_sec ) {
{
stop.tv_nsec += ( stop.tv_sec - start.tv_sec ) * 1e9; stop.tv_nsec += ( stop.tv_sec - start.tv_sec ) * 1e9;
} }
@ -220,8 +203,7 @@ SwitcherMode run_switcher_dialog ( char **input )
// act as a launcher // act as a launcher
char **cmd_list = get_apps ( ); char **cmd_list = get_apps ( );
if ( cmd_list == NULL ) if ( cmd_list == NULL ) {
{
cmd_list = malloc ( 2 * sizeof ( char * ) ); cmd_list = malloc ( 2 * sizeof ( char * ) );
cmd_list[0] = strdup ( "No applications found" ); cmd_list[0] = strdup ( "No applications found" );
cmd_list[1] = NULL; cmd_list[1] = NULL;
@ -229,31 +211,25 @@ SwitcherMode run_switcher_dialog ( char **input )
int mretv = menu ( cmd_list, input, "run:", NULL, &shift, token_match, NULL, &selected_line ); int mretv = menu ( cmd_list, input, "run:", NULL, &shift, token_match, NULL, &selected_line );
if ( mretv == MENU_NEXT ) if ( mretv == MENU_NEXT ) {
{
retv = NEXT_DIALOG; retv = NEXT_DIALOG;
} }
else if ( mretv == MENU_OK && cmd_list[selected_line] != NULL ) else if ( mretv == MENU_OK && cmd_list[selected_line] != NULL ) {
{
exec_cmd ( cmd_list[selected_line], shift ); exec_cmd ( cmd_list[selected_line], shift );
} }
else if ( mretv == MENU_CUSTOM_INPUT && *input != NULL && *input[0] != '\0' ) else if ( mretv == MENU_CUSTOM_INPUT && *input != NULL && *input[0] != '\0' ) {
{
exec_cmd ( *input, shift ); exec_cmd ( *input, shift );
} }
else if ( mretv == MENU_ENTRY_DELETE && cmd_list[selected_line] ) else if ( mretv == MENU_ENTRY_DELETE && cmd_list[selected_line] ) {
{
delete_entry ( cmd_list[selected_line] ); delete_entry ( cmd_list[selected_line] );
retv = RUN_DIALOG; retv = RUN_DIALOG;
} }
for ( int i = 0; cmd_list != NULL && cmd_list[i] != NULL; i++ ) for ( int i = 0; cmd_list != NULL && cmd_list[i] != NULL; i++ ) {
{
free ( cmd_list[i] ); free ( cmd_list[i] );
} }
if ( cmd_list != NULL ) if ( cmd_list != NULL ) {
{
free ( cmd_list ); free ( cmd_list );
} }

View File

@ -56,11 +56,9 @@ static inline int execshssh ( const char *host )
char **args = malloc ( sizeof ( char* ) * 7 ); char **args = malloc ( sizeof ( char* ) * 7 );
int i = 0; int i = 0;
args[i++] = config.terminal_emulator; args[i++] = config.terminal_emulator;
if ( config.ssh_set_title ) if ( config.ssh_set_title ) {
{
char *buffer = NULL; char *buffer = NULL;
if ( asprintf ( &buffer, "ssh %s", host ) > 0 ) if ( asprintf ( &buffer, "ssh %s", host ) > 0 ) {
{
args[i++] = strdup ( "-T" ); args[i++] = strdup ( "-T" );
args[i++] = buffer; args[i++] = buffer;
} }
@ -72,10 +70,8 @@ static inline int execshssh ( const char *host )
int retv = execvp ( config.terminal_emulator, (char * const *) args ); int retv = execvp ( config.terminal_emulator, (char * const *) args );
// Free the args list. // Free the args list.
for ( i = 0; i < 7; i++ ) for ( i = 0; i < 7; i++ ) {
{ if ( args[i] != NULL ) {
if ( args[i] != NULL )
{
free ( args[i] ); free ( args[i] );
} }
} }
@ -85,16 +81,14 @@ static inline int execshssh ( const char *host )
// execute sub-process // execute sub-process
static pid_t exec_ssh ( const char *cmd ) static pid_t exec_ssh ( const char *cmd )
{ {
if ( !cmd || !cmd[0] ) if ( !cmd || !cmd[0] ) {
{
return -1; return -1;
} }
signal ( SIGCHLD, catch_exit ); signal ( SIGCHLD, catch_exit );
pid_t pid = fork (); pid_t pid = fork ();
if ( !pid ) if ( !pid ) {
{
setsid (); setsid ();
execshssh ( cmd ); execshssh ( cmd );
exit ( EXIT_FAILURE ); exit ( EXIT_FAILURE );
@ -105,8 +99,7 @@ static pid_t exec_ssh ( const char *cmd )
* It is allowed to be a bit slower. * It is allowed to be a bit slower.
*/ */
char *path = NULL; char *path = NULL;
if ( asprintf ( &path, "%s/%s", cache_dir, SSH_CACHE_FILE ) > 0 ) if ( asprintf ( &path, "%s/%s", cache_dir, SSH_CACHE_FILE ) > 0 ) {
{
history_set ( path, cmd ); history_set ( path, cmd );
free ( path ); free ( path );
} }
@ -114,13 +107,11 @@ static pid_t exec_ssh ( const char *cmd )
} }
static void delete_ssh ( const char *cmd ) static void delete_ssh ( const char *cmd )
{ {
if ( !cmd || !cmd[0] ) if ( !cmd || !cmd[0] ) {
{
return; return;
} }
char *path = NULL; char *path = NULL;
if ( asprintf ( &path, "%s/%s", cache_dir, SSH_CACHE_FILE ) > 0 ) if ( asprintf ( &path, "%s/%s", cache_dir, SSH_CACHE_FILE ) > 0 ) {
{
history_remove ( path, cmd ); history_remove ( path, cmd );
free ( path ); free ( path );
} }
@ -142,13 +133,11 @@ static char ** get_ssh ( void )
clock_gettime ( CLOCK_REALTIME, &start ); clock_gettime ( CLOCK_REALTIME, &start );
#endif #endif
if ( getenv ( "HOME" ) == NULL ) if ( getenv ( "HOME" ) == NULL ) {
{
return NULL; return NULL;
} }
if ( asprintf ( &path, "%s/%s", cache_dir, SSH_CACHE_FILE ) > 0 ) if ( asprintf ( &path, "%s/%s", cache_dir, SSH_CACHE_FILE ) > 0 ) {
{
retv = history_get_list ( path, &index ); retv = history_get_list ( path, &index );
free ( path ); free ( path );
num_favorites = index; num_favorites = index;
@ -157,58 +146,47 @@ static char ** get_ssh ( void )
FILE *fd = NULL; FILE *fd = NULL;
const char *hd = getenv ( "HOME" ); const char *hd = getenv ( "HOME" );
if ( asprintf ( &path, "%s/%s", hd, ".ssh/config" ) >= 0 ) if ( asprintf ( &path, "%s/%s", hd, ".ssh/config" ) >= 0 ) {
{
fd = fopen ( path, "r" ); fd = fopen ( path, "r" );
} }
if ( fd != NULL ) if ( fd != NULL ) {
{
char buffer[1024]; char buffer[1024];
while ( fgets ( buffer, 1024, fd ) != NULL ) while ( fgets ( buffer, 1024, fd ) != NULL ) {
{ if ( strncasecmp ( buffer, "Host", 4 ) == 0 ) {
if ( strncasecmp ( buffer, "Host", 4 ) == 0 )
{
int start = 0, stop = 0; int start = 0, stop = 0;
buffer[strlen ( buffer ) - 1] = '\0'; buffer[strlen ( buffer ) - 1] = '\0';
for ( start = 4; isspace ( buffer[start] ); start++ ) for ( start = 4; isspace ( buffer[start] ); start++ ) {
{
; ;
} }
for ( stop = start; isalnum ( buffer[stop] ) || for ( stop = start; isalnum ( buffer[stop] ) ||
buffer[stop] == '_' || buffer[stop] == '_' ||
buffer[stop] == '.'; stop++ ) buffer[stop] == '.'; stop++ ) {
{
; ;
} }
int found = 0; int found = 0;
if ( start == stop ) if ( start == stop ) {
{
continue; continue;
} }
// This is a nice little penalty, but doable? time will tell. // This is a nice little penalty, but doable? time will tell.
// given num_favorites is max 25. // given num_favorites is max 25.
for ( unsigned int j = 0; found == 0 && j < num_favorites; j++ ) for ( unsigned int j = 0; found == 0 && j < num_favorites; j++ ) {
{ if ( strncasecmp ( &buffer[start], retv[j], stop - start ) == 0 ) {
if ( strncasecmp ( &buffer[start], retv[j], stop - start ) == 0 )
{
found = 1; found = 1;
} }
} }
if ( found == 1 ) if ( found == 1 ) {
{
continue; continue;
} }
char **tr = realloc ( retv, ( index + 2 ) * sizeof ( char* ) ); char **tr = realloc ( retv, ( index + 2 ) * sizeof ( char* ) );
if ( tr != NULL ) if ( tr != NULL ) {
{
retv = tr; retv = tr;
retv[index] = strndup ( &buffer[start], stop - start ); retv[index] = strndup ( &buffer[start], stop - start );
retv[index + 1] = NULL; retv[index + 1] = NULL;
@ -221,16 +199,14 @@ static char ** get_ssh ( void )
} }
// TODO: check this is still fast enough. (takes 1ms on laptop.) // TODO: check this is still fast enough. (takes 1ms on laptop.)
if ( index > num_favorites ) if ( index > num_favorites ) {
{
qsort ( &retv[num_favorites], index - num_favorites, sizeof ( char* ), sort_func ); qsort ( &retv[num_favorites], index - num_favorites, sizeof ( char* ), sort_func );
} }
free ( path ); free ( path );
#ifdef TIMING #ifdef TIMING
clock_gettime ( CLOCK_REALTIME, &stop ); clock_gettime ( CLOCK_REALTIME, &stop );
if ( stop.tv_sec != start.tv_sec ) if ( stop.tv_sec != start.tv_sec ) {
{
stop.tv_nsec += ( stop.tv_sec - start.tv_sec ) * 1e9; stop.tv_nsec += ( stop.tv_sec - start.tv_sec ) * 1e9;
} }
@ -246,8 +222,7 @@ SwitcherMode ssh_switcher_dialog ( char **input )
// act as a launcher // act as a launcher
char **cmd_list = get_ssh ( ); char **cmd_list = get_ssh ( );
if ( cmd_list == NULL ) if ( cmd_list == NULL ) {
{
cmd_list = malloc ( 2 * sizeof ( char * ) ); cmd_list = malloc ( 2 * sizeof ( char * ) );
cmd_list[0] = strdup ( "No ssh hosts found" ); cmd_list[0] = strdup ( "No ssh hosts found" );
cmd_list[1] = NULL; cmd_list[1] = NULL;
@ -257,32 +232,26 @@ SwitcherMode ssh_switcher_dialog ( char **input )
int selected_line = 0; int selected_line = 0;
int mretv = menu ( cmd_list, input, "ssh:", NULL, &shift, token_match, NULL, &selected_line ); int mretv = menu ( cmd_list, input, "ssh:", NULL, &shift, token_match, NULL, &selected_line );
if ( mretv == MENU_NEXT ) if ( mretv == MENU_NEXT ) {
{
retv = NEXT_DIALOG; retv = NEXT_DIALOG;
} }
else if ( mretv == MENU_OK && cmd_list[selected_line] != NULL ) else if ( mretv == MENU_OK && cmd_list[selected_line] != NULL ) {
{
exec_ssh ( cmd_list[selected_line] ); exec_ssh ( cmd_list[selected_line] );
} }
else if ( mretv == MENU_CUSTOM_INPUT && *input != NULL && *input[0] != '\0' ) else if ( mretv == MENU_CUSTOM_INPUT && *input != NULL && *input[0] != '\0' ) {
{
exec_ssh ( *input ); exec_ssh ( *input );
} }
else if ( mretv == MENU_ENTRY_DELETE && cmd_list[selected_line] ) else if ( mretv == MENU_ENTRY_DELETE && cmd_list[selected_line] ) {
{
delete_ssh ( cmd_list[selected_line] ); delete_ssh ( cmd_list[selected_line] );
// Stay // Stay
retv = SSH_DIALOG; retv = SSH_DIALOG;
} }
for ( int i = 0; cmd_list[i] != NULL; i++ ) for ( int i = 0; cmd_list[i] != NULL; i++ ) {
{
free ( cmd_list[i] ); free ( cmd_list[i] );
} }
if ( cmd_list != NULL ) if ( cmd_list != NULL ) {
{
free ( cmd_list ); free ( cmd_list );
} }

View File

@ -87,8 +87,7 @@ textbox* textbox_create ( Window parent,
textbox_moveresize ( tb, tb->x, tb->y, tb->w, tb->h ); textbox_moveresize ( tb, tb->x, tb->y, tb->w, tb->h );
// edit mode controls // edit mode controls
if ( tb->flags & TB_EDITABLE ) if ( tb->flags & TB_EDITABLE ) {
{
tb->xim = XOpenIM ( display, NULL, NULL, NULL ); tb->xim = XOpenIM ( display, NULL, NULL, NULL );
tb->xic = XCreateIC ( tb->xim, XNInputStyle, XIMPreeditNothing | XIMStatusNothing, XNClientWindow, tb->window, XNFocusWindow, tb->window, NULL ); tb->xic = XCreateIC ( tb->xim, XNInputStyle, XIMPreeditNothing | XIMStatusNothing, XNClientWindow, tb->window, XNFocusWindow, tb->window, NULL );
} }
@ -134,8 +133,7 @@ void textbox_extents ( textbox *tb )
// set the default text to display // set the default text to display
void textbox_text ( textbox *tb, char *text ) void textbox_text ( textbox *tb, char *text )
{ {
if ( tb->text ) if ( tb->text ) {
{
free ( tb->text ); free ( tb->text );
} }
@ -146,8 +144,7 @@ void textbox_text ( textbox *tb, char *text )
void textbox_move ( textbox *tb, int x, int y ) void textbox_move ( textbox *tb, int x, int y )
{ {
if ( x != tb->x || y != tb->y ) if ( x != tb->x || y != tb->y ) {
{
tb->x = x; tb->x = x;
tb->y = y; tb->y = y;
XMoveResizeWindow ( display, tb->window, tb->x, tb->y, tb->w, tb->h ); XMoveResizeWindow ( display, tb->window, tb->x, tb->y, tb->w, tb->h );
@ -156,25 +153,20 @@ void textbox_move ( textbox *tb, int x, int y )
// within the parent. handled auto width/height modes // within the parent. handled auto width/height modes
void textbox_moveresize ( textbox *tb, int x, int y, int w, int h ) void textbox_moveresize ( textbox *tb, int x, int y, int w, int h )
{ {
if ( tb->flags & TB_AUTOHEIGHT ) if ( tb->flags & TB_AUTOHEIGHT ) {
{
h = tb->font->ascent + tb->font->descent; h = tb->font->ascent + tb->font->descent;
} }
if ( tb->flags & TB_AUTOWIDTH ) if ( tb->flags & TB_AUTOWIDTH ) {
{ if ( w > 1 ) {
if ( w > 1 )
{
w = MIN ( w, tb->extents.width + 2 * SIDE_MARGIN ); w = MIN ( w, tb->extents.width + 2 * SIDE_MARGIN );
} }
else else{
{
w = tb->extents.width + 2 * SIDE_MARGIN; w = tb->extents.width + 2 * SIDE_MARGIN;
} }
} }
if ( x != tb->x || y != tb->y || w != tb->w || h != tb->h ) if ( x != tb->x || y != tb->y || w != tb->w || h != tb->h ) {
{
tb->x = x; tb->x = x;
tb->y = y; tb->y = y;
tb->w = MAX ( 1, w ); tb->w = MAX ( 1, w );
@ -196,14 +188,12 @@ void textbox_hide ( textbox *tb )
// will also unmap the window if still displayed // will also unmap the window if still displayed
void textbox_free ( textbox *tb ) void textbox_free ( textbox *tb )
{ {
if ( tb->flags & TB_EDITABLE ) if ( tb->flags & TB_EDITABLE ) {
{
XDestroyIC ( tb->xic ); XDestroyIC ( tb->xic );
XCloseIM ( tb->xim ); XCloseIM ( tb->xim );
} }
if ( tb->text ) if ( tb->text ) {
{
free ( tb->text ); free ( tb->text );
} }
@ -231,8 +221,7 @@ void textbox_draw ( textbox *tb )
int cursor_x = 0; int cursor_x = 0;
int cursor_width = MAX ( 2, line_height / 10 ); int cursor_width = MAX ( 2, line_height / 10 );
if ( tb->flags & TB_EDITABLE ) if ( tb->flags & TB_EDITABLE ) {
{
int cursor_offset = 0; int cursor_offset = 0;
cursor_offset = MIN ( tb->cursor, text_len ); cursor_offset = MIN ( tb->cursor, text_len );
@ -240,8 +229,7 @@ void textbox_draw ( textbox *tb )
// The replace by _ is needed, one way or the other. // The replace by _ is needed, one way or the other.
// Make a copy, and replace all trailing spaces. // Make a copy, and replace all trailing spaces.
char *test = strdup ( text ); char *test = strdup ( text );
for ( int iter = strlen ( text ) - 1; iter >= 0 && test[iter] == ' '; iter-- ) for ( int iter = strlen ( text ) - 1; iter >= 0 && test[iter] == ' '; iter-- ) {
{
test[iter] = '_'; test[iter] = '_';
} }
// calc cursor position // calc cursor position
@ -254,30 +242,25 @@ void textbox_draw ( textbox *tb )
// calc full input text width // calc full input text width
// Calculate the right size, so no characters are cut off. // Calculate the right size, so no characters are cut off.
// TODO: Check performance of this. // TODO: Check performance of this.
do do {
{
XftTextExtentsUtf8 ( display, tb->font, ( unsigned char * ) text, length, &extents ); XftTextExtentsUtf8 ( display, tb->font, ( unsigned char * ) text, length, &extents );
line_width = extents.width; line_width = extents.width;
if ( line_width <= ( tb->w - 2 * SIDE_MARGIN ) ) if ( line_width <= ( tb->w - 2 * SIDE_MARGIN ) ) {
{
break; break;
} }
for ( length -= 1; length > 0 && ( text[length] & 0xc0 ) == 0x80; length -= 1 ) for ( length -= 1; length > 0 && ( text[length] & 0xc0 ) == 0x80; length -= 1 ) {
{
; ;
} }
} while ( line_width > 0 ); } while ( line_width > 0 );
int x = SIDE_MARGIN, y = tb->font->ascent; int x = SIDE_MARGIN, y = tb->font->ascent;
if ( tb->flags & TB_RIGHT ) if ( tb->flags & TB_RIGHT ) {
{
x = tb->w - line_width; x = tb->w - line_width;
} }
if ( tb->flags & TB_CENTER ) if ( tb->flags & TB_CENTER ) {
{
x = ( tb->w - line_width ) / 2; x = ( tb->w - line_width ) / 2;
} }
@ -285,8 +268,7 @@ void textbox_draw ( textbox *tb )
XftDrawStringUtf8 ( draw, &tb->color_fg, tb->font, x, y, ( unsigned char * ) text, length ); XftDrawStringUtf8 ( draw, &tb->color_fg, tb->font, x, y, ( unsigned char * ) text, length );
// draw the cursor // draw the cursor
if ( tb->flags & TB_EDITABLE ) if ( tb->flags & TB_EDITABLE ) {
{
XftDrawRect ( draw, &tb->color_fg, cursor_x + SIDE_MARGIN, 2, cursor_width, line_height - 4 ); XftDrawRect ( draw, &tb->color_fg, cursor_x + SIDE_MARGIN, 2, cursor_width, line_height - 4 );
} }
@ -305,8 +287,7 @@ static size_t nextrune ( textbox *tb, int inc )
ssize_t n; ssize_t n;
/* return location of next utf8 rune in the given direction (+1 or -1) */ /* return location of next utf8 rune in the given direction (+1 or -1) */
for ( n = tb->cursor + inc; n + inc >= 0 && ( tb->text[n] & 0xc0 ) == 0x80; n += inc ) for ( n = tb->cursor + inc; n + inc >= 0 && ( tb->text[n] & 0xc0 ) == 0x80; n += inc ) {
{
; ;
} }
return n; return n;
@ -372,8 +353,7 @@ void textbox_cursor_del ( textbox *tb )
// back up and delete one character // back up and delete one character
void textbox_cursor_bkspc ( textbox *tb ) void textbox_cursor_bkspc ( textbox *tb )
{ {
if ( tb->cursor > 0 ) if ( tb->cursor > 0 ) {
{
textbox_cursor_dec ( tb ); textbox_cursor_dec ( tb );
textbox_cursor_del ( tb ); textbox_cursor_del ( tb );
} }
@ -390,21 +370,18 @@ int textbox_keypress ( textbox *tb, XEvent *ev )
char pad[32]; char pad[32];
int len; int len;
if ( !( tb->flags & TB_EDITABLE ) ) if ( !( tb->flags & TB_EDITABLE ) ) {
{
return 0; return 0;
} }
len = Xutf8LookupString ( tb->xic, &ev->xkey, pad, sizeof ( pad ), &key, &stat ); len = Xutf8LookupString ( tb->xic, &ev->xkey, pad, sizeof ( pad ), &key, &stat );
pad[len] = 0; pad[len] = 0;
if ( key == XK_Left ) if ( key == XK_Left ) {
{
textbox_cursor_dec ( tb ); textbox_cursor_dec ( tb );
return 1; return 1;
} }
else if ( key == XK_Right ) else if ( key == XK_Right ) {
{
textbox_cursor_inc ( tb ); textbox_cursor_inc ( tb );
return 1; return 1;
} /*else if ( key == XK_Home ) { } /*else if ( key == XK_Home ) {
@ -415,22 +392,18 @@ int textbox_keypress ( textbox *tb, XEvent *ev )
textbox_cursor_end( tb ); textbox_cursor_end( tb );
return 1; return 1;
} */ } */
else if ( key == XK_Delete ) else if ( key == XK_Delete ) {
{
textbox_cursor_del ( tb ); textbox_cursor_del ( tb );
return 1; return 1;
} }
else if ( key == XK_BackSpace ) else if ( key == XK_BackSpace ) {
{
textbox_cursor_bkspc ( tb ); textbox_cursor_bkspc ( tb );
return 1; return 1;
} }
else if ( key == XK_Return ) else if ( key == XK_Return ) {
{
return -1; return -1;
} }
else if ( !iscntrl ( *pad ) ) else if ( !iscntrl ( *pad ) ) {
{
textbox_insert ( tb, tb->cursor, pad ); textbox_insert ( tb, tb->cursor, pad );
textbox_cursor_inc ( tb ); textbox_cursor_inc ( tb );
return 1; return 1;
@ -465,8 +438,7 @@ void textbox_setup (
void textbox_cleanup () void textbox_cleanup ()
{ {
if ( font != NULL ) if ( font != NULL ) {
{
Visual *visual = DefaultVisual ( display, DefaultScreen ( display ) ); Visual *visual = DefaultVisual ( display, DefaultScreen ( display ) );
Colormap colormap = DefaultColormap ( display, DefaultScreen ( display ) ); Colormap colormap = DefaultColormap ( display, DefaultScreen ( display ) );

View File

@ -112,8 +112,7 @@ void parse_xresource_options ( Display *display )
XrmInitialize (); XrmInitialize ();
xRMS = XResourceManagerString ( display ); xRMS = XResourceManagerString ( display );
if ( xRMS == NULL ) if ( xRMS == NULL ) {
{
return; return;
} }
XrmDatabase xDB = XrmGetStringDatabase ( xRMS ); XrmDatabase xDB = XrmGetStringDatabase ( xRMS );
@ -123,21 +122,15 @@ void parse_xresource_options ( Display *display )
const char * namePrefix = "rofi"; const char * namePrefix = "rofi";
const char * classPrefix = "rofi"; const char * classPrefix = "rofi";
for ( unsigned int i = 0; i < sizeof ( xrmOptions ) / sizeof ( *xrmOptions ); ++i ) for ( unsigned int i = 0; i < sizeof ( xrmOptions ) / sizeof ( *xrmOptions ); ++i ) {
{
char *name, *class; char *name, *class;
if ( asprintf ( &name, "%s.%s", namePrefix, xrmOptions[i].name ) == -1 ) if ( asprintf ( &name, "%s.%s", namePrefix, xrmOptions[i].name ) == -1 ) {
{
continue; continue;
} }
if ( asprintf ( &class, "%s.%s", classPrefix, xrmOptions[i].name ) > 0 ) if ( asprintf ( &class, "%s.%s", classPrefix, xrmOptions[i].name ) > 0 ) {
{ if ( XrmGetResource ( xDB, name, class, &xrmType, &xrmValue ) ) {
if ( XrmGetResource ( xDB, name, class, &xrmType, &xrmValue ) ) if ( xrmOptions[i].type == xrm_String ) {
{ if ( xrmOptions[i].mem != NULL ) {
if ( xrmOptions[i].type == xrm_String )
{
if ( xrmOptions[i].mem != NULL )
{
free ( xrmOptions[i].mem ); free ( xrmOptions[i].mem );
xrmOptions[i].mem = NULL; xrmOptions[i].mem = NULL;
} }
@ -147,18 +140,14 @@ void parse_xresource_options ( Display *display )
// Memory // Memory
xrmOptions[i].mem = ( *xrmOptions[i].str ); xrmOptions[i].mem = ( *xrmOptions[i].str );
} }
else if ( xrmOptions[i].type == xrm_Number ) else if ( xrmOptions[i].type == xrm_Number ) {
{
*xrmOptions[i].num = strtol ( xrmValue.addr, NULL, 10 ); *xrmOptions[i].num = strtol ( xrmValue.addr, NULL, 10 );
} }
else if ( xrmOptions[i].type == xrm_Boolean ) else if ( xrmOptions[i].type == xrm_Boolean ) {
{ if ( xrmValue.size > 0 && strncasecmp ( xrmValue.addr, "true", xrmValue.size ) == 0 ) {
if ( xrmValue.size > 0 && strncasecmp ( xrmValue.addr, "true", xrmValue.size ) == 0 )
{
*xrmOptions[i].num = TRUE; *xrmOptions[i].num = TRUE;
} }
else else{
{
*xrmOptions[i].num = FALSE; *xrmOptions[i].num = FALSE;
} }
} }
@ -173,10 +162,8 @@ void parse_xresource_options ( Display *display )
void parse_xresource_free ( void ) void parse_xresource_free ( void )
{ {
for ( unsigned int i = 0; i < sizeof ( xrmOptions ) / sizeof ( *xrmOptions ); ++i ) for ( unsigned int i = 0; i < sizeof ( xrmOptions ) / sizeof ( *xrmOptions ); ++i ) {
{ if ( xrmOptions[i].mem != NULL ) {
if ( xrmOptions[i].mem != NULL )
{
free ( xrmOptions[i].mem ); free ( xrmOptions[i].mem );
xrmOptions[i].mem = NULL; xrmOptions[i].mem = NULL;
} }
@ -187,13 +174,10 @@ void xresource_dump ( void )
{ {
const char * namePrefix = "rofi"; const char * namePrefix = "rofi";
unsigned int entries = sizeof ( xrmOptions ) / sizeof ( *xrmOptions ); unsigned int entries = sizeof ( xrmOptions ) / sizeof ( *xrmOptions );
for ( unsigned int i = 0; i < entries; ++i ) for ( unsigned int i = 0; i < entries; ++i ) {
{
// Skip duplicates. // Skip duplicates.
if ( ( i + 1 ) < entries ) if ( ( i + 1 ) < entries ) {
{ if ( xrmOptions[i].str == xrmOptions[i + 1].str ) {
if ( xrmOptions[i].str == xrmOptions[i + 1].str )
{
continue; continue;
} }
} }