4 * (C) 1992 Joseph H. Allen
5 * (C) 2001 Marek 'Marx' Grac
7 * This file is part of JOE (Joe's Own Editor)
11 __RCSID("$MirOS: contrib/code/jupp/utils.c,v 1.9 2017/12/06 21:17:04 tg Exp $");
22 * return minimum/maximum of two numbers
24 unsigned int uns_min(unsigned int a, unsigned int b)
29 signed int int_min(signed int a, signed int b)
34 signed long int long_max(signed long int a, signed long int b)
39 signed long int long_min(signed long int a, signed long int b)
44 /* Versions of 'read' and 'write' which automatically retry when interrupted */
45 ssize_t joe_read(int fd, void *buf, size_t size)
50 rt = read(fd, buf, size);
51 } while (rt < 0 && errno == EINTR);
55 ssize_t joe_write(int fd, void *buf, size_t size)
60 rt = write(fd, buf, size);
61 } while (rt < 0 && errno == EINTR);
66 #define SIG_ERR ((sighandler_t) -1)
69 /* wrapper to hide signal interface differrencies */
70 int joe_set_signal(int signum, sighandler_t handler)
74 struct sigaction sact;
76 mset(&sact, 0, sizeof(sact));
77 sact.sa_handler = handler;
79 sact.sa_flags = SA_INTERRUPT;
81 retval = sigaction(signum, &sact, NULL);
82 #elif defined(HAVE_SIGVEC)
85 mset(&svec, 0, sizeof(svec));
86 svec.sv_handler = handler;
87 #ifdef HAVE_SV_INTERRUPT
88 svec.sv_flags = SV_INTERRUPT;
90 retval = sigvec(signum, &svec, NULL);
92 retval = (signal(signum, handler) != SIG_ERR) ? 0 : -1;
93 #ifdef HAVE_SIGINTERRUPT
94 siginterrupt(signum, 1);
100 /* Helpful little parsing utilities */
102 /* Skip whitespace and return first non-whitespace character */
104 int parse_ws(unsigned char **pp,int cmt)
106 unsigned char *p = *pp;
107 while (*p==' ' || *p=='\t')
109 if (*p=='\r' || *p=='\n' || *p==cmt)
115 /* Parse an identifier into a buffer. Identifier is truncated to a maximum of len chars. */
117 int parse_ident(unsigned char **pp, unsigned char *buf, int len)
119 unsigned char *p = *pp;
120 if (joe_isalphx(locale_map,*p)) {
121 while(len && joe_isalnux(locale_map,*p))
124 while(joe_isalnux(locale_map,*p))
132 /* Parse to next whitespace */
134 int parse_tows(unsigned char **pp, unsigned char *buf)
136 unsigned char *p = *pp;
137 while (*p && *p!=' ' && *p!='\t' && *p!='\n' && *p!='\r' && *p!='#')
145 /* Parse a keyword */
147 int parse_kw(unsigned char **pp, unsigned char *kw)
149 unsigned char *p = *pp;
150 while(*kw && *kw==*p)
152 if(!*kw && !joe_isalnux(locale_map,*p)) {
161 int parse_field(unsigned char **pp, unsigned char *kw)
163 unsigned char *p = *pp;
164 while(*kw && *kw==*p)
166 if(!*kw && (!*p || *p==' ' || *p=='\t' || *p=='#' || *p=='\n' || *p=='\r')) {
173 /* Parse a character */
175 int parse_char(unsigned char **pp, unsigned char c)
177 unsigned char *p = *pp;
185 /* Parse a string into a buffer. Returns 0 for success.
186 Leaves escape sequences in string. */
188 int parse_string(unsigned char **pp, unsigned char *buf, int len)
190 unsigned char *p= *pp;
193 while(len && *p && *p!='\"')
194 if(*p=='\\' && p[1] && len>2) {
203 while(*p && *p!='\"')
216 /* Parse a character range: a-z */
218 int parse_range(unsigned char **pp, int *first, int *second)
220 unsigned char *p= *pp;
224 if(*p=='\\' && p[1]) {
235 if(*p=='-' && p[1]) {
237 if(*p=='\\' && p[1]) {