home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 2 / goldfish_vol2_cd1.bin / files / misc / sci / gfft / source / gfft.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-21  |  8.3 KB  |  299 lines

  1. /***************************************************************************
  2.  *          Copyright (C) 1994  Charles P. Peterson                  *
  3.  *         4007 Enchanted Sun, San Antonio, Texas 78244-1254             *
  4.  *              Email: Charles_P_Peterson@fcircus.sat.tx.us                *
  5.  *                                                                         *
  6.  *          This is free software with NO WARRANTY.                  *
  7.  *              See below for details.                           *
  8.  *              Support is available for a fee.                      *
  9.  ***************************************************************************
  10.  *
  11.  * Program:     gfft--General FFT analysis
  12.  * File:        gfft.c
  13.  * Purpose:     main() function--get args and/or commands
  14.  * Author:      Charles Peterson (CPP)
  15.  * History:     27-May-1993 CPP; Created.
  16.  *              6-July-1994 CPP (0.74); allow simultaneous MORE sessions
  17.  *              21-July-1994 CPP (1.02); remove "delete failed..." msgs
  18.  */
  19.  
  20. /*
  21.  *  Copyright (C) 1994  Charles P. Peterson
  22.  *  4007 Enchanted Sun, San Antonio, TX 78244-1254
  23.  *  Email: Charles_P_Peterson@fcircus.sat.tx.us
  24.  *
  25.  *  Please refer to the README file included in this distribution
  26.  *  concerning low-cost support and update services available from the
  27.  *  author, as well as encouragement to make suggestions, bug reports,
  28.  *  usage reports, enhancement requests, donations, equipment loans, and
  29.  *  ports to different systems.  If the README files is unavailable, Email
  30.  *  or send a SASE to the author (address above).  A limited amount of free
  31.  *  help may also be available by Email or mail (SASE required).  If
  32.  *  address has changed, try finding the most recent distribution.
  33.  *
  34.  *  This program is free software; you can redistribute it and/or modify
  35.  *  it under the terms of the GNU General Public License as published by
  36.  *  the Free Software Foundation; either version 2 of the License, or
  37.  *  (at your option) any later version.
  38.  *
  39.  *  This program is distributed in the hope that it will be useful,
  40.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  41.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  42.  *  GNU General Public License for more details.
  43.  *
  44.  *  You should have received a copy of the GNU General Public License
  45.  *  along with this program; if not, write to the Free Software
  46.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  The
  47.  *  GNU General Public License should be in a file named 'COPYING'.
  48.  *  Do not contact the Free Software Foundation for any other information
  49.  *  about this program; contact the author instead.
  50.  *
  51.  */
  52.  
  53. #include <stdlib.h>    /* exit(), system() */
  54. #include <stdio.h>     /* printf()   */
  55. #include <dos.h>       /* _CXBRK */
  56. #include <math.h>      /* _CXFERR */
  57. #include <string.h>
  58.  
  59. #define DEFINE_HERE
  60. #include "gfft.h"
  61. #undef DEFINE_HERE
  62. #include "settings.h"
  63.  
  64. extern Name_Info_St Gfft_Command[];  /* GFFT commands */
  65.  
  66. char *StartupPathList[] = STARTUP_PATH_LIST;  /* From Xdef.h where X is sys */
  67.  
  68. static void cleanup (void);
  69. static void delete_temp_files (void);
  70.  
  71. void main (int argc, char *argv[])
  72. {
  73.  
  74.     CATCH_ERROR  /* This is the ultimate safety net */
  75.     {
  76.  
  77.     startup_cli_file ();
  78.  
  79. /**************************************************************************\
  80.  * Workbench Mode                                                         *
  81. \**************************************************************************/
  82.  
  83. #ifdef AMIGA
  84.  
  85.     if (argc < 1)
  86.     {
  87.         CommandMode = WORKBENCH_MODE;
  88.         Plot = ANY_PLOT;
  89.         workbench_main ();  /* Failures gabort() directly */
  90.         quit (NullString);  /* Successful exit */
  91.     }
  92.  
  93. #endif
  94.  
  95. /**************************************************************************\
  96.  * CLI-Batch Mode                                                         *
  97. \**************************************************************************/
  98.  
  99.     if (argc > 1)
  100.     {
  101.         /*
  102.          * This is a non-interactive (i.e. batch) session
  103.          * qabort on error (indicated by longjmp).
  104.          */
  105.         CommandMode = BATCH_MODE;
  106.         Plot = NO_PLOT;
  107.         CATCH_ERROR
  108.         {
  109.         batch_command (argc, argv);
  110.         quit (NullString);
  111.         }
  112.         ON_ERROR
  113.         {
  114.         gabort (EXIT_FAILURE);
  115.         }
  116.         END_CATCH_ERROR;
  117.  
  118.     } /* End of batch session handling */
  119.  
  120. /**************************************************************************\
  121.  * CLI-Interactive Mode (gets called from here)                           *
  122. \**************************************************************************/
  123.  
  124.     Plot = ANY_PLOT;
  125.     banner_message (NullString);
  126.     cli_interactive_loop (TRUE);
  127.  
  128. /* Shouldn't get here; quit() or gabort() are called somewhere to exit */
  129.     }
  130.  
  131. #ifdef _DEBUG
  132.     ON_ERROR
  133.     {
  134.     /*
  135.      * WARNING!  Do not put any code (or calls) here which could possibly
  136.      * raise an error.  If it does, the 'ultimate' safety net will be
  137.      * leaky, and a system crash could result!
  138.      */
  139.     fprintf (stderr,"\nWarning.  Error caught by safety net.\n");
  140.     }
  141. #endif /* ifdef _DEBUG */
  142.  
  143.     END_CATCH_ERROR;  /* End of the ultimate safety net. */
  144.  
  145. /* Shouldn't get here; quit() or gabort() are called somewhere to exit */
  146.  
  147. }
  148.  
  149.  
  150. /**************************************************************************\
  151.  * CLI-Interactive Mode (implemented here)                                *
  152. \**************************************************************************/
  153.  
  154. void cli_interactive_loop (BOOLEAN started_from_cli)
  155. {
  156.     int previous_command_mode = CommandMode;
  157.  
  158.     CommandMode = INTERACTIVE_MODE;
  159.     while (started_from_cli || CommandMode == INTERACTIVE_MODE)
  160.     {
  161.     prompt_command ();  /* Might gabort() from here on EOF */
  162.     CATCH_ERROR
  163.     {
  164.         invoke_method (Command, Gfft_Command);
  165.     }
  166.     END_CATCH_ERROR;  /* We don't break loop on error here */
  167.     }
  168.     CommandMode = previous_command_mode;
  169. }
  170.  
  171.  
  172. void startup_cli_file ()
  173. {
  174.     int previous_command_mode = CommandMode;
  175.     FILE * startup_file_ptr = NULL;
  176.  
  177.     CommandMode = BATCH_MODE;
  178.  
  179.     if ( startup_file_ptr = gopen (StartupPathList, ".gfft", "r"))
  180.     {
  181.     char *next_command = Command;
  182.  
  183.     while (next_command)
  184.     {
  185.         next_command = fgets (Command, COMMAND_BUFFER_SIZE, 
  186.                   startup_file_ptr);
  187.         if (next_command)
  188.         {
  189.         int length = strlen (Command);
  190.         
  191.         if (length == 0) continue;
  192.         if (Command[length-1] == '\n')
  193.         {
  194.             Command[length-1] = '\0';
  195.             if (length == 1) continue;
  196.         }
  197.         if (Command[0] == ';' || Command[0] == '#' ||
  198.             Command[0] == '!')
  199.         {
  200.             continue;
  201.         }
  202.         CATCH_ERROR
  203.         {
  204.             invoke_method (Command, Gfft_Command);
  205.         }
  206.         ON_ERROR
  207.         {
  208.             error_message (STARTUP_FILE_ERROR);
  209.             gabort (EXIT_FAILURE);
  210.         }
  211.         END_CATCH_ERROR;
  212.         }
  213.     }
  214.     fclose (startup_file_ptr);
  215.     }
  216.     CommandMode = previous_command_mode;
  217. }
  218.  
  219.  
  220. /**************************************************************************\
  221.  * Termination Handlers                                                   *
  222. \**************************************************************************/
  223.  
  224. /*
  225.  * The system function exit() should not be called directly except
  226.  * by quit() and gabort(), which follow.
  227.  */
  228.  
  229.  
  230. char *quit (char *arguments)
  231. {
  232.     cleanup ();
  233.     exit (EXIT_SUCCESS);
  234.     return arguments;    /* This is a dummy to make compiler happy */
  235. }
  236.  
  237. void gabort (int status)
  238. {
  239.     cleanup ();
  240.     exit (status);
  241. }
  242.  
  243. static void cleanup (void)
  244. {
  245.     delete_temp_files ();
  246. #ifdef AMIGA
  247.     close_amiga_stuff ();
  248. #endif
  249. }
  250.  
  251. static void delete_temp_files (void)  /* rework when adding other systems */
  252. {
  253.     char buffer[COMMAND_BUFFER_SIZE];
  254.  
  255.     if (Plot && CommandMode == BATCH_MODE)
  256.     {
  257.     error_message (CANT_CLEANUP_BATCH_PLOT);
  258.     return;
  259.     }
  260.  
  261.     sprintf (buffer, 
  262.          "run >nil: delete >nil: %s QUIET\n", DATA_FILE_WILDCARD);
  263.     system (buffer);
  264.  
  265.     sprintf (buffer, 
  266.          "run >nil: delete >nil: %s QUIET\n", MESSAGE_FILE_WILDCARD);
  267.     system (buffer);
  268.  
  269.     sprintf (buffer, 
  270.          "run >nil: delete >nil: %s QUIET\n", COMMAND_FILE_WILDCARD);
  271.     system (buffer);
  272.  
  273.     remove (GNUPLOT_COMMAND_FILE_NAME);
  274. }
  275.  
  276. /*
  277.  * Well, C= says to disable SAS interrupt handler
  278.  * though they don't say why...
  279.  * So, I'm doing it, reluctantly...
  280.  */
  281.  
  282. void __regargs _CXBRK(void) { return; }  /* Disable SAS interrupt handlr */
  283.     
  284. /*
  285.  * This was part of an attempt to intercept the divide by zero error
  286.  * so the damned requester doesn't come up.
  287.  * Unfortunately, FFP library doesn't follow ANSI C,
  288.  * so this didn't work.
  289.  * I'd have to write processor dependent assy code to intercept the error.
  290.  */
  291.  
  292. #if FALSE
  293. void _CXFERR (int code)
  294. {
  295.     _FPERR = code;
  296.     return;
  297. }
  298. #endif
  299.