]> git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - lex.c
Fixes to README
[xonotic/gmqcc.git] / lex.c
diff --git a/lex.c b/lex.c
index 556f485536934d6a860ddc40197bb5aa6327cb0f..5673701157a7aaf1b47fd043d821054dcaa0e488 100644 (file)
--- a/lex.c
+++ b/lex.c
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  * SOFTWARE.
  */
-#include <stdio.h>
-#include <limits.h>
-#include <stdlib.h>
-#include <ctype.h>
-#include <string.h>
 #include "gmqcc.h"
 
 /*
@@ -58,7 +53,7 @@ void lex_close(struct lex_file *file) {
        if (!file) return;
        
        fclose(file->file); /* may already be closed */
-       mem_d(file);
+       mem_d (file);
 }
 
 static void lex_addch(int ch, struct lex_file *file) {
@@ -249,7 +244,7 @@ static int lex_skipcmt(struct lex_file *file) {
                lex_addch(ch, file);
                while ((ch = lex_getch(file)) != '*') {
                        if (ch == EOF)
-                               return error(ERROR_LEX, "malformatted comment at line", "");
+                               return error(file, ERROR_LEX, "malformatted comment");
                        else
                                lex_addch(ch, file);
                }
@@ -337,3 +332,24 @@ void lex_reset(struct lex_file *file) {
        memset(file->peek,   0, sizeof(file->peek  ));
        memset(file->lastok, 0, sizeof(file->lastok));
 }
+
+/*
+ * Include a file into the lexer / parsing process:  This really
+ * should check if names are the same to prevent endless include
+ * recrusion.
+ */
+struct lex_file *lex_include(struct lex_file *lex, char *file) {
+       util_strrq(file);
+       if (strncmp(lex->name, file, strlen(lex->name)) == 0) {
+               error(lex, ERROR_LEX, "Source file cannot include itself\n");
+               exit (-1);
+       }
+       
+       FILE *fp = fopen(file, "r");
+       if  (!fp) {
+               error(lex, ERROR_LEX, "Include file `%s` doesn't exist\n", file);
+               exit (-1);
+       }
+       
+       return lex_open(fp);
+}