summaryrefslogtreecommitdiff
path: root/open-vm-tools/lib/include/msgfmt.h
blob: 5739577a54cd13556292cd4fcbe032963adfa288 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*********************************************************
 * Copyright (C) 2007-2015 VMware, Inc. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation version 2.1 and no later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the Lesser GNU General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA.
 *
 *********************************************************/

/*
 * msgfmg.h --
 *
 *	MsgFmt: format messages for the Msg module
 */

#ifndef _MSGFMT_H_
#define _MSGFMT_H_

#define INCLUDE_ALLOW_USERLEVEL
#define INCLUDE_ALLOW_VMCORE
#define INCLUDE_ALLOW_VMKERNEL
#include "includeCheck.h"

#ifndef VMKERNEL
#include "str.h" // for HAS_BSD_PRINTF
#endif


/*
 * Format parser callback functions
 */

typedef int
MsgFmt_LitFunc(void *clientData, // IN
               char const *buf,  // IN
               int bufSize);     // IN

typedef int
MsgFmt_SpecFunc(void *clientData,       // IN
                char const *pos,        // IN
                unsigned int posSize,   // IN
                char const *type,       // IN
                unsigned int typeSize); // IN


/*
 * Format specifier flags from MsgFmt_ParseSpec()
 */

#define MSGFMT_FLAG_ALT		0x0001
#define MSGFMT_FLAG_ZERO	0x0002
#define MSGFMT_FLAG_MINUS	0x0004
#define MSGFMT_FLAG_SPACE	0x0008
#define MSGFMT_FLAG_PLUS	0x0010
#define MSGFMT_FLAG_QUOTE	0x0020


/*
 * A format argument
 *
 * In addition to being a internal data structure,
 * MsgFmt_Arg defines the Vob (vmkernel observations) protocol
 * between vmkernel and vmx.  As such, it must be carefully aligned,
 * so that all the fields (except the pointers) have fixed sizes
 * and the same offsets in the 64-bit vmkernel, the 32-bit vmx,
 * and the 64-bit vmx.
 */

typedef enum MsgFmt_ArgType {
   MSGFMT_ARG_INVALID, // must be 0
   MSGFMT_ARG_INT32,
   MSGFMT_ARG_INT64,
   MSGFMT_ARG_PTR32,
   MSGFMT_ARG_PTR64,
   MSGFMT_ARG_FLOAT64,
   MSGFMT_ARG_STRING8,
   MSGFMT_ARG_STRING16,
   MSGFMT_ARG_STRING32,
   MSGFMT_ARG_ERRNO,
} MsgFmt_ArgType;

typedef enum MsgFmt_ArgPlatform {
   MSGFMT_PLATFORM_UNKNOWN,
   MSGFMT_PLATFORM_LINUX,
   MSGFMT_PLATFORM_WINDOWS,
   MSGFMT_PLATFORM_MACOS,
} MsgFmt_ArgPlatform;

typedef struct MsgFmt_Arg {
   int32 type;
   int32 pad;
   union {
      int32 signed32;
      int64 signed64;
      uint32 unsigned32;
      uint64 unsigned64;
      double float64;
      char *string8char;   // same as string8, different type
      int8 *string8;
      int16 *string16;
      int32 *string32;
      int32 offset;

      void *ptr;	// private
   } v;
   struct {
      int32 platform;
      int32 number;
   } e;
   union {		// private
      int32 precision;
      char *localString;
      uint64 pad;
   } p;
} MsgFmt_Arg;

#if defined __linux__
#define MSGFMT_CURRENT_PLATFORM MSGFMT_PLATFORM_LINUX
#elif defined _WIN32
#define MSGFMT_CURRENT_PLATFORM MSGFMT_PLATFORM_WINDOWS
#elif defined __APPLE__
#define MSGFMT_CURRENT_PLATFORM MSGFMT_PLATFORM_MACOS
#else
#define MSGFMT_CURRENT_PLATFORM MSGFMT_PLATFORM_UNKNOWN
#endif


/*
 * Global functions
 */

typedef int
MsgFmt_ParseFunc(MsgFmt_LitFunc *litFunc,    // IN
                 MsgFmt_SpecFunc *specFunc,  // IN
                 void *clientData,           // IN
                 char const *in);            // IN

MsgFmt_ParseFunc MsgFmt_Parse;
MsgFmt_ParseFunc MsgFmt_ParseWin32;

int
MsgFmt_ParseSpec(char const *pos,       // IN: n$ location
                 unsigned int posSize,  // IN: n$ length
                 char const *type,      // IN: flags, width, etc.
                 unsigned int typeSize, // IN: size of above
		 int *position,         // OUT: argument position
		 int *flags,            // OUT: flags, see MSGFMT_FLAG_*
		 int *width,            // OUT: width
		 int *precision,        // OUT: precision
		 char *lengthMod,       // OUT: length modifier
		 char *conversion);     // OUT: conversion specifier

Bool MsgFmt_GetArgs(const char *fmt, va_list va,
                    MsgFmt_Arg **args, int *numArgs, char **error);
Bool MsgFmt_GetArgsWithBuf(const char *fmt, va_list va,
                           MsgFmt_Arg **args, int *numArgs, char **error,
			   void *buf, size_t *bufSize);
void MsgFmt_FreeArgs(MsgFmt_Arg *args, int numArgs);

void MsgFmt_SwizzleArgs(MsgFmt_Arg *args,
                        int numArgs);
int MsgFmt_GetSwizzledString(const MsgFmt_Arg *args, int numArgs, int idx,
                             const void *bufEnd, const int8 **str);
int MsgFmt_UnswizzleArgs(MsgFmt_Arg *args,
                          int numArgs,
                          void *bufEnd);

MsgFmt_Arg* MsgFmt_CopyArgs(MsgFmt_Arg* copyArgs,
                            int numArgs);
int MsgFmt_Snprintf(char *buf, size_t size, const char *format,
                    const MsgFmt_Arg *args, int numArgs);
char *MsgFmt_Asprintf(size_t *length, const char *format,
                      const MsgFmt_Arg *args, int numArgs);


#endif // ifndef _MSGFMT_H_