/* Collection of code snippets by Arne Vajhøj */
/* (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) */
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

#include <ssdef.h>
#include <iodef.h>
#include <descrip.h>
#include <starlet.h>
#include <lib$routines.h>

struct ioctx
{
    int8_t *c;
    int16_t *iosb;
};

void gotit(struct ioctx *ctx)
{
    int32_t stat;
    if(ctx->iosb[0] != SS$_NORMAL) lib$signal(ctx->iosb[0]);
    printf("You entered: %c\n", *(ctx->c));
    stat = sys$wake(0, 0);
    if(stat != SS$_NORMAL) lib$signal(stat);
}

int main()
{
    $DESCRIPTOR(ttdesc, "TT:");
    int8_t c;
    int16_t chan, iosb[4];
    int32_t stat;
    struct ioctx ctx = { &c, iosb };
    stat = sys$assign(&ttdesc, &chan, 0, 0, 0);
    if(stat != SS$_NORMAL) lib$signal(stat);
    stat = sys$qio(0, chan, IO$_READVBLK|IO$M_NOECHO, iosb, gotit, &ctx, &c, 1, 0, 0, 0, 0);
    if(stat != SS$_NORMAL) lib$signal(stat);
    printf("Doing something\n");
    stat = sys$hiber();
    if(stat != SS$_NORMAL) lib$signal(stat);
    stat = sys$dassgn(chan);
    if(stat != SS$_NORMAL) lib$signal(stat);
    return 0;
}
