1
0
mirror of git://projects.qi-hardware.com/eda-tools.git synced 2025-04-21 12:27:27 +03:00

eeshow/: util.h and diag.c go to misc/

This commit is contained in:
Werner Almesberger
2016-08-17 21:37:15 -03:00
parent 4836dcb0ca
commit c4811c8dd6
24 changed files with 43 additions and 43 deletions

86
eeshow/misc/diag.c Normal file
View File

@@ -0,0 +1,86 @@
/*
* misc/diag.h - Diagnostics
*
* Written 2016 by Werner Almesberger
* Copyright 2016 by Werner Almesberger
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "misc/diag.h"
unsigned verbose = 0;
/* ----- Specialized diagnostic functions ---------------------------------- */
void diag_pfatal(const char *s)
{
fatal("%s: %s\n", s, strerror(errno));
}
void diag_perror(const char *s)
{
error("%s: %s\n", s, strerror(errno));
}
/* ----- General diagnostic functions -------------------------------------- */
void fatal(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
exit(1); /* @@@ for now ... */
}
void error(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
}
void warning(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
fprintf(stderr, "warning: ");
vfprintf(stderr, fmt, ap);
va_end(ap);
}
void progress(unsigned level, const char *fmt, ...)
{
va_list ap;
if (level > verbose)
return;
va_start(ap, fmt);
fprintf(stderr, "%*s", level * 2, "");
vfprintf(stderr, fmt, ap);
va_end(ap);
}

71
eeshow/misc/diag.h Normal file
View File

@@ -0,0 +1,71 @@
/*
* misc/diag.h - Diagnostics
*
* Written 2016 by Werner Almesberger
* Copyright 2016 by Werner Almesberger
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#ifndef MISC_DIAG_H
#define MISC_DIAG_H
/*
* 0: no progress indications
* 1: reasonable progress indications
* 2: verbose output
* > 2: go wild !
*/
extern unsigned verbose;
/* ----- Specialized diagnostic functions ---------------------------------- */
/* perror, based on "fatal" or "error" */
void diag_pfatal(const char *s);
void diag_perror(const char *s);
/* ----- General diagnostic functions -------------------------------------- */
/*
* Terminate immediately. Further execution makes no sense.
* E.g., out of memory.
*/
void __attribute__((noreturn)) fatal(const char *fmt, ...)
__attribute__((format(printf, 1, 2)));
/*
* Operation has failed, but the program as a whole may still be able to
* continue. E.g., a schematics component was not found.
*/
void error(const char *fmt, ...)
__attribute__((format(printf, 1, 2)));
/*
* A minor operation has failed or some other issue was detected. This may
* be (or lead to) a more serious problem, but does not immediately affect
* operation.
*/
void warning(const char *fmt, ...)
__attribute__((format(printf, 1, 2)));
/*
* Progress message, used mainly for debugging. "level" is the minimum
* verbosity level required.
*/
void progress(unsigned level, const char *fmt, ...)
__attribute__((format(printf, 2, 3)));
#endif /* !MISC_DIAG_H */

53
eeshow/misc/util.h Normal file
View File

@@ -0,0 +1,53 @@
/*
* misc/util.h - Common utility functions
*
* Written 2016 by Werner Almesberger
* Copyright 2016 Werner Almesberger
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#ifndef MISC_UTIL_H
#define MISC_UTIL_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define alloc_size(s) \
({ void *alloc_size_tmp = malloc(s); \
if (!alloc_size_tmp) { \
perror("malloc"); \
exit(1); \
} \
alloc_size_tmp; })
#define alloc_type(t) ((t *) alloc_size(sizeof(t)))
#define alloc_type_n(t, n) ((t *) alloc_size(sizeof(t) * (n)))
#define stralloc(s) \
({ char *stralloc_tmp = strdup(s); \
if (!stralloc_tmp) { \
perror("strdup"); \
exit(1); \
} \
stralloc_tmp; })
#define ARRAY_ELEMENTS(a) (sizeof(a) / sizeof(a[0]))
#define ARRAY_END(a) ((a) + ARRAY_ELEMENTS(a))
#define swap(a, b) \
({ typeof(a) _tmp = (a); a = (b); b = _tmp; })
#define unsupported(s) \
fprintf(stderr, __FILE__ ":%d: unsupported: " s "\n", __LINE__)
#endif /* !MISC_UTIL_H */