~ubuntu-branches/ubuntu/saucy/steam/saucy-backports

« back to all changes in this revision

Viewing changes to sources/pike_doc/doc.c

  • Committer: Package Import Robot
  • Author(s): Felix Geyer
  • Date: 2013-10-29 19:51:18 UTC
  • mfrom: (0.2.4) (3.2.1 trusty-proposed)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: package-import@ubuntu.com-20131029195118-b9bxciz5hwx5z459
Tags: 1:1.0.0.39-2ubuntu1
Add an epoch to the version number as there was an unrelated steam package
in the archive with a higher version number.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2000-2004  Thomas Bopp, Thorsten Hampel, Ludger Merkens
2
 
 *
3
 
 *  This program is free software; you can redistribute it and/or modify
4
 
 *  it under the terms of the GNU General Public License as published by
5
 
 *  the Free Software Foundation; either version 2 of the License, or
6
 
 *  (at your option) any later version.
7
 
 *
8
 
 *  This program is distributed in the hope that it will be useful,
9
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
 *  GNU General Public License for more details.
12
 
 *
13
 
 *  You should have received a copy of the GNU General Public License
14
 
 *  along with this program; if not, write to the Free Software
15
 
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16
 
 * 
17
 
 * $Id: doc.c,v 1.1.1.1 2006/03/27 12:40:19 exodusd Exp $
18
 
 */
19
 
 
20
 
#include "mem.c"
21
 
#include <mapping.h>
22
 
 
23
 
#define MODE_RETURNTYPE 2
24
 
#define MODE_COMMENT    1
25
 
#define MODE_NOCOMMENT  0
26
 
#define MODE_NOTHING    -1
27
 
 
28
 
struct ParameterDesc {
29
 
    char*                 description;
30
 
    struct ParameterDesc*        next;
31
 
};
32
 
 
33
 
struct Parameters {
34
 
    char*                 description;
35
 
    struct ParameterDesc*      params;
36
 
};
37
 
 
38
 
/**
39
 
 *
40
 
 *  
41
 
 * @param 
42
 
 * @return 
43
 
 * @author Thomas Bopp (astra@upb.de) 
44
 
 * @see 
45
 
 */
46
 
void 
47
 
parse_header(char* fname, char* synopsis, char* keywords, 
48
 
                   char* header, int len, struct svalue* callback)
49
 
{
50
 
    int      begin,  i, j, args;
51
 
    char*           description;
52
 
    struct mapping*     doc_map;
53
 
    struct array*           arr;
54
 
    char*                params;
55
 
    char*                  name;
56
 
    char*                 value;
57
 
    char*               context;
58
 
    struct svalue skey, sval, sarr;
59
 
    struct svalue*          res;
60
 
 
61
 
 
62
 
    description = _MALLOC(len);
63
 
    description[0] = '\0';
64
 
    /* first parse the function description */
65
 
    i = j = 0;
66
 
    while ( i < len && header[i] != '@' ) {
67
 
        if ( header[i] != '*' )
68
 
            description[j++] = header[i];
69
 
        else 
70
 
            description[j++] = ' ';
71
 
        i++;
72
 
    }
73
 
    while ( j > 0 && description[j] != '\n' ) j--;
74
 
    description[j] = '\0';
75
 
 
76
 
    push_svalue(callback);
77
 
    push_text(fname);
78
 
    push_text(synopsis);
79
 
    push_text(keywords);
80
 
    push_text(description);    
81
 
 
82
 
    begin = i;
83
 
 
84
 
    args = 0;
85
 
    doc_map = allocate_mapping(10);
86
 
    name = NULL;
87
 
    i-=2;
88
 
    while ( i < len ) {
89
 
        int nl, l;
90
 
        
91
 
        if ( strncmp(&header[i], " @", 2) == 0 || 
92
 
             strncmp(&header[i], "\t@",2) == 0 ) 
93
 
        {
94
 
            i+=1;
95
 
            nl = 0;
96
 
            j = i+1;
97
 
            while ( j < len && 
98
 
                    ((header[j] >= 'a' && header[j] <= 'z') ||
99
 
                     (header[j] >= 'A' && header[j] <= 'Z') ) )
100
 
                j++;
101
 
            name = create_string(name, &header[i+1], (j-i));
102
 
            skey.u.string = make_shared_string(name);
103
 
            skey.type = T_STRING;
104
 
            i = j;
105
 
            while ( i < len && (header[i] != '@' || !nl) && 
106
 
                    strncmp(&header[i], "*/",2) != 0 )  
107
 
            {
108
 
                if ( header[i] == '\n' ) nl = 1;
109
 
                if ( header[i] == '*' ) header[i] = ' ';
110
 
                i++;
111
 
            }
112
 
            args++;
113
 
            res = simple_mapping_string_lookup(doc_map, name);
114
 
            value = create_string_strip_spaces(NULL, &header[j], i-j);
115
 
            //fprintf(stderr, "Found argument to function %s,%s:%s\n",
116
 
            //fname, name, value);
117
 
            if ( res != NULL ) {
118
 
                
119
 
                if ( res->type == T_ARRAY ) {
120
 
                    arr = res->u.array;
121
 
                    arr = resize_array(arr, arr->size+1);
122
 
                    arr = array_insert(arr, &sval, arr->size-1);
123
 
                }
124
 
                else {
125
 
                    arr = allocate_array(2);
126
 
                    params = malloc(res->u.string->len+1);
127
 
                    strncpy(params,
128
 
                            res->u.string->str, 
129
 
                            res->u.string->len);
130
 
                    params[res->u.string->len] = '\0';
131
 
                    sval.u.string = make_shared_string(params);
132
 
                    arr = array_insert(arr, &sval, 0);
133
 
                    free_string(sval.u.string);
134
 
 
135
 
                    sval.u.string = make_shared_string(value);
136
 
                    sval.type = T_STRING;
137
 
                    arr = array_insert(arr, &sval, 1);
138
 
                    free_string(sval.u.string);
139
 
                }
140
 
                sarr.u.array = arr;
141
 
                sarr.type = T_ARRAY;
142
 
                mapping_insert(doc_map, &skey, &sarr);
143
 
            }
144
 
            else {
145
 
                sval.u.string = make_shared_string(value);
146
 
                sval.type = T_STRING;
147
 
                mapping_insert(doc_map, &skey, &sval);
148
 
                free_string(sval.u.string);
149
 
            }
150
 
            free_string(skey.u.string);
151
 
            i -= 4;
152
 
        }
153
 
        i++;
154
 
    }
155
 
    push_mapping(doc_map);
156
 
    f_call_function(6);
157
 
    pop_stack();
158
 
    _FREE(description);
159
 
    _FREE(fname);
160
 
    _FREE(synopsis);
161
 
    _FREE(keywords);
162
 
 
163
 
}
164
 
 
165
 
