2014-01-21 04:01:55 -05:00
|
|
|
/**
|
2014-03-01 11:27:52 -05:00
|
|
|
* rofi
|
2014-01-21 04:01:55 -05:00
|
|
|
*
|
|
|
|
* MIT/X11 License
|
|
|
|
* Copyright 2013-2014 Qball Cow <qball@gmpclient.org>
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
|
|
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
|
|
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
|
|
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <X11/X.h>
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <strings.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
2014-01-21 08:56:25 -05:00
|
|
|
#include <errno.h>
|
2014-01-21 04:01:55 -05:00
|
|
|
|
2014-03-12 03:41:38 -04:00
|
|
|
#include "rofi.h"
|
2014-05-13 04:45:59 -04:00
|
|
|
#include "history.h"
|
2014-01-21 04:01:55 -05:00
|
|
|
#include "ssh-dialog.h"
|
|
|
|
#ifdef TIMING
|
|
|
|
#include <time.h>
|
|
|
|
#endif
|
|
|
|
|
2014-05-13 04:45:59 -04:00
|
|
|
#define SSH_CACHE_FILE "rofi-2.sshcache"
|
2014-01-21 04:01:55 -05:00
|
|
|
|
2014-03-22 16:04:19 -04:00
|
|
|
static inline int execshssh ( const char *host )
|
2014-01-21 04:01:55 -05:00
|
|
|
{
|
2014-04-22 05:11:46 -04:00
|
|
|
/**
|
|
|
|
* I am not happy about this code, it causes 7 mallocs and frees
|
|
|
|
*/
|
2014-05-22 03:33:32 -04:00
|
|
|
char **args = malloc ( sizeof ( char* ) * 7 );
|
|
|
|
int i = 0;
|
2014-04-22 05:11:46 -04:00
|
|
|
args[i++] = config.terminal_emulator;
|
2014-06-04 15:29:23 -04:00
|
|
|
if ( config.ssh_set_title ) {
|
2014-04-22 08:38:36 -04:00
|
|
|
char *buffer = NULL;
|
2014-06-04 15:29:23 -04:00
|
|
|
if ( asprintf ( &buffer, "ssh %s", host ) > 0 ) {
|
2014-05-22 03:33:32 -04:00
|
|
|
args[i++] = strdup ( "-T" );
|
2014-04-22 08:38:36 -04:00
|
|
|
args[i++] = buffer;
|
|
|
|
}
|
2014-04-22 05:11:46 -04:00
|
|
|
}
|
2014-05-22 03:33:32 -04:00
|
|
|
args[i++] = strdup ( "-e" );
|
|
|
|
args[i++] = strdup ( "ssh" );
|
|
|
|
args[i++] = strdup ( host );
|
2014-04-22 05:11:46 -04:00
|
|
|
args[i++] = NULL;
|
2014-05-22 03:33:32 -04:00
|
|
|
int retv = execvp ( config.terminal_emulator, (char * const *) args );
|
2014-04-22 05:11:46 -04:00
|
|
|
|
|
|
|
// Free the args list.
|
2014-06-04 15:29:23 -04:00
|
|
|
for ( i = 0; i < 7; i++ ) {
|
|
|
|
if ( args[i] != NULL ) {
|
2014-05-22 03:33:32 -04:00
|
|
|
free ( args[i] );
|
2014-04-22 08:38:36 -04:00
|
|
|
}
|
2014-04-22 05:11:46 -04:00
|
|
|
}
|
2014-05-22 03:33:32 -04:00
|
|
|
free ( args );
|
2014-04-22 05:11:46 -04:00
|
|
|
return retv;
|
2014-01-21 04:01:55 -05:00
|
|
|
}
|
|
|
|
// execute sub-process
|
2014-03-22 16:04:19 -04:00
|
|
|
static pid_t exec_ssh ( const char *cmd )
|
2014-01-21 04:01:55 -05:00
|
|
|
{
|
2014-06-04 15:29:23 -04:00
|
|
|
if ( !cmd || !cmd[0] ) {
|
2014-03-22 16:04:19 -04:00
|
|
|
return -1;
|
|
|
|
}
|
2014-01-21 04:01:55 -05:00
|
|
|
|
2014-03-22 16:04:19 -04:00
|
|
|
signal ( SIGCHLD, catch_exit );
|
|
|
|
pid_t pid = fork ();
|
2014-01-21 04:01:55 -05:00
|
|
|
|
2014-06-04 15:29:23 -04:00
|
|
|
if ( !pid ) {
|
2014-03-22 16:04:19 -04:00
|
|
|
setsid ();
|
|
|
|
execshssh ( cmd );
|
|
|
|
exit ( EXIT_FAILURE );
|
2014-01-21 04:01:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This happens in non-critical time (After launching app)
|
|
|
|
* It is allowed to be a bit slower.
|
|
|
|
*/
|
2014-05-13 04:45:59 -04:00
|
|
|
char *path = NULL;
|
2014-06-04 15:29:23 -04:00
|
|
|
if ( asprintf ( &path, "%s/%s", cache_dir, SSH_CACHE_FILE ) > 0 ) {
|
2014-05-22 03:33:32 -04:00
|
|
|
history_set ( path, cmd );
|
|
|
|
free ( path );
|
2014-01-21 04:01:55 -05:00
|
|
|
}
|
|
|
|
return pid;
|
|
|
|
}
|
2014-03-22 16:04:19 -04:00
|
|
|
static void delete_ssh ( const char *cmd )
|
2014-02-01 08:06:08 -05:00
|
|
|
{
|
2014-06-04 15:29:23 -04:00
|
|
|
if ( !cmd || !cmd[0] ) {
|
2014-03-22 16:04:19 -04:00
|
|
|
return;
|
|
|
|
}
|
2014-05-13 04:45:59 -04:00
|
|
|
char *path = NULL;
|
2014-06-04 15:29:23 -04:00
|
|
|
if ( asprintf ( &path, "%s/%s", cache_dir, SSH_CACHE_FILE ) > 0 ) {
|
2014-05-22 03:33:32 -04:00
|
|
|
history_remove ( path, cmd );
|
|
|
|
free ( path );
|
2014-02-01 08:06:08 -05:00
|
|
|
}
|
|
|
|
}
|
2014-01-21 04:01:55 -05:00
|
|
|
static int sort_func ( const void *a, const void *b )
|
|
|
|
{
|
2014-03-22 16:04:19 -04:00
|
|
|
const char *astr = *( const char * const * ) a;
|
|
|
|
const char *bstr = *( const char * const * ) b;
|
2014-08-07 15:42:16 -04:00
|
|
|
return g_utf8_collate ( astr, bstr );
|
2014-01-21 04:01:55 -05:00
|
|
|
}
|
2014-07-20 06:29:27 -04:00
|
|
|
static char ** get_ssh ( unsigned int *length )
|
2014-01-21 04:01:55 -05:00
|
|
|
{
|
2014-03-22 16:04:19 -04:00
|
|
|
unsigned int num_favorites = 0;
|
|
|
|
char *path;
|
|
|
|
char **retv = NULL;
|
2014-01-21 04:01:55 -05:00
|
|
|
#ifdef TIMING
|
|
|
|
struct timespec start, stop;
|
2014-03-22 16:04:19 -04:00
|
|
|
clock_gettime ( CLOCK_REALTIME, &start );
|
2014-01-21 04:01:55 -05:00
|
|
|
#endif
|
|
|
|
|
2014-06-04 15:29:23 -04:00
|
|
|
if ( getenv ( "HOME" ) == NULL ) {
|
2014-03-22 16:04:19 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
2014-01-21 04:01:55 -05:00
|
|
|
|
2014-06-04 15:29:23 -04:00
|
|
|
if ( asprintf ( &path, "%s/%s", cache_dir, SSH_CACHE_FILE ) > 0 ) {
|
2014-07-20 06:29:27 -04:00
|
|
|
retv = history_get_list ( path, length );
|
2014-05-22 03:33:32 -04:00
|
|
|
free ( path );
|
2014-07-20 06:29:27 -04:00
|
|
|
num_favorites = ( *length );
|
2014-01-21 04:01:55 -05:00
|
|
|
}
|
|
|
|
|
2014-05-13 16:11:42 -04:00
|
|
|
|
2014-05-22 03:33:32 -04:00
|
|
|
FILE *fd = NULL;
|
2014-03-22 16:04:19 -04:00
|
|
|
const char *hd = getenv ( "HOME" );
|
2014-06-04 15:29:23 -04:00
|
|
|
if ( asprintf ( &path, "%s/%s", hd, ".ssh/config" ) >= 0 ) {
|
2014-05-13 16:11:42 -04:00
|
|
|
fd = fopen ( path, "r" );
|
|
|
|
}
|
2014-01-21 04:01:55 -05:00
|
|
|
|
2014-06-04 15:29:23 -04:00
|
|
|
if ( fd != NULL ) {
|
2014-05-13 04:45:59 -04:00
|
|
|
char buffer[1024];
|
2014-06-04 15:29:23 -04:00
|
|
|
while ( fgets ( buffer, 1024, fd ) != NULL ) {
|
|
|
|
if ( strncasecmp ( buffer, "Host", 4 ) == 0 ) {
|
2014-03-22 16:04:19 -04:00
|
|
|
int start = 0, stop = 0;
|
|
|
|
buffer[strlen ( buffer ) - 1] = '\0';
|
|
|
|
|
2014-06-04 15:29:23 -04:00
|
|
|
for ( start = 4; isspace ( buffer[start] ); start++ ) {
|
2014-03-22 16:04:19 -04:00
|
|
|
;
|
|
|
|
}
|
2014-01-21 04:13:42 -05:00
|
|
|
|
2014-03-22 16:04:19 -04:00
|
|
|
for ( stop = start; isalnum ( buffer[stop] ) ||
|
2014-01-26 10:47:26 -05:00
|
|
|
buffer[stop] == '_' ||
|
2014-06-04 15:29:23 -04:00
|
|
|
buffer[stop] == '.'; stop++ ) {
|
2014-03-22 16:04:19 -04:00
|
|
|
;
|
|
|
|
}
|
2014-01-21 04:13:42 -05:00
|
|
|
|
2014-01-21 04:35:59 -05:00
|
|
|
int found = 0;
|
2014-01-21 08:56:25 -05:00
|
|
|
|
2014-06-04 15:29:23 -04:00
|
|
|
if ( start == stop ) {
|
2014-03-22 16:04:19 -04:00
|
|
|
continue;
|
|
|
|
}
|
2014-01-22 04:01:45 -05:00
|
|
|
|
2014-01-21 04:35:59 -05:00
|
|
|
// This is a nice little penalty, but doable? time will tell.
|
|
|
|
// given num_favorites is max 25.
|
2014-06-04 15:29:23 -04:00
|
|
|
for ( unsigned int j = 0; found == 0 && j < num_favorites; j++ ) {
|
|
|
|
if ( strncasecmp ( &buffer[start], retv[j], stop - start ) == 0 ) {
|
2014-03-22 16:04:19 -04:00
|
|
|
found = 1;
|
|
|
|
}
|
2014-01-21 04:35:59 -05:00
|
|
|
}
|
2014-01-21 08:56:25 -05:00
|
|
|
|
2014-06-04 15:29:23 -04:00
|
|
|
if ( found == 1 ) {
|
2014-03-22 16:04:19 -04:00
|
|
|
continue;
|
|
|
|
}
|
2014-01-21 04:35:59 -05:00
|
|
|
|
2014-07-20 06:29:27 -04:00
|
|
|
char **tr = realloc ( retv, ( ( *length ) + 2 ) * sizeof ( char* ) );
|
2014-06-04 15:29:23 -04:00
|
|
|
if ( tr != NULL ) {
|
2014-07-20 06:29:27 -04:00
|
|
|
retv = tr;
|
|
|
|
retv[( *length )] = strndup ( &buffer[start], stop - start );
|
|
|
|
retv[( *length ) + 1] = NULL;
|
|
|
|
( *length )++;
|
2014-05-26 03:19:58 -04:00
|
|
|
}
|
2014-01-21 04:01:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-22 16:04:19 -04:00
|
|
|
fclose ( fd );
|
2014-01-21 04:01:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: check this is still fast enough. (takes 1ms on laptop.)
|
2014-07-20 06:29:27 -04:00
|
|
|
if ( ( *length ) > num_favorites ) {
|
|
|
|
qsort ( &retv[num_favorites], ( *length ) - num_favorites, sizeof ( char* ), sort_func );
|
2014-03-11 15:16:44 -04:00
|
|
|
}
|
2014-03-22 16:04:19 -04:00
|
|
|
free ( path );
|
2014-01-21 04:01:55 -05:00
|
|
|
#ifdef TIMING
|
2014-03-22 16:04:19 -04:00
|
|
|
clock_gettime ( CLOCK_REALTIME, &stop );
|
2014-01-21 04:01:55 -05:00
|
|
|
|
2014-06-04 15:29:23 -04:00
|
|
|
if ( stop.tv_sec != start.tv_sec ) {
|
2014-03-22 16:04:19 -04:00
|
|
|
stop.tv_nsec += ( stop.tv_sec - start.tv_sec ) * 1e9;
|
2014-01-21 04:01:55 -05:00
|
|
|
}
|
|
|
|
|
2014-03-22 16:04:19 -04:00
|
|
|
long diff = stop.tv_nsec - start.tv_nsec;
|
|
|
|
printf ( "Time elapsed: %ld us\n", diff / 1000 );
|
2014-01-21 04:01:55 -05:00
|
|
|
#endif
|
|
|
|
return retv;
|
|
|
|
}
|
|
|
|
|
2014-07-21 15:39:24 -04:00
|
|
|
SwitcherMode ssh_switcher_dialog ( char **input, void *data )
|
2014-01-21 04:01:55 -05:00
|
|
|
{
|
|
|
|
SwitcherMode retv = MODE_EXIT;
|
|
|
|
// act as a launcher
|
2014-07-20 06:29:27 -04:00
|
|
|
unsigned int cmd_list_length = 0;
|
|
|
|
char **cmd_list = get_ssh ( &cmd_list_length );
|
2014-01-21 04:01:55 -05:00
|
|
|
|
2014-06-04 15:29:23 -04:00
|
|
|
if ( cmd_list == NULL ) {
|
2014-05-19 10:48:50 -04:00
|
|
|
cmd_list = malloc ( 2 * sizeof ( char * ) );
|
2014-03-22 16:04:19 -04:00
|
|
|
cmd_list[0] = strdup ( "No ssh hosts found" );
|
2014-01-21 04:01:55 -05:00
|
|
|
cmd_list[1] = NULL;
|
|
|
|
}
|
|
|
|
|
2014-03-22 16:04:19 -04:00
|
|
|
int shift = 0;
|
2014-02-01 08:03:23 -05:00
|
|
|
int selected_line = 0;
|
2014-08-03 11:05:06 -04:00
|
|
|
int mretv = menu ( cmd_list, cmd_list_length, input, "ssh:",
|
2014-08-03 15:09:20 -04:00
|
|
|
NULL, &shift, token_match, NULL, &selected_line, config.levenshtein_sort );
|
2014-02-01 17:04:45 -05:00
|
|
|
|
2014-06-04 15:29:23 -04:00
|
|
|
if ( mretv == MENU_NEXT ) {
|
2014-01-22 03:24:31 -05:00
|
|
|
retv = NEXT_DIALOG;
|
2014-03-22 16:04:19 -04:00
|
|
|
}
|
2014-07-21 17:19:45 -04:00
|
|
|
else if ( mretv == MENU_QUICK_SWITCH ) {
|
|
|
|
retv = selected_line;
|
|
|
|
}
|
2014-06-04 15:29:23 -04:00
|
|
|
else if ( mretv == MENU_OK && cmd_list[selected_line] != NULL ) {
|
2014-03-22 16:04:19 -04:00
|
|
|
exec_ssh ( cmd_list[selected_line] );
|
|
|
|
}
|
2014-06-04 15:29:23 -04:00
|
|
|
else if ( mretv == MENU_CUSTOM_INPUT && *input != NULL && *input[0] != '\0' ) {
|
2014-03-22 16:04:19 -04:00
|
|
|
exec_ssh ( *input );
|
|
|
|
}
|
2014-06-04 15:29:23 -04:00
|
|
|
else if ( mretv == MENU_ENTRY_DELETE && cmd_list[selected_line] ) {
|
2014-02-01 08:06:08 -05:00
|
|
|
delete_ssh ( cmd_list[selected_line] );
|
2014-02-01 08:39:49 -05:00
|
|
|
// Stay
|
2014-07-21 15:39:24 -04:00
|
|
|
retv = RELOAD_DIALOG;
|
2014-01-21 04:01:55 -05:00
|
|
|
}
|
|
|
|
|
2014-06-04 15:29:23 -04:00
|
|
|
for ( int i = 0; cmd_list[i] != NULL; i++ ) {
|
2014-03-22 16:04:19 -04:00
|
|
|
free ( cmd_list[i] );
|
2014-01-21 04:01:55 -05:00
|
|
|
}
|
|
|
|
|
2014-06-04 15:29:23 -04:00
|
|
|
if ( cmd_list != NULL ) {
|
2014-03-22 16:04:19 -04:00
|
|
|
free ( cmd_list );
|
|
|
|
}
|
2014-01-21 04:01:55 -05:00
|
|
|
|
|
|
|
return retv;
|
|
|
|
}
|