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

#ifndef AMIGA_H
#define AMIGA_H
/*{{{}}}*/
/*{{{  general defines*/
#define _POSIX_PATH_MAX 120
#define STD_BASENAME ".origami"
#define MOUSE_BUTTONS 30
#define RC_BUFFER_SIZE 8192
#define BASENAMEF       ".%s"
#define ORIGAMI_RC_PATH   "S:"

/*{{{  directory defines*/
#undef  CURR_DIR
#undef  PREV_DIR
#define CURR_DIR   getcwd(buffer_dir,_POSIX_PATH_MAX)
#define PREV_DIR "/"

#define IS_ROOT(s) (!strcmp(s,":"))
/*}}}  */
/*{{{  stat stuff*/
#define S_ISDIR(x)  (x & S_IFDIR)
#define S_ISCHR(x)  (0)
#define S_ISBLK(x)  (0)
#define S_ISREG(x)  (x & S_IFREG)
#define S_ISFIFO(x) (0)

#define S_IRUSR     (0)
#define S_IRGRP     (0)
#define S_IROTH     (S_IREAD)
/*}}}  */
/*{{{  passwd defines*/
#define getpwuid(x)	((struct passwd *)0)
/*{{{  struct passwd*/
struct passwd { char *pw_name; };
/*}}}  */
/*}}}  */
/*}}}  */

#ifndef FOLDER
#define boolean int
/*{{{  not dirfold*/
/*{{{  compiler defines (switches for features)*/
/* There are some switches, that enable several features in origami :
 * ASL : will use the asl requester, if asl.library is available
 * PRIORITY : will change origamis priority, when activated (3)
 * AREXX : will enable the arexx port (isn't functional)
 * MOUSY : will enable mouse using
 * MENU  : will enable menus
 * ICONIFY : will enable the inconify feature
 * CUTPASTE : will enable the cut & paste feature
 *
 * There are some switches that define some of the above features :
 * REQUEST : ASL
 * NORMAL : MENU & MOUSY & CUTPASTE
 * BIG1 : REQUEST & NORMAL
 * BIG : BIG1 & ICONIFY
 *
 * VIRTUAL will not be supported
 */

#define DOC_MSG           ORIGAMI_RC_PATH
#define KBD_PATH          "S:origami"
#define NOSYSEXIT
#define DEFAULT_NAME      "Origami"
#define DEFAULT_TERM      "AMIGA"
#define PORTER            "Amiga Port by Thomas Hadig.\n"
#define USE_SCRBUFF
#define BLOCK_FLUSH
#define SW_MSG
/*{{{  dir_edit*/
#define DIREDT
#define DC_OPT_F "-s -M \"%s%s%s%s\" -f \"%s\" -F \"%s\" %s %s"
#define DC_OPT_ARG(na,long,ma,noma)                                 \
  bd.f.str.open_f,bd.f.str.file_f,bd.f.str.line_f,bd.f.str.close_f, \
  ma,noma,((long)?"-l":""),na
#define DIR_CMD(name,long,match,nomatch)                     \
  (char*)get_msg                                             \
   ( MSG_ARG_FORMAT,                                         \
     "dirfold "DC_OPT_F,                                     \
     DC_OPT_ARG(name,long,match,nomatch)                     \
   )
#ifdef DIREDT_PATH
# define DIR_CMD2(name,long,match,nomatch)                 \
   (char*)get_msg                                          \
    ( MSG_ARG_FORMAT,                                      \
      DIREDT_PATH PATH_SEP "dirfold "DC_OPT_F,             \
      DC_OPT_ARG(name,long,match,nomatch)                  \
    )
# endif
/*}}}  */

#ifdef BIG
#  define BIG1
#  define ICONIFY
#endif
#ifdef BIG1
#  define REQUEST
#  define NORMAL
#endif
#ifdef REQUEST
#  define ASL
#endif
#ifdef NORMAL
#  define MENU
#  define MOUSY
#  define CUTPASTE
#endif
/*}}}  */

/*{{{  additional includes*/
#ifdef AREXX
#include <amiga/h/ori_arexx.h>
#endif
#include <exec/types.h>
#include <stdio.h>
/*}}}  */
/*{{{  stdio-stuff*/
#ifndef _OFF_T
#define _OFF_T 1
typedef int off_t;
#endif

