HTDOS.c 4.6 KB

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