/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "Licence").
 * You may not use this file except in compliance with the Licence.
 *
 * You can obtain a copy of the licence at RISC OS path @.^.LICENCE
 * or  http://www.riscosdev.com/lanman98/LICENCE.CDDL
 * See the Licence for the specific language governing permissions
 * and limitations under the Licence.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the Licence file. If applicable, add the
 * following below this CDDL HEADER, with the fields enclosed by
 * brackets "[]" replaced with your own identifying information:
 * Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
 
/*
 *   Portions Copyright 1996 Warm Silence Software Ltd.  All rights reserved.
 *   Use is subject to license terms.
 */

#include "kernel.h"
#include "swis.h"
#include <string.h>
#include <stdlib.h>
#include "error.h"
#include "var.h"

char *VarRead(char *var)
{
    _kernel_swi_regs r;
    int len;
    char *buf = NULL;

    r.r[0] = (int) var;
    r.r[1] = 0;
    r.r[2] = -1;
    r.r[3] = 0;
    r.r[4] = 3;
    _kernel_swi(OS_ReadVarVal, &r, &r);
    if(r.r[2] == 0)
        return NULL;
    len = ~(r.r[2]);
    buf = Malloc(len + 1);
    r.r[0] = (int) var;
    r.r[1] = (int) buf;
    r.r[2] = len + 1;
    r.r[3] = 0;
    r.r[4] = 3;
    _kernel_swi(OS_ReadVarVal, &r, &r);
    buf[len] = 0;
    return buf;
}

void VarSet(char *var, char *str)
{
    _kernel_swi_regs r;

    r.r[0] = (int) var;
    r.r[1] = (int) str;
    r.r[2] = str ?strlen(str) : -1;
    r.r[3] = 0;
    r.r[4] = 0;
    _kernel_swi(OS_SetVarVal, &r, &r);
}