void
166
 
parse_doc(char* source, struct svalue* callback)
167
 
{
168
 
    int         begin, i, len, end;
169
 
    int        mode = MODE_NOTHING;
170
 
    int                    f_begin;
171
 
    char*                   f_name;
172
 
    char*               f_keywords;
173
 
    char*               f_synopsis;
174
 
    char* keyw[11] ={"void","array","string","object","float","int","mixed","mapping", "bool","function","program"};
175
 
 
176
 
    len = strlen(source);
177
 
    i   = 0;
178
 
    
179
 
    while ( i < len ) {
180
 
        if ( mode == MODE_NOCOMMENT || mode == MODE_NOTHING ) {
181
 
            if ( strncmp(&source[i], "/**", 3) == 0 && 
182
 
                 strncmp(&source[i], "/**/", 4) != 0 ) 
183
 
            {
184
 
                mode = MODE_COMMENT;
185
 
                begin = i+4;
186
 
            }
187
 
        }
188
 
        else if ( mode == MODE_COMMENT ) 
189
 
        {
190
 
            if ( strncmp(&source[i], "*/", 2) == 0 ) {
191
 
                int brackets, keyword_start;
192
 
 
193
 
                end  = f_begin = i;
194
 
 
195
 
                while ( i < len && mode != MODE_RETURNTYPE ) {
196
 
                    int j;
197
 
 
198
 
                    for ( j = 0; j < 11; j++ ) {
199
 
                        if ( strncmp(&source[i], keyw[j], strlen(keyw[j]))==0 )
200
 
                            mode = MODE_RETURNTYPE;
201
 
                    }
202
 
                    i++;
203
 
                }
204
 
                /* goto first space after function types and return value */
205
 
                brackets = 0;
206
 
                while ( i < len && (brackets != 0 || 
207
 
                        (source[i] != ' ' && source[i] != '\n')) ) {
208
 
                    if ( source[i] == '(' ) brackets++;
209
 
                    else if ( source[i] == ')' ) brackets--;
210
 
                    i++;
211
 
                }
212
 
                while ( i < len && source[i] != '(' ) 
213
 
                {
214
 
                    if ( !( (source[i] >= 'a' && source[i] <='z') ||
215
 
                            (source[i] >= 'A' && source[i] <= 'Z') ||
216
 
                            (source[i] == '_')) )
217
 
                        f_begin = i;
218
 
                    i++;
219
 
                }
220
 
                f_keywords = (char*)create_string(NULL, &source[end+3], 
221
 
                                                        f_begin - end - 1);
222
 
                f_name = (char*)
223
 
                    create_string(NULL, &source[f_begin+1], i-f_begin);
224
 
 
225
 
                if ( strncmp(f_name, "ASSERTINFO", 10) == 0 ) {
226
 
                    fprintf(stderr,
227
 
                            "Error, assertinfo function found ?!\nContext:%s",
228
 
                            &source[begin]);
229
 
                }
230
 
                else if ( f_name[0] == '/' || f_name[1] == '/' ) {
231
 
                    fprintf(stderr,
232
 
                            "error: Wrong beginning of function?!\nContext:%s",
233
 
                            &source[begin]);
234
 
                }
235
 
                
236
 
                brackets++;
237
 
                i++;
238
 
                while ( i < len && brackets != 0 ) {
239
 
                    if ( source[i] == '(' ) brackets++;
240
 
                    if ( source[i] == ')' ) brackets--;
241
 
                    i++;
242
 
                }
243
 
                f_synopsis = (char*) create_string(NULL, &source[f_begin+1],
244
 
                                                   i-f_begin);
245
 
                parse_header(f_name, f_synopsis, f_keywords,
246
 
                                      &source[begin], end-begin, callback);
247
 
                mode = MODE_NOCOMMENT;
248
 
            }
249
 
        }
250
 
        i++;
251
 
    }
252
 
}
253
 
 
254
 
 
255
 
void
256
 
f_parse_functions(INT32 args)
257
 
{
258
 
    struct svalue* f;
259
 
    char*        buf;
260
 
    
261
 
    get_all_args("parse_functions", args, "%s%*", &buf, &f);
262
 
    parse_doc(buf, f);
263
 
    pop_n_elems(args);
264
 
}
265
 
 
266
 
void
267
 
pike_module_init()
268
 
{
269
 
    memory = NULL;
270
 
    add_function("parse_functions", f_parse_functions,
271
 
                 "function(string,function:void)", 0);
272
 
}
273
 
 
274
 
void
275
 
pike_module_exit()
276
 
{
277
 
}