#define READ            "r"
#define READ_BIN        "r"
#define WRITE           "w"
#define WRITE_BIN       "w"
#define LIST_SEP        ";"
#define LIST_C          ';'
#define LIST_C_1        '\001'
#define OS_NULL_DEVICE  "nil:"
#define NEVER           0
#define RC_OS_EXT_HANDLE(f,i) os_ext_handle(f,i)
/*}}}  */
/*{{{  defines*/
#define SIGWINCH

/*{{{  Amiga Arguments*/
#define OWN_ARG_HANDLE nok = handle_amiga_args (optarg);
#define OWN_ARG_USAGE_SHORT \
"\n        [-X S[CREEN][=Width/Height]|W[INDOW]=X/Y/Width/Height|A[REXX][=Portname]]\n       "
#define OWN_ARG_USAGE_LONG \
    "-X S[CREEN][=Width/Height] opens origami on own screen (size w,h)\n"\
"    -X W[INDOW]=X/Y/Width/Height] specifies window size\n"\
"    -X A[REXX][=Portname]\n"
/*}}}  */
/*{{{  mouse and menu defines*/
#define AMIGA_MOUSE_TAG    amiga
#define OS_MOUSE_TAG       AMIGA_MOUSE_TAG
#define AMIGA_MOUSE_NAME   "amiga"
#define OS_MOUSE_NAME      AMIGA_MOUSE_NAME
#define AMIGA_MOUSE_COUNT  MOUSE_BUTTONS
#define OS_MOUSE_COUNT     AMIGA_MOUSE_COUNT
#define AMIGA_MOUSE_NAMES { \
  "LMBU", "RMBU", "MMBU", "LMBD", "MMBD", \
  "S-LMBU", "S-RMBU", "S-MMBU", "S-LMBD", "S-MMBD", \
  "C-LMBU", "C-RMBU", "C-MMBU", "C-LMBD", "C-MMBD", \
  "A-LMBU", "A-RMBU", "A-MMBU", "A-LMBD", "A-MMBD", \
  "L-LMBU", "L-RMBU", "L-MMBU", "L-LMBD", "L-MMBD", \
  "R-LMBU", "R-RMBU", "R-MMBU", "R-LMBD", "R-MMBD"}
#define OS_MOUSE_TAG_MOUSE_UP(a) ((a%5)<3)
#define OS_MOUSE_NAMES     AMIGA_MOUSE_NAMES
#define OS_MOUSE_LG        0
#define OS_MOUSE_CODE      ""
#define DEFAULT_MOUSE      "amiga"
/*}}}  */
/*{{{  screen defines*/
#define Off_X 4          /* defines start position X-coordinate  */
#define Off_X_Right 19   /* defines space right in pixels */
#define Off_Y_Bottom 3   /* .. bottom */
/*}}}  */
/*{{{  stdio defines*/
#define oputs(a)  eputs(a)
#define oflush    FlushOneLine();
/*}}}  */
/*{{{  get-key defines*/
#define PSEUDO_ABORT_KEY 0x9C
#define CSI 0x9B
#define buffer_keys_max  200
#define CSI_RAW_KEY 69
#define CSI_NUMBER_PAD 70
#define CSI_MENU_PICK 71
#ifdef CUTPASTE
  /*{{{  cut & paste stuff*/
#  define CUPA_TEXT_LEN    256
  struct cupa
   {
    struct cupa *Next;
    int cur_pos;
    char text[CUPA_TEXT_LEN];
   };
  /*}}}  */
#endif
/*}}}  */
/*{{{  GETMSG_C*/
#ifdef GETMSG_C
#  define I_SHELL_C
#endif
/*}}}  */
/*{{{  KEYBIND_C*/
#ifdef KEYBIND_C
#  undef strlen
#  define SIGQUIT   SIGINT
#  define SIGHUP    SIGINT
#  define I_AMIGAFILEIO_C
#  define REF_TMP_FILE "T:kbrefXXXXXX"
   char buffer_dir[_POSIX_PATH_MAX];
