1 /* riscos.c - RISC OS stuff
2 * Copyright (C) 2001 Free Software Foundation, Inc.
4 * This file is part of GnuPG for RISC OS.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
35 #define __UNIXLIB_INTERNALS
36 #include <unixlib/swiparams.h> /* needed for MMM_TYPE_* definitions */
37 #undef __UNIXLIB_INTERNALS
40 /* RISC OS file open descriptor control list */
44 struct fds_item *next;
46 static struct fds_item *fds_list = NULL;
47 static int fdlist_initialized = 0;
50 /* local RISC OS functions */
53 is_read_only(const char *filename)
57 if (_swix(OS_File, _INR(0,1) | _OUT(0) | _OUT(5),
58 17, filename, &type, &attr))
59 log_fatal("Can't get file attributes for file \"%s\"!\n", filename);
62 log_fatal("Can't find file \"%s\"!\n", filename);
64 if (_swix(OS_File, _INR(0,1) | _IN(5), 4, filename, attr))
70 /* exported RISC OS functions */
73 riscos_global_defaults(void)
75 __riscosify_control = __RISCOSIFY_NO_PROCESS;
76 __feature_imagefs_is_file = 1;
80 riscos_load_module(const char *name, const char * const path[], int fatal)
84 /* Is module already loaded? */
85 if (!_swix(OS_Module, _INR(0,1), 18, name))
88 /* Check all the places where the module could be located */
89 for (i=0; path[i]; ++i)
90 if (!_swix(OS_Module, _INR(0,1), 1, path[i]))
93 /* Can't find module in the default locations */
95 log_fatal("Operation cannot be performed without \"%s\" module!\n",
98 log_info("Can't load \"%s\" module, continuing anyway!\n", name);
104 riscos_get_filetype_from_string(const char *string, int len)
108 if (strlen(string) < 5 || string[len - 4] != ',')
111 sscanf(string+len-3, "%3x", &result);
117 riscos_get_filetype(const char *filename)
121 if (_swix(OS_File, _INR(0,1) | _OUT(6), 23, filename, &result))
122 log_fatal("Can't get filetype for file \"%s\"!\n", filename);
128 riscos_set_filetype_by_number(const char *filename, int type)
130 if (_swix(OS_File, _INR(0,2), 18, filename, type))
131 log_fatal("Can't set filetype for file \"%s\"!\n"
132 "Is the file on a read-only file system?\n", filename);
136 riscos_set_filetype_by_mimetype(const char *filename, const char *mimetype)
140 if (_swix(MimeMap_Translate, _INR(0,2) | _OUT(3),
141 MMM_TYPE_MIME, mimetype, MMM_TYPE_RISCOS, &result))
142 log_fatal("Can't translate MIME type \"%s\"!\n", mimetype);
144 riscos_set_filetype_by_number(filename, result);
152 if (_swix(Wimp_ReadSysInfo, _IN(0) | _OUT(0), 3, &state))
153 log_fatal("Wimp_ReadSysInfo failed: Can't get WimpState (R0=3)!\n");
156 if (_swix(Wimp_ReadSysInfo, _IN(0) | _OUT(0), 5, &state))
157 log_fatal("Wimp_ReadSysInfo failed: "
158 "Can't get task handle (R0=5)!\n");
160 return (pid_t) state;
164 riscos_kill(pid_t pid, int sig)
166 int buf[4], iter = 0;
172 if (_swix(TaskManager_EnumerateTasks, _INR(0,2) | _OUT(0),
173 iter, buf, 16, &iter))
174 log_fatal("TaskManager_EnumerateTasks failed!\n");
179 return __set_errno(ESRCH);
183 riscos_access(const char *path, int amode)
185 /* Do additional check, i.e. whether path is on write-protected floppy */
186 if ((amode & W_OK) && is_read_only(path))
188 return access(path, amode);
196 if (_swix(OS_ReadC, _OUT(0) | _OUT(_FLAGS), &c, &flags))
197 log_fatal("OS_ReadC failed: Couldn't read from keyboard!\n");
199 log_fatal("OS_ReadC failed: Return Code = %i!\n", c);
206 riscos_dump_fdlist(void)
208 struct fds_item *iter = fds_list;
209 printf("List of open file descriptors:\n");
211 printf(" %i\n", iter->fd);
218 riscos_fdopenfile(const char *filename, const int allow_write)
223 fd = open(filename, O_CREAT | O_TRUNC | O_RDWR, S_IRUSR | S_IWUSR);
225 fd = open(filename, O_RDONLY);
227 log_error("Can't open file \"%s\": %i, %s!\n",
228 filename, errno, strerror(errno));
230 if (!fdlist_initialized) {
231 atexit (riscos_close_fds);
232 fdlist_initialized = 1;
236 fds_list = (struct fds_item *) m_alloc(sizeof(struct fds_item));
238 log_fatal("Can't claim memory for fdopenfile() buffer!\n");
246 riscos_close_fds(void)
249 struct fds_item *h = fds_list;
252 fp = fdopen (fds_list->fd, "a");
262 riscos_renamefile(const char *old, const char *new)
266 if (e = _swix(OS_FSControl, _INR(0,2), 25, old, new)) {
267 if (e->errnum == 214)
268 return __set_errno(ENOENT);
269 if (e->errnum == 176)
270 return __set_errno(EEXIST);
271 printf("Error during renaming: %i, %s!\n", e->errnum, e->errmess);
272 return __set_errno(EOPSYS);
278 riscos_gstrans(const char *old)
280 int size = 256, last;
283 buf = (char *) m_alloc(size);
285 log_fatal("Can't claim memory for OS_GSTrans buffer!\n");
286 while (_C & _swi(OS_GSTrans, _INR(0,2) | _OUT(2) | _RETURN(_FLAGS),
287 old, buf, size, &last)) {
289 tmp = (char *) m_realloc(buf, size);
291 log_fatal("Can't claim memory for OS_GSTrans buffer!\n");
296 tmp = (char *) m_realloc(buf, last + 1);
298 log_fatal("Can't realloc memory after OS_GSTrans!\n");
304 * Extract from a given path the filename component.
305 * (cloned from util/fileutil.c and then heavily modified)
308 riscos_make_basename(const char *filepath, const char *realfname)
310 char *result, *p = (char*)filepath-1;
313 if ( !(p=strrchr(filepath, DIRSEP_C)) )
314 if ( !(p=strrchr(filepath, ':')) )
318 result = m_alloc(i + 5);
320 log_fatal("Can't claim memory for riscos_make_basename() buffer!\n");
323 filetype = riscos_get_filetype( realfname );
325 result[i++] = "0123456789abcdef"[(filetype >> 8) & 0xf];
326 result[i++] = "0123456789abcdef"[(filetype >> 4) & 0xf];
327 result[i++] = "0123456789abcdef"[(filetype >> 0) & 0xf];
330 for(i=0; i<strlen(result); ++i)
337 #define RegEx_CompilePattern 0x52AC0
338 #define RegEx_Search 0x52AC2
339 #define RegEx_Free 0x52AC7
340 #define RegEx_CompileExtendedPattern 0x52AC9
342 static const char * const regex_path[] = {
344 "System:310.Modules.RegEx",
345 "System:Modules.RegEx",
350 riscos_check_regexp(const char *exp, const char *string, int debug)
352 static int regex_initialized = 0;
356 if (!regex_initialized)
357 regex_initialized = riscos_load_module("RegEx", regex_path, 0);
359 if (!regex_initialized) {
360 log_info("Regular expressions cannot be used!\n");
364 if (_swix(RegEx_CompileExtendedPattern, _INR(0,2) | _OUT(0) | _OUT(3),
367 log_info("RegEx could not compile pattern \"%s\".\n", exp);
368 log_info("ErrorCode = %i\n", ret);
372 if (_swix(RegEx_Search, _INR(0,4) | _OUT(5),
373 buf, string, -1, 0, -1,
375 log_info("RegEx error during execution of serach pattern \"%s\"\n",
377 log_info("on string \"%s\"\n", string);
381 _swix(RegEx_Free, _IN(0), buf);
384 log_debug("regexp \"%s\" on \"%s\": %s\n",exp,string,ret>=0?"YES":"NO");
391 riscos_list_openfiles(void)
396 for (i = 255; i >= 0; --i) {
397 if (_swix(OS_Args, _INR(0,2) | _IN(5) | _OUT(5), 7, i, 0, 0, &len))
400 name = (char *) m_alloc(1-len);
402 log_fatal("Can't claim memory for OS_Args buffer!\n");
404 if (_swix(OS_Args, _INR(0,2) | _IN(5), 7, i, name, 1-len)) {
406 log_fatal("Error when calling OS_Args(7)!\n");
409 if (_swix(OS_Args, _INR(0,1) | _OUT(0), 254, i, &len)) {
411 log_fatal("Error when calling OS_Args(254)!\n");
414 printf("%3i: %s (%c%c)\n", i, name,
415 (len & 0x40) ? 'R' : 0,
416 (len & 0x80) ? 'W' : 0);
423 riscos_not_implemented(const char *feature)
425 log_info("%s is not implemented in the RISC OS version!\n", feature);
428 #endif /* !__RISCOS__C__ */