MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
comp_sql.c
1 /* Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
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 as published by
5  the Free Software Foundation; version 2 of the License.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program; if not, write to the Free Software Foundation,
14  51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
15 
16 /*
17  Converts a SQL file into a C file that can be compiled and linked
18  into other programs
19 */
20 
21 #include <stdarg.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 
25 #include "../sql/sql_bootstrap.h"
26 
27 /*
28  This is an internal tool used during the build process only,
29  - do not make a library just for this,
30  which would make the Makefiles and the server link
31  more complex than necessary,
32  - do not duplicate the code either.
33  so just add the sql_bootstrap.cc code as is.
34 */
35 #include "../sql/sql_bootstrap.cc"
36 
37 FILE *in;
38 FILE *out;
39 
40 static void die(const char *fmt, ...)
41 {
42  va_list args;
43 
44  /* Print the error message */
45  fprintf(stderr, "FATAL ERROR: ");
46  if (fmt)
47  {
48  va_start(args, fmt);
49  vfprintf(stderr, fmt, args);
50  va_end(args);
51  }
52  else
53  fprintf(stderr, "unknown error");
54  fprintf(stderr, "\n");
55  fflush(stderr);
56 
57  /* Close any open files */
58  if (in)
59  fclose(in);
60  if (out)
61  fclose(out);
62 
63  exit(1);
64 }
65 
66 char *fgets_fn(char *buffer, size_t size, fgets_input_t input, int *error)
67 {
68  char *line= fgets(buffer, size, (FILE*) input);
69  if (error)
70  *error= (line == NULL) ? ferror((FILE*)input) : 0;
71  return line;
72 }
73 
74 static void print_query(FILE *out, const char *query)
75 {
76  const char *ptr= query;
77  int column= 0;
78 
79  fprintf(out, "\"");
80  while (*ptr)
81  {
82  if (column >= 120)
83  {
84  /* Wrap to the next line, tabulated. */
85  fprintf(out, "\"\n \"");
86  column= 2;
87  }
88  switch(*ptr)
89  {
90  case '\n':
91  /*
92  Preserve the \n character in the query text,
93  and wrap to the next line, tabulated.
94  */
95  fprintf(out, "\\n\"\n \"");
96  column= 2;
97  break;
98  case '\r':
99  /* Skipped */
100  break;
101  case '\"':
102  fprintf(out, "\\\"");
103  column++;
104  break;
105  default:
106  putc(*ptr, out);
107  column++;
108  break;
109  }
110  ptr++;
111  }
112  fprintf(out, "\\n\",\n");
113 }
114 
115 int main(int argc, char *argv[])
116 {
117  char query[MAX_BOOTSTRAP_QUERY_SIZE];
118  char* struct_name= argv[1];
119  char* infile_name= argv[2];
120  char* outfile_name= argv[3];
121  int rc;
122  int query_length= 0;
123  int error= 0;
124  char *err_ptr;
125 
126  if (argc != 4)
127  die("Usage: comp_sql <struct_name> <sql_filename> <c_filename>");
128 
129  /* Open input and output file */
130  if (!(in= fopen(infile_name, "r")))
131  die("Failed to open SQL file '%s'", infile_name);
132  if (!(out= fopen(outfile_name, "w")))
133  die("Failed to open output file '%s'", outfile_name);
134 
135  fprintf(out, "/*\n");
136  fprintf(out, " Do not edit this file, it is automatically generated from:\n");
137  fprintf(out, " <%s>\n", infile_name);
138  fprintf(out, "*/\n");
139  fprintf(out, "const char* %s[]={\n", struct_name);
140 
141  for ( ; ; )
142  {
143  rc= read_bootstrap_query(query, &query_length,
144  (fgets_input_t) in, fgets_fn, &error);
145 
146  if (rc == READ_BOOTSTRAP_EOF)
147  break;
148 
149  if (rc != READ_BOOTSTRAP_SUCCESS)
150  {
151  /* Get the most recent query text for reference. */
152  err_ptr= query + (query_length <= MAX_BOOTSTRAP_ERROR_LEN ?
153  0 : (query_length - MAX_BOOTSTRAP_ERROR_LEN));
154  switch (rc)
155  {
156  case READ_BOOTSTRAP_ERROR:
157  die("Failed to read the bootstrap input file. Return code (%d).\n"
158  "Last query: '%s'\n", error, err_ptr);
159  break;
160 
161  case READ_BOOTSTRAP_QUERY_SIZE:
162  die("Failed to read the boostrap input file. Query size exceeded %d bytes.\n"
163  "Last query: '%s'.\n", MAX_BOOTSTRAP_LINE_SIZE, err_ptr);
164  break;
165 
166  default:
167  die("Failed to read the boostrap input file. Unknown error.\n");
168  break;
169  }
170  }
171 
172  print_query(out, query);
173  }
174 
175  fprintf(out, "NULL\n};\n");
176 
177  fclose(in);
178  fclose(out);
179 
180  exit(0);
181 }
182