home *** CD-ROM | disk | FTP | other *** search
/ Fish 'n' More 2 / fishmore-publicdomainlibraryvol.ii1991xetec.iso / fish / text / readers / stv / source / main.c < prev    next >
C/C++ Source or Header  |  1991-01-09  |  7KB  |  237 lines

  1. /*********************************
  2. *  MAIN  01/02/91
  3. *  Source file for STV
  4. *  © Copyright 1990 Timm Martin
  5. *  All Rights Reserved Worldwide
  6. **********************************/
  7.  
  8. /*
  9. WW WW WW   AAAA   RRRRR   NN  NN  II  NN  NN   GGGGG
  10. WW WW WW  AA  AA  RR  RR  NNN NN  II  NNN NN  GG      XX
  11. WW WW WW  AA  AA  RRRRR   NNNNNN  II  NNNNNN  GG GGG
  12. WW WW WW  AAAAAA  RR RR   NN NNN  II  NN NNN  GG  GG  XX
  13.  WWWWWW   AA  AA  RR  RR  NN  NN  II  NN  NN   GGGGG
  14.  
  15. The program STV, this source file, and all related source
  16. files are © Copyright 1990 by Timm Martin.  All rights are
  17. reserved worldwide.  This program itself is freeware.
  18. It may be distributed alone or in conjunction with any
  19. program, commercial or otherwise, with no compensation to
  20. the author.  The source files may also be freely distributed
  21. but cannot appear in any commercial product without permission
  22. from the author.
  23. */
  24.  
  25. #include <ctype.h>
  26. #include <exec/types.h>
  27. #include <functions.h>
  28. #include <graphics/text.h>
  29. #include <intuition/intuition.h>
  30. #include <stdlib.h>
  31. #include <stdio.h>
  32. #include <workbench/startup.h>
  33. #include <workbench/workbench.h>
  34. #include "func.h"
  35. #include "main.h"
  36.  
  37. /********************
  38. *  GLOBAL VARIABLES
  39. *********************/
  40.  
  41. /* whether this program was run from the CLI or Workbench */
  42. BOOL from_cli;
  43. /* if running under WB 2.x */
  44. BOOL wb2;
  45.  
  46. struct GfxBase *        GfxBase       = NULL;
  47. extern struct Library * IconBase = NULL;
  48. struct IntuitionBase *  IntuitionBase = NULL;
  49.  
  50. /*************
  51. *  ARGUMENTS
  52. **************/
  53.  
  54. struct Argument arguments =  /* GLOBAL */
  55. {
  56.   /* short           Files     */ 0,
  57.   /* char *          Names[]   */ NULL,
  58.   /* struct WBArg *  ArgList   */ NULL,
  59.   /* short           Left      */ NO_ARGUMENT,
  60.   /* short           Top       */ NO_ARGUMENT,
  61.   /* short           Width     */ NO_ARGUMENT,
  62.   /* short           Height    */ NO_ARGUMENT,
  63.   /* short           TextColor */ WHITE,
  64.   /* struct Screen * Screen    */ NULL,
  65. };
  66.  
  67. /***********
  68. *  M A I N
  69. ************/
  70.  
  71. /*
  72. This procedure runs the program.  If the user specified at least one file to
  73. be read, the libraries and window will be opened and the first file will be
  74. displayed.  If the user didn't specify any file or if [s]he requested help, a
  75. "USAGE" message will be displayed and the program will end.
  76. */
  77.  
  78. void main( int argc, char *argv[] )
  79. {
  80.   if (parse_arguments( argc, argv ))
  81.   {
  82.     program_begin();
  83.     file_select();
  84.   }
  85.   else
  86.     printf( "USAGE: stv file [file...]\n" );
  87.  
  88.   program_end( NULL );
  89. }
  90.  
  91. /*******************
  92. *  PARSE ARGUMENTS
  93. ********************/
  94.  
  95. /*
  96. This function parses the programs arguments whether run from the CLI or
  97. Workbench.  These arguments can include the name[s] of the file[s] to be
  98. viewed, the left edge (-l), top edge (-t), width (-w), and height
  99. (-h) of the window, the text color (-c), and a pointer to the custom screen
  100. (-!) in which the window should be opened.  File names are listed as is, and
  101. special values are listed as ASCII decimal numbers preceeded by a hyphen and
  102. a keyletter.  YES is returned if there is a file to read.  NO is returned if
  103. there is no file or if the user requested help (a "USAGE" message) as
  104. indicated by specifying no arguments or a question mark as the first argument.
  105. */
  106.  
  107. /* the first WB argument passed is this program and must be ignored */
  108. #define WBSTART 1
  109.  
  110. BOOL parse_arguments( int argc, char *argv[] )
  111. {
  112.   int a;              /* which CLI argument working on */
  113.   struct DiskObject *obj;
  114.   char *type;         /* tool type */
  115.   long value;         /* long integer value of CLI argument */
  116.   extern struct WBStartup *WBenchMsg;
  117.  
  118.   /* if run from the CLI (set flag, too) and user doesn't want help */
  119.   if (from_cli = argc > 0)
  120.   {
  121.     /* if not asking for help */
  122.     if (*argv[1] != '?')
  123.     {
  124.       /* check out each argument */
  125.       for (a = 1; a < argc; a++)
  126.       {
  127.         /* is a keyletter, not a file name */
  128.         if (*argv[a] == '-')
  129.         {
  130.           /* get integer value of this argument (starts at third char) */
  131.           value = atol( argv[a]+2 );
  132.           /* convert keyletter to lower case */
  133.           tolower( *(argv[a]+1) );
  134.           /* which keyletter  is it? */
  135.           switch( *(argv[a]+1) )
  136.           {
  137.             case 'l': arguments.Left      = value; break;
  138.             case 't': arguments.Top       = value; break;
  139.             case 'w': arguments.Width     = value; break;
  140.             case 'h': arguments.Height    = value; break;
  141.             case 'c': arguments.TextColor = value; break;
  142.             case '!': arguments.Screen    = (struct Screen *)value; break;
  143.           }
  144.         }
  145.         /* else this is a file; copy file names into beginning of argument
  146.          * array so that all filenames are located between 0..files-1
  147.          */
  148.         else
  149.           argv[arguments.Files++] = argv[a];
  150.       } /* for each argument */
  151.  
  152.       /* get pointer to argument names */
  153.       arguments.Names = argv;
  154.     } /* if not asking for help */
  155.   }   /* if run from the CLI */
  156.  
  157.   /* else run from the Workbench */
  158.   else if (IconBase = OpenLibrary( "icon.library", NULL ))
  159.   {
  160.     /* don't count the program itself */
  161.     if (WBenchMsg->sm_NumArgs > WBSTART)
  162.     {
  163.       arguments.Files = WBenchMsg->sm_NumArgs - WBSTART;
  164.       arguments.ArgList = (struct WBArg *)&WBenchMsg->sm_ArgList[WBSTART];
  165.  
  166.       CurrentDir( (struct FileLock *)WBenchMsg->sm_ArgList[WBSTART].wa_Lock );
  167.       if (obj = GetDiskObject( WBenchMsg->sm_ArgList[WBSTART].wa_Name ))
  168.       {
  169.         if (type = FindToolType( obj->do_ToolTypes, "LEFT" ))
  170.           arguments.Left = atol( type );
  171.         if (type = FindToolType( obj->do_ToolTypes, "TOP" ))
  172.           arguments.Top = atol( type );
  173.         if (type = FindToolType( obj->do_ToolTypes, "WIDTH" ))
  174.           arguments.Width = atol( type );
  175.         if (type = FindToolType( obj->do_ToolTypes, "HEIGHT" ))
  176.           arguments.Height = atol( type );
  177.         if (type = FindToolType( obj->do_ToolTypes, "COLOR" ))
  178.           arguments.TextColor = atol( type );
  179.         FreeDiskObject( obj );
  180.       }
  181.     }
  182.  
  183.     CloseLibrary( IconBase );
  184.   }
  185.  
  186.   return (arguments.Files > 0);
  187. }
  188.  
  189. /*****************
  190. *  PROGRAM BEGIN
  191. ******************/
  192.  
  193. /*
  194. This procedure starts the program, opens the libraries, etc.
  195. */
  196.  
  197. void program_begin( void )
  198. {
  199.   if (IntuitionBase = (struct IntuitionBase *)
  200.                       OpenLibrary( "intuition.library", 36L ))
  201.     wb2 = YES;
  202.   else if (IntuitionBase = (struct IntuitionBase *)
  203.                            OpenLibrary( "intuition.library", 34L ))
  204.     wb2 = NO;
  205.   else
  206.     program_end( "intuition" );
  207.  
  208.   if (!(GfxBase = (struct GfxBase *)
  209.                   OpenLibrary( "graphics.library", NULL )))
  210.     program_end( "graphics" );
  211.  
  212.   pointer_open();
  213.   window_open();
  214. }
  215.  
  216. /***************
  217. *  PROGRAM END
  218. ****************/
  219.  
  220. /*
  221. This procedure ends the program, closes things up, and exits.
  222. */
  223.  
  224. void program_end( char *error )
  225. {
  226.   if (from_cli && error)
  227.     printf( "STV failed: %s\n", error );
  228.  
  229.   window_close();
  230.   pointer_close();
  231.  
  232.   if (GfxBase)       CloseLibrary( GfxBase );
  233.   if (IntuitionBase) CloseLibrary( IntuitionBase );
  234.  
  235.   exit( 0 );
  236. }
  237.