File: llist.c

package info (click to toggle)
kbtin 1.0.15-1.1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,852 kB
  • ctags: 1,074
  • sloc: ansic: 17,345; sh: 7,584; makefile: 135; perl: 118
file content (419 lines) | stat: -rw-r--r-- 11,984 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/* Autoconf patching by David Hedbor, neotron@lysator.liu.se */
/*********************************************************************/
/* file: llist.c - linked-list datastructure                         */
/*                             TINTIN III                            */
/*          (T)he K(I)cki(N) (T)ickin D(I)kumud Clie(N)t             */
/*                     coded by peter unold 1992                     */
/*********************************************************************/
#include "tintin.h"
#include "protos/glob.h"
#include "protos/hash.h"
#include "protos/llist.h"
#include "protos/print.h"
#include "protos/parse.h"
#include "protos/routes.h"
#include "protos/utils.h"


/***************************************/
/* init list - return: ptr to listhead */
/***************************************/
struct listnode* init_list(void)
{
    struct listnode *listhead;

    if ((listhead = TALLOC(struct listnode)) == NULL)
        syserr("couldn't alloc listhead");
    listhead->next = NULL;
    return listhead;
}

/***********************************************/
/* kill list - run through list and free nodes */
/***********************************************/
void kill_list(struct listnode *nptr)
{
    struct listnode *nexttodel;

    if (nptr == NULL)
        syserr("NULL PTR");
    nexttodel = nptr->next;
    LFREE(nptr);

    for (nptr = nexttodel; nptr; nptr = nexttodel)
    {
        nexttodel = nptr->next;
        SFREE(nptr->left);
        SFREE(nptr->right);
        SFREE(nptr->pr);
        LFREE(nptr);
    }
}


/***********************************************************/
/* zap only the list structure, without freeing the string */
/***********************************************************/
void zap_list(struct listnode *nptr)
{
    struct listnode *nexttodel;

    if (nptr == NULL)
        syserr("NULL PTR");
    nexttodel = nptr->next;
    LFREE(nptr);

    for (nptr = nexttodel; nptr; nptr = nexttodel)
    {
        nexttodel = nptr->next;
        LFREE(nptr);
    }
}


/********************************************************************
**   This function will clear all lists associated with a session  **
********************************************************************/
void kill_all(struct session *ses, int mode)
{
    switch (mode)
    {
    case CLEAN:
        if (ses != NULL)
        {
            kill_hash(ses->aliases);
            ses->aliases = init_hash();
            kill_list(ses->actions);
            ses->actions = init_list();
            kill_list(ses->prompts);
            ses->prompts = init_list();
            kill_hash(ses->myvars);
            ses->myvars = init_hash();
            kill_list(ses->highs);
            ses->highs = init_list();
            kill_list(ses->subs);
            ses->subs = init_list();
            kill_list(ses->antisubs);
            ses->antisubs = init_list();
            kill_list(ses->path);
            ses->path = init_list();
            kill_hash(ses->binds);
            ses->binds = init_hash();
            kill_hash(ses->pathdirs);
            ses->pathdirs = init_hash();
            kill_routes(ses);
            tintin_printf(ses,"#Lists cleared.");
            prompt(NULL);
        }
        else
        {       /* can't happen */
            tintin_printf(0,"#Can't clean the common lists (yet).");
            prompt(NULL);
        }
        break;

    case END:
        if (ses != NULL)
        {
            kill_hash(ses->aliases);
            kill_list(ses->actions);
            kill_list(ses->prompts);
            kill_hash(ses->myvars);
            kill_list(ses->highs);
            kill_list(ses->subs);
            kill_list(ses->antisubs);
            kill_list(ses->path);
            kill_hash(ses->pathdirs);
            kill_hash(ses->binds);
            kill_routes(ses);
        }
        break;
    }
}
/***********************************************/
/* make a copy of a list - return: ptr to copy */
/***********************************************/
struct listnode* copy_list(struct listnode *sourcelist,int mode)
{
    struct listnode *resultlist;

    resultlist = init_list();
    while ((sourcelist = sourcelist->next))
        insertnode_list(resultlist, sourcelist->left, sourcelist->right,
                        sourcelist->pr, mode);

    return resultlist;
}

/******************************************************************/
/* compare priorities of a and b in a semi-lexicographical order: */
/* strings generally sort in ASCIIbetical order, however numbers  */
/* sort according to their numerical values.                      */
/******************************************************************/
static int prioritycmp(char *a, char *b)
{
    int res;

not_numeric:
    while(*a && *a==*b && !isadigit(*a))
    {
        a++;
        b++;
    }
    if (!a && !b)
        return 0;
    if(!isadigit(*a) || !isadigit(*b))
        return (*a<*b)? -1 : (*a>*b)? 1 : 0;
    while(*a=='0')
        a++;
    while(*b=='0')
        b++;
    res=0;
    while(isadigit(*a))
    {
        if (!isadigit(*b))
            return 1;
        if (*a!=*b && !res)
            res=(*a<*b)? -1 : 1;
        a++;
        b++;
    }
    if (isadigit(*b))
        return -1;
    if (res)
        return res;
    goto not_numeric;
}

