~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/get_password.cc

  • Committer: Brian Aker
  • Date: 2009-10-07 16:55:53 UTC
  • mfrom: (1161.2.1 bug444827)
  • Revision ID: brian@gaz-20091007165553-9tnp7liw1k9g6gvc
Merge Padraig

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
** This is an own file to avoid conflicts with curses
23
23
*/
24
24
 
25
 
#include "config.h"
 
25
#include <drizzled/global.h>
26
26
#include "client/get_password.h"
27
27
 
28
 
#include <string>
29
 
 
30
28
#include <string.h>
31
29
#include <stdio.h>
32
30
#include <stdlib.h>
47
45
#  endif
48
46
#endif
49
47
 
50
 
using namespace std;
51
 
 
52
 
bool tty_password= false;
53
 
const std::string PASSWORD_SENTINEL("\0\0\0\0\0", 5);
54
 
 
55
48
/*
56
49
  Can't use fgets, because readline will get confused
57
50
  length is max number of chars in to, not counting \0
73
66
      {
74
67
        if (echo)
75
68
        {
76
 
          fputs("\b \b",stderr);
77
 
          fflush(stderr);
 
69
          fputs("\b \b",stdout);
 
70
          fflush(stdout);
78
71
        }
79
72
        pos--;
80
73
        continue;
84
77
      break;
85
78
    if (iscntrl(tmp) || pos == end)
86
79
      continue;
 
80
    if (echo)
 
81
    {
 
82
      fputc('*',stdout);
 
83
      fflush(stdout);
 
84
    }
87
85
    *(pos++) = tmp;
88
86
  }
89
87
  while (pos != to && isspace(pos[-1]) == ' ')
98
96
  TERMIO org,tmp;
99
97
  char buff[80];
100
98
 
101
 
  if (isatty(fileno(stderr)))
 
99
  if (isatty(fileno(stdout)))
102
100
  {
103
 
    fputs(opt_message ? opt_message : "Enter password: ",stderr);
104
 
    fflush(stderr);
 
101
    fputs(opt_message ? opt_message : "Enter password: ",stdout);
 
102
    fflush(stdout);
105
103
  }
106
104
#  if defined(HAVE_TERMIOS_H)
107
105
  tcgetattr(fileno(stdin), &org);
110
108
  tmp.c_cc[VMIN] = 1;
111
109
  tmp.c_cc[VTIME] = 0;
112
110
  tcsetattr(fileno(stdin), TCSADRAIN, &tmp);
113
 
  get_password(buff, sizeof(buff)-1, fileno(stdin), isatty(fileno(stderr)));
 
111
  get_password(buff, sizeof(buff)-1, fileno(stdin), isatty(fileno(stdout)));
114
112
  tcsetattr(fileno(stdin), TCSADRAIN, &org);
115
113
#  elif defined(HAVE_TERMIO_H)
116
114
  ioctl(fileno(stdin), (int) TCGETA, &org);
119
117
  tmp.c_cc[VMIN] = 1;
120
118
  tmp.c_cc[VTIME]= 0;
121
119
  ioctl(fileno(stdin),(int) TCSETA, &tmp);
122
 
  get_password(buff,sizeof(buff)-1,fileno(stdin),isatty(fileno(stderr)));
 
120
  get_password(buff,sizeof(buff)-1,fileno(stdin),isatty(fileno(stdout)));
123
121
  ioctl(fileno(stdin),(int) TCSETA, &org);
124
122
#  else
125
123
  gtty(fileno(stdin), &org);
127
125
  tmp.sg_flags &= ~ECHO;
128
126
  tmp.sg_flags |= RAW;
129
127
  stty(fileno(stdin), &tmp);
130
 
  get_password(buff,sizeof(buff)-1,fileno(stdin),isatty(fileno(stderr)));
 
128
  get_password(buff,sizeof(buff)-1,fileno(stdin),isatty(fileno(stdout)));
131
129
  stty(fileno(stdin), &org);
132
130
#  endif
133
 
  if (isatty(fileno(stderr)))
134
 
    fputc('\n',stderr);
 
131
  if (isatty(fileno(stdout)))
 
132
    fputc('\n',stdout);
135
133
 
136
134
  return strdup(buff);
137
135
}
138
 
 
139
 
pair<string, string> parse_password_arg(string s)
140
 
{
141
 
  if (s.find("--password") == 0)
142
 
  {
143
 
    if (s == "--password")
144
 
    {
145
 
      tty_password= true;
146
 
      //check if no argument is passed.
147
 
      return make_pair("password", PASSWORD_SENTINEL);
148
 
    }
149
 
 
150
 
    if (s.substr(10,3) == "=\"\"" || s.substr(10,3) == "=''")
151
 
    {
152
 
      // Check if --password="" or --password=''
153
 
      return make_pair("password", PASSWORD_SENTINEL);
154
 
    }
155
 
    
156
 
    if(s.substr(10) == "=" && s.length() == 11)
157
 
    {
158
 
      // check if --password= and return a default value
159
 
      return make_pair("password", PASSWORD_SENTINEL);
160
 
    }
161
 
  }
162
 
 
163
 
  return make_pair(string(""), string(""));
164
 
}
165