File: ui_connect_dialog.c

package info (click to toggle)
mlterm 3.9.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 25,340 kB
  • sloc: ansic: 154,713; sh: 5,302; cpp: 2,953; objc: 2,776; java: 2,472; makefile: 2,445; perl: 1,674; xml: 44
file content (141 lines) | stat: -rw-r--r-- 3,654 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* -*- c-basic-offset:2; tab-width:2; indent-tabs-mode:nil -*- */

/* Note that protocols except ssh aren't supported if USE_LIBSSH2 is defined. */
#ifdef USE_LIBSSH2

#include <stdio.h>
#include <pobl/bl_str.h>
#include <pobl/bl_mem.h>

#include "../ui_connect_dialog.h"
#include "../ui_screen.h"
#ifdef USE_WAYLAND
#include "ui_display.h"
#define ui_display_receive_next_event(disp) ui_display_receive_next_event_singly(disp)
#endif

/* --- static variables --- */

static int end_input;
static char *password;
static size_t password_len;

/* --- static functions --- */

static void key_pressed(ui_window_t *win, XKeyEvent *event) {
  u_char seq[1];
  KeySym ksym;
  ef_parser_t *parser;

  if (ui_window_get_str(win, seq, 1, &parser, &ksym, event) == 0) {
    return;
  }

  if (ksym == XK_Return) {
    if (!password) {
      password = strdup("");
    }

    end_input = 1;
  } else {
    if (ksym == XK_BackSpace) {
      if (password_len > 0) {
        password[--password_len] = '\0';
      }
    } else if (0x20 <= seq[0] && seq[0] <= 0x7e) {
      void *p;

      if ((p = realloc(password, password_len + 2))) {
        password = p;
        password[password_len++] = seq[0];
        password[password_len] = '\0';
      }
    }
  }
}


/* --- global functions --- */

int ui_connect_dialog(char **uri,      /* Should be free'ed by those who call this. */
                      char **pass,     /* Same as uri. If pass is not input, "" is set. */
                      char **exec_cmd, /* Same as uri. If exec_cmd is not input, NULL is set. */
                      char **privkey,  /* in/out */
                      int *x11_fwd,    /* in/out */
                      char *display_name, Window parent_window,
                      char *def_server /* (<user>@)(<proto>:)<server address>(:<encoding>). */
                      ) {
  ui_screen_t *screen;
  char *prompt;
  size_t prompt_len;
  void (*orig_key_pressed)();

  prompt_len = 12 + strlen(def_server) + 11;
  if (!(prompt = alloca(prompt_len + 1))) {
    return 0;
  }

  sprintf(prompt, " Connect to %s. Password:", def_server);

  if (!(*uri = strdup(def_server))) {
    return 0;
  }

  screen = (ui_screen_t *)parent_window;

  orig_key_pressed = screen->window.key_pressed;
  screen->window.key_pressed = key_pressed;

  ui_window_clear_all(&screen->window);

#ifndef USE_CONSOLE
  ui_window_draw_image_string(&screen->window, ui_get_usascii_font(screen->font_man),
                              ui_get_xcolor(screen->color_man, VT_FG_COLOR),
                              ui_get_xcolor(screen->color_man, VT_BG_COLOR), 0,
                              ui_line_ascent(screen), prompt, prompt_len);
#endif

  do {
#ifdef USE_CONSOLE
    /*
     * prompt is redrawn every time because ui_display_receive_next_event() receives
     * "\x1b[8;%d;%d;4;%d;%dt" in startup.
     */
    ui_window_console_draw_string(&screen->window, ui_get_usascii_font(screen->font_man),
                                  ui_get_xcolor(screen->color_man, VT_FG_COLOR),
                                  ui_get_xcolor(screen->color_man, VT_BG_COLOR), 0,
                                  ui_line_ascent(screen), prompt, prompt_len, 0);
#endif
#ifdef USE_SDL2
    {
      u_int num;
      ui_get_opened_displays(&num);
    }
#else
    ui_display_receive_next_event(screen->window.disp);
#endif
  } while (!end_input);

  end_input = 0;
  screen->window.key_pressed = orig_key_pressed;

  if (!password) {
    free(*uri);

    return 0;
  }

  *pass = password;
  password = NULL;
  password_len = 0;

  *exec_cmd = NULL;

#if 0
  ui_window_update_all(&screen->window);
#endif

  return 1;
}

#endif