SCRIPLIB.C 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. Copyright (C) 1994-1995 Apogee Software, Ltd.
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. See the GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. // scriplib.c
  16. #ifdef __NeXT__
  17. #include <libc.h>
  18. #endif
  19. #ifndef __NeXT__
  20. #include <io.h>
  21. #include <dos.h>
  22. #include <fcntl.h>
  23. #endif
  24. #include "cmdlib.h"
  25. #include "scriplib.h"
  26. /*
  27. =============================================================================
  28. PARSING STUFF
  29. =============================================================================
  30. */
  31. char token[MAXTOKEN];
  32. char *scriptbuffer,*script_p,*scriptend_p;
  33. int grabbed;
  34. int scriptline;
  35. boolean endofscript;
  36. boolean tokenready; // only true if UnGetToken was just called
  37. /*
  38. ==============
  39. =
  40. = LoadScriptFile
  41. =
  42. ==============
  43. */
  44. void LoadScriptFile (char *filename)
  45. {
  46. long size;
  47. size = LoadFile (filename, (void **)&scriptbuffer);
  48. script_p = scriptbuffer;
  49. scriptend_p = script_p + size;
  50. scriptline = 1;
  51. endofscript = false;
  52. tokenready = false;
  53. }
  54. /*
  55. ==============
  56. =
  57. = UnGetToken
  58. =
  59. = Signals that the current token was not used, and should be reported
  60. = for the next GetToken. Note that
  61. GetToken (true);
  62. UnGetToken ();
  63. GetToken (false);
  64. = could cross a line boundary.
  65. =
  66. ==============
  67. */
  68. void UnGetToken (void)
  69. {
  70. tokenready = true;
  71. }
  72. /*
  73. ==============
  74. =
  75. = GetToken
  76. =
  77. ==============
  78. */
  79. void GetToken (boolean crossline)
  80. {
  81. char *token_p;
  82. if (tokenready) // is a token allready waiting?
  83. {
  84. tokenready = false;
  85. return;
  86. }
  87. if (script_p >= scriptend_p)
  88. {
  89. if (!crossline)
  90. Error ("Line %i is incomplete\n",scriptline);
  91. endofscript = true;
  92. return;
  93. }
  94. //
  95. // skip space
  96. //
  97. skipspace:
  98. while (*script_p <= 32)
  99. {
  100. if (script_p >= scriptend_p)
  101. {
  102. if (!crossline)
  103. Error ("Line %i is incomplete\n",scriptline);
  104. endofscript = true;
  105. return;
  106. }
  107. if (*script_p++ == '\n')
  108. {
  109. if (!crossline)
  110. Error ("Line %i is incomplete\n",scriptline);
  111. scriptline++;
  112. }
  113. }
  114. if (script_p >= scriptend_p)
  115. {
  116. if (!crossline)
  117. Error ("Line %i is incomplete\n",scriptline);
  118. endofscript = true;
  119. return;
  120. }
  121. if (*script_p == ';') // semicolon is comment field
  122. {
  123. if (!crossline)
  124. Error ("Line %i is incomplete\n",scriptline);
  125. while (*script_p++ != '\n')
  126. if (script_p >= scriptend_p)
  127. {
  128. endofscript = true;
  129. return;
  130. }
  131. goto skipspace;
  132. }
  133. //
  134. // copy token
  135. //
  136. token_p = token;
  137. while ( *script_p > 32 && *script_p != ';')
  138. {
  139. *token_p++ = *script_p++;
  140. if (script_p == scriptend_p)
  141. break;
  142. if (token_p == &token[MAXTOKEN])
  143. Error ("Token too large on line %i\n",scriptline);
  144. }
  145. *token_p = 0;
  146. }
  147. /*
  148. ==============
  149. =
  150. = TokenAvailable
  151. =
  152. = Returns true if there is another token on the line
  153. =
  154. ==============
  155. */
  156. boolean TokenAvailable (void)
  157. {
  158. char *search_p;
  159. search_p = script_p;
  160. if (search_p >= scriptend_p)
  161. return false;
  162. while ( *search_p <= 32)
  163. {
  164. if (*search_p == '\n')
  165. return false;
  166. search_p++;
  167. if (search_p == scriptend_p)
  168. return false;
  169. }
  170. if (*search_p == ';')
  171. return false;
  172. return true;
  173. }