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=0;
  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. {
  164. //default parameters (has to be the first record in arg description)
  165. k=0;
  166. }
  167. else
  168. {
  169. _tprintf(_T("default arguments not expected\n"));/*error*/
  170. DisplayUsageAndExit(argv,tt);
  171. }
  172. if(tt[k].param_number==0) //switch without parameters
  173. {
  174. if(paramLex.IsEnd()==FALSE && paramLex.IsNextSwitch()==FALSE)
  175. {
  176. _tprintf(_T("switch \"%s\" takes no parameters \n"),tt[k].sw);
  177. DisplayUsageAndExit(argv,tt);
  178. }
  179. tt[k].curr_param_read++;
  180. *((int *)tt[k].ptr)=1;
  181. }
  182. else if(tt[k].param_number>0) //swith with more then 0ne parameter
  183. {
  184. if(paramLex.IsEnd()==TRUE || paramLex.IsNextSwitch()==TRUE)
  185. { _tprintf(_T(" switch \"%s\" expects parameter\n"),tt[k].sw);//error
  186. DisplayUsageAndExit(argv,tt);
  187. }
  188. else
  189. {
  190. _TCHAR * prm;
  191. do
  192. {
  193. prm=paramLex.ReadNext();
  194. if(tt[k].param_number <= tt[k].curr_param_read)
  195. {
  196. _tprintf(_T("number of parameters for switch -%s exceeds maximum allowed (%d)\n"),tt[k].sw,tt[k].param_number);
  197. DisplayUsageAndExit(argv,tt);
  198. }
  199. if(tt[k].ptr_type==TYPE_TCHAR || tt[k].ptr_type==TYPE_LPCTSTR)
  200. *(((_TCHAR **)tt[k].ptr) + tt[k].curr_param_read++)=prm;
  201. else if(tt[k].ptr_type==TYPE_INT ||tt[k].ptr_type==TYPE_WORD)
  202. *(((int *)tt[k].ptr) + tt[k].curr_param_read++)=_ttoi(prm);
  203. else if(tt[k].ptr_type==TYPE_LONG || tt[k].ptr_type==TYPE_DWORD)
  204. *(((long *)tt[k].ptr) + tt[k].curr_param_read++)=_ttol(prm);
  205. }while (paramLex.IsEnd()==FALSE && paramLex.IsNextSwitch()==FALSE);
  206. }
  207. }//end tt[k].param_number
  208. } // end while
  209. for(i=0; tt[i].sw!=0;i++) //check for mandatory switches
  210. {
  211. if (tt[i].opt_mand==MAND && tt[i].curr_param_read==0)
  212. {
  213. _tprintf(_T("mandatory switch -%s missing\n"),tt[i].sw);
  214. DisplayUsageAndExit(argv,tt);
  215. }
  216. if(tt[i].param_read!=NULL) // set number of params for switch
  217. *tt[i].param_read=tt[i].curr_param_read;
  218. }
  219. }
  220. /******************************************
  221. time_printf
  222. *******************************************/
  223. int time_printf(_TCHAR *format, ...)
  224. {
  225. static CRITICAL_SECTION cs;
  226. static BOOL fInit=0;
  227. va_list marker;
  228. if(fInit==0)
  229. {
  230. fInit=1;
  231. InitializeCriticalSection(&cs);
  232. }
  233. _TCHAR buf[80];
  234. EnterCriticalSection(&cs);
  235. va_start( marker, format ); /* Initialize variable arguments. */
  236. _tprintf(_TEXT("%s - "),_tstrtime(buf));
  237. _vtprintf(format,marker);
  238. LeaveCriticalSection(&cs);
  239. va_end( marker ); /* Reset variable arguments. */
  240. // printf("%s%s",bufa,bufb); //for multithreaded will be printed as one line
  241. return 1;
  242. }
  243. void error_printf(_TCHAR *format, ...)
  244. {
  245. va_list marker;
  246. va_start( marker, format ); /* Initialize variable arguments. */
  247. _tprintf(_TEXT("Error: "));
  248. _vftprintf(stderr,format,marker);
  249. va_end( marker ); /* Reset variable arguments. */
  250. }
  251. void fatal_error_printf(_TCHAR *format, ...)
  252. {
  253. va_list marker;
  254. va_start( marker, format ); /* Initialize variable arguments. */
  255. _tprintf(_TEXT("Error: "));
  256. _vftprintf(stderr,format,marker);
  257. va_end( marker ); /* Reset variable arguments. */
  258. exit(EXIT_FAILURE);
  259. }