/*
 *  RequestFile clone for AmigaDOS 2.1 (V38) or better.
 *
 *  Copyright (C) 1994 Torsten Poulin Nielsen
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *             The author can be contacted by mail at
 *               Torsten Poulin
 *               Banebrinken 99, 2, 77
 *               DK-2400 Copenhagen NV
 *               Denmark
 *             or via email: torsten@diku.dk
 *
 * $Id: RequestFile.c,v 1.2 94/02/08 23:54:04 Torsten Rel $
 * $Log:	RequestFile.c,v $
 * Revision 1.2  94/02/08  23:54:04  Torsten
 * Changed DIR to DRAWER in template.
 * Filenames are now printed all on one line,
 * separated by one space. They are enclosed in quotes, too.
 * WARN (5) is returned if the request is cancelled.
 * We return FAIL (20) if the requester didn't open for some reason
 *    With the above changes, we're a lot more compatible
 *    with the official Commodore-Amiga command for V39.
 * 
 * Revision 1.1  94/01/31  14:46:25  Torsten
 * Initial revision
 * 
 */

#define ASL_V38_NAMES_ONLY

#include <exec/types.h>
#include <exec/memory.h>
#include <dos/dosextens.h>
#include <dos/rdargs.h>
#include <dos/dosasl.h>
#include <libraries/asl.h>
#include <workbench/startup.h>
#include <clib/exec_protos.h>
#include <clib/dos_protos.h>
#include <clib/asl_protos.h>

#ifdef __SASC
#include <pragmas/exec_pragmas.h>
#include <pragmas/dos_pragmas.h>
#include <pragmas/asl_pragmas.h>
#endif
#ifndef __AMIGADATE__
#define __AMIGADATE__ "(31.1.94)"
#endif

#include <string.h>

#define PATHLEN 512
#define TEMPLATE \
"DRAWER,FILE/K,PATTERN/K,TITLE/K,POSITIVE/K,NEGATIVE/K,ACCEPTPATTERN/K,\
REJECTPATTERN/K,SAVEMODE/S,MULTISELECT/S,DRAWERSONLY/S,\
NOICONS/S,PUBSCREEN/K"

char const ver[] = "$VER: RequestFile 38.2 " __AMIGADATE__;
char const cop[] = "$COPYRIGHT: © 1994 Torsten Poulin$";

