]> de.git.xonotic.org Git - xonotic/netradiant.git/blobdiff - tools/quake3/common/scriplib.c
Merge commit '48410b113dd2036e69dbf723a39ec9af02fc9b12'
[xonotic/netradiant.git] / tools / quake3 / common / scriplib.c
index 267078c606d498969b9f65f3af48a0d86e6dd7ec..5502d35af3de3162471ebc10d63331c5bd1c07fc 100644 (file)
-/*\r
-Copyright (C) 1999-2007 id Software, Inc. and contributors.\r
-For a list of contributors, see the accompanying CONTRIBUTORS file.\r
-\r
-This file is part of GtkRadiant.\r
-\r
-GtkRadiant is free software; you can redistribute it and/or modify\r
-it under the terms of the GNU General Public License as published by\r
-the Free Software Foundation; either version 2 of the License, or\r
-(at your option) any later version.\r
-\r
-GtkRadiant is distributed in the hope that it will be useful,\r
-but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
-GNU General Public License for more details.\r
-\r
-You should have received a copy of the GNU General Public License\r
-along with GtkRadiant; if not, write to the Free Software\r
-Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA\r
-*/\r
-\r
-// scriplib.c\r
-\r
-#include "cmdlib.h"\r
-#include "mathlib.h"\r
-#include "inout.h"\r
-#include "scriplib.h"\r
-#include "vfs.h"\r
-\r
-/*\r
-=============================================================================\r
-\r
-                                               PARSING STUFF\r
-\r
-=============================================================================\r
-*/\r
-\r
-typedef struct\r
-{\r
-       char    filename[1024];\r
-       char    *buffer,*script_p,*end_p;\r
-       int     line;\r
-} script_t;\r
-\r
-#define        MAX_INCLUDES    8\r
-script_t       scriptstack[MAX_INCLUDES];\r
-script_t       *script;\r
-int                    scriptline;\r
-\r
-char    token[MAXTOKEN];\r
-qboolean endofscript;\r
-qboolean tokenready;                     // only qtrue if UnGetToken was just called\r
-\r
-/*\r
-==============\r
-AddScriptToStack\r
-==============\r
-*/\r
-void AddScriptToStack (const char *filename, int index)\r
-{\r
-  int size;\r
-\r
-  script++;\r
-  if (script == &scriptstack[MAX_INCLUDES])\r
-    Error ("script file exceeded MAX_INCLUDES");\r
-  strcpy (script->filename, ExpandPath (filename));\r
-\r
-  size = vfsLoadFile (script->filename, (void **)&script->buffer, index);\r
-\r
-  if (size == -1)\r
-    Sys_Printf ("Script file %s was not found\n", script->filename);\r
-  else\r
-  {\r
-    if (index > 0)\r
-      Sys_Printf ("entering %s (%d)\n", script->filename, index+1);\r
-    else\r
-      Sys_Printf ("entering %s\n", script->filename);\r
-  }\r
-\r
-  script->line = 1;\r
-  script->script_p = script->buffer;\r
-  script->end_p = script->buffer + size;\r
-}\r
-\r
-\r
-/*\r
-==============\r
-LoadScriptFile\r
-==============\r
-*/\r
-void LoadScriptFile (const char *filename, int index)\r
-{\r
-  script = scriptstack;\r
-  AddScriptToStack (filename, index);\r
-\r
-  endofscript = qfalse;\r
-  tokenready = qfalse;\r
-}\r
-\r
-\r
-/*\r
-==============\r
-ParseFromMemory\r
-==============\r
-*/\r
-void ParseFromMemory (char *buffer, int size)\r
-{\r
-       script = scriptstack;\r
-       script++;\r
-       if (script == &scriptstack[MAX_INCLUDES])\r
-               Error ("script file exceeded MAX_INCLUDES");\r
-       strcpy (script->filename, "memory buffer" );\r
-\r
-       script->buffer = buffer;\r
-       script->line = 1;\r
-       script->script_p = script->buffer;\r
-       script->end_p = script->buffer + size;\r
-\r
-       endofscript = qfalse;\r
-       tokenready = qfalse;\r
-}\r
-\r
-\r
-/*\r
-==============\r
-UnGetToken\r
-\r
-Signals that the current token was not used, and should be reported\r
-for the next GetToken.  Note that\r
-\r
-GetToken (qtrue);\r
-UnGetToken ();\r
-GetToken (qfalse);\r
-\r
-could cross a line boundary.\r
-==============\r
-*/\r
-void UnGetToken (void)\r
-{\r
-       tokenready = qtrue;\r
-}\r
-\r
-\r
-qboolean EndOfScript (qboolean crossline)\r
-{\r
-       if (!crossline)\r
-               Error ("Line %i is incomplete\n",scriptline);\r
-\r
-       if (!strcmp (script->filename, "memory buffer"))\r
-       {\r
-               endofscript = qtrue;\r
-               return qfalse;\r
-       }\r
-       \r
-       if( script->buffer == NULL )\r
-               Sys_Printf( "WARNING: Attempt to free already freed script buffer\n" );\r
-       else\r
-               free( script->buffer );\r
-       script->buffer = NULL;\r
-       if (script == scriptstack+1)\r
-       {\r
-               endofscript = qtrue;\r
-               return qfalse;\r
-       }\r
-       script--;\r
-       scriptline = script->line;\r
-       Sys_Printf ("returning to %s\n", script->filename);\r
-       return GetToken (crossline);\r
-}\r
-\r
-/*\r
-==============\r
-GetToken\r
-==============\r
-*/\r
-qboolean GetToken (qboolean crossline)\r
-{\r
-       char    *token_p;\r
-\r
-       \r
-       /* ydnar: dummy testing */\r
-       if( script == NULL || script->buffer == NULL )\r
-               return qfalse;\r
-       \r
-       if (tokenready)                         // is a token already waiting?\r
-       {\r
-               tokenready = qfalse;\r
-               return qtrue;\r
-       }\r
-\r
-       if ((script->script_p >= script->end_p) || (script->script_p == NULL))\r
-          return EndOfScript (crossline);\r
-\r
-//\r
-// skip space\r
-//\r
-skipspace:\r
-       while (*script->script_p <= 32)\r
-       {\r
-               if (script->script_p >= script->end_p)\r
-                       return EndOfScript (crossline);\r
-               if (*script->script_p++ == '\n')\r
-               {\r
-                       if (!crossline)\r
-                               Error ("Line %i is incomplete\n",scriptline);\r
-                       script->line++;\r
-                       scriptline = script->line;\r
-               }\r
-       }\r
-\r
-       if (script->script_p >= script->end_p)\r
-               return EndOfScript (crossline);\r
-\r
-       // ; # // comments\r
-       if (*script->script_p == ';' || *script->script_p == '#'\r
-               || ( script->script_p[0] == '/' && script->script_p[1] == '/') )\r
-       {\r
-               if (!crossline)\r
-                       Error ("Line %i is incomplete\n",scriptline);\r
-               while (*script->script_p++ != '\n')\r
-                       if (script->script_p >= script->end_p)\r
-                               return EndOfScript (crossline);\r
-               script->line++;\r
-               scriptline = script->line;\r
-               goto skipspace;\r
-       }\r
-\r
-       // /* */ comments\r
-       if (script->script_p[0] == '/' && script->script_p[1] == '*')\r
-       {\r
-               if (!crossline)\r
-                       Error ("Line %i is incomplete\n",scriptline);\r
-               script->script_p+=2;\r
-               while (script->script_p[0] != '*' && script->script_p[1] != '/')\r
-               {\r
-                       if ( *script->script_p == '\n' )\r
-                       {\r
-                               script->line++;\r
-                               scriptline = script->line;\r
-                       }\r
-                       script->script_p++;\r
-                       if (script->script_p >= script->end_p)\r
-                               return EndOfScript (crossline);\r
-               }\r
-               script->script_p += 2;\r
-               goto skipspace;\r
-       }\r
-\r
-//\r
-// copy token\r
-//\r
-       token_p = token;\r
-\r
-       if (*script->script_p == '"')\r
-       {\r
-               // quoted token\r
-               script->script_p++;\r
-               while (*script->script_p != '"')\r
-               {\r
-                       *token_p++ = *script->script_p++;\r
-                       if (script->script_p == script->end_p)\r
-                               break;\r
-                       if (token_p == &token[MAXTOKEN])\r
-                               Error ("Token too large on line %i\n",scriptline);\r
-               }\r
-               script->script_p++;\r
-       }\r
-       else    // regular token\r
-       while ( *script->script_p > 32 && *script->script_p != ';')\r
-       {\r
-               *token_p++ = *script->script_p++;\r
-               if (script->script_p == script->end_p)\r
-                       break;\r
-               if (token_p == &token[MAXTOKEN])\r
-                       Error ("Token too large on line %i\n",scriptline);\r
-       }\r
-\r
-       *token_p = 0;\r
-\r
-       if (!strcmp (token, "$include"))\r
-       {\r
-               GetToken (qfalse);\r
-               AddScriptToStack (token, 0);\r
-               return GetToken (crossline);\r
-       }\r
-\r
-       return qtrue;\r
-}\r
-\r
-\r
-/*\r
-==============\r
-TokenAvailable\r
-\r
-Returns qtrue if there is another token on the line\r
-==============\r
-*/\r
-qboolean TokenAvailable (void) {\r
-       int             oldLine, oldScriptLine;\r
-       qboolean        r;\r
-       \r
-       /* save */\r
-       oldLine = scriptline;\r
-       oldScriptLine = script->line;\r
-       \r
-       /* test */\r
-       r = GetToken( qtrue );\r
-       if ( !r ) {\r
-               return qfalse;\r
-       }\r
-       UnGetToken();\r
-       if ( oldLine == scriptline ) {\r
-               return qtrue;\r
-       }\r
-       \r
-       /* restore */\r
-       //%     scriptline = oldLine;\r
-       //%     script->line = oldScriptLine;\r
-\r
-       return qfalse;\r
-}\r
-\r
-\r
-//=====================================================================\r
-\r
-\r
-void MatchToken( char *match ) {\r
-       GetToken( qtrue );\r
-\r
-       if ( strcmp( token, match ) ) {\r
-               Error( "MatchToken( \"%s\" ) failed at line %i in file %s", match, scriptline, script->filename);\r
-       }\r
-}\r
-\r
-\r
-void Parse1DMatrix (int x, vec_t *m) {\r
-       int             i;\r
-\r
-       MatchToken( "(" );\r
-\r
-       for (i = 0 ; i < x ; i++) {\r
-               GetToken( qfalse );\r
-               m[i] = atof(token);\r
-       }\r
-\r
-       MatchToken( ")" );\r
-}\r
-\r
-void Parse2DMatrix (int y, int x, vec_t *m) {\r
-       int             i;\r
-\r
-       MatchToken( "(" );\r
-\r
-       for (i = 0 ; i < y ; i++) {\r
-               Parse1DMatrix (x, m + i * x);\r
-       }\r
-\r
-       MatchToken( ")" );\r
-}\r
-\r
-void Parse3DMatrix (int z, int y, int x, vec_t *m) {\r
-       int             i;\r
-\r
-       MatchToken( "(" );\r
-\r
-       for (i = 0 ; i < z ; i++) {\r
-               Parse2DMatrix (y, x, m + i * x*y);\r
-       }\r
-\r
-       MatchToken( ")" );\r
-}\r
-\r
-\r
-void Write1DMatrix (FILE *f, int x, vec_t *m) {\r
-       int             i;\r
-\r
-       fprintf (f, "( ");\r
-       for (i = 0 ; i < x ; i++) {\r
-               if (m[i] == (int)m[i] ) {\r
-                       fprintf (f, "%i ", (int)m[i]);\r
-               } else {\r
-                       fprintf (f, "%f ", m[i]);\r
-               }\r
-       }\r
-       fprintf (f, ")");\r
-}\r
-\r
-void Write2DMatrix (FILE *f, int y, int x, vec_t *m) {\r
-       int             i;\r
-\r
-       fprintf (f, "( ");\r
-       for (i = 0 ; i < y ; i++) {\r
-               Write1DMatrix (f, x, m + i*x);\r
-               fprintf (f, " ");\r
-       }\r
-       fprintf (f, ")\n");\r
-}\r
-\r
-\r
-void Write3DMatrix (FILE *f, int z, int y, int x, vec_t *m) {\r
-       int             i;\r
-\r
-       fprintf (f, "(\n");\r
-       for (i = 0 ; i < z ; i++) {\r
-               Write2DMatrix (f, y, x, m + i*(x*y) );\r
-       }\r
-       fprintf (f, ")\n");\r
-}\r
-\r
+/*
+Copyright (C) 1999-2006 Id Software, Inc. and contributors.
+For a list of contributors, see the accompanying CONTRIBUTORS file.
+
+This file is part of GtkRadiant.
+
+GtkRadiant is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+GtkRadiant is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GtkRadiant; if not, write to the Free Software
+Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+*/
+
+// scriplib.c
+
+#include "cmdlib.h"
+#include "mathlib.h"
+#include "inout.h"
+#include "scriplib.h"
+#include "vfs.h"
+
+/*
+=============================================================================
+
+                                               PARSING STUFF
+
+=============================================================================
+*/
+
+typedef struct
+{
+       char    filename[1024];
+       char    *buffer,*script_p,*end_p;
+       int     line;
+} script_t;
+
+#define        MAX_INCLUDES    8
+script_t       scriptstack[MAX_INCLUDES];
+script_t       *script;
+int                    scriptline;
+
+char    token[MAXTOKEN];
+qboolean endofscript;
+qboolean tokenready;                     // only qtrue if UnGetToken was just called
+
+/*
+==============
+AddScriptToStack
+==============
+*/
+void AddScriptToStack (const char *filename, int index)
+{
+  int size;
+
+  script++;
+  if (script == &scriptstack[MAX_INCLUDES])
+    Error ("script file exceeded MAX_INCLUDES");
+  strcpy (script->filename, ExpandPath (filename));
+
+  size = vfsLoadFile (script->filename, (void **)&script->buffer, index);
+
+  if (size == -1)
+    Sys_Printf ("Script file %s was not found\n", script->filename);
+  else
+  {
+    if (index > 0)
+      Sys_Printf ("entering %s (%d)\n", script->filename, index+1);
+    else
+      Sys_Printf ("entering %s\n", script->filename);
+  }
+
+  script->line = 1;
+  script->script_p = script->buffer;
+  script->end_p = script->buffer + size;
+}
+
+
+/*
+==============
+LoadScriptFile
+==============
+*/
+void LoadScriptFile (const char *filename, int index)
+{
+  script = scriptstack;
+  AddScriptToStack (filename, index);
+
+  endofscript = qfalse;
+  tokenready = qfalse;
+}
+
+
+/*
+==============
+ParseFromMemory
+==============
+*/
+void ParseFromMemory (char *buffer, int size)
+{
+       script = scriptstack;
+       script++;
+       if (script == &scriptstack[MAX_INCLUDES])
+               Error ("script file exceeded MAX_INCLUDES");
+       strcpy (script->filename, "memory buffer" );
+
+       script->buffer = buffer;
+       script->line = 1;
+       script->script_p = script->buffer;
+       script->end_p = script->buffer + size;
+
+       endofscript = qfalse;
+       tokenready = qfalse;
+}
+
+
+/*
+==============
+UnGetToken
+
+Signals that the current token was not used, and should be reported
+for the next GetToken.  Note that
+
+GetToken (qtrue);
+UnGetToken ();
+GetToken (qfalse);
+
+could cross a line boundary.
+==============
+*/
+void UnGetToken (void)
+{
+       tokenready = qtrue;
+}
+
+
+qboolean EndOfScript (qboolean crossline)
+{
+       if (!crossline)
+               Error ("Line %i is incomplete\n",scriptline);
+
+       if (!strcmp (script->filename, "memory buffer"))
+       {
+               endofscript = qtrue;
+               return qfalse;
+       }
+       
+       if( script->buffer == NULL )
+               Sys_Printf( "WARNING: Attempt to free already freed script buffer\n" );
+       else
+               free( script->buffer );
+       script->buffer = NULL;
+       if (script == scriptstack+1)
+       {
+               endofscript = qtrue;
+               return qfalse;
+       }
+       script--;
+       scriptline = script->line;
+       Sys_Printf ("returning to %s\n", script->filename);
+       return GetToken (crossline);
+}
+
+/*
+==============
+GetToken
+==============
+*/
+qboolean GetToken (qboolean crossline)
+{
+       char    *token_p;
+
+       
+       /* ydnar: dummy testing */
+       if( script == NULL || script->buffer == NULL )
+               return qfalse;
+       
+       if (tokenready)                         // is a token already waiting?
+       {
+               tokenready = qfalse;
+               return qtrue;
+       }
+
+       if ((script->script_p >= script->end_p) || (script->script_p == NULL))
+          return EndOfScript (crossline);
+
+//
+// skip space
+//
+skipspace:
+       while (*script->script_p <= 32)
+       {
+               if (script->script_p >= script->end_p)
+                       return EndOfScript (crossline);
+               if (*script->script_p++ == '\n')
+               {
+                       if (!crossline)
+                               Error ("Line %i is incomplete\n",scriptline);
+                       script->line++;
+                       scriptline = script->line;
+               }
+       }
+
+       if (script->script_p >= script->end_p)
+               return EndOfScript (crossline);
+
+       // ; # // comments
+       if (*script->script_p == ';' || *script->script_p == '#'
+               || ( script->script_p[0] == '/' && script->script_p[1] == '/') )
+       {
+               if (!crossline)
+                       Error ("Line %i is incomplete\n",scriptline);
+               while (*script->script_p++ != '\n')
+                       if (script->script_p >= script->end_p)
+                               return EndOfScript (crossline);
+               script->line++;
+               scriptline = script->line;
+               goto skipspace;
+       }
+
+       // /* */ comments
+       if (script->script_p[0] == '/' && script->script_p[1] == '*')
+       {
+               if (!crossline)
+                       Error ("Line %i is incomplete\n",scriptline);
+               script->script_p+=2;
+               while (script->script_p[0] != '*' && script->script_p[1] != '/')
+               {
+                       if ( *script->script_p == '\n' )
+                       {
+                               script->line++;
+                               scriptline = script->line;
+                       }
+                       script->script_p++;
+                       if (script->script_p >= script->end_p)
+                               return EndOfScript (crossline);
+               }
+               script->script_p += 2;
+               goto skipspace;
+       }
+
+//
+// copy token
+//
+       token_p = token;
+
+       if (*script->script_p == '"')
+       {
+               // quoted token
+               script->script_p++;
+               while (*script->script_p != '"')
+               {
+                       *token_p++ = *script->script_p++;
+                       if (script->script_p == script->end_p)
+                               break;
+                       if (token_p == &token[MAXTOKEN])
+                               Error ("Token too large on line %i\n",scriptline);
+               }
+               script->script_p++;
+       }
+       else    // regular token
+       while ( *script->script_p > 32 && *script->script_p != ';')
+       {
+               *token_p++ = *script->script_p++;
+               if (script->script_p == script->end_p)
+                       break;
+               if (token_p == &token[MAXTOKEN])
+                       Error ("Token too large on line %i\n",scriptline);
+       }
+
+       *token_p = 0;
+
+       if (!strcmp (token, "$include"))
+       {
+               GetToken (qfalse);
+               AddScriptToStack (token, 0);
+               return GetToken (crossline);
+       }
+
+       return qtrue;
+}
+
+
+/*
+==============
+TokenAvailable
+
+Returns qtrue if there is another token on the line
+==============
+*/
+qboolean TokenAvailable (void) {
+       int             oldLine;
+       qboolean        r;
+       
+       /* save */
+       oldLine = scriptline;
+       
+       /* test */
+       r = GetToken( qtrue );
+       if ( !r ) {
+               return qfalse;
+       }
+       UnGetToken();
+       if ( oldLine == scriptline ) {
+               return qtrue;
+       }
+       
+       /* restore */
+       //%     scriptline = oldLine;
+       //%     script->line = oldScriptLine;
+
+       return qfalse;
+}
+
+
+//=====================================================================
+
+
+void MatchToken( char *match ) {
+       GetToken( qtrue );
+
+       if ( strcmp( token, match ) ) {
+               Error( "MatchToken( \"%s\" ) failed at line %i in file %s", match, scriptline, script->filename);
+       }
+}
+
+
+void Parse1DMatrix (int x, vec_t *m) {
+       int             i;
+
+       MatchToken( "(" );
+
+       for (i = 0 ; i < x ; i++) {
+               GetToken( qfalse );
+               m[i] = atof(token);
+       }
+
+       MatchToken( ")" );
+}
+
+void Parse2DMatrix (int y, int x, vec_t *m) {
+       int             i;
+
+       MatchToken( "(" );
+
+       for (i = 0 ; i < y ; i++) {
+               Parse1DMatrix (x, m + i * x);
+       }
+
+       MatchToken( ")" );
+}
+
+void Parse3DMatrix (int z, int y, int x, vec_t *m) {
+       int             i;
+
+       MatchToken( "(" );
+
+       for (i = 0 ; i < z ; i++) {
+               Parse2DMatrix (y, x, m + i * x*y);
+       }
+
+       MatchToken( ")" );
+}
+
+
+void Write1DMatrix (FILE *f, int x, vec_t *m) {
+       int             i;
+
+       fprintf (f, "( ");
+       for (i = 0 ; i < x ; i++) {
+               if (m[i] == (int)m[i] ) {
+                       fprintf (f, "%i ", (int)m[i]);
+               } else {
+                       fprintf (f, "%f ", m[i]);
+               }
+       }
+       fprintf (f, ")");
+}
+
+void Write2DMatrix (FILE *f, int y, int x, vec_t *m) {
+       int             i;
+
+       fprintf (f, "( ");
+       for (i = 0 ; i < y ; i++) {
+               Write1DMatrix (f, x, m + i*x);
+               fprintf (f, " ");
+       }
+       fprintf (f, ")\n");
+}
+
+
+void Write3DMatrix (FILE *f, int z, int y, int x, vec_t *m) {
+       int             i;
+
+       fprintf (f, "(\n");
+       for (i = 0 ; i < z ; i++) {
+               Write2DMatrix (f, y, x, m + i*(x*y) );
+       }
+       fprintf (f, ")\n");
+}
+