Windows NT 4.0 source code leak
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

224 lines
4.9 KiB

4 years ago
  1. /* --------------------------------------------------------------------
  2. Microsoft OS/2 LAN Manager
  3. Copyright(c) Microsoft Corp., 1990
  4. RPC locator - Written by Steven Zeck
  5. This file contains a class definition for switch processing
  6. -------------------------------------------------------------------- */
  7. #include <switch.hxx>
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #if defined (RPC_CXX_20)
  11. #define RpcStrdup _strdup
  12. #else
  13. #define RpcStrdup strdup
  14. #endif
  15. #ifndef TRUE
  16. #define TRUE (~0)
  17. #endif
  18. #define FALSE 0
  19. #define USED(arg) ((void)(arg))
  20. typedef char *SZ;
  21. int TailMatch( SZ szPatt, SZ szIn);
  22. char *ProcessArgs(
  23. SwitchList aSLCur,
  24. char **aArgs
  25. // Process a list of arguments
  26. ) //-----------------------------------------------------------------------//
  27. {
  28. SZ szArgCur, szParm;
  29. for (; *aArgs; aArgs++) {
  30. for (SWitch *pSW = aSLCur; pSW->name; pSW++) {
  31. szArgCur = *aArgs;
  32. szParm = pSW->name;
  33. while (*szParm) {
  34. if (*szParm == '#') {
  35. // optional space between flag and argument
  36. if (!*szArgCur) {
  37. szArgCur = *(++aArgs);
  38. if (!szArgCur)
  39. return(aArgs[-1]);
  40. }
  41. if (TailMatch(szParm, szArgCur))
  42. goto found;
  43. }
  44. else if (*szParm == '*') {
  45. // no space allowed between flag and argument
  46. if (*szArgCur && TailMatch(szParm, szArgCur))
  47. goto found;
  48. break;
  49. }
  50. else {
  51. // do a case insensitive compare, pattern is always lower case
  52. if (*szArgCur >= 'A' && *szArgCur <= 'Z') {
  53. if ((*szArgCur | 0x20) != *szParm)
  54. break;
  55. }
  56. else if (*szArgCur != *szParm)
  57. break;
  58. szArgCur++; szParm++;
  59. if (! *szArgCur && !*szParm)
  60. goto found;
  61. }
  62. }
  63. }
  64. return(*aArgs); // parm in error
  65. found:
  66. if ((*pSW->pProcess)(pSW, szArgCur))
  67. return(*aArgs);
  68. }
  69. return(0); // sucess all parms matched
  70. }
  71. int TailMatch( // match substrings from right to left *^
  72. SZ szPatt, // pattern to match
  73. SZ szIn // input szInint to match
  74. //compare a tail szPatt (as in *.c) with a szIning. if there is no
  75. //tail, anything matches. (null szInings are detected elsewhere)
  76. //the current implementation only allows one wild card
  77. )//-----------------------------------------------------------------------//
  78. {
  79. register SZ szPattT = szPatt;
  80. register SZ szInT = szIn;
  81. if (szPattT[1] == 0) /* wild card is the last thing in the szPatt, it matches */
  82. return(TRUE);
  83. while(szPattT[1]) szPattT++; // find char in front of null in szPatt
  84. while(szInT[1]) szInT++; // find char in front of null in szIning to check
  85. while(1) { // check chars walking towards front
  86. // do a case insensitive compare, pattern is always lower case
  87. if (*szInT >= 'A' && *szInT <= 'Z') {
  88. if ((*szInT | 0x20) != *szPattT)
  89. return (FALSE);
  90. }
  91. else if (*szInT != *szPattT)
  92. return(FALSE);
  93. szInT--;
  94. szPattT--;
  95. /* if we're back at the beginning of the szPatt and
  96. * the szIn is either at the beginning (but not before)
  97. * or somewhere inside then we have a match. */
  98. if (szPattT == szPatt)
  99. return(szInT >= szIn);
  100. }
  101. return(FALSE);
  102. }
  103. int ProcessInt( // Set a flag numeric value *^
  104. SWitch *pSW, // pSW to modify
  105. SZ szText // pointer to number to set
  106. )/*-----------------------------------------------------------------------*/
  107. {
  108. for (SZ sz=szText; *sz; ++sz)
  109. if (*sz < '0' || *sz > '9')
  110. return(TRUE);
  111. *(int *)pSW->p = atoi(szText);
  112. return(FALSE);
  113. }
  114. int ProcessLong( // Set a flag numeric value *^
  115. SWitch *pSW, // pSW to modify
  116. SZ szText // pointer to number to set
  117. )/*-----------------------------------------------------------------------*/
  118. {
  119. for (SZ sz=szText; *sz; ++sz)
  120. if (*sz < '0' || *sz > '9')
  121. return(TRUE);
  122. *(long *)pSW->p = atol(szText);
  123. return(FALSE);
  124. }
  125. int ProcessChar( // Set a flag numeric value *^
  126. SWitch *pSW, // pSW to modify
  127. SZ szText // pointer to number to set
  128. )/*-----------------------------------------------------------------------*/
  129. {
  130. // if (*(SZ *)pSW->p) // can only set SZ's once
  131. // return(TRUE);
  132. *(SZ *)pSW->p = RpcStrdup(szText);
  133. return(FALSE);
  134. }
  135. int ProcessSetFlag( // Set a flag numeric value *^
  136. SWitch *pSW, // pSW to modify
  137. SZ szText // pointer to number to set
  138. )/*-----------------------------------------------------------------------*/
  139. {
  140. USED(szText);
  141. *(int *)pSW->p = TRUE;
  142. return(FALSE);
  143. }
  144. int ProcessResetFlag( // Set a flag numeric value *^
  145. SWitch *pSW, // pSW to modify
  146. SZ szText // pointer to number to set
  147. )/*-----------------------------------------------------------------------*/
  148. {
  149. USED(szText);
  150. *(int *)pSW->p = FALSE;
  151. return(FALSE);
  152. }
  153. int ProcessYesNo( // Set a flag numeric value, either Yes or No *^
  154. SWitch *pSW, // pSW to modify
  155. SZ szText // pointer to number to set
  156. )/*-----------------------------------------------------------------------*/
  157. {
  158. *(int *)pSW->p = (*szText | 0x20) == 'y';
  159. return(FALSE);
  160. }