summaryrefslogtreecommitdiffhomepage
path: root/src/sys.h
blob: 72b1fd85ebe24bdafe7d60f0a5d6116564b89696 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/**
 * @file sys.h
 * @brief The platform layer.  sys_posix.c and sys_zos.c implement it. No other
 * file contains platform-specific code.
 */
#ifndef ZASH_SYS_H
#define ZASH_SYS_H

#include <signal.h>

#include "mvsfmt.h"
#include "shell.h"

/**
 * @brief Descriptors 0..SYS_NFD-1 are the script's. The shell's own are above.
 */
#define SYS_NFD 10
/**
 * @brief In an fd map: the child's descriptor is to be closed.
 */
#define FD_CLOSED (-1)

/**
 * @brief Everything needed to create a process.
 */
typedef struct spawn_req {
    const char *path;
    char *const *argv;
    char *const *envp;
    /** Child descriptor i is the parent's fdmap[i], or FD_CLOSED. */
    const int *fdmap;
    /** Bit i set: parent descriptor i is open (for identity entries). */
    unsigned open_mask;
    /** Signals to reset to default in the child, or NULL. */
    const sigset_t *sigdfl;
    /** Process group for the child: -1 to inherit the shell's, 0 to start
     * a new group, otherwise the group to join.  (Job control.) */
    pid_t pgroup;
    /** When not -1: the terminal to make the child's group the foreground
     * group of, as part of creating the child.  (Job control.) */
    int tty_fd;
} spawn_req;

/* Create a process running `path`.  Returns its pid, or -1 with errno. */
pid_t sys_spawn(const spawn_req *rq);
bool sys_spawn_gives_tty(void);

/* Like pipe(), dup-above-min and open(), with close-on-exec set. */
int sys_pipe(int fd[2]);
int sys_dupfd(int fd, int min);
int sys_open(const char *path, int flags, mode_t mode);

/* Returns the mask of descriptors 0..SYS_NFD-1 open at startup. */
unsigned sys_init(void);

/* Description of a signal, as strsignal() (which z/OS lacks). */
const char *sys_strsignal(int sig);

/**
 * @brief 1 when built for z/OS (file tags, MVS services), else 0.
 *
 * A compile-time constant, so that on other systems the calls guarded by
 * it are removed entirely.
 */
#if defined(__MVS__) || defined(ZASH_LINT_MVS) /* lint: make zos-lint */
#define SYS_MVS 1
#else
#define SYS_MVS 0
#endif

/* ---- file tags and automatic conversion (z/OS) ------------------------ */

/**
 * @brief What a `_TAG_REDIR_*` variable asks for.
 */
typedef enum tag_mode {
    TAG_UNSET, /**< variable not set: the default behaviour */
    TAG_TXT,   /**< treat or tag as text */
    TAG_BIN    /**< treat or tag as binary */
} tag_mode;

/**
 * @brief Which standard stream a redirection is for.
 */
typedef enum tag_stream { TAG_IN, TAG_OUT, TAG_ERR } tag_stream;

/**
 * @brief The cached values of `_TAG_REDIR_IN`, `_OUT` and `_ERR`,
 *        indexed by tag_stream.  Maintained by variable hooks, so the
 *        redirection code tests a byte and never searches the environment.
 */
extern tag_mode tag_redir[3];

tag_mode tag_value(const char *v);

/**
 * @brief Does this platform have file tags at all?
 *
 * False on Linux, where the calls below do nothing and are never made.
 */
bool sys_has_tags(void);

void sys_redir_tag(int fd, tag_stream stream, tag_mode mode, bool created);
void sys_tmp_tag(int fd);
void sys_input_cvt(int fd);

/**
 * @brief A file's tag, as `tag` and `chtag -p` show it.
 */
typedef struct sys_tag {
    int ccsid;        /**< 0: untagged; 65535: binary */
    bool text;        /**< the text flag */
    char codeset[32]; /**< name of the CCSID, or "" */
} sys_tag;

int sys_tag_get(const char *path, sys_tag *tag);
int sys_tag_set(const char *path, const char *codeset, int text);

/* ---- MVS services (z/OS) --------------------------------------------- */

/**
 * @brief Facts about the system, for the ZOS_* variables and the prompt.
 */
typedef enum sys_info_kind {
    SI_SYSNAME, /**< system (LPAR) name */
    SI_SYSPLEX, /**< sysplex name */
    SI_RELEASE, /**< z/OS version and release, "03.02" */
    SI_ASID,    /**< address space ID of the shell, 4 hex digits */
    SI_JOBNAME  /**< job name of the shell's address space */
} sys_info_kind;

const char *sys_info(sys_info_kind kind);

bool sys_has_catalog(void);
bool sys_catalog_list(const char *filter, mvs_name_fn *fn, void *arg);
bool sys_pds_members(const char *dsn, mvs_name_fn *fn, void *arg);
bool sys_dd_names(mvs_name_fn *fn, void *arg);
int sys_dynalloc(const char *request, char *msg, size_t msglen);

#endif