glcpp.c revision cf8bb19a114d753bca94f920b87dcf51aa26af99
1/* 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 24#include <sys/types.h> 25#include <sys/stat.h> 26#include <fcntl.h> 27#include <unistd.h> 28#include <string.h> 29#include <errno.h> 30#include "glcpp.h" 31 32extern int yydebug; 33 34static 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; 40 int fd; 41 42 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 } 52 } 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 82main (int argc, char *argv[]) 83{ 84 char *filename = NULL; 85 void *ctx = talloc(NULL, void*); 86 char *info_log = talloc_strdup(ctx, ""); 87 const char *shader; 88 int ret; 89 90 if (argc) { 91 filename = argv[1]; 92 } 93 94 shader = load_text_file(ctx, filename); 95 if (shader == NULL) 96 return 1; 97 98 ret = preprocess(ctx, &shader, &info_log, NULL); 99 100 printf("%s", shader); 101 fprintf(stderr, "%s", info_log); 102 103 talloc_free(ctx); 104 105 return ret; 106} 107