#endif
/*}}}  */
/*{{{  FINDS_C*/
#ifdef FINDS_C
#  define I_SIGNALS_C
#endif
/*}}}  */
/*{{{  STRRSTR_C*/
char *strrstr (char *, char*);
/*}}}  */
/*{{{  KEYTAB_C*/
#ifdef KEYTAB_C
#define I_SIGNALS_C
#endif
/*}}}  */

#define os_init_args_read() init_amiga_post_arg()
#define os_init_rc_read() init_amiga_post_rc()
#define os_init() init_amiga_pre_arg(&argc, ((struct WBStartup **)&argv))
#define open_shell popen
#define close_shell pclose
/*{{{  min*/
#ifndef min
#define min(a,b) ((a)<=(b)?(a):(b))
#endif
/*}}}  */
/*}}}  */
/*{{{  additional define for some modules*/
/*{{{  INITRESET*/
#ifdef INITRESET_C
# define CONU_LIBRARY -1
#endif
/*}}}  */
/*{{{  GETTK*/
#ifdef GETTK_C
#  undef signal
#  define signal(a,b)  {}
#  define chartest if (ch=='\0' || ch=='\n') ch=O_NOP;
#endif
/*}}}  */
/*{{{  SCANNER*/
#ifdef SCANNER_C
#  define OS_USINGS  use_it ("AMIGA");
  extern char *buffer_dir;
#  define OS_VARS init_arexx_var();
  extern void init_arexx_var(void);
#endif
/*}}}  */
/*{{{  KEYBIND*/
#ifdef KEYBIND_C
  /*{{{  write binding to .origamibind*/
#  define bnd_wr(bnd,kbd,name,code) \
    fprintf(bnd,kbd==mouse_kbd?"%d|%-35.*sMouse: %c\n":"%d|%-35.*s%s\n", \
                kbd,strlen(name),name,code)
  /*}}}  */
  /*{{{  SORTCMD*/
#  define SORTCMD "orisort >%s "
  /*}}}  */
#endif
/*}}}  */
/*{{{  ORIEDT*/
#ifdef ORIEDT_C
#  define BASIC_ELEMENTS    256   /* number of first element's block */
#  define NEW_ELEMENT_LIMIT  16   /* minimum number of malloced      */
                                  /* element's block.                */
                                  /* if malloc cannot return a block */
                                  /* of NEW_ELEMENT_LIMIT elements,  */
                                  /* a `not enough memory' warning   */
                                  /* appears!                        */
#  define NEW_ELEMENT_MAX    64   /* maximum number of malloced      */
                                  /* element's block (used if lines  */
                                  /* are handled virtuell)           */
#endif
/*}}}  */
/*{{{  FOLDFILLING*/
#ifdef FOLDFILING_C
#  define ORICORE_NAME "oricoreXXXXXXXX"
#  define CHMOD_PRIVATE(f) chmod(f,S_IWRITE | S_IREAD)
#endif
/*}}}  */
/*{{{  SET || PARSECOND || MAIN*/
#if defined(SET_C) || defined(PARSECOND_C) || defined(MAIN_C)
#  define SETPART 8
#  define SETSIZE (256/SETPART)
#endif
/*}}}  */
/*{{{  WRITEFOLDS*/
#ifdef WRITEFOLDS_C
#  undef fopen
#  undef fclose
#  define fopen(a,b) my_fopen(a,b)
#  define fclose(a)  my_fclose(a)
#endif
/*}}}  */
/*{{{  FIELDEDIT*/
#ifdef FIELDEDIT_C
# define MGR
# define USER_SIGNAL
  extern void user_signal_handle(void);
#endif
/*}}}  */
/*{{{  LOOP*/
#ifdef LOOP_C
struct utsname { int i;};
#endif
/*}}}  */
/*{{{  SCRBUFF*/
#ifdef SCRBUFF_C
#define EXT_SCRBUFF_ACCESS
#define fl_overwrite my_fl_overwrite
#endif
/*}}}  */
/*{{{  SCRBUFF || SCREEN*/
#if defined (SCRBUFF_C) || defined (SCREEN_C)
/*{{{  line_store struct*/
struct line_store
 {
  int x;
  int y;
  int changed;
  char *str;
  int len;
  int maxlen;
 };
