/* This file is part of the origami distrubution. (Amiga Port)
 *
 * Source code written by Thomas Hadig in September 1991
 * Version : 1.6.42 alpha, June 1992
 * (C) 1992 by Thomas Hadig
 * may be distributed unchanged ! (see copyright notes)
 */

/*{{{  #includes*/
/*{{{  stardard includes*/
#include <sys/types.h>
#include <sys/stat.h>
#include <ctype.h>
#include <string.h>
#include <signal.h>
#include <limits.h>
#include <stdlib.h>
#include <stdio.h>
/*}}}  */
#include <local/bool.h>
#ifdef LATTICE
#include <proto/exec.h>
#else
#include <clib/exec_protos.h>
#endif

#define MISC_C
#define I_VIRTUAL_C
#define I_FOLDING_C
#define I_GETMSG_C
#define I_MAIN_C
#define I_PROMPT_C
#define I_MESSAGES_C
#define I_ORIEDT_C
#define I_LOOP_C
#define I_FINDS_C

#ifndef LATTICE
#define C_Args
#endif

#include "origami.h"
#include "origami:h/envvar_str.h"
#include "macros.h"

/*{{{  ASL*/
#ifdef ASL
#include <libraries/asl.h>
#ifdef LATTICE
#include <proto/asl.h>
#else
#include <clib/asl_protos.h>
#endif
#endif
/*}}}  */
/*{{{  ARP*/
#ifdef ARP
/* ATTENTION : the FileRequester structure has to been renamed to
 * ArpFileRequester to be used with the ASL requester !
 */
#include <arpbase.h>
#ifdef LATTICE
#include <proto/arp.h>
#else
#include <proto/arp.h>
#endif
#endif
/*}}}  */
/*}}}  */
/*{{{  variables*/
#ifdef ARP
struct ArpBase *ArpBase;
#endif
char filename_for_stat[_POSIX_PATH_MAX]="";
int mode_of_file;
/*}}}  */

/*{{{  fileprompt*/
char *fileprompt (char n[_POSIX_PATH_MAX+1])

 {
  /*{{{  variables*/
  char file[_POSIX_PATH_MAX],path[_POSIX_PATH_MAX];

#    ifdef ASL
  struct AslBase *AslBase;
  struct FileRequester *req1;
#    endif
#    ifdef ARP
  struct ArpFileRequester req2;
#    endif
  /*}}}  */

#  if defined(ASL) | defined(ARP)
  stcgfp (path,get_data(real_tail));
  stcgfn (file,get_data(real_tail));
#  endif
  /*{{{  read from statusline*/
  readprompt(n,get_msg(M_FILENAME),_POSIX_PATH_MAX);
  if (!*n)
    if (real_tail == real_head)
      *n = '\0';
    else if (!aborted)
      strcpy(n,get_data(real_tail));
  /*}}}  */
#  if defined(ASL) | defined(ARP)
  /*{{{  get requester, if posible*/
  if ((strlen (n)==1)&&((n[0]==':')||(n[0]=='/')))
   {
    if (n[0]!='/')
     {
      path[0]=0x00;
      file[0]=0x00;
     }
#      ifdef ASL
    /*{{{  asl.library*/
    if (AslBase = (struct AslBase *)OpenLibrary (AslName,0L))
     {
      if (req1 = AllocFileRequest ())
       {
        req1->rf_Reserved1 = "Origami-FileRequester"; /* was rf_Hail */
        req1->rf_File = file;
        req1->rf_Dir  = path;
        if (RequestFile (req1))
         {
          strcpy (n,path);
          if ((strlen(n))&&(n[strlen(n)-1]!=':')&&(n[strlen(n)-1]!='/'))
            strcat (n,"/");
          strncat (n,file,_POSIX_PATH_MAX+1-strlen(n));
          FreeFileRequest (req1);
          return n;
         }
        FreeFileRequest (req1);
        n[0]=0;
        return n;
       }
     }
    /*}}}  */
#      endif
#      ifdef ARP
    /*{{{  arp.library*/
    if (ArpBase = (struct ArpBase *)OpenLibrary ("arp.library",0L))
     {
      req2.fr_Hail = "Origami-FileRequester";
      req2.fr_File = file;
      req2.fr_Dir  = path;
      req2.fr_Window = NULL;
      req2.fr_FuncFlags = 0;
      req2.fr_Flags2 = 0;
      req2.fr_Function = NULL;
      req2.fr_LeftEdge = 0;
      req2.fr_TopEdge = 0;
      if (FileRequest (&req2))
       {
        strcpy (n,path);
        if ((strlen(n))&&(n[strlen(n)-1]!=':')&&(n[strlen(n)-1]!='/'))
          strcat (n,"/");
        strncat (n,file,_POSIX_PATH_MAX+1-strlen(n));
        return n;
       }
      n[0]=0;
      return n;
     }
    /*}}}  */
#      endif
    /*{{{  read from statusline*/
    readprompt(n,get_msg(M_FILENAME),_POSIX_PATH_MAX);
    if (!*n)
      if (real_tail == real_head)
        *n = '\0';
      else if (!aborted)
        strcpy (n,get_data(real_tail));
    /*}}}  */
   }
  /*}}}  */
#  endif
  return(n);
 }
