source: TI05-delivery/trunk/src/python_ext/bbftpc.c @ 1246

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI05-delivery/trunk/src/python_ext/bbftpc.c@1246
Revision 1246, 2.8 KB checked in by spascoe, 15 years ago (diff)

bbftpc.run() has been split into run(), docommand() and close().
Multiple commands per connection should now be possible.

Line 
1/**
2 * NDG python embedded bbftp daemon module.
3 *
4 * @author Stephen Pascoe
5 *
6 * Copyright (C) 2006 CCLRC & NERC
7 *
8 * This software may be distributed under the terms of the Q Public Licence, version 1.0 or later.
9 *
10 */
11
12#include <Python.h>
13#include <stdio.h>
14
15#include "util.h"
16
17extern char **environ;
18
19int bbftpc_init(int argc, char **argv, char **envp);
20void bbftpc_final(void);
21int treatcommand(char *cmd);
22
23/**
24 * Main entry point into the bbftp client from python.
25 */
26static PyObject *bbftpc_run(PyObject *self, PyObject *args) {
27  int i, argc;
28  struct ndg_argv argv_s;
29  char *arg;
30  PyObject *client_args, *item;
31
32  if (!PyArg_ParseTuple(args, "O", &client_args)) {
33    return NULL;
34  }
35  Py_INCREF(client_args);
36
37  /*
38   * Convert arguments into a standard argv sequence.
39   */
40
41  if ((argc = PySequence_Size(client_args)) == -1) {
42    return NULL;
43  }
44  if (!ndg_argv_init(&argv_s, argc + 1)) {
45    PyErr_SetString(PyExc_MemoryError, "malloc failed");
46    Py_DECREF(client_args);
47    return NULL;
48  }
49
50  ndg_argv_add(&argv_s, "bbftpc_run");
51  for (i=0; i<argc; i++) {
52    if ((item = PySequence_GetItem(client_args, i)) == NULL) {
53      ndg_argv_clear(&argv_s); ndg_argv_free(&argv_s);
54      Py_DECREF(client_args);
55      return NULL;
56    }
57    if ((arg = PyString_AsString(item)) == NULL) {
58      ndg_argv_clear(&argv_s); ndg_argv_free(&argv_s);
59      Py_DECREF(item);
60      Py_DECREF(client_args);
61      return NULL;
62    }
63
64    if (!ndg_argv_add(&argv_s, arg)) {
65
66      PyErr_SetString(PyExc_MemoryError, "ndg_argv_add failed");
67      ndg_argv_clear(&argv_s); ndg_argv_free(&argv_s);
68      Py_DECREF(item);
69      Py_DECREF(client_args);
70      return NULL;
71    }
72
73    Py_DECREF(item);
74
75  }
76  Py_DECREF(client_args);
77
78  bbftpc_init(argv_s.argc, argv_s.argv, environ);
79  ndg_argv_clear(&argv_s); ndg_argv_free(&argv_s);
80
81  Py_RETURN_NONE;
82}
83
84/**
85 * Close the connection
86 */
87static PyObject *bbftpc_close(PyObject *self, PyObject *args) {
88
89  // Check there are no arguments
90  if (!PyArg_ParseTuple(args, "", NULL)) {
91    return NULL;
92  }
93
94  bbftpc_final();
95
96  Py_RETURN_NONE;
97}
98
99static PyObject *bbftpc_docommand(PyObject *self, PyObject *args) {
100  char *cmd;
101  PyObject *ret;
102
103  if (!PyArg_ParseTuple(args, "s", &cmd)) {
104    return NULL;
105  }
106
107  if ((ret = Py_BuildValue("i", treatcommand(cmd))) == NULL) {
108    return NULL;
109  }
110
111  return ret;
112}
113
114   
115
116static PyMethodDef BbftpcMethods[] = {
117  {
118    "run", bbftpc_run, METH_VARARGS,
119    "Execute the bbftp client.\n"
120    "\n"
121    "@param args: command line arguments"
122  },
123  {
124    "close", bbftpc_close, METH_VARARGS,
125    "Close the bbftp client."
126  },
127  {
128    "docommand", bbftpc_docommand, METH_VARARGS,
129    "Run a command in the client.\n"
130    "\n"
131    "@param cmd: The command string."
132  },
133  {NULL, NULL, 0, NULL}
134};
135
136PyMODINIT_FUNC initbbftpc(void) {
137  (void) Py_InitModule("bbftpc", BbftpcMethods);
138}
Note: See TracBrowser for help on using the repository browser.