| 1 | /* |
| 2 | * Unix PuTTY main program. |
| 3 | */ |
| 4 | |
| 5 | #include <stdio.h> |
| 6 | #include <ctype.h> |
| 7 | #include <stdlib.h> |
| 8 | #include <assert.h> |
| 9 | #include <unistd.h> |
| 10 | |
| 11 | #include "putty.h" |
| 12 | #include "storage.h" |
| 13 | |
| 14 | /* |
| 15 | * TODO: |
| 16 | * |
| 17 | * - Go through all the config options and ensure they can all be |
| 18 | * configured and reconfigured properly. |
| 19 | * |
| 20 | * - Remainder of the context menu: |
| 21 | * |
| 22 | * - New Session, Duplicate Session and the Saved Sessions |
| 23 | * submenu. |
| 24 | * + at least New and Duplicate probably _should_ be in |
| 25 | * pterm. |
| 26 | * + Duplicate Session will be fun, since we must work out |
| 27 | * how to pass the config data through. |
| 28 | * + In fact this should be easier on Unix, since fork() is |
| 29 | * available so we need not even exec (this also saves us |
| 30 | * the trouble of scrabbling around trying to find our own |
| 31 | * binary). Possible scenario: respond to Duplicate |
| 32 | * Session by forking. Parent continues as before; child |
| 33 | * unceremoniously frees all extant resources (backend, |
| 34 | * terminal, ldisc, frontend etc) and then _longjmps_ (I |
| 35 | * kid you not) back to a point in pt_main() which causes |
| 36 | * it to go back round to the point of opening a new |
| 37 | * terminal window and a new backend. |
| 38 | * + A tricky bit here is how to free everything without |
| 39 | * also _destroying_ things - calling GTK to free up |
| 40 | * existing widgets is liable to send destroy messages to |
| 41 | * the X server, which won't go down too well with the |
| 42 | * parent process. exec() is a much cleaner solution to |
| 43 | * this bit, but requires us to invent some ghastly IPC as |
| 44 | * we did in Windows PuTTY. |
| 45 | * + Arrgh! Also, this won't work in pterm since we'll |
| 46 | * already have dropped privileges by this point, so we |
| 47 | * can't get another pty. Sigh. Looks like exec has to be |
| 48 | * the way forward then :-/ |
| 49 | */ |
| 50 | |
| 51 | /* |
| 52 | * Clean up and exit. |
| 53 | */ |
| 54 | void cleanup_exit(int code) |
| 55 | { |
| 56 | /* |
| 57 | * Clean up. |
| 58 | */ |
| 59 | sk_cleanup(); |
| 60 | random_save_seed(); |
| 61 | exit(code); |
| 62 | } |
| 63 | |
| 64 | Backend *select_backend(Config *cfg) |
| 65 | { |
| 66 | int i; |
| 67 | Backend *back = NULL; |
| 68 | for (i = 0; backends[i].backend != NULL; i++) |
| 69 | if (backends[i].protocol == cfg->protocol) { |
| 70 | back = backends[i].backend; |
| 71 | break; |
| 72 | } |
| 73 | assert(back != NULL); |
| 74 | return back; |
| 75 | } |
| 76 | |
| 77 | int cfgbox(Config *cfg) |
| 78 | { |
| 79 | return do_config_box("PuTTY Configuration", cfg, 0); |
| 80 | } |
| 81 | |
| 82 | static int got_host = 0; |
| 83 | |
| 84 | const int use_event_log = 1; |
| 85 | |
| 86 | int process_nonoption_arg(char *arg, Config *cfg) |
| 87 | { |
| 88 | char *p, *q = arg; |
| 89 | |
| 90 | if (got_host) { |
| 91 | /* |
| 92 | * If we already have a host name, treat this argument as a |
| 93 | * port number. NB we have to treat this as a saved -P |
| 94 | * argument, so that it will be deferred until it's a good |
| 95 | * moment to run it. |
| 96 | */ |
| 97 | int ret = cmdline_process_param("-P", arg, 1, cfg); |
| 98 | assert(ret == 2); |
| 99 | } else if (!strncmp(q, "telnet:", 7)) { |
| 100 | /* |
| 101 | * If the hostname starts with "telnet:", |
| 102 | * set the protocol to Telnet and process |
| 103 | * the string as a Telnet URL. |
| 104 | */ |
| 105 | char c; |
| 106 | |
| 107 | q += 7; |
| 108 | if (q[0] == '/' && q[1] == '/') |
| 109 | q += 2; |
| 110 | cfg->protocol = PROT_TELNET; |
| 111 | p = q; |
| 112 | while (*p && *p != ':' && *p != '/') |
| 113 | p++; |
| 114 | c = *p; |
| 115 | if (*p) |
| 116 | *p++ = '\0'; |
| 117 | if (c == ':') |
| 118 | cfg->port = atoi(p); |
| 119 | else |
| 120 | cfg->port = -1; |
| 121 | strncpy(cfg->host, q, sizeof(cfg->host) - 1); |
| 122 | cfg->host[sizeof(cfg->host) - 1] = '\0'; |
| 123 | got_host = 1; |
| 124 | } else { |
| 125 | /* |
| 126 | * Otherwise, treat this argument as a host name. |
| 127 | */ |
| 128 | p = arg; |
| 129 | while (*p && !isspace((unsigned char)*p)) |
| 130 | p++; |
| 131 | if (*p) |
| 132 | *p++ = '\0'; |
| 133 | strncpy(cfg->host, q, sizeof(cfg->host) - 1); |
| 134 | cfg->host[sizeof(cfg->host) - 1] = '\0'; |
| 135 | got_host = 1; |
| 136 | } |
| 137 | return 1; |
| 138 | } |
| 139 | |
| 140 | char *make_default_wintitle(char *hostname) |
| 141 | { |
| 142 | return dupcat(hostname, " - PuTTY", NULL); |
| 143 | } |
| 144 | |
| 145 | int main(int argc, char **argv) |
| 146 | { |
| 147 | extern int pt_main(int argc, char **argv); |
| 148 | sk_init(); |
| 149 | flags = FLAG_VERBOSE | FLAG_INTERACTIVE; |
| 150 | default_protocol = be_default_protocol; |
| 151 | /* Find the appropriate default port. */ |
| 152 | { |
| 153 | int i; |
| 154 | default_port = 0; /* illegal */ |
| 155 | for (i = 0; backends[i].backend != NULL; i++) |
| 156 | if (backends[i].protocol == default_protocol) { |
| 157 | default_port = backends[i].backend->default_port; |
| 158 | break; |
| 159 | } |
| 160 | } |
| 161 | return pt_main(argc, argv); |
| 162 | } |