long reqfile(void)
{
  struct Library *AslBase;
  struct Library *DOSBase;
  struct RDArgs *rdarg;
  struct FileRequester *fr;
  struct TagItem tags[15];
  struct WBArg *frargs;
  int t = 0;
  UBYTE *dirname, *accept = NULL, *reject = NULL;
  long patlen;
  long rc = RETURN_OK;
  struct {
    STRPTR path;
    STRPTR file;
    STRPTR pattern;
    STRPTR title;
    STRPTR positive;
    STRPTR negative;
    STRPTR acceptpat;
    STRPTR rejectpat;
    LONG save;
    LONG multi;
    LONG drawers;
    LONG noicons;
    STRPTR pubscreen;
  } opt;

  if (DOSBase = OpenLibrary("dos.library", 37)) {
    if (AslBase = OpenLibrary("asl.library", 38)) {
      if (dirname = AllocVec(PATHLEN+1, MEMF_ANY)) {
	memset((char *) &opt, 0, sizeof(opt));
	if (rdarg = ReadArgs(TEMPLATE, (LONG *) &opt, NULL)) {

	  if (opt.path) {
	    tags[t].ti_Tag = ASLFR_InitialDrawer;
	    tags[t++].ti_Data = (ULONG) opt.path;
	  }
	  if (opt.file) {
	    tags[t].ti_Tag = ASLFR_InitialFile;
	    tags[t++].ti_Data = (ULONG) opt.file;
	  }
	  if (opt.pattern) {
	    tags[t].ti_Tag = ASLFR_InitialPattern;
	    tags[t++].ti_Data = (ULONG) opt.pattern;
	    tags[t].ti_Tag = ASLFR_DoPatterns;
	    tags[t++].ti_Data = TRUE;
	  }
	  if (opt.title) {
	    tags[t].ti_Tag = ASLFR_TitleText;
	    tags[t++].ti_Data = (ULONG) opt.title;
	  }
	  if (opt.positive) {
	    tags[t].ti_Tag = ASLFR_PositiveText;
	    tags[t++].ti_Data = (ULONG) opt.positive;
	  }
	  if (opt.negative) {
	    tags[t].ti_Tag = ASLFR_NegativeText;
	    tags[t++].ti_Data = (ULONG) opt.negative;
	  }
	  if (opt.acceptpat) {
	    patlen = strlen(opt.acceptpat) * 2 + 2;
	    if (accept = AllocVec(patlen+1, MEMF_ANY))
	      if (ParsePatternNoCase(opt.acceptpat, accept, patlen) == 1) {
		tags[t].ti_Tag = ASLFR_AcceptPattern;
		tags[t++].ti_Data = (ULONG) accept;
	      }
	  }
	  if (opt.rejectpat) {
	    patlen = strlen(opt.rejectpat) * 2 + 2;
	    if (reject = AllocVec(patlen+1, MEMF_ANY))
	      if (ParsePatternNoCase(opt.rejectpat, reject, patlen) == 1) {
		tags[t].ti_Tag = ASLFR_RejectPattern;
		tags[t++].ti_Data = (ULONG) reject;
	      }
	  }
	  if (opt.save) {
	    tags[t].ti_Tag = ASLFR_DoSaveMode;
	    tags[t++].ti_Data = TRUE;
	  }
	  if (opt.multi) {
	    tags[t].ti_Tag = ASLFR_DoMultiSelect;
	    tags[t++].ti_Data = TRUE;
	  }
	  if (opt.drawers) {
	    tags[t].ti_Tag = ASLFR_DrawersOnly;
	    tags[t++].ti_Data = TRUE;
	  }
	  if (opt.noicons) {
	    tags[t].ti_Tag = ASLFR_RejectIcons;
	    tags[t++].ti_Data = TRUE;
	  }
	  if (opt.pubscreen) {
	    tags[t].ti_Tag = ASLFR_PubScreenName;
	    tags[t++].ti_Data = (ULONG) opt.pubscreen;
	  }
	  tags[t].ti_Tag = TAG_END;

	  if (fr = (struct FileRequester *)
	      AllocAslRequest(ASL_FileRequest, tags)) {
	    if (AslRequest(fr, NULL)) {
	      if (opt.multi && fr->fr_NumArgs) {
		frargs = fr->fr_ArgList;
		for (t = 0; t < fr->fr_NumArgs; t++) {
		  strcpy(dirname, fr->fr_Drawer);
		  AddPart(dirname, frargs[t].wa_Name, PATHLEN);
		  PutStr("\"");
		  PutStr(dirname);
		  PutStr(t < fr->fr_NumArgs - 1 ? "\" " : "\"\n");
		}
	      }
	      else {
		strcpy(dirname, fr->fr_Drawer);
		AddPart(dirname, fr->fr_File, PATHLEN);
		PutStr("\"");
		PutStr(dirname);
		PutStr("\"\n");
	      }
	    }
	    else if (IoErr())
	      rc = RETURN_FAIL; /* something went wrong; no pubscreen? */
	    else
	    {
	      /* Nothing selected */
	      rc = RETURN_WARN;
	      SetIoErr(ERROR_BREAK);
	    }
	    FreeAslRequest(fr);
	  }
	  FreeArgs(rdarg);
	}
	else rc = RETURN_FAIL;
	if (accept) FreeVec(accept);
	if (reject) FreeVec(reject);
	FreeVec(dirname);
      }
      else rc = RETURN_FAIL;
      CloseLibrary(AslBase);
    }
    else {
      SetIoErr(ERROR_INVALID_RESIDENT_LIBRARY); /* Not a perfect message */
      rc = RETURN_FAIL;
    }
    if (rc == RETURN_FAIL) PrintFault(IoErr(), "RequestFile");
    CloseLibrary(DOSBase);
  }
  else rc = RETURN_FAIL;
  return rc;
}

