mssh.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <getopt.h>
  5. #include <errno.h>
  6. #define GETTEXT_PACKAGE "mssh"
  7. #include <glib/gi18n-lib.h>
  8. #include <gtk/gtk.h>
  9. #include "config.h"
  10. #include "mssh-window.h"
  11. #define CONFFILE ".mssh_clusters"
  12. #define PKGINFO PACKAGE_NAME " " VERSION
  13. #define COPYRIGHT "Copyright (C) 2009 Bradley Smith <[email protected]>"
  14. static void on_mssh_destroy(GtkWidget *widget, gpointer data)
  15. {
  16. gtk_widget_hide(widget);
  17. gtk_main_quit();
  18. }
  19. void usage(const char *argv0)
  20. {
  21. fprintf(stderr, "%s\n", PKGINFO);
  22. fprintf(stderr, "%s\n", COPYRIGHT);
  23. fprintf(stderr, "An ssh client to issue the same commands to multiple servers\n\n");
  24. fprintf(stderr, "Usage: %s [OPTION]... (-a ALIAS | HOSTS)\n\n",
  25. argv0);
  26. fprintf(stderr,
  27. " -a, --alias=ALIAS Open hosts associated with named alias\n");
  28. fprintf(stderr,
  29. " -c, --columns=NUM Override gconf for number of columns\n");
  30. fprintf(stderr,
  31. " -h, --help Display this help and exit\n");
  32. fprintf(stderr,
  33. " -V, --version Output version information and exit\n");
  34. fprintf(stderr, "\nReport bugs to <%s>.\n", PACKAGE_BUGREPORT);
  35. exit(EXIT_FAILURE);
  36. }
  37. static char *fgetline(FILE *stream)
  38. {
  39. size_t len = 64;
  40. size_t pos = 0;
  41. char c;
  42. char *buf;
  43. if((buf = malloc(len)) == NULL)
  44. {
  45. perror("malloc");
  46. exit(EXIT_FAILURE);
  47. }
  48. while((c = fgetc(stream)) != EOF)
  49. {
  50. if(pos >= len)
  51. {
  52. len *= 2;
  53. if((buf = realloc(buf, len)) == NULL)
  54. {
  55. perror("realloc");
  56. exit(EXIT_FAILURE);
  57. }
  58. }
  59. if(c == '\n')
  60. {
  61. buf[pos++] = '\0';
  62. break;
  63. }
  64. else
  65. {
  66. buf[pos++] = c;
  67. }
  68. }
  69. if(c == EOF)
  70. {
  71. free(buf);
  72. return NULL;
  73. }
  74. return buf;
  75. }
  76. void append_alias(char *alias, GArray *hosts, GData **aliases, int lineno)
  77. {
  78. int i;
  79. GArray *fetched;
  80. if((fetched = g_datalist_get_data(aliases, alias)) == NULL)
  81. {
  82. printf("Line %d: Alias '%s' not defined\n", lineno, alias);
  83. exit(EXIT_FAILURE);
  84. }
  85. for(i = 0; i < fetched->len; i++)
  86. {
  87. g_array_append_val(hosts, g_array_index(fetched, char*, i));
  88. }
  89. }
  90. GData **parse_aliases(char *conffile)
  91. {
  92. FILE *file;
  93. char *line;
  94. int lineno = 0;
  95. GData **aliases = malloc(sizeof(GData*));
  96. g_datalist_init(aliases);
  97. if((file = fopen(conffile, "r")) == NULL)
  98. return aliases;
  99. while((line = fgetline(file)) != NULL)
  100. {
  101. char *sep, *alias, *hoststr, *tmp;
  102. GArray *hosts;
  103. lineno++;
  104. if(strcmp(line, "") == 0)
  105. continue;
  106. if(*line == '#')
  107. continue;
  108. if(*line == '{')
  109. continue;
  110. if((sep = strchr(line, ':')) == NULL)
  111. {
  112. printf("Line %d: Failed to parse line '%s'\n", lineno, line);
  113. exit(EXIT_FAILURE);
  114. }
  115. *sep = '\0';
  116. alias = line;
  117. hoststr = sep + 1;
  118. if((tmp = strtok(hoststr, " ")) == NULL)
  119. {
  120. printf("Line %d: Alias '%s' specifies no hosts\n", lineno,
  121. alias);
  122. exit(EXIT_FAILURE);
  123. }
  124. hosts = g_array_new(FALSE, TRUE, sizeof(char*));
  125. do
  126. {
  127. if(tmp[0] == '[' && tmp[strlen(tmp) - 1] == ']')
  128. {
  129. tmp++;
  130. tmp[strlen(tmp) - 1] = '\0';
  131. append_alias(tmp, hosts, aliases, lineno);
  132. }
  133. else
  134. g_array_append_val(hosts, tmp);
  135. }
  136. while((tmp = strtok(NULL, " ")) != NULL);
  137. g_datalist_set_data(aliases, alias, hosts);
  138. }
  139. return aliases;
  140. }
  141. GData **parse_commands(char *conffile)
  142. {
  143. FILE *file;
  144. char *line;
  145. int lineno = 0;
  146. GData **commands = malloc(sizeof(GData*));
  147. g_datalist_init(commands);
  148. if((file = fopen(conffile, "r")) == NULL)
  149. return commands;
  150. while((line = fgetline(file)) != NULL)
  151. {
  152. char *sep, *command, *commandline;
  153. lineno++;
  154. if(strcmp(line, "") == 0)
  155. continue;
  156. if(*line == '#')
  157. continue;
  158. if(*line != '{')
  159. continue;
  160. if((sep = strchr(line, '}')) == NULL)
  161. {
  162. printf("Line %d: Failed to parse line '%s'\n", lineno, line);
  163. exit(EXIT_FAILURE);
  164. }
  165. *sep = '\0';
  166. command = line + 1;
  167. if((commandline = index(sep + 1, ' ')) == NULL)
  168. {
  169. printf("Line %d: Command Alias '%s' specifies no command\n", lineno,
  170. command);
  171. exit(EXIT_FAILURE);
  172. }
  173. while ( *commandline == ' ' ) { commandline++; }
  174. sep = commandline;
  175. while ( ( sep = index(sep, '\\') ) != NULL ) {
  176. if ( *(sep+1) == 'n' ) {
  177. *sep = ' ';
  178. *(sep+1) = '\n';
  179. }
  180. sep++;
  181. }
  182. g_datalist_set_data(commands, command, commandline);
  183. }
  184. return commands;
  185. }
  186. int main(int argc, char* argv[], char* env[])
  187. {
  188. GtkWidget* window;
  189. int c, option_index = 0;
  190. char *home, *conffile;
  191. long cols = 0;
  192. GData **aliases = NULL;
  193. GData **commands = NULL;
  194. GArray *hosts = NULL;
  195. static struct option long_options[] =
  196. {
  197. {"alias", required_argument, 0, 'a'},
  198. {"columns", required_argument, 0, 'c'},
  199. {"help", no_argument, 0, 'h'},
  200. {"version", no_argument, 0, 'V'},
  201. {0, 0, 0, 0}
  202. };
  203. if((home = getenv("HOME")) != NULL)
  204. {
  205. int len = strlen(home) + strlen(CONFFILE) + 2;
  206. conffile = malloc(len);
  207. snprintf(conffile, len, "%s/%s", home, CONFFILE);
  208. aliases = parse_aliases(conffile);
  209. commands = parse_commands(conffile);
  210. free(conffile);
  211. }
  212. else
  213. {
  214. fprintf(stderr,
  215. "Warning: $HOME not set, not reading config file\n");
  216. }
  217. for(;;)
  218. {
  219. c = getopt_long(argc, argv, "a:c:hV", long_options, &option_index);
  220. if(c == -1)
  221. break;
  222. switch(c)
  223. {
  224. case 'a':
  225. if(aliases && (hosts = g_datalist_get_data(aliases,
  226. optarg)) == NULL)
  227. {
  228. fprintf(stderr, "Alias '%s' not found\n\n", optarg);
  229. usage(argv[0]);
  230. }
  231. break;
  232. case 'c':
  233. errno = 0;
  234. cols = strtol(optarg, NULL, 10);
  235. if(cols <= 0 || errno != 0)
  236. {
  237. fprintf(stderr, "Invalid number of columns '%s'\n\n",
  238. optarg);
  239. usage(argv[0]);
  240. }
  241. break;
  242. case 'h':
  243. usage(argv[0]);
  244. break;
  245. case 'V':
  246. printf("%s\n\n", PKGINFO);
  247. printf("%s\n\n", COPYRIGHT);
  248. printf("Redistribution and use in source and binary forms, with or without\n");
  249. printf("modification, are permitted provided that the following conditions are met:\n");
  250. printf("\n");
  251. printf(" 1. Redistributions of source code must retain the copyright notice,\n");
  252. printf(" this list of conditions and the following disclaimer.\n");
  253. printf(" 2. Redistributions in binary form must reproduce the copyright notice,\n");
  254. printf(" this list of conditions and the following disclaimer in the\n");
  255. printf(" documentation and/or other materials provided with the distribution.\n");
  256. printf(" 3. The name of the author may not be used to endorse or promote\n");
  257. printf(" products derived from this software without specific prior written\n");
  258. printf(" permission.\n");
  259. printf("\n");
  260. printf("THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR\n");
  261. printf("IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\n");
  262. printf("OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN\n");
  263. printf("NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n");
  264. printf("SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED\n");
  265. printf("TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\n");
  266. printf("PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\n");
  267. printf("LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\n");
  268. printf("NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\n");
  269. printf("SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n");
  270. exit(EXIT_SUCCESS);
  271. break;
  272. case '?':
  273. printf("\n");
  274. usage(argv[0]);
  275. exit(EXIT_FAILURE);
  276. break;
  277. default:
  278. abort();
  279. }
  280. }
  281. if(hosts == NULL)
  282. {
  283. hosts = g_array_new(FALSE, TRUE, sizeof(char*));
  284. if (optind < argc)
  285. {
  286. while (optind < argc)
  287. {
  288. char *host = strdup(argv[optind++]);
  289. g_array_append_val(hosts, host);
  290. }
  291. }
  292. else
  293. {
  294. fprintf(stderr, "No hosts specified\n\n");
  295. usage(argv[0]);
  296. }
  297. }
  298. gtk_init(&argc, &argv);
  299. setlocale (LC_ALL, "");
  300. bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR);
  301. bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
  302. textdomain(GETTEXT_PACKAGE);
  303. window = GTK_WIDGET(mssh_window_new());
  304. g_signal_connect(G_OBJECT(window), "destroy",
  305. G_CALLBACK(on_mssh_destroy), NULL);
  306. MSSH_WINDOW(window)->commands = commands;
  307. mssh_window_start_session(MSSH_WINDOW(window), env, hosts, cols);
  308. g_datalist_foreach(commands, mssh_window_add_command, window);
  309. gtk_widget_show_all(window);
  310. gtk_main();
  311. return 0;
  312. }