/*
 * mars-nwe-dosutils - NetWare/DOS utility tools.
 *
 * Copyright (C) 2026 Mario Fetka
 * Copyright (C) 1993,1996 Martin Stover, Marburg, Germany
 *
 * 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, see <http://www.gnu.org/licenses/>.
 */

/*
 * Purpose: Shared public header for the DOS utility collection: types, prototypes and common structures.
 * Depends on: ipx.h, ncp.h, all command modules, netcall.c, ncpcall.c, tools.c and ncpapi.c.
 */

#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <conio.h>
#include <io.h>
#include <bios.h>
#include <dos.h>
#include <process.h>
#include <stdarg.h>
#ifdef __WATCOMC__
#include <direct.h>
extern int tool_page_line(int *line_count, int *continuous);
#endif

typedef unsigned int       UI;
typedef unsigned int       uint;
typedef unsigned char      UC;
typedef unsigned char      uint8;
typedef unsigned short int uint16;
typedef unsigned long  int uint32;

typedef union  REGS  REGS;
typedef struct SREGS SREGS;

typedef void  (*FUNC_VOID)();
typedef int   (*FUNC_INT)();


#include "ipx.h"
#include "ncp.h"


#define UI2NET(i)  ( ( (i) << 8)  |  ( ((i)>>8) & 0xFF) )
#define NET2UI(i)  ( ( (i) << 8)  |  ( ((i)>>8) & 0xFF) )

#define U16_TO_BE16(u, b) { uint16 a=(u); \
               *(  (uint8*) (b) )    = *( ((uint8*) (&a)) +1); \
               *( ((uint8*) (b)) +1) = *(  (uint8*) (&a)); }


#define U32_TO_BE32(u, ar) { uint32 a= (u); uint8 *b= ((uint8*)(ar))+3; \
               *b-- = (uint8)a; a >>= 8;  \
               *b-- = (uint8)a; a >>= 8;  \
               *b-- = (uint8)a; a >>= 8;  \
               *b   = (uint8)a; }

#define U16_TO_16(u, b) { uint16 a=(u); memcpy(b, &a, 2); }
#define U32_TO_32(u, b) { uint32 a=(u); memcpy(b, &a, 4); }

#define GET_BE16(b)  (     (int) *(((uint8*)(b))+1)  \
                     | ( ( (int) *( (uint8*)(b)   )  << 8) ) )

#define GET_BE32(b)  (   (uint32)   *(((uint8*)(b))+3)  \
                   | (  ((uint32)   *(((uint8*)(b))+2) ) << 8)  \
                   | (  ((uint32)   *(((uint8*)(b))+1) ) << 16) \
                   | (  ((uint32)   *( (uint8*)(b)   ) ) << 24) )


#define GET_16(b)    (     (int) *( (uint8*)(b)   )  \
                     | ( ( (int) *(((uint8*)(b))+1)  << 8) ) )

#define GET_32(b)    (   (uint32)   *( (uint8*)(b)   )  \
                   | (  ((uint32)   *(((uint8*)(b))+1) ) << 8)  \
                   | (  ((uint32)   *(((uint8*)(b))+2) ) << 16) \
                   | (  ((uint32)   *(((uint8*)(b))+3) ) << 24) )

#define MAX_U32    ((uint32)0xffffffffL)
#define MAX_U16    ((uint16)0xffff)

#define NWSERV   1
#define NCPSERV  2
#define NWCONN   3
#define NWCLIENT 4
#define NWBIND   5

#define NCP_BINDERY_FSERVER 0x0004

typedef struct {
  uint32 object_id;
  uint16 object_type;
  uint8  object_name[49];
  uint8  object_flags;
  uint8  object_security;
  uint8  object_has_prop;
} BINDERY_OBJECT;

typedef struct {
  uint8 value[128];
  uint8 more_flag;
  uint8 property_flag;
} NW_PROPERTY;


/* net.c */
extern char  *funcname;
extern char   prgpath[];

