| 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 | * - Copy-and-paste from the Event Log. |
| 18 | * |
| 19 | * - Remainder of the context menu: |
| 20 | * |
| 21 | * - New Session and Duplicate Session (perhaps in pterm, in fact?!) |
| 22 | * + Duplicate Session will be fun, since we must work out |
| 23 | * how to pass the config data through. |
| 24 | * + In fact this should be easier on Unix, since fork() is |
| 25 | * available so we need not even exec (this also saves us |
| 26 | * the trouble of scrabbling around trying to find our own |
| 27 | * binary). Possible scenario: respond to Duplicate |
| 28 | * Session by forking. Parent continues as before; child |
| 29 | * unceremoniously frees all extant resources (backend, |
| 30 | * terminal, ldisc, frontend etc) and then _longjmps_ (I |
| 31 | * kid you not) back to a point in pt_main() which causes |
| 32 | * it to go back round to the point of opening a new |
| 33 | * terminal window and a new backend. |
| 34 | * + A tricky bit here is how to free everything without |
| 35 | * also _destroying_ things - calling GTK to free up |
| 36 | * existing widgets is liable to send destroy messages to |
| 37 | * the X server, which won't go down too well with the |
| 38 | * parent process. exec() is a much cleaner solution to |
| 39 | * this bit, but requires us to invent some ghastly IPC as |
| 40 | * we did in Windows PuTTY. |
| 41 | * + Arrgh! Also, this won't work in pterm since we'll |
| 42 | * already have dropped privileges by this point, so we |
| 43 | * can't get another pty. Sigh. Looks like exec has to be |
| 44 | * the way forward then :-/ |
| 45 | * |
| 46 | * - Saved Sessions submenu (not in pterm of course) |
| 47 | * |
| 48 | * - Change Settings |
| 49 | * + we must also implement mid-session reconfig in pterm.c. |
| 50 | * + This will require some work. We have to throw the new |
| 51 | * config at the log module, the ldisc, the terminal, and |
| 52 | * the backend; that's the easy bit. But within pterm.c |
| 53 | * itself we must also: |
| 54 | * - redo the colour palette if necessary |
| 55 | * * might be nice to move this over into terminal.c. |
| 56 | * That way we could check which palette entries in |
| 57 | * cfg have actually been _changed_ during |
| 58 | * reconfiguration, and only update those ones in |
| 59 | * the currently visible palette. Also it'd save |
| 60 | * some of this hassle in the next port. |
| 61 | * - enable/disable/move the scroll bar if necessary |
| 62 | * - change the window title if necessary |
| 63 | * - reinitialise the fonts |
| 64 | * - resize the window if necessary (may be required |
| 65 | * either by terminal size change or font size change |
| 66 | * or both) |
| 67 | * - redraw everything, just to be safe. |
| 68 | * + In particular, among the above chaos, we must look into |
| 69 | * how the choice of font affects the choice of codepage |
| 70 | * since the Unix default is to derive the latter from the |
| 71 | * former. |
| 72 | * |
| 73 | * - Copy All to Clipboard (for what that's worth) |
| 74 | */ |
| 75 | |
| 76 | /* |
| 77 | * Clean up and exit. |
| 78 | */ |
| 79 | void cleanup_exit(int code) |
| 80 | { |
| 81 | /* |
| 82 | * Clean up. |
| 83 | */ |
| 84 | sk_cleanup(); |
| 85 | random_save_seed(); |
| 86 | exit(code); |
| 87 | } |
| 88 | |
| 89 | Backend *select_backend(Config *cfg) |
| 90 | { |
| 91 | int i; |
| 92 | Backend *back = NULL; |
| 93 | for (i = 0; backends[i].backend != NULL; i++) |
| 94 | if (backends[i].protocol == cfg->protocol) { |
| 95 | back = backends[i].backend; |
| 96 | break; |
| 97 | } |
| 98 | assert(back != NULL); |
| 99 | return back; |
| 100 | } |
| 101 | |
| 102 | int cfgbox(Config *cfg) |
| 103 | { |
| 104 | extern int do_config_box(const char *title, Config *cfg); |
| 105 | return do_config_box("PuTTY Configuration", cfg); |
| 106 | } |
| 107 | |
| 108 | static int got_host = 0; |
| 109 | |
| 110 | const int use_event_log = 1; |
| 111 | |
| 112 | int process_nonoption_arg(char *arg, Config *cfg) |
| 113 | { |
| 114 | char *p, *q = arg; |
| 115 | |
| 116 | if (got_host) { |
| 117 | /* |
| 118 | * If we already have a host name, treat this argument as a |
| 119 | * port number. NB we have to treat this as a saved -P |
| 120 | * argument, so that it will be deferred until it's a good |
| 121 | * moment to run it. |
| 122 | */ |
| 123 | int ret = cmdline_process_param("-P", arg, 1, cfg); |
| 124 | assert(ret == 2); |
| 125 | } else if (!strncmp(q, "telnet:", 7)) { |
| 126 | /* |
| 127 | * If the hostname starts with "telnet:", |
| 128 | * set the protocol to Telnet and process |
| 129 | * the string as a Telnet URL. |
| 130 | */ |
| 131 | char c; |
| 132 | |
| 133 | q += 7; |
| 134 | if (q[0] == '/' && q[1] == '/') |
| 135 | q += 2; |
| 136 | cfg->protocol = PROT_TELNET; |
| 137 | p = q; |
| 138 | while (*p && *p != ':' && *p != '/') |
| 139 | p++; |
| 140 | c = *p; |
| 141 | if (*p) |
| 142 | *p++ = '\0'; |
| 143 | if (c == ':') |
| 144 | cfg->port = atoi(p); |
| 145 | else |
| 146 | cfg->port = -1; |
| 147 | strncpy(cfg->host, q, sizeof(cfg->host) - 1); |
| 148 | cfg->host[sizeof(cfg->host) - 1] = '\0'; |
| 149 | got_host = 1; |
| 150 | } else { |
| 151 | /* |
| 152 | * Otherwise, treat this argument as a host name. |
| 153 | */ |
| 154 | p = arg; |
| 155 | while (*p && !isspace((unsigned char)*p)) |
| 156 | p++; |
| 157 | if (*p) |
| 158 | *p++ = '\0'; |
| 159 | strncpy(cfg->host, q, sizeof(cfg->host) - 1); |
| 160 | cfg->host[sizeof(cfg->host) - 1] = '\0'; |
| 161 | got_host = 1; |
| 162 | } |
| 163 | return 1; |
| 164 | } |
| 165 | |
| 166 | char *make_default_wintitle(char *hostname) |
| 167 | { |
| 168 | return dupcat(hostname, " - PuTTY", NULL); |
| 169 | } |
| 170 | |
| 171 | int main(int argc, char **argv) |
| 172 | { |
| 173 | extern int pt_main(int argc, char **argv); |
| 174 | sk_init(); |
| 175 | flags = FLAG_VERBOSE | FLAG_INTERACTIVE; |
| 176 | default_protocol = be_default_protocol; |
| 177 | /* Find the appropriate default port. */ |
| 178 | { |
| 179 | int i; |
| 180 | default_port = 0; /* illegal */ |
| 181 | for (i = 0; backends[i].backend != NULL; i++) |
| 182 | if (backends[i].protocol == default_protocol) { |
| 183 | default_port = backends[i].backend->default_port; |
| 184 | break; |
| 185 | } |
| 186 | } |
| 187 | return pt_main(argc, argv); |
| 188 | } |