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.

316 lines
7.7 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name:
  4. jd_misc.cpp
  5. Abstract:
  6. variety of assisting functions
  7. - command line parameters parsing
  8. - displaying the error messages
  9. - creating the random file
  10. - creating the unique identifier based on thread and process
  11. Author:
  12. jaroslad
  13. Revision History:
  14. 06-01-96 ( Jaroslad ) Original.
  15. --*/
  16. #include <tchar.h>
  17. #include "jd_misc.h"
  18. #include <stdio.h>
  19. #include <stdarg.h>
  20. #include <string.h>
  21. #include <stdlib.h>
  22. #include <time.h>
  23. #include <windows.h>
  24. int random (int low, int high)
  25. {
  26. return ( low+((long)rand()*(long)high)/RAND_MAX );
  27. }
  28. /* Sample structure demonstating how to use comand line definition structure
  29. TParamDef CmdLineParam[]=
  30. {
  31. {"c" ,1, &FtpServerIpAddr,TYPE_TCHAR,OPT, "ftp server computer name", "computer"},
  32. {"b", 0, &fStop, TYPE_INT, OPT, "binary flag"},
  33. {"start", 0, &fStart, TYPE_INT, OPT, "start service"},
  34. {"pause", 0, &fPause, TYPE_INT, OPT, "pause service"},
  35. {"s" ,1, &ServiceName, TYPE_TCHAR, MAND,"service name","svc"},
  36. { {NULL,0, NULL , TYPE_TCHAR, OPT, "Place the description of the program here" };
  37. */
  38. void DisplayUsage( _TCHAR **argv, TParamDef *tt)
  39. {
  40. _tprintf(_T("Usage:\n\t%s "), argv[0]);
  41. for(int i=0; tt[i].sw!=0;i++)
  42. {
  43. if(tt[i].sw[0]==0) //default (do not need switch) parameters
  44. {
  45. _tprintf(_T("%s "),(tt[i].text_param!=NULL)?tt[i].text_param:_T("parm"));
  46. }
  47. else
  48. {
  49. _tprintf(_T("-%s "),tt[i].sw);
  50. if(tt[i].param_number==1)
  51. _tprintf(_T("[%s] "),(tt[i].text_param!=NULL)?tt[i].text_param:_T("parm"));
  52. else if(tt[i].param_number>1)
  53. _tprintf(_T("[[%s]...] "),(tt[i].text_param!=NULL)?tt[i].text_param:_T("parm"));
  54. }
  55. }
  56. _tprintf(_T("\n\n"));
  57. for(i=0; tt[i].sw!=0; i++)
  58. {
  59. if(tt[i].sw[0]==0) //default parameters
  60. {
  61. _tprintf(_T("\"no switch\" %s\n"),tt[i].text_desc);
  62. }
  63. else if(tt[i].text_desc!=NULL)
  64. {
  65. _tprintf(_T("-%-6s %s\n"),tt[i].sw,tt[i].text_desc);
  66. }
  67. }
  68. //print description
  69. if( tt[i].text_desc!=NULL && tt[i].text_desc[0]!=0)
  70. {
  71. _tprintf(_T("\nDescription:\n"));
  72. _tprintf(_T("%s \n"), tt[i].text_desc);
  73. }
  74. }
  75. void DisplayUsageAndExit( _TCHAR **argv, TParamDef *tt)
  76. {
  77. DisplayUsage( argv, tt);
  78. exit(1);
  79. }
  80. //structure that makes easy lexical parsing of the command line arguments
  81. struct sParamLex
  82. {
  83. int argc;
  84. TCHAR **argv;
  85. _TCHAR ParamBuffer[400];
  86. int iCurrentParamChar; //character index within the current parameter being processed
  87. int iCurrentParam; //index to the current parameter that is processed
  88. public:
  89. sParamLex(int argc,_TCHAR **argv):argc(argc),argv(argv){iCurrentParamChar=0; iCurrentParam=1;};
  90. BOOL IsNextSwitch();
  91. BOOL IsEnd();
  92. LPTSTR ReadNext();
  93. };
  94. BOOL sParamLex::IsNextSwitch()
  95. {
  96. if(IsEnd())
  97. return FALSE;
  98. if (argv[iCurrentParam][iCurrentParamChar]==_T('-'))
  99. return TRUE;
  100. else
  101. return FALSE;
  102. }
  103. BOOL sParamLex::IsEnd()
  104. {
  105. if(iCurrentParam>=argc)
  106. return TRUE;
  107. else
  108. return FALSE;
  109. }
  110. LPTSTR sParamLex::ReadNext()
  111. {
  112. LPTSTR lpszRetval;
  113. if (IsEnd())
  114. return NULL;
  115. if(IsNextSwitch())
  116. { int i=0;
  117. iCurrentParamChar++; //skip '/' or '-'
  118. while (argv[iCurrentParam][iCurrentParamChar]!=0 && argv[iCurrentParam][iCurrentParamChar]!=_T(':'))
  119. ParamBuffer[i++]=argv[iCurrentParam][iCurrentParamChar++];
  120. if(argv[iCurrentParam][iCurrentParamChar]==_T(':'))
  121. iCurrentParamChar++;
  122. if(argv[iCurrentParam][iCurrentParamChar]==0)
  123. {
  124. iCurrentParam++; iCurrentParamChar=0;
  125. }
  126. ParamBuffer[i]=0;
  127. lpszRetval=ParamBuffer;
  128. }
  129. else
  130. {
  131. lpszRetval=&argv[iCurrentParam][iCurrentParamChar];
  132. iCurrentParam++; iCurrentParamChar=0;
  133. }
  134. return lpszRetval;
  135. }
  136. void ParseParam(int argc, _TCHAR ** argv, TParamDef * tt)
  137. {
  138. for(int i=0; tt[i].sw!=NULL ; i++)
  139. {
  140. tt[i].curr_param_read=0; //initialize
  141. }
  142. sParamLex paramLex(argc,argv);
  143. BOOL fParseBegin=TRUE;
  144. while(!paramLex.IsEnd())
  145. {
  146. int k;
  147. if(paramLex.IsNextSwitch())
  148. {
  149. _TCHAR * sw = paramLex.ReadNext();
  150. /*find the switch in switch table*/
  151. for( k=0; tt[k].sw!=NULL ;k++)
  152. {
  153. if(tt[k].sw[0]==0) continue; //skip the default parameters
  154. if(_tcscmp(tt[k].sw, sw)==0 /*equal*/ )
  155. break;
  156. }
  157. if(tt[k].sw == NULL) //switch not found
  158. { _tprintf(_T("invalid switch \"%s\"\n"),sw);/*error*/
  159. DisplayUsageAndExit(argv,tt);
  160. }
  161. }
  162. else if( fParseBegin==TRUE && (_tcscmp(tt[0].sw, _T(""))==0 /*equal*/ ) )
  163. { //default parameters (has to be the first record in arg description)
  164. k=0;
  165. }
  166. else
  167. {
  168. _tprintf(_T("default arguments not expected\n"));/*error*/
  169. DisplayUsageAndExit(argv,tt);
  170. }
  171. if(tt[k].param_number==0) //switch without parameters
  172. {
  173. if(paramLex.IsEnd()==FALSE && paramLex.IsNextSwitch()==FALSE)
  174. {
  175. _tprintf(_T("switch \"%s\" takes no parameters \n"),tt[k].sw);
  176. DisplayUsageAndExit(argv,tt);
  177. }
  178. tt[k].curr_param_read++;
  179. *((int *)tt[k].ptr)=1;
  180. }
  181. else if(tt[k].param_number>0) //swith with more then 0ne parameter
  182. {
  183. if(paramLex.IsEnd()==TRUE || paramLex.IsNextSwitch()==TRUE)
  184. { _tprintf(_T(" switch \"%s\" expects parameter\n"),tt[k].sw);//error
  185. DisplayUsageAndExit(argv,tt);
  186. }
  187. else
  188. {
  189. _TCHAR * prm;
  190. do
  191. {
  192. prm=paramLex.ReadNext();
  193. if(tt[k].param_number <= tt[k].curr_param_read)
  194. {
  195. _tprintf(_T("number of parameters for switch -%s exceeds maximum allowed (%d)\n"),tt[k].sw,tt[k].param_number);
  196. DisplayUsageAndExit(argv,tt);
  197. }
  198. if(tt[k].ptr_type==TYPE_TCHAR || tt[k].ptr_type==TYPE_LPCTSTR)
  199. *(((_TCHAR **)tt[k].ptr) + tt[k].curr_param_read++)=prm;
  200. else if(tt[k].ptr_type==TYPE_INT ||tt[k].ptr_type==TYPE_WORD)
  201. *(((int *)tt[k].ptr) + tt[k].curr_param_read++)=_ttoi(prm);
  202. else if(tt[k].ptr_type==TYPE_LONG || tt[k].ptr_type==TYPE_DWORD)
  203. *(((long *)tt[k].ptr) + tt[k].curr_param_read++)=_ttol(prm);
  204. }while (paramLex.IsEnd()==FALSE && paramLex.IsNextSwitch()==FALSE);
  205. }
  206. }//end tt[k].param_number
  207. } // end while
  208. for(i=0; tt[i].sw!=0;i++) //check for mandatory switches
  209. {
  210. if (tt[i].opt_mand==MAND && tt[i].curr_param_read==0)
  211. {
  212. _tprintf(_T("mandatory switch -%s missing\n"),tt[i].sw);
  213. DisplayUsageAndExit(argv,tt);
  214. }
  215. if(tt[i].param_read!=NULL) // set number of params for switch
  216. *tt[i].param_read=tt[i].curr_param_read;
  217. }
  218. }
  219. /******************************************
  220. time_printf
  221. *******************************************/
  222. int time_printf(_TCHAR *format, ...)
  223. {
  224. static CRITICAL_SECTION cs;
  225. static BOOL fInit=0;
  226. va_list marker;
  227. if(fInit==0)
  228. {
  229. fInit=1;
  230. InitializeCriticalSection(&cs);
  231. }
  232. _TCHAR buf[80];
  233. EnterCriticalSection(&cs);
  234. va_start( marker, format ); /* Initialize variable arguments. */
  235. _tprintf(_TEXT("%s - "),_tstrtime(buf));
  236. _vtprintf(format,marker);
  237. LeaveCriticalSection(&cs);
  238. va_end( marker ); /* Reset variable arguments. */
  239. // printf("%s%s",bufa,bufb); //for multithreaded will be printed as one line
  240. return 1;
  241. }
  242. void error_printf(_TCHAR *format, ...)
  243. {
  244. va_list marker;
  245. va_start( marker, format ); /* Initialize variable arguments. */
  246. _tprintf(_TEXT("Error: "));
  247. int x=_vftprintf(stderr,format,marker);
  248. va_end( marker ); /* Reset variable arguments. */
  249. }
  250. void fatal_error_printf(_TCHAR *format, ...)
  251. {
  252. va_list marker;
  253. va_start( marker, format ); /* Initialize variable arguments. */
  254. _tprintf(_TEXT("Error: "));
  255. int x=_vftprintf(stderr,format,marker);
  256. va_end( marker ); /* Reset variable arguments. */
  257. exit(EXIT_FAILURE);
  258. }