mssh.c 10 KB

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