windows/SysUtilities.cpp
Go to the documentation of this file.
1 /*----------------------------------------------------------------------------*/
2 /* */
3 /* Copyright (c) 1995, 2004 IBM Corporation. All rights reserved. */
4 /* Copyright (c) 2005-2010 Rexx Language Association. All rights reserved. */
5 /* */
6 /* This program and the accompanying materials are made available under */
7 /* the terms of the Common Public License v1.0 which accompanies this */
8 /* distribution. A copy is also available at the following address: */
9 /* http://www.oorexx.org/license.html */
10 /* */
11 /* Redistribution and use in source and binary forms, with or */
12 /* without modification, are permitted provided that the following */
13 /* conditions are met: */
14 /* */
15 /* Redistributions of source code must retain the above copyright */
16 /* notice, this list of conditions and the following disclaimer. */
17 /* Redistributions in binary form must reproduce the above copyright */
18 /* notice, this list of conditions and the following disclaimer in */
19 /* the documentation and/or other materials provided with the distribution. */
20 /* */
21 /* Neither the name of Rexx Language Association nor the names */
22 /* of its contributors may be used to endorse or promote products */
23 /* derived from this software without specific prior written permission. */
24 /* */
25 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS */
26 /* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT */
27 /* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS */
28 /* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT */
29 /* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */
30 /* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED */
31 /* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, */
32 /* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY */
33 /* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING */
34 /* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
35 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
36 /* */
37 /*----------------------------------------------------------------------------*/
38 /****************************************************************************/
39 /* REXX Kernel */
40 /* */
41 /* Utility Functions */
42 /* */
43 /****************************************************************************/
44 
45 #include <stdio.h>
46 #include <stdarg.h>
47 
48 #include "Utilities.hpp"
49 
50 /**
51  * Encapsulation of _vsnprintf Windows implementation.
52  * The goal is to have the same behavior on all platforms.
53  *
54  * @param buffer Buffer receiving the formated output.
55  * @param size Size of buffer.
56  * @param format Format string.
57  * @param args Pointer to optional arguments
58  *
59  * @return Upon successful completion, return the number of bytes stored in buffer, not counting the terminating null character
60  * or a negative value if an error was encountered.
61  */
62 int Utilities::vsnprintf(char *buffer, size_t count, const char *format, va_list args)
63 {
64  /*
65  http://msdn.microsoft.com/en-us/library/1kt27hek%28v=VS.71%29.aspx
66 
67  Return value
68  _vsnprintf returns the number of bytes stored in buffer, not counting the terminating null character.
69  If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.
70 
71  Remarks
72  The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended
73  unless count is zero or the formatted string length is greater than or equal to count characters) in buffer.
74  */
75 
76  /*
77  _vsnprintf behavior, assuming the buffer is 4 bytes :
78  0 1 2 3
79  "x" x \0 return value = 1
80  "xx" x x \0 return value = 2
81  "xxx" x x x \0 return value = 3
82  "xxxx" x x x x return value = -1 (truncated, no final '\0')
83  "xxxxx" x x x x return value = -1 (truncated, no final '\0')
84  */
85 
86  if (buffer == NULL || count == 0) return -1;
87  int n = _vsnprintf(buffer, count, format, args);
88  buffer[count-1] = '\0'; // Unlike Unix implementation, we are not sure to have a final '\0'
89  return n;
90 }
91 
92 
93 /**
94  * Encapsulation of snprintf Windows implementation.
95  * The goal is to have the same behavior on all platforms.
96  *
97  * @param buffer Buffer receiving the formated output.
98  * @param size Size of buffer.
99  * @param format Format string.
100  * @param ... Optional arguments
101  *
102  * @return Upon successful completion, return the number of bytes stored in buffer, not counting the terminating null character
103  * or a negative value if an error was encountered.
104  */
105 int Utilities::snprintf(char *buffer, size_t count, const char *format, ...)
106 {
107  va_list args;
108  va_start(args, format);
109  int n = Utilities::vsnprintf(buffer, count, format, args);
110  va_end(args);
111  return n;
112 }
113 
114 
115 // Could be in SysThread.cpp, but for the moment, it's here...
117 {
118  return (wholenumber_t)GetCurrentThreadId();
119 }
120 
121 
122 // This indicator is used to control the display of additional informations in the trace output for concurrency.
123 static bool TRACE_CONCURRENCY = false;
124 
125 void Utilities::traceConcurrency(bool trace)
126 {
127  TRACE_CONCURRENCY = trace;
128 }
129 
130 
132 {
133  // I don't put this part of code in SystemInterpreter::setupProgram
134  // where RXTRACE is managed, because would be initialized too late :
135  // Some mutexes/semaphores have been already used before calling setupProgram.
136  static bool firstcall = true;
137  if (firstcall)
138  {
139  firstcall = false;
140  TCHAR rxTraceBuf[8];
141  if (GetEnvironmentVariable("RXTRACE_CONCURRENCY", rxTraceBuf, 8))
142  {
143  if (!Utilities::strCaselessCompare(rxTraceBuf, "ON")) /* request to turn on? */
144  {
145  /* turn on tracing */
147  }
148  }
149  }
150  return TRACE_CONCURRENCY;
151 }
152 
153 // This indicator is used to control the display of additional informations in the trace output while parsing.
154 static bool TRACE_PARSING = false;
155 
156 void Utilities::traceParsing(bool trace)
157 {
158  TRACE_PARSING = trace;
159 }
160 
161 
163 {
164  static bool firstcall = true;
165  if (firstcall)
166  {
167  firstcall = false;
168  TCHAR rxTraceBuf[8];
169  if (GetEnvironmentVariable("RXTRACE_PARSING", rxTraceBuf, 8))
170  {
171  if (!Utilities::strCaselessCompare(rxTraceBuf, "ON")) /* request to turn on? */
172  {
173  /* turn on tracing */
175  }
176  }
177  }
178  return TRACE_PARSING;
179 }
180 
static wholenumber_t currentThreadId()
static bool traceConcurrency()
static bool traceParsing()
static int strCaselessCompare(const char *opt1, const char *opt2)
Definition: Utilities.cpp:82
static int vsnprintf(char *buffer, size_t count, const char *format, va_list args)
static int snprintf(char *buffer, size_t count, const char *format,...)
ssize_t wholenumber_t
Definition: rexx.h:230
static bool TRACE_PARSING
static bool TRACE_CONCURRENCY