1 static char *rcsid = "$Id: Misc.c,v 1.1 1992/05/18 21:43:03 tvr Exp $";
6 void displaypage(char *topaddress, HText_t * parenthtext, HText_t * htext,
8 int addresscmp(char *addr1, char *addr2);
11 Connection_t *Connections = (Connection_t *) NULL;
12 Page_t *Pages = (Page_t *) NULL;
15 Page_t *FindPage(hierarchy, address)
19 while (address && hierarchy)
20 if (!addresscmp(hierarchy->Address, address))
23 hierarchy = hierarchy->Next;
25 return (Page_t *) NULL;
30 GlobalFindPage(address)
33 Page_t *hierarchy = Pages, *tmppage;
35 while (address && hierarchy)
36 if (tmppage = FindPage(hierarchy->Children, address))
39 hierarchy = hierarchy->Next;
41 return (Page_t *) NULL;
46 AddPage(page, address, htext, toppage)
54 page = &(*page)->Next;
55 (*page)->Next = (Page_t *) Malloc(sizeof(**page));
56 page = &(*page)->Next;
58 *page = (Page_t *) Malloc(sizeof(**page));
60 (*page)->Address = strdup(address);
61 (*page)->HText = htext;
62 (*page)->ParentPage = toppage;
63 (*page)->Parents = (Page_t *) NULL;
64 (*page)->Children = (Page_t *) NULL;
65 (*page)->Next = (Page_t *) NULL;
71 void DeletePage(page, address)
75 Page_t *oldpage = *page;
77 if (!strcmp((*page)->Address, address)) {
78 *page = (*page)->Next;
82 if (!strcmp((*page)->Address, address)) {
83 *page = (*page)->Next;
86 page = &(*page)->Next;
92 void DisplayWarning(text)
95 fprintf(stderr, "Warning: %s\n", text);
99 void DisplayFatal(text)
102 fprintf(stderr, "Fatal: %s\n", text);
108 int CanBeCursor(htextobject)
109 HTextObject_t *htextobject;
113 if (htextobject->paragraph)
116 for (i = 0; i < htextobject->length; i++)
117 if (htextobject->data[i] != ' ')
130 if (!(tmpptr = (void *) malloc(size))) {
131 DisplayFatal("No swap, buy a computer");
147 else if (!(tmpptr = (void *) realloc((char *) ptr, size))) {
148 DisplayFatal("No swap, buy a computer");
163 void StartLoading(address, topaddress, parentaddress)
168 Page_t *oldpage, *tmppage, *toppage, *parentpage;
170 ClConnection_t *clconnection;
171 Connection_t *connection;
173 toppage = FindPage(Pages, topaddress);
174 parentpage = (toppage && parentaddress) ?
175 FindPage(toppage->Children, parentaddress) : (Page_t *) NULL;
177 if (toppage && (oldpage = GlobalFindPage(address))) {
178 if (tmppage = FindPage(toppage->Children, address)) {
179 if (tmppage->HText) {
181 if (parentaddress && strcmp(parentaddress, address) &&
182 !FindPage(tmppage->Parents, parentaddress))
183 AddPage(&tmppage->Parents, parentaddress, tmppage->HText,
185 /* display the page */
186 displaypage(toppage ? toppage->Address : (char *) NULL,
187 parentpage ? parentpage->HText : (HText_t *) NULL,
188 tmppage->HText, address);
193 if (oldpage->HText) {
196 newhtext = HtDuplicate(oldpage->HText);
198 tmppage = AddPage(&toppage->Children, address, newhtext,
201 if (parentaddress && strcmp(parentaddress, address) &&
202 !FindPage(tmppage->Parents, parentaddress))
203 AddPage(&tmppage->Parents, parentaddress, tmppage->HText,
205 /* display the page */
206 displaypage(toppage ? toppage->Address : (char *) NULL,
207 parentpage ? parentpage->HText : (HText_t *) NULL,
212 HTAnchor_setDocument((void *) oldpage->HText->node_anchor,
218 if (FindConnection(address))
221 if (!(clconnection = ClOpenConnection(address))) {
222 DisplayWarning("Error in ClOpenConnection");
225 connection = AddConnection(address, toppage, parentpage, clconnection);
227 PollConnection(connection);
231 void PollConnection(connection)
232 Connection_t *connection;
236 int oldfd = connection->FD;
237 char *topaddress, *parentaddress;
238 Page_t *tmppage, *toppage, *parentpage;
240 htext = ClReadData(connection->ClConnection, &status, &connection->FD);
244 if (connection->FD && connection->Status == POLLING) {
245 connection->InputId =
246 UiAddInputFD(connection->FD,
247 (void (*) (void *)) &PollConnection,
248 (void *) connection);
249 connection->Status = SELECTING;
250 } else if (!connection->FD) {
252 UiDeleteInputFD(connection->InputId);
253 connection->TimeOutId =
254 UiAddTimeOut(200, (void (*) (void *)) &PollConnection,
255 (void *) connection);
256 connection->Status = POLLING;
260 if (connection->Status == SELECTING)
261 UiDeleteInputFD(connection->InputId);
262 if (htext && !connection->TopPage->HText)
263 connection->TopPage->HText = htext;
266 parentaddress = connection->ParentPage ?
267 connection->ParentPage->Address : (char *) NULL;
268 toppage = connection->TopPage;
270 parentpage = connection->ParentPage;
271 if (!(tmppage = FindPage(toppage->Children, connection->Address))) {
272 tmppage = AddPage(&toppage->Children, connection->Address,
274 /* add the parent (what the fuck have I been thinkin' here?) */
275 if (connection->ParentPage && parentaddress &&
276 !FindPage(tmppage->Parents, parentaddress))
277 AddPage(&tmppage->Parents, parentaddress,
278 tmppage->HText, parentpage);
280 tmppage->HText = htext;
282 displaypage(connection->TopPage ?
283 connection->TopPage->Address : (char *) NULL,
284 connection->ParentPage ?
285 connection->ParentPage->HText : (HText_t *) NULL,
286 htext, connection->Address);
288 DeleteConnection(connection->Address);
291 if (connection->Status == SELECTING)
292 UiDeleteInputFD(connection->InputId);
293 DeleteConnection(connection->Address);
296 DisplayWarning("Uh? :O");
301 void displaypage(topaddress, parenthtext, htext, address)
303 HText_t *parenthtext;
307 HTextAnchor_t *htanchor = (HTextAnchor_t *) NULL;
308 HTextObject_t *htextobject;
309 char *tag = HTParse(address, "", PARSE_ANCHOR);
312 htanchor = htext->anchorlist;
314 if (htanchor->anchor && htanchor->anchor->tag &&
315 !strcmp(htanchor->anchor->tag, tag))
318 htanchor = htanchor->next;
321 htextobject = htanchor ? htanchor->object : htext->first;
323 while (htextobject && !CanBeCursor(htextobject))
324 htextobject = htextobject->next;
326 UiDisplayPage(topaddress, parenthtext, htext, htextobject,
327 (char *) HTAnchor_title(htext->node_anchor));
332 AddConnection(address, toppage, parentpage, clconnection)
336 ClConnection_t *clconnection;
338 Connection_t *tmpconnection = Connections;
341 while (tmpconnection->Next)
342 tmpconnection = tmpconnection->Next;
343 tmpconnection = tmpconnection->Next = Malloc(sizeof(*tmpconnection));
345 tmpconnection = Connections = Malloc(sizeof(*tmpconnection));
347 tmpconnection->Address = strdup(address);
348 tmpconnection->TopPage = toppage;
349 tmpconnection->ParentPage = parentpage;
350 tmpconnection->ClConnection = clconnection;
351 tmpconnection->FD = 0;
352 tmpconnection->Status = POLLING;
353 tmpconnection->Next = (Connection_t *) NULL;
355 if (UiConnectionsDialogDisplayed())
356 ConnectionsCB((char *) NULL, (HText_t *) NULL,
357 (HTextObject_t *) NULL, (void *) NULL);
359 return tmpconnection;
364 FindConnection(address)
367 Connection_t *tmpconnection = Connections;
369 while (tmpconnection)
370 if (!addresscmp(tmpconnection->Address, address))
371 return tmpconnection;
373 tmpconnection = tmpconnection->Next;
375 return (Connection_t *) NULL;
379 void DeleteConnection(address)
382 Connection_t *tmpconnection = Connections;
385 if (!strcmp(tmpconnection->Address, address))
386 Connections = Connections->Next;
388 while (tmpconnection->Next &&
389 strcmp(tmpconnection->Next->Address, address))
390 tmpconnection = tmpconnection->Next;
391 if (tmpconnection->Next) {
392 Connection_t *tmptmpconnection;
394 tmptmpconnection = tmpconnection->Next;
395 tmpconnection->Next = tmpconnection->Next->Next;
396 tmpconnection = tmptmpconnection;
398 tmpconnection = (Connection_t *) NULL;
401 if (UiConnectionsDialogDisplayed())
402 ConnectionsCB((char *) NULL, (HText_t *) NULL,
403 (HTextObject_t *) NULL, (void *) NULL);
404 Free(tmpconnection->Address);
407 DisplayWarning("DeleteConnection failed");
411 int addresscmp(addr1, addr2)
416 while (addr1[i] && addr2[i] && addr1[i] != '#' && addr2[i] != '#')
417 if (addr1[i] != addr2[i])
422 if ((addr1[i] && addr1[i] != '#') || (addr2[i] && addr2[i] != '#'))