Leaked source code of windows server 2003
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.

128 lines
3.6 KiB

  1. /******************************Module*Header*******************************\
  2. * Module Name: extparse.cxx
  3. *
  4. * Copyright (c) 1998-2000 Microsoft Corporation
  5. *
  6. \**************************************************************************/
  7. #include "precomp.hxx"
  8. /* Command line parsing routines
  9. *
  10. * This routine should return an array of char* 's in the idx parameter
  11. * with the beginning of each token in the array.
  12. * It also returns the number of tokens found.
  13. */
  14. int parse_Tokenizer(char *cmdstr, char **tok) {
  15. char *seps=" \t\n"; //white space separators
  16. int tok_count = 0; //the token count
  17. char *token = strtok(cmdstr, seps); //get the first token
  18. while(token) {
  19. tok[tok_count++]=token;
  20. token = strtok(NULL, seps);
  21. }
  22. return tok_count;
  23. }
  24. /* This routine finds the token specified in srchtok
  25. * and returns the index into tok.
  26. * A return value of -1 is used if the token is not found.
  27. *
  28. * Generally we use the case insensitive version (parse_iFindToken)
  29. * but occasionally we need the case sensitive version (parse_FindToken).
  30. */
  31. int parse_FindToken(char **tok, int ntok, char *srchtok) {
  32. for(int i=0; i<ntok; i++) {
  33. if(strcmp(tok[i], srchtok)==0) break;
  34. }
  35. if(i>=ntok) return -1;
  36. return i;
  37. }
  38. int parse_iFindToken(char **tok, int ntok, char *srchtok) {
  39. for(int i=0; i<ntok; i++) {
  40. if(_strnicmp(tok[i], srchtok, strlen(srchtok))==0) break;
  41. }
  42. if(i>=ntok) return -1;
  43. return i;
  44. }
  45. /* Verifies that the given token at tok[tok_pos] is a switch
  46. * and contains the switch value sw.
  47. *
  48. * Both case sensitive and insensitive versions.
  49. */
  50. int parse_iIsSwitch(char **tok, int tok_pos, char sw) {
  51. if(tok_pos<0) return 0;
  52. char *s=tok[tok_pos];
  53. if((s[0]=='-')||(s[0]=='/')) { //is a switch.
  54. for(s++; *s; s++) {
  55. if(toupper(*s)==toupper(sw)) {return 1;}
  56. }
  57. }
  58. return 0;
  59. }
  60. int parse_IsSwitch(char **tok, int tok_pos, char sw) {
  61. if(tok_pos<0) return 0;
  62. char *s=tok[tok_pos];
  63. if((s[0]=='-')||(s[0]=='/')) { //is a switch.
  64. for(s++; *s; s++) {
  65. if(*s==sw) {return 1;} //search each char
  66. }
  67. }
  68. return 0;
  69. }
  70. /* Finds a switch in a given list of tokens.
  71. * of the form -xxx(sw)xxx or /xxx(sw)xxx
  72. * example:
  73. * searching for 'a' in -jklabw returns true.
  74. *
  75. * Again both case sensitive and insensitive versions are needed.
  76. */
  77. int parse_FindSwitch(char **tok, int ntok, char sw) {
  78. for(int i=0; i<ntok; i++) { //search each token
  79. if(parse_IsSwitch(tok, i, sw)) {return i;} //found it? return position.
  80. }
  81. return -1;
  82. }
  83. int parse_iFindSwitch(char **tok, int ntok, char sw) {
  84. for(int i=0; i<ntok; i++) {
  85. if(parse_IsSwitch(tok, i, sw)) {return i;} //found it? return position.
  86. }
  87. return -1;
  88. }
  89. /* Find the first non-switch token starting from position start
  90. * Will find token at position start
  91. */
  92. int parse_FindNonSwitch(char **tok, int ntok, int start) {
  93. for(int i=start; i<ntok; i++) {
  94. if((tok[i][0]!='-')&&(tok[i][0]!='/')) break;
  95. }
  96. if(i>=ntok) return -1;
  97. return i;
  98. }
  99. /* case insensitive token comparer.
  100. * returns 1 if chk==tok[tok_pos] otherwise returns 0
  101. *
  102. * Pay careful attention to the length specifier in the _strnicmp
  103. */
  104. int parse_iIsToken(char **tok, int tok_pos, char *chk) {
  105. if(tok_pos<0) {return 0;}
  106. return (_strnicmp(tok[tok_pos], chk, strlen(chk))==0);
  107. }
  108. /* case sensitive token comparer.
  109. * returns 1 if chk==tok[tok_pos] otherwise returns 0
  110. */
  111. int parse_IsToken(char **tok, int tok_pos, char *chk) {
  112. if(tok_pos<0) {return 0;}
  113. return (strcmp(tok[tok_pos], chk)==0);
  114. }