rpm  5.4.15
rpmficl.c
Go to the documentation of this file.
1 #include "system.h"
2 
3 #include <argv.h>
4 
5 #ifdef WITH_FICL
6 #include <ficl.h>
7 #endif
8 #define _RPMFICL_INTERNAL
9 #include "rpmficl.h"
10 
11 #include "debug.h"
12 
13 /*@unchecked@*/
15 
16 /*@unchecked@*/ /*@relnull@*/
18 
19 #if defined(WITH_FICL)
20 /* Capture stdout from FICL VM in a buffer. */
21 static void rpmficlTextOut(ficlCallback *callback, char *text)
22  /*@*/
23 {
24  rpmficl ficl = (rpmficl) callback->context;
25 
26  if (ficl->iob == NULL)
27  ficl->iob = rpmiobNew(0);
28  (void) rpmiobAppend(ficl->iob, text, 0);
29 }
30 #endif
31 
32 static void rpmficlFini(void * _ficl)
33  /*@globals fileSystem @*/
34  /*@modifies *_ficl, fileSystem @*/
35 {
36  rpmficl ficl = (rpmficl) _ficl;
37 
38 #if defined(WITH_FICL)
39  ficlSystem * sys = ficl->sys;
40  ficlSystemDestroy(sys);
41 #endif
42  ficl->sys = NULL;
43  ficl->vm = NULL;
44  (void) rpmiobFree(ficl->iob);
45  ficl->iob = NULL;
46 }
47 
48 /*@unchecked@*/ /*@only@*/ /*@null@*/
50 
52  /*@globals _rpmficlPool, fileSystem @*/
53  /*@modifies pool, _rpmficlPool, fileSystem @*/
54 {
55  rpmficl ficl;
56 
57  if (_rpmficlPool == NULL) {
58  _rpmficlPool = rpmioNewPool("ficl", sizeof(*ficl), -1, _rpmficl_debug,
59  NULL, NULL, rpmficlFini);
60  pool = _rpmficlPool;
61  }
62  return (rpmficl) rpmioGetPool(pool, sizeof(*ficl));
63 }
64 
65 static rpmficl rpmficlI(void)
66  /*@globals _rpmficlI @*/
67  /*@modifies _rpmficlI @*/
68 {
69  if (_rpmficlI == NULL)
70  _rpmficlI = rpmficlNew(NULL, 0);
71  return _rpmficlI;
72 }
73 
74 rpmficl rpmficlNew(char ** av, uint32_t flags)
75 {
76  rpmficl ficl =
77 #ifdef NOTYET
78  (flags & 0x80000000) ? rpmficlI() :
79 #endif
80  rpmficlGetPool(_rpmficlPool);
81 #if defined(WITH_FICL)
82  static const char * _av[] = { "rpmficl", NULL };
83  ficlSystemInformation fsi;
84  ficlSystem *sys;
85  ficlVm *vm;
86  int ac;
87  int xx;
88 
89  if (av == NULL) av = _av;
90  ac = argvCount((ARGV_t)av);
91 
92  ficlSystemInformationInitialize(&fsi);
93  fsi.context = (void *)ficl;
94  fsi.textOut = rpmficlTextOut;
95 
96  sys = ficlSystemCreate(&fsi);
97  ficl->sys = sys;
98  ficlSystemCompileExtras(sys);
99  vm = ficlSystemCreateVm(sys);
100  ficl->vm = vm;
101 
102  xx = ficlVmEvaluate(vm, ".ver .( " __DATE__ " ) cr quit");
103 
104 #ifdef NOTYET
105  if (ac > 1) {
106  char b[256];
107  sprintf(b, ".( loading %s ) cr load %s\n cr", av[1], av[1]);
108  xx = ficlVmEvaluate(vm, b);
109  }
110 #endif
111 
112  if (ficl->iob) {
113  const char * s = rpmiobStr(ficl->iob);
114 if (_rpmficl_debug && s && *s)
115 fprintf(stderr, "%s", s);
116  (void)rpmiobEmpty(ficl->iob);
117  }
118 #endif
119 
120  return rpmficlLink(ficl);
121 }
122 
123 rpmRC rpmficlRunFile(rpmficl ficl, const char * fn, const char ** resultp)
124 {
125  rpmRC rc = RPMRC_FAIL;
126 
127 if (_rpmficl_debug)
128 fprintf(stderr, "==> %s(%p,%s)\n", __FUNCTION__, ficl, fn);
129 
130  if (ficl == NULL) ficl = rpmficlI();
131 
132  if (fn != NULL) {
133 #if defined(WITH_FICL)
134  rc = RPMRC_OK;
135 #endif
136  }
137  return rc;
138 }
139 
140 rpmRC rpmficlRun(rpmficl ficl, const char * str, const char ** resultp)
141 {
142  rpmRC rc = RPMRC_FAIL;
143 
144 if (_rpmficl_debug)
145 fprintf(stderr, "==> %s(%p,%s)\n", __FUNCTION__, ficl, str);
146 
147  if (ficl == NULL) ficl = rpmficlI();
148 
149  if (str != NULL) {
150 #if defined(WITH_FICL)
151  ficlVm *vm = ficl->vm;
152  switch (ficlVmEvaluate(vm, (char *)str)) {
153  default:
154  break;
155  case FICL_VM_STATUS_OUT_OF_TEXT:
156  rc = RPMRC_OK;
157  if (resultp && ficl->iob)
158  *resultp = rpmiobStr(ficl->iob);
159  break;
160  }
161 #endif
162  }
163  return rc;
164 }
const bson * b
Definition: bson.h:280
rpmioPool _rpmficlPool
Definition: rpmficl.c:49
static rpmficl rpmficlGetPool(rpmioPool pool)
Definition: rpmficl.c:51
static void rpmficlFini(void *_ficl)
Definition: rpmficl.c:32
rpmficl rpmficlNew(char **av, uint32_t flags)
Create and load a ficl interpreter.
Definition: rpmficl.c:74
rpmiob rpmiobFree(rpmiob iob)
Destroy a I/O buffer instance.
rpmiob rpmiobAppend(rpmiob iob, const char *s, size_t nl)
Append string to I/O buffer.
Definition: rpmiob.c:77
const char * str
Definition: bson.h:593
rpmioItem rpmioGetPool(rpmioPool pool, size_t size)
Get unused item from pool, or alloc a new item.
Definition: rpmmalloc.c:220
int argvCount(const ARGV_t argv)
Return no.
Definition: argv.c:71
static rpmficl rpmficlI(void)
Definition: rpmficl.c:65
rpmRC rpmficlRun(rpmficl ficl, const char *str, const char **resultp)
Execute ficl string.
Definition: rpmficl.c:140
rpmficl rpmficlLink(rpmficl ficl)
Reference a ficl interpreter instance.
rpmficl _rpmficlI
Definition: rpmficl.c:17
rpmiob rpmiobNew(size_t len)
Create an I/O buffer.
Definition: rpmiob.c:44
const char const bson int mongo_write_concern int flags
Definition: mongo.h:485
enum rpmRC_e rpmRC
RPM return codes.
char * rpmiobStr(rpmiob iob)
Return I/O buffer (as string).
Definition: rpmiob.c:112
rpmioPool rpmioNewPool(const char *name, size_t size, int limit, int flags, char *(*dbg)(void *item), void(*init)(void *item), void(*fini)(void *item))
Create a memory pool.
Definition: rpmmalloc.c:109
rpmiob rpmiobEmpty(rpmiob iob)
Empty an I/O buffer.
Definition: rpmiob.c:57
struct rpmficl_s * rpmficl
Definition: rpmficl.h:11
ARGstr_t * ARGV_t
Definition: argv.h:12
int _rpmficl_debug
Definition: rpmficl.c:14
rpmRC rpmficlRunFile(rpmficl ficl, const char *fn, const char **resultp)
Execute ficl from a file.
Definition: rpmficl.c:123