source: TI05-delivery/trunk/src/python_ext/util.c @ 1259

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

Client-side authentication handling is implemented but currently broken.
I'm still working out what's wrong.

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 <stdlib.h>
13#include <string.h>
14#include "util.h"
15
16/**
17 * Routines for constructing a argc/argv style argument list.
18 */
19
20/**
21 * Initialise a structure.
22 * @return TRUE on success or FALSE on a memory error.
23 */
24int ndg_argv_init(struct ndg_argv *argv_s, int argc) {
25  argv_s->argc = argc;
26
27  if ((argv_s->argv = (char **)malloc(argc * sizeof(char *))) == NULL) {
28    return FALSE;
29  }
30
31  argv_s->next = 0;
32
33  return TRUE;
34}
35
36/**
37 * Free a structure.  \a argv_s should be cleared first with \c ndg_argv_clear().
38 *
39 * @return TRUE on success or FALSE if the structure isn't empty.
40 */
41int ndg_argv_free(struct ndg_argv *argv_s) {
42  if (argv_s->next != 0) return FALSE;
43  free(argv_s->argv);
44  return TRUE;
45}
46
47/**
48 * Clear all arguments from the structure.
49 *
50 */
51void ndg_argv_clear(struct ndg_argv *argv_s) {
52  char *arg;
53  while (argv_s->next > 0) {
54    argv_s->next--;
55    arg = argv_s->argv[argv_s->next];
56    free(arg);
57  }
58}
59
60/**
61 * Add an argument.
62 *
63 * @return TRUE on success or FALSE on an error.
64 */
65int ndg_argv_add(struct ndg_argv *argv_s, char *arg) {
66
67  if (argv_s->argc <= argv_s->next) return FALSE;
68  if ((argv_s->argv[argv_s->next] = (char *)malloc(strlen(arg) + 1)) == NULL) {
69    return FALSE;
70  }
71  strcpy(argv_s->argv[argv_s->next], arg);
72  argv_s->next++;
73  return TRUE;
74}
75
76
77/**
78 * Copy the python exception message to a bbftp logmessage.
79 *
80 * If the python error indicator is set it will be cleared.
81 * If the python error indicator is not set the empty string will be copied to \a logmessage.
82 * The exception message is truncated if it exceeds \c NDG_MAX_LOGMESSAGE.
83 *
84 * @param logmessage a pointer to a buffer of length \c NDG_MAX_LOGMESSAGE
85 *     for storing an error message when returning -1.
86 * @param prefix a string to be prefixed to the logmessage.
87 */
88void ndg_pyerr_to_logmessage(char *logmessage, char *prefix) {
89  PyObject *ptype, *pvalue, *ptraceback, *obj;
90  char *exname, *exvalue;
91
92  PyErr_Fetch(&ptype, &pvalue, &ptraceback);
93 
94  if ((obj = PyObject_GetAttrString(ptype, "__name__")) == NULL) goto error;
95  exname = PyString_AsString(obj); Py_DECREF(obj);
96  if (exname == NULL) goto error;
97
98  if ((obj = PyObject_Str(pvalue)) == NULL) goto error;
99  exvalue = PyString_AsString(obj); Py_DECREF(obj);
100  if (exvalue == NULL) goto error;
101
102  snprintf(logmessage, NDG_MAX_LOGMESSAGE, "%s: %s: %s", prefix, exname, exvalue);
103  return;
104 
105 error:
106  sprintf("ndg_pyerr_to_logmessage: internal python error", logmessage);
107  return;
108}
Note: See TracBrowser for help on using the repository browser.