2021-01-12 21:04:43 -08:00
//
// utils.h - Some utility and printing functions.
//
2022-11-07 19:54:59 -08:00
# pragma once
2020-09-11 01:28:06 -07:00
2021-08-06 17:52:20 -07:00
# include <err.h>
# include <stdarg.h>
2021-01-18 10:30:17 -08:00
# include <stdbool.h>
2020-09-11 01:28:06 -07:00
# include <stdio.h>
2021-01-18 10:30:17 -08:00
# include <stdlib.h>
2020-09-11 01:28:06 -07:00
# include <string.h>
# include <unistd.h>
2024-05-29 10:12:34 -07:00
# ifndef auto
# define auto __auto_type
# endif
2021-07-30 14:54:28 -07:00
# define S1(x) #x
# define S2(x) S1(x)
2024-05-27 22:41:07 -07:00
# define require(e, msg) ({\
__typeof__ ( e ) __expr = e ; \
if ( _Generic ( __expr , int : ( ssize_t ) __expr < 0 , ssize_t : ( ssize_t ) __expr < 0 , default : ! __expr ) ) errx ( 1 , __FILE__ " : " S2 ( __LINE__ ) " : " msg ) ; \
__expr ; \
} )
2021-08-06 17:52:20 -07:00
2024-05-27 23:05:57 -07:00
# define When(x, _tag) ((x)->type == _tag ? &(x)->__tagged._tag : (errx(1, __FILE__ ":%d This was supposed to be a " # _tag "\n", __LINE__), &(x)->__tagged._tag))
2024-05-27 22:56:13 -07:00
# ifndef public
# define public __attribute__ ((visibility ("default")))
# endif
2024-05-27 22:41:07 -07:00
# define new(t) require(calloc(1, sizeof(t)), "`new(" #t ")` allocation failure")
# define checked_strdup(s) require(strdup(s), "`checked_strdup(" #s ")` allocation failure")
# define grow(arr,n) require(realloc(arr,sizeof(arr[0])*(n)), "`grow(" #arr ", " #n ")` allocation failure")
2021-08-06 17:52:20 -07:00
2020-09-09 22:29:09 -07:00
# define streq(a, b) (strcmp(a, b) == 0)
2020-09-08 19:45:00 -07:00
2021-01-19 21:35:34 -08:00
__attribute__ ( ( nonnull ( 1 ) ) )
2021-09-28 16:59:01 -07:00
char unescapechar ( const char * escaped , const char * * after , const char * end ) ;
2020-12-30 21:20:54 -08:00
__attribute__ ( ( pure , nonnull ) )
2021-09-28 16:59:01 -07:00
const char * after_name ( const char * str , const char * end ) ;
2020-09-23 22:37:28 -07:00
__attribute__ ( ( pure , nonnull , returns_nonnull ) )
2021-09-28 16:59:01 -07:00
const char * after_spaces ( const char * str , bool skip_nl , const char * end ) ;
2020-09-23 22:37:28 -07:00
__attribute__ ( ( nonnull ) )
2021-09-28 16:59:01 -07:00
bool matchchar ( const char * * str , char c , bool skip_nl , const char * end ) ;
2020-09-23 22:37:28 -07:00
__attribute__ ( ( nonnull ) )
2021-09-28 16:59:01 -07:00
bool matchstr ( const char * * str , const char * target , bool skip_nl , const char * end ) ;
2021-01-10 00:12:09 -08:00
__attribute__ ( ( nonnull ) )
2021-07-30 15:06:04 -07:00
void delete ( void * p ) ;
2020-09-10 03:41:03 -07:00
2021-08-28 16:05:30 -07:00
// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0