ODAuto.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /* OpenDoors Online Software Programming Toolkit
  2. * (C) Copyright 1991 - 1999 by Brian Pirie.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. *
  18. *
  19. * File: ODAuto.c
  20. *
  21. * Description: Implements od_autodetect() for automatic detection of
  22. * terminal emulation supported by remote system.
  23. *
  24. * Revisions: Date Ver Who Change
  25. * ---------------------------------------------------------------
  26. * Oct 13, 1994 6.00 BP New file header format.
  27. * Oct 14, 1994 6.00 BP Standardized coding style.
  28. * Dec 31, 1994 6.00 BP Use new millisecond timer functions.
  29. * Nov 12, 1995 6.00 BP 32-bit portability.
  30. * Nov 13, 1995 6.00 BP Fixed non-functioning RIP autodetect.
  31. * Nov 16, 1995 6.00 BP Removed oddoor.h, added odcore.h.
  32. * Dec 12, 1995 6.00 BP Added entry, exit and kernel macros.
  33. * Dec 30, 1995 6.00 BP Added ODCALL for calling convention.
  34. * Feb 19, 1996 6.00 BP Changed version number to 6.00.
  35. * Mar 03, 1996 6.10 BP Begin version 6.10.
  36. * Aug 10, 2003 6.23 SH *nix support
  37. */
  38. #define BUILDING_OPENDOORS
  39. #include <string.h>
  40. #include <ctype.h>
  41. #include "OpenDoor.h"
  42. #include "ODStr.h"
  43. #include "ODTypes.h"
  44. #include "ODGen.h"
  45. #include "ODPlat.h"
  46. #include "ODCore.h"
  47. #include "ODKrnl.h"
  48. /* Private function prototypes. */
  49. static char ODWaitNoCase(char *pszWaitFor, tODMilliSec WaitTime);
  50. /* Number of attempts and timeout values for testing each terminal emulation */
  51. /* protocol. */
  52. #define ANSI_TRIES 1
  53. #define ANSI_WAIT 660 /* Time in milliseconds. */
  54. #define RIP_TRIES 1
  55. #define RIP_WAIT 660 /* Time in milliseconds. */
  56. /* Strings to use for autodetection. */
  57. #define ANSI_QUERY "\x1b[6n\r \r"
  58. #define ANSI_RESPONSE "\x1b["
  59. #define RIP_QUERY "\r\x1b[!\r \r"
  60. #define RIP_RESPONSE "RIP"
  61. /* Maximum number of characters to match with _waitnocase(). */
  62. #define MATCH_LEN 3
  63. /* ----------------------------------------------------------------------------
  64. * od_autodetect()
  65. *
  66. * Determines the terminal emulation capabilities of the remote communications
  67. * software, when possible. Turns on ANSI and/or RIP modes if they are
  68. * supported by the remote system.
  69. *
  70. * Parameters: nFlags - Currently unused.
  71. *
  72. * Return: void
  73. */
  74. ODAPIDEF void ODCALL od_autodetect(INT nFlags)
  75. {
  76. INT nCount;
  77. /* Log function entry if running in trace mode. */
  78. TRACE(TRACE_API, "od_autodetect()");
  79. /* Initialize OpenDoors if it hasn't aready been done. */
  80. if(!bODInitialized) od_init();
  81. OD_API_ENTRY();
  82. /* Temporary code that will be optimized out, which prevents a compiler */
  83. /* warning from being generated for the currently unused flags parameter. */
  84. (void)nFlags;
  85. /* If operating in local mode, turn on ANSI mode, but not RIP. */
  86. if(od_control.baud == 0)
  87. {
  88. od_control.user_ansi = TRUE;
  89. OD_API_EXIT();
  90. return;
  91. }
  92. /* If user_ansi is not set, attempt to determine ANSI capabilities. */
  93. if(!od_control.user_ansi)
  94. {
  95. /* Clear inbound keyboard buffer. */
  96. od_clear_keybuffer();
  97. /* Try twice to test ANSI capabilities. */
  98. for(nCount = 0; nCount < ANSI_TRIES; ++nCount)
  99. {
  100. /* Send a string that an ANSI capable terminal will usually */
  101. /* respond to. */
  102. od_disp(ANSI_QUERY, strlen(ANSI_QUERY), FALSE);
  103. /* Wait for response expected from an ANSI terminal, for up to */
  104. /* 12/18.2 second. */
  105. if(ODWaitNoCase(ANSI_RESPONSE, ANSI_WAIT))
  106. {
  107. /* If expected sequence was received, turn on ANSI mode and */
  108. /* exit the loop. */
  109. od_control.user_ansi = TRUE;
  110. break;
  111. }
  112. }
  113. od_clear_keybuffer();
  114. }
  115. /* If user_rip is not set, attempt to determine RIP capabilities. */
  116. if(!od_control.user_rip)
  117. {
  118. /* Clear inbound keyboard buffer. */
  119. od_clear_keybuffer();
  120. /* Try twice to test RIP capabilities. */
  121. for(nCount = 0; nCount < RIP_TRIES; ++nCount)
  122. {
  123. /* Send a string that a RIP capable terminal will usually */
  124. /* respond to. */
  125. od_disp(RIP_QUERY, strlen(RIP_QUERY), FALSE);
  126. /* Wait for response expected from a RIP terminal. */
  127. if(ODWaitNoCase(RIP_RESPONSE, RIP_WAIT))
  128. {
  129. /* If expected sequence was received, turn on RIP mode and */
  130. /* exit the loop. */
  131. od_control.user_rip = TRUE;
  132. break;
  133. }
  134. }
  135. od_clear_keybuffer();
  136. }
  137. OD_API_EXIT();
  138. }
  139. /* ----------------------------------------------------------------------------
  140. * ODWaitNoCase() *** PRIVATE FUNCTION ***
  141. *
  142. * Waits up to the specified maximum time for a specified string to be sent
  143. * from the remote system. String matching is not case sensitive.
  144. *
  145. * Parameters: pszWaitFor - String to wait for.
  146. *
  147. * WaitTime - Maximum time, in milliseconds, to wait.
  148. *
  149. * Return: TRUE on success, FALSE on failure.
  150. */
  151. static char ODWaitNoCase(char *pszWaitFor, tODMilliSec WaitTime)
  152. {
  153. tODTimer Timer;
  154. char szReceived[MATCH_LEN + 1];
  155. int nCount;
  156. char chReceived;
  157. int nMatchChars = MIN(MATCH_LEN, strlen(pszWaitFor));
  158. ASSERT(pszWaitFor != NULL);
  159. ASSERT(strlen(pszWaitFor) != 0);
  160. ASSERT(WaitTime >= 0);
  161. ODTimerStart(&Timer, WaitTime);
  162. for(nCount = 0; nCount <= MATCH_LEN; ++nCount)
  163. {
  164. szReceived[nCount] = '\0';
  165. }
  166. do
  167. {
  168. if((chReceived = od_get_key(FALSE)) != 0)
  169. {
  170. for(nCount = 0; nCount < MATCH_LEN - 1; ++ nCount)
  171. {
  172. szReceived[nCount] = szReceived[nCount + 1];
  173. }
  174. szReceived[MATCH_LEN - 1] = chReceived;
  175. if(strnicmp(szReceived + (MATCH_LEN - nMatchChars), pszWaitFor,
  176. nMatchChars) == 0)
  177. {
  178. return(TRUE);
  179. }
  180. }
  181. } while(!ODTimerElapsed(&Timer));
  182. return(FALSE);
  183. }