/*}}}  */

/*{{{  my_fopen*/
FILE *my_fopen (char *name,char *mode)
{
  struct stat st;

  if (!stat (name,&st))
  {
    strcpy (filename_for_stat,name);
    mode_of_file = st.st_mode &
#ifdef LATTICE
       (S_ISCRIPT|S_IPURE|S_IREAD|S_IWRITE|S_IEXECUTE|S_IDELETE);
#else
       (FIBF_SCRIPT|FIBF_PURE|FIBF_READ|FIBF_WRITE|FIBF_EXECUTE|FIBF_DELETE);
#endif
  }
  return fopen (name,mode);
}
/*}}}  */
/*{{{  my_fclose*/
int my_fclose(FILE *fp)
{
  int ret;

  ret = fclose (fp);
  if (*filename_for_stat)
  {
#ifdef LATTICE
    chmod (filename_for_stat,mode_of_file);
#else
    SetProtection (filename_for_stat,mode_of_file);
#endif
    filename_for_stat[0]=0;
  }
  return ret;
}
/*}}}  */
/*{{{  getpid*/
long getpid(void)
{
  return FindTask (NULL);
}
/*}}}  */
/*{{{  usleep*/
void usleep (long x)
{
  if (x>20000) Delay (x/20000);
}
/*}}}  */

/*{{{  is_file*/
public bool is_file(FILE *f)
{
#if 0
  struct stat buff;
  int stat_ret;

  if (!f) return(TRUE);
  if (stat_ret=fstat(fileno(f),&buff)) return(TRUE);
  if (S_ISDIR(buff.st_mode)) return(FALSE);
#endif
  return(TRUE);
}
/*}}}  */
/*{{{  is_dir*/
public bool is_dir(FILE *f)
{ return(!is_file(f));
}
/*}}}  */

#ifdef LATTICE
/*{{{  functions not included in lattice*/
/*{{{  sleep*/
void sleep(int len)
{
  Delay (50*len);
}
/*}}}  */
/*}}}  */
#endif
#ifndef LATTICE
/*{{{  functions not included in dice*/
/*{{{  variables*/
int sys_nerr = 0,msflag=0;
char sys_errlist[0] = "";
/*}}}  */

/*{{{  stpblk*/
char *stpblk (char *p)

 {
  char *q=p;

  while ((q[0]!=0)&&(isspace (q[0]))) q++;
  return q;
 }
/*}}}  */
/*{{{  putenv*/
int putenv (char *env)

 {
  FILE *fd;
  int i;
  char *ptr=env;

  while ((*ptr)&&(*ptr!=' ')) ptr++;
  if (*ptr!=' ') return -1;
  *ptr=0;
  ptr++;
  if (fd = fopen (env,"w")) return -2;
  fprintf (fd,"%s",ptr);
  fclose (fd);
  return 0;
 }
/*}}}  */
/*{{{  stcgfn*/
int stcgfn (char *node, char *name)

 {
  char *ptr = name;
  char *pptr=ptr;

  while (ptr = strchr (ptr,'/')) pptr=ptr;
  strcpy (node,++pptr);
  return strlen (node);
 }
/*}}}  */
/*{{{  stcgfp*/
int stcgfp (char *path, char *name)

 {
  char *ptr = path;
  char *pptr=ptr;

  strcpy (path,name);
  while (ptr = strchr (ptr,'/')) pptr=ptr;
  pptr[0]=0;
  return strlen (path);
 }
/*}}}  */

/*}}}  */
#endif
