Debian Codegraph @

src/_riscos.c

raw symbols SCIP
1 // ACME - a crossassembler for producing 6502/65c02/65816/65ce02 code.
2 // Copyright (C) 1998-2024 Marco Baye
3 // Have a look at "acme.c" for further info
4 //
5 // Platform specific stuff (in this case, for RISC OS)
6 #ifndef platform_C
7 #define platform_C
8
9
10 #include <stdlib.h>
11 #include <string.h> // for strlen and memmove
12 #include <kernel.h>
13 #include "input.h" // for input_get_location()
14
15
16 // constants
17
18 // SWIs
19 #define OS_FILE 0x00008
20 #define XDDEUTILS_THROWBACKSTART 0x62587
21 #define XDDEUTILS_THROWBACKSEND 0x62588
22 #define XDDEUTILS_THROWBACKEND 0x62589
23
24
25 // variables
26 bits RISCOS_flags = 0; // used to store platform-specific flags
27
28
29 // exit handler: if throwback was used, de-register now
30 void RISCOS_exit(void)
31 {
32 _kernel_swi_regs regs;
33
34 if (RISCOS_flags & RISCOSFLAG_THROWN) {
35 _kernel_swi(XDDEUTILS_THROWBACKEND, &regs, &regs);
36 RISCOS_flags &= ~RISCOSFLAG_THROWN;
37 }
38 }
39
40
41 // used as PLATFORM_INIT: registers exit handler
42 void RISCOS_entry(void)
43 {
44 atexit(RISCOS_exit);
45 }
46
47
48 // convert UNIX-style path name to local platform style path name and decide
49 // whether path is absolute or relative. p points to the terminated input
50 // string, to be overwritten with the terminated output string.
51 // there is enough space allocated for the output string to be _one_(1!) byte
52 // longer than the input string!
53 // conversions done for RISC OS:
54 // "path/to/file.ext" -> "path.to.file/ext"
55 // "/absolute/path" -> "$.absolute.path" (this needs the one extra byte!)
56 // "../../twolevelsup" -> "^.^.twolevelsup"
57 void platform_convert_path(boolean *is_absolute, char *readptr)
58 {
59 char *writeptr,
60 previous;
61 size_t bytes;
62
63 // init
64 *is_absolute = FALSE; // there are several ways for this to become true
65 // check for leading '/' and add '$' prefix:
66 if (*readptr == '/') {
67 // this is the extremely unlikely situation where an extra byte
68 // is needed, so let's get it done:
69 *is_absolute = TRUE;
70 bytes = strlen(readptr) + 1; // count terminator as well
71 memmove(readptr + 1, readptr, bytes); // "/path\0\0" -> "//path\0"
72 *(readptr++) = '$'; // "//path\0" -> "$/path\0"
73 }
74
75 writeptr = readptr;
76 previous = '/'; // make sure ".." substitution also works for the first component
77 // now scan remaining characters and convert all "../" components to "^."
78 while (*readptr) {
79 if ((previous == '/')
80 && (*readptr == '.')
81 && (readptr[1] == '.')
82 && (readptr[2] == '/')) {
83 readptr += 2;
84 *(writeptr++) = '^';
85 }
86 previous = *readptr; // remember for next ".." check
87 if ((*readptr == ':') // path prefixes like "myproject:"...
88 || (*readptr == '$') // ... or root directory indicator '$'...
89 || (*readptr == '&') // ... or "user root directory" indicator '&'...
90 || (*readptr == '%')) { // ... or "user library directory" indicator '%'...
91 *is_absolute = TRUE; // ...mean path is absolute
92 }
93 // convert characters
94 if (*readptr == '.') {
95 *writeptr = '/';
96 } else if (*readptr == '/') {
97 *writeptr = '.';
98 } else if (*readptr == '?') {
99 *writeptr = '#';
100 } else if (*readptr == '#') {
101 *writeptr = '?';
102 } else {
103 *writeptr = *readptr; // copy character
104 }
105 ++readptr;
106 ++writeptr;
107 }
108 *writeptr = *readptr; // copy terminator
109 }
110
111
112 // setting the created files' types
113 void RISCOS_set_filetype(const char *filename, int file_type)
114 {
115 _kernel_swi_regs regs;
116
117 regs.r[0] = 18; // reason code (set file type)
118 regs.r[1] = (int) filename;
119 regs.r[2] = file_type;
120 _kernel_swi(OS_FILE, &regs, &regs);
121 }
122
123
124 // throwback protocol: "type" can be 0, 1 or 2 (DDEUtils message types)
125 void RISCOS_throwback(const char *message, int type)
126 {
127 struct location location;
128 _kernel_swi_regs regs;
129
130 // only use throwback protocol if wanted
131 if ((RISCOS_flags & RISCOSFLAG_THROWBACK) == 0)
132 return;
133
134 input_get_location(&location);
135
136 // if this is the first throwback, set it up and send info
137 if ((RISCOS_flags & RISCOSFLAG_THROWN) == 0) {
138 RISCOS_flags |= RISCOSFLAG_THROWN;
139 _kernel_swi(XDDEUTILS_THROWBACKSTART, &regs, &regs);
140 regs.r[0] = 0;
141 regs.r[1] = 0;
142 // regs.r[2] = (int) toplevel_source;
143 regs.r[2] = (int) location.plat_filename;
144 _kernel_swi(XDDEUTILS_THROWBACKSEND, &regs, &regs);
145 }
146 // send throwback message
147 regs.r[0] = 1;
148 regs.r[1] = 0;
149 regs.r[2] = (int) location.plat_filename;
150 regs.r[3] = location.line_number;
151 regs.r[4] = type;
152 regs.r[5] = (int) message;
153 _kernel_swi(XDDEUTILS_THROWBACKSEND, &regs, &regs);
154 }
155
156
157 #endif