extern int   call_func_entry(int argc, char *argv[]);

/* tools.c */
extern void  clear_kb(void);
extern int   key_pressed(void);
extern int   ask_user(char *p, ...);
#define xfree(p)      x_x_xfree((char **)&(p))
extern  void x_x_xfree(char **p);
extern char  *xmalloc(uint  size);
extern char  *xcmalloc(uint size);

extern int   strmaxcpy(char *dest, char *source, int len);
extern char  *xadd_char(char *s, int c, int maxlen);
extern uint8 *upstr(uint8 *s);
extern void korrpath(char *s);
extern void get_path_fn(char *s, char *p, char *fn);

/* Shared DOS utility helpers.  Keep command frontends small so the
 * historical multicall net.exe can later be split into smaller groups. */
extern void  tool_put_word_lh(uint8 *p, uint16 v);
extern void  tool_put_dword_lh(uint8 *p, uint32 v);
extern uint16 tool_get_word_lh(uint8 *p);
extern uint32 tool_get_dword_lh(uint8 *p);
extern void  tool_put_word_hl(uint8 *p, uint16 v);
extern void  tool_put_dword_hl(uint8 *p, uint32 v);
extern uint16 tool_get_word_hl(uint8 *p);
extern uint32 tool_get_dword_hl(uint8 *p);
extern int   tool_copy_ncp22_name(uint8 *dst, char *src, uint8 *len_out);
extern int   tool_strsame(char *a, char *b);
extern int   tool_is_help_arg(char *s);
extern int   tool_is_option(char *s);
extern int   tool_is_files_option(char *s);
extern int   tool_is_subdirs_option(char *s);
extern int   tool_get_current_drive(void);
extern int   tool_current_dhandle(uint8 *connid, uint8 *dhandle);
extern int   tool_current_dhandle_only(uint8 *dhandle);
extern int   tool_current_prefix(char *out, int max);
extern int   tool_is_current_path(char *path);
extern int   tool_path_is_dir(char *path);
extern void  tool_upcopy(char *dst, char *src, int max);
extern void  tool_basename(char *dst, char *src, int max);
extern void  tool_header_path(char *out, char *path, int max);
extern int   tool_is_dot_dir(char *name);
extern void  tool_join_path(char *out, char *base, char *name, int max);
extern int   tool_has_wildcards(char *path);
extern void  tool_parent_path(char *dst, char *src, int max);
extern void  tool_parent_pattern(char *dir, char *pattern, char *path,
                                 int maxdir, int maxpat);

#define reb(s) deb((s)),leb((s))

extern void  deb(uint8 *s);
extern void  leb(uint8 *s);


#define add_char(s, c) xadd_char((s), (c), -1)

extern char *getglobenv(char *option);
extern int  putglobenv(char  *option);

#ifdef __WATCOMC__
/* Borland C compatibility wrappers implemented in tools.c. */
extern int  getcurdir(int drive, char *directory);
extern void setdisk(int drive);
#endif

/* NETCALLS */
#define DRIVE_ADD     1
#define DRIVE_INSERT  2
#define DRIVE_DELETE  3

typedef struct {
  uint8  drivenummer;   /* 0xff=last of list,  0xfe only DOSPATH */
  uint8  flags;         /* 0x80 = local drive */
  char   dospath[65];
  uint8  connid;
  uint8  dhandle;
} SEARCH_VECTOR_ENTRY;

typedef SEARCH_VECTOR_ENTRY  SEARCH_VECTOR[17];

extern int neterrno;

#define alloc_permanent_dir_handle(dhandle, path, drive, rights) \
  alloc_dir_handle(0x12, (dhandle), (path), (drive), (rights))

#define alloc_temp_dir_handle(dhandle, path, drive, rights) \
  alloc_dir_handle(0x13, (dhandle), (path), (drive), (rights))

