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.

121 lines
3.4 KiB

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