uxputty.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Unix PuTTY main program.
  3. */
  4. #include <stdio.h>
  5. #include <ctype.h>
  6. #include <stdlib.h>
  7. #include <assert.h>
  8. #include <unistd.h>
  9. #include <gdk/gdk.h>
  10. #include "putty.h"
  11. #include "storage.h"
  12. /*
  13. * Clean up and exit.
  14. */
  15. void cleanup_exit(int code)
  16. {
  17. /*
  18. * Clean up.
  19. */
  20. sk_cleanup();
  21. random_save_seed();
  22. exit(code);
  23. }
  24. Backend *select_backend(Config *cfg)
  25. {
  26. int i;
  27. Backend *back = NULL;
  28. for (i = 0; backends[i].backend != NULL; i++)
  29. if (backends[i].protocol == cfg->protocol) {
  30. back = backends[i].backend;
  31. break;
  32. }
  33. assert(back != NULL);
  34. return back;
  35. }
  36. int cfgbox(Config *cfg)
  37. {
  38. return do_config_box("PuTTY Configuration", cfg, 0);
  39. }
  40. static int got_host = 0;
  41. const int use_event_log = 1, new_session = 1, saved_sessions = 1;
  42. int process_nonoption_arg(char *arg, Config *cfg)
  43. {
  44. char *p, *q = arg;
  45. if (got_host) {
  46. /*
  47. * If we already have a host name, treat this argument as a
  48. * port number. NB we have to treat this as a saved -P
  49. * argument, so that it will be deferred until it's a good
  50. * moment to run it.
  51. */
  52. int ret = cmdline_process_param("-P", arg, 1, cfg);
  53. assert(ret == 2);
  54. } else if (!strncmp(q, "telnet:", 7)) {
  55. /*
  56. * If the hostname starts with "telnet:",
  57. * set the protocol to Telnet and process
  58. * the string as a Telnet URL.
  59. */
  60. char c;
  61. q += 7;
  62. if (q[0] == '/' && q[1] == '/')
  63. q += 2;
  64. cfg->protocol = PROT_TELNET;
  65. p = q;
  66. while (*p && *p != ':' && *p != '/')
  67. p++;
  68. c = *p;
  69. if (*p)
  70. *p++ = '\0';
  71. if (c == ':')
  72. cfg->port = atoi(p);
  73. else
  74. cfg->port = -1;
  75. strncpy(cfg->host, q, sizeof(cfg->host) - 1);
  76. cfg->host[sizeof(cfg->host) - 1] = '\0';
  77. got_host = 1;
  78. } else {
  79. /*
  80. * Otherwise, treat this argument as a host name.
  81. */
  82. p = arg;
  83. while (*p && !isspace((unsigned char)*p))
  84. p++;
  85. if (*p)
  86. *p++ = '\0';
  87. strncpy(cfg->host, q, sizeof(cfg->host) - 1);
  88. cfg->host[sizeof(cfg->host) - 1] = '\0';
  89. got_host = 1;
  90. }
  91. return 1;
  92. }
  93. char *make_default_wintitle(char *hostname)
  94. {
  95. return dupcat(hostname, " - PuTTY", NULL);
  96. }
  97. /*
  98. * X11-forwarding-related things suitable for Gtk app.
  99. */
  100. const char platform_x11_best_transport[] = "unix";
  101. char *platform_get_x_display(void) {
  102. const char *display;
  103. /* Try to take account of --display and what have you. */
  104. if (!(display = gdk_get_display()))
  105. /* fall back to traditional method */
  106. display = getenv("DISPLAY");
  107. return dupstr(display);
  108. }
  109. int main(int argc, char **argv)
  110. {
  111. extern int pt_main(int argc, char **argv);
  112. sk_init();
  113. flags = FLAG_VERBOSE | FLAG_INTERACTIVE;
  114. default_protocol = be_default_protocol;
  115. /* Find the appropriate default port. */
  116. {
  117. int i;
  118. default_port = 0; /* illegal */
  119. for (i = 0; backends[i].backend != NULL; i++)
  120. if (backends[i].protocol == default_protocol) {
  121. default_port = backends[i].backend->default_port;
  122. break;
  123. }
  124. }
  125. return pt_main(argc, argv);
  126. }