home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 4 / FreshFish_May-June1994.bin / new / misc / math / cp / source / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-01  |  11.7 KB  |  443 lines

  1. #include "cp.h"
  2. #include "spr.h"
  3.  
  4. UBYTE     *version_string     = "$VER: cP 4.2 (30.4.94)";
  5.  
  6. LONG      __oslibversion      = 37;     /* requires KickStart 37 */
  7. LONG      __BackGroundIO      = 1;
  8. LONG      __stack             = 8192;   /* stack size doesn't seem to work for cli proc??? */
  9. UBYTE     *__procname         = "cPlot"; /* process name */
  10.  
  11. extern BPTR _Backstdout;      /* keep stdout till we get past startup */
  12.  
  13. struct List         *SetList       = NULL;
  14. struct RDArgs       *cli_args      = NULL;
  15. struct RDArgs       *env_args      = NULL;
  16. struct TextFont     *fontPtr       = NULL;
  17. struct TextAttr     CPFont         = { (STRPTR)"topaz.font", 8, 0x00, 0x00 };
  18. struct TextAttr     *cPFont        = NULL;
  19. struct AnchorPath   *anchorpath;
  20.  
  21. AREXXCONTEXT    RexxStuff;
  22.  
  23. /* Set Defaults */
  24.  
  25. BOOL grid    = FALSE;     /* No Grid */
  26. BOOL LOGX    = FALSE;     /* Lin X */
  27. BOOL LOGY    = FALSE;     /* Lin Y */
  28. BOOL sym     = FALSE;     /* No Symbols */
  29. BOOL DEFS    = FALSE;     /* Don't make cP */
  30. BOOL MONO    = FALSE;     /* color default */
  31. BOOL CPANEL  = TRUE;      /* control panel on */
  32. BOOL NOFNAME = FALSE;     /* usually put filename boxes on screen */
  33. BOOL BEHIND  = FALSE;     /* usually open screen to front */
  34. BOOL RealTime = FALSE;
  35.  
  36. LONG Disp = HIRESLACE_KEY;    /* Hires Lace Default */
  37.  
  38. UBYTE *path;        /* extern pointer to Path */
  39. UBYTE Path[255];    /* Path String same as path but not external */
  40.  
  41. UBYTE *fName;        /* extern pointer to fontname */
  42. UBYTE fontName[32]; /* im too lazy to allocate dynamically */
  43.  
  44. UBYTE *MyName;        /* extern pointer to fontname */
  45. UBYTE Name[32];
  46.  
  47. UBYTE *XLAB=NULL;
  48. UBYTE xlabel[32];
  49. UBYTE *YLAB=NULL;
  50. UBYTE ylabel[32];
  51. UBYTE *TITLE=NULL;
  52. UBYTE title[64];
  53.  
  54.  
  55. LONG points    = 1000000; /* should be enough max per set */
  56. LONG thin      = 1;    /* dont depopulate */
  57.  
  58. WORD lastcolor = 15;     /* 16 colors default palette */
  59. WORD depth     = 4;
  60.  
  61. LONG *idkey;             /* pointer to Display ID */
  62.  
  63. UBYTE **files=NULL;           /* pointer to pointer to FILES/M */
  64.  
  65. UBYTE RexxName[32];
  66.  
  67. #define ENVSIZE 1024     /* max char length for envvar */
  68.  
  69. UBYTE *template = "FILES=WILD/M,LX=LOGX/S,LY=LOGY/S,GRID/S,SYM/S,ID=DISPLAYID/N/K,FONT/K,FS=SIZE/N/K,LORES/S,HIRES/S,LACE/S,SETDEF/S,W=WIDTH/N/K,H=HEIGHT/N/K,P=POINTS/N/K,T=THIN/N/K,D=DEPTH/N/K,MONO/S,CPANEL/S,NOFN=NOFNBOXES/S,OB=OPENBEHIND/S,MYNAME/K,XLAB/K,YLAB/K,TITLE/K,REALTIME/S,SD=DIGITS/N/K";
  70.  
  71. LONG cli_array[ 27 ] = { 0L, 0L, 0L, 0L, 0L ,0L ,0L ,0L ,0L ,0L ,0L ,0L ,0L ,0L ,0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L};
  72. LONG env_array[ 27 ] = { 0L, 0L, 0L, 0L, 0L ,0L ,0L ,0L ,0L ,0L ,0L ,0L ,0L ,0L ,0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L};
  73.  
  74. extern void exit(int);   /* from stdlib.h */
  75.  
  76. /* Use the same function to process command line and envvar */
  77.  
  78. void arg_array_Parse( LONG *arg_array )
  79. {
  80. LONG *ptr;
  81.  
  82.      if ( arg_array[ 0 ] ) files = ( UBYTE ** ) arg_array[ 0 ];
  83.  
  84.      if ( arg_array[ 1 ] ) LOGX = TRUE;
  85.      if ( arg_array[ 2 ] ) LOGY = TRUE;
  86.  
  87.      if ( arg_array[ 3 ] )
  88.        {
  89.           if ( grid ) grid = FALSE;
  90.           else        grid = TRUE;
  91.        }
  92.      if ( arg_array[ 4 ] )
  93.        {
  94.           if ( sym ) sym = FALSE;
  95.           else       sym = TRUE;
  96.        }
  97.  
  98.      if ( arg_array[ 5 ] )
  99.        {
  100.           ptr = (LONG *)arg_array[ 5 ];
  101.           Disp = *ptr;
  102.        }
  103.  
  104.      if ( arg_array[ 6 ] )
  105.        {
  106.           strcpy( fName, (UBYTE *)arg_array[ 6 ]);
  107.           strcat( fName, ".font");
  108.           cPFont->ta_Name = (STRPTR)fName;
  109.        }
  110.  
  111.      if ( arg_array[ 7 ] )
  112.        {
  113.           ptr = (LONG *)arg_array[ 7 ];
  114.           cPFont->ta_YSize = (UWORD) *ptr;
  115.        }
  116.  
  117.      if ( arg_array[ 8 ] )  Disp  = LORES_KEY;
  118.      if ( arg_array[ 9 ] )  Disp  = HIRES_KEY;
  119.      if ( arg_array[ 10 ] ) Disp |= LORESLACE_KEY;
  120.      if ( arg_array[ 11 ] ) DEFS  = TRUE;
  121.  
  122.      if ( arg_array[ 12 ] )
  123.        {
  124.           ptr = (LONG *)arg_array[ 12 ];
  125.           SWIDTH = *ptr;
  126.        }
  127.  
  128.      if ( arg_array[ 13 ] )
  129.        {
  130.           ptr = (LONG *)arg_array[ 13 ];
  131.           SHEIGHT = *ptr;
  132.        }
  133.  
  134.      if ( arg_array[ 14 ] )
  135.        {
  136.           ptr = (LONG *)arg_array[ 14 ];
  137.           points = *ptr;
  138.        }
  139.      if ( arg_array[ 15 ] )
  140.        {
  141.           ptr = (LONG *)arg_array[ 15 ];
  142.           thin = *ptr;
  143.           if (thin <= 0) thin = 1;
  144.        }
  145.      if ( arg_array[ 16 ] )
  146.        {
  147.           ptr = (LONG *)arg_array[ 16 ];
  148.           depth = (WORD)*ptr;
  149.  
  150.           if (depth <= 1 )
  151.             {
  152.                depth = 1;
  153.                lastcolor = 1;
  154.                MONO = TRUE;
  155.             }
  156.           if (depth == 2) lastcolor = 3;
  157.           if (depth == 3) lastcolor = 7;
  158.           if (depth >= 4)
  159.             {
  160.                depth = 4;
  161.                lastcolor = 15;
  162.             }
  163.        }
  164.      if ( arg_array[ 17 ] )
  165.        {
  166.           MONO  = TRUE;
  167.           depth = 1;
  168.           lastcolor = 1;
  169.        }
  170.      if ( arg_array[ 18 ] )
  171.        {
  172.           if ( CPANEL ) CPANEL = FALSE;
  173.           else          CPANEL = TRUE;
  174.        }
  175.      if ( arg_array[ 19 ] )
  176.        {
  177.           if ( NOFNAME ) NOFNAME = FALSE;
  178.           else           NOFNAME = TRUE;
  179.        }
  180.      if ( arg_array[ 20 ] )
  181.        {
  182.           if ( BEHIND ) BEHIND = FALSE;
  183.           else          BEHIND = TRUE;
  184.        }
  185.      if ( arg_array[ 21 ] )
  186.        {
  187.           strcpy( Name, (UBYTE *)arg_array[ 21 ]);
  188.        }
  189.      if ( arg_array[ 22 ] )
  190.        {
  191.           strcpy( xlabel, (UBYTE *)arg_array[ 22 ]);
  192.        }
  193.      if ( arg_array[ 23 ] )
  194.        {
  195.           strcpy( ylabel, (UBYTE *)arg_array[ 23 ]);
  196.        }
  197.      if ( arg_array[ 24 ] )
  198.        {
  199.           strcpy( title, (UBYTE *)arg_array[ 24 ]);
  200.        }
  201.      if ( arg_array[ 25 ] )
  202.        {
  203.           if ( RealTime ) RealTime = FALSE;
  204.           else            RealTime = TRUE;
  205.        }
  206.      if ( arg_array[ 26 ] )
  207.        {
  208.           ptr = (LONG *)arg_array[ 26 ];
  209.           precision = *ptr - 1;
  210.        }
  211.  
  212. }
  213.  
  214.  
  215. /* Clean up and die duh */
  216.  
  217. void Death( int v )
  218. {
  219.     if ( delwinWnd )     ClosedelwinWindow();
  220.  
  221.     if ( PlotWindowWnd ) ClosePlotWindowWindow();
  222.  
  223.     if ( Scr )           CloseDownScreen();
  224.  
  225.     if ( fontPtr )       CloseFont( fontPtr );
  226.  
  227.     FreeARexx(RexxStuff);
  228.  
  229.     FreeAllSets();
  230.  
  231.     if ( cli_args )      FreeArgs( cli_args );
  232.     if ( anchorpath )    FreeVec( anchorpath );
  233.  
  234.  
  235.     exit( v );
  236. }
  237.  
  238.  
  239. void main(int argc, char **argv)
  240. {
  241. ULONG sigs;
  242. ULONG rexxsigs;
  243. LONG  mres;
  244. struct List slist;
  245. LONG j = 0;
  246. BPTR lock;
  247. LONG len;
  248. UBYTE fault[300];
  249. UBYTE *environ;
  250. LONG alive;
  251.  
  252.  
  253.  
  254.      slist.lh_Head = (struct Node *) &slist.lh_Tail;
  255.      slist.lh_Tail = 0;
  256.      slist.lh_TailPred = (struct Node *) &slist.lh_Head;
  257.      slist.lh_Type = NT_DATA;
  258.  
  259.      SetList   = &slist;
  260.  
  261.      cPFont    = &CPFont;
  262.      fName     = &fontName[0];
  263.      path      = (STRPTR)Path;
  264.      MyName    = (STRPTR)Name;
  265.     XLAB      = (STRPTR)xlabel;
  266.     YLAB      = (STRPTR)ylabel;
  267.     TITLE     = (STRPTR)title;
  268.  
  269.     strcpy(MyName,&version_string[5]);
  270.  
  271.      if ( argc == 2 && Stricmp(argv[1],"?")==0)  /* give help and exit */
  272.        {
  273.           if ( _Backstdout )
  274.             {
  275.                 FPuts(_Backstdout, template);
  276.                FPutC ( _Backstdout, '\n');
  277.                Close( _Backstdout);
  278.             }
  279.           Death ( 1 );
  280.       }       
  281.  
  282.  
  283.  
  284.      /* Parse envvar CPCONFIG */
  285.  
  286.      if (( environ = AllocVec( ENVSIZE, MEMF_CLEAR )))
  287.        {
  288.           len = GetVar( "CPCONFIG", environ, ENVSIZE, GVF_GLOBAL_ONLY );
  289.           if ( len > 0 )
  290.             {
  291.                environ[len]   = '\n';
  292.                environ[++len] = '\0';
  293.  
  294.                if ((env_args = (struct RDArgs *)AllocDosObject(DOS_RDARGS, TAG_DONE)))
  295.                  {
  296.                     env_args->RDA_Source.CS_Buffer = environ;
  297.                     env_args->RDA_Source.CS_Length = len;
  298.                     env_args->RDA_Source.CS_CurChr = 0L;
  299.  
  300.                    memset(env_array,0,sizeof(env_array));
  301.  
  302.                     if( ReadArgs( template, (LONG *)&env_array[ 0 ], env_args ))
  303.                          arg_array_Parse( &env_array[ 0 ] );
  304.  
  305.                     FreeArgs( env_args );
  306.                     FreeDosObject(DOS_RDARGS, env_args);
  307.                  }
  308.             }
  309.           FreeVec( environ );
  310.        }
  311.  
  312.      /* Read command line with ReadArgs */
  313.  
  314.      memset(cli_array,0,sizeof(cli_array));
  315.  
  316.      if ( ! ( cli_args = ReadArgs( template, &cli_array[ 0 ], 0L )))
  317.        {
  318.           if ( _Backstdout )
  319.             {
  320.                 if ( Fault(IoErr(), "ERROR", fault, 300)) FPuts(_Backstdout, fault);
  321.                FPutC ( _Backstdout, '\n');
  322.                Close( _Backstdout);
  323.             }
  324.           Death ( 1 );
  325.        }
  326.  
  327.      arg_array_Parse( &cli_array[ 0 ] );
  328.  
  329.  
  330.     if (argc==0) AddNewSet(); /* program was run from workbench */
  331.  
  332.  
  333.  
  334.      if ( ! (fontPtr = OpencPFont()))
  335.        {
  336.             if ( _Backstdout) Close( _Backstdout);
  337.             Death(10);
  338.        }
  339.  
  340.      if ( OpenGTXScreen())
  341.        {
  342.             if ( _Backstdout) Close( _Backstdout);
  343.             Death(10);
  344.        }
  345.  
  346.      if ( OpenGTXWindow( OpenPlotWindowWindow ))
  347.        {
  348.             if ( _Backstdout) Close( _Backstdout);
  349.             Death(10);
  350.        }
  351.  
  352.  
  353. /* load files from Wild pattern */
  354.  
  355.      SetPointer( PlotWindowWnd, WAITPointer, WAITHEIGHT, WAITWIDTH, WAITXOFF , WAITYOFF);  /* set wait pointer image */
  356.  
  357.     anchorpath = (struct AnchorPath *) AllocVec(sizeof(struct AnchorPath)+255,MEMF_CLEAR);
  358.      anchorpath->ap_Strlen = 255;
  359.  
  360.      while ( files && files[ j ]  && anchorpath )
  361.        {
  362.           if ( ! Stricmp(files[ j ], "*")) files[ j ] = "#?";
  363.           
  364.           mres = MatchFirst( files[ j ], anchorpath );
  365.  
  366.           if ( mres == NULL )
  367.             {
  368.                 if (anchorpath->ap_Info.fib_DirEntryType < 0)
  369.                   {
  370.                    lock = CurrentDir( anchorpath->ap_Current-> an_Lock );
  371.                    AddSet( anchorpath->ap_Info.fib_FileName );
  372.                    CurrentDir( lock );
  373.               }
  374.                while ( MatchNext( anchorpath ) == NULL )
  375.                  {
  376.                     if (anchorpath->ap_Info.fib_DirEntryType < 0)
  377.                       {
  378.                         lock = CurrentDir( anchorpath->ap_Current-> an_Lock );
  379.                         AddSet( anchorpath->ap_Info.fib_FileName );
  380.                         CurrentDir( lock );
  381.                      }
  382.               }
  383.             }
  384.           else
  385.             {
  386.                if ( _Backstdout)
  387.                  {
  388.                 if ( Fault(IoErr(), files[ j ], fault, 300)) FPuts(_Backstdout, fault);
  389.                     FPutC ( _Backstdout, '\n');
  390.                     Close( _Backstdout);
  391.                  }
  392.                MatchEnd( anchorpath );
  393.                Death ( 5 );
  394.             }
  395.  
  396.           MatchEnd( anchorpath );
  397.           j++;
  398.        }
  399.  
  400.  
  401.      if ( _Backstdout) Close( _Backstdout);  /* close output so we detache from shell */
  402.  
  403.      DrawView( TRUE );   /* draw everything */
  404.  
  405.     /*
  406.      * Note that SimpleRexx is set up such that you do not
  407.      * need to check for an error to initialize your REXX port
  408.      * This is so application could run without REXX...
  409.      */
  410.  
  411.     if (Stricmp(MyName,&version_string[5])==0) strcpy (RexxName,"CPLOT_REXX");
  412.     else strcpy(RexxName,MyName);
  413.  
  414.     RexxStuff=InitARexx(RexxName,NULL);
  415.  
  416.     rexxsigs = ARexxSignal(RexxStuff);
  417.  
  418. /* loop to monitor window events and rexx port and watch for break */
  419.  
  420.      do
  421.        {
  422.          ClearPointer( PlotWindowWnd );    /* clear wait pointer image */
  423.  
  424.           sigs = Wait ( rexxsigs | SIGBREAKF_CTRL_C | 1L<<PlotWindowWnd->UserPort->mp_SigBit );
  425.  
  426.          SetPointer( PlotWindowWnd, WAITPointer, WAITHEIGHT, WAITWIDTH, WAITXOFF , WAITYOFF);    /* Set wait pointer image */
  427.  
  428.           SetWindowTitles( PlotWindowWnd, MyName, MyName );
  429.  
  430.         if (sigs & 1L<<PlotWindowWnd->UserPort->mp_SigBit) alive = HandlePlotWindowIDCMP();
  431.           else if (sigs & rexxsigs ) alive = HandleARexxMes();
  432.           else if (sigs & SIGBREAKF_CTRL_C ) alive = 0;
  433.           
  434.           if      (alive == 10) NewDepth(1);
  435.           else if (alive == 20) NewDepth(2);
  436.           else if (alive == 30) NewDepth(3);
  437.           else if (alive == 40) NewDepth(4);
  438.        }
  439.      while ( alive );
  440.  
  441.      Death(0); /* normal exit */
  442. }
  443.