~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/readline.cc

Moved the last of the libdrizzleclient calls into Protocol.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
 
16
16
/* readline for batch mode */
17
17
 
18
 
#include "client_priv.h"
 
18
#include <drizzled/global.h>
19
19
#include <mysys/my_sys.h>
20
20
#include <mystrings/m_string.h>
21
21
#include "my_readline.h"
22
22
 
 
23
using namespace std;
 
24
 
23
25
static bool init_line_buffer(LINE_BUFFER *buffer,File file,uint32_t size,
24
26
                            uint32_t max_size);
25
27
static bool init_line_buffer_from_string(LINE_BUFFER *buffer,char * str);
30
32
LINE_BUFFER *batch_readline_init(uint32_t max_size,FILE *file)
31
33
{
32
34
  LINE_BUFFER *line_buff;
33
 
  if (!(line_buff=(LINE_BUFFER*)
34
 
        my_malloc(sizeof(*line_buff),MYF(MY_WME | MY_ZEROFILL))))
 
35
  if (!(line_buff=(LINE_BUFFER*) malloc(sizeof(*line_buff))))
35
36
    return 0;
 
37
  memset(line_buff, 0, sizeof(*line_buff));
36
38
  if (init_line_buffer(line_buff,fileno(file),IO_SIZE,max_size))
37
39
  {
38
40
    free(line_buff);
71
73
LINE_BUFFER *batch_readline_command(LINE_BUFFER *line_buff, char * str)
72
74
{
73
75
  if (!line_buff)
74
 
    if (!(line_buff=(LINE_BUFFER*)
75
 
          my_malloc(sizeof(*line_buff),MYF(MY_WME | MY_ZEROFILL))))
 
76
  {
 
77
    if (!(line_buff=(LINE_BUFFER*) malloc(sizeof(*line_buff))))
76
78
      return 0;
 
79
    memset(line_buff, 0, sizeof(*line_buff));
 
80
  }
77
81
  if (init_line_buffer_from_string(line_buff,str))
78
82
  {
79
83
    free(line_buff);
93
97
  buffer->file=file;
94
98
  buffer->bufread=size;
95
99
  buffer->max_size=max_buffer;
96
 
  if (!(buffer->buffer = (char*) my_malloc(buffer->bufread+1,
97
 
                                           MYF(MY_WME | MY_FAE))))
 
100
  if (!(buffer->buffer = (char*) malloc(buffer->bufread+1)))
98
101
    return 1;
99
102
  buffer->end_of_line=buffer->end=buffer->buffer;
100
103
  buffer->buffer[0]=0;                          /* For easy start test */
108
111
*/
109
112
static bool init_line_buffer_from_string(LINE_BUFFER *buffer,char * str)
110
113
{
111
 
  uint old_length=(uint)(buffer->end - buffer->buffer);
112
 
  uint length= (uint) strlen(str);
113
 
  if (!(buffer->buffer= buffer->start_of_line= buffer->end_of_line=
114
 
        (char*) my_realloc((unsigned char*) buffer->buffer, old_length+length+2,
115
 
                           MYF(MY_FAE|MY_ALLOW_ZERO_PTR))))
 
114
  uint32_t old_length=(uint32_t)(buffer->end - buffer->buffer);
 
115
  uint32_t length= (uint32_t) strlen(str);
 
116
  char * tmpptr= (char*)realloc(buffer->buffer, old_length+length+2);
 
117
  if (tmpptr == NULL)
116
118
    return 1;
 
119
  
 
120
  buffer->buffer= buffer->start_of_line= buffer->end_of_line= tmpptr;
117
121
  buffer->end= buffer->buffer + old_length;
118
122
  if (old_length)
119
123
    buffer->end[-1]=' ';
136
140
static size_t fill_buffer(LINE_BUFFER *buffer)
137
141
{
138
142
  size_t read_count;
139
 
  uint bufbytes= (uint) (buffer->end - buffer->start_of_line);
 
143
  uint32_t bufbytes= (uint32_t) (buffer->end - buffer->start_of_line);
140
144
 
141
145
  if (buffer->eof)
142
146
    return 0;                                   /* Everything read */
145
149
 
146
150
  for (;;)
147
151
  {
148
 
    uint start_offset=(uint) (buffer->start_of_line - buffer->buffer);
 
152
    uint32_t start_offset=(uint32_t) (buffer->start_of_line - buffer->buffer);
149
153
    read_count=(buffer->bufread - bufbytes)/IO_SIZE;
150
154
    if ((read_count*=IO_SIZE))
151
155
      break;
152
156
    buffer->bufread *= 2;
153
 
    if (!(buffer->buffer = (char*) my_realloc(buffer->buffer,
154
 
                                              buffer->bufread+1,
155
 
                                              MYF(MY_WME | MY_FAE))))
156
 
      return (uint) -1;
 
157
    if (!(buffer->buffer = (char*) realloc(buffer->buffer,
 
158
                                           buffer->bufread+1)))
 
159
      return (uint32_t) -1;
157
160
    buffer->start_of_line=buffer->buffer+start_offset;
158
161
    buffer->end=buffer->buffer+bufbytes;
159
162
  }
161
164
  /* Shift stuff down. */
162
165
  if (buffer->start_of_line != buffer->buffer)
163
166
  {
164
 
    memcpy(buffer->buffer, buffer->start_of_line, (uint) bufbytes);
 
167
    memmove(buffer->buffer, buffer->start_of_line, (uint32_t) bufbytes);
165
168
    buffer->end=buffer->buffer+bufbytes;
166
169
  }
167
170
 
198
201
      pos++;
199
202
    if (pos == buffer->end)
200
203
    {
201
 
      if ((uint) (pos - buffer->start_of_line) < buffer->max_size)
 
204
      if ((uint32_t) (pos - buffer->start_of_line) < buffer->max_size)
202
205
      {
203
206
        if (!(length=fill_buffer(buffer)) || length == (size_t) -1)
204
207
          return(0);