Bläddra i källkod

New feature command aliases

Héctor García 10 år sedan
förälder
incheckning
64a76976ca
4 ändrade filer med 110 tillägg och 11 borttagningar
  1. 14 10
      ChangeLog
  2. 36 0
      src/mssh-window.c
  3. 3 0
      src/mssh-window.h
  4. 57 1
      src/mssh.c

+ 14 - 10
ChangeLog

@@ -1,28 +1,32 @@
+2014-03-13  Héctor García  <[email protected]>
+
+	Added a new feature to the aliases file. Now you can define aliases for command line text to send using {}
+
 2014-01-20  Héctor García  <[email protected]>
 
-    Make paste with mouse middle button focus on terminal
+	Make paste with mouse middle button focus on terminal
 
 2014-01-15  Héctor García  <[email protected]>
 
-    Added patch from Oscar Fernandez to support comments on configuration archive
+	Added patch from Oscar Fernandez to support comments on configuration archive
 
-    Added patch from Mario J. Barchéin and Francisco Licerán to support port numbers on host
+	Added patch from Mario J. Barchéin and Francisco Licerán to support port numbers on host
 
-    Copy Debian's mssh man page to upstream and added Karl Goetz <[email protected]> patch
+	Copy Debian's mssh man page to upstream and added Karl Goetz <[email protected]> patch
 
-    Allow window to be smaller than 1024x768. Patch from Lorenzo Masini <[email protected]>
+	Allow window to be smaller than 1024x768. Patch from Lorenzo Masini <[email protected]>
 
 2014-01-15  Héctor García  <[email protected]>
 
-    Bumped version to 1.3 gtk2 deprecated symbols free
+	Bumped version to 1.3 gtk2 deprecated symbols free
 
-    Replaced GDK_<keyname> with GDK_KEY_<keyname>
+	Replaced GDK_<keyname> with GDK_KEY_<keyname>
 
-    Changed direct access to use accessor for terminal->parent
+	Changed direct access to use accessor for terminal->parent
 
-    Changed deprecated gtk_menu_bar_append with gtk_menu_shell_append
+	Changed deprecated gtk_menu_bar_append with gtk_menu_shell_append
 
-    Migrate from vte_terminal_fork_command to vte_terminal_fork_command_full to make compile against GTK 2.24
+	Migrate from vte_terminal_fork_command to vte_terminal_fork_command_full to make compile against GTK 2.24
 
 2010-01-15  Bradley Smith  <[email protected]>
 

+ 36 - 0
src/mssh-window.c

@@ -55,6 +55,24 @@ static void mssh_window_sendhost(GtkWidget *widget, gpointer data)
     }
 }
 
+static void mssh_window_sendcommand(GtkWidget *widget, gpointer data)
+{
+    int i;
+    char *command;
+
+    MSSHWindow *window = MSSH_WINDOW(data);
+    GtkMenuItem *item = (GtkMenuItem *)widget;
+
+    command = g_datalist_get_data(MSSH_WINDOW(data)->commands, gtk_menu_item_get_label (item));
+
+    for(i = 0; i < window->terminals->len; i++)
+    {
+        mssh_terminal_send_string(g_array_index(window->terminals,
+            MSSHTerminal*, i), command);
+    }
+
+}
+
 static void mssh_window_destroy(GtkWidget *widget, gpointer data)
 {
     gtk_main_quit();
@@ -388,6 +406,7 @@ static void mssh_window_init(MSSHWindow* window)
     GtkWidget *file_item = gtk_menu_item_new_with_label("File");
     GtkWidget *edit_item = gtk_menu_item_new_with_label("Edit");
     GtkWidget *server_item = gtk_menu_item_new_with_label("Servers");
+    GtkWidget *command_item = gtk_menu_item_new_with_label("Commands");
 
     GtkWidget *file_quit = gtk_image_menu_item_new_from_stock(
         GTK_STOCK_QUIT, NULL);
@@ -405,6 +424,8 @@ static void mssh_window_init(MSSHWindow* window)
 
     window->server_menu = gtk_menu_new();
 
+    window->command_menu = gtk_menu_new();
+
     window->global_entry = entry;
 
     window->last_closed = -1;
@@ -415,6 +436,8 @@ static void mssh_window_init(MSSHWindow* window)
     gtk_menu_item_set_submenu(GTK_MENU_ITEM(edit_item), edit_menu);
     gtk_menu_item_set_submenu(GTK_MENU_ITEM(server_item),
         window->server_menu);
+    gtk_menu_item_set_submenu(GTK_MENU_ITEM(command_item),
+        window->command_menu);
 
 /*  gtk_menu_shell_append(GTK_MENU_SHELL(file_menu), file_add);*/
     gtk_menu_shell_append(GTK_MENU_SHELL(file_menu), file_sendhost);
@@ -430,6 +453,7 @@ static void mssh_window_init(MSSHWindow* window)
     gtk_menu_shell_append(GTK_MENU_SHELL(menu_bar), file_item);
     gtk_menu_shell_append(GTK_MENU_SHELL(menu_bar), edit_item);
     gtk_menu_shell_append(GTK_MENU_SHELL(menu_bar), server_item);
+    gtk_menu_shell_append(GTK_MENU_SHELL(menu_bar), command_item);
 
     g_signal_connect(G_OBJECT(entry), "key-press-event",
         G_CALLBACK(mssh_window_key_press), window);
@@ -525,6 +549,18 @@ void mssh_window_start_session(MSSHWindow* window, char **env,
     mssh_window_relayout(window);
 }
 
+void mssh_window_add_command(GQuark key_id, gpointer data, gpointer user_data)
+{
+    GtkWidget *menu_item;
+    GtkWidget* window = (GtkWidget *)user_data;
+
+    menu_item = gtk_menu_item_new_with_label(g_quark_to_string (key_id));
+
+    gtk_menu_shell_append(GTK_MENU_SHELL(MSSH_WINDOW(window)->command_menu), menu_item);
+    g_signal_connect(G_OBJECT(menu_item), "activate",
+        G_CALLBACK(mssh_window_sendcommand), window);
+}
+
 static void mssh_window_class_init(MSSHWindowClass *klass)
 {
 }

+ 3 - 0
src/mssh-window.h

@@ -23,6 +23,7 @@ typedef struct
     GtkWindow widget;
     GtkWidget *table;
     GtkWidget *server_menu;
+    GtkWidget *command_menu;
     GtkWidget *global_entry;
     GtkAccelGroup *accel;
     GArray *terminals;
@@ -35,6 +36,7 @@ typedef struct
     gint modifier;
     gint dir_focus;
     gint last_closed;
+    GData **commands;
 } MSSHWindow;
 
 typedef struct