/*****************************************************************/
/* create a node containing the ltext, rtext fields and stuff it */
/* into the list - in lexicographical order, or by numerical     */
/* priority (dependent on mode) - Mods by Joann Ellsworth 2/2/94 */
/*****************************************************************/
void insertnode_list(struct listnode *listhead, char *ltext, char *rtext, char *prtext, int mode)
{
    struct listnode *nptr, *nptrlast, *newnode;
    int lo,ln;

    if ((newnode = (TALLOC(struct listnode))) == NULL)
        syserr("couldn't malloc listhead");
    newnode->left = (char *)MALLOC(strlen(ltext) + 1);
    newnode->right = (char *)MALLOC(strlen(rtext) + 1);
    newnode->pr = prtext? (char *)MALLOC(strlen(prtext) + 1) : 0;
    strcpy(newnode->left, ltext);
    strcpy(newnode->right, rtext);
    if (prtext)
        strcpy(newnode->pr, prtext);

    nptr = listhead;
    switch (mode)
    {
    case PRIORITY:
        while ((nptrlast = nptr) && (nptr = nptr->next))
        {
            if (prioritycmp(prtext, nptr->pr) < 0)
            {
                newnode->next = nptr;
                nptrlast->next = newnode;
                return;
            }
            else if (prioritycmp(prtext, nptr->pr) == 0)
            {
                while ((nptrlast) && (nptr) &&
                        (prioritycmp(prtext, nptr->pr) == 0))
                {
                    if (prioritycmp(ltext, nptr->left) <= 0)
                    {
                        newnode->next = nptr;
                        nptrlast->next = newnode;
                        return;
                    }
                    nptrlast = nptr;
                    nptr = nptr->next;
                }
                nptrlast->next = newnode;
                newnode->next = nptr;
                return;
            }
        }
        nptrlast->next = newnode;
        newnode->next = NULL;
        return;
        break;


    case LENGTH:
        ln=strlen(ltext);
        while ((nptrlast = nptr) && (nptr = nptr->next))
        {
            lo=strlen(nptr->left);
            if (ln<lo)
            {
                newnode->next = nptr;
                nptrlast->next = newnode;
                return;
            }
            else if (ln==lo)
            {
                while ((nptrlast) && (nptr) &&
                        (ln==lo))
                {
                    if (prioritycmp(ltext, nptr->left) <= 0)
                    {
                        newnode->next = nptr;
                        nptrlast->next = newnode;
                        return;
                    }
                    nptrlast = nptr;
                    nptr = nptr->next;
                }
                nptrlast->next = newnode;
                newnode->next = nptr;
                return;
            }
        }
        nptrlast->next = newnode;
        newnode->next = NULL;
        return;
        break;



    case ALPHA:
        while ((nptrlast = nptr) && (nptr = nptr->next))
        {
            if (strcmp(ltext, nptr->left) <= 0)
            {
                newnode->next = nptr;
                nptrlast->next = newnode;
                return;
            }
        }
        nptrlast->next = newnode;
        newnode->next = NULL;
        return;
        break;
    }
}

/*****************************/
/* delete a node from a list */
/*****************************/
void deletenode_list(struct listnode *listhead, struct listnode *nptr)
{
    struct listnode *lastnode = listhead;

    while ((listhead = listhead->next))
    {
        if (listhead == nptr)
        {
            lastnode->next = listhead->next;
            SFREE(listhead->left);
            SFREE(listhead->right);
            SFREE(listhead->pr);
            LFREE(listhead);
            return;
        }
        lastnode = listhead;
    }
    return;
}

/********************************************************/
/* search for a node containing the ltext in left-field */
/* return: ptr to node on succes / NULL on failure      */
/********************************************************/
struct listnode* searchnode_list(struct listnode *listhead, char *cptr)
{
    int i;

    while ((listhead = listhead->next))
    {
        if ((i = strcmp(listhead->left, cptr)) == 0)
            return listhead;
        /* CHANGED to fix bug when list isn't alphabetically sorted
           else if(i>0)
           return NULL;
         */
    }
    return NULL;
}

/*************************************/
/* show contents of a node on screen */
/*************************************/
void shownode_list(struct listnode *nptr)
{
    tintin_printf(0, "~7~{%s~7~}={%s~7~}", nptr->left, nptr->right);
}

void shownode_list_action(struct listnode *nptr)
{
    tintin_printf(0, "~7~{%s~7~}={%s~7~} @ {%s}", nptr->left, nptr->right, nptr->pr);
}

/*************************************/
/* list contents of a list on screen */
/*************************************/
void show_list(struct listnode *listhead)
{
    while ((listhead = listhead->next))
        shownode_list(listhead);
}

void show_list_action(struct listnode *listhead)
{
    while ((listhead = listhead->next))
        if (strcmp(listhead->left,K_ACTION_MAGIC))
            shownode_list_action(listhead);
}

struct listnode* search_node_with_wild(struct listnode *listhead, char *cptr)
{
    while ((listhead = listhead->next))
    {
        /* CHANGED to fix silly globbing behavior
           if(check_one_node(listhead->left, cptr))
         */
        if (match(cptr, listhead->left))
            return listhead;
    }
    return NULL;
}


/*********************************************************************/
/* create a node containing the ltext, rtext fields and place at the */
/* end of a list - as insertnode_list(), but not alphabetical        */
/*********************************************************************/
void addnode_list(struct listnode *listhead, char *ltext, char *rtext, char *prtext)
{
    struct listnode *newnode;

    if ((newnode = TALLOC(struct listnode)) == NULL)
        syserr("couldn't malloc listhead");
    newnode->left = mystrdup(ltext);
    if (rtext)
        newnode->right = mystrdup(rtext);
    else
        newnode->right = 0;
    if (prtext)
        newnode->pr = mystrdup(prtext);
    else
        newnode->pr = 0;
    newnode->next = NULL;
    while (listhead->next != NULL)
        (listhead = listhead->next);
    listhead->next = newnode;
}

int count_list(struct listnode *listhead)
{
    int count = 0;
    struct listnode *nptr;

    nptr = listhead;
    while ((nptr = nptr->next))
        ++count;
    return count;
}