File: internals.c

package info (click to toggle)
chpp 0.3.4-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 3,228 kB
  • ctags: 5,154
  • sloc: ansic: 30,186; cpp: 575; sh: 500; makefile: 314; yacc: 265; asm: 261; lex: 64
file content (215 lines) | stat: -rw-r--r-- 4,933 bytes parent folder | download | duplicates (3)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/* -*- c -*- */

/*
 * internals.c
 *
 * chpp
 *
 * Copyright (C) 1997-1998 Mark Probst
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "dynstring.h"
#include "error.h"
#include "main.h"
#include "bytecode.h"
#include "value.h"
#include "environment.h"
#include "builtins/builtins.h"
#include "output.h"

extern int generateDependencies;
extern dynstring mainFileName;

char topLevelMetaChar = '%';

char *metaChar = &topLevelMetaChar;
char quoteChar = '\\';

extern char **environ;

void
internalSetMetaChar (value *val, int needCopy, bcSubscript *subscripts, environment *env)
{
    assert(subscripts == 0);
    assert(val->type == VALUE_SCALAR && val->v.scalar.scalar.length == 1);

    *metaChar = val->v.scalar.scalar.data[0];
}

void
internalGetMetaChar (bcSubscript *subscripts, environment *env, outputWriter *ow)
{
    assert(subscripts == 0);

    OUT_CHAR(ow, *metaChar);
}

void
internalSetQuoteChar (value *val, int needCopy, bcSubscript *subscripts, environment *env)
{
    assert(subscripts == 0);
    assert(val->type == VALUE_SCALAR && val->v.scalar.scalar.length == 1);

    quoteChar = val->v.scalar.scalar.data[0];
}

void
internalGetQuoteChar (bcSubscript *subscripts, environment *env, outputWriter *ow)
{
    assert(subscripts == 0);

    OUT_CHAR(ow, quoteChar);
}

void
internalSetOutputenabled (value *val, int needCopy, bcSubscript *subscripts, environment *env)
{
    assert(subscripts == 0);

    toplevelOutputWriter.enabled = valueBoolValue(val);
}

void
internalGetOutputenabled (bcSubscript *subscripts, environment *env, outputWriter *ow)
{
    assert(subscripts == 0);

    if (toplevelOutputWriter.enabled)
    {
	OUT_CHAR(ow, '1');
    }
    else
    {
	OUT_CHAR(ow, '0');
    }
}

void
internalGetDependencing (bcSubscript *subscripts, environment *env, outputWriter *ow)
{
    assert(subscripts == 0);

    if (generateDependencies)
    {
	OUT_CHAR(ow, '1');
    }
    else
    {
	OUT_CHAR(ow, '0');
    }
}

void
internalGetMainfilename (bcSubscript *subscripts, environment *env, outputWriter *ow)
{
    assert(subscripts == 0);

    OUT_STRING(ow, mainFileName.data, mainFileName.length);
}

void
internalGetEnv (bcSubscript *subscripts, environment *env, outputWriter *ow)
{
    if (subscripts == 0)
    {
	int i;
	value *theHash = valueNewHash();

	for (i = 0; environ[i] != 0; ++i)
	{
	    char *pos = strchr(environ[i], '=');
	    dynstring name = dsNewFromBytes(environ[i], pos - environ[i]);

	    valueHashDefine(theHash, &name, valueNewScalarFromCString(pos + 1));
	}

	OUT_VALUE_REF(ow, theHash);
    }
    else
    {
	dynstring index;
	char *result;

	assert(subscripts->type == BYTECODE_SUBSCRIPT_HASH
	       && subscripts->next == 0);

	index = bcExecuteIntoDS(subscripts->bc, env);

	result = getenv(index.data);

	if (result == 0)
	    issueError(ERRMAC_KEY_NOT_IN_HASH, index.data);
	else
	    OUT_VALUE_REF(ow, valueNewScalarFromCString(result));
    }
}

void
internalGetMan (bcSubscript *subscripts, environment *env, outputWriter *ow)
{
    static char *manString =
	"  O  \n"
	"  |  \n"
	" --- \n"
	"/ | \\\n"
	"  |  \n"
	" /'\\ \n"
	"/   \\\n";

    OUT_STRING(ow, manString, strlen(manString));
}

void
internalGetWoman (bcSubscript *subscripts, environment *env, outputWriter *ow)
{
    static char *womanString =
	"  O  \n"
	"  |  \n"
	" --- \n"
	"/o|o\\\n"
	"  |  \n"
	" / \\ \n"
	"/   \\\n";

    OUT_STRING(ow, womanString, strlen(womanString));
}

void
registerInternal (const char *name, internalSet setFunc, internalGet getFunc)
{
    dynstring nameString = dsNewFrom(name);

    envAddBinding(0, &nameString, valueNewInternal(setFunc, getFunc));
}

void
registerInternals (void)
{
    registerInternal("v_metachar", internalSetMetaChar, internalGetMetaChar);
    registerInternal("v_quotechar", internalSetQuoteChar, internalGetQuoteChar);
    registerInternal("outputenabled", internalSetOutputenabled, internalGetOutputenabled);
    registerInternal("dependencing", 0, internalGetDependencing);
    registerInternal("mainfilename", 0, internalGetMainfilename);
    registerInternal("env", 0, internalGetEnv);
    registerInternal("man", 0, internalGetMan);
    registerInternal("woman", 0, internalGetWoman);
}