~ubuntu-branches/ubuntu/jaunty/libapache2-mod-python/jaunty

« back to all changes in this revision

Viewing changes to src/_pspmodule.c

  • Committer: Bazaar Package Importer
  • Author(s): Thom May
  • Date: 2004-09-06 20:27:57 UTC
  • Revision ID: james.westby@ubuntu.com-20040906202757-yzpyu1bcabgpjtiu
Tags: upstream-3.1.3
ImportĀ upstreamĀ versionĀ 3.1.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2004 Apache Software Foundation 
 
3
 * 
 
4
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 
5
 * may not use this file except in compliance with the License.  You
 
6
 * may obtain a copy of the License at
 
7
 *
 
8
 *      http://www.apache.org/licenses/LICENSE-2.0
 
9
 *
 
10
 * Unless required by applicable law or agreed to in writing, software
 
11
 * distributed under the License is distributed on an "AS IS" BASIS,
 
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 
13
 * implied.  See the License for the specific language governing
 
14
 * permissions and limitations under the License.
 
15
 *
 
16
 * This file originally written by Stering Hughes
 
17
 * 
 
18
 * $Id: _pspmodule.c,v 1.7 2004/02/16 19:47:27 grisha Exp $
 
19
 *
 
20
 * See accompanying documentation and source code comments for
 
21
 * details.
 
22
 *
 
23
 */
 
24
 
 
25
#include "psp_flex.h"
 
26
#include "psp_parser.h"
 
27
#include "psp_string.h"
 
28
#include "_pspmodule.h"
 
29
#include "Python.h"
 
30
 
 
31
/* calm down compile warning from psp_flex.h*/
 
32
static int yy_init_globals (yyscan_t yyscanner ) {return 0;};
 
33
 
 
34
static psp_parser_t *psp_parser_init(void)
 
35
{
 
36
    psp_parser_t *parser;
 
37
 
 
38
    parser = (psp_parser_t *) malloc(sizeof(*parser));
 
39
 
 
40
    memset(&parser->pycode, 0, sizeof(psp_string));
 
41
    memset(&parser->whitespace, 0, sizeof(psp_string));
 
42
    parser->dir = NULL;
 
43
    parser->is_psp_echo = 0;
 
44
    parser->after_colon = 0;
 
45
    parser->seen_newline = 0;
 
46
 
 
47
    return parser;
 
48
}
 
49
 
 
50
static void psp_parser_cleanup(psp_parser_t *parser)
 
51
{
 
52
    if (parser->pycode.allocated) {
 
53
        free(parser->pycode.blob);
 
54
    }
 
55
    
 
56
    if (parser->whitespace.allocated) {
 
57
        free(parser->whitespace.blob);
 
58
    }
 
59
 
 
60
    free(parser);
 
61
}
 
62
 
 
63
static PyObject * _psp_module_parse(PyObject *self, PyObject *argv)
 
64
{
 
65
    PyObject *code;
 
66
    char     *filename;
 
67
    char     *dir = NULL;
 
68
    char     *path;
 
69
    psp_parser_t  *parser;
 
70
    yyscan_t scanner;
 
71
    FILE *f;
 
72
    
 
73
    if (!PyArg_ParseTuple(argv, "s|s", &filename, &dir)) {
 
74
        return NULL;
 
75
    }
 
76
 
 
77
    if (dir) {
 
78
        path = malloc(strlen(filename)+strlen(dir)+1);
 
79
        if (!path) 
 
80
            return PyErr_NoMemory();
 
81
        strcpy(path, dir);
 
82
        strcat(path, filename);
 
83
    }
 
84
    else {
 
85
        path = filename;
 
86
    }
 
87
    
 
88
    Py_BEGIN_ALLOW_THREADS
 
89
    f = fopen(path, "rb");
 
90
    Py_END_ALLOW_THREADS
 
91
 
 
92
    if (f == NULL) {
 
93
        PyErr_SetFromErrnoWithFilename(PyExc_IOError, path);
 
94
        if (dir) free(path);
 
95
        return NULL;
 
96
    }
 
97
    if (dir) free(path);
 
98
 
 
99
    parser = psp_parser_init();
 
100
    if (dir)
 
101
        parser->dir = dir;
 
102
 
 
103
    yylex_init(&scanner);
 
104
    yyset_in(f, scanner);
 
105
    yyset_extra(parser, scanner);
 
106
    yylex(scanner);
 
107
    yylex_destroy(scanner);
 
108
 
 
109
    fclose(f);
 
110
    psp_string_0(&parser->pycode);
 
111
 
 
112
    if (PyErr_Occurred()) {
 
113
        psp_parser_cleanup(parser);
 
114
        return NULL;
 
115
    }
 
116
 
 
117
    if (parser->pycode.blob) {
 
118
        code = PyString_FromString(parser->pycode.blob);
 
119
    }
 
120
    else {
 
121
        code = PyString_FromString("");
 
122
    }
 
123
 
 
124
    psp_parser_cleanup(parser);
 
125
    
 
126
    return code; 
 
127
}
 
128
 
 
129
static PyObject * _psp_module_parsestring(PyObject *self, PyObject *argv)
 
130
{
 
131
    PyObject *code;
 
132
    PyObject *str;
 
133
    yyscan_t scanner;
 
134
    psp_parser_t  *parser;
 
135
    YY_BUFFER_STATE bs;
 
136
 
 
137
    if (!PyArg_ParseTuple(argv, "S", &str)) {
 
138
        return NULL;
 
139
    }
 
140
 
 
141
    Py_BEGIN_ALLOW_THREADS
 
142
    parser = psp_parser_init();
 
143
    yylex_init(&scanner);
 
144
    yyset_extra(parser, scanner);
 
145
 
 
146
    bs = yy_scan_string(PyString_AsString(str), scanner);
 
147
    yylex(scanner);
 
148
 
 
149
    yy_delete_buffer(bs, scanner);
 
150
    yylex_destroy(scanner);
 
151
    
 
152
    psp_string_0(&parser->pycode);
 
153
    Py_END_ALLOW_THREADS
 
154
 
 
155
    code = PyString_FromString(parser->pycode.blob);
 
156
 
 
157
    psp_parser_cleanup(parser);
 
158
 
 
159
    return code; 
 
160
}
 
161
 
 
162
struct PyMethodDef _psp_module_methods[] = {
 
163
    {"parse",       (PyCFunction) _psp_module_parse,       METH_VARARGS},
 
164
    {"parsestring", (PyCFunction) _psp_module_parsestring, METH_VARARGS},
 
165
    {NULL, NULL}
 
166
};
 
167
 
 
168
void init_psp(void)
 
169
{
 
170
    Py_InitModule("_psp", _psp_module_methods);
 
171
}