Guido van Rossum | 228d807 | 2001-03-02 05:58:11 +0000 | [diff] [blame] | 1 | /* RISCOS module implementation */ |
| 2 | |
| 3 | #include "h.osfscontrol" |
| 4 | #include "h.osgbpb" |
| 5 | #include "h.os" |
| 6 | #include "h.osfile" |
| 7 | |
| 8 | #include "Python.h" |
| 9 | |
| 10 | #include <errno.h> |
| 11 | |
| 12 | static os_error *e; |
| 13 | |
| 14 | static PyObject *RiscosError; /* Exception riscos.error */ |
| 15 | |
| 16 | static PyObject *riscos_oserror(void) |
| 17 | { PyErr_SetString(RiscosError,e->errmess); |
| 18 | return 0; |
| 19 | } |
| 20 | |
| 21 | static PyObject *riscos_error(char *s) { PyErr_SetString(RiscosError,s);return 0;} |
| 22 | |
| 23 | /* RISCOS file commands */ |
| 24 | |
| 25 | static PyObject *riscos_remove(PyObject *self,PyObject *args) |
| 26 | { char *path1; |
| 27 | if (!PyArg_Parse(args, "s", &path1)) return NULL; |
| 28 | if (remove(path1)) return PyErr_SetFromErrno(RiscosError); |
| 29 | Py_INCREF(Py_None); |
| 30 | return Py_None; |
| 31 | } |
| 32 | |
| 33 | static PyObject *riscos_rename(PyObject *self,PyObject *args) |
| 34 | { char *path1, *path2; |
| 35 | if (!PyArg_Parse(args, "(ss)", &path1, &path2)) return NULL; |
| 36 | if (rename(path1,path2)) return PyErr_SetFromErrno(RiscosError);; |
| 37 | Py_INCREF(Py_None); |
| 38 | return Py_None; |
| 39 | } |
| 40 | |
| 41 | static PyObject *riscos_system(PyObject *self,PyObject *args) |
| 42 | { char *command; |
| 43 | if (!PyArg_Parse(args, "s", &command)) return NULL; |
| 44 | return PyInt_FromLong(system(command)); |
| 45 | } |
| 46 | |
| 47 | static PyObject *riscos_chdir(PyObject *self,PyObject *args) |
| 48 | { char *path; |
| 49 | if (!PyArg_Parse(args, "s", &path)) return NULL; |
| 50 | e=xosfscontrol_dir(path); |
| 51 | if(e) return riscos_oserror(); |
| 52 | Py_INCREF(Py_None); |
| 53 | return Py_None; |
| 54 | } |
| 55 | |
| 56 | static PyObject *canon(char *path) |
| 57 | { int len; |
| 58 | PyObject *obj; |
| 59 | char *buf; |
| 60 | e=xosfscontrol_canonicalise_path(path,0,0,0,0,&len); |
| 61 | if(e) return riscos_oserror(); |
| 62 | obj=PyString_FromStringAndSize(NULL,-len); |
| 63 | if(obj==NULL) return NULL; |
| 64 | buf=PyString_AsString(obj); |
| 65 | e=xosfscontrol_canonicalise_path(path,buf,0,0,1-len,&len); |
| 66 | if(len!=1) return riscos_error("Error expanding path"); |
| 67 | if(!e) return obj; |
| 68 | Py_DECREF(obj); |
| 69 | return riscos_oserror(); |
| 70 | } |
| 71 | |
| 72 | static PyObject *riscos_getcwd(PyObject *self,PyObject *args) |
| 73 | { if(!PyArg_NoArgs(args)) return NULL; |
| 74 | return canon("@"); |
| 75 | } |
| 76 | |
| 77 | static PyObject *riscos_expand(PyObject *self,PyObject *args) |
| 78 | { char *path; |
| 79 | if (!PyArg_Parse(args, "s", &path)) return NULL; |
| 80 | return canon(path); |
| 81 | } |
| 82 | |
| 83 | static PyObject *riscos_mkdir(PyObject *self,PyObject *args) |
| 84 | { char *path; |
| 85 | int mode; |
| 86 | if (!PyArg_ParseTuple(args, "s|i", &path, &mode)) return NULL; |
| 87 | e=xosfile_create_dir(path,0); |
| 88 | if(e) return riscos_oserror(); |
| 89 | Py_INCREF(Py_None); |
| 90 | return Py_None; |
| 91 | } |
| 92 | |
| 93 | static PyObject *riscos_listdir(PyObject *self,PyObject *args) |
| 94 | { char *path,buf[256]; |
| 95 | PyObject *d, *v; |
| 96 | int c=0,count; |
| 97 | if (!PyArg_Parse(args, "s", &path)) return NULL; |
| 98 | d=PyList_New(0); |
| 99 | if(!d) return NULL; |
| 100 | for(;;) |
| 101 | { e=xosgbpb_dir_entries(path,(osgbpb_string_list*)buf, |
| 102 | 1,c,256,0,&count,&c); |
| 103 | if(e) |
| 104 | { Py_DECREF(d);return riscos_oserror(); |
| 105 | } |
| 106 | if(count) |
| 107 | { v=PyString_FromString(buf); |
| 108 | if(!v) { Py_DECREF(d);return 0;} |
| 109 | if(PyList_Append(d,v)) {Py_DECREF(d);Py_DECREF(v);return 0;} |
| 110 | } |
| 111 | if(c==-1) break; |
| 112 | } |
| 113 | return d; |
| 114 | } |
| 115 | |
| 116 | static PyObject *riscos_stat(PyObject *self,PyObject *args) |
| 117 | { char *path; |
| 118 | int ob,len; |
| 119 | bits t=0; |
| 120 | bits ld,ex,at,ft,mode; |
| 121 | if (!PyArg_Parse(args, "s", &path)) return NULL; |
| 122 | e=xosfile_read_stamped_no_path(path,&ob,&ld,&ex,&len,&at,&ft); |
| 123 | if(e) return riscos_oserror(); |
| 124 | switch (ob) |
| 125 | { case osfile_IS_FILE:mode=0100000;break; /* OCTAL */ |
| 126 | case osfile_IS_DIR:mode=040000;break; |
| 127 | case osfile_IS_IMAGE:mode=0140000;break; |
| 128 | default:return riscos_error("Not found"); |
| 129 | } |
| 130 | if(ft!=-1) t=unixtime(ld,ex); |
| 131 | mode|=(at&7)<<6; |
| 132 | mode|=((at&112)*9)>>4; |
| 133 | return Py_BuildValue("(lllllllllllll)", |
| 134 | (long)mode,/*st_mode*/ |
| 135 | 0,/*st_ino*/ |
| 136 | 0,/*st_dev*/ |
| 137 | 0,/*st_nlink*/ |
| 138 | 0,/*st_uid*/ |
| 139 | 0,/*st_gid*/ |
| 140 | (long)len,/*st_size*/ |
| 141 | (long)t,/*st_atime*/ |
| 142 | (long)t,/*st_mtime*/ |
| 143 | (long)t,/*st_ctime*/ |
| 144 | (long)ft,/*file type*/ |
| 145 | (long)at,/*attributes*/ |
| 146 | (long)ob/*object type*/ |
| 147 | ); |
| 148 | } |
| 149 | |
| 150 | static PyObject *riscos_chmod(PyObject *self,PyObject *args) |
| 151 | { char *path; |
| 152 | bits mode; |
| 153 | bits attr; |
| 154 | attr=(mode&0x700)>>8; |
| 155 | attr|=(mode&7)<<4; |
| 156 | if (!PyArg_Parse(args, "(si)", &path,(int*)&mode)) return NULL; |
| 157 | e=xosfile_write_attr(path,attr); |
| 158 | if(e) return riscos_oserror(); |
| 159 | Py_INCREF(Py_None); |
| 160 | return Py_None; |
| 161 | } |
| 162 | |
| 163 | static PyObject *riscos_utime(PyObject *self,PyObject *args) |
| 164 | { char *path; |
| 165 | int x,y; |
| 166 | if (!PyArg_Parse(args, "(s(ii))", &path,&x,&y)) return NULL; |
| 167 | e=xosfile_stamp(path); |
| 168 | if(e) return riscos_oserror(); |
| 169 | Py_INCREF(Py_None); |
| 170 | return Py_None; |
| 171 | } |
| 172 | |
| 173 | static PyObject *riscos_settype(PyObject *self,PyObject *args) |
| 174 | { char *path,*name; |
| 175 | int type; |
| 176 | if (!PyArg_Parse(args, "(si)", &path,&type)) |
| 177 | { PyErr_Clear(); |
| 178 | if (!PyArg_Parse(args, "(ss)", &path,&name)) return NULL; |
| 179 | e=xosfscontrol_file_type_from_string(name,(bits*)&type); |
| 180 | if(e) return riscos_oserror(); |
| 181 | } |
| 182 | e=xosfile_set_type(path,type); |
| 183 | if(e) return riscos_oserror(); |
| 184 | Py_INCREF(Py_None); |
| 185 | return Py_None; |
| 186 | } |
| 187 | |
| 188 | static PyObject *riscos_getenv(PyObject *self,PyObject *args) |
| 189 | { char *name,*value; |
| 190 | if(!PyArg_Parse(args,"s",&name)) return NULL; |
| 191 | value=getenv(name); |
| 192 | if(value) return PyString_FromString(value); |
| 193 | Py_INCREF(Py_None); |
| 194 | return Py_None; |
| 195 | } |
| 196 | |
| 197 | static PyObject *riscos_putenv(PyObject *self,PyObject *args) |
| 198 | { char *name,*value; |
| 199 | int len; |
| 200 | os_var_type type=os_VARTYPE_LITERAL_STRING; |
| 201 | if(!PyArg_ParseTuple(args,"ss|i",&name,&value,&type)) return NULL; |
| 202 | if(type!=os_VARTYPE_STRING&&type!=os_VARTYPE_MACRO&&type!=os_VARTYPE_EXPANDED |
| 203 | &&type!=os_VARTYPE_LITERAL_STRING) |
| 204 | return riscos_error("Bad putenv type"); |
| 205 | len=strlen(value); |
| 206 | if(type!=os_VARTYPE_LITERAL_STRING) len++; |
| 207 | /* Other types need null terminator! */ |
| 208 | e=xos_set_var_val(name,(byte*)value,len,0,type,0,0); |
| 209 | if(e) return riscos_oserror(); |
| 210 | Py_INCREF(Py_None); |
| 211 | return Py_None; |
| 212 | } |
| 213 | |
| 214 | static PyObject *riscos_delenv(PyObject *self,PyObject *args) |
| 215 | { char *name; |
| 216 | if(!PyArg_Parse(args,"s",&name)) return NULL; |
| 217 | e=xos_set_var_val(name,NULL,-1,0,0,0,0); |
| 218 | if(e) return riscos_oserror(); |
| 219 | Py_INCREF(Py_None); |
| 220 | return Py_None; |
| 221 | } |
| 222 | |
| 223 | static PyObject *riscos_getenvdict(PyObject *self,PyObject *args) |
| 224 | { PyObject *dict; |
| 225 | char value[257]; |
| 226 | char *which="*"; |
| 227 | int size; |
| 228 | char *context=NULL; |
| 229 | if(!PyArg_ParseTuple(args,"|s",&which)) return NULL; |
| 230 | dict = PyDict_New(); |
| 231 | if (!dict) return NULL; |
| 232 | /* XXX This part ignores errors */ |
| 233 | while(!xos_read_var_val(which,value,sizeof(value)-1,(int)context, |
| 234 | os_VARTYPE_EXPANDED,&size,(int *)&context,0)) |
| 235 | { PyObject *v; |
| 236 | value[size]='\0'; |
| 237 | v = PyString_FromString(value); |
| 238 | if (v == NULL) continue; |
| 239 | PyDict_SetItemString(dict, context, v); |
| 240 | Py_DECREF(v); |
| 241 | } |
| 242 | return dict; |
| 243 | } |
| 244 | |
| 245 | static PyMethodDef riscos_methods[] = { |
| 246 | |
| 247 | {"unlink", riscos_remove}, |
| 248 | {"remove", riscos_remove}, |
| 249 | {"rename", riscos_rename}, |
| 250 | {"system", riscos_system}, |
| 251 | {"rmdir", riscos_remove}, |
| 252 | {"chdir", riscos_chdir}, |
| 253 | {"getcwd", riscos_getcwd}, |
| 254 | {"expand", riscos_expand}, |
| 255 | {"mkdir", riscos_mkdir,1}, |
| 256 | {"listdir", riscos_listdir}, |
| 257 | {"stat", riscos_stat}, |
| 258 | {"lstat", riscos_stat}, |
| 259 | {"chmod", riscos_chmod}, |
| 260 | {"utime", riscos_utime}, |
| 261 | {"settype", riscos_settype}, |
| 262 | {"getenv", riscos_getenv}, |
| 263 | {"putenv", riscos_putenv}, |
| 264 | {"delenv", riscos_delenv}, |
| 265 | {"getenvdict", riscos_getenvdict,1}, |
| 266 | {NULL, NULL} /* Sentinel */ |
| 267 | }; |
| 268 | |
| 269 | |
| 270 | |
| 271 | void |
| 272 | initriscos() |
| 273 | { |
| 274 | PyObject *m, *d; |
| 275 | |
| 276 | m = Py_InitModule("riscos", riscos_methods); |
| 277 | d = PyModule_GetDict(m); |
| 278 | |
| 279 | /* Initialize riscos.error exception */ |
| 280 | RiscosError = PyString_FromString("riscos.error"); |
| 281 | if (RiscosError == NULL || PyDict_SetItemString(d, "error", RiscosError) != 0) |
| 282 | Py_FatalError("can't define riscos.error"); |
| 283 | } |