/*}}}  */
#endif
/*}}}  */
/*}}}  */
/*{{{  prototypes of amiga files*/
/*{{{  AMIGAMISC*/
#ifndef AMIGA_MISC_C
#  ifdef AREXX
  extern char *portname;
  struct RexxMsg *GetArexxMsg (void);
  void ReplyARexxMsg(struct RexxMsg *,char *,long);
  short SetARexxLastError(struct Message *,char *);
  int handle_rexx (void);
#  endif
  FILE *my_fopen (char *,char *);
  int my_fclose (FILE *);
  int getpid (void);
  void usleep (long);
  void sleep (int);
  void user_signal_handle (void);
  void os_ext_handle (FILE *, int);
  char *strerror(int);
  char *handle_amiga_args(char *);
#endif
/*}}}  */
/*{{{  INITRESET*/
#ifndef INITRESET_C
  /*{{{  MENU*/
#  ifdef MENU
  extern boolean menu_on;
  extern struct Menu *menu_root;
#  endif
  /*}}}  */
  /*{{{  AREXX*/
#  ifdef AREXX
  extern boolean arexx;
  extern _AREXX *_arexx;
  extern struct Library *RexxSysBase;
#  endif
  /*}}}  */
  extern struct Library *IFFParseBase;
  extern struct Screen *beep_screen;
  extern struct Window *wind;
  extern struct RastPort *rp;
  extern short screenw,screenh,windx,windy,windw,windh,
               height,mul_x,mul_y,off_y,off_y_base,init_wind;
  extern boolean screen_yes;
  extern struct MsgPort *mpp;
  extern struct timerequest *tr;
  extern ULONG my_signal;
  extern char window_name[_POSIX_PATH_MAX+1];
  /*{{{  only in Main*/
#  ifdef MAIN_C
  void init_amiga_pre_arg (int *argc, struct WBStartup **argv);
  void init_amiga_post_arg (void);
  void init_amiga_post_rc (void);
  void reset_amiga (void);
#  endif
  /*}}}  */
#endif
/*}}}  */
/*{{{  KEYBOARD*/
#ifndef KEYBOARD_C
#ifdef KEYS_H
  extern TOKEN buffer_keys[buffer_keys_max];
#endif
  extern int key_buffer_max;
  void scan_abort_key (void);
#else
  unsigned char cur_scr_char(int,int);
#endif
/*}}}  */
/*{{{  SCREEN*/
#ifndef SCREEN_C
  void FlushOneLine (void);
  int oputc (char ch);
  int eputc (unsigned char ch);
  int eputs (unsigned char *ptr);
#endif
/*}}}  */
/*{{{  MISC*/
#ifdef MISC_C
#  define OS_FILEPROMPT 1
#endif
/*}}}  */
/*{{{  SIGNALS*/
#ifndef SIGNALS_C
  extern boolean timer_running;
  void alarm (int);
  void sig_alarm_reached (int);
#endif
/*}}}  */
/*}}}  */
/*}}}  */
#undef boolean
#else
/*{{{  dirfold*/
#include <fcntl.h>

#define geteuid()	1
#define getegid()	1
#define getuid()	1
#define getgid()	1
#define gid_t		unsigned short
#define uid_t		unsigned short

char buffer_dir[_POSIX_PATH_MAX]; /* used for getcwd */
/*}}}  */
#endif
/*{{{  os version string*/
#if defined(MAIN_C)||defined(KEYBIND_C)||defined(FOLDER) || defined(VIEW_MAIN_C)
#ifdef ORIGAMI
#define PROGNAME "Origami"
#endif
#ifdef KEYBIND
#define PROGNAME "Keybind"
#endif
#ifdef FOLDER
#define PROGNAME "Dirfold"
#endif
#ifdef VIEWRC
#define PROGNAME "Viewrc"
#endif
#include <h/envvar_str.h>
static char version_string_os_2_x_conform[]= "$VER: " PROGNAME " " MINOR_VERSION "." REVISION " (" __DATE__ ") Major Version " MAJOR_VERSION ;
#endif
#endif
/*}}}  */
