~ubuntu-branches/ubuntu/lucid/x11-apps/lucid

« back to all changes in this revision

Viewing changes to xedit/lisp/struct.c

  • Committer: Bazaar Package Importer
  • Author(s): Julien Cristau
  • Date: 2008-09-23 00:24:45 UTC
  • mfrom: (1.1.2 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080923002445-mb2rwkif45zz1vlj
Tags: 7.3+4
* Remove xedit from the package, it's unmaintained and broken
  (closes: #321434).
* Remove xedit's conffiles on upgrade.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (c) 2001 by The XFree86 Project, Inc.
3
 
 *
4
 
 * Permission is hereby granted, free of charge, to any person obtaining a
5
 
 * copy of this software and associated documentation files (the "Software"),
6
 
 * to deal in the Software without restriction, including without limitation
7
 
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8
 
 * and/or sell copies of the Software, and to permit persons to whom the
9
 
 * Software is furnished to do so, subject to the following conditions:
10
 
 *
11
 
 * The above copyright notice and this permission notice shall be included in
12
 
 * all copies or substantial portions of the Software.
13
 
 *  
14
 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17
 
 * THE XFREE86 PROJECT BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18
 
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
19
 
 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
 
 * SOFTWARE.
21
 
 *
22
 
 * Except as contained in this notice, the name of the XFree86 Project shall
23
 
 * not be used in advertising or otherwise to promote the sale, use or other
24
 
 * dealings in this Software without prior written authorization from the
25
 
 * XFree86 Project.
26
 
 *
27
 
 * Author: Paulo César Pereira de Andrade
28
 
 */
29
 
 
30
 
/* $XFree86: xc/programs/xedit/lisp/struct.c,v 1.22tsi Exp $ */
31
 
 
32
 
#include "lisp/struct.h"
33
 
 
34
 
/*
35
 
 * Prototypes
36
 
 */
37
 
static LispObj *LispStructAccessOrStore(LispBuiltin*, int);
38
 
 
39
 
/*
40
 
 * Initialization
41
 
 */
42
 
LispObj *Omake_struct, *Ostruct_access, *Ostruct_store, *Ostruct_type;
43
 
 
44
 
Atom_id Smake_struct, Sstruct_access, Sstruct_store, Sstruct_type;
45
 
 
46
 
/*
47
 
 * Implementation
48
 
 */
49
 
LispObj *
50
 
Lisp_Defstruct(LispBuiltin *builtin)
51
 
/*
52
 
 defstruct name &rest description
53
 
 */
54
 
{
55
 
    int intern;
56
 
    LispAtom *atom;
57
 
    int i, size, length, slength;
58
 
    char *name, *strname, *sname;
59
 
    LispObj *list, *cons, *object, *definition, *documentation;
60
 
 
61
 
    LispObj *oname, *description;
62
 
 
63
 
    description = ARGUMENT(1);
64
 
    oname = ARGUMENT(0);
65
 
 
66
 
    CHECK_SYMBOL(oname);
67
 
 
68
 
    strname = ATOMID(oname);
69
 
    length = strlen(strname);
70
 
 
71
 
            /* MAKE- */
72
 
    size = length + 6;
73
 
    name = LispMalloc(size);
74
 
 
75
 
    sprintf(name, "MAKE-%s", strname);
76
 
    atom = (object = ATOM(name))->data.atom;
77
 
 
78
 
    if (atom->a_builtin)
79
 
        LispDestroy("%s: %s cannot be a structure name",
80
 
                    STRFUN(builtin), STROBJ(oname));
81
 
 
82
 
    intern = !atom->ext;
83
 
 
84
 
    if (CONSP(description) && STRINGP(CAR(description))) {
85
 
        documentation = CAR(description);
86
 
        description = CDR(description);
87
 
    }
88
 
    else
89
 
        documentation = NIL;
90
 
 
91
 
    /* get structure fields and default values */
92
 
    for (list = description; CONSP(list); list = CDR(list)) {
93
 
        object = CAR(list);
94
 
 
95
 
        cons = list;
96
 
        if (CONSP(object)) {
97
 
            if ((CONSP(CDR(object)) && CDR(CDR(object)) != NIL) ||
98
 
                (!CONSP(CDR(object)) && CDR(object) != NIL))
99
 
            LispDestroy("%s: bad initialization %s",
100
 
                        STRFUN(builtin), STROBJ(object));
101
 
            cons = object;
102
 
            object = CAR(object);
103
 
        }
104
 
        if (!SYMBOLP(object) || strcmp(ATOMID(object), "P") == 0)
105
 
            /* p is invalid as a field name due to `type'-p */
106
 
            LispDestroy("%s: %s cannot be a field for %s",
107
 
                        STRFUN(builtin), STROBJ(object), ATOMID(oname));
108
 
 
109
 
        if (!KEYWORDP(object))
110
 
            CAR(cons) = KEYWORD(ATOMID(object));
111
 
 
112
 
        /* check for repeated field names */
113
 
        for (object = description; object != list; object = CDR(object)) {
114
 
            LispObj *left = CAR(object), *right = CAR(list);
115
 
 
116
 
            if (CONSP(left))
117
 
                left = CAR(left);
118
 
            if (CONSP(right))
119
 
                right = CAR(right);
120
 
 
121
 
            if (ATOMID(left) == ATOMID(right))
122
 
                LispDestroy("%s: only one slot named %s allowed",
123
 
                            STRFUN(builtin), STROBJ(left));
124
 
        }
125
 
    }
126
 
 
127
 
    /* atom should not have been modified */
128
 
    definition = CONS(oname, description);
129
 
    LispSetAtomStructProperty(atom, definition, STRUCT_CONSTRUCTOR);
130
 
    if (!intern)
131
 
        LispExportSymbol(object);
132
 
 
133
 
    atom = oname->data.atom;
134
 
    if (atom->a_defstruct)
135
 
        LispWarning("%s: structure %s is being redefined",
136
 
                    STRFUN(builtin), strname);
137
 
    LispSetAtomStructProperty(atom, definition, STRUCT_NAME);
138
 
 
139
 
    sprintf(name, "%s-P", strname);
140
 
    atom = (object = ATOM(name))->data.atom;
141
 
    LispSetAtomStructProperty(atom, definition, STRUCT_CHECK);
142
 
    if (!intern)
143
 
        LispExportSymbol(object);
144
 
 
145
 
    for (i = 0, list = description; CONSP(list); i++, list = CDR(list)) {
146
 
        if (CONSP(CAR(list)))
147
 
            sname = ATOMID(CAR(CAR(list)));
148
 
        else
149
 
            sname = ATOMID(CAR(list));
150
 
        slength = strlen(sname);
151
 
        if (length + slength + 2 > size) {
152
 
            size = length + slength + 2;
153
 
            name = LispRealloc(name, size);
154
 
        }
155
 
        sprintf(name, "%s-%s", strname, sname);
156
 
        atom = (object = ATOM(name))->data.atom;
157
 
        LispSetAtomStructProperty(atom, definition, i);
158
 
        if (!intern)
159
 
            LispExportSymbol(object);
160
 
    }
161
 
 
162
 
    LispFree(name);
163
 
 
164
 
    if (documentation != NIL)
165
 
        LispAddDocumentation(oname, documentation, LispDocStructure);
166
 
 
167
 
    return (oname);
168
 
}
169
 
 
170
 
/* helper functions
171
 
 *      DONT explicitly call them. Non standard functions.
172
 
 */
173
 
LispObj *
174
 
Lisp_XeditMakeStruct(LispBuiltin *builtin)
175
 
/*
176
 
 lisp::make-struct atom &rest init
177
 
 */
178
 
{
179
 
    int nfld, ncvt, length = lisp__data.protect.length;
180
 
    LispAtom *atom = NULL;
181
 
 
182
 
    LispObj *definition, *object, *field, *fields, *value = NIL, *cons, *list;
183
 
    LispObj *struc, *init;
184
 
 
185
 
    init = ARGUMENT(1);
186
 
    struc = ARGUMENT(0);
187
 
 
188
 
    field = cons = NIL;
189
 
    if (!POINTERP(struc) ||
190
 
        !(XSYMBOLP(struc) || XFUNCTIONP(struc)) ||
191
 
        (atom = struc->data.atom)->a_defstruct == 0 ||
192
 
         atom->property->structure.function != STRUCT_CONSTRUCTOR)
193
 
        LispDestroy("%s: invalid constructor %s",
194
 
                    STRFUN(builtin), STROBJ(struc));
195
 
    definition = atom->property->structure.definition;
196
 
 
197
 
    ncvt = nfld = 0;
198
 
    fields = NIL;
199
 
 
200
 
    /* check for errors in argument list */
201
 
    for (list = init, nfld = 0; CONSP(list); list = CDR(list)) {
202
 
        CHECK_KEYWORD(CAR(list));
203
 
        if (!CONSP(CDR(list)))
204
 
            LispDestroy("%s: values must be provided as pairs",
205
 
                        ATOMID(struc));
206
 
        nfld++;
207
 
        list = CDR(list);
208
 
    }
209
 
 
210
 
    /* create structure, CAR(definition) is structure name */
211
 
    for (list = CDR(definition); CONSP(list); list = CDR(list)) {
212
 
        Atom_id id;
213
 
        LispObj *defvalue = NIL;
214
 
 
215
 
        ++nfld;
216
 
        field = CAR(list);
217
 
        if (CONSP(field)) {
218
 
            /* if default value provided */
219
 
            if (CONSP(CDR(field)))
220
 
                defvalue = CAR(CDR(field));
221
 
            field = CAR(field);
222
 
        }
223
 
        id = ATOMID(field);
224
 
 
225
 
        for (object = init; CONSP(object); object = CDR(object)) {
226
 
            /* field is a keyword, test above checked it */
227
 
            field = CAR(object);
228
 
            if (id == ATOMID(field)) {
229
 
                /* value provided */
230
 
                value = CAR(CDR(object));
231
 
                ncvt++;
232
 
                break;
233
 
            }
234
 
            object = CDR(object);
235
 
        }
236
 
 
237
 
        /* if no initialization given */
238
 
        if (!CONSP(object)) {
239
 
            /* if default value in structure definition */
240
 
            if (defvalue != NIL)
241
 
                value = EVAL(defvalue);
242
 
            else
243
 
                value = NIL;
244
 
        }
245
 
 
246
 
        if (fields == NIL) {
247
 
            fields = cons = CONS(value, NIL);
248
 
            if (length + 1 >= lisp__data.protect.space)
249
 
                LispMoreProtects();
250
 
            lisp__data.protect.objects[lisp__data.protect.length++] = fields;
251
 
        }
252
 
        else {
253
 
            RPLACD(cons, CONS(value, NIL));
254
 
            cons = CDR(cons);
255
 
        }
256
 
    }
257
 
 
258
 
    /* if not enough arguments were converted, need to check because
259
 
     * it is acceptable to set a field more than once, but in that case,
260
 
     * only the first value will be used. */
261
 
    if (nfld > ncvt) {
262
 
        for (list = init; CONSP(list); list = CDR(list)) {
263
 
            Atom_id id = ATOMID(CAR(list));
264
 
 
265
 
            for (object = CDR(definition); CONSP(object);
266
 
                 object = CDR(object)) {
267
 
                field = CAR(object);
268
 
                if (CONSP(field))
269
 
                    field = CAR(field);
270
 
                if (ATOMID(field) == id)
271
 
                    break;
272
 
            }
273
 
            if (!CONSP(object))
274
 
                LispDestroy("%s: %s is not a field for %s",
275
 
                            ATOMID(struc), STROBJ(CAR(list)),
276
 
                            ATOMID(CAR(definition)));
277
 
            list = CDR(list);
278
 
        }
279
 
    }
280
 
 
281
 
    lisp__data.protect.length = length;
282
 
 
283
 
    return (STRUCT(fields, definition));
284
 
}
285
 
 
286
 
static LispObj *
287
 
LispStructAccessOrStore(LispBuiltin *builtin, int store)
288
 
/*
289
 
 lisp::struct-access atom struct
290
 
 lisp::struct-store atom struct value
291
 
 */
292
 
{
293
 
    long offset;
294
 
    LispAtom *atom;
295
 
    LispObj *definition, *list;
296
 
 
297
 
    LispObj *name, *struc, *value = NIL;
298
 
 
299
 
    if (store)
300
 
        value = ARGUMENT(2);
301
 
    struc = ARGUMENT(1);
302
 
    name = ARGUMENT(0);
303
 
 
304
 
    if (!POINTERP(name) ||
305
 
        !(XSYMBOLP(name) || XFUNCTIONP(name)) ||
306
 
        (atom = name->data.atom)->a_defstruct == 0 ||
307
 
        (offset = atom->property->structure.function) < 0) {
308
 
        LispDestroy("%s: invalid argument %s",
309
 
                    STRFUN(builtin), STROBJ(name));
310
 
        /*NOTREACHED*/
311
 
        offset = 0;
312
 
        atom = NULL;
313
 
    }
314
 
    definition = atom->property->structure.definition;
315
 
 
316
 
    /* check if the object is of the required type */
317
 
    if (!STRUCTP(struc) || struc->data.struc.def != definition)
318
 
        LispDestroy("%s: %s is not a %s",
319
 
                    ATOMID(name), STROBJ(struc), ATOMID(CAR(definition)));
320
 
 
321
 
    for (list = struc->data.struc.fields; offset; list = CDR(list), offset--)
322
 
        ;
323
 
 
324
 
    return (store ? RPLACA(list, value) : CAR(list));
325
 
}
326
 
 
327
 
LispObj *
328
 
Lisp_XeditStructAccess(LispBuiltin *builtin)
329
 
/*
330
 
 lisp::struct-access atom struct
331
 
 */
332
 
{
333
 
    return (LispStructAccessOrStore(builtin, 0));
334
 
}
335
 
 
336
 
LispObj *
337
 
Lisp_XeditStructStore(LispBuiltin *builtin)
338
 
/*
339
 
 lisp::struct-store atom struct value
340
 
 */
341
 
{
342
 
    return (LispStructAccessOrStore(builtin, 1));
343
 
}
344
 
 
345
 
LispObj *
346
 
Lisp_XeditStructType(LispBuiltin *builtin)
347
 
/*
348
 
 lisp::struct-type atom struct
349
 
 */
350
 
{
351
 
    LispAtom *atom = NULL;
352
 
 
353
 
    LispObj *definition, *struc, *name;
354
 
 
355
 
    struc = ARGUMENT(1);
356
 
    name = ARGUMENT(0);
357
 
 
358
 
    if (!POINTERP(name) ||
359
 
        !(XSYMBOLP(name) || XFUNCTIONP(name)) ||
360
 
        (atom = name->data.atom)->a_defstruct == 0 ||
361
 
        (atom->property->structure.function != STRUCT_CHECK))
362
 
        LispDestroy("%s: invalid argument %s",
363
 
                    STRFUN(builtin), STROBJ(name));
364
 
    definition = atom->property->structure.definition;
365
 
 
366
 
    /* check if the object is of the required type */
367
 
    if (STRUCTP(struc) && struc->data.struc.def == definition)
368
 
        return (T);
369
 
 
370
 
    return (NIL);
371
 
}