summaryrefslogtreecommitdiff
path: root/common/repfile.c
blob: eec6ca20eac240805d6d35b0083a022276ca6ba1 (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
/*
 * AUTHOR
 * N. Nielsen
 *
 * LICENSE
 * This software is in the public domain.
 *
 * The software is provided "as is", without warranty of any kind,
 * express or implied, including but not limited to the warranties
 * of merchantability, fitness for a particular purpose, and
 * noninfringement. In no event shall the author(s) be liable for any
 * claim, damages, or other liability, whether in an action of
 * contract, tort, or otherwise, arising from, out of, or in connection
 * with the software or the use or other dealings in the software.
 *
 * SUPPORT
 * Send bug reports to: <nielsen@memberwebs.com>
 */

#include <stdlib.h>
#include <string.h>
#include "common/usuals.h"
#include "common/compat.h"
#include "common/binfile.h"
#include "common/repfile.h"

/* errmsg: -----------------------------------------------------------
 *  Display an rlib error on the console
 */
int errmsg(int ret, r_script* script)
{
	const char* msg;

	switch(ret)
	{
	case R_NOMEM:
		msg = "out of memory";
		break;
	case R_SYNTAX:
		msg = "syntax error:";
		break;
	case R_REGEXP:
		msg = "reg expression error:";
		break;
	case R_LOOP:
		msg = "an endless loop was encountered ";
		break;
	case R_USER:
		msg = "stop: ";
		break;
	case R_IOERR:
		msg = "read or write error";
		break;
	case R_INVARG:
		ASSERT(0 && "This error should be taken care of by programmer");
		msg = "internal programmer error";
		break;
	default:
		msg = "unknown error";
		break;
	}

	if(script && script->error)
	{
		if(script->errline > 0)
			warnx("%s %s (at line %d)", msg, script->error, script->errline);
		else
			warnx("%s %s", msg, script->error);
	}
	else
	{
		warnx(msg);
	}

	return ret;
}

/* repscriptheader: -------------------------------------------------
 *  The top of every compiled rep file has this signature
 */
typedef struct _repscriptheader
{
	uint sig;			/* 'rep0' */
	uint version;		/* Note this is the file version */
}
repscriptheader;

const uint kReplSig = '0per';
const uint kReplVer = 0x00023100;

/* repfWriteHeader: -------------------------------------------------
 *  Write out a valid header
 */
bool repfWriteHeader(BFILE h)
{
	repscriptheader head;
	head.sig = kReplSig;
	head.version = kReplVer;

	bfWriteRaw(h, &head, sizeof(head));
	return !bfError(h);
}

/* repfReadHeader: --------------------------------------------------
 *  Read and validate rep file header
 */
bool repfReadHeader(BFILE h)
{
	repscriptheader head;
	memset(&head, 0, sizeof(head));
	bfReadRaw(h, &head, sizeof(head));
	return head.sig == kReplSig && head.version == kReplVer;
}