/* Copyright 1998 Acorn Computers Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <stdlib.h>
#include <stdio.h>

#define Throwback_Start       0x42587
#define Throwback_Send        0x42588
#define Throwback_End         0x42589

#include "swis.h"

#include "throwback.h"

#define Throwback_ReasonProcessing   0
#define Throwback_ReasonErrorDetails 1

static int throwback_id;
static int sent_procfile_msg;

static void throwback_exit(void)
{
    if (throwback_id >= 0)
        _swix(Throwback_End, _IN(0), throwback_id);
}

static void throwback_start(void)
{
    atexit(throwback_exit);
    if (!_swix(Throwback_Start, 0))
        throwback_id = 1;
}

void throwback_send(int severity, int line, const char *msg, const char *sourcefile, const char *current)
{
    if (!sent_procfile_msg) {
        throwback_start();
        sent_procfile_msg = 1;
        if (throwback_id >= 0) {
            _swix(Throwback_Send, _INR(0,2),
                  Throwback_ReasonProcessing, throwback_id, sourcefile);
        }
    }
    if (throwback_id >= 0) {
        _swix(Throwback_Send, _INR(0,5),
              Throwback_ReasonErrorDetails, throwback_id, current,
              line, severity, msg);
    }
}

const char *dependfilename;

void add_dependency(const char *target, const char *filename)
{
    static FILE *dependfile;

    if (!dependfile)
    {
        dependfile = fopen(dependfilename, "w");
        if (!dependfile) return;
    }


    fprintf(dependfile, "%s: %s\n", target, filename);
}