ChipMaster's bwBASIC This also includes history going back to v2.10. *WARN* some binary files might have been corrupted by CRLF.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

10218 lines
215 KiB

  1. /***************************************************************
  2. bwb_cmd.c Miscellaneous Commands
  3. for Bywater BASIC Interpreter
  4. Copyright (c) 1993, Ted A. Campbell
  5. Bywater Software
  6. email: tcamp@delphi.com
  7. Copyright and Permissions Information:
  8. All U.S. and international rights are claimed by the author,
  9. Ted A. Campbell.
  10. This software is released under the terms of the GNU General
  11. Public License (GPL), which is distributed with this software
  12. in the file "COPYING". The GPL specifies the terms under
  13. which users may copy and use the software in this distribution.
  14. A separate license is available for commercial distribution,
  15. for information on which you should contact the author.
  16. ***************************************************************/
  17. /*---------------------------------------------------------------*/
  18. /* NOTE: Modifications marked "JBV" were made by Jon B. Volkoff, */
  19. /* 11/1995 (eidetics@cerf.net). */
  20. /* */
  21. /* Those additionally marked with "DD" were at the suggestion of */
  22. /* Dale DePriest (daled@cadence.com). */
  23. /* */
  24. /* Version 3.00 by Howard Wulf, AF5NE */
  25. /* */
  26. /* Version 3.10 by Howard Wulf, AF5NE */
  27. /* */
  28. /* Version 3.20 by Howard Wulf, AF5NE */
  29. /* */
  30. /*---------------------------------------------------------------*/
  31. #include "bwbasic.h"
  32. static void bwb_copy_file (char *Source, char *Target);
  33. static LineType *bwb_delete (LineType * l);
  34. static void bwb_display_file (char *Source);
  35. static LineType *bwb_load (LineType * Line, char *Prompt, int IsNew);
  36. static void bwb_new (void);
  37. static LineType *bwb_run_filename_or_linenumber (LineType * L);
  38. static LineType *bwb_save (LineType * Line, char *Prompt);
  39. static LineType *bwb_system (LineType * l);
  40. static LineType *bwb_xlist (LineType * l, FILE * file);
  41. static LineType *bwx_run (LineType * Line, char *ProgramName);
  42. static void CommandOptionVersion (int n, char *OutputLine);
  43. static void CommandUniqueID (int i, char *UniqueID);
  44. static void CommandVector (int i, char *Vector);
  45. static VariableType *find_variable_by_type (char *name, int dimensions,
  46. char VariableTypeCode);
  47. static void FixUp (char *Name);
  48. static LineType *H14_RENAME (LineType * l);
  49. static int line_read_matrix_redim (LineType * l, VariableType * v);
  50. static void ProcessEscapeChars (const char *Input, char *Output);
  51. static int xl_line (FILE * file, LineType * l);
  52. /*
  53. fprintf( file, "------------------------------------------------------------\n");
  54. 123456789012345678901234567890123456789012345678901234567890
  55. fprintf( file, " SYNTAX: %s\n", IntrinsicCommandTable[n].Syntax);
  56. sprintf( tbuf, "DESCRIPTION: %s\n", IntrinsicCommandTable[n].Description);
  57. fprintf( file, " " );
  58. fprintf( file, " [%c] %s\n", X, bwb_vertable[i].Name);
  59. 1234567890123
  60. */
  61. #define LEFT_LENGTH 13
  62. #define RIGHT_LENGTH 47
  63. #define TOTAL_LENGTH ( LEFT_LENGTH + RIGHT_LENGTH )
  64. /*
  65. --------------------------------------------------------------------------------------------
  66. EDIT, RENUM, RENUMBER
  67. --------------------------------------------------------------------------------------------
  68. */
  69. static LineType *
  70. bwx_run (LineType * Line, char *ProgramName)
  71. {
  72. size_t n;
  73. char *tbuf;
  74. assert (Line != NULL);
  75. assert( My != NULL );
  76. if (is_empty_string (ProgramName))
  77. {
  78. WARN_BAD_FILE_NAME;
  79. return (Line);
  80. }
  81. if (is_empty_string (My->ProgramFilename))
  82. {
  83. WARN_BAD_FILE_NAME;
  84. return (Line);
  85. }
  86. n = bwb_strlen (ProgramName) + 1 + bwb_strlen (My->ProgramFilename);
  87. if ((tbuf = (char *) calloc (n + 1 /* NulChar */ , sizeof (char))) == NULL)
  88. {
  89. WARN_OUT_OF_MEMORY;
  90. return (Line);
  91. }
  92. bwb_strcpy (tbuf, ProgramName);
  93. bwb_strcat (tbuf, " ");
  94. bwb_strcat (tbuf, My->ProgramFilename);
  95. system (tbuf);
  96. free (tbuf);
  97. tbuf = NULL;
  98. /* open edited file for read */
  99. bwb_NEW (Line); /* Relocated by JBV (bug found by DD) */
  100. if (bwb_fload (NULL) == FALSE)
  101. {
  102. WARN_BAD_FILE_NAME;
  103. return (Line);
  104. }
  105. return (Line);
  106. }
  107. /***************************************************************
  108. FUNCTION: bwb_edit()
  109. DESCRIPTION: This function implements the BASIC EDIT
  110. program by shelling out to a default editor
  111. specified by the variable BWB.EDITOR$.
  112. SYNTAX: EDIT
  113. ***************************************************************/
  114. LineType *
  115. bwb_EDIT (LineType * Line)
  116. {
  117. /*
  118. SYNTAX: EDIT
  119. */
  120. assert (Line != NULL);
  121. assert( My != NULL );
  122. return bwx_run (Line, My->OptionEditString);
  123. }
  124. /***************************************************************
  125. FUNCTION: bwb_renum()
  126. DESCRIPTION: This function implements the BASIC RENUM
  127. command by shelling out to a default
  128. renumbering program called "renum".
  129. Added by JBV 10/95
  130. SYNTAX: RENUM
  131. ***************************************************************/
  132. LineType *
  133. bwb_RENUM (LineType * Line)
  134. {
  135. /*
  136. SYNTAX: RENUM
  137. */
  138. assert (Line != NULL);
  139. assert( My != NULL );
  140. return bwx_run (Line, My->OptionRenumString);
  141. }
  142. LineType *
  143. bwb_RENUMBER (LineType * Line)
  144. {
  145. /*
  146. SYNTAX: RENUMBER
  147. */
  148. assert (Line != NULL);
  149. assert( My != NULL );
  150. return bwx_run (Line, My->OptionRenumString);
  151. }
  152. /*
  153. --------------------------------------------------------------------------------------------
  154. REM
  155. --------------------------------------------------------------------------------------------
  156. */
  157. LineType *
  158. bwb_REM (LineType * L)
  159. {
  160. /*
  161. SYNTAX: REM comment
  162. */
  163. /*
  164. This line holds BASIC comments.
  165. */
  166. assert (L != NULL);
  167. line_skip_eol (L);
  168. return L;
  169. }
  170. /*
  171. --------------------------------------------------------------------------------------------
  172. IMAGE
  173. --------------------------------------------------------------------------------------------
  174. */
  175. LineType *
  176. bwb_IMAGE (LineType * L)
  177. {
  178. /*
  179. SYNTAX: IMAGE print-using-format
  180. */
  181. assert (L != NULL);
  182. line_skip_eol (L);
  183. return L;
  184. }
  185. /*
  186. --------------------------------------------------------------------------------------------
  187. LET
  188. --------------------------------------------------------------------------------------------
  189. */
  190. LineType *
  191. bwb_LET (LineType * L)
  192. {
  193. /*
  194. SYNTAX: LET variable [,...] = expression
  195. */
  196. VariableType *v;
  197. VariantType x;
  198. VariantType *X;
  199. assert (L != NULL);
  200. X = &x;
  201. CLEAR_VARIANT (X);
  202. /* read the list of variables */
  203. do
  204. {
  205. if ((v = line_read_scalar (L)) == NULL)
  206. {
  207. WARN_VARIABLE_NOT_DECLARED;
  208. goto EXIT;
  209. }
  210. }
  211. while (line_skip_seperator (L));
  212. /* skip the equal sign */
  213. if (line_skip_EqualChar (L))
  214. {
  215. /* OK */
  216. }
  217. else if (line_skip_word (L, "EQ"))
  218. {
  219. /* OK */
  220. }
  221. else if (line_skip_word (L, ".EQ."))
  222. {
  223. /* OK */
  224. }
  225. else
  226. {
  227. WARN_SYNTAX_ERROR;
  228. goto EXIT;
  229. }
  230. /* evaluate the expression */
  231. if (line_read_expression (L, X)) /* bwb_LET */
  232. {
  233. /* save the value */
  234. if (line_is_eol (L) == FALSE)
  235. {
  236. WARN_SYNTAX_ERROR;
  237. goto EXIT;
  238. }
  239. L->position = L->Startpos;
  240. /* for each variable, assign the value */
  241. do
  242. {
  243. /* read a variable */
  244. if ((v = line_read_scalar (L)) == NULL)
  245. {
  246. WARN_VARIABLE_NOT_DECLARED;
  247. goto EXIT;
  248. }
  249. assert (v != NULL);
  250. assert (X != NULL);
  251. if (var_set (v, X) == FALSE)
  252. {
  253. WARN_TYPE_MISMATCH;
  254. goto EXIT;
  255. }
  256. }
  257. while (line_skip_seperator (L));
  258. /* we are now at the equals sign */
  259. line_skip_eol (L);
  260. }
  261. else
  262. {
  263. WARN_SYNTAX_ERROR;
  264. }
  265. EXIT:
  266. RELEASE_VARIANT (X);
  267. return L;
  268. }
  269. LineType *
  270. bwb_CONST (LineType * L)
  271. {
  272. /*
  273. SYNTAX: CONST variable [,...] = expression
  274. */
  275. VariableType *v;
  276. VariantType x;
  277. VariantType *X;
  278. assert (L != NULL);
  279. X = &x;
  280. CLEAR_VARIANT (X);
  281. /* read the list of variables */
  282. do
  283. {
  284. if ((v = line_read_scalar (L)) == NULL)
  285. {
  286. WARN_VARIABLE_NOT_DECLARED;
  287. goto EXIT;
  288. }
  289. }
  290. while (line_skip_seperator (L));
  291. /* we are now at the equals sign */
  292. /* skip the equal sign */
  293. if (line_skip_EqualChar (L))
  294. {
  295. /* OK */
  296. }
  297. else if (line_skip_word (L, "EQ"))
  298. {
  299. /* OK */
  300. }
  301. else if (line_skip_word (L, ".EQ."))
  302. {
  303. /* OK */
  304. }
  305. else
  306. {
  307. WARN_SYNTAX_ERROR;
  308. goto EXIT;
  309. }
  310. /* evaluate the expression */
  311. if (line_read_expression (L, X)) /* bwb_LET */
  312. {
  313. /* save the value */
  314. if (line_is_eol (L) == FALSE)
  315. {
  316. WARN_SYNTAX_ERROR;
  317. goto EXIT;
  318. }
  319. /* for each variable, assign the value */
  320. L->position = L->Startpos;
  321. do
  322. {
  323. /* read a variable */
  324. if ((v = line_read_scalar (L)) == NULL)
  325. {
  326. WARN_VARIABLE_NOT_DECLARED;
  327. goto EXIT;
  328. }
  329. assert (v != NULL);
  330. assert (X != NULL);
  331. if (var_set (v, X) == FALSE)
  332. {
  333. WARN_TYPE_MISMATCH;
  334. goto EXIT;
  335. }
  336. }
  337. while (line_skip_seperator (L));
  338. /* we are now at the equals sign */
  339. /* for each variable, mark as constant */
  340. L->position = L->Startpos;
  341. do
  342. {
  343. /* read a variable */
  344. if ((v = line_read_scalar (L)) == NULL)
  345. {
  346. WARN_VARIABLE_NOT_DECLARED;
  347. goto EXIT;
  348. }
  349. assert (v != NULL);
  350. v->VariableFlags |= VARIABLE_CONSTANT;
  351. }
  352. while (line_skip_seperator (L));
  353. /* we are now at the equals sign */
  354. line_skip_eol (L);
  355. }
  356. else
  357. {
  358. WARN_SYNTAX_ERROR;
  359. }
  360. EXIT:
  361. RELEASE_VARIANT (X);
  362. return L;
  363. }
  364. LineType *
  365. bwb_DEC (LineType * L)
  366. {
  367. /*
  368. SYNTAX: DEC variable [,...]
  369. */
  370. VariableType *v;
  371. VariantType x;
  372. VariantType *X;
  373. assert (L != NULL);
  374. X = &x;
  375. CLEAR_VARIANT (X);
  376. /* read the list of variables */
  377. do
  378. {
  379. if ((v = line_read_scalar (L)) == NULL)
  380. {
  381. WARN_VARIABLE_NOT_DECLARED;
  382. goto EXIT;
  383. }
  384. if (v->VariableTypeCode == StringTypeCode)
  385. {
  386. WARN_TYPE_MISMATCH;
  387. goto EXIT;
  388. }
  389. }
  390. while (line_skip_seperator (L));
  391. /* we are now at the end of the line */
  392. if (line_is_eol (L) == FALSE)
  393. {
  394. WARN_SYNTAX_ERROR;
  395. goto EXIT;
  396. }
  397. L->position = L->Startpos;
  398. /* for each variable, assign the value */
  399. do
  400. {
  401. /* read a variable */
  402. if ((v = line_read_scalar (L)) == NULL)
  403. {
  404. WARN_VARIABLE_NOT_DECLARED;
  405. goto EXIT;
  406. }
  407. assert (v != NULL);
  408. assert (X != NULL);
  409. if (var_get (v, X) == FALSE)
  410. {
  411. WARN_VARIABLE_NOT_DECLARED;
  412. goto EXIT;
  413. }
  414. X->Number--;
  415. if (var_set (v, X) == FALSE)
  416. {
  417. WARN_VARIABLE_NOT_DECLARED;
  418. goto EXIT;
  419. }
  420. }
  421. while (line_skip_seperator (L));
  422. /* we are now at the end of the line */
  423. EXIT:
  424. RELEASE_VARIANT (X);
  425. return L;
  426. }
  427. LineType *
  428. bwb_INC (LineType * L)
  429. {
  430. /*
  431. SYNTAX: INC variable [,...]
  432. */
  433. VariableType *v;
  434. VariantType x;
  435. VariantType *X;
  436. assert (L != NULL);
  437. X = &x;
  438. CLEAR_VARIANT (X);
  439. /* read the list of variables */
  440. do
  441. {
  442. if ((v = line_read_scalar (L)) == NULL)
  443. {
  444. WARN_VARIABLE_NOT_DECLARED;
  445. goto EXIT;
  446. }
  447. if (v->VariableTypeCode == StringTypeCode)
  448. {
  449. WARN_TYPE_MISMATCH;
  450. goto EXIT;
  451. }
  452. }
  453. while (line_skip_seperator (L));
  454. /* we are now at the end of the line */
  455. if (line_is_eol (L) == FALSE)
  456. {
  457. WARN_SYNTAX_ERROR;
  458. goto EXIT;
  459. }
  460. L->position = L->Startpos;
  461. /* for each variable, assign the value */
  462. do
  463. {
  464. /* read a variable */
  465. if ((v = line_read_scalar (L)) == NULL)
  466. {
  467. WARN_VARIABLE_NOT_DECLARED;
  468. goto EXIT;
  469. }
  470. assert (v != NULL);
  471. assert (X != NULL);
  472. if (var_get (v, X) == FALSE)
  473. {
  474. WARN_VARIABLE_NOT_DECLARED;
  475. goto EXIT;
  476. }
  477. X->Number++;
  478. if (var_set (v, X) == FALSE)
  479. {
  480. WARN_VARIABLE_NOT_DECLARED;
  481. goto EXIT;
  482. }
  483. }
  484. while (line_skip_seperator (L));
  485. /* we are now at the end of the line */
  486. EXIT:
  487. RELEASE_VARIANT (X);
  488. return L;
  489. }
  490. /*
  491. --------------------------------------------------------------------------------------------
  492. GO
  493. --------------------------------------------------------------------------------------------
  494. */
  495. LineType *
  496. bwb_GO (LineType * L)
  497. {
  498. assert (L != NULL);
  499. WARN_SYNTAX_ERROR;
  500. return L;
  501. }
  502. LineType *
  503. bwb_THEN (LineType * L)
  504. {
  505. assert (L != NULL);
  506. WARN_SYNTAX_ERROR;
  507. return L;
  508. }
  509. LineType *
  510. bwb_TO (LineType * L)
  511. {
  512. assert (L != NULL);
  513. WARN_SYNTAX_ERROR;
  514. return L;
  515. }
  516. LineType *
  517. bwb_STEP (LineType * L)
  518. {
  519. assert (L != NULL);
  520. WARN_SYNTAX_ERROR;
  521. return L;
  522. }
  523. LineType *
  524. bwb_OF (LineType * L)
  525. {
  526. assert (L != NULL);
  527. WARN_SYNTAX_ERROR;
  528. return L;
  529. }
  530. LineType *
  531. bwb_AS (LineType * L)
  532. {
  533. assert (L != NULL);
  534. WARN_SYNTAX_ERROR;
  535. return L;
  536. }
  537. /*
  538. --------------------------------------------------------------------------------------------
  539. AUTO
  540. --------------------------------------------------------------------------------------------
  541. */
  542. LineType *
  543. bwb_BUILD (LineType * L)
  544. {
  545. /*
  546. SYNTAX: BUILD
  547. SYNTAX: BUILD start
  548. SYNTAX: BUILD start, increment
  549. */
  550. assert (L != NULL);
  551. return bwb_AUTO (L);
  552. }
  553. LineType *
  554. bwb_AUTO (LineType * L)
  555. {
  556. /*
  557. SYNTAX: AUTO
  558. SYNTAX: AUTO start
  559. SYNTAX: AUTO start , increment
  560. */
  561. assert (L != NULL);
  562. assert( My != NULL );
  563. My->AutomaticLineNumber = 0;
  564. My->AutomaticLineIncrement = 0;
  565. if (line_is_eol (L))
  566. {
  567. /* AUTO */
  568. My->AutomaticLineNumber = 10;
  569. My->AutomaticLineIncrement = 10;
  570. return L;
  571. }
  572. if (line_read_line_number (L, &My->AutomaticLineNumber))
  573. {
  574. /* AUTO ### ... */
  575. if (My->AutomaticLineNumber < MINLIN || My->AutomaticLineNumber > MAXLIN)
  576. {
  577. WARN_UNDEFINED_LINE;
  578. return L;
  579. }
  580. if (line_is_eol (L))
  581. {
  582. /* AUTO start */
  583. My->AutomaticLineIncrement = 10;
  584. return L;
  585. }
  586. else if (line_skip_seperator (L))
  587. {
  588. /* AUTO ### , ... */
  589. if (line_read_line_number (L, &My->AutomaticLineIncrement))
  590. {
  591. /* AUTO start , increment */
  592. if (My->AutomaticLineIncrement < MINLIN
  593. || My->AutomaticLineIncrement > MAXLIN)
  594. {
  595. WARN_UNDEFINED_LINE;
  596. return L;
  597. }
  598. return L;
  599. }
  600. }
  601. }
  602. My->AutomaticLineNumber = 0;
  603. My->AutomaticLineIncrement = 0;
  604. WARN_SYNTAX_ERROR;
  605. return L;
  606. }
  607. /*
  608. --------------------------------------------------------------------------------------------
  609. BREAK
  610. --------------------------------------------------------------------------------------------
  611. */
  612. LineType *
  613. bwb_BREAK (LineType * l)
  614. {
  615. /*
  616. SYNTAX: BREAK
  617. SYNTAX: BREAK line [,...]
  618. SYNTAX: BREAK line - line
  619. */
  620. assert (l != NULL);
  621. assert( My != NULL );
  622. assert( My->StartMarker != NULL );
  623. assert( My->EndMarker != NULL );
  624. if (line_is_eol (l))
  625. {
  626. /* BREAK */
  627. /* remove all line breaks */
  628. LineType *x;
  629. for (x = My->StartMarker->next; x != My->EndMarker; x = x->next)
  630. {
  631. x->LineFlags &= ~LINE_BREAK;
  632. }
  633. return (l);
  634. }
  635. else
  636. {
  637. do
  638. {
  639. int head;
  640. int tail;
  641. if (line_read_line_sequence (l, &head, &tail))
  642. {
  643. /* BREAK 's' - 'e' */
  644. LineType *x;
  645. if (head < MINLIN || head > MAXLIN)
  646. {
  647. WARN_UNDEFINED_LINE;
  648. return (l);
  649. }
  650. if (tail < MINLIN || tail > MAXLIN)
  651. {
  652. WARN_UNDEFINED_LINE;
  653. return (l);
  654. }
  655. if (head > tail)
  656. {
  657. WARN_SYNTAX_ERROR;
  658. return (l);
  659. }
  660. /* valid range */
  661. /* now go through and list appropriate lines */
  662. for (x = My->StartMarker->next; x != My->EndMarker; x = x->next)
  663. {
  664. if (head <= x->number && x->number <= tail)
  665. {
  666. if (x->LineFlags & LINE_NUMBERED)
  667. {
  668. x->LineFlags |= LINE_BREAK;
  669. }
  670. }
  671. }
  672. }
  673. else
  674. {
  675. WARN_SYNTAX_ERROR;
  676. return (l);
  677. }
  678. }
  679. while (line_skip_seperator (l));
  680. }
  681. return (l);
  682. }
  683. /*
  684. --------------------------------------------------------------------------------------------
  685. DSP
  686. --------------------------------------------------------------------------------------------
  687. */
  688. LineType *
  689. bwb_DSP (LineType * l)
  690. {
  691. /*
  692. SYNTAX: DSP
  693. SYNTAX: DSP variablename [,...]
  694. */
  695. VariableType *v;
  696. assert (l != NULL);
  697. assert( My != NULL );
  698. if (line_is_eol (l))
  699. {
  700. /* DSP */
  701. /* remove all variable displays */
  702. for (v = My->VariableHead; v != NULL; v = v->next)
  703. {
  704. v->VariableFlags &= ~VARIABLE_DISPLAY; /* bwb_DSP() */
  705. }
  706. return (l);
  707. }
  708. /* DSP variablename [,...] */
  709. do
  710. {
  711. char varname[NameLengthMax + 1];
  712. if (line_read_varname (l, varname))
  713. {
  714. /* mark the variable */
  715. for (v = My->VariableHead; v != NULL; v = v->next)
  716. {
  717. if (bwb_stricmp (v->name, varname) == 0)
  718. {
  719. v->VariableFlags |= VARIABLE_DISPLAY; /* bwb_DSP() */
  720. }
  721. }
  722. }
  723. }
  724. while (line_skip_seperator (l));
  725. return (l);
  726. }
  727. /*
  728. --------------------------------------------------------------------------------------------
  729. GOTO
  730. --------------------------------------------------------------------------------------------
  731. */
  732. LineType *
  733. bwb_GO_TO (LineType * l)
  734. {
  735. assert (l != NULL);
  736. return bwb_GOTO (l);
  737. }
  738. LineType *
  739. bwb_GOTO (LineType * l)
  740. {
  741. /*
  742. SYNTAX: GOTO line ' standard GOTO
  743. SYNTAX: GOTO expression ' calculated GOTO
  744. SYNTAX: GOTO expression OF line,... ' indexed GOTO, same as ON expression GOTO line,...
  745. SYNTAX: GOTO line [,...] ON expression ' indexed GOTO, same as ON expression GOTO line,...
  746. */
  747. int Value;
  748. int LineNumber;
  749. LineType *x;
  750. assert (l != NULL);
  751. assert( My != NULL );
  752. assert( My->CurrentVersion != NULL );
  753. Value = 0;
  754. LineNumber = 0;
  755. if (l->LineFlags & (LINE_USER))
  756. {
  757. WARN_ILLEGAL_DIRECT;
  758. return (l);
  759. }
  760. if (line_is_eol (l))
  761. {
  762. WARN_SYNTAX_ERROR;
  763. return (l);
  764. }
  765. if (line_read_integer_expression (l, &Value) == FALSE)
  766. {
  767. WARN_SYNTAX_ERROR;
  768. return (l);
  769. }
  770. if (line_is_eol (l))
  771. {
  772. /* GOTO linenumber */
  773. /* 'Value' is the line number */
  774. LineNumber = Value;
  775. }
  776. else if (line_skip_word (l, "OF"))
  777. {
  778. /* GOTO expression OF line, ... */
  779. /* 'Value' is an index into a list of line numbers */
  780. if (line_read_index_item (l, Value, &LineNumber))
  781. {
  782. /* found 'LineNumber' */
  783. }
  784. else if (My->CurrentVersion->OptionFlags & (OPTION_BUGS_ON) ) /* GOTO X OF ... */
  785. {
  786. /* silently fall-thru to the following line */
  787. line_skip_eol (l);
  788. return (l);
  789. }
  790. else
  791. {
  792. /* ERROR */
  793. WARN_UNDEFINED_LINE;
  794. return (l);
  795. }
  796. }
  797. else if (My->CurrentVersion->OptionVersionValue & (S70 | I70 | I73))
  798. {
  799. /* GOTO line [,...] ON expression */
  800. while (line_skip_seperator (l))
  801. {
  802. if (line_read_integer_expression (l, &Value) == FALSE)
  803. {
  804. WARN_SYNTAX_ERROR;
  805. return (l);
  806. }
  807. }
  808. if (line_skip_word (l, "ON") == FALSE)
  809. {
  810. WARN_SYNTAX_ERROR;
  811. return (l);
  812. }
  813. if (line_read_integer_expression (l, &Value) == FALSE)
  814. {
  815. WARN_SYNTAX_ERROR;
  816. return (l);
  817. }
  818. /* 'Value' is an index into a list of line numbers */
  819. l->position = l->Startpos;
  820. if (line_read_index_item (l, Value, &LineNumber))
  821. {
  822. /* found 'LineNumber' */
  823. }
  824. else
  825. {
  826. /* silently fall-thru to the following line */
  827. line_skip_eol (l);
  828. return (l);
  829. }
  830. line_skip_eol (l);
  831. }
  832. else
  833. {
  834. WARN_SYNTAX_ERROR;
  835. return (l);
  836. }
  837. if (LineNumber < MINLIN || LineNumber > MAXLIN)
  838. {
  839. WARN_UNDEFINED_LINE;
  840. return (l);
  841. }
  842. /* valid range */
  843. x = NULL;
  844. #if THE_PRICE_IS_RIGHT
  845. if (l->OtherLine != NULL)
  846. {
  847. /* look in the cache */
  848. if (l->OtherLine->number == LineNumber)
  849. {
  850. x = l->OtherLine; /* found in cache */
  851. }
  852. }
  853. #endif /* THE_PRICE_IS_RIGHT */
  854. if (x == NULL)
  855. {
  856. x = find_line_number (LineNumber); /* not found in the cache */
  857. }
  858. if (x != NULL)
  859. {
  860. /* FOUND */
  861. line_skip_eol (l);
  862. x->position = 0;
  863. #if THE_PRICE_IS_RIGHT
  864. l->OtherLine = x; /* save in cache */
  865. #endif /* THE_PRICE_IS_RIGHT */
  866. return x;
  867. }
  868. /* NOT FOUND */
  869. WARN_UNDEFINED_LINE;
  870. return (l);
  871. }
  872. /*
  873. --------------------------------------------------------------------------------------------
  874. GOSUB
  875. --------------------------------------------------------------------------------------------
  876. */
  877. LineType *
  878. bwb_GO_SUB (LineType * l)
  879. {
  880. assert (l != NULL);
  881. return bwb_GOSUB (l);
  882. }
  883. LineType *
  884. bwb_GOSUB (LineType * l)
  885. {
  886. /*
  887. SYNTAX: GOSUB line ' standard GOSUB
  888. SYNTAX: GOSUB expression ' calculated GOSUB
  889. SYNTAX: GOSUB expression OF line,... ' indexed GOSUB, same as ON expression GOSUB line,...
  890. SYNTAX: GOSUB line [,...] ON expression ' indexed GOSUB, same as ON expression GOSUB line,...
  891. */
  892. int Value;
  893. int LineNumber;
  894. LineType *x;
  895. assert (l != NULL);
  896. assert( My != NULL );
  897. assert( My->CurrentVersion != NULL );
  898. Value = 0;
  899. LineNumber = 0;
  900. x = NULL;
  901. if (l->LineFlags & (LINE_USER))
  902. {
  903. WARN_ILLEGAL_DIRECT;
  904. return (l);
  905. }
  906. if (line_is_eol (l))
  907. {
  908. WARN_SYNTAX_ERROR;
  909. return (l);
  910. }
  911. if (line_read_integer_expression (l, &Value) == FALSE)
  912. {
  913. WARN_SYNTAX_ERROR;
  914. return (l);
  915. }
  916. if (line_is_eol (l))
  917. {
  918. /* GOSUB linenumber */
  919. /* 'Value' is the line number */
  920. LineNumber = Value;
  921. }
  922. else if (line_skip_word (l, "OF"))
  923. {
  924. /* GOSUB linenumber [,...] OF expression */
  925. /* 'Value' is an index into a list of line numbers */
  926. if (line_read_index_item (l, Value, &LineNumber))
  927. {
  928. /* found 'LineNumber' */
  929. }
  930. else if (My->CurrentVersion->OptionFlags & (OPTION_BUGS_ON) ) /* GOSUB X OF ... */
  931. {
  932. /* silently fall-thru to the following line */
  933. line_skip_eol (l);
  934. return (l);
  935. }
  936. else
  937. {
  938. /* ERROR */
  939. WARN_UNDEFINED_LINE;
  940. return (l);
  941. }
  942. }
  943. else if (My->CurrentVersion->OptionVersionValue & (S70 | I70 | I73))
  944. {
  945. /* GOSUB line [,...] ON expression */
  946. while (line_skip_seperator (l))
  947. {
  948. if (line_read_integer_expression (l, &Value) == FALSE)
  949. {
  950. WARN_SYNTAX_ERROR;
  951. return (l);
  952. }
  953. }
  954. if (line_skip_word (l, "ON") == FALSE)
  955. {
  956. WARN_SYNTAX_ERROR;
  957. return (l);
  958. }
  959. if (line_read_integer_expression (l, &Value) == FALSE)
  960. {
  961. WARN_SYNTAX_ERROR;
  962. return (l);
  963. }
  964. /* 'Value' is an index into a list of line numbers */
  965. l->position = l->Startpos;
  966. if (line_read_index_item (l, Value, &LineNumber))
  967. {
  968. /* found 'LineNumber' */
  969. }
  970. else
  971. {
  972. /* silently fall-thru to the following line */
  973. line_skip_eol (l);
  974. return (l);
  975. }
  976. line_skip_eol (l);
  977. }
  978. else
  979. {
  980. WARN_SYNTAX_ERROR;
  981. return (l);
  982. }
  983. if (LineNumber < MINLIN || LineNumber > MAXLIN)
  984. {
  985. WARN_UNDEFINED_LINE;
  986. return (l);
  987. }
  988. /* valid range */
  989. x = NULL;
  990. #if THE_PRICE_IS_RIGHT
  991. if (l->OtherLine != NULL)
  992. {
  993. /* look in the cache */
  994. if (l->OtherLine->number == LineNumber)
  995. {
  996. x = l->OtherLine; /* found in cache */
  997. }
  998. }
  999. #endif /* THE_PRICE_IS_RIGHT */
  1000. if (x == NULL)
  1001. {
  1002. x = find_line_number (LineNumber); /* not found in the cache */
  1003. }
  1004. if (x != NULL)
  1005. {
  1006. /* FOUND */
  1007. line_skip_eol (l);
  1008. /* save current stack level */
  1009. My->StackHead->line = l;
  1010. /* increment exec stack */
  1011. if (bwb_incexec ())
  1012. {
  1013. /* set the new position to x and return x */
  1014. x->position = 0;
  1015. My->StackHead->line = x;
  1016. My->StackHead->ExecCode = EXEC_GOSUB;
  1017. #if THE_PRICE_IS_RIGHT
  1018. l->OtherLine = x; /* save in cache */
  1019. #endif /* THE_PRICE_IS_RIGHT */
  1020. return x;
  1021. }
  1022. else
  1023. {
  1024. /* ERROR */
  1025. WARN_OUT_OF_MEMORY;
  1026. return My->EndMarker;
  1027. }
  1028. }
  1029. /* NOT FOUND */
  1030. WARN_UNDEFINED_LINE;
  1031. return (l);
  1032. }
  1033. /*
  1034. --------------------------------------------------------------------------------------------
  1035. RETURN
  1036. --------------------------------------------------------------------------------------------
  1037. */
  1038. LineType *
  1039. bwb_RETURN (LineType * l)
  1040. {
  1041. /*
  1042. SYNTAX: RETURN
  1043. */
  1044. assert (l != NULL);
  1045. assert (My != NULL);
  1046. assert (My->CurrentVersion != NULL);
  1047. assert (My->StackHead != NULL);
  1048. if (My->CurrentVersion->OptionVersionValue & (S70 | I70 | I73))
  1049. {
  1050. /* RETURN [comment] */
  1051. line_skip_eol (l);
  1052. }
  1053. if (My->CurrentVersion->OptionVersionValue & (C77))
  1054. {
  1055. /* CBASIC-II: RETURN exits the first FUNCTION or GOSUB */
  1056. while (My->StackHead->ExecCode != EXEC_GOSUB
  1057. && My->StackHead->ExecCode != EXEC_FUNCTION)
  1058. {
  1059. bwb_decexec ();
  1060. if (My->StackHead == NULL)
  1061. {
  1062. WARN_RETURN_WITHOUT_GOSUB;
  1063. return (l);
  1064. }
  1065. if (My->StackHead->ExecCode == EXEC_NORM) /* End of the line? */
  1066. {
  1067. WARN_RETURN_WITHOUT_GOSUB;
  1068. return (l);
  1069. }
  1070. }
  1071. }
  1072. else
  1073. {
  1074. /* RETURN exits the first GOSUB */
  1075. while (My->StackHead->ExecCode != EXEC_GOSUB)
  1076. {
  1077. bwb_decexec ();
  1078. if (My->StackHead == NULL)
  1079. {
  1080. WARN_RETURN_WITHOUT_GOSUB;
  1081. return (l);
  1082. }
  1083. if (My->StackHead->ExecCode == EXEC_NORM) /* End of the line? */
  1084. {
  1085. WARN_RETURN_WITHOUT_GOSUB;
  1086. return (l);
  1087. }
  1088. }
  1089. }
  1090. /* decrement the EXEC stack counter */
  1091. bwb_decexec ();
  1092. assert (My->StackHead != NULL);
  1093. return My->StackHead->line;
  1094. }
  1095. /*
  1096. --------------------------------------------------------------------------------------------
  1097. POP
  1098. --------------------------------------------------------------------------------------------
  1099. */
  1100. LineType *
  1101. bwb_POP (LineType * l)
  1102. {
  1103. /*
  1104. SYNTAX: POP
  1105. */
  1106. StackType *StackItem;
  1107. assert (l != NULL);
  1108. assert (My != NULL);
  1109. assert (My->CurrentVersion != NULL);
  1110. assert (My->StackHead != NULL);
  1111. StackItem = My->StackHead;
  1112. while (StackItem->ExecCode != EXEC_GOSUB)
  1113. {
  1114. StackItem = StackItem->next;
  1115. if (StackItem == NULL)
  1116. {
  1117. WARN_RETURN_WITHOUT_GOSUB;
  1118. return (l);
  1119. }
  1120. if (StackItem->ExecCode == EXEC_NORM)
  1121. {
  1122. /* End of the line */
  1123. WARN_RETURN_WITHOUT_GOSUB;
  1124. return (l);
  1125. }
  1126. }
  1127. /* hide the GOSUB */
  1128. StackItem->ExecCode = EXEC_POPPED;
  1129. return (l);
  1130. }
  1131. /*
  1132. --------------------------------------------------------------------------------------------
  1133. ON
  1134. --------------------------------------------------------------------------------------------
  1135. */
  1136. LineType *
  1137. bwb_ON (LineType * l)
  1138. {
  1139. /*
  1140. SYNTAX: ON expression GOTO line,... ' expression evaluates to an index
  1141. SYNTAX: ON expression GOSUB line,... ' expression evaluates to an index
  1142. */
  1143. int Value;
  1144. int command;
  1145. int LineNumber;
  1146. LineType *x;
  1147. assert (l != NULL);
  1148. assert (My != NULL);
  1149. assert (My->CurrentVersion != NULL);
  1150. Value = 0;
  1151. command = 0;
  1152. LineNumber = 0;
  1153. x = NULL;
  1154. if (l->LineFlags & (LINE_USER))
  1155. {
  1156. WARN_ILLEGAL_DIRECT;
  1157. return (l);
  1158. }
  1159. if (line_is_eol (l))
  1160. {
  1161. WARN_SYNTAX_ERROR;
  1162. return (l);
  1163. }
  1164. if (line_read_integer_expression (l, &Value) == FALSE)
  1165. {
  1166. WARN_UNDEFINED_LINE;
  1167. return (l);
  1168. }
  1169. if (line_skip_word (l, "GO"))
  1170. {
  1171. if (line_skip_word (l, "TO"))
  1172. {
  1173. command = C_GOTO;
  1174. }
  1175. else if (line_skip_word (l, "SUB"))
  1176. {
  1177. command = C_GOSUB;
  1178. }
  1179. else
  1180. {
  1181. WARN_SYNTAX_ERROR;
  1182. return (l);
  1183. }
  1184. }
  1185. else if (line_skip_word (l, "GOTO"))
  1186. {
  1187. command = C_GOTO;
  1188. }
  1189. else if (line_skip_word (l, "GOSUB"))
  1190. {
  1191. command = C_GOSUB;
  1192. }
  1193. else
  1194. {
  1195. WARN_SYNTAX_ERROR;
  1196. return (l);
  1197. }
  1198. /* 'Value' is an index into a list of line numbers */
  1199. if (line_read_index_item (l, Value, &LineNumber))
  1200. {
  1201. /* found 'LineNumber' */
  1202. }
  1203. else if (My->CurrentVersion->OptionFlags & (OPTION_BUGS_ON) ) /* ON X GOTO|GOSUB ... */
  1204. {
  1205. /* silently fall-thru to the following line */
  1206. line_skip_eol (l);
  1207. return (l);
  1208. }
  1209. else
  1210. {
  1211. /* ERROR */
  1212. WARN_UNDEFINED_LINE;
  1213. return (l);
  1214. }
  1215. if (LineNumber < MINLIN || LineNumber > MAXLIN)
  1216. {
  1217. WARN_UNDEFINED_LINE;
  1218. return (l);
  1219. }
  1220. /* valid range */
  1221. x = NULL;
  1222. #if THE_PRICE_IS_RIGHT
  1223. if (l->OtherLine != NULL)
  1224. {
  1225. /* look in the cache */
  1226. if (l->OtherLine->number == LineNumber)
  1227. {
  1228. x = l->OtherLine; /* found in cache */
  1229. }
  1230. }
  1231. #endif /* THE_PRICE_IS_RIGHT */
  1232. if (x == NULL)
  1233. {
  1234. x = find_line_number (LineNumber); /* not found in the cache */
  1235. }
  1236. if (x != NULL)
  1237. {
  1238. /* FOUND */
  1239. if (command == C_GOTO)
  1240. {
  1241. /* ON ... GOTO ... */
  1242. line_skip_eol (l);
  1243. x->position = 0;
  1244. #if THE_PRICE_IS_RIGHT
  1245. l->OtherLine = x; /* save in cache */
  1246. #endif /* THE_PRICE_IS_RIGHT */
  1247. return x;
  1248. }
  1249. else if (command == C_GOSUB)
  1250. {
  1251. /* ON ... GOSUB ... */
  1252. line_skip_eol (l);
  1253. /* save current stack level */
  1254. My->StackHead->line = l;
  1255. /* increment exec stack */
  1256. if (bwb_incexec ())
  1257. {
  1258. /* set the new position to x and return x */
  1259. x->position = 0;
  1260. My->StackHead->line = x;
  1261. My->StackHead->ExecCode = EXEC_GOSUB;
  1262. #if THE_PRICE_IS_RIGHT
  1263. l->OtherLine = x; /* save in cache */
  1264. #endif /* THE_PRICE_IS_RIGHT */
  1265. return x;
  1266. }
  1267. else
  1268. {
  1269. /* ERROR */
  1270. WARN_OUT_OF_MEMORY;
  1271. return My->EndMarker;
  1272. }
  1273. }
  1274. else
  1275. {
  1276. /* ERROR */
  1277. WARN_SYNTAX_ERROR;
  1278. return (l);
  1279. }
  1280. }
  1281. /* NOT FOUND */
  1282. WARN_UNDEFINED_LINE;
  1283. return (l);
  1284. }
  1285. /*
  1286. --------------------------------------------------------------------------------------------
  1287. PAUSE
  1288. --------------------------------------------------------------------------------------------
  1289. */
  1290. LineType *
  1291. bwb_PAUSE (LineType * l)
  1292. {
  1293. /*
  1294. SYNTAX: PAUSE
  1295. */
  1296. char *pstring;
  1297. char *tbuf;
  1298. int tlen;
  1299. assert (l != NULL);
  1300. assert (My != NULL);
  1301. assert (My->CurrentVersion != NULL);
  1302. assert (My->ConsoleOutput != NULL);
  1303. assert (My->ConsoleInput != NULL);
  1304. pstring = My->ConsoleOutput;
  1305. tbuf = My->ConsoleInput;
  1306. tlen = MAX_LINE_LENGTH;
  1307. if (My->CurrentVersion->OptionVersionValue & (S70 | I70 | I73))
  1308. {
  1309. /* PAUSE [comment] */
  1310. line_skip_eol (l);
  1311. }
  1312. sprintf (pstring, "PAUSE AT %d\n", l->number);
  1313. bwx_input (pstring, FALSE, tbuf, tlen);
  1314. return (l);
  1315. }
  1316. /*
  1317. --------------------------------------------------------------------------------------------
  1318. STOP
  1319. --------------------------------------------------------------------------------------------
  1320. */
  1321. LineType *
  1322. bwb_STOP (LineType * l)
  1323. {
  1324. /*
  1325. SYNTAX: STOP
  1326. */
  1327. assert (l != NULL);
  1328. assert (My != NULL);
  1329. assert (My->CurrentVersion != NULL);
  1330. if (My->CurrentVersion->OptionVersionValue & (S70 | I70 | I73))
  1331. {
  1332. /* STOP [comment] */
  1333. line_skip_eol (l);
  1334. }
  1335. My->ContinueLine = l->next;
  1336. bwx_STOP (TRUE);
  1337. return bwb_END (l);
  1338. }
  1339. /*
  1340. --------------------------------------------------------------------------------------------
  1341. END
  1342. --------------------------------------------------------------------------------------------
  1343. */
  1344. LineType *
  1345. bwb_END (LineType * l)
  1346. {
  1347. /*
  1348. SYNTAX: END
  1349. */
  1350. assert (l != NULL);
  1351. assert (My != NULL);
  1352. assert (My->CurrentVersion != NULL);
  1353. if (My->CurrentVersion->OptionVersionValue & (S70 | I70 | I73))
  1354. {
  1355. /* END [comment] */
  1356. line_skip_eol (l);
  1357. }
  1358. My->ContinueLine = l->next;
  1359. bwx_STOP (FALSE);
  1360. return My->EndMarker;
  1361. }
  1362. /*
  1363. --------------------------------------------------------------------------------------------
  1364. RUN
  1365. --------------------------------------------------------------------------------------------
  1366. */
  1367. static LineType *
  1368. bwb_run_filename_or_linenumber (LineType * L)
  1369. {
  1370. LineType *current = NULL;
  1371. VariantType x;
  1372. VariantType *X;
  1373. assert (L != NULL);
  1374. assert (My != NULL);
  1375. assert (My->StartMarker != NULL);
  1376. X = &x;
  1377. CLEAR_VARIANT (X);
  1378. if (line_read_expression (L, X) == FALSE) /* bwb_run_filename_or_linenumber */
  1379. {
  1380. WARN_SYNTAX_ERROR;
  1381. return L;
  1382. }
  1383. if (X->VariantTypeCode == StringTypeCode)
  1384. {
  1385. /* RUN "filename" */
  1386. /* RUN A$ */
  1387. if (is_empty_string (X->Buffer))
  1388. {
  1389. WARN_BAD_FILE_NAME;
  1390. return L;
  1391. }
  1392. /* open the file and execute it */
  1393. bwb_new (); /* clear memory */
  1394. if (My->ProgramFilename != NULL)
  1395. {
  1396. free (My->ProgramFilename);
  1397. My->ProgramFilename = NULL;
  1398. }
  1399. My->ProgramFilename = bwb_strdup (X->Buffer);
  1400. if (bwb_fload (NULL) == FALSE)
  1401. {
  1402. WARN_BAD_FILE_NAME;
  1403. return L;
  1404. }
  1405. /*
  1406. **
  1407. ** FORCE SCAN
  1408. **
  1409. */
  1410. if (bwb_scan () == FALSE)
  1411. {
  1412. WARN_CANT_CONTINUE;
  1413. return L;
  1414. }
  1415. current = My->StartMarker->next;
  1416. }
  1417. else
  1418. {
  1419. /* RUN 100 */
  1420. /* RUN N */
  1421. /* execute the line */
  1422. int LineNumber;
  1423. LineNumber = (int) bwb_rint (X->Number);
  1424. /*
  1425. **
  1426. ** FORCE SCAN
  1427. **
  1428. */
  1429. if (bwb_scan () == FALSE)
  1430. {
  1431. WARN_CANT_CONTINUE;
  1432. goto EXIT;
  1433. }
  1434. current = find_line_number (LineNumber); /* RUN 100 */
  1435. if (current == NULL)
  1436. {
  1437. WARN_CANT_CONTINUE;
  1438. return L;
  1439. }
  1440. }
  1441. EXIT:
  1442. RELEASE_VARIANT (X);
  1443. return current;
  1444. }
  1445. LineType *
  1446. bwb_RUNNH (LineType * L)
  1447. {
  1448. assert (L != NULL);
  1449. return bwb_RUN (L);
  1450. }
  1451. LineType *
  1452. bwb_RUN (LineType * L)
  1453. {
  1454. /*
  1455. SYNTAX: RUN
  1456. SYNTAX: RUN filename$
  1457. SYNTAX: RUN linenumber
  1458. */
  1459. LineType *current;
  1460. assert (L != NULL);
  1461. assert (My != NULL);
  1462. assert (My->EndMarker != NULL);
  1463. assert (My->DefaultVariableType != NULL);
  1464. /* clear the STACK */
  1465. bwb_clrexec ();
  1466. if (bwb_incexec ())
  1467. {
  1468. /* OK */
  1469. }
  1470. else
  1471. {
  1472. /* ERROR */
  1473. WARN_OUT_OF_MEMORY;
  1474. return My->EndMarker;
  1475. }
  1476. if (line_is_eol (L))
  1477. {
  1478. /* RUN */
  1479. var_CLEAR ();
  1480. /* if( TRUE ) */
  1481. {
  1482. int n;
  1483. for (n = 0; n < 26; n++)
  1484. {
  1485. My->DefaultVariableType[n] = DoubleTypeCode;
  1486. }
  1487. }
  1488. /*
  1489. **
  1490. ** FORCE SCAN
  1491. **
  1492. */
  1493. if (bwb_scan () == FALSE)
  1494. {
  1495. WARN_CANT_CONTINUE;
  1496. return My->EndMarker;
  1497. }
  1498. current = My->StartMarker->next;
  1499. }
  1500. else
  1501. {
  1502. /* RUN 100 : RUN filename$ */
  1503. current = bwb_run_filename_or_linenumber (L);
  1504. if (current == NULL)
  1505. {
  1506. WARN_UNDEFINED_LINE;
  1507. return My->EndMarker;
  1508. }
  1509. }
  1510. current->position = 0;
  1511. assert (My->StackHead != NULL);
  1512. My->StackHead->line = current;
  1513. My->StackHead->ExecCode = EXEC_NORM;
  1514. /* RUN */
  1515. WARN_CLEAR; /* bwb_RUN */
  1516. My->ContinueLine = NULL;
  1517. SetOnError (0);
  1518. /* if( TRUE ) */
  1519. {
  1520. time_t t;
  1521. struct tm *lt;
  1522. time (&t);
  1523. lt = localtime (&t);
  1524. My->StartTimeInteger = lt->tm_hour;
  1525. My->StartTimeInteger *= 60;
  1526. My->StartTimeInteger += lt->tm_min;
  1527. My->StartTimeInteger *= 60;
  1528. My->StartTimeInteger += lt->tm_sec;
  1529. /* number of seconds since midnight */
  1530. }
  1531. return current;
  1532. }
  1533. /*
  1534. --------------------------------------------------------------------------------------------
  1535. CONT
  1536. --------------------------------------------------------------------------------------------
  1537. */
  1538. LineType *
  1539. bwb_CONTINUE (LineType * l)
  1540. {
  1541. /*
  1542. SYNTAX: CONTINUE
  1543. */
  1544. assert (l != NULL);
  1545. return bwb_CONT (l);
  1546. }
  1547. LineType *
  1548. bwb_CONT (LineType * l)
  1549. {
  1550. /*
  1551. SYNTAX: CONT
  1552. */
  1553. LineType *current;
  1554. assert (l != NULL);
  1555. assert (My != NULL);
  1556. assert (My->EndMarker != NULL);
  1557. assert (My->StartMarker != NULL);
  1558. current = NULL;
  1559. /* see if there is an element */
  1560. if (line_is_eol (l))
  1561. {
  1562. /* CONT */
  1563. current = My->ContinueLine;
  1564. }
  1565. else
  1566. {
  1567. /* CONT 100 */
  1568. int LineNumber;
  1569. LineNumber = 0;
  1570. if (line_read_line_number (l, &LineNumber))
  1571. {
  1572. current = find_line_number (LineNumber); /* CONT 100 */
  1573. }
  1574. }
  1575. if (current == NULL || current == My->EndMarker)
  1576. {
  1577. /* same as RUN */
  1578. current = My->StartMarker->next;
  1579. }
  1580. /*
  1581. **
  1582. ** FORCE SCAN
  1583. **
  1584. */
  1585. if (bwb_scan () == FALSE)
  1586. {
  1587. WARN_CANT_CONTINUE;
  1588. return (l);
  1589. }
  1590. current->position = 0;
  1591. bwb_clrexec ();
  1592. if (bwb_incexec ())
  1593. {
  1594. /* OK */
  1595. My->StackHead->line = current;
  1596. My->StackHead->ExecCode = EXEC_NORM;
  1597. }
  1598. else
  1599. {
  1600. /* ERROR */
  1601. WARN_OUT_OF_MEMORY;
  1602. return My->EndMarker;
  1603. }
  1604. /* CONT */
  1605. My->ContinueLine = NULL;
  1606. return current;
  1607. }
  1608. /*
  1609. --------------------------------------------------------------------------------------------
  1610. NEW
  1611. --------------------------------------------------------------------------------------------
  1612. */
  1613. void
  1614. bwb_xnew (LineType * l)
  1615. {
  1616. LineType *current;
  1617. LineType *previous;
  1618. int wait;
  1619. assert (l != NULL);
  1620. assert (My != NULL);
  1621. assert (My->EndMarker != NULL);
  1622. previous = NULL; /* JBV */
  1623. wait = TRUE;
  1624. for (current = l->next; current != My->EndMarker; current = current->next)
  1625. {
  1626. assert (current != NULL);
  1627. if (wait == FALSE)
  1628. {
  1629. free (previous);
  1630. previous = NULL;
  1631. }
  1632. wait = FALSE;
  1633. previous = current;
  1634. }
  1635. l->next = My->EndMarker;
  1636. }
  1637. static void
  1638. bwb_new ()
  1639. {
  1640. assert (My != NULL);
  1641. assert (My->StartMarker != NULL);
  1642. assert (My->DefaultVariableType != NULL);
  1643. /* clear program in memory */
  1644. bwb_xnew (My->StartMarker);
  1645. /* clear all variables */
  1646. var_CLEAR ();
  1647. /* if( TRUE ) */
  1648. {
  1649. int n;
  1650. for (n = 0; n < 26; n++)
  1651. {
  1652. My->DefaultVariableType[n] = DoubleTypeCode;
  1653. }
  1654. }
  1655. /* NEW */
  1656. WARN_CLEAR; /* bwb_new */
  1657. My->ContinueLine = NULL;
  1658. SetOnError (0);
  1659. }
  1660. LineType *
  1661. bwb_NEW (LineType * l)
  1662. {
  1663. /*
  1664. SYNTAX: NEW
  1665. */
  1666. assert (l != NULL);
  1667. assert (My != NULL);
  1668. assert (My->CurrentVersion != NULL);
  1669. bwb_new ();
  1670. if (My->CurrentVersion->OptionVersionValue & (D64 | G65 | G67 | G74))
  1671. {
  1672. if (line_is_eol (l))
  1673. {
  1674. /* NEW */
  1675. char *tbuf;
  1676. int tlen;
  1677. tbuf = My->ConsoleInput;
  1678. tlen = MAX_LINE_LENGTH;
  1679. /* prompt for the program name */
  1680. bwx_input ("NEW PROBLEM NAME:", FALSE, tbuf, tlen);
  1681. if (is_empty_string (tbuf))
  1682. {
  1683. WARN_BAD_FILE_NAME;
  1684. return l;
  1685. }
  1686. if (My->ProgramFilename != NULL)
  1687. {
  1688. free (My->ProgramFilename);
  1689. My->ProgramFilename = NULL;
  1690. }
  1691. My->ProgramFilename = bwb_strdup (tbuf);
  1692. }
  1693. else
  1694. {
  1695. /* NEW filename$ */
  1696. /* the parameter is the program name */
  1697. char *Value;
  1698. Value = NULL;
  1699. if (line_read_string_expression (l, &Value) == FALSE)
  1700. {
  1701. WARN_SYNTAX_ERROR;
  1702. return (l);
  1703. }
  1704. if (is_empty_string (Value))
  1705. {
  1706. WARN_BAD_FILE_NAME;
  1707. return l;
  1708. }
  1709. if (My->ProgramFilename != NULL)
  1710. {
  1711. free (My->ProgramFilename);
  1712. My->ProgramFilename = NULL;
  1713. }
  1714. My->ProgramFilename = Value;
  1715. }
  1716. }
  1717. else
  1718. {
  1719. /* ignore any parameters */
  1720. line_skip_eol (l);
  1721. }
  1722. return (l);
  1723. }
  1724. /*
  1725. --------------------------------------------------------------------------------------------
  1726. SCRATCH
  1727. --------------------------------------------------------------------------------------------
  1728. */
  1729. LineType *
  1730. bwb_SCRATCH (LineType * l)
  1731. {
  1732. /*
  1733. SYNTAX: SCRATCH -- same as NEW
  1734. SYNTAX: SCRATCH # filenumber -- close file and re-open for output
  1735. */
  1736. assert (l != NULL);
  1737. if (line_is_eol (l))
  1738. {
  1739. /* SCRATCH */
  1740. bwb_new ();
  1741. return (l);
  1742. }
  1743. if (line_skip_FilenumChar (l))
  1744. {
  1745. /* SCRATCH # X */
  1746. int FileNumber;
  1747. if (line_read_integer_expression (l, &FileNumber) == FALSE)
  1748. {
  1749. WARN_BAD_FILE_NUMBER;
  1750. return (l);
  1751. }
  1752. if (FileNumber < 0)
  1753. {
  1754. /* SCRATCH # -1 is silently ignored */
  1755. return (l);
  1756. }
  1757. if (FileNumber == 0)
  1758. {
  1759. /* SCRATCH # 0 is silently ignored */
  1760. return (l);
  1761. }
  1762. My->CurrentFile = find_file_by_number (FileNumber);
  1763. if (My->CurrentFile == NULL)
  1764. {
  1765. WARN_BAD_FILE_NUMBER;
  1766. return (l);
  1767. }
  1768. if (My->CurrentFile->DevMode != DEVMODE_CLOSED)
  1769. {
  1770. My->CurrentFile->DevMode = DEVMODE_CLOSED;
  1771. }
  1772. if (My->CurrentFile->cfp != NULL)
  1773. {
  1774. bwb_fclose (My->CurrentFile->cfp);
  1775. My->CurrentFile->cfp = NULL;
  1776. }
  1777. if (My->CurrentFile->buffer != NULL)
  1778. {
  1779. free (My->CurrentFile->buffer);
  1780. My->CurrentFile->buffer = NULL;
  1781. }
  1782. My->CurrentFile->width = 0;
  1783. My->CurrentFile->col = 1;
  1784. My->CurrentFile->row = 1;
  1785. My->CurrentFile->delimit = ',';
  1786. if (is_empty_string (My->CurrentFile->FileName))
  1787. {
  1788. WARN_BAD_FILE_NAME;
  1789. return (l);
  1790. }
  1791. if (bwb_strcmp (My->CurrentFile->FileName, "*") != 0)
  1792. {
  1793. if ((My->CurrentFile->cfp =
  1794. fopen (My->CurrentFile->FileName, "w")) == NULL)
  1795. {
  1796. WARN_BAD_FILE_NAME;
  1797. return (l);
  1798. }
  1799. My->CurrentFile->DevMode = DEVMODE_OUTPUT;
  1800. }
  1801. /* OK */
  1802. return (l);
  1803. }
  1804. WARN_SYNTAX_ERROR;
  1805. return (l);
  1806. }
  1807. /*
  1808. ============================================================================================
  1809. SYSTEM and so on
  1810. ============================================================================================
  1811. */
  1812. static LineType *
  1813. bwb_system (LineType * l)
  1814. {
  1815. /*
  1816. SYNTAX: SYSTEM
  1817. */
  1818. assert (l != NULL);
  1819. assert (My != NULL);
  1820. assert (My->SYSOUT != NULL);
  1821. assert (My->SYSOUT->cfp != NULL);
  1822. fprintf (My->SYSOUT->cfp, "\n");
  1823. fflush (My->SYSOUT->cfp);
  1824. bwx_terminate ();
  1825. return (l); /* never reached */
  1826. }
  1827. /*
  1828. --------------------------------------------------------------------------------------------
  1829. BYE
  1830. --------------------------------------------------------------------------------------------
  1831. */
  1832. LineType *
  1833. bwb_BYE (LineType * l)
  1834. {
  1835. /*
  1836. SYNTAX: BYE
  1837. */
  1838. assert (l != NULL);
  1839. return bwb_system (l);
  1840. }
  1841. /*
  1842. --------------------------------------------------------------------------------------------
  1843. DOS
  1844. --------------------------------------------------------------------------------------------
  1845. */
  1846. LineType *
  1847. bwb_DOS (LineType * l)
  1848. {
  1849. /*
  1850. SYNTAX: DOS
  1851. */
  1852. assert (l != NULL);
  1853. return bwb_system (l);
  1854. }
  1855. /*
  1856. --------------------------------------------------------------------------------------------
  1857. FLEX
  1858. --------------------------------------------------------------------------------------------
  1859. */
  1860. LineType *
  1861. bwb_FLEX (LineType * l)
  1862. {
  1863. /*
  1864. SYNTAX: FLEX
  1865. */
  1866. assert (l != NULL);
  1867. return bwb_system (l);
  1868. }
  1869. /*
  1870. --------------------------------------------------------------------------------------------
  1871. GOODBYE
  1872. --------------------------------------------------------------------------------------------
  1873. */
  1874. LineType *
  1875. bwb_GOODBYE (LineType * l)
  1876. {
  1877. /*
  1878. SYNTAX: GOODBYE
  1879. */
  1880. assert (l != NULL);
  1881. return bwb_system (l);
  1882. }
  1883. /*
  1884. --------------------------------------------------------------------------------------------
  1885. MON
  1886. --------------------------------------------------------------------------------------------
  1887. */
  1888. LineType *
  1889. bwb_MON (LineType * l)
  1890. {
  1891. /*
  1892. SYNTAX: MON
  1893. */
  1894. assert (l != NULL);
  1895. return bwb_system (l);
  1896. }
  1897. /*
  1898. --------------------------------------------------------------------------------------------
  1899. QUIT
  1900. --------------------------------------------------------------------------------------------
  1901. */
  1902. LineType *
  1903. bwb_QUIT (LineType * l)
  1904. {
  1905. /*
  1906. SYNTAX: QUIT
  1907. */
  1908. assert (l != NULL);
  1909. return bwb_system (l);
  1910. }
  1911. /*
  1912. --------------------------------------------------------------------------------------------
  1913. SYSTEM
  1914. --------------------------------------------------------------------------------------------
  1915. */
  1916. LineType *
  1917. bwb_SYSTEM (LineType * l)
  1918. {
  1919. /*
  1920. SYNTAX: SYSTEM
  1921. */
  1922. assert (l != NULL);
  1923. return bwb_system (l);
  1924. }
  1925. /*
  1926. ============================================================================================
  1927. LOAD and so on
  1928. ============================================================================================
  1929. */
  1930. static LineType *
  1931. bwb_load (LineType * Line, char *Prompt, int IsNew)
  1932. {
  1933. /*
  1934. **
  1935. ** load a BASIC program from a file
  1936. **
  1937. */
  1938. /*
  1939. SYNTAX: ... [filename$]
  1940. */
  1941. assert (Line != NULL);
  1942. assert (Prompt != NULL);
  1943. assert (My != NULL);
  1944. assert (My->CurrentVersion != NULL);
  1945. if (IsNew)
  1946. {
  1947. /* TRUE == LOAD */
  1948. bwb_new ();
  1949. }
  1950. else
  1951. {
  1952. /* FALSE == MERGE */
  1953. if (My->ProgramFilename != NULL)
  1954. {
  1955. free (My->ProgramFilename);
  1956. My->ProgramFilename = NULL;
  1957. }
  1958. }
  1959. if (line_is_eol (Line))
  1960. {
  1961. /* default is the last filename used by LOAD or SAVE */
  1962. /* if( My->CurrentVersion->OptionVersionValue & (D64 | G65 | G67 | G74) ) */
  1963. if (is_empty_string (My->ProgramFilename))
  1964. {
  1965. /* prompt for the program name */
  1966. char *tbuf;
  1967. int tlen;
  1968. tbuf = My->ConsoleInput;
  1969. tlen = MAX_LINE_LENGTH;
  1970. bwx_input (Prompt, FALSE, tbuf, tlen);
  1971. if (is_empty_string (tbuf))
  1972. {
  1973. WARN_BAD_FILE_NAME;
  1974. return (Line);
  1975. }
  1976. if (My->ProgramFilename != NULL)
  1977. {
  1978. free (My->ProgramFilename);
  1979. My->ProgramFilename = NULL;
  1980. }
  1981. My->ProgramFilename = bwb_strdup (tbuf);
  1982. }
  1983. fprintf (My->SYSOUT->cfp, "Loading %s\n", My->ProgramFilename);
  1984. ResetConsoleColumn ();
  1985. }
  1986. else
  1987. {
  1988. /* Get an argument for filename */
  1989. char *Value;
  1990. Value = NULL;
  1991. if (line_read_string_expression (Line, &Value) == FALSE)
  1992. {
  1993. WARN_SYNTAX_ERROR;
  1994. return (Line);
  1995. }
  1996. if (is_empty_string (Value))
  1997. {
  1998. WARN_BAD_FILE_NAME;
  1999. return (Line);
  2000. }
  2001. if (My->ProgramFilename != NULL)
  2002. {
  2003. free (My->ProgramFilename);
  2004. My->ProgramFilename = NULL;
  2005. }
  2006. My->ProgramFilename = Value;
  2007. }
  2008. if (bwb_fload (NULL) == FALSE)
  2009. {
  2010. WARN_BAD_FILE_NAME;
  2011. return (Line);
  2012. }
  2013. if (IsNew)
  2014. {
  2015. /* TRUE == LOAD */
  2016. }
  2017. else
  2018. {
  2019. /* FALSE == MERGE */
  2020. if (My->ProgramFilename != NULL)
  2021. {
  2022. free (My->ProgramFilename);
  2023. My->ProgramFilename = NULL;
  2024. }
  2025. }
  2026. /*
  2027. **
  2028. ** FORCE SCAN
  2029. **
  2030. */
  2031. if (bwb_scan () == FALSE)
  2032. {
  2033. WARN_CANT_CONTINUE;
  2034. }
  2035. return (Line);
  2036. }
  2037. /*
  2038. --------------------------------------------------------------------------------------------
  2039. CLOAD
  2040. --------------------------------------------------------------------------------------------
  2041. */
  2042. LineType *
  2043. bwb_CLOAD (LineType * Line)
  2044. {
  2045. /*
  2046. SYNTAX: CLOAD [filename$]
  2047. */
  2048. assert (Line != NULL);
  2049. return bwb_load (Line, "CLOAD FILE NAME:", TRUE);
  2050. }
  2051. /*
  2052. --------------------------------------------------------------------------------------------
  2053. LOAD
  2054. --------------------------------------------------------------------------------------------
  2055. */
  2056. LineType *
  2057. bwb_LOAD (LineType * Line)
  2058. {
  2059. /*
  2060. SYNTAX: LOAD [filename$]
  2061. */
  2062. assert (Line != NULL);
  2063. return bwb_load (Line, "LOAD FILE NAME:", TRUE);
  2064. }
  2065. /*
  2066. --------------------------------------------------------------------------------------------
  2067. MERGE
  2068. --------------------------------------------------------------------------------------------
  2069. */
  2070. LineType *
  2071. bwb_MERGE (LineType * l)
  2072. {
  2073. /*
  2074. SYNTAX: MERGE [filename$]
  2075. */
  2076. assert (l != NULL);
  2077. return bwb_load (l, "MERGE FILE NAME:", FALSE);
  2078. }
  2079. /*
  2080. --------------------------------------------------------------------------------------------
  2081. OLD
  2082. --------------------------------------------------------------------------------------------
  2083. */
  2084. LineType *
  2085. bwb_OLD (LineType * Line)
  2086. {
  2087. /*
  2088. SYNTAX: OLD [filename$]
  2089. */
  2090. assert (Line != NULL);
  2091. return bwb_load (Line, "OLD PROBLEM NAME:", TRUE);
  2092. }
  2093. /*
  2094. --------------------------------------------------------------------------------------------
  2095. TLOAD
  2096. --------------------------------------------------------------------------------------------
  2097. */
  2098. LineType *
  2099. bwb_TLOAD (LineType * Line)
  2100. {
  2101. /*
  2102. SYNTAX: TLOAD [filename$]
  2103. */
  2104. assert (Line != NULL);
  2105. return bwb_load (Line, "TLOAD FILE NAME:", TRUE);
  2106. }
  2107. /*
  2108. --------------------------------------------------------------------------------------------
  2109. RENAME
  2110. --------------------------------------------------------------------------------------------
  2111. */
  2112. static LineType *
  2113. H14_RENAME (LineType * l)
  2114. {
  2115. /*
  2116. SYNTAX: RENAME from$ TO to$
  2117. */
  2118. char *From;
  2119. char *To;
  2120. assert (l != NULL);
  2121. From = NULL;
  2122. To = NULL;
  2123. if (line_read_string_expression (l, &From) == FALSE)
  2124. {
  2125. WARN_SYNTAX_ERROR;
  2126. return (l);
  2127. }
  2128. if (is_empty_string (From))
  2129. {
  2130. WARN_BAD_FILE_NAME;
  2131. return (l);
  2132. }
  2133. if (line_skip_word (l, "TO") == FALSE)
  2134. {
  2135. WARN_SYNTAX_ERROR;
  2136. return (l);
  2137. }
  2138. if (line_read_string_expression (l, &To) == FALSE)
  2139. {
  2140. WARN_SYNTAX_ERROR;
  2141. return (l);
  2142. }
  2143. if (is_empty_string (To))
  2144. {
  2145. WARN_BAD_FILE_NAME;
  2146. return (l);
  2147. }
  2148. if (rename (From, To))
  2149. {
  2150. WARN_BAD_FILE_NAME;
  2151. return (l);
  2152. }
  2153. return (l);
  2154. }
  2155. LineType *
  2156. bwb_RENAME (LineType * l)
  2157. {
  2158. /*
  2159. SYNTAX: RENAME filename$
  2160. */
  2161. assert (l != NULL);
  2162. assert( My != NULL );
  2163. assert( My->CurrentVersion != NULL );
  2164. assert( My->ConsoleInput != NULL );
  2165. if (My->CurrentVersion->OptionVersionValue & (H14))
  2166. {
  2167. /* RENAME == change an exisiting file's name */
  2168. return H14_RENAME (l);
  2169. }
  2170. /* RENAME == change the BASIC program's name for a later SAVE */
  2171. if (line_is_eol (l))
  2172. {
  2173. /* RENAME */
  2174. if (My->CurrentVersion->OptionVersionValue & (D64 | G65 | G67 | G74))
  2175. {
  2176. /* prompt for the program name */
  2177. char *tbuf;
  2178. int tlen;
  2179. tbuf = My->ConsoleInput;
  2180. tlen = MAX_LINE_LENGTH;
  2181. bwx_input ("RENAME PROBLEM NAME:", FALSE, tbuf, tlen);
  2182. if (is_empty_string (tbuf))
  2183. {
  2184. WARN_BAD_FILE_NAME;
  2185. return (l);
  2186. }
  2187. if (My->ProgramFilename != NULL)
  2188. {
  2189. free (My->ProgramFilename);
  2190. My->ProgramFilename = NULL;
  2191. }
  2192. My->ProgramFilename = bwb_strdup (tbuf);
  2193. }
  2194. else
  2195. {
  2196. WARN_SYNTAX_ERROR;
  2197. return (l);
  2198. }
  2199. }
  2200. else
  2201. {
  2202. /* RENAME value$ */
  2203. char *Value;
  2204. Value = NULL;
  2205. if (line_read_string_expression (l, &Value) == FALSE)
  2206. {
  2207. WARN_SYNTAX_ERROR;
  2208. return (l);
  2209. }
  2210. if (is_empty_string (Value))
  2211. {
  2212. WARN_BAD_FILE_NAME;
  2213. return (l);
  2214. }
  2215. if (My->ProgramFilename != NULL)
  2216. {
  2217. free (My->ProgramFilename);
  2218. My->ProgramFilename = NULL;
  2219. }
  2220. My->ProgramFilename = Value;
  2221. }
  2222. return (l);
  2223. }
  2224. /*
  2225. --------------------------------------------------------------------------------------------
  2226. MAT
  2227. --------------------------------------------------------------------------------------------
  2228. */
  2229. extern void
  2230. Determinant (VariableType * v)
  2231. {
  2232. /* http://easy-learn-c-language.blogspot.com/search/label/Numerical%20Methods */
  2233. /* Numerical Methods: Determinant of nxn matrix using C */
  2234. DoubleType **matrix;
  2235. DoubleType ratio;
  2236. int i;
  2237. int j;
  2238. int k;
  2239. int n;
  2240. assert (v != NULL);
  2241. assert( My != NULL );
  2242. My->LastDeterminant = 0; /* default */
  2243. n = v->UBOUND[0] - v->LBOUND[0] + 1;
  2244. if ((matrix = (DoubleType **) calloc (n, sizeof (DoubleType *))) == NULL)
  2245. {
  2246. goto EXIT;
  2247. }
  2248. assert( matrix != NULL );
  2249. for (i = 0; i < n; i++)
  2250. {
  2251. if ((matrix[i] = (DoubleType *) calloc (n, sizeof (DoubleType))) == NULL)
  2252. {
  2253. goto EXIT;
  2254. }
  2255. assert( matrix[i] != NULL );
  2256. }
  2257. for (i = 0; i < n; i++)
  2258. {
  2259. for (j = 0; j < n; j++)
  2260. {
  2261. VariantType variant;
  2262. CLEAR_VARIANT (&variant);
  2263. v->VINDEX[0] = v->LBOUND[0] + i;
  2264. v->VINDEX[1] = v->LBOUND[1] + j;
  2265. if (var_get (v, &variant) == FALSE)
  2266. {
  2267. WARN_VARIABLE_NOT_DECLARED;
  2268. goto EXIT;
  2269. }
  2270. if (variant.VariantTypeCode == StringTypeCode)
  2271. {
  2272. WARN_TYPE_MISMATCH;
  2273. goto EXIT;
  2274. }
  2275. matrix[i][j] = variant.Number;
  2276. }
  2277. }
  2278. /* Conversion of matrix to upper triangular */
  2279. for (i = 0; i < n; i++)
  2280. {
  2281. for (j = 0; j < n; j++)
  2282. {
  2283. if (j > i)
  2284. {
  2285. if (matrix[i][i] == 0)
  2286. {
  2287. /* - Evaluation of an expression results in division
  2288. * by zero (nonfatal, the recommended recovery
  2289. * procedure is to supply machine infinity with the
  2290. * sign of the numerator and continue)
  2291. */
  2292. if (WARN_DIVISION_BY_ZERO)
  2293. {
  2294. /* ERROR */
  2295. goto EXIT;
  2296. }
  2297. /* CONTINUE */
  2298. if (matrix[j][i] < 0)
  2299. {
  2300. ratio = MINDBL;
  2301. }
  2302. else
  2303. {
  2304. ratio = MAXDBL;
  2305. }
  2306. }
  2307. else
  2308. {
  2309. ratio = matrix[j][i] / matrix[i][i];
  2310. }
  2311. for (k = 0; k < n; k++)
  2312. {
  2313. matrix[j][k] -= ratio * matrix[i][k];
  2314. }
  2315. }
  2316. }
  2317. }
  2318. My->LastDeterminant = 1; /* storage for determinant */
  2319. for (i = 0; i < n; i++)
  2320. {
  2321. DoubleType Value;
  2322. Value = matrix[i][i];
  2323. My->LastDeterminant *= Value;
  2324. }
  2325. EXIT:
  2326. if( matrix != NULL )
  2327. {
  2328. for (i = 0; i < n; i++)
  2329. {
  2330. if( matrix[i] != NULL )
  2331. {
  2332. free (matrix[i]);
  2333. /* matrix[i] = NULL; */
  2334. }
  2335. }
  2336. free (matrix);
  2337. /* matrix = NULL; */
  2338. }
  2339. }
  2340. int
  2341. InvertMatrix (VariableType * vOut, VariableType * vIn)
  2342. {
  2343. /* http://easy-learn-c-language.blogspot.com/search/label/Numerical%20Methods */
  2344. /* Numerical Methods: Inverse of nxn matrix using C */
  2345. int Result;
  2346. DoubleType **matrix;
  2347. DoubleType ratio;
  2348. int i;
  2349. int j;
  2350. int k;
  2351. int n;
  2352. assert (vOut != NULL);
  2353. assert (vIn != NULL);
  2354. Result = FALSE;
  2355. n = vIn->UBOUND[0] - vIn->LBOUND[0] + 1;
  2356. if ((matrix = (DoubleType **) calloc (n, sizeof (DoubleType *))) == NULL)
  2357. {
  2358. goto EXIT;
  2359. }
  2360. assert( matrix != NULL );
  2361. for (i = 0; i < n; i++)
  2362. {
  2363. if ((matrix[i] =
  2364. (DoubleType *) calloc (n + n, sizeof (DoubleType))) == NULL)
  2365. {
  2366. goto EXIT;
  2367. }
  2368. assert( matrix[i] != NULL );
  2369. }
  2370. for (i = 0; i < n; i++)
  2371. {
  2372. for (j = 0; j < n; j++)
  2373. {
  2374. VariantType variant;
  2375. CLEAR_VARIANT (&variant);
  2376. vIn->VINDEX[0] = vIn->LBOUND[0] + i;
  2377. vIn->VINDEX[1] = vIn->LBOUND[1] + j;
  2378. if (var_get (vIn, &variant) == FALSE)
  2379. {
  2380. WARN_VARIABLE_NOT_DECLARED;
  2381. goto EXIT;
  2382. }
  2383. if (variant.VariantTypeCode == StringTypeCode)
  2384. {
  2385. WARN_TYPE_MISMATCH;
  2386. goto EXIT;
  2387. }
  2388. matrix[i][j] = variant.Number;
  2389. }
  2390. }
  2391. for (i = 0; i < n; i++)
  2392. {
  2393. for (j = n; j < 2 * n; j++)
  2394. {
  2395. if (i == (j - n))
  2396. {
  2397. matrix[i][j] = 1.0;
  2398. }
  2399. else
  2400. {
  2401. matrix[i][j] = 0.0;
  2402. }
  2403. }
  2404. }
  2405. for (i = 0; i < n; i++)
  2406. {
  2407. for (j = 0; j < n; j++)
  2408. {
  2409. if (i != j)
  2410. {
  2411. if (matrix[i][i] == 0)
  2412. {
  2413. /* - Evaluation of an expression results in division
  2414. * by zero (nonfatal, the recommended recovery
  2415. * procedure is to supply machine infinity with the
  2416. * sign of the numerator and continue)
  2417. */
  2418. if (WARN_DIVISION_BY_ZERO)
  2419. {
  2420. /* ERROR */
  2421. goto EXIT;
  2422. }
  2423. /* CONTINUE */
  2424. if (matrix[j][i] < 0)
  2425. {
  2426. ratio = MINDBL;
  2427. }
  2428. else
  2429. {
  2430. ratio = MAXDBL;
  2431. }
  2432. }
  2433. else
  2434. {
  2435. ratio = matrix[j][i] / matrix[i][i];
  2436. }
  2437. for (k = 0; k < 2 * n; k++)
  2438. {
  2439. matrix[j][k] -= ratio * matrix[i][k];
  2440. }
  2441. }
  2442. }
  2443. }
  2444. for (i = 0; i < n; i++)
  2445. {
  2446. DoubleType a;
  2447. a = matrix[i][i];
  2448. if (a == 0)
  2449. {
  2450. /* - Evaluation of an expression results in division
  2451. * by zero (nonfatal, the recommended recovery
  2452. * procedure is to supply machine infinity with the
  2453. * sign of the numerator and continue)
  2454. */
  2455. if (WARN_DIVISION_BY_ZERO)
  2456. {
  2457. /* ERROR */
  2458. goto EXIT;
  2459. }
  2460. /* CONTINUE */
  2461. for (j = 0; j < 2 * n; j++)
  2462. {
  2463. if (matrix[i][j] < 0)
  2464. {
  2465. matrix[i][j] = MINDBL;
  2466. }
  2467. else
  2468. {
  2469. matrix[i][j] = MAXDBL;
  2470. }
  2471. }
  2472. }
  2473. else
  2474. {
  2475. for (j = 0; j < 2 * n; j++)
  2476. {
  2477. matrix[i][j] /= a;
  2478. }
  2479. }
  2480. }
  2481. for (i = 0; i < n; i++)
  2482. {
  2483. for (j = 0; j < n; j++)
  2484. {
  2485. VariantType variant;
  2486. CLEAR_VARIANT (&variant);
  2487. vOut->VINDEX[0] = vOut->LBOUND[0] + i;
  2488. vOut->VINDEX[1] = vOut->LBOUND[0] + j;
  2489. variant.VariantTypeCode = vOut->VariableTypeCode;
  2490. variant.Number = matrix[i][j + n];
  2491. if (var_set (vOut, &variant) == FALSE)
  2492. {
  2493. WARN_VARIABLE_NOT_DECLARED;
  2494. goto EXIT;
  2495. }
  2496. }
  2497. }
  2498. /*
  2499. **
  2500. ** Everything is OK
  2501. **
  2502. */
  2503. Result = TRUE;
  2504. EXIT:
  2505. if (matrix != NULL)
  2506. {
  2507. for (i = 0; i < n; i++)
  2508. {
  2509. if (matrix[i] != NULL)
  2510. {
  2511. free (matrix[i]);
  2512. /* matrix[i] = NULL; */
  2513. }
  2514. }
  2515. free (matrix);
  2516. /* matrix = NULL; */
  2517. }
  2518. return Result;
  2519. }
  2520. static int
  2521. line_read_matrix_redim (LineType * l, VariableType * v)
  2522. {
  2523. /* get OPTIONAL parameters if the variable is dimensioned */
  2524. assert (l != NULL);
  2525. assert (v != NULL);
  2526. if (line_peek_LparenChar (l))
  2527. {
  2528. /* get requested size, which is <= original array size */
  2529. size_t array_units;
  2530. int n;
  2531. int dimensions;
  2532. int LBOUND[MAX_DIMS];
  2533. int UBOUND[MAX_DIMS];
  2534. if (line_read_array_redim (l, &dimensions, LBOUND, UBOUND) == FALSE)
  2535. {
  2536. WARN_SYNTAX_ERROR;
  2537. return FALSE;
  2538. }
  2539. /* update array dimensions */
  2540. array_units = 1;
  2541. for (n = 0; n < dimensions; n++)
  2542. {
  2543. if (UBOUND[n] < LBOUND[n])
  2544. {
  2545. WARN_SUBSCRIPT_OUT_OF_RANGE;
  2546. return FALSE;
  2547. }
  2548. array_units *= UBOUND[n] - LBOUND[n] + 1;
  2549. }
  2550. if (array_units > v->array_units)
  2551. {
  2552. WARN_SUBSCRIPT_OUT_OF_RANGE;
  2553. return FALSE;
  2554. }
  2555. v->dimensions = dimensions;
  2556. for (n = 0; n < dimensions; n++)
  2557. {
  2558. v->LBOUND[n] = LBOUND[n];
  2559. v->UBOUND[n] = UBOUND[n];
  2560. }
  2561. }
  2562. return TRUE;
  2563. }
  2564. LineType *
  2565. bwb_MAT (LineType * l)
  2566. {
  2567. /*
  2568. SYNTAX: MAT A = CON
  2569. SYNTAX: MAT A = IDN
  2570. SYNTAX: MAT A = ZER
  2571. SYNTAX: MAT A = INV B
  2572. SYNTAX: MAT A = TRN B
  2573. SYNTAX: MAT A = (k) * B
  2574. SYNTAX: MAT A = B
  2575. SYNTAX: MAT A = B + C
  2576. SYNTAX: MAT A = B - C
  2577. SYNTAX: MAT A = B * C
  2578. */
  2579. VariableType *v_A;
  2580. char varname_A[NameLengthMax + 1];
  2581. assert (l != NULL);
  2582. /* just a placeholder for now. this will grow. */
  2583. if (line_read_varname (l, varname_A) == FALSE)
  2584. {
  2585. WARN_SYNTAX_ERROR;
  2586. return (l);
  2587. }
  2588. v_A = mat_find (varname_A);
  2589. if (v_A == NULL)
  2590. {
  2591. WARN_VARIABLE_NOT_DECLARED;
  2592. return (l);
  2593. }
  2594. /* variable MUST be numeric */
  2595. if (VAR_IS_STRING (v_A))
  2596. {
  2597. WARN_SYNTAX_ERROR;
  2598. return (l);
  2599. }
  2600. if (line_read_matrix_redim (l, v_A) == FALSE)
  2601. {
  2602. WARN_SYNTAX_ERROR;
  2603. return (l);
  2604. }
  2605. if (line_skip_EqualChar (l) == FALSE)
  2606. {
  2607. WARN_SYNTAX_ERROR;
  2608. return (l);
  2609. }
  2610. /* MAT A = ... */
  2611. if (line_skip_word (l, "CON"))
  2612. {
  2613. /* MAT A = CON */
  2614. /* MAT A = CON(I) */
  2615. /* MAT A = CON(I,J) */
  2616. /* MAT A = CON(I,J,K) */
  2617. /* OK */
  2618. int i;
  2619. int j;
  2620. int k;
  2621. if (line_read_matrix_redim (l, v_A) == FALSE)
  2622. {
  2623. WARN_SYNTAX_ERROR;
  2624. return (l);
  2625. }
  2626. /* both arrays are of the same size */
  2627. switch (v_A->dimensions)
  2628. {
  2629. case 1:
  2630. for (i = v_A->LBOUND[0]; i <= v_A->UBOUND[0]; i++)
  2631. {
  2632. VariantType variant;
  2633. CLEAR_VARIANT (&variant);
  2634. variant.VariantTypeCode = v_A->VariableTypeCode;
  2635. variant.Number = 1;
  2636. v_A->VINDEX[0] = i;
  2637. if (var_set (v_A, &variant) == FALSE)
  2638. {
  2639. WARN_VARIABLE_NOT_DECLARED;
  2640. return (l);
  2641. }
  2642. }
  2643. break;
  2644. case 2:
  2645. for (i = v_A->LBOUND[0]; i <= v_A->UBOUND[0]; i++)
  2646. {
  2647. for (j = v_A->LBOUND[1]; j <= v_A->UBOUND[1]; j++)
  2648. {
  2649. VariantType variant;
  2650. CLEAR_VARIANT (&variant);
  2651. variant.VariantTypeCode = v_A->VariableTypeCode;
  2652. variant.Number = 1;
  2653. v_A->VINDEX[0] = i;
  2654. v_A->VINDEX[1] = j;
  2655. if (var_set (v_A, &variant) == FALSE)
  2656. {
  2657. WARN_VARIABLE_NOT_DECLARED;
  2658. return (l);
  2659. }
  2660. }
  2661. }
  2662. break;
  2663. case 3:
  2664. for (i = v_A->LBOUND[0]; i <= v_A->UBOUND[0]; i++)
  2665. {
  2666. for (j = v_A->LBOUND[1]; j <= v_A->UBOUND[1]; j++)
  2667. {
  2668. for (k = v_A->LBOUND[2]; k <= v_A->UBOUND[2]; k++)
  2669. {
  2670. VariantType variant;
  2671. CLEAR_VARIANT (&variant);
  2672. variant.VariantTypeCode = v_A->VariableTypeCode;
  2673. variant.Number = 1;
  2674. v_A->VINDEX[0] = i;
  2675. v_A->VINDEX[1] = j;
  2676. v_A->VINDEX[2] = k;
  2677. if (var_set (v_A, &variant) == FALSE)
  2678. {
  2679. WARN_VARIABLE_NOT_DECLARED;
  2680. return (l);
  2681. }
  2682. }
  2683. }
  2684. }
  2685. break;
  2686. default:
  2687. WARN_SYNTAX_ERROR;
  2688. return (l);
  2689. }
  2690. }
  2691. else if (line_skip_word (l, "IDN"))
  2692. {
  2693. /* MAT A = IDN */
  2694. /* MAT A = IDN(I,J) */
  2695. /* OK */
  2696. int i;
  2697. int j;
  2698. if (line_read_matrix_redim (l, v_A) == FALSE)
  2699. {
  2700. WARN_SYNTAX_ERROR;
  2701. return (l);
  2702. }
  2703. if (v_A->dimensions != 2)
  2704. {
  2705. WARN_SUBSCRIPT_OUT_OF_RANGE;
  2706. return (l);
  2707. }
  2708. if (v_A->LBOUND[0] != v_A->LBOUND[1])
  2709. {
  2710. WARN_SUBSCRIPT_OUT_OF_RANGE;
  2711. return (l);
  2712. }
  2713. if (v_A->UBOUND[0] != v_A->UBOUND[1])
  2714. {
  2715. WARN_SUBSCRIPT_OUT_OF_RANGE;
  2716. return (l);
  2717. }
  2718. /* square matrix */
  2719. for (i = v_A->LBOUND[0]; i <= v_A->UBOUND[0]; i++)
  2720. {
  2721. for (j = v_A->LBOUND[1]; j <= v_A->UBOUND[1]; j++)
  2722. {
  2723. VariantType variant;
  2724. CLEAR_VARIANT (&variant);
  2725. variant.VariantTypeCode = v_A->VariableTypeCode;
  2726. if (i == j)
  2727. {
  2728. variant.Number = 1;
  2729. }
  2730. else
  2731. {
  2732. variant.Number = 0;
  2733. }
  2734. v_A->VINDEX[0] = i;
  2735. v_A->VINDEX[1] = j;
  2736. if (var_set (v_A, &variant) == FALSE)
  2737. {
  2738. WARN_VARIABLE_NOT_DECLARED;
  2739. return (l);
  2740. }
  2741. }
  2742. }
  2743. }
  2744. else if (line_skip_word (l, "ZER"))
  2745. {
  2746. /* MAT A = ZER */
  2747. /* MAT A = ZER(I) */
  2748. /* MAT A = ZER(I,J) */
  2749. /* MAT A = ZER(I,J,K) */
  2750. /* OK */
  2751. int i;
  2752. int j;
  2753. int k;
  2754. if (line_read_matrix_redim (l, v_A) == FALSE)
  2755. {
  2756. WARN_SYNTAX_ERROR;
  2757. return (l);
  2758. }
  2759. /* both arrays are of the same size */
  2760. switch (v_A->dimensions)
  2761. {
  2762. case 1:
  2763. for (i = v_A->LBOUND[0]; i <= v_A->UBOUND[0]; i++)
  2764. {
  2765. VariantType variant;
  2766. CLEAR_VARIANT (&variant);
  2767. variant.VariantTypeCode = v_A->VariableTypeCode;
  2768. variant.Number = 0;
  2769. v_A->VINDEX[0] = i;
  2770. if (var_set (v_A, &variant) == FALSE)
  2771. {
  2772. WARN_VARIABLE_NOT_DECLARED;
  2773. return (l);
  2774. }
  2775. }
  2776. break;
  2777. case 2:
  2778. for (i = v_A->LBOUND[0]; i <= v_A->UBOUND[0]; i++)
  2779. {
  2780. for (j = v_A->LBOUND[1]; j <= v_A->UBOUND[1]; j++)
  2781. {
  2782. VariantType variant;
  2783. CLEAR_VARIANT (&variant);
  2784. variant.VariantTypeCode = v_A->VariableTypeCode;
  2785. variant.Number = 0;
  2786. v_A->VINDEX[0] = i;
  2787. v_A->VINDEX[1] = j;
  2788. if (var_set (v_A, &variant) == FALSE)
  2789. {
  2790. WARN_VARIABLE_NOT_DECLARED;
  2791. return (l);
  2792. }
  2793. }
  2794. }
  2795. break;
  2796. case 3:
  2797. for (i = v_A->LBOUND[0]; i <= v_A->UBOUND[0]; i++)
  2798. {
  2799. for (j = v_A->LBOUND[1]; j <= v_A->UBOUND[1]; j++)
  2800. {
  2801. for (k = v_A->LBOUND[2]; k <= v_A->UBOUND[2]; k++)
  2802. {
  2803. VariantType variant;
  2804. CLEAR_VARIANT (&variant);
  2805. variant.VariantTypeCode = v_A->VariableTypeCode;
  2806. variant.Number = 0;
  2807. v_A->VINDEX[0] = i;
  2808. v_A->VINDEX[1] = j;
  2809. v_A->VINDEX[2] = k;
  2810. if (var_set (v_A, &variant) == FALSE)
  2811. {
  2812. WARN_VARIABLE_NOT_DECLARED;
  2813. return (l);
  2814. }
  2815. }
  2816. }
  2817. }
  2818. break;
  2819. default:
  2820. WARN_SYNTAX_ERROR;
  2821. return (l);
  2822. }
  2823. }
  2824. else if (line_skip_word (l, "INV"))
  2825. {
  2826. /* MAT A = INV B */
  2827. /* MAT A = INV( B ) */
  2828. /* OK */
  2829. VariableType *v_B;
  2830. char varname_B[NameLengthMax + 1];
  2831. if (v_A->dimensions != 2)
  2832. {
  2833. WARN_SYNTAX_ERROR;
  2834. return (l);
  2835. }
  2836. if (v_A->LBOUND[0] != v_A->LBOUND[1] || v_A->UBOUND[0] != v_A->UBOUND[1])
  2837. {
  2838. WARN_SUBSCRIPT_OUT_OF_RANGE;
  2839. return (l);
  2840. }
  2841. if (line_skip_LparenChar (l))
  2842. {
  2843. /* optional */
  2844. }
  2845. if (line_read_varname (l, varname_B) == FALSE)
  2846. {
  2847. WARN_SYNTAX_ERROR;
  2848. return (l);
  2849. }
  2850. if ((v_B = mat_find (varname_B)) == NULL)
  2851. {
  2852. WARN_VARIABLE_NOT_DECLARED;
  2853. return (l);
  2854. }
  2855. /* variable MUST be numeric */
  2856. if (VAR_IS_STRING (v_B))
  2857. {
  2858. WARN_SYNTAX_ERROR;
  2859. return (l);
  2860. }
  2861. if (line_read_matrix_redim (l, v_B) == FALSE)
  2862. {
  2863. WARN_SYNTAX_ERROR;
  2864. return (l);
  2865. }
  2866. if (line_skip_RparenChar (l))
  2867. {
  2868. /* optional */
  2869. }
  2870. if (v_B->dimensions != 2)
  2871. {
  2872. WARN_SYNTAX_ERROR;
  2873. return (l);
  2874. }
  2875. if (v_B->LBOUND[0] != v_B->LBOUND[1] || v_B->UBOUND[0] != v_B->UBOUND[1])
  2876. {
  2877. WARN_SUBSCRIPT_OUT_OF_RANGE;
  2878. return (l);
  2879. }
  2880. if (v_A->LBOUND[0] != v_B->LBOUND[0] || v_A->UBOUND[0] != v_B->UBOUND[0])
  2881. {
  2882. WARN_SUBSCRIPT_OUT_OF_RANGE;
  2883. return (l);
  2884. }
  2885. /* square matrix */
  2886. Determinant (v_B);
  2887. if (My->LastDeterminant == 0)
  2888. {
  2889. WARN_ILLEGAL_FUNCTION_CALL;
  2890. return (l);
  2891. }
  2892. if (InvertMatrix (v_A, v_B) == FALSE)
  2893. {
  2894. WARN_ILLEGAL_FUNCTION_CALL;
  2895. return (l);
  2896. }
  2897. }
  2898. else if (line_skip_word (l, "TRN"))
  2899. {
  2900. /* MAT A = TRN B */
  2901. /* MAT A = TRN( B ) */
  2902. /* OK */
  2903. int i;
  2904. int j;
  2905. VariableType *v_B;
  2906. char varname_B[NameLengthMax + 1];
  2907. if (v_A->dimensions != 2)
  2908. {
  2909. WARN_SYNTAX_ERROR;
  2910. return (l);
  2911. }
  2912. if (line_skip_LparenChar (l))
  2913. {
  2914. /* optional */
  2915. }
  2916. if (line_read_varname (l, varname_B) == FALSE)
  2917. {
  2918. WARN_SYNTAX_ERROR;
  2919. return (l);
  2920. }
  2921. if ((v_B = mat_find (varname_B)) == NULL)
  2922. {
  2923. WARN_VARIABLE_NOT_DECLARED;
  2924. return (l);
  2925. }
  2926. /* variable MUST be numeric */
  2927. if (VAR_IS_STRING (v_B))
  2928. {
  2929. WARN_SYNTAX_ERROR;
  2930. return (l);
  2931. }
  2932. if (line_read_matrix_redim (l, v_B) == FALSE)
  2933. {
  2934. WARN_SYNTAX_ERROR;
  2935. return (l);
  2936. }
  2937. if (line_skip_RparenChar (l))
  2938. {
  2939. /* optional */
  2940. }
  2941. if (v_B->dimensions != 2)
  2942. {
  2943. WARN_SYNTAX_ERROR;
  2944. return (l);
  2945. }
  2946. /* MxN */
  2947. if (v_A->LBOUND[0] != v_B->LBOUND[1] || v_A->UBOUND[0] != v_B->UBOUND[1])
  2948. {
  2949. WARN_SYNTAX_ERROR;
  2950. return (l);
  2951. }
  2952. if (v_A->LBOUND[1] != v_B->LBOUND[0] || v_A->UBOUND[1] != v_B->UBOUND[0])
  2953. {
  2954. WARN_SYNTAX_ERROR;
  2955. return (l);
  2956. }
  2957. /* transpose matrix */
  2958. for (i = v_A->LBOUND[0]; i <= v_A->UBOUND[0]; i++)
  2959. {
  2960. for (j = v_A->LBOUND[1]; j <= v_A->UBOUND[1]; j++)
  2961. {
  2962. VariantType variant;
  2963. CLEAR_VARIANT (&variant);
  2964. v_B->VINDEX[1] = i;
  2965. v_B->VINDEX[0] = j;
  2966. if (var_get (v_B, &variant) == FALSE)
  2967. {
  2968. WARN_VARIABLE_NOT_DECLARED;
  2969. return (l);
  2970. }
  2971. v_A->VINDEX[0] = i;
  2972. v_A->VINDEX[1] = j;
  2973. if (var_set (v_A, &variant) == FALSE)
  2974. {
  2975. WARN_VARIABLE_NOT_DECLARED;
  2976. return (l);
  2977. }
  2978. }
  2979. }
  2980. }
  2981. else if (line_peek_LparenChar (l))
  2982. {
  2983. /* MAT A = (k) * B */
  2984. DoubleType Multiplier;
  2985. VariableType *v_B;
  2986. int i;
  2987. int j;
  2988. int k;
  2989. char *E;
  2990. int p;
  2991. char varname_B[NameLengthMax + 1];
  2992. char *tbuf;
  2993. tbuf = My->ConsoleInput;
  2994. bwb_strcpy (tbuf, &(l->buffer[l->position]));
  2995. E = bwb_strrchr (tbuf, '*');
  2996. if (E == NULL)
  2997. {
  2998. WARN_SYNTAX_ERROR;
  2999. return (l);
  3000. }
  3001. *E = NulChar;
  3002. p = 0;
  3003. if (buff_read_numeric_expression (tbuf, &p, &Multiplier) == FALSE)
  3004. {
  3005. WARN_SYNTAX_ERROR;
  3006. return (l);
  3007. }
  3008. l->position += p;
  3009. if (line_skip_StarChar (l) == FALSE)
  3010. {
  3011. WARN_SYNTAX_ERROR;
  3012. return (l);
  3013. }
  3014. if (line_read_varname (l, varname_B) == FALSE)
  3015. {
  3016. WARN_SYNTAX_ERROR;
  3017. return (l);
  3018. }
  3019. if ((v_B = mat_find (varname_B)) == NULL)
  3020. {
  3021. WARN_VARIABLE_NOT_DECLARED;
  3022. return (l);
  3023. }
  3024. /* variable MUST be numeric */
  3025. if (VAR_IS_STRING (v_B))
  3026. {
  3027. WARN_SYNTAX_ERROR;
  3028. return (l);
  3029. }
  3030. if (line_read_matrix_redim (l, v_B) == FALSE)
  3031. {
  3032. WARN_SYNTAX_ERROR;
  3033. return (l);
  3034. }
  3035. if (v_A->dimensions != v_B->dimensions)
  3036. {
  3037. WARN_SYNTAX_ERROR;
  3038. return (l);
  3039. }
  3040. /* both arrays are of the same size */
  3041. switch (v_A->dimensions)
  3042. {
  3043. case 1:
  3044. for (i = v_A->LBOUND[0]; i <= v_A->UBOUND[0]; i++)
  3045. {
  3046. VariantType variant;
  3047. CLEAR_VARIANT (&variant);
  3048. v_B->VINDEX[0] = i;
  3049. if (var_get (v_B, &variant) == FALSE)
  3050. {
  3051. WARN_VARIABLE_NOT_DECLARED;
  3052. return (l);
  3053. }
  3054. variant.Number *= Multiplier;
  3055. v_A->VINDEX[0] = i;
  3056. if (var_set (v_A, &variant) == FALSE)
  3057. {
  3058. WARN_VARIABLE_NOT_DECLARED;
  3059. return (l);
  3060. }
  3061. }
  3062. break;
  3063. case 2:
  3064. for (i = v_A->LBOUND[0]; i <= v_A->UBOUND[0]; i++)
  3065. {
  3066. for (j = v_A->LBOUND[1]; j <= v_A->UBOUND[1]; j++)
  3067. {
  3068. VariantType variant;
  3069. CLEAR_VARIANT (&variant);
  3070. v_B->VINDEX[0] = i;
  3071. v_B->VINDEX[1] = j;
  3072. if (var_get (v_B, &variant) == FALSE)
  3073. {
  3074. WARN_VARIABLE_NOT_DECLARED;
  3075. return (l);
  3076. }
  3077. variant.Number *= Multiplier;
  3078. v_A->VINDEX[0] = i;
  3079. v_A->VINDEX[1] = j;
  3080. if (var_set (v_A, &variant) == FALSE)
  3081. {
  3082. WARN_VARIABLE_NOT_DECLARED;
  3083. return (l);
  3084. }
  3085. }
  3086. }
  3087. break;
  3088. case 3:
  3089. for (i = v_A->LBOUND[0]; i <= v_A->UBOUND[0]; i++)
  3090. {
  3091. for (j = v_A->LBOUND[1]; j <= v_A->UBOUND[1]; j++)
  3092. {
  3093. for (k = v_A->LBOUND[2]; k <= v_A->UBOUND[2]; k++)
  3094. {
  3095. VariantType variant;
  3096. CLEAR_VARIANT (&variant);
  3097. v_B->VINDEX[0] = i;
  3098. v_B->VINDEX[1] = j;
  3099. v_B->VINDEX[2] = k;
  3100. if (var_get (v_B, &variant) == FALSE)
  3101. {
  3102. WARN_VARIABLE_NOT_DECLARED;
  3103. return (l);
  3104. }
  3105. variant.Number *= Multiplier;
  3106. v_A->VINDEX[0] = i;
  3107. v_A->VINDEX[1] = j;
  3108. v_A->VINDEX[2] = k;
  3109. if (var_set (v_A, &variant) == FALSE)
  3110. {
  3111. WARN_VARIABLE_NOT_DECLARED;
  3112. return (l);
  3113. }
  3114. }
  3115. }
  3116. }
  3117. break;
  3118. default:
  3119. WARN_SYNTAX_ERROR;
  3120. return (l);
  3121. }
  3122. }
  3123. else
  3124. {
  3125. /* MAT A = B */
  3126. /* MAT A = B + C */
  3127. /* MAT A = B - C */
  3128. /* MAT A = B * C */
  3129. VariableType *v_B;
  3130. char varname_B[NameLengthMax + 1];
  3131. if (line_read_varname (l, varname_B) == FALSE)
  3132. {
  3133. WARN_SYNTAX_ERROR;
  3134. return (l);
  3135. }
  3136. if ((v_B = mat_find (varname_B)) == NULL)
  3137. {
  3138. WARN_VARIABLE_NOT_DECLARED;
  3139. return (l);
  3140. }
  3141. /* variable MUST be numeric */
  3142. if (VAR_IS_STRING (v_B))
  3143. {
  3144. WARN_SYNTAX_ERROR;
  3145. return (l);
  3146. }
  3147. if (line_read_matrix_redim (l, v_B) == FALSE)
  3148. {
  3149. WARN_SYNTAX_ERROR;
  3150. return (l);
  3151. }
  3152. if (line_is_eol (l))
  3153. {
  3154. /* MAT A = B */
  3155. /* OK */
  3156. int i;
  3157. int j;
  3158. int k;
  3159. if (v_A->dimensions != v_B->dimensions)
  3160. {
  3161. WARN_SYNTAX_ERROR;
  3162. return (l);
  3163. }
  3164. /* both arrays are of the same size */
  3165. switch (v_A->dimensions)
  3166. {
  3167. case 1:
  3168. for (i = v_A->LBOUND[0]; i <= v_A->UBOUND[0]; i++)
  3169. {
  3170. VariantType variant;
  3171. CLEAR_VARIANT (&variant);
  3172. v_B->VINDEX[0] = i;
  3173. if (var_get (v_B, &variant) == FALSE)
  3174. {
  3175. WARN_VARIABLE_NOT_DECLARED;
  3176. return (l);
  3177. }
  3178. v_A->VINDEX[0] = i;
  3179. if (var_set (v_A, &variant) == FALSE)
  3180. {
  3181. WARN_VARIABLE_NOT_DECLARED;
  3182. return (l);
  3183. }
  3184. }
  3185. break;
  3186. case 2:
  3187. for (i = v_A->LBOUND[0]; i <= v_A->UBOUND[0]; i++)
  3188. {
  3189. for (j = v_A->LBOUND[1]; j <= v_A->UBOUND[1]; j++)
  3190. {
  3191. VariantType variant;
  3192. CLEAR_VARIANT (&variant);
  3193. v_B->VINDEX[0] = i;
  3194. v_B->VINDEX[1] = j;
  3195. if (var_get (v_B, &variant) == FALSE)
  3196. {
  3197. WARN_VARIABLE_NOT_DECLARED;
  3198. return (l);
  3199. }
  3200. v_A->VINDEX[0] = i;
  3201. v_A->VINDEX[1] = j;
  3202. if (var_set (v_A, &variant) == FALSE)
  3203. {
  3204. WARN_VARIABLE_NOT_DECLARED;
  3205. return (l);
  3206. }
  3207. }
  3208. }
  3209. break;
  3210. case 3:
  3211. for (i = v_A->LBOUND[0]; i <= v_A->UBOUND[0]; i++)
  3212. {
  3213. for (j = v_A->LBOUND[1]; j <= v_A->UBOUND[1]; j++)
  3214. {
  3215. for (k = v_A->LBOUND[2]; k <= v_A->UBOUND[2]; k++)
  3216. {
  3217. VariantType variant;
  3218. CLEAR_VARIANT (&variant);
  3219. v_B->VINDEX[0] = i;
  3220. v_B->VINDEX[1] = j;
  3221. v_B->VINDEX[2] = k;
  3222. if (var_get (v_B, &variant) == FALSE)
  3223. {
  3224. WARN_VARIABLE_NOT_DECLARED;
  3225. return (l);
  3226. }
  3227. v_A->VINDEX[0] = i;
  3228. v_A->VINDEX[1] = j;
  3229. v_A->VINDEX[2] = k;
  3230. if (var_set (v_A, &variant) == FALSE)
  3231. {
  3232. WARN_VARIABLE_NOT_DECLARED;
  3233. return (l);
  3234. }
  3235. }
  3236. }
  3237. }
  3238. break;
  3239. default:
  3240. WARN_SYNTAX_ERROR;
  3241. return (l);
  3242. }
  3243. }
  3244. else if (line_skip_PlusChar (l))
  3245. {
  3246. /* MAT A = B + C */
  3247. /* OK */
  3248. int i;
  3249. int j;
  3250. int k;
  3251. VariableType *v_C;
  3252. char varname_C[NameLengthMax + 1];
  3253. if (v_A->dimensions != v_B->dimensions)
  3254. {
  3255. WARN_SYNTAX_ERROR;
  3256. return (l);
  3257. }
  3258. /* both arrays are of the same size */
  3259. if (line_read_varname (l, varname_C) == FALSE)
  3260. {
  3261. WARN_SYNTAX_ERROR;
  3262. return (l);
  3263. }
  3264. if ((v_C = mat_find (varname_C)) == NULL)
  3265. {
  3266. WARN_VARIABLE_NOT_DECLARED;
  3267. return (l);
  3268. }
  3269. /* variable MUST be numeric */
  3270. if (VAR_IS_STRING (v_C))
  3271. {
  3272. WARN_SYNTAX_ERROR;
  3273. return (l);
  3274. }
  3275. if (line_read_matrix_redim (l, v_C) == FALSE)
  3276. {
  3277. WARN_SYNTAX_ERROR;
  3278. return (l);
  3279. }
  3280. if (v_B->dimensions != v_C->dimensions)
  3281. {
  3282. WARN_SYNTAX_ERROR;
  3283. return (l);
  3284. }
  3285. /* both arrays are of the same size */
  3286. switch (v_A->dimensions)
  3287. {
  3288. case 1:
  3289. for (i = v_A->LBOUND[0]; i <= v_A->UBOUND[0]; i++)
  3290. {
  3291. VariantType variant_L;
  3292. VariantType variant_R;
  3293. CLEAR_VARIANT (&variant_L);
  3294. CLEAR_VARIANT (&variant_R);
  3295. v_B->VINDEX[0] = i;
  3296. if (var_get (v_B, &variant_L) == FALSE)
  3297. {
  3298. WARN_VARIABLE_NOT_DECLARED;
  3299. return (l);
  3300. }
  3301. v_C->VINDEX[0] = i;
  3302. if (var_get (v_C, &variant_R) == FALSE)
  3303. {
  3304. WARN_VARIABLE_NOT_DECLARED;
  3305. return (l);
  3306. }
  3307. variant_L.Number += variant_R.Number;
  3308. v_A->VINDEX[0] = i;
  3309. if (var_set (v_A, &variant_L) == FALSE)
  3310. {
  3311. WARN_VARIABLE_NOT_DECLARED;
  3312. return (l);
  3313. }
  3314. }
  3315. break;
  3316. case 2:
  3317. for (i = v_A->LBOUND[0]; i <= v_A->UBOUND[0]; i++)
  3318. {
  3319. for (j = v_A->LBOUND[1]; j <= v_A->UBOUND[1]; j++)
  3320. {
  3321. VariantType variant_L;
  3322. VariantType variant_R;
  3323. CLEAR_VARIANT (&variant_L);
  3324. CLEAR_VARIANT (&variant_R);
  3325. v_B->VINDEX[0] = i;
  3326. v_B->VINDEX[1] = j;
  3327. if (var_get (v_B, &variant_L) == FALSE)
  3328. {
  3329. WARN_VARIABLE_NOT_DECLARED;
  3330. return (l);
  3331. }
  3332. v_C->VINDEX[0] = i;
  3333. v_C->VINDEX[1] = j;
  3334. if (var_get (v_C, &variant_R) == FALSE)
  3335. {
  3336. WARN_VARIABLE_NOT_DECLARED;
  3337. return (l);
  3338. }
  3339. variant_L.Number += variant_R.Number;
  3340. v_A->VINDEX[0] = i;
  3341. v_A->VINDEX[1] = j;
  3342. if (var_set (v_A, &variant_L) == FALSE)
  3343. {
  3344. WARN_VARIABLE_NOT_DECLARED;
  3345. return (l);
  3346. }
  3347. }
  3348. }
  3349. break;
  3350. case 3:
  3351. for (i = v_A->LBOUND[0]; i <= v_A->UBOUND[0]; i++)
  3352. {
  3353. for (j = v_A->LBOUND[1]; j <= v_A->UBOUND[1]; j++)
  3354. {
  3355. for (k = v_A->LBOUND[2]; k <= v_A->UBOUND[2]; k++)
  3356. {
  3357. VariantType variant_L;
  3358. VariantType variant_R;
  3359. CLEAR_VARIANT (&variant_L);
  3360. CLEAR_VARIANT (&variant_R);
  3361. v_B->VINDEX[0] = i;
  3362. v_B->VINDEX[1] = j;
  3363. v_B->VINDEX[2] = k;
  3364. if (var_get (v_B, &variant_L) == FALSE)
  3365. {
  3366. WARN_VARIABLE_NOT_DECLARED;
  3367. return (l);
  3368. }
  3369. v_C->VINDEX[0] = i;
  3370. v_C->VINDEX[1] = j;
  3371. v_C->VINDEX[2] = k;
  3372. if (var_get (v_C, &variant_R) == FALSE)
  3373. {
  3374. WARN_VARIABLE_NOT_DECLARED;
  3375. return (l);
  3376. }
  3377. variant_L.Number += variant_R.Number;
  3378. v_A->VINDEX[0] = i;
  3379. v_A->VINDEX[1] = j;
  3380. v_A->VINDEX[2] = k;
  3381. if (var_set (v_A, &variant_L) == FALSE)
  3382. {
  3383. WARN_VARIABLE_NOT_DECLARED;
  3384. return (l);
  3385. }
  3386. }
  3387. }
  3388. }
  3389. break;
  3390. default:
  3391. WARN_SYNTAX_ERROR;
  3392. return (l);
  3393. }
  3394. }
  3395. else if (line_skip_MinusChar (l))
  3396. {
  3397. /* MAT A = B - C */
  3398. /* OK */
  3399. int i;
  3400. int j;
  3401. int k;
  3402. VariableType *v_C;
  3403. char varname_C[NameLengthMax + 1];
  3404. if (v_A->dimensions != v_B->dimensions)
  3405. {
  3406. WARN_SYNTAX_ERROR;
  3407. return (l);
  3408. }
  3409. /* both arrays are of the same size */
  3410. if (line_read_varname (l, varname_C) == FALSE)
  3411. {
  3412. WARN_SYNTAX_ERROR;
  3413. return (l);
  3414. }
  3415. if ((v_C = mat_find (varname_C)) == NULL)
  3416. {
  3417. WARN_VARIABLE_NOT_DECLARED;
  3418. return (l);
  3419. }
  3420. /* variable MUST be numeric */
  3421. if (VAR_IS_STRING (v_C))
  3422. {
  3423. WARN_SYNTAX_ERROR;
  3424. return (l);
  3425. }
  3426. if (line_read_matrix_redim (l, v_C) == FALSE)
  3427. {
  3428. WARN_SYNTAX_ERROR;
  3429. return (l);
  3430. }
  3431. if (v_B->dimensions != v_C->dimensions)
  3432. {
  3433. WARN_SYNTAX_ERROR;
  3434. return (l);
  3435. }
  3436. /* both arrays are of the same dimension */
  3437. switch (v_A->dimensions)
  3438. {
  3439. case 1:
  3440. for (i = v_A->LBOUND[0]; i <= v_A->UBOUND[0]; i++)
  3441. {
  3442. VariantType variant_L;
  3443. VariantType variant_R;
  3444. CLEAR_VARIANT (&variant_L);
  3445. CLEAR_VARIANT (&variant_R);
  3446. v_B->VINDEX[0] = i;
  3447. if (var_get (v_B, &variant_L) == FALSE)
  3448. {
  3449. WARN_VARIABLE_NOT_DECLARED;
  3450. return (l);
  3451. }
  3452. v_C->VINDEX[0] = i;
  3453. if (var_get (v_C, &variant_R) == FALSE)
  3454. {
  3455. WARN_VARIABLE_NOT_DECLARED;
  3456. return (l);
  3457. }
  3458. variant_L.Number -= variant_R.Number;
  3459. v_A->VINDEX[0] = i;
  3460. if (var_set (v_A, &variant_L) == FALSE)
  3461. {
  3462. WARN_VARIABLE_NOT_DECLARED;
  3463. return (l);
  3464. }
  3465. }
  3466. break;
  3467. case 2:
  3468. for (i = v_A->LBOUND[0]; i <= v_A->UBOUND[0]; i++)
  3469. {
  3470. for (j = v_A->LBOUND[1]; j <= v_A->UBOUND[1]; j++)
  3471. {
  3472. VariantType variant_L;
  3473. VariantType variant_R;
  3474. CLEAR_VARIANT (&variant_L);
  3475. CLEAR_VARIANT (&variant_R);
  3476. v_B->VINDEX[0] = i;
  3477. v_B->VINDEX[1] = j;
  3478. if (var_get (v_B, &variant_L) == FALSE)
  3479. {
  3480. WARN_VARIABLE_NOT_DECLARED;
  3481. return (l);
  3482. }
  3483. v_C->VINDEX[0] = i;
  3484. v_C->VINDEX[1] = j;
  3485. if (var_get (v_C, &variant_R) == FALSE)
  3486. {
  3487. WARN_VARIABLE_NOT_DECLARED;
  3488. return (l);
  3489. }
  3490. variant_L.Number -= variant_R.Number;
  3491. v_A->VINDEX[0] = i;
  3492. v_A->VINDEX[1] = j;
  3493. if (var_set (v_A, &variant_L) == FALSE)
  3494. {
  3495. WARN_VARIABLE_NOT_DECLARED;
  3496. return (l);
  3497. }
  3498. }
  3499. }
  3500. break;
  3501. case 3:
  3502. for (i = v_A->LBOUND[0]; i <= v_A->UBOUND[0]; i++)
  3503. {
  3504. for (j = v_A->LBOUND[1]; j <= v_A->UBOUND[1]; j++)
  3505. {
  3506. for (k = v_A->LBOUND[2]; k <= v_A->UBOUND[2]; k++)
  3507. {
  3508. VariantType variant_L;
  3509. VariantType variant_R;
  3510. CLEAR_VARIANT (&variant_L);
  3511. CLEAR_VARIANT (&variant_R);
  3512. v_B->VINDEX[0] = i;
  3513. v_B->VINDEX[1] = j;
  3514. v_B->VINDEX[2] = k;
  3515. if (var_get (v_B, &variant_L) == FALSE)
  3516. {
  3517. WARN_VARIABLE_NOT_DECLARED;
  3518. return (l);
  3519. }
  3520. v_C->VINDEX[0] = i;
  3521. v_C->VINDEX[1] = j;
  3522. v_C->VINDEX[2] = k;
  3523. if (var_get (v_C, &variant_R) == FALSE)
  3524. {
  3525. WARN_VARIABLE_NOT_DECLARED;
  3526. return (l);
  3527. }
  3528. variant_L.Number -= variant_R.Number;
  3529. v_A->VINDEX[0] = i;
  3530. v_A->VINDEX[1] = j;
  3531. v_A->VINDEX[2] = k;
  3532. if (var_set (v_A, &variant_L) == FALSE)
  3533. {
  3534. WARN_VARIABLE_NOT_DECLARED;
  3535. return (l);
  3536. }
  3537. }
  3538. }
  3539. }
  3540. break;
  3541. default:
  3542. WARN_SYNTAX_ERROR;
  3543. return (l);
  3544. }
  3545. }
  3546. else if (line_skip_StarChar (l))
  3547. {
  3548. /* MAT A = B * C */
  3549. int i;
  3550. int j;
  3551. int k;
  3552. VariableType *v_C;
  3553. char varname_C[NameLengthMax + 1];
  3554. if (v_A->dimensions != 2)
  3555. {
  3556. WARN_SUBSCRIPT_OUT_OF_RANGE;
  3557. return (l);
  3558. }
  3559. if (v_B->dimensions != 2)
  3560. {
  3561. WARN_SUBSCRIPT_OUT_OF_RANGE;
  3562. return (l);
  3563. }
  3564. if (line_read_varname (l, varname_C) == FALSE)
  3565. {
  3566. WARN_SYNTAX_ERROR;
  3567. return (l);
  3568. }
  3569. if ((v_C = mat_find (varname_C)) == NULL)
  3570. {
  3571. WARN_VARIABLE_NOT_DECLARED;
  3572. return (l);
  3573. }
  3574. /* variable MUST be numeric */
  3575. if (VAR_IS_STRING (v_C))
  3576. {
  3577. WARN_TYPE_MISMATCH;
  3578. return (l);
  3579. }
  3580. if (line_read_matrix_redim (l, v_C) == FALSE)
  3581. {
  3582. WARN_SYNTAX_ERROR;
  3583. return (l);
  3584. }
  3585. if (v_C->dimensions != 2)
  3586. {
  3587. WARN_SUBSCRIPT_OUT_OF_RANGE;
  3588. return (l);
  3589. }
  3590. if (v_A->LBOUND[0] != v_B->LBOUND[0])
  3591. {
  3592. WARN_SUBSCRIPT_OUT_OF_RANGE;
  3593. return (l);
  3594. }
  3595. if (v_A->UBOUND[0] != v_B->UBOUND[0])
  3596. {
  3597. WARN_SUBSCRIPT_OUT_OF_RANGE;
  3598. return (l);
  3599. }
  3600. if (v_A->LBOUND[1] != v_C->LBOUND[1])
  3601. {
  3602. WARN_SUBSCRIPT_OUT_OF_RANGE;
  3603. return (l);
  3604. }
  3605. if (v_A->UBOUND[1] != v_C->UBOUND[1])
  3606. {
  3607. WARN_SUBSCRIPT_OUT_OF_RANGE;
  3608. return (l);
  3609. }
  3610. if (v_B->LBOUND[1] != v_C->LBOUND[0])
  3611. {
  3612. WARN_SUBSCRIPT_OUT_OF_RANGE;
  3613. return (l);
  3614. }
  3615. if (v_B->UBOUND[1] != v_C->UBOUND[0])
  3616. {
  3617. WARN_SUBSCRIPT_OUT_OF_RANGE;
  3618. return (l);
  3619. }
  3620. for (i = v_A->LBOUND[0]; i <= v_A->UBOUND[0]; i++)
  3621. {
  3622. for (j = v_A->LBOUND[1]; j <= v_A->UBOUND[1]; j++)
  3623. {
  3624. VariantType variant_A;
  3625. CLEAR_VARIANT (&variant_A);
  3626. variant_A.VariantTypeCode = v_A->VariableTypeCode;
  3627. variant_A.Number = 0;
  3628. v_A->VINDEX[0] = i;
  3629. v_A->VINDEX[1] = j;
  3630. if (var_set (v_A, &variant_A) == FALSE)
  3631. {
  3632. WARN_VARIABLE_NOT_DECLARED;
  3633. return (l);
  3634. }
  3635. for (k = v_C->LBOUND[0]; k <= v_C->UBOUND[0]; k++)
  3636. {
  3637. VariantType variant_B;
  3638. VariantType variant_C;
  3639. CLEAR_VARIANT (&variant_B);
  3640. CLEAR_VARIANT (&variant_C);
  3641. v_A->VINDEX[0] = i;
  3642. v_A->VINDEX[1] = j;
  3643. if (var_get (v_A, &variant_A) == FALSE)
  3644. {
  3645. WARN_VARIABLE_NOT_DECLARED;
  3646. return (l);
  3647. }
  3648. v_B->VINDEX[0] = i;
  3649. v_B->VINDEX[1] = k;
  3650. if (var_get (v_B, &variant_B) == FALSE)
  3651. {
  3652. WARN_VARIABLE_NOT_DECLARED;
  3653. return (l);
  3654. }
  3655. v_C->VINDEX[0] = k;
  3656. v_C->VINDEX[1] = j;
  3657. if (var_get (v_C, &variant_C) == FALSE)
  3658. {
  3659. WARN_VARIABLE_NOT_DECLARED;
  3660. return (l);
  3661. }
  3662. variant_A.Number += variant_B.Number * variant_C.Number;
  3663. v_A->VINDEX[0] = i;
  3664. v_A->VINDEX[1] = j;
  3665. if (var_set (v_A, &variant_A) == FALSE)
  3666. {
  3667. WARN_VARIABLE_NOT_DECLARED;
  3668. return (l);
  3669. }
  3670. }
  3671. }
  3672. }
  3673. }
  3674. else
  3675. {
  3676. WARN_SYNTAX_ERROR;
  3677. return (l);
  3678. }
  3679. }
  3680. return (l);
  3681. }
  3682. /*
  3683. --------------------------------------------------------------------------------------------
  3684. STORE
  3685. --------------------------------------------------------------------------------------------
  3686. */
  3687. LineType *
  3688. bwb_STORE (LineType * l)
  3689. {
  3690. /*
  3691. SYNTAX: STORE NumericArrayName
  3692. */
  3693. assert (l != NULL);
  3694. return bwb_CSAVE8 (l);
  3695. }
  3696. /*
  3697. --------------------------------------------------------------------------------------------
  3698. CSAVE*
  3699. --------------------------------------------------------------------------------------------
  3700. */
  3701. #define CSAVE_VERSION_1 0x20150218L
  3702. LineType *
  3703. bwb_CSAVE8 (LineType * l)
  3704. {
  3705. /*
  3706. SYNTAX: CSAVE* NumericArrayName
  3707. */
  3708. VariableType *v = NULL;
  3709. FILE *f;
  3710. unsigned long n;
  3711. size_t t;
  3712. char varname[NameLengthMax + 1];
  3713. assert (l != NULL);
  3714. if (line_read_varname (l, varname) == FALSE)
  3715. {
  3716. WARN_SYNTAX_ERROR;
  3717. return (l);
  3718. }
  3719. v = mat_find (varname);
  3720. if (v == NULL)
  3721. {
  3722. WARN_VARIABLE_NOT_DECLARED;
  3723. return (l);
  3724. }
  3725. /* variable MUST be numeric */
  3726. if (VAR_IS_STRING (v))
  3727. {
  3728. WARN_SYNTAX_ERROR;
  3729. return (l);
  3730. }
  3731. /* variable MUST be an array */
  3732. if (v->dimensions == 0)
  3733. {
  3734. WARN_SYNTAX_ERROR;
  3735. return (l);
  3736. }
  3737. if (line_read_matrix_redim (l, v) == FALSE)
  3738. {
  3739. WARN_SYNTAX_ERROR;
  3740. return (l);
  3741. }
  3742. /* variable storage is a mess, we bypass that tradition here. */
  3743. t = v->array_units;
  3744. if (t <= 1)
  3745. {
  3746. WARN_SYNTAX_ERROR;
  3747. return (l);
  3748. }
  3749. /* open file */
  3750. f = fopen (v->name, "w");
  3751. if (f == NULL)
  3752. {
  3753. WARN_SYNTAX_ERROR;
  3754. return (l);
  3755. }
  3756. /* write version number */
  3757. n = CSAVE_VERSION_1;
  3758. fwrite (&n, sizeof (long), 1, f);
  3759. /* write total number of elements */
  3760. fwrite (&t, sizeof (long), 1, f);
  3761. /* write data */
  3762. fwrite (v->Value.Number, sizeof (DoubleType), t, f);
  3763. /* OK */
  3764. bwb_fclose (f);
  3765. return (l);
  3766. }
  3767. /*
  3768. --------------------------------------------------------------------------------------------
  3769. RECALL
  3770. --------------------------------------------------------------------------------------------
  3771. */
  3772. LineType *
  3773. bwb_RECALL (LineType * l)
  3774. {
  3775. /*
  3776. SYNTAX: RECALL NumericArrayName
  3777. */
  3778. assert (l != NULL);
  3779. return bwb_CLOAD8 (l);
  3780. }
  3781. /*
  3782. --------------------------------------------------------------------------------------------
  3783. CLOAD*
  3784. --------------------------------------------------------------------------------------------
  3785. */
  3786. LineType *
  3787. bwb_CLOAD8 (LineType * l)
  3788. {
  3789. /*
  3790. SYNTAX: CLOAD* NumericArrayName
  3791. */
  3792. VariableType *v = NULL;
  3793. FILE *f;
  3794. unsigned long n;
  3795. size_t t;
  3796. char varname[NameLengthMax + 1];
  3797. assert (l != NULL);
  3798. if (line_read_varname (l, varname) == FALSE)
  3799. {
  3800. WARN_SYNTAX_ERROR;
  3801. return (l);
  3802. }
  3803. v = mat_find (varname);
  3804. if (v == NULL)
  3805. {
  3806. WARN_VARIABLE_NOT_DECLARED;
  3807. return (l);
  3808. }
  3809. /* variable MUST be numeric */
  3810. if (VAR_IS_STRING (v))
  3811. {
  3812. WARN_SYNTAX_ERROR;
  3813. return (l);
  3814. }
  3815. /* variable MUST be an array */
  3816. if (v->dimensions == 0)
  3817. {
  3818. WARN_SYNTAX_ERROR;
  3819. return (l);
  3820. }
  3821. if (line_read_matrix_redim (l, v) == FALSE)
  3822. {
  3823. WARN_SYNTAX_ERROR;
  3824. return (l);
  3825. }
  3826. /* variable storage is a mess, we bypass that tradition here. */
  3827. t = v->array_units;
  3828. if (t <= 1)
  3829. {
  3830. WARN_SYNTAX_ERROR;
  3831. return (l);
  3832. }
  3833. /* open file */
  3834. f = fopen (v->name, "r");
  3835. if (f == NULL)
  3836. {
  3837. WARN_BAD_FILE_NAME;
  3838. return (l);
  3839. }
  3840. /* read version number */
  3841. n = 0;
  3842. fread (&n, sizeof (long), 1, f);
  3843. if (n != CSAVE_VERSION_1)
  3844. {
  3845. bwb_fclose (f);
  3846. WARN_BAD_FILE_NAME;
  3847. return (l);
  3848. }
  3849. /* read total number of elements */
  3850. n = 0;
  3851. fread (&n, sizeof (long), 1, f);
  3852. if (n != t)
  3853. {
  3854. bwb_fclose (f);
  3855. WARN_BAD_FILE_NAME;
  3856. return (l);
  3857. }
  3858. /* read data */
  3859. fread (v->Value.Number, sizeof (DoubleType), t, f);
  3860. /* OK */
  3861. bwb_fclose (f);
  3862. return (l);
  3863. }
  3864. /*
  3865. ============================================================================================
  3866. SAVE and so on
  3867. ============================================================================================
  3868. */
  3869. static LineType *
  3870. bwb_save (LineType * Line, char *Prompt)
  3871. {
  3872. /*
  3873. SYNTAX: SAVE [filename$]
  3874. */
  3875. FILE *outfile;
  3876. assert (Line != NULL);
  3877. assert (Prompt != NULL);
  3878. assert( My != NULL );
  3879. assert( My->ConsoleInput != NULL );
  3880. assert( My->SYSOUT != NULL );
  3881. assert( My->SYSOUT->cfp != NULL );
  3882. /* Get an argument for filename */
  3883. if (line_is_eol (Line))
  3884. {
  3885. /* default is the last filename used by LOAD or SAVE */
  3886. if (is_empty_string (My->ProgramFilename) && Prompt != NULL)
  3887. {
  3888. /* prompt for the program name */
  3889. char *tbuf;
  3890. int tlen;
  3891. tbuf = My->ConsoleInput;
  3892. tlen = MAX_LINE_LENGTH;
  3893. bwx_input (Prompt, FALSE, tbuf, tlen);
  3894. if (is_empty_string (tbuf))
  3895. {
  3896. WARN_BAD_FILE_NAME;
  3897. return (Line);
  3898. }
  3899. if (My->ProgramFilename != NULL)
  3900. {
  3901. free (My->ProgramFilename);
  3902. My->ProgramFilename = NULL;
  3903. }
  3904. My->ProgramFilename = bwb_strdup (tbuf);
  3905. }
  3906. assert( My->ProgramFilename != NULL );
  3907. fprintf (My->SYSOUT->cfp, "Saving %s\n", My->ProgramFilename);
  3908. ResetConsoleColumn ();
  3909. }
  3910. else
  3911. {
  3912. char *Value;
  3913. Value = NULL;
  3914. if (line_read_string_expression (Line, &Value) == FALSE)
  3915. {
  3916. WARN_SYNTAX_ERROR;
  3917. return (Line);
  3918. }
  3919. if (is_empty_string (Value))
  3920. {
  3921. WARN_BAD_FILE_NAME;
  3922. return (Line);
  3923. }
  3924. if (My->ProgramFilename != NULL)
  3925. {
  3926. free (My->ProgramFilename);
  3927. }
  3928. My->ProgramFilename = Value;
  3929. }
  3930. assert( My->ProgramFilename != NULL );
  3931. if ((outfile = fopen (My->ProgramFilename, "w")) == NULL)
  3932. {
  3933. WARN_BAD_FILE_NAME;
  3934. return (Line);
  3935. }
  3936. bwb_xlist (Line, outfile);
  3937. bwb_fclose (outfile);
  3938. return (Line);
  3939. }
  3940. /*
  3941. --------------------------------------------------------------------------------------------
  3942. CSAVE
  3943. --------------------------------------------------------------------------------------------
  3944. */
  3945. LineType *
  3946. bwb_CSAVE (LineType * Line)
  3947. {
  3948. /*
  3949. SYNTAX: CSAVE [filename$]
  3950. */
  3951. assert (Line != NULL);
  3952. return bwb_save (Line, "CSAVE FILE NAME:");
  3953. }
  3954. /*
  3955. --------------------------------------------------------------------------------------------
  3956. REPLACE
  3957. --------------------------------------------------------------------------------------------
  3958. */
  3959. LineType *
  3960. bwb_REPLACE (LineType * Line)
  3961. {
  3962. /*
  3963. SYNTAX: REPLACE [filename$]
  3964. */
  3965. assert (Line != NULL);
  3966. return bwb_save (Line, "REPLACE FILE NAME:");
  3967. }
  3968. /*
  3969. --------------------------------------------------------------------------------------------
  3970. SAVE
  3971. --------------------------------------------------------------------------------------------
  3972. */
  3973. LineType *
  3974. bwb_SAVE (LineType * l)
  3975. {
  3976. /*
  3977. SYNTAX: SAVE [filename$]
  3978. */
  3979. assert (l != NULL);
  3980. return bwb_save (l, "SAVE FILE NAME:");
  3981. }
  3982. /*
  3983. --------------------------------------------------------------------------------------------
  3984. TSAVE
  3985. --------------------------------------------------------------------------------------------
  3986. */
  3987. LineType *
  3988. bwb_TSAVE (LineType * Line)
  3989. {
  3990. /*
  3991. SYNTAX: TSAVE [filename$]
  3992. */
  3993. assert (Line != NULL);
  3994. return bwb_save (Line, "TSAVE FILE NAME:");
  3995. }
  3996. /*
  3997. ============================================================================================
  3998. LIST and so on
  3999. ============================================================================================
  4000. */
  4001. static int
  4002. xl_line (FILE * file, LineType * l)
  4003. {
  4004. char LineExecuted;
  4005. char *C; /* start of comment text */
  4006. char *buffer; /* 0...99999 */
  4007. assert (file != NULL);
  4008. assert (l != NULL);
  4009. assert( My != NULL );
  4010. assert( My->NumLenBuffer != NULL );
  4011. assert( My->CurrentVersion != NULL );
  4012. assert( My->SYSOUT != NULL );
  4013. assert( My->SYSOUT->cfp != NULL );
  4014. assert( My->SYSPRN != NULL );
  4015. assert( My->SYSPRN->cfp != NULL );
  4016. /*
  4017. ** The only difference between LIST, LLIST and SAVE is:
  4018. ** LIST and LLIST display an '*'
  4019. ** when a line has been executed
  4020. ** and OPTION COVERAGE ON is enabled.
  4021. */
  4022. buffer = My->NumLenBuffer;
  4023. LineExecuted = ' ';
  4024. if (My->CurrentVersion->OptionFlags & (OPTION_COVERAGE_ON))
  4025. {
  4026. if (l->LineFlags & LINE_EXECUTED)
  4027. {
  4028. if (file == My->SYSOUT->cfp || file == My->SYSPRN->cfp)
  4029. {
  4030. /* LIST */
  4031. /* LLIST */
  4032. LineExecuted = '*';
  4033. }
  4034. else
  4035. {
  4036. /* SAVE */
  4037. /* EDIT implies SAVE */
  4038. }
  4039. }
  4040. }
  4041. C = l->buffer;
  4042. if (l->LineFlags & LINE_NUMBERED)
  4043. {
  4044. /* explicitly numbered */
  4045. sprintf (buffer, "%*d", LineNumberDigits, l->number);
  4046. /* ##### xxx */
  4047. }
  4048. else
  4049. {
  4050. /* implicitly numbered */
  4051. if (My->LastLineNumber == l->number)
  4052. {
  4053. /* multi-statement line */
  4054. if (l->cmdnum == C_REM
  4055. && IS_CHAR (l->buffer[0], My->CurrentVersion->OptionCommentChar))
  4056. {
  4057. /* trailing comment */
  4058. sprintf (buffer, "%*s%c", LineNumberDigits - 1, "",
  4059. My->CurrentVersion->OptionCommentChar);
  4060. C++; /* skip comment char */
  4061. while (*C == ' ')
  4062. {
  4063. /* skip spaces */
  4064. C++;
  4065. }
  4066. /* ____' xxx */
  4067. }
  4068. else if (My->CurrentVersion->OptionStatementChar)
  4069. {
  4070. /* all other commands, add a colon */
  4071. sprintf (buffer, "%*s%c", LineNumberDigits - 1, "",
  4072. My->CurrentVersion->OptionStatementChar);
  4073. /* ____: xxx */
  4074. }
  4075. else
  4076. {
  4077. /*
  4078. The user is trying to list a multi-line statement
  4079. in a dialect that does NOT support multi-line statements.
  4080. This could occur when LOADing in one dialect and then SAVEing as another dialect, such as:
  4081. OPTION VERSION BASIC-80
  4082. LOAD "TEST1.BAS"
  4083. 100 REM TEST
  4084. 110 PRINT:PRINT:PRINT
  4085. OPTION VERSION MARK-I
  4086. EDIT
  4087. 100 REM TEST
  4088. 110 PRINT
  4089. PRINT
  4090. PRINT
  4091. The only thing we can reasonably do is put spaces for the line number,
  4092. since the user will have to edit the results manually anyways.
  4093. */
  4094. sprintf (buffer, "%*s", LineNumberDigits, "");
  4095. /* _____ xxx */
  4096. }
  4097. }
  4098. else
  4099. {
  4100. /* single-statement line */
  4101. sprintf (buffer, "%*s", LineNumberDigits, "");
  4102. /* _____ xxx */
  4103. }
  4104. }
  4105. fprintf (file, "%s", buffer);
  4106. fprintf (file, "%c", LineExecuted);
  4107. /* if( TRUE ) */
  4108. {
  4109. /* %INCLUDE */
  4110. int i;
  4111. for (i = 0; i < l->IncludeLevel; i++)
  4112. {
  4113. fputc (' ', file);
  4114. }
  4115. }
  4116. if (My->OptionIndentInteger > 0)
  4117. {
  4118. int i;
  4119. for (i = 0; i < l->Indention; i++)
  4120. {
  4121. int j;
  4122. for (j = 0; j < My->OptionIndentInteger; j++)
  4123. {
  4124. fputc (' ', file);
  4125. }
  4126. }
  4127. }
  4128. fprintf (file, "%s\n", C);
  4129. My->LastLineNumber = l->number;
  4130. return TRUE;
  4131. }
  4132. static LineType *
  4133. bwb_xlist (LineType * l, FILE * file)
  4134. {
  4135. assert (l != NULL);
  4136. assert (file != NULL);
  4137. assert( My != NULL );
  4138. assert( My->StartMarker != NULL );
  4139. assert( My->EndMarker != NULL );
  4140. /*
  4141. **
  4142. ** FORCE SCAN
  4143. **
  4144. */
  4145. if (bwb_scan () == FALSE)
  4146. {
  4147. /*
  4148. **
  4149. ** we are used by bwb_SAVE and bwb_EDIT
  4150. **
  4151. WARN_CANT_CONTINUE;
  4152. return (l);
  4153. */
  4154. }
  4155. if (line_is_eol (l))
  4156. {
  4157. /* LIST */
  4158. LineType *x;
  4159. /* now go through and list appropriate lines */
  4160. My->LastLineNumber = -1;
  4161. for (x = My->StartMarker->next; x != My->EndMarker; x = x->next)
  4162. {
  4163. xl_line (file, x);
  4164. }
  4165. fprintf (file, "\n");
  4166. }
  4167. else
  4168. {
  4169. do
  4170. {
  4171. int head;
  4172. int tail;
  4173. if (line_read_line_sequence (l, &head, &tail))
  4174. {
  4175. /* LIST 's' - 'e' */
  4176. LineType *x;
  4177. if (head < MINLIN || head > MAXLIN)
  4178. {
  4179. WARN_UNDEFINED_LINE;
  4180. return (l);
  4181. }
  4182. if (tail < MINLIN || tail > MAXLIN)
  4183. {
  4184. WARN_UNDEFINED_LINE;
  4185. return (l);
  4186. }
  4187. if (head > tail)
  4188. {
  4189. WARN_UNDEFINED_LINE;
  4190. return (l);
  4191. }
  4192. /* valid range */
  4193. /* now go through and list appropriate lines */
  4194. My->LastLineNumber = -1;
  4195. for (x = My->StartMarker->next; x != My->EndMarker; x = x->next)
  4196. {
  4197. if (head <= x->number && x->number <= tail)
  4198. {
  4199. xl_line (file, x);
  4200. }
  4201. }
  4202. fprintf (file, "\n");
  4203. }
  4204. else
  4205. {
  4206. WARN_SYNTAX_ERROR;
  4207. return (l);
  4208. }
  4209. }
  4210. while (line_skip_seperator (l));
  4211. }
  4212. if (file == My->SYSOUT->cfp)
  4213. {
  4214. ResetConsoleColumn ();
  4215. }
  4216. return (l);
  4217. }
  4218. /*
  4219. --------------------------------------------------------------------------------------------
  4220. LIST
  4221. --------------------------------------------------------------------------------------------
  4222. */
  4223. LineType *
  4224. bwb_LIST (LineType * l)
  4225. {
  4226. /*
  4227. SYNTAX: LIST
  4228. SYNTAX: LIST line [,...]
  4229. SYNTAX: LIST line - line
  4230. */
  4231. assert (l != NULL);
  4232. return bwb_xlist (l, My->SYSOUT->cfp);
  4233. }
  4234. /*
  4235. --------------------------------------------------------------------------------------------
  4236. LISTNH
  4237. --------------------------------------------------------------------------------------------
  4238. */
  4239. LineType *
  4240. bwb_LISTNH (LineType * l)
  4241. {
  4242. /*
  4243. SYNTAX: LISTNH
  4244. SYNTAX: LISTNH line [,...]
  4245. SYNTAX: LISTNH line - line
  4246. */
  4247. assert (l != NULL);
  4248. return bwb_xlist (l, My->SYSOUT->cfp);
  4249. }
  4250. /*
  4251. --------------------------------------------------------------------------------------------
  4252. LLIST
  4253. --------------------------------------------------------------------------------------------
  4254. */
  4255. LineType *
  4256. bwb_LLIST (LineType * l)
  4257. {
  4258. /*
  4259. SYNTAX: LLIST
  4260. SYNTAX: LLIST line [,...]
  4261. SYNTAX: LLIST line - line
  4262. */
  4263. assert (l != NULL);
  4264. return bwb_xlist (l, My->SYSPRN->cfp);
  4265. }
  4266. /*
  4267. ============================================================================================
  4268. DELETE and so on
  4269. ============================================================================================
  4270. */
  4271. static LineType *
  4272. bwb_delete (LineType * l)
  4273. {
  4274. assert (l != NULL);
  4275. assert( My != NULL );
  4276. assert( My->CurrentVersion != NULL );
  4277. assert( My->StartMarker != NULL );
  4278. assert( My->EndMarker != NULL );
  4279. if (line_is_eol (l))
  4280. {
  4281. /* DELETE */
  4282. WARN_SYNTAX_ERROR;
  4283. return (l);
  4284. }
  4285. else if (My->CurrentVersion->OptionVersionValue & (C77))
  4286. {
  4287. /*
  4288. SYNTAX: DELETE filenum [,...]
  4289. */
  4290. do
  4291. {
  4292. int FileNumber;
  4293. FileNumber = 0;
  4294. if (line_read_integer_expression (l, &FileNumber) == FALSE)
  4295. {
  4296. WARN_BAD_FILE_NUMBER;
  4297. return (l);
  4298. }
  4299. if (FileNumber <= 0)
  4300. {
  4301. WARN_BAD_FILE_NUMBER;
  4302. return (l);
  4303. }
  4304. My->CurrentFile = find_file_by_number (FileNumber);
  4305. if (My->CurrentFile == NULL)
  4306. {
  4307. WARN_BAD_FILE_NUMBER;
  4308. return (l);
  4309. }
  4310. if (My->CurrentFile->DevMode == DEVMODE_CLOSED)
  4311. {
  4312. WARN_BAD_FILE_NUMBER;
  4313. return (l);
  4314. }
  4315. if (My->CurrentFile->cfp != NULL)
  4316. {
  4317. bwb_fclose (My->CurrentFile->cfp);
  4318. My->CurrentFile->cfp = NULL;
  4319. }
  4320. if (My->CurrentFile->buffer != NULL)
  4321. {
  4322. free (My->CurrentFile->buffer);
  4323. My->CurrentFile->buffer = NULL;
  4324. }
  4325. My->CurrentFile->width = 0;
  4326. My->CurrentFile->col = 1;
  4327. My->CurrentFile->row = 1;
  4328. My->CurrentFile->delimit = ',';
  4329. My->CurrentFile->DevMode = DEVMODE_CLOSED;
  4330. if (My->CurrentFile->FileName == NULL)
  4331. {
  4332. WARN_BAD_FILE_NAME;
  4333. return (l);
  4334. }
  4335. remove (My->CurrentFile->FileName);
  4336. free (My->CurrentFile->FileName);
  4337. My->CurrentFile->FileName = NULL;
  4338. }
  4339. while (line_skip_seperator (l));
  4340. /* OK */
  4341. return (l);
  4342. }
  4343. else
  4344. {
  4345. /*
  4346. SYNTAX: DELETE line [,...]
  4347. SYNTAX: DELETE line - line
  4348. */
  4349. do
  4350. {
  4351. int head;
  4352. int tail;
  4353. if (line_read_line_sequence (l, &head, &tail))
  4354. {
  4355. /* DELETE 's' - 'e' */
  4356. LineType *x;
  4357. LineType *previous;
  4358. if (head < MINLIN || head > MAXLIN)
  4359. {
  4360. WARN_UNDEFINED_LINE;
  4361. return (l);
  4362. }
  4363. if (tail < MINLIN || tail > MAXLIN)
  4364. {
  4365. WARN_UNDEFINED_LINE;
  4366. return (l);
  4367. }
  4368. if (head > tail)
  4369. {
  4370. WARN_UNDEFINED_LINE;
  4371. return (l);
  4372. }
  4373. /* valid range */
  4374. /* avoid deleting ourself */
  4375. if (l->LineFlags & (LINE_USER))
  4376. {
  4377. /* console line (immediate mode) */
  4378. }
  4379. else if (head <= l->number && l->number <= tail)
  4380. {
  4381. /* 100 DELETE 100 */
  4382. WARN_CANT_CONTINUE;
  4383. return (l);
  4384. }
  4385. /* now go through and list appropriate lines */
  4386. previous = My->StartMarker;
  4387. for (x = My->StartMarker->next; x != My->EndMarker;)
  4388. {
  4389. LineType *next;
  4390. next = x->next;
  4391. if (x->number < head)
  4392. {
  4393. previous = x;
  4394. }
  4395. else if (head <= x->number && x->number <= tail)
  4396. {
  4397. if (x == l)
  4398. {
  4399. /* 100 DELETE 100 */
  4400. WARN_CANT_CONTINUE;
  4401. return (l);
  4402. }
  4403. bwb_freeline (x);
  4404. previous->next = next;
  4405. }
  4406. x = next;
  4407. }
  4408. }
  4409. else
  4410. {
  4411. WARN_SYNTAX_ERROR;
  4412. return (l);
  4413. }
  4414. }
  4415. while (line_skip_seperator (l));
  4416. /*
  4417. **
  4418. ** FORCE SCAN
  4419. **
  4420. */
  4421. if (bwb_scan () == FALSE)
  4422. {
  4423. WARN_CANT_CONTINUE;
  4424. return (l);
  4425. }
  4426. }
  4427. return (l);
  4428. }
  4429. /*
  4430. --------------------------------------------------------------------------------------------
  4431. DELETE
  4432. --------------------------------------------------------------------------------------------
  4433. */
  4434. LineType *
  4435. bwb_DELETE (LineType * l)
  4436. {
  4437. assert (l != NULL);
  4438. return bwb_delete (l);
  4439. }
  4440. /*
  4441. --------------------------------------------------------------------------------------------
  4442. PDEL
  4443. --------------------------------------------------------------------------------------------
  4444. */
  4445. LineType *
  4446. bwb_PDEL (LineType * l)
  4447. {
  4448. assert (l != NULL);
  4449. return bwb_delete (l);
  4450. }
  4451. #if FALSE /* keep the source to DONUM and DOUNNUM */
  4452. /*
  4453. --------------------------------------------------------------------------------------------
  4454. DONUM
  4455. --------------------------------------------------------------------------------------------
  4456. */
  4457. LineType *
  4458. bwb_donum (LineType * l)
  4459. {
  4460. /*
  4461. SYNTAX: DONUM
  4462. */
  4463. LineType *current;
  4464. int lnumber;
  4465. assert (l != NULL);
  4466. assert( My != NULL );
  4467. assert( My->StartMarker != NULL );
  4468. assert( My->EndMarker != NULL );
  4469. lnumber = 10;
  4470. for (current = My->StartMarker->next; current != My->EndMarker;
  4471. current = current->next)
  4472. {
  4473. current->number = lnumber;
  4474. lnumber += 10;
  4475. if (lnumber > MAXLIN)
  4476. {
  4477. return (l);
  4478. }
  4479. }
  4480. return (l);
  4481. }
  4482. /*
  4483. --------------------------------------------------------------------------------------------
  4484. DOUNUM
  4485. --------------------------------------------------------------------------------------------
  4486. */
  4487. LineType *
  4488. bwb_dounnum (LineType * l)
  4489. {
  4490. /*
  4491. SYNTAX: DOUNNUM
  4492. */
  4493. LineType *current;
  4494. assert (l != NULL);
  4495. assert( My != NULL );
  4496. assert( My->StartMarker != NULL );
  4497. assert( My->EndMarker != NULL );
  4498. for (current = My->StartMarker->next; current != My->EndMarker;
  4499. current = current->next)
  4500. {
  4501. current->number = 0;
  4502. }
  4503. return (l);
  4504. }
  4505. #endif /* FALSE */
  4506. /*
  4507. --------------------------------------------------------------------------------------------
  4508. FILES
  4509. --------------------------------------------------------------------------------------------
  4510. */
  4511. LineType *
  4512. bwb_FILES (LineType * l)
  4513. {
  4514. /*
  4515. SYNTAX: FILES A$ [, ...]
  4516. */
  4517. /* open a list of files in READ mode */
  4518. assert (l != NULL);
  4519. assert( My != NULL );
  4520. do
  4521. {
  4522. int FileNumber;
  4523. FileNumber = My->LastFileNumber;
  4524. FileNumber++;
  4525. if (FileNumber < 0)
  4526. {
  4527. WARN_BAD_FILE_NUMBER;
  4528. return (l);
  4529. }
  4530. if (FileNumber == 0)
  4531. {
  4532. WARN_BAD_FILE_NUMBER;
  4533. return (l);
  4534. }
  4535. My->CurrentFile = find_file_by_number (FileNumber);
  4536. if (My->CurrentFile == NULL)
  4537. {
  4538. My->CurrentFile = file_new ();
  4539. My->CurrentFile->FileNumber = FileNumber;
  4540. }
  4541. {
  4542. char *Value;
  4543. Value = NULL;
  4544. if (line_read_string_expression (l, &Value) == FALSE)
  4545. {
  4546. WARN_SYNTAX_ERROR;
  4547. return (l);
  4548. }
  4549. if (Value == NULL)
  4550. {
  4551. WARN_SYNTAX_ERROR;
  4552. return (l);
  4553. }
  4554. if (My->CurrentFile->FileName != NULL)
  4555. {
  4556. free (My->CurrentFile->FileName);
  4557. My->CurrentFile->FileName = NULL;
  4558. }
  4559. My->CurrentFile->FileName = Value;
  4560. Value = NULL;
  4561. }
  4562. if (My->CurrentFile->DevMode != DEVMODE_CLOSED)
  4563. {
  4564. My->CurrentFile->DevMode = DEVMODE_CLOSED;
  4565. }
  4566. if (My->CurrentFile->cfp != NULL)
  4567. {
  4568. bwb_fclose (My->CurrentFile->cfp);
  4569. My->CurrentFile->cfp = NULL;
  4570. }
  4571. if (My->CurrentFile->buffer != NULL)
  4572. {
  4573. free (My->CurrentFile->buffer);
  4574. My->CurrentFile->buffer = NULL;
  4575. }
  4576. My->CurrentFile->width = 0;
  4577. My->CurrentFile->col = 1;
  4578. My->CurrentFile->row = 1;
  4579. My->CurrentFile->delimit = ',';
  4580. if (is_empty_string (My->CurrentFile->FileName))
  4581. {
  4582. WARN_BAD_FILE_NAME;
  4583. return (l);
  4584. }
  4585. if (bwb_strcmp (My->CurrentFile->FileName, "*") != 0)
  4586. {
  4587. if ((My->CurrentFile->cfp =
  4588. fopen (My->CurrentFile->FileName, "r")) == NULL)
  4589. {
  4590. WARN_BAD_FILE_NAME;
  4591. return (l);
  4592. }
  4593. My->CurrentFile->DevMode = DEVMODE_INPUT;
  4594. }
  4595. My->LastFileNumber = FileNumber;
  4596. /* OK */
  4597. }
  4598. while (line_skip_seperator (l));
  4599. return (l);
  4600. }
  4601. /*
  4602. --------------------------------------------------------------------------------------------
  4603. FILE
  4604. --------------------------------------------------------------------------------------------
  4605. */
  4606. LineType *
  4607. bwb_FILE (LineType * l)
  4608. {
  4609. assert (l != NULL);
  4610. assert( My != NULL );
  4611. assert( My->CurrentVersion != NULL );
  4612. if (My->CurrentVersion->OptionVersionValue & (C77))
  4613. {
  4614. /*
  4615. CBASIC-II:
  4616. FILE file_name$ ' filename$ must be a simple string scalar (no arrays)
  4617. FILE file_name$ ( record_length% ) ' filename$ must be a simple string scalar (no arrays)
  4618. -- if the file exists,
  4619. then it is used,
  4620. else it is created.
  4621. -- Does not trigger IF END #
  4622. */
  4623. do
  4624. {
  4625. int FileNumber;
  4626. VariableType *v;
  4627. char varname[NameLengthMax + 1];
  4628. if (line_read_varname (l, varname) == FALSE)
  4629. {
  4630. WARN_BAD_FILE_NAME;
  4631. return (l);
  4632. }
  4633. if (is_empty_string (varname))
  4634. {
  4635. WARN_BAD_FILE_NAME;
  4636. return (l);
  4637. }
  4638. v = find_variable_by_type (varname, 0, StringTypeCode);
  4639. if (v == NULL)
  4640. {
  4641. WARN_VARIABLE_NOT_DECLARED;
  4642. return (l);
  4643. }
  4644. if (VAR_IS_STRING (v))
  4645. {
  4646. /* OK */
  4647. }
  4648. else
  4649. {
  4650. WARN_TYPE_MISMATCH;
  4651. return (l);
  4652. }
  4653. FileNumber = My->LastFileNumber;
  4654. FileNumber++;
  4655. if (FileNumber < 0)
  4656. {
  4657. WARN_BAD_FILE_NUMBER;
  4658. return (l);
  4659. }
  4660. if (FileNumber == 0)
  4661. {
  4662. WARN_BAD_FILE_NUMBER;
  4663. return (l);
  4664. }
  4665. My->CurrentFile = find_file_by_number (FileNumber);
  4666. if (My->CurrentFile == NULL)
  4667. {
  4668. My->CurrentFile = file_new ();
  4669. My->CurrentFile->FileNumber = FileNumber;
  4670. }
  4671. if (My->CurrentFile->DevMode != DEVMODE_CLOSED)
  4672. {
  4673. My->CurrentFile->DevMode = DEVMODE_CLOSED;
  4674. }
  4675. if (My->CurrentFile->cfp != NULL)
  4676. {
  4677. bwb_fclose (My->CurrentFile->cfp);
  4678. My->CurrentFile->cfp = NULL;
  4679. }
  4680. if (My->CurrentFile->buffer != NULL)
  4681. {
  4682. free (My->CurrentFile->buffer);
  4683. My->CurrentFile->buffer = NULL;
  4684. }
  4685. My->CurrentFile->width = 0;
  4686. My->CurrentFile->col = 1;
  4687. My->CurrentFile->row = 1;
  4688. My->CurrentFile->delimit = ',';
  4689. /* OK */
  4690. if (line_skip_LparenChar (l))
  4691. {
  4692. /* RANDOM file */
  4693. int RecLen;
  4694. if (line_read_integer_expression (l, &RecLen) == FALSE)
  4695. {
  4696. WARN_FIELD_OVERFLOW;
  4697. return (l);
  4698. }
  4699. if (RecLen <= 0)
  4700. {
  4701. WARN_FIELD_OVERFLOW;
  4702. return (l);
  4703. }
  4704. if (line_skip_RparenChar (l) == FALSE)
  4705. {
  4706. WARN_SYNTAX_ERROR;
  4707. return (l);
  4708. }
  4709. if ((My->CurrentFile->buffer =
  4710. (char *) calloc (RecLen + 1 /* NulChar */ ,
  4711. sizeof (char))) == NULL)
  4712. {
  4713. WARN_OUT_OF_MEMORY;
  4714. return (l);
  4715. }
  4716. My->CurrentFile->width = RecLen;
  4717. }
  4718. /* if( TRUE ) */
  4719. {
  4720. VariantType variant;
  4721. CLEAR_VARIANT (&variant);
  4722. if (var_get (v, &variant) == FALSE)
  4723. {
  4724. WARN_VARIABLE_NOT_DECLARED;
  4725. return (l);
  4726. }
  4727. if (variant.VariantTypeCode == StringTypeCode)
  4728. {
  4729. if (My->CurrentFile->FileName != NULL)
  4730. {
  4731. free (My->CurrentFile->FileName);
  4732. My->CurrentFile->FileName = NULL;
  4733. }
  4734. My->CurrentFile->FileName = variant.Buffer;
  4735. variant.Buffer = NULL;
  4736. }
  4737. else
  4738. {
  4739. WARN_TYPE_MISMATCH;
  4740. return (l);
  4741. }
  4742. }
  4743. if (is_empty_string (My->CurrentFile->FileName))
  4744. {
  4745. WARN_BAD_FILE_NAME;
  4746. return (l);
  4747. }
  4748. My->CurrentFile->cfp = fopen (My->CurrentFile->FileName, "r+");
  4749. if (My->CurrentFile->cfp == NULL)
  4750. {
  4751. My->CurrentFile->cfp = fopen (My->CurrentFile->FileName, "w");
  4752. if (My->CurrentFile->cfp != NULL)
  4753. {
  4754. bwb_fclose (My->CurrentFile->cfp);
  4755. My->CurrentFile->cfp = fopen (My->CurrentFile->FileName, "r+");
  4756. }
  4757. }
  4758. if (My->CurrentFile->cfp == NULL)
  4759. {
  4760. WARN_BAD_FILE_NAME;
  4761. return (l);
  4762. }
  4763. if (My->CurrentFile->width > 0)
  4764. {
  4765. /* RANDOM file */
  4766. My->CurrentFile->DevMode = DEVMODE_RANDOM;
  4767. }
  4768. else
  4769. {
  4770. /* SERIAL file */
  4771. My->CurrentFile->DevMode = DEVMODE_INPUT | DEVMODE_OUTPUT;
  4772. }
  4773. /* OK */
  4774. My->LastFileNumber = FileNumber;
  4775. }
  4776. while (line_skip_seperator (l));
  4777. /* OK */
  4778. return (l);
  4779. }
  4780. if (line_skip_FilenumChar (l))
  4781. {
  4782. /*
  4783. SYNTAX: FILE # X, A$
  4784. */
  4785. int FileNumber;
  4786. if (line_read_integer_expression (l, &FileNumber) == FALSE)
  4787. {
  4788. WARN_BAD_FILE_NUMBER;
  4789. return (l);
  4790. }
  4791. if (line_skip_seperator (l))
  4792. {
  4793. /* OK */
  4794. }
  4795. else
  4796. {
  4797. WARN_SYNTAX_ERROR;
  4798. return (l);
  4799. }
  4800. if (FileNumber < 0)
  4801. {
  4802. /* "FILE # -1" is an ERROR */
  4803. WARN_BAD_FILE_NUMBER;
  4804. return (l);
  4805. }
  4806. if (FileNumber == 0)
  4807. {
  4808. /* "FILE # 0" is an ERROR */
  4809. WARN_BAD_FILE_NUMBER;
  4810. return (l);
  4811. }
  4812. My->CurrentFile = find_file_by_number (FileNumber);
  4813. if (My->CurrentFile == NULL)
  4814. {
  4815. My->CurrentFile = file_new ();
  4816. My->CurrentFile->FileNumber = FileNumber;
  4817. }
  4818. {
  4819. char *Value;
  4820. Value = NULL;
  4821. if (line_read_string_expression (l, &Value) == FALSE)
  4822. {
  4823. WARN_SYNTAX_ERROR;
  4824. return (l);
  4825. }
  4826. if (Value == NULL)
  4827. {
  4828. WARN_SYNTAX_ERROR;
  4829. return (l);
  4830. }
  4831. if (My->CurrentFile->FileName != NULL)
  4832. {
  4833. free (My->CurrentFile->FileName);
  4834. My->CurrentFile->FileName = NULL;
  4835. }
  4836. My->CurrentFile->FileName = Value;
  4837. Value = NULL;
  4838. }
  4839. if (My->CurrentFile->DevMode != DEVMODE_CLOSED)
  4840. {
  4841. My->CurrentFile->DevMode = DEVMODE_CLOSED;
  4842. }
  4843. if (My->CurrentFile->cfp != NULL)
  4844. {
  4845. bwb_fclose (My->CurrentFile->cfp);
  4846. My->CurrentFile->cfp = NULL;
  4847. }
  4848. if (My->CurrentFile->buffer != NULL)
  4849. {
  4850. free (My->CurrentFile->buffer);
  4851. My->CurrentFile->buffer = NULL;
  4852. }
  4853. My->CurrentFile->width = 0;
  4854. My->CurrentFile->col = 1;
  4855. My->CurrentFile->row = 1;
  4856. My->CurrentFile->delimit = ',';
  4857. if (is_empty_string (My->CurrentFile->FileName))
  4858. {
  4859. WARN_BAD_FILE_NAME;
  4860. return (l);
  4861. }
  4862. if (bwb_strcmp (My->CurrentFile->FileName, "*") != 0)
  4863. {
  4864. if ((My->CurrentFile->cfp =
  4865. fopen (My->CurrentFile->FileName, "r")) == NULL)
  4866. {
  4867. WARN_BAD_FILE_NAME;
  4868. return (l);
  4869. }
  4870. My->CurrentFile->DevMode = DEVMODE_INPUT;
  4871. }
  4872. /* OK */
  4873. return (l);
  4874. }
  4875. WARN_SYNTAX_ERROR;
  4876. return (l);
  4877. }
  4878. /*
  4879. --------------------------------------------------------------------------------------------
  4880. DELIMIT
  4881. --------------------------------------------------------------------------------------------
  4882. */
  4883. LineType *
  4884. bwb_DELIMIT (LineType * l)
  4885. {
  4886. /*
  4887. SYNTAX: DELIMIT # X, A$
  4888. */
  4889. assert (l != NULL);
  4890. assert( My != NULL );
  4891. assert( My->SYSIN != NULL );
  4892. if (line_skip_FilenumChar (l))
  4893. {
  4894. /* DELIMIT # */
  4895. int FileNumber;
  4896. char delimit;
  4897. My->CurrentFile = My->SYSIN;
  4898. if (line_read_integer_expression (l, &FileNumber) == FALSE)
  4899. {
  4900. WARN_BAD_FILE_NUMBER;
  4901. return (l);
  4902. }
  4903. if (line_skip_seperator (l))
  4904. {
  4905. /* OK */
  4906. }
  4907. else
  4908. {
  4909. WARN_SYNTAX_ERROR;
  4910. return (l);
  4911. }
  4912. {
  4913. char *Value;
  4914. Value = NULL;
  4915. if (line_read_string_expression (l, &Value) == FALSE)
  4916. {
  4917. WARN_SYNTAX_ERROR;
  4918. return (l);
  4919. }
  4920. if (Value == NULL)
  4921. {
  4922. WARN_SYNTAX_ERROR;
  4923. return (l);
  4924. }
  4925. delimit = Value[0];
  4926. free (Value);
  4927. Value = NULL;
  4928. if (bwb_ispunct (delimit))
  4929. {
  4930. /* OK */
  4931. }
  4932. else
  4933. {
  4934. WARN_ILLEGAL_FUNCTION_CALL;
  4935. return (l);
  4936. }
  4937. }
  4938. if (FileNumber < 0)
  4939. {
  4940. /* "DELIMIT # -1" is SYSPRN */
  4941. My->SYSPRN->delimit = delimit;
  4942. return (l);
  4943. }
  4944. if (FileNumber == 0)
  4945. {
  4946. /* "DELIMIT # 0" is SYSOUT */
  4947. My->SYSOUT->delimit = delimit;
  4948. return (l);
  4949. }
  4950. /* normal file */
  4951. My->CurrentFile = find_file_by_number (FileNumber);
  4952. if (My->CurrentFile == NULL)
  4953. {
  4954. WARN_BAD_FILE_NUMBER;
  4955. return (l);
  4956. }
  4957. My->CurrentFile->delimit = delimit;
  4958. /* OK */
  4959. return (l);
  4960. }
  4961. WARN_SYNTAX_ERROR;
  4962. return (l);
  4963. }
  4964. /*
  4965. --------------------------------------------------------------------------------------------
  4966. MARGIN
  4967. --------------------------------------------------------------------------------------------
  4968. */
  4969. LineType *
  4970. bwb_MARGIN (LineType * l)
  4971. {
  4972. /*
  4973. SYNTAX: MARGIN # X, Y
  4974. */
  4975. /* set width for OUTPUT */
  4976. int FileNumber;
  4977. int Value;
  4978. assert (l != NULL);
  4979. assert( My != NULL );
  4980. assert( My->SYSIN != NULL );
  4981. if (line_skip_FilenumChar (l))
  4982. {
  4983. /* MARGIN # */
  4984. My->CurrentFile = My->SYSIN;
  4985. if (line_read_integer_expression (l, &FileNumber) == FALSE)
  4986. {
  4987. WARN_BAD_FILE_NUMBER;
  4988. return (l);
  4989. }
  4990. if (line_skip_seperator (l))
  4991. {
  4992. /* OK */
  4993. }
  4994. else
  4995. {
  4996. WARN_SYNTAX_ERROR;
  4997. return (l);
  4998. }
  4999. if (line_read_integer_expression (l, &Value) == FALSE)
  5000. {
  5001. WARN_SYNTAX_ERROR;
  5002. return (l);
  5003. }
  5004. if (Value < 0)
  5005. {
  5006. WARN_ILLEGAL_FUNCTION_CALL;
  5007. return (l);
  5008. }
  5009. if (FileNumber < 0)
  5010. {
  5011. /* "MARGIN # -1" is SYSPRN */
  5012. My->SYSPRN->width = Value;
  5013. return (l);
  5014. }
  5015. if (FileNumber == 0)
  5016. {
  5017. /* "MARGIN # 0" is SYSOUT */
  5018. My->SYSOUT->width = Value;
  5019. return (l);
  5020. }
  5021. /* normal file */
  5022. My->CurrentFile = find_file_by_number (FileNumber);
  5023. if (My->CurrentFile == NULL)
  5024. {
  5025. WARN_BAD_FILE_NUMBER;
  5026. return (l);
  5027. }
  5028. if ((My->CurrentFile->DevMode & DEVMODE_WRITE) == 0)
  5029. {
  5030. WARN_BAD_FILE_NUMBER;
  5031. return (l);
  5032. }
  5033. My->CurrentFile->width = Value;
  5034. /* OK */
  5035. return (l);
  5036. }
  5037. WARN_SYNTAX_ERROR;
  5038. return (l);
  5039. }
  5040. /*
  5041. --------------------------------------------------------------------------------------------
  5042. USE
  5043. --------------------------------------------------------------------------------------------
  5044. */
  5045. LineType *
  5046. bwb_USE (LineType * l)
  5047. {
  5048. /*
  5049. SYNTAX: USE parameter$ ' CALL/360, System/360, System/370
  5050. */
  5051. VariableType *v;
  5052. assert (l != NULL);
  5053. assert( My != NULL );
  5054. if ((v = line_read_scalar (l)) == NULL)
  5055. {
  5056. WARN_SYNTAX_ERROR;
  5057. return (l);
  5058. }
  5059. if (v->VariableTypeCode != StringTypeCode)
  5060. {
  5061. WARN_SYNTAX_ERROR;
  5062. return (l);
  5063. }
  5064. /* OK */
  5065. if (My->UseParameterString)
  5066. {
  5067. VariantType variant;
  5068. CLEAR_VARIANT (&variant);
  5069. variant.VariantTypeCode = StringTypeCode;
  5070. variant.Buffer = My->UseParameterString;
  5071. variant.Length = bwb_strlen (My->UseParameterString);
  5072. var_set (v, &variant);
  5073. }
  5074. return (l);
  5075. }
  5076. /*
  5077. --------------------------------------------------------------------------------------------
  5078. CHAIN
  5079. --------------------------------------------------------------------------------------------
  5080. */
  5081. LineType *
  5082. bwb_CHAIN (LineType * l)
  5083. {
  5084. /*
  5085. SYNTAX: CHAIN file-name$ [, linenumber] ' most dialects
  5086. SYNTAX: CHAIN file-name$ [, parameter$] ' CALL/360, System/360, System/370
  5087. */
  5088. /* originally based upon bwb_load() */
  5089. int LineNumber;
  5090. LineType *x;
  5091. assert (l != NULL);
  5092. assert( My != NULL );
  5093. assert( My->CurrentVersion != NULL );
  5094. assert( My->StartMarker != NULL );
  5095. assert( My->EndMarker != NULL );
  5096. /* Get an argument for filename */
  5097. if (line_is_eol (l))
  5098. {
  5099. WARN_BAD_FILE_NAME;
  5100. return (l);
  5101. }
  5102. else
  5103. {
  5104. char *Value;
  5105. Value = NULL;
  5106. if (line_read_string_expression (l, &Value) == FALSE)
  5107. {
  5108. WARN_SYNTAX_ERROR;
  5109. return (l);
  5110. }
  5111. if (is_empty_string (Value))
  5112. {
  5113. WARN_BAD_FILE_NAME;
  5114. return (l);
  5115. }
  5116. if (My->ProgramFilename != NULL)
  5117. {
  5118. free (My->ProgramFilename);
  5119. My->ProgramFilename = NULL;
  5120. }
  5121. My->ProgramFilename = Value;
  5122. }
  5123. /* optional linenumber */
  5124. LineNumber = 0;
  5125. if (line_skip_seperator (l))
  5126. {
  5127. if (My->CurrentVersion->OptionVersionValue & (S70 | I70 | I73))
  5128. {
  5129. /* CHAIN filename$, parameter$ */
  5130. {
  5131. char *Value;
  5132. Value = NULL;
  5133. if (line_read_string_expression (l, &Value) == FALSE)
  5134. {
  5135. WARN_SYNTAX_ERROR;
  5136. return (l);
  5137. }
  5138. if (Value == NULL)
  5139. {
  5140. WARN_SYNTAX_ERROR;
  5141. return (l);
  5142. }
  5143. if (My->UseParameterString)
  5144. {
  5145. free (My->UseParameterString);
  5146. My->UseParameterString = NULL;
  5147. }
  5148. My->UseParameterString = Value;
  5149. }
  5150. }
  5151. else
  5152. {
  5153. /* CHAIN filename$, linenumber */
  5154. if (line_read_integer_expression (l, &LineNumber) == FALSE)
  5155. {
  5156. WARN_SYNTAX_ERROR;
  5157. return (l);
  5158. }
  5159. if (LineNumber < MINLIN || LineNumber > MAXLIN)
  5160. {
  5161. WARN_UNDEFINED_LINE;
  5162. return (l);
  5163. }
  5164. }
  5165. }
  5166. /* deallocate all variables except common ones */
  5167. var_delcvars ();
  5168. /* remove old program from memory */
  5169. bwb_xnew (My->StartMarker);
  5170. /* load new program in memory */
  5171. if (bwb_fload (NULL) == FALSE)
  5172. {
  5173. WARN_BAD_FILE_NAME;
  5174. return (l);
  5175. }
  5176. /* FIXME */
  5177. x = My->StartMarker;
  5178. if (MINLIN <= LineNumber && LineNumber <= MAXLIN)
  5179. {
  5180. /* search for a matching line number */
  5181. while (x->number != LineNumber && x != My->EndMarker)
  5182. {
  5183. x = x->next;
  5184. }
  5185. if (x == My->EndMarker)
  5186. {
  5187. /* NOT FOUND */
  5188. x = My->StartMarker;
  5189. }
  5190. }
  5191. x->position = 0;
  5192. /*
  5193. **
  5194. ** FORCE SCAN
  5195. **
  5196. */
  5197. if (bwb_scan () == FALSE)
  5198. {
  5199. WARN_CANT_CONTINUE;
  5200. return (l);
  5201. }
  5202. /* reset all stack counters */
  5203. bwb_clrexec ();
  5204. if (bwb_incexec ())
  5205. {
  5206. /* OK */
  5207. My->StackHead->line = x;
  5208. My->StackHead->ExecCode = EXEC_NORM;
  5209. }
  5210. else
  5211. {
  5212. /* ERROR */
  5213. WARN_OUT_OF_MEMORY;
  5214. return My->EndMarker;
  5215. }
  5216. /* run the program */
  5217. /* CHAIN */
  5218. WARN_CLEAR; /* bwb_CHAIN */
  5219. My->ContinueLine = NULL;
  5220. SetOnError (0);
  5221. return x;
  5222. }
  5223. /*
  5224. --------------------------------------------------------------------------------------------
  5225. APPEND
  5226. --------------------------------------------------------------------------------------------
  5227. */
  5228. LineType *
  5229. bwb_APPEND (LineType * l)
  5230. {
  5231. /*
  5232. SYNTAX: APPEND # filenumber ' Dartmouth, Mark-I, Mark-II, GCOS
  5233. SYNTAX: APPEND [filename$] ' all others
  5234. */
  5235. assert (l != NULL);
  5236. assert( My != NULL );
  5237. assert( My->CurrentVersion != NULL );
  5238. if (My->CurrentVersion->OptionVersionValue & (D64 | G65 | G67 | G74))
  5239. {
  5240. if (line_skip_FilenumChar (l))
  5241. {
  5242. /* APPEND # filenumber */
  5243. int FileNumber;
  5244. if (line_read_integer_expression (l, &FileNumber) == FALSE)
  5245. {
  5246. WARN_SYNTAX_ERROR;
  5247. return (l);
  5248. }
  5249. if (FileNumber < 0)
  5250. {
  5251. /* "APPEND # -1" is silently ignored */
  5252. return (l);
  5253. }
  5254. if (FileNumber == 0)
  5255. {
  5256. /* "APPEND # 0" is silently ignored */
  5257. return (l);
  5258. }
  5259. My->CurrentFile = find_file_by_number (FileNumber);
  5260. if (My->CurrentFile == NULL)
  5261. {
  5262. WARN_BAD_FILE_NUMBER;
  5263. return (l);
  5264. }
  5265. /* normal file */
  5266. fseek (My->CurrentFile->cfp, 0, SEEK_END);
  5267. My->CurrentFile->DevMode = DEVMODE_APPEND;
  5268. /* OK */
  5269. return (l);
  5270. }
  5271. }
  5272. /* APPEND filename$ */
  5273. return bwb_load (l, "APPEND FILE NAME:", FALSE);
  5274. }
  5275. /*
  5276. --------------------------------------------------------------------------------------------
  5277. ON ERROR and so on
  5278. --------------------------------------------------------------------------------------------
  5279. */
  5280. extern void
  5281. SetOnError (int LineNumber)
  5282. {
  5283. /* scan the stack looking for a FUNCTION/SUB */
  5284. StackType *StackItem;
  5285. assert( My != NULL );
  5286. if (My->StackHead == NULL)
  5287. {
  5288. return;
  5289. }
  5290. for (StackItem = My->StackHead; StackItem->next != NULL;
  5291. StackItem = StackItem->next)
  5292. {
  5293. LineType *current;
  5294. current = StackItem->LoopTopLine;
  5295. if (current != NULL)
  5296. {
  5297. switch (current->cmdnum)
  5298. {
  5299. case C_FUNCTION:
  5300. case C_SUB:
  5301. /* FOUND */
  5302. /* we are in a FUNCTION/SUB, so this is LOCAL */
  5303. StackItem->OnErrorGoto = LineNumber;
  5304. return;
  5305. /* break; */
  5306. }
  5307. }
  5308. }
  5309. /* StackItem->next == NULL */
  5310. /* NOT FOUND */
  5311. /* we are NOT in a FUNCTION/SUB */
  5312. assert (StackItem != NULL);
  5313. StackItem->OnErrorGoto = LineNumber;
  5314. }
  5315. extern int
  5316. GetOnError (void)
  5317. {
  5318. /* scan the stack looking for an active "ON ERROR GOTO linenumber" */
  5319. StackType *StackItem;
  5320. assert( My != NULL );
  5321. for (StackItem = My->StackHead; StackItem != NULL;
  5322. StackItem = StackItem->next)
  5323. {
  5324. if (StackItem->OnErrorGoto != 0)
  5325. {
  5326. /* FOUND */
  5327. return StackItem->OnErrorGoto;
  5328. }
  5329. }
  5330. /* NOT FOUND */
  5331. return 0;
  5332. }
  5333. /*
  5334. --------------------------------------------------------------------------------------------
  5335. ON ERROR
  5336. --------------------------------------------------------------------------------------------
  5337. */
  5338. LineType *
  5339. bwb_ON_ERROR (LineType * l)
  5340. {
  5341. assert (l != NULL);
  5342. WARN_SYNTAX_ERROR;
  5343. return (l);
  5344. }
  5345. /*
  5346. --------------------------------------------------------------------------------------------
  5347. ON ERROR GOTO
  5348. --------------------------------------------------------------------------------------------
  5349. */
  5350. LineType *
  5351. bwb_ON_ERROR_GOTO (LineType * l)
  5352. {
  5353. /* ON ERROR GOTO line */
  5354. int LineNumber;
  5355. assert (l != NULL);
  5356. WARN_CLEAR; /* bwb_ON_ERROR_GOTO */
  5357. /* get the line number */
  5358. LineNumber = 0;
  5359. if (line_is_eol (l))
  5360. {
  5361. /* ON ERROR GOTO */
  5362. SetOnError (0);
  5363. return (l);
  5364. }
  5365. if (line_read_integer_expression (l, &LineNumber) == FALSE)
  5366. {
  5367. WARN_SYNTAX_ERROR;
  5368. return (l);
  5369. }
  5370. /* ON ERORR GOTO linenumber */
  5371. if (LineNumber == 0)
  5372. {
  5373. /* ON ERROR GOTO 0 */
  5374. SetOnError (0);
  5375. return (l);
  5376. }
  5377. if (LineNumber < MINLIN || LineNumber > MAXLIN)
  5378. {
  5379. /* ERROR */
  5380. WARN_UNDEFINED_LINE;
  5381. return (l);
  5382. }
  5383. /* OK */
  5384. SetOnError (LineNumber);
  5385. return (l);
  5386. }
  5387. /*
  5388. --------------------------------------------------------------------------------------------
  5389. ON ERROR GOSUB
  5390. --------------------------------------------------------------------------------------------
  5391. */
  5392. LineType *
  5393. bwb_ON_ERROR_GOSUB (LineType * l)
  5394. {
  5395. /* ON ERROR GOSUB line */
  5396. assert (l != NULL);
  5397. return bwb_ON_ERROR_GOTO (l);
  5398. }
  5399. /*
  5400. --------------------------------------------------------------------------------------------
  5401. ON ERROR RESUME
  5402. --------------------------------------------------------------------------------------------
  5403. */
  5404. LineType *
  5405. bwb_ON_ERROR_RESUME (LineType * l)
  5406. {
  5407. assert (l != NULL);
  5408. WARN_SYNTAX_ERROR;
  5409. return (l);
  5410. }
  5411. /*
  5412. --------------------------------------------------------------------------------------------
  5413. ON ERROR RESUME NEXT
  5414. --------------------------------------------------------------------------------------------
  5415. */
  5416. LineType *
  5417. bwb_ON_ERROR_RESUME_NEXT (LineType * l)
  5418. {
  5419. assert (l != NULL);
  5420. WARN_CLEAR; /* bwb_ON_ERROR_RESUME_NEXT */
  5421. SetOnError (-1);
  5422. return (l);
  5423. }
  5424. /*
  5425. --------------------------------------------------------------------------------------------
  5426. ON ERROR RETURN
  5427. --------------------------------------------------------------------------------------------
  5428. */
  5429. LineType *
  5430. bwb_ON_ERROR_RETURN (LineType * l)
  5431. {
  5432. assert (l != NULL);
  5433. WARN_SYNTAX_ERROR;
  5434. return (l);
  5435. }
  5436. /*
  5437. --------------------------------------------------------------------------------------------
  5438. ON ERROR RETURN NEXT
  5439. --------------------------------------------------------------------------------------------
  5440. */
  5441. LineType *
  5442. bwb_ON_ERROR_RETURN_NEXT (LineType * l)
  5443. {
  5444. assert (l != NULL);
  5445. return bwb_ON_ERROR_RESUME_NEXT (l);
  5446. }
  5447. /*
  5448. --------------------------------------------------------------------------------------------
  5449. ON TIMER
  5450. --------------------------------------------------------------------------------------------
  5451. */
  5452. LineType *
  5453. bwb_ON_TIMER (LineType * l)
  5454. {
  5455. /* ON TIMER(...) GOSUB ... */
  5456. DoubleType v;
  5457. DoubleType minv;
  5458. int LineNumber;
  5459. assert (l != NULL);
  5460. assert( My != NULL );
  5461. My->IsTimerOn = FALSE; /* bwb_ON_TIMER */
  5462. My->OnTimerLineNumber = 0;
  5463. My->OnTimerCount = 0;
  5464. /* get the SECOMDS parameter */
  5465. if (line_read_numeric_expression (l, &v) == FALSE)
  5466. {
  5467. WARN_SYNTAX_ERROR;
  5468. return (l);
  5469. }
  5470. minv = 1;
  5471. assert (CLOCKS_PER_SEC > 0);
  5472. minv /= CLOCKS_PER_SEC;
  5473. if (v < minv)
  5474. {
  5475. /* ERROR */
  5476. WARN_ILLEGAL_FUNCTION_CALL;
  5477. return (l);
  5478. }
  5479. /* get the GOSUB keyword */
  5480. if (line_skip_word (l, "GOSUB") == FALSE)
  5481. {
  5482. WARN_SYNTAX_ERROR;
  5483. return (l);
  5484. }
  5485. /* ON TIMER(X) GOSUB line */
  5486. if (line_read_integer_expression (l, &LineNumber) == FALSE)
  5487. {
  5488. WARN_SYNTAX_ERROR;
  5489. return (l);
  5490. }
  5491. if (LineNumber < MINLIN || LineNumber > MAXLIN)
  5492. {
  5493. /* ERROR */
  5494. WARN_UNDEFINED_LINE;
  5495. return (l);
  5496. }
  5497. /* OK */
  5498. My->OnTimerLineNumber = LineNumber;
  5499. My->OnTimerCount = v;
  5500. return (l);
  5501. }
  5502. /*
  5503. --------------------------------------------------------------------------------------------
  5504. TIMER
  5505. --------------------------------------------------------------------------------------------
  5506. */
  5507. LineType *
  5508. bwb_TIMER (LineType * l)
  5509. {
  5510. assert (l != NULL);
  5511. assert( My != NULL );
  5512. My->IsTimerOn = FALSE; /* bwb_TIMER */
  5513. WARN_SYNTAX_ERROR;
  5514. return (l);
  5515. }
  5516. /*
  5517. --------------------------------------------------------------------------------------------
  5518. TIMER OFF
  5519. --------------------------------------------------------------------------------------------
  5520. */
  5521. LineType *
  5522. bwb_TIMER_OFF (LineType * l)
  5523. {
  5524. assert (l != NULL);
  5525. assert( My != NULL );
  5526. /* TIMER OFF */
  5527. My->IsTimerOn = FALSE; /* bwb_TIMER_OFF */
  5528. My->OnTimerLineNumber = 0;
  5529. My->OnTimerCount = 0;
  5530. return (l);
  5531. }
  5532. /*
  5533. --------------------------------------------------------------------------------------------
  5534. TIMER ON
  5535. --------------------------------------------------------------------------------------------
  5536. */
  5537. LineType *
  5538. bwb_TIMER_ON (LineType * l)
  5539. {
  5540. assert (l != NULL);
  5541. assert( My != NULL );
  5542. My->IsTimerOn = FALSE; /* bwb_TIMER_ON */
  5543. /* TIMER ON */
  5544. if (My->OnTimerCount > 0 && My->OnTimerLineNumber > 0)
  5545. {
  5546. My->OnTimerExpires = bwx_TIMER (My->OnTimerCount);
  5547. My->IsTimerOn = TRUE; /* bwb_TIMER_ON */
  5548. }
  5549. return (l);
  5550. }
  5551. /*
  5552. --------------------------------------------------------------------------------------------
  5553. TIMER STOP
  5554. --------------------------------------------------------------------------------------------
  5555. */
  5556. LineType *
  5557. bwb_TIMER_STOP (LineType * l)
  5558. {
  5559. assert (l != NULL);
  5560. assert( My != NULL );
  5561. My->IsTimerOn = FALSE; /* bwb_TIMER_STOP */
  5562. return (l);
  5563. }
  5564. /*
  5565. --------------------------------------------------------------------------------------------
  5566. RESUME
  5567. --------------------------------------------------------------------------------------------
  5568. */
  5569. LineType *
  5570. bwb_RESUME (LineType * l)
  5571. {
  5572. int LineNumber;
  5573. LineType *x;
  5574. assert (l != NULL);
  5575. assert( My != NULL );
  5576. LineNumber = 0;
  5577. x = My->ERL; /* bwb_RESUME */
  5578. WARN_CLEAR; /* bwb_RESUME */
  5579. if (l->LineFlags & (LINE_USER))
  5580. {
  5581. WARN_ILLEGAL_DIRECT;
  5582. return (l);
  5583. }
  5584. if (x == NULL)
  5585. {
  5586. WARN_RESUME_WITHOUT_ERROR;
  5587. return (l);
  5588. }
  5589. /* Get optional argument for RESUME */
  5590. if (line_is_eol (l))
  5591. {
  5592. /* RESUME */
  5593. /*
  5594. Execution resumes at the statement which caused the error
  5595. For structured commands, this is the top line of the structure.
  5596. */
  5597. x->position = 0;
  5598. return x;
  5599. }
  5600. if (line_skip_word (l, "NEXT"))
  5601. {
  5602. /* RESUME NEXT */
  5603. /*
  5604. Execution resumes at the statement immediately following the one which caused the error.
  5605. For structured commands, this is the bottom line of the structure.
  5606. */
  5607. switch (x->cmdnum)
  5608. {
  5609. case C_IF8THEN:
  5610. /* skip to END_IF */
  5611. assert (x->OtherLine != NULL);
  5612. for (x = x->OtherLine; x->cmdnum != C_END_IF; x = x->OtherLine);
  5613. break;
  5614. case C_SELECT_CASE:
  5615. /* skip to END_SELECT */
  5616. assert (x->OtherLine != NULL);
  5617. for (x = x->OtherLine; x->cmdnum != C_END_SELECT; x = x->OtherLine);
  5618. break;
  5619. default:
  5620. x = x->next;
  5621. }
  5622. x->position = 0;
  5623. return x;
  5624. }
  5625. /* RESUME ### */
  5626. if (line_read_integer_expression (l, &LineNumber) == FALSE)
  5627. {
  5628. WARN_SYNTAX_ERROR;
  5629. return (l);
  5630. }
  5631. if (LineNumber == 0)
  5632. {
  5633. /* SPECIAL CASE */
  5634. /* RESUME 0 */
  5635. /* Execution resumes at the statement which caused the error */
  5636. x->position = 0;
  5637. return x;
  5638. }
  5639. /* VERIFY LINE EXISTS */
  5640. x = find_line_number (LineNumber); /* RESUME 100 */
  5641. if (x != NULL)
  5642. {
  5643. /* FOUND */
  5644. x->position = 0;
  5645. return x;
  5646. }
  5647. /* NOT FOUND */
  5648. WARN_UNDEFINED_LINE;
  5649. return (l);
  5650. }
  5651. /*
  5652. --------------------------------------------------------------------------------------------
  5653. CMDS
  5654. --------------------------------------------------------------------------------------------
  5655. */
  5656. LineType *
  5657. bwb_CMDS (LineType * l)
  5658. {
  5659. int n;
  5660. int t;
  5661. assert (l != NULL);
  5662. assert( My != NULL );
  5663. assert( My->SYSOUT != NULL );
  5664. assert( My->SYSOUT->cfp != NULL );
  5665. My->CurrentFile = My->SYSOUT;
  5666. fprintf (My->SYSOUT->cfp, "BWBASIC COMMANDS AVAILABLE:\n");
  5667. /* run through the command table and print comand names */
  5668. t = 0;
  5669. for (n = 0; n < NUM_COMMANDS; n++)
  5670. {
  5671. fprintf (My->SYSOUT->cfp, IntrinsicCommandTable[n].name);
  5672. if (t < 4)
  5673. {
  5674. fprintf (My->SYSOUT->cfp, "\t");
  5675. t++;
  5676. }
  5677. else
  5678. {
  5679. fprintf (My->SYSOUT->cfp, "\n");
  5680. t = 0;
  5681. }
  5682. }
  5683. if (t > 0)
  5684. {
  5685. fprintf (My->SYSOUT->cfp, "\n");
  5686. }
  5687. ResetConsoleColumn ();
  5688. return (l);
  5689. }
  5690. static void
  5691. FixUp (char *Name)
  5692. {
  5693. char *C;
  5694. assert (Name != NULL);
  5695. C = Name;
  5696. while (*C)
  5697. {
  5698. if (bwb_isalnum (*C))
  5699. {
  5700. /* OK */
  5701. }
  5702. else
  5703. {
  5704. /* FIX */
  5705. switch (*C)
  5706. {
  5707. case '!':
  5708. *C = '1';
  5709. break;
  5710. case '@':
  5711. *C = '2';
  5712. break;
  5713. case '#':
  5714. *C = '3';
  5715. break;
  5716. case '$':
  5717. *C = '4';
  5718. break;
  5719. case '%':
  5720. *C = '5';
  5721. break;
  5722. case '^':
  5723. *C = '6';
  5724. break;
  5725. case '&':
  5726. *C = '7';
  5727. break;
  5728. case '*':
  5729. *C = '8';
  5730. break;
  5731. case '(':
  5732. *C = '9';
  5733. break;
  5734. case ')':
  5735. *C = '0';
  5736. break;
  5737. default:
  5738. *C = '_';
  5739. }
  5740. }
  5741. C++;
  5742. }
  5743. }
  5744. static void
  5745. CommandUniqueID (int i, char *UniqueID)
  5746. {
  5747. assert (UniqueID != NULL);
  5748. bwb_strcpy (UniqueID, "C_");
  5749. bwb_strcat (UniqueID, IntrinsicCommandTable[i].name);
  5750. FixUp (UniqueID);
  5751. }
  5752. static void
  5753. CommandVector (int i, char *Vector)
  5754. {
  5755. assert (Vector != NULL);
  5756. bwb_strcpy (Vector, "bwb_");
  5757. bwb_strcat (Vector, IntrinsicCommandTable[i].name);
  5758. FixUp (Vector);
  5759. }
  5760. static void
  5761. CommandOptionVersion (int n, char *OutputLine)
  5762. {
  5763. int i;
  5764. int j;
  5765. assert (OutputLine != NULL);
  5766. bwb_strcpy (OutputLine, "");
  5767. j = 0;
  5768. for (i = 0; i < NUM_VERSIONS; i++)
  5769. {
  5770. if (IntrinsicCommandTable[n].OptionVersionBitmask & bwb_vertable[i].
  5771. OptionVersionValue)
  5772. {
  5773. if (j > 0)
  5774. {
  5775. bwb_strcat (OutputLine, " | ");
  5776. }
  5777. bwb_strcat (OutputLine, bwb_vertable[i].ID);
  5778. j++;
  5779. }
  5780. }
  5781. }
  5782. void
  5783. SortAllCommands (void)
  5784. {
  5785. /* sort by name */
  5786. int i;
  5787. assert( My != NULL );
  5788. for (i = 0; i < NUM_COMMANDS - 1; i++)
  5789. {
  5790. int j;
  5791. int k;
  5792. k = i;
  5793. for (j = i + 1; j < NUM_COMMANDS; j++)
  5794. {
  5795. if (bwb_stricmp
  5796. (IntrinsicCommandTable[j].name, IntrinsicCommandTable[k].name) < 0)
  5797. {
  5798. k = j;
  5799. }
  5800. }
  5801. if (k > i)
  5802. {
  5803. CommandType t;
  5804. bwb_memcpy (&t, &(IntrinsicCommandTable[i]), sizeof (CommandType));
  5805. bwb_memcpy (&(IntrinsicCommandTable[i]), &(IntrinsicCommandTable[k]),
  5806. sizeof (CommandType));
  5807. bwb_memcpy (&(IntrinsicCommandTable[k]), &t, sizeof (CommandType));
  5808. }
  5809. }
  5810. #if THE_PRICE_IS_RIGHT
  5811. for (i = 0; i < 26; i++)
  5812. {
  5813. My->CommandStart[i] = -1;
  5814. }
  5815. for (i = 0; i < NUM_COMMANDS; i++)
  5816. {
  5817. int j;
  5818. j = VarTypeIndex (IntrinsicCommandTable[i].name[0]);
  5819. if (j < 0)
  5820. {
  5821. /* non-alpha */
  5822. }
  5823. else if (My->CommandStart[j] < 0)
  5824. {
  5825. /* this is the first command starting with this letter */
  5826. My->CommandStart[j] = i;
  5827. }
  5828. }
  5829. #endif /* THE_PRICE_IS_RIGHT */
  5830. }
  5831. void
  5832. SortAllFunctions (void)
  5833. {
  5834. /* sort by name then number of parameters */
  5835. int i;
  5836. assert( My != NULL );
  5837. for (i = 0; i < NUM_FUNCTIONS - 1; i++)
  5838. {
  5839. int j;
  5840. int k;
  5841. k = i;
  5842. for (j = i + 1; j < NUM_FUNCTIONS; j++)
  5843. {
  5844. int n;
  5845. n =
  5846. bwb_stricmp (IntrinsicFunctionTable[j].Name,
  5847. IntrinsicFunctionTable[k].Name);
  5848. if (n < 0)
  5849. {
  5850. k = j;
  5851. }
  5852. else if (n == 0)
  5853. {
  5854. if (IntrinsicFunctionTable[j].ParameterCount <
  5855. IntrinsicFunctionTable[k].ParameterCount)
  5856. {
  5857. k = j;
  5858. }
  5859. }
  5860. }
  5861. if (k > i)
  5862. {
  5863. IntrinsicFunctionType t;
  5864. bwb_memcpy (&t, &(IntrinsicFunctionTable[i]),
  5865. sizeof (IntrinsicFunctionType));
  5866. bwb_memcpy (&(IntrinsicFunctionTable[i]), &(IntrinsicFunctionTable[k]),
  5867. sizeof (IntrinsicFunctionType));
  5868. bwb_memcpy (&(IntrinsicFunctionTable[k]), &t,
  5869. sizeof (IntrinsicFunctionType));
  5870. }
  5871. }
  5872. #if THE_PRICE_IS_RIGHT
  5873. for (i = 0; i < 26; i++)
  5874. {
  5875. My->IntrinsicFunctionStart[i] = -1;
  5876. }
  5877. for (i = 0; i < NUM_FUNCTIONS; i++)
  5878. {
  5879. int j;
  5880. j = VarTypeIndex (IntrinsicFunctionTable[i].Name[0]);
  5881. if (j < 0)
  5882. {
  5883. /* non-alpha */
  5884. }
  5885. else if (My->IntrinsicFunctionStart[j] < 0)
  5886. {
  5887. /* this is the first command starting with this letter */
  5888. My->IntrinsicFunctionStart[j] = i;
  5889. }
  5890. }
  5891. #endif /* THE_PRICE_IS_RIGHT */
  5892. }
  5893. void
  5894. DumpAllCommandUniqueID (FILE * file)
  5895. {
  5896. int i;
  5897. int j;
  5898. char LastUniqueID[NameLengthMax + 1];
  5899. assert (file != NULL);
  5900. j = 0;
  5901. LastUniqueID[0] = NulChar;
  5902. fprintf (file, "/* COMMANDS */\n");
  5903. /* run through the command table and print comand #define */
  5904. for (i = 0; i < NUM_COMMANDS; i++)
  5905. {
  5906. char UniqueID[NameLengthMax + 1];
  5907. CommandUniqueID (i, UniqueID);
  5908. if (bwb_stricmp (LastUniqueID, UniqueID) != 0)
  5909. {
  5910. /* not a duplicate */
  5911. bwb_strcpy (LastUniqueID, UniqueID);
  5912. j = j + 1;
  5913. fprintf (file, "#define %-30s %3d /* %-30s */\n", UniqueID, j,
  5914. IntrinsicCommandTable[i].name);
  5915. }
  5916. }
  5917. fprintf (file, "#define NUM_COMMANDS %d\n", j);
  5918. fflush (file);
  5919. }
  5920. static void
  5921. ProcessEscapeChars (const char *Input, char *Output)
  5922. {
  5923. int n;
  5924. assert (Input != NULL);
  5925. assert (Output != NULL);
  5926. n = 0;
  5927. while (*Input)
  5928. {
  5929. /* \a \b \f \n \r \t \v \" \\ */
  5930. switch (*Input)
  5931. {
  5932. case '\a':
  5933. *Output = '\\';
  5934. Output++;
  5935. *Output = 'a';
  5936. Output++;
  5937. break;
  5938. case '\b':
  5939. *Output = '\\';
  5940. Output++;
  5941. *Output = 'b';
  5942. Output++;
  5943. break;
  5944. case '\f':
  5945. *Output = '\\';
  5946. Output++;
  5947. *Output = 'f';
  5948. Output++;
  5949. break;
  5950. case '\n':
  5951. *Output = '\\';
  5952. Output++;
  5953. *Output = 'n';
  5954. Output++;
  5955. break;
  5956. case '\r':
  5957. *Output = '\\';
  5958. Output++;
  5959. *Output = 'r';
  5960. Output++;
  5961. break;
  5962. case '\t':
  5963. *Output = '\\';
  5964. Output++;
  5965. *Output = 't';
  5966. Output++;
  5967. break;
  5968. case '\v':
  5969. *Output = '\\';
  5970. Output++;
  5971. *Output = 'n';
  5972. Output++;
  5973. break;
  5974. case '\"':
  5975. *Output = '\\';
  5976. Output++;
  5977. *Output = '"';
  5978. Output++;
  5979. break;
  5980. case '\\':
  5981. *Output = '\\';
  5982. Output++;
  5983. *Output = '\\';
  5984. Output++;
  5985. break;
  5986. default:
  5987. *Output = *Input;
  5988. Output++;
  5989. break;
  5990. }
  5991. *Output = NulChar;
  5992. n++;
  5993. if (n > 60 && *Input == ' ')
  5994. {
  5995. *Output = '\"';
  5996. Output++;
  5997. *Output = '\n';
  5998. Output++;
  5999. *Output = ' ';
  6000. Output++;
  6001. *Output = ' ';
  6002. Output++;
  6003. *Output = '\"';
  6004. Output++;
  6005. *Output = NulChar;
  6006. n = 0;
  6007. }
  6008. Input++;
  6009. }
  6010. }
  6011. void
  6012. DumpAllCommandTableDefinitions (FILE * file)
  6013. {
  6014. /* generate bwd_cmd.c */
  6015. int i;
  6016. assert (file != NULL);
  6017. fprintf (file, "/* COMMAND TABLE */\n\n");
  6018. fprintf (file, "#include \"bwbasic.h\"\n\n");
  6019. fprintf (file,
  6020. "CommandType IntrinsicCommandTable[ /* NUM_COMMANDS */ ] =\n");
  6021. fprintf (file, "{\n");
  6022. /* run through the command table and print comand #define */
  6023. for (i = 0; i < NUM_COMMANDS; i++)
  6024. {
  6025. char tbuf[MAINTAINER_BUFFER_LENGTH + 1]; /* DumpAllCommandTableDefinitions */
  6026. fprintf (file, "{\n");
  6027. fprintf (file, " ");
  6028. CommandUniqueID (i, tbuf);
  6029. fprintf (file, tbuf);
  6030. fprintf (file, ", /* UniqueID */\n");
  6031. fprintf (file, " ");
  6032. fprintf (file, "\"");
  6033. ProcessEscapeChars (IntrinsicCommandTable[i].Syntax, tbuf);
  6034. fprintf (file, tbuf);
  6035. fprintf (file, "\"");
  6036. fprintf (file, ", /* Syntax */\n");
  6037. fprintf (file, " ");
  6038. fprintf (file, "\"");
  6039. ProcessEscapeChars (IntrinsicCommandTable[i].Description, tbuf);
  6040. fprintf (file, tbuf);
  6041. fprintf (file, "\"");
  6042. fprintf (file, ", /* Description */\n");
  6043. fprintf (file, " ");
  6044. fprintf (file, "\"");
  6045. fprintf (file, IntrinsicCommandTable[i].name);
  6046. fprintf (file, "\"");
  6047. fprintf (file, ", /* Name */\n");
  6048. fprintf (file, " ");
  6049. CommandOptionVersion (i, tbuf);
  6050. fprintf (file, tbuf);
  6051. fprintf (file, " /* OptionVersionBitmask */\n");
  6052. fprintf (file, "},\n");
  6053. }
  6054. fprintf (file, "};\n");
  6055. fprintf (file, "\n");
  6056. fprintf (file,
  6057. "const size_t NUM_COMMANDS = sizeof( IntrinsicCommandTable ) / sizeof( CommandType );\n");
  6058. fprintf (file, "\n");
  6059. fflush (file);
  6060. }
  6061. void
  6062. DumpAllCommandSwitchStatement (FILE * file)
  6063. {
  6064. int i;
  6065. char LastUniqueID[NameLengthMax + 1];
  6066. assert (file != NULL);
  6067. LastUniqueID[0] = NulChar;
  6068. /* run through the command table and print comand #define */
  6069. fprintf (file, "/* SWITCH */\n");
  6070. fprintf (file, "LineType *bwb_vector( LineType *l )\n");
  6071. fprintf (file, "{\n");
  6072. fprintf (file, " ");
  6073. fprintf (file, "LineType *r;\n");
  6074. fprintf (file, " ");
  6075. fprintf (file, "switch( l->cmdnum )\n");
  6076. fprintf (file, " ");
  6077. fprintf (file, "{\n");
  6078. for (i = 0; i < NUM_COMMANDS; i++)
  6079. {
  6080. char tbuf[NameLengthMax + 1];
  6081. CommandUniqueID (i, tbuf);
  6082. if (bwb_stricmp (LastUniqueID, tbuf) != 0)
  6083. {
  6084. /* not a duplicate */
  6085. bwb_strcpy (LastUniqueID, tbuf);
  6086. fprintf (file, " ");
  6087. fprintf (file, "case ");
  6088. CommandUniqueID (i, tbuf);
  6089. fprintf (file, tbuf);
  6090. fprintf (file, ":\n");
  6091. fprintf (file, " ");
  6092. fprintf (file, " ");
  6093. fprintf (file, "r = ");
  6094. CommandVector (i, tbuf);
  6095. fprintf (file, tbuf);
  6096. fprintf (file, "( l );\n");
  6097. fprintf (file, " ");
  6098. fprintf (file, " ");
  6099. fprintf (file, "break;\n");
  6100. }
  6101. }
  6102. fprintf (file, " ");
  6103. fprintf (file, "default:\n");
  6104. fprintf (file, " ");
  6105. fprintf (file, " ");
  6106. fprintf (file, "WARN_INTERNAL_ERROR;\n");
  6107. fprintf (file, " ");
  6108. fprintf (file, " ");
  6109. fprintf (file, "r = l;\n");
  6110. fprintf (file, " ");
  6111. fprintf (file, " ");
  6112. fprintf (file, "break;\n");
  6113. fprintf (file, " ");
  6114. fprintf (file, "}\n");
  6115. fprintf (file, " ");
  6116. fprintf (file, "return r;\n");
  6117. fprintf (file, "}\n");
  6118. fflush (file);
  6119. }
  6120. void
  6121. FixDescription (FILE * file, const char *left, const char *right)
  6122. {
  6123. char buffer[MAINTAINER_BUFFER_LENGTH + 1]; /* FixDescription */
  6124. int l; /* length of left side */
  6125. int p; /* current position */
  6126. int n; /* position of the last space character, zero means none yet seen */
  6127. int i; /* number of characters since last '\n' */
  6128. assert (left != NULL);
  6129. assert (right != NULL);
  6130. l = bwb_strlen (left);
  6131. p = 0;
  6132. n = 0;
  6133. i = 0;
  6134. bwb_strcpy (buffer, right);
  6135. while (buffer[p])
  6136. {
  6137. if (buffer[p] == '\n')
  6138. {
  6139. n = p;
  6140. i = 0;
  6141. }
  6142. if (buffer[p] == ' ')
  6143. {
  6144. n = p;
  6145. }
  6146. if (i > 45 && n > 0)
  6147. {
  6148. buffer[n] = '\n';
  6149. i = p - n;
  6150. }
  6151. p++;
  6152. i++;
  6153. }
  6154. fputs (left, file);
  6155. p = 0;
  6156. while (buffer[p])
  6157. {
  6158. if (buffer[p] == '\n')
  6159. {
  6160. fputc (buffer[p], file);
  6161. p++;
  6162. while (buffer[p] == ' ')
  6163. {
  6164. p++;
  6165. }
  6166. for (i = 0; i < l; i++)
  6167. {
  6168. fputc (' ', file);
  6169. }
  6170. }
  6171. else
  6172. {
  6173. fputc (buffer[p], file);
  6174. p++;
  6175. }
  6176. }
  6177. fputc ('\n', file);
  6178. }
  6179. void
  6180. DumpOneCommandSyntax (FILE * file, int IsXref, int n)
  6181. {
  6182. assert (file != NULL);
  6183. if (n < 0 || n >= NUM_COMMANDS)
  6184. {
  6185. return;
  6186. }
  6187. /* NAME */
  6188. {
  6189. FixDescription (file, " SYNTAX: ", IntrinsicCommandTable[n].Syntax);
  6190. }
  6191. /* DESCRIPTION */
  6192. {
  6193. FixDescription (file, "DESCRIPTION: ",
  6194. IntrinsicCommandTable[n].Description);
  6195. }
  6196. /* COMPATIBILITY */
  6197. if (IsXref)
  6198. {
  6199. int i;
  6200. fprintf (file, " VERSIONS:\n");
  6201. for (i = 0; i < NUM_VERSIONS; i++)
  6202. {
  6203. char X;
  6204. if (IntrinsicCommandTable[n].OptionVersionBitmask & bwb_vertable[i].
  6205. OptionVersionValue)
  6206. {
  6207. /* SUPPORTED */
  6208. X = 'X';
  6209. }
  6210. else
  6211. {
  6212. /* NOT SUPPORTED */
  6213. X = '_';
  6214. }
  6215. fprintf (file, " [%c] %s\n", X, bwb_vertable[i].Name);
  6216. }
  6217. }
  6218. fflush (file);
  6219. }
  6220. void
  6221. DumpAllCommandSyntax (FILE * file, int IsXref,
  6222. OptionVersionType OptionVersionValue)
  6223. {
  6224. /* for the C maintainer */
  6225. int i;
  6226. assert (file != NULL);
  6227. fprintf (file,
  6228. "============================================================\n");
  6229. fprintf (file,
  6230. " COMMANDS \n");
  6231. fprintf (file,
  6232. "============================================================\n");
  6233. fprintf (file, "\n");
  6234. fprintf (file, "\n");
  6235. for (i = 0; i < NUM_COMMANDS; i++)
  6236. {
  6237. if (IntrinsicCommandTable[i].OptionVersionBitmask & OptionVersionValue)
  6238. {
  6239. fprintf (file,
  6240. "------------------------------------------------------------\n");
  6241. DumpOneCommandSyntax (file, IsXref, i);
  6242. }
  6243. }
  6244. fprintf (file,
  6245. "------------------------------------------------------------\n");
  6246. fprintf (file, "\n");
  6247. fprintf (file, "\n");
  6248. fflush (file);
  6249. }
  6250. void
  6251. DumpAllCommandHtmlTable (FILE * file)
  6252. {
  6253. /* generate bwd_cmd.htm */
  6254. int i;
  6255. int j;
  6256. assert (file != NULL);
  6257. /* LEGEND */
  6258. fprintf (file, "<html><head><title>CMDS</title></head><body>\n");
  6259. fprintf (file, "<h1>LEGEND</h1><br>\n");
  6260. fprintf (file, "<table>\n");
  6261. fprintf (file, "<tr>");
  6262. fprintf (file, "<td>");
  6263. fprintf (file, "<b>");
  6264. fprintf (file, "ID");
  6265. fprintf (file, "</b>");
  6266. fprintf (file, "</td>");
  6267. fprintf (file, "<td>");
  6268. fprintf (file, "<b>");
  6269. fprintf (file, "NAME");
  6270. fprintf (file, "</b>");
  6271. fprintf (file, "</td>");
  6272. fprintf (file, "<td>");
  6273. fprintf (file, "<b>");
  6274. fprintf (file, "DESCRIPTION");
  6275. fprintf (file, "</b>");
  6276. fprintf (file, "</td>");
  6277. fprintf (file, "</tr>\n");
  6278. for (j = 0; j < NUM_VERSIONS; j++)
  6279. {
  6280. fprintf (file, "<tr>");
  6281. fprintf (file, "<td>");
  6282. fprintf (file, bwb_vertable[j].ID);
  6283. fprintf (file, "</td>");
  6284. fprintf (file, "<td>");
  6285. fprintf (file, bwb_vertable[j].Name);
  6286. fprintf (file, "</td>");
  6287. fprintf (file, "<td>");
  6288. fprintf (file, bwb_vertable[j].Description);
  6289. fprintf (file, "</td>");
  6290. fprintf (file, "</tr>\n");
  6291. }
  6292. fprintf (file, "</table>\n");
  6293. fprintf (file, "<hr>\n");
  6294. /* DETAILS */
  6295. fprintf (file, "<h1>DETAILS</h1><br>\n");
  6296. fprintf (file, "<table>\n");
  6297. fprintf (file, "<tr>");
  6298. fprintf (file, "<td>");
  6299. fprintf (file, "<b>");
  6300. fprintf (file, "COMMAND");
  6301. fprintf (file, "</b>");
  6302. fprintf (file, "</td>");
  6303. for (j = 0; j < NUM_VERSIONS; j++)
  6304. {
  6305. fprintf (file, "<td>");
  6306. fprintf (file, "<b>");
  6307. fprintf (file, bwb_vertable[j].ID);
  6308. fprintf (file, "</b>");
  6309. fprintf (file, "</td>");
  6310. }
  6311. fprintf (file, "</tr>\n");
  6312. /* run through the command table and print comand -vs- OPTION VERSION */
  6313. for (i = 0; i < NUM_COMMANDS; i++)
  6314. {
  6315. fprintf (file, "<tr>");
  6316. fprintf (file, "<td>");
  6317. fprintf (file, (char *) IntrinsicCommandTable[i].Syntax);
  6318. fprintf (file, "</td>");
  6319. for (j = 0; j < NUM_VERSIONS; j++)
  6320. {
  6321. fprintf (file, "<td>");
  6322. if (IntrinsicCommandTable[i].OptionVersionBitmask & bwb_vertable[j].
  6323. OptionVersionValue)
  6324. {
  6325. fprintf (file, "X");
  6326. }
  6327. else
  6328. {
  6329. fprintf (file, " ");
  6330. }
  6331. fprintf (file, "</td>");
  6332. }
  6333. fprintf (file, "</tr>\n");
  6334. }
  6335. fprintf (file, "</table>\n");
  6336. fprintf (file, "</body></html>\n");
  6337. fprintf (file, "\n");
  6338. fflush (file);
  6339. }
  6340. /*
  6341. --------------------------------------------------------------------------------------------
  6342. HELP
  6343. --------------------------------------------------------------------------------------------
  6344. */
  6345. LineType *
  6346. bwb_HELP (LineType * l)
  6347. {
  6348. /* HELP ... */
  6349. int n;
  6350. int Found;
  6351. char *C;
  6352. char *tbuf;
  6353. assert (l != NULL);
  6354. assert( My != NULL );
  6355. assert( My->ConsoleInput != NULL );
  6356. assert( My->SYSOUT != NULL );
  6357. assert( My->SYSOUT->cfp != NULL );
  6358. tbuf = My->ConsoleInput;
  6359. Found = FALSE;
  6360. C = l->buffer;
  6361. C += l->position;
  6362. bwb_strcpy (tbuf, C);
  6363. /* RTRIM$ */
  6364. C = tbuf;
  6365. if (*C != 0)
  6366. {
  6367. /* not an empty line, so remove one (or more) trailing spaces */
  6368. char *E;
  6369. E = bwb_strchr (tbuf, 0);
  6370. E--;
  6371. while (E >= tbuf && *E == ' ')
  6372. {
  6373. *E = 0;
  6374. E--;
  6375. }
  6376. }
  6377. /* EXACT match */
  6378. for (n = 0; n < NUM_COMMANDS; n++)
  6379. {
  6380. if (bwb_stricmp (IntrinsicCommandTable[n].name, tbuf) == 0)
  6381. {
  6382. fprintf (My->SYSOUT->cfp,
  6383. "------------------------------------------------------------\n");
  6384. DumpOneCommandSyntax (My->SYSOUT->cfp, FALSE, n);
  6385. Found = TRUE;
  6386. }
  6387. }
  6388. for (n = 0; n < NUM_FUNCTIONS; n++)
  6389. {
  6390. if (bwb_stricmp (IntrinsicFunctionTable[n].Name, tbuf) == 0)
  6391. {
  6392. fprintf (My->SYSOUT->cfp,
  6393. "------------------------------------------------------------\n");
  6394. DumpOneFunctionSyntax (My->SYSOUT->cfp, FALSE, n);
  6395. Found = TRUE;
  6396. }
  6397. }
  6398. if (Found == FALSE)
  6399. {
  6400. /* PARTIAL match */
  6401. int Length;
  6402. Length = bwb_strlen (tbuf);
  6403. for (n = 0; n < NUM_COMMANDS; n++)
  6404. {
  6405. if (bwb_strnicmp (IntrinsicCommandTable[n].name, tbuf, Length) == 0)
  6406. {
  6407. if (Found == FALSE)
  6408. {
  6409. fprintf (My->SYSOUT->cfp,
  6410. "The following topics are a partial match:\n");
  6411. }
  6412. fprintf (My->SYSOUT->cfp, IntrinsicCommandTable[n].name);
  6413. fprintf (My->SYSOUT->cfp, "\t");
  6414. Found = TRUE;
  6415. }
  6416. }
  6417. for (n = 0; n < NUM_FUNCTIONS; n++)
  6418. {
  6419. if (bwb_strnicmp (IntrinsicFunctionTable[n].Name, tbuf, Length) == 0)
  6420. {
  6421. if (Found == FALSE)
  6422. {
  6423. fprintf (My->SYSOUT->cfp,
  6424. "The following topics are a partial match:\n");
  6425. }
  6426. fprintf (My->SYSOUT->cfp, IntrinsicFunctionTable[n].Name);
  6427. fprintf (My->SYSOUT->cfp, "\t");
  6428. Found = TRUE;
  6429. }
  6430. }
  6431. if (Found == TRUE)
  6432. {
  6433. /* match */
  6434. fprintf (My->SYSOUT->cfp, "\n");
  6435. }
  6436. }
  6437. if (Found == FALSE)
  6438. {
  6439. /* NO match */
  6440. fprintf (My->SYSOUT->cfp, "No help found.\n");
  6441. }
  6442. ResetConsoleColumn ();
  6443. line_skip_eol (l);
  6444. return (l);
  6445. }
  6446. int
  6447. NumberValueCheck (ParamTestType ParameterTests, DoubleType X)
  6448. {
  6449. DoubleType XR; /* rounded value */
  6450. unsigned char TestNibble;
  6451. /* VerifyNumeric */
  6452. if (isnan (X))
  6453. {
  6454. /* INTERNAL ERROR */
  6455. return -1;
  6456. }
  6457. if (isinf (X))
  6458. {
  6459. /* - Evaluation of an expression results in an overflow
  6460. * (nonfatal, the recommended recovery procedure is to supply
  6461. * machine in- finity with the algebraically correct sign and
  6462. * continue). */
  6463. if (X < 0)
  6464. {
  6465. X = MINDBL;
  6466. }
  6467. else
  6468. {
  6469. X = MAXDBL;
  6470. }
  6471. if (WARN_OVERFLOW)
  6472. {
  6473. /* ERROR */
  6474. return -1;
  6475. }
  6476. /* CONTINUE */
  6477. }
  6478. /* OK */
  6479. /* VALID NUMERIC VALUE */
  6480. XR = bwb_rint (X);
  6481. ParameterTests &= 0x0000000F;
  6482. TestNibble = (unsigned char) ParameterTests;
  6483. switch (TestNibble)
  6484. {
  6485. case P1ERR:
  6486. /* INTERNAL ERROR */
  6487. return -1;
  6488. /* break; */
  6489. case P1ANY:
  6490. if (X < MINDBL || X > MAXDBL)
  6491. {
  6492. /* ERROR */
  6493. return -1;
  6494. }
  6495. /* OK */
  6496. return 0;
  6497. /* break; */
  6498. case P1BYT:
  6499. if (XR < MINBYT || XR > MAXBYT)
  6500. {
  6501. /* ERROR */
  6502. return -1;
  6503. }
  6504. /* OK */
  6505. return 0;
  6506. /* break; */
  6507. case P1INT:
  6508. if (XR < MININT || XR > MAXINT)
  6509. {
  6510. /* ERROR */
  6511. return -1;
  6512. }
  6513. /* OK */
  6514. return 0;
  6515. /* break; */
  6516. case P1LNG:
  6517. if (XR < MINLNG || XR > MAXLNG)
  6518. {
  6519. /* ERROR */
  6520. return -1;
  6521. }
  6522. /* OK */
  6523. return 0;
  6524. /* break; */
  6525. case P1CUR:
  6526. if (XR < MINCUR || XR > MAXCUR)
  6527. {
  6528. /* ERROR */
  6529. return -1;
  6530. }
  6531. /* OK */
  6532. return 0;
  6533. /* break; */
  6534. case P1FLT:
  6535. if (X < MINSNG || X > MAXSNG)
  6536. {
  6537. /* ERROR */
  6538. return -1;
  6539. }
  6540. /* OK */
  6541. return 0;
  6542. /* break; */
  6543. case P1DBL:
  6544. if (X < MINDBL || X > MAXDBL)
  6545. {
  6546. /* ERROR */
  6547. return -1;
  6548. }
  6549. /* OK */
  6550. return 0;
  6551. /* break; */
  6552. case P1DEV:
  6553. /* ERROR */
  6554. return -1;
  6555. /* break; */
  6556. case P1LEN:
  6557. if (XR < MINLEN || XR > MAXLEN)
  6558. {
  6559. /* ERROR */
  6560. return -1;
  6561. }
  6562. /* OK */
  6563. return 0;
  6564. /* break; */
  6565. case P1POS:
  6566. if (XR < 1 || XR > MAXLEN)
  6567. {
  6568. /* ERROR */
  6569. return -1;
  6570. }
  6571. /* OK */
  6572. return 0;
  6573. /* break; */
  6574. case P1COM:
  6575. /* ERROR */
  6576. return -1;
  6577. /* break; */
  6578. case P1LPT:
  6579. /* ERROR */
  6580. return -1;
  6581. /* break; */
  6582. case P1GTZ:
  6583. if (X > 0)
  6584. {
  6585. /* OK */
  6586. return 0;
  6587. }
  6588. break;
  6589. case P1GEZ:
  6590. if (X >= 0)
  6591. {
  6592. /* OK */
  6593. return 0;
  6594. }
  6595. break;
  6596. case P1NEZ:
  6597. if (X != 0)
  6598. {
  6599. /* OK */
  6600. return 0;
  6601. }
  6602. break;
  6603. }
  6604. /* ERROR */
  6605. return -1;
  6606. }
  6607. int
  6608. StringLengthCheck (ParamTestType ParameterTests, int s)
  6609. {
  6610. unsigned char TestNibble;
  6611. /* check for invalid string length */
  6612. if (s < 0 || s > MAXLEN)
  6613. {
  6614. /* INTERNAL ERROR */
  6615. return -1;
  6616. }
  6617. /* VALID STRING LENGTH */
  6618. ParameterTests &= 0x0000000F;
  6619. TestNibble = (unsigned char) ParameterTests;
  6620. switch (TestNibble)
  6621. {
  6622. case P1ERR:
  6623. /* INTERNAL ERROR */
  6624. return -1;
  6625. /* break; */
  6626. case P1ANY:
  6627. /* OK */
  6628. return 0;
  6629. /* break; */
  6630. case P1BYT:
  6631. if (s >= sizeof (ByteType))
  6632. {
  6633. /* OK */
  6634. return 0;
  6635. }
  6636. break;
  6637. case P1INT:
  6638. if (s >= sizeof (IntegerType))
  6639. {
  6640. /* OK */
  6641. return 0;
  6642. }
  6643. break;
  6644. case P1LNG:
  6645. if (s >= sizeof (LongType))
  6646. {
  6647. /* OK */
  6648. return 0;
  6649. }
  6650. break;
  6651. case P1CUR:
  6652. if (s >= sizeof (CurrencyType))
  6653. {
  6654. /* OK */
  6655. return 0;
  6656. }
  6657. break;
  6658. case P1FLT:
  6659. if (s >= sizeof (SingleType))
  6660. {
  6661. /* OK */
  6662. return 0;
  6663. }
  6664. break;
  6665. case P1DBL:
  6666. if (s >= sizeof (DoubleType))
  6667. {
  6668. /* OK */
  6669. return 0;
  6670. }
  6671. break;
  6672. case P1DEV:
  6673. /* ERROR */
  6674. return -1;
  6675. /* break; */
  6676. case P1LEN:
  6677. /* ERROR */
  6678. return -1;
  6679. /* break; */
  6680. case P1POS:
  6681. /* ERROR */
  6682. return -1;
  6683. /* break; */
  6684. case P1GEZ:
  6685. /* ERROR */
  6686. return -1;
  6687. /* break; */
  6688. case P1GTZ:
  6689. /* ERROR */
  6690. return -1;
  6691. /* break; */
  6692. case P1NEZ:
  6693. /* ERROR */
  6694. return -1;
  6695. /* break; */
  6696. }
  6697. /* ERROR */
  6698. return -1;
  6699. }
  6700. void
  6701. IntrinsicFunctionDefinitionCheck (IntrinsicFunctionType * f)
  6702. {
  6703. /* function definition check -- look for obvious errors */
  6704. assert (f != NULL);
  6705. assert( My != NULL );
  6706. assert( My->SYSOUT != NULL );
  6707. assert( My->SYSOUT->cfp != NULL );
  6708. /* sanity check */
  6709. if (f->ParameterCount == PNONE || f->ParameterCount == 0xFF)
  6710. {
  6711. /* function has NO explicit parameters */
  6712. if (f->ParameterTypes == PNONE)
  6713. {
  6714. /* OK */
  6715. }
  6716. else
  6717. {
  6718. /* oops */
  6719. fprintf (My->SYSOUT->cfp, "invalid ParameterTypes <%s>\n", f->Name);
  6720. }
  6721. if (f->ParameterTests == PNONE)
  6722. {
  6723. /* OK */
  6724. }
  6725. else
  6726. {
  6727. /* oops */
  6728. fprintf (My->SYSOUT->cfp, "invalid ParameterTests <%s>\n", f->Name);
  6729. }
  6730. }
  6731. else
  6732. {
  6733. /* function HAS an explicit number of parameters */
  6734. int i;
  6735. ParamTestType ParameterTests;
  6736. ParameterTests = f->ParameterTests;
  6737. for (i = 0; i < f->ParameterCount; i++)
  6738. {
  6739. /* sanity check this parameter */
  6740. ParamTestType thischeck;
  6741. thischeck = ParameterTests & 0x0000000F;
  6742. /* verify parameter check */
  6743. if (f->ParameterTypes & (1 << i))
  6744. {
  6745. /* STRING */
  6746. if (thischeck >= P1ANY && thischeck <= P1DBL)
  6747. {
  6748. /* OK */
  6749. }
  6750. else
  6751. {
  6752. /* oops */
  6753. fprintf (My->SYSOUT->cfp,
  6754. "invalid ParameterTests <%s> parameter %d\n", f->Name,
  6755. i + 1);
  6756. }
  6757. }
  6758. else
  6759. {
  6760. /* NUMBER */
  6761. if (thischeck >= P1ANY && thischeck <= P1NEZ)
  6762. {
  6763. /* OK */
  6764. }
  6765. else
  6766. {
  6767. /* oops */
  6768. fprintf (My->SYSOUT->cfp,
  6769. "invalid ParameterTests <%s> parameter %d\n", f->Name,
  6770. i + 1);
  6771. }
  6772. }
  6773. ParameterTests = ParameterTests >> 4;
  6774. }
  6775. if (ParameterTests != 0)
  6776. {
  6777. /* oops */
  6778. fprintf (My->SYSOUT->cfp, "invalid ParameterTests <%s> parameter %d\n",
  6779. f->Name, i + 1);
  6780. }
  6781. }
  6782. }
  6783. void
  6784. IntrinsicFunctionUniqueID (IntrinsicFunctionType * f, char *UniqueID)
  6785. {
  6786. /* generate the function's UniqueID */
  6787. /* manual fixup required for duplicates */
  6788. char NumVar;
  6789. char StrVar;
  6790. assert (f != NULL);
  6791. assert (UniqueID != NULL);
  6792. NumVar = 'X';
  6793. StrVar = 'A';
  6794. /* F_ */
  6795. bwb_strcpy (UniqueID, "F_");
  6796. /* NAME */
  6797. bwb_strcat (UniqueID, f->Name);
  6798. /* PARAMETERS */
  6799. if (f->ParameterCount == PNONE || f->ParameterCount == 0xFF)
  6800. {
  6801. /* function has NO explicit parameters */
  6802. }
  6803. else
  6804. {
  6805. /* function HAS explicit parameters */
  6806. int i;
  6807. ParamBitsType ParameterTypes;
  6808. ParameterTypes = f->ParameterTypes;
  6809. for (i = 0; i < f->ParameterCount; i++)
  6810. {
  6811. char VarName[NameLengthMax + 1];
  6812. if (ParameterTypes & 1)
  6813. {
  6814. /* STRING */
  6815. sprintf (VarName, "_%c", StrVar);
  6816. StrVar++;
  6817. }
  6818. else
  6819. {
  6820. /* NUMBER */
  6821. sprintf (VarName, "_%c", NumVar);
  6822. NumVar++;
  6823. }
  6824. bwb_strcat (UniqueID, VarName);
  6825. ParameterTypes = ParameterTypes >> 1;
  6826. }
  6827. }
  6828. /* RETURN TYPE */
  6829. if (f->ReturnTypeCode == StringTypeCode)
  6830. {
  6831. bwb_strcat (UniqueID, "_S");
  6832. }
  6833. else
  6834. {
  6835. bwb_strcat (UniqueID, "_N");
  6836. }
  6837. /* fixup illegal characters, "DEF FN" "BLOAD:", "CLOAD*" */
  6838. FixUp (UniqueID);
  6839. }
  6840. void
  6841. IntrinsicFunctionSyntax (IntrinsicFunctionType * f, char *Syntax)
  6842. {
  6843. /* generate the function's Syntax */
  6844. char NumVar;
  6845. char StrVar;
  6846. assert (f != NULL);
  6847. assert (Syntax != NULL);
  6848. NumVar = 'X';
  6849. StrVar = 'A';
  6850. /* RETURN TYPE */
  6851. if (f->ReturnTypeCode == StringTypeCode)
  6852. {
  6853. bwb_strcpy (Syntax, "S$ = ");
  6854. }
  6855. else
  6856. {
  6857. bwb_strcpy (Syntax, "N = ");
  6858. }
  6859. /* NAME */
  6860. bwb_strcat (Syntax, f->Name);
  6861. /* PARAMETERS */
  6862. if (f->ParameterCount == PNONE)
  6863. {
  6864. /* function has NO explicit parameters */
  6865. }
  6866. else if (f->ParameterCount == 0xFF)
  6867. {
  6868. /* function has a variable number of parameters */
  6869. bwb_strcat (Syntax, "( ... )");
  6870. }
  6871. else
  6872. {
  6873. /* function HAS explicit parameters */
  6874. int i;
  6875. ParamBitsType ParameterTypes;
  6876. ParameterTypes = f->ParameterTypes;
  6877. if (f->ReturnTypeCode == StringTypeCode)
  6878. {
  6879. bwb_strcat (Syntax, "( ");
  6880. }
  6881. else
  6882. {
  6883. bwb_strcat (Syntax, "( ");
  6884. }
  6885. for (i = 0; i < f->ParameterCount; i++)
  6886. {
  6887. char VarName[NameLengthMax + 1];
  6888. if (i > 0)
  6889. {
  6890. bwb_strcat (Syntax, ", ");
  6891. }
  6892. /* verify parameter check */
  6893. if (ParameterTypes & 1)
  6894. {
  6895. /* STRING */
  6896. sprintf (VarName, "%c$", StrVar);
  6897. StrVar++;
  6898. }
  6899. else
  6900. {
  6901. /* NUMBER */
  6902. sprintf (VarName, "%c", NumVar);
  6903. NumVar++;
  6904. }
  6905. bwb_strcat (Syntax, VarName);
  6906. ParameterTypes = ParameterTypes >> 1;
  6907. }
  6908. if (f->ReturnTypeCode == StringTypeCode)
  6909. {
  6910. bwb_strcat (Syntax, " )");
  6911. }
  6912. else
  6913. {
  6914. bwb_strcat (Syntax, " )");
  6915. }
  6916. }
  6917. }
  6918. void
  6919. DumpAllFunctionUniqueID (FILE * file)
  6920. {
  6921. /* for the C maintainer */
  6922. int i;
  6923. int j;
  6924. char LastUniqueID[NameLengthMax + 1];
  6925. assert (file != NULL);
  6926. j = 0;
  6927. LastUniqueID[0] = NulChar;
  6928. fprintf (file, "/* FUNCTIONS */\n");
  6929. for (i = 0; i < NUM_FUNCTIONS; i++)
  6930. {
  6931. char UniqueID[NameLengthMax + 1];
  6932. IntrinsicFunctionUniqueID (&(IntrinsicFunctionTable[i]), UniqueID);
  6933. if (bwb_stricmp (LastUniqueID, UniqueID) != 0)
  6934. {
  6935. /* not a duplicate */
  6936. char Syntax[NameLengthMax + 1];
  6937. bwb_strcpy (LastUniqueID, UniqueID);
  6938. j = j + 1;
  6939. IntrinsicFunctionSyntax (&(IntrinsicFunctionTable[i]), Syntax);
  6940. fprintf (file, "#define %-30s %3d /* %-30s */\n", UniqueID, j, Syntax);
  6941. }
  6942. }
  6943. fprintf (file, "#define NUM_FUNCTIONS %d\n", j);
  6944. fflush (file);
  6945. }
  6946. void
  6947. DumpAllFunctionSwitch (FILE * file)
  6948. {
  6949. /* for the C maintainer */
  6950. int i;
  6951. assert (file != NULL);
  6952. fprintf (file, "/* SWITCH */\n");
  6953. fprintf (file, "switch( UniqueID )\n");
  6954. fprintf (file, "{\n");
  6955. for (i = 0; i < NUM_FUNCTIONS; i++)
  6956. {
  6957. char tbuf[MAINTAINER_BUFFER_LENGTH + 1]; /* DumpAllFunctionSwitch */
  6958. fprintf (file, "case ");
  6959. IntrinsicFunctionUniqueID (&(IntrinsicFunctionTable[i]), tbuf);
  6960. fprintf (file, tbuf);
  6961. fprintf (file, ":\n");
  6962. fprintf (file, " break;\n");
  6963. }
  6964. fprintf (file, "}\n");
  6965. fflush (file);
  6966. }
  6967. static const char *ParameterRangeID[16] = {
  6968. "P%dERR",
  6969. "P%dANY",
  6970. "P%dBYT",
  6971. "P%dINT",
  6972. "P%dLNG",
  6973. "P%dCUR",
  6974. "P%dFLT",
  6975. "P%dDBL",
  6976. "P%dDEV",
  6977. "P%dLEN",
  6978. "P%dPOS",
  6979. "P%dCOM",
  6980. "P%dLPT",
  6981. "P%dGTZ",
  6982. "P%dGEZ",
  6983. "P%dNEZ",
  6984. };
  6985. static const char *NumberVariableRange[16] = {
  6986. /* P1ERR */ " PARAMETER: %c is a number, INTERNAL ERROR",
  6987. /* P1ANY */ " PARAMETER: %c is a number",
  6988. /* P1BYT */ " PARAMETER: %c is a number, [0,255]",
  6989. /* P1INT */ " PARAMETER: %c is a number, [MININT,MAXINT]",
  6990. /* P1LNG */ " PARAMETER: %c is a number, [MINLNG,MAXLNG]",
  6991. /* P1CUR */ " PARAMETER: %c is a number, [MINCUR,MAXCUR]",
  6992. /* P1FLT */ " PARAMETER: %c is a number, [MINFLT,MAXFLT]",
  6993. /* P1DBL */ " PARAMETER: %c is a number, [MINDBL,MAXDBL]",
  6994. /* P1DEV */ " PARAMETER: %c is a number, RESERVED",
  6995. /* P1LEN */ " PARAMETER: %c is a number, [0,MAXLEN]",
  6996. /* P1POS */ " PARAMETER: %c is a number, [1,MAXLEN]",
  6997. /* P1COM */ " PARAMETER: %c is a number, RESERVED",
  6998. /* P1LPT */ " PARAMETER: %c is a number, RESERVED",
  6999. /* P1GTZ */ " PARAMETER: %c is a number, > 0",
  7000. /* P1GEZ */ " PARAMETER: %c is a number, >= 0",
  7001. /* P1NEZ */ " PARAMETER: %c is a number, <> 0",
  7002. };
  7003. static const char *StringVariableRange[16] = {
  7004. /* P1ERR */ " PARAMETER: %c$ is a string, INTERNAL ERROR",
  7005. /* P1ANY */ " PARAMETER: %c$ is a string, LEN >= 0",
  7006. /* P1BYT */ " PARAMETER: %c$ is a string, LEN >= 1",
  7007. /* P1INT */ " PARAMETER: %c$ is a string, LEN >= sizeof(INT)",
  7008. /* P1LNG */ " PARAMETER: %c$ is a string, LEN >= sizeof(LNG)",
  7009. /* P1CUR */ " PARAMETER: %c$ is a string, LEN >= sizeof(CUR)",
  7010. /* P1FLT */ " PARAMETER: %c$ is a string, LEN >= sizeof(FLT)",
  7011. /* P1DBL */ " PARAMETER: %c$ is a string, LEN >= sizeof(DBL)",
  7012. /* P1DEV */ " PARAMETER: %c$ is a string, RESERVED",
  7013. /* P1LEN */ " PARAMETER: %c$ is a string, RESERVED",
  7014. /* P1POS */ " PARAMETER: %c$ is a string, RESERVED",
  7015. /* P1COM */ " PARAMETER: %c$ is a string, RESERVED",
  7016. /* P1LPT */ " PARAMETER: %c$ is a string, RESERVED",
  7017. /* P1GTZ */ " PARAMETER: %c$ is a string, RESERVED",
  7018. /* P1GEZ */ " PARAMETER: %c$ is a string, RESERVED",
  7019. /* P1NEZ */ " PARAMETER: %c$ is a string, RESERVED",
  7020. };
  7021. void
  7022. DumpAllFuctionTableDefinitions (FILE * file)
  7023. {
  7024. /* generate bwd_fun.c */
  7025. int n;
  7026. assert (file != NULL);
  7027. fprintf (file, "/* FUNCTION TABLE */\n");
  7028. fprintf (file, "\n");
  7029. fprintf (file, "#include \"bwbasic.h\"\n");
  7030. fprintf (file, "\n");
  7031. fprintf (file,
  7032. "IntrinsicFunctionType IntrinsicFunctionTable[ /* NUM_FUNCTIONS */ ] =\n");
  7033. fprintf (file, "{\n");
  7034. for (n = 0; n < NUM_FUNCTIONS; n++)
  7035. {
  7036. int i;
  7037. int j;
  7038. char tbuf[MAINTAINER_BUFFER_LENGTH + 1]; /* DumpAllFuctionTableDefinitions */
  7039. char UniqueID[MAINTAINER_BUFFER_LENGTH + 1]; /* DumpAllFuctionTableDefinitions */
  7040. char Syntax[MAINTAINER_BUFFER_LENGTH + 1]; /* DumpAllFuctionTableDefinitions */
  7041. IntrinsicFunctionType *f;
  7042. f = &(IntrinsicFunctionTable[n]);
  7043. IntrinsicFunctionUniqueID (f, UniqueID);
  7044. IntrinsicFunctionSyntax (f, Syntax);
  7045. fprintf (file, "{\n");
  7046. fprintf (file, " %s, /* UniqueID */\n", UniqueID);
  7047. fprintf (file, " \"%s\", /* Syntax */\n", Syntax);
  7048. fprintf (file, " ");
  7049. fprintf (file, "\"");
  7050. ProcessEscapeChars (f->Description, tbuf);
  7051. fprintf (file, tbuf);
  7052. fprintf (file, "\"");
  7053. fprintf (file, ", /* Description */\n");
  7054. fprintf (file, " \"%s\", /* Name */\n", f->Name);
  7055. switch (f->ReturnTypeCode)
  7056. {
  7057. case ByteTypeCode:
  7058. fprintf (file, " %s, /* ReturnTypeCode */\n", "ByteTypeCode");
  7059. break;
  7060. case IntegerTypeCode:
  7061. fprintf (file, " %s, /* ReturnTypeCode */\n", "IntegerTypeCode");
  7062. break;
  7063. case LongTypeCode:
  7064. fprintf (file, " %s, /* ReturnTypeCode */\n", "LongTypeCode");
  7065. break;
  7066. case CurrencyTypeCode:
  7067. fprintf (file, " %s, /* ReturnTypeCode */\n", "CurrencyTypeCode");
  7068. break;
  7069. case SingleTypeCode:
  7070. fprintf (file, " %s, /* ReturnTypeCode */\n", "SingleTypeCode");
  7071. break;
  7072. case DoubleTypeCode:
  7073. fprintf (file, " %s, /* ReturnTypeCode */\n", "DoubleTypeCode");
  7074. break;
  7075. case StringTypeCode:
  7076. fprintf (file, " %s, /* ReturnTypeCode */\n", "StringTypeCode");
  7077. break;
  7078. default:
  7079. fprintf (file, " %s, /* ReturnTypeCode */\n", "INTERNAL ERROR");
  7080. break;
  7081. }
  7082. fprintf (file, " %d, /* ParameterCount */\n", f->ParameterCount);
  7083. if (f->ParameterCount == 0 || f->ParameterCount == 0xFF)
  7084. {
  7085. /* function has NO explicit parameters */
  7086. fprintf (file, " %s, /* ParameterTypes */\n", "PNONE");
  7087. fprintf (file, " %s, /* ParameterTests */\n", "PNONE");
  7088. }
  7089. else
  7090. {
  7091. /* function has explicit parameters */
  7092. bwb_strcpy (tbuf, " ");
  7093. for (i = 0; i < f->ParameterCount; i++)
  7094. {
  7095. ParamBitsType ParameterTypes;
  7096. ParameterTypes = f->ParameterTypes >> i;
  7097. ParameterTypes &= 0x1;
  7098. if (i > 0)
  7099. {
  7100. bwb_strcat (tbuf, " | ");
  7101. }
  7102. if (ParameterTypes)
  7103. {
  7104. sprintf (bwb_strchr (tbuf, NulChar), "P%dSTR", i + 1);
  7105. }
  7106. else
  7107. {
  7108. sprintf (bwb_strchr (tbuf, NulChar), "P%dNUM", i + 1);
  7109. }
  7110. }
  7111. bwb_strcat (tbuf, ", /* ParameterTypes */\n");
  7112. fprintf (file, tbuf);
  7113. bwb_strcpy (tbuf, " ");
  7114. for (i = 0; i < f->ParameterCount; i++)
  7115. {
  7116. ParamTestType ParameterTests;
  7117. ParameterTests = f->ParameterTests >> (i * 4);
  7118. ParameterTests &= 0xF;
  7119. if (i > 0)
  7120. {
  7121. bwb_strcat (tbuf, " | ");
  7122. }
  7123. sprintf (bwb_strchr (tbuf, 0), ParameterRangeID[ParameterTests],
  7124. i + 1);
  7125. /* Conversion may lose significant digits */
  7126. }
  7127. bwb_strcat (tbuf, ", /* ParameterTests */\n");
  7128. fprintf (file, tbuf);
  7129. }
  7130. bwb_strcpy (tbuf, " ");
  7131. j = 0;
  7132. for (i = 0; i < NUM_VERSIONS; i++)
  7133. {
  7134. if (f->OptionVersionBitmask & bwb_vertable[i].OptionVersionValue)
  7135. {
  7136. if (j > 0)
  7137. {
  7138. bwb_strcat (tbuf, " | ");
  7139. }
  7140. bwb_strcat (tbuf, bwb_vertable[i].ID);
  7141. j++;
  7142. }
  7143. }
  7144. bwb_strcat (tbuf, " /* OptionVersionBitmask */\n");
  7145. fprintf (file, tbuf);
  7146. fprintf (file, "},\n");
  7147. }
  7148. fprintf (file, "};\n");
  7149. fprintf (file, "\n");
  7150. fprintf (file,
  7151. "const size_t NUM_FUNCTIONS = sizeof( IntrinsicFunctionTable ) / sizeof( IntrinsicFunctionType );\n");
  7152. fprintf (file, "\n");
  7153. fflush (file);
  7154. }
  7155. void
  7156. DumpOneFunctionSyntax (FILE * file, int IsXref, int n)
  7157. {
  7158. IntrinsicFunctionType *f;
  7159. assert (file != NULL);
  7160. if (n < 0 || n >= NUM_FUNCTIONS)
  7161. {
  7162. return;
  7163. }
  7164. f = &(IntrinsicFunctionTable[n]);
  7165. /* NAME */
  7166. {
  7167. char UniqueID[MAINTAINER_BUFFER_LENGTH + 1]; /* DumpOneFunctionSyntax */
  7168. char Syntax[MAINTAINER_BUFFER_LENGTH + 1]; /* DumpOneFunctionSyntax */
  7169. IntrinsicFunctionUniqueID (f, UniqueID);
  7170. IntrinsicFunctionSyntax (f, Syntax);
  7171. fprintf (file, " SYNTAX: %s\n", Syntax);
  7172. }
  7173. /* PARAMETERS */
  7174. if (f->ParameterCount == PNONE || f->ParameterCount == 0xFF)
  7175. {
  7176. /* function has NO explicit parameters */
  7177. }
  7178. else
  7179. {
  7180. /* function HAS explicit parameters */
  7181. int i;
  7182. ParamBitsType ParameterTypes;
  7183. ParamTestType ParameterTests;
  7184. char NumVar;
  7185. char StrVar;
  7186. ParameterTypes = f->ParameterTypes;
  7187. ParameterTests = f->ParameterTests;
  7188. NumVar = 'X';
  7189. StrVar = 'A';
  7190. for (i = 0; i < f->ParameterCount; i++)
  7191. {
  7192. /* sanity check this parameter */
  7193. unsigned long thischeck;
  7194. char tbuf[MAINTAINER_BUFFER_LENGTH + 1]; /* DumpOneFunctionSyntax */
  7195. thischeck = ParameterTests & 0x0000000F;
  7196. /* verify parameter check */
  7197. if (ParameterTypes & 1)
  7198. {
  7199. /* STRING */
  7200. sprintf (tbuf, StringVariableRange[thischeck], StrVar);
  7201. /* Conversion may lose significant digits */
  7202. StrVar++;
  7203. }
  7204. else
  7205. {
  7206. /* NUMBER */
  7207. sprintf (tbuf, NumberVariableRange[thischeck], NumVar);
  7208. /* Conversion may lose significant digits */
  7209. NumVar++;
  7210. }
  7211. fprintf (file, tbuf);
  7212. fprintf (file, "\n");
  7213. ParameterTypes = ParameterTypes >> 1;
  7214. ParameterTests = ParameterTests >> 4;
  7215. }
  7216. }
  7217. /* DESCRIPTION */
  7218. {
  7219. FixDescription (file, "DESCRIPTION: ", f->Description);
  7220. }
  7221. /* COMPATIBILITY */
  7222. if (IsXref)
  7223. {
  7224. int i;
  7225. fprintf (file, " VERSIONS:\n");
  7226. for (i = 0; i < NUM_VERSIONS; i++)
  7227. {
  7228. char X;
  7229. if (f->OptionVersionBitmask & bwb_vertable[i].OptionVersionValue)
  7230. {
  7231. /* SUPPORTED */
  7232. X = 'X';
  7233. }
  7234. else
  7235. {
  7236. /* NOT SUPPORTED */
  7237. X = '_';
  7238. }
  7239. fprintf (file, " [%c] %s\n", X, bwb_vertable[i].Name);
  7240. }
  7241. }
  7242. fflush (file);
  7243. }
  7244. void
  7245. DumpAllFunctionSyntax (FILE * file, int IsXref,
  7246. OptionVersionType OptionVersionValue)
  7247. {
  7248. /* for the C maintainer */
  7249. int i;
  7250. assert (file != NULL);
  7251. fprintf (file,
  7252. "============================================================\n");
  7253. fprintf (file,
  7254. " FUNCTIONS \n");
  7255. fprintf (file,
  7256. "============================================================\n");
  7257. fprintf (file, "\n");
  7258. fprintf (file, "\n");
  7259. for (i = 0; i < NUM_FUNCTIONS; i++)
  7260. {
  7261. if (IntrinsicFunctionTable[i].OptionVersionBitmask & OptionVersionValue)
  7262. {
  7263. fprintf (file,
  7264. "------------------------------------------------------------\n");
  7265. DumpOneFunctionSyntax (file, IsXref, i);
  7266. }
  7267. }
  7268. fprintf (file,
  7269. "------------------------------------------------------------\n");
  7270. fprintf (file, "\n");
  7271. fprintf (file, "\n");
  7272. fflush (file);
  7273. }
  7274. void
  7275. DumpAllFunctionHtmlTable (FILE * file)
  7276. {
  7277. /* generate bwd_cmd.htm */
  7278. int i;
  7279. int j;
  7280. assert (file != NULL);
  7281. /* LEGEND */
  7282. fprintf (file, "<html><head><title>FNCS</title></head><body>\n");
  7283. fprintf (file, "<h1>LEGEND</h1><br>\n");
  7284. fprintf (file, "<table>\n");
  7285. fprintf (file, "<tr>");
  7286. fprintf (file, "<td>");
  7287. fprintf (file, "<b>");
  7288. fprintf (file, "ID");
  7289. fprintf (file, "</b>");
  7290. fprintf (file, "</td>");
  7291. fprintf (file, "<td>");
  7292. fprintf (file, "<b>");
  7293. fprintf (file, "NAME");
  7294. fprintf (file, "</b>");
  7295. fprintf (file, "</td>");
  7296. fprintf (file, "<td>");
  7297. fprintf (file, "<b>");
  7298. fprintf (file, "DESCRIPTION");
  7299. fprintf (file, "</b>");
  7300. fprintf (file, "</td>");
  7301. fprintf (file, "</tr>\n");
  7302. for (j = 0; j < NUM_VERSIONS; j++)
  7303. {
  7304. fprintf (file, "<tr>");
  7305. fprintf (file, "<td>");
  7306. fprintf (file, bwb_vertable[j].ID);
  7307. fprintf (file, "</td>");
  7308. fprintf (file, "<td>");
  7309. fprintf (file, bwb_vertable[j].Name);
  7310. fprintf (file, "</td>");
  7311. fprintf (file, "<td>");
  7312. fprintf (file, bwb_vertable[j].Description);
  7313. fprintf (file, "</td>");
  7314. fprintf (file, "</tr>\n");
  7315. }
  7316. fprintf (file, "</table>\n");
  7317. fprintf (file, "<hr>\n");
  7318. /* DETAILS */
  7319. fprintf (file, "<h1>DETAILS</h1><br>\n");
  7320. fprintf (file, "<table>\n");
  7321. fprintf (file, "<tr>");
  7322. fprintf (file, "<td>");
  7323. fprintf (file, "<b>");
  7324. fprintf (file, "FUNCTION");
  7325. fprintf (file, "</b>");
  7326. fprintf (file, "</td>");
  7327. for (j = 0; j < NUM_VERSIONS; j++)
  7328. {
  7329. fprintf (file, "<td>");
  7330. fprintf (file, "<b>");
  7331. fprintf (file, bwb_vertable[j].ID);
  7332. fprintf (file, "</b>");
  7333. fprintf (file, "</td>");
  7334. }
  7335. fprintf (file, "</tr>\n");
  7336. /* run through the command table and print comand -vs- OPTION VERSION */
  7337. for (i = 0; i < NUM_FUNCTIONS; i++)
  7338. {
  7339. fprintf (file, "<tr>");
  7340. fprintf (file, "<td>");
  7341. fprintf (file, (char *) IntrinsicFunctionTable[i].Syntax);
  7342. fprintf (file, "</td>");
  7343. for (j = 0; j < NUM_VERSIONS; j++)
  7344. {
  7345. fprintf (file, "<td>");
  7346. if (IntrinsicFunctionTable[i].OptionVersionBitmask & bwb_vertable[j].
  7347. OptionVersionValue)
  7348. {
  7349. fprintf (file, "X");
  7350. }
  7351. else
  7352. {
  7353. fprintf (file, " ");
  7354. }
  7355. fprintf (file, "</td>");
  7356. }
  7357. fprintf (file, "</tr>\n");
  7358. }
  7359. fprintf (file, "</table>\n");
  7360. fprintf (file, "</body></html>\n");
  7361. fprintf (file, "\n");
  7362. fflush (file);
  7363. }
  7364. /*
  7365. --------------------------------------------------------------------------------------------
  7366. FNCS
  7367. --------------------------------------------------------------------------------------------
  7368. */
  7369. LineType *
  7370. bwb_FNCS (LineType * l)
  7371. {
  7372. int n;
  7373. int t;
  7374. assert (l != NULL);
  7375. assert( My != NULL );
  7376. assert( My->SYSOUT != NULL );
  7377. assert( My->SYSOUT->cfp != NULL );
  7378. My->CurrentFile = My->SYSOUT;
  7379. fprintf (My->SYSOUT->cfp, "BWBASIC FUNCTIONS AVAILABLE:\n");
  7380. /* run through the command table and print comand names */
  7381. t = 0;
  7382. for (n = 0; n < NUM_FUNCTIONS; n++)
  7383. {
  7384. fprintf (My->SYSOUT->cfp, IntrinsicFunctionTable[n].Name);
  7385. if (t < 4)
  7386. {
  7387. fprintf (My->SYSOUT->cfp, "\t");
  7388. t++;
  7389. }
  7390. else
  7391. {
  7392. fprintf (My->SYSOUT->cfp, "\n");
  7393. t = 0;
  7394. }
  7395. }
  7396. if (t > 0)
  7397. {
  7398. fprintf (My->SYSOUT->cfp, "\n");
  7399. }
  7400. ResetConsoleColumn ();
  7401. return (l);
  7402. }
  7403. /*
  7404. --------------------------------------------------------------------------------------------
  7405. MAINTAINER
  7406. --------------------------------------------------------------------------------------------
  7407. */
  7408. LineType *
  7409. bwb_MAINTAINER (LineType * l)
  7410. {
  7411. assert (l != NULL);
  7412. WARN_SYNTAX_ERROR;
  7413. return (l);
  7414. }
  7415. LineType *
  7416. bwb_MAINTAINER_CMDS (LineType * l)
  7417. {
  7418. assert (l != NULL);
  7419. WARN_SYNTAX_ERROR;
  7420. return (l);
  7421. }
  7422. LineType *
  7423. bwb_MAINTAINER_CMDS_HTML (LineType * l)
  7424. {
  7425. assert (l != NULL);
  7426. assert(My != NULL);
  7427. assert(My->SYSPRN != NULL);
  7428. assert(My->SYSPRN->cfp != NULL);
  7429. DumpAllCommandHtmlTable (My->SYSPRN->cfp);
  7430. return (l);
  7431. }
  7432. LineType *
  7433. bwb_MAINTAINER_CMDS_ID (LineType * l)
  7434. {
  7435. assert (l != NULL);
  7436. assert(My != NULL);
  7437. assert(My->SYSPRN != NULL);
  7438. assert(My->SYSPRN->cfp != NULL);
  7439. DumpAllCommandUniqueID (My->SYSPRN->cfp);
  7440. return (l);
  7441. }
  7442. LineType *
  7443. bwb_MAINTAINER_CMDS_MANUAL (LineType * l)
  7444. {
  7445. assert (l != NULL);
  7446. assert(My != NULL);
  7447. assert(My->SYSPRN != NULL);
  7448. assert(My->SYSPRN->cfp != NULL);
  7449. DumpAllCommandSyntax (My->SYSPRN->cfp, TRUE, (OptionVersionType)(-1));
  7450. return (l);
  7451. }
  7452. LineType *
  7453. bwb_MAINTAINER_CMDS_SWITCH (LineType * l)
  7454. {
  7455. assert (l != NULL);
  7456. assert(My != NULL);
  7457. assert(My->SYSPRN != NULL);
  7458. assert(My->SYSPRN->cfp != NULL);
  7459. DumpAllCommandSwitchStatement (My->SYSPRN->cfp);
  7460. return (l);
  7461. }
  7462. LineType *
  7463. bwb_MAINTAINER_CMDS_TABLE (LineType * l)
  7464. {
  7465. assert (l != NULL);
  7466. assert(My != NULL);
  7467. assert(My->SYSPRN != NULL);
  7468. assert(My->SYSPRN->cfp != NULL);
  7469. DumpAllCommandTableDefinitions (My->SYSPRN->cfp);
  7470. return (l);
  7471. }
  7472. LineType *
  7473. bwb_MAINTAINER_DEBUG (LineType * l)
  7474. {
  7475. assert (l != NULL);
  7476. WARN_SYNTAX_ERROR;
  7477. return (l);
  7478. }
  7479. LineType *
  7480. bwb_MAINTAINER_DEBUG_ON (LineType * l)
  7481. {
  7482. assert (l != NULL);
  7483. return (l);
  7484. }
  7485. LineType *
  7486. bwb_MAINTAINER_DEBUG_OFF (LineType * l)
  7487. {
  7488. assert (l != NULL);
  7489. return (l);
  7490. }
  7491. LineType *
  7492. bwb_MAINTAINER_FNCS (LineType * l)
  7493. {
  7494. assert (l != NULL);
  7495. WARN_SYNTAX_ERROR;
  7496. return (l);
  7497. }
  7498. LineType *
  7499. bwb_MAINTAINER_FNCS_HTML (LineType * l)
  7500. {
  7501. assert (l != NULL);
  7502. assert(My != NULL);
  7503. assert(My->SYSPRN != NULL);
  7504. assert(My->SYSPRN->cfp != NULL);
  7505. DumpAllFunctionHtmlTable (My->SYSPRN->cfp);
  7506. return (l);
  7507. }
  7508. LineType *
  7509. bwb_MAINTAINER_FNCS_ID (LineType * l)
  7510. {
  7511. assert (l != NULL);
  7512. assert(My != NULL);
  7513. assert(My->SYSPRN != NULL);
  7514. assert(My->SYSPRN->cfp != NULL);
  7515. DumpAllFunctionUniqueID (My->SYSPRN->cfp);
  7516. return (l);
  7517. }
  7518. LineType *
  7519. bwb_MAINTAINER_FNCS_MANUAL (LineType * l)
  7520. {
  7521. assert (l != NULL);
  7522. assert(My != NULL);
  7523. assert(My->SYSPRN != NULL);
  7524. assert(My->SYSPRN->cfp != NULL);
  7525. DumpAllFunctionSyntax (My->SYSPRN->cfp, TRUE, (OptionVersionType)(-1));
  7526. DumpAllOperatorSyntax (My->SYSPRN->cfp, TRUE, (OptionVersionType)(-1));
  7527. return (l);
  7528. }
  7529. LineType *
  7530. bwb_MAINTAINER_FNCS_SWITCH (LineType * l)
  7531. {
  7532. assert (l != NULL);
  7533. assert(My != NULL);
  7534. assert(My->SYSPRN != NULL);
  7535. assert(My->SYSPRN->cfp != NULL);
  7536. DumpAllFunctionSwitch (My->SYSPRN->cfp);
  7537. return (l);
  7538. }
  7539. LineType *
  7540. bwb_MAINTAINER_FNCS_TABLE (LineType * l)
  7541. {
  7542. assert (l != NULL);
  7543. assert(My != NULL);
  7544. assert(My->SYSPRN != NULL);
  7545. assert(My->SYSPRN->cfp != NULL);
  7546. DumpAllFuctionTableDefinitions (My->SYSPRN->cfp);
  7547. return (l);
  7548. }
  7549. void
  7550. DumpHeader (FILE * file)
  7551. {
  7552. char c;
  7553. assert (file != NULL);
  7554. assert(My != NULL);
  7555. assert(My->CurrentVersion != NULL);
  7556. fprintf (file,
  7557. "============================================================\n");
  7558. fprintf (file,
  7559. " GENERAL \n");
  7560. fprintf (file,
  7561. "============================================================\n");
  7562. fprintf (file, "\n");
  7563. fprintf (file, "\n");
  7564. fprintf (file, "OPTION VERSION \"%s\"\n", My->CurrentVersion->Name);
  7565. fprintf (file, "REM INTERNAL ID: %s\n", My->CurrentVersion->ID);
  7566. fprintf (file, "REM DESCRIPTION: %s\n", My->CurrentVersion->Description);
  7567. fprintf (file, "REM REFERENCE: %s\n", My->CurrentVersion->ReferenceTitle);
  7568. fprintf (file, "REM %s\n",
  7569. My->CurrentVersion->ReferenceAuthor);
  7570. fprintf (file, "REM %s\n",
  7571. My->CurrentVersion->ReferenceCopyright);
  7572. fprintf (file, "REM %s\n", My->CurrentVersion->ReferenceURL1);
  7573. fprintf (file, "REM %s\n", My->CurrentVersion->ReferenceURL2);
  7574. fprintf (file, "REM\n");
  7575. if (My->CurrentVersion->OptionFlags & (OPTION_STRICT_ON))
  7576. {
  7577. fprintf (file, "OPTION STRICT ON\n");
  7578. }
  7579. else
  7580. {
  7581. fprintf (file, "OPTION STRICT OFF\n");
  7582. }
  7583. if (My->CurrentVersion->OptionFlags & (OPTION_ANGLE_DEGREES))
  7584. {
  7585. fprintf (file, "OPTION ANGLE DEGREES\n");
  7586. }
  7587. else if (My->CurrentVersion->OptionFlags & (OPTION_ANGLE_GRADIANS))
  7588. {
  7589. fprintf (file, "OPTION ANGLE GRADIANS\n");
  7590. }
  7591. else
  7592. {
  7593. fprintf (file, "OPTION ANGLE RADIANS\n");
  7594. }
  7595. if (My->CurrentVersion->OptionFlags & (OPTION_BUGS_ON))
  7596. {
  7597. fprintf (file, "OPTION BUGS ON\n");
  7598. }
  7599. else
  7600. {
  7601. fprintf (file, "OPTION BUGS OFF\n");
  7602. }
  7603. if (My->CurrentVersion->OptionFlags & (OPTION_LABELS_ON))
  7604. {
  7605. fprintf (file, "OPTION LABELS ON\n");
  7606. }
  7607. else
  7608. {
  7609. fprintf (file, "OPTION LABELS OFF\n");
  7610. }
  7611. if (My->CurrentVersion->OptionFlags & (OPTION_COMPARE_TEXT))
  7612. {
  7613. fprintf (file, "OPTION COMPARE TEXT\n");
  7614. }
  7615. else
  7616. {
  7617. fprintf (file, "OPTION COMPARE BINARY\n");
  7618. }
  7619. if (My->CurrentVersion->OptionFlags & (OPTION_COVERAGE_ON))
  7620. {
  7621. fprintf (file, "OPTION COVERAGE ON\n");
  7622. }
  7623. else
  7624. {
  7625. fprintf (file, "OPTION COVERAGE OFF\n");
  7626. }
  7627. if (My->CurrentVersion->OptionFlags & (OPTION_TRACE_ON))
  7628. {
  7629. fprintf (file, "OPTION TRACE ON\n");
  7630. }
  7631. else
  7632. {
  7633. fprintf (file, "OPTION TRACE OFF\n");
  7634. }
  7635. if (My->CurrentVersion->OptionFlags & (OPTION_ERROR_GOSUB))
  7636. {
  7637. fprintf (file, "OPTION ERROR GOSUB\n");
  7638. }
  7639. else
  7640. {
  7641. fprintf (file, "OPTION ERROR GOTO\n");
  7642. }
  7643. if (My->CurrentVersion->OptionFlags & (OPTION_EXPLICIT_ON))
  7644. {
  7645. fprintf (file, "OPTION EXPLICIT\n");
  7646. }
  7647. else
  7648. {
  7649. fprintf (file, "OPTION IMPLICIT\n");
  7650. }
  7651. fprintf (file, "OPTION BASE %d\n",
  7652. My->CurrentVersion->OptionBaseInteger);
  7653. fprintf (file, "OPTION RECLEN %d\n",
  7654. My->CurrentVersion->OptionReclenInteger);
  7655. fprintf (file, "OPTION DATE \"%s\"\n",
  7656. My->CurrentVersion->OptionDateFormat);
  7657. fprintf (file, "OPTION TIME \"%s\"\n",
  7658. My->CurrentVersion->OptionTimeFormat);
  7659. c = My->CurrentVersion->OptionStringChar;
  7660. if (!bwb_isgraph (c))
  7661. {
  7662. c = ' ';
  7663. };
  7664. fprintf (file, "OPTION PUNCT STRING \"%c\"\n", c);
  7665. c = My->CurrentVersion->OptionDoubleChar;
  7666. if (!bwb_isgraph (c))
  7667. {
  7668. c = ' ';
  7669. };
  7670. fprintf (file, "OPTION PUNCT DOUBLE \"%c\"\n", c);
  7671. c = My->CurrentVersion->OptionSingleChar;
  7672. if (!bwb_isgraph (c))
  7673. {
  7674. c = ' ';
  7675. };
  7676. fprintf (file, "OPTION PUNCT SINGLE \"%c\"\n", c);
  7677. c = My->CurrentVersion->OptionCurrencyChar;
  7678. if (!bwb_isgraph (c))
  7679. {
  7680. c = ' ';
  7681. };
  7682. fprintf (file, "OPTION PUNCT CURRENCY \"%c\"\n", c);
  7683. c = My->CurrentVersion->OptionLongChar;
  7684. if (!bwb_isgraph (c))
  7685. {
  7686. c = ' ';
  7687. };
  7688. fprintf (file, "OPTION PUNCT LONG \"%c\"\n", c);
  7689. c = My->CurrentVersion->OptionIntegerChar;
  7690. if (!bwb_isgraph (c))
  7691. {
  7692. c = ' ';
  7693. };
  7694. fprintf (file, "OPTION PUNCT INTEGER \"%c\"\n", c);
  7695. c = My->CurrentVersion->OptionByteChar;
  7696. if (!bwb_isgraph (c))
  7697. {
  7698. c = ' ';
  7699. };
  7700. fprintf (file, "OPTION PUNCT BYTE \"%c\"\n", c);
  7701. c = My->CurrentVersion->OptionQuoteChar;
  7702. if (!bwb_isgraph (c))
  7703. {
  7704. c = ' ';
  7705. };
  7706. fprintf (file, "OPTION PUNCT QUOTE \"%c\"\n", c);
  7707. c = My->CurrentVersion->OptionCommentChar;
  7708. if (!bwb_isgraph (c))
  7709. {
  7710. c = ' ';
  7711. };
  7712. fprintf (file, "OPTION PUNCT COMMENT \"%c\"\n", c);
  7713. c = My->CurrentVersion->OptionStatementChar;
  7714. if (!bwb_isgraph (c))
  7715. {
  7716. c = ' ';
  7717. };
  7718. fprintf (file, "OPTION PUNCT STATEMENT \"%c\"\n", c);
  7719. c = My->CurrentVersion->OptionPrintChar;
  7720. if (!bwb_isgraph (c))
  7721. {
  7722. c = ' ';
  7723. };
  7724. fprintf (file, "OPTION PUNCT PRINT \"%c\"\n", c);
  7725. c = My->CurrentVersion->OptionInputChar;
  7726. if (!bwb_isgraph (c))
  7727. {
  7728. c = ' ';
  7729. };
  7730. fprintf (file, "OPTION PUNCT INPUT \"%c\"\n", c);
  7731. c = My->CurrentVersion->OptionImageChar;
  7732. if (!bwb_isgraph (c))
  7733. {
  7734. c = ' ';
  7735. };
  7736. fprintf (file, "OPTION PUNCT IMAGE \"%c\"\n", c);
  7737. c = My->CurrentVersion->OptionLparenChar;
  7738. if (!bwb_isgraph (c))
  7739. {
  7740. c = ' ';
  7741. };
  7742. fprintf (file, "OPTION PUNCT LPAREN \"%c\"\n", c);
  7743. c = My->CurrentVersion->OptionRparenChar;
  7744. if (!bwb_isgraph (c))
  7745. {
  7746. c = ' ';
  7747. };
  7748. fprintf (file, "OPTION PUNCT RPAREN \"%c\"\n", c);
  7749. c = My->CurrentVersion->OptionFilenumChar;
  7750. if (!bwb_isgraph (c))
  7751. {
  7752. c = ' ';
  7753. };
  7754. fprintf (file, "OPTION PUNCT FILENUM \"%c\"\n", c);
  7755. c = My->CurrentVersion->OptionAtChar;
  7756. if (!bwb_isgraph (c))
  7757. {
  7758. c = ' ';
  7759. };
  7760. fprintf (file, "OPTION PUNCT AT \"%c\"\n", c);
  7761. c = My->CurrentVersion->OptionUsingDigit;
  7762. if (!bwb_isgraph (c))
  7763. {
  7764. c = ' ';
  7765. };
  7766. fprintf (file, "OPTION USING DIGIT \"%c\"\n", c);
  7767. c = My->CurrentVersion->OptionUsingComma;
  7768. if (!bwb_isgraph (c))
  7769. {
  7770. c = ' ';
  7771. };
  7772. fprintf (file, "OPTION USING COMMA \"%c\"\n", c);
  7773. c = My->CurrentVersion->OptionUsingPeriod;
  7774. if (!bwb_isgraph (c))
  7775. {
  7776. c = ' ';
  7777. };
  7778. fprintf (file, "OPTION USING PERIOD \"%c\"\n", c);
  7779. c = My->CurrentVersion->OptionUsingPlus;
  7780. if (!bwb_isgraph (c))
  7781. {
  7782. c = ' ';
  7783. };
  7784. fprintf (file, "OPTION USING PLUS \"%c\"\n", c);
  7785. c = My->CurrentVersion->OptionUsingMinus;
  7786. if (!bwb_isgraph (c))
  7787. {
  7788. c = ' ';
  7789. };
  7790. fprintf (file, "OPTION USING MINUS \"%c\"\n", c);
  7791. c = My->CurrentVersion->OptionUsingExrad;
  7792. if (!bwb_isgraph (c))
  7793. {
  7794. c = ' ';
  7795. };
  7796. fprintf (file, "OPTION USING EXRAD \"%c\"\n", c);
  7797. c = My->CurrentVersion->OptionUsingDollar;
  7798. if (!bwb_isgraph (c))
  7799. {
  7800. c = ' ';
  7801. };
  7802. fprintf (file, "OPTION USING DOLLAR \"%c\"\n", c);
  7803. c = My->CurrentVersion->OptionUsingFiller;
  7804. if (!bwb_isgraph (c))
  7805. {
  7806. c = ' ';
  7807. };
  7808. fprintf (file, "OPTION USING FILLER \"%c\"\n", c);
  7809. c = My->CurrentVersion->OptionUsingLiteral;
  7810. if (!bwb_isgraph (c))
  7811. {
  7812. c = ' ';
  7813. };
  7814. fprintf (file, "OPTION USING LITERAL \"%c\"\n", c);
  7815. c = My->CurrentVersion->OptionUsingFirst;
  7816. if (!bwb_isgraph (c))
  7817. {
  7818. c = ' ';
  7819. };
  7820. fprintf (file, "OPTION USING FIRST \"%c\"\n", c);
  7821. c = My->CurrentVersion->OptionUsingAll;
  7822. if (!bwb_isgraph (c))
  7823. {
  7824. c = ' ';
  7825. };
  7826. fprintf (file, "OPTION USING ALL \"%c\"\n", c);
  7827. c = My->CurrentVersion->OptionUsingLength;
  7828. if (!bwb_isgraph (c))
  7829. {
  7830. c = ' ';
  7831. };
  7832. fprintf (file, "OPTION USING LENGTH \"%c\"\n", c);
  7833. fprintf (file, "\n");
  7834. fprintf (file, "\n");
  7835. fflush (file);
  7836. }
  7837. LineType *
  7838. bwb_MAINTAINER_MANUAL (LineType * l)
  7839. {
  7840. assert (l != NULL);
  7841. DumpHeader (My->SYSPRN->cfp);
  7842. DumpAllCommandSyntax (My->SYSPRN->cfp, FALSE,
  7843. My->CurrentVersion->OptionVersionValue);
  7844. DumpAllFunctionSyntax (My->SYSPRN->cfp, FALSE,
  7845. My->CurrentVersion->OptionVersionValue);
  7846. DumpAllOperatorSyntax (My->SYSPRN->cfp, FALSE,
  7847. My->CurrentVersion->OptionVersionValue);
  7848. return (l);
  7849. }
  7850. LineType *
  7851. bwb_MAINTAINER_STACK (LineType * l)
  7852. {
  7853. /*
  7854. dump the current execution stack,
  7855. Leftmost is the top,
  7856. Rigthmost is the bottom.
  7857. */
  7858. StackType *StackItem;
  7859. assert (l != NULL);
  7860. for (StackItem = My->StackHead; StackItem != NULL;
  7861. StackItem = StackItem->next)
  7862. {
  7863. LineType *l;
  7864. l = StackItem->line;
  7865. if (l != NULL)
  7866. {
  7867. fprintf (My->SYSOUT->cfp, "%d:", l->number);
  7868. }
  7869. }
  7870. fprintf (My->SYSOUT->cfp, "\n");
  7871. ResetConsoleColumn ();
  7872. return (l);
  7873. }
  7874. /***************************************************************
  7875. FUNCTION: IntrinsicFunction_init()
  7876. DESCRIPTION: This command initializes the function
  7877. linked list, placing all predefined functions
  7878. in the list.
  7879. ***************************************************************/
  7880. int
  7881. IntrinsicFunction_init (void)
  7882. {
  7883. int n;
  7884. for (n = 0; n < NUM_FUNCTIONS; n++)
  7885. {
  7886. IntrinsicFunctionDefinitionCheck (&(IntrinsicFunctionTable[n]));
  7887. }
  7888. return TRUE;
  7889. }
  7890. VariableType *
  7891. IntrinsicFunction_deffn (int argc, VariableType * argv, UserFunctionType * f)
  7892. {
  7893. /*
  7894. The generic handler for user defined functions.
  7895. When called by exp_function(), f->id will be set to the line number of a specific DEF USR.
  7896. */
  7897. VariableType *v;
  7898. VariableType *argn;
  7899. int i;
  7900. LineType *call_line;
  7901. StackType *save_elevel;
  7902. assert (argc >= 0);
  7903. assert (argv != NULL);
  7904. assert (f != NULL);
  7905. assert(My != NULL);
  7906. /* initialize the variable if necessary */
  7907. /* these errors should not occur */
  7908. if (f == NULL)
  7909. {
  7910. WARN_INTERNAL_ERROR;
  7911. return NULL;
  7912. }
  7913. if (f->line == NULL)
  7914. {
  7915. WARN_INTERNAL_ERROR;
  7916. return NULL;
  7917. }
  7918. if (argv == NULL)
  7919. {
  7920. WARN_INTERNAL_ERROR;
  7921. return NULL;
  7922. }
  7923. if (f->ParameterCount == 0xFF)
  7924. {
  7925. /* VARIANT */
  7926. }
  7927. else if (argc != f->ParameterCount)
  7928. {
  7929. WARN_INTERNAL_ERROR;
  7930. return NULL;
  7931. }
  7932. if (f->ParameterCount == 0xFF)
  7933. {
  7934. /* VARIANT */
  7935. f->local_variable = argv;
  7936. }
  7937. else if (argc > 0)
  7938. {
  7939. v = f->local_variable;
  7940. argn = argv;
  7941. for (i = 0; i < argc; i++)
  7942. {
  7943. argn = argn->next;
  7944. if (v == NULL)
  7945. {
  7946. WARN_INTERNAL_ERROR;
  7947. return NULL;
  7948. }
  7949. if (argn == NULL)
  7950. {
  7951. WARN_INTERNAL_ERROR;
  7952. return NULL;
  7953. }
  7954. if (VAR_IS_STRING (v) != VAR_IS_STRING (argn))
  7955. {
  7956. WARN_INTERNAL_ERROR;
  7957. return NULL;
  7958. }
  7959. if (is_empty_string (v->name) == FALSE)
  7960. {
  7961. int IsError;
  7962. IsError = 0;
  7963. switch (v->VariableTypeCode)
  7964. {
  7965. case ByteTypeCode:
  7966. IsError = NumberValueCheck (P1BYT, PARAM_NUMBER);
  7967. break;
  7968. case IntegerTypeCode:
  7969. IsError = NumberValueCheck (P1INT, PARAM_NUMBER);
  7970. break;
  7971. case LongTypeCode:
  7972. IsError = NumberValueCheck (P1LNG, PARAM_NUMBER);
  7973. break;
  7974. case CurrencyTypeCode:
  7975. IsError = NumberValueCheck (P1CUR, PARAM_NUMBER);
  7976. break;
  7977. case SingleTypeCode:
  7978. IsError = NumberValueCheck (P1FLT, PARAM_NUMBER);
  7979. break;
  7980. case DoubleTypeCode:
  7981. IsError = NumberValueCheck (P1DBL, PARAM_NUMBER);
  7982. break;
  7983. case StringTypeCode:
  7984. IsError = StringLengthCheck (P1ANY, PARAM_LENGTH);
  7985. break;
  7986. default:
  7987. WARN_TYPE_MISMATCH;
  7988. return NULL;
  7989. }
  7990. if (IsError != 0)
  7991. {
  7992. WARN_ILLEGAL_FUNCTION_CALL;
  7993. return argv;
  7994. }
  7995. }
  7996. v = v->next;
  7997. }
  7998. }
  7999. /* OK */
  8000. call_line = f->line; /* line to call for function */
  8001. call_line->position = f->startpos;
  8002. if (call_line->cmdnum == C_DEF)
  8003. {
  8004. if (line_skip_EqualChar (call_line) == FALSE)
  8005. {
  8006. WARN_INTERNAL_ERROR;
  8007. return NULL;
  8008. }
  8009. }
  8010. /* PUSH STACK */
  8011. save_elevel = My->StackHead;
  8012. if (bwb_incexec ())
  8013. {
  8014. /* OK */
  8015. My->StackHead->line = call_line;
  8016. My->StackHead->ExecCode = EXEC_FUNCTION;
  8017. }
  8018. else
  8019. {
  8020. /* ERROR */
  8021. WARN_OUT_OF_MEMORY;
  8022. return NULL;
  8023. }
  8024. /* create variable chain */
  8025. if (f->ParameterCount == 0xFF)
  8026. {
  8027. /* VARIANT */
  8028. }
  8029. else if (argc > 0)
  8030. {
  8031. VariableType *source = NULL; /* source variable */
  8032. source = f->local_variable;
  8033. argn = argv;
  8034. for (i = 0; i < argc; i++)
  8035. {
  8036. argn = argn->next;
  8037. /* copy the name */
  8038. bwb_strcpy (argn->name, source->name);
  8039. if (VAR_IS_STRING (source))
  8040. {
  8041. }
  8042. else
  8043. {
  8044. int IsError;
  8045. double Value;
  8046. VariantType variant;
  8047. CLEAR_VARIANT (&variant);
  8048. if (var_get (argn, &variant) == FALSE)
  8049. {
  8050. WARN_VARIABLE_NOT_DECLARED;
  8051. return NULL;
  8052. }
  8053. if (variant.VariantTypeCode == StringTypeCode)
  8054. {
  8055. WARN_TYPE_MISMATCH;
  8056. return NULL;
  8057. }
  8058. Value = variant.Number;
  8059. IsError = 0;
  8060. switch (source->VariableTypeCode)
  8061. {
  8062. case ByteTypeCode:
  8063. IsError = NumberValueCheck (P1BYT, Value);
  8064. Value = bwb_rint (Value);
  8065. break;
  8066. case IntegerTypeCode:
  8067. IsError = NumberValueCheck (P1INT, Value);
  8068. Value = bwb_rint (Value);
  8069. break;
  8070. case LongTypeCode:
  8071. IsError = NumberValueCheck (P1LNG, Value);
  8072. Value = bwb_rint (Value);
  8073. break;
  8074. case CurrencyTypeCode:
  8075. IsError = NumberValueCheck (P1CUR, Value);
  8076. Value = bwb_rint (Value);
  8077. break;
  8078. case SingleTypeCode:
  8079. IsError = NumberValueCheck (P1FLT, Value);
  8080. break;
  8081. case DoubleTypeCode:
  8082. IsError = NumberValueCheck (P1DBL, Value);
  8083. break;
  8084. case StringTypeCode:
  8085. WARN_TYPE_MISMATCH;
  8086. return NULL;
  8087. /* break; */
  8088. default:
  8089. WARN_TYPE_MISMATCH;
  8090. return NULL;
  8091. }
  8092. if (IsError != 0)
  8093. {
  8094. WARN_ILLEGAL_FUNCTION_CALL;
  8095. return argv;
  8096. }
  8097. variant.Number = Value;
  8098. if (var_set (argn, &variant) == FALSE)
  8099. {
  8100. WARN_VARIABLE_NOT_DECLARED;
  8101. return NULL;
  8102. }
  8103. }
  8104. source = source->next;
  8105. }
  8106. }
  8107. if (call_line->cmdnum == C_DEF)
  8108. {
  8109. VariantType x;
  8110. VariantType *X;
  8111. X = &x;
  8112. CLEAR_VARIANT (X);
  8113. /* the function return variable is hidden */
  8114. My->StackHead->local_variable = argv->next;
  8115. /* var_islocal() uses the LoopTopLine to find local variables */
  8116. My->StackHead->LoopTopLine = call_line; /* FUNCTION, SUB */
  8117. /* evaluate the expression */
  8118. if (line_read_expression (call_line, X) == FALSE) /* IntrinsicFunction_deffn */
  8119. {
  8120. WARN_SYNTAX_ERROR;
  8121. goto EXIT;
  8122. }
  8123. /* save the value */
  8124. switch (X->VariantTypeCode)
  8125. {
  8126. case ByteTypeCode:
  8127. case IntegerTypeCode:
  8128. case LongTypeCode:
  8129. case CurrencyTypeCode:
  8130. case SingleTypeCode:
  8131. case DoubleTypeCode:
  8132. if (argv->VariableTypeCode == StringTypeCode)
  8133. {
  8134. WARN_TYPE_MISMATCH;
  8135. goto EXIT;
  8136. }
  8137. /* OK */
  8138. {
  8139. int IsError;
  8140. double Value;
  8141. IsError = 0;
  8142. Value = X->Number;
  8143. /* VerifyNumeric */
  8144. if (isnan (Value))
  8145. {
  8146. /*** FATAL - INTERNAL ERROR - SHOULD NEVER HAPPEN ***/
  8147. WARN_INTERNAL_ERROR;
  8148. return FALSE;
  8149. }
  8150. if (isinf (Value))
  8151. {
  8152. /* - Evaluation of an expression results in an overflow
  8153. * (nonfatal, the recommended recovery procedure is to supply
  8154. * machine in- finity with the algebraically correct sign and
  8155. * continue). */
  8156. if (Value < 0)
  8157. {
  8158. Value = MINDBL;
  8159. }
  8160. else
  8161. {
  8162. Value = MAXDBL;
  8163. }
  8164. if (WARN_OVERFLOW)
  8165. {
  8166. /* ERROR */
  8167. goto EXIT;
  8168. }
  8169. /* CONTINUE */
  8170. }
  8171. /* OK */
  8172. switch (argv->VariableTypeCode)
  8173. {
  8174. case ByteTypeCode:
  8175. IsError = NumberValueCheck (P1BYT, Value);
  8176. Value = bwb_rint (Value);
  8177. break;
  8178. case IntegerTypeCode:
  8179. IsError = NumberValueCheck (P1INT, Value);
  8180. Value = bwb_rint (Value);
  8181. break;
  8182. case LongTypeCode:
  8183. IsError = NumberValueCheck (P1LNG, Value);
  8184. Value = bwb_rint (Value);
  8185. break;
  8186. case CurrencyTypeCode:
  8187. IsError = NumberValueCheck (P1CUR, Value);
  8188. Value = bwb_rint (Value);
  8189. break;
  8190. case SingleTypeCode:
  8191. IsError = NumberValueCheck (P1FLT, Value);
  8192. break;
  8193. case DoubleTypeCode:
  8194. IsError = NumberValueCheck (P1DBL, Value);
  8195. break;
  8196. default:
  8197. WARN_TYPE_MISMATCH;
  8198. goto EXIT;
  8199. /* break; */
  8200. }
  8201. if (IsError != 0)
  8202. {
  8203. if (WARN_OVERFLOW)
  8204. {
  8205. /* ERROR */
  8206. goto EXIT;
  8207. }
  8208. /* CONTINUE */
  8209. }
  8210. /* assign Value */
  8211. RESULT_NUMBER = Value;
  8212. }
  8213. break;
  8214. case StringTypeCode:
  8215. if (argv->VariableTypeCode != StringTypeCode)
  8216. {
  8217. WARN_TYPE_MISMATCH;
  8218. goto EXIT;
  8219. }
  8220. /* OK */
  8221. if (RESULT_BUFFER != My->MaxLenBuffer)
  8222. {
  8223. WARN_INTERNAL_ERROR;
  8224. goto EXIT;
  8225. }
  8226. if (X->Length > MAXLEN)
  8227. {
  8228. WARN_STRING_TOO_LONG; /* IntrinsicFunction_deffn */
  8229. X->Length = MAXLEN;
  8230. }
  8231. bwb_memcpy (RESULT_BUFFER, X->Buffer, X->Length);
  8232. RESULT_LENGTH = X->Length;
  8233. break;
  8234. default:
  8235. WARN_TYPE_MISMATCH;
  8236. goto EXIT;
  8237. /* break; */
  8238. }
  8239. EXIT:
  8240. RELEASE_VARIANT (X);
  8241. /* break variable chain */
  8242. My->StackHead->local_variable = NULL;
  8243. /* POP STACK */
  8244. bwb_decexec ();
  8245. }
  8246. else
  8247. {
  8248. /* the function return variable is visible */
  8249. My->StackHead->local_variable = argv;
  8250. /* var_islocal() uses the LoopTopLine to find local variables */
  8251. My->StackHead->LoopTopLine = call_line; /* FUNCTION, SUB */
  8252. /* execute until function returns */
  8253. while (My->StackHead != save_elevel)
  8254. {
  8255. bwb_execline ();
  8256. }
  8257. }
  8258. if (f->ParameterCount == 0xFF)
  8259. {
  8260. /* VARIANT */
  8261. f->local_variable = NULL;
  8262. }
  8263. if (is_empty_string (argv->name) == FALSE)
  8264. {
  8265. int IsError;
  8266. IsError = 0;
  8267. switch (argv->VariableTypeCode)
  8268. {
  8269. case ByteTypeCode:
  8270. IsError = NumberValueCheck (P1BYT, RESULT_NUMBER);
  8271. break;
  8272. case IntegerTypeCode:
  8273. IsError = NumberValueCheck (P1INT, RESULT_NUMBER);
  8274. break;
  8275. case LongTypeCode:
  8276. IsError = NumberValueCheck (P1LNG, RESULT_NUMBER);
  8277. break;
  8278. case CurrencyTypeCode:
  8279. IsError = NumberValueCheck (P1CUR, RESULT_NUMBER);
  8280. break;
  8281. case SingleTypeCode:
  8282. IsError = NumberValueCheck (P1FLT, RESULT_NUMBER);
  8283. break;
  8284. case DoubleTypeCode:
  8285. IsError = NumberValueCheck (P1DBL, RESULT_NUMBER);
  8286. break;
  8287. case StringTypeCode:
  8288. IsError = StringLengthCheck (P1ANY, RESULT_LENGTH);
  8289. break;
  8290. default:
  8291. /* no check */
  8292. break;
  8293. }
  8294. if (IsError != 0)
  8295. {
  8296. if (WARN_OVERFLOW)
  8297. {
  8298. /* ERROR */
  8299. }
  8300. /* CONTINUE */
  8301. }
  8302. }
  8303. return argv;
  8304. }
  8305. /***************************************************************
  8306. FUNCTION: IntrinsicFunction_find()
  8307. DESCRIPTION: This C function attempts to locate
  8308. a BASIC function with the specified name.
  8309. If successful, it returns a pointer to
  8310. the C structure for the BASIC function,
  8311. if not successful, it returns NULL.
  8312. ***************************************************************/
  8313. extern int
  8314. IntrinsicFunction_name (char *name)
  8315. {
  8316. /* search INTRINSIC functions */
  8317. IntrinsicFunctionType *f;
  8318. int i;
  8319. assert (name != NULL);
  8320. assert(My != NULL);
  8321. assert(My->CurrentVersion != NULL);
  8322. #if THE_PRICE_IS_RIGHT
  8323. /* start with the closest function, without going over */
  8324. i = VarTypeIndex (name[0]);
  8325. if (i < 0)
  8326. {
  8327. /* non-alpha */
  8328. return FALSE;
  8329. }
  8330. i = My->IntrinsicFunctionStart[i]; /* first function starting with this letter */
  8331. if (i < 0)
  8332. {
  8333. /* NOT FOUND */
  8334. return FALSE;
  8335. }
  8336. #else /* THE_PRICE_IS_RIGHT */
  8337. i = 0;
  8338. #endif /* THE_PRICE_IS_RIGHT */
  8339. for (; i < NUM_FUNCTIONS; i++)
  8340. {
  8341. f = &IntrinsicFunctionTable[i];
  8342. if (My->CurrentVersion->OptionVersionValue & f->OptionVersionBitmask)
  8343. {
  8344. int result;
  8345. result = bwb_stricmp (f->Name, name);
  8346. if (result == 0)
  8347. {
  8348. /* FOUND */
  8349. return TRUE;
  8350. }
  8351. if (result > 0 /* found > searched */ )
  8352. {
  8353. /* NOT FOUND */
  8354. return FALSE;
  8355. }
  8356. }
  8357. }
  8358. /* NOT FOUND */
  8359. return FALSE;
  8360. }
  8361. IntrinsicFunctionType *
  8362. IntrinsicFunction_find_exact (char *name, int ParameterCount,
  8363. ParamBitsType ParameterTypes)
  8364. {
  8365. IntrinsicFunctionType *f;
  8366. int i;
  8367. assert (name != NULL);
  8368. assert(My != NULL);
  8369. assert(My->CurrentVersion != NULL);
  8370. /* search INTRINSIC functions */
  8371. #if THE_PRICE_IS_RIGHT
  8372. /* start with the closest function, without going over */
  8373. i = VarTypeIndex (name[0]);
  8374. if (i < 0)
  8375. {
  8376. /* non-alpha */
  8377. return NULL;
  8378. }
  8379. i = My->IntrinsicFunctionStart[i]; /* first function starting with this letter */
  8380. if (i < 0)
  8381. {
  8382. /* NOT FOUND */
  8383. return NULL;
  8384. }
  8385. #else /* THE_PRICE_IS_RIGHT */
  8386. i = 0;
  8387. #endif /* THE_PRICE_IS_RIGHT */
  8388. for (; i < NUM_FUNCTIONS; i++)
  8389. {
  8390. f = &IntrinsicFunctionTable[i];
  8391. if (My->CurrentVersion->OptionVersionValue & f->OptionVersionBitmask)
  8392. {
  8393. if (f->ParameterCount == ParameterCount)
  8394. {
  8395. if (f->ParameterTypes == ParameterTypes)
  8396. {
  8397. int result;
  8398. result = bwb_stricmp (f->Name, name);
  8399. if (result == 0)
  8400. {
  8401. /* FOUND */
  8402. return f;
  8403. }
  8404. if (result > 0 /* found > searched */ )
  8405. {
  8406. /* NOT FOUND */
  8407. return NULL;
  8408. }
  8409. }
  8410. }
  8411. }
  8412. }
  8413. /* NOT FOUND */
  8414. return NULL;
  8415. }
  8416. static VariableType *
  8417. find_variable_by_type (char *name, int dimensions, char VariableTypeCode)
  8418. {
  8419. VariableType *v = NULL;
  8420. assert (name != NULL);
  8421. v = var_find (name, dimensions, FALSE);
  8422. if (v)
  8423. {
  8424. if (VAR_IS_STRING (v))
  8425. {
  8426. if (VariableTypeCode == StringTypeCode)
  8427. {
  8428. /* found */
  8429. return v;
  8430. }
  8431. }
  8432. else
  8433. {
  8434. if (VariableTypeCode != StringTypeCode)
  8435. {
  8436. /* found */
  8437. return v;
  8438. }
  8439. }
  8440. }
  8441. /* not found */
  8442. return NULL;
  8443. }
  8444. /*
  8445. --------------------------------------------------------------------------------------------
  8446. CHANGE
  8447. --------------------------------------------------------------------------------------------
  8448. */
  8449. LineType *
  8450. bwb_CHANGE (LineType * l)
  8451. {
  8452. /* SYNTAX: CHANGE A$ TO X */
  8453. /* SYNTAX: CHANGE X TO A$ */
  8454. char varname[NameLengthMax + 1];
  8455. VariableType *v;
  8456. VariableType *A;
  8457. VariableType *X;
  8458. int IsStringToArray;
  8459. assert (l != NULL);
  8460. v = NULL;
  8461. A = NULL;
  8462. X = NULL;
  8463. IsStringToArray = FALSE;
  8464. /* get 1st variable */
  8465. if (line_read_varname (l, varname) == FALSE)
  8466. {
  8467. WARN_SYNTAX_ERROR;
  8468. return (l);
  8469. }
  8470. v = find_variable_by_type (varname, 0, StringTypeCode);
  8471. if (v)
  8472. {
  8473. /* STRING to ARRAY */
  8474. A = v;
  8475. IsStringToArray = TRUE;
  8476. }
  8477. else
  8478. {
  8479. /* ARRAY to STRING */
  8480. v = find_variable_by_type (varname, 1, DoubleTypeCode);
  8481. if (v)
  8482. {
  8483. X = v;
  8484. IsStringToArray = FALSE;
  8485. }
  8486. }
  8487. if (v == NULL)
  8488. {
  8489. WARN_VARIABLE_NOT_DECLARED;
  8490. return (l);
  8491. }
  8492. /* get "TO" */
  8493. if (line_skip_word (l, "TO") == FALSE)
  8494. {
  8495. WARN_SYNTAX_ERROR;
  8496. return (l);
  8497. }
  8498. /* get 2nd variable */
  8499. if (line_read_varname (l, varname) == FALSE)
  8500. {
  8501. WARN_SYNTAX_ERROR;
  8502. return (l);
  8503. }
  8504. if (IsStringToArray)
  8505. {
  8506. /* STRING to ARRAY */
  8507. v = find_variable_by_type (varname, 1, DoubleTypeCode);
  8508. if (v == NULL)
  8509. {
  8510. v = var_find (varname, 1, TRUE);
  8511. }
  8512. if (v)
  8513. {
  8514. X = v;
  8515. }
  8516. }
  8517. else
  8518. {
  8519. /* ARRAY to STRING */
  8520. v = find_variable_by_type (varname, 0, StringTypeCode);
  8521. if (v == NULL)
  8522. {
  8523. v = var_find (varname, 0, TRUE);
  8524. }
  8525. if (v)
  8526. {
  8527. A = v;
  8528. }
  8529. }
  8530. if (v == NULL)
  8531. {
  8532. WARN_VARIABLE_NOT_DECLARED;
  8533. return (l);
  8534. }
  8535. assert(A != NULL);
  8536. assert(X != NULL);
  8537. if (IsStringToArray)
  8538. {
  8539. /* CHANGE A$ TO X */
  8540. int i;
  8541. int n;
  8542. char *a;
  8543. DoubleType *x;
  8544. unsigned long t;
  8545. if (A->Value.String == NULL)
  8546. {
  8547. WARN_INTERNAL_ERROR;
  8548. return (l);
  8549. }
  8550. if (A->Value.String->sbuffer == NULL)
  8551. {
  8552. WARN_INTERNAL_ERROR;
  8553. return (l);
  8554. }
  8555. /* variable storage is a mess, we bypass that tradition here. */
  8556. t = 1;
  8557. for (n = 0; n < X->dimensions; n++)
  8558. {
  8559. t *= X->UBOUND[n] - X->LBOUND[n] + 1;
  8560. }
  8561. if (t <= A->Value.String->length)
  8562. {
  8563. WARN_SUBSCRIPT_OUT_OF_RANGE;
  8564. return (l);
  8565. }
  8566. n = A->Value.String->length;
  8567. a = A->Value.String->sbuffer;
  8568. x = X->Value.Number;
  8569. *x = n;
  8570. x++;
  8571. for (i = 0; i < n; i++)
  8572. {
  8573. char C;
  8574. DoubleType V;
  8575. C = *a;
  8576. V = C;
  8577. *x = V;
  8578. x++;
  8579. a++;
  8580. }
  8581. }
  8582. else
  8583. {
  8584. /* CHANGE X TO A$ */
  8585. int i;
  8586. int n;
  8587. char *a;
  8588. DoubleType *x;
  8589. unsigned long t;
  8590. /* variable storage is a mess, we bypass that tradition here. */
  8591. t = 1;
  8592. for (n = 0; n < X->dimensions; n++)
  8593. {
  8594. t *= X->UBOUND[n] - X->LBOUND[n] + 1;
  8595. }
  8596. if (t <= 1)
  8597. {
  8598. WARN_SUBSCRIPT_OUT_OF_RANGE;
  8599. return (l);
  8600. }
  8601. if (t > MAXLEN)
  8602. {
  8603. WARN_STRING_TOO_LONG; /* bwb_CHANGE */
  8604. t = MAXLEN;
  8605. }
  8606. if (A->Value.String == NULL)
  8607. {
  8608. if ((A->Value.String =
  8609. (StringType *) calloc (1, sizeof (StringType))) == NULL)
  8610. {
  8611. WARN_OUT_OF_MEMORY;
  8612. return (l);
  8613. }
  8614. A->Value.String->sbuffer = NULL;
  8615. A->Value.String->length = 0;
  8616. }
  8617. if (A->Value.String->sbuffer != NULL)
  8618. {
  8619. free (A->Value.String->sbuffer);
  8620. A->Value.String->sbuffer = NULL;
  8621. A->Value.String->length = 0;
  8622. }
  8623. if (A->Value.String->sbuffer == NULL)
  8624. {
  8625. A->Value.String->length = 0;
  8626. if ((A->Value.String->sbuffer =
  8627. (char *) calloc (t + 1 /* NulChar */ , sizeof (char))) == NULL)
  8628. {
  8629. WARN_OUT_OF_MEMORY;
  8630. return (l);
  8631. }
  8632. }
  8633. a = A->Value.String->sbuffer;
  8634. x = X->Value.Number;
  8635. n = (int) bwb_rint (*x);
  8636. if (n > MAXLEN)
  8637. {
  8638. WARN_STRING_TOO_LONG; /* bwb_CHANGE */
  8639. n = MAXLEN;
  8640. }
  8641. A->Value.String->length = n;
  8642. x++;
  8643. for (i = 0; i < n; i++)
  8644. {
  8645. char C;
  8646. DoubleType V;
  8647. V = *x;
  8648. C = V;
  8649. *a = C;
  8650. x++;
  8651. a++;
  8652. }
  8653. }
  8654. return (l);
  8655. }
  8656. /*
  8657. --------------------------------------------------------------------------------------------
  8658. CONSOLE
  8659. --------------------------------------------------------------------------------------------
  8660. */
  8661. LineType *
  8662. bwb_CONSOLE (LineType * l)
  8663. {
  8664. /* SYNTAX: CONSOLE */
  8665. /* SYNTAX: CONSOLE WIDTH width */
  8666. assert (l != NULL);
  8667. assert(My != NULL);
  8668. assert(My->SYSPRN != NULL);
  8669. assert(My->SYSPRN->cfp != NULL);
  8670. assert(My->SYSOUT != NULL);
  8671. assert(My->SYSOUT->cfp != NULL);
  8672. if (My->IsPrinter == TRUE)
  8673. {
  8674. /* reset printer column */
  8675. if (My->SYSPRN->col != 1)
  8676. {
  8677. fputc ('\n', My->SYSPRN->cfp);
  8678. My->SYSPRN->col = 1;
  8679. }
  8680. My->IsPrinter = FALSE;
  8681. }
  8682. if (line_skip_word (l, "WIDTH"))
  8683. {
  8684. int width;
  8685. width = 0;
  8686. if (line_read_integer_expression (l, &width) == FALSE)
  8687. {
  8688. WARN_ILLEGAL_FUNCTION_CALL;
  8689. return (l);
  8690. }
  8691. if (width < 0)
  8692. {
  8693. WARN_ILLEGAL_FUNCTION_CALL;
  8694. return (l);
  8695. }
  8696. My->SYSOUT->width = width;
  8697. }
  8698. return (l);
  8699. }
  8700. /*
  8701. --------------------------------------------------------------------------------------------
  8702. LPRINTER
  8703. --------------------------------------------------------------------------------------------
  8704. */
  8705. LineType *
  8706. bwb_LPRINTER (LineType * l)
  8707. {
  8708. /* SYNTAX: LPRINTER */
  8709. /* SYNTAX: LPRINTER WIDTH width */
  8710. assert (l != NULL);
  8711. assert(My != NULL);
  8712. assert(My->SYSPRN != NULL);
  8713. assert(My->SYSPRN->cfp != NULL);
  8714. assert(My->SYSOUT != NULL);
  8715. assert(My->SYSOUT->cfp != NULL);
  8716. if (My->IsPrinter == FALSE)
  8717. {
  8718. /* reset console column */
  8719. if (My->SYSOUT->col != 1)
  8720. {
  8721. fputc ('\n', My->SYSOUT->cfp);
  8722. My->SYSOUT->col = 1;
  8723. }
  8724. My->IsPrinter = TRUE;
  8725. }
  8726. if (line_skip_word (l, "WIDTH"))
  8727. {
  8728. int width;
  8729. width = 0;
  8730. if (line_read_integer_expression (l, &width) == FALSE)
  8731. {
  8732. WARN_ILLEGAL_FUNCTION_CALL;
  8733. return (l);
  8734. }
  8735. if (width < 0)
  8736. {
  8737. WARN_ILLEGAL_FUNCTION_CALL;
  8738. return (l);
  8739. }
  8740. My->SYSPRN->width = width;
  8741. }
  8742. return (l);
  8743. }
  8744. extern void
  8745. bwb_fclose (FILE * file)
  8746. {
  8747. if (file == NULL)
  8748. {
  8749. /* don't close */
  8750. }
  8751. else if (file == stdin)
  8752. {
  8753. /* don't close */
  8754. }
  8755. else if (file == stdout)
  8756. {
  8757. /* don't close */
  8758. }
  8759. else if (file == stderr)
  8760. {
  8761. /* don't close */
  8762. }
  8763. else
  8764. {
  8765. fclose (file);
  8766. }
  8767. }
  8768. LineType *
  8769. bwb_LPT (LineType * l)
  8770. {
  8771. /* SYNTAX: LPT */
  8772. /* SYNTAX: LPT filename$ */
  8773. FILE *file;
  8774. char *filename;
  8775. assert (l != NULL);
  8776. assert(My != NULL);
  8777. assert(My->SYSOUT != NULL);
  8778. assert(My->SYSOUT->cfp != NULL);
  8779. file = NULL;
  8780. filename = NULL;
  8781. if (line_is_eol (l))
  8782. {
  8783. /* OK */
  8784. file = stderr;
  8785. }
  8786. else if (line_read_string_expression (l, &filename))
  8787. {
  8788. /* OK */
  8789. if (is_empty_string (filename))
  8790. {
  8791. WARN_BAD_FILE_NAME;
  8792. return (l);
  8793. }
  8794. file = fopen (filename, "w");
  8795. free (filename);
  8796. }
  8797. else
  8798. {
  8799. WARN_SYNTAX_ERROR;
  8800. return (l);
  8801. }
  8802. if (file == NULL)
  8803. {
  8804. WARN_BAD_FILE_NAME;
  8805. return (l);
  8806. }
  8807. bwb_fclose (My->SYSOUT->cfp);
  8808. My->SYSOUT->cfp = file;
  8809. return (l);
  8810. }
  8811. LineType *
  8812. bwb_PTP (LineType * l)
  8813. {
  8814. /* SYNTAX: PTP */
  8815. /* SYNTAX: PTP filename$ */
  8816. FILE *file;
  8817. char *filename;
  8818. assert (l != NULL);
  8819. assert(My != NULL);
  8820. assert(My->SYSOUT != NULL);
  8821. assert(My->SYSOUT->cfp != NULL);
  8822. file = NULL;
  8823. filename = NULL;
  8824. if (line_is_eol (l))
  8825. {
  8826. /* OK */
  8827. file = fopen ("PTP", "w");
  8828. }
  8829. else if (line_read_string_expression (l, &filename))
  8830. {
  8831. /* OK */
  8832. if (is_empty_string (filename))
  8833. {
  8834. WARN_BAD_FILE_NAME;
  8835. return (l);
  8836. }
  8837. file = fopen (filename, "w");
  8838. free (filename);
  8839. }
  8840. else
  8841. {
  8842. WARN_SYNTAX_ERROR;
  8843. return (l);
  8844. }
  8845. if (file == NULL)
  8846. {
  8847. WARN_BAD_FILE_NAME;
  8848. return (l);
  8849. }
  8850. bwb_fclose (My->SYSOUT->cfp);
  8851. My->SYSOUT->cfp = file;
  8852. return (l);
  8853. }
  8854. LineType *
  8855. bwb_PTR (LineType * l)
  8856. {
  8857. /* SYNTAX: PTR */
  8858. /* SYNTAX: PTR filename$ */
  8859. FILE *file;
  8860. char *filename;
  8861. assert (l != NULL);
  8862. assert(My != NULL);
  8863. assert(My->SYSIN != NULL);
  8864. assert(My->SYSIN->cfp != NULL);
  8865. file = NULL;
  8866. filename = NULL;
  8867. if (line_is_eol (l))
  8868. {
  8869. /* OK */
  8870. file = fopen ("PTR", "r");
  8871. }
  8872. else if (line_read_string_expression (l, &filename))
  8873. {
  8874. /* OK */
  8875. if (is_empty_string (filename))
  8876. {
  8877. WARN_BAD_FILE_NAME;
  8878. return (l);
  8879. }
  8880. file = fopen (filename, "r");
  8881. free (filename);
  8882. }
  8883. else
  8884. {
  8885. WARN_SYNTAX_ERROR;
  8886. return (l);
  8887. }
  8888. if (file == NULL)
  8889. {
  8890. WARN_BAD_FILE_NAME;
  8891. return (l);
  8892. }
  8893. bwb_fclose (My->SYSIN->cfp);
  8894. My->SYSIN->cfp = file;
  8895. return (l);
  8896. }
  8897. LineType *
  8898. bwb_TTY (LineType * l)
  8899. {
  8900. /* SYNTAX: TTY */
  8901. assert (l != NULL);
  8902. bwb_TTY_IN (l);
  8903. bwb_TTY_OUT (l);
  8904. return (l);
  8905. }
  8906. LineType *
  8907. bwb_TTY_IN (LineType * l)
  8908. {
  8909. /* SYNTAX: TTY IN */
  8910. assert (l != NULL);
  8911. assert(My != NULL);
  8912. assert(My->SYSIN != NULL);
  8913. assert(My->SYSIN->cfp != NULL);
  8914. bwb_fclose (My->SYSIN->cfp);
  8915. My->SYSIN->cfp = stdin;
  8916. return (l);
  8917. }
  8918. LineType *
  8919. bwb_TTY_OUT (LineType * l)
  8920. {
  8921. /* SYNTAX: TTY OUT */
  8922. assert (l != NULL);
  8923. assert(My != NULL);
  8924. assert(My->SYSOUT != NULL);
  8925. assert(My->SYSOUT->cfp != NULL);
  8926. bwb_fclose (My->SYSOUT->cfp);
  8927. My->SYSOUT->cfp = stdout;
  8928. return (l);
  8929. }
  8930. /*
  8931. --------------------------------------------------------------------------------------------
  8932. CREATE
  8933. --------------------------------------------------------------------------------------------
  8934. */
  8935. LineType *
  8936. bwb_CREATE (LineType * l)
  8937. {
  8938. /* SYNTAX: CREATE filename$ [ RECL reclen ] AS filenum [ BUFF number ] [ RECS size ] */
  8939. int FileNumber;
  8940. int width;
  8941. int buffnum;
  8942. int recsnum;
  8943. char *filename;
  8944. assert (l != NULL);
  8945. assert(My != NULL);
  8946. FileNumber = 0;
  8947. width = 0;
  8948. buffnum = 0;
  8949. recsnum = 0;
  8950. filename = NULL;
  8951. if (line_read_string_expression (l, &filename) == FALSE)
  8952. {
  8953. WARN_SYNTAX_ERROR;
  8954. return (l);
  8955. }
  8956. if (is_empty_string (filename))
  8957. {
  8958. WARN_BAD_FILE_NAME;
  8959. return (l);
  8960. }
  8961. if (line_skip_word (l, "RECL"))
  8962. {
  8963. if (line_read_integer_expression (l, &width) == FALSE)
  8964. {
  8965. WARN_FIELD_OVERFLOW;
  8966. return (l);
  8967. }
  8968. if (width <= 0)
  8969. {
  8970. WARN_FIELD_OVERFLOW;
  8971. return (l);
  8972. }
  8973. }
  8974. if (line_skip_word (l, "AS") == FALSE)
  8975. {
  8976. WARN_SYNTAX_ERROR;
  8977. return (l);
  8978. }
  8979. if (line_read_integer_expression (l, &FileNumber) == FALSE)
  8980. {
  8981. WARN_BAD_FILE_NUMBER;
  8982. return (l);
  8983. }
  8984. if (FileNumber <= 0)
  8985. {
  8986. WARN_BAD_FILE_NUMBER;
  8987. return (l);
  8988. }
  8989. if (line_skip_word (l, "BUFF"))
  8990. {
  8991. if (line_read_integer_expression (l, &buffnum) == FALSE)
  8992. {
  8993. WARN_FIELD_OVERFLOW;
  8994. return (l);
  8995. }
  8996. if (buffnum <= 0)
  8997. {
  8998. WARN_FIELD_OVERFLOW;
  8999. return (l);
  9000. }
  9001. }
  9002. if (line_skip_word (l, "RECS"))
  9003. {
  9004. if (line_read_integer_expression (l, &recsnum) == FALSE)
  9005. {
  9006. WARN_FIELD_OVERFLOW;
  9007. return (l);
  9008. }
  9009. if (recsnum <= 0)
  9010. {
  9011. WARN_FIELD_OVERFLOW;
  9012. return (l);
  9013. }
  9014. }
  9015. /* now, we are ready to create the file */
  9016. My->CurrentFile = find_file_by_number (FileNumber);
  9017. if (My->CurrentFile == NULL)
  9018. {
  9019. My->CurrentFile = file_new ();
  9020. My->CurrentFile->FileNumber = FileNumber;
  9021. }
  9022. if (My->CurrentFile->FileName != NULL)
  9023. {
  9024. free (My->CurrentFile->FileName);
  9025. My->CurrentFile->FileName = NULL;
  9026. }
  9027. My->CurrentFile->FileName = filename;
  9028. filename = NULL;
  9029. if (My->CurrentFile->DevMode != DEVMODE_CLOSED)
  9030. {
  9031. My->CurrentFile->DevMode = DEVMODE_CLOSED;
  9032. }
  9033. if (My->CurrentFile->cfp != NULL)
  9034. {
  9035. bwb_fclose (My->CurrentFile->cfp);
  9036. My->CurrentFile->cfp = NULL;
  9037. }
  9038. if (My->CurrentFile->buffer != NULL)
  9039. {
  9040. free (My->CurrentFile->buffer);
  9041. My->CurrentFile->buffer = NULL;
  9042. }
  9043. My->CurrentFile->width = 0;
  9044. My->CurrentFile->col = 1;
  9045. My->CurrentFile->row = 1;
  9046. My->CurrentFile->delimit = ',';
  9047. /* truncate to zero length or create text file for update (reading and writing) */
  9048. if (is_empty_string (My->CurrentFile->FileName))
  9049. {
  9050. WARN_BAD_FILE_NAME;
  9051. return (l);
  9052. }
  9053. if ((My->CurrentFile->cfp =
  9054. fopen (My->CurrentFile->FileName, "w+")) == NULL)
  9055. {
  9056. WARN_BAD_FILE_NAME;
  9057. return (l);
  9058. }
  9059. if (width > 0)
  9060. {
  9061. My->CurrentFile->width = width;
  9062. My->CurrentFile->DevMode = DEVMODE_RANDOM;
  9063. }
  9064. else
  9065. {
  9066. My->CurrentFile->DevMode = DEVMODE_INPUT | DEVMODE_OUTPUT;
  9067. }
  9068. return (l);
  9069. }
  9070. /*
  9071. --------------------------------------------------------------------------------------------
  9072. COPY
  9073. --------------------------------------------------------------------------------------------
  9074. */
  9075. static void
  9076. bwb_copy_file (char *Source, char *Target)
  9077. {
  9078. FILE *source;
  9079. FILE *target;
  9080. source = NULL;
  9081. target = NULL;
  9082. if (is_empty_string (Source))
  9083. {
  9084. WARN_BAD_FILE_NAME;
  9085. goto EXIT;
  9086. }
  9087. if (is_empty_string (Target))
  9088. {
  9089. WARN_BAD_FILE_NAME;
  9090. goto EXIT;
  9091. }
  9092. source = fopen (Source, "rb");
  9093. if (source == NULL)
  9094. {
  9095. WARN_BAD_FILE_NAME;
  9096. goto EXIT;
  9097. }
  9098. target = fopen (Target, "wb");
  9099. if (target == NULL)
  9100. {
  9101. WARN_BAD_FILE_NAME;
  9102. goto EXIT;
  9103. }
  9104. /* OK */
  9105. while (TRUE)
  9106. {
  9107. int C;
  9108. C = fgetc (source);
  9109. if (C < 0 /* EOF */ || feof (source) || ferror (source))
  9110. {
  9111. break;
  9112. }
  9113. fputc (C, target);
  9114. if (ferror (target))
  9115. {
  9116. break;
  9117. }
  9118. }
  9119. /* DONE */
  9120. EXIT:
  9121. if (source)
  9122. {
  9123. fclose (source);
  9124. }
  9125. if (target)
  9126. {
  9127. fclose (target);
  9128. }
  9129. }
  9130. LineType *
  9131. bwb_COPY (LineType * Line)
  9132. {
  9133. /* SYNTAX: COPY source$ TO target$ */
  9134. char *Source;
  9135. char *Target;
  9136. assert (Line != NULL);
  9137. Source = NULL;
  9138. Target = NULL;
  9139. if (line_read_string_expression (Line, &Source) == FALSE)
  9140. {
  9141. WARN_SYNTAX_ERROR;
  9142. goto EXIT;
  9143. }
  9144. if (line_skip_word (Line, "TO") == FALSE)
  9145. {
  9146. WARN_SYNTAX_ERROR;
  9147. goto EXIT;
  9148. }
  9149. if (line_read_string_expression (Line, &Target) == FALSE)
  9150. {
  9151. WARN_SYNTAX_ERROR;
  9152. goto EXIT;
  9153. }
  9154. bwb_copy_file (Source, Target);
  9155. EXIT:
  9156. if (Source)
  9157. {
  9158. free (Source);
  9159. }
  9160. if (Target)
  9161. {
  9162. free (Target);
  9163. }
  9164. return (Line);
  9165. }
  9166. /*
  9167. --------------------------------------------------------------------------------------------
  9168. DISPLAY
  9169. --------------------------------------------------------------------------------------------
  9170. */
  9171. static void
  9172. bwb_display_file (char *Source)
  9173. {
  9174. FILE *source;
  9175. assert (My->SYSOUT != NULL);
  9176. assert (My->SYSOUT->cfp != NULL);
  9177. source = NULL;
  9178. if (is_empty_string (Source))
  9179. {
  9180. WARN_BAD_FILE_NAME;
  9181. goto EXIT;
  9182. }
  9183. source = fopen (Source, "rb");
  9184. if (source == NULL)
  9185. {
  9186. WARN_BAD_FILE_NAME;
  9187. goto EXIT;
  9188. }
  9189. /* OK */
  9190. while (TRUE)
  9191. {
  9192. int C;
  9193. C = fgetc (source);
  9194. if (C < 0 /* EOF */ || feof (source) || ferror (source))
  9195. {
  9196. break;
  9197. }
  9198. fputc (C, My->SYSOUT->cfp);
  9199. }
  9200. /* DONE */
  9201. EXIT:
  9202. if (source)
  9203. {
  9204. fclose (source);
  9205. }
  9206. }
  9207. LineType *
  9208. bwb_DISPLAY (LineType * Line)
  9209. {
  9210. /* SYNTAX: DISPLAY source$ */
  9211. char *Source;
  9212. assert (Line != NULL);
  9213. Source = NULL;
  9214. if (line_read_string_expression (Line, &Source) == FALSE)
  9215. {
  9216. WARN_SYNTAX_ERROR;
  9217. goto EXIT;
  9218. }
  9219. bwb_display_file (Source);
  9220. EXIT:
  9221. if (Source)
  9222. {
  9223. free (Source);
  9224. }
  9225. return (Line);
  9226. }
  9227. /*
  9228. --------------------------------------------------------------------------------------------
  9229. EOF
  9230. --------------------------------------------------------------------------------------------
  9231. */
  9232. /* EOF */