summaryrefslogtreecommitdiff
path: root/support/dktools/dk4error.ctr
blob: d717a0fb6c417346c361506dd524941efb1083e5 (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
%%	options

copyright owner	=	Dirk Krause
copyright year	=	2015-xxxx
license		=	bsd


%%	header

/**	@file
	Return error information from
	called function to caller, more detailed than just an
	error code.

	In addition to the error code we can save details, i.e.
	the errno or GetLastError() value or the element size
	and number of elements for a failed memory allocation...
	This allows the caller to produce detailed error messages
	if necessary.

	Before processing a code section where errors can occur,
	use dk4error_init() to initialize an error structure.
	A value indicating ``no error occured'' is set in the structure.

	Depending on the errors occuring, the code section might use
	one from the dk4error_set_...() functions.

	At the end of the code section, check the error code in the
	dk4_er_t structure.


	The address of one dk4_er_t can be passed to several function
	calls. Only the first error occured is noted in the structure,
	it is not overwritten by later errors.
*/

#ifndef DK4CONF_H_INCLUDED
#include "dk4conf.h"
#endif

#ifndef DK4TYPES_H_INCLUDED
#include "dk4types.h"
#endif



/**	Error report.
*/
typedef struct {
  /**	Details.
  */
  union {
    /**	Position within a file or data stream.
    */
    struct {
      dk4_um_t	byteno;		/**< Byte number. */
      dk4_um_t	lineno;		/**< Line number. */
      dk4_um_t	charno;		/**< Character number. */
      dk4_um_t	charinline;	/**< Character number within the line. */
    }	fpos;
    /**	Failed memory allocation.
    */
    struct {
      size_t	elsize;		/**< Element size. */
      size_t	nelem;		/**< Number of elements. */
    }	mem;
    int		iDetails1;	/**< Integer details. */
    long	lDetails1;	/**< Long details. */
  } dt;
  int		ec;		/**< Error code. */
} dk4_er_t;



#ifdef __cplusplus
extern "C" {
#endif

/**	Initialize error report.
	
	CRT on Windows: Optional.
	@param	erp	Error report to initialize.
*/
void
dk4error_init(dk4_er_t *erp);

/**	Set simple error code.
	
	CRT on Windows: Not used.
	@param	erp	Error report to modify, may be NULL.
	@param	ec	New error code.
*/
void
dk4error_set_simple_error_code(dk4_er_t *erp, int ec);

/**	Set error code for dealing with a number of elements
	(memory allocation).
	
	CRT on Windows: Not used.
	@param	erp	Error report to modify, may be NULL.
	@param	ec	New error code to set.
	@param	sz	Element size.
	@param	ne	Number of elements.
*/
void
dk4error_set_elsize_nelem(dk4_er_t *erp, int ec, size_t sz, size_t ne);

/**	Set error code for error from a system function, save
	errno value in details.
	
	CRT on Windows: Not used.
	@param	erp	Error report to modify, may be NULL.
	@param	ec	New error code to set.
	@param	errnval	Value of errno, stored in iDetails1.
*/
void
dk4error_set_idetails(dk4_er_t *erp, int ec, int errnval);

/**	Set long error code for error from a Windows system function,
	save GetLastError() value in details.
	
	CRT on Windows: Not used.
	@param	erp	Error report to modify, may be NULL.
	@param	ec	New error code to set.
	@param	errnval	Value obtained from GetLastError().
*/
void
dk4error_set_ldetails(dk4_er_t *erp, int ec, long errnval);

/**	Set error code and keep file or data stream position.
	
	CRT on Windows: Not used.
	@param	erp		Error report to modify, may be NULL.
	@param	ec		New error code to set.
	@param	byteno		Byte position in file.
	@param	lineno		Current line number.
	@param	charno		Character number in file.
	@param	charinline	Character position in line.
*/
void
dk4error_set_with_position(
  dk4_er_t	*erp,
  int		 ec,
  dk4_um_t	 byteno,
  dk4_um_t	 lineno,
  dk4_um_t	 charno,
  dk4_um_t	 charinline
);

/**	Copy error report if there was an error in the source report
	and no error yet in the destination report.
	
	CRT on Windows: Optional.
	@param	dptr	Destination report.
	@param	sptr	Source report.
*/
void
dk4error_copy(dk4_er_t *dptr, const dk4_er_t *sptr);

#ifdef __cplusplus
}
#endif



/**	Error codes.
*/
enum {

			/**	No error occured.
			*/
  DK4_E_NONE	=	0,

			/**	System error, the errno value is saved to
				det,syserr.
			*/
  DK4_E_SYSTEM ,

			/**	Something failed although it should not.
			*/
  DK4_E_BUG ,
  
			/**	Invalid arguments (in example NULL pointers,
				0 sizes) were passed to a function.
				No details are reported.
			*/
  DK4_E_INVALID_ARGUMENTS ,

  			/**	A buffer was too small to take a text
				completely.
  			*/
  DK4_E_BUFFER_TOO_SMALL ,

			/**	The requested information was not found.
			*/
  DK4_E_NOT_FOUND ,

			/**	Memory allocation failed. Element size and
				number of elements are stored in
				dt,mem,elsize and dt,mem,nelem.
  			*/
  DK4_E_MEMORY_ALLOCATION_FAILED ,

			/**	Numeric overflow. For floating point numbers
				the absolute value exceeded DBL_MAX.
				For integer numbers the result is outside
				the range MIN...MAX.
				No details are reported.
			*/
  DK4_E_MATH_OVERFLOW ,

			/**	Numeric underflow.
				The underflow is reported if the absolute
				value of a floating point operation is too
				small.
				For integer numbers only overflow is reported
				even if the result is smaller than MIN.
				No details are reported.
			*/
  DK4_E_MATH_UNDERFLOW ,

			/**	Division by zero. No details are reported.
			*/
  DK4_E_MATH_DIVZERO ,

			/**	An invalid argument was passed to a
				mathematical function.
			*/
  DK4_E_MATH_INVALID ,

			/**	Mathematical result is inexact, cutting bits.
			*/
  DK4_E_MATH_INEXACT ,

			/**	Syntax error when processing text, illegal
				character when decoding or encoding.
			*/
  DK4_E_SYNTAX ,

			/**	Functionality not supported on this system.
			*/
  DK4_E_NOT_SUPPORTED ,

			/**	A write operation failed.
			*/
  DK4_E_WRITE_FAILED ,

			/**	A data flush operation failed.
			*/
  DK4_E_FLUSH_FAILED ,

			/**	Failed to read data.
			*/
  DK4_E_READ_FAILED ,
			/**	A file or other stream close operation failed.
			*/
  DK4_E_CLOSE_FAILED ,

			/**	Additional security checks before opening a
				file failed.
			*/
  DK4_E_SEC_CHECK ,

			/**	Attempt to open a file failed.
			*/
  DK4_E_OPEN_FAILED ,

			/**	Failed to open file for reading.
			*/
  DK4_E_OPEN_READ_FAILED ,

			/**	Failed to open file for writing.
			*/
  DK4_E_OPEN_WRITE_FAILED ,

			/**	Failed to encode 32 bit character to
				output encoding.
			*/
  DK4_E_ENCODING_FAILED ,

			/**	Failed to decode 32 bit character from
				input encoding.
			*/
  DK4_E_DECODING_FAILED ,

			/**	Failed to create a directory.
			*/
  DK4_E_MKDIR_FAILED ,

			/**	Failed to change file or directory ownership.
			*/
  DK4_E_CHOWN_FAILED ,

			/**	Failed to change file or directory permissions.
			*/
  DK4_E_CHMOD_FAILED ,

			/**	Non-directory component in path name.
			*/
  DK4_E_NON_DIR ,

			/**	File exists, but is not a socket.
			*/
  DK4_E_NON_SOCKET ,

			/**	An unlink operation failed.
			*/
  DK4_E_UNLINK_FAILED ,

			/**	The DeleteFile() function on Windows failed.
			*/
  DK4_E_DELETE_FILE_FAILED ,

			/**	An rmdir operation failed.
			*/
  DK4_E_RMDIR_FAILED ,

			/**	The RemoveDirectory() function failed
				on Windows.
			*/
  DK4_E_REMOVE_DIRECTORY_FAILED ,

			/**	Opendir failed.
			*/
  DK4_E_OPENDIR_FAILED ,

			/**	The CreateFile() function failed on Windows.
				The result of GetLastError() is stored
				in dt.lDetails1.
			*/
  DK4_E_CREATE_FILE_FAILED ,

			/**	The CreateDirectory() function failed on
				Windows. The result of GetLastError() is
				stored in dt.lDetails1.
			*/
  DK4_E_CREATE_DIR_FAILED ,

			/**	The GetFileInformationByHandle() function
				failed on Windows. The result of
				GetLastError() is stored in dt.lDetails1.
			*/
  DK4_E_FILE_INFORMATION_FAILED ,

			/**	The FindFirstFile() function on Windows failed.
				The GetLastError() value is stored in lDetails1.
			*/
  DK4_E_FINDFIRSTFILE_FAILED ,

			/**	Timeout occured in socket operation,
				no details in iDetails1.
			*/
  DK4_E_SOCKET_TIMEOUT ,

			/**	The getaddrinfo() function failed to
				find the remote address.
				The function result is stored in iDetails1.
			*/
  DK4_E_SOCKET_GETADDRINFO ,

			/**	The getaddrinfo() function failed to
				find the local address.
				The function result is stored in iDetails1.
			*/
  DK4_E_SOCKET_GETADDRLOC ,

			/**	Failed to start up Windows sockets.
				The error code from WSAGetLastError() on
				Windows / errno on non-Windows is stored
				in iDetails1.
			*/
  DK4_E_SOCKET_UP ,

			/**	Failed to shut down Windows sockets.
				The error code from WSAGetLastError() on
				Windows / errno on non-Windows is stored
				in iDetails1.
			*/
  DK4_E_SOCKET_DOWN ,

			/**	The socket() function failed.
				The error code from WSAGetLastError() on
				Windows / errno on non-Windows is stored
				in iDetails1.
			*/
  DK4_E_SOCKET_SOCKET ,

			/**	The close() or closesocket() function failed.
				The error code from WSAGetLastError() on
				Windows / errno on non-Windows is stored
				in iDetails1.
			*/
  DK4_E_SOCKET_CLOSE ,

			/**	The bind() function failed.
				The error code from WSAGetLastError() on
				Windows / errno on non-Windows is stored
				in iDetails1.
			*/
  DK4_E_SOCKET_BIND ,

			/**	The connect() function failed.
				The error code from WSAGetLastError() on
				Windows / errno on non-Windows is stored
				in iDetails1.
			*/
  DK4_E_SOCKET_CONNECT ,

			/**	The listen() function failed.
				The error code from WSAGetLastError() on
				Windows / errno on non-Windows is stored
				in iDetails1.
			*/
  DK4_E_SOCKET_LISTEN ,

			/**	The accept() function failed.
				The error code from WSAGetLastError() on
				Windows / errno on non-Windows is stored
				in iDetails1.
			*/
  DK4_E_SOCKET_ACCEPT ,

			/**	The select() function failed.
				The error code from WSAGetLastError() on
				Windows / errno on non-Windows is stored
				in iDetails1.
			*/
  DK4_E_SOCKET_SELECT ,

			/**	The setsockopt() function failed.
				The error code from WSAGetLastError() on
				Windows / errno on non-Windows is stored
				in iDetails1.
			*/
  DK4_E_SOCKET_SETSOCKOPT ,

  			/**	The getsockopt() function failed.
				The error code from WSAGetLastError() on
				Windows / errno on non-Windows is stored
				in iDetails1.
			*/
  DK4_E_SOCKET_GETSOCKOPT ,

  			/**	The shutdown() function failed.
				The error code from WSAGetLastError() on
				Windows / errno on non-Windows is stored
				in iDetails1.
			*/
  DK4_E_SOCKET_SHUTDOWN ,

  			/**	The send()/sendto() function failed.
				The error code from WSAGetLastError() on
				Windows / errno on non-Windows is stored
				in iDetails1.
			*/
  DK4_E_SOCKET_SEND ,

  			/**	The recv()/recvfrom() function failed.
				The error code from WSAGetLastError() on
				Windows / errno on non-Windows is stored
				in iDetails1.
			*/
  DK4_E_SOCKET_RECV ,

			/**	The ioctlsocket() function failed.
				The error code from WSAGetLastError() on
				Windows is stored in iDetails1.
				On Windows only.
			*/
  DK4_E_SOCKET_IOCTLSOCKET ,

			/**	The fcntl() function failed.
				The error code from
				errno on non-Windows is stored
				in iDetails1.
				Not on Windows systems (ioctlsocket
				is used to switch between blocking
				and non-blocking).
			*/
  DK4_E_SOCKET_FCNTL ,

			/**	The getservbyname() function failed.
				The error code from WSAGetLastError() on
				Windows / errno on non-Windows is stored
				in iDetails1.
			*/
  DK4_E_SOCKET_GETSERVBYNAME ,

			/**	The gethostbyname() function failed.
				The error code from WSAGetLastError() on
				Windows / errno on non-Windows is stored
				in iDetails1.
			*/
  DK4_E_SOCKET_GETHOSTBYNAME ,

			/**	The inet_ntop() function failed.
				The error code from WSAGetLastError() on
				Windows / errno on non-Windows is stored
				in iDetails1.
			*/
  DK4_E_SOCKET_INET_NTOP ,

			/**	The inet_pton() function failed.
				The error code from WSAGetLastError() on
				Windows / errno on non-Windows is stored
				in iDetails1.
			*/
  DK4_E_SOCKET_INET_PTON ,
};



%%	module

#include "dk4conf.h"

#if DK4_ON_WINDOWS && (DK4_WIN_AVOID_CRT || DK4_WIN_DENY_CRT)
#ifndef WINDOWS_H_INCLUDED
#include <windows.h>
#define	WINDOWS_H_INCLUDED 1
#endif
#endif

#include "dk4types.h"
#include "dk4error.h"
#include "dk4mem.h"

#if DK4_HAVE_STRING_H
#ifndef STRING_H_INCLUDED
#include <string.h>
#define	STRING_H_INCLUDED 1
#endif
#endif



$!trace-include



void
dk4error_init(dk4_er_t *erp)
{
  $? "+ dk4error_init"
  if (NULL != erp) {
#if DK4_ON_WINDOWS
  /* +++ Windows */
#if DK4_WIN_AVOID_CRT || DK4_WIN_DENY_CRT
    ZeroMemory(erp, sizeof(dk4_er_t));
#else
    memset(erp, 0, sizeof(dk4_er_t));
#endif
  /* --- Windows */
#else
  /* +++ non-Windows */
#if DK4_HAVE_MEMSET
    memset(erp, 0, sizeof(dk4_er_t));
#else
#if DK4_HAVE_BZERO
    bzero(erp, sizeof(dk4_er_t);
#else
    {
      register char	*ptr;
      register size_t	 sz;
      ptr = (char *)erp; sz = sizeof(dk4_er_t);
      while(sz--) { *(ptr++) = '\0'; }
    }
#endif
#endif
  /* --- non-Windows */
#endif
    erp->ec = DK4_E_NONE;
  }
  $? "- dk4error_init"
}



void
dk4error_set_simple_error_code(dk4_er_t *erp, int ec)
{
  if (NULL != erp) {
    if (DK4_E_NONE == erp->ec) {
      erp->ec = ec;
    }
  }
}



void
dk4error_set_elsize_nelem(dk4_er_t *erp, int ec, size_t sz, size_t ne)
{
  if (NULL != erp) {
    if (DK4_E_NONE == erp->ec) {
      erp->ec = ec;
      (erp->dt).mem.elsize = sz;
      (erp->dt).mem.nelem  = ne;
    }
  }
}



void
dk4error_set_idetails(dk4_er_t *erp, int ec, int errnval)
{
  if (NULL != erp) {
    if (DK4_E_NONE == erp->ec) {
      erp->ec = ec;
      (erp->dt).iDetails1 = errnval;
    }
  }
}


void
dk4error_set_ldetails(dk4_er_t *erp, int ec, long errnval)
{
  if (NULL != erp) {
    if (DK4_E_NONE == erp->ec) {
      erp->ec = ec;
      (erp->dt).lDetails1 = errnval;
    }
  }
}



void
dk4error_set_with_position(
  dk4_er_t	*erp,
  int		 ec,
  dk4_um_t	 byteno,
  dk4_um_t	 lineno,
  dk4_um_t	 charno,
  dk4_um_t	 charinline
)
{
  if (NULL != erp) {
    if (DK4_E_NONE == erp->ec) {
      erp->ec = ec;
      $? ". byteno =     %lu", (unsigned long)byteno
      $? ". lineno =     %lu", (unsigned long)lineno
      $? ". charno =     %lu", (unsigned long)charno
      $? ". charinline = %lu", (unsigned long)charinline
      (erp->dt).fpos.byteno = byteno;
      (erp->dt).fpos.lineno = lineno;
      (erp->dt).fpos.charno = charno;
      (erp->dt).fpos.charinline = charinline;
    }
  }
}



void
dk4error_copy(dk4_er_t *dptr, const dk4_er_t *sptr)
{
  if ((NULL != dptr) && (NULL != sptr)) {
    if (DK4_E_NONE == dptr->ec) {
#if DK4_ON_WINDOWS
  /* +++ Windows */
#if DK4_WIN_AVOID_CRT || DK4_WIN_DENY_CRT
      CopyMemory(dptr, sptr, sizeof(dk4_er_t));
#else
      memcpy(dptr, sptr, sizeof(dk4_er_t));
#endif
  /* --- Windows */
#else
  /* +++ non-Windows */
#if DK4_HAVE_MEMCPY
      memcpy(dptr, sptr, sizeof(dk4_er_t));
#else
#if DK4_HAVE_BCOPY
      bcopy(sptr, dptr, sizeof(dk4_er_t));
#else
      register 		unsigned char	*dp;	/* Destination pointer */
      register const	unsigned char	*sp;	/* Source pointer */
      register		size_t		 xsz;	/* Number remaining bytes */

      dp = (unsigned char *)dptr;
      sp = (const unsigned char *)sptr;
      xsz = sizeof(dk4_er_t);
      while(0 < xsz--) { *(dp++) = *(sp++); }
#endif
#endif
  /* --- non-Windows */
#endif
    }
  }
}