2010-05-10 11:44:09 -07:00
|
|
|
/*
|
|
|
|
* Copyright © 2010 Intel Corporation
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice (including the next
|
|
|
|
* paragraph) shall be included in all copies or substantial portions of the
|
|
|
|
* Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2010-06-23 14:00:27 -07:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
2010-08-23 09:42:14 -07:00
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2010-05-10 13:17:25 -07:00
|
|
|
#include "glcpp.h"
|
2010-09-05 01:27:02 -07:00
|
|
|
#include "main/mtypes.h"
|
2010-10-14 13:28:42 -07:00
|
|
|
#include "main/shaderobj.h"
|
2010-05-10 13:17:25 -07:00
|
|
|
|
2010-05-12 12:17:10 -07:00
|
|
|
extern int yydebug;
|
|
|
|
|
2010-10-14 13:28:42 -07:00
|
|
|
void
|
|
|
|
_mesa_reference_shader(struct gl_context *ctx, struct gl_shader **ptr,
|
|
|
|
struct gl_shader *sh)
|
|
|
|
{
|
|
|
|
*ptr = sh;
|
|
|
|
}
|
|
|
|
|
2010-08-23 10:43:27 -07:00
|
|
|
/* Read from fd until EOF and return a string of everything read.
|
|
|
|
*/
|
2010-06-23 14:00:27 -07:00
|
|
|
static char *
|
2010-08-23 10:43:27 -07:00
|
|
|
load_text_fd (void *ctx, int fd)
|
2010-05-10 11:44:09 -07:00
|
|
|
{
|
2010-08-23 10:43:27 -07:00
|
|
|
#define CHUNK 4096
|
2010-06-23 14:00:27 -07:00
|
|
|
char *text = NULL;
|
2010-08-23 10:43:27 -07:00
|
|
|
ssize_t text_size = 0;
|
2010-06-23 14:00:27 -07:00
|
|
|
ssize_t total_read = 0;
|
2010-08-23 10:43:27 -07:00
|
|
|
ssize_t bytes;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
if (total_read + CHUNK + 1 > text_size) {
|
|
|
|
text_size = text_size ? text_size * 2 : CHUNK + 1;
|
2011-01-21 14:32:31 -08:00
|
|
|
text = reralloc_size (ctx, text, text_size);
|
2010-08-23 10:43:27 -07:00
|
|
|
if (text == NULL) {
|
|
|
|
fprintf (stderr, "Out of memory\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bytes = read (fd, text + total_read, CHUNK);
|
|
|
|
if (bytes < 0) {
|
|
|
|
fprintf (stderr, "Error while reading: %s\n",
|
|
|
|
strerror (errno));
|
2011-01-21 14:32:31 -08:00
|
|
|
ralloc_free (text);
|
2010-08-23 10:43:27 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
2010-08-23 09:42:14 -07:00
|
|
|
|
2010-08-23 10:43:27 -07:00
|
|
|
if (bytes == 0) {
|
|
|
|
break;
|
|
|
|
}
|
2010-08-23 09:42:14 -07:00
|
|
|
|
2010-08-23 10:43:27 -07:00
|
|
|
total_read += bytes;
|
2010-06-23 14:00:27 -07:00
|
|
|
}
|
|
|
|
|
2010-08-23 10:43:27 -07:00
|
|
|
text[total_read] = '\0';
|
|
|
|
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
load_text_file(void *ctx, const char *filename)
|
|
|
|
{
|
|
|
|
char *text;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
if (filename == NULL || strcmp (filename, "-") == 0)
|
|
|
|
return load_text_fd (ctx, STDIN_FILENO);
|
|
|
|
|
|
|
|
fd = open (filename, O_RDONLY);
|
|
|
|
if (fd < 0) {
|
|
|
|
fprintf (stderr, "Failed to open file %s: %s\n",
|
|
|
|
filename, strerror (errno));
|
|
|
|
return NULL;
|
2010-06-23 14:00:27 -07:00
|
|
|
}
|
2010-05-10 11:52:29 -07:00
|
|
|
|
2010-08-23 10:43:27 -07:00
|
|
|
text = load_text_fd (ctx, fd);
|
|
|
|
|
2010-06-23 14:00:27 -07:00
|
|
|
close(fd);
|
2010-05-10 16:16:06 -07:00
|
|
|
|
2010-06-23 14:00:27 -07:00
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2010-08-17 23:13:56 -07:00
|
|
|
main (int argc, char *argv[])
|
2010-06-23 14:00:27 -07:00
|
|
|
{
|
2010-08-17 23:13:56 -07:00
|
|
|
char *filename = NULL;
|
2011-01-21 14:32:31 -08:00
|
|
|
void *ctx = ralloc(NULL, void*);
|
|
|
|
char *info_log = ralloc_strdup(ctx, "");
|
2010-08-17 23:13:56 -07:00
|
|
|
const char *shader;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (argc) {
|
|
|
|
filename = argv[1];
|
|
|
|
}
|
|
|
|
|
2010-08-23 10:43:27 -07:00
|
|
|
shader = load_text_file (ctx, filename);
|
2010-08-23 09:42:14 -07:00
|
|
|
if (shader == NULL)
|
|
|
|
return 1;
|
|
|
|
|
2010-09-05 01:27:02 -07:00
|
|
|
ret = preprocess(ctx, &shader, &info_log, NULL, API_OPENGL);
|
2010-05-10 16:16:06 -07:00
|
|
|
|
2010-06-23 14:00:27 -07:00
|
|
|
printf("%s", shader);
|
|
|
|
fprintf(stderr, "%s", info_log);
|
2010-06-16 11:57:48 -07:00
|
|
|
|
2011-01-21 14:32:31 -08:00
|
|
|
ralloc_free(ctx);
|
2010-05-10 11:52:29 -07:00
|
|
|
|
|
|
|
return ret;
|
2010-05-10 11:44:09 -07:00
|
|
|
}
|