summaryrefslogtreecommitdiffhomepage
path: root/tests/tools/csi.c
blob: 25333d83bdc523e15f5c2a35a1b3568853bfc66c (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
/**
 * @file csi.c
 * @brief Hand csi_parse() a Catalog Search Interface work area and check
 *        what it makes of each entry.
 *
 * tests/zos/csi-parse.sh runs this in both character sets.  The entry type
 * byte is the point: an ASCII build converts it from EBCDIC, an EBCDIC build
 * must use it as it is (notes/review-2026-11-27.md, item 12 -- an EBCDIC
 * build converted it a second time, so every entry arrived with a type that
 * was not its own).
 *
 * The result is printed as a verdict, not as the decoded text: the same words
 * then come out of an ASCII build and an EBCDIC build, and one expectation
 * serves both.
 *
 * The byte layout is the one src/mvsfmt.c documents above csi_parse(); the
 * constants here are offsets into that layout, not parameters.
 */
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>

#include "mvsfmt.h"

/** @brief Offsets in a CSI return work area (src/mvsfmt.c). */
enum {
    WORK = 4096,       /**< size passed to csi_parse() */
    USDLN = 8,         /**< +8: bytes of return information used */
    FIRST = 14,        /**< offset of the first entry */
    NAME = 2,          /**< name, within an entry */
    FIXED = 46,        /**< flags, type and name */
    RETINFO = 50,      /**< an entry with 4 bytes of return information */
    TOTAL = 12,        /**< CSITOTLN with one 6-byte field */
    VOLSER = 6,        /**< bytes of that field */
    EB_CATALOG = 0xf0, /**< type '0': a catalog entry, no name to list */
    EB_CLUSTER = 0xc1, /**< type 'A': a non-VSAM data set */
    EB_USER = 0xe4     /**< type 'U': a user catalog connector */
};

/** @brief Store a big-endian halfword. */
static void put16(unsigned char *p, unsigned v) {
    p[0] = (unsigned char)(v >> 8);
    p[1] = (unsigned char)v;
}

/** @brief Store a big-endian fullword. */
static void put32(unsigned char *p, unsigned long v) {
    p[0] = (unsigned char)(v >> 24);
    p[1] = (unsigned char)(v >> 16);
    p[2] = (unsigned char)(v >> 8);
    p[3] = (unsigned char)v;
}

/** @brief Store @p s blank-padded to @p n bytes, in EBCDIC. */
static void put_field(unsigned char *p, const char *s, size_t n) {
    size_t len = strlen(s) < n ? strlen(s) : n;
    memset(p, 0x40, n); /* EBCDIC blanks, not converted */
    memcpy(p, s, len);
    mvs_to_ebcdic((char *)p, len);
}

/**
 * @brief Append one entry with a VOLSER field.
 * @param work The work area.
 * @param off  Offset to write at.
 * @param type The EBCDIC CSI entry type byte.
 * @param name The data set name.
 * @return The offset after the entry.
 */
static size_t entry(unsigned char *work, size_t off, unsigned char type,
                    const char *name) {
    unsigned char *e = work + off;
    memset(e, 0, FIXED + TOTAL);
    e[1] = type;
    put_field(e + NAME, name, MVS_DSN_MAX);
    put16(e + FIXED, TOTAL);      /* CSITOTLN, counting itself */
    put16(e + FIXED + 2, 0);      /* reserved */
    put16(e + FIXED + 4, VOLSER); /* length of the one field */
    put_field(e + FIXED + 6, "TEST01", VOLSER);
    return off + FIXED + TOTAL;
}

/** @brief What the listed entries came out as. */
static int listed;
static bool name_ok = true;
static bool type_ok = true;

/** @brief Called once per entry csi_parse() accepts. */
static void check(const char *name, char type, void *arg) {
    (void)arg;
    listed++;
    if (strcmp(name, "SYS1.TEST.PDS") != 0)
        name_ok = false;
    if (type != 'A')
        type_ok = false;
}

int main(void) {
    static unsigned char work[WORK];
    size_t off = FIRST;
    /* A data set, a user catalog connector and a catalog entry: only the
     * first is a name to list. */
    off = entry(work, off, EB_CLUSTER, "SYS1.TEST.PDS");
    off = entry(work, off, EB_USER, "SYS1.UCAT");
    memset(work + off, 0, RETINFO);
    work[off + 1] = EB_CATALOG;
    put_field(work + off + NAME, "CATALOG.TEST.MASTER", MVS_DSN_MAX);
    off += RETINFO;
    put32(work + USDLN, (unsigned long)off);
    if (!csi_parse(work, WORK, check, NULL)) {
        puts("csi_parse: malformed");
        return 1;
    }
    printf("%d listed, type %s, name %s\n", listed, type_ok ? "ok" : "WRONG",
           name_ok ? "ok" : "WRONG");
    return listed == 1 && type_ok && name_ok ? 0 : 1;
}