glsl: Generate compile errors for explicit blend indices < 0 or > 1.
[mesa.git] / src / glsl / glcpp / glcpp.c
index a245cb54060809ebb6ff0adbc6dbf74b2620243d..e461a6502652fa8e023a77ae08d4248f63358ce8 100644 (file)
  * DEALINGS IN THE SOFTWARE.
  */
 
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <unistd.h>
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
 #include "glcpp.h"
+#include "main/mtypes.h"
+#include "main/shaderobj.h"
 
 extern int yydebug;
 
+void
+_mesa_reference_shader(struct gl_context *ctx, struct gl_shader **ptr,
+                       struct gl_shader *sh)
+{
+   (void) ctx;
+   *ptr = sh;
+}
+
+/* Read from fp until EOF and return a string of everything read.
+ */
 static char *
-load_text_file(void *ctx, const char *file_name)
+load_text_fp (void *ctx, FILE *fp)
 {
+#define CHUNK 4096
        char *text = NULL;
-       struct stat st;
-       ssize_t total_read = 0;
-       int fd = file_name == NULL ? STDIN_FILENO : open(file_name, O_RDONLY);
+       size_t text_size = 0;
+       size_t total_read = 0;
+       size_t bytes;
 
-       if (fd < 0) {
-               return NULL;
-       }
+       while (1) {
+               if (total_read + CHUNK + 1 > text_size) {
+                       text_size = text_size ? text_size * 2 : CHUNK + 1;
+                       text = reralloc_size (ctx, text, text_size);
+                       if (text == NULL) {
+                               fprintf (stderr, "Out of memory\n");
+                               return NULL;
+                       }
+               }
+               bytes = fread (text + total_read, 1, CHUNK, fp);
+               total_read += bytes;
 
-       if (fstat(fd, & st) == 0) {
-               text = (char *) talloc_size(ctx, st.st_size + 1);
-               if (text != NULL) {
-                       do {
-                               ssize_t bytes = read(fd, text + total_read,
-                                                    st.st_size - total_read);
-                               if (bytes < 0) {
-                                       text = NULL;
-                                       break;
-                               }
-
-                               if (bytes == 0) {
-                                       break;
-                               }
-
-                               total_read += bytes;
-                       } while (total_read < st.st_size);
-
-                       text[total_read] = '\0';
+               if (bytes < CHUNK) {
+                       break;
                }
        }
 
-       close(fd);
+       text[total_read] = '\0';
+
+       return text;
+}
+
+static char *
+load_text_file(void *ctx, const char *filename)
+{
+       char *text;
+       FILE *fp;
+
+       if (filename == NULL || strcmp (filename, "-") == 0)
+               return load_text_fp (ctx, stdin);
+
+       fp = fopen (filename, "r");
+       if (fp == NULL) {
+               fprintf (stderr, "Failed to open file %s: %s\n",
+                        filename, strerror (errno));
+               return NULL;
+       }
+
+       text = load_text_fp (ctx, fp);
+
+       fclose(fp);
 
        return text;
 }
 
 int
-main (void)
+main (int argc, char *argv[])
 {
-       void *ctx = talloc(NULL, void*);
-       const char *shader = load_text_file(ctx, NULL);
-       char *info_log = talloc_strdup(ctx, "");
-       int ret = preprocess(ctx, &shader, &info_log, NULL);
+       char *filename = NULL;
+       void *ctx = ralloc(NULL, void*);
+       char *info_log = ralloc_strdup(ctx, "");
+       const char *shader;
+       int ret;
+
+       if (argc) {
+               filename = argv[1];
+       }
+
+       shader = load_text_file (ctx, filename);
+       if (shader == NULL)
+          return 1;
+
+       ret = preprocess(ctx, &shader, &info_log, NULL, API_OPENGL);
 
        printf("%s", shader);
        fprintf(stderr, "%s", info_log);
 
-       talloc_free(ctx);
+       ralloc_free(ctx);
 
        return ret;
 }