Debian Codegraph @

WWW/Library/Implementation/HTDOS.c

raw
1 /*
2 * $LynxId: HTDOS.c,v 1.40 2013/11/28 11:11:05 tom Exp $
3 * DOS specific routines
4 */
5
6 #include <HTUtils.h>
7 #include <LYUtils.h>
8 #include <HTDOS.h>
9 #include <LYStrings.h>
10
11 #include <LYLeaks.h>
12
13 #ifdef _WINDOWS
14 #include <LYGlobalDefs.h>
15 #include <HTAlert.h>
16 #endif
17
18 /*
19 * Make a copy of the source argument in the result, allowing some extra
20 * space so we can append directly onto the result without reallocating.
21 */
22 static char *copy_plus(char **result, const char *source)
23 {
24 int length = (int) strlen(source);
25 int extra = 10;
26 int n;
27
28 for (n = 0; n < length; ++n) {
29 if (source[n] == ' ')
30 ++extra;
31 }
32
33 HTSprintf0(result, "%-*s", length + extra, source);
34 (*result)[length] = 0;
35 return (*result);
36 }
37
38 /* PUBLIC HTDOS_wwwName()
39 * CONVERTS DOS Name into WWW Name
40 * ON ENTRY:
41 * dosname DOS file specification (NO NODE)
42 *
43 * ON EXIT:
44 * returns WWW file specification
45 *
46 */
47 const char *HTDOS_wwwName(const char *dosname)
48 {
49 static char *wwwname = NULL;
50 char *cp_url = copy_plus(&wwwname, dosname);
51 int wwwname_len;
52 char ch;
53
54 while ((ch = *dosname) != '\0') {
55 switch (ch) {
56 case '\\':
57 /* convert dos backslash to unix-style */
58 *cp_url++ = '/';
59 break;
60 case ' ':
61 *cp_url++ = '%';
62 *cp_url++ = '2';
63 *cp_url++ = '0';
64 break;
65 default:
66 *cp_url++ = ch;
67 break;
68 }
69 dosname++;
70 }
71 *cp_url = '\0';
72
73 wwwname_len = (int) strlen(wwwname);
74 if (wwwname_len > 1)
75 cp_url--; /* point last char */
76
77 if (wwwname_len > 3 && *cp_url == '/') {
78 cp_url++;
79 *cp_url = '\0';
80 }
81 return (wwwname);
82 }
83
84 /*
85 * Convert slashes from Unix to DOS
86 */
87 char *HTDOS_slashes(char *path)
88 {
89 char *s;
90
91 for (s = path; *s != '\0'; ++s) {
92 if (*s == '/') {
93 *s = '\\';
94 }
95 }
96 return path;
97 }
98
99 /* PUBLIC HTDOS_name()
100 * CONVERTS WWW name into a DOS name
101 * ON ENTRY:
102 * wwwname WWW file name
103 *
104 * ON EXIT:
105 * returns DOS file specification
106 */
107 char *HTDOS_name(const char *wwwname)
108 {
109 static char *result = NULL;
110 int joe;
111
112 #if defined(SH_EX) /* 2000/03/07 (Tue) 18:32:42 */
113 if (unsafe_filename(wwwname)) {
114 HTUserMsg2("unsafe filename : %s", wwwname);
115 copy_plus(&result, "BAD_LOCAL_FILE_NAME");
116 } else {
117 copy_plus(&result, wwwname);
118 }
119 #else
120 copy_plus(&result, wwwname);
121 #endif
122 #ifdef __DJGPP__
123 if (result[0] == '/'
124 && result[1] == 'd'
125 && result[2] == 'e'
126 && result[3] == 'v'
127 && result[4] == '/'
128 && isalpha(result[5])) {
129 return (result);
130 }
131 #endif /* __DJGPP__ */
132
133 (void) HTDOS_slashes(result);
134
135 /* pesky leading slash, rudiment from file://localhost/ */
136 /* the rest of path may be with or without drive letter */
137 if ((result[1] != '\\') && (result[0] == '\\')) {
138 for (joe = 0; (result[joe] = result[joe + 1]) != 0; joe++) ;
139 }
140 /* convert '|' after the drive letter to ':' */
141 if (isalpha(UCH(result[0])) && result[1] == '|') {
142 result[1] = ':';
143 }
144 #ifdef _WINDOWS /* 1998/04/02 (Thu) 08:59:48 */
145 if (LYLastPathSep(result) != NULL
146 && !LYIsDosDrive(result)) {
147 char temp_buff[LY_MAXPATH];
148
149 sprintf(temp_buff, "%.3s\\%.*s", windows_drive,
150 (int) (sizeof(temp_buff) - 5), result);
151 StrAllocCopy(result, temp_buff);
152 }
153 #endif
154 /*
155 * If we have only a device, add a trailing slash. Otherwise it just
156 * refers to the current directory on the given device.
157 */
158 if (LYLastPathSep(result) == NULL
159 && LYIsDosDrive(result))
160 LYAddPathSep0(result);
161
162 CTRACE((tfp, "HTDOS_name changed `%s' to `%s'\n", wwwname, result));
163 return (result);
164 }
165
166 #ifdef WIN_EX
167 char *HTDOS_short_name(const char *path)
168 {
169 static char sbuf[LY_MAXPATH];
170 char *ret;
171 DWORD r;
172
173 if (StrChr(path, '/'))
174 path = HTDOS_name(path);
175 r = GetShortPathName(path, sbuf, sizeof sbuf);
176 if (r >= sizeof(sbuf) || r == 0) {
177 ret = LYStrNCpy(sbuf, path, sizeof(sbuf));
178 } else {
179 ret = sbuf;
180 }
181 return ret;
182 }
183 #endif
184
185 #if defined(DJGPP)
186 /*
187 * Poll tcp/ip lib and yield to DPMI-host while nothing in
188 * keyboard buffer (head = tail) (simpler than kbhit).
189 * This is required to be able to finish off dead sockets,
190 * answer pings etc.
191 */
192 #include <pc.h>
193 #include <dpmi.h>
194 #include <libc/farptrgs.h>
195 #include <go32.h>
196
197 void djgpp_idle_loop(void)
198 {
199 while (_farpeekw(_dos_ds, 0x41a) == _farpeekw(_dos_ds, 0x41c)) {
200 tcp_tick(NULL);
201 __dpmi_yield();
202 #if defined(USE_SLANG)
203 if (SLang_input_pending(1))
204 break;
205 #endif
206 }
207 }
208
209 /* PUBLIC getxkey()
210 * Replaces libc's getxkey() with polling of tcp/ip
211 * library (WatTcp or Watt-32). *
212 * ON EXIT:
213 * returns extended keypress.
214 */
215
216 /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
217
218 int getxkey(void)
219 {
220 #if defined(DJGPP_KEYHANDLER)
221 __dpmi_regs r;
222
223 djgpp_idle_loop();
224
225 r.h.ah = 0x10;
226 __dpmi_int(0x16, &r);
227
228 if (r.h.al == 0x00)
229 return 0x0100 | r.h.ah;
230 if (r.h.al == 0xe0)
231 return 0x0200 | r.h.ah;
232 return r.h.al;
233
234 #elif defined(USE_SLANG)
235 djgpp_idle_loop();
236 return SLkp_getkey();
237 #else
238 /* PDcurses uses myGetChar() in LYString.c */
239 #endif
240 }
241 #endif /* DJGPP */