blob: 011058a36acc7e7224853fd622a41dca8e100a56 [file] [log] [blame]
Carl Worth3a37b872010-05-10 11:44:09 -07001/*
2 * Copyright © 2010 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
Kenneth Graunke186e2632010-06-23 14:00:27 -070024#include <sys/types.h>
25#include <sys/stat.h>
26#include <fcntl.h>
27#include <unistd.h>
Carl Worthcf8bb192010-08-23 09:42:14 -070028#include <string.h>
29#include <errno.h>
Carl Wortha1e32bc2010-05-10 13:17:25 -070030#include "glcpp.h"
31
Carl Worth33cc4002010-05-12 12:17:10 -070032extern int yydebug;
33
Kenneth Graunke186e2632010-06-23 14:00:27 -070034static char *
35load_text_file(void *ctx, const char *file_name)
36{
37 char *text = NULL;
38 struct stat st;
39 ssize_t total_read = 0;
Carl Worthcf8bb192010-08-23 09:42:14 -070040 int fd;
Kenneth Graunke186e2632010-06-23 14:00:27 -070041
Carl Worthcf8bb192010-08-23 09:42:14 -070042 if (file_name == NULL || strcmp(file_name, "-") == 0) {
43 fd = STDIN_FILENO;
44 } else {
45 fd = open (file_name, O_RDONLY);
46
47 if (fd < 0) {
48 fprintf (stderr, "Failed to open file %s: %s\n",
49 file_name, strerror (errno));
50 return NULL;
51 }
Kenneth Graunke186e2632010-06-23 14:00:27 -070052 }
53
54 if (fstat(fd, & st) == 0) {
55 text = (char *) talloc_size(ctx, st.st_size + 1);
56 if (text != NULL) {
57 do {
58 ssize_t bytes = read(fd, text + total_read,
59 st.st_size - total_read);
60 if (bytes < 0) {
61 text = NULL;
62 break;
63 }
64
65 if (bytes == 0) {
66 break;
67 }
68
69 total_read += bytes;
70 } while (total_read < st.st_size);
71
72 text[total_read] = '\0';
73 }
74 }
75
76 close(fd);
77
78 return text;
79}
80
81int
Carl Worth97638aa2010-08-17 23:13:56 -070082main (int argc, char *argv[])
Carl Worth3a37b872010-05-10 11:44:09 -070083{
Carl Worth97638aa2010-08-17 23:13:56 -070084 char *filename = NULL;
Kenneth Graunke186e2632010-06-23 14:00:27 -070085 void *ctx = talloc(NULL, void*);
Kenneth Graunke186e2632010-06-23 14:00:27 -070086 char *info_log = talloc_strdup(ctx, "");
Carl Worth97638aa2010-08-17 23:13:56 -070087 const char *shader;
88 int ret;
89
90 if (argc) {
91 filename = argv[1];
92 }
93
94 shader = load_text_file(ctx, filename);
Carl Worthcf8bb192010-08-23 09:42:14 -070095 if (shader == NULL)
96 return 1;
97
Carl Worth97638aa2010-08-17 23:13:56 -070098 ret = preprocess(ctx, &shader, &info_log, NULL);
Carl Worth38aa8352010-05-10 11:52:29 -070099
Kenneth Graunke186e2632010-06-23 14:00:27 -0700100 printf("%s", shader);
101 fprintf(stderr, "%s", info_log);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700102
Kenneth Graunke186e2632010-06-23 14:00:27 -0700103 talloc_free(ctx);
Carl Worth38aa8352010-05-10 11:52:29 -0700104
105 return ret;
Carl Worth3a37b872010-05-10 11:44:09 -0700106}