1 /* Copyright (c) 2010, 2021, Oracle and/or its affiliates.
2 
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License, version 2.0,
5    as published by the Free Software Foundation.
6 
7    This program is also distributed with certain software (including
8    but not limited to OpenSSL) that is licensed under separate terms,
9    as designated in a particular file or component or in included license
10    documentation.  The authors of MySQL hereby grant you an additional
11    permission to link the program and your derivative works with the
12    separately licensed software that they have included with MySQL.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License, version 2.0, for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software Foundation,
21    51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
22 
23 
24 #include <stdlib.h>
25 #include <errno.h>
26 #include <ctype.h>
27 #include <string.h>
28 #include "sql_bootstrap.h"
29 
read_bootstrap_query(char * query,size_t * query_length,fgets_input_t input,fgets_fn_t fgets_fn,int * error)30 int read_bootstrap_query(char *query, size_t *query_length,
31                          fgets_input_t input, fgets_fn_t fgets_fn, int *error)
32 {
33   char line_buffer[MAX_BOOTSTRAP_LINE_SIZE];
34   const char *line;
35   size_t len;
36   size_t query_len= 0;
37   int fgets_error= 0;
38   *error= 0;
39 
40   for ( ; ; )
41   {
42     line= (*fgets_fn)(line_buffer, sizeof(line_buffer), input, &fgets_error);
43 
44     if (error)
45       *error= fgets_error;
46 
47     if (fgets_error != 0)
48       return READ_BOOTSTRAP_ERROR;
49 
50     if (line == NULL)
51       return (query_len == 0) ? READ_BOOTSTRAP_EOF : READ_BOOTSTRAP_ERROR;
52 
53     len= strlen(line);
54 
55     /*
56       Remove trailing whitespace characters.
57       This assumes:
58       - no multibyte encoded character can be found at the very end of a line,
59       - whitespace characters from the "C" locale only.
60      which is sufficient for the kind of queries found
61      in the bootstrap scripts.
62     */
63     while (len && (isspace(line[len - 1])))
64       len--;
65     /*
66       Cleanly end the string, so we don't have to test len > x
67       all the time before reading line[x], in the code below.
68     */
69     line_buffer[len]= '\0';
70 
71     /* Skip blank lines */
72     if (len == 0)
73       continue;
74 
75     /* Skip # comments */
76     if (line[0] == '#')
77       continue;
78 
79     /* Skip -- comments */
80     if ((line[0] == '-') && (line[1] == '-'))
81       continue;
82 
83     /* Skip delimiter, ignored. */
84     if (strncmp(line, "delimiter", 9) == 0)
85       continue;
86 
87     /* Append the current line to a multi line query. If the new line will make
88        the query too long, preserve the partial line to provide context for the
89        error message.
90     */
91     if (query_len + len + 1 >= MAX_BOOTSTRAP_QUERY_SIZE)
92     {
93       size_t new_len= MAX_BOOTSTRAP_QUERY_SIZE - query_len - 1;
94       if ((new_len > 0) && (query_len < MAX_BOOTSTRAP_QUERY_SIZE))
95       {
96         memcpy(query + query_len, line, new_len);
97         query_len+= new_len;
98       }
99       query[query_len]= '\0';
100       *query_length= query_len;
101       return READ_BOOTSTRAP_QUERY_SIZE;
102     }
103 
104     if (query_len != 0)
105     {
106       /*
107         Append a \n to the current line, if any,
108         to preserve the intended presentation.
109        */
110       query[query_len++]= '\n';
111     }
112     memcpy(query + query_len, line, len);
113     query_len+= len;
114 
115     if (line[len - 1] == ';')
116     {
117       /*
118         The last line is terminated by ';'.
119         Return the query found.
120       */
121       query[query_len]= '\0';
122       *query_length= query_len;
123       return READ_BOOTSTRAP_SUCCESS;
124     }
125   }
126 }
127 
128