Dillo v3.1.1-98-g318d1f14
Loading...
Searching...
No Matches
chain.c
Go to the documentation of this file.
1/*
2 * Copyright 2001-2007 Jorge Arellano Cid <jcid@dillo.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 */
9
16#include "msg.h"
17#include "chain.h"
18#include "../dlib/dlib.h"
19
20#define VERBOSE 0
21
25#if VERBOSE
26static void Chain_debug_msg(char *FuncStr, int Op, int Branch, int Dir,
27 ChainLink *Info)
28{
29 const char *StrOps[] = {"", "OpStart", "OpSend",
30 "OpStop", "OpEnd", "OpAbort"};
31 MSG("%-*s: %-*s [%d%s] Info=%p Flags=%d\n",
32 12, FuncStr, 7, StrOps[Op], Branch, (Dir == 1) ? "F" : "B",
33 Info, Info ? Info->Flags : -1);
34}
35#else
36static void Chain_debug_msg(char *FuncStr, int Op, int Branch, int Dir,
37 ChainLink *Info) { }
38#endif
43{
44 return dNew0(ChainLink, 1);
45}
46
56 int Direction, ChainFunction_t BFunc,
57 int AtoB_branch, int BtoA_branch)
58{
59 ChainLink *NewLink = a_Chain_new();
60 ChainLink *OldLink = AInfo;
61
62 if (Direction == BCK) {
63 NewLink->Fcb = AFunc;
64 NewLink->FcbInfo = AInfo;
65 NewLink->FcbBranch = BtoA_branch;
66 OldLink->Bcb = BFunc;
67 OldLink->BcbInfo = NewLink;
68 OldLink->BcbBranch = AtoB_branch;
69
70 } else { /* FWD */
71 NewLink->Bcb = AFunc;
72 NewLink->BcbInfo = AInfo;
73 NewLink->BcbBranch = BtoA_branch;
74 OldLink->Fcb = BFunc;
75 OldLink->FcbInfo = NewLink;
76 OldLink->FcbBranch = AtoB_branch;
77 }
78
79 return NewLink;
80}
81
86void a_Chain_unlink(ChainLink *Info, int Direction)
87{
88 if (Direction == FWD) {
89 Info->Fcb = NULL;
90 Info->FcbInfo = NULL;
91 Info->FcbBranch = 0;
92 } else { /* BCK */
93 Info->Bcb = NULL;
94 Info->BcbInfo = NULL;
95 Info->BcbBranch = 0;
96 }
97}
98
103int a_Chain_fcb(int Op, ChainLink *Info, void *Data1, void *Data2)
104{
105 int ret = 0;
106
107 if (Info->Flags & (CCC_Ended + CCC_Aborted)) {
108 /* CCC is not operative */
109 } else if (Info->Fcb) {
110 /* flag the caller */
111 if (Op == OpEnd)
112 Info->Flags |= CCC_Ended;
113 else if (Op == OpAbort)
114 Info->Flags |= CCC_Aborted;
115
116 Info->Fcb(Op, Info->FcbBranch, FWD, Info->FcbInfo, Data1, Data2);
117 ret = 1;
118 }
119 return ret;
120}
121
126int a_Chain_bcb(int Op, ChainLink *Info, void *Data1, void *Data2)
127{
128 int ret = 0;
129
130 if (Info->Flags & (CCC_Ended + CCC_Aborted)) {
131 /* CCC is not operative */
132 } else if (Info->Bcb) {
133 /* flag the caller */
134 if (Op == OpEnd)
135 Info->Flags |= CCC_Ended;
136 else if (Op == OpAbort)
137 Info->Flags |= CCC_Aborted;
138
139 Info->Bcb(Op, Info->BcbBranch, BCK, Info->BcbInfo, Data1, Data2);
140 ret = 1;
141 }
142 return ret;
143}
144
150int a_Chain_bfcb(int Op, ChainLink *Info, void *Data1, void *Data2)
151{
152 int ret;
153
154 ret = a_Chain_bcb(Op, Info, Data1, Data2);
155 if (ret == 1) {
156 /* we need to clear the flag to reuse this 'Info' ChainLink */
157 if (Op == OpEnd)
158 Info->Flags &= ~CCC_Ended;
159 else if (Op == OpAbort)
160 Info->Flags &= ~CCC_Aborted;
161
162 ret = a_Chain_fcb(Op, Info, Data1, Data2);
163 }
164 return ret;
165}
166
167
171DataBuf *a_Chain_dbuf_new(void *buf, int size, int code)
172{
173 DataBuf *dbuf = dNew(DataBuf, 1);
174 dbuf->Buf = buf;
175 dbuf->Size = size;
176 dbuf->Code = code;
177 return dbuf;
178}
179
186int a_Chain_check(char *FuncStr, int Op, int Branch, int Dir,
187 ChainLink *Info)
188{
189 int ret = 0;
190
191 /* Show status information */
192 Chain_debug_msg(FuncStr, Op, Branch, Dir, Info);
193
194 if (Info->Flags & (CCC_Ended + CCC_Aborted)) {
195 /* CCC is not operative */
196 MSG_WARN("CCC: call on already finished chain. Flags=%s%s\n",
197 Info->Flags & CCC_Ended ? "CCC_Ended " : "",
198 Info->Flags & CCC_Aborted ? "CCC_Aborted" : "");
199 } else {
200 ret = 1;
201 }
202 return ret;
203}
204
#define MSG(...)
Definition bookmarks.c:46
static void Chain_debug_msg(char *FuncStr, int Op, int Branch, int Dir, ChainLink *Info)
Show debugging info.
Definition chain.c:36
int a_Chain_bcb(int Op, ChainLink *Info, void *Data1, void *Data2)
Issue the backward callback of the 'Info' link.
Definition chain.c:126
DataBuf * a_Chain_dbuf_new(void *buf, int size, int code)
Allocate and initialize a new DataBuf structure.
Definition chain.c:171
ChainLink * a_Chain_link_new(ChainLink *AInfo, ChainFunction_t AFunc, int Direction, ChainFunction_t BFunc, int AtoB_branch, int BtoA_branch)
Create a new link from module A to module B.
Definition chain.c:55
void a_Chain_unlink(ChainLink *Info, int Direction)
Unlink a previously used link.
Definition chain.c:86
int a_Chain_check(char *FuncStr, int Op, int Branch, int Dir, ChainLink *Info)
Check whether the CCC is operative.
Definition chain.c:186
int a_Chain_bfcb(int Op, ChainLink *Info, void *Data1, void *Data2)
Issue the backward callback of the 'Info' link and then the forward callback (used for OpAbort and Op...
Definition chain.c:150
int a_Chain_fcb(int Op, ChainLink *Info, void *Data1, void *Data2)
Issue the forward callback of the 'Info' link.
Definition chain.c:103
ChainLink * a_Chain_new(void)
Create and initialize a new chain-link.
Definition chain.c:42
#define OpAbort
Definition chain.h:17
#define CCC_Aborted
Definition chain.h:24
#define CCC_Ended
Definition chain.h:23
#define FWD
Definition chain.h:29
void(* ChainFunction_t)(int Op, int Branch, int Dir, ChainLink *Info, void *Data1, void *Data2)
Definition chain.h:33
#define BCK
Definition chain.h:30
#define OpEnd
Definition chain.h:16
#define dNew0(type, count)
Definition dlib.h:51
#define dNew(type, count)
Definition dlib.h:49
#define MSG_WARN(...)
Definition msg.h:26
A convenience data structure for passing data chunks between nodes.
Definition chain.h:52
char * Buf
Definition chain.h:53
int Code
Definition chain.h:55
int Size
Definition chain.h:54