@@ -47,6 +49,7 @@ GType mssh_window_get_type(void) G_GNUC_CONST;
 GtkWidget* mssh_window_new(void);
 void mssh_window_start_session(MSSHWindow* window, char **env,
     GArray *hosts, long cols);
+void mssh_window_add_command(GQuark key_id, gpointer data, gpointer user_data);
 void mssh_window_relayout(MSSHWindow *window);
 void mssh_window_session_closed(MSSHTerminal *terminal, gpointer data);
 gboolean mssh_window_focus(GtkWidget *widget, GObject *acceleratable,

+ 57 - 1
src/mssh.c

@@ -121,7 +121,10 @@ GData **parse_aliases(char *conffile)
         if(strcmp(line, "") == 0)
             continue;
 
-        if( *line == '#')
+        if(*line == '#')
+            continue;
+
+        if(*line == '{')
             continue;
 
         if((sep = strchr(line, ':')) == NULL)
@@ -162,6 +165,55 @@ GData **parse_aliases(char *conffile)
     return aliases;
 }
 
+GData **parse_commands(char *conffile)
+{
+    FILE *file;
+    char *line;
+    int lineno = 0;
+
+    GData **commands = malloc(sizeof(GData*));
+    g_datalist_init(commands);
+
+    if((file = fopen(conffile, "r")) == NULL)
+        return commands;
+
+    while((line = fgetline(file)) != NULL)
+    {
+        char *sep, *command, *commandline;
+
+        lineno++;
+
+        if(strcmp(line, "") == 0)
+            continue;
+
+        if(*line == '#')
+            continue;
+
+        if(*line != '{')
+            continue;
+
+        if((sep = strchr(line, '}')) == NULL)
+        {
+            printf("Line %d: Failed to parse line '%s'\n", lineno, line);
+            exit(EXIT_FAILURE);
+        }
+
+        *sep = '\0';
+        command = line + 1;
+
+        if((commandline = strtok(sep + 1, " ")) == NULL)
+        {
+            printf("Line %d: Command Alias '%s' specifies no command\n", lineno,
+                command);
+            exit(EXIT_FAILURE);
+        }
+
+        g_datalist_set_data(commands, command, commandline);
+    }
+
+    return commands;
+}
+
 int main(int argc, char* argv[], char* env[])
 {
     GtkWidget* window;
@@ -169,6 +221,7 @@ int main(int argc, char* argv[], char* env[])
     char *home, *conffile;
     long cols = 0;
     GData **aliases = NULL;
+    GData **commands = NULL;
     GArray *hosts = NULL;
 
     static struct option long_options[] =
@@ -188,6 +241,7 @@ int main(int argc, char* argv[], char* env[])
         snprintf(conffile, len, "%s/%s", home, CONFFILE);
 
         aliases = parse_aliases(conffile);
+        commands = parse_commands(conffile);
         free(conffile);
     }
     else
@@ -290,6 +344,8 @@ int main(int argc, char* argv[], char* env[])
         G_CALLBACK(on_mssh_destroy), NULL);
 
     mssh_window_start_session(MSSH_WINDOW(window), env, hosts, cols);
+    g_datalist_foreach(commands, mssh_window_add_command, window);
+    MSSH_WINDOW(window)->commands = commands;
 
     gtk_widget_show_all(window);
     gtk_main();