~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/readline.cc

mergeĀ mainline

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"
19
 
#include <mysys/my_sys.h>
20
 
#include <mystrings/m_string.h>
 
18
#include <my_global.h>
 
19
#include <my_sys.h>
 
20
#include <m_string.h>
21
21
#include "my_readline.h"
22
22
 
23
 
static bool init_line_buffer(LINE_BUFFER *buffer,File file,uint32_t size,
24
 
                            uint32_t max_size);
 
23
static bool init_line_buffer(LINE_BUFFER *buffer,File file,ulong size,
 
24
                            ulong max_size);
25
25
static bool init_line_buffer_from_string(LINE_BUFFER *buffer,char * str);
26
26
static size_t fill_buffer(LINE_BUFFER *buffer);
27
 
static char *intern_read_line(LINE_BUFFER *buffer,uint32_t *out_length);
28
 
 
29
 
 
30
 
LINE_BUFFER *batch_readline_init(uint32_t max_size,FILE *file)
 
27
static char *intern_read_line(LINE_BUFFER *buffer,ulong *out_length);
 
28
 
 
29
 
 
30
LINE_BUFFER *batch_readline_init(ulong max_size,FILE *file)
31
31
{
32
32
  LINE_BUFFER *line_buff;
33
33
  if (!(line_buff=(LINE_BUFFER*)
35
35
    return 0;
36
36
  if (init_line_buffer(line_buff,fileno(file),IO_SIZE,max_size))
37
37
  {
38
 
    free(line_buff);
 
38
    my_free(line_buff,MYF(0));
39
39
    return 0;
40
40
  }
41
41
  return line_buff;
45
45
char *batch_readline(LINE_BUFFER *line_buff)
46
46
{
47
47
  char *pos;
48
 
  uint32_t out_length;
 
48
  ulong out_length;
49
49
 
50
50
  if (!(pos=intern_read_line(line_buff,&out_length)))
51
51
    return 0;
62
62
{
63
63
  if (line_buff)
64
64
  {
65
 
    free(line_buff->buffer);
66
 
    free(line_buff);
 
65
    my_free(line_buff->buffer,MYF(MY_ALLOW_ZERO_PTR));
 
66
    my_free(line_buff,MYF(0));
67
67
  }
68
68
}
69
69
 
76
76
      return 0;
77
77
  if (init_line_buffer_from_string(line_buff,str))
78
78
  {
79
 
    free(line_buff);
 
79
    my_free(line_buff,MYF(0));
80
80
    return 0;
81
81
  }
82
82
  return line_buff;
88
88
******************************************************************************/
89
89
 
90
90
static bool
91
 
init_line_buffer(LINE_BUFFER *buffer,File file,uint32_t size,uint32_t max_buffer)
 
91
init_line_buffer(LINE_BUFFER *buffer,File file,ulong size,ulong max_buffer)
92
92
{
93
93
  buffer->file=file;
94
94
  buffer->bufread=size;
111
111
  uint old_length=(uint)(buffer->end - buffer->buffer);
112
112
  uint length= (uint) strlen(str);
113
113
  if (!(buffer->buffer= buffer->start_of_line= buffer->end_of_line=
114
 
        (char*) my_realloc((unsigned char*) buffer->buffer, old_length+length+2,
 
114
        (char*) my_realloc((uchar*) buffer->buffer, old_length+length+2,
115
115
                           MYF(MY_FAE|MY_ALLOW_ZERO_PTR))))
116
116
    return 1;
117
117
  buffer->end= buffer->buffer + old_length;
161
161
  /* Shift stuff down. */
162
162
  if (buffer->start_of_line != buffer->buffer)
163
163
  {
164
 
    memcpy(buffer->buffer, buffer->start_of_line, (uint) bufbytes);
 
164
    bmove(buffer->buffer,buffer->start_of_line,(uint) bufbytes);
165
165
    buffer->end=buffer->buffer+bufbytes;
166
166
  }
167
167
 
168
168
  /* Read in new stuff. */
169
 
  if ((read_count= my_read(buffer->file, (unsigned char*) buffer->end, read_count,
 
169
  if ((read_count= my_read(buffer->file, (uchar*) buffer->end, read_count,
170
170
                           MYF(MY_WME))) == MY_FILE_ERROR)
171
171
    return (size_t) -1;
172
172
 
 
173
  DBUG_PRINT("fill_buff", ("Got %lu bytes", (ulong) read_count));
 
174
 
173
175
  /* Kludge to pretend every nonempty file ends with a newline. */
174
176
  if (!read_count && bufbytes && buffer->end[-1] != '\n')
175
177
  {
184
186
 
185
187
 
186
188
 
187
 
char *intern_read_line(LINE_BUFFER *buffer,uint32_t *out_length)
 
189
char *intern_read_line(LINE_BUFFER *buffer,ulong *out_length)
188
190
{
189
191
  char *pos;
190
192
  size_t length;
191
 
 
 
193
  DBUG_ENTER("intern_read_line");
192
194
 
193
195
  buffer->start_of_line=buffer->end_of_line;
194
196
  for (;;)
201
203
      if ((uint) (pos - buffer->start_of_line) < buffer->max_size)
202
204
      {
203
205
        if (!(length=fill_buffer(buffer)) || length == (size_t) -1)
204
 
          return(0);
 
206
          DBUG_RETURN(0);
205
207
        continue;
206
208
      }
207
209
      pos--;                                    /* break line here */
208
210
    }
209
211
    buffer->end_of_line=pos+1;
210
 
    *out_length=(uint32_t) (pos + 1 - buffer->eof - buffer->start_of_line);
211
 
    return(buffer->start_of_line);
 
212
    *out_length=(ulong) (pos + 1 - buffer->eof - buffer->start_of_line);
 
213
    DBUG_RETURN(buffer->start_of_line);
212
214
  }
213
215
}