/*static char *SCCSID = "@(#)inidinc.c	6.2 91/11/01";*/
#define SCCSID "@(#)inidinc.c	6.2 91/11/01"
#define FNO_NUM FNO_INIDINC_C
/****************************************************************************/
/*                                                                          */
/*                       IBM/Microsoft Confidential                         */
/*                                                                          */
/*                 Copyright (c) IBM Corporation  1987, 1990                */
/*                 Copyright (c) Microsoft Corp.  1987, 1990                */
/*                           All Rights Reserved                            */
/*                                                                          */
/****************************************************************************/

#include <os2.h>

#define INCL_DOSP
#include <os2p.h>

#include <fnoinit.h>
#include <osmac.h>
#include <stdarg.h>
#include <mi.h>
#include <pgexport.h>
#include <vmexport.h>
#include <vmowner.h>
#include <selxport.h>
#include <syiexp.h>
#include <inisms.h>

extern void SYISaveMessageSub(struct FarPtr);

#pragma code_seg("CINIT_TEXT","CODE")
void cdecl SYISaveMessage(idMsg, cStr, ...)
ushort_t        idMsg;
int             cStr;
{
    va_list     pszSubst;
    int         i;
    struct SMT  *pSMT;
    struct FarPtr *pFP, FP, SMT;
    char        *pszSrc;
    VMAC        vmac;
    unsigned    err;
    ulong_t     *aVA;

    /*
     * This should be done as
     *          va_start(pszSubst,cStr);
     * except that stdarg doesn't know about SSToDS
     */
    pszSubst = (va_list)SSToDS(&cStr) + sizeof(cStr);
    err = VMAllocMem(sizeof(*pSMT) + sizeof(ulong_t)*cStr,
                     sizeof(*pSMT) + sizeof(ulong_t)*cStr,
                     VMAC_LOCANY | VMAC_ARENASYS | VMAC_SELALLOC |
                     PG_RESIDENT | PG_R | PG_W | PG_ZEROFILL,
                     HOBNULL, INITOWNER, HOBNULL, NA, NA,
                     SSToDS(&vmac));
    pSMT = (struct SMT *)vmac.ac_va;
    SMT.Segmt = vmac.ac_sel;
    SMT.Offst = 0;
    err = VMAllocKHB(VM_HKH_PUB_RESRW, sizeof(ulong_t) * cStr, INITMSGOWNER,
        NA, SSToDS(&aVA));
    pSMT->SMT_Message_ID = idMsg;
    pSMT->SMT_No_Vars = (ushort_t)cStr;

    pFP = (struct FarPtr *) (pSMT + 1);

    FP.Offst = 0;

    for (i=0; i<cStr; i++) {
        pszSrc = va_arg(pszSubst, char *);
        err = VMMapAlias(VMAC_ARENASYS | VMAC_SELALLOC | VMAC_LOCANY |
            PG_R | PG_W,
            SELAL_SELMAP | SELAL_R,
            0, 0, HOBNULL,
            (ulong_t)pszSrc & ~PAGEMASK,
            HOBNULL,
            2*PAGESIZE,
            SSToDS(&vmac));
        FP.Segmt = vmac.ac_sel;
        FP.Offst = (ushort_t)((ulong_t)pszSrc & PAGEMASK);
        aVA[i] = vmac.ac_va;
        *pFP++ = FP;
    }

    SYISaveMessageSub(SMT);

    for (i=0; i<cStr; i++)
        VMFreeMem(aVA[i], HOBNULL, NA);
    VMFreeMem((ulong_t)pSMT, HOBNULL, NA);
    VMFreeKHB(VM_HKH_PUB_RESRW, aVA);
}
