
/*
 *  wbinfo.c
 *
 *  Author: Georg Hessmann (hessmann@informatik.uni-hamburg.de)
 *
 *
 *  Program for 3.x or higher!
 *
 *  Open the workbnch information requester for a given file.
 *
 *  Need SAS/C to compile. Other compilers need small changes.
 *
 *
 *  Copyright: source is public domain, no copyright
 *
 *  Version history:
 *
 *  1.0  20.Jul.93  First release.
 *
 *
 */


#define VERSION "1.0"

static const char version[] = "\0$VER: wbinfo " VERSION " (20.06.93)";



/*
 * Amiga-Includes
 */

#include <exec/types.h>
#include <dos/dos.h>
#include <dos/dosasl.h>
#include <workbench/workbench.h>

#include <clib/dos_protos.h>
#include <clib/exec_protos.h>
#include <clib/wb_protos.h>
#include <clib/intuition_protos.h>
#include <pragmas/dos_pragmas.h>
#include <pragmas/exec_pragmas.h>
#include <pragmas/wb_pragmas.h>
#include <pragmas/intuition_pragmas.h>


/*
 * Define SAS/C builtin functions
 */

typedef unsigned int size_t;
#define strlen __builtin_strlen
extern int strlen(char *);
extern void   *__builtin_memcpy(void *, const void *, size_t);
#define memcpy(a,b,c) __builtin_memcpy(a,b,c)
extern char *strcpy(char *, const char *);
extern char   *__builtin_strcpy(char *, const char *);
#define strcpy(a,b) __builtin_strcpy(a,b)




/*
 * Template for ReadArgs()
 */

#define TEMPLATE	"NAME/A,PUBSCREEN/K"




/*
 * Main function. Must be the first in the module
 */

long __saveds main(void)
{
  long rc = RETURN_OK;

  struct Library       * DOSBase;
  struct Library       * WorkbenchBase;
  struct Library       * IntuitionBase;

  long            options[2];
  struct RDArgs * args;

  char PubName[MAXPUBSCREENNAME+1];

  DOSBase = OpenLibrary("dos.library", 36);	// need at least 36 for PutStr()
  
  if (DOSBase && DOSBase->lib_Version < 39) {
    PutStr("Need system version 39.x or higher!\n");
    CloseLibrary(DOSBase);
    return RETURN_FAIL;
  }

  WorkbenchBase = OpenLibrary("workbench.library", 39);
  IntuitionBase = OpenLibrary("intuition.library", 39);
  if (!(WorkbenchBase && IntuitionBase)) return RETURN_FAIL;

  options[0] = options[1] = 0L;
  args = ReadArgs(TEMPLATE, options, NULL);

  if (args) {
    char * file    = (char *)options[0];
    char * pubname = (char *)options[1];
    
    if (pubname) {
      if (strlen(pubname) >= sizeof(PubName)) {		// test max length
        PrintFault(ERROR_BUFFER_OVERFLOW, pubname);
        rc = RETURN_FAIL;
      }
      else {
        strcpy(PubName, pubname);
      }
    }
    else {
      GetDefaultPubScreen(PubName);
    }

    if (rc == RETURN_OK) {
      struct Screen * screen = LockPubScreen(PubName);
        
      if (screen) {
        BPTR lock = Lock(file, ACCESS_READ);

        if (lock) {
          BPTR par;
          par = ParentDir(lock);
          if (!par) par = DupLock(lock);	// for Devices
          
          if (!WBInfo(par, file, screen)) rc = RETURN_WARN;
          if (par) UnLock(par);

          UnLock(lock);
        }
        else {
          PrintFault(IoErr(), file);
          rc = RETURN_ERROR;
        }
     
        UnlockPubScreen(NULL, screen);
      }
      else {	// error message for 'pubscreen not found' ... ??
        PrintFault(ERROR_OBJECT_NOT_FOUND, pubname);
        rc = RETURN_FAIL;
      }
    }
    
    FreeArgs(args);
  }
  else {
    PrintFault(IoErr(), NULL);
    rc = RETURN_ERROR;
  }
  
  CloseLibrary(WorkbenchBase);
  CloseLibrary(IntuitionBase);
  CloseLibrary(DOSBase);

  return rc;
}

