1 /* $MirOS: contrib/code/jupp/utils.c,v 1.4 2017/01/10 19:05:17 tg Exp $ */
5 * (C) 1992 Joseph H. Allen
6 * (C) 2001 Marek 'Marx' Grac
8 * This file is part of JOE (Joe's Own Editor)
27 * return minimum/maximum of two numbers
29 unsigned int uns_min(unsigned int a, unsigned int b)
34 signed int int_min(signed int a, signed int b)
39 signed long int long_max(signed long int a, signed long int b)
44 signed long int long_min(signed long int a, signed long int b)
49 /* Versions of 'read' and 'write' which automatically retry when interrupted */
50 ssize_t joe_read(int fd, void *buf, size_t size)
55 rt = read(fd, buf, size);
56 } while (rt < 0 && errno == EINTR);
60 ssize_t joe_write(int fd, void *buf, size_t size)
65 rt = write(fd, buf, size);
66 } while (rt < 0 && errno == EINTR);
71 #define SIG_ERR ((sighandler_t) -1)
74 /* wrapper to hide signal interface differrencies */
75 int joe_set_signal(int signum, sighandler_t handler)
79 struct sigaction sact;
81 mset(&sact, 0, sizeof(sact));
82 sact.sa_handler = handler;
84 sact.sa_flags = SA_INTERRUPT;
86 retval = sigaction(signum, &sact, NULL);
87 #elif defined(HAVE_SIGVEC)
90 mset(&svec, 0, sizeof(svec));
91 svec.sv_handler = handler;
92 #ifdef HAVE_SV_INTERRUPT
93 svec.sv_flags = SV_INTERRUPT;
95 retval = sigvec(signum, &svec, NULL);
97 retval = (signal(signum, handler) != SIG_ERR) ? 0 : -1;
98 #ifdef HAVE_SIGINTERRUPT
99 siginterrupt(signum, 1);
105 /* Helpful little parsing utilities */
107 /* Skip whitespace and return first non-whitespace character */
109 int parse_ws(unsigned char **pp,int cmt)
111 unsigned char *p = *pp;
112 while (*p==' ' || *p=='\t')
114 if (*p=='\r' || *p=='\n' || *p==cmt)
120 /* Parse an identifier into a buffer. Identifier is truncated to a maximum of len chars. */
122 int parse_ident(unsigned char **pp, unsigned char *buf, int len)
124 unsigned char *p = *pp;
125 if (joe_isalpha_(locale_map,*p)) {
126 while(len && joe_isalnum_(locale_map,*p))
129 while(joe_isalnum_(locale_map,*p))
137 /* Parse to next whitespace */
139 int parse_tows(unsigned char **pp, unsigned char *buf)
141 unsigned char *p = *pp;
142 while (*p && *p!=' ' && *p!='\t' && *p!='\n' && *p!='\r' && *p!='#')
150 /* Parse a keyword */
152 int parse_kw(unsigned char **pp, unsigned char *kw)
154 unsigned char *p = *pp;
155 while(*kw && *kw==*p)
157 if(!*kw && !joe_isalnum_(locale_map,*p)) {
166 int parse_field(unsigned char **pp, unsigned char *kw)
168 unsigned char *p = *pp;
169 while(*kw && *kw==*p)
171 if(!*kw && (!*p || *p==' ' || *p=='\t' || *p=='#' || *p=='\n' || *p=='\r')) {
178 /* Parse a character */
180 int parse_char(unsigned char **pp, unsigned char c)
182 unsigned char *p = *pp;
190 /* Parse an integer. Returns 0 for success. */
192 int parse_int(unsigned char **pp, int *buf)
194 unsigned char *p = *pp;
195 if ((*p>='0' && *p<='9') || *p=='-') {
196 *buf = atoi((char *)p);
199 while(*p>='0' && *p<='9')
207 /* Parse a string into a buffer. Returns 0 for success.
208 Leaves escape sequences in string. */
210 int parse_string(unsigned char **pp, unsigned char *buf, int len)
212 unsigned char *p= *pp;
215 while(len && *p && *p!='\"')
216 if(*p=='\\' && p[1] && len>2) {
225 while(*p && *p!='\"')
238 /* Parse a character range: a-z */
240 int parse_range(unsigned char **pp, int *first, int *second)
242 unsigned char *p= *pp;
246 if(*p=='\\' && p[1]) {
257 if(*p=='-' && p[1]) {
259 if(*p=='\\' && p[1]) {