another update from CVS HEAD, for QA
[alioth/jupp.git] / builtin.c
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
9 #include "config.h"
10
11 __RCSID("$MirOS: contrib/code/jupp/builtin.c,v 1.8 2017/12/08 02:00:38 tg Exp $");
12
13 #include <stdlib.h>
14 #include <string.h>
15
16 #include "types.h"
17 #include "builtin.h"
18 #include "utils.h"
19
20 #define zcmp(a,b) strcmp((char *)(a), (char *)(b))
21
22 JFILE *
23 jfopen(const unsigned char *name, const char *mode)
24 {
25         if (name[0] == '*') {
26                 int x;
27                 char *xname, *cp;
28
29                 xname = strdup((const char *)name + 1);
30                 cp = xname;
31                 while ((x = *cp++)) {
32                         if (x >= 'A' && x <= 'Z')
33                                 cp[-1] = x - 'A' + 'a';
34                 }
35
36                 for (x = 0; builtins[x]; x += 2) {
37                         if (!zcmp(builtins[x], xname)) {
38                                 JFILE *j = malloc(sizeof(JFILE));
39                                 j->f = 0;
40                                 j->p = builtins[x + 1];
41                                 free(xname);
42                                 return j;
43                         }
44                 }
45                 free(xname);
46                 return 0;
47         } else {
48                 FILE *f = fopen((const char *)name, (const char *)mode);
49                 if (f) {
50                         JFILE *j = malloc(sizeof(JFILE));
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);
65         free(f);
66         return rtn;
67 }
68
69 unsigned char *jfgets(unsigned char *buf,int len,JFILE *f)
70 {
71         if (f->f)
72                 return (unsigned char *)fgets((char *)buf, len, f->f);
73         else {
74                 if (f->p[0]) {
75                         int x;
76                         for (x = 0; f->p[x] && f->p[x] != '\n'; ++x)
77                                 buf[x] = f->p[x];
78                         if (f->p[x] == '\n') {
79                                 buf[x++] = '\n';
80                         }
81                         buf[x] = 0;
82                         f->p += x;
83                         return buf;
84                 } else
85                         return 0;
86         }
87 }