~mirabilos/jupp/trunk

120 by tg
add built-in (minimal) jupprc, for stand-alone things
1
/*
2
 *	Built-in config files
3
 *	Copyright
4
 *		(C) 2006 Joseph H. Allen
5
 *
6
 *	This file is part of JOE (Joe's Own Editor)
7
 */
8
325 by tg
move a few includes so that _FILE_OFFSET_BITS is always correctly defined,
9
#include "config.h"
10
596 by tg
this is ugly enough, and jfgets mirrors fgets which gets an int ipv size_t,
11
__RCSID("$MirOS: contrib/code/jupp/builtin.c,v 1.10 2018/01/08 00:48:06 tg Exp $");
473 by tg
overhaul the way includes work; give jupp proper RCS IDs
12
392 by tg
these now all need <stdlib.h>
13
#include <stdlib.h>
120 by tg
add built-in (minimal) jupprc, for stand-alone things
14
#include <string.h>
325 by tg
move a few includes so that _FILE_OFFSET_BITS is always correctly defined,
15
120 by tg
add built-in (minimal) jupprc, for stand-alone things
16
#include "types.h"
17
#include "builtin.h"
18
#include "utils.h"
19
20
#define zcmp(a,b) strcmp((char *)(a), (char *)(b))
21
521 by tg
lots of fixes related to prototyping
22
JFILE *
23
jfopen(const unsigned char *name, const char *mode)
120 by tg
add built-in (minimal) jupprc, for stand-alone things
24
{
25
	if (name[0] == '*') {
26
		int x;
521 by tg
lots of fixes related to prototyping
27
		char *xname, *cp;
230 by tg
let it find *JUPP32rc on Natureshadow’s Windows® 98…
28
521 by tg
lots of fixes related to prototyping
29
		xname = strdup((const char *)name + 1);
30
		cp = xname;
31
		while ((x = *cp++)) {
230 by tg
let it find *JUPP32rc on Natureshadow’s Windows® 98…
32
			if (x >= 'A' && x <= 'Z')
521 by tg
lots of fixes related to prototyping
33
				cp[-1] = x - 'A' + 'a';
230 by tg
let it find *JUPP32rc on Natureshadow’s Windows® 98…
34
		}
35
120 by tg
add built-in (minimal) jupprc, for stand-alone things
36
		for (x = 0; builtins[x]; x += 2) {
230 by tg
let it find *JUPP32rc on Natureshadow’s Windows® 98…
37
			if (!zcmp(builtins[x], xname)) {
539 by tg
don’t box malloc/calloc/realloc/free; don’t cast malloc result; order calloc args
38
				JFILE *j = malloc(sizeof(JFILE));
120 by tg
add built-in (minimal) jupprc, for stand-alone things
39
				j->f = 0;
40
				j->p = builtins[x + 1];
539 by tg
don’t box malloc/calloc/realloc/free; don’t cast malloc result; order calloc args
41
				free(xname);
120 by tg
add built-in (minimal) jupprc, for stand-alone things
42
				return j;
43
			}
44
		}
539 by tg
don’t box malloc/calloc/realloc/free; don’t cast malloc result; order calloc args
45
		free(xname);
120 by tg
add built-in (minimal) jupprc, for stand-alone things
46
		return 0;
47
	} else {
521 by tg
lots of fixes related to prototyping
48
		FILE *f = fopen((const char *)name, (const char *)mode);
120 by tg
add built-in (minimal) jupprc, for stand-alone things
49
		if (f) {
539 by tg
don’t box malloc/calloc/realloc/free; don’t cast malloc result; order calloc args
50
			JFILE *j = malloc(sizeof(JFILE));
120 by tg
add built-in (minimal) jupprc, for stand-alone things
51
			j->f = f;
52
			j->p = 0;
53
			return j;
54
		} else {
55
			return 0;
56
		}
57
	}
58
}
59
60
int jfclose(JFILE *f)
61
{
62
	int rtn = 0;
63
	if (f->f)
64
		rtn = fclose(f->f);
539 by tg
don’t box malloc/calloc/realloc/free; don’t cast malloc result; order calloc args
65
	free(f);
120 by tg
add built-in (minimal) jupprc, for stand-alone things
66
	return rtn;
67
}
68
596 by tg
this is ugly enough, and jfgets mirrors fgets which gets an int ipv size_t,
69
/*XXX fails to honour len (= 1024, in practice) for builtins */
584 by tg
shocking truths
70
unsigned char *
71
jfgets(unsigned char *buf, int len, JFILE *f)
120 by tg
add built-in (minimal) jupprc, for stand-alone things
72
{
73
	if (f->f)
74
		return (unsigned char *)fgets((char *)buf, len, f->f);
75
	else {
76
		if (f->p[0]) {
77
			int x;
78
			for (x = 0; f->p[x] && f->p[x] != '\n'; ++x)
79
				buf[x] = f->p[x];
80
			if (f->p[x] == '\n') {
81
				buf[x++] = '\n';
82
			}
83
			buf[x] = 0;
84
			f->p += x;
85
			return buf;
86
		} else
87
			return 0;
88
	}
89
}