extern int ipx_init(void);
extern int logout(void);
extern int redir_device_drive(int devicetyp, uint8 *devname, uint8 *remotename);
extern int list_redir(int index, int *devicetyp, uint8 *devname, uint8 *remotename);
extern int get_drive_info(uint8 drivenumber, uint8 *connid,
                          uint8 *dhandle, uint8 *statusflags);
extern int get_fs_name(int connid, char *name);

extern int alloc_dir_handle(int func, int dhandle, char *path,
                            int driveletter, uint8 *effrights);

extern int dealloc_dir_handle(int dhandle);

extern int get_dir_path(uint8 dhandle, char *path);
extern int get_volume_name(uint8 nr, char *name);

extern int get_search_drive_vektor(SEARCH_VECTOR_ENTRY *vec);
extern int set_search_drive_vektor(SEARCH_VECTOR_ENTRY *vec);

/********* ncp.h  ***********/
extern  int   ncp16_02_get_directory_entry(int dirhandle,
              uint8  *path,
              int    *sub_dir,
              uint8  *resultpath,
              uint32 *creattime,
              uint32 *owner_id);

extern int    ncp14_46_get_bindery_access_level(uint32 *obj_id);
extern int    ncp17_02_set_debug_level(int   module, int debuglevel);
extern int    ncp17_14_login_object_unencrypted(uint8 *objname, uint16 objtyp, uint8 *password);
extern int    ncp17_17_get_encryption_key(uint8 *key);
extern int    ncp17_18_keyed_object_login(uint8 *cryptkey, uint8 *objname, uint16 objtyp);
extern uint32 ncp17_35_get_bindery_object_id(uint8 *objname, uint16 objtyp);
extern int    ncp17_36_get_bindery_object_name(uint32 obj_id, uint8 *objname, uint16 *objtyp);
extern int    ncp17_40_change_password_unencrypted(uint8 *objname, uint16 objtyp, uint8 *password,
                                                       uint8 *newpassword);

extern int    ncp17_4b_keyed_change_password(uint8 *cryptkey, uint8 *objname, uint16 objtyp,
                               int passwx, uint8 *newpassword);

/* map.c */
extern int func_map   (int argc, char *argv[], int mode);
extern int func_path  (int argc, char *argv[], int mode);
extern void remove_nwpathes(void);

/* login.c */
extern int func_login  (int argc, char *argv[], int mode);
extern int func_logout (int argc, char *argv[], int mode);
extern int func_passwd (int argc, char *argv[], int mode);
extern int func_profile(int argc, char *argv[], int mode);
extern int func_cwd    (int argc, char *argv[], int mode);
extern int func_echo   (int argc, char *argv[], int mode);
extern int func_exec   (int argc, char *argv[], int mode);
extern int read_command_file(char *fstr);

/* slist.c */
extern int func_slist (int argc, char *argv[], int mode);
extern int func_whoami(int argc, char *argv[], int mode);
extern int func_ndir  (int argc, char *argv[], int mode);

/* nwdebug.c */
extern int func_debug (int argc, char *argv[], int mode);

/* nwtests.c */
extern int func_tests (int argc, char *argv[], int mode);

/* capture.c */
extern int func_capture(int argc, char *argv[], int mode);

/* flag.c */
extern int func_flag   (int argc, char *argv[], int mode);
extern int func_flagdir(int argc, char *argv[], int mode);
extern int func_grant (int argc, char *argv[], int mode);
extern int func_revoke(int argc, char *argv[], int mode);
extern int func_remove(int argc, char *argv[], int mode);
extern int func_rights (int argc, char *argv[], int mode);
extern int func_creator(int argc, char *argv[], int mode);
extern int func_ncopy (int argc, char *argv[], int mode);


extern int    ncp17_37_scan_bindery_object(uint32 last_id, uint16 objtyp, uint8 *pattern,
                        BINDERY_OBJECT *target);
extern int    ncp17_3d_read_property_value(uint16 objtyp, uint8 *objname, int segment,
                        uint8 *propname, NW_PROPERTY *target);

