From 1d0f70c2f894b27e98773a282871d32802f67964 Mon Sep 17 00:00:00 2001 From: Michael Widenius Date: Wed, 1 Aug 2012 17:27:34 +0300 Subject: Temporary commit of merge of MariaDB 10.0-base and MySQL 5.6 --- scripts/comp_sql.c | 165 ++++++++++++++++++++++++++--------------------------- 1 file changed, 82 insertions(+), 83 deletions(-) (limited to 'scripts/comp_sql.c') diff --git a/scripts/comp_sql.c b/scripts/comp_sql.c index e067d0757bf..f1b327b24ac 100644 --- a/scripts/comp_sql.c +++ b/scripts/comp_sql.c @@ -1,4 +1,5 @@ -/* Copyright (C) 2004 MySQL AB +/* Copyright (c) 2004, 2010, Oracle and/or its affiliates. + Copyright (c) 2012 Monty Program Ab This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -11,10 +12,10 @@ You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ + 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */ /* - Written by Magnus Svensson + Originally written by Magnus Svensson */ /* @@ -25,10 +26,18 @@ #include #include #include -#include -/* Compiler-dependent constant for maximum string constant */ -#define MAX_STRING_CONSTANT_LENGTH 65535 +#include "../sql/sql_bootstrap.h" + +/* + This is an internal tool used during the build process only, + - do not make a library just for this, + which would make the Makefiles and the server link + more complex than necessary, + - do not duplicate the code either. + so just add the sql_bootstrap.cc code as is. +*/ +#include "../sql/sql_bootstrap.cc" FILE *in, *out; @@ -58,15 +67,60 @@ static void die(const char *fmt, ...) exit(1); } +char *fgets_fn(char *buffer, size_t size, fgets_input_t input) +{ + return fgets(buffer, size, (FILE*) input); +} + +static void print_query(FILE *out, const char *query) +{ + const char *ptr= query; + int column= 0; + + fprintf(out, "\""); + while (*ptr) + { + if (column >= 120) + { + /* Wrap to the next line, tabulated. */ + fprintf(out, "\"\n \""); + column= 2; + } + switch(*ptr) + { + case '\n': + /* + Preserve the \n character in the query text, + and wrap to the next line, tabulated. + */ + fprintf(out, "\\n\"\n \""); + column= 2; + break; + case '\r': + /* Skipped */ + break; + case '\"': + fprintf(out, "\\\""); + column++; + break; + default: + putc(*ptr, out); + column++; + break; + } + ptr++; + } + fprintf(out, "\\n\",\n"); +} int main(int argc, char *argv[]) { - char buff[512]; - struct stat st; + char query[MAX_BOOTSTRAP_QUERY_SIZE]; char* struct_name= argv[1]; char* infile_name= argv[2]; char* outfile_name= argv[3]; - + int rc; + int query_length; if (argc != 4) die("Usage: comp_sql "); @@ -74,86 +128,31 @@ int main(int argc, char *argv[]) /* Open input and output file */ if (!(in= fopen(infile_name, "r"))) die("Failed to open SQL file '%s'", infile_name); - - if (!(out= fopen(outfile_name, "w"))) die("Failed to open output file '%s'", outfile_name); - fprintf(out, "const char %s[]={\n",struct_name); - - /* - Some compilers have limitations how long a string constant can be. - We'll output very long strings as hexadecimal arrays, and short ones - as strings (prettier) - */ - stat(infile_name, &st); - if (st.st_size > MAX_STRING_CONSTANT_LENGTH) - { - int cnt=0; - int c; - for(cnt=0;;cnt++) - { - c= fgetc(in); - if (c== -1) - break; - if(cnt != 0) - fputc(',', out); + fprintf(out, "/*\n"); + fprintf(out, " Do not edit this file, it is automatically generated from:\n"); + fprintf(out, " <%s>\n", infile_name); + fprintf(out, "*/\n"); + fprintf(out, "const char* %s[]={\n", struct_name); - /* Put line break after each 16 hex characters */ - if(cnt && (cnt%16 == 0)) - fputc('\n', out); - - fprintf(out,"0x%02x",c); - } - fprintf(out,",0x00"); - } - else + for ( ; ; ) { - fprintf(out,"\""); - while (fgets(buff, sizeof(buff), in)) - { - char *curr= buff; - while (*curr) - { - if (*curr == '\n') - { - /* - Reached end of line, add escaped newline, escaped - backslash and a newline to outfile - */ - fprintf(out, "\\n \"\n\""); - curr++; - } - else if (*curr == '\r') - { - curr++; /* Skip */ - } - else - { - if (*curr == '"') - { - /* Needs escape */ - fputc('\\', out); - } - - fputc(*curr, out); - curr++; - } - } - if (*(curr-1) != '\n') - { - /* - Some compilers have a max string length, - insert a newline at every 512th char in long - strings - */ - fprintf(out, "\"\n\""); - } - } - fprintf(out, "\\\n\""); + rc= read_bootstrap_query(query, &query_length, + (fgets_input_t) in, fgets_fn); + + if (rc == READ_BOOTSTRAP_ERROR) + die("Failed to read the bootstrap input file.\n"); + + if (rc == READ_BOOTSTRAP_EOF) + break; + + print_query(out, query); } - - fprintf(out, "};\n"); + + fprintf(out, "NULL\n};\n"); + fclose(in); fclose(out); -- cgit v1.2.1