1- /* Copyright (C ) 2004 MySQL AB
1+ /* Copyright (c ) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
22
33 This program is free software; you can redistribute it and/or modify
44 it under the terms of the GNU General Public License as published by
1313 along with this program; if not, write to the Free Software
1414 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
1515
16- /*
17- Written by Magnus Svensson
18- */
19-
2016/*
2117 Converts a SQL file into a C file that can be compiled and linked
2218 into other programs
2622#include <stdlib.h>
2723#include <stdio.h>
2824
29- FILE * in , * out ;
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 ;
3039
3140static void die (const char * fmt , ...)
3241{
@@ -54,13 +63,60 @@ static void die(const char *fmt, ...)
5463 exit (1 );
5564}
5665
66+ char * fgets_fn (char * buffer , size_t size , fgets_input_t input )
67+ {
68+ return fgets (buffer , size , (FILE * ) input );
69+ }
70+
71+ static void print_query (FILE * out , const char * query )
72+ {
73+ const char * ptr = query ;
74+ int column = 0 ;
75+
76+ fprintf (out , "\"" );
77+ while (* ptr )
78+ {
79+ if (column >= 120 )
80+ {
81+ /* Wrap to the next line, tabulated. */
82+ fprintf (out , "\"\n \"" );
83+ column = 2 ;
84+ }
85+ switch (* ptr )
86+ {
87+ case '\n' :
88+ /*
89+ Preserve the \n character in the query text,
90+ and wrap to the next line, tabulated.
91+ */
92+ fprintf (out , "\\n\"\n \"" );
93+ column = 2 ;
94+ break ;
95+ case '\r' :
96+ /* Skipped */
97+ break ;
98+ case '\"' :
99+ fprintf (out , "\\\"" );
100+ column ++ ;
101+ break ;
102+ default :
103+ putc (* ptr , out );
104+ column ++ ;
105+ break ;
106+ }
107+ ptr ++ ;
108+ }
109+ fprintf (out , "\\n\",\n" );
110+ }
57111
58112int main (int argc , char * argv [])
59113{
60- char buff [ 512 ];
114+ char query [ MAX_BOOTSTRAP_QUERY_SIZE ];
61115 char * struct_name = argv [1 ];
62116 char * infile_name = argv [2 ];
63117 char * outfile_name = argv [3 ];
118+ int rc ;
119+ int query_length ;
64120
65121 if (argc != 4 )
66122 die ("Usage: comp_sql <struct_name> <sql_filename> <c_filename>" );
@@ -71,55 +127,31 @@ int main(int argc, char *argv[])
71127 if (!(out = fopen (outfile_name , "w" )))
72128 die ("Failed to open output file '%s'" , outfile_name );
73129
74- fprintf (out , "const char* %s={\n\"" , struct_name );
130+ fprintf (out , "/*\n" );
131+ fprintf (out , " Do not edit this file, it is automatically generated from:\n" );
132+ fprintf (out , " <%s>\n" , infile_name );
133+ fprintf (out , "*/\n" );
134+ fprintf (out , "const char* %s[]={\n" , struct_name );
75135
76- while ( fgets ( buff , sizeof ( buff ), in ) )
136+ for ( ; ; )
77137 {
78- char * curr = buff ;
79- while (* curr )
80- {
81- if (* curr == '\n' )
82- {
83- /*
84- Reached end of line, add escaped newline, escaped
85- backslash and a newline to outfile
86- */
87- fprintf (out , "\\n \"\n\"" );
88- curr ++ ;
89- }
90- else if (* curr == '\r' )
91- {
92- curr ++ ; /* Skip */
93- }
94- else
95- {
96- if (* curr == '"' )
97- {
98- /* Needs escape */
99- fputc ('\\' , out );
100- }
101-
102- fputc (* curr , out );
103- curr ++ ;
104- }
105- }
106- if (* (curr - 1 ) != '\n' )
107- {
108- /*
109- Some compilers have a max string length,
110- insert a newline at every 512th char in long
111- strings
112- */
113- fprintf (out , "\"\n\"" );
114- }
138+ rc = read_bootstrap_query (query , & query_length ,
139+ (fgets_input_t ) in , fgets_fn );
140+
141+ if (rc == READ_BOOTSTRAP_ERROR )
142+ die ("Failed to read the bootstrap input file.\n" );
143+
144+ if (rc == READ_BOOTSTRAP_EOF )
145+ break ;
146+
147+ print_query (out , query );
115148 }
116149
117- fprintf (out , "\\\n\" };\n" );
150+ fprintf (out , "NULL\n };\n" );
118151
119152 fclose (in );
120153 fclose (out );
121154
122155 exit (0 );
123-
124156}
125157
0 commit comments