From c1fe19d4c1db610692365472a90f4661e48449c1 Mon Sep 17 00:00:00 2001 From: Eric Andersen Date: Mon, 9 Oct 2000 20:06:30 +0000 Subject: Bug ugly formatting update --- Makefile | 11 +- include/fcntl.h | 130 +- include/stdlib.h | 9 +- include/unistd.h | 3 +- libc/inet/.indent.pro | 33 + libc/inet/Makefile | 10 +- libc/inet/addr.c | 108 +- libc/inet/resolv.c | 552 +- libc/inet/rpc/auth_none.c | 57 +- libc/inet/rpc/auth_unix.c | 126 +- libc/inet/rpc/authunix_prot.c | 22 +- libc/inet/rpc/bindresvport.c | 12 +- libc/inet/rpc/clnt_generic.c | 19 +- libc/inet/rpc/clnt_perror.c | 218 +- libc/inet/rpc/clnt_raw.c | 106 +- libc/inet/rpc/clnt_simple.c | 39 +- libc/inet/rpc/clnt_tcp.c | 234 +- libc/inet/rpc/clnt_udp.c | 261 +- libc/inet/rpc/get_myaddress.c | 22 +- libc/inet/rpc/getrpcent.c | 74 +- libc/inet/rpc/getrpcport.c | 6 +- libc/inet/rpc/pmap_clnt.c | 40 +- libc/inet/rpc/pmap_getmaps.c | 20 +- libc/inet/rpc/pmap_getport.c | 26 +- libc/inet/rpc/pmap_prot.c | 17 +- libc/inet/rpc/pmap_prot2.c | 26 +- libc/inet/rpc/pmap_rmt.c | 212 +- libc/inet/rpc/rpc_callmsg.c | 74 +- libc/inet/rpc/rpc_commondata.c | 3 +- libc/inet/rpc/rpc_dtablesize.c | 6 +- libc/inet/rpc/rpc_prot.c | 124 +- libc/inet/rpc/svc.c | 301 +- libc/inet/rpc/svc_auth.c | 33 +- libc/inet/rpc/svc_auth_unix.c | 39 +- libc/inet/rpc/svc_raw.c | 81 +- libc/inet/rpc/svc_run.c | 13 +- libc/inet/rpc/svc_simple.c | 51 +- libc/inet/rpc/svc_tcp.c | 219 +- libc/inet/rpc/svc_udp.c | 229 +- libc/inet/rpc/xdr.c | 198 +- libc/inet/rpc/xdr_array.c | 52 +- libc/inet/rpc/xdr_float.c | 154 +- libc/inet/rpc/xdr_mem.c | 101 +- libc/inet/rpc/xdr_rec.c | 312 +- libc/inet/rpc/xdr_reference.c | 37 +- libc/inet/rpc/xdr_stdio.c | 132 +- libc/misc/regex/.indent.pro | 33 + libc/misc/regex/Makefile | 11 +- libc/misc/regex/rx.c | 11355 ++++++++++++++++---------------- libc/misc/time/.indent.pro | 33 + libc/misc/time/Makefile | 11 +- libc/misc/time/asc_conv.c | 68 +- libc/misc/time/asctime.c | 14 +- libc/misc/time/asctime_r.c | 14 +- libc/misc/time/ctime.c | 35 +- libc/misc/time/ctime_r.c | 35 +- libc/misc/time/gmtime.c | 12 +- libc/misc/time/gmtime_r.c | 12 +- libc/misc/time/localtime.c | 21 +- libc/misc/time/localtime_r.c | 21 +- libc/misc/time/mktime.c | 314 +- libc/misc/time/tm_conv.c | 207 +- libc/pwd_grp/Makefile | 10 +- libc/stdio/.indent.pro | 33 + libc/stdio/Makefile | 11 +- libc/stdio/printf.c | 610 +- libc/stdio/scanf.c | 827 ++- libc/stdio/stdio.c | 1330 ++-- libc/stdlib/.indent.pro | 33 + libc/stdlib/Makefile | 11 +- libc/stdlib/atexit.c | 108 +- libc/stdlib/bsearch.c | 56 +- libc/stdlib/getenv.c | 32 +- libc/stdlib/malloc-simple/.indent.pro | 33 + libc/stdlib/malloc-simple/Makefile | 46 + libc/stdlib/malloc-simple/alloc.c | 106 + libc/stdlib/malloc/Makefile | 17 +- libc/stdlib/malloc/alloc.c | 48 + libc/stdlib/mkstemp.c | 28 +- libc/stdlib/mktemp.c | 24 +- libc/stdlib/putenv.c | 103 +- libc/stdlib/qsort.c | 225 +- libc/stdlib/rand.c | 24 +- libc/stdlib/realpath.c | 30 +- libc/stdlib/setenv.c | 122 +- libc/stdlib/strtod.c | 113 +- libc/stdlib/system.c | 80 +- libc/string/Makefile | 11 +- libc/sysdeps/linux/common/Makefile | 9 +- libc/sysdeps/linux/i386/Makefile | 17 +- libc/sysdeps/linux/i386/clone.S | 11 +- libc/termios/.indent.pro | 33 + libc/termios/Makefile | 11 +- libc/termios/termios.c | 294 +- libc/termios/ttyname.c | 66 +- test/stdlib/.cvsignore | 2 + test/stdlib/Makefile | 31 +- test/stdlib/mallocbug.c | 67 + 98 files changed, 10790 insertions(+), 10570 deletions(-) create mode 100644 libc/inet/.indent.pro create mode 100644 libc/misc/regex/.indent.pro create mode 100644 libc/misc/time/.indent.pro create mode 100644 libc/stdio/.indent.pro create mode 100644 libc/stdlib/.indent.pro create mode 100644 libc/stdlib/malloc-simple/.indent.pro create mode 100644 libc/stdlib/malloc-simple/Makefile create mode 100644 libc/stdlib/malloc-simple/alloc.c create mode 100644 libc/stdlib/malloc/alloc.c create mode 100644 libc/termios/.indent.pro create mode 100644 test/stdlib/mallocbug.c diff --git a/Makefile b/Makefile index dd22dd58b..88fbc0c19 100644 --- a/Makefile +++ b/Makefile @@ -20,16 +20,21 @@ # other sundry sources. Files within this library are copyright by their # respective copyright holders. -DIRS = error getent malloc misc regex stdio \ +#MALLOC = malloc +MALLOC = malloc-simple + +DIRS = error getent $(MALLOC) misc regex stdio \ string termios time sysdeps shm #rpc all: libc.a -libc.a: headers subdirs +libc.a: halfclean headers subdirs @echo @echo Finally finished compiling... @echo - $(CROSS)ranlib $@ + $(CROSS)ranlib libc.a +halfclean: + @rm -f libc.a headers: dummy @if [ ! -L "include/asm" ]; then ln -s /usr/include/asm include/asm ; fi diff --git a/include/fcntl.h b/include/fcntl.h index b9f727216..deea51182 100644 --- a/include/fcntl.h +++ b/include/fcntl.h @@ -1,20 +1,128 @@ -#ifndef __FCNTL_H -#define __FCNTL_H +/* Copyright (C) 1991, 92, 94, 95, 96, 97, 98 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with the GNU C Library; see the file COPYING.LIB. If not, + write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. */ + +/* + * POSIX Standard: 6.5 File Control Operations + */ + +#ifndef _FCNTL_H +#define _FCNTL_H 1 #include -#include -#include -#ifndef FNDELAY -#define FNDELAY O_NDELAY +/* This must be early so can define types winningly. */ +__BEGIN_DECLS + +/* Get the definitions of O_*, F_*, FD_*: all the + numbers and flag bits for `open', `fcntl', et al. */ +#include + +/* For XPG all symbols from should also be available. */ +#ifdef __USE_XOPEN +# include #endif -__BEGIN_DECLS +#ifdef __USE_MISC +# ifndef R_OK /* Verbatim from . Ugh. */ +/* Values for the second argument to access. + These may be OR'd together. */ +# define R_OK 4 /* Test for read permission. */ +# define W_OK 2 /* Test for write permission. */ +# define X_OK 1 /* Test for execute permission. */ +# define F_OK 0 /* Test for existence. */ +# endif +#endif /* Use misc. */ -extern int creat __P ((__const char * __filename, mode_t __mode)); -extern int fcntl __P ((int __fildes,int __cmd, ...)); -extern int open __P ((__const char * __filename, int __flags, ...)); +/* XPG wants the following symbols. */ +#ifdef __USE_XOPEN /* has the same definitions. */ +# define SEEK_SET 0 /* Seek from beginning of file. */ +# define SEEK_CUR 1 /* Seek from current position. */ +# define SEEK_END 2 /* Seek from end of file. */ +#endif /* XPG */ -__END_DECLS +/* Do the file control operation described by CMD on FD. + The remaining arguments are interpreted depending on CMD. */ +extern int fcntl __P ((int __fd, int __cmd, ...)); + +/* Open FILE and return a new file descriptor for it, or -1 on error. + OFLAG determines the type of access used. If O_CREAT is on OFLAG, + the third argument is taken as a `mode_t', the mode of the created file. */ +#ifndef __USE_FILE_OFFSET64 +extern int open __P ((__const char *__file, int __oflag, ...)); +#else +# ifdef __REDIRECT +extern int __REDIRECT (open, __P ((__const char *__file, int __oflag, ...)), + open64); +# else +# define open open64 +# endif +#endif +#ifdef __USE_LARGEFILE64 +extern int open64 __P ((__const char *__file, int __oflag, ...)); +#endif +/* Create and open FILE, with mode MODE. + This takes an `int' MODE argument because that is + what `mode_t' will be widened to. */ +#ifndef __USE_FILE_OFFSET64 +extern int creat __P ((__const char *__file, __mode_t __mode)); +#else +# ifdef __REDIRECT +extern int __REDIRECT (creat, __P ((__const char *__file, __mode_t __mode)), + creat64); +# else +# define creat creat64 +# endif +#endif +#ifdef __USE_LARGEFILE64 +extern int creat64 __P ((__const char *__file, __mode_t __mode)); #endif + +#if !defined F_LOCK && (defined __USE_MISC || (defined __USE_XOPEN_EXTENDED \ + && !defined __USE_POSIX)) +/* NOTE: These declarations also appear in ; be sure to keep both + files consistent. Some systems have them there and some here, and some + software depends on the macros being defined without including both. */ + +/* `lockf' is a simpler interface to the locking facilities of `fcntl'. + LEN is always relative to the current file position. + The CMD argument is one of the following. */ + +# define F_ULOCK 0 /* Unlock a previously locked region. */ +# define F_LOCK 1 /* Lock a region for exclusive use. */ +# define F_TLOCK 2 /* Test and lock a region for exclusive use. */ +# define F_TEST 3 /* Test a region for other processes locks. */ + +# ifndef __USE_FILE_OFFSET64 +extern int lockf __P ((int __fd, int __cmd, __off_t __len)); +# else +# ifdef __REDIRECT +extern int __REDIRECT (lockf, __P ((int __fd, int __cmd, __off64_t __len)), + lockf64); +# else +# define lockf lockf64 +# endif +# endif +# ifdef __USE_LARGEFILE64 +extern int lockf64 __P ((int __fd, int __cmd, __off64_t __len)); +# endif +#endif + +__END_DECLS + +#endif /* fcntl.h */ diff --git a/include/stdlib.h b/include/stdlib.h index b1ae61d06..08af451f0 100644 --- a/include/stdlib.h +++ b/include/stdlib.h @@ -58,21 +58,20 @@ extern void srand __P ((unsigned int seed)); /* Memory management functions */ -extern __ptr_t alloca __P ((size_t __size)); extern __ptr_t calloc __P ((size_t, size_t)); extern __ptr_t malloc __P ((size_t)); extern __ptr_t realloc __P ((__ptr_t, size_t)); extern void free __P ((__ptr_t)); #ifdef DEBUG_MALLOC -extern __ptr_t malloc_dbg __P ((size_t, char* func, char* file, int line)); extern __ptr_t calloc_dbg __P ((size_t, size_t, char* func, char* file, int line)); -extern void free_dbg __P ((__ptr_t, char* func, char* file, int line)); +extern __ptr_t malloc_dbg __P ((size_t, char* func, char* file, int line)); extern __ptr_t realloc_dbg __P ((__ptr_t, size_t, char* func, char* file, int line)); -#define malloc(x) malloc_dbg((x),__FUNCTION__,__FILE__,__LINE__) +extern void free_dbg __P ((__ptr_t, char* func, char* file, int line)); #define calloc(x,y) calloc_dbg((x),(y),__FUNCTION__,__FILE__,__LINE__) -#define free(x) free_dbg((x),__FUNCTION__,__FILE__,__LINE__) +#define malloc(x) malloc_dbg((x),__FUNCTION__,__FILE__,__LINE__) #define realloc(x) realloc((x),__FUNCTION__,__FILE__,__LINE__) +#define free(x) free_dbg((x),__FUNCTION__,__FILE__,__LINE__) #endif diff --git a/include/unistd.h b/include/unistd.h index d8a25dfa1..a070b03df 100644 --- a/include/unistd.h +++ b/include/unistd.h @@ -25,7 +25,7 @@ extern size_t write __P ((int __fd, __const char * __buf, size_t __n)); extern off_t lseek __P ((int __fd, off_t __n, int __whence)); extern int pipe __P ((int __pipedes[2])); extern unsigned int alarm __P ((unsigned int __seconds)); -extern int sleep __P ((unsigned int __seconds)); +extern unsigned int sleep __P ((unsigned int __seconds)); extern void usleep __P ((unsigned long __microseconds)); extern int pause __P ((void)); extern char* crypt __P((__const char *__key, __const char *__salt)); @@ -163,7 +163,6 @@ extern void _exit __P ((int __status)) __attribute__ ((__noreturn__)); /* Clone the calling process, creating an exact copy. * Return -1 for errors, 0 to the new process, * and the process ID of the new process to the old process. */ -extern __pid_t __fork __P ((void)); extern __pid_t fork __P ((void)); /* Clone the calling process, but without copying the whole address space. diff --git a/libc/inet/.indent.pro b/libc/inet/.indent.pro new file mode 100644 index 000000000..492ecf1c7 --- /dev/null +++ b/libc/inet/.indent.pro @@ -0,0 +1,33 @@ +--blank-lines-after-declarations +--blank-lines-after-procedures +--break-before-boolean-operator +--no-blank-lines-after-commas +--braces-on-if-line +--braces-on-struct-decl-line +--comment-indentation25 +--declaration-comment-column25 +--no-comment-delimiters-on-blank-lines +--cuddle-else +--continuation-indentation4 +--case-indentation0 +--else-endif-column33 +--space-after-cast +--line-comments-indentation0 +--declaration-indentation1 +--dont-format-first-column-comments +--dont-format-comments +--honour-newlines +--indent-level4 +/* changed from 0 to 4 */ +--parameter-indentation4 +--line-length78 /* changed from 75 */ +--continue-at-parentheses +--no-space-after-function-call-names +--dont-break-procedure-type +--dont-star-comments +--leave-optional-blank-lines +--dont-space-special-semicolon +--tab-size4 +/* additions by Mark */ +--case-brace-indentation0 +--leave-preprocessor-space diff --git a/libc/inet/Makefile b/libc/inet/Makefile index 519501180..07d84d6eb 100644 --- a/libc/inet/Makefile +++ b/libc/inet/Makefile @@ -33,11 +33,13 @@ MOBJ2=encodeh.o decodeh.o encoded.o decoded.o lengthd.o encodeq.o \ formquery.o dnslookup.o resolveaddress.o resolvemailbox.o \ opennameservers.o closenameservers.o resolvename.o gethostbyname.o\ gethostbyaddr.o +OBJS=$(MOBJ) $(MOBJ2) +all: $(OBJS) $(LIBC) -all: $(MOBJ) $(MOBJ2) $(LIBC) +$(LIBC): ar-target -$(LIBC): $(MOBJ) $(MOBJ2) - $(AR) $(ARFLAGS) $(LIBC) $(MOBJ) $(MOBJ2) +ar-target: $(OBJS) + $(AR) $(ARFLAGS) $(LIBC) $(OBJS) $(MOBJ): $(MSRC) $(CC) $(CFLAGS) -DL_$* $< -c -o $*.o @@ -45,6 +47,8 @@ $(MOBJ): $(MSRC) $(MOBJ2): $(MSRC2) $(CC) $(CFLAGS) -DL_$* $< -c -o $*.o +$(OBJS): Makefile + clean: rm -f *.[oa] *~ core diff --git a/libc/inet/addr.c b/libc/inet/addr.c index dcdd06cd4..142363ccc 100644 --- a/libc/inet/addr.c +++ b/libc/inet/addr.c @@ -10,77 +10,75 @@ int inet_aton(const char *cp, struct in_addr *inp); #ifdef L_inet_aton -int -inet_aton(cp, inp) +int inet_aton(cp, inp) const char *cp; struct in_addr *inp; { - unsigned long addr; - int value; - int part; - - if (!inp) - return 0; - - addr = 0; - for (part=1;part<=4;part++) { - - if (!isdigit(*cp)) - return 0; - - value = 0; - while (isdigit(*cp)) { - value *= 10; - value += *cp++ - '0'; - if (value > 255) - return 0; - } - - if (*cp++ != ((part == 4) ? '\0' : '.')) - return 0; - - addr <<= 8; - addr |= value; - } - - inp->s_addr = htonl(addr); - - return 1; + unsigned long addr; + int value; + int part; + + if (!inp) + return 0; + + addr = 0; + for (part = 1; part <= 4; part++) { + + if (!isdigit(*cp)) + return 0; + + value = 0; + while (isdigit(*cp)) { + value *= 10; + value += *cp++ - '0'; + if (value > 255) + return 0; + } + + if (*cp++ != ((part == 4) ? '\0' : '.')) + return 0; + + addr <<= 8; + addr |= value; + } + + inp->s_addr = htonl(addr); + + return 1; } #endif #ifdef L_inet_addr -unsigned long -inet_addr(cp) +unsigned long inet_addr(cp) const char *cp; { - struct in_addr a; - if (!inet_aton(cp, &a)) - return -1; - else - return a.s_addr; + struct in_addr a; + + if (!inet_aton(cp, &a)) + return -1; + else + return a.s_addr; } #endif #ifdef L_inet_ntoa -extern char * itoa(int); +extern char *itoa(int); -char * -inet_ntoa(in) +char *inet_ntoa(in) struct in_addr in; { - static char buf[18]; - unsigned long addr = ntohl(in.s_addr); - - strcpy(buf, itoa((addr >> 24) & 0xff)); - strcat(buf, "."); - strcat(buf, itoa((addr >> 16) & 0xff)); - strcat(buf, "."); - strcat(buf, itoa((addr >> 8) & 0xff)); - strcat(buf, "."); - strcat(buf, itoa(addr & 0xff)); - - return buf; + static char buf[18]; + unsigned long addr = ntohl(in.s_addr); + + strcpy(buf, itoa((addr >> 24) & 0xff)); + strcat(buf, "."); + strcat(buf, itoa((addr >> 16) & 0xff)); + strcat(buf, "."); + strcat(buf, itoa((addr >> 8) & 0xff)); + strcat(buf, "."); + strcat(buf, itoa(addr & 0xff)); + + return buf; } #endif diff --git a/libc/inet/resolv.c b/libc/inet/resolv.c index 27daaf4d6..ae23064bb 100644 --- a/libc/inet/resolv.c +++ b/libc/inet/resolv.c @@ -31,10 +31,10 @@ #define DPRINTF(X,args...) printf(X,args...) #else #define DPRINTF(X,args...) -#endif /* DEBUG */ +#endif /* DEBUG */ #ifdef L_encodeh -int encode_header(struct resolv_header * h, unsigned char * dest, int maxlen) +int encode_header(struct resolv_header *h, unsigned char *dest, int maxlen) { if (maxlen < 12) return -1; @@ -42,12 +42,9 @@ int encode_header(struct resolv_header * h, unsigned char * dest, int maxlen) dest[0] = (h->id & 0xff00) >> 8; dest[1] = (h->id & 0x00ff) >> 0; dest[2] = (h->qr ? 0x80 : 0) | - ((h->opcode & 0x0f) << 3) | - (h->aa ? 0x04 : 0) | - (h->tc ? 0x02 : 0) | - (h->rd ? 0x01 : 0); - dest[3] = (h->ra ? 0x80 : 0) | - (h->rcode & 0x0f); + ((h->opcode & 0x0f) << 3) | + (h->aa ? 0x04 : 0) | (h->tc ? 0x02 : 0) | (h->rd ? 0x01 : 0); + dest[3] = (h->ra ? 0x80 : 0) | (h->rcode & 0x0f); dest[4] = (h->qdcount & 0xff00) >> 8; dest[5] = (h->qdcount & 0x00ff) >> 0; dest[6] = (h->ancount & 0xff00) >> 8; @@ -56,13 +53,13 @@ int encode_header(struct resolv_header * h, unsigned char * dest, int maxlen) dest[9] = (h->nscount & 0x00ff) >> 0; dest[10] = (h->arcount & 0xff00) >> 8; dest[11] = (h->arcount & 0x00ff) >> 0; - + return 12; } #endif #ifdef L_decodeh -int decode_header(unsigned char * data, struct resolv_header * h) +int decode_header(unsigned char *data, struct resolv_header *h) { h->id = (data[0] << 8) | data[1]; h->qr = (data[2] & 0x80) ? 1 : 0; @@ -76,7 +73,7 @@ int decode_header(unsigned char * data, struct resolv_header * h) h->ancount = (data[6] << 8) | data[7]; h->nscount = (data[8] << 8) | data[9]; h->arcount = (data[10] << 8) | data[11]; - + return 12; } #endif @@ -85,33 +82,33 @@ int decode_header(unsigned char * data, struct resolv_header * h) /* Encode a dotted string into nameserver transport-level encoding. This routine is fairly dumb, and doesn't attempt to compress the data */ - -int encode_dotted(const char * dotted, unsigned char * dest, int maxlen) + +int encode_dotted(const char *dotted, unsigned char *dest, int maxlen) { - int used=0; + int used = 0; - while(dotted && *dotted) { - char * c = strchr(dotted, '.'); + while (dotted && *dotted) { + char *c = strchr(dotted, '.'); int l = c ? c - dotted : strlen(dotted); - - if (l >= (maxlen-used-1)) + + if (l >= (maxlen - used - 1)) return -1; - + dest[used++] = l; - memcpy(dest+used, dotted, l); + memcpy(dest + used, dotted, l); used += l; - + if (c) - dotted = c+1; + dotted = c + 1; else break; } - + if (maxlen < 1) return -1; - + dest[used++] = 0; - + return used; } #endif @@ -120,19 +117,19 @@ int encode_dotted(const char * dotted, unsigned char * dest, int maxlen) /* Decode a dotted string from nameserver transport-level encoding. This routine understands compressed data. */ -int decode_dotted(const unsigned char * data, int offset, - char * dest, int maxlen) +int decode_dotted(const unsigned char *data, int offset, + char *dest, int maxlen) { int l; - int measure=1; + int measure = 1; int total = 0; - int used=0; - + int used = 0; + if (!data) return -1; - - while ((measure && total++), (l=data[offset++])) { - + + while ((measure && total++), (l = data[offset++])) { + if ((l & 0xc0) == (0xc0)) { if (measure) total++; @@ -141,80 +138,80 @@ int decode_dotted(const unsigned char * data, int offset, measure = 0; continue; } - - if ((used+l+1) >= maxlen) + + if ((used + l + 1) >= maxlen) return -1; - - memcpy(dest+used, data+offset, l); + + memcpy(dest + used, data + offset, l); offset += l; used += l; if (measure) total += l; - + if (data[offset] != 0) dest[used++] = '.'; else dest[used++] = '\0'; } - + DPRINTF("Total decode len = %d\n", total); - + return total; } #endif #ifdef L_lengthd -int length_dotted(const unsigned char * data, int offset) +int length_dotted(const unsigned char *data, int offset) { int orig_offset = offset; int l; - + if (!data) return -1; - - while ((l=data[offset++])) { - + + while ((l = data[offset++])) { + if ((l & 0xc0) == (0xc0)) { offset++; break; } - + offset += l; } - - return offset-orig_offset; + + return offset - orig_offset; } #endif #ifdef L_encodeq -int encode_question(struct resolv_question * q, - unsigned char * dest, int maxlen) +int encode_question(struct resolv_question *q, + unsigned char *dest, int maxlen) { int i; i = encode_dotted(q->dotted, dest, maxlen); if (i < 0) return i; - + dest += i; maxlen -= i; - + if (maxlen < 4) return -1; - + dest[0] = (q->qtype & 0xff00) >> 8; dest[1] = (q->qtype & 0x00ff) >> 0; dest[2] = (q->qclass & 0xff00) >> 8; dest[3] = (q->qclass & 0x00ff) >> 0; - - return i+4; + + return i + 4; } #endif #ifdef L_decodeq -int decode_question(unsigned char * message, int offset, - struct resolv_question * q) +int decode_question(unsigned char *message, int offset, + struct resolv_question *q) { char temp[256]; int i; @@ -222,46 +219,45 @@ int decode_question(unsigned char * message, int offset, i = decode_dotted(message, offset, temp, 256); if (i < 0) return i; - + offset += i; - + q->dotted = strdup(temp); - q->qtype = (message[offset+0] << 8) | message[offset+1]; - q->qclass = (message[offset+2] << 8) | message[offset+3]; - - return i+4; + q->qtype = (message[offset + 0] << 8) | message[offset + 1]; + q->qclass = (message[offset + 2] << 8) | message[offset + 3]; + + return i + 4; } #endif #ifdef L_lengthq -int length_question(unsigned char * message, int offset) +int length_question(unsigned char *message, int offset) { int i; i = length_dotted(message, offset); if (i < 0) return i; - - return i+4; + + return i + 4; } #endif #ifdef L_encodea -int encode_answer(struct resolv_answer * a, - unsigned char * dest, int maxlen) +int encode_answer(struct resolv_answer *a, unsigned char *dest, int maxlen) { int i; i = encode_dotted(a->dotted, dest, maxlen); if (i < 0) return i; - + dest += i; maxlen -= i; - - if (maxlen < (10+a->rdlength)) + + if (maxlen < (10 + a->rdlength)) return -1; - + *dest++ = (a->atype & 0xff00) >> 8; *dest++ = (a->atype & 0x00ff) >> 0; *dest++ = (a->aclass & 0xff00) >> 8; @@ -273,14 +269,14 @@ int encode_answer(struct resolv_answer * a, *dest++ = (a->rdlength & 0xff00) >> 8; *dest++ = (a->rdlength & 0x00ff) >> 0; memcpy(dest, a->rdata, a->rdlength); - - return i+10+a->rdlength; + + return i + 10 + a->rdlength; } #endif #ifdef L_decodea -int decode_answer(unsigned char * message, int offset, - struct resolv_answer * a) +int decode_answer(unsigned char *message, int offset, + struct resolv_answer *a) { char temp[256]; int i; @@ -288,47 +284,48 @@ int decode_answer(unsigned char * message, int offset, i = decode_dotted(message, offset, temp, 256); if (i < 0) return i; - - message += offset+i; - + + message += offset + i; + a->dotted = strdup(temp); - a->atype = (message[0] << 8) | message[1]; message += 2; - a->aclass = (message[0] << 8) | message[1]; message += 2; + a->atype = (message[0] << 8) | message[1]; + message += 2; + a->aclass = (message[0] << 8) | message[1]; + message += 2; a->ttl = (message[0] << 24) | - (message[1] << 16) | - (message[2] << 8) | - (message[3] << 0); + (message[1] << 16) | (message[2] << 8) | (message[3] << 0); message += 4; - a->rdlength = (message[0] << 8) | message[1]; message += 2; + a->rdlength = (message[0] << 8) | message[1]; + message += 2; a->rdata = message; - a->rdoffset = offset+i+10; - + a->rdoffset = offset + i + 10; + DPRINTF("i=%d,rdlength=%d\n", i, a->rdlength); - - return i+10+a->rdlength; + + return i + 10 + a->rdlength; } #endif #ifdef L_encodep -int encode_packet(struct resolv_header * h, - struct resolv_question ** q, - struct resolv_answer ** an, - struct resolv_answer ** ns, - struct resolv_answer ** ar, - unsigned char * dest, int maxlen) +int encode_packet(struct resolv_header *h, + struct resolv_question **q, + struct resolv_answer **an, + struct resolv_answer **ns, + struct resolv_answer **ar, + unsigned char *dest, int maxlen) { - int i, total=0; + int i, total = 0; int j; i = encode_header(h, dest, maxlen); if (i < 0) return i; - + dest += i; maxlen -= i; total += i; - - for(j=0;jqdcount;j++) { + + for (j = 0; j < h->qdcount; j++) { i = encode_question(q[j], dest, maxlen); if (i < 0) return i; @@ -337,7 +334,7 @@ int encode_packet(struct resolv_header * h, total += i; } - for(j=0;jancount;j++) { + for (j = 0; j < h->ancount; j++) { i = encode_answer(an[j], dest, maxlen); if (i < 0) return i; @@ -345,7 +342,7 @@ int encode_packet(struct resolv_header * h, maxlen -= i; total += i; } - for(j=0;jnscount;j++) { + for (j = 0; j < h->nscount; j++) { i = encode_answer(ns[j], dest, maxlen); if (i < 0) return i; @@ -353,7 +350,7 @@ int encode_packet(struct resolv_header * h, maxlen -= i; total += i; } - for(j=0;jarcount;j++) { + for (j = 0; j < h->arcount; j++) { i = encode_answer(ar[j], dest, maxlen); if (i < 0) return i; @@ -361,57 +358,59 @@ int encode_packet(struct resolv_header * h, maxlen -= i; total += i; } - + return total; } #endif #ifdef L_decodep -int decode_packet(unsigned char * data, struct resolv_header * h) +int decode_packet(unsigned char *data, struct resolv_header *h) { return decode_header(data, h); } #endif #ifdef L_formquery -int form_query(int id, const char * name, int type, unsigned char * packet, int maxlen) +int form_query(int id, const char *name, int type, unsigned char *packet, + int maxlen) { struct resolv_header h; struct resolv_question q; - int i,j; - + int i, j; + memset(&h, 0, sizeof(h)); h.id = id; h.qdcount = 1; - - q.dotted = (char*)name; + + q.dotted = (char *) name; q.qtype = type; - q.qclass = 1 /*CLASS_IN*/; - + q.qclass = 1 /*CLASS_IN */ ; + i = encode_header(&h, packet, maxlen); if (i < 0) return i; - - j = encode_question(&q, packet+i, maxlen-i); + + j = encode_question(&q, packet + i, maxlen - i); if (j < 0) return j; - return i+j; + return i + j; } #endif #ifdef L_dnslookup int dns_caught_signal = 0; -void dns_catch_signal(int signo) { +void dns_catch_signal(int signo) +{ dns_caught_signal = 1; -} +} -int dns_lookup(const char * name, int type, int nscount, const char ** nsip, - unsigned char ** outpacket, struct resolv_answer * a) +int dns_lookup(const char *name, int type, int nscount, const char **nsip, + unsigned char **outpacket, struct resolv_answer *a) { - static int id=1; - int i,j,len; + static int id = 1; + int i, j, len; int fd; int pos; static int ns = 0; @@ -421,63 +420,63 @@ int dns_lookup(const char * name, int type, int nscount, const char ** nsip, struct resolv_header h; struct resolv_question q; int retries = 0; - unsigned char * packet = malloc(512); - + unsigned char *packet = malloc(512); + if (!packet) goto fail1; - + DPRINTF("Looking up type %d answer for '%s'\n", type, name); - + if (!nscount) goto fail1; - + ns %= nscount; - + fd = -1; - + while (retries++ < MAX_RETRIES) { - + if (fd != -1) close(fd); fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); - - if (fd==-1) + + if (fd == -1) goto fail2; - - + + memset(packet, 0, 512); memset(&h, 0, sizeof(h)); h.id = ++id; h.qdcount = 1; h.rd = 1; - + DPRINTF("encoding header\n"); i = encode_header(&h, packet, 512); if (i < 0) goto fail3; - q.dotted = (char*)name; + q.dotted = (char *) name; q.qtype = type; - q.qclass = 1 /*CLASS_IN*/; - - j = encode_question(&q, packet+i, 512-i); + q.qclass = 1 /*CLASS_IN */ ; + + j = encode_question(&q, packet + i, 512 - i); if (j < 0) goto fail3; - - len = i+j; + + len = i + j; DPRINTF("On try %d, sending query to port %d of machine %s\n", - retries, DNS_SERVICE, nsip[ns]); + retries, DNS_SERVICE, nsip[ns]); sa.sin_family = AF_INET; sa.sin_port = htons(DNS_SERVICE); sa.sin_addr.s_addr = inet_addr(nsip[ns]); - if (connect(fd, (struct sockaddr*)&sa, sizeof(sa))==-1) { + if (connect(fd, (struct sockaddr *) &sa, sizeof(sa)) == -1) { if (errno == ENETUNREACH) { /* routing error, presume not transient */ goto tryall; @@ -485,57 +484,55 @@ int dns_lookup(const char * name, int type, int nscount, const char ** nsip, /* retry */ break; } - - DPRINTF("Transmitting packet of length %d, id=%d, qr=%d\n", - len, h.id, h.qr); + + DPRINTF("Transmitting packet of length %d, id=%d, qr=%d\n", + len, h.id, h.qr); send(fd, packet, len, 0); dns_caught_signal = 0; oldalarm = alarm(REPLY_TIMEOUT); oldhandler = signal(SIGALRM, dns_catch_signal); - + i = recv(fd, packet, 512, 0); - + alarm(0); signal(SIGALRM, oldhandler); alarm(oldalarm); - - DPRINTF("Timeout=%d, len=%d\n", - dns_caught_signal, i); - + + DPRINTF("Timeout=%d, len=%d\n", dns_caught_signal, i); + if (dns_caught_signal) /* timed out, so retry send and receive, to next nameserver on queue */ goto again; - + if (i < 12) /* too short ! */ goto again; - + decode_header(packet, &h); - - DPRINTF("id = %d, qr = %d\n", - h.id, h.qr); - + + DPRINTF("id = %d, qr = %d\n", h.id, h.qr); + if ((h.id != id) || (!h.qr)) /* unsolicited */ goto again; - + DPRINTF("Got response (i think)!\n"); DPRINTF("qrcount=%d,ancount=%d,nscount=%d,arcount=%d\n", - h.qdcount, h.ancount, h.nscount, h.arcount); + h.qdcount, h.ancount, h.nscount, h.arcount); DPRINTF("opcode=%d,aa=%d,tc=%d,rd=%d,ra=%d,rcode=%d\n", - h.opcode,h.aa,h.tc,h.rd,h.ra,h.rcode); - + h.opcode, h.aa, h.tc, h.rd, h.ra, h.rcode); + if ((h.rcode) || (h.ancount < 1)) { /* negative result, not present */ goto tryall; } - + pos = 12; - for(j=0;jdotted); DPRINTF("Answer type = |%d|\n", a->atype); - + close(fd); - + if (outpacket) *outpacket = packet; else free(packet); - return (0); /* success! */ + return (0); /* success! */ - tryall: + tryall: /* if there are other nameservers, give them a go, otherwise return with error */ if (retries >= nscount) break; - again: + again: ns = (ns + 1) % nscount; continue; } - - -fail3: + + + fail3: close(fd); -fail2: + fail2: free(packet); -fail1: + fail1: return -1; } #endif #ifdef L_resolveaddress -int resolve_address(const char * address, - int nscount, const char ** nsip, - struct in_addr * in) +int resolve_address(const char *address, + int nscount, const char **nsip, struct in_addr *in) { - unsigned char * packet; + unsigned char *packet; struct resolv_answer a; char temp[256]; int i; - int nest=0; - + int nest = 0; + if (!address || !in) return -1; - + strcpy(temp, address); - - for(;;) { - + + for (;;) { + i = dns_lookup(temp, 1, nscount, nsip, &packet, &a); - - if (i<0) + + if (i < 0) return -1; - + free(a.dotted); - - if (a.atype == 5) { /* CNAME*/ + + if (a.atype == 5) { /* CNAME */ i = decode_dotted(packet, a.rdoffset, temp, 256); free(packet); - - if (i <0) + + if (i < 0) return -1; if (++nest > MAX_RECURSE) return -1; continue; - } else if (a.atype == 1) { /* ADDRESS */ + } else if (a.atype == 1) { /* ADDRESS */ free(packet); break; } else { @@ -627,57 +623,56 @@ int resolve_address(const char * address, return -1; } } - + if (in) memcpy(in, a.rdata, 4); - + return 0; } #endif #ifdef L_resolvemailbox -int resolve_mailbox(const char * address, - int nscount, const char ** nsip, - struct in_addr * in) +int resolve_mailbox(const char *address, + int nscount, const char **nsip, struct in_addr *in) { struct resolv_answer a; - unsigned char * packet; + unsigned char *packet; char temp[256]; - int nest=0; + int nest = 0; int i; - + if (!address || !in) return -1; - + /* look up mail exchange */ i = dns_lookup(address, 15, nscount, nsip, &packet, &a); - + strcpy(temp, address); - - if (i>=0) { - i = decode_dotted(packet, a.rdoffset+2, temp, 256); + + if (i >= 0) { + i = decode_dotted(packet, a.rdoffset + 2, temp, 256); free(packet); } - - for(;;) { - + + for (;;) { + i = dns_lookup(temp, 1, nscount, nsip, &packet, &a); - - if (i<0) + + if (i < 0) return -1; - + free(a.dotted); - - if (a.atype == 5) { /* CNAME*/ + + if (a.atype == 5) { /* CNAME */ i = decode_dotted(packet, a.rdoffset, temp, 256); free(packet); - if (i<0) + if (i < 0) return i; if (++nest > MAX_RECURSE) return -1; continue; - } else if (a.atype == 1) { /* ADDRESS */ + } else if (a.atype == 1) { /* ADDRESS */ free(packet); break; } else { @@ -688,7 +683,7 @@ int resolve_mailbox(const char * address, if (in) memcpy(in, a.rdata, 4); - + return 0; } #endif @@ -709,7 +704,7 @@ int open_nameservers() if ((fp = fopen("/etc/resolv.conf", "r"))) { if ((arg = cfgfind(fp, "nameserver"))) { - for (i=1; arg[i]; i++) { + for (i = 1; arg[i]; i++) { nameserver[nameservers++] = strdup(arg[i]); } } @@ -720,9 +715,10 @@ int open_nameservers() #endif #ifdef L_closenameservers -void close_nameservers(void) { +void close_nameservers(void) +{ - while(nameservers>0) + while (nameservers > 0) free(nameserver[--nameservers]); } #endif @@ -730,85 +726,85 @@ void close_nameservers(void) { #ifdef L_resolvename -const char * resolve_name(const char * name, int mailbox) +const char *resolve_name(const char *name, int mailbox) { struct in_addr in; int i; - + /* shortcut: is it a valid IP address to begin with? */ if (inet_aton(name, &in)) return name; - + open_nameservers(); - + DPRINTF("looking up '%s', mailbox=%d, nameservers=%d\n", - name, mailbox, nameservers); - + name, mailbox, nameservers); + if (mailbox) i = resolve_mailbox(name, nameservers, nameserver, &in); else i = resolve_address(name, nameservers, nameserver, &in); - - if (i<0) + + if (i < 0) return 0; - + DPRINTF("success = '%s'\n", inet_ntoa(in)); - + return inet_ntoa(in); } #endif #ifdef L_gethostbyname -struct hostent * gethostbyname(const char * name) +struct hostent *gethostbyname(const char *name) { static struct hostent h; static char namebuf[256]; static struct in_addr in; static struct in_addr *addr_list[2]; - unsigned char * packet; + unsigned char *packet; struct resolv_answer a; int i; - int nest=0; + int nest = 0; open_nameservers(); - + if (!name) return 0; - + memset(&h, 0, sizeof(h)); - + addr_list[0] = ∈ addr_list[1] = 0; - + strcpy(namebuf, name); - - for(;;) { - + + for (;;) { + i = dns_lookup(namebuf, 1, nameservers, nameserver, &packet, &a); - - if (i<0) + + if (i < 0) return 0; - + strcpy(namebuf, a.dotted); free(a.dotted); - - - if (a.atype == 5) { /* CNAME*/ + + + if (a.atype == 5) { /* CNAME */ i = decode_dotted(packet, a.rdoffset, namebuf, 256); free(packet); - - if (i <0) + + if (i < 0) return 0; if (++nest > MAX_RECURSE) return 0; continue; - } else if (a.atype == 1) { /* ADDRESS */ + } else if (a.atype == 1) { /* ADDRESS */ memcpy(&in, a.rdata, sizeof(in)); h.h_name = namebuf; h.h_addrtype = AF_INET; h.h_length = sizeof(in); - h.h_addr_list = (char**)addr_list; + h.h_addr_list = (char **) addr_list; free(packet); break; } else { @@ -816,77 +812,75 @@ struct hostent * gethostbyname(const char * name) return 0; } } - + return &h; } #endif #ifdef L_gethostbyaddr -struct hostent * gethostbyaddr(const char * addr, int len, int type) +struct hostent *gethostbyaddr(const char *addr, int len, int type) { static struct hostent h; static char namebuf[256]; static struct in_addr in; static struct in_addr *addr_list[2]; - unsigned char * packet; + unsigned char *packet; struct resolv_answer a; int i; - int nest=0; - + int nest = 0; + if (!addr || (len != sizeof(in)) || (type != AF_INET)) return 0; - + memcpy(&in.s_addr, addr, len); open_nameservers(); - + memset(&h, 0, sizeof(h)); - + addr_list[0] = ∈ addr_list[1] = 0; - + sprintf(namebuf, "%d.%d.%d.%d.in-addr.arpa", - (in.s_addr >> 24) & 0xff, - (in.s_addr >> 16) & 0xff, - (in.s_addr >> 8) & 0xff, - (in.s_addr >> 0) & 0xff - ); - - for(;;) { - + (in.s_addr >> 24) & 0xff, + (in.s_addr >> 16) & 0xff, + (in.s_addr >> 8) & 0xff, (in.s_addr >> 0) & 0xff); + + for (;;) { + i = dns_lookup(namebuf, 12, nameservers, nameserver, &packet, &a); - - if (i<0) + + if (i < 0) return 0; - + strcpy(namebuf, a.dotted); free(a.dotted); - - if (a.atype == 5) { /* CNAME*/ + + if (a.atype == 5) { /* CNAME */ i = decode_dotted(packet, a.rdoffset, namebuf, 256); free(packet); - - if (i <0) + + if (i < 0) return 0; if (++nest > MAX_RECURSE) return 0; continue; - } else if (a.atype == 12) { /* ADDRESS */ + } else if (a.atype == 12) { /* ADDRESS */ i = decode_dotted(packet, a.rdoffset, namebuf, 256); free(packet); - + h.h_name = namebuf; h.h_addrtype = AF_INET; h.h_length = sizeof(in); - h.h_addr_list = (char**)addr_list; + h.h_addr_list = (char **) addr_list; break; } else { free(packet); return 0; } } - + return &h; } #endif diff --git a/libc/inet/rpc/auth_none.c b/libc/inet/rpc/auth_none.c index 630037fb4..7a4dffe87 100644 --- a/libc/inet/rpc/auth_none.c +++ b/libc/inet/rpc/auth_none.c @@ -28,7 +28,9 @@ * Mountain View, California 94043 */ #if !defined(lint) && defined(SCCSIDS) -static char sccsid[] = "@(#)auth_none.c 1.19 87/08/11 Copyr 1984 Sun Micro"; +static char sccsid[] = + + "@(#)auth_none.c 1.19 87/08/11 Copyr 1984 Sun Micro"; #endif /* @@ -47,11 +49,11 @@ static char sccsid[] = "@(#)auth_none.c 1.19 87/08/11 Copyr 1984 Sun Micro"; /* * Authenticator operations routines */ -static void authnone_verf(); -static void authnone_destroy(); -static bool_t authnone_marshal(); -static bool_t authnone_validate(); -static bool_t authnone_refresh(); +static void authnone_verf(); +static void authnone_destroy(); +static bool_t authnone_marshal(); +static bool_t authnone_validate(); +static bool_t authnone_refresh(); static struct auth_ops ops = { authnone_verf, @@ -62,20 +64,19 @@ static struct auth_ops ops = { }; static struct authnone_private { - AUTH no_client; - char marshalled_client[MAX_MARSHEL_SIZE]; - u_int mcnt; + AUTH no_client; + char marshalled_client[MAX_MARSHEL_SIZE]; + u_int mcnt; } *authnone_private; -AUTH * -authnone_create() +AUTH *authnone_create() { register struct authnone_private *ap = authnone_private; XDR xdr_stream; register XDR *xdrs; if (ap == 0) { - ap = (struct authnone_private *)calloc(1, sizeof (*ap)); + ap = (struct authnone_private *) calloc(1, sizeof(*ap)); if (ap == 0) return (0); authnone_private = ap; @@ -84,50 +85,44 @@ authnone_create() ap->no_client.ah_cred = ap->no_client.ah_verf = _null_auth; ap->no_client.ah_ops = &ops; xdrs = &xdr_stream; - xdrmem_create(xdrs, ap->marshalled_client, (u_int)MAX_MARSHEL_SIZE, - XDR_ENCODE); - (void)xdr_opaque_auth(xdrs, &ap->no_client.ah_cred); - (void)xdr_opaque_auth(xdrs, &ap->no_client.ah_verf); + xdrmem_create(xdrs, ap->marshalled_client, + (u_int) MAX_MARSHEL_SIZE, XDR_ENCODE); + (void) xdr_opaque_auth(xdrs, &ap->no_client.ah_cred); + (void) xdr_opaque_auth(xdrs, &ap->no_client.ah_verf); ap->mcnt = XDR_GETPOS(xdrs); XDR_DESTROY(xdrs); } return (&ap->no_client); } -/*ARGSUSED*/ -static bool_t -authnone_marshal(client, xdrs) - AUTH *client; - XDR *xdrs; + /*ARGSUSED*/ static bool_t authnone_marshal(client, xdrs) +AUTH *client; +XDR *xdrs; { register struct authnone_private *ap = authnone_private; if (ap == 0) return (0); - return ((*xdrs->x_ops->x_putbytes)(xdrs, - ap->marshalled_client, ap->mcnt)); + return ((*xdrs->x_ops->x_putbytes) (xdrs, + ap->marshalled_client, ap->mcnt)); } -static void -authnone_verf() +static void authnone_verf() { } -static bool_t -authnone_validate() +static bool_t authnone_validate() { return (TRUE); } -static bool_t -authnone_refresh() +static bool_t authnone_refresh() { return (FALSE); } -static void -authnone_destroy() +static void authnone_destroy() { } diff --git a/libc/inet/rpc/auth_unix.c b/libc/inet/rpc/auth_unix.c index ffdfa11a0..dcd7df98f 100644 --- a/libc/inet/rpc/auth_unix.c +++ b/libc/inet/rpc/auth_unix.c @@ -28,7 +28,9 @@ * Mountain View, California 94043 */ #if !defined(lint) && defined(SCCSIDS) -static char sccsid[] = "@(#)auth_unix.c 1.19 87/08/11 Copyr 1984 Sun Micro"; +static char sccsid[] = + + "@(#)auth_unix.c 1.19 87/08/11 Copyr 1984 Sun Micro"; #endif /* @@ -56,11 +58,11 @@ static char sccsid[] = "@(#)auth_unix.c 1.19 87/08/11 Copyr 1984 Sun Micro"; /* * Unix authenticator operations vector */ -static void authunix_nextverf(); -static bool_t authunix_marshal(); -static bool_t authunix_validate(); -static bool_t authunix_refresh(); -static void authunix_destroy(); +static void authunix_nextverf(); +static bool_t authunix_marshal(); +static bool_t authunix_validate(); +static bool_t authunix_refresh(); +static void authunix_destroy(); static struct auth_ops auth_unix_ops = { authunix_nextverf, @@ -74,12 +76,13 @@ static struct auth_ops auth_unix_ops = { * This struct is pointed to by the ah_private field of an auth_handle. */ struct audata { - struct opaque_auth au_origcred; /* original credentials */ - struct opaque_auth au_shcred; /* short hand cred */ - u_long au_shfaults; /* short hand cache faults */ - char au_marshed[MAX_AUTH_BYTES]; - u_int au_mpos; /* xdr pos at end of marshed */ + struct opaque_auth au_origcred; /* original credentials */ + struct opaque_auth au_shcred; /* short hand cred */ + u_long au_shfaults; /* short hand cache faults */ + char au_marshed[MAX_AUTH_BYTES]; + u_int au_mpos; /* xdr pos at end of marshed */ }; + #define AUTH_PRIVATE(auth) ((struct audata *)auth->ah_private) static bool_t marshal_new_auth(); @@ -89,13 +92,12 @@ static bool_t marshal_new_auth(); * Create a unix style authenticator. * Returns an auth handle with the given stuff in it. */ -AUTH * -authunix_create(machname, uid, gid, len, aup_gids) - char *machname; - int uid; - int gid; - register int len; - int *aup_gids; +AUTH *authunix_create(machname, uid, gid, len, aup_gids) +char *machname; +int uid; +int gid; +register int len; +int *aup_gids; { struct authunix_parms aup; char mymem[MAX_AUTH_BYTES]; @@ -107,41 +109,41 @@ authunix_create(machname, uid, gid, len, aup_gids) /* * Allocate and set up auth handle */ - auth = (AUTH *)mem_alloc(sizeof(*auth)); + auth = (AUTH *) mem_alloc(sizeof(*auth)); #ifndef KERNEL if (auth == NULL) { - (void)fprintf(stderr, "authunix_create: out of memory\n"); + (void) fprintf(stderr, "authunix_create: out of memory\n"); return (NULL); } #endif - au = (struct audata *)mem_alloc(sizeof(*au)); + au = (struct audata *) mem_alloc(sizeof(*au)); #ifndef KERNEL if (au == NULL) { - (void)fprintf(stderr, "authunix_create: out of memory\n"); + (void) fprintf(stderr, "authunix_create: out of memory\n"); return (NULL); } #endif auth->ah_ops = &auth_unix_ops; - auth->ah_private = (caddr_t)au; + auth->ah_private = (caddr_t) au; auth->ah_verf = au->au_shcred = _null_auth; au->au_shfaults = 0; /* * fill in param struct from the given params */ - (void)gettimeofday(&now, (struct timezone *)0); + (void) gettimeofday(&now, (struct timezone *) 0); aup.aup_time = now.tv_sec; aup.aup_machname = machname; aup.aup_uid = uid; aup.aup_gid = gid; - aup.aup_len = (u_int)len; + aup.aup_len = (u_int) len; aup.aup_gids = aup_gids; /* * Serialize the parameters into origcred */ xdrmem_create(&xdrs, mymem, MAX_AUTH_BYTES, XDR_ENCODE); - if (! xdr_authunix_parms(&xdrs, &aup)) + if (!xdr_authunix_parms(&xdrs, &aup)) abort(); au->au_origcred.oa_length = len = XDR_GETPOS(&xdrs); au->au_origcred.oa_flavor = AUTH_UNIX; @@ -149,11 +151,11 @@ authunix_create(machname, uid, gid, len, aup_gids) au->au_origcred.oa_base = mem_alloc((u_int) len); #else if ((au->au_origcred.oa_base = mem_alloc((u_int) len)) == NULL) { - (void)fprintf(stderr, "authunix_create: out of memory\n"); + (void) fprintf(stderr, "authunix_create: out of memory\n"); return (NULL); } #endif - bcopy(mymem, au->au_origcred.oa_base, (u_int)len); + bcopy(mymem, au->au_origcred.oa_base, (u_int) len); /* * set auth handle to reflect new cred. @@ -167,8 +169,7 @@ authunix_create(machname, uid, gid, len, aup_gids) * Returns an auth handle with parameters determined by doing lots of * syscalls. */ -AUTH * -authunix_create_default() +AUTH *authunix_create_default() { register int len; char machname[MAX_MACHINE_NAME + 1]; @@ -190,27 +191,24 @@ authunix_create_default() * authunix operations */ -static void -authunix_nextverf(auth) - AUTH *auth; +static void authunix_nextverf(auth) +AUTH *auth; { /* no action necessary */ } -static bool_t -authunix_marshal(auth, xdrs) - AUTH *auth; - XDR *xdrs; +static bool_t authunix_marshal(auth, xdrs) +AUTH *auth; +XDR *xdrs; { register struct audata *au = AUTH_PRIVATE(auth); return (XDR_PUTBYTES(xdrs, au->au_marshed, au->au_mpos)); } -static bool_t -authunix_validate(auth, verf) - register AUTH *auth; - struct opaque_auth verf; +static bool_t authunix_validate(auth, verf) +register AUTH *auth; +struct opaque_auth verf; { register struct audata *au; XDR xdrs; @@ -220,15 +218,14 @@ authunix_validate(auth, verf) xdrmem_create(&xdrs, verf.oa_base, verf.oa_length, XDR_DECODE); if (au->au_shcred.oa_base != NULL) { - mem_free(au->au_shcred.oa_base, - au->au_shcred.oa_length); + mem_free(au->au_shcred.oa_base, au->au_shcred.oa_length); au->au_shcred.oa_base = NULL; } if (xdr_opaque_auth(&xdrs, &au->au_shcred)) { auth->ah_cred = au->au_shcred; } else { xdrs.x_op = XDR_FREE; - (void)xdr_opaque_auth(&xdrs, &au->au_shcred); + (void) xdr_opaque_auth(&xdrs, &au->au_shcred); au->au_shcred.oa_base = NULL; auth->ah_cred = au->au_origcred; } @@ -237,9 +234,8 @@ authunix_validate(auth, verf) return (TRUE); } -static bool_t -authunix_refresh(auth) - register AUTH *auth; +static bool_t authunix_refresh(auth) +register AUTH *auth; { register struct audata *au = AUTH_PRIVATE(auth); struct authunix_parms aup; @@ -251,38 +247,37 @@ authunix_refresh(auth) /* there is no hope. Punt */ return (FALSE); } - au->au_shfaults ++; + au->au_shfaults++; /* first deserialize the creds back into a struct authunix_parms */ aup.aup_machname = NULL; - aup.aup_gids = (int *)NULL; + aup.aup_gids = (int *) NULL; xdrmem_create(&xdrs, au->au_origcred.oa_base, - au->au_origcred.oa_length, XDR_DECODE); + au->au_origcred.oa_length, XDR_DECODE); stat = xdr_authunix_parms(&xdrs, &aup); - if (! stat) + if (!stat) goto done; /* update the time and serialize in place */ - (void)gettimeofday(&now, (struct timezone *)0); + (void) gettimeofday(&now, (struct timezone *) 0); aup.aup_time = now.tv_sec; xdrs.x_op = XDR_ENCODE; XDR_SETPOS(&xdrs, 0); stat = xdr_authunix_parms(&xdrs, &aup); - if (! stat) + if (!stat) goto done; auth->ah_cred = au->au_origcred; marshal_new_auth(auth); -done: + done: /* free the struct authunix_parms created by deserializing */ xdrs.x_op = XDR_FREE; - (void)xdr_authunix_parms(&xdrs, &aup); + (void) xdr_authunix_parms(&xdrs, &aup); XDR_DESTROY(&xdrs); return (stat); } -static void -authunix_destroy(auth) - register AUTH *auth; +static void authunix_destroy(auth) +register AUTH *auth; { register struct audata *au = AUTH_PRIVATE(auth); @@ -296,24 +291,23 @@ authunix_destroy(auth) if (auth->ah_verf.oa_base != NULL) mem_free(auth->ah_verf.oa_base, auth->ah_verf.oa_length); - mem_free((caddr_t)auth, sizeof(*auth)); + mem_free((caddr_t) auth, sizeof(*auth)); } /* * Marshals (pre-serializes) an auth struct. * sets private data, au_marshed and au_mpos */ -static bool_t -marshal_new_auth(auth) - register AUTH *auth; +static bool_t marshal_new_auth(auth) +register AUTH *auth; { - XDR xdr_stream; - register XDR *xdrs = &xdr_stream; + XDR xdr_stream; + register XDR *xdrs = &xdr_stream; register struct audata *au = AUTH_PRIVATE(auth); xdrmem_create(xdrs, au->au_marshed, MAX_AUTH_BYTES, XDR_ENCODE); - if ((! xdr_opaque_auth(xdrs, &(auth->ah_cred))) || - (! xdr_opaque_auth(xdrs, &(auth->ah_verf)))) { + if ((!xdr_opaque_auth(xdrs, &(auth->ah_cred))) || + (!xdr_opaque_auth(xdrs, &(auth->ah_verf)))) { perror("auth_none.c - Fatal marshalling problem"); } else { au->au_mpos = XDR_GETPOS(xdrs); diff --git a/libc/inet/rpc/authunix_prot.c b/libc/inet/rpc/authunix_prot.c index a60d99a57..cae173571 100644 --- a/libc/inet/rpc/authunix_prot.c +++ b/libc/inet/rpc/authunix_prot.c @@ -28,7 +28,9 @@ * Mountain View, California 94043 */ #if !defined(lint) && defined(SCCSIDS) -static char sccsid[] = "@(#)authunix_prot.c 1.15 87/08/11 Copyr 1984 Sun Micro"; +static char sccsid[] = + + "@(#)authunix_prot.c 1.15 87/08/11 Copyr 1984 Sun Micro"; #endif /* @@ -47,20 +49,18 @@ static char sccsid[] = "@(#)authunix_prot.c 1.15 87/08/11 Copyr 1984 Sun Micro"; /* * XDR for unix authentication parameters. */ -bool_t -xdr_authunix_parms(xdrs, p) - register XDR *xdrs; - register struct authunix_parms *p; +bool_t xdr_authunix_parms(xdrs, p) +register XDR *xdrs; +register struct authunix_parms *p; { if (xdr_u_long(xdrs, &(p->aup_time)) - && xdr_string(xdrs, &(p->aup_machname), MAX_MACHINE_NAME) - && xdr_int(xdrs, &(p->aup_uid)) - && xdr_int(xdrs, &(p->aup_gid)) - && xdr_array(xdrs, (caddr_t *)&(p->aup_gids), - &(p->aup_len), NGRPS, sizeof(int), xdr_int) ) { + && xdr_string(xdrs, &(p->aup_machname), MAX_MACHINE_NAME) + && xdr_int(xdrs, &(p->aup_uid)) + && xdr_int(xdrs, &(p->aup_gid)) + && xdr_array(xdrs, (caddr_t *) & (p->aup_gids), + &(p->aup_len), NGRPS, sizeof(int), xdr_int)) { return (TRUE); } return (FALSE); } - diff --git a/libc/inet/rpc/bindresvport.c b/libc/inet/rpc/bindresvport.c index 71803dd41..6a6f385cd 100644 --- a/libc/inet/rpc/bindresvport.c +++ b/libc/inet/rpc/bindresvport.c @@ -1,4 +1,6 @@ -static char sccsid[] = "@(#)bindresvport.c 2.2 88/07/29 4.0 RPCSRC 1.8 88/02/08 SMI"; +static char sccsid[] = + + "@(#)bindresvport.c 2.2 88/07/29 4.0 RPCSRC 1.8 88/02/08 SMI"; /* * Sun RPC is a product of Sun Microsystems, Inc. and is provided for * unrestricted use provided that this legend is included on all tape @@ -41,8 +43,8 @@ static char sccsid[] = "@(#)bindresvport.c 2.2 88/07/29 4.0 RPCSRC 1.8 88/02/08 * Bind a socket to a privileged IP port */ bindresvport(sd, sin) - int sd; - struct sockaddr_in *sin; +int sd; +struct sockaddr_in *sin; { int res; static short port; @@ -54,9 +56,9 @@ bindresvport(sd, sin) #define ENDPORT (IPPORT_RESERVED - 1) #define NPORTS (ENDPORT - STARTPORT + 1) - if (sin == (struct sockaddr_in *)0) { + if (sin == (struct sockaddr_in *) 0) { sin = &myaddr; - bzero(sin, sizeof (*sin)); + bzero(sin, sizeof(*sin)); sin->sin_family = AF_INET; } else if (sin->sin_family != AF_INET) { errno = EPFNOSUPPORT; diff --git a/libc/inet/rpc/clnt_generic.c b/libc/inet/rpc/clnt_generic.c index fabcc2b32..ba5309492 100644 --- a/libc/inet/rpc/clnt_generic.c +++ b/libc/inet/rpc/clnt_generic.c @@ -43,12 +43,11 @@ static char sccsid[] = "@(#)clnt_generic.c 1.4 87/08/11 (C) 1987 SMI"; * returns client handle. Default options are set, which the user can * change using the rpc equivalent of ioctl()'s. */ -CLIENT * -clnt_create(hostname, prog, vers, proto) - char *hostname; - unsigned prog; - unsigned vers; - char *proto; +CLIENT *clnt_create(hostname, prog, vers, proto) +char *hostname; +unsigned prog; +unsigned vers; +char *proto; { struct hostent *h; struct protoent *p; @@ -67,7 +66,7 @@ clnt_create(hostname, prog, vers, proto) * Only support INET for now */ rpc_createerr.cf_stat = RPC_SYSTEMERROR; - rpc_createerr.cf_error.re_errno = EAFNOSUPPORT; + rpc_createerr.cf_error.re_errno = EAFNOSUPPORT; return (NULL); } #ifdef __linux__ @@ -78,11 +77,11 @@ clnt_create(hostname, prog, vers, proto) #ifndef __linux__ bzero(sin.sin_zero, sizeof(sin.sin_zero)); #endif - bcopy(h->h_addr, (char*)&sin.sin_addr, h->h_length); + bcopy(h->h_addr, (char *) &sin.sin_addr, h->h_length); p = getprotobyname(proto); if (p == NULL) { rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; - rpc_createerr.cf_error.re_errno = EPFNOSUPPORT; + rpc_createerr.cf_error.re_errno = EPFNOSUPPORT; return (NULL); } sock = RPC_ANYSOCK; @@ -108,7 +107,7 @@ clnt_create(hostname, prog, vers, proto) break; default: rpc_createerr.cf_stat = RPC_SYSTEMERROR; - rpc_createerr.cf_error.re_errno = EPFNOSUPPORT; + rpc_createerr.cf_error.re_errno = EPFNOSUPPORT; return (NULL); } return (client); diff --git a/libc/inet/rpc/clnt_perror.c b/libc/inet/rpc/clnt_perror.c index 80f51aa79..84d2658ee 100644 --- a/libc/inet/rpc/clnt_perror.c +++ b/libc/inet/rpc/clnt_perror.c @@ -28,7 +28,9 @@ * Mountain View, California 94043 */ #if !defined(lint) && defined(SCCSIDS) -static char sccsid[] = "@(#)clnt_perror.c 1.15 87/10/07 Copyr 1984 Sun Micro"; +static char sccsid[] = + + "@(#)clnt_perror.c 1.15 87/10/07 Copyr 1984 Sun Micro"; #endif /* @@ -50,22 +52,20 @@ extern char *strcpy(); static char *buf; -static char * -_buf() +static char *_buf() { if (buf == 0) - buf = (char *)malloc(256); + buf = (char *) malloc(256); return (buf); } /* * Print reply error info */ -char * -clnt_sperror(rpch, s) - CLIENT *rpch; - char *s; +char *clnt_sperror(rpch, s) +CLIENT *rpch; +char *s; { #if 0 struct rpc_err e; @@ -78,17 +78,17 @@ clnt_sperror(rpch, s) return (0); CLNT_GETERR(rpch, &e); - (void) sprintf(str, "%s: ", s); + (void) sprintf(str, "%s: ", s); str += strlen(str); - (void) strcpy(str, clnt_sperrno(e.re_status)); + (void) strcpy(str, clnt_sperrno(e.re_status)); str += strlen(str); switch (e.re_status) { case RPC_SUCCESS: case RPC_CANTENCODEARGS: case RPC_CANTDECODERES: - case RPC_TIMEDOUT: + case RPC_TIMEDOUT: case RPC_PROGUNAVAIL: case RPC_PROCUNAVAIL: case RPC_CANTDECODEARGS: @@ -102,57 +102,54 @@ clnt_sperror(rpch, s) case RPC_CANTSEND: case RPC_CANTRECV: - (void) sprintf(str, "; errno = %s", - sys_errlist[e.re_errno]); + (void) sprintf(str, "; errno = %s", sys_errlist[e.re_errno]); str += strlen(str); break; case RPC_VERSMISMATCH: (void) sprintf(str, - "; low version = %lu, high version = %lu", - e.re_vers.low, e.re_vers.high); + "; low version = %lu, high version = %lu", + e.re_vers.low, e.re_vers.high); str += strlen(str); break; case RPC_AUTHERROR: err = auth_errmsg(e.re_why); - (void) sprintf(str,"; why = "); + (void) sprintf(str, "; why = "); str += strlen(str); if (err != NULL) { - (void) sprintf(str, "%s",err); + (void) sprintf(str, "%s", err); } else { (void) sprintf(str, - "(unknown authentication error - %d)", - (int) e.re_why); + "(unknown authentication error - %d)", + (int) e.re_why); } str += strlen(str); break; case RPC_PROGVERSMISMATCH: - (void) sprintf(str, - "; low version = %lu, high version = %lu", - e.re_vers.low, e.re_vers.high); + (void) sprintf(str, + "; low version = %lu, high version = %lu", + e.re_vers.low, e.re_vers.high); str += strlen(str); break; - default: /* unknown */ - (void) sprintf(str, - "; s1 = %lu, s2 = %lu", - e.re_lb.s1, e.re_lb.s2); + default: /* unknown */ + (void) sprintf(str, + "; s1 = %lu, s2 = %lu", e.re_lb.s1, e.re_lb.s2); str += strlen(str); break; } (void) sprintf(str, "\n"); - return(strstart) ; + return (strstart); #endif } -void -clnt_perror(rpch, s) - CLIENT *rpch; - char *s; +void clnt_perror(rpch, s) +CLIENT *rpch; +char *s; { - (void) fprintf(stderr,"%s",clnt_sperror(rpch,s)); + (void) fprintf(stderr, "%s", clnt_sperror(rpch, s)); } @@ -160,58 +157,58 @@ struct rpc_errtab { enum clnt_stat status; char *message; }; + #if 0 -static struct rpc_errtab rpc_errlist[] = { - { RPC_SUCCESS, - "RPC: Success" }, - { RPC_CANTENCODEARGS, - "RPC: Can't encode arguments" }, - { RPC_CANTDECODERES, - "RPC: Can't decode result" }, - { RPC_CANTSEND, - "RPC: Unable to send" }, - { RPC_CANTRECV, - "RPC: Unable to receive" }, - { RPC_TIMEDOUT, - "RPC: Timed out" }, - { RPC_VERSMISMATCH, - "RPC: Incompatible versions of RPC" }, - { RPC_AUTHERROR, - "RPC: Authentication error" }, - { RPC_PROGUNAVAIL, - "RPC: Program unavailable" }, - { RPC_PROGVERSMISMATCH, - "RPC: Program/version mismatch" }, - { RPC_PROCUNAVAIL, - "RPC: Procedure unavailable" }, - { RPC_CANTDECODEARGS, - "RPC: Server can't decode arguments" }, - { RPC_SYSTEMERROR, - "RPC: Remote system error" }, - { RPC_UNKNOWNHOST, - "RPC: Unknown host" }, - { RPC_UNKNOWNPROTO, - "RPC: Unknown protocol" }, - { RPC_PMAPFAILURE, - "RPC: Port mapper failure" }, - { RPC_PROGNOTREGISTERED, - "RPC: Program not registered"}, - { RPC_FAILED, - "RPC: Failed (unspecified error)"} +static struct rpc_errtab rpc_errlist[] = { + {RPC_SUCCESS, + "RPC: Success"}, + {RPC_CANTENCODEARGS, + "RPC: Can't encode arguments"}, + {RPC_CANTDECODERES, + "RPC: Can't decode result"}, + {RPC_CANTSEND, + "RPC: Unable to send"}, + {RPC_CANTRECV, + "RPC: Unable to receive"}, + {RPC_TIMEDOUT, + "RPC: Timed out"}, + {RPC_VERSMISMATCH, + "RPC: Incompatible versions of RPC"}, + {RPC_AUTHERROR, + "RPC: Authentication error"}, + {RPC_PROGUNAVAIL, + "RPC: Program unavailable"}, + {RPC_PROGVERSMISMATCH, + "RPC: Program/version mismatch"}, + {RPC_PROCUNAVAIL, + "RPC: Procedure unavailable"}, + {RPC_CANTDECODEARGS, + "RPC: Server can't decode arguments"}, + {RPC_SYSTEMERROR, + "RPC: Remote system error"}, + {RPC_UNKNOWNHOST, + "RPC: Unknown host"}, + {RPC_UNKNOWNPROTO, + "RPC: Unknown protocol"}, + {RPC_PMAPFAILURE, + "RPC: Port mapper failure"}, + {RPC_PROGNOTREGISTERED, + "RPC: Program not registered"}, + {RPC_FAILED, + "RPC: Failed (unspecified error)"} }; #endif /* * This interface for use by clntrpc */ -char * -clnt_sperrno(stat) - enum clnt_stat stat; +char *clnt_sperrno(stat) +enum clnt_stat stat; { #if 0 int i; - for (i = 0; i < sizeof(rpc_errlist)/sizeof(struct rpc_errtab); i++) { + for (i = 0; i < sizeof(rpc_errlist) / sizeof(struct rpc_errtab); i++) { if (rpc_errlist[i].status == stat) { return (rpc_errlist[i].message); } @@ -220,17 +217,15 @@ clnt_sperrno(stat) return ("RPC: (unknown error code)"); } -void -clnt_perrno(num) - enum clnt_stat num; +void clnt_perrno(num) +enum clnt_stat num; { - (void) fprintf(stderr,"%s",clnt_sperrno(num)); + (void) fprintf(stderr, "%s", clnt_sperrno(num)); } -char * -clnt_spcreateerror(s) - char *s; +char *clnt_spcreateerror(s) +char *s; { #if 0 extern int sys_nerr; @@ -238,25 +233,24 @@ clnt_spcreateerror(s) char *str = _buf(); if (str == 0) - return(0); + return (0); (void) sprintf(str, "%s: ", s); (void) strcat(str, clnt_sperrno(rpc_createerr.cf_stat)); switch (rpc_createerr.cf_stat) { case RPC_PMAPFAILURE: (void) strcat(str, " - "); - (void) strcat(str, - clnt_sperrno(rpc_createerr.cf_error.re_status)); + (void) strcat(str, clnt_sperrno(rpc_createerr.cf_error.re_status)); break; case RPC_SYSTEMERROR: (void) strcat(str, " - "); if (rpc_createerr.cf_error.re_errno > 0 - && rpc_createerr.cf_error.re_errno < sys_nerr) + && rpc_createerr.cf_error.re_errno < sys_nerr) (void) strcat(str, - sys_errlist[rpc_createerr.cf_error.re_errno]); + sys_errlist[rpc_createerr.cf_error.re_errno]); else (void) sprintf(&str[strlen(str)], "Error %d", - rpc_createerr.cf_error.re_errno); + rpc_createerr.cf_error.re_errno); break; } (void) strcat(str, "\n"); @@ -264,47 +258,45 @@ clnt_spcreateerror(s) #endif } -void -clnt_pcreateerror(s) - char *s; +void clnt_pcreateerror(s) +char *s; { - (void) fprintf(stderr,"%s",clnt_spcreateerror(s)); + (void) fprintf(stderr, "%s", clnt_spcreateerror(s)); } struct auth_errtab { - enum auth_stat status; + enum auth_stat status; char *message; }; static struct auth_errtab auth_errlist[] = { - { AUTH_OK, - "Authentication OK" }, - { AUTH_BADCRED, - "Invalid client credential" }, - { AUTH_REJECTEDCRED, - "Server rejected credential" }, - { AUTH_BADVERF, - "Invalid client verifier" }, - { AUTH_REJECTEDVERF, - "Server rejected verifier" }, - { AUTH_TOOWEAK, - "Client credential too weak" }, - { AUTH_INVALIDRESP, - "Invalid server verifier" }, - { AUTH_FAILED, - "Failed (unspecified error)" }, + {AUTH_OK, + "Authentication OK"}, + {AUTH_BADCRED, + "Invalid client credential"}, + {AUTH_REJECTEDCRED, + "Server rejected credential"}, + {AUTH_BADVERF, + "Invalid client verifier"}, + {AUTH_REJECTEDVERF, + "Server rejected verifier"}, + {AUTH_TOOWEAK, + "Client credential too weak"}, + {AUTH_INVALIDRESP, + "Invalid server verifier"}, + {AUTH_FAILED, + "Failed (unspecified error)"}, }; -static char * -auth_errmsg(stat) - enum auth_stat stat; +static char *auth_errmsg(stat) +enum auth_stat stat; { int i; - for (i = 0; i < sizeof(auth_errlist)/sizeof(struct auth_errtab); i++) { + for (i = 0; i < sizeof(auth_errlist) / sizeof(struct auth_errtab); i++) { if (auth_errlist[i].status == stat) { - return(auth_errlist[i].message); + return (auth_errlist[i].message); } } - return(NULL); + return (NULL); } diff --git a/libc/inet/rpc/clnt_raw.c b/libc/inet/rpc/clnt_raw.c index 89059ae2d..7479b55d6 100644 --- a/libc/inet/rpc/clnt_raw.c +++ b/libc/inet/rpc/clnt_raw.c @@ -50,19 +50,19 @@ static char sccsid[] = "@(#)clnt_raw.c 1.22 87/08/11 Copyr 1984 Sun Micro"; * This is the "network" we will be moving stuff over. */ static struct clntraw_private { - CLIENT client_object; - XDR xdr_stream; - char _raw_buf[UDPMSGSIZE]; - char mashl_callmsg[MCALL_MSG_SIZE]; - u_int mcnt; + CLIENT client_object; + XDR xdr_stream; + char _raw_buf[UDPMSGSIZE]; + char mashl_callmsg[MCALL_MSG_SIZE]; + u_int mcnt; } *clntraw_private; -static enum clnt_stat clntraw_call(); -static void clntraw_abort(); -static void clntraw_geterr(); -static bool_t clntraw_freeres(); -static bool_t clntraw_control(); -static void clntraw_destroy(); +static enum clnt_stat clntraw_call(); +static void clntraw_abort(); +static void clntraw_geterr(); +static bool_t clntraw_freeres(); +static bool_t clntraw_control(); +static void clntraw_destroy(); static struct clnt_ops client_ops = { clntraw_call, @@ -73,23 +73,22 @@ static struct clnt_ops client_ops = { clntraw_control }; -void svc_getreq(); +void svc_getreq(); /* * Create a client handle for memory based rpc. */ -CLIENT * -clntraw_create(prog, vers) - u_long prog; - u_long vers; +CLIENT *clntraw_create(prog, vers) +u_long prog; +u_long vers; { register struct clntraw_private *clp = clntraw_private; struct rpc_msg call_msg; XDR *xdrs = &clp->xdr_stream; - CLIENT *client = &clp->client_object; + CLIENT *client = &clp->client_object; if (clp == 0) { - clp = (struct clntraw_private *)calloc(1, sizeof (*clp)); + clp = (struct clntraw_private *) calloc(1, sizeof(*clp)); if (clp == 0) return (0); clntraw_private = clp; @@ -101,8 +100,8 @@ clntraw_create(prog, vers) call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION; call_msg.rm_call.cb_prog = prog; call_msg.rm_call.cb_vers = vers; - xdrmem_create(xdrs, clp->mashl_callmsg, MCALL_MSG_SIZE, XDR_ENCODE); - if (! xdr_callhdr(xdrs, &call_msg)) { + xdrmem_create(xdrs, clp->mashl_callmsg, MCALL_MSG_SIZE, XDR_ENCODE); + if (!xdr_callhdr(xdrs, &call_msg)) { perror("clnt_raw.c - Fatal header serialization error."); } clp->mcnt = XDR_GETPOS(xdrs); @@ -121,15 +120,15 @@ clntraw_create(prog, vers) return (client); } -static enum clnt_stat +static enum clnt_stat clntraw_call(h, proc, xargs, argsp, xresults, resultsp, timeout) - CLIENT *h; - u_long proc; - xdrproc_t xargs; - caddr_t argsp; - xdrproc_t xresults; - caddr_t resultsp; - struct timeval timeout; +CLIENT *h; +u_long proc; +xdrproc_t xargs; +caddr_t argsp; +xdrproc_t xresults; +caddr_t resultsp; +struct timeval timeout; { register struct clntraw_private *clp = clntraw_private; register XDR *xdrs = &clp->xdr_stream; @@ -139,20 +138,19 @@ clntraw_call(h, proc, xargs, argsp, xresults, resultsp, timeout) if (clp == 0) return (RPC_FAILED); -call_again: + call_again: /* * send request */ xdrs->x_op = XDR_ENCODE; XDR_SETPOS(xdrs, 0); - ((struct rpc_msg *)clp->mashl_callmsg)->rm_xid ++ ; - if ((! XDR_PUTBYTES(xdrs, clp->mashl_callmsg, clp->mcnt)) || - (! XDR_PUTLONG(xdrs, (long *)&proc)) || - (! AUTH_MARSHALL(h->cl_auth, xdrs)) || - (! (*xargs)(xdrs, argsp))) { + ((struct rpc_msg *) clp->mashl_callmsg)->rm_xid++; + if ((!XDR_PUTBYTES(xdrs, clp->mashl_callmsg, clp->mcnt)) || + (!XDR_PUTLONG(xdrs, (long *) &proc)) || + (!AUTH_MARSHALL(h->cl_auth, xdrs)) || (!(*xargs) (xdrs, argsp))) { return (RPC_CANTENCODEARGS); } - (void)XDR_GETPOS(xdrs); /* called just to cause overhead */ + (void) XDR_GETPOS(xdrs); /* called just to cause overhead */ /* * We have to call server input routine here because this is @@ -168,71 +166,65 @@ call_again: msg.acpted_rply.ar_verf = _null_auth; msg.acpted_rply.ar_results.where = resultsp; msg.acpted_rply.ar_results.proc = xresults; - if (! xdr_replymsg(xdrs, &msg)) + if (!xdr_replymsg(xdrs, &msg)) return (RPC_CANTDECODERES); _seterr_reply(&msg, &error); status = error.re_status; if (status == RPC_SUCCESS) { - if (! AUTH_VALIDATE(h->cl_auth, &msg.acpted_rply.ar_verf)) { + if (!AUTH_VALIDATE(h->cl_auth, &msg.acpted_rply.ar_verf)) { status = RPC_AUTHERROR; } - } /* end successful completion */ + } /* end successful completion */ else { if (AUTH_REFRESH(h->cl_auth)) goto call_again; - } /* end of unsuccessful completion */ + } /* end of unsuccessful completion */ if (status == RPC_SUCCESS) { - if (! AUTH_VALIDATE(h->cl_auth, &msg.acpted_rply.ar_verf)) { + if (!AUTH_VALIDATE(h->cl_auth, &msg.acpted_rply.ar_verf)) { status = RPC_AUTHERROR; } if (msg.acpted_rply.ar_verf.oa_base != NULL) { xdrs->x_op = XDR_FREE; - (void)xdr_opaque_auth(xdrs, &(msg.acpted_rply.ar_verf)); + (void) xdr_opaque_auth(xdrs, &(msg.acpted_rply.ar_verf)); } } return (status); } -static void -clntraw_geterr() +static void clntraw_geterr() { } -static bool_t -clntraw_freeres(cl, xdr_res, res_ptr) - CLIENT *cl; - xdrproc_t xdr_res; - caddr_t res_ptr; +static bool_t clntraw_freeres(cl, xdr_res, res_ptr) +CLIENT *cl; +xdrproc_t xdr_res; +caddr_t res_ptr; { register struct clntraw_private *clp = clntraw_private; register XDR *xdrs = &clp->xdr_stream; bool_t rval; - if (clp == 0) - { + if (clp == 0) { rval = (bool_t) RPC_FAILED; return (rval); } xdrs->x_op = XDR_FREE; - return ((*xdr_res)(xdrs, res_ptr)); + return ((*xdr_res) (xdrs, res_ptr)); } -static void -clntraw_abort() +static void clntraw_abort() { } -static bool_t -clntraw_control() +static bool_t clntraw_control() { return (FALSE); } -static void -clntraw_destroy() +static void clntraw_destroy() { } diff --git a/libc/inet/rpc/clnt_simple.c b/libc/inet/rpc/clnt_simple.c index 043ce0a3e..77657ecf3 100644 --- a/libc/inet/rpc/clnt_simple.c +++ b/libc/inet/rpc/clnt_simple.c @@ -28,7 +28,9 @@ * Mountain View, California 94043 */ #if !defined(lint) && defined(SCCSIDS) -static char sccsid[] = "@(#)clnt_simple.c 1.35 87/08/11 Copyr 1984 Sun Micro"; +static char sccsid[] = + + "@(#)clnt_simple.c 1.35 87/08/11 Copyr 1984 Sun Micro"; #endif /* @@ -45,16 +47,16 @@ static char sccsid[] = "@(#)clnt_simple.c 1.35 87/08/11 Copyr 1984 Sun Micro"; #include static struct callrpc_private { - CLIENT *client; - int socket; - int oldprognum, oldversnum, valid; - char *oldhost; + CLIENT *client; + int socket; + int oldprognum, oldversnum, valid; + char *oldhost; } *callrpc_private; callrpc(host, prognum, versnum, procnum, inproc, in, outproc, out) - char *host; - xdrproc_t inproc, outproc; - char *in, *out; +char *host; +xdrproc_t inproc, outproc; +char *in, *out; { register struct callrpc_private *crp = callrpc_private; struct sockaddr_in server_addr; @@ -63,7 +65,7 @@ callrpc(host, prognum, versnum, procnum, inproc, in, outproc, out) struct timeval timeout, tottimeout; if (crp == 0) { - crp = (struct callrpc_private *)calloc(1, sizeof (*crp)); + crp = (struct callrpc_private *) calloc(1, sizeof(*crp)); if (crp == 0) return (0); callrpc_private = crp; @@ -73,12 +75,12 @@ callrpc(host, prognum, versnum, procnum, inproc, in, outproc, out) crp->oldhost[0] = 0; crp->socket = RPC_ANYSOCK; } - if (crp->valid && crp->oldprognum == prognum && crp->oldversnum == versnum - && strcmp(crp->oldhost, host) == 0) { - /* reuse old client */ + if (crp->valid && crp->oldprognum == prognum + && crp->oldversnum == versnum && strcmp(crp->oldhost, host) == 0) { + /* reuse old client */ } else { crp->valid = 0; - (void)close(crp->socket); + (void) close(crp->socket); crp->socket = RPC_ANYSOCK; if (crp->client) { clnt_destroy(crp->client); @@ -88,11 +90,12 @@ callrpc(host, prognum, versnum, procnum, inproc, in, outproc, out) return ((int) RPC_UNKNOWNHOST); timeout.tv_usec = 0; timeout.tv_sec = 5; - bcopy(hp->h_addr, (char *)&server_addr.sin_addr, hp->h_length); + bcopy(hp->h_addr, (char *) &server_addr.sin_addr, hp->h_length); server_addr.sin_family = AF_INET; - server_addr.sin_port = 0; - if ((crp->client = clntudp_create(&server_addr, (u_long)prognum, - (u_long)versnum, timeout, &crp->socket)) == NULL) + server_addr.sin_port = 0; + if ((crp->client = clntudp_create(&server_addr, (u_long) prognum, + (u_long) versnum, timeout, + &crp->socket)) == NULL) return ((int) rpc_createerr.cf_stat); crp->valid = 1; crp->oldprognum = prognum; @@ -102,7 +105,7 @@ callrpc(host, prognum, versnum, procnum, inproc, in, outproc, out) tottimeout.tv_sec = 25; tottimeout.tv_usec = 0; clnt_stat = clnt_call(crp->client, procnum, inproc, in, - outproc, out, tottimeout); + outproc, out, tottimeout); /* * if call failed, empty cache */ diff --git a/libc/inet/rpc/clnt_tcp.c b/libc/inet/rpc/clnt_tcp.c index 2222bc657..30422268a 100644 --- a/libc/inet/rpc/clnt_tcp.c +++ b/libc/inet/rpc/clnt_tcp.c @@ -30,7 +30,7 @@ #if !defined(lint) && defined(SCCSIDS) static char sccsid[] = "@(#)clnt_tcp.c 1.37 87/10/05 Copyr 1984 Sun Micro"; #endif - + /* * clnt_tcp.c, Implements a TCP/IP based, client side RPC. * @@ -61,15 +61,15 @@ static char sccsid[] = "@(#)clnt_tcp.c 1.37 87/10/05 Copyr 1984 Sun Micro"; extern int errno; -static int readtcp(); -static int writetcp(); +static int readtcp(); +static int writetcp(); -static enum clnt_stat clnttcp_call(); -static void clnttcp_abort(); -static void clnttcp_geterr(); -static bool_t clnttcp_freeres(); -static bool_t clnttcp_control(); -static void clnttcp_destroy(); +static enum clnt_stat clnttcp_call(); +static void clnttcp_abort(); +static void clnttcp_geterr(); +static bool_t clnttcp_freeres(); +static bool_t clnttcp_control(); +static void clnttcp_destroy(); static struct clnt_ops tcp_ops = { clnttcp_call, @@ -81,15 +81,15 @@ static struct clnt_ops tcp_ops = { }; struct ct_data { - int ct_sock; - bool_t ct_closeit; - struct timeval ct_wait; - bool_t ct_waitset; /* wait set by clnt_control? */ - struct sockaddr_in ct_addr; - struct rpc_err ct_error; - char ct_mcall[MCALL_MSG_SIZE]; /* marshalled callmsg */ - u_int ct_mpos; /* pos after marshal */ - XDR ct_xdrs; + int ct_sock; + bool_t ct_closeit; + struct timeval ct_wait; + bool_t ct_waitset; /* wait set by clnt_control? */ + struct sockaddr_in ct_addr; + struct rpc_err ct_error; + char ct_mcall[MCALL_MSG_SIZE]; /* marshalled callmsg */ + u_int ct_mpos; /* pos after marshal */ + XDR ct_xdrs; }; /* @@ -106,30 +106,29 @@ struct ct_data { * NB: The rpch->cl_auth is set null authentication. Caller may wish to set this * something more useful. */ -CLIENT * -clnttcp_create(raddr, prog, vers, sockp, sendsz, recvsz) - struct sockaddr_in *raddr; - u_long prog; - u_long vers; - register int *sockp; - u_int sendsz; - u_int recvsz; +CLIENT *clnttcp_create(raddr, prog, vers, sockp, sendsz, recvsz) +struct sockaddr_in *raddr; +u_long prog; +u_long vers; +register int *sockp; +u_int sendsz; +u_int recvsz; { CLIENT *h; register struct ct_data *ct; struct timeval now; struct rpc_msg call_msg; - h = (CLIENT *)mem_alloc(sizeof(*h)); + h = (CLIENT *) mem_alloc(sizeof(*h)); if (h == NULL) { - (void)fprintf(stderr, "clnttcp_create: out of memory\n"); + (void) fprintf(stderr, "clnttcp_create: out of memory\n"); rpc_createerr.cf_stat = RPC_SYSTEMERROR; rpc_createerr.cf_error.re_errno = errno; goto fooy; } - ct = (struct ct_data *)mem_alloc(sizeof(*ct)); + ct = (struct ct_data *) mem_alloc(sizeof(*ct)); if (ct == NULL) { - (void)fprintf(stderr, "clnttcp_create: out of memory\n"); + (void) fprintf(stderr, "clnttcp_create: out of memory\n"); rpc_createerr.cf_stat = RPC_SYSTEMERROR; rpc_createerr.cf_error.re_errno = errno; goto fooy; @@ -140,10 +139,12 @@ clnttcp_create(raddr, prog, vers, sockp, sendsz, recvsz) */ if (raddr->sin_port == 0) { u_short port; + if ((port = pmap_getport(raddr, prog, vers, IPPROTO_TCP)) == 0) { - mem_free((caddr_t)ct, sizeof(struct ct_data)); - mem_free((caddr_t)h, sizeof(CLIENT)); - return ((CLIENT *)NULL); + mem_free((caddr_t) ct, sizeof(struct ct_data)); + + mem_free((caddr_t) h, sizeof(CLIENT)); + return ((CLIENT *) NULL); } raddr->sin_port = htons(port); } @@ -153,13 +154,13 @@ clnttcp_create(raddr, prog, vers, sockp, sendsz, recvsz) */ if (*sockp < 0) { *sockp = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); - (void)bindresvport(*sockp, (struct sockaddr_in *)0); + (void) bindresvport(*sockp, (struct sockaddr_in *) 0); if ((*sockp < 0) - || (connect(*sockp, (struct sockaddr *)raddr, - sizeof(*raddr)) < 0)) { + || (connect(*sockp, (struct sockaddr *) raddr, + sizeof(*raddr)) < 0)) { rpc_createerr.cf_stat = RPC_SYSTEMERROR; rpc_createerr.cf_error.re_errno = errno; - (void)close(*sockp); + (void) close(*sockp); goto fooy; } ct->ct_closeit = TRUE; @@ -178,7 +179,7 @@ clnttcp_create(raddr, prog, vers, sockp, sendsz, recvsz) /* * Initialize call message */ - (void)gettimeofday(&now, (struct timezone *)0); + (void) gettimeofday(&now, (struct timezone *) 0); call_msg.rm_xid = getpid() ^ now.tv_sec ^ now.tv_usec; call_msg.rm_direction = CALL; call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION; @@ -189,10 +190,10 @@ clnttcp_create(raddr, prog, vers, sockp, sendsz, recvsz) * pre-serialize the staic part of the call msg and stash it away */ xdrmem_create(&(ct->ct_xdrs), ct->ct_mcall, MCALL_MSG_SIZE, - XDR_ENCODE); - if (! xdr_callhdr(&(ct->ct_xdrs), &call_msg)) { + XDR_ENCODE); + if (!xdr_callhdr(&(ct->ct_xdrs), &call_msg)) { if (ct->ct_closeit) { - (void)close(*sockp); + (void) close(*sockp); } goto fooy; } @@ -204,36 +205,38 @@ clnttcp_create(raddr, prog, vers, sockp, sendsz, recvsz) * and authnone for authentication. */ xdrrec_create(&(ct->ct_xdrs), sendsz, recvsz, - (caddr_t)ct, readtcp, writetcp); + (caddr_t) ct, readtcp, writetcp); h->cl_ops = &tcp_ops; h->cl_private = (caddr_t) ct; h->cl_auth = authnone_create(); return (h); -fooy: + fooy: /* * Something goofed, free stuff and barf */ - mem_free((caddr_t)ct, sizeof(struct ct_data)); - mem_free((caddr_t)h, sizeof(CLIENT)); - return ((CLIENT *)NULL); + mem_free((caddr_t) ct, sizeof(struct ct_data)); + + mem_free((caddr_t) h, sizeof(CLIENT)); + return ((CLIENT *) NULL); } static enum clnt_stat -clnttcp_call(h, proc, xdr_args, args_ptr, xdr_results, results_ptr, timeout) - register CLIENT *h; - u_long proc; - xdrproc_t xdr_args; - caddr_t args_ptr; - xdrproc_t xdr_results; - caddr_t results_ptr; - struct timeval timeout; +clnttcp_call(h, proc, xdr_args, args_ptr, xdr_results, results_ptr, + timeout) +register CLIENT *h; +u_long proc; +xdrproc_t xdr_args; +caddr_t args_ptr; +xdrproc_t xdr_results; +caddr_t results_ptr; +struct timeval timeout; { register struct ct_data *ct = (struct ct_data *) h->cl_private; register XDR *xdrs = &(ct->ct_xdrs); struct rpc_msg reply_msg; u_long x_id; - u_long *msg_x_id = (u_long *)(ct->ct_mcall); /* yuk */ + u_long *msg_x_id = (u_long *) (ct->ct_mcall); /* yuk */ register bool_t shipnow; int refreshes = 2; @@ -242,31 +245,31 @@ clnttcp_call(h, proc, xdr_args, args_ptr, xdr_results, results_ptr, timeout) } shipnow = - (xdr_results == (xdrproc_t)0 && timeout.tv_sec == 0 - && timeout.tv_usec == 0) ? FALSE : TRUE; + (xdr_results == (xdrproc_t) 0 && timeout.tv_sec == 0 + && timeout.tv_usec == 0) ? FALSE : TRUE; -call_again: + call_again: xdrs->x_op = XDR_ENCODE; ct->ct_error.re_status = RPC_SUCCESS; x_id = ntohl(--(*msg_x_id)); - if ((! XDR_PUTBYTES(xdrs, ct->ct_mcall, ct->ct_mpos)) || - (! XDR_PUTLONG(xdrs, (long *)&proc)) || - (! AUTH_MARSHALL(h->cl_auth, xdrs)) || - (! (*xdr_args)(xdrs, args_ptr))) { + if ((!XDR_PUTBYTES(xdrs, ct->ct_mcall, ct->ct_mpos)) || + (!XDR_PUTLONG(xdrs, (long *) &proc)) || + (!AUTH_MARSHALL(h->cl_auth, xdrs)) || + (!(*xdr_args) (xdrs, args_ptr))) { if (ct->ct_error.re_status == RPC_SUCCESS) ct->ct_error.re_status = RPC_CANTENCODEARGS; - (void)xdrrec_endofrecord(xdrs, TRUE); + (void) xdrrec_endofrecord(xdrs, TRUE); return (ct->ct_error.re_status); } - if (! xdrrec_endofrecord(xdrs, shipnow)) + if (!xdrrec_endofrecord(xdrs, shipnow)) return (ct->ct_error.re_status = RPC_CANTSEND); - if (! shipnow) + if (!shipnow) return (RPC_SUCCESS); /* * Hack to provide rpc-based message passing */ if (timeout.tv_sec == 0 && timeout.tv_usec == 0) { - return(ct->ct_error.re_status = RPC_TIMEDOUT); + return (ct->ct_error.re_status = RPC_TIMEDOUT); } @@ -278,10 +281,10 @@ call_again: reply_msg.acpted_rply.ar_verf = _null_auth; reply_msg.acpted_rply.ar_results.where = NULL; reply_msg.acpted_rply.ar_results.proc = xdr_void; - if (! xdrrec_skiprecord(xdrs)) + if (!xdrrec_skiprecord(xdrs)) return (ct->ct_error.re_status); /* now decode and validate the response header */ - if (! xdr_replymsg(xdrs, &reply_msg)) { + if (!xdr_replymsg(xdrs, &reply_msg)) { if (ct->ct_error.re_status == RPC_SUCCESS) continue; return (ct->ct_error.re_status); @@ -295,74 +298,69 @@ call_again: */ _seterr_reply(&reply_msg, &(ct->ct_error)); if (ct->ct_error.re_status == RPC_SUCCESS) { - if (! AUTH_VALIDATE(h->cl_auth, &reply_msg.acpted_rply.ar_verf)) { + if (!AUTH_VALIDATE(h->cl_auth, &reply_msg.acpted_rply.ar_verf)) { ct->ct_error.re_status = RPC_AUTHERROR; ct->ct_error.re_why = AUTH_INVALIDRESP; - } else if (! (*xdr_results)(xdrs, results_ptr)) { + } else if (!(*xdr_results) (xdrs, results_ptr)) { if (ct->ct_error.re_status == RPC_SUCCESS) ct->ct_error.re_status = RPC_CANTDECODERES; } /* free verifier ... */ if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) { xdrs->x_op = XDR_FREE; - (void)xdr_opaque_auth(xdrs, &(reply_msg.acpted_rply.ar_verf)); + (void) xdr_opaque_auth(xdrs, &(reply_msg.acpted_rply.ar_verf)); } - } /* end successful completion */ + } /* end successful completion */ else { /* maybe our credentials need to be refreshed ... */ if (refreshes-- && AUTH_REFRESH(h->cl_auth)) goto call_again; - } /* end of unsuccessful completion */ + } /* end of unsuccessful completion */ return (ct->ct_error.re_status); } -static void -clnttcp_geterr(h, errp) - CLIENT *h; - struct rpc_err *errp; +static void clnttcp_geterr(h, errp) +CLIENT *h; +struct rpc_err *errp; { - register struct ct_data *ct = - (struct ct_data *) h->cl_private; + register struct ct_data *ct = (struct ct_data *) h->cl_private; *errp = ct->ct_error; } -static bool_t -clnttcp_freeres(cl, xdr_res, res_ptr) - CLIENT *cl; - xdrproc_t xdr_res; - caddr_t res_ptr; +static bool_t clnttcp_freeres(cl, xdr_res, res_ptr) +CLIENT *cl; +xdrproc_t xdr_res; +caddr_t res_ptr; { - register struct ct_data *ct = (struct ct_data *)cl->cl_private; + register struct ct_data *ct = (struct ct_data *) cl->cl_private; register XDR *xdrs = &(ct->ct_xdrs); xdrs->x_op = XDR_FREE; - return ((*xdr_res)(xdrs, res_ptr)); + return ((*xdr_res) (xdrs, res_ptr)); } -static void -clnttcp_abort() +static void clnttcp_abort() { } -static bool_t -clnttcp_control(cl, request, info) - CLIENT *cl; - int request; - char *info; +static bool_t clnttcp_control(cl, request, info) +CLIENT *cl; +int request; +char *info; { - register struct ct_data *ct = (struct ct_data *)cl->cl_private; + register struct ct_data *ct = (struct ct_data *) cl->cl_private; switch (request) { case CLSET_TIMEOUT: - ct->ct_wait = *(struct timeval *)info; + ct->ct_wait = *(struct timeval *) info; ct->ct_waitset = TRUE; break; case CLGET_TIMEOUT: - *(struct timeval *)info = ct->ct_wait; + *(struct timeval *) info = ct->ct_wait; break; case CLGET_SERVER_ADDR: - *(struct sockaddr_in *)info = ct->ct_addr; + *(struct sockaddr_in *) info = ct->ct_addr; break; default: return (FALSE); @@ -371,19 +369,18 @@ clnttcp_control(cl, request, info) } -static void -clnttcp_destroy(h) - CLIENT *h; +static void clnttcp_destroy(h) +CLIENT *h; { - register struct ct_data *ct = - (struct ct_data *) h->cl_private; + register struct ct_data *ct = (struct ct_data *) h->cl_private; if (ct->ct_closeit) { - (void)close(ct->ct_sock); + (void) close(ct->ct_sock); } XDR_DESTROY(&(ct->ct_xdrs)); - mem_free((caddr_t)ct, sizeof(struct ct_data)); - mem_free((caddr_t)h, sizeof(CLIENT)); + mem_free((caddr_t) ct, sizeof(struct ct_data)); + + mem_free((caddr_t) h, sizeof(CLIENT)); } /* @@ -391,11 +388,10 @@ clnttcp_destroy(h) * Behaves like the system calls, read & write, but keeps some error state * around for the rpc level. */ -static int -readtcp(ct, buf, len) - register struct ct_data *ct; - caddr_t buf; - register int len; +static int readtcp(ct, buf, len) +register struct ct_data *ct; +caddr_t buf; +register int len; { #ifdef FD_SETSIZE fd_set mask; @@ -412,11 +408,12 @@ readtcp(ct, buf, len) if (len == 0) return (0); -#endif /* def FD_SETSIZE */ +#endif /* def FD_SETSIZE */ while (TRUE) { readfds = mask; - switch (select(_rpc_dtablesize(), &readfds, (int*)NULL, (int*)NULL, - &(ct->ct_wait))) { + switch (select + (_rpc_dtablesize(), &readfds, (int *) NULL, (int *) NULL, + &(ct->ct_wait))) { case 0: ct->ct_error.re_status = RPC_TIMEDOUT; return (-1); @@ -436,7 +433,7 @@ readtcp(ct, buf, len) /* premature eof */ ct->ct_error.re_errno = ECONNRESET; ct->ct_error.re_status = RPC_CANTRECV; - len = -1; /* it's really an error */ + len = -1; /* it's really an error */ break; case -1: @@ -447,11 +444,10 @@ readtcp(ct, buf, len) return (len); } -static int -writetcp(ct, buf, len) - struct ct_data *ct; - caddr_t buf; - int len; +static int writetcp(ct, buf, len) +struct ct_data *ct; +caddr_t buf; +int len; { register int i, cnt; diff --git a/libc/inet/rpc/clnt_udp.c b/libc/inet/rpc/clnt_udp.c index 815cbb4ed..7857f86fd 100644 --- a/libc/inet/rpc/clnt_udp.c +++ b/libc/inet/rpc/clnt_udp.c @@ -50,12 +50,12 @@ extern int errno; /* * UDP bases client side rpc operations */ -static enum clnt_stat clntudp_call(); -static void clntudp_abort(); -static void clntudp_geterr(); -static bool_t clntudp_freeres(); -static bool_t clntudp_control(); -static void clntudp_destroy(); +static enum clnt_stat clntudp_call(); +static void clntudp_abort(); +static void clntudp_geterr(); +static bool_t clntudp_freeres(); +static bool_t clntudp_control(); +static void clntudp_destroy(); static struct clnt_ops udp_ops = { clntudp_call, @@ -70,19 +70,19 @@ static struct clnt_ops udp_ops = { * Private data kept per client handle */ struct cu_data { - int cu_sock; - bool_t cu_closeit; + int cu_sock; + bool_t cu_closeit; struct sockaddr_in cu_raddr; - int cu_rlen; - struct timeval cu_wait; - struct timeval cu_total; - struct rpc_err cu_error; - XDR cu_outxdrs; - u_int cu_xdrpos; - u_int cu_sendsz; - char *cu_outbuf; - u_int cu_recvsz; - char cu_inbuf[1]; + int cu_rlen; + struct timeval cu_wait; + struct timeval cu_total; + struct rpc_err cu_error; + XDR cu_outxdrs; + u_int cu_xdrpos; + u_int cu_sendsz; + char *cu_outbuf; + u_int cu_recvsz; + char cu_inbuf[1]; }; /* @@ -101,22 +101,22 @@ struct cu_data { * sendsz and recvsz are the maximum allowable packet sizes that can be * sent and received. */ -CLIENT * -clntudp_bufcreate(raddr, program, version, wait, sockp, sendsz, recvsz) - struct sockaddr_in *raddr; - u_long program; - u_long version; - struct timeval wait; - register int *sockp; - u_int sendsz; - u_int recvsz; +CLIENT *clntudp_bufcreate(raddr, program, version, wait, sockp, sendsz, + recvsz) +struct sockaddr_in *raddr; +u_long program; +u_long version; +struct timeval wait; +register int *sockp; +u_int sendsz; +u_int recvsz; { CLIENT *cl; register struct cu_data *cu; struct timeval now; struct rpc_msg call_msg; - cl = (CLIENT *)mem_alloc(sizeof(CLIENT)); + cl = (CLIENT *) mem_alloc(sizeof(CLIENT)); if (cl == NULL) { (void) fprintf(stderr, "clntudp_create: out of memory\n"); rpc_createerr.cf_stat = RPC_SYSTEMERROR; @@ -125,7 +125,7 @@ clntudp_bufcreate(raddr, program, version, wait, sockp, sendsz, recvsz) } sendsz = ((sendsz + 3) / 4) * 4; recvsz = ((recvsz + 3) / 4) * 4; - cu = (struct cu_data *)mem_alloc(sizeof(*cu) + sendsz + recvsz); + cu = (struct cu_data *) mem_alloc(sizeof(*cu) + sendsz + recvsz); if (cu == NULL) { (void) fprintf(stderr, "clntudp_create: out of memory\n"); rpc_createerr.cf_stat = RPC_SYSTEMERROR; @@ -134,19 +134,20 @@ clntudp_bufcreate(raddr, program, version, wait, sockp, sendsz, recvsz) } cu->cu_outbuf = &cu->cu_inbuf[recvsz]; - (void)gettimeofday(&now, (struct timezone *)0); + (void) gettimeofday(&now, (struct timezone *) 0); if (raddr->sin_port == 0) { u_short port; + if ((port = - pmap_getport(raddr, program, version, IPPROTO_UDP)) == 0) { + pmap_getport(raddr, program, version, IPPROTO_UDP)) == 0) { goto fooy; } raddr->sin_port = htons(port); } cl->cl_ops = &udp_ops; - cl->cl_private = (caddr_t)cu; + cl->cl_private = (caddr_t) cu; cu->cu_raddr = *raddr; - cu->cu_rlen = sizeof (cu->cu_raddr); + cu->cu_rlen = sizeof(cu->cu_raddr); cu->cu_wait = wait; cu->cu_total.tv_sec = -1; cu->cu_total.tv_usec = -1; @@ -157,9 +158,8 @@ clntudp_bufcreate(raddr, program, version, wait, sockp, sendsz, recvsz) call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION; call_msg.rm_call.cb_prog = program; call_msg.rm_call.cb_vers = version; - xdrmem_create(&(cu->cu_outxdrs), cu->cu_outbuf, - sendsz, XDR_ENCODE); - if (! xdr_callhdr(&(cu->cu_outxdrs), &call_msg)) { + xdrmem_create(&(cu->cu_outxdrs), cu->cu_outbuf, sendsz, XDR_ENCODE); + if (!xdr_callhdr(&(cu->cu_outxdrs), &call_msg)) { goto fooy; } cu->cu_xdrpos = XDR_GETPOS(&(cu->cu_outxdrs)); @@ -173,9 +173,9 @@ clntudp_bufcreate(raddr, program, version, wait, sockp, sendsz, recvsz) goto fooy; } /* attempt to bind to prov port */ - (void)bindresvport(*sockp, (struct sockaddr_in *)0); + (void) bindresvport(*sockp, (struct sockaddr_in *) 0); /* the sockets rpc controls are non-blocking */ - (void)ioctl(*sockp, FIONBIO, (char *) &dontblock); + (void) ioctl(*sockp, FIONBIO, (char *) &dontblock); cu->cu_closeit = TRUE; } else { cu->cu_closeit = FALSE; @@ -183,83 +183,82 @@ clntudp_bufcreate(raddr, program, version, wait, sockp, sendsz, recvsz) cu->cu_sock = *sockp; cl->cl_auth = authnone_create(); return (cl); -fooy: + fooy: if (cu) - mem_free((caddr_t)cu, sizeof(*cu) + sendsz + recvsz); + mem_free((caddr_t) cu, sizeof(*cu) + sendsz + recvsz); if (cl) - mem_free((caddr_t)cl, sizeof(CLIENT)); - return ((CLIENT *)NULL); + mem_free((caddr_t) cl, sizeof(CLIENT)); + return ((CLIENT *) NULL); } -CLIENT * -clntudp_create(raddr, program, version, wait, sockp) - struct sockaddr_in *raddr; - u_long program; - u_long version; - struct timeval wait; - register int *sockp; +CLIENT *clntudp_create(raddr, program, version, wait, sockp) +struct sockaddr_in *raddr; +u_long program; +u_long version; +struct timeval wait; +register int *sockp; { - return(clntudp_bufcreate(raddr, program, version, wait, sockp, - UDPMSGSIZE, UDPMSGSIZE)); + return (clntudp_bufcreate(raddr, program, version, wait, sockp, + UDPMSGSIZE, UDPMSGSIZE)); } -static enum clnt_stat +static enum clnt_stat clntudp_call(cl, proc, xargs, argsp, xresults, resultsp, utimeout) - register CLIENT *cl; /* client handle */ - u_long proc; /* procedure number */ - xdrproc_t xargs; /* xdr routine for args */ - caddr_t argsp; /* pointer to args */ - xdrproc_t xresults; /* xdr routine for results */ - caddr_t resultsp; /* pointer to results */ - struct timeval utimeout; /* seconds to wait before giving up */ +register CLIENT *cl; /* client handle */ +u_long proc; /* procedure number */ +xdrproc_t xargs; /* xdr routine for args */ +caddr_t argsp; /* pointer to args */ +xdrproc_t xresults; /* xdr routine for results */ +caddr_t resultsp; /* pointer to results */ +struct timeval utimeout; /* seconds to wait before giving up */ { - register struct cu_data *cu = (struct cu_data *)cl->cl_private; + register struct cu_data *cu = (struct cu_data *) cl->cl_private; register XDR *xdrs; register int outlen; register int inlen; int fromlen; + #ifdef FD_SETSIZE fd_set readfds; fd_set mask; #else int readfds; register int mask; -#endif /* def FD_SETSIZE */ +#endif /* def FD_SETSIZE */ struct sockaddr_in from; struct rpc_msg reply_msg; XDR reply_xdrs; struct timeval time_waited; bool_t ok; - int nrefreshes = 2; /* number of times to refresh cred */ + int nrefreshes = 2; /* number of times to refresh cred */ struct timeval timeout; if (cu->cu_total.tv_usec == -1) { - timeout = utimeout; /* use supplied timeout */ + timeout = utimeout; /* use supplied timeout */ } else { - timeout = cu->cu_total; /* use default timeout */ + timeout = cu->cu_total; /* use default timeout */ } time_waited.tv_sec = 0; time_waited.tv_usec = 0; -call_again: + call_again: xdrs = &(cu->cu_outxdrs); xdrs->x_op = XDR_ENCODE; XDR_SETPOS(xdrs, cu->cu_xdrpos); /* * the transaction is the first thing in the out buffer */ - (*(u_short *)(cu->cu_outbuf))++; - if ((! XDR_PUTLONG(xdrs, (long *)&proc)) || - (! AUTH_MARSHALL(cl->cl_auth, xdrs)) || - (! (*xargs)(xdrs, argsp))) + (*(u_short *) (cu->cu_outbuf))++; + if ((!XDR_PUTLONG(xdrs, (long *) &proc)) || + (!AUTH_MARSHALL(cl->cl_auth, xdrs)) || (!(*xargs) (xdrs, argsp))) return (cu->cu_error.re_status = RPC_CANTENCODEARGS); - outlen = (int)XDR_GETPOS(xdrs); + outlen = (int) XDR_GETPOS(xdrs); -send_again: + send_again: if (sendto(cu->cu_sock, cu->cu_outbuf, outlen, 0, - (struct sockaddr *)&(cu->cu_raddr), cu->cu_rlen) - != outlen) { + (struct sockaddr *) &(cu->cu_raddr), cu->cu_rlen) + != outlen) { cu->cu_error.re_errno = errno; return (cu->cu_error.re_status = RPC_CANTSEND); } @@ -283,11 +282,11 @@ send_again: FD_SET(cu->cu_sock, &mask); #else mask = 1 << cu->cu_sock; -#endif /* def FD_SETSIZE */ +#endif /* def FD_SETSIZE */ for (;;) { readfds = mask; - switch (select(_rpc_dtablesize(), &readfds, (int *)NULL, - (int *)NULL, &(cu->cu_wait))) { + switch (select(_rpc_dtablesize(), &readfds, (int *) NULL, + (int *) NULL, &(cu->cu_wait))) { case 0: time_waited.tv_sec += cu->cu_wait.tv_sec; @@ -298,37 +297,38 @@ send_again: } if ((time_waited.tv_sec < timeout.tv_sec) || ((time_waited.tv_sec == timeout.tv_sec) && - (time_waited.tv_usec < timeout.tv_usec))) - goto send_again; + (time_waited.tv_usec < timeout.tv_usec))) + goto send_again; return (cu->cu_error.re_status = RPC_TIMEDOUT); - /* - * buggy in other cases because time_waited is not being - * updated. - */ + /* + * buggy in other cases because time_waited is not being + * updated. + */ case -1: if (errno == EINTR) - continue; + continue; cu->cu_error.re_errno = errno; return (cu->cu_error.re_status = RPC_CANTRECV); } do { fromlen = sizeof(struct sockaddr); - inlen = recvfrom(cu->cu_sock, cu->cu_inbuf, - (int) cu->cu_recvsz, 0, - (struct sockaddr *)&from, &fromlen); + + inlen = recvfrom(cu->cu_sock, cu->cu_inbuf, + (int) cu->cu_recvsz, 0, + (struct sockaddr *) &from, &fromlen); } while (inlen < 0 && errno == EINTR); if (inlen < 0) { if (errno == EWOULDBLOCK) - continue; + continue; cu->cu_error.re_errno = errno; return (cu->cu_error.re_status = RPC_CANTRECV); } if (inlen < sizeof(u_long)) - continue; + continue; /* see if reply transaction id matches sent id */ - if (*((u_long *)(cu->cu_inbuf)) != *((u_long *)(cu->cu_outbuf))) - continue; + if (*((u_long *) (cu->cu_inbuf)) != *((u_long *) (cu->cu_outbuf))) + continue; /* we now assume we have the proper reply */ break; } @@ -336,107 +336,102 @@ send_again: /* * now decode and validate the response */ - xdrmem_create(&reply_xdrs, cu->cu_inbuf, (u_int)inlen, XDR_DECODE); + xdrmem_create(&reply_xdrs, cu->cu_inbuf, (u_int) inlen, XDR_DECODE); ok = xdr_replymsg(&reply_xdrs, &reply_msg); /* XDR_DESTROY(&reply_xdrs); save a few cycles on noop destroy */ if (ok) { _seterr_reply(&reply_msg, &(cu->cu_error)); if (cu->cu_error.re_status == RPC_SUCCESS) { - if (! AUTH_VALIDATE(cl->cl_auth, - &reply_msg.acpted_rply.ar_verf)) { + if (!AUTH_VALIDATE(cl->cl_auth, + &reply_msg.acpted_rply.ar_verf)) { cu->cu_error.re_status = RPC_AUTHERROR; cu->cu_error.re_why = AUTH_INVALIDRESP; } if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) { xdrs->x_op = XDR_FREE; - (void)xdr_opaque_auth(xdrs, - &(reply_msg.acpted_rply.ar_verf)); - } - } /* end successful completion */ + (void) xdr_opaque_auth(xdrs, + &(reply_msg.acpted_rply.ar_verf)); + } + } /* end successful completion */ else { /* maybe our credentials need to be refreshed ... */ if (nrefreshes > 0 && AUTH_REFRESH(cl->cl_auth)) { nrefreshes--; goto call_again; } - } /* end of unsuccessful completion */ - } /* end of valid reply message */ + } /* end of unsuccessful completion */ + } /* end of valid reply message */ else { cu->cu_error.re_status = RPC_CANTDECODERES; } return (cu->cu_error.re_status); } -static void -clntudp_geterr(cl, errp) - CLIENT *cl; - struct rpc_err *errp; +static void clntudp_geterr(cl, errp) +CLIENT *cl; +struct rpc_err *errp; { - register struct cu_data *cu = (struct cu_data *)cl->cl_private; + register struct cu_data *cu = (struct cu_data *) cl->cl_private; *errp = cu->cu_error; } -static bool_t -clntudp_freeres(cl, xdr_res, res_ptr) - CLIENT *cl; - xdrproc_t xdr_res; - caddr_t res_ptr; +static bool_t clntudp_freeres(cl, xdr_res, res_ptr) +CLIENT *cl; +xdrproc_t xdr_res; +caddr_t res_ptr; { - register struct cu_data *cu = (struct cu_data *)cl->cl_private; + register struct cu_data *cu = (struct cu_data *) cl->cl_private; register XDR *xdrs = &(cu->cu_outxdrs); xdrs->x_op = XDR_FREE; - return ((*xdr_res)(xdrs, res_ptr)); + return ((*xdr_res) (xdrs, res_ptr)); } -static void -clntudp_abort(/*h*/) - /*CLIENT *h;*/ +static void clntudp_abort( /*h */ ) + /*CLIENT *h; */ { } -static bool_t -clntudp_control(cl, request, info) - CLIENT *cl; - int request; - char *info; +static bool_t clntudp_control(cl, request, info) +CLIENT *cl; +int request; +char *info; { - register struct cu_data *cu = (struct cu_data *)cl->cl_private; + register struct cu_data *cu = (struct cu_data *) cl->cl_private; switch (request) { case CLSET_TIMEOUT: - cu->cu_total = *(struct timeval *)info; + cu->cu_total = *(struct timeval *) info; break; case CLGET_TIMEOUT: - *(struct timeval *)info = cu->cu_total; + *(struct timeval *) info = cu->cu_total; break; case CLSET_RETRY_TIMEOUT: - cu->cu_wait = *(struct timeval *)info; + cu->cu_wait = *(struct timeval *) info; break; case CLGET_RETRY_TIMEOUT: - *(struct timeval *)info = cu->cu_wait; + *(struct timeval *) info = cu->cu_wait; break; case CLGET_SERVER_ADDR: - *(struct sockaddr_in *)info = cu->cu_raddr; + *(struct sockaddr_in *) info = cu->cu_raddr; break; default: return (FALSE); } return (TRUE); } - -static void -clntudp_destroy(cl) - CLIENT *cl; + +static void clntudp_destroy(cl) +CLIENT *cl; { - register struct cu_data *cu = (struct cu_data *)cl->cl_private; + register struct cu_data *cu = (struct cu_data *) cl->cl_private; if (cu->cu_closeit) { - (void)close(cu->cu_sock); + (void) close(cu->cu_sock); } XDR_DESTROY(&(cu->cu_outxdrs)); - mem_free((caddr_t)cu, (sizeof(*cu) + cu->cu_sendsz + cu->cu_recvsz)); - mem_free((caddr_t)cl, sizeof(CLIENT)); + mem_free((caddr_t) cu, (sizeof(*cu) + cu->cu_sendsz + cu->cu_recvsz)); + mem_free((caddr_t) cl, sizeof(CLIENT)); } diff --git a/libc/inet/rpc/get_myaddress.c b/libc/inet/rpc/get_myaddress.c index cbfc05b8d..95ddc71b4 100644 --- a/libc/inet/rpc/get_myaddress.c +++ b/libc/inet/rpc/get_myaddress.c @@ -28,7 +28,9 @@ * Mountain View, California 94043 */ #if !defined(lint) && defined(SCCSIDS) -static char sccsid[] = "@(#)get_myaddress.c 1.4 87/08/11 Copyr 1984 Sun Micro"; +static char sccsid[] = + + "@(#)get_myaddress.c 1.4 87/08/11 Copyr 1984 Sun Micro"; #endif /* @@ -51,7 +53,7 @@ static char sccsid[] = "@(#)get_myaddress.c 1.4 87/08/11 Copyr 1984 Sun Micro"; /* DO use gethostbyname because it's portable */ #include get_myaddress(addr) - struct sockaddr_in *addr; +struct sockaddr_in *addr; { char localhost[256 + 1]; struct hostent *hp; @@ -70,7 +72,7 @@ get_myaddress(addr) * don't use gethostbyname, which would invoke yellow pages */ get_myaddress(addr) - struct sockaddr_in *addr; +struct sockaddr_in *addr; { int s; char buf[BUFSIZ]; @@ -79,25 +81,25 @@ get_myaddress(addr) int len; if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { - perror("get_myaddress: socket"); - exit(1); + perror("get_myaddress: socket"); + exit(1); } - ifc.ifc_len = sizeof (buf); + ifc.ifc_len = sizeof(buf); ifc.ifc_buf = buf; - if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) { + if (ioctl(s, SIOCGIFCONF, (char *) &ifc) < 0) { perror("get_myaddress: ioctl (get interface configuration)"); exit(1); } ifr = ifc.ifc_req; for (len = ifc.ifc_len; len; len -= sizeof ifreq) { ifreq = *ifr; - if (ioctl(s, SIOCGIFFLAGS, (char *)&ifreq) < 0) { + if (ioctl(s, SIOCGIFFLAGS, (char *) &ifreq) < 0) { perror("get_myaddress: ioctl"); exit(1); } if ((ifreq.ifr_flags & IFF_UP) && - ifr->ifr_addr.sa_family == AF_INET) { - *addr = *((struct sockaddr_in *)&ifr->ifr_addr); + ifr->ifr_addr.sa_family == AF_INET) { + *addr = *((struct sockaddr_in *) &ifr->ifr_addr); addr->sin_port = htons(PMAPPORT); break; } diff --git a/libc/inet/rpc/getrpcent.c b/libc/inet/rpc/getrpcent.c index b3b3ece4c..06c272c89 100644 --- a/libc/inet/rpc/getrpcent.c +++ b/libc/inet/rpc/getrpcent.c @@ -1,6 +1,8 @@ /* @(#)getrpcent.c 2.2 88/07/29 4.0 RPCSRC */ #if !defined(lint) && defined(SCCSIDS) -static char sccsid[] = "@(#)getrpcent.c 1.9 87/08/11 Copyr 1984 Sun Micro"; +static char sccsid[] = + + "@(#)getrpcent.c 1.9 87/08/11 Copyr 1984 Sun Micro"; #endif /* @@ -46,43 +48,43 @@ static char sccsid[] = "@(#)getrpcent.c 1.9 87/08/11 Copyr 1984 Sun Micro"; * Internet version. */ struct rpcdata { - FILE *rpcf; - char *current; - int currentlen; - int stayopen; + FILE *rpcf; + char *current; + int currentlen; + int stayopen; #define MAXALIASES 35 - char *rpc_aliases[MAXALIASES]; - struct rpcent rpc; - char line[BUFSIZ+1]; - char *domain; + char *rpc_aliases[MAXALIASES]; + struct rpcent rpc; + char line[BUFSIZ + 1]; + char *domain; } *rpcdata, *_rpcdata(); -static struct rpcent *interpret(); -struct hostent *gethostent(); -char *inet_ntoa(); +static struct rpcent *interpret(); +struct hostent *gethostent(); +char *inet_ntoa(); + #ifndef __linux__ -static char *index(); +static char *index(); #else char *index(); #endif static char RPCDB[] = "/etc/rpc"; -static struct rpcdata * -_rpcdata() +static struct rpcdata *_rpcdata() { register struct rpcdata *d = rpcdata; if (d == 0) { - d = (struct rpcdata *)calloc(1, sizeof (struct rpcdata)); + d = (struct rpcdata *) calloc(1, sizeof(struct rpcdata)); + rpcdata = d; } return (d); } -struct rpcent * -getrpcbynumber(number) - register int number; +struct rpcent *getrpcbynumber(number) +register int number; { register struct rpcdata *d = _rpcdata(); register struct rpcent *p; @@ -106,14 +108,14 @@ struct rpcent * getrpcbyname(const char *name) #else getrpcbyname(name) - char *name; +char *name; #endif { struct rpcent *rpc; char **rp; setrpcent(0); - while(rpc = getrpcent()) { + while (rpc = getrpcent()) { if (strcmp(rpc->r_name, name) == 0) return (rpc); for (rp = rpc->r_aliases; *rp != NULL; rp++) { @@ -129,7 +131,7 @@ getrpcbyname(name) void #endif setrpcent(f) - int f; +int f; { register struct rpcdata *d = _rpcdata(); @@ -164,8 +166,7 @@ endrpcent() } } -struct rpcent * -getrpcent() +struct rpcent *getrpcent() { struct rpcent *hp; int reason; @@ -174,17 +175,16 @@ getrpcent() register struct rpcdata *d = _rpcdata(); if (d == 0) - return(NULL); + return (NULL); if (d->rpcf == NULL && (d->rpcf = fopen(RPCDB, "r")) == NULL) return (NULL); - if (fgets(d->line, BUFSIZ, d->rpcf) == NULL) + if (fgets(d->line, BUFSIZ, d->rpcf) == NULL) return (NULL); return interpret(d->line, strlen(d->line)); } #ifdef __linux__ -static char * -firstwhite(s) +static char *firstwhite(s) char *s; { char *s1, *s2; @@ -196,14 +196,12 @@ char *s; return (s1 < s2) ? s1 : s2; else return s1; - } - else + } else return s2; } #endif -static struct rpcent * -interpret(val, len) +static struct rpcent *interpret(val, len) { register struct rpcdata *d = _rpcdata(); char *p; @@ -217,8 +215,7 @@ interpret(val, len) if (*p == '#') return (getrpcent()); cp = index(p, '#'); - if (cp == NULL) - { + if (cp == NULL) { cp = index(p, '\n'); if (cp == NULL) return (getrpcent()); @@ -231,8 +228,7 @@ interpret(val, len) return (getrpcent()); #else cp = index(p, ' '); - if (cp == NULL) - { + if (cp == NULL) { cp = index(p, '\t'); if (cp == NULL) return (getrpcent()); @@ -252,8 +248,7 @@ interpret(val, len) cp = index(p, ' '); if (cp != NULL) *cp++ = '\0'; - else - { + else { cp = index(p, '\t'); if (cp != NULL) *cp++ = '\0'; @@ -273,8 +268,7 @@ interpret(val, len) cp = index(p, ' '); if (cp != NULL) *cp++ = '\0'; - else - { + else { cp = index(p, '\t'); if (cp != NULL) *cp++ = '\0'; diff --git a/libc/inet/rpc/getrpcport.c b/libc/inet/rpc/getrpcport.c index 9b13bac6b..af4f7daf6 100644 --- a/libc/inet/rpc/getrpcport.c +++ b/libc/inet/rpc/getrpcport.c @@ -1,6 +1,6 @@ /* @(#)getrpcport.c 2.1 88/07/29 4.0 RPCSRC */ #if !defined(lint) && defined(SCCSIDS) -static char sccsid[] = "@(#)getrpcport.c 1.3 87/08/11 SMI"; +static char sccsid[] = "@(#)getrpcport.c 1.3 87/08/11 SMI"; #endif /* * Sun RPC is a product of Sun Microsystems, Inc. and is provided for @@ -41,7 +41,7 @@ static char sccsid[] = "@(#)getrpcport.c 1.3 87/08/11 SMI"; #include getrpcport(host, prognum, versnum, proto) - char *host; +char *host; { struct sockaddr_in addr; struct hostent *hp; @@ -50,6 +50,6 @@ getrpcport(host, prognum, versnum, proto) return (0); bcopy(hp->h_addr, (char *) &addr.sin_addr, hp->h_length); addr.sin_family = AF_INET; - addr.sin_port = 0; + addr.sin_port = 0; return (pmap_getport(&addr, prognum, versnum, proto)); } diff --git a/libc/inet/rpc/pmap_clnt.c b/libc/inet/rpc/pmap_clnt.c index 09220e77b..921575e02 100644 --- a/libc/inet/rpc/pmap_clnt.c +++ b/libc/inet/rpc/pmap_clnt.c @@ -28,7 +28,9 @@ * Mountain View, California 94043 */ #if !defined(lint) && defined(SCCSIDS) -static char sccsid[] = "@(#)pmap_clnt.c 1.37 87/08/11 Copyr 1984 Sun Micro"; +static char sccsid[] = + + "@(#)pmap_clnt.c 1.37 87/08/11 Copyr 1984 Sun Micro"; #endif /* @@ -52,12 +54,11 @@ void clnt_perror(); * Set a mapping between program,version and port. * Calls the pmap service remotely to do the mapping. */ -bool_t -pmap_set(program, version, protocol, port) - u_long program; - u_long version; - int protocol; - u_short port; +bool_t pmap_set(program, version, protocol, port) +u_long program; +u_long version; +int protocol; +u_short port; { struct sockaddr_in myaddress; int socket = -1; @@ -67,20 +68,21 @@ pmap_set(program, version, protocol, port) get_myaddress(&myaddress); client = clntudp_bufcreate(&myaddress, PMAPPROG, PMAPVERS, - timeout, &socket, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE); - if (client == (CLIENT *)NULL) + timeout, &socket, RPCSMALLMSGSIZE, + RPCSMALLMSGSIZE); + if (client == (CLIENT *) NULL) return (FALSE); parms.pm_prog = program; parms.pm_vers = version; parms.pm_prot = protocol; parms.pm_port = port; if (CLNT_CALL(client, PMAPPROC_SET, xdr_pmap, &parms, xdr_bool, &rslt, - tottimeout) != RPC_SUCCESS) { + tottimeout) != RPC_SUCCESS) { clnt_perror(client, "Cannot register service"); return (FALSE); } CLNT_DESTROY(client); - (void)close(socket); + (void) close(socket); return (rslt); } @@ -88,10 +90,9 @@ pmap_set(program, version, protocol, port) * Remove the mapping between program,version and port. * Calls the pmap service remotely to do the un-mapping. */ -bool_t -pmap_unset(program, version) - u_long program; - u_long version; +bool_t pmap_unset(program, version) +u_long program; +u_long version; { struct sockaddr_in myaddress; int socket = -1; @@ -101,15 +102,16 @@ pmap_unset(program, version) get_myaddress(&myaddress); client = clntudp_bufcreate(&myaddress, PMAPPROG, PMAPVERS, - timeout, &socket, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE); - if (client == (CLIENT *)NULL) + timeout, &socket, RPCSMALLMSGSIZE, + RPCSMALLMSGSIZE); + if (client == (CLIENT *) NULL) return (FALSE); parms.pm_prog = program; parms.pm_vers = version; parms.pm_port = parms.pm_prot = 0; CLNT_CALL(client, PMAPPROC_UNSET, xdr_pmap, &parms, xdr_bool, &rslt, - tottimeout); + tottimeout); CLNT_DESTROY(client); - (void)close(socket); + (void) close(socket); return (rslt); } diff --git a/libc/inet/rpc/pmap_getmaps.c b/libc/inet/rpc/pmap_getmaps.c index 98803d3c5..837875328 100644 --- a/libc/inet/rpc/pmap_getmaps.c +++ b/libc/inet/rpc/pmap_getmaps.c @@ -28,7 +28,9 @@ * Mountain View, California 94043 */ #if !defined(lint) && defined(SCCSIDS) -static char sccsid[] = "@(#)pmap_getmaps.c 1.10 87/08/11 Copyr 1984 Sun Micro"; +static char sccsid[] = + + "@(#)pmap_getmaps.c 1.10 87/08/11 Copyr 1984 Sun Micro"; #endif /* @@ -57,11 +59,10 @@ extern int errno; * Get a copy of the current port maps. * Calls the pmap service remotely to do get the maps. */ -struct pmaplist * -pmap_getmaps(address) - struct sockaddr_in *address; +struct pmaplist *pmap_getmaps(address) +struct sockaddr_in *address; { - struct pmaplist *head = (struct pmaplist *)NULL; + struct pmaplist *head = (struct pmaplist *) NULL; int socket = -1; struct timeval minutetimeout; register CLIENT *client; @@ -69,16 +70,15 @@ pmap_getmaps(address) minutetimeout.tv_sec = 60; minutetimeout.tv_usec = 0; address->sin_port = htons(PMAPPORT); - client = clnttcp_create(address, PMAPPROG, - PMAPVERS, &socket, 50, 500); - if (client != (CLIENT *)NULL) { + client = clnttcp_create(address, PMAPPROG, PMAPVERS, &socket, 50, 500); + if (client != (CLIENT *) NULL) { if (CLNT_CALL(client, PMAPPROC_DUMP, xdr_void, NULL, xdr_pmaplist, - &head, minutetimeout) != RPC_SUCCESS) { + &head, minutetimeout) != RPC_SUCCESS) { clnt_perror(client, "pmap_getmaps rpc problem"); } CLNT_DESTROY(client); } - (void)close(socket); + (void) close(socket); address->sin_port = 0; return (head); } diff --git a/libc/inet/rpc/pmap_getport.c b/libc/inet/rpc/pmap_getport.c index 5eb2eee3b..122105d8c 100644 --- a/libc/inet/rpc/pmap_getport.c +++ b/libc/inet/rpc/pmap_getport.c @@ -28,7 +28,9 @@ * Mountain View, California 94043 */ #if !defined(lint) && defined(SCCSIDS) -static char sccsid[] = "@(#)pmap_getport.c 1.9 87/08/11 Copyr 1984 Sun Micro"; +static char sccsid[] = + + "@(#)pmap_getport.c 1.9 87/08/11 Copyr 1984 Sun Micro"; #endif /* @@ -52,12 +54,11 @@ static struct timeval tottimeout = { 60, 0 }; * Calls the pmap service remotely to do the lookup. * Returns 0 if no map exists. */ -u_short -pmap_getport(address, program, version, protocol) - struct sockaddr_in *address; - u_long program; - u_long version; - u_int protocol; +u_short pmap_getport(address, program, version, protocol) +struct sockaddr_in *address; +u_long program; +u_long version; +u_int protocol; { u_short port = 0; int socket = -1; @@ -66,14 +67,15 @@ pmap_getport(address, program, version, protocol) address->sin_port = htons(PMAPPORT); client = clntudp_bufcreate(address, PMAPPROG, - PMAPVERS, timeout, &socket, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE); - if (client != (CLIENT *)NULL) { + PMAPVERS, timeout, &socket, RPCSMALLMSGSIZE, + RPCSMALLMSGSIZE); + if (client != (CLIENT *) NULL) { parms.pm_prog = program; parms.pm_vers = version; parms.pm_prot = protocol; - parms.pm_port = 0; /* not needed or used */ + parms.pm_port = 0; /* not needed or used */ if (CLNT_CALL(client, PMAPPROC_GETPORT, xdr_pmap, &parms, - xdr_u_short, &port, tottimeout) != RPC_SUCCESS){ + xdr_u_short, &port, tottimeout) != RPC_SUCCESS) { rpc_createerr.cf_stat = RPC_PMAPFAILURE; clnt_geterr(client, &rpc_createerr.cf_error); } else if (port == 0) { @@ -81,7 +83,7 @@ pmap_getport(address, program, version, protocol) } CLNT_DESTROY(client); } - (void)close(socket); + (void) close(socket); address->sin_port = 0; return (port); } diff --git a/libc/inet/rpc/pmap_prot.c b/libc/inet/rpc/pmap_prot.c index 643c2ff6a..ec8b5af79 100644 --- a/libc/inet/rpc/pmap_prot.c +++ b/libc/inet/rpc/pmap_prot.c @@ -28,7 +28,9 @@ * Mountain View, California 94043 */ #if !defined(lint) && defined(SCCSIDS) -static char sccsid[] = "@(#)pmap_prot.c 1.17 87/08/11 Copyr 1984 Sun Micro"; +static char sccsid[] = + + "@(#)pmap_prot.c 1.17 87/08/11 Copyr 1984 Sun Micro"; #endif /* @@ -43,15 +45,14 @@ static char sccsid[] = "@(#)pmap_prot.c 1.17 87/08/11 Copyr 1984 Sun Micro"; #include -bool_t -xdr_pmap(xdrs, regs) - XDR *xdrs; - struct pmap *regs; +bool_t xdr_pmap(xdrs, regs) +XDR *xdrs; +struct pmap *regs; { - if (xdr_u_long(xdrs, ®s->pm_prog) && - xdr_u_long(xdrs, ®s->pm_vers) && + if (xdr_u_long(xdrs, ®s->pm_prog) && + xdr_u_long(xdrs, ®s->pm_vers) && xdr_u_long(xdrs, ®s->pm_prot)) - return (xdr_u_long(xdrs, ®s->pm_port)); + return (xdr_u_long(xdrs, ®s->pm_port)); return (FALSE); } diff --git a/libc/inet/rpc/pmap_prot2.c b/libc/inet/rpc/pmap_prot2.c index e2a8214d4..1a6207728 100644 --- a/libc/inet/rpc/pmap_prot2.c +++ b/libc/inet/rpc/pmap_prot2.c @@ -28,7 +28,9 @@ * Mountain View, California 94043 */ #if !defined(lint) && defined(SCCSIDS) -static char sccsid[] = "@(#)pmap_prot2.c 1.3 87/08/11 Copyr 1984 Sun Micro"; +static char sccsid[] = + + "@(#)pmap_prot2.c 1.3 87/08/11 Copyr 1984 Sun Micro"; #endif /* @@ -81,10 +83,9 @@ static char sccsid[] = "@(#)pmap_prot2.c 1.3 87/08/11 Copyr 1984 Sun Micro"; * the net, yet is the data that the pointer points to which is interesting; * this sounds like a job for xdr_reference! */ -bool_t -xdr_pmaplist(xdrs, rp) - register XDR *xdrs; - register struct pmaplist **rp; +bool_t xdr_pmaplist(xdrs, rp) +register XDR *xdrs; +register struct pmaplist **rp; { /* * more_elements is pre-computed in case the direction is @@ -96,21 +97,22 @@ xdr_pmaplist(xdrs, rp) register struct pmaplist **next; while (TRUE) { - more_elements = (bool_t)(*rp != NULL); - if (! xdr_bool(xdrs, &more_elements)) + more_elements = (bool_t) (*rp != NULL); + if (!xdr_bool(xdrs, &more_elements)) return (FALSE); - if (! more_elements) - return (TRUE); /* we are done */ + if (!more_elements) + return (TRUE); /* we are done */ /* * the unfortunate side effect of non-recursion is that in * the case of freeing we must remember the next object * before we free the current object ... */ if (freeing) - next = &((*rp)->pml_next); - if (! xdr_reference(xdrs, (caddr_t *)rp, - (u_int)sizeof(struct pmaplist), xdr_pmap)) + next = &((*rp)->pml_next); + if (!xdr_reference(xdrs, (caddr_t *) rp, + (u_int) sizeof(struct pmaplist), xdr_pmap)) return (FALSE); + rp = (freeing) ? next : &((*rp)->pml_next); } } diff --git a/libc/inet/rpc/pmap_rmt.c b/libc/inet/rpc/pmap_rmt.c index 1921b74c1..1f2b47cba 100644 --- a/libc/inet/rpc/pmap_rmt.c +++ b/libc/inet/rpc/pmap_rmt.c @@ -63,13 +63,14 @@ static struct timeval timeout = { 3, 0 }; * programs to do a lookup and call in one step. */ enum clnt_stat -pmap_rmtcall(addr, prog, vers, proc, xdrargs, argsp, xdrres, resp, tout, port_ptr) - struct sockaddr_in *addr; - u_long prog, vers, proc; - xdrproc_t xdrargs, xdrres; - caddr_t argsp, resp; - struct timeval tout; - u_long *port_ptr; +pmap_rmtcall(addr, prog, vers, proc, xdrargs, argsp, xdrres, resp, tout, + port_ptr) +struct sockaddr_in *addr; +u_long prog, vers, proc; +xdrproc_t xdrargs, xdrres; +caddr_t argsp, resp; +struct timeval tout; +u_long *port_ptr; { int socket = -1; register CLIENT *client; @@ -79,7 +80,7 @@ pmap_rmtcall(addr, prog, vers, proc, xdrargs, argsp, xdrres, resp, tout, port_pt addr->sin_port = htons(PMAPPORT); client = clntudp_create(addr, PMAPPROG, PMAPVERS, timeout, &socket); - if (client != (CLIENT *)NULL) { + if (client != (CLIENT *) NULL) { a.prog = prog; a.vers = vers; a.proc = proc; @@ -89,12 +90,12 @@ pmap_rmtcall(addr, prog, vers, proc, xdrargs, argsp, xdrres, resp, tout, port_pt r.results_ptr = resp; r.xdr_results = xdrres; stat = CLNT_CALL(client, PMAPPROC_CALLIT, xdr_rmtcall_args, &a, - xdr_rmtcallres, &r, tout); + xdr_rmtcallres, &r, tout); CLNT_DESTROY(client); } else { stat = RPC_FAILED; } - (void)close(socket); + (void) close(socket); addr->sin_port = 0; return (stat); } @@ -104,27 +105,25 @@ pmap_rmtcall(addr, prog, vers, proc, xdrargs, argsp, xdrres, resp, tout, port_pt * XDR remote call arguments * written for XDR_ENCODE direction only */ -bool_t -xdr_rmtcall_args(xdrs, cap) - register XDR *xdrs; - register struct rmtcallargs *cap; +bool_t xdr_rmtcall_args(xdrs, cap) +register XDR *xdrs; +register struct rmtcallargs *cap; { u_int lenposition, argposition, position; if (xdr_u_long(xdrs, &(cap->prog)) && - xdr_u_long(xdrs, &(cap->vers)) && - xdr_u_long(xdrs, &(cap->proc))) { + xdr_u_long(xdrs, &(cap->vers)) && xdr_u_long(xdrs, &(cap->proc))) { lenposition = XDR_GETPOS(xdrs); - if (! xdr_u_long(xdrs, &(cap->arglen))) - return (FALSE); + if (!xdr_u_long(xdrs, &(cap->arglen))) + return (FALSE); argposition = XDR_GETPOS(xdrs); - if (! (*(cap->xdr_args))(xdrs, cap->args_ptr)) - return (FALSE); + if (!(*(cap->xdr_args)) (xdrs, cap->args_ptr)) + return (FALSE); position = XDR_GETPOS(xdrs); - cap->arglen = (u_long)position - (u_long)argposition; + cap->arglen = (u_long) position - (u_long) argposition; XDR_SETPOS(xdrs, lenposition); - if (! xdr_u_long(xdrs, &(cap->arglen))) - return (FALSE); + if (!xdr_u_long(xdrs, &(cap->arglen))) + return (FALSE); XDR_SETPOS(xdrs, position); return (TRUE); } @@ -135,18 +134,17 @@ xdr_rmtcall_args(xdrs, cap) * XDR remote call results * written for XDR_DECODE direction only */ -bool_t -xdr_rmtcallres(xdrs, crp) - register XDR *xdrs; - register struct rmtcallres *crp; +bool_t xdr_rmtcallres(xdrs, crp) +register XDR *xdrs; +register struct rmtcallres *crp; { caddr_t port_ptr; - port_ptr = (caddr_t)crp->port_ptr; - if (xdr_reference(xdrs, &port_ptr, sizeof (u_long), - xdr_u_long) && xdr_u_long(xdrs, &crp->resultslen)) { - crp->port_ptr = (u_long *)port_ptr; - return ((*(crp->xdr_results))(xdrs, crp->results_ptr)); + port_ptr = (caddr_t) crp->port_ptr; + if (xdr_reference(xdrs, &port_ptr, sizeof(u_long), + xdr_u_long) && xdr_u_long(xdrs, &crp->resultslen)) { + crp->port_ptr = (u_long *) port_ptr; + return ((*(crp->xdr_results)) (xdrs, crp->results_ptr)); } return (FALSE); } @@ -158,11 +156,10 @@ xdr_rmtcallres(xdrs, crp) * routines which only support udp/ip . */ -static int -getbroadcastnets(addrs, sock, buf) - struct in_addr *addrs; - int sock; /* any valid socket will do */ - char *buf; /* why allocxate more when we can use existing... */ +static int getbroadcastnets(addrs, sock, buf) +struct in_addr *addrs; +int sock; /* any valid socket will do */ +char *buf; /* why allocxate more when we can use existing... */ { #ifdef __linux__ struct sockaddr_in addr; @@ -176,45 +173,46 @@ getbroadcastnets(addrs, sock, buf) return 1; #else struct ifconf ifc; - struct ifreq ifreq, *ifr; + struct ifreq ifreq, *ifr; struct sockaddr_in *sin; - int n, i; + int n, i; - ifc.ifc_len = UDPMSGSIZE; - ifc.ifc_buf = buf; - if (ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0) { - perror("broadcast: ioctl (get interface configuration)"); - return (0); - } - ifr = ifc.ifc_req; - for (i = 0, n = ifc.ifc_len/sizeof (struct ifreq); n > 0; n--, ifr++) { - ifreq = *ifr; - if (ioctl(sock, SIOCGIFFLAGS, (char *)&ifreq) < 0) { - perror("broadcast: ioctl (get interface flags)"); - continue; - } - if ((ifreq.ifr_flags & IFF_BROADCAST) && - (ifreq.ifr_flags & IFF_UP) && - ifr->ifr_addr.sa_family == AF_INET) { - sin = (struct sockaddr_in *)&ifr->ifr_addr; -#ifdef SIOCGIFBRDADDR /* 4.3BSD */ - if (ioctl(sock, SIOCGIFBRDADDR, (char *)&ifreq) < 0) { + ifc.ifc_len = UDPMSGSIZE; + ifc.ifc_buf = buf; + if (ioctl(sock, SIOCGIFCONF, (char *) &ifc) < 0) { + perror("broadcast: ioctl (get interface configuration)"); + return (0); + } + ifr = ifc.ifc_req; + for (i = 0, n = ifc.ifc_len / sizeof(struct ifreq); n > 0; n--, ifr++) { + ifreq = *ifr; + if (ioctl(sock, SIOCGIFFLAGS, (char *) &ifreq) < 0) { + perror("broadcast: ioctl (get interface flags)"); + continue; + } + if ((ifreq.ifr_flags & IFF_BROADCAST) && + (ifreq.ifr_flags & IFF_UP) && + ifr->ifr_addr.sa_family == AF_INET) { + sin = (struct sockaddr_in *) &ifr->ifr_addr; +#ifdef SIOCGIFBRDADDR /* 4.3BSD */ + if (ioctl(sock, SIOCGIFBRDADDR, (char *) &ifreq) < 0) { #if 1 -printf("%s(%d): no inet_makeaddr()\n", __FILE__, __LINE__); + printf("%s(%d): no inet_makeaddr()\n", __FILE__, __LINE__); #else addrs[i++] = inet_makeaddr(inet_netof - (sin->sin_addr.s_addr), INADDR_ANY); + (sin->sin_addr.s_addr), + INADDR_ANY); #endif } else { - addrs[i++] = ((struct sockaddr_in*) - &ifreq.ifr_addr)->sin_addr; + addrs[i++] = ((struct sockaddr_in *) + &ifreq.ifr_addr)->sin_addr; } -#else /* 4.2 BSD */ +#else /* 4.2 BSD */ #if 1 -printf("%s(%d): no inet_makeaddr()\n", __FILE__, __LINE__); + printf("%s(%d): no inet_makeaddr()\n", __FILE__, __LINE__); #else addrs[i++] = inet_makeaddr(inet_netof - (sin->sin_addr.s_addr), INADDR_ANY); + (sin->sin_addr.s_addr), INADDR_ANY); #endif #endif } @@ -223,18 +221,19 @@ printf("%s(%d): no inet_makeaddr()\n", __FILE__, __LINE__); #endif } -typedef bool_t (*resultproc_t)(); +typedef bool_t(*resultproc_t) (); -enum clnt_stat -clnt_broadcast(prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult) - u_long prog; /* program number */ - u_long vers; /* version number */ - u_long proc; /* procedure number */ - xdrproc_t xargs; /* xdr routine for args */ - caddr_t argsp; /* pointer to args */ - xdrproc_t xresults; /* xdr routine for results */ - caddr_t resultsp; /* pointer to results */ - resultproc_t eachresult; /* call with each result obtained */ +enum clnt_stat +clnt_broadcast(prog, vers, proc, xargs, argsp, xresults, resultsp, + eachresult) +u_long prog; /* program number */ +u_long vers; /* version number */ +u_long proc; /* procedure number */ +xdrproc_t xargs; /* xdr routine for args */ +caddr_t argsp; /* pointer to args */ +xdrproc_t xresults; /* xdr routine for results */ +caddr_t resultsp; /* pointer to results */ +resultproc_t eachresult; /* call with each result obtained */ { enum clnt_stat stat; AUTH *unix_auth = authunix_create_default(); @@ -243,23 +242,24 @@ clnt_broadcast(prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult) int outlen, inlen, fromlen, nets; register int sock; int on = 1; + #ifdef FD_SETSIZE fd_set mask; fd_set readfds; #else int readfds; register int mask; -#endif /* def FD_SETSIZE */ +#endif /* def FD_SETSIZE */ register int i; bool_t done = FALSE; register u_long xid; u_long port; struct in_addr addrs[20]; - struct sockaddr_in baddr, raddr; /* broadcast and response addresses */ + struct sockaddr_in baddr, raddr; /* broadcast and response addresses */ struct rmtcallargs a; struct rmtcallres r; struct rpc_msg msg; - struct timeval t; + struct timeval t; char outbuf[MAX_BROADCAST_SIZE], inbuf[UDPMSGSIZE]; /* @@ -272,25 +272,25 @@ clnt_broadcast(prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult) goto done_broad; } #ifdef SO_BROADCAST - if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &on, sizeof (on)) < 0) { + if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0) { perror("Cannot set socket option SO_BROADCAST"); stat = RPC_CANTSEND; goto done_broad; } -#endif /* def SO_BROADCAST */ +#endif /* def SO_BROADCAST */ #ifdef FD_SETSIZE FD_ZERO(&mask); FD_SET(sock, &mask); #else mask = (1 << sock); -#endif /* def FD_SETSIZE */ +#endif /* def FD_SETSIZE */ nets = getbroadcastnets(addrs, sock, inbuf); - bzero((char *)&baddr, sizeof (baddr)); + bzero((char *) &baddr, sizeof(baddr)); baddr.sin_family = AF_INET; baddr.sin_port = htons(PMAPPORT); baddr.sin_addr.s_addr = htonl(INADDR_ANY); /* baddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY); */ - (void)gettimeofday(&t, (struct timezone *)0); + (void) gettimeofday(&t, (struct timezone *) 0); msg.rm_xid = xid = getpid() ^ t.tv_sec ^ t.tv_usec; t.tv_usec = 0; msg.rm_direction = CALL; @@ -309,11 +309,11 @@ clnt_broadcast(prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult) r.xdr_results = xresults; r.results_ptr = resultsp; xdrmem_create(xdrs, outbuf, MAX_BROADCAST_SIZE, XDR_ENCODE); - if ((! xdr_callmsg(xdrs, &msg)) || (! xdr_rmtcall_args(xdrs, &a))) { + if ((!xdr_callmsg(xdrs, &msg)) || (!xdr_rmtcall_args(xdrs, &a))) { stat = RPC_CANTENCODEARGS; goto done_broad; } - outlen = (int)xdr_getpos(xdrs); + outlen = (int) xdr_getpos(xdrs); xdr_destroy(xdrs); /* * Basic loop: broadcast a packet and wait a while for response(s). @@ -323,8 +323,8 @@ clnt_broadcast(prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult) for (i = 0; i < nets; i++) { baddr.sin_addr = addrs[i]; if (sendto(sock, outbuf, outlen, 0, - (struct sockaddr *)&baddr, - sizeof (struct sockaddr)) != outlen) { + (struct sockaddr *) &baddr, + sizeof(struct sockaddr)) != outlen) { perror("Cannot send broadcast packet"); stat = RPC_CANTSEND; goto done_broad; @@ -334,30 +334,31 @@ clnt_broadcast(prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult) stat = RPC_SUCCESS; goto done_broad; } - recv_again: + recv_again: msg.acpted_rply.ar_verf = _null_auth; - msg.acpted_rply.ar_results.where = (caddr_t)&r; - msg.acpted_rply.ar_results.proc = xdr_rmtcallres; + msg.acpted_rply.ar_results.where = (caddr_t) & r; + msg.acpted_rply.ar_results.proc = xdr_rmtcallres; readfds = mask; - switch (select(_rpc_dtablesize(), &readfds, (int *)NULL, - (int *)NULL, &t)) { + switch (select(_rpc_dtablesize(), &readfds, (int *) NULL, + (int *) NULL, &t)) { - case 0: /* timed out */ + case 0: /* timed out */ stat = RPC_TIMEDOUT; continue; - case -1: /* some kind of error */ + case -1: /* some kind of error */ if (errno == EINTR) goto recv_again; perror("Broadcast select problem"); stat = RPC_CANTRECV; goto done_broad; - } /* end of select results switch */ - try_again: + } /* end of select results switch */ + try_again: fromlen = sizeof(struct sockaddr); + inlen = recvfrom(sock, inbuf, UDPMSGSIZE, 0, - (struct sockaddr *)&raddr, &fromlen); + (struct sockaddr *) &raddr, &fromlen); if (inlen < 0) { if (errno == EINTR) goto try_again; @@ -371,13 +372,13 @@ clnt_broadcast(prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult) * see if reply transaction id matches sent id. * If so, decode the results. */ - xdrmem_create(xdrs, inbuf, (u_int)inlen, XDR_DECODE); + xdrmem_create(xdrs, inbuf, (u_int) inlen, XDR_DECODE); if (xdr_replymsg(xdrs, &msg)) { if ((msg.rm_xid == xid) && (msg.rm_reply.rp_stat == MSG_ACCEPTED) && (msg.acpted_rply.ar_stat == SUCCESS)) { - raddr.sin_port = htons((u_short)port); - done = (*eachresult)(resultsp, &raddr); + raddr.sin_port = htons((u_short) port); + done = (*eachresult) (resultsp, &raddr); } /* otherwise, we just ignore the errors ... */ } else { @@ -390,8 +391,8 @@ clnt_broadcast(prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult) } xdrs->x_op = XDR_FREE; msg.acpted_rply.ar_results.proc = xdr_void; - (void)xdr_replymsg(xdrs, &msg); - (void)(*xresults)(xdrs, resultsp); + (void) xdr_replymsg(xdrs, &msg); + (void) (*xresults) (xdrs, resultsp); xdr_destroy(xdrs); if (done) { stat = RPC_SUCCESS; @@ -400,9 +401,8 @@ clnt_broadcast(prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult) goto recv_again; } } -done_broad: - (void)close(sock); + done_broad: + (void) close(sock); AUTH_DESTROY(unix_auth); return (stat); } - diff --git a/libc/inet/rpc/rpc_callmsg.c b/libc/inet/rpc/rpc_callmsg.c index d9d815a6f..1bd6bb6d6 100644 --- a/libc/inet/rpc/rpc_callmsg.c +++ b/libc/inet/rpc/rpc_callmsg.c @@ -28,7 +28,9 @@ * Mountain View, California 94043 */ #if !defined(lint) && defined(SCCSIDS) -static char sccsid[] = "@(#)rpc_callmsg.c 1.4 87/08/11 Copyr 1984 Sun Micro"; +static char sccsid[] = + + "@(#)rpc_callmsg.c 1.4 87/08/11 Copyr 1984 Sun Micro"; #endif /* @@ -45,10 +47,9 @@ static char sccsid[] = "@(#)rpc_callmsg.c 1.4 87/08/11 Copyr 1984 Sun Micro"; /* * XDR a call message */ -bool_t -xdr_callmsg(xdrs, cmsg) - register XDR *xdrs; - register struct rpc_msg *cmsg; +bool_t xdr_callmsg(xdrs, cmsg) +register XDR *xdrs; +register struct rpc_msg *cmsg; { register long *buf; register struct opaque_auth *oa; @@ -61,9 +62,9 @@ xdr_callmsg(xdrs, cmsg) return (FALSE); } buf = XDR_INLINE(xdrs, 8 * BYTES_PER_XDR_UNIT - + RNDUP(cmsg->rm_call.cb_cred.oa_length) - + 2 * BYTES_PER_XDR_UNIT - + RNDUP(cmsg->rm_call.cb_verf.oa_length)); + + RNDUP(cmsg->rm_call.cb_cred.oa_length) + + 2 * BYTES_PER_XDR_UNIT + + RNDUP(cmsg->rm_call.cb_verf.oa_length)); if (buf != NULL) { IXDR_PUT_LONG(buf, cmsg->rm_xid); IXDR_PUT_ENUM(buf, cmsg->rm_direction); @@ -81,17 +82,17 @@ xdr_callmsg(xdrs, cmsg) IXDR_PUT_ENUM(buf, oa->oa_flavor); IXDR_PUT_LONG(buf, oa->oa_length); if (oa->oa_length) { - bcopy(oa->oa_base, (caddr_t)buf, oa->oa_length); - buf += RNDUP(oa->oa_length) / sizeof (long); + bcopy(oa->oa_base, (caddr_t) buf, oa->oa_length); + buf += RNDUP(oa->oa_length) / sizeof(long); } oa = &cmsg->rm_call.cb_verf; IXDR_PUT_ENUM(buf, oa->oa_flavor); IXDR_PUT_LONG(buf, oa->oa_length); if (oa->oa_length) { - bcopy(oa->oa_base, (caddr_t)buf, oa->oa_length); + bcopy(oa->oa_base, (caddr_t) buf, oa->oa_length); /* no real need.... - buf += RNDUP(oa->oa_length) / sizeof (long); - */ + buf += RNDUP(oa->oa_length) / sizeof (long); + */ } return (TRUE); } @@ -101,6 +102,7 @@ xdr_callmsg(xdrs, cmsg) if (buf != NULL) { cmsg->rm_xid = IXDR_GET_LONG(buf); cmsg->rm_direction = IXDR_GET_ENUM(buf, enum msg_type); + if (cmsg->rm_direction != CALL) { return (FALSE); } @@ -125,23 +127,22 @@ xdr_callmsg(xdrs, cmsg) buf = XDR_INLINE(xdrs, RNDUP(oa->oa_length)); if (buf == NULL) { if (xdr_opaque(xdrs, oa->oa_base, - oa->oa_length) == FALSE) { + oa->oa_length) == FALSE) { return (FALSE); } } else { - bcopy((caddr_t)buf, oa->oa_base, - oa->oa_length); + bcopy((caddr_t) buf, oa->oa_base, oa->oa_length); /* no real need.... - buf += RNDUP(oa->oa_length) / - sizeof (long); - */ + buf += RNDUP(oa->oa_length) / + sizeof (long); + */ } } oa = &cmsg->rm_call.cb_verf; buf = XDR_INLINE(xdrs, 2 * BYTES_PER_XDR_UNIT); if (buf == NULL) { if (xdr_enum(xdrs, &oa->oa_flavor) == FALSE || - xdr_u_int(xdrs, &oa->oa_length) == FALSE) { + xdr_u_int(xdrs, &oa->oa_length) == FALSE) { return (FALSE); } } else { @@ -159,32 +160,29 @@ xdr_callmsg(xdrs, cmsg) buf = XDR_INLINE(xdrs, RNDUP(oa->oa_length)); if (buf == NULL) { if (xdr_opaque(xdrs, oa->oa_base, - oa->oa_length) == FALSE) { + oa->oa_length) == FALSE) { return (FALSE); } } else { - bcopy((caddr_t)buf, oa->oa_base, - oa->oa_length); + bcopy((caddr_t) buf, oa->oa_base, oa->oa_length); /* no real need... - buf += RNDUP(oa->oa_length) / - sizeof (long); - */ + buf += RNDUP(oa->oa_length) / + sizeof (long); + */ } } return (TRUE); } } - if ( - xdr_u_long(xdrs, &(cmsg->rm_xid)) && - xdr_enum(xdrs, (enum_t *)&(cmsg->rm_direction)) && - (cmsg->rm_direction == CALL) && - xdr_u_long(xdrs, &(cmsg->rm_call.cb_rpcvers)) && - (cmsg->rm_call.cb_rpcvers == RPC_MSG_VERSION) && - xdr_u_long(xdrs, &(cmsg->rm_call.cb_prog)) && - xdr_u_long(xdrs, &(cmsg->rm_call.cb_vers)) && - xdr_u_long(xdrs, &(cmsg->rm_call.cb_proc)) && - xdr_opaque_auth(xdrs, &(cmsg->rm_call.cb_cred)) ) - return (xdr_opaque_auth(xdrs, &(cmsg->rm_call.cb_verf))); + if (xdr_u_long(xdrs, &(cmsg->rm_xid)) && + xdr_enum(xdrs, (enum_t *) & (cmsg->rm_direction)) && + (cmsg->rm_direction == CALL) && + xdr_u_long(xdrs, &(cmsg->rm_call.cb_rpcvers)) && + (cmsg->rm_call.cb_rpcvers == RPC_MSG_VERSION) && + xdr_u_long(xdrs, &(cmsg->rm_call.cb_prog)) && + xdr_u_long(xdrs, &(cmsg->rm_call.cb_vers)) && + xdr_u_long(xdrs, &(cmsg->rm_call.cb_proc)) && + xdr_opaque_auth(xdrs, &(cmsg->rm_call.cb_cred))) + return (xdr_opaque_auth(xdrs, &(cmsg->rm_call.cb_verf))); return (FALSE); } - diff --git a/libc/inet/rpc/rpc_commondata.c b/libc/inet/rpc/rpc_commondata.c index 75cead087..39648d284 100644 --- a/libc/inet/rpc/rpc_commondata.c +++ b/libc/inet/rpc/rpc_commondata.c @@ -33,9 +33,10 @@ * by public interfaces */ struct opaque_auth _null_auth; + #ifdef FD_SETSIZE fd_set svc_fdset; #else int svc_fds; -#endif /* def FD_SETSIZE */ +#endif /* def FD_SETSIZE */ struct rpc_createerr rpc_createerr; diff --git a/libc/inet/rpc/rpc_dtablesize.c b/libc/inet/rpc/rpc_dtablesize.c index a8488172e..960bb4699 100644 --- a/libc/inet/rpc/rpc_dtablesize.c +++ b/libc/inet/rpc/rpc_dtablesize.c @@ -28,7 +28,9 @@ * Mountain View, California 94043 */ #if !defined(lint) && defined(SCCSIDS) -static char sccsid[] = "@(#)rpc_dtablesize.c 1.2 87/08/11 Copyr 1987 Sun Micro"; +static char sccsid[] = + + "@(#)rpc_dtablesize.c 1.2 87/08/11 Copyr 1987 Sun Micro"; #endif /* @@ -38,7 +40,7 @@ static char sccsid[] = "@(#)rpc_dtablesize.c 1.2 87/08/11 Copyr 1987 Sun Micro"; _rpc_dtablesize() { static int size; - + if (size == 0) { size = getdtablesize(); } diff --git a/libc/inet/rpc/rpc_prot.c b/libc/inet/rpc/rpc_prot.c index 4b1319ad5..e3c873b44 100644 --- a/libc/inet/rpc/rpc_prot.c +++ b/libc/inet/rpc/rpc_prot.c @@ -56,27 +56,25 @@ struct opaque_auth _null_auth; * XDR an opaque authentication struct * (see auth.h) */ -bool_t -xdr_opaque_auth(xdrs, ap) - register XDR *xdrs; - register struct opaque_auth *ap; +bool_t xdr_opaque_auth(xdrs, ap) +register XDR *xdrs; +register struct opaque_auth *ap; { if (xdr_enum(xdrs, &(ap->oa_flavor))) return (xdr_bytes(xdrs, &ap->oa_base, - &ap->oa_length, MAX_AUTH_BYTES)); + &ap->oa_length, MAX_AUTH_BYTES)); return (FALSE); } /* * XDR a DES block */ -bool_t -xdr_des_block(xdrs, blkp) - register XDR *xdrs; - register des_block *blkp; +bool_t xdr_des_block(xdrs, blkp) +register XDR *xdrs; +register des_block *blkp; { - return (xdr_opaque(xdrs, (caddr_t)blkp, sizeof(des_block))); + return (xdr_opaque(xdrs, (caddr_t) blkp, sizeof(des_block))); } /* * * * * * * * * * * * * * XDR RPC MESSAGE * * * * * * * * * * * * * * * */ @@ -84,74 +82,72 @@ xdr_des_block(xdrs, blkp) /* * XDR the MSG_ACCEPTED part of a reply message union */ -bool_t -xdr_accepted_reply(xdrs, ar) - register XDR *xdrs; - register struct accepted_reply *ar; +bool_t xdr_accepted_reply(xdrs, ar) +register XDR *xdrs; +register struct accepted_reply *ar; { /* personalized union, rather than calling xdr_union */ - if (! xdr_opaque_auth(xdrs, &(ar->ar_verf))) + if (!xdr_opaque_auth(xdrs, &(ar->ar_verf))) return (FALSE); - if (! xdr_enum(xdrs, (enum_t *)&(ar->ar_stat))) + if (!xdr_enum(xdrs, (enum_t *) & (ar->ar_stat))) return (FALSE); switch (ar->ar_stat) { case SUCCESS: - return ((*(ar->ar_results.proc))(xdrs, ar->ar_results.where)); + return ((*(ar->ar_results.proc)) (xdrs, ar->ar_results.where)); case PROG_MISMATCH: - if (! xdr_u_long(xdrs, &(ar->ar_vers.low))) + if (!xdr_u_long(xdrs, &(ar->ar_vers.low))) return (FALSE); return (xdr_u_long(xdrs, &(ar->ar_vers.high))); } - return (TRUE); /* TRUE => open ended set of problems */ + return (TRUE); /* TRUE => open ended set of problems */ } /* * XDR the MSG_DENIED part of a reply message union */ -bool_t -xdr_rejected_reply(xdrs, rr) - register XDR *xdrs; - register struct rejected_reply *rr; +bool_t xdr_rejected_reply(xdrs, rr) +register XDR *xdrs; +register struct rejected_reply *rr; { /* personalized union, rather than calling xdr_union */ - if (! xdr_enum(xdrs, (enum_t *)&(rr->rj_stat))) + if (!xdr_enum(xdrs, (enum_t *) & (rr->rj_stat))) return (FALSE); switch (rr->rj_stat) { case RPC_MISMATCH: - if (! xdr_u_long(xdrs, &(rr->rj_vers.low))) + if (!xdr_u_long(xdrs, &(rr->rj_vers.low))) return (FALSE); return (xdr_u_long(xdrs, &(rr->rj_vers.high))); case AUTH_ERROR: - return (xdr_enum(xdrs, (enum_t *)&(rr->rj_why))); + return (xdr_enum(xdrs, (enum_t *) & (rr->rj_why))); } return (FALSE); } static struct xdr_discrim reply_dscrm[3] = { - { (int)MSG_ACCEPTED, xdr_accepted_reply }, - { (int)MSG_DENIED, xdr_rejected_reply }, - { __dontcare__, NULL_xdrproc_t } }; + {(int) MSG_ACCEPTED, xdr_accepted_reply}, + {(int) MSG_DENIED, xdr_rejected_reply}, + {__dontcare__, NULL_xdrproc_t} +}; /* * XDR a reply message */ -bool_t -xdr_replymsg(xdrs, rmsg) - register XDR *xdrs; - register struct rpc_msg *rmsg; +bool_t xdr_replymsg(xdrs, rmsg) +register XDR *xdrs; +register struct rpc_msg *rmsg; { - if ( - xdr_u_long(xdrs, &(rmsg->rm_xid)) && - xdr_enum(xdrs, (enum_t *)&(rmsg->rm_direction)) && - (rmsg->rm_direction == REPLY) ) - return (xdr_union(xdrs, (enum_t *)&(rmsg->rm_reply.rp_stat), - (caddr_t)&(rmsg->rm_reply.ru), reply_dscrm, NULL_xdrproc_t)); + if (xdr_u_long(xdrs, &(rmsg->rm_xid)) && + xdr_enum(xdrs, (enum_t *) & (rmsg->rm_direction)) && + (rmsg->rm_direction == REPLY)) + return (xdr_union(xdrs, (enum_t *) & (rmsg->rm_reply.rp_stat), + (caddr_t) & (rmsg->rm_reply.ru), reply_dscrm, + NULL_xdrproc_t)); return (FALSE); } @@ -161,30 +157,28 @@ xdr_replymsg(xdrs, rmsg) * The fields include: rm_xid, rm_direction, rpcvers, prog, and vers. * The rm_xid is not really static, but the user can easily munge on the fly. */ -bool_t -xdr_callhdr(xdrs, cmsg) - register XDR *xdrs; - register struct rpc_msg *cmsg; +bool_t xdr_callhdr(xdrs, cmsg) +register XDR *xdrs; +register struct rpc_msg *cmsg; { cmsg->rm_direction = CALL; cmsg->rm_call.cb_rpcvers = RPC_MSG_VERSION; if ( - (xdrs->x_op == XDR_ENCODE) && - xdr_u_long(xdrs, &(cmsg->rm_xid)) && - xdr_enum(xdrs, (enum_t *)&(cmsg->rm_direction)) && - xdr_u_long(xdrs, &(cmsg->rm_call.cb_rpcvers)) && - xdr_u_long(xdrs, &(cmsg->rm_call.cb_prog)) ) - return (xdr_u_long(xdrs, &(cmsg->rm_call.cb_vers))); + (xdrs->x_op == XDR_ENCODE) && + xdr_u_long(xdrs, &(cmsg->rm_xid)) && + xdr_enum(xdrs, (enum_t *) & (cmsg->rm_direction)) && + xdr_u_long(xdrs, &(cmsg->rm_call.cb_rpcvers)) && + xdr_u_long(xdrs, &(cmsg->rm_call.cb_prog))) + return (xdr_u_long(xdrs, &(cmsg->rm_call.cb_vers))); return (FALSE); } /* ************************** Client utility routine ************* */ -static void -accepted(acpt_stat, error) - register enum accept_stat acpt_stat; - register struct rpc_err *error; +static void accepted(acpt_stat, error) +register enum accept_stat acpt_stat; +register struct rpc_err *error; { switch (acpt_stat) { @@ -215,14 +209,13 @@ accepted(acpt_stat, error) } /* something's wrong, but we don't know what ... */ error->re_status = RPC_FAILED; - error->re_lb.s1 = (long)MSG_ACCEPTED; - error->re_lb.s2 = (long)acpt_stat; + error->re_lb.s1 = (long) MSG_ACCEPTED; + error->re_lb.s2 = (long) acpt_stat; } -static void -rejected(rjct_stat, error) - register enum reject_stat rjct_stat; - register struct rpc_err *error; +static void rejected(rjct_stat, error) +register enum reject_stat rjct_stat; +register struct rpc_err *error; { switch (rjct_stat) { @@ -237,17 +230,16 @@ rejected(rjct_stat, error) } /* something's wrong, but we don't know what ... */ error->re_status = RPC_FAILED; - error->re_lb.s1 = (long)MSG_DENIED; - error->re_lb.s2 = (long)rjct_stat; + error->re_lb.s1 = (long) MSG_DENIED; + error->re_lb.s2 = (long) rjct_stat; } /* * given a reply message, fills in the error */ -void -_seterr_reply(msg, error) - register struct rpc_msg *msg; - register struct rpc_err *error; +void _seterr_reply(msg, error) +register struct rpc_msg *msg; +register struct rpc_err *error; { /* optimized for normal, SUCCESSful case */ @@ -267,7 +259,7 @@ _seterr_reply(msg, error) default: error->re_status = RPC_FAILED; - error->re_lb.s1 = (long)(msg->rm_reply.rp_stat); + error->re_lb.s1 = (long) (msg->rm_reply.rp_stat); break; } switch (error->re_status) { diff --git a/libc/inet/rpc/svc.c b/libc/inet/rpc/svc.c index 9d0092d8a..1ba12efd5 100644 --- a/libc/inet/rpc/svc.c +++ b/libc/inet/rpc/svc.c @@ -56,10 +56,10 @@ static SVCXPRT **xports; #define NOFILE 32 static SVCXPRT *xports[NOFILE]; -#endif /* def FD_SETSIZE */ +#endif /* def FD_SETSIZE */ #define NULL_SVC ((struct svc_callout *)0) -#define RQCRED_SIZE 400 /* this size is excessive */ +#define RQCRED_SIZE 400 /* this size is excessive */ /* * The services list @@ -69,9 +69,9 @@ static SVCXPRT *xports[NOFILE]; */ static struct svc_callout { struct svc_callout *sc_next; - u_long sc_prog; - u_long sc_vers; - void (*sc_dispatch)(); + u_long sc_prog; + u_long sc_vers; + void (*sc_dispatch) (); } *svc_head; static struct svc_callout *svc_find(); @@ -81,9 +81,8 @@ static struct svc_callout *svc_find(); /* * Activate a transport handle. */ -void -xprt_register(xprt) - SVCXPRT *xprt; +void xprt_register(xprt) +SVCXPRT *xprt; { register int sock = xprt->xp_sock; @@ -101,30 +100,29 @@ xprt_register(xprt) xports[sock] = xprt; svc_fds |= (1 << sock); } -#endif /* def FD_SETSIZE */ +#endif /* def FD_SETSIZE */ } /* * De-activate a transport handle. */ -void -xprt_unregister(xprt) - SVCXPRT *xprt; -{ +void xprt_unregister(xprt) +SVCXPRT *xprt; +{ register int sock = xprt->xp_sock; #ifdef FD_SETSIZE if ((sock < _rpc_dtablesize()) && (xports[sock] == xprt)) { - xports[sock] = (SVCXPRT *)0; + xports[sock] = (SVCXPRT *) 0; FD_CLR(sock, &svc_fdset); } #else if ((sock < NOFILE) && (xports[sock] == xprt)) { - xports[sock] = (SVCXPRT *)0; + xports[sock] = (SVCXPRT *) 0; svc_fds &= ~(1 << sock); } -#endif /* def FD_SETSIZE */ +#endif /* def FD_SETSIZE */ } @@ -135,24 +133,24 @@ xprt_unregister(xprt) * The dispatch routine will be called when a rpc request for this * program number comes in. */ -bool_t -svc_register(xprt, prog, vers, dispatch, protocol) - SVCXPRT *xprt; - u_long prog; - u_long vers; - void (*dispatch)(); - int protocol; +bool_t svc_register(xprt, prog, vers, dispatch, protocol) +SVCXPRT *xprt; +u_long prog; +u_long vers; +void (*dispatch) (); +int protocol; { struct svc_callout *prev; register struct svc_callout *s; if ((s = svc_find(prog, vers, &prev)) != NULL_SVC) { if (s->sc_dispatch == dispatch) - goto pmap_it; /* he is registering another xptr */ + goto pmap_it; /* he is registering another xptr */ return (FALSE); } - s = (struct svc_callout *)mem_alloc(sizeof(struct svc_callout)); - if (s == (struct svc_callout *)0) { + s = (struct svc_callout *) mem_alloc(sizeof(struct svc_callout)); + + if (s == (struct svc_callout *) 0) { return (FALSE); } s->sc_prog = prog; @@ -160,7 +158,7 @@ svc_register(xprt, prog, vers, dispatch, protocol) s->sc_dispatch = dispatch; s->sc_next = svc_head; svc_head = s; -pmap_it: + pmap_it: /* now register the information with the local binder service */ if (protocol) { return (pmap_set(prog, vers, protocol, xprt->xp_port)); @@ -171,10 +169,9 @@ pmap_it: /* * Remove a service program from the callout list. */ -void -svc_unregister(prog, vers) - u_long prog; - u_long vers; +void svc_unregister(prog, vers) +u_long prog; +u_long vers; { struct svc_callout *prev; register struct svc_callout *s; @@ -188,19 +185,19 @@ svc_unregister(prog, vers) } s->sc_next = NULL_SVC; mem_free((char *) s, (u_int) sizeof(struct svc_callout)); + /* now unregister the information with the local binder service */ - (void)pmap_unset(prog, vers); + (void) pmap_unset(prog, vers); } /* * Search the callout list for a program number, return the callout * struct. */ -static struct svc_callout * -svc_find(prog, vers, prev) - u_long prog; - u_long vers; - struct svc_callout **prev; +static struct svc_callout *svc_find(prog, vers, prev) +u_long prog; +u_long vers; +struct svc_callout **prev; { register struct svc_callout *s, *p; @@ -210,7 +207,7 @@ svc_find(prog, vers, prev) goto done; p = s; } -done: + done: *prev = p; return (s); } @@ -220,29 +217,27 @@ done: /* * Send a reply to an rpc request */ -bool_t -svc_sendreply(xprt, xdr_results, xdr_location) - register SVCXPRT *xprt; - xdrproc_t xdr_results; - caddr_t xdr_location; +bool_t svc_sendreply(xprt, xdr_results, xdr_location) +register SVCXPRT *xprt; +xdrproc_t xdr_results; +caddr_t xdr_location; { - struct rpc_msg rply; + struct rpc_msg rply; - rply.rm_direction = REPLY; - rply.rm_reply.rp_stat = MSG_ACCEPTED; - rply.acpted_rply.ar_verf = xprt->xp_verf; + rply.rm_direction = REPLY; + rply.rm_reply.rp_stat = MSG_ACCEPTED; + rply.acpted_rply.ar_verf = xprt->xp_verf; rply.acpted_rply.ar_stat = SUCCESS; rply.acpted_rply.ar_results.where = xdr_location; rply.acpted_rply.ar_results.proc = xdr_results; - return (SVC_REPLY(xprt, &rply)); + return (SVC_REPLY(xprt, &rply)); } /* * No procedure error reply */ -void -svcerr_noproc(xprt) - register SVCXPRT *xprt; +void svcerr_noproc(xprt) +register SVCXPRT *xprt; { struct rpc_msg rply; @@ -256,42 +251,39 @@ svcerr_noproc(xprt) /* * Can't decode args error reply */ -void -svcerr_decode(xprt) - register SVCXPRT *xprt; +void svcerr_decode(xprt) +register SVCXPRT *xprt; { - struct rpc_msg rply; + struct rpc_msg rply; - rply.rm_direction = REPLY; - rply.rm_reply.rp_stat = MSG_ACCEPTED; + rply.rm_direction = REPLY; + rply.rm_reply.rp_stat = MSG_ACCEPTED; rply.acpted_rply.ar_verf = xprt->xp_verf; rply.acpted_rply.ar_stat = GARBAGE_ARGS; - SVC_REPLY(xprt, &rply); + SVC_REPLY(xprt, &rply); } /* * Some system error */ -void -svcerr_systemerr(xprt) - register SVCXPRT *xprt; +void svcerr_systemerr(xprt) +register SVCXPRT *xprt; { - struct rpc_msg rply; + struct rpc_msg rply; - rply.rm_direction = REPLY; - rply.rm_reply.rp_stat = MSG_ACCEPTED; + rply.rm_direction = REPLY; + rply.rm_reply.rp_stat = MSG_ACCEPTED; rply.acpted_rply.ar_verf = xprt->xp_verf; rply.acpted_rply.ar_stat = SYSTEM_ERR; - SVC_REPLY(xprt, &rply); + SVC_REPLY(xprt, &rply); } /* * Authentication error reply */ -void -svcerr_auth(xprt, why) - SVCXPRT *xprt; - enum auth_stat why; +void svcerr_auth(xprt, why) +SVCXPRT *xprt; +enum auth_stat why; { struct rpc_msg rply; @@ -305,9 +297,8 @@ svcerr_auth(xprt, why) /* * Auth too weak error reply */ -void -svcerr_weakauth(xprt) - SVCXPRT *xprt; +void svcerr_weakauth(xprt) +SVCXPRT *xprt; { svcerr_auth(xprt, AUTH_TOOWEAK); @@ -316,15 +307,14 @@ svcerr_weakauth(xprt) /* * Program unavailable error reply */ -void -svcerr_noprog(xprt) - register SVCXPRT *xprt; +void svcerr_noprog(xprt) +register SVCXPRT *xprt; { - struct rpc_msg rply; + struct rpc_msg rply; - rply.rm_direction = REPLY; - rply.rm_reply.rp_stat = MSG_ACCEPTED; - rply.acpted_rply.ar_verf = xprt->xp_verf; + rply.rm_direction = REPLY; + rply.rm_reply.rp_stat = MSG_ACCEPTED; + rply.acpted_rply.ar_verf = xprt->xp_verf; rply.acpted_rply.ar_stat = PROG_UNAVAIL; SVC_REPLY(xprt, &rply); } @@ -332,11 +322,10 @@ svcerr_noprog(xprt) /* * Program version mismatch error reply */ -void -svcerr_progvers(xprt, low_vers, high_vers) - register SVCXPRT *xprt; - u_long low_vers; - u_long high_vers; +void svcerr_progvers(xprt, low_vers, high_vers) +register SVCXPRT *xprt; +u_long low_vers; +u_long high_vers; { struct rpc_msg rply; @@ -367,9 +356,8 @@ svcerr_progvers(xprt, low_vers, high_vers) * is mallocated in kernel land. */ -void -svc_getreq(rdfds) - int rdfds; +void svc_getreq(rdfds) +int rdfds; { #ifdef FD_SETSIZE fd_set readfds; @@ -386,19 +374,18 @@ svc_getreq(rdfds) int readfds = rdfds & svc_fds; svc_getreqset(&readfds); -#endif /* def FD_SETSIZE */ +#endif /* def FD_SETSIZE */ } -void -svc_getreqset(readfds) +void svc_getreqset(readfds) #ifdef FD_SETSIZE - fd_set *readfds; +fd_set *readfds; { #else - int *readfds; +int *readfds; { - int readfds_local = *readfds; -#endif /* def FD_SETSIZE */ + int readfds_local = *readfds; +#endif /* def FD_SETSIZE */ enum xprt_stat stat; struct rpc_msg msg; int prog_found; @@ -411,82 +398,82 @@ svc_getreqset(readfds) register u_long *maskp; register int setsize; register int sock; - char cred_area[2*MAX_AUTH_BYTES + RQCRED_SIZE]; + char cred_area[2 * MAX_AUTH_BYTES + RQCRED_SIZE]; + msg.rm_call.cb_cred.oa_base = cred_area; msg.rm_call.cb_verf.oa_base = &(cred_area[MAX_AUTH_BYTES]); - r.rq_clntcred = &(cred_area[2*MAX_AUTH_BYTES]); + r.rq_clntcred = &(cred_area[2 * MAX_AUTH_BYTES]); #ifdef FD_SETSIZE - setsize = _rpc_dtablesize(); + setsize = _rpc_dtablesize(); #ifdef __linux__ /*#define NFDBITS 32*/ - maskp = (u_long *)readfds; + maskp = (u_long *) readfds; #else - maskp = (u_long *)readfds->fds_bits; + maskp = (u_long *) readfds->fds_bits; #endif for (sock = 0; sock < setsize; sock += NFDBITS) { - for (mask = *maskp++; bit = ffs(mask); mask ^= (1 << (bit - 1))) { - /* sock has input waiting */ - xprt = xports[sock + bit - 1]; + for (mask = *maskp++; bit = ffs(mask); mask ^= (1 << (bit - 1))) { + /* sock has input waiting */ + xprt = xports[sock + bit - 1]; #else for (sock = 0; readfds_local != 0; sock++, readfds_local >>= 1) { - if ((readfds_local & 1) != 0) { - /* sock has input waiting */ - xprt = xports[sock]; -#endif /* def FD_SETSIZE */ - /* now receive msgs from xprtprt (support batch calls) */ - do { - if (SVC_RECV(xprt, &msg)) { - - /* now find the exported program and call it */ - register struct svc_callout *s; - enum auth_stat why; - - r.rq_xprt = xprt; - r.rq_prog = msg.rm_call.cb_prog; - r.rq_vers = msg.rm_call.cb_vers; - r.rq_proc = msg.rm_call.cb_proc; - r.rq_cred = msg.rm_call.cb_cred; - /* first authenticate the message */ - if ((why= _authenticate(&r, &msg)) != AUTH_OK) { - svcerr_auth(xprt, why); - goto call_done; + if ((readfds_local & 1) != 0) { + /* sock has input waiting */ + xprt = xports[sock]; +#endif /* def FD_SETSIZE */ + /* now receive msgs from xprtprt (support batch calls) */ + do { + if (SVC_RECV(xprt, &msg)) { + + /* now find the exported program and call it */ + register struct svc_callout *s; + enum auth_stat why; + + r.rq_xprt = xprt; + r.rq_prog = msg.rm_call.cb_prog; + r.rq_vers = msg.rm_call.cb_vers; + r.rq_proc = msg.rm_call.cb_proc; + r.rq_cred = msg.rm_call.cb_cred; + /* first authenticate the message */ + if ((why = _authenticate(&r, &msg)) != AUTH_OK) { + svcerr_auth(xprt, why); + goto call_done; + } + /* now match message with a registered service */ + prog_found = FALSE; + low_vers = 0 - 1; + high_vers = 0; + for (s = svc_head; s != NULL_SVC; s = s->sc_next) { + if (s->sc_prog == r.rq_prog) { + if (s->sc_vers == r.rq_vers) { + (*s->sc_dispatch) (&r, xprt); + goto call_done; + } /* found correct version */ + prog_found = TRUE; + if (s->sc_vers < low_vers) + low_vers = s->sc_vers; + if (s->sc_vers > high_vers) + high_vers = s->sc_vers; + } /* found correct program */ + } + /* + * if we got here, the program or version + * is not served ... + */ + if (prog_found) + svcerr_progvers(xprt, low_vers, high_vers); + else + svcerr_noprog(xprt); + /* Fall through to ... */ } - /* now match message with a registered service*/ - prog_found = FALSE; - low_vers = 0 - 1; - high_vers = 0; - for (s = svc_head; s != NULL_SVC; s = s->sc_next) { - if (s->sc_prog == r.rq_prog) { - if (s->sc_vers == r.rq_vers) { - (*s->sc_dispatch)(&r, xprt); - goto call_done; - } /* found correct version */ - prog_found = TRUE; - if (s->sc_vers < low_vers) - low_vers = s->sc_vers; - if (s->sc_vers > high_vers) - high_vers = s->sc_vers; - } /* found correct program */ + call_done: + if ((stat = SVC_STAT(xprt)) == XPRT_DIED) { + SVC_DESTROY(xprt); + break; } - /* - * if we got here, the program or version - * is not served ... - */ - if (prog_found) - svcerr_progvers(xprt, - low_vers, high_vers); - else - svcerr_noprog(xprt); - /* Fall through to ... */ - } - call_done: - if ((stat = SVC_STAT(xprt)) == XPRT_DIED){ - SVC_DESTROY(xprt); - break; - } - } while (stat == XPRT_MOREREQS); - } + } while (stat == XPRT_MOREREQS); + } } } diff --git a/libc/inet/rpc/svc_auth.c b/libc/inet/rpc/svc_auth.c index ab7ab6942..60e4fa74b 100644 --- a/libc/inet/rpc/svc_auth.c +++ b/libc/inet/rpc/svc_auth.c @@ -1,5 +1,7 @@ #if !defined(lint) && defined(SCCSIDS) -static char sccsid[] = "@(#)svc_auth.c 2.1 88/08/07 4.0 RPCSRC; from 1.19 87/08/11 Copyr 1984 Sun Micro"; +static char sccsid[] = + + "@(#)svc_auth.c 2.1 88/08/07 4.0 RPCSRC; from 1.19 87/08/11 Copyr 1984 Sun Micro"; #endif /* * Sun RPC is a product of Sun Microsystems, Inc. and is provided for @@ -54,18 +56,19 @@ static char sccsid[] = "@(#)svc_auth.c 2.1 88/08/07 4.0 RPCSRC; from 1.19 87/08/ * */ -enum auth_stat _svcauth_null(); /* no authentication */ -enum auth_stat _svcauth_unix(); /* unix style (uid, gids) */ +enum auth_stat _svcauth_null(); /* no authentication */ +enum auth_stat _svcauth_unix(); /* unix style (uid, gids) */ enum auth_stat _svcauth_short(); /* short hand unix style */ static struct { - enum auth_stat (*authenticator)(); + enum auth_stat (*authenticator) (); } svcauthsw[] = { - _svcauth_null, /* AUTH_NULL */ - _svcauth_unix, /* AUTH_UNIX */ - _svcauth_short, /* AUTH_SHORT */ + _svcauth_null, /* AUTH_NULL */ + _svcauth_unix, /* AUTH_UNIX */ + _svcauth_short, /* AUTH_SHORT */ }; -#define AUTH_MAX 2 /* HIGHEST AUTH NUMBER */ + +#define AUTH_MAX 2 /* HIGHEST AUTH NUMBER */ /* @@ -86,10 +89,9 @@ static struct { * There is an assumption that any flavour less than AUTH_NULL is * invalid. */ -enum auth_stat -_authenticate(rqst, msg) - register struct svc_req *rqst; - struct rpc_msg *msg; +enum auth_stat _authenticate(rqst, msg) +register struct svc_req *rqst; +struct rpc_msg *msg; { register int cred_flavor; @@ -98,16 +100,15 @@ _authenticate(rqst, msg) rqst->rq_xprt->xp_verf.oa_length = 0; cred_flavor = rqst->rq_cred.oa_flavor; if ((cred_flavor <= AUTH_MAX) && (cred_flavor >= AUTH_NULL)) { - return ((*(svcauthsw[cred_flavor].authenticator))(rqst, msg)); + return ((*(svcauthsw[cred_flavor].authenticator)) (rqst, msg)); } return (AUTH_REJECTEDCRED); } -enum auth_stat -_svcauth_null(/*rqst, msg*/) +enum auth_stat _svcauth_null( /*rqst, msg */ ) /*struct svc_req *rqst; - struct rpc_msg *msg;*/ + struct rpc_msg *msg; */ { return (AUTH_OK); diff --git a/libc/inet/rpc/svc_auth_unix.c b/libc/inet/rpc/svc_auth_unix.c index ea00b7895..212f9cb3a 100644 --- a/libc/inet/rpc/svc_auth_unix.c +++ b/libc/inet/rpc/svc_auth_unix.c @@ -28,7 +28,9 @@ * Mountain View, California 94043 */ #if !defined(lint) && defined(SCCSIDS) -static char sccsid[] = "@(#)svc_auth_unix.c 1.28 88/02/08 Copyr 1984 Sun Micro"; +static char sccsid[] = + + "@(#)svc_auth_unix.c 1.28 88/02/08 Copyr 1984 Sun Micro"; #endif /* @@ -48,10 +50,9 @@ static char sccsid[] = "@(#)svc_auth_unix.c 1.28 88/02/08 Copyr 1984 Sun Micro"; /* * Unix longhand authenticator */ -enum auth_stat -_svcauth_unix(rqst, msg) - register struct svc_req *rqst; - register struct rpc_msg *msg; +enum auth_stat _svcauth_unix(rqst, msg) +register struct svc_req *rqst; +register struct rpc_msg *msg; { register enum auth_stat stat; XDR xdrs; @@ -59,7 +60,7 @@ _svcauth_unix(rqst, msg) register long *buf; struct area { struct authunix_parms area_aup; - char area_machname[MAX_MACHINE_NAME+1]; + char area_machname[MAX_MACHINE_NAME + 1]; int area_gids[NGRPS]; } *area; u_int auth_len; @@ -70,8 +71,9 @@ _svcauth_unix(rqst, msg) aup = &area->area_aup; aup->aup_machname = area->area_machname; aup->aup_gids = area->area_gids; - auth_len = (u_int)msg->rm_call.cb_cred.oa_length; - xdrmem_create(&xdrs, msg->rm_call.cb_cred.oa_base, auth_len,XDR_DECODE); + auth_len = (u_int) msg->rm_call.cb_cred.oa_length; + xdrmem_create(&xdrs, msg->rm_call.cb_cred.oa_base, auth_len, + XDR_DECODE); buf = XDR_INLINE(&xdrs, auth_len); if (buf != NULL) { aup->aup_time = IXDR_GET_LONG(buf); @@ -80,10 +82,11 @@ _svcauth_unix(rqst, msg) stat = AUTH_BADCRED; goto done; } - bcopy((caddr_t)buf, aup->aup_machname, (u_int)str_len); + bcopy((caddr_t) buf, aup->aup_machname, (u_int) str_len); aup->aup_machname[str_len] = 0; str_len = RNDUP(str_len); - buf += str_len / sizeof (long); + buf += str_len / sizeof(long); + aup->aup_uid = IXDR_GET_LONG(buf); aup->aup_gid = IXDR_GET_LONG(buf); gid_len = IXDR_GET_U_LONG(buf); @@ -101,20 +104,20 @@ _svcauth_unix(rqst, msg) */ if ((5 + gid_len) * BYTES_PER_XDR_UNIT + str_len > auth_len) { (void) printf("bad auth_len gid %d str %d auth %d\n", - gid_len, str_len, auth_len); + gid_len, str_len, auth_len); stat = AUTH_BADCRED; goto done; } - } else if (! xdr_authunix_parms(&xdrs, aup)) { + } else if (!xdr_authunix_parms(&xdrs, aup)) { xdrs.x_op = XDR_FREE; - (void)xdr_authunix_parms(&xdrs, aup); + (void) xdr_authunix_parms(&xdrs, aup); stat = AUTH_BADCRED; goto done; } rqst->rq_xprt->xp_verf.oa_flavor = AUTH_NULL; rqst->rq_xprt->xp_verf.oa_length = 0; stat = AUTH_OK; -done: + done: XDR_DESTROY(&xdrs); return (stat); } @@ -124,11 +127,9 @@ done: * Shorthand unix authenticator * Looks up longhand in a cache. */ -/*ARGSUSED*/ -enum auth_stat -_svcauth_short(rqst, msg) - struct svc_req *rqst; - struct rpc_msg *msg; + /*ARGSUSED*/ enum auth_stat _svcauth_short(rqst, msg) +struct svc_req *rqst; +struct rpc_msg *msg; { return (AUTH_REJECTEDCRED); } diff --git a/libc/inet/rpc/svc_raw.c b/libc/inet/rpc/svc_raw.c index 1170ecec8..c13ea5e06 100644 --- a/libc/inet/rpc/svc_raw.c +++ b/libc/inet/rpc/svc_raw.c @@ -47,18 +47,18 @@ static char sccsid[] = "@(#)svc_raw.c 1.15 87/08/11 Copyr 1984 Sun Micro"; * This is the "network" that we will be moving data over */ static struct svcraw_private { - char _raw_buf[UDPMSGSIZE]; - SVCXPRT server; - XDR xdr_stream; - char verf_body[MAX_AUTH_BYTES]; + char _raw_buf[UDPMSGSIZE]; + SVCXPRT server; + XDR xdr_stream; + char verf_body[MAX_AUTH_BYTES]; } *svcraw_private; -static bool_t svcraw_recv(); -static enum xprt_stat svcraw_stat(); -static bool_t svcraw_getargs(); -static bool_t svcraw_reply(); -static bool_t svcraw_freeargs(); -static void svcraw_destroy(); +static bool_t svcraw_recv(); +static enum xprt_stat svcraw_stat(); +static bool_t svcraw_getargs(); +static bool_t svcraw_reply(); +static bool_t svcraw_freeargs(); +static void svcraw_destroy(); static struct xp_ops server_ops = { svcraw_recv, @@ -69,13 +69,12 @@ static struct xp_ops server_ops = { svcraw_destroy }; -SVCXPRT * -svcraw_create() +SVCXPRT *svcraw_create() { register struct svcraw_private *srp = svcraw_private; if (srp == 0) { - srp = (struct svcraw_private *)calloc(1, sizeof (*srp)); + srp = (struct svcraw_private *) calloc(1, sizeof(*srp)); if (srp == 0) return (0); } @@ -87,17 +86,15 @@ svcraw_create() return (&srp->server); } -static enum xprt_stat -svcraw_stat() +static enum xprt_stat svcraw_stat() { return (XPRT_IDLE); } -static bool_t -svcraw_recv(xprt, msg) - SVCXPRT *xprt; - struct rpc_msg *msg; +static bool_t svcraw_recv(xprt, msg) +SVCXPRT *xprt; +struct rpc_msg *msg; { register struct svcraw_private *srp = svcraw_private; register XDR *xdrs; @@ -107,15 +104,14 @@ svcraw_recv(xprt, msg) xdrs = &srp->xdr_stream; xdrs->x_op = XDR_DECODE; XDR_SETPOS(xdrs, 0); - if (! xdr_callmsg(xdrs, msg)) - return (FALSE); + if (!xdr_callmsg(xdrs, msg)) + return (FALSE); return (TRUE); } -static bool_t -svcraw_reply(xprt, msg) - SVCXPRT *xprt; - struct rpc_msg *msg; +static bool_t svcraw_reply(xprt, msg) +SVCXPRT *xprt; +struct rpc_msg *msg; { register struct svcraw_private *srp = svcraw_private; register XDR *xdrs; @@ -125,31 +121,29 @@ svcraw_reply(xprt, msg) xdrs = &srp->xdr_stream; xdrs->x_op = XDR_ENCODE; XDR_SETPOS(xdrs, 0); - if (! xdr_replymsg(xdrs, msg)) - return (FALSE); - (void)XDR_GETPOS(xdrs); /* called just for overhead */ + if (!xdr_replymsg(xdrs, msg)) + return (FALSE); + (void) XDR_GETPOS(xdrs); /* called just for overhead */ return (TRUE); } -static bool_t -svcraw_getargs(xprt, xdr_args, args_ptr) - SVCXPRT *xprt; - xdrproc_t xdr_args; - caddr_t args_ptr; +static bool_t svcraw_getargs(xprt, xdr_args, args_ptr) +SVCXPRT *xprt; +xdrproc_t xdr_args; +caddr_t args_ptr; { register struct svcraw_private *srp = svcraw_private; if (srp == 0) return (FALSE); - return ((*xdr_args)(&srp->xdr_stream, args_ptr)); + return ((*xdr_args) (&srp->xdr_stream, args_ptr)); } -static bool_t -svcraw_freeargs(xprt, xdr_args, args_ptr) - SVCXPRT *xprt; - xdrproc_t xdr_args; - caddr_t args_ptr; -{ +static bool_t svcraw_freeargs(xprt, xdr_args, args_ptr) +SVCXPRT *xprt; +xdrproc_t xdr_args; +caddr_t args_ptr; +{ register struct svcraw_private *srp = svcraw_private; register XDR *xdrs; @@ -157,10 +151,9 @@ svcraw_freeargs(xprt, xdr_args, args_ptr) return (FALSE); xdrs = &srp->xdr_stream; xdrs->x_op = XDR_FREE; - return ((*xdr_args)(xdrs, args_ptr)); -} + return ((*xdr_args) (xdrs, args_ptr)); +} -static void -svcraw_destroy() +static void svcraw_destroy() { } diff --git a/libc/inet/rpc/svc_run.c b/libc/inet/rpc/svc_run.c index c1c3e0478..092fcb9a8 100644 --- a/libc/inet/rpc/svc_run.c +++ b/libc/inet/rpc/svc_run.c @@ -39,14 +39,13 @@ static char sccsid[] = "@(#)svc_run.c 1.1 87/10/13 Copyr 1984 Sun Micro"; #include #include -void -svc_run() +void svc_run() { #ifdef FD_SETSIZE fd_set readfds; #else - int readfds; -#endif /* def FD_SETSIZE */ + int readfds; +#endif /* def FD_SETSIZE */ extern int errno; for (;;) { @@ -54,9 +53,9 @@ svc_run() readfds = svc_fdset; #else readfds = svc_fds; -#endif /* def FD_SETSIZE */ - switch (select(_rpc_dtablesize(), &readfds, (int *)0, (int *)0, - (struct timeval *)0)) { +#endif /* def FD_SETSIZE */ + switch (select(_rpc_dtablesize(), &readfds, (int *) 0, (int *) 0, + (struct timeval *) 0)) { case -1: if (errno == EINTR) { continue; diff --git a/libc/inet/rpc/svc_simple.c b/libc/inet/rpc/svc_simple.c index d6bcbd3c0..3c75de3f0 100644 --- a/libc/inet/rpc/svc_simple.c +++ b/libc/inet/rpc/svc_simple.c @@ -28,7 +28,9 @@ * Mountain View, California 94043 */ #if !defined(lint) && defined(SCCSIDS) -static char sccsid[] = "@(#)svc_simple.c 1.18 87/08/11 Copyr 1984 Sun Micro"; +static char sccsid[] = + + "@(#)svc_simple.c 1.18 87/08/11 Copyr 1984 Sun Micro"; #endif /* @@ -44,9 +46,9 @@ static char sccsid[] = "@(#)svc_simple.c 1.18 87/08/11 Copyr 1984 Sun Micro"; #include static struct proglst { - char *(*p_progname)(); - int p_prognum; - int p_procnum; + char *(*p_progname) (); + int p_prognum; + int p_procnum; xdrproc_t p_inproc, p_outproc; struct proglst *p_nxt; } *proglst; @@ -55,13 +57,13 @@ static SVCXPRT *transp; struct proglst *pl; registerrpc(prognum, versnum, procnum, progname, inproc, outproc) - char *(*progname)(); - xdrproc_t inproc, outproc; +char *(*progname) (); +xdrproc_t inproc, outproc; { - + if (procnum == NULLPROC) { (void) fprintf(stderr, - "can't reassign procedure number %d\n", NULLPROC); + "can't reassign procedure number %d\n", NULLPROC); return (-1); } if (transp == 0) { @@ -71,14 +73,15 @@ registerrpc(prognum, versnum, procnum, progname, inproc, outproc) return (-1); } } - (void) pmap_unset((u_long)prognum, (u_long)versnum); - if (!svc_register(transp, (u_long)prognum, (u_long)versnum, - universal, IPPROTO_UDP)) { - (void) fprintf(stderr, "couldn't register prog %d vers %d\n", - prognum, versnum); + (void) pmap_unset((u_long) prognum, (u_long) versnum); + if (!svc_register(transp, (u_long) prognum, (u_long) versnum, + universal, IPPROTO_UDP)) { + (void) fprintf(stderr, "couldn't register prog %d vers %d\n", + prognum, versnum); return (-1); } - pl = (struct proglst *)malloc(sizeof(struct proglst)); + pl = (struct proglst *) malloc(sizeof(struct proglst)); + if (pl == NULL) { (void) fprintf(stderr, "registerrpc: out of memory\n"); return (-1); @@ -93,10 +96,9 @@ registerrpc(prognum, versnum, procnum, progname, inproc, outproc) return (0); } -static void -universal(rqstp, transp) - struct svc_req *rqstp; - SVCXPRT *transp; +static void universal(rqstp, transp) +struct svc_req *rqstp; +SVCXPRT *transp; { int prog, proc; char *outdata; @@ -107,7 +109,7 @@ universal(rqstp, transp) * enforce "procnum 0 is echo" convention */ if (rqstp->rq_proc == NULLPROC) { - if (svc_sendreply(transp, xdr_void, (char *)NULL) == FALSE) { + if (svc_sendreply(transp, xdr_void, (char *) NULL) == FALSE) { (void) fprintf(stderr, "xxx\n"); exit(1); } @@ -118,26 +120,25 @@ universal(rqstp, transp) for (pl = proglst; pl != NULL; pl = pl->p_nxt) if (pl->p_prognum == prog && pl->p_procnum == proc) { /* decode arguments into a CLEAN buffer */ - bzero(xdrbuf, sizeof(xdrbuf)); /* required ! */ + bzero(xdrbuf, sizeof(xdrbuf)); /* required ! */ if (!svc_getargs(transp, pl->p_inproc, xdrbuf)) { svcerr_decode(transp); return; } - outdata = (*(pl->p_progname))(xdrbuf); + outdata = (*(pl->p_progname)) (xdrbuf); if (outdata == NULL && pl->p_outproc != xdr_void) /* there was an error */ return; if (!svc_sendreply(transp, pl->p_outproc, outdata)) { (void) fprintf(stderr, - "trouble replying to prog %d\n", - pl->p_prognum); + "trouble replying to prog %d\n", + pl->p_prognum); exit(1); } /* free the decoded arguments */ - (void)svc_freeargs(transp, pl->p_inproc, xdrbuf); + (void) svc_freeargs(transp, pl->p_inproc, xdrbuf); return; } (void) fprintf(stderr, "never registered prog %d\n", prog); exit(1); } - diff --git a/libc/inet/rpc/svc_tcp.c b/libc/inet/rpc/svc_tcp.c index 837e70c82..0008eaa0c 100644 --- a/libc/inet/rpc/svc_tcp.c +++ b/libc/inet/rpc/svc_tcp.c @@ -51,12 +51,12 @@ extern errno; /* * Ops vector for TCP/IP based rpc service handle */ -static bool_t svctcp_recv(); -static enum xprt_stat svctcp_stat(); -static bool_t svctcp_getargs(); -static bool_t svctcp_reply(); -static bool_t svctcp_freeargs(); -static void svctcp_destroy(); +static bool_t svctcp_recv(); +static enum xprt_stat svctcp_stat(); +static bool_t svctcp_getargs(); +static bool_t svctcp_reply(); +static bool_t svctcp_freeargs(); +static void svctcp_destroy(); static struct xp_ops svctcp_op = { svctcp_recv, @@ -70,8 +70,8 @@ static struct xp_ops svctcp_op = { /* * Ops vector for TCP/IP rendezvous handler */ -static bool_t rendezvous_request(); -static enum xprt_stat rendezvous_stat(); +static bool_t rendezvous_request(); +static enum xprt_stat rendezvous_stat(); static struct xp_ops svctcp_rendezvous_op = { rendezvous_request, @@ -85,12 +85,12 @@ static struct xp_ops svctcp_rendezvous_op = { static int readtcp(), writetcp(); static SVCXPRT *makefd_xprt(); -struct tcp_rendezvous { /* kept in xprt->xp_p1 */ +struct tcp_rendezvous { /* kept in xprt->xp_p1 */ u_int sendsize; u_int recvsize; }; -struct tcp_conn { /* kept in xprt->xp_p1 */ +struct tcp_conn { /* kept in xprt->xp_p1 */ enum xprt_stat strm_stat; u_long x_id; XDR xdrs; @@ -117,11 +117,10 @@ struct tcp_conn { /* kept in xprt->xp_p1 */ * how big the send and receive buffers are via the second and third parms; * 0 => use the system default. */ -SVCXPRT * -svctcp_create(sock, sendsize, recvsize) - register int sock; - u_int sendsize; - u_int recvsize; +SVCXPRT *svctcp_create(sock, sendsize, recvsize) +register int sock; +u_int sendsize; +u_int recvsize; { bool_t madesock = FALSE; register SVCXPRT *xprt; @@ -132,37 +131,37 @@ svctcp_create(sock, sendsize, recvsize) if (sock == RPC_ANYSOCK) { if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) { perror("svc_tcp.c - tcp socket creation problem"); - return ((SVCXPRT *)NULL); + return ((SVCXPRT *) NULL); } madesock = TRUE; } - bzero((char *)&addr, sizeof (addr)); + bzero((char *) &addr, sizeof(addr)); addr.sin_family = AF_INET; if (bindresvport(sock, &addr)) { addr.sin_port = 0; - (void)bind(sock, (struct sockaddr *)&addr, len); + (void) bind(sock, (struct sockaddr *) &addr, len); } - if ((getsockname(sock, (struct sockaddr *)&addr, &len) != 0) || - (listen(sock, 2) != 0)) { + if ((getsockname(sock, (struct sockaddr *) &addr, &len) != 0) || + (listen(sock, 2) != 0)) { perror("svctcp_.c - cannot getsockname or listen"); if (madesock) - (void)close(sock); - return ((SVCXPRT *)NULL); + (void) close(sock); + return ((SVCXPRT *) NULL); } - r = (struct tcp_rendezvous *)mem_alloc(sizeof(*r)); + r = (struct tcp_rendezvous *) mem_alloc(sizeof(*r)); if (r == NULL) { (void) fprintf(stderr, "svctcp_create: out of memory\n"); return (NULL); } r->sendsize = sendsize; r->recvsize = recvsize; - xprt = (SVCXPRT *)mem_alloc(sizeof(SVCXPRT)); + xprt = (SVCXPRT *) mem_alloc(sizeof(SVCXPRT)); if (xprt == NULL) { (void) fprintf(stderr, "svctcp_create: out of memory\n"); return (NULL); } xprt->xp_p2 = NULL; - xprt->xp_p1 = (caddr_t)r; + xprt->xp_p1 = (caddr_t) r; xprt->xp_verf = _null_auth; xprt->xp_ops = &svctcp_rendezvous_op; xprt->xp_port = ntohs(addr.sin_port); @@ -175,69 +174,68 @@ svctcp_create(sock, sendsize, recvsize) * Like svtcp_create(), except the routine takes any *open* UNIX file * descriptor as its first input. */ -SVCXPRT * -svcfd_create(fd, sendsize, recvsize) - int fd; - u_int sendsize; - u_int recvsize; +SVCXPRT *svcfd_create(fd, sendsize, recvsize) +int fd; +u_int sendsize; +u_int recvsize; { return (makefd_xprt(fd, sendsize, recvsize)); } -static SVCXPRT * -makefd_xprt(fd, sendsize, recvsize) - int fd; - u_int sendsize; - u_int recvsize; +static SVCXPRT *makefd_xprt(fd, sendsize, recvsize) +int fd; +u_int sendsize; +u_int recvsize; { register SVCXPRT *xprt; register struct tcp_conn *cd; - - xprt = (SVCXPRT *)mem_alloc(sizeof(SVCXPRT)); - if (xprt == (SVCXPRT *)NULL) { + + xprt = (SVCXPRT *) mem_alloc(sizeof(SVCXPRT)); + if (xprt == (SVCXPRT *) NULL) { (void) fprintf(stderr, "svc_tcp: makefd_xprt: out of memory\n"); goto done; } - cd = (struct tcp_conn *)mem_alloc(sizeof(struct tcp_conn)); - if (cd == (struct tcp_conn *)NULL) { + cd = (struct tcp_conn *) mem_alloc(sizeof(struct tcp_conn)); + + if (cd == (struct tcp_conn *) NULL) { (void) fprintf(stderr, "svc_tcp: makefd_xprt: out of memory\n"); mem_free((char *) xprt, sizeof(SVCXPRT)); - xprt = (SVCXPRT *)NULL; + xprt = (SVCXPRT *) NULL; goto done; } cd->strm_stat = XPRT_IDLE; xdrrec_create(&(cd->xdrs), sendsize, recvsize, - (caddr_t)xprt, readtcp, writetcp); + (caddr_t) xprt, readtcp, writetcp); xprt->xp_p2 = NULL; - xprt->xp_p1 = (caddr_t)cd; + xprt->xp_p1 = (caddr_t) cd; xprt->xp_verf.oa_base = cd->verf_body; xprt->xp_addrlen = 0; - xprt->xp_ops = &svctcp_op; /* truely deals with calls */ - xprt->xp_port = 0; /* this is a connection, not a rendezvouser */ + xprt->xp_ops = &svctcp_op; /* truely deals with calls */ + xprt->xp_port = 0; /* this is a connection, not a rendezvouser */ xprt->xp_sock = fd; xprt_register(xprt); - done: + done: return (xprt); } -static bool_t -rendezvous_request(xprt) - register SVCXPRT *xprt; +static bool_t rendezvous_request(xprt) +register SVCXPRT *xprt; { int sock; struct tcp_rendezvous *r; struct sockaddr_in addr; int len; - r = (struct tcp_rendezvous *)xprt->xp_p1; - again: + r = (struct tcp_rendezvous *) xprt->xp_p1; + again: len = sizeof(struct sockaddr_in); - if ((sock = accept(xprt->xp_sock, (struct sockaddr *)&addr, - &len)) < 0) { + + if ((sock = accept(xprt->xp_sock, (struct sockaddr *) &addr, + &len)) < 0) { if (errno == EINTR) goto again; - return (FALSE); + return (FALSE); } /* * make a new transporter (re-uses xprt) @@ -245,24 +243,22 @@ rendezvous_request(xprt) xprt = makefd_xprt(sock, r->sendsize, r->recvsize); xprt->xp_raddr = addr; xprt->xp_addrlen = len; - return (FALSE); /* there is never an rpc msg to be processed */ + return (FALSE); /* there is never an rpc msg to be processed */ } -static enum xprt_stat -rendezvous_stat() +static enum xprt_stat rendezvous_stat() { return (XPRT_IDLE); } -static void -svctcp_destroy(xprt) - register SVCXPRT *xprt; +static void svctcp_destroy(xprt) +register SVCXPRT *xprt; { - register struct tcp_conn *cd = (struct tcp_conn *)xprt->xp_p1; + register struct tcp_conn *cd = (struct tcp_conn *) xprt->xp_p1; xprt_unregister(xprt); - (void)close(xprt->xp_sock); + (void) close(xprt->xp_sock); if (xprt->xp_port != 0) { /* a rendezvouser socket */ xprt->xp_port = 0; @@ -270,8 +266,9 @@ svctcp_destroy(xprt) /* an actual connection socket */ XDR_DESTROY(&(cd->xdrs)); } - mem_free((caddr_t)cd, sizeof(struct tcp_conn)); - mem_free((caddr_t)xprt, sizeof(SVCXPRT)); + mem_free((caddr_t) cd, sizeof(struct tcp_conn)); + + mem_free((caddr_t) xprt, sizeof(SVCXPRT)); } /* @@ -285,13 +282,13 @@ static struct timeval wait_per_try = { 35, 0 }; * any error is fatal and the connection is closed. * (And a read of zero bytes is a half closed stream => error.) */ -static int -readtcp(xprt, buf, len) - register SVCXPRT *xprt; - caddr_t buf; - register int len; +static int readtcp(xprt, buf, len) +register SVCXPRT *xprt; +caddr_t buf; +register int len; { register int sock = xprt->xp_sock; + #ifdef FD_SETSIZE fd_set mask; fd_set readfds; @@ -301,11 +298,11 @@ readtcp(xprt, buf, len) #else register int mask = 1 << sock; int readfds; -#endif /* def FD_SETSIZE */ +#endif /* def FD_SETSIZE */ do { readfds = mask; - if (select(_rpc_dtablesize(), &readfds, (int*)NULL, (int*)NULL, - &wait_per_try) <= 0) { + if (select(_rpc_dtablesize(), &readfds, (int *) NULL, (int *) NULL, + &wait_per_try) <= 0) { if (errno == EINTR) { continue; } @@ -315,12 +312,12 @@ readtcp(xprt, buf, len) } while (!FD_ISSET(sock, &readfds)); #else } while (readfds != mask); -#endif /* def FD_SETSIZE */ +#endif /* def FD_SETSIZE */ if ((len = read(sock, buf, len)) > 0) { return (len); } -fatal_err: - ((struct tcp_conn *)(xprt->xp_p1))->strm_stat = XPRT_DIED; + fatal_err: + ((struct tcp_conn *) (xprt->xp_p1))->strm_stat = XPRT_DIED; return (-1); } @@ -328,49 +325,43 @@ fatal_err: * writes data to the tcp connection. * Any error is fatal and the connection is closed. */ -static int -writetcp(xprt, buf, len) - register SVCXPRT *xprt; - caddr_t buf; - int len; +static int writetcp(xprt, buf, len) +register SVCXPRT *xprt; +caddr_t buf; +int len; { register int i, cnt; for (cnt = len; cnt > 0; cnt -= i, buf += i) { if ((i = write(xprt->xp_sock, buf, cnt)) < 0) { - ((struct tcp_conn *)(xprt->xp_p1))->strm_stat = - XPRT_DIED; + ((struct tcp_conn *) (xprt->xp_p1))->strm_stat = XPRT_DIED; return (-1); } } return (len); } -static enum xprt_stat -svctcp_stat(xprt) - SVCXPRT *xprt; +static enum xprt_stat svctcp_stat(xprt) +SVCXPRT *xprt; { - register struct tcp_conn *cd = - (struct tcp_conn *)(xprt->xp_p1); + register struct tcp_conn *cd = (struct tcp_conn *) (xprt->xp_p1); if (cd->strm_stat == XPRT_DIED) return (XPRT_DIED); - if (! xdrrec_eof(&(cd->xdrs))) + if (!xdrrec_eof(&(cd->xdrs))) return (XPRT_MOREREQS); return (XPRT_IDLE); } -static bool_t -svctcp_recv(xprt, msg) - SVCXPRT *xprt; - register struct rpc_msg *msg; +static bool_t svctcp_recv(xprt, msg) +SVCXPRT *xprt; +register struct rpc_msg *msg; { - register struct tcp_conn *cd = - (struct tcp_conn *)(xprt->xp_p1); + register struct tcp_conn *cd = (struct tcp_conn *) (xprt->xp_p1); register XDR *xdrs = &(cd->xdrs); xdrs->x_op = XDR_DECODE; - (void)xdrrec_skiprecord(xdrs); + (void) xdrrec_skiprecord(xdrs); if (xdr_callmsg(xdrs, msg)) { cd->x_id = msg->rm_xid; return (TRUE); @@ -378,42 +369,38 @@ svctcp_recv(xprt, msg) return (FALSE); } -static bool_t -svctcp_getargs(xprt, xdr_args, args_ptr) - SVCXPRT *xprt; - xdrproc_t xdr_args; - caddr_t args_ptr; +static bool_t svctcp_getargs(xprt, xdr_args, args_ptr) +SVCXPRT *xprt; +xdrproc_t xdr_args; +caddr_t args_ptr; { - return ((*xdr_args)(&(((struct tcp_conn *)(xprt->xp_p1))->xdrs), args_ptr)); + return ((*xdr_args) + (&(((struct tcp_conn *) (xprt->xp_p1))->xdrs), args_ptr)); } -static bool_t -svctcp_freeargs(xprt, xdr_args, args_ptr) - SVCXPRT *xprt; - xdrproc_t xdr_args; - caddr_t args_ptr; +static bool_t svctcp_freeargs(xprt, xdr_args, args_ptr) +SVCXPRT *xprt; +xdrproc_t xdr_args; +caddr_t args_ptr; { - register XDR *xdrs = - &(((struct tcp_conn *)(xprt->xp_p1))->xdrs); + register XDR *xdrs = &(((struct tcp_conn *) (xprt->xp_p1))->xdrs); xdrs->x_op = XDR_FREE; - return ((*xdr_args)(xdrs, args_ptr)); + return ((*xdr_args) (xdrs, args_ptr)); } -static bool_t -svctcp_reply(xprt, msg) - SVCXPRT *xprt; - register struct rpc_msg *msg; +static bool_t svctcp_reply(xprt, msg) +SVCXPRT *xprt; +register struct rpc_msg *msg; { - register struct tcp_conn *cd = - (struct tcp_conn *)(xprt->xp_p1); + register struct tcp_conn *cd = (struct tcp_conn *) (xprt->xp_p1); register XDR *xdrs = &(cd->xdrs); register bool_t stat; xdrs->x_op = XDR_ENCODE; msg->rm_xid = cd->x_id; stat = xdr_replymsg(xdrs, msg); - (void)xdrrec_endofrecord(xdrs, TRUE); + (void) xdrrec_endofrecord(xdrs, TRUE); return (stat); } diff --git a/libc/inet/rpc/svc_udp.c b/libc/inet/rpc/svc_udp.c index 69ef7a1ce..b8495a127 100644 --- a/libc/inet/rpc/svc_udp.c +++ b/libc/inet/rpc/svc_udp.c @@ -48,12 +48,12 @@ static char sccsid[] = "@(#)svc_udp.c 1.24 87/08/11 Copyr 1984 Sun Micro"; #define rpc_buffer(xprt) ((xprt)->xp_p1) #define MAX(a, b) ((a > b) ? a : b) -static bool_t svcudp_recv(); -static bool_t svcudp_reply(); -static enum xprt_stat svcudp_stat(); -static bool_t svcudp_getargs(); -static bool_t svcudp_freeargs(); -static void svcudp_destroy(); +static bool_t svcudp_recv(); +static bool_t svcudp_reply(); +static enum xprt_stat svcudp_stat(); +static bool_t svcudp_getargs(); +static bool_t svcudp_freeargs(); +static void svcudp_destroy(); static struct xp_ops svcudp_op = { svcudp_recv, @@ -70,12 +70,13 @@ extern int errno; * kept in xprt->xp_p2 */ struct svcudp_data { - u_int su_iosz; /* byte size of send.recv buffer */ - u_long su_xid; /* transaction id */ - XDR su_xdrs; /* XDR handle */ - char su_verfbody[MAX_AUTH_BYTES]; /* verifier body */ - char * su_cache; /* cached data, NULL if no cache */ + u_int su_iosz; /* byte size of send.recv buffer */ + u_long su_xid; /* transaction id */ + XDR su_xdrs; /* XDR handle */ + char su_verfbody[MAX_AUTH_BYTES]; /* verifier body */ + char *su_cache; /* cached data, NULL if no cache */ }; + #define su_data(xprt) ((struct svcudp_data *)(xprt->xp_p2)) /* @@ -91,10 +92,9 @@ struct svcudp_data { * see (svc.h, xprt_register). * The routines returns NULL if a problem occurred. */ -SVCXPRT * -svcudp_bufcreate(sock, sendsz, recvsz) - register int sock; - u_int sendsz, recvsz; +SVCXPRT *svcudp_bufcreate(sock, sendsz, recvsz) +register int sock; +u_int sendsz, recvsz; { bool_t madesock = FALSE; register SVCXPRT *xprt; @@ -105,41 +105,41 @@ svcudp_bufcreate(sock, sendsz, recvsz) if (sock == RPC_ANYSOCK) { if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) { perror("svcudp_create: socket creation problem"); - return ((SVCXPRT *)NULL); + return ((SVCXPRT *) NULL); } madesock = TRUE; } - bzero((char *)&addr, sizeof (addr)); + bzero((char *) &addr, sizeof(addr)); addr.sin_family = AF_INET; if (bindresvport(sock, &addr)) { addr.sin_port = 0; - (void)bind(sock, (struct sockaddr *)&addr, len); + (void) bind(sock, (struct sockaddr *) &addr, len); } - if (getsockname(sock, (struct sockaddr *)&addr, &len) != 0) { + if (getsockname(sock, (struct sockaddr *) &addr, &len) != 0) { perror("svcudp_create - cannot getsockname"); if (madesock) - (void)close(sock); - return ((SVCXPRT *)NULL); + (void) close(sock); + return ((SVCXPRT *) NULL); } - xprt = (SVCXPRT *)mem_alloc(sizeof(SVCXPRT)); + xprt = (SVCXPRT *) mem_alloc(sizeof(SVCXPRT)); if (xprt == NULL) { - (void)fprintf(stderr, "svcudp_create: out of memory\n"); + (void) fprintf(stderr, "svcudp_create: out of memory\n"); return (NULL); } - su = (struct svcudp_data *)mem_alloc(sizeof(*su)); + su = (struct svcudp_data *) mem_alloc(sizeof(*su)); if (su == NULL) { - (void)fprintf(stderr, "svcudp_create: out of memory\n"); + (void) fprintf(stderr, "svcudp_create: out of memory\n"); return (NULL); } su->su_iosz = ((MAX(sendsz, recvsz) + 3) / 4) * 4; if ((rpc_buffer(xprt) = mem_alloc(su->su_iosz)) == NULL) { - (void)fprintf(stderr, "svcudp_create: out of memory\n"); + (void) fprintf(stderr, "svcudp_create: out of memory\n"); return (NULL); } - xdrmem_create( - &(su->su_xdrs), rpc_buffer(xprt), su->su_iosz, XDR_DECODE); + xdrmem_create(&(su->su_xdrs), rpc_buffer(xprt), su->su_iosz, + XDR_DECODE); su->su_cache = NULL; - xprt->xp_p2 = (caddr_t)su; + xprt->xp_p2 = (caddr_t) su; xprt->xp_verf.oa_base = su->su_verfbody; xprt->xp_ops = &svcudp_op; xprt->xp_port = ntohs(addr.sin_port); @@ -148,26 +148,23 @@ svcudp_bufcreate(sock, sendsz, recvsz) return (xprt); } -SVCXPRT * -svcudp_create(sock) - int sock; +SVCXPRT *svcudp_create(sock) +int sock; { - return(svcudp_bufcreate(sock, UDPMSGSIZE, UDPMSGSIZE)); + return (svcudp_bufcreate(sock, UDPMSGSIZE, UDPMSGSIZE)); } -static enum xprt_stat -svcudp_stat(xprt) - SVCXPRT *xprt; +static enum xprt_stat svcudp_stat(xprt) +SVCXPRT *xprt; { - return (XPRT_IDLE); + return (XPRT_IDLE); } -static bool_t -svcudp_recv(xprt, msg) - register SVCXPRT *xprt; - struct rpc_msg *msg; +static bool_t svcudp_recv(xprt, msg) +register SVCXPRT *xprt; +struct rpc_msg *msg; { register struct svcudp_data *su = su_data(xprt); register XDR *xdrs = &(su->su_xdrs); @@ -175,33 +172,35 @@ svcudp_recv(xprt, msg) char *reply; u_long replylen; - again: + again: xprt->xp_addrlen = sizeof(struct sockaddr_in); + rlen = recvfrom(xprt->xp_sock, rpc_buffer(xprt), (int) su->su_iosz, - 0, (struct sockaddr *)&(xprt->xp_raddr), &(xprt->xp_addrlen)); + 0, (struct sockaddr *) &(xprt->xp_raddr), + &(xprt->xp_addrlen)); if (rlen == -1 && errno == EINTR) goto again; - if (rlen < 4*sizeof(u_long)) + if (rlen < 4 * sizeof(u_long)) return (FALSE); xdrs->x_op = XDR_DECODE; XDR_SETPOS(xdrs, 0); - if (! xdr_callmsg(xdrs, msg)) + if (!xdr_callmsg(xdrs, msg)) return (FALSE); su->su_xid = msg->rm_xid; if (su->su_cache != NULL) { if (cache_get(xprt, msg, &reply, &replylen)) { (void) sendto(xprt->xp_sock, reply, (int) replylen, 0, - (struct sockaddr *) &xprt->xp_raddr, xprt->xp_addrlen); + (struct sockaddr *) &xprt->xp_raddr, + xprt->xp_addrlen); return (TRUE); } } return (TRUE); } -static bool_t -svcudp_reply(xprt, msg) - register SVCXPRT *xprt; - struct rpc_msg *msg; +static bool_t svcudp_reply(xprt, msg) +register SVCXPRT *xprt; +struct rpc_msg *msg; { register struct svcudp_data *su = su_data(xprt); register XDR *xdrs = &(su->su_xdrs); @@ -212,10 +211,10 @@ svcudp_reply(xprt, msg) XDR_SETPOS(xdrs, 0); msg->rm_xid = su->su_xid; if (xdr_replymsg(xdrs, msg)) { - slen = (int)XDR_GETPOS(xdrs); + slen = (int) XDR_GETPOS(xdrs); if (sendto(xprt->xp_sock, rpc_buffer(xprt), slen, 0, - (struct sockaddr *)&(xprt->xp_raddr), xprt->xp_addrlen) - == slen) { + (struct sockaddr *) &(xprt->xp_raddr), xprt->xp_addrlen) + == slen) { stat = TRUE; if (su->su_cache && slen >= 0) { cache_set(xprt, (u_long) slen); @@ -225,40 +224,38 @@ svcudp_reply(xprt, msg) return (stat); } -static bool_t -svcudp_getargs(xprt, xdr_args, args_ptr) - SVCXPRT *xprt; - xdrproc_t xdr_args; - caddr_t args_ptr; +static bool_t svcudp_getargs(xprt, xdr_args, args_ptr) +SVCXPRT *xprt; +xdrproc_t xdr_args; +caddr_t args_ptr; { - return ((*xdr_args)(&(su_data(xprt)->su_xdrs), args_ptr)); + return ((*xdr_args) (&(su_data(xprt)->su_xdrs), args_ptr)); } -static bool_t -svcudp_freeargs(xprt, xdr_args, args_ptr) - SVCXPRT *xprt; - xdrproc_t xdr_args; - caddr_t args_ptr; +static bool_t svcudp_freeargs(xprt, xdr_args, args_ptr) +SVCXPRT *xprt; +xdrproc_t xdr_args; +caddr_t args_ptr; { register XDR *xdrs = &(su_data(xprt)->su_xdrs); xdrs->x_op = XDR_FREE; - return ((*xdr_args)(xdrs, args_ptr)); + return ((*xdr_args) (xdrs, args_ptr)); } -static void -svcudp_destroy(xprt) - register SVCXPRT *xprt; +static void svcudp_destroy(xprt) +register SVCXPRT *xprt; { register struct svcudp_data *su = su_data(xprt); xprt_unregister(xprt); - (void)close(xprt->xp_sock); + (void) close(xprt->xp_sock); XDR_DESTROY(&(su->su_xdrs)); mem_free(rpc_buffer(xprt), su->su_iosz); - mem_free((caddr_t)su, sizeof(struct svcudp_data)); - mem_free((caddr_t)xprt, sizeof(SVCXPRT)); + mem_free((caddr_t) su, sizeof(struct svcudp_data)); + + mem_free((caddr_t) xprt, sizeof(SVCXPRT)); } @@ -270,7 +267,7 @@ svcudp_destroy(xprt) * Buffers are sent again if retransmissions are detected. */ -#define SPARSENESS 4 /* 75% sparse */ +#define SPARSENESS 4 /* 75% sparse */ #define CACHE_PERROR(msg) \ (void) fprintf(stderr,"%s\n", msg) @@ -279,7 +276,7 @@ svcudp_destroy(xprt) (type *) mem_alloc((unsigned) (sizeof(type) * (size))) #define BZERO(addr, type, size) \ - bzero((char *) addr, sizeof(type) * (int) (size)) + bzero((char *) addr, sizeof(type) * (int) (size)) /* * An entry in the cache @@ -297,12 +294,12 @@ struct cache_node { /* * The cached reply and length */ - char * cache_reply; + char *cache_reply; u_long cache_replylen; /* - * Next node on the list, if there is a collision + * Next node on the list, if there is a collision */ - cache_ptr cache_next; + cache_ptr cache_next; }; @@ -311,14 +308,14 @@ struct cache_node { * The entire cache */ struct udp_cache { - u_long uc_size; /* size of cache */ - cache_ptr *uc_entries; /* hash table of entries in cache */ - cache_ptr *uc_fifo; /* fifo list of entries in cache */ - u_long uc_nextvictim; /* points to next victim in fifo list */ - u_long uc_prog; /* saved program number */ - u_long uc_vers; /* saved version number */ - u_long uc_proc; /* saved procedure number */ - struct sockaddr_in uc_addr; /* saved caller's address */ + u_long uc_size; /* size of cache */ + cache_ptr *uc_entries; /* hash table of entries in cache */ + cache_ptr *uc_fifo; /* fifo list of entries in cache */ + u_long uc_nextvictim; /* points to next victim in fifo list */ + u_long uc_prog; /* saved program number */ + u_long uc_vers; /* saved version number */ + u_long uc_proc; /* saved procedure number */ + struct sockaddr_in uc_addr; /* saved caller's address */ }; @@ -326,7 +323,7 @@ struct udp_cache { * the hashing function */ #define CACHE_LOC(transp, xid) \ - (xid % (SPARSENESS*((struct udp_cache *) su_data(transp)->su_cache)->uc_size)) + (xid % (SPARSENESS*((struct udp_cache *) su_data(transp)->su_cache)->uc_size)) /* @@ -334,49 +331,48 @@ struct udp_cache { * Note: there is no disable. */ svcudp_enablecache(transp, size) - SVCXPRT *transp; - u_long size; +SVCXPRT *transp; +u_long size; { struct svcudp_data *su = su_data(transp); struct udp_cache *uc; if (su->su_cache != NULL) { CACHE_PERROR("enablecache: cache already enabled"); - return(0); + return (0); } uc = ALLOC(struct udp_cache, 1); if (uc == NULL) { CACHE_PERROR("enablecache: could not allocate cache"); - return(0); + return (0); } uc->uc_size = size; uc->uc_nextvictim = 0; uc->uc_entries = ALLOC(cache_ptr, size * SPARSENESS); if (uc->uc_entries == NULL) { CACHE_PERROR("enablecache: could not allocate cache data"); - return(0); + return (0); } BZERO(uc->uc_entries, cache_ptr, size * SPARSENESS); uc->uc_fifo = ALLOC(cache_ptr, size); if (uc->uc_fifo == NULL) { CACHE_PERROR("enablecache: could not allocate cache fifo"); - return(0); + return (0); } BZERO(uc->uc_fifo, cache_ptr, size); su->su_cache = (char *) uc; - return(1); + return (1); } /* * Set an entry in the cache */ -static -cache_set(xprt, replylen) - SVCXPRT *xprt; - u_long replylen; +static cache_set(xprt, replylen) +SVCXPRT *xprt; +u_long replylen; { - register cache_ptr victim; + register cache_ptr victim; register cache_ptr *vicp; register struct svcudp_data *su = su_data(xprt); struct udp_cache *uc = (struct udp_cache *) su->su_cache; @@ -384,16 +380,15 @@ cache_set(xprt, replylen) char *newbuf; /* - * Find space for the new entry, either by + * Find space for the new entry, either by * reusing an old entry, or by mallocing a new one */ victim = uc->uc_fifo[uc->uc_nextvictim]; if (victim != NULL) { loc = CACHE_LOC(xprt, victim->cache_xid); - for (vicp = &uc->uc_entries[loc]; - *vicp != NULL && *vicp != victim; - vicp = &(*vicp)->cache_next) - ; + for (vicp = &uc->uc_entries[loc]; + *vicp != NULL && *vicp != victim; + vicp = &(*vicp)->cache_next); if (*vicp == NULL) { CACHE_PERROR("cache_set: victim not found"); return; @@ -402,6 +397,7 @@ cache_set(xprt, replylen) newbuf = victim->cache_reply; } else { victim = ALLOC(struct cache_node, 1); + if (victim == NULL) { CACHE_PERROR("cache_set: victim alloc failed"); return; @@ -419,14 +415,15 @@ cache_set(xprt, replylen) victim->cache_replylen = replylen; victim->cache_reply = rpc_buffer(xprt); rpc_buffer(xprt) = newbuf; - xdrmem_create(&(su->su_xdrs), rpc_buffer(xprt), su->su_iosz, XDR_ENCODE); + xdrmem_create(&(su->su_xdrs), rpc_buffer(xprt), su->su_iosz, + XDR_ENCODE); victim->cache_xid = su->su_xid; victim->cache_proc = uc->uc_proc; victim->cache_vers = uc->uc_vers; victim->cache_prog = uc->uc_prog; victim->cache_addr = uc->uc_addr; loc = CACHE_LOC(xprt, victim->cache_xid); - victim->cache_next = uc->uc_entries[loc]; + victim->cache_next = uc->uc_entries[loc]; uc->uc_entries[loc] = victim; uc->uc_fifo[uc->uc_nextvictim++] = victim; uc->uc_nextvictim %= uc->uc_size; @@ -436,12 +433,11 @@ cache_set(xprt, replylen) * Try to get an entry from the cache * return 1 if found, 0 if not found */ -static -cache_get(xprt, msg, replyp, replylenp) - SVCXPRT *xprt; - struct rpc_msg *msg; - char **replyp; - u_long *replylenp; +static cache_get(xprt, msg, replyp, replylenp) +SVCXPRT *xprt; +struct rpc_msg *msg; +char **replyp; +u_long *replylenp; { u_int loc; register cache_ptr ent; @@ -453,13 +449,13 @@ cache_get(xprt, msg, replyp, replylenp) loc = CACHE_LOC(xprt, su->su_xid); for (ent = uc->uc_entries[loc]; ent != NULL; ent = ent->cache_next) { if (ent->cache_xid == su->su_xid && - ent->cache_proc == uc->uc_proc && - ent->cache_vers == uc->uc_vers && - ent->cache_prog == uc->uc_prog && - EQADDR(ent->cache_addr, uc->uc_addr)) { + ent->cache_proc == uc->uc_proc && + ent->cache_vers == uc->uc_vers && + ent->cache_prog == uc->uc_prog && + EQADDR(ent->cache_addr, uc->uc_addr)) { *replyp = ent->cache_reply; *replylenp = ent->cache_replylen; - return(1); + return (1); } } /* @@ -470,6 +466,5 @@ cache_get(xprt, msg, replyp, replylenp) uc->uc_vers = msg->rm_call.cb_vers; uc->uc_prog = msg->rm_call.cb_prog; uc->uc_addr = xprt->xp_raddr; - return(0); + return (0); } - diff --git a/libc/inet/rpc/xdr.c b/libc/inet/rpc/xdr.c index cc2783813..88600732a 100644 --- a/libc/inet/rpc/xdr.c +++ b/libc/inet/rpc/xdr.c @@ -62,22 +62,20 @@ static char xdr_zero[BYTES_PER_XDR_UNIT] = { 0, 0, 0, 0 }; * Free a data structure using XDR * Not a filter, but a convenient utility nonetheless */ -void -xdr_free(proc, objp) - xdrproc_t proc; - char *objp; +void xdr_free(proc, objp) +xdrproc_t proc; +char *objp; { XDR x; - + x.x_op = XDR_FREE; - (*proc)(&x, objp); + (*proc) (&x, objp); } /* * XDR nothing */ -bool_t -xdr_void(/* xdrs, addr */) +bool_t xdr_void( /* xdrs, addr */ ) /* XDR *xdrs; */ /* caddr_t addr; */ { @@ -88,20 +86,19 @@ xdr_void(/* xdrs, addr */) /* * XDR integers */ -bool_t -xdr_int(xdrs, ip) - XDR *xdrs; - int *ip; +bool_t xdr_int(xdrs, ip) +XDR *xdrs; +int *ip; { #ifdef lint - (void) (xdr_short(xdrs, (short *)ip)); - return (xdr_long(xdrs, (long *)ip)); + (void) (xdr_short(xdrs, (short *) ip)); + return (xdr_long(xdrs, (long *) ip)); #else - if (sizeof (int) == sizeof (long)) { - return (xdr_long(xdrs, (long *)ip)); + if (sizeof(int) == sizeof(long)) { + return (xdr_long(xdrs, (long *) ip)); } else { - return (xdr_short(xdrs, (short *)ip)); + return (xdr_short(xdrs, (short *) ip)); } #endif } @@ -109,20 +106,19 @@ xdr_int(xdrs, ip) /* * XDR unsigned integers */ -bool_t -xdr_u_int(xdrs, up) - XDR *xdrs; - u_int *up; +bool_t xdr_u_int(xdrs, up) +XDR *xdrs; +u_int *up; { #ifdef lint - (void) (xdr_short(xdrs, (short *)up)); - return (xdr_u_long(xdrs, (u_long *)up)); + (void) (xdr_short(xdrs, (short *) up)); + return (xdr_u_long(xdrs, (u_long *) up)); #else - if (sizeof (u_int) == sizeof (u_long)) { - return (xdr_u_long(xdrs, (u_long *)up)); + if (sizeof(u_int) == sizeof(u_long)) { + return (xdr_u_long(xdrs, (u_long *) up)); } else { - return (xdr_short(xdrs, (short *)up)); + return (xdr_short(xdrs, (short *) up)); } #endif } @@ -131,10 +127,9 @@ xdr_u_int(xdrs, up) * XDR long integers * same as xdr_u_long - open coded to save a proc call! */ -bool_t -xdr_long(xdrs, lp) - register XDR *xdrs; - long *lp; +bool_t xdr_long(xdrs, lp) +register XDR *xdrs; +long *lp; { if (xdrs->x_op == XDR_ENCODE) @@ -153,16 +148,15 @@ xdr_long(xdrs, lp) * XDR unsigned long integers * same as xdr_long - open coded to save a proc call! */ -bool_t -xdr_u_long(xdrs, ulp) - register XDR *xdrs; - u_long *ulp; +bool_t xdr_u_long(xdrs, ulp) +register XDR *xdrs; +u_long *ulp; { if (xdrs->x_op == XDR_DECODE) - return (XDR_GETLONG(xdrs, (long *)ulp)); + return (XDR_GETLONG(xdrs, (long *) ulp)); if (xdrs->x_op == XDR_ENCODE) - return (XDR_PUTLONG(xdrs, (long *)ulp)); + return (XDR_PUTLONG(xdrs, (long *) ulp)); if (xdrs->x_op == XDR_FREE) return (TRUE); return (FALSE); @@ -171,10 +165,9 @@ xdr_u_long(xdrs, ulp) /* * XDR short integers */ -bool_t -xdr_short(xdrs, sp) - register XDR *xdrs; - short *sp; +bool_t xdr_short(xdrs, sp) +register XDR *xdrs; +short *sp; { long l; @@ -200,17 +193,16 @@ xdr_short(xdrs, sp) /* * XDR unsigned short integers */ -bool_t -xdr_u_short(xdrs, usp) - register XDR *xdrs; - u_short *usp; +bool_t xdr_u_short(xdrs, usp) +register XDR *xdrs; +u_short *usp; { u_long l; switch (xdrs->x_op) { case XDR_ENCODE: - l = (u_long) *usp; + l = (u_long) * usp; return (XDR_PUTLONG(xdrs, &l)); case XDR_DECODE: @@ -230,10 +222,9 @@ xdr_u_short(xdrs, usp) /* * XDR a char */ -bool_t -xdr_char(xdrs, cp) - XDR *xdrs; - char *cp; +bool_t xdr_char(xdrs, cp) +XDR *xdrs; +char *cp; { int i; @@ -248,10 +239,9 @@ xdr_char(xdrs, cp) /* * XDR an unsigned char */ -bool_t -xdr_u_char(xdrs, cp) - XDR *xdrs; - char *cp; +bool_t xdr_u_char(xdrs, cp) +XDR *xdrs; +char *cp; { u_int u; @@ -266,10 +256,9 @@ xdr_u_char(xdrs, cp) /* * XDR booleans */ -bool_t -xdr_bool(xdrs, bp) - register XDR *xdrs; - bool_t *bp; +bool_t xdr_bool(xdrs, bp) +register XDR *xdrs; +bool_t *bp; { long lb; @@ -295,10 +284,9 @@ xdr_bool(xdrs, bp) /* * XDR enumerations */ -bool_t -xdr_enum(xdrs, ep) - XDR *xdrs; - enum_t *ep; +bool_t xdr_enum(xdrs, ep) +XDR *xdrs; +enum_t *ep; { #ifndef lint enum sizecheck { SIZEVAL }; /* used to find the size of an enum */ @@ -306,16 +294,16 @@ xdr_enum(xdrs, ep) /* * enums are treated as ints */ - if (sizeof (enum sizecheck) == sizeof (long)) { - return (xdr_long(xdrs, (long *)ep)); - } else if (sizeof (enum sizecheck) == sizeof (short)) { - return (xdr_short(xdrs, (short *)ep)); + if (sizeof(enum sizecheck) == sizeof(long)) { + return (xdr_long(xdrs, (long *) ep)); + } else if (sizeof(enum sizecheck) == sizeof(short)) { + return (xdr_short(xdrs, (short *) ep)); } else { return (FALSE); } #else - (void) (xdr_short(xdrs, (short *)ep)); - return (xdr_long(xdrs, (long *)ep)); + (void) (xdr_short(xdrs, (short *) ep)); + return (xdr_long(xdrs, (long *) ep)); #endif } @@ -324,11 +312,10 @@ xdr_enum(xdrs, ep) * Allows the specification of a fixed size sequence of opaque bytes. * cp points to the opaque object and cnt gives the byte length. */ -bool_t -xdr_opaque(xdrs, cp, cnt) - register XDR *xdrs; - caddr_t cp; - register u_int cnt; +bool_t xdr_opaque(xdrs, cp, cnt) +register XDR *xdrs; +caddr_t cp; +register u_int cnt; { register u_int rndup; static crud[BYTES_PER_XDR_UNIT]; @@ -376,20 +363,19 @@ xdr_opaque(xdrs, cp, cnt) * *cpp is a pointer to the bytes, *sizep is the count. * If *cpp is NULL maxsize bytes are allocated */ -bool_t -xdr_bytes(xdrs, cpp, sizep, maxsize) - register XDR *xdrs; - char **cpp; - register u_int *sizep; - u_int maxsize; +bool_t xdr_bytes(xdrs, cpp, sizep, maxsize) +register XDR *xdrs; +char **cpp; +register u_int *sizep; +u_int maxsize; { - register char *sp = *cpp; /* sp is the actual string pointer */ + register char *sp = *cpp; /* sp is the actual string pointer */ register u_int nodesize; /* * first deal with the length since xdr bytes are counted */ - if (! xdr_u_int(xdrs, sizep)) { + if (!xdr_u_int(xdrs, sizep)) { return (FALSE); } nodesize = *sizep; @@ -407,7 +393,7 @@ xdr_bytes(xdrs, cpp, sizep, maxsize) return (TRUE); } if (sp == NULL) { - *cpp = sp = (char *)mem_alloc(nodesize); + *cpp = sp = (char *) mem_alloc(nodesize); } if (sp == NULL) { (void) fprintf(stderr, "xdr_bytes: out of memory\n"); @@ -431,10 +417,9 @@ xdr_bytes(xdrs, cpp, sizep, maxsize) /* * Implemented here due to commonality of the object. */ -bool_t -xdr_netobj(xdrs, np) - XDR *xdrs; - struct netobj *np; +bool_t xdr_netobj(xdrs, np) +XDR *xdrs; +struct netobj *np; { return (xdr_bytes(xdrs, &np->n_bytes, &np->n_len, MAX_NETOBJ_SZ)); @@ -451,20 +436,19 @@ xdr_netobj(xdrs, np) * routine may be called. * If there is no specific or default routine an error is returned. */ -bool_t -xdr_union(xdrs, dscmp, unp, choices, dfault) - register XDR *xdrs; - enum_t *dscmp; /* enum to decide which arm to work on */ - char *unp; /* the union itself */ - struct xdr_discrim *choices; /* [value, xdr proc] for each arm */ - xdrproc_t dfault; /* default xdr routine */ +bool_t xdr_union(xdrs, dscmp, unp, choices, dfault) +register XDR *xdrs; +enum_t *dscmp; /* enum to decide which arm to work on */ +char *unp; /* the union itself */ +struct xdr_discrim *choices; /* [value, xdr proc] for each arm */ +xdrproc_t dfault; /* default xdr routine */ { register enum_t dscm; /* * we deal with the discriminator; it's an enum */ - if (! xdr_enum(xdrs, dscmp)) { + if (!xdr_enum(xdrs, dscmp)) { return (FALSE); } dscm = *dscmp; @@ -475,14 +459,14 @@ xdr_union(xdrs, dscmp, unp, choices, dfault) */ for (; choices->proc != NULL_xdrproc_t; choices++) { if (choices->value == dscm) - return ((*(choices->proc))(xdrs, unp, LASTUNSIGNED)); + return ((*(choices->proc)) (xdrs, unp, LASTUNSIGNED)); } /* * no match - execute the default xdr routine if there is one */ return ((dfault == NULL_xdrproc_t) ? FALSE : - (*dfault)(xdrs, unp, LASTUNSIGNED)); + (*dfault) (xdrs, unp, LASTUNSIGNED)); } @@ -500,13 +484,12 @@ xdr_union(xdrs, dscmp, unp, choices, dfault) * storage is allocated. The last parameter is the max allowed length * of the string as specified by a protocol. */ -bool_t -xdr_string(xdrs, cpp, maxsize) - register XDR *xdrs; - char **cpp; - u_int maxsize; +bool_t xdr_string(xdrs, cpp, maxsize) +register XDR *xdrs; +char **cpp; +u_int maxsize; { - register char *sp = *cpp; /* sp is the actual string pointer */ + register char *sp = *cpp; /* sp is the actual string pointer */ u_int size; u_int nodesize; @@ -516,14 +499,14 @@ xdr_string(xdrs, cpp, maxsize) switch (xdrs->x_op) { case XDR_FREE: if (sp == NULL) { - return(TRUE); /* already free */ + return (TRUE); /* already free */ } /* fall through... */ case XDR_ENCODE: size = strlen(sp); break; } - if (! xdr_u_int(xdrs, &size)) { + if (!xdr_u_int(xdrs, &size)) { return (FALSE); } if (size > maxsize) { @@ -541,7 +524,7 @@ xdr_string(xdrs, cpp, maxsize) return (TRUE); } if (sp == NULL) - *cpp = sp = (char *)mem_alloc(nodesize); + *cpp = sp = (char *) mem_alloc(nodesize); if (sp == NULL) { (void) fprintf(stderr, "xdr_string: out of memory\n"); return (FALSE); @@ -564,10 +547,9 @@ xdr_string(xdrs, cpp, maxsize) * Wrapper for xdr_string that can be called directly from * routines like clnt_call */ -bool_t -xdr_wrapstring(xdrs, cpp) - XDR *xdrs; - char **cpp; +bool_t xdr_wrapstring(xdrs, cpp) +XDR *xdrs; +char **cpp; { if (xdr_string(xdrs, cpp, LASTUNSIGNED)) { return (TRUE); diff --git a/libc/inet/rpc/xdr_array.c b/libc/inet/rpc/xdr_array.c index 7c2831ccd..1e6101719 100644 --- a/libc/inet/rpc/xdr_array.c +++ b/libc/inet/rpc/xdr_array.c @@ -28,7 +28,9 @@ * Mountain View, California 94043 */ #if !defined(lint) && defined(SCCSIDS) -static char sccsid[] = "@(#)xdr_array.c 1.10 87/08/11 Copyr 1984 Sun Micro"; +static char sccsid[] = + + "@(#)xdr_array.c 1.10 87/08/11 Copyr 1984 Sun Micro"; #endif /* @@ -55,23 +57,22 @@ static char sccsid[] = "@(#)xdr_array.c 1.10 87/08/11 Copyr 1984 Sun Micro"; * elsize is the size (in bytes) of each element, and elproc is the * xdr procedure to call to handle each element of the array. */ -bool_t -xdr_array(xdrs, addrp, sizep, maxsize, elsize, elproc) - register XDR *xdrs; - caddr_t *addrp; /* array pointer */ - u_int *sizep; /* number of elements */ - u_int maxsize; /* max numberof elements */ - u_int elsize; /* size in bytes of each element */ - xdrproc_t elproc; /* xdr routine to handle each element */ +bool_t xdr_array(xdrs, addrp, sizep, maxsize, elsize, elproc) +register XDR *xdrs; +caddr_t *addrp; /* array pointer */ +u_int *sizep; /* number of elements */ +u_int maxsize; /* max numberof elements */ +u_int elsize; /* size in bytes of each element */ +xdrproc_t elproc; /* xdr routine to handle each element */ { register u_int i; register caddr_t target = *addrp; - register u_int c; /* the actual element count */ + register u_int c; /* the actual element count */ register bool_t stat = TRUE; register u_int nodesize; /* like strings, arrays are really counted arrays */ - if (! xdr_u_int(xdrs, sizep)) { + if (!xdr_u_int(xdrs, sizep)) { return (FALSE); } c = *sizep; @@ -91,8 +92,7 @@ xdr_array(xdrs, addrp, sizep, maxsize, elsize, elproc) return (TRUE); *addrp = target = mem_alloc(nodesize); if (target == NULL) { - (void) fprintf(stderr, - "xdr_array: out of memory\n"); + (void) fprintf(stderr, "xdr_array: out of memory\n"); return (FALSE); } bzero(target, nodesize); @@ -100,13 +100,13 @@ xdr_array(xdrs, addrp, sizep, maxsize, elsize, elproc) case XDR_FREE: return (TRUE); - } - + } + /* * now we xdr each element of array */ for (i = 0; (i < c) && stat; i++) { - stat = (*elproc)(xdrs, target, LASTUNSIGNED); + stat = (*elproc) (xdrs, target, LASTUNSIGNED); target += elsize; } @@ -130,24 +130,22 @@ xdr_array(xdrs, addrp, sizep, maxsize, elsize, elproc) * > elemsize: size of each element * > xdr_elem: routine to XDR each element */ -bool_t -xdr_vector(xdrs, basep, nelem, elemsize, xdr_elem) - register XDR *xdrs; - register char *basep; - register u_int nelem; - register u_int elemsize; - register xdrproc_t xdr_elem; +bool_t xdr_vector(xdrs, basep, nelem, elemsize, xdr_elem) +register XDR *xdrs; +register char *basep; +register u_int nelem; +register u_int elemsize; +register xdrproc_t xdr_elem; { register u_int i; register char *elptr; elptr = basep; for (i = 0; i < nelem; i++) { - if (! (*xdr_elem)(xdrs, elptr, LASTUNSIGNED)) { - return(FALSE); + if (!(*xdr_elem) (xdrs, elptr, LASTUNSIGNED)) { + return (FALSE); } elptr += elemsize; } - return(TRUE); + return (TRUE); } - diff --git a/libc/inet/rpc/xdr_float.c b/libc/inet/rpc/xdr_float.c index ce7b6e07f..f8eb5e99c 100644 --- a/libc/inet/rpc/xdr_float.c +++ b/libc/inet/rpc/xdr_float.c @@ -28,7 +28,9 @@ * Mountain View, California 94043 */ #if !defined(lint) && defined(SCCSIDS) -static char sccsid[] = "@(#)xdr_float.c 1.12 87/08/11 Copyr 1984 Sun Micro"; +static char sccsid[] = + + "@(#)xdr_float.c 1.12 87/08/11 Copyr 1984 Sun Micro"; #endif /* @@ -59,18 +61,18 @@ static char sccsid[] = "@(#)xdr_float.c 1.12 87/08/11 Copyr 1984 Sun Micro"; #ifdef vax /* What IEEE single precision floating point looks like on a Vax */ -struct ieee_single { - unsigned int mantissa: 23; - unsigned int exp : 8; - unsigned int sign : 1; +struct ieee_single { + unsigned int mantissa:23; + unsigned int exp:8; + unsigned int sign:1; }; /* Vax single precision floating point */ -struct vax_single { - unsigned int mantissa1 : 7; - unsigned int exp : 8; - unsigned int sign : 1; - unsigned int mantissa2 : 16; +struct vax_single { + unsigned int mantissa1:7; + unsigned int exp:8; + unsigned int sign:1; + unsigned int mantissa2:16; }; #define VAX_SNG_BIAS 0x81 @@ -80,17 +82,20 @@ static struct sgl_limits { struct vax_single s; struct ieee_single ieee; } sgl_limits[2] = { - {{ 0x7f, 0xff, 0x0, 0xffff }, /* Max Vax */ - { 0x0, 0xff, 0x0 }}, /* Max IEEE */ - {{ 0x0, 0x0, 0x0, 0x0 }, /* Min Vax */ - { 0x0, 0x0, 0x0 }} /* Min IEEE */ + { { + 0x7f, 0xff, 0x0, 0xffff}, /* Max Vax */ + { + 0x0, 0xff, 0x0}}, /* Max IEEE */ + { { + 0x0, 0x0, 0x0, 0x0}, /* Min Vax */ + { + 0x0, 0x0, 0x0}} /* Min IEEE */ }; -#endif /* vax */ +#endif /* vax */ -bool_t -xdr_float(xdrs, fp) - register XDR *xdrs; - register float *fp; +bool_t xdr_float(xdrs, fp) +register XDR *xdrs; +register float *fp; { #if !defined(mc68000) && !defined(sparc) struct ieee_single is; @@ -102,12 +107,13 @@ xdr_float(xdrs, fp) case XDR_ENCODE: #if defined(mc68000) || defined(sparc) - return (XDR_PUTLONG(xdrs, (long *)fp)); + return (XDR_PUTLONG(xdrs, (long *) fp)); #else - vs = *((struct vax_single *)fp); + vs = *((struct vax_single *) fp); for (i = 0, lim = sgl_limits; - i < sizeof(sgl_limits)/sizeof(struct sgl_limits); - i++, lim++) { + + i < sizeof(sgl_limits) / sizeof(struct sgl_limits); + i++, lim++) { if ((vs.mantissa2 == lim->s.mantissa2) && (vs.exp == lim->s.exp) && (vs.mantissa1 == lim->s.mantissa1)) { @@ -117,21 +123,22 @@ xdr_float(xdrs, fp) } is.exp = vs.exp - VAX_SNG_BIAS + IEEE_SNG_BIAS; is.mantissa = (vs.mantissa1 << 16) | vs.mantissa2; - shipit: + shipit: is.sign = vs.sign; - return (XDR_PUTLONG(xdrs, (long *)&is)); + return (XDR_PUTLONG(xdrs, (long *) &is)); #endif case XDR_DECODE: #if defined(mc68000) || defined(sparc) - return (XDR_GETLONG(xdrs, (long *)fp)); + return (XDR_GETLONG(xdrs, (long *) fp)); #else - vsp = (struct vax_single *)fp; - if (!XDR_GETLONG(xdrs, (long *)&is)) + vsp = (struct vax_single *) fp; + if (!XDR_GETLONG(xdrs, (long *) &is)) return (FALSE); for (i = 0, lim = sgl_limits; - i < sizeof(sgl_limits)/sizeof(struct sgl_limits); - i++, lim++) { + + i < sizeof(sgl_limits) / sizeof(struct sgl_limits); + i++, lim++) { if ((is.exp == lim->ieee.exp) && (is.mantissa == lim->ieee.mantissa)) { *vsp = lim->s; @@ -141,7 +148,7 @@ xdr_float(xdrs, fp) vsp->exp = is.exp - IEEE_SNG_BIAS + VAX_SNG_BIAS; vsp->mantissa2 = is.mantissa; vsp->mantissa1 = (is.mantissa >> 16); - doneit: + doneit: vsp->sign = is.sign; return (TRUE); #endif @@ -158,21 +165,21 @@ xdr_float(xdrs, fp) #ifdef vax /* What IEEE double precision floating point looks like on a Vax */ -struct ieee_double { - unsigned int mantissa1 : 20; - unsigned int exp : 11; - unsigned int sign : 1; - unsigned int mantissa2 : 32; +struct ieee_double { + unsigned int mantissa1:20; + unsigned int exp:11; + unsigned int sign:1; + unsigned int mantissa2:32; }; /* Vax double precision floating point */ -struct vax_double { - unsigned int mantissa1 : 7; - unsigned int exp : 8; - unsigned int sign : 1; - unsigned int mantissa2 : 16; - unsigned int mantissa3 : 16; - unsigned int mantissa4 : 16; +struct vax_double { + unsigned int mantissa1:7; + unsigned int exp:8; + unsigned int sign:1; + unsigned int mantissa2:16; + unsigned int mantissa3:16; + unsigned int mantissa4:16; }; #define VAX_DBL_BIAS 0x81 @@ -180,27 +187,31 @@ struct vax_double { #define MASK(nbits) ((1 << nbits) - 1) static struct dbl_limits { - struct vax_double d; - struct ieee_double ieee; + struct vax_double d; + struct ieee_double ieee; } dbl_limits[2] = { - {{ 0x7f, 0xff, 0x0, 0xffff, 0xffff, 0xffff }, /* Max Vax */ - { 0x0, 0x7ff, 0x0, 0x0 }}, /* Max IEEE */ - {{ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, /* Min Vax */ - { 0x0, 0x0, 0x0, 0x0 }} /* Min IEEE */ + { { + 0x7f, 0xff, 0x0, 0xffff, 0xffff, 0xffff}, /* Max Vax */ + { + 0x0, 0x7ff, 0x0, 0x0}}, /* Max IEEE */ + { { + 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, /* Min Vax */ + { + 0x0, 0x0, 0x0, 0x0}} /* Min IEEE */ }; -#endif /* vax */ +#endif /* vax */ -bool_t -xdr_double(xdrs, dp) - register XDR *xdrs; - double *dp; +bool_t xdr_double(xdrs, dp) +register XDR *xdrs; +double *dp; { register long *lp; + #if !defined(mc68000) && !defined(sparc) - struct ieee_double id; - struct vax_double vd; + struct ieee_double id; + struct vax_double vd; register struct dbl_limits *lim; int i; #endif @@ -209,12 +220,13 @@ xdr_double(xdrs, dp) case XDR_ENCODE: #if defined(mc68000) || defined(sparc) - lp = (long *)dp; + lp = (long *) dp; #else - vd = *((struct vax_double *)dp); + vd = *((struct vax_double *) dp); for (i = 0, lim = dbl_limits; - i < sizeof(dbl_limits)/sizeof(struct dbl_limits); - i++, lim++) { + + i < sizeof(dbl_limits) / sizeof(struct dbl_limits); + i++, lim++) { if ((vd.mantissa4 == lim->d.mantissa4) && (vd.mantissa3 == lim->d.mantissa3) && (vd.mantissa2 == lim->d.mantissa2) && @@ -227,25 +239,25 @@ xdr_double(xdrs, dp) id.exp = vd.exp - VAX_DBL_BIAS + IEEE_DBL_BIAS; id.mantissa1 = (vd.mantissa1 << 13) | (vd.mantissa2 >> 3); id.mantissa2 = ((vd.mantissa2 & MASK(3)) << 29) | - (vd.mantissa3 << 13) | - ((vd.mantissa4 >> 3) & MASK(13)); - shipit: + (vd.mantissa3 << 13) | ((vd.mantissa4 >> 3) & MASK(13)); + shipit: id.sign = vd.sign; - lp = (long *)&id; + lp = (long *) &id; #endif return (XDR_PUTLONG(xdrs, lp++) && XDR_PUTLONG(xdrs, lp)); case XDR_DECODE: #if defined(mc68000) || defined(sparc) - lp = (long *)dp; + lp = (long *) dp; return (XDR_GETLONG(xdrs, lp++) && XDR_GETLONG(xdrs, lp)); #else - lp = (long *)&id; + lp = (long *) &id; if (!XDR_GETLONG(xdrs, lp++) || !XDR_GETLONG(xdrs, lp)) return (FALSE); for (i = 0, lim = dbl_limits; - i < sizeof(dbl_limits)/sizeof(struct dbl_limits); - i++, lim++) { + + i < sizeof(dbl_limits) / sizeof(struct dbl_limits); + i++, lim++) { if ((id.mantissa2 == lim->ieee.mantissa2) && (id.mantissa1 == lim->ieee.mantissa1) && (id.exp == lim->ieee.exp)) { @@ -256,12 +268,12 @@ xdr_double(xdrs, dp) vd.exp = id.exp - IEEE_DBL_BIAS + VAX_DBL_BIAS; vd.mantissa1 = (id.mantissa1 >> 13); vd.mantissa2 = ((id.mantissa1 & MASK(13)) << 3) | - (id.mantissa2 >> 29); + (id.mantissa2 >> 29); vd.mantissa3 = (id.mantissa2 >> 13); vd.mantissa4 = (id.mantissa2 << 3); - doneit: + doneit: vd.sign = id.sign; - *dp = *((double *)&vd); + *dp = *((double *) &vd); return (TRUE); #endif diff --git a/libc/inet/rpc/xdr_mem.c b/libc/inet/rpc/xdr_mem.c index 558d36922..b05f2b6f3 100644 --- a/libc/inet/rpc/xdr_mem.c +++ b/libc/inet/rpc/xdr_mem.c @@ -47,16 +47,16 @@ static char sccsid[] = "@(#)xdr_mem.c 1.19 87/08/11 Copyr 1984 Sun Micro"; #include #include -static bool_t xdrmem_getlong(); -static bool_t xdrmem_putlong(); -static bool_t xdrmem_getbytes(); -static bool_t xdrmem_putbytes(); -static u_int xdrmem_getpos(); -static bool_t xdrmem_setpos(); -static long * xdrmem_inline(); -static void xdrmem_destroy(); - -static struct xdr_ops xdrmem_ops = { +static bool_t xdrmem_getlong(); +static bool_t xdrmem_putlong(); +static bool_t xdrmem_getbytes(); +static bool_t xdrmem_putbytes(); +static u_int xdrmem_getpos(); +static bool_t xdrmem_setpos(); +static long *xdrmem_inline(); +static void xdrmem_destroy(); + +static struct xdr_ops xdrmem_ops = { xdrmem_getlong, xdrmem_putlong, xdrmem_getbytes, @@ -71,12 +71,11 @@ static struct xdr_ops xdrmem_ops = { * The procedure xdrmem_create initializes a stream descriptor for a * memory buffer. */ -void -xdrmem_create(xdrs, addr, size, op) - register XDR *xdrs; - caddr_t addr; - u_int size; - enum xdr_op op; +void xdrmem_create(xdrs, addr, size, op) +register XDR *xdrs; +caddr_t addr; +u_int size; +enum xdr_op op; { xdrs->x_op = op; @@ -85,43 +84,43 @@ xdrmem_create(xdrs, addr, size, op) xdrs->x_handy = size; } -static void -xdrmem_destroy(/*xdrs*/) - /*XDR *xdrs;*/ +static void xdrmem_destroy( /*xdrs */ ) + /*XDR *xdrs; */ { } -static bool_t -xdrmem_getlong(xdrs, lp) - register XDR *xdrs; - long *lp; +static bool_t xdrmem_getlong(xdrs, lp) +register XDR *xdrs; +long *lp; { if ((xdrs->x_handy -= sizeof(long)) < 0) return (FALSE); - *lp = (long)ntohl((u_long)(*((long *)(xdrs->x_private)))); + + *lp = (long) ntohl((u_long) (*((long *) (xdrs->x_private)))); xdrs->x_private += sizeof(long); + return (TRUE); } -static bool_t -xdrmem_putlong(xdrs, lp) - register XDR *xdrs; - long *lp; +static bool_t xdrmem_putlong(xdrs, lp) +register XDR *xdrs; +long *lp; { if ((xdrs->x_handy -= sizeof(long)) < 0) return (FALSE); - *(long *)xdrs->x_private = (long)htonl((u_long)(*lp)); + + *(long *) xdrs->x_private = (long) htonl((u_long) (*lp)); xdrs->x_private += sizeof(long); + return (TRUE); } -static bool_t -xdrmem_getbytes(xdrs, addr, len) - register XDR *xdrs; - caddr_t addr; - register u_int len; +static bool_t xdrmem_getbytes(xdrs, addr, len) +register XDR *xdrs; +caddr_t addr; +register u_int len; { if ((xdrs->x_handy -= len) < 0) @@ -131,11 +130,10 @@ xdrmem_getbytes(xdrs, addr, len) return (TRUE); } -static bool_t -xdrmem_putbytes(xdrs, addr, len) - register XDR *xdrs; - caddr_t addr; - register u_int len; +static bool_t xdrmem_putbytes(xdrs, addr, len) +register XDR *xdrs; +caddr_t addr; +register u_int len; { if ((xdrs->x_handy -= len) < 0) @@ -145,33 +143,30 @@ xdrmem_putbytes(xdrs, addr, len) return (TRUE); } -static u_int -xdrmem_getpos(xdrs) - register XDR *xdrs; +static u_int xdrmem_getpos(xdrs) +register XDR *xdrs; { - return ((u_int)xdrs->x_private - (u_int)xdrs->x_base); + return ((u_int) xdrs->x_private - (u_int) xdrs->x_base); } -static bool_t -xdrmem_setpos(xdrs, pos) - register XDR *xdrs; - u_int pos; +static bool_t xdrmem_setpos(xdrs, pos) +register XDR *xdrs; +u_int pos; { register caddr_t newaddr = xdrs->x_base + pos; register caddr_t lastaddr = xdrs->x_private + xdrs->x_handy; - if ((long)newaddr > (long)lastaddr) + if ((long) newaddr > (long) lastaddr) return (FALSE); xdrs->x_private = newaddr; - xdrs->x_handy = (int)lastaddr - (int)newaddr; + xdrs->x_handy = (int) lastaddr - (int) newaddr; return (TRUE); } -static long * -xdrmem_inline(xdrs, len) - register XDR *xdrs; - int len; +static long *xdrmem_inline(xdrs, len) +register XDR *xdrs; +int len; { long *buf = 0; diff --git a/libc/inet/rpc/xdr_rec.c b/libc/inet/rpc/xdr_rec.c index 4d0d4ecfb..94fcc3809 100644 --- a/libc/inet/rpc/xdr_rec.c +++ b/libc/inet/rpc/xdr_rec.c @@ -53,20 +53,20 @@ static char sccsid[] = "@(#)xdr_rec.c 1.21 87/08/11 Copyr 1984 Sun Micro"; #include #include -extern long lseek(); +extern long lseek(); -static u_int fix_buf_size(); +static u_int fix_buf_size(); -static bool_t xdrrec_getlong(); -static bool_t xdrrec_putlong(); -static bool_t xdrrec_getbytes(); -static bool_t xdrrec_putbytes(); -static u_int xdrrec_getpos(); -static bool_t xdrrec_setpos(); -static long * xdrrec_inline(); -static void xdrrec_destroy(); +static bool_t xdrrec_getlong(); +static bool_t xdrrec_putlong(); +static bool_t xdrrec_getbytes(); +static bool_t xdrrec_putbytes(); +static u_int xdrrec_getpos(); +static bool_t xdrrec_setpos(); +static long *xdrrec_inline(); +static void xdrrec_destroy(); -static struct xdr_ops xdrrec_ops = { +static struct xdr_ops xdrrec_ops = { xdrrec_getlong, xdrrec_putlong, xdrrec_getbytes, @@ -98,21 +98,21 @@ typedef struct rec_strm { /* * out-goung bits */ - int (*writeit)(); - caddr_t out_base; /* output buffer (points to frag header) */ - caddr_t out_finger; /* next output position */ - caddr_t out_boundry; /* data cannot up to this address */ - u_long *frag_header; /* beginning of curren fragment */ - bool_t frag_sent; /* true if buffer sent in middle of record */ + int (*writeit) (); + caddr_t out_base; /* output buffer (points to frag header) */ + caddr_t out_finger; /* next output position */ + caddr_t out_boundry; /* data cannot up to this address */ + u_long *frag_header; /* beginning of curren fragment */ + bool_t frag_sent; /* true if buffer sent in middle of record */ /* * in-coming bits */ - int (*readit)(); - u_long in_size; /* fixed size of the input buffer */ + int (*readit) (); + u_long in_size; /* fixed size of the input buffer */ caddr_t in_base; - caddr_t in_finger; /* location of next byte to be had */ - caddr_t in_boundry; /* can read up to this location */ - long fbtbc; /* fragment bytes to be consumed */ + caddr_t in_finger; /* location of next byte to be had */ + caddr_t in_boundry; /* can read up to this location */ + long fbtbc; /* fragment bytes to be consumed */ bool_t last_frag; u_int sendsize; u_int recvsize; @@ -128,20 +128,18 @@ typedef struct rec_strm { * write respectively. They are like the system * calls expect that they take an opaque handle rather than an fd. */ -void -xdrrec_create(xdrs, sendsize, recvsize, tcp_handle, readit, writeit) - register XDR *xdrs; - register u_int sendsize; - register u_int recvsize; - caddr_t tcp_handle; - int (*readit)(); /* like read, but pass it a tcp_handle, not sock */ - int (*writeit)(); /* like write, but pass it a tcp_handle, not sock */ +void xdrrec_create(xdrs, sendsize, recvsize, tcp_handle, readit, writeit) +register XDR *xdrs; +register u_int sendsize; +register u_int recvsize; +caddr_t tcp_handle; +int (*readit) (); /* like read, but pass it a tcp_handle, not sock */ +int (*writeit) (); /* like write, but pass it a tcp_handle, not sock */ { - register RECSTREAM *rstrm = - (RECSTREAM *)mem_alloc(sizeof(RECSTREAM)); + register RECSTREAM *rstrm = (RECSTREAM *) mem_alloc(sizeof(RECSTREAM)); if (rstrm == NULL) { - (void)fprintf(stderr, "xdrrec_create: out of memory\n"); + (void) fprintf(stderr, "xdrrec_create: out of memory\n"); /* * This is bad. Should rework xdrrec_create to * return a handle, and in this case return NULL @@ -153,25 +151,26 @@ xdrrec_create(xdrs, sendsize, recvsize, tcp_handle, readit, writeit) */ rstrm->sendsize = sendsize = fix_buf_size(sendsize); rstrm->recvsize = recvsize = fix_buf_size(recvsize); - rstrm->the_buffer = mem_alloc(sendsize + recvsize + BYTES_PER_XDR_UNIT); + rstrm->the_buffer = + mem_alloc(sendsize + recvsize + BYTES_PER_XDR_UNIT); if (rstrm->the_buffer == NULL) { - (void)fprintf(stderr, "xdrrec_create: out of memory\n"); + (void) fprintf(stderr, "xdrrec_create: out of memory\n"); return; } for (rstrm->out_base = rstrm->the_buffer; - (u_int)rstrm->out_base % BYTES_PER_XDR_UNIT != 0; - rstrm->out_base++); + (u_int) rstrm->out_base % BYTES_PER_XDR_UNIT != 0; + rstrm->out_base++); rstrm->in_base = rstrm->out_base + sendsize; /* * now the rest ... */ xdrs->x_ops = &xdrrec_ops; - xdrs->x_private = (caddr_t)rstrm; + xdrs->x_private = (caddr_t) rstrm; rstrm->tcp_handle = tcp_handle; rstrm->readit = readit; rstrm->writeit = writeit; rstrm->out_finger = rstrm->out_boundry = rstrm->out_base; - rstrm->frag_header = (u_long *)rstrm->out_base; + rstrm->frag_header = (u_long *) rstrm->out_base; rstrm->out_finger += sizeof(u_long); rstrm->out_boundry += sendsize; rstrm->frag_sent = FALSE; @@ -188,36 +187,35 @@ xdrrec_create(xdrs, sendsize, recvsize, tcp_handle, readit, writeit) * xdr handle filled in by xdrrec_create. */ -static bool_t -xdrrec_getlong(xdrs, lp) - XDR *xdrs; - long *lp; +static bool_t xdrrec_getlong(xdrs, lp) +XDR *xdrs; +long *lp; { - register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private); - register long *buflp = (long *)(rstrm->in_finger); + register RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private); + register long *buflp = (long *) (rstrm->in_finger); long mylong; /* first try the inline, fast case */ if ((rstrm->fbtbc >= sizeof(long)) && - (((int)rstrm->in_boundry - (int)buflp) >= sizeof(long))) { - *lp = (long)ntohl((u_long)(*buflp)); + (((int) rstrm->in_boundry - (int) buflp) >= sizeof(long))) { + *lp = (long) ntohl((u_long) (*buflp)); rstrm->fbtbc -= sizeof(long); rstrm->in_finger += sizeof(long); } else { - if (! xdrrec_getbytes(xdrs, (caddr_t)&mylong, sizeof(long))) + if (!xdrrec_getbytes(xdrs, (caddr_t) & mylong, sizeof(long))) return (FALSE); - *lp = (long)ntohl((u_long)mylong); + + *lp = (long) ntohl((u_long) mylong); } return (TRUE); } -static bool_t -xdrrec_putlong(xdrs, lp) - XDR *xdrs; - long *lp; +static bool_t xdrrec_putlong(xdrs, lp) +XDR *xdrs; +long *lp; { - register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private); - register long *dest_lp = ((long *)(rstrm->out_finger)); + register RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private); + register long *dest_lp = ((long *) (rstrm->out_finger)); if ((rstrm->out_finger += sizeof(long)) > rstrm->out_boundry) { /* @@ -225,23 +223,25 @@ xdrrec_putlong(xdrs, lp) * inefficient */ rstrm->out_finger -= sizeof(long); + rstrm->frag_sent = TRUE; - if (! flush_out(rstrm, FALSE)) + if (!flush_out(rstrm, FALSE)) return (FALSE); - dest_lp = ((long *)(rstrm->out_finger)); + dest_lp = ((long *) (rstrm->out_finger)); rstrm->out_finger += sizeof(long); } - *dest_lp = (long)htonl((u_long)(*lp)); + *dest_lp = (long) htonl((u_long) (*lp)); return (TRUE); } -static bool_t /* must manage buffers, fragments, and records */ +static bool_t +/* must manage buffers, fragments, and records */ xdrrec_getbytes(xdrs, addr, len) - XDR *xdrs; - register caddr_t addr; - register u_int len; +XDR *xdrs; +register caddr_t addr; +register u_int len; { - register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private); + register RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private); register int current; while (len > 0) { @@ -249,31 +249,30 @@ xdrrec_getbytes(xdrs, addr, len) if (current == 0) { if (rstrm->last_frag) return (FALSE); - if (! set_input_fragment(rstrm)) + if (!set_input_fragment(rstrm)) return (FALSE); continue; } current = (len < current) ? len : current; - if (! get_input_bytes(rstrm, addr, current)) + if (!get_input_bytes(rstrm, addr, current)) return (FALSE); - addr += current; + addr += current; rstrm->fbtbc -= current; len -= current; } return (TRUE); } -static bool_t -xdrrec_putbytes(xdrs, addr, len) - XDR *xdrs; - register caddr_t addr; - register u_int len; +static bool_t xdrrec_putbytes(xdrs, addr, len) +XDR *xdrs; +register caddr_t addr; +register u_int len; { - register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private); + register RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private); register int current; while (len > 0) { - current = (u_int)rstrm->out_boundry - (u_int)rstrm->out_finger; + current = (u_int) rstrm->out_boundry - (u_int) rstrm->out_finger; current = (len < current) ? len : current; bcopy(addr, rstrm->out_finger, current); rstrm->out_finger += current; @@ -281,21 +280,20 @@ xdrrec_putbytes(xdrs, addr, len) len -= current; if (rstrm->out_finger == rstrm->out_boundry) { rstrm->frag_sent = TRUE; - if (! flush_out(rstrm, FALSE)) + if (!flush_out(rstrm, FALSE)) return (FALSE); } } return (TRUE); } -static u_int -xdrrec_getpos(xdrs) - register XDR *xdrs; +static u_int xdrrec_getpos(xdrs) +register XDR *xdrs; { - register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private; + register RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private; register long pos; - pos = lseek((int)rstrm->tcp_handle, (long) 0, 1); + pos = lseek((int) rstrm->tcp_handle, (long) 0, 1); if (pos != -1) switch (xdrs->x_op) { @@ -308,28 +306,27 @@ xdrrec_getpos(xdrs) break; default: - pos = (u_int) -1; + pos = (u_int) - 1; break; } return ((u_int) pos); } -static bool_t -xdrrec_setpos(xdrs, pos) - register XDR *xdrs; - u_int pos; +static bool_t xdrrec_setpos(xdrs, pos) +register XDR *xdrs; +u_int pos; { - register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private; + register RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private; u_int currpos = xdrrec_getpos(xdrs); int delta = currpos - pos; caddr_t newpos; - if ((int)currpos != -1) + if ((int) currpos != -1) switch (xdrs->x_op) { case XDR_ENCODE: newpos = rstrm->out_finger - delta; - if ((newpos > (caddr_t)(rstrm->frag_header)) && + if ((newpos > (caddr_t) (rstrm->frag_header)) && (newpos < rstrm->out_boundry)) { rstrm->out_finger = newpos; return (TRUE); @@ -338,7 +335,7 @@ xdrrec_setpos(xdrs, pos) case XDR_DECODE: newpos = rstrm->in_finger - delta; - if ((delta < (int)(rstrm->fbtbc)) && + if ((delta < (int) (rstrm->fbtbc)) && (newpos <= rstrm->in_boundry) && (newpos >= rstrm->in_base)) { rstrm->in_finger = newpos; @@ -350,13 +347,12 @@ xdrrec_setpos(xdrs, pos) return (FALSE); } -static long * -xdrrec_inline(xdrs, len) - register XDR *xdrs; - int len; +static long *xdrrec_inline(xdrs, len) +register XDR *xdrs; +int len; { - register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private; - long * buf = NULL; + register RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private; + long *buf = NULL; switch (xdrs->x_op) { @@ -379,15 +375,14 @@ xdrrec_inline(xdrs, len) return (buf); } -static void -xdrrec_destroy(xdrs) - register XDR *xdrs; +static void xdrrec_destroy(xdrs) +register XDR *xdrs; { - register RECSTREAM *rstrm = (RECSTREAM *)xdrs->x_private; + register RECSTREAM *rstrm = (RECSTREAM *) xdrs->x_private; mem_free(rstrm->the_buffer, - rstrm->sendsize + rstrm->recvsize + BYTES_PER_XDR_UNIT); - mem_free((caddr_t)rstrm, sizeof(RECSTREAM)); + rstrm->sendsize + rstrm->recvsize + BYTES_PER_XDR_UNIT); + mem_free((caddr_t) rstrm, sizeof(RECSTREAM)); } @@ -399,17 +394,16 @@ xdrrec_destroy(xdrs) * Before reading (deserializing from the stream, one should always call * this procedure to guarantee proper record alignment. */ -bool_t -xdrrec_skiprecord(xdrs) - XDR *xdrs; +bool_t xdrrec_skiprecord(xdrs) +XDR *xdrs; { - register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private); + register RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private); - while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) { - if (! skip_input_bytes(rstrm, rstrm->fbtbc)) + while (rstrm->fbtbc > 0 || (!rstrm->last_frag)) { + if (!skip_input_bytes(rstrm, rstrm->fbtbc)) return (FALSE); rstrm->fbtbc = 0; - if ((! rstrm->last_frag) && (! set_input_fragment(rstrm))) + if ((!rstrm->last_frag) && (!set_input_fragment(rstrm))) return (FALSE); } rstrm->last_frag = FALSE; @@ -421,17 +415,16 @@ xdrrec_skiprecord(xdrs) * Returns TRUE iff there is no more input in the buffer * after consuming the rest of the current record. */ -bool_t -xdrrec_eof(xdrs) - XDR *xdrs; +bool_t xdrrec_eof(xdrs) +XDR *xdrs; { - register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private); + register RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private); - while (rstrm->fbtbc > 0 || (! rstrm->last_frag)) { - if (! skip_input_bytes(rstrm, rstrm->fbtbc)) + while (rstrm->fbtbc > 0 || (!rstrm->last_frag)) { + if (!skip_input_bytes(rstrm, rstrm->fbtbc)) return (TRUE); rstrm->fbtbc = 0; - if ((! rstrm->last_frag) && (! set_input_fragment(rstrm))) + if ((!rstrm->last_frag) && (!set_input_fragment(rstrm))) return (TRUE); } if (rstrm->in_finger == rstrm->in_boundry) @@ -445,24 +438,23 @@ xdrrec_eof(xdrs) * (output) tcp stream. (This let's the package support batched or * pipelined procedure calls.) TRUE => immmediate flush to tcp connection. */ -bool_t -xdrrec_endofrecord(xdrs, sendnow) - XDR *xdrs; - bool_t sendnow; +bool_t xdrrec_endofrecord(xdrs, sendnow) +XDR *xdrs; +bool_t sendnow; { - register RECSTREAM *rstrm = (RECSTREAM *)(xdrs->x_private); - register u_long len; /* fragment length */ + register RECSTREAM *rstrm = (RECSTREAM *) (xdrs->x_private); + register u_long len; /* fragment length */ if (sendnow || rstrm->frag_sent || - ((u_long)rstrm->out_finger + sizeof(u_long) >= - (u_long)rstrm->out_boundry)) { + ((u_long) rstrm->out_finger + sizeof(u_long) >= + (u_long) rstrm->out_boundry)) { rstrm->frag_sent = FALSE; return (flush_out(rstrm, TRUE)); } - len = (u_long)(rstrm->out_finger) - (u_long)(rstrm->frag_header) - - sizeof(u_long); - *(rstrm->frag_header) = htonl((u_long)len | LAST_FRAG); - rstrm->frag_header = (u_long *)rstrm->out_finger; + len = (u_long) (rstrm->out_finger) - (u_long) (rstrm->frag_header) - + sizeof(u_long); + *(rstrm->frag_header) = htonl((u_long) len | LAST_FRAG); + rstrm->frag_header = (u_long *) rstrm->out_finger; rstrm->out_finger += sizeof(u_long); return (TRUE); } @@ -471,38 +463,38 @@ xdrrec_endofrecord(xdrs, sendnow) /* * Internal useful routines */ -static bool_t -flush_out(rstrm, eor) - register RECSTREAM *rstrm; - bool_t eor; +static bool_t flush_out(rstrm, eor) +register RECSTREAM *rstrm; +bool_t eor; { register u_long eormask = (eor == TRUE) ? LAST_FRAG : 0; - register u_long len = (u_long)(rstrm->out_finger) - - (u_long)(rstrm->frag_header) - sizeof(u_long); + register u_long len = (u_long) (rstrm->out_finger) - + (u_long) (rstrm->frag_header) - sizeof(u_long); *(rstrm->frag_header) = htonl(len | eormask); - len = (u_long)(rstrm->out_finger) - (u_long)(rstrm->out_base); - if ((*(rstrm->writeit))(rstrm->tcp_handle, rstrm->out_base, (int)len) - != (int)len) + len = (u_long) (rstrm->out_finger) - (u_long) (rstrm->out_base); + if ((*(rstrm->writeit)) (rstrm->tcp_handle, rstrm->out_base, (int) len) + != (int) len) return (FALSE); - rstrm->frag_header = (u_long *)rstrm->out_base; - rstrm->out_finger = (caddr_t)rstrm->out_base + sizeof(u_long); + rstrm->frag_header = (u_long *) rstrm->out_base; + rstrm->out_finger = (caddr_t) rstrm->out_base + sizeof(u_long); return (TRUE); } -static bool_t /* knows nothing about records! Only about input buffers */ +static bool_t +/* knows nothing about records! Only about input buffers */ fill_input_buf(rstrm) - register RECSTREAM *rstrm; +register RECSTREAM *rstrm; { register caddr_t where; u_int i; register int len; where = rstrm->in_base; - i = (u_int)rstrm->in_boundry % BYTES_PER_XDR_UNIT; + i = (u_int) rstrm->in_boundry % BYTES_PER_XDR_UNIT; where += i; len = rstrm->in_size - i; - if ((len = (*(rstrm->readit))(rstrm->tcp_handle, where, len)) == -1) + if ((len = (*(rstrm->readit)) (rstrm->tcp_handle, where, len)) == -1) return (FALSE); rstrm->in_finger = where; where += len; @@ -510,18 +502,19 @@ fill_input_buf(rstrm) return (TRUE); } -static bool_t /* knows nothing about records! Only about input buffers */ +static bool_t +/* knows nothing about records! Only about input buffers */ get_input_bytes(rstrm, addr, len) - register RECSTREAM *rstrm; - register caddr_t addr; - register int len; +register RECSTREAM *rstrm; +register caddr_t addr; +register int len; { register int current; while (len > 0) { - current = (int)rstrm->in_boundry - (int)rstrm->in_finger; + current = (int) rstrm->in_boundry - (int) rstrm->in_finger; if (current == 0) { - if (! fill_input_buf(rstrm)) + if (!fill_input_buf(rstrm)) return (FALSE); continue; } @@ -534,31 +527,33 @@ get_input_bytes(rstrm, addr, len) return (TRUE); } -static bool_t /* next two bytes of the input stream are treated as a header */ +static bool_t +/* next two bytes of the input stream are treated as a header */ set_input_fragment(rstrm) - register RECSTREAM *rstrm; +register RECSTREAM *rstrm; { u_long header; - if (! get_input_bytes(rstrm, (caddr_t)&header, sizeof(header))) + if (!get_input_bytes(rstrm, (caddr_t) & header, sizeof(header))) return (FALSE); - header = (long)ntohl(header); + header = (long) ntohl(header); rstrm->last_frag = ((header & LAST_FRAG) == 0) ? FALSE : TRUE; rstrm->fbtbc = header & (~LAST_FRAG); return (TRUE); } -static bool_t /* consumes input bytes; knows nothing about records! */ +static bool_t +/* consumes input bytes; knows nothing about records! */ skip_input_bytes(rstrm, cnt) - register RECSTREAM *rstrm; - long cnt; +register RECSTREAM *rstrm; +long cnt; { register int current; while (cnt > 0) { - current = (int)rstrm->in_boundry - (int)rstrm->in_finger; + current = (int) rstrm->in_boundry - (int) rstrm->in_finger; if (current == 0) { - if (! fill_input_buf(rstrm)) + if (!fill_input_buf(rstrm)) return (FALSE); continue; } @@ -569,9 +564,8 @@ skip_input_bytes(rstrm, cnt) return (TRUE); } -static u_int -fix_buf_size(s) - register u_int s; +static u_int fix_buf_size(s) +register u_int s; { if (s < 100) diff --git a/libc/inet/rpc/xdr_reference.c b/libc/inet/rpc/xdr_reference.c index 32d91d999..61050b6e7 100644 --- a/libc/inet/rpc/xdr_reference.c +++ b/libc/inet/rpc/xdr_reference.c @@ -55,12 +55,11 @@ static char sccsid[] = "@(#)xdr_reference.c 1.11 87/08/11 SMI"; * size is the sizeof the referneced structure. * proc is the routine to handle the referenced structure. */ -bool_t -xdr_reference(xdrs, pp, size, proc) - register XDR *xdrs; - caddr_t *pp; /* the pointer to work on */ - u_int size; /* size of the object pointed to */ - xdrproc_t proc; /* xdr routine to handle the object */ +bool_t xdr_reference(xdrs, pp, size, proc) +register XDR *xdrs; +caddr_t *pp; /* the pointer to work on */ +u_int size; /* size of the object pointed to */ +xdrproc_t proc; /* xdr routine to handle the object */ { register caddr_t loc = *pp; register bool_t stat; @@ -73,15 +72,14 @@ xdr_reference(xdrs, pp, size, proc) case XDR_DECODE: *pp = loc = (caddr_t) mem_alloc(size); if (loc == NULL) { - (void) fprintf(stderr, - "xdr_reference: out of memory\n"); + (void) fprintf(stderr, "xdr_reference: out of memory\n"); return (FALSE); } - bzero(loc, (int)size); + bzero(loc, (int) size); break; - } + } - stat = (*proc)(xdrs, loc, LASTUNSIGNED); + stat = (*proc) (xdrs, loc, LASTUNSIGNED); if (xdrs->x_op == XDR_FREE) { mem_free(loc, size); @@ -110,23 +108,22 @@ xdr_reference(xdrs, pp, size, proc) * > xdr_obj: routine to XDR an object. * */ -bool_t -xdr_pointer(xdrs,objpp,obj_size,xdr_obj) - register XDR *xdrs; - char **objpp; - u_int obj_size; - xdrproc_t xdr_obj; +bool_t xdr_pointer(xdrs, objpp, obj_size, xdr_obj) +register XDR *xdrs; +char **objpp; +u_int obj_size; +xdrproc_t xdr_obj; { bool_t more_data; more_data = (*objpp != NULL); - if (! xdr_bool(xdrs,&more_data)) { + if (!xdr_bool(xdrs, &more_data)) { return (FALSE); } - if (! more_data) { + if (!more_data) { *objpp = NULL; return (TRUE); } - return (xdr_reference(xdrs,objpp,obj_size,xdr_obj)); + return (xdr_reference(xdrs, objpp, obj_size, xdr_obj)); } diff --git a/libc/inet/rpc/xdr_stdio.c b/libc/inet/rpc/xdr_stdio.c index 694774f6f..7ae5fbdab 100644 --- a/libc/inet/rpc/xdr_stdio.c +++ b/libc/inet/rpc/xdr_stdio.c @@ -28,7 +28,9 @@ * Mountain View, California 94043 */ #if !defined(lint) && defined(SCCSIDS) -static char sccsid[] = "@(#)xdr_stdio.c 1.16 87/08/11 Copyr 1984 Sun Micro"; +static char sccsid[] = + + "@(#)xdr_stdio.c 1.16 87/08/11 Copyr 1984 Sun Micro"; #endif /* @@ -45,27 +47,27 @@ static char sccsid[] = "@(#)xdr_stdio.c 1.16 87/08/11 Copyr 1984 Sun Micro"; #include #include -static bool_t xdrstdio_getlong(); -static bool_t xdrstdio_putlong(); -static bool_t xdrstdio_getbytes(); -static bool_t xdrstdio_putbytes(); -static u_int xdrstdio_getpos(); -static bool_t xdrstdio_setpos(); -static long * xdrstdio_inline(); -static void xdrstdio_destroy(); +static bool_t xdrstdio_getlong(); +static bool_t xdrstdio_putlong(); +static bool_t xdrstdio_getbytes(); +static bool_t xdrstdio_putbytes(); +static u_int xdrstdio_getpos(); +static bool_t xdrstdio_setpos(); +static long *xdrstdio_inline(); +static void xdrstdio_destroy(); /* * Ops vector for stdio type XDR */ -static struct xdr_ops xdrstdio_ops = { - xdrstdio_getlong, /* deseraialize a long int */ - xdrstdio_putlong, /* seraialize a long int */ - xdrstdio_getbytes, /* deserialize counted bytes */ - xdrstdio_putbytes, /* serialize counted bytes */ - xdrstdio_getpos, /* get offset in the stream */ - xdrstdio_setpos, /* set offset in the stream */ - xdrstdio_inline, /* prime stream for inline macros */ - xdrstdio_destroy /* destroy stream */ +static struct xdr_ops xdrstdio_ops = { + xdrstdio_getlong, /* deseraialize a long int */ + xdrstdio_putlong, /* seraialize a long int */ + xdrstdio_getbytes, /* deserialize counted bytes */ + xdrstdio_putbytes, /* serialize counted bytes */ + xdrstdio_getpos, /* get offset in the stream */ + xdrstdio_setpos, /* set offset in the stream */ + xdrstdio_inline, /* prime stream for inline macros */ + xdrstdio_destroy /* destroy stream */ }; /* @@ -73,16 +75,15 @@ static struct xdr_ops xdrstdio_ops = { * Sets the xdr stream handle xdrs for use on the stream file. * Operation flag is set to op. */ -void -xdrstdio_create(xdrs, file, op) - register XDR *xdrs; - FILE *file; - enum xdr_op op; +void xdrstdio_create(xdrs, file, op) +register XDR *xdrs; +FILE *file; +enum xdr_op op; { xdrs->x_op = op; xdrs->x_ops = &xdrstdio_ops; - xdrs->x_private = (caddr_t)file; + xdrs->x_private = (caddr_t) file; xdrs->x_handy = 0; xdrs->x_base = 0; } @@ -91,89 +92,86 @@ xdrstdio_create(xdrs, file, op) * Destroy a stdio xdr stream. * Cleans up the xdr stream handle xdrs previously set up by xdrstdio_create. */ -static void -xdrstdio_destroy(xdrs) - register XDR *xdrs; +static void xdrstdio_destroy(xdrs) +register XDR *xdrs; { - (void)fflush((FILE *)xdrs->x_private); + (void) fflush((FILE *) xdrs->x_private); /* xx should we close the file ?? */ }; -static bool_t -xdrstdio_getlong(xdrs, lp) - XDR *xdrs; - register long *lp; +static bool_t xdrstdio_getlong(xdrs, lp) +XDR *xdrs; +register long *lp; { - if (fread((caddr_t)lp, sizeof(long), 1, (FILE *)xdrs->x_private) != 1) - return (FALSE); + if (fread((caddr_t) lp, sizeof(long), 1, (FILE *) xdrs->x_private) != + 1) return (FALSE); + #ifndef mc68000 *lp = ntohl(*lp); #endif return (TRUE); } -static bool_t -xdrstdio_putlong(xdrs, lp) - XDR *xdrs; - long *lp; +static bool_t xdrstdio_putlong(xdrs, lp) +XDR *xdrs; +long *lp; { #ifndef mc68000 long mycopy = htonl(*lp); + lp = &mycopy; #endif - if (fwrite((caddr_t)lp, sizeof(long), 1, (FILE *)xdrs->x_private) != 1) - return (FALSE); + if (fwrite((caddr_t) lp, sizeof(long), 1, (FILE *) xdrs->x_private) != + 1) return (FALSE); + return (TRUE); } -static bool_t -xdrstdio_getbytes(xdrs, addr, len) - XDR *xdrs; - caddr_t addr; - u_int len; +static bool_t xdrstdio_getbytes(xdrs, addr, len) +XDR *xdrs; +caddr_t addr; +u_int len; { - if ((len != 0) && (fread(addr, (int)len, 1, (FILE *)xdrs->x_private) != 1)) + if ((len != 0) + && (fread(addr, (int) len, 1, (FILE *) xdrs->x_private) != 1)) return (FALSE); return (TRUE); } -static bool_t -xdrstdio_putbytes(xdrs, addr, len) - XDR *xdrs; - caddr_t addr; - u_int len; +static bool_t xdrstdio_putbytes(xdrs, addr, len) +XDR *xdrs; +caddr_t addr; +u_int len; { - if ((len != 0) && (fwrite(addr, (int)len, 1, (FILE *)xdrs->x_private) != 1)) + if ((len != 0) + && (fwrite(addr, (int) len, 1, (FILE *) xdrs->x_private) != 1)) return (FALSE); return (TRUE); } -static u_int -xdrstdio_getpos(xdrs) - XDR *xdrs; +static u_int xdrstdio_getpos(xdrs) +XDR *xdrs; { - return ((u_int) ftell((FILE *)xdrs->x_private)); + return ((u_int) ftell((FILE *) xdrs->x_private)); } -static bool_t -xdrstdio_setpos(xdrs, pos) - XDR *xdrs; - u_int pos; -{ +static bool_t xdrstdio_setpos(xdrs, pos) +XDR *xdrs; +u_int pos; +{ - return ((fseek((FILE *)xdrs->x_private, (long)pos, 0) < 0) ? - FALSE : TRUE); + return ((fseek((FILE *) xdrs->x_private, (long) pos, 0) < 0) ? + FALSE : TRUE); } -static long * -xdrstdio_inline(xdrs, len) - XDR *xdrs; - u_int len; +static long *xdrstdio_inline(xdrs, len) +XDR *xdrs; +u_int len; { /* diff --git a/libc/misc/regex/.indent.pro b/libc/misc/regex/.indent.pro new file mode 100644 index 000000000..492ecf1c7 --- /dev/null +++ b/libc/misc/regex/.indent.pro @@ -0,0 +1,33 @@ +--blank-lines-after-declarations +--blank-lines-after-procedures +--break-before-boolean-operator +--no-blank-lines-after-commas +--braces-on-if-line +--braces-on-struct-decl-line +--comment-indentation25 +--declaration-comment-column25 +--no-comment-delimiters-on-blank-lines +--cuddle-else +--continuation-indentation4 +--case-indentation0 +--else-endif-column33 +--space-after-cast +--line-comments-indentation0 +--declaration-indentation1 +--dont-format-first-column-comments +--dont-format-comments +--honour-newlines +--indent-level4 +/* changed from 0 to 4 */ +--parameter-indentation4 +--line-length78 /* changed from 75 */ +--continue-at-parentheses +--no-space-after-function-call-names +--dont-break-procedure-type +--dont-star-comments +--leave-optional-blank-lines +--dont-space-special-semicolon +--tab-size4 +/* additions by Mark */ +--case-brace-indentation0 +--leave-preprocessor-space diff --git a/libc/misc/regex/Makefile b/libc/misc/regex/Makefile index b8b38faa9..006ea75cc 100644 --- a/libc/misc/regex/Makefile +++ b/libc/misc/regex/Makefile @@ -26,13 +26,16 @@ LIBC=$(TOPDIR)libc.a CSRC=rx.c COBJS=$(patsubst %.c,%.o, $(CSRC)) +OBJS=$(COBJS) -all: $(COBJS) $(LIBC) +all: $(OBJS) $(LIBC) -$(LIBC): $(COBJS) - $(AR) $(ARFLAGS) $(LIBC) $(COBJS) +$(LIBC): ar-target -$(COBJS): Makefile +ar-target: $(OBJS) + $(AR) $(ARFLAGS) $(LIBC) $(OBJS) + +$(OBJS): Makefile clean: rm -f *.[oa] *~ core diff --git a/libc/misc/regex/rx.c b/libc/misc/regex/rx.c index 1abff8cc5..39f77adb6 100644 --- a/libc/misc/regex/rx.c +++ b/libc/misc/regex/rx.c @@ -22,7 +22,7 @@ write to the Free Software Foundation, 675 Mass Ave, Cambridge, MA * Do not put ANYTHING before it! */ #if !defined (__GNUC__) && defined (_AIX) - #pragma alloca +#pragma alloca #endif /* To make linux happy? */ @@ -50,6 +50,7 @@ write to the Free Software Foundation, 675 Mass Ave, Cambridge, MA #define MIN(a, b) ((a) < (b) ? (a) : (b)) typedef char boolean; + #define false 0 #define true 1 @@ -64,17 +65,17 @@ typedef char boolean; /* Make alloca work the best possible way. */ #ifdef __GNUC__ #define alloca __builtin_alloca -#else /* not __GNUC__ */ +#else /* not __GNUC__ */ #if HAVE_ALLOCA_H #include -#else /* not __GNUC__ or HAVE_ALLOCA_H */ -#ifndef _AIX /* Already did AIX, up at the top. */ -char *alloca (); -#endif /* not _AIX */ -#endif /* not HAVE_ALLOCA_H */ -#endif /* not __GNUC__ */ +#else /* not __GNUC__ or HAVE_ALLOCA_H */ +#ifndef _AIX /* Already did AIX, up at the top. */ +char *alloca(); +#endif /* not _AIX */ +#endif /* not HAVE_ALLOCA_H */ +#endif /* not __GNUC__ */ -#endif /* not alloca */ +#endif /* not alloca */ /* Memory management and stuff for emacs. */ @@ -90,14 +91,14 @@ char *alloca (); #ifdef REGEX_MALLOC #define REGEX_ALLOCATE malloc -#else /* not REGEX_MALLOC */ +#else /* not REGEX_MALLOC */ #define REGEX_ALLOCATE alloca -#endif /* not REGEX_MALLOC */ +#endif /* not REGEX_MALLOC */ #ifdef RX_WANT_RX_DEFS #define RX_DECL extern -#define RX_DEF_QUAL +#define RX_DEF_QUAL #else #define RX_WANT_RX_DEFS #define RX_DECL static @@ -114,137 +115,132 @@ char *alloca (); */ #ifdef __STDC__ RX_DECL struct rx_hash_item - *rx_hash_find (struct rx_hash *, unsigned long, - void *, struct rx_hash_rules *); +*rx_hash_find(struct rx_hash *, unsigned long, + + void *, struct rx_hash_rules *); RX_DECL struct rx_hash_item - *rx_hash_find (struct rx_hash *, unsigned long, - void *, struct rx_hash_rules *); +*rx_hash_find(struct rx_hash *, unsigned long, + + void *, struct rx_hash_rules *); RX_DECL struct rx_hash_item - *rx_hash_store (struct rx_hash *, unsigned long, - void *, struct rx_hash_rules *); -RX_DECL void rx_hash_free (struct rx_hash_item *, - struct rx_hash_rules *); -RX_DECL void rx_free_hash_table (struct rx_hash *, rx_hash_freefn, - struct rx_hash_rules *); -RX_DECL rx_Bitset - rx_cset (struct rx *); -RX_DECL rx_Bitset - rx_copy_cset (struct rx *, rx_Bitset); -RX_DECL void rx_free_cset (struct rx *, rx_Bitset); +*rx_hash_store(struct rx_hash *, unsigned long, + + void *, struct rx_hash_rules *); +RX_DECL void rx_hash_free(struct rx_hash_item *, struct rx_hash_rules *); +RX_DECL void rx_free_hash_table(struct rx_hash *, rx_hash_freefn, + + struct rx_hash_rules *); +RX_DECL rx_Bitset rx_cset(struct rx *); +RX_DECL rx_Bitset rx_copy_cset(struct rx *, rx_Bitset); +RX_DECL void rx_free_cset(struct rx *, rx_Bitset); static struct rx_hash_item - *compiler_hash_item_alloc (struct rx_hash_rules *, void *); +*compiler_hash_item_alloc(struct rx_hash_rules *, void *); static struct rx_hash - *compiler_hash_alloc (struct rx_hash_rules *); -static void compiler_free_hash (struct rx_hash *, - struct rx_hash_rules *); -static void compiler_free_hash_item (struct rx_hash_item *, - struct rx_hash_rules *); +*compiler_hash_alloc(struct rx_hash_rules *); +static void compiler_free_hash(struct rx_hash *, struct rx_hash_rules *); +static void compiler_free_hash_item(struct rx_hash_item *, + + struct rx_hash_rules *); RX_DECL struct rexp_node - *rexp_node (struct rx *, enum rexp_node_type); +*rexp_node(struct rx *, enum rexp_node_type); RX_DECL struct rexp_node - *rx_mk_r_cset (struct rx *, rx_Bitset); +*rx_mk_r_cset(struct rx *, rx_Bitset); RX_DECL struct rexp_node - *rx_mk_r_concat (struct rx *, struct rexp_node *, - struct rexp_node *); +*rx_mk_r_concat(struct rx *, struct rexp_node *, struct rexp_node *); RX_DECL struct rexp_node - *rx_mk_r_alternate (struct rx *, struct rexp_node *, - struct rexp_node *); +*rx_mk_r_alternate(struct rx *, struct rexp_node *, struct rexp_node *); RX_DECL struct rexp_node - *rx_mk_r_alternate (struct rx *, struct rexp_node *, - struct rexp_node *); +*rx_mk_r_alternate(struct rx *, struct rexp_node *, struct rexp_node *); RX_DECL struct rexp_node - *rx_mk_r_opt (struct rx *, struct rexp_node *); +*rx_mk_r_opt(struct rx *, struct rexp_node *); RX_DECL struct rexp_node - *rx_mk_r_star (struct rx *, struct rexp_node *); +*rx_mk_r_star(struct rx *, struct rexp_node *); RX_DECL struct rexp_node - *rx_mk_r_2phase_star (struct rx *, struct rexp_node *, - struct rexp_node *); +*rx_mk_r_2phase_star(struct rx *, struct rexp_node *, struct rexp_node *); RX_DECL struct rexp_node - *rx_mk_r_side_effect (struct rx *, rx_side_effect); +*rx_mk_r_side_effect(struct rx *, rx_side_effect); + //RX_DECL struct rexp_node // *rx_mk_r_data (struct rx *, void *); -RX_DECL void rx_free_rexp (struct rx *, struct rexp_node *); +RX_DECL void rx_free_rexp(struct rx *, struct rexp_node *); RX_DECL struct rexp_node - *rx_copy_rexp (struct rx *, struct rexp_node *); +*rx_copy_rexp(struct rx *, struct rexp_node *); RX_DECL struct rx_nfa_state - *rx_nfa_state (struct rx *); -RX_DECL void rx_free_nfa_state (struct rx_nfa_state *); +*rx_nfa_state(struct rx *); +RX_DECL void rx_free_nfa_state(struct rx_nfa_state *); RX_DECL struct rx_nfa_state - *rx_id_to_nfa_state (struct rx *, int); +*rx_id_to_nfa_state(struct rx *, int); RX_DECL struct rx_nfa_edge - *rx_nfa_edge (struct rx *, enum rx_nfa_etype, - struct rx_nfa_state *, - struct rx_nfa_state *); -RX_DECL void rx_free_nfa_edge (struct rx_nfa_edge *); +*rx_nfa_edge(struct rx *, enum rx_nfa_etype, + + struct rx_nfa_state *, struct rx_nfa_state *); +RX_DECL void rx_free_nfa_edge(struct rx_nfa_edge *); static struct rx_possible_future - *rx_possible_future (struct rx *, struct rx_se_list *); -static void rx_free_possible_future (struct rx_possible_future *); -RX_DECL void rx_free_nfa (struct rx *); -RX_DECL int rx_build_nfa (struct rx *, struct rexp_node *, - struct rx_nfa_state **, - struct rx_nfa_state **); -RX_DECL void rx_name_nfa_states (struct rx *); -static int se_list_cmp (void *, void *); -static int se_list_equal (void *, void *); +*rx_possible_future(struct rx *, struct rx_se_list *); +static void rx_free_possible_future(struct rx_possible_future *); +RX_DECL void rx_free_nfa(struct rx *); +RX_DECL int rx_build_nfa(struct rx *, struct rexp_node *, + struct rx_nfa_state **, struct rx_nfa_state **); +RX_DECL void rx_name_nfa_states(struct rx *); +static int se_list_cmp(void *, void *); +static int se_list_equal(void *, void *); static struct rx_se_list - *hash_cons_se_prog (struct rx *, struct rx_hash *, - void *, struct rx_se_list *); +*hash_cons_se_prog(struct rx *, struct rx_hash *, + + void *, struct rx_se_list *); static struct rx_se_list - *hash_se_prog (struct rx *, struct rx_hash *, - struct rx_se_list *); -static int nfa_set_cmp (void *, void *); -static int nfa_set_equal (void *, void *); +*hash_se_prog(struct rx *, struct rx_hash *, struct rx_se_list *); +static int nfa_set_cmp(void *, void *); +static int nfa_set_equal(void *, void *); static struct rx_nfa_state_set - *nfa_set_cons (struct rx *, struct rx_hash *, - struct rx_nfa_state *, - struct rx_nfa_state_set *); +*nfa_set_cons(struct rx *, struct rx_hash *, + + struct rx_nfa_state *, struct rx_nfa_state_set *); static struct rx_nfa_state_set - *nfa_set_enjoin (struct rx *, struct rx_hash *, - struct rx_nfa_state *, - struct rx_nfa_state_set *); +*nfa_set_enjoin(struct rx *, struct rx_hash *, + + struct rx_nfa_state *, struct rx_nfa_state_set *); #endif #ifndef emacs #ifdef SYNTAX_TABLE extern char *re_syntax_table; -#else /* not SYNTAX_TABLE */ +#else /* not SYNTAX_TABLE */ #ifndef RX_WANT_RX_DEFS RX_DECL char re_syntax_table[CHAR_SET_SIZE]; #endif #ifdef __STDC__ -static void -init_syntax_once (void) +static void init_syntax_once(void) #else -static void -init_syntax_once () +static void init_syntax_once() #endif { - register int c; - static int done = 0; + register int c; + static int done = 0; - if (done) - return; + if (done) + return; - bzero (re_syntax_table, sizeof re_syntax_table); + bzero(re_syntax_table, sizeof re_syntax_table); - for (c = 'a'; c <= 'z'; c++) - re_syntax_table[c] = Sword; + for (c = 'a'; c <= 'z'; c++) + re_syntax_table[c] = Sword; - for (c = 'A'; c <= 'Z'; c++) - re_syntax_table[c] = Sword; + for (c = 'A'; c <= 'Z'; c++) + re_syntax_table[c] = Sword; - for (c = '0'; c <= '9'; c++) - re_syntax_table[c] = Sword; + for (c = '0'; c <= '9'; c++) + re_syntax_table[c] = Sword; - re_syntax_table['_'] = Sword; + re_syntax_table['_'] = Sword; - done = 1; + done = 1; } -#endif /* not SYNTAX_TABLE */ -#endif /* not emacs */ +#endif /* not SYNTAX_TABLE */ +#endif /* not emacs */ /* Compile with `-DRX_DEBUG' and use the following flags. * @@ -257,7 +253,7 @@ init_syntax_once () int rx_debug_compile = 0; int rx_debug_trace = 0; -static struct re_pattern_buffer * dbug_rxb = 0; +static struct re_pattern_buffer *dbug_rxb = 0; /* @@ -265,903 +261,845 @@ static struct re_pattern_buffer * dbug_rxb = 0; */ #ifdef __STDC__ typedef void (*side_effect_printer) (struct rx *, void *, FILE *); -static void print_cset (struct rx *, rx_Bitset, FILE *); -static void print_rexp (struct rx *, struct rexp_node *, int, - side_effect_printer, FILE *); -static void print_nfa (struct rx *, struct rx_nfa_state *, - side_effect_printer, FILE *); -static void re_seprint (struct rx *, void *, FILE *); -void print_compiled_pattern (struct re_pattern_buffer *); -void print_fastmap (char *); +static void print_cset(struct rx *, rx_Bitset, FILE *); +static void print_rexp(struct rx *, struct rexp_node *, int, + side_effect_printer, FILE *); +static void print_nfa(struct rx *, struct rx_nfa_state *, + side_effect_printer, FILE *); +static void re_seprint(struct rx *, void *, FILE *); +void print_compiled_pattern(struct re_pattern_buffer *); +void print_fastmap(char *); #else typedef void (*side_effect_printer) (); -static void print_cset (); +static void print_cset(); #endif #ifdef __STDC__ static void -print_rexp (struct rx *rx, - struct rexp_node *node, int depth, - side_effect_printer seprint, FILE * fp) -#else -static void -print_rexp (rx, node, depth, seprint, fp) - struct rx *rx; - struct rexp_node *node; - int depth; - side_effect_printer seprint; - FILE * fp; -#endif -{ - if (!node) - return; - else - { - switch (node->type) - { - case r_cset: - { - fprintf (fp, "%*s", depth, ""); - print_cset (rx, node->params.cset, fp); - fputc ('\n', fp); - break; - } - - case r_opt: - case r_star: - fprintf (fp, "%*s%s\n", depth, "", - node->type == r_opt ? "opt" : "star"); - print_rexp (rx, node->params.pair.left, depth + 3, seprint, fp); - break; - - case r_2phase_star: - fprintf (fp, "%*s2phase star\n", depth, ""); - print_rexp (rx, node->params.pair.right, depth + 3, seprint, fp); - print_rexp (rx, node->params.pair.left, depth + 3, seprint, fp); - break; - +print_rexp(struct rx *rx, + struct rexp_node *node, int depth, + side_effect_printer seprint, FILE * fp) +#else +static void print_rexp(rx, node, depth, seprint, fp) +struct rx *rx; +struct rexp_node *node; +int depth; +side_effect_printer seprint; +FILE *fp; +#endif +{ + if (!node) + return; + else { + switch (node->type) { + case r_cset: + { + fprintf(fp, "%*s", depth, ""); + print_cset(rx, node->params.cset, fp); + fputc('\n', fp); + break; + } - case r_alternate: - case r_concat: - fprintf (fp, "%*s%s\n", depth, "", - node->type == r_alternate ? "alt" : "concat"); - print_rexp (rx, node->params.pair.left, depth + 3, seprint, fp); - print_rexp (rx, node->params.pair.right, depth + 3, seprint, fp); - break; - case r_side_effect: - fprintf (fp, "%*sSide effect: ", depth, ""); - seprint (rx, node->params.side_effect, fp); - fputc ('\n', fp); + case r_opt: + case r_star: + fprintf(fp, "%*s%s\n", depth, "", + node->type == r_opt ? "opt" : "star"); + print_rexp(rx, node->params.pair.left, depth + 3, seprint, fp); + break; + + case r_2phase_star: + fprintf(fp, "%*s2phase star\n", depth, ""); + print_rexp(rx, node->params.pair.right, depth + 3, seprint, + fp); + print_rexp(rx, node->params.pair.left, depth + 3, seprint, fp); + break; + + + case r_alternate: + case r_concat: + fprintf(fp, "%*s%s\n", depth, "", + node->type == r_alternate ? "alt" : "concat"); + print_rexp(rx, node->params.pair.left, depth + 3, seprint, fp); + print_rexp(rx, node->params.pair.right, depth + 3, seprint, + fp); + break; + case r_side_effect: + fprintf(fp, "%*sSide effect: ", depth, ""); + seprint(rx, node->params.side_effect, fp); + fputc('\n', fp); + } } - } } #ifdef __STDC__ static void -print_nfa (struct rx * rx, - struct rx_nfa_state * n, - side_effect_printer seprint, FILE * fp) -#else -static void -print_nfa (rx, n, seprint, fp) - struct rx * rx; - struct rx_nfa_state * n; - side_effect_printer seprint; - FILE * fp; -#endif -{ - while (n) - { - struct rx_nfa_edge *e = n->edges; - struct rx_possible_future *ec = n->futures; - fprintf (fp, "node %d %s\n", n->id, - n->is_final ? "final" : (n->is_start ? "start" : "")); - while (e) - { - fprintf (fp, " edge to %d, ", e->dest->id); - switch (e->type) - { - case ne_epsilon: - fprintf (fp, "epsilon\n"); - break; - case ne_side_effect: - fprintf (fp, "side effect "); - seprint (rx, e->params.side_effect, fp); - fputc ('\n', fp); - break; - case ne_cset: - fprintf (fp, "cset "); - print_cset (rx, e->params.cset, fp); - fputc ('\n', fp); - break; - } - e = e->next; - } +print_nfa(struct rx *rx, + struct rx_nfa_state *n, side_effect_printer seprint, FILE * fp) +#else +static void print_nfa(rx, n, seprint, fp) +struct rx *rx; +struct rx_nfa_state *n; +side_effect_printer seprint; +FILE *fp; +#endif +{ + while (n) { + struct rx_nfa_edge *e = n->edges; + struct rx_possible_future *ec = n->futures; + + fprintf(fp, "node %d %s\n", n->id, + n->is_final ? "final" : (n->is_start ? "start" : "")); + while (e) { + fprintf(fp, " edge to %d, ", e->dest->id); + switch (e->type) { + case ne_epsilon: + fprintf(fp, "epsilon\n"); + break; + case ne_side_effect: + fprintf(fp, "side effect "); + seprint(rx, e->params.side_effect, fp); + fputc('\n', fp); + break; + case ne_cset: + fprintf(fp, "cset "); + print_cset(rx, e->params.cset, fp); + fputc('\n', fp); + break; + } + e = e->next; + } - while (ec) - { - int x; - struct rx_nfa_state_set * s; - struct rx_se_list * l; - fprintf (fp, " eclosure to {"); - for (s = ec->destset; s; s = s->cdr) - fprintf (fp, "%d ", s->car->id); - fprintf (fp, "} ("); - for (l = ec->effects; l; l = l->cdr) - { - seprint (rx, l->car, fp); - fputc (' ', fp); - } - fprintf (fp, ")\n"); - ec = ec->next; + while (ec) { + int x; + struct rx_nfa_state_set *s; + struct rx_se_list *l; + + fprintf(fp, " eclosure to {"); + for (s = ec->destset; s; s = s->cdr) + fprintf(fp, "%d ", s->car->id); + fprintf(fp, "} ("); + for (l = ec->effects; l; l = l->cdr) { + seprint(rx, l->car, fp); + fputc(' ', fp); + } + fprintf(fp, ")\n"); + ec = ec->next; + } + n = n->next; } - n = n->next; - } } -static char * efnames [] = -{ - "bogon", - "re_se_try", - "re_se_pushback", - "re_se_push0", - "re_se_pushpos", - "re_se_chkpos", - "re_se_poppos", - "re_se_at_dot", - "re_se_syntax", - "re_se_not_syntax", - "re_se_begbuf", - "re_se_hat", - "re_se_wordbeg", - "re_se_wordbound", - "re_se_notwordbound", - "re_se_wordend", - "re_se_endbuf", - "re_se_dollar", - "re_se_fail", +static char *efnames[] = { + "bogon", + "re_se_try", + "re_se_pushback", + "re_se_push0", + "re_se_pushpos", + "re_se_chkpos", + "re_se_poppos", + "re_se_at_dot", + "re_se_syntax", + "re_se_not_syntax", + "re_se_begbuf", + "re_se_hat", + "re_se_wordbeg", + "re_se_wordbound", + "re_se_notwordbound", + "re_se_wordend", + "re_se_endbuf", + "re_se_dollar", + "re_se_fail", }; -static char * efnames2[] = -{ - "re_se_win", - "re_se_lparen", - "re_se_rparen", - "re_se_backref", - "re_se_iter", - "re_se_end_iter", - "re_se_tv" +static char *efnames2[] = { + "re_se_win", + "re_se_lparen", + "re_se_rparen", + "re_se_backref", + "re_se_iter", + "re_se_end_iter", + "re_se_tv" }; -static char * inx_names[] = -{ - "rx_backtrack_point", - "rx_do_side_effects", - "rx_cache_miss", - "rx_next_char", - "rx_backtrack", - "rx_error_inx", - "rx_num_instructions" +static char *inx_names[] = { + "rx_backtrack_point", + "rx_do_side_effects", + "rx_cache_miss", + "rx_next_char", + "rx_backtrack", + "rx_error_inx", + "rx_num_instructions" }; #ifdef __STDC__ -static void -re_seprint (struct rx * rx, void * effect, FILE * fp) +static void re_seprint(struct rx *rx, void *effect, FILE * fp) #else -static void -re_seprint (rx, effect, fp) - struct rx * rx; - void * effect; - FILE * fp; -#endif -{ - if ((int)effect < 0) - fputs (efnames[-(int)effect], fp); - else if (dbug_rxb) - { - struct re_se_params * p = &dbug_rxb->se_params[(int)effect]; - fprintf (fp, "%s(%d,%d)", efnames2[p->se], p->op1, p->op2); - } - else - fprintf (fp, "[complex op # %d]", (int)effect); +static void re_seprint(rx, effect, fp) +struct rx *rx; +void *effect; +FILE *fp; +#endif +{ + if ((int) effect < 0) + fputs(efnames[-(int) effect], fp); + else if (dbug_rxb) { + struct re_se_params *p = &dbug_rxb->se_params[(int) effect]; + + fprintf(fp, "%s(%d,%d)", efnames2[p->se], p->op1, p->op2); + } else + fprintf(fp, "[complex op # %d]", (int) effect); } /* These are so the regex.c regression tests will compile. */ -void -print_compiled_pattern (rxb) - struct re_pattern_buffer * rxb; +void print_compiled_pattern(rxb) +struct re_pattern_buffer *rxb; { } -void -print_fastmap (fm) - char * fm; +void print_fastmap(fm) +char *fm; { } -#endif /* RX_DEBUG */ - +#endif /* RX_DEBUG */ + /* This page: Bitsets. Completely unintersting. */ //RX_DECL int rx_bitset_is_equal (int, rx_Bitset, rx_Bitset); -RX_DECL int rx_bitset_is_subset (int, rx_Bitset, rx_Bitset); +RX_DECL int rx_bitset_is_subset(int, rx_Bitset, rx_Bitset); + //RX_DECL int rx_bitset_empty (int, rx_Bitset); -RX_DECL void rx_bitset_null (int, rx_Bitset); -RX_DECL void rx_bitset_complement (int, rx_Bitset); -RX_DECL void rx_bitset_complement (int, rx_Bitset); -RX_DECL void rx_bitset_assign (int, rx_Bitset, rx_Bitset); -RX_DECL void rx_bitset_union (int, rx_Bitset, rx_Bitset); -RX_DECL void rx_bitset_intersection (int, rx_Bitset, rx_Bitset); -RX_DECL void rx_bitset_difference (int, rx_Bitset, rx_Bitset); +RX_DECL void rx_bitset_null(int, rx_Bitset); +RX_DECL void rx_bitset_complement(int, rx_Bitset); +RX_DECL void rx_bitset_complement(int, rx_Bitset); +RX_DECL void rx_bitset_assign(int, rx_Bitset, rx_Bitset); +RX_DECL void rx_bitset_union(int, rx_Bitset, rx_Bitset); +RX_DECL void rx_bitset_intersection(int, rx_Bitset, rx_Bitset); +RX_DECL void rx_bitset_difference(int, rx_Bitset, rx_Bitset); + //RX_DECL void rx_bitset_revdifference (int, rx_Bitset, rx_Bitset); #ifdef emacs -RX_DECL void rx_bitset_xor (int, rx_Bitset, rx_Bitset); +RX_DECL void rx_bitset_xor(int, rx_Bitset, rx_Bitset); #endif -RX_DECL unsigned long - rx_bitset_hash (int, rx_Bitset); +RX_DECL unsigned long rx_bitset_hash(int, rx_Bitset); #if 0 #ifdef __STDC__ -RX_DECL int -rx_bitset_is_equal (int size, rx_Bitset a, rx_Bitset b) +RX_DECL int rx_bitset_is_equal(int size, rx_Bitset a, rx_Bitset b) #else -RX_DECL int -rx_bitset_is_equal (size, a, b) - int size; - rx_Bitset a; - rx_Bitset b; +RX_DECL int rx_bitset_is_equal(size, a, b) +int size; +rx_Bitset a; +rx_Bitset b; #endif { - int x; - RX_subset s = b[0]; - b[0] = ~a[0]; + int x; + RX_subset s = b[0]; - for (x = rx_bitset_numb_subsets(size) - 1; a[x] == b[x]; --x) - ; + b[0] = ~a[0]; - b[0] = s; - return !x && s == a[0]; + for (x = rx_bitset_numb_subsets(size) - 1; a[x] == b[x]; --x); + + b[0] = s; + return !x && s == a[0]; } #endif #ifdef __STDC__ -RX_DECL int -rx_bitset_is_subset (int size, rx_Bitset a, rx_Bitset b) +RX_DECL int rx_bitset_is_subset(int size, rx_Bitset a, rx_Bitset b) #else -RX_DECL int -rx_bitset_is_subset (size, a, b) - int size; - rx_Bitset a; - rx_Bitset b; +RX_DECL int rx_bitset_is_subset(size, a, b) +int size; +rx_Bitset a; +rx_Bitset b; #endif { - int x = rx_bitset_numb_subsets(size) - 1; - while (x-- && (a[x] & b[x]) == a[x]); - return x == -1; + int x = rx_bitset_numb_subsets(size) - 1; + + while (x-- && (a[x] & b[x]) == a[x]); + return x == -1; } #if 0 #ifdef __STDC__ -RX_DECL int -rx_bitset_empty (int size, rx_Bitset set) +RX_DECL int rx_bitset_empty(int size, rx_Bitset set) #else -RX_DECL int -rx_bitset_empty (size, set) - int size; - rx_Bitset set; +RX_DECL int rx_bitset_empty(size, set) +int size; +rx_Bitset set; #endif { - int x; - RX_subset s = set[0]; - set[0] = 1; - for (x = rx_bitset_numb_subsets(size) - 1; !set[x]; --x) - ; - set[0] = s; - return !s; + int x; + RX_subset s = set[0]; + + set[0] = 1; + for (x = rx_bitset_numb_subsets(size) - 1; !set[x]; --x); + set[0] = s; + return !s; } #endif #ifdef __STDC__ -RX_DECL void -rx_bitset_null (int size, rx_Bitset b) +RX_DECL void rx_bitset_null(int size, rx_Bitset b) #else -RX_DECL void -rx_bitset_null (size, b) - int size; - rx_Bitset b; +RX_DECL void rx_bitset_null(size, b) +int size; +rx_Bitset b; #endif { - bzero (b, rx_sizeof_bitset(size)); + bzero(b, rx_sizeof_bitset(size)); } #ifdef __STDC__ -RX_DECL void -rx_bitset_universe (int size, rx_Bitset b) +RX_DECL void rx_bitset_universe(int size, rx_Bitset b) #else -RX_DECL void -rx_bitset_universe (size, b) - int size; - rx_Bitset b; +RX_DECL void rx_bitset_universe(size, b) +int size; +rx_Bitset b; #endif { - int x = rx_bitset_numb_subsets (size); - while (x--) - *b++ = ~(RX_subset)0; + int x = rx_bitset_numb_subsets(size); + + while (x--) + *b++ = ~(RX_subset) 0; } #ifdef __STDC__ -RX_DECL void -rx_bitset_complement (int size, rx_Bitset b) +RX_DECL void rx_bitset_complement(int size, rx_Bitset b) #else -RX_DECL void -rx_bitset_complement (size, b) - int size; - rx_Bitset b; +RX_DECL void rx_bitset_complement(size, b) +int size; +rx_Bitset b; #endif { - int x = rx_bitset_numb_subsets (size); - while (x--) - { - *b = ~*b; - ++b; - } + int x = rx_bitset_numb_subsets(size); + + while (x--) { + *b = ~*b; + ++b; + } } #ifdef __STDC__ -RX_DECL void -rx_bitset_assign (int size, rx_Bitset a, rx_Bitset b) +RX_DECL void rx_bitset_assign(int size, rx_Bitset a, rx_Bitset b) #else -RX_DECL void -rx_bitset_assign (size, a, b) - int size; - rx_Bitset a; - rx_Bitset b; +RX_DECL void rx_bitset_assign(size, a, b) +int size; +rx_Bitset a; +rx_Bitset b; #endif { - int x; - for (x = rx_bitset_numb_subsets(size) - 1; x >=0; --x) - a[x] = b[x]; + int x; + + for (x = rx_bitset_numb_subsets(size) - 1; x >= 0; --x) + a[x] = b[x]; } #ifdef __STDC__ -RX_DECL void -rx_bitset_union (int size, rx_Bitset a, rx_Bitset b) +RX_DECL void rx_bitset_union(int size, rx_Bitset a, rx_Bitset b) #else -RX_DECL void -rx_bitset_union (size, a, b) - int size; - rx_Bitset a; - rx_Bitset b; +RX_DECL void rx_bitset_union(size, a, b) +int size; +rx_Bitset a; +rx_Bitset b; #endif { - int x; - for (x = rx_bitset_numb_subsets(size) - 1; x >=0; --x) - a[x] |= b[x]; + int x; + + for (x = rx_bitset_numb_subsets(size) - 1; x >= 0; --x) + a[x] |= b[x]; } #ifdef __STDC__ -RX_DECL void -rx_bitset_intersection (int size, - rx_Bitset a, rx_Bitset b) +RX_DECL void rx_bitset_intersection(int size, rx_Bitset a, rx_Bitset b) #else -RX_DECL void -rx_bitset_intersection (size, a, b) - int size; - rx_Bitset a; - rx_Bitset b; +RX_DECL void rx_bitset_intersection(size, a, b) +int size; +rx_Bitset a; +rx_Bitset b; #endif { - int x; - for (x = rx_bitset_numb_subsets(size) - 1; x >=0; --x) - a[x] &= b[x]; + int x; + + for (x = rx_bitset_numb_subsets(size) - 1; x >= 0; --x) + a[x] &= b[x]; } #ifdef __STDC__ -RX_DECL void -rx_bitset_difference (int size, rx_Bitset a, rx_Bitset b) +RX_DECL void rx_bitset_difference(int size, rx_Bitset a, rx_Bitset b) #else -RX_DECL void -rx_bitset_difference (size, a, b) - int size; - rx_Bitset a; - rx_Bitset b; +RX_DECL void rx_bitset_difference(size, a, b) +int size; +rx_Bitset a; +rx_Bitset b; #endif { - int x; - for (x = rx_bitset_numb_subsets(size) - 1; x >=0; --x) - a[x] &= ~ b[x]; + int x; + + for (x = rx_bitset_numb_subsets(size) - 1; x >= 0; --x) + a[x] &= ~b[x]; } #if 0 #ifdef __STDC__ -RX_DECL void -rx_bitset_revdifference (int size, - rx_Bitset a, rx_Bitset b) +RX_DECL void rx_bitset_revdifference(int size, rx_Bitset a, rx_Bitset b) #else -RX_DECL void -rx_bitset_revdifference (size, a, b) - int size; - rx_Bitset a; - rx_Bitset b; +RX_DECL void rx_bitset_revdifference(size, a, b) +int size; +rx_Bitset a; +rx_Bitset b; #endif { - int x; - for (x = rx_bitset_numb_subsets(size) - 1; x >=0; --x) - a[x] = ~a[x] & b[x]; + int x; + + for (x = rx_bitset_numb_subsets(size) - 1; x >= 0; --x) + a[x] = ~a[x] & b[x]; } #endif #ifdef emacs #ifdef __STDC__ -RX_DECL void -rx_bitset_xor (int size, rx_Bitset a, rx_Bitset b) +RX_DECL void rx_bitset_xor(int size, rx_Bitset a, rx_Bitset b) #else -RX_DECL void -rx_bitset_xor (size, a, b) - int size; - rx_Bitset a; - rx_Bitset b; -#endif -{ - int x; - for (x = rx_bitset_numb_subsets(size) - 1; x >=0; --x) - a[x] ^= b[x]; -} -#endif - - -#ifdef __STDC__ -RX_DECL unsigned long -rx_bitset_hash (int size, rx_Bitset b) -#else -RX_DECL unsigned long -rx_bitset_hash (size, b) - int size; - rx_Bitset b; -#endif -{ - int x; - unsigned long hash = (unsigned long)rx_bitset_hash; - - for (x = rx_bitset_numb_subsets(size) - 1; x >= 0; --x) - hash ^= rx_bitset_subset_val(b, x); - - return hash; -} - -RX_DECL RX_subset rx_subset_singletons [RX_subset_bits] = -{ - 0x1, - 0x2, - 0x4, - 0x8, - 0x10, - 0x20, - 0x40, - 0x80, - 0x100, - 0x200, - 0x400, - 0x800, - 0x1000, - 0x2000, - 0x4000, - 0x8000, - 0x10000, - 0x20000, - 0x40000, - 0x80000, - 0x100000, - 0x200000, - 0x400000, - 0x800000, - 0x1000000, - 0x2000000, - 0x4000000, - 0x8000000, - 0x10000000, - 0x20000000, - 0x40000000, - 0x80000000 +RX_DECL void rx_bitset_xor(size, a, b) +int size; +rx_Bitset a; +rx_Bitset b; +#endif +{ + int x; + + for (x = rx_bitset_numb_subsets(size) - 1; x >= 0; --x) + a[x] ^= b[x]; +} +#endif + + +#ifdef __STDC__ +RX_DECL unsigned long rx_bitset_hash(int size, rx_Bitset b) +#else +RX_DECL unsigned long rx_bitset_hash(size, b) +int size; +rx_Bitset b; +#endif +{ + int x; + unsigned long hash = (unsigned long) rx_bitset_hash; + + for (x = rx_bitset_numb_subsets(size) - 1; x >= 0; --x) + hash ^= rx_bitset_subset_val(b, x); + + return hash; +} + +RX_DECL RX_subset rx_subset_singletons[RX_subset_bits] = { + 0x1, + 0x2, + 0x4, + 0x8, + 0x10, + 0x20, + 0x40, + 0x80, + 0x100, + 0x200, + 0x400, + 0x800, + 0x1000, + 0x2000, + 0x4000, + 0x8000, + 0x10000, + 0x20000, + 0x40000, + 0x80000, + 0x100000, + 0x200000, + 0x400000, + 0x800000, + 0x1000000, + 0x2000000, + 0x4000000, + 0x8000000, + 0x10000000, + 0x20000000, + 0x40000000, + 0x80000000 }; #ifdef RX_DEBUG #ifdef __STDC__ -static void -print_cset (struct rx *rx, rx_Bitset cset, FILE * fp) +static void print_cset(struct rx *rx, rx_Bitset cset, FILE * fp) #else -static void -print_cset (rx, cset, fp) - struct rx *rx; - rx_Bitset cset; - FILE * fp; -#endif -{ - int x; - fputc ('[', fp); - for (x = 0; x < rx->local_cset_size; ++x) - if (RX_bitset_member (cset, x)) - { - if (isprint(x)) - fputc (x, fp); - else - fprintf (fp, "\\0%o ", x); - } - fputc (']', fp); -} +static void print_cset(rx, cset, fp) +struct rx *rx; +rx_Bitset cset; +FILE *fp; +#endif +{ + int x; -#endif /* RX_DEBUG */ + fputc('[', fp); + for (x = 0; x < rx->local_cset_size; ++x) + if (RX_bitset_member(cset, x)) { + if (isprint(x)) + fputc(x, fp); + else + fprintf(fp, "\\0%o ", x); + } + fputc(']', fp); +} +#endif /* RX_DEBUG */ -static unsigned long rx_hash_masks[4] = -{ - 0x12488421, - 0x96699669, - 0xbe7dd7eb, - 0xffffffff + +static unsigned long rx_hash_masks[4] = { + 0x12488421, + 0x96699669, + 0xbe7dd7eb, + 0xffffffff }; /* Hash tables */ #ifdef __STDC__ -RX_DECL struct rx_hash_item * -rx_hash_find (struct rx_hash * table, - unsigned long hash, - void * value, - struct rx_hash_rules * rules) -#else -RX_DECL struct rx_hash_item * -rx_hash_find (table, hash, value, rules) - struct rx_hash * table; - unsigned long hash; - void * value; - struct rx_hash_rules * rules; -#endif -{ - rx_hash_eq eq = rules->eq; - int maskc = 0; - long mask = rx_hash_masks [0]; - int bucket = (hash & mask) % 13; - - while (table->children [bucket]) - { - table = table->children [bucket]; - ++maskc; - mask = rx_hash_masks[maskc]; - bucket = (hash & mask) % 13; - } +RX_DECL struct rx_hash_item *rx_hash_find(struct rx_hash *table, + unsigned long hash, + void *value, + struct rx_hash_rules *rules) +#else +RX_DECL struct rx_hash_item *rx_hash_find(table, hash, value, rules) +struct rx_hash *table; +unsigned long hash; +void *value; +struct rx_hash_rules *rules; +#endif +{ + rx_hash_eq eq = rules->eq; + int maskc = 0; + long mask = rx_hash_masks[0]; + int bucket = (hash & mask) % 13; + + while (table->children[bucket]) { + table = table->children[bucket]; + ++maskc; + mask = rx_hash_masks[maskc]; + bucket = (hash & mask) % 13; + } - { - struct rx_hash_item * it = table->buckets[bucket]; - while (it) - if (eq (it->data, value)) - return it; - else - it = it->next_same_hash; - } - - return 0; -} - -#ifdef __STDC__ -RX_DECL struct rx_hash_item * -rx_hash_store (struct rx_hash * table, - unsigned long hash, - void * value, - struct rx_hash_rules * rules) -#else -RX_DECL struct rx_hash_item * -rx_hash_store (table, hash, value, rules) - struct rx_hash * table; - unsigned long hash; - void * value; - struct rx_hash_rules * rules; -#endif -{ - rx_hash_eq eq = rules->eq; - int maskc = 0; - long mask = rx_hash_masks[0]; - int bucket = (hash & mask) % 13; - int depth = 0; - - while (table->children [bucket]) - { - table = table->children [bucket]; - ++maskc; - mask = rx_hash_masks[maskc]; - bucket = (hash & mask) % 13; - ++depth; - } - - { - struct rx_hash_item * it = table->buckets[bucket]; - while (it) - if (eq (it->data, value)) - return it; - else - it = it->next_same_hash; - } - - { - if ( (depth < 3) - && (table->bucket_size [bucket] >= 4)) - { - struct rx_hash * newtab = ((struct rx_hash *) - rules->hash_alloc (rules)); - if (!newtab) - goto add_to_bucket; - bzero (newtab, sizeof (*newtab)); - newtab->parent = table; { - struct rx_hash_item * them = table->buckets[bucket]; - unsigned long newmask = rx_hash_masks[maskc + 1]; - while (them) - { - struct rx_hash_item * save = them->next_same_hash; - int new_buck = (them->hash & newmask) % 13; - them->next_same_hash = newtab->buckets[new_buck]; - newtab->buckets[new_buck] = them; - them->table = newtab; - them = save; - ++newtab->bucket_size[new_buck]; - ++newtab->refs; - } - table->refs = (table->refs - table->bucket_size[bucket] + 1); - table->bucket_size[bucket] = 0; - table->buckets[bucket] = 0; - table->children[bucket] = newtab; - table = newtab; - bucket = (hash & newmask) % 13; + struct rx_hash_item *it = table->buckets[bucket]; + + while (it) + if (eq(it->data, value)) + return it; + else + it = it->next_same_hash; } - } - } - add_to_bucket: - { - struct rx_hash_item * it = ((struct rx_hash_item *) - rules->hash_item_alloc (rules, value)); - if (!it) - return 0; - it->hash = hash; - it->table = table; - /* DATA and BINDING are to be set in hash_item_alloc */ - it->next_same_hash = table->buckets [bucket]; - table->buckets[bucket] = it; - ++table->bucket_size [bucket]; - ++table->refs; - return it; - } -} + return 0; +} #ifdef __STDC__ -RX_DECL void -rx_hash_free (struct rx_hash_item * it, struct rx_hash_rules * rules) +RX_DECL struct rx_hash_item *rx_hash_store(struct rx_hash *table, + unsigned long hash, + void *value, + struct rx_hash_rules *rules) #else -RX_DECL void -rx_hash_free (it, rules) - struct rx_hash_item * it; - struct rx_hash_rules * rules; -#endif -{ - if (it) - { - struct rx_hash * table = it->table; - unsigned long hash = it->hash; - int depth = (table->parent - ? (table->parent->parent - ? (table->parent->parent->parent - ? 3 - : 2) - : 1) - : 0); - int bucket = (hash & rx_hash_masks [depth]) % 13; - struct rx_hash_item ** pos = &table->buckets [bucket]; - - while (*pos != it) - pos = &(*pos)->next_same_hash; - *pos = it->next_same_hash; - rules->free_hash_item (it, rules); - --table->bucket_size[bucket]; - --table->refs; - while (!table->refs && depth) +RX_DECL struct rx_hash_item *rx_hash_store(table, hash, value, rules) +struct rx_hash *table; +unsigned long hash; +void *value; +struct rx_hash_rules *rules; +#endif +{ + rx_hash_eq eq = rules->eq; + int maskc = 0; + long mask = rx_hash_masks[0]; + int bucket = (hash & mask) % 13; + int depth = 0; + + while (table->children[bucket]) { + table = table->children[bucket]; + ++maskc; + mask = rx_hash_masks[maskc]; + bucket = (hash & mask) % 13; + ++depth; + } + { - struct rx_hash * save = table; - table = table->parent; - --depth; - bucket = (hash & rx_hash_masks [depth]) % 13; - --table->refs; - table->children[bucket] = 0; - rules->free_hash (save, rules); + struct rx_hash_item *it = table->buckets[bucket]; + + while (it) + if (eq(it->data, value)) + return it; + else + it = it->next_same_hash; + } + + { + if ((depth < 3) + && (table->bucket_size[bucket] >= 4)) { + struct rx_hash *newtab = ((struct rx_hash *) + rules->hash_alloc(rules)); + + if (!newtab) + goto add_to_bucket; + bzero(newtab, sizeof(*newtab)); + newtab->parent = table; + { + struct rx_hash_item *them = table->buckets[bucket]; + unsigned long newmask = rx_hash_masks[maskc + 1]; + + while (them) { + struct rx_hash_item *save = them->next_same_hash; + int new_buck = (them->hash & newmask) % 13; + + them->next_same_hash = newtab->buckets[new_buck]; + newtab->buckets[new_buck] = them; + them->table = newtab; + them = save; + ++newtab->bucket_size[new_buck]; + ++newtab->refs; + } + table->refs = + (table->refs - table->bucket_size[bucket] + 1); + table->bucket_size[bucket] = 0; + table->buckets[bucket] = 0; + table->children[bucket] = newtab; + table = newtab; + bucket = (hash & newmask) % 13; + } + } + } + add_to_bucket: + { + struct rx_hash_item *it = ((struct rx_hash_item *) + rules->hash_item_alloc(rules, value)); + + if (!it) + return 0; + it->hash = hash; + it->table = table; + /* DATA and BINDING are to be set in hash_item_alloc */ + it->next_same_hash = table->buckets[bucket]; + table->buckets[bucket] = it; + ++table->bucket_size[bucket]; + ++table->refs; + return it; + } +} + + +#ifdef __STDC__ +RX_DECL void +rx_hash_free(struct rx_hash_item *it, struct rx_hash_rules *rules) +#else +RX_DECL void rx_hash_free(it, rules) +struct rx_hash_item *it; +struct rx_hash_rules *rules; +#endif +{ + if (it) { + struct rx_hash *table = it->table; + unsigned long hash = it->hash; + int depth = (table->parent + ? (table->parent->parent + ? (table->parent->parent->parent ? 3 : 2) + : 1) + : 0); + int bucket = (hash & rx_hash_masks[depth]) % 13; + struct rx_hash_item **pos = &table->buckets[bucket]; + + while (*pos != it) + pos = &(*pos)->next_same_hash; + *pos = it->next_same_hash; + rules->free_hash_item(it, rules); + --table->bucket_size[bucket]; + --table->refs; + while (!table->refs && depth) { + struct rx_hash *save = table; + + table = table->parent; + --depth; + bucket = (hash & rx_hash_masks[depth]) % 13; + --table->refs; + table->children[bucket] = 0; + rules->free_hash(save, rules); + } } - } } #ifdef __STDC__ RX_DECL void -rx_free_hash_table (struct rx_hash * tab, rx_hash_freefn freefn, - struct rx_hash_rules * rules) +rx_free_hash_table(struct rx_hash *tab, rx_hash_freefn freefn, + struct rx_hash_rules *rules) #else -RX_DECL void -rx_free_hash_table (tab, freefn, rules) - struct rx_hash * tab; - rx_hash_freefn freefn; - struct rx_hash_rules * rules; -#endif -{ - int x; - - for (x = 0; x < 13; ++x) - if (tab->children[x]) - { - rx_free_hash_table (tab->children[x], freefn, rules); - rules->free_hash (tab->children[x], rules); - } - else - { - struct rx_hash_item * them = tab->buckets[x]; - while (them) - { - struct rx_hash_item * that = them; - them = that->next_same_hash; - freefn (that); - rules->free_hash_item (that, rules); - } - } -} +RX_DECL void rx_free_hash_table(tab, freefn, rules) +struct rx_hash *tab; +rx_hash_freefn freefn; +struct rx_hash_rules *rules; +#endif +{ + int x; + for (x = 0; x < 13; ++x) + if (tab->children[x]) { + rx_free_hash_table(tab->children[x], freefn, rules); + rules->free_hash(tab->children[x], rules); + } else { + struct rx_hash_item *them = tab->buckets[x]; + while (them) { + struct rx_hash_item *that = them; + + them = that->next_same_hash; + freefn(that); + rules->free_hash_item(that, rules); + } + } +} + + /* Utilities for manipulating bitset represntations of characters sets. */ #ifdef __STDC__ -RX_DECL rx_Bitset -rx_cset (struct rx *rx) +RX_DECL rx_Bitset rx_cset(struct rx *rx) #else -RX_DECL rx_Bitset -rx_cset (rx) - struct rx *rx; +RX_DECL rx_Bitset rx_cset(rx) +struct rx *rx; #endif { - rx_Bitset b = (rx_Bitset) malloc (rx_sizeof_bitset (rx->local_cset_size)); - if (b) - rx_bitset_null (rx->local_cset_size, b); - return b; + rx_Bitset b = + + (rx_Bitset) malloc(rx_sizeof_bitset(rx->local_cset_size)); + if (b) + rx_bitset_null(rx->local_cset_size, b); + return b; } #ifdef __STDC__ -RX_DECL rx_Bitset -rx_copy_cset (struct rx *rx, rx_Bitset a) +RX_DECL rx_Bitset rx_copy_cset(struct rx * rx, rx_Bitset a) #else -RX_DECL rx_Bitset -rx_copy_cset (rx, a) - struct rx *rx; - rx_Bitset a; +RX_DECL rx_Bitset rx_copy_cset(rx, a) +struct rx *rx; +rx_Bitset a; #endif { - rx_Bitset cs = rx_cset (rx); + rx_Bitset cs = rx_cset(rx); - if (cs) - rx_bitset_union (rx->local_cset_size, cs, a); + if (cs) + rx_bitset_union(rx->local_cset_size, cs, a); - return cs; + return cs; } #ifdef __STDC__ -RX_DECL void -rx_free_cset (struct rx * rx, rx_Bitset c) +RX_DECL void rx_free_cset(struct rx *rx, rx_Bitset c) #else -RX_DECL void -rx_free_cset (rx, c) - struct rx * rx; - rx_Bitset c; +RX_DECL void rx_free_cset(rx, c) +struct rx *rx; +rx_Bitset c; #endif { - if (c) - free ((char *)c); + if (c) + free((char *) c); } - + /* Hash table memory allocation policy for the regexp compiler */ #ifdef __STDC__ -static struct rx_hash * -compiler_hash_alloc (struct rx_hash_rules * rules) +static struct rx_hash *compiler_hash_alloc(struct rx_hash_rules *rules) #else -static struct rx_hash * -compiler_hash_alloc (rules) - struct rx_hash_rules * rules; +static struct rx_hash *compiler_hash_alloc(rules) +struct rx_hash_rules *rules; #endif { - return (struct rx_hash *)malloc (sizeof (struct rx_hash)); + return (struct rx_hash *) malloc(sizeof(struct rx_hash)); } #ifdef __STDC__ -static struct rx_hash_item * -compiler_hash_item_alloc (struct rx_hash_rules * rules, void * value) +static struct rx_hash_item *compiler_hash_item_alloc(struct rx_hash_rules + *rules, void *value) #else -static struct rx_hash_item * -compiler_hash_item_alloc (rules, value) - struct rx_hash_rules * rules; - void * value; +static struct rx_hash_item *compiler_hash_item_alloc(rules, value) +struct rx_hash_rules *rules; +void *value; #endif { - struct rx_hash_item * it; - it = (struct rx_hash_item *)malloc (sizeof (*it)); - if (it) - { - it->data = value; - it->binding = 0; - } - return it; + struct rx_hash_item *it; + + it = (struct rx_hash_item *) malloc(sizeof(*it)); + if (it) { + it->data = value; + it->binding = 0; + } + return it; } #ifdef __STDC__ static void -compiler_free_hash (struct rx_hash * tab, - struct rx_hash_rules * rules) +compiler_free_hash(struct rx_hash *tab, struct rx_hash_rules *rules) #else -static void -compiler_free_hash (tab, rules) - struct rx_hash * tab; - struct rx_hash_rules * rules; +static void compiler_free_hash(tab, rules) +struct rx_hash *tab; +struct rx_hash_rules *rules; #endif { - free ((char *)tab); + free((char *) tab); } #ifdef __STDC__ static void -compiler_free_hash_item (struct rx_hash_item * item, - struct rx_hash_rules * rules) +compiler_free_hash_item(struct rx_hash_item *item, + struct rx_hash_rules *rules) #else -static void -compiler_free_hash_item (item, rules) - struct rx_hash_item * item; - struct rx_hash_rules * rules; +static void compiler_free_hash_item(item, rules) +struct rx_hash_item *item; +struct rx_hash_rules *rules; #endif { - free ((char *)item); + free((char *) item); } - + /* This page: REXP_NODE (expression tree) structures. */ #ifdef __STDC__ -RX_DECL struct rexp_node * -rexp_node (struct rx *rx, - enum rexp_node_type type) +RX_DECL struct rexp_node *rexp_node(struct rx *rx, + enum rexp_node_type type) #else -RX_DECL struct rexp_node * -rexp_node (rx, type) - struct rx *rx; - enum rexp_node_type type; +RX_DECL struct rexp_node *rexp_node(rx, type) +struct rx *rx; +enum rexp_node_type type; #endif { - struct rexp_node *n; + struct rexp_node *n; - n = (struct rexp_node *)malloc (sizeof (*n)); - if (n) - { - bzero (n, sizeof (*n)); - n->type = type; - } - return n; + n = (struct rexp_node *) malloc(sizeof(*n)); + if (n) { + bzero(n, sizeof(*n)); + n->type = type; + } + return n; } @@ -1169,303 +1107,271 @@ rexp_node (rx, type) * can be freed using rx_free_cset. */ #ifdef __STDC__ -RX_DECL struct rexp_node * -rx_mk_r_cset (struct rx * rx, - rx_Bitset b) +RX_DECL struct rexp_node *rx_mk_r_cset(struct rx *rx, rx_Bitset b) #else -RX_DECL struct rexp_node * -rx_mk_r_cset (rx, b) - struct rx * rx; - rx_Bitset b; +RX_DECL struct rexp_node *rx_mk_r_cset(rx, b) +struct rx *rx; +rx_Bitset b; #endif { - struct rexp_node * n = rexp_node (rx, r_cset); - if (n) - n->params.cset = b; - return n; + struct rexp_node *n = rexp_node(rx, r_cset); + + if (n) + n->params.cset = b; + return n; } #ifdef __STDC__ -RX_DECL struct rexp_node * -rx_mk_r_concat (struct rx * rx, - struct rexp_node * a, - struct rexp_node * b) +RX_DECL struct rexp_node *rx_mk_r_concat(struct rx *rx, + struct rexp_node *a, + struct rexp_node *b) #else -RX_DECL struct rexp_node * -rx_mk_r_concat (rx, a, b) - struct rx * rx; - struct rexp_node * a; - struct rexp_node * b; +RX_DECL struct rexp_node *rx_mk_r_concat(rx, a, b) +struct rx *rx; +struct rexp_node *a; +struct rexp_node *b; #endif { - struct rexp_node * n = rexp_node (rx, r_concat); - if (n) - { - n->params.pair.left = a; - n->params.pair.right = b; - } - return n; + struct rexp_node *n = rexp_node(rx, r_concat); + + if (n) { + n->params.pair.left = a; + n->params.pair.right = b; + } + return n; } #ifdef __STDC__ -RX_DECL struct rexp_node * -rx_mk_r_alternate (struct rx * rx, - struct rexp_node * a, - struct rexp_node * b) +RX_DECL struct rexp_node *rx_mk_r_alternate(struct rx *rx, + struct rexp_node *a, + struct rexp_node *b) #else -RX_DECL struct rexp_node * -rx_mk_r_alternate (rx, a, b) - struct rx * rx; - struct rexp_node * a; - struct rexp_node * b; +RX_DECL struct rexp_node *rx_mk_r_alternate(rx, a, b) +struct rx *rx; +struct rexp_node *a; +struct rexp_node *b; #endif { - struct rexp_node * n = rexp_node (rx, r_alternate); - if (n) - { - n->params.pair.left = a; - n->params.pair.right = b; - } - return n; + struct rexp_node *n = rexp_node(rx, r_alternate); + + if (n) { + n->params.pair.left = a; + n->params.pair.right = b; + } + return n; } #ifdef __STDC__ -RX_DECL struct rexp_node * -rx_mk_r_opt (struct rx * rx, - struct rexp_node * a) +RX_DECL struct rexp_node *rx_mk_r_opt(struct rx *rx, struct rexp_node *a) #else -RX_DECL struct rexp_node * -rx_mk_r_opt (rx, a) - struct rx * rx; - struct rexp_node * a; +RX_DECL struct rexp_node *rx_mk_r_opt(rx, a) +struct rx *rx; +struct rexp_node *a; #endif { - struct rexp_node * n = rexp_node (rx, r_opt); - if (n) - { - n->params.pair.left = a; - n->params.pair.right = 0; - } - return n; + struct rexp_node *n = rexp_node(rx, r_opt); + + if (n) { + n->params.pair.left = a; + n->params.pair.right = 0; + } + return n; } #ifdef __STDC__ -RX_DECL struct rexp_node * -rx_mk_r_star (struct rx * rx, - struct rexp_node * a) +RX_DECL struct rexp_node *rx_mk_r_star(struct rx *rx, struct rexp_node *a) #else -RX_DECL struct rexp_node * -rx_mk_r_star (rx, a) - struct rx * rx; - struct rexp_node * a; +RX_DECL struct rexp_node *rx_mk_r_star(rx, a) +struct rx *rx; +struct rexp_node *a; #endif { - struct rexp_node * n = rexp_node (rx, r_star); - if (n) - { - n->params.pair.left = a; - n->params.pair.right = 0; - } - return n; + struct rexp_node *n = rexp_node(rx, r_star); + + if (n) { + n->params.pair.left = a; + n->params.pair.right = 0; + } + return n; } #ifdef __STDC__ -RX_DECL struct rexp_node * -rx_mk_r_2phase_star (struct rx * rx, - struct rexp_node * a, - struct rexp_node * b) +RX_DECL struct rexp_node *rx_mk_r_2phase_star(struct rx *rx, + struct rexp_node *a, + struct rexp_node *b) #else -RX_DECL struct rexp_node * -rx_mk_r_2phase_star (rx, a, b) - struct rx * rx; - struct rexp_node * a; - struct rexp_node * b; +RX_DECL struct rexp_node *rx_mk_r_2phase_star(rx, a, b) +struct rx *rx; +struct rexp_node *a; +struct rexp_node *b; #endif { - struct rexp_node * n = rexp_node (rx, r_2phase_star); - if (n) - { - n->params.pair.left = a; - n->params.pair.right = b; - } - return n; + struct rexp_node *n = rexp_node(rx, r_2phase_star); + + if (n) { + n->params.pair.left = a; + n->params.pair.right = b; + } + return n; } #ifdef __STDC__ -RX_DECL struct rexp_node * -rx_mk_r_side_effect (struct rx * rx, - rx_side_effect a) +RX_DECL struct rexp_node *rx_mk_r_side_effect(struct rx *rx, + rx_side_effect a) #else -RX_DECL struct rexp_node * -rx_mk_r_side_effect (rx, a) - struct rx * rx; - rx_side_effect a; +RX_DECL struct rexp_node *rx_mk_r_side_effect(rx, a) +struct rx *rx; +rx_side_effect a; #endif { - struct rexp_node * n = rexp_node (rx, r_side_effect); - if (n) - { - n->params.side_effect = a; - n->params.pair.right = 0; - } - return n; + struct rexp_node *n = rexp_node(rx, r_side_effect); + + if (n) { + n->params.side_effect = a; + n->params.pair.right = 0; + } + return n; } #if 0 #ifdef __STDC__ -RX_DECL struct rexp_node * -rx_mk_r_data (struct rx * rx, - void * a) +RX_DECL struct rexp_node *rx_mk_r_data(struct rx *rx, void *a) #else -RX_DECL struct rexp_node * -rx_mk_r_data (rx, a) - struct rx * rx; - void * a; +RX_DECL struct rexp_node *rx_mk_r_data(rx, a) +struct rx *rx; +void *a; #endif { - struct rexp_node * n = rexp_node (rx, r_data); - if (n) - { - n->params.pair.left = a; - n->params.pair.right = 0; - } - return n; + struct rexp_node *n = rexp_node(rx, r_data); + + if (n) { + n->params.pair.left = a; + n->params.pair.right = 0; + } + return n; } #endif #ifdef __STDC__ -RX_DECL void -rx_free_rexp (struct rx * rx, struct rexp_node * node) +RX_DECL void rx_free_rexp(struct rx *rx, struct rexp_node *node) #else -RX_DECL void -rx_free_rexp (rx, node) - struct rx * rx; - struct rexp_node * node; +RX_DECL void rx_free_rexp(rx, node) +struct rx *rx; +struct rexp_node *node; #endif { - if (node) - { - switch (node->type) - { - case r_cset: - if (node->params.cset) - rx_free_cset (rx, node->params.cset); + if (node) { + switch (node->type) { + case r_cset: + if (node->params.cset) + rx_free_cset(rx, node->params.cset); - case r_side_effect: - break; - - case r_concat: - case r_alternate: - case r_2phase_star: - case r_opt: - case r_star: - rx_free_rexp (rx, node->params.pair.left); - rx_free_rexp (rx, node->params.pair.right); - break; + case r_side_effect: + break; - case r_data: - /* This shouldn't occur. */ - break; + case r_concat: + case r_alternate: + case r_2phase_star: + case r_opt: + case r_star: + rx_free_rexp(rx, node->params.pair.left); + rx_free_rexp(rx, node->params.pair.right); + break; + + case r_data: + /* This shouldn't occur. */ + break; + } + free((char *) node); } - free ((char *)node); - } } #ifdef __STDC__ -RX_DECL struct rexp_node * -rx_copy_rexp (struct rx *rx, - struct rexp_node *node) +RX_DECL struct rexp_node *rx_copy_rexp(struct rx *rx, + struct rexp_node *node) #else -RX_DECL struct rexp_node * -rx_copy_rexp (rx, node) - struct rx *rx; - struct rexp_node *node; +RX_DECL struct rexp_node *rx_copy_rexp(rx, node) +struct rx *rx; +struct rexp_node *node; #endif { - if (!node) - return 0; - else - { - struct rexp_node *n = rexp_node (rx, node->type); - if (!n) - return 0; - switch (node->type) - { - case r_cset: - n->params.cset = rx_copy_cset (rx, node->params.cset); - if (!n->params.cset) - { - rx_free_rexp (rx, n); - return 0; - } - break; - - case r_side_effect: - n->params.side_effect = node->params.side_effect; - break; + if (!node) + return 0; + else { + struct rexp_node *n = rexp_node(rx, node->type); - case r_concat: - case r_alternate: - case r_opt: - case r_2phase_star: - case r_star: - n->params.pair.left = - rx_copy_rexp (rx, node->params.pair.left); - n->params.pair.right = - rx_copy_rexp (rx, node->params.pair.right); - if ( (node->params.pair.left && !n->params.pair.left) - || (node->params.pair.right && !n->params.pair.right)) - { - rx_free_rexp (rx, n); - return 0; - } - break; - case r_data: - /* shouldn't happen */ - break; + if (!n) + return 0; + switch (node->type) { + case r_cset: + n->params.cset = rx_copy_cset(rx, node->params.cset); + if (!n->params.cset) { + rx_free_rexp(rx, n); + return 0; + } + break; + + case r_side_effect: + n->params.side_effect = node->params.side_effect; + break; + + case r_concat: + case r_alternate: + case r_opt: + case r_2phase_star: + case r_star: + n->params.pair.left = rx_copy_rexp(rx, node->params.pair.left); + n->params.pair.right = + rx_copy_rexp(rx, node->params.pair.right); + if ((node->params.pair.left && !n->params.pair.left) + || (node->params.pair.right && !n->params.pair.right)) { + rx_free_rexp(rx, n); + return 0; + } + break; + case r_data: + /* shouldn't happen */ + break; + } + return n; } - return n; - } } + - /* This page: functions to build and destroy graphs that describe nfa's */ /* Constructs a new nfa node. */ #ifdef __STDC__ -RX_DECL struct rx_nfa_state * -rx_nfa_state (struct rx *rx) +RX_DECL struct rx_nfa_state *rx_nfa_state(struct rx *rx) #else -RX_DECL struct rx_nfa_state * -rx_nfa_state (rx) - struct rx *rx; +RX_DECL struct rx_nfa_state *rx_nfa_state(rx) +struct rx *rx; #endif { - struct rx_nfa_state * n = (struct rx_nfa_state *)malloc (sizeof (*n)); - if (!n) - return 0; - bzero (n, sizeof (*n)); - n->next = rx->nfa_states; - rx->nfa_states = n; - return n; + struct rx_nfa_state *n = (struct rx_nfa_state *) malloc(sizeof(*n)); + + if (!n) + return 0; + bzero(n, sizeof(*n)); + n->next = rx->nfa_states; + rx->nfa_states = n; + return n; } #ifdef __STDC__ -RX_DECL void -rx_free_nfa_state (struct rx_nfa_state * n) +RX_DECL void rx_free_nfa_state(struct rx_nfa_state *n) #else -RX_DECL void -rx_free_nfa_state (n) - struct rx_nfa_state * n; +RX_DECL void rx_free_nfa_state(n) +struct rx_nfa_state *n; #endif { - free ((char *)n); + free((char *) n); } @@ -1473,21 +1379,19 @@ rx_free_nfa_state (n) * assigned after the nfa has been built. */ #ifdef __STDC__ -RX_DECL struct rx_nfa_state * -rx_id_to_nfa_state (struct rx * rx, - int id) +RX_DECL struct rx_nfa_state *rx_id_to_nfa_state(struct rx *rx, int id) #else -RX_DECL struct rx_nfa_state * -rx_id_to_nfa_state (rx, id) - struct rx * rx; - int id; +RX_DECL struct rx_nfa_state *rx_id_to_nfa_state(rx, id) +struct rx *rx; +int id; #endif { - struct rx_nfa_state * n; - for (n = rx->nfa_states; n; n = n->next) - if (n->id == id) - return n; - return 0; + struct rx_nfa_state *n; + + for (n = rx->nfa_states; n; n = n->next) + if (n->id == id) + return n; + return 0; } @@ -1496,133 +1400,125 @@ rx_id_to_nfa_state (rx, id) */ #ifdef __STDC__ -RX_DECL struct rx_nfa_edge * -rx_nfa_edge (struct rx *rx, - enum rx_nfa_etype type, - struct rx_nfa_state *start, - struct rx_nfa_state *dest) +RX_DECL struct rx_nfa_edge *rx_nfa_edge(struct rx *rx, + enum rx_nfa_etype type, + struct rx_nfa_state *start, + struct rx_nfa_state *dest) #else -RX_DECL struct rx_nfa_edge * -rx_nfa_edge (rx, type, start, dest) - struct rx *rx; - enum rx_nfa_etype type; - struct rx_nfa_state *start; - struct rx_nfa_state *dest; +RX_DECL struct rx_nfa_edge *rx_nfa_edge(rx, type, start, dest) +struct rx *rx; +enum rx_nfa_etype type; +struct rx_nfa_state *start; +struct rx_nfa_state *dest; #endif { - struct rx_nfa_edge *e; - e = (struct rx_nfa_edge *)malloc (sizeof (*e)); - if (!e) - return 0; - e->next = start->edges; - start->edges = e; - e->type = type; - e->dest = dest; - return e; + struct rx_nfa_edge *e; + + e = (struct rx_nfa_edge *) malloc(sizeof(*e)); + if (!e) + return 0; + e->next = start->edges; + start->edges = e; + e->type = type; + e->dest = dest; + return e; } #ifdef __STDC__ -RX_DECL void -rx_free_nfa_edge (struct rx_nfa_edge * e) +RX_DECL void rx_free_nfa_edge(struct rx_nfa_edge *e) #else -RX_DECL void -rx_free_nfa_edge (e) - struct rx_nfa_edge * e; +RX_DECL void rx_free_nfa_edge(e) +struct rx_nfa_edge *e; #endif { - free ((char *)e); + free((char *) e); } /* This constructs a POSSIBLE_FUTURE, which is a kind epsilon-closure * of an NFA. These are added to an nfa automaticly by eclose_nfa. - */ + */ #ifdef __STDC__ -static struct rx_possible_future * -rx_possible_future (struct rx * rx, - struct rx_se_list * effects) +static struct rx_possible_future *rx_possible_future(struct rx *rx, struct rx_se_list + *effects) #else -static struct rx_possible_future * -rx_possible_future (rx, effects) - struct rx * rx; - struct rx_se_list * effects; +static struct rx_possible_future *rx_possible_future(rx, effects) +struct rx *rx; +struct rx_se_list *effects; #endif { - struct rx_possible_future *ec; - ec = (struct rx_possible_future *) malloc (sizeof (*ec)); - if (!ec) - return 0; - ec->destset = 0; - ec->next = 0; - ec->effects = effects; - return ec; + struct rx_possible_future *ec; + + ec = (struct rx_possible_future *) malloc(sizeof(*ec)); + if (!ec) + return 0; + ec->destset = 0; + ec->next = 0; + ec->effects = effects; + return ec; } #ifdef __STDC__ -static void -rx_free_possible_future (struct rx_possible_future * pf) +static void rx_free_possible_future(struct rx_possible_future *pf) #else -static void -rx_free_possible_future (pf) - struct rx_possible_future * pf; +static void rx_free_possible_future(pf) +struct rx_possible_future *pf; #endif { - free ((char *)pf); + free((char *) pf); } #ifdef __STDC__ -RX_DECL void -rx_free_nfa (struct rx *rx) +RX_DECL void rx_free_nfa(struct rx *rx) #else -RX_DECL void -rx_free_nfa (rx) - struct rx *rx; +RX_DECL void rx_free_nfa(rx) +struct rx *rx; #endif { - while (rx->nfa_states) - { - while (rx->nfa_states->edges) - { - switch (rx->nfa_states->edges->type) - { - case ne_cset: - rx_free_cset (rx, rx->nfa_states->edges->params.cset); - break; - default: - break; - } - { - struct rx_nfa_edge * e; - e = rx->nfa_states->edges; - rx->nfa_states->edges = rx->nfa_states->edges->next; - rx_free_nfa_edge (e); - } - } /* while (rx->nfa_states->edges) */ - { - /* Iterate over the partial epsilon closures of rx->nfa_states */ - struct rx_possible_future * pf = rx->nfa_states->futures; - while (pf) - { - struct rx_possible_future * pft = pf; - pf = pf->next; - rx_free_possible_future (pft); - } - } - { - struct rx_nfa_state *n; - n = rx->nfa_states; - rx->nfa_states = rx->nfa_states->next; - rx_free_nfa_state (n); - } - } -} - + while (rx->nfa_states) { + while (rx->nfa_states->edges) { + switch (rx->nfa_states->edges->type) { + case ne_cset: + rx_free_cset(rx, rx->nfa_states->edges->params.cset); + break; + default: + break; + } + { + struct rx_nfa_edge *e; + e = rx->nfa_states->edges; + rx->nfa_states->edges = rx->nfa_states->edges->next; + rx_free_nfa_edge(e); + } + } /* while (rx->nfa_states->edges) */ + { + /* Iterate over the partial epsilon closures of rx->nfa_states */ + struct rx_possible_future *pf = rx->nfa_states->futures; + + while (pf) { + struct rx_possible_future *pft = pf; + + pf = pf->next; + rx_free_possible_future(pft); + } + } + { + struct rx_nfa_state *n; + + n = rx->nfa_states; + rx->nfa_states = rx->nfa_states->next; + rx_free_nfa_state(n); + } + } +} + + /* This page: translating a pattern expression into an nfa and doing the * static part of the nfa->super-nfa translation. */ @@ -1639,133 +1535,130 @@ rx_free_nfa (rx) #ifdef __STDC__ RX_DECL int -rx_build_nfa (struct rx *rx, - struct rexp_node *rexp, - struct rx_nfa_state **start, - struct rx_nfa_state **end) +rx_build_nfa(struct rx *rx, + struct rexp_node *rexp, + struct rx_nfa_state **start, struct rx_nfa_state **end) #else -RX_DECL int -rx_build_nfa (rx, rexp, start, end) - struct rx *rx; - struct rexp_node *rexp; - struct rx_nfa_state **start; - struct rx_nfa_state **end; +RX_DECL int rx_build_nfa(rx, rexp, start, end) +struct rx *rx; +struct rexp_node *rexp; +struct rx_nfa_state **start; +struct rx_nfa_state **end; #endif { - struct rx_nfa_edge *edge; + struct rx_nfa_edge *edge; - /* Start & end nodes may have been allocated by the caller. */ - *start = *start ? *start : rx_nfa_state (rx); + /* Start & end nodes may have been allocated by the caller. */ + *start = *start ? *start : rx_nfa_state(rx); - if (!*start) - return 0; + if (!*start) + return 0; - if (!rexp) - { - *end = *start; - return 1; - } + if (!rexp) { + *end = *start; + return 1; + } - *end = *end ? *end : rx_nfa_state (rx); + *end = *end ? *end : rx_nfa_state(rx); - if (!*end) - { - rx_free_nfa_state (*start); - return 0; - } + if (!*end) { + rx_free_nfa_state(*start); + return 0; + } - switch (rexp->type) - { - case r_data: - return 0; + switch (rexp->type) { + case r_data: + return 0; - case r_cset: - edge = rx_nfa_edge (rx, ne_cset, *start, *end); - if (!edge) - return 0; - edge->params.cset = rx_copy_cset (rx, rexp->params.cset); - if (!edge->params.cset) + case r_cset: + edge = rx_nfa_edge(rx, ne_cset, *start, *end); + if (!edge) + return 0; + edge->params.cset = rx_copy_cset(rx, rexp->params.cset); + if (!edge->params.cset) { + rx_free_nfa_edge(edge); + return 0; + } + return 1; + + case r_opt: + return (rx_build_nfa(rx, rexp->params.pair.left, start, end) + && rx_nfa_edge(rx, ne_epsilon, *start, *end)); + + case r_star: { - rx_free_nfa_edge (edge); - return 0; + struct rx_nfa_state *star_start = 0; + struct rx_nfa_state *star_end = 0; + + return (rx_build_nfa(rx, rexp->params.pair.left, + &star_start, &star_end) + && star_start + && star_end + && rx_nfa_edge(rx, ne_epsilon, star_start, star_end) + && rx_nfa_edge(rx, ne_epsilon, *start, star_start) + && rx_nfa_edge(rx, ne_epsilon, star_end, *end) + + && rx_nfa_edge(rx, ne_epsilon, star_end, star_start)); } - return 1; - - case r_opt: - return (rx_build_nfa (rx, rexp->params.pair.left, start, end) - && rx_nfa_edge (rx, ne_epsilon, *start, *end)); - - case r_star: - { - struct rx_nfa_state * star_start = 0; - struct rx_nfa_state * star_end = 0; - return (rx_build_nfa (rx, rexp->params.pair.left, - &star_start, &star_end) - && star_start - && star_end - && rx_nfa_edge (rx, ne_epsilon, star_start, star_end) - && rx_nfa_edge (rx, ne_epsilon, *start, star_start) - && rx_nfa_edge (rx, ne_epsilon, star_end, *end) - - && rx_nfa_edge (rx, ne_epsilon, star_end, star_start)); - } - - case r_2phase_star: - { - struct rx_nfa_state * star_start = 0; - struct rx_nfa_state * star_end = 0; - struct rx_nfa_state * loop_exp_start = 0; - struct rx_nfa_state * loop_exp_end = 0; - - return (rx_build_nfa (rx, rexp->params.pair.left, - &star_start, &star_end) - && rx_build_nfa (rx, rexp->params.pair.right, - &loop_exp_start, &loop_exp_end) - && star_start - && star_end - && loop_exp_end - && loop_exp_start - && rx_nfa_edge (rx, ne_epsilon, star_start, *end) - && rx_nfa_edge (rx, ne_epsilon, *start, star_start) - && rx_nfa_edge (rx, ne_epsilon, star_end, *end) - - && rx_nfa_edge (rx, ne_epsilon, star_end, loop_exp_start) - && rx_nfa_edge (rx, ne_epsilon, loop_exp_end, star_start)); - } - - - case r_concat: - { - struct rx_nfa_state *shared = 0; - return - (rx_build_nfa (rx, rexp->params.pair.left, start, &shared) - && rx_build_nfa (rx, rexp->params.pair.right, &shared, end)); - } - - case r_alternate: - { - struct rx_nfa_state *ls = 0; - struct rx_nfa_state *le = 0; - struct rx_nfa_state *rs = 0; - struct rx_nfa_state *re = 0; - return (rx_build_nfa (rx, rexp->params.pair.left, &ls, &le) - && rx_build_nfa (rx, rexp->params.pair.right, &rs, &re) - && rx_nfa_edge (rx, ne_epsilon, *start, ls) - && rx_nfa_edge (rx, ne_epsilon, *start, rs) - && rx_nfa_edge (rx, ne_epsilon, le, *end) - && rx_nfa_edge (rx, ne_epsilon, re, *end)); - } - - case r_side_effect: - edge = rx_nfa_edge (rx, ne_side_effect, *start, *end); - if (!edge) - return 0; - edge->params.side_effect = rexp->params.side_effect; - return 1; - } - /* this should never happen */ - return 0; + case r_2phase_star: + { + struct rx_nfa_state *star_start = 0; + struct rx_nfa_state *star_end = 0; + struct rx_nfa_state *loop_exp_start = 0; + struct rx_nfa_state *loop_exp_end = 0; + + return (rx_build_nfa(rx, rexp->params.pair.left, + &star_start, &star_end) + && rx_build_nfa(rx, rexp->params.pair.right, + &loop_exp_start, &loop_exp_end) + && star_start + && star_end + && loop_exp_end + && loop_exp_start + && rx_nfa_edge(rx, ne_epsilon, star_start, *end) + && rx_nfa_edge(rx, ne_epsilon, *start, star_start) + && rx_nfa_edge(rx, ne_epsilon, star_end, *end) + + && rx_nfa_edge(rx, ne_epsilon, star_end, loop_exp_start) + && rx_nfa_edge(rx, ne_epsilon, loop_exp_end, star_start)); + } + + + case r_concat: + { + struct rx_nfa_state *shared = 0; + + return (rx_build_nfa(rx, rexp->params.pair.left, start, &shared) + && rx_build_nfa(rx, rexp->params.pair.right, &shared, + end)); + } + + case r_alternate: + { + struct rx_nfa_state *ls = 0; + struct rx_nfa_state *le = 0; + struct rx_nfa_state *rs = 0; + struct rx_nfa_state *re = 0; + + return (rx_build_nfa(rx, rexp->params.pair.left, &ls, &le) + && rx_build_nfa(rx, rexp->params.pair.right, &rs, &re) + && rx_nfa_edge(rx, ne_epsilon, *start, ls) + && rx_nfa_edge(rx, ne_epsilon, *start, rs) + && rx_nfa_edge(rx, ne_epsilon, le, *end) + && rx_nfa_edge(rx, ne_epsilon, re, *end)); + } + + case r_side_effect: + edge = rx_nfa_edge(rx, ne_side_effect, *start, *end); + if (!edge) + return 0; + edge->params.side_effect = rexp->params.side_effect; + return 1; + } + + /* this should never happen */ + return 0; } @@ -1778,56 +1671,51 @@ rx_build_nfa (rx, rexp, start, end) */ #ifdef __STDC__ -RX_DECL void -rx_name_nfa_states (struct rx *rx) +RX_DECL void rx_name_nfa_states(struct rx *rx) #else -RX_DECL void -rx_name_nfa_states (rx) - struct rx *rx; +RX_DECL void rx_name_nfa_states(rx) +struct rx *rx; #endif { - struct rx_nfa_state *n = rx->nfa_states; + struct rx_nfa_state *n = rx->nfa_states; - rx->nodec = 0; - rx->epsnodec = -1; + rx->nodec = 0; + rx->epsnodec = -1; - while (n) - { - struct rx_nfa_edge *e = n->edges; + while (n) { + struct rx_nfa_edge *e = n->edges; - if (n->is_start) - n->eclosure_needed = 1; + if (n->is_start) + n->eclosure_needed = 1; - while (e) - { - switch (e->type) - { - case ne_epsilon: - case ne_side_effect: - break; - - case ne_cset: - n->id = rx->nodec++; - { - struct rx_nfa_edge *from_n = n->edges; - while (from_n) - { - from_n->dest->eclosure_needed = 1; - from_n = from_n->next; - } - } - goto cont; - } - e = e->next; + while (e) { + switch (e->type) { + case ne_epsilon: + case ne_side_effect: + break; + + case ne_cset: + n->id = rx->nodec++; + { + struct rx_nfa_edge *from_n = n->edges; + + while (from_n) { + from_n->dest->eclosure_needed = 1; + from_n = from_n->next; + } + } + goto cont; + } + e = e->next; + } + n->id = rx->epsnodec--; + cont: + n = n->next; } - n->id = rx->epsnodec--; - cont: - n = n->next; - } - rx->epsnodec = -rx->epsnodec; + rx->epsnodec = -rx->epsnodec; } - + /* This page: data structures for the static part of the nfa->supernfa * translation. * @@ -1852,470 +1740,448 @@ rx_name_nfa_states (rx) */ #ifdef __STDC__ -static int -se_list_cmp (void * va, void * vb) +static int se_list_cmp(void *va, void *vb) #else -static int -se_list_cmp (va, vb) - void * va; - void * vb; +static int se_list_cmp(va, vb) +void *va; +void *vb; #endif { - struct rx_se_list * a = (struct rx_se_list *)va; - struct rx_se_list * b = (struct rx_se_list *)vb; + struct rx_se_list *a = (struct rx_se_list *) va; + struct rx_se_list *b = (struct rx_se_list *) vb; - return ((va == vb) - ? 0 - : (!va - ? -1 - : (!vb - ? 1 - : ((long)a->car < (long)b->car - ? 1 - : ((long)a->car > (long)b->car - ? -1 - : se_list_cmp ((void *)a->cdr, (void *)b->cdr)))))); + return ((va == vb) + ? 0 + : (!va + ? -1 + : (!vb + ? 1 + : ((long) a->car < (long) b->car + ? 1 + : ((long) a->car > (long) b->car + ? -1 + : se_list_cmp((void *) a->cdr, + (void *) b->cdr)))))); } #ifdef __STDC__ -static int -se_list_equal (void * va, void * vb) +static int se_list_equal(void *va, void *vb) #else -static int -se_list_equal (va, vb) - void * va; - void * vb; +static int se_list_equal(va, vb) +void *va; +void *vb; #endif { - return !(se_list_cmp (va, vb)); + return !(se_list_cmp(va, vb)); } -static struct rx_hash_rules se_list_hash_rules = -{ - se_list_equal, - compiler_hash_alloc, - compiler_free_hash, - compiler_hash_item_alloc, - compiler_free_hash_item +static struct rx_hash_rules se_list_hash_rules = { + se_list_equal, + compiler_hash_alloc, + compiler_free_hash, + compiler_hash_item_alloc, + compiler_free_hash_item }; #ifdef __STDC__ -static struct rx_se_list * -side_effect_cons (struct rx * rx, - void * se, struct rx_se_list * list) +static struct rx_se_list *side_effect_cons(struct rx *rx, + void *se, + struct rx_se_list *list) #else -static struct rx_se_list * -side_effect_cons (rx, se, list) - struct rx * rx; - void * se; - struct rx_se_list * list; +static struct rx_se_list *side_effect_cons(rx, se, list) +struct rx *rx; +void *se; +struct rx_se_list *list; #endif { - struct rx_se_list * l; - l = ((struct rx_se_list *) malloc (sizeof (*l))); - if (!l) - return 0; - l->car = se; - l->cdr = list; - return l; + struct rx_se_list *l; + + l = ((struct rx_se_list *) malloc(sizeof(*l))); + if (!l) + return 0; + l->car = se; + l->cdr = list; + return l; } #ifdef __STDC__ -static struct rx_se_list * -hash_cons_se_prog (struct rx * rx, - struct rx_hash * memo, - void * car, struct rx_se_list * cdr) +static struct rx_se_list *hash_cons_se_prog(struct rx *rx, + struct rx_hash *memo, + void *car, + struct rx_se_list *cdr) #else -static struct rx_se_list * -hash_cons_se_prog (rx, memo, car, cdr) - struct rx * rx; - struct rx_hash * memo; - void * car; - struct rx_se_list * cdr; +static struct rx_se_list *hash_cons_se_prog(rx, memo, car, cdr) +struct rx *rx; +struct rx_hash *memo; +void *car; +struct rx_se_list *cdr; #endif { - long hash = (long)car ^ (long)cdr; - struct rx_se_list template; + long hash = (long) car ^ (long) cdr; + struct rx_se_list template; - template.car = car; - template.cdr = cdr; - { - struct rx_hash_item * it = rx_hash_store (memo, hash, - (void *)&template, - &se_list_hash_rules); - if (!it) - return 0; - if (it->data == (void *)&template) - { - struct rx_se_list * consed; - consed = (struct rx_se_list *) malloc (sizeof (*consed)); - if (! consed) - { - free ((char *)it); - return 0; - } - *consed = template; - it->data = (void *)consed; - } - return (struct rx_se_list *)it->data; - } + template.car = car; + template.cdr = cdr; + { + struct rx_hash_item *it = rx_hash_store(memo, hash, + (void *) &template, + &se_list_hash_rules); + + if (!it) + return 0; + if (it->data == (void *) &template) { + struct rx_se_list *consed; + + consed = (struct rx_se_list *) malloc(sizeof(*consed)); + if (!consed) { + free((char *) it); + return 0; + } + *consed = template; + it->data = (void *) consed; + } + return (struct rx_se_list *) it->data; + } } - + #ifdef __STDC__ -static struct rx_se_list * -hash_se_prog (struct rx * rx, struct rx_hash * memo, struct rx_se_list * prog) +static struct rx_se_list *hash_se_prog(struct rx *rx, struct rx_hash *memo, + struct rx_se_list *prog) #else -static struct rx_se_list * -hash_se_prog (rx, memo, prog) - struct rx * rx; - struct rx_hash * memo; - struct rx_se_list * prog; +static struct rx_se_list *hash_se_prog(rx, memo, prog) +struct rx *rx; +struct rx_hash *memo; +struct rx_se_list *prog; #endif { - struct rx_se_list * answer = 0; - while (prog) - { - answer = hash_cons_se_prog (rx, memo, prog->car, answer); - if (!answer) - return 0; - prog = prog->cdr; - } - return answer; + struct rx_se_list *answer = 0; + + while (prog) { + answer = hash_cons_se_prog(rx, memo, prog->car, answer); + if (!answer) + return 0; + prog = prog->cdr; + } + return answer; } #ifdef __STDC__ -static int -nfa_set_cmp (void * va, void * vb) +static int nfa_set_cmp(void *va, void *vb) #else -static int -nfa_set_cmp (va, vb) - void * va; - void * vb; +static int nfa_set_cmp(va, vb) +void *va; +void *vb; #endif { - struct rx_nfa_state_set * a = (struct rx_nfa_state_set *)va; - struct rx_nfa_state_set * b = (struct rx_nfa_state_set *)vb; + struct rx_nfa_state_set *a = (struct rx_nfa_state_set *) va; + struct rx_nfa_state_set *b = (struct rx_nfa_state_set *) vb; - return ((va == vb) - ? 0 - : (!va - ? -1 - : (!vb - ? 1 - : (a->car->id < b->car->id - ? 1 - : (a->car->id > b->car->id - ? -1 - : nfa_set_cmp ((void *)a->cdr, (void *)b->cdr)))))); + return ((va == vb) + ? 0 + : (!va + ? -1 + : (!vb + ? 1 + : (a->car->id < b->car->id + ? 1 + : (a->car->id > b->car->id + ? -1 + : nfa_set_cmp((void *) a->cdr, + (void *) b->cdr)))))); } #ifdef __STDC__ -static int -nfa_set_equal (void * va, void * vb) +static int nfa_set_equal(void *va, void *vb) #else -static int -nfa_set_equal (va, vb) - void * va; - void * vb; +static int nfa_set_equal(va, vb) +void *va; +void *vb; #endif { - return !nfa_set_cmp (va, vb); + return !nfa_set_cmp(va, vb); } -static struct rx_hash_rules nfa_set_hash_rules = -{ - nfa_set_equal, - compiler_hash_alloc, - compiler_free_hash, - compiler_hash_item_alloc, - compiler_free_hash_item +static struct rx_hash_rules nfa_set_hash_rules = { + nfa_set_equal, + compiler_hash_alloc, + compiler_free_hash, + compiler_hash_item_alloc, + compiler_free_hash_item }; #ifdef __STDC__ -static struct rx_nfa_state_set * -nfa_set_cons (struct rx * rx, - struct rx_hash * memo, struct rx_nfa_state * state, - struct rx_nfa_state_set * set) -#else -static struct rx_nfa_state_set * -nfa_set_cons (rx, memo, state, set) - struct rx * rx; - struct rx_hash * memo; - struct rx_nfa_state * state; - struct rx_nfa_state_set * set; -#endif -{ - struct rx_nfa_state_set template; - struct rx_hash_item * node; - template.car = state; - template.cdr = set; - node = rx_hash_store (memo, - (((long)state) >> 8) ^ (long)set, - &template, &nfa_set_hash_rules); - if (!node) - return 0; - if (node->data == &template) - { - struct rx_nfa_state_set * l; - l = (struct rx_nfa_state_set *) malloc (sizeof (*l)); - node->data = (void *) l; - if (!l) - return 0; - *l = template; - } - return (struct rx_nfa_state_set *)node->data; -} - -#ifdef __STDC__ -static struct rx_nfa_state_set * -nfa_set_enjoin (struct rx * rx, - struct rx_hash * memo, struct rx_nfa_state * state, - struct rx_nfa_state_set * set) -#else -static struct rx_nfa_state_set * -nfa_set_enjoin (rx, memo, state, set) - struct rx * rx; - struct rx_hash * memo; - struct rx_nfa_state * state; - struct rx_nfa_state_set * set; -#endif -{ - if (!set || state->id < set->car->id) - return nfa_set_cons (rx, memo, state, set); - if (state->id == set->car->id) - return set; - else - { - struct rx_nfa_state_set * newcdr - = nfa_set_enjoin (rx, memo, state, set->cdr); - if (newcdr != set->cdr) - set = nfa_set_cons (rx, memo, set->car, newcdr); - return set; - } +static struct rx_nfa_state_set *nfa_set_cons(struct rx *rx, + struct rx_hash *memo, + struct rx_nfa_state *state, + struct rx_nfa_state_set *set) +#else +static struct rx_nfa_state_set *nfa_set_cons(rx, memo, state, set) +struct rx *rx; +struct rx_hash *memo; +struct rx_nfa_state *state; +struct rx_nfa_state_set *set; +#endif +{ + struct rx_nfa_state_set template; + struct rx_hash_item *node; + + template.car = state; + template.cdr = set; + node = rx_hash_store(memo, + (((long) state) >> 8) ^ (long) set, + &template, &nfa_set_hash_rules); + if (!node) + return 0; + if (node->data == &template) { + struct rx_nfa_state_set *l; + + l = (struct rx_nfa_state_set *) malloc(sizeof(*l)); + node->data = (void *) l; + if (!l) + return 0; + *l = template; + } + return (struct rx_nfa_state_set *) node->data; } +#ifdef __STDC__ +static struct rx_nfa_state_set *nfa_set_enjoin(struct rx *rx, + struct rx_hash *memo, + struct rx_nfa_state *state, + struct rx_nfa_state_set + *set) +#else +static struct rx_nfa_state_set *nfa_set_enjoin(rx, memo, state, set) +struct rx *rx; +struct rx_hash *memo; +struct rx_nfa_state *state; +struct rx_nfa_state_set *set; +#endif +{ + if (!set || state->id < set->car->id) + return nfa_set_cons(rx, memo, state, set); + if (state->id == set->car->id) + return set; + else { + struct rx_nfa_state_set *newcdr + = nfa_set_enjoin(rx, memo, state, set->cdr); + if (newcdr != set->cdr) + set = nfa_set_cons(rx, memo, set->car, newcdr); + return set; + } +} + + /* This page: computing epsilon closures. The closures aren't total. * Each node's closures are partitioned according to the side effects entailed * along the epsilon edges. Return true on success. - */ + */ -struct eclose_frame -{ - struct rx_se_list *prog_backwards; +struct eclose_frame { + struct rx_se_list *prog_backwards; }; -static int eclose_node (struct rx *, struct rx_nfa_state *, - struct rx_nfa_state *, - struct eclose_frame *); -RX_DECL int rx_eclose_nfa (struct rx *); -RX_DECL void rx_delete_epsilon_transitions - (struct rx *); -static int nfacmp (void *, void *); -static int count_hash_nodes (struct rx_hash *); -static void nfa_set_freer (struct rx_hash_item *); -RX_DECL int rx_compactify_nfa (struct rx *, void **, - unsigned long *); -static char *rx_cache_malloc (struct rx_cache *, int); -static void rx_cache_free (struct rx_cache *, - struct rx_freelist **, char *); -static void install_transition (struct rx_superstate *, - struct rx_inx *, rx_Bitset); -static int qlen (struct rx_superstate *); -static void check_cache (struct rx_cache *); -static void semifree_superstate (struct rx_cache *); -static void refresh_semifree_superstate - (struct rx_cache *, - struct rx_superstate *); -static void rx_refresh_this_superstate - (struct rx_cache *, - struct rx_superstate *); -static void release_superset_low (struct rx_cache *, - struct rx_superset *); -RX_DECL void rx_release_superset (struct rx *, struct rx_superset *); -static int rx_really_free_superstate (struct rx_cache *); -static char *rx_cache_get (struct rx_cache *, - struct rx_freelist **); -static char *rx_cache_malloc_or_get (struct rx_cache *, - struct rx_freelist **, int); -static char *rx_cache_get_superstate (struct rx_cache *); -static int supersetcmp (void *, void *); +static int eclose_node(struct rx *, struct rx_nfa_state *, + struct rx_nfa_state *, struct eclose_frame *); +RX_DECL int rx_eclose_nfa(struct rx *); +RX_DECL void rx_delete_epsilon_transitions(struct rx *); +static int nfacmp(void *, void *); +static int count_hash_nodes(struct rx_hash *); +static void nfa_set_freer(struct rx_hash_item *); +RX_DECL int rx_compactify_nfa(struct rx *, void **, unsigned long *); +static char *rx_cache_malloc(struct rx_cache *, int); +static void rx_cache_free(struct rx_cache *, + + struct rx_freelist **, char *); +static void install_transition(struct rx_superstate *, + + struct rx_inx *, rx_Bitset); +static int qlen(struct rx_superstate *); +static void check_cache(struct rx_cache *); +static void semifree_superstate(struct rx_cache *); +static void refresh_semifree_superstate + + (struct rx_cache *, struct rx_superstate *); +static void rx_refresh_this_superstate + + (struct rx_cache *, struct rx_superstate *); +static void release_superset_low(struct rx_cache *, struct rx_superset *); +RX_DECL void rx_release_superset(struct rx *, struct rx_superset *); +static int rx_really_free_superstate(struct rx_cache *); +static char *rx_cache_get(struct rx_cache *, struct rx_freelist **); +static char *rx_cache_malloc_or_get(struct rx_cache *, + struct rx_freelist **, int); +static char *rx_cache_get_superstate(struct rx_cache *); +static int supersetcmp(void *, void *); static struct rx_hash_item - *superset_allocator (struct rx_hash_rules *, void *); +*superset_allocator(struct rx_hash_rules *, void *); static struct rx_hash - *super_hash_allocator (struct rx_hash_rules *); -static void super_hash_liberator (struct rx_hash *, - struct rx_hash_rules *); -static void superset_hash_item_liberator - (struct rx_hash_item *, - struct rx_hash_rules *); -static int bytes_for_cache_size (int, int); -static void rx_morecore (struct rx_cache *); +*super_hash_allocator(struct rx_hash_rules *); +static void super_hash_liberator(struct rx_hash *, struct rx_hash_rules *); +static void superset_hash_item_liberator + + (struct rx_hash_item *, struct rx_hash_rules *); +static int bytes_for_cache_size(int, int); +static void rx_morecore(struct rx_cache *); RX_DECL struct rx_superset - *rx_superset_cons (struct rx *, struct rx_nfa_state *, - struct rx_superset *); +*rx_superset_cons(struct rx *, struct rx_nfa_state *, + + struct rx_superset *); RX_DECL struct rx_superset - *rx_superstate_eclosure_union - (struct rx *, struct rx_superset *, - struct rx_nfa_state_set *); +*rx_superstate_eclosure_union + + (struct rx *, struct rx_superset *, struct rx_nfa_state_set *); static struct rx_distinct_future - *include_futures (struct rx *, - struct rx_distinct_future *, - struct rx_nfa_state *, - struct rx_superstate *); +*include_futures(struct rx *, + struct rx_distinct_future *, + + struct rx_nfa_state *, struct rx_superstate *); RX_DECL struct rx_superstate - *rx_superstate (struct rx *, struct rx_superset *); -static int solve_destination (struct rx *, - struct rx_distinct_future *); -static int compute_super_edge (struct rx *, - struct rx_distinct_future **, - rx_Bitset, struct rx_superstate *, - unsigned char); +*rx_superstate(struct rx *, struct rx_superset *); +static int solve_destination(struct rx *, struct rx_distinct_future *); +static int compute_super_edge(struct rx *, + struct rx_distinct_future **, + + rx_Bitset, struct rx_superstate *, + unsigned char); static struct rx_super_edge - *rx_super_edge (struct rx *, struct rx_superstate *, - rx_Bitset, - struct rx_distinct_future *); -static void install_partial_transition - (struct rx_superstate *, - struct rx_inx *, RX_subset, int); +*rx_super_edge(struct rx *, struct rx_superstate *, + + rx_Bitset, struct rx_distinct_future *); +static void install_partial_transition + (struct rx_superstate *, struct rx_inx *, RX_subset, int); RX_DECL struct rx_inx - *rx_handle_cache_miss (struct rx *, struct rx_superstate *, - unsigned char, void *); -static boolean - at_begline_loc_p (__const__ char *, __const__ char *, - reg_syntax_t); +*rx_handle_cache_miss(struct rx *, struct rx_superstate *, + + unsigned char, void *); static boolean - at_endline_loc_p (__const__ char *, __const__ char *, - int); + +at_begline_loc_p(__const__ char *, __const__ char *, reg_syntax_t); +static boolean at_endline_loc_p(__const__ char *, __const__ char *, int); static rx_Bitset - inverse_translation (struct re_pattern_buffer *, char *, - rx_Bitset, unsigned char *, int); +inverse_translation(struct re_pattern_buffer *, char *, + rx_Bitset, unsigned char *, int); #ifdef __STDC__ -static int -eclose_node (struct rx *rx, struct rx_nfa_state *outnode, - struct rx_nfa_state *node, struct eclose_frame *frame) +static int +eclose_node(struct rx *rx, struct rx_nfa_state *outnode, + struct rx_nfa_state *node, struct eclose_frame *frame) #else -static int -eclose_node (rx, outnode, node, frame) - struct rx *rx; - struct rx_nfa_state *outnode; - struct rx_nfa_state *node; - struct eclose_frame *frame; +static int eclose_node(rx, outnode, node, frame) +struct rx *rx; +struct rx_nfa_state *outnode; +struct rx_nfa_state *node; +struct eclose_frame *frame; #endif { - struct rx_nfa_edge *e = node->edges; - - /* For each node, we follow all epsilon paths to build the closure. - * The closure omits nodes that have only epsilon edges. - * The closure is split into partial closures -- all the states in - * a partial closure are reached by crossing the same list of - * of side effects (though not necessarily the same path). - */ - if (node->mark) - return 1; - node->mark = 1; + struct rx_nfa_edge *e = node->edges; - if (node->id >= 0 || node->is_final) - { - struct rx_possible_future **ec; - struct rx_se_list * prog_in_order - = ((struct rx_se_list *)hash_se_prog (rx, - &rx->se_list_memo, - frame->prog_backwards)); - int cmp; - - ec = &outnode->futures; + /* For each node, we follow all epsilon paths to build the closure. + * The closure omits nodes that have only epsilon edges. + * The closure is split into partial closures -- all the states in + * a partial closure are reached by crossing the same list of + * of side effects (though not necessarily the same path). + */ + if (node->mark) + return 1; + node->mark = 1; + + if (node->id >= 0 || node->is_final) { + struct rx_possible_future **ec; + struct rx_se_list *prog_in_order + = ((struct rx_se_list *) hash_se_prog(rx, + &rx->se_list_memo, + frame->prog_backwards)); + int cmp; + + ec = &outnode->futures; + + while (*ec) { + cmp = + se_list_cmp((void *) (*ec)->effects, + (void *) prog_in_order); + if (cmp <= 0) + break; + ec = &(*ec)->next; + } + if (!*ec || (cmp < 0)) { + struct rx_possible_future *saved = *ec; - while (*ec) - { - cmp = se_list_cmp ((void *)(*ec)->effects, (void *)prog_in_order); - if (cmp <= 0) - break; - ec = &(*ec)->next; - } - if (!*ec || (cmp < 0)) - { - struct rx_possible_future * saved = *ec; - *ec = rx_possible_future (rx, prog_in_order); - (*ec)->next = saved; - if (!*ec) - return 0; - } - if (node->id >= 0) - { - (*ec)->destset = nfa_set_enjoin (rx, &rx->set_list_memo, - node, (*ec)->destset); - if (!(*ec)->destset) - return 0; + *ec = rx_possible_future(rx, prog_in_order); + (*ec)->next = saved; + if (!*ec) + return 0; + } + if (node->id >= 0) { + (*ec)->destset = nfa_set_enjoin(rx, &rx->set_list_memo, + node, (*ec)->destset); + if (!(*ec)->destset) + return 0; + } } - } - while (e) - { - switch (e->type) - { - case ne_epsilon: - if (!eclose_node (rx, outnode, e->dest, frame)) - return 0; - break; - case ne_side_effect: - { - frame->prog_backwards = side_effect_cons (rx, - e->params.side_effect, - frame->prog_backwards); - if (!frame->prog_backwards) - return 0; - if (!eclose_node (rx, outnode, e->dest, frame)) - return 0; - { - struct rx_se_list * dying = frame->prog_backwards; - frame->prog_backwards = frame->prog_backwards->cdr; - free ((char *)dying); - } - break; - } - default: - break; + while (e) { + switch (e->type) { + case ne_epsilon: + if (!eclose_node(rx, outnode, e->dest, frame)) + return 0; + break; + case ne_side_effect: + { + frame->prog_backwards = side_effect_cons(rx, + e->params.side_effect, + frame->prog_backwards); + if (!frame->prog_backwards) + return 0; + if (!eclose_node(rx, outnode, e->dest, frame)) + return 0; + { + struct rx_se_list *dying = frame->prog_backwards; + + frame->prog_backwards = frame->prog_backwards->cdr; + free((char *) dying); + } + break; + } + default: + break; + } + e = e->next; } - e = e->next; - } - node->mark = 0; - return 1; + node->mark = 0; + return 1; } #ifdef __STDC__ -RX_DECL int -rx_eclose_nfa (struct rx *rx) +RX_DECL int rx_eclose_nfa(struct rx *rx) #else -RX_DECL int -rx_eclose_nfa (rx) - struct rx *rx; +RX_DECL int rx_eclose_nfa(rx) +struct rx *rx; #endif { - struct rx_nfa_state *n = rx->nfa_states; - struct eclose_frame frame; - static int rx_id = 0; - - frame.prog_backwards = 0; - rx->rx_id = rx_id++; - bzero (&rx->se_list_memo, sizeof (rx->se_list_memo)); - bzero (&rx->set_list_memo, sizeof (rx->set_list_memo)); - while (n) - { - n->futures = 0; - if (n->eclosure_needed && !eclose_node (rx, n, n, &frame)) - return 0; - /* clear_marks (rx); */ - n = n->next; - } - return 1; + struct rx_nfa_state *n = rx->nfa_states; + struct eclose_frame frame; + static int rx_id = 0; + + frame.prog_backwards = 0; + rx->rx_id = rx_id++; + bzero(&rx->se_list_memo, sizeof(rx->se_list_memo)); + bzero(&rx->set_list_memo, sizeof(rx->set_list_memo)); + while (n) { + n->futures = 0; + if (n->eclosure_needed && !eclose_node(rx, n, n, &frame)) + return 0; + /* clear_marks (rx); */ + n = n->next; + } + return 1; } @@ -2325,42 +2191,38 @@ rx_eclose_nfa (rx) */ #ifdef __STDC__ -RX_DECL void -rx_delete_epsilon_transitions (struct rx *rx) +RX_DECL void rx_delete_epsilon_transitions(struct rx *rx) #else -RX_DECL void -rx_delete_epsilon_transitions (rx) - struct rx *rx; +RX_DECL void rx_delete_epsilon_transitions(rx) +struct rx *rx; #endif { - struct rx_nfa_state *n = rx->nfa_states; - struct rx_nfa_edge **e; + struct rx_nfa_state *n = rx->nfa_states; + struct rx_nfa_edge **e; - while (n) - { - e = &n->edges; - while (*e) - { - struct rx_nfa_edge *t; - switch ((*e)->type) - { - case ne_epsilon: - case ne_side_effect: - t = *e; - *e = t->next; - rx_free_nfa_edge (t); - break; - - default: - e = &(*e)->next; - break; - } + while (n) { + e = &n->edges; + while (*e) { + struct rx_nfa_edge *t; + + switch ((*e)->type) { + case ne_epsilon: + case ne_side_effect: + t = *e; + *e = t->next; + rx_free_nfa_edge(t); + break; + + default: + e = &(*e)->next; + break; + } + } + n = n->next; } - n = n->next; - } } - + /* This page: storing the nfa in a contiguous region of memory for * subsequent conversion to a super-nfa. */ @@ -2372,68 +2234,61 @@ rx_delete_epsilon_transitions (rx) */ #ifdef __STDC__ -static int -nfacmp (void * va, void * vb) +static int nfacmp(void *va, void *vb) #else -static int -nfacmp (va, vb) - void * va; - void * vb; +static int nfacmp(va, vb) +void *va; +void *vb; #endif { - struct rx_nfa_state **a = (struct rx_nfa_state **)va; - struct rx_nfa_state **b = (struct rx_nfa_state **)vb; - return (*a == *b /* &&&& 3.18 */ - ? 0 - : (((*a)->id < 0) == ((*b)->id < 0) - ? (((*a)->id < (*b)->id) ? -1 : 1) - : (((*a)->id < 0) - ? 1 : -1))); + struct rx_nfa_state **a = (struct rx_nfa_state **) va; + struct rx_nfa_state **b = (struct rx_nfa_state **) vb; + + return (*a == *b /* &&&& 3.18 */ + ? 0 : (((*a)->id < 0) == ((*b)->id < 0) + ? (((*a)->id < (*b)->id) ? -1 : 1) + : (((*a)->id < 0) + ? 1 : -1))); } #ifdef __STDC__ -static int -count_hash_nodes (struct rx_hash * st) +static int count_hash_nodes(struct rx_hash *st) #else -static int -count_hash_nodes (st) - struct rx_hash * st; +static int count_hash_nodes(st) +struct rx_hash *st; #endif { - int x; - int count = 0; - for (x = 0; x < 13; ++x) - count += ((st->children[x]) - ? count_hash_nodes (st->children[x]) - : st->bucket_size[x]); - - return count; + int x; + int count = 0; + + for (x = 0; x < 13; ++x) + count += ((st->children[x]) + ? count_hash_nodes(st->children[x]) + : st->bucket_size[x]); + + return count; } #ifdef __STDC__ -static void -se_memo_freer (struct rx_hash_item * node) +static void se_memo_freer(struct rx_hash_item *node) #else -static void -se_memo_freer (node) - struct rx_hash_item * node; +static void se_memo_freer(node) +struct rx_hash_item *node; #endif { - free ((char *)node->data); + free((char *) node->data); } #ifdef __STDC__ -static void -nfa_set_freer (struct rx_hash_item * node) +static void nfa_set_freer(struct rx_hash_item *node) #else -static void -nfa_set_freer (node) - struct rx_hash_item * node; +static void nfa_set_freer(node) +struct rx_hash_item *node; #endif { - free ((char *)node->data); + free((char *) node->data); } @@ -2443,199 +2298,197 @@ nfa_set_freer (node) */ #ifdef __STDC__ -RX_DECL int -rx_compactify_nfa (struct rx *rx, - void **mem, unsigned long *size) -#else -RX_DECL int -rx_compactify_nfa (rx, mem, size) - struct rx *rx; - void **mem; - unsigned long *size; -#endif -{ - int total_nodec; - struct rx_nfa_state *n; - int edgec = 0; - int eclosec = 0; - int se_list_consc = count_hash_nodes (&rx->se_list_memo); - int nfa_setc = count_hash_nodes (&rx->set_list_memo); - unsigned long total_size; - - /* This takes place in two stages. First, the total size of the - * nfa is computed, then structures are copied. - */ - n = rx->nfa_states; - total_nodec = 0; - while (n) - { - struct rx_nfa_edge *e = n->edges; - struct rx_possible_future *ec = n->futures; - ++total_nodec; - while (e) - { - ++edgec; - e = e->next; +RX_DECL int +rx_compactify_nfa(struct rx *rx, void **mem, unsigned long *size) +#else +RX_DECL int rx_compactify_nfa(rx, mem, size) +struct rx *rx; +void **mem; +unsigned long *size; +#endif +{ + int total_nodec; + struct rx_nfa_state *n; + int edgec = 0; + int eclosec = 0; + int se_list_consc = count_hash_nodes(&rx->se_list_memo); + int nfa_setc = count_hash_nodes(&rx->set_list_memo); + unsigned long total_size; + + /* This takes place in two stages. First, the total size of the + * nfa is computed, then structures are copied. + */ + n = rx->nfa_states; + total_nodec = 0; + while (n) { + struct rx_nfa_edge *e = n->edges; + struct rx_possible_future *ec = n->futures; + + ++total_nodec; + while (e) { + ++edgec; + e = e->next; + } + while (ec) { + ++eclosec; + ec = ec->next; + } + n = n->next; + } + + total_size = (total_nodec * sizeof(struct rx_nfa_state) + + edgec * rx_sizeof_bitset(rx->local_cset_size) + + edgec * sizeof(struct rx_nfa_edge) + + nfa_setc * sizeof(struct rx_nfa_state_set) + + eclosec * sizeof(struct rx_possible_future) + + se_list_consc * sizeof(struct rx_se_list) + + rx->reserved); + + if (total_size > *size) { + *mem = remalloc(*mem, total_size); + if (*mem) + *size = total_size; + else + return 0; } - while (ec) + /* Now we've allocated the memory; this copies the NFA. */ { - ++eclosec; - ec = ec->next; + static struct rx_nfa_state **scratch = 0; + static int scratch_alloc = 0; + struct rx_nfa_state *state_base = (struct rx_nfa_state *) *mem; + struct rx_nfa_state *new_state = state_base; + struct rx_nfa_edge *new_edge = (struct rx_nfa_edge *) + ((char *) state_base + total_nodec * sizeof(struct rx_nfa_state)); + struct rx_se_list *new_se_list = (struct rx_se_list *) + ((char *) new_edge + edgec * sizeof(struct rx_nfa_edge)); + struct rx_possible_future *new_close = + ((struct rx_possible_future *) + ((char *) new_se_list + + + se_list_consc * sizeof(struct rx_se_list))); + struct rx_nfa_state_set *new_nfa_set = ((struct rx_nfa_state_set *) + + ((char *) new_close + + eclosec * + + sizeof(struct + rx_possible_future))); + char *new_bitset = + + ((char *) new_nfa_set + + nfa_setc * sizeof(struct rx_nfa_state_set)); + int x; + struct rx_nfa_state *n; + + if (scratch_alloc < total_nodec) { + scratch = ((struct rx_nfa_state **) + remalloc(scratch, total_nodec * sizeof(*scratch))); + if (scratch) + scratch_alloc = total_nodec; + else { + scratch_alloc = 0; + return 0; + } + } + + for (x = 0, n = rx->nfa_states; n; n = n->next) + scratch[x++] = n; + + qsort(scratch, total_nodec, sizeof(struct rx_nfa_state *), + (__compar_fn_t) nfacmp); + + for (x = 0; x < total_nodec; ++x) { + struct rx_possible_future *eclose = scratch[x]->futures; + struct rx_nfa_edge *edge = scratch[x]->edges; + struct rx_nfa_state *cn = new_state++; + + cn->futures = 0; + cn->edges = 0; + cn->next = (x == total_nodec - 1) ? 0 : (cn + 1); + cn->id = scratch[x]->id; + cn->is_final = scratch[x]->is_final; + cn->is_start = scratch[x]->is_start; + cn->mark = 0; + while (edge) { + int indx = (edge->dest->id < 0 + ? (total_nodec + edge->dest->id) + + : edge->dest->id); + struct rx_nfa_edge *e = new_edge++; + rx_Bitset cset = (rx_Bitset) new_bitset; + + new_bitset += rx_sizeof_bitset(rx->local_cset_size); + rx_bitset_null(rx->local_cset_size, cset); + rx_bitset_union(rx->local_cset_size, cset, + edge->params.cset); + e->next = cn->edges; + cn->edges = e; + e->type = edge->type; + e->dest = state_base + indx; + e->params.cset = cset; + edge = edge->next; + } + while (eclose) { + struct rx_possible_future *ec = new_close++; + struct rx_hash_item *sp; + struct rx_se_list **sepos; + struct rx_se_list *sesrc; + struct rx_nfa_state_set *destlst; + struct rx_nfa_state_set **destpos; + + ec->next = cn->futures; + cn->futures = ec; + for (sepos = &ec->effects, sesrc = eclose->effects; + sesrc; sesrc = sesrc->cdr, sepos = &(*sepos)->cdr) { + sp = rx_hash_find(&rx->se_list_memo, + (long) sesrc-> + car ^ (long) sesrc->cdr, sesrc, + &se_list_hash_rules); + if (sp->binding) { + sesrc = (struct rx_se_list *) sp->binding; + break; + } + *new_se_list = *sesrc; + sp->binding = (void *) new_se_list; + *sepos = new_se_list; + ++new_se_list; + } + *sepos = sesrc; + for (destpos = &ec->destset, destlst = eclose->destset; + destlst; + destpos = &(*destpos)->cdr, destlst = destlst->cdr) { + sp = rx_hash_find(&rx->set_list_memo, + ((((long) destlst->car) >> 8) + ^ (long) destlst->cdr), + destlst, &nfa_set_hash_rules); + if (sp->binding) { + destlst = (struct rx_nfa_state_set *) sp->binding; + break; + } + *new_nfa_set = *destlst; + new_nfa_set->car = state_base + destlst->car->id; + sp->binding = (void *) new_nfa_set; + *destpos = new_nfa_set; + ++new_nfa_set; + } + *destpos = destlst; + eclose = eclose->next; + } + } } - n = n->next; - } + rx_free_hash_table(&rx->se_list_memo, se_memo_freer, + &se_list_hash_rules); + bzero(&rx->se_list_memo, sizeof(rx->se_list_memo)); + rx_free_hash_table(&rx->set_list_memo, nfa_set_freer, + &nfa_set_hash_rules); + bzero(&rx->set_list_memo, sizeof(rx->set_list_memo)); - total_size = (total_nodec * sizeof (struct rx_nfa_state) - + edgec * rx_sizeof_bitset (rx->local_cset_size) - + edgec * sizeof (struct rx_nfa_edge) - + nfa_setc * sizeof (struct rx_nfa_state_set) - + eclosec * sizeof (struct rx_possible_future) - + se_list_consc * sizeof (struct rx_se_list) - + rx->reserved); - - if (total_size > *size) - { - *mem = remalloc (*mem, total_size); - if (*mem) - *size = total_size; - else - return 0; - } - /* Now we've allocated the memory; this copies the NFA. */ - { - static struct rx_nfa_state **scratch = 0; - static int scratch_alloc = 0; - struct rx_nfa_state *state_base = (struct rx_nfa_state *) * mem; - struct rx_nfa_state *new_state = state_base; - struct rx_nfa_edge *new_edge = - (struct rx_nfa_edge *) - ((char *) state_base + total_nodec * sizeof (struct rx_nfa_state)); - struct rx_se_list * new_se_list = - (struct rx_se_list *) - ((char *)new_edge + edgec * sizeof (struct rx_nfa_edge)); - struct rx_possible_future *new_close = - ((struct rx_possible_future *) - ((char *) new_se_list - + se_list_consc * sizeof (struct rx_se_list))); - struct rx_nfa_state_set * new_nfa_set = - ((struct rx_nfa_state_set *) - ((char *)new_close + eclosec * sizeof (struct rx_possible_future))); - char *new_bitset = - ((char *) new_nfa_set + nfa_setc * sizeof (struct rx_nfa_state_set)); - int x; - struct rx_nfa_state *n; - - if (scratch_alloc < total_nodec) - { - scratch = ((struct rx_nfa_state **) - remalloc (scratch, total_nodec * sizeof (*scratch))); - if (scratch) - scratch_alloc = total_nodec; - else - { - scratch_alloc = 0; - return 0; - } - } - - for (x = 0, n = rx->nfa_states; n; n = n->next) - scratch[x++] = n; - - qsort (scratch, total_nodec, sizeof (struct rx_nfa_state *), - (__compar_fn_t)nfacmp); - - for (x = 0; x < total_nodec; ++x) - { - struct rx_possible_future *eclose = scratch[x]->futures; - struct rx_nfa_edge *edge = scratch[x]->edges; - struct rx_nfa_state *cn = new_state++; - cn->futures = 0; - cn->edges = 0; - cn->next = (x == total_nodec - 1) ? 0 : (cn + 1); - cn->id = scratch[x]->id; - cn->is_final = scratch[x]->is_final; - cn->is_start = scratch[x]->is_start; - cn->mark = 0; - while (edge) - { - int indx = (edge->dest->id < 0 - ? (total_nodec + edge->dest->id) - : edge->dest->id); - struct rx_nfa_edge *e = new_edge++; - rx_Bitset cset = (rx_Bitset) new_bitset; - new_bitset += rx_sizeof_bitset (rx->local_cset_size); - rx_bitset_null (rx->local_cset_size, cset); - rx_bitset_union (rx->local_cset_size, cset, edge->params.cset); - e->next = cn->edges; - cn->edges = e; - e->type = edge->type; - e->dest = state_base + indx; - e->params.cset = cset; - edge = edge->next; - } - while (eclose) - { - struct rx_possible_future *ec = new_close++; - struct rx_hash_item * sp; - struct rx_se_list ** sepos; - struct rx_se_list * sesrc; - struct rx_nfa_state_set * destlst; - struct rx_nfa_state_set ** destpos; - ec->next = cn->futures; - cn->futures = ec; - for (sepos = &ec->effects, sesrc = eclose->effects; - sesrc; - sesrc = sesrc->cdr, sepos = &(*sepos)->cdr) - { - sp = rx_hash_find (&rx->se_list_memo, - (long)sesrc->car ^ (long)sesrc->cdr, - sesrc, &se_list_hash_rules); - if (sp->binding) - { - sesrc = (struct rx_se_list *)sp->binding; - break; - } - *new_se_list = *sesrc; - sp->binding = (void *)new_se_list; - *sepos = new_se_list; - ++new_se_list; - } - *sepos = sesrc; - for (destpos = &ec->destset, destlst = eclose->destset; - destlst; - destpos = &(*destpos)->cdr, destlst = destlst->cdr) - { - sp = rx_hash_find (&rx->set_list_memo, - ((((long)destlst->car) >> 8) - ^ (long)destlst->cdr), - destlst, &nfa_set_hash_rules); - if (sp->binding) - { - destlst = (struct rx_nfa_state_set *)sp->binding; - break; - } - *new_nfa_set = *destlst; - new_nfa_set->car = state_base + destlst->car->id; - sp->binding = (void *)new_nfa_set; - *destpos = new_nfa_set; - ++new_nfa_set; - } - *destpos = destlst; - eclose = eclose->next; - } - } - } - rx_free_hash_table (&rx->se_list_memo, se_memo_freer, &se_list_hash_rules); - bzero (&rx->se_list_memo, sizeof (rx->se_list_memo)); - rx_free_hash_table (&rx->set_list_memo, nfa_set_freer, &nfa_set_hash_rules); - bzero (&rx->set_list_memo, sizeof (rx->set_list_memo)); - - rx_free_nfa (rx); - rx->nfa_states = (struct rx_nfa_state *)*mem; - return 1; + rx_free_nfa(rx); + rx->nfa_states = (struct rx_nfa_state *) *mem; + return 1; } - + /* The functions in the next several pages define the lazy-NFA-conversion used * by matchers. The input to this construction is an NFA such as * is built by compactify_nfa (rx.c). The output is the superNFA. @@ -2650,67 +2503,64 @@ rx_compactify_nfa (rx, mem, size) * The lables in re_search_2 would make good values for instructions. */ -void * rx_id_instruction_table[rx_num_instructions] = -{ - (void *) rx_backtrack_point, - (void *) rx_do_side_effects, - (void *) rx_cache_miss, - (void *) rx_next_char, - (void *) rx_backtrack, - (void *) rx_error_inx +void *rx_id_instruction_table[rx_num_instructions] = { + (void *) rx_backtrack_point, + (void *) rx_do_side_effects, + (void *) rx_cache_miss, + (void *) rx_next_char, + (void *) rx_backtrack, + (void *) rx_error_inx }; - + /* Memory mgt. for superstate graphs. */ #ifdef __STDC__ -static char * -rx_cache_malloc (struct rx_cache * cache, int bytes) +static char *rx_cache_malloc(struct rx_cache *cache, int bytes) #else -static char * -rx_cache_malloc (cache, bytes) - struct rx_cache * cache; - int bytes; +static char *rx_cache_malloc(cache, bytes) +struct rx_cache *cache; +int bytes; #endif { - while (cache->bytes_left < bytes) - { - if (cache->memory_pos) - cache->memory_pos = cache->memory_pos->next; - if (!cache->memory_pos) + while (cache->bytes_left < bytes) { + if (cache->memory_pos) + cache->memory_pos = cache->memory_pos->next; + if (!cache->memory_pos) { + cache->morecore(cache); + if (!cache->memory_pos) + return 0; + } + cache->bytes_left = cache->memory_pos->bytes; + cache->memory_addr = ((char *) cache->memory_pos + + + sizeof(struct rx_blocklist)); + } + cache->bytes_left -= bytes; { - cache->morecore (cache); - if (!cache->memory_pos) - return 0; + char *addr = cache->memory_addr; + + cache->memory_addr += bytes; + return addr; } - cache->bytes_left = cache->memory_pos->bytes; - cache->memory_addr = ((char *)cache->memory_pos - + sizeof (struct rx_blocklist)); - } - cache->bytes_left -= bytes; - { - char * addr = cache->memory_addr; - cache->memory_addr += bytes; - return addr; - } } #ifdef __STDC__ static void -rx_cache_free (struct rx_cache * cache, - struct rx_freelist ** freelist, char * mem) +rx_cache_free(struct rx_cache *cache, + struct rx_freelist **freelist, char *mem) #else -static void -rx_cache_free (cache, freelist, mem) - struct rx_cache * cache; - struct rx_freelist ** freelist; - char * mem; +static void rx_cache_free(cache, freelist, mem) +struct rx_cache *cache; +struct rx_freelist **freelist; +char *mem; #endif { - struct rx_freelist * it = (struct rx_freelist *)mem; - it->next = *freelist; - *freelist = it; + struct rx_freelist *it = (struct rx_freelist *) mem; + + it->next = *freelist; + *freelist = it; } /* The partially instantiated superstate graph has a transition @@ -2718,75 +2568,70 @@ rx_cache_free (cache, freelist, mem) * This fills in the transition for a set. */ #ifdef __STDC__ -static void -install_transition (struct rx_superstate *super, - struct rx_inx *answer, rx_Bitset trcset) -#else -static void -install_transition (super, answer, trcset) - struct rx_superstate *super; - struct rx_inx *answer; - rx_Bitset trcset; -#endif -{ - struct rx_inx * transitions = super->transitions; - int chr; - for (chr = 0; chr < 256; ) - if (!*trcset) - { - ++trcset; - chr += 32; - } - else - { - RX_subset sub = *trcset; - RX_subset mask = 1; - int bound = chr + 32; - while (chr < bound) - { - if (sub & mask) - transitions [chr] = *answer; - ++chr; - mask <<= 1; - } - ++trcset; - } +static void +install_transition(struct rx_superstate *super, + struct rx_inx *answer, rx_Bitset trcset) +#else +static void install_transition(super, answer, trcset) +struct rx_superstate *super; +struct rx_inx *answer; +rx_Bitset trcset; +#endif +{ + struct rx_inx *transitions = super->transitions; + int chr; + + for (chr = 0; chr < 256;) + if (!*trcset) { + ++trcset; + chr += 32; + } else { + RX_subset sub = *trcset; + RX_subset mask = 1; + int bound = chr + 32; + + while (chr < bound) { + if (sub & mask) + transitions[chr] = *answer; + ++chr; + mask <<= 1; + } + ++trcset; + } } #ifdef __STDC__ -static int -qlen (struct rx_superstate * q) +static int qlen(struct rx_superstate *q) #else -static int -qlen (q) - struct rx_superstate * q; +static int qlen(q) +struct rx_superstate *q; #endif { - int count = 1; - struct rx_superstate * it; - if (!q) - return 0; - for (it = q->next_recyclable; it != q; it = it->next_recyclable) - ++count; - return count; + int count = 1; + struct rx_superstate *it; + + if (!q) + return 0; + for (it = q->next_recyclable; it != q; it = it->next_recyclable) + ++count; + return count; } #ifdef __STDC__ -static void -check_cache (struct rx_cache * cache) +static void check_cache(struct rx_cache *cache) #else -static void -check_cache (cache) - struct rx_cache * cache; +static void check_cache(cache) +struct rx_cache *cache; #endif { - struct rx_cache * you_fucked_up = 0; - int total = cache->superstates; - int semi = cache->semifree_superstates; - if (semi != qlen (cache->semifree_superstate)) - check_cache (you_fucked_up); - if ((total - semi) != qlen (cache->lru_superstate)) - check_cache (you_fucked_up); + struct rx_cache *you_fucked_up = 0; + int total = cache->superstates; + int semi = cache->semifree_superstates; + + if (semi != qlen(cache->semifree_superstate)) + check_cache(you_fucked_up); + if ((total - semi) != qlen(cache->lru_superstate)) + check_cache(you_fucked_up); } /* When a superstate is old and neglected, it can enter a @@ -2804,196 +2649,177 @@ check_cache (cache) */ #ifdef __STDC__ -static void -semifree_superstate (struct rx_cache * cache) +static void semifree_superstate(struct rx_cache *cache) #else -static void -semifree_superstate (cache) - struct rx_cache * cache; -#endif -{ - int disqualified = cache->semifree_superstates; - if (disqualified == cache->superstates) - return; - while (cache->lru_superstate->locks) - { - cache->lru_superstate = cache->lru_superstate->next_recyclable; - ++disqualified; - if (disqualified == cache->superstates) - return; - } - { - struct rx_superstate * it = cache->lru_superstate; - it->next_recyclable->prev_recyclable = it->prev_recyclable; - it->prev_recyclable->next_recyclable = it->next_recyclable; - cache->lru_superstate = (it == it->next_recyclable - ? 0 - : it->next_recyclable); - if (!cache->semifree_superstate) - { - cache->semifree_superstate = it; - it->next_recyclable = it; - it->prev_recyclable = it; - } - else - { - it->prev_recyclable = cache->semifree_superstate->prev_recyclable; - it->next_recyclable = cache->semifree_superstate; - it->prev_recyclable->next_recyclable = it; - it->next_recyclable->prev_recyclable = it; - } - { - struct rx_distinct_future *df; - it->is_semifree = 1; - ++cache->semifree_superstates; - df = it->transition_refs; - if (df) +static void semifree_superstate(cache) +struct rx_cache *cache; +#endif +{ + int disqualified = cache->semifree_superstates; + + if (disqualified == cache->superstates) + return; + while (cache->lru_superstate->locks) { + cache->lru_superstate = cache->lru_superstate->next_recyclable; + ++disqualified; + if (disqualified == cache->superstates) + return; + } { - df->prev_same_dest->next_same_dest = 0; - for (df = it->transition_refs; df; df = df->next_same_dest) - { - df->future_frame.inx = cache->instruction_table[rx_cache_miss]; - df->future_frame.data = 0; - df->future_frame.data_2 = (void *) df; - /* If there are any NEXT-CHAR instruction frames that - * refer to this state, we convert them to CACHE-MISS frames. - */ - if (!df->effects - && (df->edge->options->next_same_super_edge[0] - == df->edge->options)) - install_transition (df->present, &df->future_frame, - df->edge->cset); - } - df = it->transition_refs; - df->prev_same_dest->next_same_dest = df; + struct rx_superstate *it = cache->lru_superstate; + + it->next_recyclable->prev_recyclable = it->prev_recyclable; + it->prev_recyclable->next_recyclable = it->next_recyclable; + cache->lru_superstate = (it == it->next_recyclable + ? 0 : it->next_recyclable); + if (!cache->semifree_superstate) { + cache->semifree_superstate = it; + it->next_recyclable = it; + it->prev_recyclable = it; + } else { + it->prev_recyclable = + cache->semifree_superstate->prev_recyclable; + it->next_recyclable = cache->semifree_superstate; + it->prev_recyclable->next_recyclable = it; + it->next_recyclable->prev_recyclable = it; + } + { + struct rx_distinct_future *df; + + it->is_semifree = 1; + ++cache->semifree_superstates; + df = it->transition_refs; + if (df) { + df->prev_same_dest->next_same_dest = 0; + for (df = it->transition_refs; df; df = df->next_same_dest) { + df->future_frame.inx = + cache->instruction_table[rx_cache_miss]; + df->future_frame.data = 0; + df->future_frame.data_2 = (void *) df; + /* If there are any NEXT-CHAR instruction frames that + * refer to this state, we convert them to CACHE-MISS frames. + */ + if (!df->effects + && (df->edge->options->next_same_super_edge[0] + == df->edge->options)) + install_transition(df->present, &df->future_frame, + df->edge->cset); + } + df = it->transition_refs; + df->prev_same_dest->next_same_dest = df; + } + } } - } - } } #ifdef __STDC__ -static void -refresh_semifree_superstate (struct rx_cache * cache, - struct rx_superstate * super) -#else -static void -refresh_semifree_superstate (cache, super) - struct rx_cache * cache; - struct rx_superstate * super; -#endif -{ - struct rx_distinct_future *df; - - if (super->transition_refs) - { - super->transition_refs->prev_same_dest->next_same_dest = 0; - for (df = super->transition_refs; df; df = df->next_same_dest) - { - df->future_frame.inx = cache->instruction_table[rx_next_char]; - df->future_frame.data = (void *) super->transitions; - /* CACHE-MISS instruction frames that refer to this state, - * must be converted to NEXT-CHAR frames. - */ - if (!df->effects - && (df->edge->options->next_same_super_edge[0] - == df->edge->options)) - install_transition (df->present, &df->future_frame, - df->edge->cset); +static void +refresh_semifree_superstate(struct rx_cache *cache, + struct rx_superstate *super) +#else +static void refresh_semifree_superstate(cache, super) +struct rx_cache *cache; +struct rx_superstate *super; +#endif +{ + struct rx_distinct_future *df; + + if (super->transition_refs) { + super->transition_refs->prev_same_dest->next_same_dest = 0; + for (df = super->transition_refs; df; df = df->next_same_dest) { + df->future_frame.inx = cache->instruction_table[rx_next_char]; + df->future_frame.data = (void *) super->transitions; + /* CACHE-MISS instruction frames that refer to this state, + * must be converted to NEXT-CHAR frames. + */ + if (!df->effects && (df->edge->options->next_same_super_edge[0] + == df->edge->options)) + install_transition(df->present, &df->future_frame, + df->edge->cset); + } + super->transition_refs->prev_same_dest->next_same_dest + = super->transition_refs; } - super->transition_refs->prev_same_dest->next_same_dest - = super->transition_refs; - } - if (cache->semifree_superstate == super) - cache->semifree_superstate = (super->prev_recyclable == super - ? 0 - : super->prev_recyclable); - super->next_recyclable->prev_recyclable = super->prev_recyclable; - super->prev_recyclable->next_recyclable = super->next_recyclable; - - if (!cache->lru_superstate) - (cache->lru_superstate - = super->next_recyclable - = super->prev_recyclable - = super); - else - { - super->next_recyclable = cache->lru_superstate; - super->prev_recyclable = cache->lru_superstate->prev_recyclable; - super->next_recyclable->prev_recyclable = super; - super->prev_recyclable->next_recyclable = super; - } - super->is_semifree = 0; - --cache->semifree_superstates; + if (cache->semifree_superstate == super) + cache->semifree_superstate = (super->prev_recyclable == super + ? 0 : super->prev_recyclable); + super->next_recyclable->prev_recyclable = super->prev_recyclable; + super->prev_recyclable->next_recyclable = super->next_recyclable; + + if (!cache->lru_superstate) + (cache->lru_superstate + = super->next_recyclable = super->prev_recyclable = super); + else { + super->next_recyclable = cache->lru_superstate; + super->prev_recyclable = cache->lru_superstate->prev_recyclable; + super->next_recyclable->prev_recyclable = super; + super->prev_recyclable->next_recyclable = super; + } + super->is_semifree = 0; + --cache->semifree_superstates; } #ifdef __STDC__ static void -rx_refresh_this_superstate (struct rx_cache * cache, struct rx_superstate * superstate) +rx_refresh_this_superstate(struct rx_cache *cache, + struct rx_superstate *superstate) #else -static void -rx_refresh_this_superstate (cache, superstate) - struct rx_cache * cache; - struct rx_superstate * superstate; -#endif -{ - if (superstate->is_semifree) - refresh_semifree_superstate (cache, superstate); - else if (cache->lru_superstate == superstate) - cache->lru_superstate = superstate->next_recyclable; - else if (superstate != cache->lru_superstate->prev_recyclable) - { - superstate->next_recyclable->prev_recyclable - = superstate->prev_recyclable; - superstate->prev_recyclable->next_recyclable - = superstate->next_recyclable; - superstate->next_recyclable = cache->lru_superstate; - superstate->prev_recyclable = cache->lru_superstate->prev_recyclable; - superstate->next_recyclable->prev_recyclable = superstate; - superstate->prev_recyclable->next_recyclable = superstate; - } +static void rx_refresh_this_superstate(cache, superstate) +struct rx_cache *cache; +struct rx_superstate *superstate; +#endif +{ + if (superstate->is_semifree) + refresh_semifree_superstate(cache, superstate); + else if (cache->lru_superstate == superstate) + cache->lru_superstate = superstate->next_recyclable; + else if (superstate != cache->lru_superstate->prev_recyclable) { + superstate->next_recyclable->prev_recyclable + = superstate->prev_recyclable; + superstate->prev_recyclable->next_recyclable + = superstate->next_recyclable; + superstate->next_recyclable = cache->lru_superstate; + superstate->prev_recyclable = + cache->lru_superstate->prev_recyclable; + superstate->next_recyclable->prev_recyclable = superstate; + superstate->prev_recyclable->next_recyclable = superstate; + } } #ifdef __STDC__ -static void -release_superset_low (struct rx_cache * cache, - struct rx_superset *set) +static void +release_superset_low(struct rx_cache *cache, struct rx_superset *set) #else -static void -release_superset_low (cache, set) - struct rx_cache * cache; - struct rx_superset *set; +static void release_superset_low(cache, set) +struct rx_cache *cache; +struct rx_superset *set; #endif { - if (!--set->refs) - { - if (set->cdr) - release_superset_low (cache, set->cdr); + if (!--set->refs) { + if (set->cdr) + release_superset_low(cache, set->cdr); - set->starts_for = 0; + set->starts_for = 0; - rx_hash_free - (rx_hash_find - (&cache->superset_table, - (unsigned long)set->car ^ set->id ^ (unsigned long)set->cdr, - (void *)set, - &cache->superset_hash_rules), - &cache->superset_hash_rules); - rx_cache_free (cache, &cache->free_supersets, (char *)set); - } + rx_hash_free + (rx_hash_find + (&cache->superset_table, + (unsigned long) set->car ^ set-> + id ^ (unsigned long) set->cdr, (void *) set, + &cache->superset_hash_rules), &cache->superset_hash_rules); + rx_cache_free(cache, &cache->free_supersets, (char *) set); + } } #ifdef __STDC__ -RX_DECL void -rx_release_superset (struct rx *rx, - struct rx_superset *set) +RX_DECL void rx_release_superset(struct rx *rx, struct rx_superset *set) #else -RX_DECL void -rx_release_superset (rx, set) - struct rx *rx; - struct rx_superset *set; +RX_DECL void rx_release_superset(rx, set) +struct rx *rx; +struct rx_superset *set; #endif { - release_superset_low (rx->cache, set); + release_superset_low(rx->cache, set); } /* This tries to add a new superstate to the superstate freelist. @@ -3002,299 +2828,286 @@ rx_release_superset (rx, set) */ #ifdef __STDC__ -static int -rx_really_free_superstate (struct rx_cache * cache) +static int rx_really_free_superstate(struct rx_cache *cache) #else -static int -rx_really_free_superstate (cache) - struct rx_cache * cache; -#endif -{ - int locked_superstates = 0; - struct rx_superstate * it; - - if (!cache->superstates) - return 0; - - { - /* This is a total guess. The idea is that we should expect as - * many misses as we've recently experienced. I.e., cache->misses - * should be the same as cache->semifree_superstates. - */ - while ((cache->hits + cache->misses) > cache->superstates_allowed) - { - cache->hits >>= 1; - cache->misses >>= 1; - } - if ( ((cache->hits + cache->misses) * cache->semifree_superstates) - < (cache->superstates * cache->misses)) - { - semifree_superstate (cache); - semifree_superstate (cache); - } - } - - while (cache->semifree_superstate && cache->semifree_superstate->locks) - { - refresh_semifree_superstate (cache, cache->semifree_superstate); - ++locked_superstates; - if (locked_superstates == cache->superstates) - return 0; - } +static int rx_really_free_superstate(cache) +struct rx_cache *cache; +#endif +{ + int locked_superstates = 0; + struct rx_superstate *it; + + if (!cache->superstates) + return 0; - if (cache->semifree_superstate) - { - it = cache->semifree_superstate; - it->next_recyclable->prev_recyclable = it->prev_recyclable; - it->prev_recyclable->next_recyclable = it->next_recyclable; - cache->semifree_superstate = ((it == it->next_recyclable) - ? 0 - : it->next_recyclable); - --cache->semifree_superstates; - } - else - { - while (cache->lru_superstate->locks) { - cache->lru_superstate = cache->lru_superstate->next_recyclable; - ++locked_superstates; - if (locked_superstates == cache->superstates) - return 0; + /* This is a total guess. The idea is that we should expect as + * many misses as we've recently experienced. I.e., cache->misses + * should be the same as cache->semifree_superstates. + */ + while ((cache->hits + cache->misses) > cache->superstates_allowed) { + cache->hits >>= 1; + cache->misses >>= 1; + } + if (((cache->hits + cache->misses) * cache->semifree_superstates) + < (cache->superstates * cache->misses)) { + semifree_superstate(cache); + semifree_superstate(cache); + } + } + + while (cache->semifree_superstate && cache->semifree_superstate->locks) { + refresh_semifree_superstate(cache, cache->semifree_superstate); + ++locked_superstates; + if (locked_superstates == cache->superstates) + return 0; + } + + if (cache->semifree_superstate) { + it = cache->semifree_superstate; + it->next_recyclable->prev_recyclable = it->prev_recyclable; + it->prev_recyclable->next_recyclable = it->next_recyclable; + cache->semifree_superstate = ((it == it->next_recyclable) + ? 0 : it->next_recyclable); + --cache->semifree_superstates; + } else { + while (cache->lru_superstate->locks) { + cache->lru_superstate = cache->lru_superstate->next_recyclable; + ++locked_superstates; + if (locked_superstates == cache->superstates) + return 0; + } + it = cache->lru_superstate; + it->next_recyclable->prev_recyclable = it->prev_recyclable; + it->prev_recyclable->next_recyclable = it->next_recyclable; + cache->lru_superstate = ((it == it->next_recyclable) + ? 0 : it->next_recyclable); } - it = cache->lru_superstate; - it->next_recyclable->prev_recyclable = it->prev_recyclable; - it->prev_recyclable->next_recyclable = it->next_recyclable; - cache->lru_superstate = ((it == it->next_recyclable) - ? 0 - : it->next_recyclable); - } - if (it->transition_refs) - { - struct rx_distinct_future *df; - for (df = it->transition_refs, - df->prev_same_dest->next_same_dest = 0; - df; - df = df->next_same_dest) + if (it->transition_refs) { + struct rx_distinct_future *df; + + for (df = it->transition_refs, + df->prev_same_dest->next_same_dest = 0; + df; df = df->next_same_dest) { + df->future_frame.inx = cache->instruction_table[rx_cache_miss]; + df->future_frame.data = 0; + df->future_frame.data_2 = (void *) df; + df->future = 0; + } + it->transition_refs->prev_same_dest->next_same_dest = + it->transition_refs; + } { - df->future_frame.inx = cache->instruction_table[rx_cache_miss]; - df->future_frame.data = 0; - df->future_frame.data_2 = (void *) df; - df->future = 0; + struct rx_super_edge *tc = it->edges; + + while (tc) { + struct rx_distinct_future *df; + struct rx_super_edge *tct = tc->next; + + df = tc->options; + df->next_same_super_edge[1]->next_same_super_edge[0] = 0; + while (df) { + struct rx_distinct_future *dft = df; + + df = df->next_same_super_edge[0]; + + + if (dft->future && dft->future->transition_refs == dft) { + dft->future->transition_refs = dft->next_same_dest; + if (dft->future->transition_refs == dft) + dft->future->transition_refs = 0; + } + dft->next_same_dest->prev_same_dest = dft->prev_same_dest; + dft->prev_same_dest->next_same_dest = dft->next_same_dest; + rx_cache_free(cache, &cache->free_discernable_futures, + (char *) dft); + } + rx_cache_free(cache, &cache->free_transition_classes, + (char *) tc); + tc = tct; + } } - it->transition_refs->prev_same_dest->next_same_dest = - it->transition_refs; - } - { - struct rx_super_edge *tc = it->edges; - while (tc) - { - struct rx_distinct_future * df; - struct rx_super_edge *tct = tc->next; - df = tc->options; - df->next_same_super_edge[1]->next_same_super_edge[0] = 0; - while (df) - { - struct rx_distinct_future *dft = df; - df = df->next_same_super_edge[0]; - - - if (dft->future && dft->future->transition_refs == dft) - { - dft->future->transition_refs = dft->next_same_dest; - if (dft->future->transition_refs == dft) - dft->future->transition_refs = 0; - } - dft->next_same_dest->prev_same_dest = dft->prev_same_dest; - dft->prev_same_dest->next_same_dest = dft->next_same_dest; - rx_cache_free (cache, &cache->free_discernable_futures, - (char *)dft); - } - rx_cache_free (cache, &cache->free_transition_classes, (char *)tc); - tc = tct; - } - } - - if (it->contents->superstate == it) - it->contents->superstate = 0; - release_superset_low (cache, it->contents); - rx_cache_free (cache, &cache->free_superstates, (char *)it); - --cache->superstates; - return 1; -} - -#ifdef __STDC__ -static char * -rx_cache_get (struct rx_cache * cache, - struct rx_freelist ** freelist) -#else -static char * -rx_cache_get (cache, freelist) - struct rx_cache * cache; - struct rx_freelist ** freelist; -#endif -{ - while (!*freelist && rx_really_free_superstate (cache)) - ; - if (!*freelist) - return 0; - { - struct rx_freelist * it = *freelist; - *freelist = it->next; - return (char *)it; - } -} - -#ifdef __STDC__ -static char * -rx_cache_malloc_or_get (struct rx_cache * cache, - struct rx_freelist ** freelist, int bytes) -#else -static char * -rx_cache_malloc_or_get (cache, freelist, bytes) - struct rx_cache * cache; - struct rx_freelist ** freelist; - int bytes; -#endif -{ - if (!*freelist) - { - char * answer = rx_cache_malloc (cache, bytes); - if (answer) - return answer; - } - return rx_cache_get (cache, freelist); + if (it->contents->superstate == it) + it->contents->superstate = 0; + release_superset_low(cache, it->contents); + rx_cache_free(cache, &cache->free_superstates, (char *) it); + --cache->superstates; + return 1; } #ifdef __STDC__ -static char * -rx_cache_get_superstate (struct rx_cache * cache) +static char *rx_cache_get(struct rx_cache *cache, + struct rx_freelist **freelist) #else -static char * -rx_cache_get_superstate (cache) - struct rx_cache * cache; +static char *rx_cache_get(cache, freelist) +struct rx_cache *cache; +struct rx_freelist **freelist; #endif { - char * answer; - int bytes = ( sizeof (struct rx_superstate) - + cache->local_cset_size * sizeof (struct rx_inx)); - if (!cache->free_superstates - && (cache->superstates < cache->superstates_allowed)) - { - answer = rx_cache_malloc (cache, bytes); - if (answer) + while (!*freelist && rx_really_free_superstate(cache)); + if (!*freelist) + return 0; { - ++cache->superstates; - return answer; + struct rx_freelist *it = *freelist; + + *freelist = it->next; + return (char *) it; } - } - answer = rx_cache_get (cache, &cache->free_superstates); - if (!answer) - { - answer = rx_cache_malloc (cache, bytes); - if (answer) - ++cache->superstates_allowed; - } - ++cache->superstates; - return answer; } - - #ifdef __STDC__ -static int -supersetcmp (void * va, void * vb) +static char *rx_cache_malloc_or_get(struct rx_cache *cache, + struct rx_freelist **freelist, + int bytes) #else -static int -supersetcmp (va, vb) - void * va; - void * vb; +static char *rx_cache_malloc_or_get(cache, freelist, bytes) +struct rx_cache *cache; +struct rx_freelist **freelist; +int bytes; #endif { - struct rx_superset * a = (struct rx_superset *)va; - struct rx_superset * b = (struct rx_superset *)vb; - return ( (a == b) - || (a && b && (a->car == b->car) && (a->cdr == b->cdr))); -} + if (!*freelist) { + char *answer = rx_cache_malloc(cache, bytes); + + if (answer) + return answer; + } + + return rx_cache_get(cache, freelist); +} #ifdef __STDC__ -static struct rx_hash_item * -superset_allocator (struct rx_hash_rules * rules, void * val) +static char *rx_cache_get_superstate(struct rx_cache *cache) #else -static struct rx_hash_item * -superset_allocator (rules, val) - struct rx_hash_rules * rules; - void * val; +static char *rx_cache_get_superstate(cache) +struct rx_cache *cache; #endif { - struct rx_cache * cache - = ((struct rx_cache *) - ((char *)rules - - (unsigned long)(&((struct rx_cache *)0)->superset_hash_rules))); - struct rx_superset * template = (struct rx_superset *)val; - struct rx_superset * newset - = ((struct rx_superset *) - rx_cache_malloc_or_get (cache, - &cache->free_supersets, - sizeof (*template))); - if (!newset) - return 0; - newset->refs = 0; - newset->car = template->car; - newset->id = template->car->id; - newset->cdr = template->cdr; - newset->superstate = 0; - rx_protect_superset (rx, template->cdr); - newset->hash_item.data = (void *)newset; - newset->hash_item.binding = 0; - return &newset->hash_item; + char *answer; + int bytes = (sizeof(struct rx_superstate) + + cache->local_cset_size * sizeof(struct rx_inx)); + + if (!cache->free_superstates + && (cache->superstates < cache->superstates_allowed)) { + answer = rx_cache_malloc(cache, bytes); + if (answer) { + ++cache->superstates; + return answer; + } + } + answer = rx_cache_get(cache, &cache->free_superstates); + if (!answer) { + answer = rx_cache_malloc(cache, bytes); + if (answer) + ++cache->superstates_allowed; + } + ++cache->superstates; + return answer; } + + #ifdef __STDC__ -static struct rx_hash * -super_hash_allocator (struct rx_hash_rules * rules) +static int supersetcmp(void *va, void *vb) #else -static struct rx_hash * -super_hash_allocator (rules) - struct rx_hash_rules * rules; +static int supersetcmp(va, vb) +void *va; +void *vb; #endif { - struct rx_cache * cache - = ((struct rx_cache *) - ((char *)rules - - (unsigned long)(&((struct rx_cache *)0)->superset_hash_rules))); - return ((struct rx_hash *) - rx_cache_malloc_or_get (cache, - &cache->free_hash, sizeof (struct rx_hash))); + struct rx_superset *a = (struct rx_superset *) va; + struct rx_superset *b = (struct rx_superset *) vb; + + return ((a == b) + || (a && b && (a->car == b->car) && (a->cdr == b->cdr))); } +#ifdef __STDC__ +static struct rx_hash_item *superset_allocator(struct rx_hash_rules *rules, + void *val) +#else +static struct rx_hash_item *superset_allocator(rules, val) +struct rx_hash_rules *rules; +void *val; +#endif +{ + struct rx_cache *cache = ((struct rx_cache *) + ((char *) rules + - + + (unsigned + long) (&((struct rx_cache *) + 0)->superset_hash_rules))); + struct rx_superset *template = (struct rx_superset *) val; + struct rx_superset *newset + = ((struct rx_superset *) rx_cache_malloc_or_get(cache, + &cache->free_supersets, + sizeof + + (*template))); + if (!newset) + return 0; + newset->refs = 0; + newset->car = template->car; + newset->id = template->car->id; + newset->cdr = template->cdr; + newset->superstate = 0; + rx_protect_superset(rx, template->cdr); + newset->hash_item.data = (void *) newset; + newset->hash_item.binding = 0; + return &newset->hash_item; +} #ifdef __STDC__ -static void -super_hash_liberator (struct rx_hash * hash, struct rx_hash_rules * rules) +static struct rx_hash *super_hash_allocator(struct rx_hash_rules *rules) #else -static void -super_hash_liberator (hash, rules) - struct rx_hash * hash; - struct rx_hash_rules * rules; +static struct rx_hash *super_hash_allocator(rules) +struct rx_hash_rules *rules; #endif { - struct rx_cache * cache - = ((struct rx_cache *) - (char *)rules - (long)(&((struct rx_cache *)0)->superset_hash_rules)); - rx_cache_free (cache, &cache->free_hash, (char *)hash); + struct rx_cache *cache = ((struct rx_cache *) + ((char *) rules + - + + (unsigned + long) (&((struct rx_cache *) + 0)->superset_hash_rules))); + return ((struct rx_hash *) + rx_cache_malloc_or_get(cache, &cache->free_hash, + + sizeof(struct rx_hash))); } + #ifdef __STDC__ static void -superset_hash_item_liberator (struct rx_hash_item * it, - struct rx_hash_rules * rules) +super_hash_liberator(struct rx_hash *hash, struct rx_hash_rules *rules) #else +static void super_hash_liberator(hash, rules) +struct rx_hash *hash; +struct rx_hash_rules *rules; +#endif +{ + struct rx_cache *cache = ((struct rx_cache *) + + (char *) rules - + (long) (& + + ((struct rx_cache *) + 0)->superset_hash_rules)); + rx_cache_free(cache, &cache->free_hash, (char *) hash); +} + +#ifdef __STDC__ static void -superset_hash_item_liberator (it, rules) /* Well, it does ya know. */ - struct rx_hash_item * it; - struct rx_hash_rules * rules; +superset_hash_item_liberator(struct rx_hash_item *it, + struct rx_hash_rules *rules) +#else +static void superset_hash_item_liberator(it, rules) /* Well, it does ya know. */ +struct rx_hash_item *it; +struct rx_hash_rules *rules; #endif { } @@ -3303,99 +3116,98 @@ int rx_cache_bound = 128; static int rx_default_cache_got = 0; #ifdef __STDC__ -static int -bytes_for_cache_size (int supers, int cset_size) +static int bytes_for_cache_size(int supers, int cset_size) #else -static int -bytes_for_cache_size (supers, cset_size) - int supers; - int cset_size; +static int bytes_for_cache_size(supers, cset_size) +int supers; +int cset_size; #endif { - /* What the hell is this? !!!*/ - return (int) - ((float)supers * - ( (1.03 * (float) ( rx_sizeof_bitset (cset_size) - + sizeof (struct rx_super_edge))) - + (1.80 * (float) sizeof (struct rx_possible_future)) - + (float) ( sizeof (struct rx_superstate) - + cset_size * sizeof (struct rx_inx)))); + /* What the hell is this? !!! */ + return (int) + ((float) supers * ((1.03 * (float) (rx_sizeof_bitset(cset_size) + + + sizeof(struct rx_super_edge))) + + + (1.80 * + (float) sizeof(struct rx_possible_future)) + + (float) (sizeof(struct rx_superstate) + + cset_size * sizeof(struct rx_inx)))); } #ifdef __STDC__ -static void -rx_morecore (struct rx_cache * cache) +static void rx_morecore(struct rx_cache *cache) #else -static void -rx_morecore (cache) - struct rx_cache * cache; -#endif -{ - if (rx_default_cache_got >= rx_cache_bound) - return; - - rx_default_cache_got += 16; - cache->superstates_allowed = rx_cache_bound; - { - struct rx_blocklist ** pos = &cache->memory; - int size = bytes_for_cache_size (16, cache->local_cset_size); - while (*pos) - pos = &(*pos)->next; - *pos = ((struct rx_blocklist *) - malloc (size + sizeof (struct rx_blocklist))); - if (!*pos) - return; - - (*pos)->next = 0; - (*pos)->bytes = size; - cache->memory_pos = *pos; - cache->memory_addr = (char *)*pos + sizeof (**pos); - cache->bytes_left = size; - } -} - -static struct rx_cache default_cache = -{ - { - supersetcmp, - super_hash_allocator, - super_hash_liberator, - superset_allocator, - superset_hash_item_liberator, - }, - 0, - 0, - 0, - 0, - rx_morecore, - - 0, - 0, - 0, - 0, - 0, - - 0, - 0, - - 0, - - 0, - 0, - 0, - 0, - 128, - - 256, - rx_id_instruction_table, - - { - 0, - 0, - {0}, - {0}, - {0} - } +static void rx_morecore(cache) +struct rx_cache *cache; +#endif +{ + if (rx_default_cache_got >= rx_cache_bound) + return; + + rx_default_cache_got += 16; + cache->superstates_allowed = rx_cache_bound; + { + struct rx_blocklist **pos = &cache->memory; + int size = bytes_for_cache_size(16, cache->local_cset_size); + + while (*pos) + pos = &(*pos)->next; + *pos = ((struct rx_blocklist *) + malloc(size + sizeof(struct rx_blocklist))); + + if (!*pos) + return; + + (*pos)->next = 0; + (*pos)->bytes = size; + cache->memory_pos = *pos; + cache->memory_addr = (char *) *pos + sizeof(**pos); + cache->bytes_left = size; + } +} + +static struct rx_cache default_cache = { + { + supersetcmp, + super_hash_allocator, + super_hash_liberator, + superset_allocator, + superset_hash_item_liberator, + }, + 0, + 0, + 0, + 0, + rx_morecore, + + 0, + 0, + 0, + 0, + 0, + + 0, + 0, + + 0, + + 0, + 0, + 0, + 0, + 128, + + 256, + rx_id_instruction_table, + + { + 0, + 0, + {0}, + {0}, + {0} + } }; /* This adds an element to a superstate set. These sets are lists, such @@ -3405,52 +3217,52 @@ static struct rx_cache default_cache = */ #ifdef __STDC__ -RX_DECL struct rx_superset * -rx_superset_cons (struct rx * rx, - struct rx_nfa_state *car, struct rx_superset *cdr) +RX_DECL struct rx_superset *rx_superset_cons(struct rx *rx, + struct rx_nfa_state *car, + struct rx_superset *cdr) #else -RX_DECL struct rx_superset * -rx_superset_cons (rx, car, cdr) - struct rx * rx; - struct rx_nfa_state *car; - struct rx_superset *cdr; +RX_DECL struct rx_superset *rx_superset_cons(rx, car, cdr) +struct rx *rx; +struct rx_nfa_state *car; +struct rx_superset *cdr; #endif { - struct rx_cache * cache = rx->cache; - if (!car && !cdr) - { - if (!cache->empty_superset) + struct rx_cache *cache = rx->cache; + + if (!car && !cdr) { + if (!cache->empty_superset) { + cache->empty_superset = ((struct rx_superset *) + rx_cache_malloc_or_get(cache, + &cache->free_supersets, + + sizeof(struct + rx_superset))); + if (!cache->empty_superset) + return 0; + bzero(cache->empty_superset, sizeof(struct rx_superset)); + + cache->empty_superset->refs = 1000; + } + return cache->empty_superset; + } { - cache->empty_superset - = ((struct rx_superset *) - rx_cache_malloc_or_get (cache, &cache->free_supersets, - sizeof (struct rx_superset))); - if (!cache->empty_superset) - return 0; - bzero (cache->empty_superset, sizeof (struct rx_superset)); - cache->empty_superset->refs = 1000; + struct rx_superset template; + struct rx_hash_item *hit; + + template.car = car; + template.cdr = cdr; + template.id = car->id; + /* While hash_store will protect cdr itself it might first allocate hash + tables and stuff which might cause it to be garbage collected before + it's protected -- [gsstark:19961026.2155EST] */ + rx_protect_superset(rx, cdr); + hit = rx_hash_store(&cache->superset_table, + (unsigned long) car ^ car->id ^ (unsigned long) + cdr, (void *) &template, + &cache->superset_hash_rules); + rx_release_superset(rx, cdr); + return (hit ? (struct rx_superset *) hit->data : 0); } - return cache->empty_superset; - } - { - struct rx_superset template; - struct rx_hash_item * hit; - template.car = car; - template.cdr = cdr; - template.id = car->id; - /* While hash_store will protect cdr itself it might first allocate hash - tables and stuff which might cause it to be garbage collected before - it's protected -- [gsstark:19961026.2155EST] */ - rx_protect_superset (rx, cdr); - hit = rx_hash_store (&cache->superset_table, - (unsigned long)car ^ car->id ^ (unsigned long)cdr, - (void *)&template, - &cache->superset_hash_rules); - rx_release_superset (rx, cdr); - return (hit - ? (struct rx_superset *)hit->data - : 0); - } } /* This computes a union of two NFA state sets. The sets do not have the @@ -3459,61 +3271,55 @@ rx_superset_cons (rx, car, cdr) */ #ifdef __STDC__ -RX_DECL struct rx_superset * -rx_superstate_eclosure_union - (struct rx * rx, struct rx_superset *set, struct rx_nfa_state_set *ecl) -#else -RX_DECL struct rx_superset * -rx_superstate_eclosure_union (rx, set, ecl) - struct rx * rx; - struct rx_superset *set; - struct rx_nfa_state_set *ecl; -#endif -{ - if (!ecl) - return set; - - if (!set->car) - return rx_superset_cons (rx, ecl->car, - rx_superstate_eclosure_union (rx, set, ecl->cdr)); - if (set->car == ecl->car) - return rx_superstate_eclosure_union (rx, set, ecl->cdr); - - { - struct rx_superset * tail; - struct rx_nfa_state * first; - - if (set->car > ecl->car) - { - tail = rx_superstate_eclosure_union (rx, set->cdr, ecl); - first = set->car; - } - else - { - tail = rx_superstate_eclosure_union (rx, set, ecl->cdr); - first = ecl->car; - } - if (!tail) - return 0; - else - { - struct rx_superset * answer; - answer = rx_superset_cons (rx, first, tail); - if (!answer) - { - rx_protect_superset (rx, tail); - rx_release_superset (rx, tail); - return 0; - } - else - return answer; - } - } -} +RX_DECL struct rx_superset *rx_superstate_eclosure_union + (struct rx *rx, struct rx_superset *set, struct rx_nfa_state_set *ecl) +#else +RX_DECL struct rx_superset *rx_superstate_eclosure_union(rx, set, ecl) +struct rx *rx; +struct rx_superset *set; +struct rx_nfa_state_set *ecl; +#endif +{ + if (!ecl) + return set; + if (!set->car) + return rx_superset_cons(rx, ecl->car, + rx_superstate_eclosure_union(rx, set, + ecl->cdr)); + if (set->car == ecl->car) + return rx_superstate_eclosure_union(rx, set, ecl->cdr); + { + struct rx_superset *tail; + struct rx_nfa_state *first; + + if (set->car > ecl->car) { + tail = rx_superstate_eclosure_union(rx, set->cdr, ecl); + first = set->car; + } else { + tail = rx_superstate_eclosure_union(rx, set, ecl->cdr); + first = ecl->car; + } + if (!tail) + return 0; + else { + struct rx_superset *answer; + + answer = rx_superset_cons(rx, first, tail); + if (!answer) { + rx_protect_superset(rx, tail); + rx_release_superset(rx, tail); + return 0; + } else + return answer; + } + } +} + + /* * This makes sure that a list of rx_distinct_futures contains * a future for each possible set of side effects in the eclosure @@ -3522,79 +3328,80 @@ rx_superstate_eclosure_union (rx, set, ecl) */ #ifdef __STDC__ -static struct rx_distinct_future * -include_futures (struct rx *rx, - struct rx_distinct_future *df, struct rx_nfa_state - *state, struct rx_superstate *superstate) -#else -static struct rx_distinct_future * -include_futures (rx, df, state, superstate) - struct rx *rx; - struct rx_distinct_future *df; - struct rx_nfa_state *state; - struct rx_superstate *superstate; -#endif -{ - struct rx_possible_future *future; - struct rx_cache * cache = rx->cache; - for (future = state->futures; future; future = future->next) - { - struct rx_distinct_future *dfp; - struct rx_distinct_future *insert_before = 0; - if (df) - df->next_same_super_edge[1]->next_same_super_edge[0] = 0; - for (dfp = df; dfp; dfp = dfp->next_same_super_edge[0]) - if (dfp->effects == future->effects) - break; - else - { - int order = rx->se_list_cmp (rx, dfp->effects, future->effects); - if (order > 0) - { - insert_before = dfp; - dfp = 0; - break; - } - } - if (df) - df->next_same_super_edge[1]->next_same_super_edge[0] = df; - if (!dfp) - { - dfp - = ((struct rx_distinct_future *) - rx_cache_malloc_or_get (cache, &cache->free_discernable_futures, - sizeof (struct rx_distinct_future))); - if (!dfp) - return 0; - if (!df) - { - df = insert_before = dfp; - df->next_same_super_edge[0] = df->next_same_super_edge[1] = df; - } - else if (!insert_before) - insert_before = df; - else if (insert_before == df) - df = dfp; - - dfp->next_same_super_edge[0] = insert_before; - dfp->next_same_super_edge[1] - = insert_before->next_same_super_edge[1]; - dfp->next_same_super_edge[1]->next_same_super_edge[0] = dfp; - dfp->next_same_super_edge[0]->next_same_super_edge[1] = dfp; - dfp->next_same_dest = dfp->prev_same_dest = dfp; - dfp->future = 0; - dfp->present = superstate; - dfp->future_frame.inx = rx->instruction_table[rx_cache_miss]; - dfp->future_frame.data = 0; - dfp->future_frame.data_2 = (void *) dfp; - dfp->side_effects_frame.inx - = rx->instruction_table[rx_do_side_effects]; - dfp->side_effects_frame.data = 0; - dfp->side_effects_frame.data_2 = (void *) dfp; - dfp->effects = future->effects; +static struct rx_distinct_future *include_futures(struct rx *rx, struct rx_distinct_future + *df, struct rx_nfa_state + *state, struct rx_superstate + *superstate) +#else +static struct rx_distinct_future *include_futures(rx, df, state, + superstate) +struct rx *rx; +struct rx_distinct_future *df; +struct rx_nfa_state *state; +struct rx_superstate *superstate; +#endif +{ + struct rx_possible_future *future; + struct rx_cache *cache = rx->cache; + + for (future = state->futures; future; future = future->next) { + struct rx_distinct_future *dfp; + struct rx_distinct_future *insert_before = 0; + + if (df) + df->next_same_super_edge[1]->next_same_super_edge[0] = 0; + for (dfp = df; dfp; dfp = dfp->next_same_super_edge[0]) + if (dfp->effects == future->effects) + break; + else { + int order = + + rx->se_list_cmp(rx, dfp->effects, future->effects); + if (order > 0) { + insert_before = dfp; + dfp = 0; + break; + } + } + if (df) + df->next_same_super_edge[1]->next_same_super_edge[0] = df; + if (!dfp) { + dfp = ((struct rx_distinct_future *) + rx_cache_malloc_or_get(cache, + &cache->free_discernable_futures, + + sizeof(struct + rx_distinct_future))); + if (!dfp) + return 0; + if (!df) { + df = insert_before = dfp; + df->next_same_super_edge[0] = df->next_same_super_edge[1] = + df; + } else if (!insert_before) + insert_before = df; + else if (insert_before == df) + df = dfp; + + dfp->next_same_super_edge[0] = insert_before; + dfp->next_same_super_edge[1] + = insert_before->next_same_super_edge[1]; + dfp->next_same_super_edge[1]->next_same_super_edge[0] = dfp; + dfp->next_same_super_edge[0]->next_same_super_edge[1] = dfp; + dfp->next_same_dest = dfp->prev_same_dest = dfp; + dfp->future = 0; + dfp->present = superstate; + dfp->future_frame.inx = rx->instruction_table[rx_cache_miss]; + dfp->future_frame.data = 0; + dfp->future_frame.data_2 = (void *) dfp; + dfp->side_effects_frame.inx + = rx->instruction_table[rx_do_side_effects]; + dfp->side_effects_frame.data = 0; + dfp->side_effects_frame.data_2 = (void *) dfp; + dfp->effects = future->effects; + } } - } - return df; + return df; } @@ -3602,128 +3409,117 @@ include_futures (rx, df, state, superstate) * complexity here is memory management. */ #ifdef __STDC__ -RX_DECL struct rx_superstate * -rx_superstate (struct rx *rx, - struct rx_superset *set) -#else -RX_DECL struct rx_superstate * -rx_superstate (rx, set) - struct rx *rx; - struct rx_superset *set; -#endif -{ - struct rx_cache * cache = rx->cache; - struct rx_superstate * superstate = 0; - - /* Does the superstate already exist in the cache? */ - if (set->superstate) - { - if (set->superstate->rx_id != rx->rx_id) - { - /* Aha. It is in the cache, but belongs to a superstate - * that refers to an NFA that no longer exists. - * (We know it no longer exists because it was evidently - * stored in the same region of memory as the current nfa - * yet it has a different id.) - */ - superstate = set->superstate; - if (!superstate->is_semifree) - { - if (cache->lru_superstate == superstate) - { - cache->lru_superstate = superstate->next_recyclable; - if (cache->lru_superstate == superstate) - cache->lru_superstate = 0; +RX_DECL struct rx_superstate *rx_superstate(struct rx *rx, + struct rx_superset *set) +#else +RX_DECL struct rx_superstate *rx_superstate(rx, set) +struct rx *rx; +struct rx_superset *set; +#endif +{ + struct rx_cache *cache = rx->cache; + struct rx_superstate *superstate = 0; + + /* Does the superstate already exist in the cache? */ + if (set->superstate) { + if (set->superstate->rx_id != rx->rx_id) { + /* Aha. It is in the cache, but belongs to a superstate + * that refers to an NFA that no longer exists. + * (We know it no longer exists because it was evidently + * stored in the same region of memory as the current nfa + * yet it has a different id.) + */ + superstate = set->superstate; + if (!superstate->is_semifree) { + if (cache->lru_superstate == superstate) { + cache->lru_superstate = superstate->next_recyclable; + if (cache->lru_superstate == superstate) + cache->lru_superstate = 0; + } + { + superstate->next_recyclable->prev_recyclable + = superstate->prev_recyclable; + superstate->prev_recyclable->next_recyclable + = superstate->next_recyclable; + if (!cache->semifree_superstate) { + (cache->semifree_superstate + = superstate->next_recyclable + = superstate->prev_recyclable = superstate); + } else { + superstate->next_recyclable = + cache->semifree_superstate; + superstate->prev_recyclable = + cache->semifree_superstate->prev_recyclable; + superstate->next_recyclable->prev_recyclable = + superstate; + superstate->prev_recyclable->next_recyclable = + superstate; + cache->semifree_superstate = superstate; + } + ++cache->semifree_superstates; + } + } + set->superstate = 0; + goto handle_cache_miss; } - { - superstate->next_recyclable->prev_recyclable - = superstate->prev_recyclable; - superstate->prev_recyclable->next_recyclable - = superstate->next_recyclable; - if (!cache->semifree_superstate) - { - (cache->semifree_superstate - = superstate->next_recyclable - = superstate->prev_recyclable - = superstate); - } - else - { - superstate->next_recyclable = cache->semifree_superstate; - superstate->prev_recyclable - = cache->semifree_superstate->prev_recyclable; - superstate->next_recyclable->prev_recyclable - = superstate; - superstate->prev_recyclable->next_recyclable - = superstate; - cache->semifree_superstate = superstate; - } - ++cache->semifree_superstates; - } - } - set->superstate = 0; - goto handle_cache_miss; - } - ++cache->hits; - superstate = set->superstate; + ++cache->hits; + superstate = set->superstate; - rx_refresh_this_superstate (cache, superstate); - return superstate; - } + rx_refresh_this_superstate(cache, superstate); + return superstate; + } - handle_cache_miss: + handle_cache_miss: - /* This point reached only for cache misses. */ - ++cache->misses; + /* This point reached only for cache misses. */ + ++cache->misses; #if RX_DEBUG - if (rx_debug_trace > 1) - { - struct rx_superset * setp = set; - fprintf (stderr, "Building a superstet %d(%d): ", rx->rx_id, set); - while (setp) - { - fprintf (stderr, "%d ", setp->id); - setp = setp->cdr; + if (rx_debug_trace > 1) { + struct rx_superset *setp = set; + + fprintf(stderr, "Building a superstet %d(%d): ", rx->rx_id, set); + while (setp) { + fprintf(stderr, "%d ", setp->id); + setp = setp->cdr; + } + fprintf(stderr, "(%d)\n", set); } - fprintf (stderr, "(%d)\n", set); - } #endif - superstate = (struct rx_superstate *)rx_cache_get_superstate (cache); - if (!superstate) - return 0; - - if (!cache->lru_superstate) - (cache->lru_superstate - = superstate->next_recyclable - = superstate->prev_recyclable - = superstate); - else - { - superstate->next_recyclable = cache->lru_superstate; - superstate->prev_recyclable = cache->lru_superstate->prev_recyclable; - ( superstate->prev_recyclable->next_recyclable - = superstate->next_recyclable->prev_recyclable - = superstate); - } - superstate->rx_id = rx->rx_id; - superstate->transition_refs = 0; - superstate->locks = 0; - superstate->is_semifree = 0; - set->superstate = superstate; - superstate->contents = set; - rx_protect_superset (rx, set); - superstate->edges = 0; - { - int x; - /* None of the transitions from this superstate are known yet. */ - for (x = 0; x < rx->local_cset_size; ++x) /* &&&&& 3.8 % */ - { - struct rx_inx * ifr = &superstate->transitions[x]; - ifr->inx = rx->instruction_table [rx_cache_miss]; - ifr->data = ifr->data_2 = 0; - } - } - return superstate; + superstate = (struct rx_superstate *) rx_cache_get_superstate(cache); + if (!superstate) + return 0; + + if (!cache->lru_superstate) + (cache->lru_superstate + = superstate->next_recyclable + = superstate->prev_recyclable = superstate); + else { + superstate->next_recyclable = cache->lru_superstate; + superstate->prev_recyclable = + cache->lru_superstate->prev_recyclable; + (superstate->prev_recyclable->next_recyclable = + superstate->next_recyclable->prev_recyclable = superstate); + } + superstate->rx_id = rx->rx_id; + superstate->transition_refs = 0; + superstate->locks = 0; + superstate->is_semifree = 0; + set->superstate = superstate; + superstate->contents = set; + rx_protect_superset(rx, set); + superstate->edges = 0; + { + int x; + + /* None of the transitions from this superstate are known yet. */ + for (x = 0; x < rx->local_cset_size; ++x) { /* &&&&& 3.8 % */ + struct rx_inx *ifr = &superstate->transitions[x]; + + ifr->inx = rx->instruction_table[rx_cache_miss]; + ifr->data = ifr->data_2 = 0; + } + } + return superstate; } @@ -3733,98 +3529,96 @@ rx_superstate (rx, set) */ #ifdef __STDC__ -static int -solve_destination (struct rx *rx, struct rx_distinct_future *df) -#else -static int -solve_destination (rx, df) - struct rx *rx; - struct rx_distinct_future *df; -#endif -{ - struct rx_super_edge *tc = df->edge; - struct rx_superset *nfa_state; - struct rx_superset *nil_set = rx_superset_cons (rx, 0, 0); - struct rx_superset *solution = nil_set; - struct rx_superstate *dest; - - rx_protect_superset (rx, solution); - /* Iterate over all NFA states in the state set of this superstate. */ - for (nfa_state = df->present->contents; - nfa_state->car; - nfa_state = nfa_state->cdr) - { - struct rx_nfa_edge *e; - /* Iterate over all edges of each NFA state. */ - for (e = nfa_state->car->edges; e; e = e->next) - /* If we find an edge that is labeled with - * the characters we are solving for..... +static int solve_destination(struct rx *rx, struct rx_distinct_future *df) +#else +static int solve_destination(rx, df) +struct rx *rx; +struct rx_distinct_future *df; +#endif +{ + struct rx_super_edge *tc = df->edge; + struct rx_superset *nfa_state; + struct rx_superset *nil_set = rx_superset_cons(rx, 0, 0); + struct rx_superset *solution = nil_set; + struct rx_superstate *dest; + + rx_protect_superset(rx, solution); + /* Iterate over all NFA states in the state set of this superstate. */ + for (nfa_state = df->present->contents; + nfa_state->car; nfa_state = nfa_state->cdr) { + struct rx_nfa_edge *e; + + /* Iterate over all edges of each NFA state. */ + for (e = nfa_state->car->edges; e; e = e->next) + /* If we find an edge that is labeled with + * the characters we are solving for..... + */ + if (rx_bitset_is_subset(rx->local_cset_size, + tc->cset, e->params.cset)) { + struct rx_nfa_state *n = e->dest; + struct rx_possible_future *pf; + + /* ....search the partial epsilon closures of the destination + * of that edge for a path that involves the same set of + * side effects we are solving for. + * If we find such a RX_POSSIBLE_FUTURE, we add members to the + * stateset we are computing. + */ + for (pf = n->futures; pf; pf = pf->next) + if (pf->effects == df->effects) { + struct rx_superset *old_sol; + + old_sol = solution; + solution = + rx_superstate_eclosure_union(rx, solution, + pf->destset); + if (!solution) + return 0; + rx_protect_superset(rx, solution); + rx_release_superset(rx, old_sol); + } + } + } + /* It is possible that the RX_DISTINCT_FUTURE we are working on has + * the empty set of NFA states as its definition. In that case, this + * is a failure point. */ - if (rx_bitset_is_subset (rx->local_cset_size, - tc->cset, e->params.cset)) - { - struct rx_nfa_state *n = e->dest; - struct rx_possible_future *pf; - /* ....search the partial epsilon closures of the destination - * of that edge for a path that involves the same set of - * side effects we are solving for. - * If we find such a RX_POSSIBLE_FUTURE, we add members to the - * stateset we are computing. - */ - for (pf = n->futures; pf; pf = pf->next) - if (pf->effects == df->effects) - { - struct rx_superset * old_sol; - old_sol = solution; - solution = rx_superstate_eclosure_union (rx, solution, - pf->destset); - if (!solution) - return 0; - rx_protect_superset (rx, solution); - rx_release_superset (rx, old_sol); + if (solution == nil_set) { + df->future_frame.inx = (void *) rx_backtrack; + df->future_frame.data = 0; + df->future_frame.data_2 = 0; + return 1; + } + dest = rx_superstate(rx, solution); + rx_release_superset(rx, solution); + if (!dest) + return 0; + + { + struct rx_distinct_future *dft; + + dft = df; + df->prev_same_dest->next_same_dest = 0; + while (dft) { + dft->future = dest; + dft->future_frame.inx = rx->instruction_table[rx_next_char]; + dft->future_frame.data = (void *) dest->transitions; + dft = dft->next_same_dest; } - } - } - /* It is possible that the RX_DISTINCT_FUTURE we are working on has - * the empty set of NFA states as its definition. In that case, this - * is a failure point. - */ - if (solution == nil_set) - { - df->future_frame.inx = (void *) rx_backtrack; - df->future_frame.data = 0; - df->future_frame.data_2 = 0; - return 1; - } - dest = rx_superstate (rx, solution); - rx_release_superset (rx, solution); - if (!dest) - return 0; - - { - struct rx_distinct_future *dft; - dft = df; - df->prev_same_dest->next_same_dest = 0; - while (dft) - { - dft->future = dest; - dft->future_frame.inx = rx->instruction_table[rx_next_char]; - dft->future_frame.data = (void *) dest->transitions; - dft = dft->next_same_dest; - } - df->prev_same_dest->next_same_dest = df; - } - if (!dest->transition_refs) - dest->transition_refs = df; - else - { - struct rx_distinct_future *dft = dest->transition_refs->next_same_dest; - dest->transition_refs->next_same_dest = df->next_same_dest; - df->next_same_dest->prev_same_dest = dest->transition_refs; - df->next_same_dest = dft; - dft->prev_same_dest = df; - } - return 1; + df->prev_same_dest->next_same_dest = df; + } + if (!dest->transition_refs) + dest->transition_refs = df; + else { + struct rx_distinct_future *dft = + + dest->transition_refs->next_same_dest; + dest->transition_refs->next_same_dest = df->next_same_dest; + df->next_same_dest->prev_same_dest = dest->transition_refs; + df->next_same_dest = dft; + dft->prev_same_dest = df; + } + return 1; } @@ -3838,81 +3632,87 @@ solve_destination (rx, df) } */ #ifdef __STDC__ -static int -compute_super_edge (struct rx *rx, struct rx_distinct_future **dfout, - rx_Bitset csetout, struct rx_superstate *superstate, - unsigned char chr) -#else -static int -compute_super_edge (rx, dfout, csetout, superstate, chr) - struct rx *rx; - struct rx_distinct_future **dfout; - rx_Bitset csetout; - struct rx_superstate *superstate; - unsigned char chr; -#endif -{ - struct rx_superset *stateset = superstate->contents; - - /* To compute the set of characters that share edges with CHR, - * we start with the full character set, and subtract. - */ - rx_bitset_universe (rx->local_cset_size, csetout); - *dfout = 0; - - /* Iterate over the NFA states in the superstate state-set. */ - while (stateset->car) - { - struct rx_nfa_edge *e; - for (e = stateset->car->edges; e; e = e->next) - if (RX_bitset_member (e->params.cset, chr)) - { - /* If we find an NFA edge that applies, we make sure there - * are corresponding edges in the superstate NFA. - */ - { - struct rx_distinct_future * saved; - saved = *dfout; - *dfout = include_futures (rx, *dfout, e->dest, superstate); - if (!*dfout) - { - struct rx_distinct_future * df; - df = saved; - if (df) - df->next_same_super_edge[1]->next_same_super_edge[0] = 0; - while (df) - { - struct rx_distinct_future *dft; - dft = df; - df = df->next_same_super_edge[0]; - - if (dft->future && dft->future->transition_refs == dft) - { - dft->future->transition_refs = dft->next_same_dest; - if (dft->future->transition_refs == dft) - dft->future->transition_refs = 0; - } - dft->next_same_dest->prev_same_dest = dft->prev_same_dest; - dft->prev_same_dest->next_same_dest = dft->next_same_dest; - rx_cache_free (rx->cache, - &rx->cache->free_discernable_futures, - (char *)dft); - } - return 0; - } - } - /* We also trim the character set a bit. */ - rx_bitset_intersection (rx->local_cset_size, - csetout, e->params.cset); - } - else - /* An edge that doesn't apply at least tells us some characters - * that don't share the same edge set as CHR. - */ - rx_bitset_difference (rx->local_cset_size, csetout, e->params.cset); - stateset = stateset->cdr; - } - return 1; +static int +compute_super_edge(struct rx *rx, struct rx_distinct_future **dfout, + rx_Bitset csetout, struct rx_superstate *superstate, + unsigned char chr) +#else +static int compute_super_edge(rx, dfout, csetout, superstate, chr) +struct rx *rx; +struct rx_distinct_future **dfout; +rx_Bitset csetout; +struct rx_superstate *superstate; +unsigned char chr; +#endif +{ + struct rx_superset *stateset = superstate->contents; + + /* To compute the set of characters that share edges with CHR, + * we start with the full character set, and subtract. + */ + rx_bitset_universe(rx->local_cset_size, csetout); + *dfout = 0; + + /* Iterate over the NFA states in the superstate state-set. */ + while (stateset->car) { + struct rx_nfa_edge *e; + + for (e = stateset->car->edges; e; e = e->next) + if (RX_bitset_member(e->params.cset, chr)) { + /* If we find an NFA edge that applies, we make sure there + * are corresponding edges in the superstate NFA. + */ + { + struct rx_distinct_future *saved; + + saved = *dfout; + *dfout = + include_futures(rx, *dfout, e->dest, superstate); + if (!*dfout) { + struct rx_distinct_future *df; + + df = saved; + if (df) + df-> + next_same_super_edge + [1]->next_same_super_edge[0] = 0; + while (df) { + struct rx_distinct_future *dft; + + dft = df; + df = df->next_same_super_edge[0]; + + if (dft->future + && dft->future->transition_refs == dft) { + dft->future->transition_refs = + dft->next_same_dest; + if (dft->future->transition_refs == dft) + dft->future->transition_refs = 0; + } + dft->next_same_dest->prev_same_dest = + dft->prev_same_dest; + dft->prev_same_dest->next_same_dest = + dft->next_same_dest; + rx_cache_free(rx->cache, + &rx-> + cache->free_discernable_futures, + (char *) dft); + } + return 0; + } + } + /* We also trim the character set a bit. */ + rx_bitset_intersection(rx->local_cset_size, + csetout, e->params.cset); + } else + /* An edge that doesn't apply at least tells us some characters + * that don't share the same edge set as CHR. + */ + rx_bitset_difference(rx->local_cset_size, csetout, + e->params.cset); + stateset = stateset->cdr; + } + return 1; } @@ -3924,46 +3724,46 @@ compute_super_edge (rx, dfout, csetout, superstate, chr) #ifdef __STDC__ -static struct rx_super_edge * -rx_super_edge (struct rx *rx, - struct rx_superstate *super, rx_Bitset cset, - struct rx_distinct_future *df) -#else -static struct rx_super_edge * -rx_super_edge (rx, super, cset, df) - struct rx *rx; - struct rx_superstate *super; - rx_Bitset cset; - struct rx_distinct_future *df; -#endif -{ - struct rx_super_edge *tc = - (struct rx_super_edge *)rx_cache_malloc_or_get - (rx->cache, &rx->cache->free_transition_classes, - sizeof (struct rx_super_edge) + rx_sizeof_bitset (rx->local_cset_size)); - - if (!tc) - return 0; - tc->next = super->edges; - super->edges = tc; - tc->rx_backtrack_frame.inx = rx->instruction_table[rx_backtrack_point]; - tc->rx_backtrack_frame.data = 0; - tc->rx_backtrack_frame.data_2 = (void *) tc; - tc->options = df; - tc->cset = (rx_Bitset) ((char *) tc + sizeof (*tc)); - rx_bitset_assign (rx->local_cset_size, tc->cset, cset); - if (df) - { - struct rx_distinct_future * dfp = df; - df->next_same_super_edge[1]->next_same_super_edge[0] = 0; - while (dfp) - { - dfp->edge = tc; - dfp = dfp->next_same_super_edge[0]; +static struct rx_super_edge *rx_super_edge(struct rx *rx, + struct rx_superstate *super, + rx_Bitset cset, + struct rx_distinct_future *df) +#else +static struct rx_super_edge *rx_super_edge(rx, super, cset, df) +struct rx *rx; +struct rx_superstate *super; +rx_Bitset cset; +struct rx_distinct_future *df; +#endif +{ + struct rx_super_edge *tc = + (struct rx_super_edge *) rx_cache_malloc_or_get + (rx->cache, &rx->cache->free_transition_classes, + sizeof(struct rx_super_edge) + + + rx_sizeof_bitset(rx->local_cset_size)); + + if (!tc) + return 0; + tc->next = super->edges; + super->edges = tc; + tc->rx_backtrack_frame.inx = rx->instruction_table[rx_backtrack_point]; + tc->rx_backtrack_frame.data = 0; + tc->rx_backtrack_frame.data_2 = (void *) tc; + tc->options = df; + tc->cset = (rx_Bitset) ((char *) tc + sizeof(*tc)); + rx_bitset_assign(rx->local_cset_size, tc->cset, cset); + if (df) { + struct rx_distinct_future *dfp = df; + + df->next_same_super_edge[1]->next_same_super_edge[0] = 0; + while (dfp) { + dfp->edge = tc; + dfp = dfp->next_same_super_edge[0]; + } + df->next_same_super_edge[1]->next_same_super_edge[0] = df; } - df->next_same_super_edge[1]->next_same_super_edge[0] = df; - } - return tc; + return tc; } @@ -3984,164 +3784,158 @@ rx_super_edge (rx, super, cset, df) #ifdef __STDC__ static void -install_partial_transition (struct rx_superstate *super, - struct rx_inx *answer, - RX_subset set, int offset) -#else -static void -install_partial_transition (super, answer, set, offset) - struct rx_superstate *super; - struct rx_inx *answer; - RX_subset set; - int offset; -#endif -{ - int start = offset; - int end = start + 32; - RX_subset pos = 1; - struct rx_inx * transitions = super->transitions; - - while (start < end) - { - if (set & pos) - transitions[start] = *answer; - pos <<= 1; - ++start; - } +install_partial_transition(struct rx_superstate *super, + struct rx_inx *answer, + RX_subset set, int offset) +#else +static void install_partial_transition(super, answer, set, offset) +struct rx_superstate *super; +struct rx_inx *answer; +RX_subset set; +int offset; +#endif +{ + int start = offset; + int end = start + 32; + RX_subset pos = 1; + struct rx_inx *transitions = super->transitions; + + while (start < end) { + if (set & pos) + transitions[start] = *answer; + pos <<= 1; + ++start; + } } #ifdef __STDC__ -RX_DECL struct rx_inx * -rx_handle_cache_miss - (struct rx *rx, struct rx_superstate *super, unsigned char chr, void *data) -#else -RX_DECL struct rx_inx * -rx_handle_cache_miss (rx, super, chr, data) - struct rx *rx; - struct rx_superstate *super; - unsigned char chr; - void *data; -#endif -{ - int offset = chr / RX_subset_bits; - struct rx_distinct_future *df = data; - - if (!df) /* must be the shared_cache_miss_frame */ - { - /* Perhaps this is just a transition waiting to be filled. */ - struct rx_super_edge *tc; - RX_subset mask = rx_subset_singletons [chr % RX_subset_bits]; - - for (tc = super->edges; tc; tc = tc->next) - if (tc->cset[offset] & mask) - { - struct rx_inx * answer; - df = tc->options; - answer = ((tc->options->next_same_super_edge[0] != tc->options) - ? &tc->rx_backtrack_frame - : (df->effects - ? &df->side_effects_frame - : &df->future_frame)); - install_partial_transition (super, answer, - tc->cset [offset], offset * 32); - return answer; - } - /* Otherwise, it's a flushed or newly encountered edge. */ - { - char cset_space[1024]; /* this limit is far from unreasonable */ - rx_Bitset trcset; - struct rx_inx *answer; - - if (rx_sizeof_bitset (rx->local_cset_size) > sizeof (cset_space)) - return 0; /* If the arbitrary limit is hit, always fail */ - /* cleanly. */ - trcset = (rx_Bitset)cset_space; - rx_lock_superstate (rx, super); - if (!compute_super_edge (rx, &df, trcset, super, chr)) - { - rx_unlock_superstate (rx, super); - return 0; - } - if (!df) /* We just computed the fail transition. */ - { - static struct rx_inx - shared_fail_frame = { 0, 0, (void *)rx_backtrack, 0 }; - answer = &shared_fail_frame; - } - else - { - tc = rx_super_edge (rx, super, trcset, df); - if (!tc) - { - rx_unlock_superstate (rx, super); - return 0; - } - answer = ((tc->options->next_same_super_edge[0] != tc->options) - ? &tc->rx_backtrack_frame - : (df->effects - ? &df->side_effects_frame - : &df->future_frame)); - } - install_partial_transition (super, answer, - trcset[offset], offset * 32); - rx_unlock_superstate (rx, super); - return answer; - } - } - else if (df->future) /* A cache miss on an edge with a future? Must be - * a semi-free destination. */ - { - if (df->future->is_semifree) - refresh_semifree_superstate (rx->cache, df->future); - return &df->future_frame; - } - else - /* no future superstate on an existing edge */ - { - rx_lock_superstate (rx, super); - if (!solve_destination (rx, df)) +RX_DECL struct rx_inx *rx_handle_cache_miss + (struct rx *rx, struct rx_superstate *super, unsigned char chr, + void *data) +#else +RX_DECL struct rx_inx *rx_handle_cache_miss(rx, super, chr, data) +struct rx *rx; +struct rx_superstate *super; +unsigned char chr; +void *data; +#endif +{ + int offset = chr / RX_subset_bits; + struct rx_distinct_future *df = data; + + if (!df) { /* must be the shared_cache_miss_frame */ + /* Perhaps this is just a transition waiting to be filled. */ + struct rx_super_edge *tc; + RX_subset mask = rx_subset_singletons[chr % RX_subset_bits]; + + for (tc = super->edges; tc; tc = tc->next) + if (tc->cset[offset] & mask) { + struct rx_inx *answer; + + df = tc->options; + answer = + ((tc->options->next_same_super_edge[0] != + tc->options) ? &tc-> + rx_backtrack_frame : (df->effects ? + &df->side_effects_frame : + &df->future_frame)); + install_partial_transition(super, answer, tc->cset[offset], + offset * 32); + return answer; + } + /* Otherwise, it's a flushed or newly encountered edge. */ + { + char cset_space[1024]; /* this limit is far from unreasonable */ + rx_Bitset trcset; + struct rx_inx *answer; + + if (rx_sizeof_bitset(rx->local_cset_size) > sizeof(cset_space)) + return 0; /* If the arbitrary limit is hit, always fail */ + /* cleanly. */ + trcset = (rx_Bitset) cset_space; + rx_lock_superstate(rx, super); + if (!compute_super_edge(rx, &df, trcset, super, chr)) { + rx_unlock_superstate(rx, super); + return 0; + } + if (!df) { /* We just computed the fail transition. */ + static struct rx_inx + shared_fail_frame = { 0, 0, (void *) rx_backtrack, 0 }; + + answer = &shared_fail_frame; + } else { + tc = rx_super_edge(rx, super, trcset, df); + if (!tc) { + rx_unlock_superstate(rx, super); + return 0; + } + answer = + ((tc->options->next_same_super_edge[0] != + tc->options) ? &tc-> + rx_backtrack_frame : (df->effects ? + &df->side_effects_frame : + &df->future_frame)); + } + install_partial_transition(super, answer, + trcset[offset], offset * 32); + rx_unlock_superstate(rx, super); + return answer; + } + } else if (df->future) { /* A cache miss on an edge with a future? Must be + * a semi-free destination. */ + if (df->future->is_semifree) + refresh_semifree_superstate(rx->cache, df->future); + return &df->future_frame; + } else + /* no future superstate on an existing edge */ { - rx_unlock_superstate (rx, super); - return 0; + rx_lock_superstate(rx, super); + if (!solve_destination(rx, df)) { + rx_unlock_superstate(rx, super); + return 0; + } + if (!df->effects + && (df->edge->options->next_same_super_edge[0] == + df->edge->options)) install_partial_transition(super, + &df->future_frame, + df-> + edge->cset + [offset], + offset * + 32); + rx_unlock_superstate(rx, super); + return &df->future_frame; } - if (!df->effects - && (df->edge->options->next_same_super_edge[0] == df->edge->options)) - install_partial_transition (super, &df->future_frame, - df->edge->cset[offset], offset * 32); - rx_unlock_superstate (rx, super); - return &df->future_frame; - } } + - /* The rest of the code provides a regex.c compatable interface. */ -__const__ char *re_error_msg[] = -{ - 0, /* REG_NOUT */ - "No match", /* REG_NOMATCH */ - "Invalid regular expression", /* REG_BADPAT */ - "Invalid collation character", /* REG_ECOLLATE */ - "Invalid character class name", /* REG_ECTYPE */ - "Trailing backslash", /* REG_EESCAPE */ - "Invalid back reference", /* REG_ESUBREG */ - "Unmatched [ or [^", /* REG_EBRACK */ - "Unmatched ( or \\(", /* REG_EPAREN */ - "Unmatched \\{", /* REG_EBRACE */ - "Invalid content of \\{\\}", /* REG_BADBR */ - "Invalid range end", /* REG_ERANGE */ - "Memory exhausted", /* REG_ESPACE */ - "Invalid preceding regular expression", /* REG_BADRPT */ - "Premature end of regular expression", /* REG_EEND */ - "Regular expression too big", /* REG_ESIZE */ - "Unmatched ) or \\)", /* REG_ERPAREN */ +__const__ char *re_error_msg[] = { + 0, /* REG_NOUT */ + "No match", /* REG_NOMATCH */ + "Invalid regular expression", /* REG_BADPAT */ + "Invalid collation character", /* REG_ECOLLATE */ + "Invalid character class name", /* REG_ECTYPE */ + "Trailing backslash", /* REG_EESCAPE */ + "Invalid back reference", /* REG_ESUBREG */ + "Unmatched [ or [^", /* REG_EBRACK */ + "Unmatched ( or \\(", /* REG_EPAREN */ + "Unmatched \\{", /* REG_EBRACE */ + "Invalid content of \\{\\}", /* REG_BADBR */ + "Invalid range end", /* REG_ERANGE */ + "Memory exhausted", /* REG_ESPACE */ + "Invalid preceding regular expression", /* REG_BADRPT */ + "Premature end of regular expression", /* REG_EEND */ + "Regular expression too big", /* REG_ESIZE */ + "Unmatched ) or \\)", /* REG_ERPAREN */ }; + - /* * Macros used while compiling patterns. * @@ -4186,61 +3980,57 @@ typedef unsigned regnum_t; */ typedef int pattern_offset_t; -typedef struct -{ - struct rexp_node ** top_expression; /* was begalt */ - struct rexp_node ** last_expression; /* was laststart */ - pattern_offset_t inner_group_offset; - regnum_t regnum; +typedef struct { + struct rexp_node **top_expression; /* was begalt */ + struct rexp_node **last_expression; /* was laststart */ + pattern_offset_t inner_group_offset; + regnum_t regnum; } compile_stack_elt_t; -typedef struct -{ - compile_stack_elt_t *stack; - unsigned size; - unsigned avail; /* Offset of next open position. */ +typedef struct { + compile_stack_elt_t *stack; + unsigned size; + unsigned avail; /* Offset of next open position. */ } compile_stack_type; -static boolean - group_in_compile_stack (compile_stack_type, regnum_t); +static boolean group_in_compile_stack(compile_stack_type, regnum_t); static reg_errcode_t - compile_range (struct re_pattern_buffer *, rx_Bitset, - __const__ char **, __const__ char *, - unsigned char *, reg_syntax_t, - rx_Bitset, char *); -static void find_backrefs (char *, struct rexp_node *, - struct re_se_params *); -static int compute_fastset (struct re_pattern_buffer *, - struct rexp_node *); -static int is_anchored (struct rexp_node *, rx_side_effect); +compile_range(struct re_pattern_buffer *, rx_Bitset, + __const__ char **, __const__ char *, + unsigned char *, reg_syntax_t, rx_Bitset, char *); +static void find_backrefs(char *, struct rexp_node *, + + struct re_se_params *); +static int compute_fastset(struct re_pattern_buffer *, struct rexp_node *); +static int is_anchored(struct rexp_node *, rx_side_effect); static struct rexp_node - *remove_unecessary_side_effects - (struct rx *, char *, - struct rexp_node *, - struct re_se_params *); -static int pointless_if_repeated (struct rexp_node *, - struct re_se_params *); -static int registers_on_stack (struct re_pattern_buffer *, - struct rexp_node *, - int, struct re_se_params *); -static int has_any_se (struct rx *, struct rexp_node *); -static int has_non_idempotent_epsilon_path - (struct rx *, struct rexp_node *, - struct re_se_params *); -static int begins_with_complex_se (struct rx *, struct rexp_node *); -static void speed_up_alt (struct rx *, struct rexp_node *, int); +*remove_unecessary_side_effects + + (struct rx *, char *, struct rexp_node *, struct re_se_params *); +static int pointless_if_repeated(struct rexp_node *, + + struct re_se_params *); +static int registers_on_stack(struct re_pattern_buffer *, + struct rexp_node *, + + int, struct re_se_params *); +static int has_any_se(struct rx *, struct rexp_node *); +static int has_non_idempotent_epsilon_path + + (struct rx *, struct rexp_node *, struct re_se_params *); +static int begins_with_complex_se(struct rx *, struct rexp_node *); +static void speed_up_alt(struct rx *, struct rexp_node *, int); RX_DECL reg_errcode_t - rx_compile (__const__ char *, int, reg_syntax_t, - struct re_pattern_buffer *); -RX_DECL void rx_blow_up_fastmap (struct re_pattern_buffer *); + +rx_compile(__const__ char *, int, reg_syntax_t, + struct re_pattern_buffer *); +RX_DECL void rx_blow_up_fastmap(struct re_pattern_buffer *); static __inline__ enum rx_get_burst_return - re_search_2_get_burst (struct rx_string_position *, - void *, int); +re_search_2_get_burst(struct rx_string_position *, void *, int); static __inline__ enum rx_back_check_return - re_search_2_back_check (struct rx_string_position *, int, - int, unsigned char *, void *, int); +re_search_2_back_check(struct rx_string_position *, int, + int, unsigned char *, void *, int); static __inline__ int - re_search_2_fetch_char (struct rx_string_position *, - int, void *, int); +re_search_2_fetch_char(struct rx_string_position *, int, void *, int); #define INIT_COMPILE_STACK_SIZE 32 @@ -4272,9 +4062,9 @@ static __inline__ int PATFETCH (c); \ } \ } \ - } + } -#define CHAR_CLASS_MAX_LENGTH 6 /* Namely, `xdigit'. */ +#define CHAR_CLASS_MAX_LENGTH 6 /* Namely, `xdigit'. */ #define IS_CHAR_CLASS(string) \ (!strcmp (string, "alpha") || !strcmp (string, "upper") \ @@ -4283,8 +4073,8 @@ static __inline__ int || !strcmp (string, "space") || !strcmp (string, "print") \ || !strcmp (string, "punct") || !strcmp (string, "graph") \ || !strcmp (string, "cntrl") || !strcmp (string, "blank")) - + /* These predicates are used in regex_compile. */ /* P points to just after a ^ in PATTERN. Return true if that ^ comes @@ -4294,26 +4084,24 @@ static __inline__ int #ifdef __STDC__ static boolean -at_begline_loc_p (__const__ char *pattern, __const__ char * p, reg_syntax_t syntax) +at_begline_loc_p(__const__ char *pattern, __const__ char *p, + reg_syntax_t syntax) #else -static boolean -at_begline_loc_p (pattern, p, syntax) - __const__ char *pattern; - __const__ char * p; - reg_syntax_t syntax; +static boolean at_begline_loc_p(pattern, p, syntax) +__const__ char *pattern; +__const__ char *p; +reg_syntax_t syntax; #endif { - __const__ char *prev = p - 2; - boolean prev_prev_backslash = ((prev > pattern) && (prev[-1] == '\\')); - - return - - (/* After a subexpression? */ - ((*prev == '(') && ((syntax & RE_NO_BK_PARENS) || prev_prev_backslash)) - || - /* After an alternative? */ - ((*prev == '|') && ((syntax & RE_NO_BK_VBAR) || prev_prev_backslash)) - ); + __const__ char *prev = p - 2; + boolean prev_prev_backslash = ((prev > pattern) && (prev[-1] == '\\')); + + return ( /* After a subexpression? */ + ((*prev == '(') && ((syntax & RE_NO_BK_PARENS) || prev_prev_backslash)) + || + /* After an alternative? */ + ((*prev == '|') && ((syntax & RE_NO_BK_VBAR) || prev_prev_backslash)) + ); } /* The dual of at_begline_loc_p. This one is for $. We assume there is @@ -4322,64 +4110,61 @@ at_begline_loc_p (pattern, p, syntax) #ifdef __STDC__ static boolean -at_endline_loc_p (__const__ char *p, __const__ char *pend, int syntax) -#else -static boolean -at_endline_loc_p (p, pend, syntax) - __const__ char *p; - __const__ char *pend; - int syntax; -#endif -{ - __const__ char *next = p; - boolean next_backslash = (*next == '\\'); - __const__ char *next_next = (p + 1 < pend) ? (p + 1) : 0; - - return - ( - /* Before a subexpression? */ - ((syntax & RE_NO_BK_PARENS) - ? (*next == ')') - : (next_backslash && next_next && (*next_next == ')'))) - || - /* Before an alternative? */ - ((syntax & RE_NO_BK_VBAR) - ? (*next == '|') - : (next_backslash && next_next && (*next_next == '|'))) - ); +at_endline_loc_p(__const__ char *p, __const__ char *pend, int syntax) +#else +static boolean at_endline_loc_p(p, pend, syntax) +__const__ char *p; +__const__ char *pend; +int syntax; +#endif +{ + __const__ char *next = p; + boolean next_backslash = (*next == '\\'); + __const__ char *next_next = (p + 1 < pend) ? (p + 1) : 0; + + return ( + /* Before a subexpression? */ + ((syntax & RE_NO_BK_PARENS) + ? (*next == ')') + : (next_backslash && next_next && (*next_next == ')'))) + || + /* Before an alternative? */ + ((syntax & RE_NO_BK_VBAR) + ? (*next == '|') + : (next_backslash && next_next && (*next_next == '|'))) + ); } -unsigned char rx_id_translation[256] = -{ - 0, 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 +unsigned char rx_id_translation[256] = { + 0, 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 }; /* The compiler keeps an inverted translation table. @@ -4389,38 +4174,38 @@ unsigned char rx_id_translation[256] = #ifdef __STDC__ static rx_Bitset -inverse_translation (struct re_pattern_buffer * rxb, - char * valid, rx_Bitset cache, - unsigned char * translate, int c) +inverse_translation(struct re_pattern_buffer *rxb, + char *valid, rx_Bitset cache, + unsigned char *translate, int c) #else -static rx_Bitset -inverse_translation (rxb, valid, cache, translate, c) - struct re_pattern_buffer * rxb; - char * valid; - rx_Bitset cache; - unsigned char * translate; - int c; -#endif -{ - rx_Bitset cs - = cache + c * rx_bitset_numb_subsets (rxb->rx.local_cset_size); - - if (!valid[c]) - { - int x; - int c_tr = TRANSLATE(c); - rx_bitset_null (rxb->rx.local_cset_size, cs); - for (x = 0; x < 256; ++x) /* &&&& 13.37 */ - if (TRANSLATE(x) == c_tr) - RX_bitset_enjoin (cs, x); - valid[c] = 1; - } - return cs; -} +static rx_Bitset inverse_translation(rxb, valid, cache, translate, c) +struct re_pattern_buffer *rxb; +char *valid; +rx_Bitset cache; +unsigned char *translate; +int c; +#endif +{ + rx_Bitset cs + + = cache + c * rx_bitset_numb_subsets(rxb->rx.local_cset_size); + + if (!valid[c]) { + int x; + int c_tr = TRANSLATE(c); + rx_bitset_null(rxb->rx.local_cset_size, cs); + for (x = 0; x < 256; ++x) /* &&&& 13.37 */ + if (TRANSLATE(x) == c_tr) + RX_bitset_enjoin(cs, x); + valid[c] = 1; + } + return cs; +} + /* More subroutine declarations and macros for regex_compile. */ /* Returns true if REGNUM is in one of COMPILE_STACK's elements and @@ -4428,23 +4213,21 @@ inverse_translation (rxb, valid, cache, translate, c) #ifdef __STDC__ static boolean -group_in_compile_stack (compile_stack_type compile_stack, regnum_t regnum) +group_in_compile_stack(compile_stack_type compile_stack, regnum_t regnum) #else -static boolean -group_in_compile_stack (compile_stack, regnum) - compile_stack_type compile_stack; - regnum_t regnum; +static boolean group_in_compile_stack(compile_stack, regnum) +compile_stack_type compile_stack; +regnum_t regnum; #endif { - int this_element; + int this_element; - for (this_element = compile_stack.avail - 1; - this_element >= 0; - this_element--) - if (compile_stack.stack[this_element].regnum == regnum) - return true; + for (this_element = compile_stack.avail - 1; + this_element >= 0; this_element--) + if (compile_stack.stack[this_element].regnum == regnum) + return true; - return false; + return false; } @@ -4463,48 +4246,51 @@ group_in_compile_stack (compile_stack, regnum) #ifdef __STDC__ static reg_errcode_t -compile_range (struct re_pattern_buffer * rxb, rx_Bitset cs, - __const__ char ** p_ptr, __const__ char * pend, - unsigned char * translate, reg_syntax_t syntax, - rx_Bitset inv_tr, char * valid_inv_tr) +compile_range(struct re_pattern_buffer *rxb, rx_Bitset cs, + __const__ char **p_ptr, __const__ char *pend, + unsigned char *translate, reg_syntax_t syntax, + rx_Bitset inv_tr, char *valid_inv_tr) #else static reg_errcode_t -compile_range (rxb, cs, p_ptr, pend, translate, syntax, inv_tr, valid_inv_tr) - struct re_pattern_buffer * rxb; - rx_Bitset cs; - __const__ char ** p_ptr; - __const__ char * pend; - unsigned char * translate; - reg_syntax_t syntax; - rx_Bitset inv_tr; - char * valid_inv_tr; +compile_range(rxb, cs, p_ptr, pend, translate, syntax, inv_tr, + valid_inv_tr) +struct re_pattern_buffer *rxb; +rx_Bitset cs; +__const__ char **p_ptr; +__const__ char *pend; +unsigned char *translate; +reg_syntax_t syntax; +rx_Bitset inv_tr; +char *valid_inv_tr; #endif { - unsigned this_char; + unsigned this_char; - __const__ char *p = *p_ptr; + __const__ char *p = *p_ptr; - unsigned char range_end; - unsigned char range_start = TRANSLATE(p[-2]); + unsigned char range_end; + unsigned char range_start = TRANSLATE(p[-2]); - if (p == pend) - return REG_ERANGE; + if (p == pend) + return REG_ERANGE; - PATFETCH (range_end); + PATFETCH(range_end); - (*p_ptr)++; + (*p_ptr)++; - if (range_start > range_end) - return syntax & RE_NO_EMPTY_RANGES ? REG_ERANGE : REG_NOERROR; + if (range_start > range_end) + return syntax & RE_NO_EMPTY_RANGES ? REG_ERANGE : REG_NOERROR; - for (this_char = range_start; this_char <= range_end; this_char++) - { - rx_Bitset it = - inverse_translation (rxb, valid_inv_tr, inv_tr, translate, this_char); - rx_bitset_union (rxb->rx.local_cset_size, cs, it); - } - - return REG_NOERROR; + for (this_char = range_start; this_char <= range_end; this_char++) { + rx_Bitset it = + inverse_translation(rxb, valid_inv_tr, inv_tr, translate, + + this_char); + + rx_bitset_union(rxb->rx.local_cset_size, cs, it); + } + + return REG_NOERROR; } @@ -4519,84 +4305,81 @@ compile_range (rxb, cs, p_ptr, pend, translate, syntax, inv_tr, valid_inv_tr) #ifdef __STDC__ static void -find_backrefs (char * out, struct rexp_node * rexp, - struct re_se_params * params) -#else -static void -find_backrefs (out, rexp, params) - char * out; - struct rexp_node * rexp; - struct re_se_params * params; -#endif -{ - if (rexp) - switch (rexp->type) - { - case r_cset: - case r_data: - return; - case r_alternate: - case r_concat: - case r_opt: - case r_star: - case r_2phase_star: - find_backrefs (out, rexp->params.pair.left, params); - find_backrefs (out, rexp->params.pair.right, params); - return; - case r_side_effect: - if ( ((long)rexp->params.side_effect >= 0) - && (params [(long)rexp->params.side_effect].se == re_se_backref)) - out[ params [(long)rexp->params.side_effect].op1] = 1; - return; - } +find_backrefs(char *out, struct rexp_node *rexp, + struct re_se_params *params) +#else +static void find_backrefs(out, rexp, params) +char *out; +struct rexp_node *rexp; +struct re_se_params *params; +#endif +{ + if (rexp) + switch (rexp->type) { + case r_cset: + case r_data: + return; + case r_alternate: + case r_concat: + case r_opt: + case r_star: + case r_2phase_star: + find_backrefs(out, rexp->params.pair.left, params); + find_backrefs(out, rexp->params.pair.right, params); + return; + case r_side_effect: + if (((long) rexp->params.side_effect >= 0) + && (params[(long) rexp->params.side_effect].se == + re_se_backref)) + out[params[(long) rexp->params.side_effect].op1] = 1; + return; + } } - + /* Returns 0 unless the pattern can match the empty string. */ #ifdef __STDC__ static int -compute_fastset (struct re_pattern_buffer * rxb, struct rexp_node * rexp) +compute_fastset(struct re_pattern_buffer *rxb, struct rexp_node *rexp) #else -static int -compute_fastset (rxb, rexp) - struct re_pattern_buffer * rxb; - struct rexp_node * rexp; -#endif -{ - if (!rexp) - return 1; - switch (rexp->type) - { - case r_data: - return 1; - case r_cset: - { - rx_bitset_union (rxb->rx.local_cset_size, - rxb->fastset, rexp->params.cset); - } - return 0; - case r_concat: - return (compute_fastset (rxb, rexp->params.pair.left) - && compute_fastset (rxb, rexp->params.pair.right)); - case r_2phase_star: - compute_fastset (rxb, rexp->params.pair.left); - /* compute_fastset (rxb, rexp->params.pair.right); nope... */ - return 1; - case r_alternate: - return !!(compute_fastset (rxb, rexp->params.pair.left) - + compute_fastset (rxb, rexp->params.pair.right)); - case r_opt: - case r_star: - compute_fastset (rxb, rexp->params.pair.left); - return 1; - case r_side_effect: - return 1; - } +static int compute_fastset(rxb, rexp) +struct re_pattern_buffer *rxb; +struct rexp_node *rexp; +#endif +{ + if (!rexp) + return 1; + switch (rexp->type) { + case r_data: + return 1; + case r_cset: + { + rx_bitset_union(rxb->rx.local_cset_size, + rxb->fastset, rexp->params.cset); + } + return 0; + case r_concat: + return (compute_fastset(rxb, rexp->params.pair.left) + && compute_fastset(rxb, rexp->params.pair.right)); + case r_2phase_star: + compute_fastset(rxb, rexp->params.pair.left); + /* compute_fastset (rxb, rexp->params.pair.right); nope... */ + return 1; + case r_alternate: + return !!(compute_fastset(rxb, rexp->params.pair.left) + + compute_fastset(rxb, rexp->params.pair.right)); + case r_opt: + case r_star: + compute_fastset(rxb, rexp->params.pair.left); + return 1; + case r_side_effect: + return 1; + } - /* this should never happen */ - return 0; + /* this should never happen */ + return 0; } @@ -4608,54 +4391,52 @@ compute_fastset (rxb, rexp) */ #ifdef __STDC__ -static int -is_anchored (struct rexp_node * rexp, rx_side_effect se) +static int is_anchored(struct rexp_node *rexp, rx_side_effect se) #else -static int -is_anchored (rexp, se) - struct rexp_node * rexp; - rx_side_effect se; -#endif -{ - if (!rexp) - return 2; - switch (rexp->type) - { - case r_cset: - case r_data: - return 0; - case r_concat: - case r_2phase_star: - { - int l = is_anchored (rexp->params.pair.left, se); - return (l == 2 ? is_anchored (rexp->params.pair.right, se) : l); - } - case r_alternate: - { - int l = is_anchored (rexp->params.pair.left, se); - int r = l ? is_anchored (rexp->params.pair.right, se) : 0; - - if (l == r) - return l; - else if ((l == 0) || (r == 0)) - return 0; - else - return 2; - } - case r_opt: - case r_star: - return is_anchored (rexp->params.pair.left, se) ? 2 : 0; - - case r_side_effect: - return ((rexp->params.side_effect == se) - ? 1 : 2); - } +static int is_anchored(rexp, se) +struct rexp_node *rexp; +rx_side_effect se; +#endif +{ + if (!rexp) + return 2; + switch (rexp->type) { + case r_cset: + case r_data: + return 0; + case r_concat: + case r_2phase_star: + { + int l = is_anchored(rexp->params.pair.left, se); - /* this should never happen */ - return 0; -} + return (l == 2 ? is_anchored(rexp->params.pair.right, se) : l); + } + case r_alternate: + { + int l = is_anchored(rexp->params.pair.left, se); + int r = l ? is_anchored(rexp->params.pair.right, se) : 0; + if (l == r) + return l; + else if ((l == 0) || (r == 0)) + return 0; + else + return 2; + } + case r_opt: + case r_star: + return is_anchored(rexp->params.pair.left, se) ? 2 : 0; + + case r_side_effect: + return ((rexp->params.side_effect == se) + ? 1 : 2); + } + + /* this should never happen */ + return 0; +} + /* This removes register assignments that aren't required by backreferencing. * This can speed up explore_future, especially if it eliminates * non-determinism in the superstate NFA. @@ -4666,374 +4447,367 @@ is_anchored (rexp, se) */ #ifdef __STDC__ -static struct rexp_node * -remove_unecessary_side_effects (struct rx * rx, char * needed, - struct rexp_node * rexp, - struct re_se_params * params) -#else -static struct rexp_node * -remove_unecessary_side_effects (rx, needed, rexp, params) - struct rx * rx; - char * needed; - struct rexp_node * rexp; - struct re_se_params * params; -#endif -{ - struct rexp_node * l; - struct rexp_node * r; - if (!rexp) - return 0; - else - switch (rexp->type) - { - case r_cset: - case r_data: - return rexp; - case r_alternate: - case r_concat: - case r_2phase_star: - l = remove_unecessary_side_effects (rx, needed, - rexp->params.pair.left, params); - r = remove_unecessary_side_effects (rx, needed, - rexp->params.pair.right, params); - if ((l && r) || (rexp->type != r_concat)) - { - rexp->params.pair.left = l; - rexp->params.pair.right = r; - return rexp; - } - else - { - rexp->params.pair.left = rexp->params.pair.right = 0; - rx_free_rexp (rx, rexp); - return l ? l : r; - } - case r_opt: - case r_star: - l = remove_unecessary_side_effects (rx, needed, - rexp->params.pair.left, params); - if (l) - { - rexp->params.pair.left = l; - return rexp; - } +static struct rexp_node *remove_unecessary_side_effects(struct rx *rx, + char *needed, + struct rexp_node + *rexp, + struct re_se_params + *params) +#else +static struct rexp_node *remove_unecessary_side_effects(rx, needed, rexp, + params) +struct rx *rx; +char *needed; +struct rexp_node *rexp; +struct re_se_params *params; +#endif +{ + struct rexp_node *l; + struct rexp_node *r; + + if (!rexp) + return 0; else - { - rexp->params.pair.left = 0; - rx_free_rexp (rx, rexp); - return 0; - } - case r_side_effect: - { - int se = (long)rexp->params.side_effect; - if ( (se >= 0) - && ( ((enum re_side_effects)params[se].se == re_se_lparen) - || ((enum re_side_effects)params[se].se == re_se_rparen)) - && (params [se].op1 > 0) - && (!needed [params [se].op1])) - { - rx_free_rexp (rx, rexp); - return 0; - } - else - return rexp; - } - } + switch (rexp->type) { + case r_cset: + case r_data: + return rexp; + case r_alternate: + case r_concat: + case r_2phase_star: + l = remove_unecessary_side_effects(rx, needed, + rexp->params.pair.left, + params); + r = + remove_unecessary_side_effects(rx, needed, + rexp->params.pair.right, + params); + if ((l && r) || (rexp->type != r_concat)) { + rexp->params.pair.left = l; + rexp->params.pair.right = r; + return rexp; + } else { + rexp->params.pair.left = rexp->params.pair.right = 0; + rx_free_rexp(rx, rexp); + return l ? l : r; + } + case r_opt: + case r_star: + l = remove_unecessary_side_effects(rx, needed, + rexp->params.pair.left, + params); + if (l) { + rexp->params.pair.left = l; + return rexp; + } else { + rexp->params.pair.left = 0; + rx_free_rexp(rx, rexp); + return 0; + } + case r_side_effect: + { + int se = (long) rexp->params.side_effect; + + if ((se >= 0) + && (((enum re_side_effects) params[se].se == re_se_lparen) + || ((enum re_side_effects) params[se].se == + re_se_rparen)) && (params[se].op1 > 0) + && (!needed[params[se].op1])) { + rx_free_rexp(rx, rexp); + return 0; + } else + return rexp; + } + } - /* this should never happen */ - return 0; + /* this should never happen */ + return 0; } - + #ifdef __STDC__ static int -pointless_if_repeated (struct rexp_node * node, struct re_se_params * params) +pointless_if_repeated(struct rexp_node *node, struct re_se_params *params) #else -static int -pointless_if_repeated (node, params) - struct rexp_node * node; - struct re_se_params * params; -#endif -{ - if (!node) - return 1; - switch (node->type) - { - case r_cset: - return 0; - case r_alternate: - case r_concat: - case r_2phase_star: - return (pointless_if_repeated (node->params.pair.left, params) - && pointless_if_repeated (node->params.pair.right, params)); - case r_opt: - case r_star: - return pointless_if_repeated (node->params.pair.left, params); - case r_side_effect: - switch (((long)node->params.side_effect < 0) - ? (enum re_side_effects)node->params.side_effect - : (enum re_side_effects)params[(long)node->params.side_effect].se) - { - case re_se_try: - case re_se_at_dot: - case re_se_begbuf: - case re_se_hat: - case re_se_wordbeg: - case re_se_wordbound: - case re_se_notwordbound: - case re_se_wordend: - case re_se_endbuf: - case re_se_dollar: - case re_se_fail: - case re_se_win: - return 1; - case re_se_lparen: - case re_se_rparen: - case re_se_iter: - case re_se_end_iter: - case re_se_syntax: - case re_se_not_syntax: - case re_se_backref: - return 0; +static int pointless_if_repeated(node, params) +struct rexp_node *node; +struct re_se_params *params; +#endif +{ + if (!node) + return 1; + switch (node->type) { + case r_cset: + return 0; + case r_alternate: + case r_concat: + case r_2phase_star: + return (pointless_if_repeated(node->params.pair.left, params) + && pointless_if_repeated(node->params.pair.right, params)); + case r_opt: + case r_star: + return pointless_if_repeated(node->params.pair.left, params); + case r_side_effect: + switch (((long) node->params.side_effect < 0) + ? (enum re_side_effects) node->params.side_effect + : (enum re_side_effects) params[(long) node-> + params.side_effect].se) { + case re_se_try: + case re_se_at_dot: + case re_se_begbuf: + case re_se_hat: + case re_se_wordbeg: + case re_se_wordbound: + case re_se_notwordbound: + case re_se_wordend: + case re_se_endbuf: + case re_se_dollar: + case re_se_fail: + case re_se_win: + return 1; + case re_se_lparen: + case re_se_rparen: + case re_se_iter: + case re_se_end_iter: + case re_se_syntax: + case re_se_not_syntax: + case re_se_backref: + return 0; + } + case r_data: + default: + return 0; } - case r_data: - default: - return 0; - } } - + #ifdef __STDC__ static int -registers_on_stack (struct re_pattern_buffer * rxb, - struct rexp_node * rexp, int in_danger, - struct re_se_params * params) +registers_on_stack(struct re_pattern_buffer *rxb, + struct rexp_node *rexp, int in_danger, + struct re_se_params *params) #else -static int -registers_on_stack (rxb, rexp, in_danger, params) - struct re_pattern_buffer * rxb; - struct rexp_node * rexp; - int in_danger; - struct re_se_params * params; -#endif -{ - if (!rexp) - return 0; - else - switch (rexp->type) - { - case r_cset: - case r_data: - return 0; - case r_alternate: - case r_concat: - return ( registers_on_stack (rxb, rexp->params.pair.left, - in_danger, params) - || (registers_on_stack - (rxb, rexp->params.pair.right, - in_danger, params))); - case r_opt: - return registers_on_stack (rxb, rexp->params.pair.left, 0, params); - case r_star: - return registers_on_stack (rxb, rexp->params.pair.left, 1, params); - case r_2phase_star: - return - ( registers_on_stack (rxb, rexp->params.pair.left, 1, params) - || registers_on_stack (rxb, rexp->params.pair.right, 1, params)); - case r_side_effect: - { - int se = (long)rexp->params.side_effect; - if ( in_danger - && (se >= 0) - && (params [se].op1 > 0) - && ( ((enum re_side_effects)params[se].se == re_se_lparen) - || ((enum re_side_effects)params[se].se == re_se_rparen))) - return 1; - else - return 0; - } - } +static int registers_on_stack(rxb, rexp, in_danger, params) +struct re_pattern_buffer *rxb; +struct rexp_node *rexp; +int in_danger; +struct re_se_params *params; +#endif +{ + if (!rexp) + return 0; + else + switch (rexp->type) { + case r_cset: + case r_data: + return 0; + case r_alternate: + case r_concat: + return (registers_on_stack(rxb, rexp->params.pair.left, + in_danger, params) + || (registers_on_stack + (rxb, rexp->params.pair.right, + in_danger, params))); + case r_opt: + return registers_on_stack(rxb, rexp->params.pair.left, 0, + params); + case r_star: + return registers_on_stack(rxb, rexp->params.pair.left, 1, + params); + case r_2phase_star: + return + (registers_on_stack(rxb, rexp->params.pair.left, 1, params) + || registers_on_stack(rxb, rexp->params.pair.right, 1, + params)); + case r_side_effect: + { + int se = (long) rexp->params.side_effect; + + if (in_danger && (se >= 0) + && (params[se].op1 > 0) + && (((enum re_side_effects) params[se].se == re_se_lparen) + || ((enum re_side_effects) params[se].se == + re_se_rparen))) return 1; + else + return 0; + } + } - /* this should never happen */ - return 0; + /* this should never happen */ + return 0; } - -static char idempotent_complex_se[] = -{ + +static char idempotent_complex_se[] = { #define RX_WANT_SE_DEFS 1 #undef RX_DEF_SE #undef RX_DEF_CPLX_SE -#define RX_DEF_SE(IDEM, NAME, VALUE) +#define RX_DEF_SE(IDEM, NAME, VALUE) #define RX_DEF_CPLX_SE(IDEM, NAME, VALUE) IDEM, #include #undef RX_DEF_SE #undef RX_DEF_CPLX_SE #undef RX_WANT_SE_DEFS - 23 + 23 }; -static char idempotent_se[] = -{ - 13, +static char idempotent_se[] = { + 13, #define RX_WANT_SE_DEFS 1 #undef RX_DEF_SE #undef RX_DEF_CPLX_SE #define RX_DEF_SE(IDEM, NAME, VALUE) IDEM, -#define RX_DEF_CPLX_SE(IDEM, NAME, VALUE) +#define RX_DEF_CPLX_SE(IDEM, NAME, VALUE) #include #undef RX_DEF_SE #undef RX_DEF_CPLX_SE #undef RX_WANT_SE_DEFS - 42 + 42 }; - + #ifdef __STDC__ -static int -has_any_se (struct rx * rx, - struct rexp_node * rexp) +static int has_any_se(struct rx *rx, struct rexp_node *rexp) #else -static int -has_any_se (rx, rexp) - struct rx * rx; - struct rexp_node * rexp; -#endif -{ - if (!rexp) - return 0; - - switch (rexp->type) - { - case r_cset: - case r_data: - return 0; - - case r_side_effect: - return 1; - - case r_2phase_star: - case r_concat: - case r_alternate: - return - ( has_any_se (rx, rexp->params.pair.left) - || has_any_se (rx, rexp->params.pair.right)); - - case r_opt: - case r_star: - return has_any_se (rx, rexp->params.pair.left); - } +static int has_any_se(rx, rexp) +struct rx *rx; +struct rexp_node *rexp; +#endif +{ + if (!rexp) + return 0; - /* this should never happen */ - return 0; -} + switch (rexp->type) { + case r_cset: + case r_data: + return 0; + + case r_side_effect: + return 1; + + case r_2phase_star: + case r_concat: + case r_alternate: + return (has_any_se(rx, rexp->params.pair.left) + || has_any_se(rx, rexp->params.pair.right)); + + case r_opt: + case r_star: + return has_any_se(rx, rexp->params.pair.left); + } + /* this should never happen */ + return 0; +} + /* This must be called AFTER `convert_hard_loops' for a given REXP. */ #ifdef __STDC__ static int -has_non_idempotent_epsilon_path (struct rx * rx, - struct rexp_node * rexp, - struct re_se_params * params) +has_non_idempotent_epsilon_path(struct rx *rx, + struct rexp_node *rexp, + struct re_se_params *params) #else -static int -has_non_idempotent_epsilon_path (rx, rexp, params) - struct rx * rx; - struct rexp_node * rexp; - struct re_se_params * params; -#endif -{ - if (!rexp) - return 0; - - switch (rexp->type) - { - case r_cset: - case r_data: - case r_star: - return 0; - - case r_side_effect: - return - !((long)rexp->params.side_effect > 0 - ? idempotent_complex_se [ params [(long)rexp->params.side_effect].se ] - : idempotent_se [-(long)rexp->params.side_effect]); - - case r_alternate: - return - ( has_non_idempotent_epsilon_path (rx, - rexp->params.pair.left, params) - || has_non_idempotent_epsilon_path (rx, - rexp->params.pair.right, params)); - - case r_2phase_star: - case r_concat: - return - ( has_non_idempotent_epsilon_path (rx, - rexp->params.pair.left, params) - && has_non_idempotent_epsilon_path (rx, - rexp->params.pair.right, params)); - - case r_opt: - return has_non_idempotent_epsilon_path (rx, - rexp->params.pair.left, params); - } +static int has_non_idempotent_epsilon_path(rx, rexp, params) +struct rx *rx; +struct rexp_node *rexp; +struct re_se_params *params; +#endif +{ + if (!rexp) + return 0; - /* this should never happen */ - return 0; -} + switch (rexp->type) { + case r_cset: + case r_data: + case r_star: + return 0; + + case r_side_effect: + return + !((long) rexp->params.side_effect > 0 + ? + idempotent_complex_se[params + [(long) rexp->params. + side_effect].se] : + idempotent_se[-(long) rexp->params.side_effect]); + + case r_alternate: + return + (has_non_idempotent_epsilon_path(rx, + rexp->params.pair.left, + params) + || has_non_idempotent_epsilon_path(rx, + rexp->params.pair.right, + params)); + case r_2phase_star: + case r_concat: + return + (has_non_idempotent_epsilon_path(rx, + rexp->params.pair.left, + params) + && has_non_idempotent_epsilon_path(rx, + rexp->params.pair.right, + params)); + + case r_opt: + return has_non_idempotent_epsilon_path(rx, + rexp->params.pair.left, + params); + } + + /* this should never happen */ + return 0; +} + /* This computes rougly what it's name suggests. It can (and does) go wrong * in the direction of returning spurious 0 without causing disasters. */ #ifdef __STDC__ -static int -begins_with_complex_se (struct rx * rx, struct rexp_node * rexp) +static int begins_with_complex_se(struct rx *rx, struct rexp_node *rexp) #else -static int -begins_with_complex_se (rx, rexp) - struct rx * rx; - struct rexp_node * rexp; -#endif -{ - if (!rexp) - return 0; - - switch (rexp->type) - { - case r_cset: - case r_data: - return 0; - - case r_side_effect: - return ((long)rexp->params.side_effect >= 0); - - case r_alternate: - return - ( begins_with_complex_se (rx, rexp->params.pair.left) - && begins_with_complex_se (rx, rexp->params.pair.right)); - - - case r_concat: - return has_any_se (rx, rexp->params.pair.left); - case r_opt: - case r_star: - case r_2phase_star: - return 0; - } +static int begins_with_complex_se(rx, rexp) +struct rx *rx; +struct rexp_node *rexp; +#endif +{ + if (!rexp) + return 0; + + switch (rexp->type) { + case r_cset: + case r_data: + return 0; + + case r_side_effect: + return ((long) rexp->params.side_effect >= 0); + + case r_alternate: + return (begins_with_complex_se(rx, rexp->params.pair.left) + && begins_with_complex_se(rx, rexp->params.pair.right)); - /* this should never happen */ - return 0; -} + case r_concat: + return has_any_se(rx, rexp->params.pair.left); + case r_opt: + case r_star: + case r_2phase_star: + return 0; + } + + /* this should never happen */ + return 0; +} + /* This destructively removes some of the re_se_tv side effects from * a rexp tree. In particular, during parsing re_se_tv was inserted on the * right half of every | to guarantee that posix path preference could be @@ -5043,60 +4817,57 @@ begins_with_complex_se (rx, rexp) #ifdef __STDC__ static void -speed_up_alt (struct rx * rx, - struct rexp_node * rexp, - int unposix) +speed_up_alt(struct rx *rx, struct rexp_node *rexp, int unposix) #else -static void -speed_up_alt (rx, rexp, unposix) - struct rx * rx; - struct rexp_node * rexp; - int unposix; -#endif -{ - if (!rexp) - return; - - switch (rexp->type) - { - case r_cset: - case r_data: - case r_side_effect: - return; - - case r_opt: - case r_star: - speed_up_alt (rx, rexp->params.pair.left, unposix); - return; - - case r_2phase_star: - case r_concat: - speed_up_alt (rx, rexp->params.pair.left, unposix); - speed_up_alt (rx, rexp->params.pair.right, unposix); - return; - - case r_alternate: - /* the right child is guaranteed to be (concat re_se_tv ) */ - - speed_up_alt (rx, rexp->params.pair.left, unposix); - speed_up_alt (rx, rexp->params.pair.right->params.pair.right, unposix); - - if ( unposix - || (begins_with_complex_se - (rx, rexp->params.pair.right->params.pair.right)) - || !( has_any_se (rx, rexp->params.pair.right->params.pair.right) - || has_any_se (rx, rexp->params.pair.left))) - { - struct rexp_node * conc = rexp->params.pair.right; - rexp->params.pair.right = conc->params.pair.right; - conc->params.pair.right = 0; - rx_free_rexp (rx, conc); +static void speed_up_alt(rx, rexp, unposix) +struct rx *rx; +struct rexp_node *rexp; +int unposix; +#endif +{ + if (!rexp) + return; + + switch (rexp->type) { + case r_cset: + case r_data: + case r_side_effect: + return; + + case r_opt: + case r_star: + speed_up_alt(rx, rexp->params.pair.left, unposix); + return; + + case r_2phase_star: + case r_concat: + speed_up_alt(rx, rexp->params.pair.left, unposix); + speed_up_alt(rx, rexp->params.pair.right, unposix); + return; + + case r_alternate: + /* the right child is guaranteed to be (concat re_se_tv ) */ + + speed_up_alt(rx, rexp->params.pair.left, unposix); + speed_up_alt(rx, rexp->params.pair.right->params.pair.right, + unposix); + + if (unposix + || (begins_with_complex_se + (rx, rexp->params.pair.right->params.pair.right)) + || !(has_any_se(rx, rexp->params.pair.right->params.pair.right) + || has_any_se(rx, rexp->params.pair.left))) { + struct rexp_node *conc = rexp->params.pair.right; + + rexp->params.pair.right = conc->params.pair.right; + conc->params.pair.right = 0; + rx_free_rexp(rx, conc); + } } - } } + - /* `regex_compile' compiles PATTERN (of length SIZE) according to SYNTAX. @@ -5120,1749 +4891,1756 @@ speed_up_alt (rx, rexp, unposix) #ifdef __STDC__ RX_DECL reg_errcode_t -rx_compile (__const__ char *pattern, int size, - reg_syntax_t syntax, - struct re_pattern_buffer * rxb) +rx_compile(__const__ char *pattern, int size, + reg_syntax_t syntax, struct re_pattern_buffer *rxb) #else -RX_DECL reg_errcode_t -rx_compile (pattern, size, syntax, rxb) - __const__ char *pattern; - int size; - reg_syntax_t syntax; - struct re_pattern_buffer * rxb; -#endif -{ - RX_subset - inverse_translate [CHAR_SET_SIZE * rx_bitset_numb_subsets(CHAR_SET_SIZE)]; - char - validate_inv_tr [CHAR_SET_SIZE * rx_bitset_numb_subsets(CHAR_SET_SIZE)]; - - /* We fetch characters from PATTERN here. Even though PATTERN is - `char *' (i.e., signed), we declare these variables as unsigned, so - they can be reliably used as array indices. */ - register unsigned char c, c1; - - /* A random tempory spot in PATTERN. */ - __const__ char *p1; - - /* Keeps track of unclosed groups. */ - compile_stack_type compile_stack; - - /* Points to the current (ending) position in the pattern. */ - __const__ char *p = pattern; - __const__ char *pend = pattern + size; - - /* How to translate the characters in the pattern. */ - unsigned char *translate = (rxb->translate - ? rxb->translate - : rx_id_translation); - - /* When parsing is done, this will hold the expression tree. */ - struct rexp_node * rexp = 0; - - /* In the midst of compilation, this holds onto the regexp - * first parst while rexp goes on to aquire additional constructs. - */ - struct rexp_node * orig_rexp = 0; - struct rexp_node * fewer_side_effects = 0; - - /* This and top_expression are saved on the compile stack. */ - struct rexp_node ** top_expression = &rexp; - struct rexp_node ** last_expression = top_expression; - - /* Parameter to `goto append_node' */ - struct rexp_node * append; - - /* Counts open-groups as they are encountered. This is the index of the - * innermost group being compiled. - */ - regnum_t regnum = 0; - - /* Place in the uncompiled pattern (i.e., the {) to - * which to go back if the interval is invalid. - */ - __const__ char *beg_interval; - - struct re_se_params * params = 0; - int paramc = 0; /* How many complex side effects so far? */ - - rx_side_effect side; /* param to `goto add_side_effect' */ - - bzero (validate_inv_tr, sizeof (validate_inv_tr)); - - rxb->rx.instruction_table = rx_id_instruction_table; - - - /* Initialize the compile stack. */ - compile_stack.stack = (( compile_stack_elt_t *) malloc ((INIT_COMPILE_STACK_SIZE) * sizeof ( compile_stack_elt_t))); - if (compile_stack.stack == 0) - return REG_ESPACE; - - compile_stack.size = INIT_COMPILE_STACK_SIZE; - compile_stack.avail = 0; - - /* Initialize the pattern buffer. */ - rxb->rx.cache = &default_cache; - rxb->syntax = syntax; - rxb->fastmap_accurate = 0; - rxb->not_bol = rxb->not_eol = 0; - rxb->least_subs = 0; - - /* Always count groups, whether or not rxb->no_sub is set. - * The whole pattern is implicitly group 0, so counting begins - * with 1. - */ - rxb->re_nsub = 0; +RX_DECL reg_errcode_t rx_compile(pattern, size, syntax, rxb) +__const__ char *pattern; +int size; +reg_syntax_t syntax; +struct re_pattern_buffer *rxb; +#endif +{ + RX_subset + inverse_translate[CHAR_SET_SIZE * + rx_bitset_numb_subsets(CHAR_SET_SIZE)]; + char validate_inv_tr[CHAR_SET_SIZE * + + rx_bitset_numb_subsets(CHAR_SET_SIZE)]; + + /* We fetch characters from PATTERN here. Even though PATTERN is + `char *' (i.e., signed), we declare these variables as unsigned, so + they can be reliably used as array indices. */ + register unsigned char c, c1; + + /* A random tempory spot in PATTERN. */ + __const__ char *p1; + + /* Keeps track of unclosed groups. */ + compile_stack_type compile_stack; + + /* Points to the current (ending) position in the pattern. */ + __const__ char *p = pattern; + __const__ char *pend = pattern + size; + + /* How to translate the characters in the pattern. */ + unsigned char *translate = (rxb->translate + + ? rxb->translate : rx_id_translation); + + /* When parsing is done, this will hold the expression tree. */ + struct rexp_node *rexp = 0; + + /* In the midst of compilation, this holds onto the regexp + * first parst while rexp goes on to aquire additional constructs. + */ + struct rexp_node *orig_rexp = 0; + struct rexp_node *fewer_side_effects = 0; + + /* This and top_expression are saved on the compile stack. */ + struct rexp_node **top_expression = &rexp; + struct rexp_node **last_expression = top_expression; + + /* Parameter to `goto append_node' */ + struct rexp_node *append; + + /* Counts open-groups as they are encountered. This is the index of the + * innermost group being compiled. + */ + regnum_t regnum = 0; + + /* Place in the uncompiled pattern (i.e., the {) to + * which to go back if the interval is invalid. + */ + __const__ char *beg_interval; + + struct re_se_params *params = 0; + int paramc = 0; /* How many complex side effects so far? */ + + rx_side_effect side; /* param to `goto add_side_effect' */ + + bzero(validate_inv_tr, sizeof(validate_inv_tr)); + + rxb->rx.instruction_table = rx_id_instruction_table; + + + /* Initialize the compile stack. */ + compile_stack.stack = ((compile_stack_elt_t *) + malloc((INIT_COMPILE_STACK_SIZE) * + sizeof(compile_stack_elt_t))); + if (compile_stack.stack == 0) + return REG_ESPACE; + + compile_stack.size = INIT_COMPILE_STACK_SIZE; + compile_stack.avail = 0; + + /* Initialize the pattern buffer. */ + rxb->rx.cache = &default_cache; + rxb->syntax = syntax; + rxb->fastmap_accurate = 0; + rxb->not_bol = rxb->not_eol = 0; + rxb->least_subs = 0; + + /* Always count groups, whether or not rxb->no_sub is set. + * The whole pattern is implicitly group 0, so counting begins + * with 1. + */ + rxb->re_nsub = 0; #if !defined (emacs) && !defined (SYNTAX_TABLE) - /* Initialize the syntax table. */ - init_syntax_once (); -#endif - - /* Loop through the uncompiled pattern until we're at the end. */ - while (p != pend) - { - PATFETCH (c); - - switch (c) - { - case '^': - { - if ( /* If at start of pattern, it's an operator. */ - p == pattern + 1 - /* If context independent, it's an operator. */ - || syntax & RE_CONTEXT_INDEP_ANCHORS - /* Otherwise, depends on what's come before. */ - || at_begline_loc_p (pattern, p, syntax)) - { - struct rexp_node * n - = rx_mk_r_side_effect (&rxb->rx, (rx_side_effect)re_se_hat); - if (!n) - return REG_ESPACE; - append = n; - goto append_node; - } - else - goto normal_char; - } - break; - - - case '$': - { - if ( /* If at end of pattern, it's an operator. */ - p == pend - /* If context independent, it's an operator. */ - || syntax & RE_CONTEXT_INDEP_ANCHORS - /* Otherwise, depends on what's next. */ - || at_endline_loc_p (p, pend, syntax)) - { - struct rexp_node * n - = rx_mk_r_side_effect (&rxb->rx, (rx_side_effect)re_se_dollar); - if (!n) - return REG_ESPACE; - append = n; - goto append_node; - } - else - goto normal_char; - } - break; - - - case '+': - case '?': - if ((syntax & RE_BK_PLUS_QM) - || (syntax & RE_LIMITED_OPS)) - goto normal_char; - - handle_plus: - case '*': - /* If there is no previous pattern... */ - if (pointless_if_repeated (*last_expression, params)) - { - if (syntax & RE_CONTEXT_INVALID_OPS) - return REG_BADRPT; - else if (!(syntax & RE_CONTEXT_INDEP_OPS)) - goto normal_char; - } - - { - /* 1 means zero (many) matches is allowed. */ - char zero_times_ok = 0, many_times_ok = 0; - - /* If there is a sequence of repetition chars, collapse it - down to just one (the right one). We can't combine - interval operators with these because of, e.g., `a{2}*', - which should only match an even number of `a's. */ - - for (;;) - { - zero_times_ok |= c != '+'; - many_times_ok |= c != '?'; - - if (p == pend) - break; - - PATFETCH (c); - - if (c == '*' - || (!(syntax & RE_BK_PLUS_QM) && (c == '+' || c == '?'))) - ; - - else if (syntax & RE_BK_PLUS_QM && c == '\\') - { - if (p == pend) return REG_EESCAPE; - - PATFETCH (c1); - if (!(c1 == '+' || c1 == '?')) - { - PATUNFETCH; - PATUNFETCH; - break; - } - - c = c1; - } - else - { - PATUNFETCH; - break; - } - - /* If we get here, we found another repeat character. */ - } - - /* Star, etc. applied to an empty pattern is equivalent - to an empty pattern. */ - if (!last_expression) - break; - - /* Now we know whether or not zero matches is allowed - * and also whether or not two or more matches is allowed. - */ - - { - struct rexp_node * inner_exp = *last_expression; - int need_sync = 0; - - if (many_times_ok - && has_non_idempotent_epsilon_path (&rxb->rx, - inner_exp, params)) - { - struct rexp_node * pusher - = rx_mk_r_side_effect (&rxb->rx, - (rx_side_effect)re_se_pushpos); - struct rexp_node * checker - = rx_mk_r_side_effect (&rxb->rx, - (rx_side_effect)re_se_chkpos); - struct rexp_node * pushback - = rx_mk_r_side_effect (&rxb->rx, - (rx_side_effect)re_se_pushback); - rx_Bitset cs = rx_cset (&rxb->rx); - struct rexp_node * lit_t; - struct rexp_node * fake_state; - struct rexp_node * phase2; - struct rexp_node * popper; - struct rexp_node * star; - struct rexp_node * a; - struct rexp_node * whole_thing; - - if (! cs) - return REG_ESPACE; - lit_t = rx_mk_r_cset (&rxb->rx, cs); - fake_state = rx_mk_r_concat (&rxb->rx, pushback, lit_t); - phase2 = rx_mk_r_concat (&rxb->rx, checker, fake_state); - popper = rx_mk_r_side_effect (&rxb->rx, - (rx_side_effect)re_se_poppos); - star = rx_mk_r_2phase_star (&rxb->rx, inner_exp, phase2); - a = rx_mk_r_concat (&rxb->rx, pusher, star); - whole_thing = rx_mk_r_concat (&rxb->rx, a, popper); - - if (!(pusher && star && pushback && lit_t && fake_state - && lit_t && phase2 && checker && popper - && a && whole_thing)) - return REG_ESPACE; - RX_bitset_enjoin (cs, 't'); - *last_expression = whole_thing; - } - else - { - struct rexp_node * star = - (many_times_ok ? rx_mk_r_star : rx_mk_r_opt) - (&rxb->rx, *last_expression); - if (!star) - return REG_ESPACE; - *last_expression = star; - need_sync = has_any_se (&rxb->rx, *last_expression); - } - if (!zero_times_ok) + /* Initialize the syntax table. */ + init_syntax_once(); +#endif + + /* Loop through the uncompiled pattern until we're at the end. */ + while (p != pend) { + PATFETCH(c); + + switch (c) { + case '^': { - struct rexp_node * concat - = rx_mk_r_concat (&rxb->rx, inner_exp, - rx_copy_rexp (&rxb->rx, - *last_expression)); - if (!concat) - return REG_ESPACE; - *last_expression = concat; + if ( /* If at start of pattern, it's an operator. */ + p == pattern + 1 + /* If context independent, it's an operator. */ + || syntax & RE_CONTEXT_INDEP_ANCHORS + /* Otherwise, depends on what's come before. */ + || at_begline_loc_p(pattern, p, syntax)) { + struct rexp_node *n = rx_mk_r_side_effect(&rxb->rx, + (rx_side_effect) re_se_hat); + + if (!n) + return REG_ESPACE; + append = n; + goto append_node; + } else + goto normal_char; } - if (need_sync) + break; + + + case '$': { - int sync_se = paramc; - params = (params - ? ((struct re_se_params *) - realloc (params, - sizeof (*params) * (1 + paramc))) - : ((struct re_se_params *) - malloc (sizeof (*params)))); - if (!params) - return REG_ESPACE; - ++paramc; - params [sync_se].se = re_se_tv; - side = (rx_side_effect)sync_se; - goto add_side_effect; + if ( /* If at end of pattern, it's an operator. */ + p == pend + /* If context independent, it's an operator. */ + || syntax & RE_CONTEXT_INDEP_ANCHORS + /* Otherwise, depends on what's next. */ + || at_endline_loc_p(p, pend, syntax)) { + struct rexp_node *n = rx_mk_r_side_effect(&rxb->rx, + (rx_side_effect) re_se_dollar); + + if (!n) + return REG_ESPACE; + append = n; + goto append_node; + } else + goto normal_char; } - } - /* The old regex.c used to optimize `.*\n'. - * Maybe rx should too? - */ - } - break; - - - case '.': - { - rx_Bitset cs = rx_cset (&rxb->rx); - struct rexp_node * n = rx_mk_r_cset (&rxb->rx, cs); - if (!(cs && n)) - return REG_ESPACE; - - rx_bitset_universe (rxb->rx.local_cset_size, cs); - if (!(rxb->syntax & RE_DOT_NEWLINE)) - RX_bitset_remove (cs, '\n'); - if (!(rxb->syntax & RE_DOT_NOT_NULL)) - RX_bitset_remove (cs, 0); - - append = n; - goto append_node; - break; - } - - - case '[': - if (p == pend) return REG_EBRACK; - { - boolean had_char_class = false; - rx_Bitset cs = rx_cset (&rxb->rx); - struct rexp_node * node = rx_mk_r_cset (&rxb->rx, cs); - int is_inverted = *p == '^'; - - if (!(node && cs)) - return REG_ESPACE; - - /* This branch of the switch is normally exited with - *`goto append_node' - */ - append = node; - - if (is_inverted) - p++; - - /* Remember the first position in the bracket expression. */ - p1 = p; - - /* Read in characters and ranges, setting map bits. */ - for (;;) - { - if (p == pend) return REG_EBRACK; - - PATFETCH (c); - - /* \ might escape characters inside [...] and [^...]. */ - if ((syntax & RE_BACKSLASH_ESCAPE_IN_LISTS) && c == '\\') - { - if (p == pend) return REG_EESCAPE; - - PATFETCH (c1); - { - rx_Bitset it = inverse_translation (rxb, - validate_inv_tr, - inverse_translate, - translate, - c1); - rx_bitset_union (rxb->rx.local_cset_size, cs, it); - } - continue; - } - - /* Could be the end of the bracket expression. If it's - not (i.e., when the bracket expression is `[]' so - far), the ']' character bit gets set way below. */ - if (c == ']' && p != p1 + 1) - goto finalize_class_and_append; - - /* Look ahead to see if it's a range when the last thing - was a character class. */ - if (had_char_class && c == '-' && *p != ']') - return REG_ERANGE; - - /* Look ahead to see if it's a range when the last thing - was a character: if this is a hyphen not at the - beginning or the end of a list, then it's the range - operator. */ - if (c == '-' - && !(p - 2 >= pattern && p[-2] == '[') - && !(p - 3 >= pattern && p[-3] == '[' && p[-2] == '^') - && *p != ']') - { - reg_errcode_t ret - = compile_range (rxb, cs, &p, pend, translate, syntax, - inverse_translate, validate_inv_tr); - if (ret != REG_NOERROR) return ret; - } - - else if (p[0] == '-' && p[1] != ']') - { /* This handles ranges made up of characters only. */ - reg_errcode_t ret; - - /* Move past the `-'. */ - PATFETCH (c1); - - ret = compile_range (rxb, cs, &p, pend, translate, syntax, - inverse_translate, validate_inv_tr); - if (ret != REG_NOERROR) return ret; - } - - /* See if we're at the beginning of a possible character - class. */ - - else if ((syntax & RE_CHAR_CLASSES) - && (c == '[') && (*p == ':')) - { - char str[CHAR_CLASS_MAX_LENGTH + 1]; - - PATFETCH (c); - c1 = 0; - - /* If pattern is `[[:'. */ - if (p == pend) return REG_EBRACK; - - for (;;) - { - PATFETCH (c); - if (c == ':' || c == ']' || p == pend - || c1 == CHAR_CLASS_MAX_LENGTH) - break; - str[c1++] = c; - } - str[c1] = '\0'; - - /* If isn't a word bracketed by `[:' and:`]': - undo the ending character, the letters, and leave - the leading `:' and `[' (but set bits for them). */ - if (c == ':' && *p == ']') - { - int ch; - boolean is_alnum = !strcmp (str, "alnum"); - boolean is_alpha = !strcmp (str, "alpha"); - boolean is_blank = !strcmp (str, "blank"); - boolean is_cntrl = !strcmp (str, "cntrl"); - boolean is_digit = !strcmp (str, "digit"); - boolean is_graph = !strcmp (str, "graph"); - boolean is_lower = !strcmp (str, "lower"); - boolean is_print = !strcmp (str, "print"); - boolean is_punct = !strcmp (str, "punct"); - boolean is_space = !strcmp (str, "space"); - boolean is_upper = !strcmp (str, "upper"); - boolean is_xdigit = !strcmp (str, "xdigit"); - - if (!IS_CHAR_CLASS (str)) return REG_ECTYPE; - - /* Throw away the ] at the end of the character - class. */ - PATFETCH (c); - - if (p == pend) return REG_EBRACK; - - for (ch = 0; ch < 1 << CHARBITS; ch++) - { - if ( (is_alnum && isalnum (ch)) - || (is_alpha && isalpha (ch)) - || (is_blank && isblank (ch)) - || (is_cntrl && iscntrl (ch)) - || (is_digit && isdigit (ch)) - || (is_graph && isgraph (ch)) - || (is_lower && islower (ch)) - || (is_print && isprint (ch)) - || (is_punct && ispunct (ch)) - || (is_space && isspace (ch)) - || (is_upper && isupper (ch)) - || (is_xdigit && isxdigit (ch))) - { - rx_Bitset it = - inverse_translation (rxb, - validate_inv_tr, - inverse_translate, - translate, - ch); - rx_bitset_union (rxb->rx.local_cset_size, - cs, it); - } - } - had_char_class = true; - } - else - { - c1++; - while (c1--) - PATUNFETCH; - { - rx_Bitset it = - inverse_translation (rxb, - validate_inv_tr, - inverse_translate, - translate, - '['); - rx_bitset_union (rxb->rx.local_cset_size, - cs, it); + break; + + + case '+': + case '?': + if ((syntax & RE_BK_PLUS_QM) + || (syntax & RE_LIMITED_OPS)) + goto normal_char; + + handle_plus: + case '*': + /* If there is no previous pattern... */ + if (pointless_if_repeated(*last_expression, params)) { + if (syntax & RE_CONTEXT_INVALID_OPS) + return REG_BADRPT; + else if (!(syntax & RE_CONTEXT_INDEP_OPS)) + goto normal_char; } + { - rx_Bitset it = - inverse_translation (rxb, - validate_inv_tr, - inverse_translate, - translate, - ':'); - rx_bitset_union (rxb->rx.local_cset_size, - cs, it); + /* 1 means zero (many) matches is allowed. */ + char zero_times_ok = 0, many_times_ok = 0; + + /* If there is a sequence of repetition chars, collapse it + down to just one (the right one). We can't combine + interval operators with these because of, e.g., `a{2}*', + which should only match an even number of `a's. */ + + for (;;) { + zero_times_ok |= c != '+'; + many_times_ok |= c != '?'; + + if (p == pend) + break; + + PATFETCH(c); + + if (c == '*' || (!(syntax & RE_BK_PLUS_QM) + && (c == '+' || c == '?'))); + + else if (syntax & RE_BK_PLUS_QM && c == '\\') { + if (p == pend) + return REG_EESCAPE; + + PATFETCH(c1); + if (!(c1 == '+' || c1 == '?')) { + PATUNFETCH; + PATUNFETCH; + break; + } + + c = c1; + } else { + PATUNFETCH; + break; + } + + /* If we get here, we found another repeat character. */ + } + + /* Star, etc. applied to an empty pattern is equivalent + to an empty pattern. */ + if (!last_expression) + break; + + /* Now we know whether or not zero matches is allowed + * and also whether or not two or more matches is allowed. + */ + + { + struct rexp_node *inner_exp = *last_expression; + int need_sync = 0; + + if (many_times_ok + && has_non_idempotent_epsilon_path(&rxb->rx, + inner_exp, + params)) { + struct rexp_node *pusher = + rx_mk_r_side_effect(&rxb->rx, + (rx_side_effect) re_se_pushpos); + struct rexp_node *checker + = rx_mk_r_side_effect(&rxb->rx, + (rx_side_effect) re_se_chkpos); + struct rexp_node *pushback + = rx_mk_r_side_effect(&rxb->rx, + (rx_side_effect) re_se_pushback); + rx_Bitset cs = rx_cset(&rxb->rx); + struct rexp_node *lit_t; + struct rexp_node *fake_state; + struct rexp_node *phase2; + struct rexp_node *popper; + struct rexp_node *star; + struct rexp_node *a; + struct rexp_node *whole_thing; + + if (!cs) + return REG_ESPACE; + lit_t = rx_mk_r_cset(&rxb->rx, cs); + fake_state = + rx_mk_r_concat(&rxb->rx, pushback, lit_t); + phase2 = + rx_mk_r_concat(&rxb->rx, checker, fake_state); + popper = + rx_mk_r_side_effect(&rxb->rx, + (rx_side_effect) + re_se_poppos); + star = + rx_mk_r_2phase_star(&rxb->rx, inner_exp, + phase2); + a = rx_mk_r_concat(&rxb->rx, pusher, star); + whole_thing = rx_mk_r_concat(&rxb->rx, a, popper); + + if (! + (pusher && star && pushback && lit_t + && fake_state && lit_t && phase2 && checker + && popper && a && whole_thing)) + return REG_ESPACE; + RX_bitset_enjoin(cs, 't'); + *last_expression = whole_thing; + } else { + struct rexp_node *star = + (many_times_ok ? rx_mk_r_star : rx_mk_r_opt) + (&rxb->rx, *last_expression); + + if (!star) + return REG_ESPACE; + *last_expression = star; + need_sync = has_any_se(&rxb->rx, *last_expression); + } + if (!zero_times_ok) { + struct rexp_node *concat + = rx_mk_r_concat(&rxb->rx, inner_exp, + rx_copy_rexp(&rxb->rx, + *last_expression)); + + if (!concat) + return REG_ESPACE; + *last_expression = concat; + } + if (need_sync) { + int sync_se = paramc; + + params = (params ? ((struct re_se_params *) + realloc(params, + sizeof(*params) * (1 + + paramc))) + : ((struct re_se_params *) + malloc(sizeof(*params)))); + if (!params) + return REG_ESPACE; + ++paramc; + params[sync_se].se = re_se_tv; + side = (rx_side_effect) sync_se; + goto add_side_effect; + } + } + /* The old regex.c used to optimize `.*\n'. + * Maybe rx should too? + */ } - had_char_class = false; - } - } - else - { - had_char_class = false; - { - rx_Bitset it = inverse_translation (rxb, - validate_inv_tr, - inverse_translate, - translate, - c); - rx_bitset_union (rxb->rx.local_cset_size, cs, it); - } - } - } - - finalize_class_and_append: - if (is_inverted) - { - rx_bitset_complement (rxb->rx.local_cset_size, cs); - if (syntax & RE_HAT_LISTS_NOT_NEWLINE) - RX_bitset_remove (cs, '\n'); - } - goto append_node; - } - break; - - - case '(': - if (syntax & RE_NO_BK_PARENS) - goto handle_open; - else - goto normal_char; - - - case ')': - if (syntax & RE_NO_BK_PARENS) - goto handle_close; - else - goto normal_char; - - - case '\n': - if (syntax & RE_NEWLINE_ALT) - goto handle_alt; - else - goto normal_char; - - - case '|': - if (syntax & RE_NO_BK_VBAR) - goto handle_alt; - else - goto normal_char; - - - case '{': - if ((syntax & RE_INTERVALS) && (syntax & RE_NO_BK_BRACES)) - goto handle_interval; - else - goto normal_char; - - - case '\\': - if (p == pend) return REG_EESCAPE; - - /* Do not translate the character after the \, so that we can - distinguish, e.g., \B from \b, even if we normally would - translate, e.g., B to b. */ - PATFETCH_RAW (c); - - switch (c) - { - case '(': - if (syntax & RE_NO_BK_PARENS) - goto normal_backslash; - - handle_open: - rxb->re_nsub++; - regnum++; - if (COMPILE_STACK_FULL) - { - ((compile_stack.stack) = - (compile_stack_elt_t *) realloc (compile_stack.stack, ( compile_stack.size << 1) * sizeof ( - compile_stack_elt_t))); - if (compile_stack.stack == 0) return REG_ESPACE; - - compile_stack.size <<= 1; - } - - if (*last_expression) - { - struct rexp_node * concat - = rx_mk_r_concat (&rxb->rx, *last_expression, 0); - if (!concat) - return REG_ESPACE; - *last_expression = concat; - last_expression = &concat->params.pair.right; - } + break; - /* - * These are the values to restore when we hit end of this - * group. - */ - COMPILE_STACK_TOP.top_expression = top_expression; - COMPILE_STACK_TOP.last_expression = last_expression; - COMPILE_STACK_TOP.regnum = regnum; - - compile_stack.avail++; - - top_expression = last_expression; - break; - - - case ')': - if (syntax & RE_NO_BK_PARENS) goto normal_backslash; - - handle_close: - /* See similar code for backslashed left paren above. */ - if (COMPILE_STACK_EMPTY) { - if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD) { - goto normal_char; - } else { - return REG_ERPAREN; - } - } - - /* Since we just checked for an empty stack above, this - ``can't happen''. */ - - { - /* We don't just want to restore into `regnum', because - later groups should continue to be numbered higher, - as in `(ab)c(de)' -- the second group is #2. */ - regnum_t this_group_regnum; - struct rexp_node ** inner = top_expression; - - compile_stack.avail--; - top_expression = COMPILE_STACK_TOP.top_expression; - last_expression = COMPILE_STACK_TOP.last_expression; - this_group_regnum = COMPILE_STACK_TOP.regnum; - { - int left_se = paramc; - int right_se = paramc + 1; - - params = (params - ? ((struct re_se_params *) - realloc (params, - (paramc + 2) * sizeof (params[0]))) - : ((struct re_se_params *) - malloc (2 * sizeof (params[0])))); - if (!params) - return REG_ESPACE; - paramc += 2; - - params[left_se].se = re_se_lparen; - params[left_se].op1 = this_group_regnum; - params[right_se].se = re_se_rparen; - params[right_se].op1 = this_group_regnum; - { - struct rexp_node * left - = rx_mk_r_side_effect (&rxb->rx, - (rx_side_effect)left_se); - struct rexp_node * right - = rx_mk_r_side_effect (&rxb->rx, - (rx_side_effect)right_se); - struct rexp_node * c1 - = (*inner - ? rx_mk_r_concat (&rxb->rx, left, *inner) : left); - struct rexp_node * c2 - = rx_mk_r_concat (&rxb->rx, c1, right); - if (!(left && right && c1 && c2)) - return REG_ESPACE; - *inner = c2; - } - } - break; - } - - case '|': /* `\|'. */ - if ((syntax & RE_LIMITED_OPS) || (syntax & RE_NO_BK_VBAR)) - goto normal_backslash; - handle_alt: - if (syntax & RE_LIMITED_OPS) - goto normal_char; - - { - struct rexp_node * alt - = rx_mk_r_alternate (&rxb->rx, *top_expression, 0); - if (!alt) - return REG_ESPACE; - *top_expression = alt; - last_expression = &alt->params.pair.right; + + case '.': { - int sync_se = paramc; - - params = (params - ? ((struct re_se_params *) - realloc (params, - (paramc + 1) * sizeof (params[0]))) - : ((struct re_se_params *) - malloc (sizeof (params[0])))); - if (!params) - return REG_ESPACE; - ++paramc; - - params[sync_se].se = re_se_tv; - { - struct rexp_node * sync - = rx_mk_r_side_effect (&rxb->rx, - (rx_side_effect)sync_se); - struct rexp_node * conc - = rx_mk_r_concat (&rxb->rx, sync, 0); - - if (!sync || !conc) - return REG_ESPACE; - - *last_expression = conc; - last_expression = &conc->params.pair.right; - } - } - } - break; - - - case '{': - /* If \{ is a literal. */ - if (!(syntax & RE_INTERVALS) - /* If we're at `\{' and it's not the open-interval - operator. */ - || ((syntax & RE_INTERVALS) && (syntax & RE_NO_BK_BRACES)) - || (p - 2 == pattern && p == pend)) - goto normal_backslash; - - handle_interval: - { - /* If got here, then the syntax allows intervals. */ - - /* At least (most) this many matches must be made. */ - int lower_bound = -1, upper_bound = -1; - - beg_interval = p - 1; - - if (p == pend) - { - if (syntax & RE_NO_BK_BRACES) - goto unfetch_interval; - else - return REG_EBRACE; - } - - GET_UNSIGNED_NUMBER (lower_bound); - - if (c == ',') - { - GET_UNSIGNED_NUMBER (upper_bound); - if (upper_bound < 0) upper_bound = RE_DUP_MAX; - } - else - /* Interval such as `{1}' => match exactly once. */ - upper_bound = lower_bound; - - if (lower_bound < 0 || upper_bound > RE_DUP_MAX - || lower_bound > upper_bound) - { - if (syntax & RE_NO_BK_BRACES) - goto unfetch_interval; - else - return REG_BADBR; - } - - if (!(syntax & RE_NO_BK_BRACES)) - { - if (c != '\\') return REG_EBRACE; - PATFETCH (c); - } - - if (c != '}') - { - if (syntax & RE_NO_BK_BRACES) - goto unfetch_interval; - else - return REG_BADBR; - } - - /* We just parsed a valid interval. */ - - /* If it's invalid to have no preceding re. */ - if (pointless_if_repeated (*last_expression, params)) - { - if (syntax & RE_CONTEXT_INVALID_OPS) - return REG_BADRPT; - else if (!(syntax & RE_CONTEXT_INDEP_OPS)) - goto unfetch_interval; - /* was: else laststart = b; */ - } - - /* If the upper bound is zero, don't want to iterate - * at all. - */ - if (upper_bound == 0) - { - if (*last_expression) - { - rx_free_rexp (&rxb->rx, *last_expression); - *last_expression = 0; - } - } - else - /* Otherwise, we have a nontrivial interval. */ - { - int iter_se = paramc; - int end_se = paramc + 1; - params = (params - ? ((struct re_se_params *) - realloc (params, - sizeof (*params) * (2 + paramc))) - : ((struct re_se_params *) - malloc (2 * sizeof (*params)))); - if (!params) - return REG_ESPACE; - paramc += 2; - params [iter_se].se = re_se_iter; - params [iter_se].op1 = lower_bound; - params[iter_se].op2 = upper_bound; - - params[end_se].se = re_se_end_iter; - params[end_se].op1 = lower_bound; - params[end_se].op2 = upper_bound; - { - struct rexp_node * push0 - = rx_mk_r_side_effect (&rxb->rx, - (rx_side_effect)re_se_push0); - struct rexp_node * start_one_iter - = rx_mk_r_side_effect (&rxb->rx, - (rx_side_effect)iter_se); - struct rexp_node * phase1 - = rx_mk_r_concat (&rxb->rx, start_one_iter, - *last_expression); - struct rexp_node * pushback - = rx_mk_r_side_effect (&rxb->rx, - (rx_side_effect)re_se_pushback); - rx_Bitset cs = rx_cset (&rxb->rx); - struct rexp_node * lit_t; - struct rexp_node * phase2; - struct rexp_node * loop; - struct rexp_node * push_n_loop; - struct rexp_node * final_test; - struct rexp_node * full_exp; - - if (! cs) - return REG_ESPACE; - lit_t = rx_mk_r_cset (&rxb->rx, cs); - phase2 = rx_mk_r_concat (&rxb->rx, pushback, lit_t); - loop = rx_mk_r_2phase_star (&rxb->rx, phase1, phase2); - push_n_loop = rx_mk_r_concat (&rxb->rx, push0, loop); - final_test = rx_mk_r_side_effect (&rxb->rx, - (rx_side_effect)end_se); - full_exp = rx_mk_r_concat (&rxb->rx, push_n_loop, final_test); - - if (!(push0 && start_one_iter && phase1 - && pushback && lit_t && phase2 - && loop && push_n_loop && final_test && full_exp)) - return REG_ESPACE; + rx_Bitset cs = rx_cset(&rxb->rx); + struct rexp_node *n = rx_mk_r_cset(&rxb->rx, cs); - RX_bitset_enjoin(cs, 't'); + if (!(cs && n)) + return REG_ESPACE; - *last_expression = full_exp; - } - } - beg_interval = 0; - } - break; + rx_bitset_universe(rxb->rx.local_cset_size, cs); + if (!(rxb->syntax & RE_DOT_NEWLINE)) + RX_bitset_remove(cs, '\n'); + if (!(rxb->syntax & RE_DOT_NOT_NULL)) + RX_bitset_remove(cs, 0); - unfetch_interval: - /* If an invalid interval, match the characters as literals. */ - p = beg_interval; - beg_interval = 0; + append = n; + goto append_node; + break; + } - /* normal_char and normal_backslash need `c'. */ - PATFETCH (c); - if (!(syntax & RE_NO_BK_BRACES)) - { - if (p > pattern && p[-1] == '\\') - goto normal_backslash; - } - goto normal_char; + case '[': + if (p == pend) + return REG_EBRACK; + { + boolean had_char_class = false; + rx_Bitset cs = rx_cset(&rxb->rx); + struct rexp_node *node = rx_mk_r_cset(&rxb->rx, cs); + int is_inverted = *p == '^'; + + if (!(node && cs)) + return REG_ESPACE; + + /* This branch of the switch is normally exited with + *`goto append_node' + */ + append = node; + + if (is_inverted) + p++; + + /* Remember the first position in the bracket expression. */ + p1 = p; + + /* Read in characters and ranges, setting map bits. */ + for (;;) { + if (p == pend) + return REG_EBRACK; + + PATFETCH(c); + + /* \ might escape characters inside [...] and [^...]. */ + if ((syntax & RE_BACKSLASH_ESCAPE_IN_LISTS) + && c == '\\') { + if (p == pend) + return REG_EESCAPE; + + PATFETCH(c1); + { + rx_Bitset it = inverse_translation(rxb, + validate_inv_tr, + inverse_translate, + translate, + c1); + + rx_bitset_union(rxb->rx.local_cset_size, cs, + it); + } + continue; + } + + /* Could be the end of the bracket expression. If it's + not (i.e., when the bracket expression is `[]' so + far), the ']' character bit gets set way below. */ + if (c == ']' && p != p1 + 1) + goto finalize_class_and_append; + + /* Look ahead to see if it's a range when the last thing + was a character class. */ + if (had_char_class && c == '-' && *p != ']') + return REG_ERANGE; + + /* Look ahead to see if it's a range when the last thing + was a character: if this is a hyphen not at the + beginning or the end of a list, then it's the range + operator. */ + if (c == '-' && !(p - 2 >= pattern && p[-2] == '[') + && !(p - 3 >= pattern && p[-3] == '[' + && p[-2] == '^') && *p != ']') { + reg_errcode_t ret = + compile_range(rxb, cs, &p, pend, translate, + syntax, + inverse_translate, + + validate_inv_tr); + + if (ret != REG_NOERROR) + return ret; + } + + else if (p[0] == '-' && p[1] != ']') { /* This handles ranges made up of characters only. */ + reg_errcode_t ret; + + /* Move past the `-'. */ + PATFETCH(c1); + + ret = + compile_range(rxb, cs, &p, pend, translate, + syntax, inverse_translate, + validate_inv_tr); + if (ret != REG_NOERROR) + return ret; + } + + /* See if we're at the beginning of a possible character + class. */ + + else if ((syntax & RE_CHAR_CLASSES) + && (c == '[') && (*p == ':')) { + char str[CHAR_CLASS_MAX_LENGTH + 1]; + + PATFETCH(c); + c1 = 0; + + /* If pattern is `[[:'. */ + if (p == pend) + return REG_EBRACK; + + for (;;) { + PATFETCH(c); + if (c == ':' || c == ']' || p == pend + || c1 == CHAR_CLASS_MAX_LENGTH) break; + str[c1++] = c; + } + str[c1] = '\0'; + + /* If isn't a word bracketed by `[:' and:`]': + undo the ending character, the letters, and leave + the leading `:' and `[' (but set bits for them). */ + if (c == ':' && *p == ']') { + int ch; + boolean is_alnum = !strcmp(str, "alnum"); + boolean is_alpha = !strcmp(str, "alpha"); + boolean is_blank = !strcmp(str, "blank"); + boolean is_cntrl = !strcmp(str, "cntrl"); + boolean is_digit = !strcmp(str, "digit"); + boolean is_graph = !strcmp(str, "graph"); + boolean is_lower = !strcmp(str, "lower"); + boolean is_print = !strcmp(str, "print"); + boolean is_punct = !strcmp(str, "punct"); + boolean is_space = !strcmp(str, "space"); + boolean is_upper = !strcmp(str, "upper"); + boolean is_xdigit = !strcmp(str, "xdigit"); + + if (!IS_CHAR_CLASS(str)) + return REG_ECTYPE; + + /* Throw away the ] at the end of the character + class. */ + PATFETCH(c); + + if (p == pend) + return REG_EBRACK; + + for (ch = 0; ch < 1 << CHARBITS; ch++) { + if ((is_alnum && isalnum(ch)) + || (is_alpha && isalpha(ch)) + || (is_blank && isblank(ch)) + || (is_cntrl && iscntrl(ch)) + || (is_digit && isdigit(ch)) + || (is_graph && isgraph(ch)) + || (is_lower && islower(ch)) + || (is_print && isprint(ch)) + || (is_punct && ispunct(ch)) + || (is_space && isspace(ch)) + || (is_upper && isupper(ch)) + || (is_xdigit && isxdigit(ch))) { + rx_Bitset it = inverse_translation(rxb, + validate_inv_tr, + inverse_translate, + translate, + ch); + + rx_bitset_union(rxb-> + rx.local_cset_size, cs, + it); + } + } + had_char_class = true; + } else { + c1++; + while (c1--) + PATUNFETCH; + { + rx_Bitset it = inverse_translation(rxb, + validate_inv_tr, + inverse_translate, + translate, + '['); + + rx_bitset_union(rxb->rx.local_cset_size, + cs, it); + } + { + rx_Bitset it = inverse_translation(rxb, + validate_inv_tr, + inverse_translate, + translate, + ':'); + + rx_bitset_union(rxb->rx.local_cset_size, + cs, it); + } + had_char_class = false; + } + } else { + had_char_class = false; + { + rx_Bitset it = inverse_translation(rxb, + validate_inv_tr, + inverse_translate, + translate, + c); + + rx_bitset_union(rxb->rx.local_cset_size, cs, + it); + } + } + } + + finalize_class_and_append: + if (is_inverted) { + rx_bitset_complement(rxb->rx.local_cset_size, cs); + if (syntax & RE_HAT_LISTS_NOT_NEWLINE) + RX_bitset_remove(cs, '\n'); + } + goto append_node; + } + break; + + + case '(': + if (syntax & RE_NO_BK_PARENS) + goto handle_open; + else + goto normal_char; + + + case ')': + if (syntax & RE_NO_BK_PARENS) + goto handle_close; + else + goto normal_char; + + + case '\n': + if (syntax & RE_NEWLINE_ALT) + goto handle_alt; + else + goto normal_char; + + + case '|': + if (syntax & RE_NO_BK_VBAR) + goto handle_alt; + else + goto normal_char; + + + case '{': + if ((syntax & RE_INTERVALS) && (syntax & RE_NO_BK_BRACES)) + goto handle_interval; + else + goto normal_char; + + + case '\\': + if (p == pend) + return REG_EESCAPE; + + /* Do not translate the character after the \, so that we can + distinguish, e.g., \B from \b, even if we normally would + translate, e.g., B to b. */ + PATFETCH_RAW(c); + + switch (c) { + case '(': + if (syntax & RE_NO_BK_PARENS) + goto normal_backslash; + + handle_open: + rxb->re_nsub++; + regnum++; + if (COMPILE_STACK_FULL) { + ((compile_stack.stack) = + (compile_stack_elt_t *) realloc(compile_stack.stack, + (compile_stack.size << + 1) * + sizeof + (compile_stack_elt_t))); + if (compile_stack.stack == 0) + return REG_ESPACE; + + compile_stack.size <<= 1; + } + + if (*last_expression) { + struct rexp_node *concat + = rx_mk_r_concat(&rxb->rx, *last_expression, 0); + + if (!concat) + return REG_ESPACE; + *last_expression = concat; + last_expression = &concat->params.pair.right; + } + + /* + * These are the values to restore when we hit end of this + * group. + */ + COMPILE_STACK_TOP.top_expression = top_expression; + COMPILE_STACK_TOP.last_expression = last_expression; + COMPILE_STACK_TOP.regnum = regnum; + + compile_stack.avail++; + + top_expression = last_expression; + break; + + + case ')': + if (syntax & RE_NO_BK_PARENS) + goto normal_backslash; + + handle_close: + /* See similar code for backslashed left paren above. */ + if (COMPILE_STACK_EMPTY) { + if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD) { + goto normal_char; + } else { + return REG_ERPAREN; + } + } + + /* Since we just checked for an empty stack above, this + ``can't happen''. */ + + { + /* We don't just want to restore into `regnum', because + later groups should continue to be numbered higher, + as in `(ab)c(de)' -- the second group is #2. */ + regnum_t this_group_regnum; + struct rexp_node **inner = top_expression; + + compile_stack.avail--; + top_expression = COMPILE_STACK_TOP.top_expression; + last_expression = COMPILE_STACK_TOP.last_expression; + this_group_regnum = COMPILE_STACK_TOP.regnum; + { + int left_se = paramc; + int right_se = paramc + 1; + + params = (params ? ((struct re_se_params *) + realloc(params, + (paramc + + 2) * + sizeof(params[0]))) + : ((struct re_se_params *) + malloc(2 * sizeof(params[0])))); + if (!params) + return REG_ESPACE; + paramc += 2; + + params[left_se].se = re_se_lparen; + params[left_se].op1 = this_group_regnum; + params[right_se].se = re_se_rparen; + params[right_se].op1 = this_group_regnum; + { + struct rexp_node *left + = rx_mk_r_side_effect(&rxb->rx, + (rx_side_effect) left_se); + struct rexp_node *right + = rx_mk_r_side_effect(&rxb->rx, + (rx_side_effect) right_se); + struct rexp_node *c1 + = (*inner ? rx_mk_r_concat(&rxb->rx, left, + *inner) : left); + struct rexp_node *c2 = + rx_mk_r_concat(&rxb->rx, c1, right); + + if (!(left && right && c1 && c2)) + return REG_ESPACE; + *inner = c2; + } + } + break; + } + + case '|': /* `\|'. */ + if ((syntax & RE_LIMITED_OPS) || (syntax & RE_NO_BK_VBAR)) + goto normal_backslash; + handle_alt: + if (syntax & RE_LIMITED_OPS) + goto normal_char; + + { + struct rexp_node *alt + = rx_mk_r_alternate(&rxb->rx, *top_expression, 0); + + if (!alt) + return REG_ESPACE; + *top_expression = alt; + last_expression = &alt->params.pair.right; + { + int sync_se = paramc; + + params = (params ? ((struct re_se_params *) + realloc(params, + (paramc + + 1) * + sizeof(params[0]))) + : ((struct re_se_params *) + malloc(sizeof(params[0])))); + if (!params) + return REG_ESPACE; + ++paramc; + + params[sync_se].se = re_se_tv; + { + struct rexp_node *sync + = rx_mk_r_side_effect(&rxb->rx, + (rx_side_effect) sync_se); + struct rexp_node *conc + = rx_mk_r_concat(&rxb->rx, sync, 0); + + if (!sync || !conc) + return REG_ESPACE; + + *last_expression = conc; + last_expression = &conc->params.pair.right; + } + } + } + break; + + + case '{': + /* If \{ is a literal. */ + if (!(syntax & RE_INTERVALS) + /* If we're at `\{' and it's not the open-interval + operator. */ + || ((syntax & RE_INTERVALS) + && (syntax & RE_NO_BK_BRACES)) || (p - 2 == pattern + && p == pend)) + goto normal_backslash; + + handle_interval: + { + /* If got here, then the syntax allows intervals. */ + + /* At least (most) this many matches must be made. */ + int lower_bound = -1, upper_bound = -1; + + beg_interval = p - 1; + + if (p == pend) { + if (syntax & RE_NO_BK_BRACES) + goto unfetch_interval; + else + return REG_EBRACE; + } + + GET_UNSIGNED_NUMBER(lower_bound); + + if (c == ',') { + GET_UNSIGNED_NUMBER(upper_bound); + if (upper_bound < 0) + upper_bound = RE_DUP_MAX; + } else + /* Interval such as `{1}' => match exactly once. */ + upper_bound = lower_bound; + + if (lower_bound < 0 || upper_bound > RE_DUP_MAX + || lower_bound > upper_bound) { + if (syntax & RE_NO_BK_BRACES) + goto unfetch_interval; + else + return REG_BADBR; + } + + if (!(syntax & RE_NO_BK_BRACES)) { + if (c != '\\') + return REG_EBRACE; + PATFETCH(c); + } + + if (c != '}') { + if (syntax & RE_NO_BK_BRACES) + goto unfetch_interval; + else + return REG_BADBR; + } + + /* We just parsed a valid interval. */ + + /* If it's invalid to have no preceding re. */ + if (pointless_if_repeated(*last_expression, params)) { + if (syntax & RE_CONTEXT_INVALID_OPS) + return REG_BADRPT; + else if (!(syntax & RE_CONTEXT_INDEP_OPS)) + goto unfetch_interval; + /* was: else laststart = b; */ + } + + /* If the upper bound is zero, don't want to iterate + * at all. + */ + if (upper_bound == 0) { + if (*last_expression) { + rx_free_rexp(&rxb->rx, *last_expression); + *last_expression = 0; + } + } else + /* Otherwise, we have a nontrivial interval. */ + { + int iter_se = paramc; + int end_se = paramc + 1; + + params = (params ? ((struct re_se_params *) + realloc(params, + sizeof(*params) * (2 + + paramc))) + : ((struct re_se_params *) + malloc(2 * sizeof(*params)))); + if (!params) + return REG_ESPACE; + paramc += 2; + params[iter_se].se = re_se_iter; + params[iter_se].op1 = lower_bound; + params[iter_se].op2 = upper_bound; + + params[end_se].se = re_se_end_iter; + params[end_se].op1 = lower_bound; + params[end_se].op2 = upper_bound; + { + struct rexp_node *push0 + = rx_mk_r_side_effect(&rxb->rx, + (rx_side_effect) re_se_push0); + struct rexp_node *start_one_iter + = rx_mk_r_side_effect(&rxb->rx, + (rx_side_effect) iter_se); + struct rexp_node *phase1 + = rx_mk_r_concat(&rxb->rx, start_one_iter, + *last_expression); + struct rexp_node *pushback + = rx_mk_r_side_effect(&rxb->rx, + (rx_side_effect) re_se_pushback); + rx_Bitset cs = rx_cset(&rxb->rx); + struct rexp_node *lit_t; + struct rexp_node *phase2; + struct rexp_node *loop; + struct rexp_node *push_n_loop; + struct rexp_node *final_test; + struct rexp_node *full_exp; + + if (!cs) + return REG_ESPACE; + lit_t = rx_mk_r_cset(&rxb->rx, cs); + phase2 = + rx_mk_r_concat(&rxb->rx, pushback, lit_t); + loop = + rx_mk_r_2phase_star(&rxb->rx, phase1, + phase2); + push_n_loop = + rx_mk_r_concat(&rxb->rx, push0, loop); + final_test = + rx_mk_r_side_effect(&rxb->rx, + (rx_side_effect) + end_se); + full_exp = + rx_mk_r_concat(&rxb->rx, push_n_loop, + final_test); + + if (!(push0 && start_one_iter && phase1 + && pushback && lit_t && phase2 + && loop && push_n_loop && final_test + && full_exp)) return REG_ESPACE; + + RX_bitset_enjoin(cs, 't'); + + *last_expression = full_exp; + } + } + beg_interval = 0; + } + break; + + unfetch_interval: + /* If an invalid interval, match the characters as literals. */ + p = beg_interval; + beg_interval = 0; + + /* normal_char and normal_backslash need `c'. */ + PATFETCH(c); + + if (!(syntax & RE_NO_BK_BRACES)) { + if (p > pattern && p[-1] == '\\') + goto normal_backslash; + } + goto normal_char; #ifdef emacs - /* There is no way to specify the before_dot and after_dot - operators. rms says this is ok. --karl */ - case '=': - side = (rx_side_effect)rx_se_at_dot; - goto add_side_effect; - break; - - case 's': - case 'S': - { - rx_Bitset cs = rx_cset (&rxb->rx); - struct rexp_node * set = rx_mk_r_cset (&rxb->rx, cs); - if (!(cs && set)) - return REG_ESPACE; - if (c == 'S') - rx_bitset_universe (rxb->rx.local_cset_size, cs); - - PATFETCH (c); - { - int x; - enum syntaxcode code = syntax_spec_code [c]; - for (x = 0; x < 256; ++x) - { - - if (SYNTAX (x) == code) + /* There is no way to specify the before_dot and after_dot + operators. rms says this is ok. --karl */ + case '=': + side = (rx_side_effect) rx_se_at_dot; + goto add_side_effect; + break; + + case 's': + case 'S': { - rx_Bitset it = - inverse_translation (rxb, validate_inv_tr, - inverse_translate, - translate, x); - rx_bitset_xor (rxb->rx.local_cset_size, cs, it); + rx_Bitset cs = rx_cset(&rxb->rx); + struct rexp_node *set = rx_mk_r_cset(&rxb->rx, cs); + + if (!(cs && set)) + return REG_ESPACE; + if (c == 'S') + rx_bitset_universe(rxb->rx.local_cset_size, cs); + + PATFETCH(c); + { + int x; + enum syntaxcode code = syntax_spec_code[c]; + + for (x = 0; x < 256; ++x) { + + if (SYNTAX(x) == code) { + rx_Bitset it = + inverse_translation(rxb, validate_inv_tr, + inverse_translate, + translate, x); + + rx_bitset_xor(rxb->rx.local_cset_size, cs, it); + } + } + } + append = set; + goto append_node; } - } - } - append = set; - goto append_node; - } - break; -#endif /* emacs */ - - - case 'w': - case 'W': - if (syntax & RE_NO_GNU_OPS) - goto normal_char; - { - rx_Bitset cs = rx_cset (&rxb->rx); - struct rexp_node * n = (cs ? rx_mk_r_cset (&rxb->rx, cs) : 0); - if (!(cs && n)) - return REG_ESPACE; - if (c == 'W') - rx_bitset_universe (rxb->rx.local_cset_size ,cs); - { - int x; - for (x = rxb->rx.local_cset_size - 1; x > 0; --x) - if (SYNTAX(x) & Sword) - RX_bitset_toggle (cs, x); - } - append = n; - goto append_node; - } - break; + break; +#endif /* emacs */ + + + case 'w': + case 'W': + if (syntax & RE_NO_GNU_OPS) + goto normal_char; + { + rx_Bitset cs = rx_cset(&rxb->rx); + struct rexp_node *n = + (cs ? rx_mk_r_cset(&rxb->rx, cs) : 0); + + if (!(cs && n)) + return REG_ESPACE; + if (c == 'W') + rx_bitset_universe(rxb->rx.local_cset_size, cs); + { + int x; + + for (x = rxb->rx.local_cset_size - 1; x > 0; --x) + if (SYNTAX(x) & Sword) + RX_bitset_toggle(cs, x); + } + append = n; + goto append_node; + } + break; /* With a little extra work, some of these side effects could be optimized * away (basicly by looking at what we already know about the surrounding * chars). */ - case '<': - if (syntax & RE_NO_GNU_OPS) - goto normal_char; - side = (rx_side_effect)re_se_wordbeg; - goto add_side_effect; - break; - - case '>': - if (syntax & RE_NO_GNU_OPS) - goto normal_char; - side = (rx_side_effect)re_se_wordend; - goto add_side_effect; - break; - - case 'b': - if (syntax & RE_NO_GNU_OPS) - goto normal_char; - side = (rx_side_effect)re_se_wordbound; - goto add_side_effect; - break; - - case 'B': - if (syntax & RE_NO_GNU_OPS) - goto normal_char; - side = (rx_side_effect)re_se_notwordbound; - goto add_side_effect; - break; - - case '`': - if (syntax & RE_NO_GNU_OPS) - goto normal_char; - side = (rx_side_effect)re_se_begbuf; - goto add_side_effect; - break; - - case '\'': - if (syntax & RE_NO_GNU_OPS) - goto normal_char; - side = (rx_side_effect)re_se_endbuf; - goto add_side_effect; - break; - - add_side_effect: - { - struct rexp_node * se - = rx_mk_r_side_effect (&rxb->rx, side); - if (!se) - return REG_ESPACE; - append = se; - goto append_node; - } - break; - - case '1': case '2': case '3': case '4': case '5': - case '6': case '7': case '8': case '9': - if (syntax & RE_NO_BK_REFS) - goto normal_char; - - c1 = c - '0'; - - if (c1 > regnum) - return REG_ESUBREG; - - /* Can't back reference to a subexpression if inside of it. */ - if (group_in_compile_stack (compile_stack, c1)) - return REG_ESUBREG; - - { - int backref_se = paramc; - params = (params - ? ((struct re_se_params *) - realloc (params, - sizeof (*params) * (1 + paramc))) - : ((struct re_se_params *) - malloc (sizeof (*params)))); - if (!params) - return REG_ESPACE; - ++paramc; - params[backref_se].se = re_se_backref; - params[backref_se].op1 = c1; - side = (rx_side_effect)backref_se; - goto add_side_effect; - } - break; - - case '+': - case '?': - if (syntax & RE_BK_PLUS_QM) - goto handle_plus; - else - goto normal_backslash; - - default: - normal_backslash: - /* You might think it would be useful for \ to mean - not to translate; but if we don't translate it - it will never match anything. */ - c = TRANSLATE (c); - goto normal_char; - } - break; + case '<': + if (syntax & RE_NO_GNU_OPS) + goto normal_char; + side = (rx_side_effect) re_se_wordbeg; + goto add_side_effect; + break; + + case '>': + if (syntax & RE_NO_GNU_OPS) + goto normal_char; + side = (rx_side_effect) re_se_wordend; + goto add_side_effect; + break; + + case 'b': + if (syntax & RE_NO_GNU_OPS) + goto normal_char; + side = (rx_side_effect) re_se_wordbound; + goto add_side_effect; + break; + + case 'B': + if (syntax & RE_NO_GNU_OPS) + goto normal_char; + side = (rx_side_effect) re_se_notwordbound; + goto add_side_effect; + break; + + case '`': + if (syntax & RE_NO_GNU_OPS) + goto normal_char; + side = (rx_side_effect) re_se_begbuf; + goto add_side_effect; + break; + + case '\'': + if (syntax & RE_NO_GNU_OPS) + goto normal_char; + side = (rx_side_effect) re_se_endbuf; + goto add_side_effect; + break; + + add_side_effect: + { + struct rexp_node *se + + = rx_mk_r_side_effect(&rxb->rx, side); + if (!se) + return REG_ESPACE; + append = se; + goto append_node; + } + break; + + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + if (syntax & RE_NO_BK_REFS) + goto normal_char; + + c1 = c - '0'; + + if (c1 > regnum) + return REG_ESUBREG; + + /* Can't back reference to a subexpression if inside of it. */ + if (group_in_compile_stack(compile_stack, c1)) + return REG_ESUBREG; + + { + int backref_se = paramc; + + params = (params ? ((struct re_se_params *) + realloc(params, + sizeof(*params) * (1 + + paramc))) + : ((struct re_se_params *) + malloc(sizeof(*params)))); + if (!params) + return REG_ESPACE; + ++paramc; + params[backref_se].se = re_se_backref; + params[backref_se].op1 = c1; + side = (rx_side_effect) backref_se; + goto add_side_effect; + } + break; + + case '+': + case '?': + if (syntax & RE_BK_PLUS_QM) + goto handle_plus; + else + goto normal_backslash; + + default: + normal_backslash: + /* You might think it would be useful for \ to mean + not to translate; but if we don't translate it + it will never match anything. */ + c = TRANSLATE(c); + goto normal_char; + } + break; - default: - /* Expects the character in `c'. */ - normal_char: - { - rx_Bitset cs = rx_cset(&rxb->rx); - struct rexp_node * match = rx_mk_r_cset (&rxb->rx, cs); - rx_Bitset it; - if (!(cs && match)) - return REG_ESPACE; - it = inverse_translation (rxb, validate_inv_tr, - inverse_translate, translate, c); - rx_bitset_union (CHAR_SET_SIZE, cs, it); - append = match; - - append_node: - /* This genericly appends the rexp APPEND to *LAST_EXPRESSION - * and then parses the next character normally. - */ - if (*last_expression) + default: + /* Expects the character in `c'. */ + normal_char: { - struct rexp_node * concat - = rx_mk_r_concat (&rxb->rx, *last_expression, append); - if (!concat) - return REG_ESPACE; - *last_expression = concat; - last_expression = &concat->params.pair.right; + rx_Bitset cs = rx_cset(&rxb->rx); + struct rexp_node *match = rx_mk_r_cset(&rxb->rx, cs); + rx_Bitset it; + + if (!(cs && match)) + return REG_ESPACE; + it = inverse_translation(rxb, validate_inv_tr, + inverse_translate, translate, c); + rx_bitset_union(CHAR_SET_SIZE, cs, it); + append = match; + + append_node: + /* This genericly appends the rexp APPEND to *LAST_EXPRESSION + * and then parses the next character normally. + */ + if (*last_expression) { + struct rexp_node *concat + = rx_mk_r_concat(&rxb->rx, *last_expression, append); + + if (!concat) + return REG_ESPACE; + *last_expression = concat; + last_expression = &concat->params.pair.right; + } else + *last_expression = append; } - else - *last_expression = append; - } - } /* switch (c) */ - } /* while p != pend */ - - - { - int win_se = paramc; - params = (params - ? ((struct re_se_params *) - realloc (params, - sizeof (*params) * (1 + paramc))) - : ((struct re_se_params *) - malloc (sizeof (*params)))); - if (!params) - return REG_ESPACE; - ++paramc; - params[win_se].se = re_se_win; - { - struct rexp_node * se - = rx_mk_r_side_effect (&rxb->rx, (rx_side_effect)win_se); - struct rexp_node * concat - = rx_mk_r_concat (&rxb->rx, rexp, se); - if (!(se && concat)) - return REG_ESPACE; - rexp = concat; - } - } + } /* switch (c) */ + } /* while p != pend */ + + + { + int win_se = paramc; + + params = (params ? ((struct re_se_params *) + realloc(params, + sizeof(*params) * (1 + paramc))) + : ((struct re_se_params *) + malloc(sizeof(*params)))); + if (!params) + return REG_ESPACE; + ++paramc; + params[win_se].se = re_se_win; + { + struct rexp_node *se + = rx_mk_r_side_effect(&rxb->rx, (rx_side_effect) win_se); + struct rexp_node *concat = rx_mk_r_concat(&rxb->rx, rexp, se); + + if (!(se && concat)) + return REG_ESPACE; + rexp = concat; + } + } - /* Through the pattern now. */ + /* Through the pattern now. */ - if (!COMPILE_STACK_EMPTY) - return REG_EPAREN; + if (!COMPILE_STACK_EMPTY) + return REG_EPAREN; - free (compile_stack.stack); + free(compile_stack.stack); - orig_rexp = rexp; + orig_rexp = rexp; #ifdef RX_DEBUG - if (rx_debug_compile) - { - dbug_rxb = rxb; - fputs ("\n\nCompiling ", stdout); - fwrite (pattern, 1, size, stdout); - fputs (":\n", stdout); - rxb->se_params = params; - print_rexp (&rxb->rx, orig_rexp, 2, re_seprint, stdout); - } + if (rx_debug_compile) { + dbug_rxb = rxb; + fputs("\n\nCompiling ", stdout); + fwrite(pattern, 1, size, stdout); + fputs(":\n", stdout); + rxb->se_params = params; + print_rexp(&rxb->rx, orig_rexp, 2, re_seprint, stdout); + } #endif - { - rx_Bitset cs = rx_cset(&rxb->rx); - rx_Bitset cs2 = rx_cset(&rxb->rx); - char * se_map = (char *) alloca (paramc); - struct rexp_node * new_rexp = 0; + { + rx_Bitset cs = rx_cset(&rxb->rx); + rx_Bitset cs2 = rx_cset(&rxb->rx); + char *se_map = (char *) alloca(paramc); + struct rexp_node *new_rexp = 0; - bzero (se_map, paramc); - find_backrefs (se_map, rexp, params); - fewer_side_effects = - remove_unecessary_side_effects (&rxb->rx, se_map, - rx_copy_rexp (&rxb->rx, rexp), params); + bzero(se_map, paramc); + find_backrefs(se_map, rexp, params); + fewer_side_effects = + remove_unecessary_side_effects(&rxb->rx, se_map, + rx_copy_rexp(&rxb->rx, rexp), + params); - speed_up_alt (&rxb->rx, rexp, 0); - speed_up_alt (&rxb->rx, fewer_side_effects, 1); + speed_up_alt(&rxb->rx, rexp, 0); + speed_up_alt(&rxb->rx, fewer_side_effects, 1); - { - char * syntax_parens = rxb->syntax_parens; - if (syntax_parens == (char *)0x1) - rexp = remove_unecessary_side_effects - (&rxb->rx, se_map, rexp, params); - else if (syntax_parens) - { - int x; - for (x = 0; x < paramc; ++x) - if (( (params[x].se == re_se_lparen) - || (params[x].se == re_se_rparen)) - && (!syntax_parens [params[x].op1])) - se_map [x] = 1; - rexp = remove_unecessary_side_effects - (&rxb->rx, se_map, rexp, params); - } - } + { + char *syntax_parens = rxb->syntax_parens; + + if (syntax_parens == (char *) 0x1) + rexp = remove_unecessary_side_effects + (&rxb->rx, se_map, rexp, params); + else if (syntax_parens) { + int x; + + for (x = 0; x < paramc; ++x) + if (((params[x].se == re_se_lparen) + || (params[x].se == re_se_rparen)) + && (!syntax_parens[params[x].op1])) + se_map[x] = 1; + rexp = remove_unecessary_side_effects + (&rxb->rx, se_map, rexp, params); + } + } - /* At least one more optimization would be nice to have here but i ran out - * of time. The idea would be to delay side effects. - * For examle, `(abc)' is the same thing as `abc()' except that the - * left paren is offset by 3 (which we know at compile time). - * (In this comment, write that second pattern `abc(:3:)' - * where `(:3:' is a syntactic unit.) - * - * Trickier: `(abc|defg)' is the same as `(abc(:3:|defg(:4:))' - * (The paren nesting may be hard to follow -- that's an alternation - * of `abc(:3:' and `defg(:4:' inside (purely syntactic) parens - * followed by the closing paren from the original expression.) - * - * Neither the expression tree representation nor the the nfa make - * this very easy to write. :( - */ - - /* What we compile is different than what the parser returns. - * Suppose the parser returns expression R. - * Let R' be R with unnecessary register assignments removed - * (see REMOVE_UNECESSARY_SIDE_EFFECTS, above). - * - * What we will compile is the expression: - * - * m{try}R{win}\|s{try}R'{win} - * - * {try} and {win} denote side effect epsilons (see EXPLORE_FUTURE). - * - * When trying a match, we insert an `m' at the beginning of the - * string if the user wants registers to be filled, `s' if not. - */ - new_rexp = - rx_mk_r_alternate - (&rxb->rx, - rx_mk_r_concat (&rxb->rx, rx_mk_r_cset (&rxb->rx, cs2), rexp), - rx_mk_r_concat (&rxb->rx, - rx_mk_r_cset (&rxb->rx, cs), fewer_side_effects)); - - if (!(new_rexp && cs && cs2)) - return REG_ESPACE; - RX_bitset_enjoin (cs2, '\0'); /* prefixed to the rexp used for matching. */ - RX_bitset_enjoin (cs, '\1'); /* prefixed to the rexp used for searching. */ - rexp = new_rexp; - } + /* At least one more optimization would be nice to have here but i ran out + * of time. The idea would be to delay side effects. + * For examle, `(abc)' is the same thing as `abc()' except that the + * left paren is offset by 3 (which we know at compile time). + * (In this comment, write that second pattern `abc(:3:)' + * where `(:3:' is a syntactic unit.) + * + * Trickier: `(abc|defg)' is the same as `(abc(:3:|defg(:4:))' + * (The paren nesting may be hard to follow -- that's an alternation + * of `abc(:3:' and `defg(:4:' inside (purely syntactic) parens + * followed by the closing paren from the original expression.) + * + * Neither the expression tree representation nor the the nfa make + * this very easy to write. :( + */ + + /* What we compile is different than what the parser returns. + * Suppose the parser returns expression R. + * Let R' be R with unnecessary register assignments removed + * (see REMOVE_UNECESSARY_SIDE_EFFECTS, above). + * + * What we will compile is the expression: + * + * m{try}R{win}\|s{try}R'{win} + * + * {try} and {win} denote side effect epsilons (see EXPLORE_FUTURE). + * + * When trying a match, we insert an `m' at the beginning of the + * string if the user wants registers to be filled, `s' if not. + */ + new_rexp = + rx_mk_r_alternate + (&rxb->rx, + rx_mk_r_concat(&rxb->rx, rx_mk_r_cset(&rxb->rx, cs2), rexp), + rx_mk_r_concat(&rxb->rx, + rx_mk_r_cset(&rxb->rx, cs), + fewer_side_effects)); + + if (!(new_rexp && cs && cs2)) + return REG_ESPACE; + RX_bitset_enjoin(cs2, '\0'); /* prefixed to the rexp used for matching. */ + RX_bitset_enjoin(cs, '\1'); /* prefixed to the rexp used for searching. */ + rexp = new_rexp; + } #ifdef RX_DEBUG - if (rx_debug_compile) - { - fputs ("\n...which is compiled as:\n", stdout); - print_rexp (&rxb->rx, rexp, 2, re_seprint, stdout); - } + if (rx_debug_compile) { + fputs("\n...which is compiled as:\n", stdout); + print_rexp(&rxb->rx, rexp, 2, re_seprint, stdout); + } #endif - { - struct rx_nfa_state *start = 0; - struct rx_nfa_state *end = 0; - - if (!rx_build_nfa (&rxb->rx, rexp, &start, &end)) - return REG_ESPACE; /* */ - else - { - void * mem = (void *)rxb->buffer; - unsigned long size = rxb->allocated; - int start_id; - char * perm_mem; - int iterator_size = paramc * sizeof (params[0]); - - end->is_final = 1; - start->is_start = 1; - rx_name_nfa_states (&rxb->rx); - start_id = start->id; + { + struct rx_nfa_state *start = 0; + struct rx_nfa_state *end = 0; + + if (!rx_build_nfa(&rxb->rx, rexp, &start, &end)) + return REG_ESPACE; /* */ + else { + void *mem = (void *) rxb->buffer; + unsigned long size = rxb->allocated; + int start_id; + char *perm_mem; + int iterator_size = paramc * sizeof(params[0]); + + end->is_final = 1; + start->is_start = 1; + rx_name_nfa_states(&rxb->rx); + start_id = start->id; #ifdef RX_DEBUG - if (rx_debug_compile) - { - fputs ("...giving the NFA: \n", stdout); - dbug_rxb = rxb; - print_nfa (&rxb->rx, rxb->rx.nfa_states, re_seprint, stdout); - } -#endif - if (!rx_eclose_nfa (&rxb->rx)) - return REG_ESPACE; - else - { - rx_delete_epsilon_transitions (&rxb->rx); - - /* For compatability reasons, we need to shove the - * compiled nfa into one chunk of malloced memory. - */ - rxb->rx.reserved = ( sizeof (params[0]) * paramc - + rx_sizeof_bitset (rxb->rx.local_cset_size)); + if (rx_debug_compile) { + fputs("...giving the NFA: \n", stdout); + dbug_rxb = rxb; + print_nfa(&rxb->rx, rxb->rx.nfa_states, re_seprint, + stdout); + } +#endif + if (!rx_eclose_nfa(&rxb->rx)) + return REG_ESPACE; + else { + rx_delete_epsilon_transitions(&rxb->rx); + + /* For compatability reasons, we need to shove the + * compiled nfa into one chunk of malloced memory. + */ + rxb->rx.reserved = (sizeof(params[0]) * paramc + + + rx_sizeof_bitset(rxb-> + rx.local_cset_size)); #ifdef RX_DEBUG - if (rx_debug_compile) - { - dbug_rxb = rxb; - fputs ("...which cooks down (uncompactified) to: \n", stdout); - print_nfa (&rxb->rx, rxb->rx.nfa_states, re_seprint, stdout); - } -#endif - if (!rx_compactify_nfa (&rxb->rx, &mem, &size)) - return REG_ESPACE; - rxb->buffer = mem; - rxb->allocated = size; - rxb->rx.buffer = mem; - rxb->rx.allocated = size; - perm_mem = ((char *)rxb->rx.buffer - + rxb->rx.allocated - rxb->rx.reserved); - rxb->se_params = ((struct re_se_params *)perm_mem); - bcopy (params, rxb->se_params, iterator_size); - perm_mem += iterator_size; - rxb->fastset = (rx_Bitset) perm_mem; - rxb->start = rx_id_to_nfa_state (&rxb->rx, start_id); - } - rx_bitset_null (rxb->rx.local_cset_size, rxb->fastset); - rxb->can_match_empty = compute_fastset (rxb, orig_rexp); - rxb->match_regs_on_stack = - registers_on_stack (rxb, orig_rexp, 0, params); - rxb->search_regs_on_stack = - registers_on_stack (rxb, fewer_side_effects, 0, params); - if (rxb->can_match_empty) - rx_bitset_universe (rxb->rx.local_cset_size, rxb->fastset); - rxb->is_anchored = is_anchored (orig_rexp, (rx_side_effect) re_se_hat); - rxb->begbuf_only = is_anchored (orig_rexp, - (rx_side_effect) re_se_begbuf); - } - rx_free_rexp (&rxb->rx, rexp); - if (params) - free (params); + if (rx_debug_compile) { + dbug_rxb = rxb; + fputs("...which cooks down (uncompactified) to: \n", + stdout); + print_nfa(&rxb->rx, rxb->rx.nfa_states, re_seprint, + stdout); + } +#endif + if (!rx_compactify_nfa(&rxb->rx, &mem, &size)) + return REG_ESPACE; + rxb->buffer = mem; + rxb->allocated = size; + rxb->rx.buffer = mem; + rxb->rx.allocated = size; + perm_mem = ((char *) rxb->rx.buffer + + rxb->rx.allocated - rxb->rx.reserved); + rxb->se_params = ((struct re_se_params *) perm_mem); + bcopy(params, rxb->se_params, iterator_size); + perm_mem += iterator_size; + rxb->fastset = (rx_Bitset) perm_mem; + rxb->start = rx_id_to_nfa_state(&rxb->rx, start_id); + } + rx_bitset_null(rxb->rx.local_cset_size, rxb->fastset); + rxb->can_match_empty = compute_fastset(rxb, orig_rexp); + rxb->match_regs_on_stack = + registers_on_stack(rxb, orig_rexp, 0, params); + rxb->search_regs_on_stack = + registers_on_stack(rxb, fewer_side_effects, 0, params); + if (rxb->can_match_empty) + rx_bitset_universe(rxb->rx.local_cset_size, rxb->fastset); + rxb->is_anchored = + is_anchored(orig_rexp, (rx_side_effect) re_se_hat); + rxb->begbuf_only = + is_anchored(orig_rexp, (rx_side_effect) re_se_begbuf); + } + rx_free_rexp(&rxb->rx, rexp); + if (params) + free(params); #ifdef RX_DEBUG - if (rx_debug_compile) - { - dbug_rxb = rxb; - fputs ("...which cooks down to: \n", stdout); - print_nfa (&rxb->rx, rxb->rx.nfa_states, re_seprint, stdout); - } + if (rx_debug_compile) { + dbug_rxb = rxb; + fputs("...which cooks down to: \n", stdout); + print_nfa(&rxb->rx, rxb->rx.nfa_states, re_seprint, stdout); + } #endif - } - return REG_NOERROR; + } + return REG_NOERROR; } - + /* This table gives an error message for each of the error codes listed in regex.h. Obviously the order here has to be same as there. */ -__const__ char * rx_error_msg[] = -{ 0, /* REG_NOERROR */ - "No match", /* REG_NOMATCH */ - "Invalid regular expression", /* REG_BADPAT */ - "Invalid collation character", /* REG_ECOLLATE */ - "Invalid character class name", /* REG_ECTYPE */ - "Trailing backslash", /* REG_EESCAPE */ - "Invalid back reference", /* REG_ESUBREG */ - "Unmatched [ or [^", /* REG_EBRACK */ - "Unmatched ( or \\(", /* REG_EPAREN */ - "Unmatched \\{", /* REG_EBRACE */ - "Invalid content of \\{\\}", /* REG_BADBR */ - "Invalid range end", /* REG_ERANGE */ - "Memory exhausted", /* REG_ESPACE */ - "Invalid preceding regular expression", /* REG_BADRPT */ - "Premature end of regular expression", /* REG_EEND */ - "Regular expression too big", /* REG_ESIZE */ - "Unmatched ) or \\)", /* REG_ERPAREN */ +__const__ char *rx_error_msg[] = { 0, /* REG_NOERROR */ + "No match", /* REG_NOMATCH */ + "Invalid regular expression", /* REG_BADPAT */ + "Invalid collation character", /* REG_ECOLLATE */ + "Invalid character class name", /* REG_ECTYPE */ + "Trailing backslash", /* REG_EESCAPE */ + "Invalid back reference", /* REG_ESUBREG */ + "Unmatched [ or [^", /* REG_EBRACK */ + "Unmatched ( or \\(", /* REG_EPAREN */ + "Unmatched \\{", /* REG_EBRACE */ + "Invalid content of \\{\\}", /* REG_BADBR */ + "Invalid range end", /* REG_ERANGE */ + "Memory exhausted", /* REG_ESPACE */ + "Invalid preceding regular expression", /* REG_BADRPT */ + "Premature end of regular expression", /* REG_EEND */ + "Regular expression too big", /* REG_ESIZE */ + "Unmatched ) or \\)", /* REG_ERPAREN */ }; - -char rx_slowmap [256] = -{ - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + +char rx_slowmap[256] = { + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, }; #ifdef __STDC__ -RX_DECL void -rx_blow_up_fastmap (struct re_pattern_buffer * rxb) +RX_DECL void rx_blow_up_fastmap(struct re_pattern_buffer *rxb) #else -RX_DECL void -rx_blow_up_fastmap (rxb) - struct re_pattern_buffer * rxb; +RX_DECL void rx_blow_up_fastmap(rxb) +struct re_pattern_buffer *rxb; #endif { - int x; - for (x = 0; x < 256; ++x) /* &&&& 3.6 % */ - rxb->fastmap [x] = !!RX_bitset_member (rxb->fastset, x); - rxb->fastmap_accurate = 1; + int x; + + for (x = 0; x < 256; ++x) /* &&&& 3.6 % */ + rxb->fastmap[x] = !!RX_bitset_member(rxb->fastset, x); + rxb->fastmap_accurate = 1; } + - #if !defined(REGEX_MALLOC) && !defined(__GNUC__) #define RE_SEARCH_2_FN inner_re_search_2 #define RE_S2_QUAL static #else #define RE_SEARCH_2_FN re_search_2 -#define RE_S2_QUAL +#define RE_S2_QUAL #endif -struct re_search_2_closure -{ - __const__ char * string1; - int size1; - __const__ char * string2; - int size2; +struct re_search_2_closure { + __const__ char *string1; + int size1; + __const__ char *string2; + int size2; }; RE_S2_QUAL int - RE_SEARCH_2_FN (struct re_pattern_buffer *, - __const__ char *, - int, __const__ char *, int, int, - int, struct re_registers *, int); -int re_rx_search (struct re_pattern_buffer *, int, - int, int, int, rx_get_burst_fn, - rx_back_check_fn, rx_fetch_char_fn, - void *, struct re_registers *, - struct rx_search_state *, - struct rx_search_state *); +RE_SEARCH_2_FN(struct re_pattern_buffer *, + __const__ char *, + int, __const__ char *, int, int, + + int, struct re_registers *, int); +int re_rx_search(struct re_pattern_buffer *, int, + int, int, int, rx_get_burst_fn, + rx_back_check_fn, rx_fetch_char_fn, + void *, struct re_registers *, + + struct rx_search_state *, struct rx_search_state *); #if !defined(REGEX_MALLOC) && !defined(__GNUC__) -int re_search_2 (struct re_pattern_buffer *, - __const__ char *, int, - __const__ char *, int, - int, int, struct re_registers *, - int); -#endif -int re_search (struct re_pattern_buffer *, - __const__ char *, int, int, int, - struct re_registers *); -int re_match_2 (struct re_pattern_buffer *, - __const__ char *, int, - __const__ char *, int, - int, struct re_registers *, int); -int re_match (struct re_pattern_buffer *, - __const__ char *, int, int, - struct re_registers *); -reg_syntax_t re_set_syntax (reg_syntax_t); -void re_set_registers (struct re_pattern_buffer *, - struct re_registers *, unsigned, - regoff_t *, regoff_t *); -static int cplx_se_sublist_len (struct rx_se_list *); -static int posix_se_list_order (struct rx *, struct rx_se_list *, - struct rx_se_list *); +int re_search_2(struct re_pattern_buffer *, + __const__ char *, int, + __const__ char *, int, + + int, int, struct re_registers *, int); +#endif +int re_search(struct re_pattern_buffer *, + + __const__ char *, int, int, int, struct re_registers *); +int re_match_2(struct re_pattern_buffer *, + __const__ char *, int, + __const__ char *, int, int, struct re_registers *, int); +int re_match(struct re_pattern_buffer *, + + __const__ char *, int, int, struct re_registers *); +reg_syntax_t re_set_syntax(reg_syntax_t); +void re_set_registers(struct re_pattern_buffer *, + struct re_registers *, unsigned, + regoff_t *, regoff_t *); +static int cplx_se_sublist_len(struct rx_se_list *); +static int posix_se_list_order(struct rx *, struct rx_se_list *, + + struct rx_se_list *); __const__ char - *re_compile_pattern (__const__ char *, int, - struct re_pattern_buffer *); -int re_compile_fastmap (struct re_pattern_buffer *); -char *re_comp (__const__ char *); -int re_exec (__const__ char *); -int regcomp (regex_t *, __const__ char *, int); -int regexec (__const__ regex_t *, - __const__ char *, size_t, - regmatch_t pmatch[], int); -size_t regerror (int, __const__ regex_t *, - char *, size_t); +*re_compile_pattern(__const__ char *, int, struct re_pattern_buffer *); +int re_compile_fastmap(struct re_pattern_buffer *); +char *re_comp(__const__ char *); +int re_exec(__const__ char *); +int regcomp(regex_t *, __const__ char *, int); +int regexec(__const__ regex_t *, + __const__ char *, size_t, regmatch_t pmatch[], int); +size_t regerror(int, __const__ regex_t *, char *, size_t); #ifdef __STDC__ static __inline__ enum rx_get_burst_return -re_search_2_get_burst ( struct rx_string_position * pos, - void * vclosure, int stop ) +re_search_2_get_burst(struct rx_string_position *pos, + void *vclosure, int stop) #else static __inline__ enum rx_get_burst_return -re_search_2_get_burst (pos, vclosure, stop) - struct rx_string_position * pos; - void * vclosure; - int stop; -#endif -{ - struct re_search_2_closure * closure; - closure = (struct re_search_2_closure *)vclosure; - if (!closure->string2) - { - int inset; - - inset = pos->pos - pos->string; - if ((inset < -1) || (inset > closure->size1)) - return rx_get_burst_no_more; - else - { - pos->pos = (__const__ unsigned char *) closure->string1 + inset; - pos->string = (__const__ unsigned char *) closure->string1; - pos->size = closure->size1; - pos->end = ((__const__ unsigned char *) - MIN(closure->string1 + closure->size1, - closure->string1 + stop)); - pos->offset = 0; - return ((pos->pos < pos->end) - ? rx_get_burst_ok - : rx_get_burst_no_more); - } - } - else if (!closure->string1) - { - int inset; - - inset = pos->pos - pos->string; - pos->pos = (__const__ unsigned char *) closure->string2 + inset; - pos->string = (__const__ unsigned char *) closure->string2; - pos->size = closure->size2; - pos->end = ((__const__ unsigned char *) - MIN(closure->string2 + closure->size2, - closure->string2 + stop)); - pos->offset = 0; - return ((pos->pos < pos->end) - ? rx_get_burst_ok - : rx_get_burst_no_more); - } - else - { - int inset; - - inset = pos->pos - pos->string + pos->offset; - if (inset < closure->size1) - { - pos->pos = (__const__ unsigned char *) closure->string1 + inset; - pos->string = (__const__ unsigned char *) closure->string1; - pos->size = closure->size1; - pos->end = ((__const__ unsigned char *) - MIN(closure->string1 + closure->size1, - closure->string1 + stop)); - pos->offset = 0; - return rx_get_burst_ok; - } - else - { - pos->pos = ((__const__ unsigned char *) - closure->string2 + inset - closure->size1); - pos->string = (__const__ unsigned char *) closure->string2; - pos->size = closure->size2; - pos->end = ((__const__ unsigned char *) - MIN(closure->string2 + closure->size2, - closure->string2 + stop - closure->size1)); - pos->offset = closure->size1; - return ((pos->pos < pos->end) - ? rx_get_burst_ok - : rx_get_burst_no_more); +re_search_2_get_burst(pos, vclosure, stop) +struct rx_string_position *pos; +void *vclosure; +int stop; +#endif +{ + struct re_search_2_closure *closure; + + closure = (struct re_search_2_closure *) vclosure; + if (!closure->string2) { + int inset; + + inset = pos->pos - pos->string; + if ((inset < -1) || (inset > closure->size1)) + return rx_get_burst_no_more; + else { + pos->pos = + (__const__ unsigned char *) closure->string1 + inset; + pos->string = (__const__ unsigned char *) closure->string1; + pos->size = closure->size1; + pos->end = ((__const__ unsigned char *) + MIN(closure->string1 + closure->size1, + closure->string1 + stop)); + pos->offset = 0; + return ((pos->pos < pos->end) + ? rx_get_burst_ok : rx_get_burst_no_more); + } + } else if (!closure->string1) { + int inset; + + inset = pos->pos - pos->string; + pos->pos = (__const__ unsigned char *) closure->string2 + inset; + pos->string = (__const__ unsigned char *) closure->string2; + pos->size = closure->size2; + pos->end = ((__const__ unsigned char *) + MIN(closure->string2 + closure->size2, + closure->string2 + stop)); + pos->offset = 0; + return ((pos->pos < pos->end) + ? rx_get_burst_ok : rx_get_burst_no_more); + } else { + int inset; + + inset = pos->pos - pos->string + pos->offset; + if (inset < closure->size1) { + pos->pos = + (__const__ unsigned char *) closure->string1 + inset; + pos->string = (__const__ unsigned char *) closure->string1; + pos->size = closure->size1; + pos->end = ((__const__ unsigned char *) + MIN(closure->string1 + closure->size1, + closure->string1 + stop)); + pos->offset = 0; + return rx_get_burst_ok; + } else { + pos->pos = ((__const__ unsigned char *) + closure->string2 + inset - closure->size1); + pos->string = (__const__ unsigned char *) closure->string2; + pos->size = closure->size2; + pos->end = ((__const__ unsigned char *) + MIN(closure->string2 + closure->size2, + closure->string2 + stop - closure->size1)); + pos->offset = closure->size1; + return ((pos->pos < pos->end) + ? rx_get_burst_ok : rx_get_burst_no_more); + } } - } } #ifdef __STDC__ static __inline__ enum rx_back_check_return -re_search_2_back_check ( struct rx_string_position * pos, - int lparen, int rparen, unsigned char * translate, - void * vclosure, int stop ) +re_search_2_back_check(struct rx_string_position *pos, + int lparen, int rparen, unsigned char *translate, + void *vclosure, int stop) #else static __inline__ enum rx_back_check_return -re_search_2_back_check (pos, lparen, rparen, translate, vclosure, stop) - struct rx_string_position * pos; - int lparen; - int rparen; - unsigned char * translate; - void * vclosure; - int stop; -#endif -{ - struct rx_string_position there; - struct rx_string_position past; - - there = *pos; - there.pos = there.string + lparen - there.offset; - re_search_2_get_burst (&there, vclosure, stop); - - past = *pos; - past.pos = past.string + rparen - there.offset; - re_search_2_get_burst (&past, vclosure, stop); - - ++pos->pos; - re_search_2_get_burst (pos, vclosure, stop); - - while ( (there.pos != past.pos) - && (pos->pos != pos->end)) - if (TRANSLATE(*there.pos) != TRANSLATE(*pos->pos)) - return rx_back_check_fail; - else - { - ++there.pos; +re_search_2_back_check(pos, lparen, rparen, translate, vclosure, stop) +struct rx_string_position *pos; +int lparen; +int rparen; +unsigned char *translate; +void *vclosure; +int stop; +#endif +{ + struct rx_string_position there; + struct rx_string_position past; + + there = *pos; + there.pos = there.string + lparen - there.offset; + re_search_2_get_burst(&there, vclosure, stop); + + past = *pos; + past.pos = past.string + rparen - there.offset; + re_search_2_get_burst(&past, vclosure, stop); + ++pos->pos; - if (there.pos == there.end) - re_search_2_get_burst (&there, vclosure, stop); - if (pos->pos == pos->end) - re_search_2_get_burst (pos, vclosure, stop); - } + re_search_2_get_burst(pos, vclosure, stop); + + while ((there.pos != past.pos) + && (pos->pos != pos->end)) + if (TRANSLATE(*there.pos) != TRANSLATE(*pos->pos)) + return rx_back_check_fail; + else { + ++there.pos; + ++pos->pos; + if (there.pos == there.end) + re_search_2_get_burst(&there, vclosure, stop); + if (pos->pos == pos->end) + re_search_2_get_burst(pos, vclosure, stop); + } - if (there.pos != past.pos) - return rx_back_check_fail; - --pos->pos; - re_search_2_get_burst (pos, vclosure, stop); - return rx_back_check_pass; + if (there.pos != past.pos) + return rx_back_check_fail; + --pos->pos; + re_search_2_get_burst(pos, vclosure, stop); + return rx_back_check_pass; } #ifdef __STDC__ static __inline__ int -re_search_2_fetch_char ( struct rx_string_position * pos, int offset, - void * app_closure, int stop ) +re_search_2_fetch_char(struct rx_string_position *pos, int offset, + void *app_closure, int stop) #else static __inline__ int -re_search_2_fetch_char (pos, offset, app_closure, stop) - struct rx_string_position * pos; - int offset; - void * app_closure; - int stop; -#endif -{ - struct re_search_2_closure * closure; - closure = (struct re_search_2_closure *)app_closure; - if (offset == 0) - { - if (pos->pos >= pos->string) - return *pos->pos; - else - { - if ( (pos->string == (__const__ unsigned char *) closure->string2) - && (closure->string1) - && (closure->size1)) - return closure->string1[closure->size1 - 1]; - else - return 0; /* sure, why not. */ +re_search_2_fetch_char(pos, offset, app_closure, stop) +struct rx_string_position *pos; +int offset; +void *app_closure; +int stop; +#endif +{ + struct re_search_2_closure *closure; + + closure = (struct re_search_2_closure *) app_closure; + if (offset == 0) { + if (pos->pos >= pos->string) + return *pos->pos; + else { + if ( + (pos->string == + (__const__ unsigned char *) closure->string2) + && (closure->string1) && (closure->size1)) + return closure->string1[closure->size1 - 1]; + else + return 0; /* sure, why not. */ + } } - } - if (pos->pos == pos->end) - return *closure->string2; - else + if (pos->pos == pos->end) + return *closure->string2; + else #if 0 - return pos->pos[1]; + return pos->pos[1]; #else - return pos->pos[offset]; /* FIXME */ + return pos->pos[offset]; /* FIXME */ #endif } #ifdef __STDC__ RE_S2_QUAL int -RE_SEARCH_2_FN (struct re_pattern_buffer *rxb, - __const__ char * string1, int size1, - __const__ char * string2, int size2, - int startpos, int range, - struct re_registers *regs, - int stop) +RE_SEARCH_2_FN(struct re_pattern_buffer *rxb, + __const__ char *string1, int size1, + __const__ char *string2, int size2, + int startpos, int range, + struct re_registers *regs, int stop) #else RE_S2_QUAL int -RE_SEARCH_2_FN (rxb, - string1, size1, string2, size2, startpos, range, regs, stop) - struct re_pattern_buffer *rxb; - __const__ char * string1; - int size1; - __const__ char * string2; - int size2; - int startpos; - int range; - struct re_registers *regs; - int stop; -#endif -{ - int answer; - struct re_search_2_closure closure; - closure.string1 = string1; - closure.size1 = size1; - closure.string2 = string2; - closure.size2 = size2; - answer = rx_search (rxb, startpos, range, stop, size1 + size2, - re_search_2_get_burst, - re_search_2_back_check, - re_search_2_fetch_char, - (void *)&closure, - regs, - 0, - 0); - switch (answer) - { - case rx_search_continuation: - abort (); - case rx_search_error: - return -2; - case rx_search_soft_fail: - case rx_search_fail: - return -1; - default: - return answer; - } +RE_SEARCH_2_FN(rxb, + string1, size1, string2, size2, startpos, range, regs, stop) +struct re_pattern_buffer *rxb; +__const__ char *string1; +int size1; +__const__ char *string2; +int size2; +int startpos; +int range; +struct re_registers *regs; +int stop; +#endif +{ + int answer; + struct re_search_2_closure closure; + + closure.string1 = string1; + closure.size1 = size1; + closure.string2 = string2; + closure.size2 = size2; + answer = rx_search(rxb, startpos, range, stop, size1 + size2, + re_search_2_get_burst, + re_search_2_back_check, + re_search_2_fetch_char, + (void *) &closure, regs, 0, 0); + switch (answer) { + case rx_search_continuation: + abort(); + case rx_search_error: + return -2; + case rx_search_soft_fail: + case rx_search_fail: + return -1; + default: + return answer; + } } /* Export rx_search to callers outside this file. */ #ifdef __STDC__ int -re_rx_search ( struct re_pattern_buffer * rxb, int startpos, int range, - int stop, int total_size, rx_get_burst_fn get_burst, - rx_back_check_fn back_check, rx_fetch_char_fn fetch_char, - void * app_closure, struct re_registers * regs, - struct rx_search_state * resume_state, - struct rx_search_state * save_state ) +re_rx_search(struct re_pattern_buffer *rxb, int startpos, int range, + int stop, int total_size, rx_get_burst_fn get_burst, + rx_back_check_fn back_check, rx_fetch_char_fn fetch_char, + void *app_closure, struct re_registers *regs, + struct rx_search_state *resume_state, + struct rx_search_state *save_state) #else int -re_rx_search (rxb, startpos, range, stop, total_size, - get_burst, back_check, fetch_char, - app_closure, regs, resume_state, save_state) - struct re_pattern_buffer * rxb; - int startpos; - int range; - int stop; - int total_size; - rx_get_burst_fn get_burst; - rx_back_check_fn back_check; - rx_fetch_char_fn fetch_char; - void * app_closure; - struct re_registers * regs; - struct rx_search_state * resume_state; - struct rx_search_state * save_state; -#endif -{ - return rx_search (rxb, startpos, range, stop, total_size, - get_burst, back_check, fetch_char, app_closure, - regs, resume_state, save_state); +re_rx_search(rxb, startpos, range, stop, total_size, + get_burst, back_check, fetch_char, + app_closure, regs, resume_state, save_state) +struct re_pattern_buffer *rxb; +int startpos; +int range; +int stop; +int total_size; +rx_get_burst_fn get_burst; +rx_back_check_fn back_check; +rx_fetch_char_fn fetch_char; +void *app_closure; +struct re_registers *regs; +struct rx_search_state *resume_state; +struct rx_search_state *save_state; +#endif +{ + return rx_search(rxb, startpos, range, stop, total_size, + get_burst, back_check, fetch_char, app_closure, + regs, resume_state, save_state); } #if !defined(REGEX_MALLOC) && !defined(__GNUC__) #ifdef __STDC__ int -re_search_2 (struct re_pattern_buffer *rxb, - __const__ char * string1, int size1, - __const__ char * string2, int size2, - int startpos, int range, - struct re_registers *regs, - int stop) +re_search_2(struct re_pattern_buffer *rxb, + __const__ char *string1, int size1, + __const__ char *string2, int size2, + int startpos, int range, struct re_registers *regs, int stop) #else int -re_search_2 (rxb, string1, size1, string2, size2, startpos, range, regs, stop) - struct re_pattern_buffer *rxb; - __const__ char * string1; - int size1; - __const__ char * string2; - int size2; - int startpos; - int range; - struct re_registers *regs; - int stop; +re_search_2(rxb, string1, size1, string2, size2, startpos, range, regs, + stop) +struct re_pattern_buffer *rxb; +__const__ char *string1; +int size1; +__const__ char *string2; +int size2; +int startpos; +int range; +struct re_registers *regs; +int stop; #endif { - int ret; - ret = inner_re_search_2 (rxb, string1, size1, string2, size2, startpos, - range, regs, stop); - alloca (0); - return ret; + int ret; + + ret = inner_re_search_2(rxb, string1, size1, string2, size2, startpos, + range, regs, stop); + alloca(0); + return ret; } #endif @@ -6873,93 +6651,88 @@ re_search_2 (rxb, string1, size1, string2, size2, startpos, range, regs, stop) #ifdef __STDC__ int -re_search (struct re_pattern_buffer * rxb, __const__ char *string, - int size, int startpos, int range, - struct re_registers *regs) +re_search(struct re_pattern_buffer *rxb, __const__ char *string, + int size, int startpos, int range, struct re_registers *regs) #else -int -re_search (rxb, string, size, startpos, range, regs) - struct re_pattern_buffer * rxb; - __const__ char * string; - int size; - int startpos; - int range; - struct re_registers *regs; +int re_search(rxb, string, size, startpos, range, regs) +struct re_pattern_buffer *rxb; +__const__ char *string; +int size; +int startpos; +int range; +struct re_registers *regs; #endif { - return re_search_2 (rxb, 0, 0, string, size, startpos, range, regs, size); + return re_search_2(rxb, 0, 0, string, size, startpos, range, regs, + size); } #ifdef __STDC__ int -re_match_2 (struct re_pattern_buffer * rxb, - __const__ char * string1, int size1, - __const__ char * string2, int size2, - int pos, struct re_registers *regs, int stop) -#else -int -re_match_2 (rxb, string1, size1, string2, size2, pos, regs, stop) - struct re_pattern_buffer * rxb; - __const__ char * string1; - int size1; - __const__ char * string2; - int size2; - int pos; - struct re_registers *regs; - int stop; -#endif -{ - struct re_registers some_regs; - regoff_t start; - regoff_t end; - int srch; - int save = rxb->regs_allocated; - struct re_registers * regs_to_pass = regs; - char *old_fastmap = rxb->fastmap; - - if (!regs) - { - some_regs.start = &start; - some_regs.end = &end; - some_regs.num_regs = 1; - regs_to_pass = &some_regs; - rxb->regs_allocated = REGS_FIXED; - } +re_match_2(struct re_pattern_buffer *rxb, + __const__ char *string1, int size1, + __const__ char *string2, int size2, + int pos, struct re_registers *regs, int stop) +#else +int re_match_2(rxb, string1, size1, string2, size2, pos, regs, stop) +struct re_pattern_buffer *rxb; +__const__ char *string1; +int size1; +__const__ char *string2; +int size2; +int pos; +struct re_registers *regs; +int stop; +#endif +{ + struct re_registers some_regs; + regoff_t start; + regoff_t end; + int srch; + int save = rxb->regs_allocated; + struct re_registers *regs_to_pass = regs; + char *old_fastmap = rxb->fastmap; + + if (!regs) { + some_regs.start = &start; + some_regs.end = &end; + some_regs.num_regs = 1; + regs_to_pass = &some_regs; + rxb->regs_allocated = REGS_FIXED; + } - rxb->fastmap = NULL; - srch = re_search_2 (rxb, string1, size1, string2, size2, - pos, 1, regs_to_pass, stop); - rxb->fastmap = old_fastmap; - if (regs_to_pass != regs) - rxb->regs_allocated = save; - if (srch < 0) - return srch; - return regs_to_pass->end[0] - regs_to_pass->start[0]; + rxb->fastmap = NULL; + srch = re_search_2(rxb, string1, size1, string2, size2, + pos, 1, regs_to_pass, stop); + rxb->fastmap = old_fastmap; + if (regs_to_pass != regs) + rxb->regs_allocated = save; + if (srch < 0) + return srch; + return regs_to_pass->end[0] - regs_to_pass->start[0]; } /* re_match is like re_match_2 except it takes only a single string. */ #ifdef __STDC__ int -re_match (struct re_pattern_buffer * rxb, - __const__ char * string, - int size, int pos, - struct re_registers *regs) +re_match(struct re_pattern_buffer *rxb, + __const__ char *string, + int size, int pos, struct re_registers *regs) #else -int -re_match (rxb, string, size, pos, regs) - struct re_pattern_buffer * rxb; - __const__ char *string; - int size; - int pos; - struct re_registers *regs; +int re_match(rxb, string, size, pos, regs) +struct re_pattern_buffer *rxb; +__const__ char *string; +int size; +int pos; +struct re_registers *regs; #endif { - return re_match_2 (rxb, string, size, 0, 0, pos, regs, size); + return re_match_2(rxb, string, size, 0, 0, pos, regs, size); } + - /* Set by `re_set_syntax' to the current regexp syntax to recognize. Can also be assigned to arbitrarily: each pattern buffer stores its own syntax, so it can be changed between regex compilations. */ @@ -6974,18 +6747,16 @@ reg_syntax_t re_syntax_options = RE_SYNTAX_EMACS; defined in regex.h. We return the old syntax. */ #ifdef __STDC__ -reg_syntax_t -re_set_syntax (reg_syntax_t syntax) +reg_syntax_t re_set_syntax(reg_syntax_t syntax) #else -reg_syntax_t -re_set_syntax (syntax) - reg_syntax_t syntax; +reg_syntax_t re_set_syntax(syntax) +reg_syntax_t syntax; #endif { - reg_syntax_t ret = re_syntax_options; + reg_syntax_t ret = re_syntax_options; - re_syntax_options = syntax; - return ret; + re_syntax_options = syntax; + return ret; } @@ -7004,127 +6775,116 @@ re_set_syntax (syntax) #ifdef __STDC__ void -re_set_registers (struct re_pattern_buffer *bufp, - struct re_registers *regs, - unsigned num_regs, - regoff_t * starts, regoff_t * ends) -#else -void -re_set_registers (bufp, regs, num_regs, starts, ends) - struct re_pattern_buffer *bufp; - struct re_registers *regs; - unsigned num_regs; - regoff_t * starts; - regoff_t * ends; -#endif -{ - if (num_regs) - { - bufp->regs_allocated = REGS_REALLOCATE; - regs->num_regs = num_regs; - regs->start = starts; - regs->end = ends; - } - else - { - bufp->regs_allocated = REGS_UNALLOCATED; - regs->num_regs = 0; - regs->start = regs->end = (regoff_t) 0; - } +re_set_registers(struct re_pattern_buffer *bufp, + struct re_registers *regs, + unsigned num_regs, regoff_t * starts, regoff_t * ends) +#else +void re_set_registers(bufp, regs, num_regs, starts, ends) +struct re_pattern_buffer *bufp; +struct re_registers *regs; +unsigned num_regs; +regoff_t *starts; +regoff_t *ends; +#endif +{ + if (num_regs) { + bufp->regs_allocated = REGS_REALLOCATE; + regs->num_regs = num_regs; + regs->start = starts; + regs->end = ends; + } else { + bufp->regs_allocated = REGS_UNALLOCATED; + regs->num_regs = 0; + regs->start = regs->end = (regoff_t) 0; + } } + - #ifdef __STDC__ -static int -cplx_se_sublist_len (struct rx_se_list * list) +static int cplx_se_sublist_len(struct rx_se_list *list) #else -static int -cplx_se_sublist_len (list) - struct rx_se_list * list; +static int cplx_se_sublist_len(list) +struct rx_se_list *list; #endif { - int x = 0; - while (list) - { - if ((long)list->car >= 0) - ++x; - list = list->cdr; - } - return x; + int x = 0; + + while (list) { + if ((long) list->car >= 0) + ++x; + list = list->cdr; + } + return x; } /* For rx->se_list_cmp */ #ifdef __STDC__ -static int -posix_se_list_order (struct rx * rx, - struct rx_se_list * a, struct rx_se_list * b) -#else -static int -posix_se_list_order (rx, a, b) - struct rx * rx; - struct rx_se_list * a; - struct rx_se_list * b; -#endif -{ - int al = cplx_se_sublist_len (a); - int bl = cplx_se_sublist_len (b); - - if (!al && !bl) - return ((a == b) - ? 0 - : ((a < b) ? -1 : 1)); - - else if (!al) - return -1; - - else if (!bl) - return 1; - - else - { - rx_side_effect * av = ((rx_side_effect *) - alloca (sizeof (rx_side_effect) * (al + 1))); - rx_side_effect * bv = ((rx_side_effect *) - alloca (sizeof (rx_side_effect) * (bl + 1))); - struct rx_se_list * ap = a; - struct rx_se_list * bp = b; - int ai, bi; - - for (ai = al - 1; ai >= 0; --ai) - { - while ((long)ap->car < 0) - ap = ap->cdr; - av[ai] = ap->car; - ap = ap->cdr; - } - av[al] = (rx_side_effect)-2; - for (bi = bl - 1; bi >= 0; --bi) - { - while ((long)bp->car < 0) - bp = bp->cdr; - bv[bi] = bp->car; - bp = bp->cdr; - } - bv[bl] = (rx_side_effect)-1; +static int +posix_se_list_order(struct rx *rx, + struct rx_se_list *a, struct rx_se_list *b) +#else +static int posix_se_list_order(rx, a, b) +struct rx *rx; +struct rx_se_list *a; +struct rx_se_list *b; +#endif +{ + int al = cplx_se_sublist_len(a); + int bl = cplx_se_sublist_len(b); + + if (!al && !bl) + return ((a == b) + ? 0 : ((a < b) ? -1 : 1)); + + else if (!al) + return -1; + + else if (!bl) + return 1; + + else { + rx_side_effect *av = ((rx_side_effect *) + alloca(sizeof(rx_side_effect) * (al + 1))); + rx_side_effect *bv = ((rx_side_effect *) + alloca(sizeof(rx_side_effect) * (bl + 1))); + struct rx_se_list *ap = a; + struct rx_se_list *bp = b; + int ai, bi; + + for (ai = al - 1; ai >= 0; --ai) { + while ((long) ap->car < 0) + ap = ap->cdr; + av[ai] = ap->car; + ap = ap->cdr; + } + av[al] = (rx_side_effect) - 2; + for (bi = bl - 1; bi >= 0; --bi) { + while ((long) bp->car < 0) + bp = bp->cdr; + bv[bi] = bp->car; + bp = bp->cdr; + } + bv[bl] = (rx_side_effect) - 1; - { - int ret; - int x = 0; - while (av[x] == bv[x]) - ++x; - ret = (((unsigned *)(av[x]) < (unsigned *)(bv[x])) ? -1 : 1); - return ret; - } - } + { + int ret; + int x = 0; + + while (av[x] == bv[x]) + ++x; + ret = (((unsigned *) (av[x]) < (unsigned *) (bv[x])) ? -1 : 1); + return ret; + } + } } + - /* re_compile_pattern is the GNU regular expression compiler: it compiles PATTERN (of length SIZE) and puts the result in RXB. Returns 0 if the pattern was valid, otherwise an error string. @@ -7135,66 +6895,62 @@ posix_se_list_order (rx, a, b) We call rx_compile to do the actual compilation. */ #ifdef __STDC__ -__const__ char * -re_compile_pattern (__const__ char *pattern, - int length, - struct re_pattern_buffer * rxb) +__const__ char *re_compile_pattern(__const__ char *pattern, + int length, + struct re_pattern_buffer *rxb) #else -__const__ char * -re_compile_pattern (pattern, length, rxb) - __const__ char *pattern; - int length; - struct re_pattern_buffer * rxb; +__const__ char *re_compile_pattern(pattern, length, rxb) +__const__ char *pattern; +int length; +struct re_pattern_buffer *rxb; #endif { - reg_errcode_t ret; + reg_errcode_t ret; + + /* GNU code is written to assume at least RE_NREGS registers will be set + (and at least one extra will be -1). */ + rxb->regs_allocated = REGS_UNALLOCATED; - /* GNU code is written to assume at least RE_NREGS registers will be set - (and at least one extra will be -1). */ - rxb->regs_allocated = REGS_UNALLOCATED; + /* And GNU code determines whether or not to get register information + by passing null for the REGS argument to re_match, etc., not by + setting no_sub. */ + rxb->no_sub = 0; - /* And GNU code determines whether or not to get register information - by passing null for the REGS argument to re_match, etc., not by - setting no_sub. */ - rxb->no_sub = 0; + rxb->rx.local_cset_size = 256; - rxb->rx.local_cset_size = 256; + /* Match anchors at newline. */ + rxb->newline_anchor = 1; - /* Match anchors at newline. */ - rxb->newline_anchor = 1; - - rxb->re_nsub = 0; - rxb->start = 0; - rxb->se_params = 0; - rxb->rx.nodec = 0; - rxb->rx.epsnodec = 0; - rxb->rx.instruction_table = 0; - rxb->rx.nfa_states = 0; - rxb->rx.se_list_cmp = posix_se_list_order; - rxb->rx.start_set = 0; + rxb->re_nsub = 0; + rxb->start = 0; + rxb->se_params = 0; + rxb->rx.nodec = 0; + rxb->rx.epsnodec = 0; + rxb->rx.instruction_table = 0; + rxb->rx.nfa_states = 0; + rxb->rx.se_list_cmp = posix_se_list_order; + rxb->rx.start_set = 0; - ret = rx_compile (pattern, length, re_syntax_options, rxb); - alloca (0); - return rx_error_msg[(int) ret]; + ret = rx_compile(pattern, length, re_syntax_options, rxb); + alloca(0); + return rx_error_msg[(int) ret]; } #ifdef __STDC__ -int -re_compile_fastmap (struct re_pattern_buffer * rxb) +int re_compile_fastmap(struct re_pattern_buffer *rxb) #else -int -re_compile_fastmap (rxb) - struct re_pattern_buffer * rxb; +int re_compile_fastmap(rxb) +struct re_pattern_buffer *rxb; #endif { - rx_blow_up_fastmap (rxb); - return 0; + rx_blow_up_fastmap(rxb); + return 0; } + - /* Entry points compatible with 4.2 BSD regex library. We don't define them if this is an Emacs or POSIX compilation. */ @@ -7204,73 +6960,69 @@ re_compile_fastmap (rxb) static struct re_pattern_buffer rx_comp_buf; #ifdef __STDC__ -char * -re_comp (__const__ char *s) +char *re_comp(__const__ char *s) #else -char * -re_comp (s) - __const__ char *s; +char *re_comp(s) +__const__ char *s; #endif { - reg_errcode_t ret; + reg_errcode_t ret; - if (!s || (*s == '\0')) - { - if (!rx_comp_buf.buffer) - return "No previous regular expression"; - return 0; - } + if (!s || (*s == '\0')) { + if (!rx_comp_buf.buffer) + return "No previous regular expression"; + return 0; + } - if (!rx_comp_buf.fastmap) - { - rx_comp_buf.fastmap = (char *) malloc (1 << CHARBITS); - if (!rx_comp_buf.fastmap) - return "Memory exhausted"; - } + if (!rx_comp_buf.fastmap) { + rx_comp_buf.fastmap = (char *) malloc(1 << CHARBITS); + if (!rx_comp_buf.fastmap) + return "Memory exhausted"; + } - /* Since `rx_exec' always passes NULL for the `regs' argument, we - don't need to initialize the pattern buffer fields which affect it. */ + /* Since `rx_exec' always passes NULL for the `regs' argument, we + don't need to initialize the pattern buffer fields which affect it. */ - /* Match anchors at newlines. */ - rx_comp_buf.newline_anchor = 1; + /* Match anchors at newlines. */ + rx_comp_buf.newline_anchor = 1; - rx_comp_buf.re_nsub = 0; - rx_comp_buf.start = 0; - rx_comp_buf.se_params = 0; - rx_comp_buf.rx.nodec = 0; - rx_comp_buf.rx.epsnodec = 0; - rx_comp_buf.rx.instruction_table = 0; - rx_comp_buf.rx.nfa_states = 0; - rx_comp_buf.rx.start = 0; - rx_comp_buf.rx.se_list_cmp = posix_se_list_order; - rx_comp_buf.rx.start_set = 0; - rx_comp_buf.rx.local_cset_size = 256; + rx_comp_buf.re_nsub = 0; + rx_comp_buf.start = 0; + rx_comp_buf.se_params = 0; + rx_comp_buf.rx.nodec = 0; + rx_comp_buf.rx.epsnodec = 0; + rx_comp_buf.rx.instruction_table = 0; + rx_comp_buf.rx.nfa_states = 0; + rx_comp_buf.rx.start = 0; + rx_comp_buf.rx.se_list_cmp = posix_se_list_order; + rx_comp_buf.rx.start_set = 0; + rx_comp_buf.rx.local_cset_size = 256; - ret = rx_compile (s, strlen (s), re_syntax_options, &rx_comp_buf); - alloca (0); + ret = rx_compile(s, strlen(s), re_syntax_options, &rx_comp_buf); + alloca(0); - /* Yes, we're discarding `__const__' here. */ - return (char *) rx_error_msg[(int) ret]; + /* Yes, we're discarding `__const__' here. */ + return (char *) rx_error_msg[(int) ret]; } #ifdef __STDC__ -int -re_exec (__const__ char *s) +int re_exec(__const__ char *s) #else -int -re_exec (s) - __const__ char *s; +int re_exec(s) +__const__ char *s; #endif { - __const__ int len = strlen (s); - return - 0 <= re_search (&rx_comp_buf, s, len, 0, len, (struct re_registers *) 0); -} -#endif /* not emacs and not _POSIX_SOURCE */ + __const__ int len = strlen(s); + return + 0 <= re_search(&rx_comp_buf, s, len, 0, len, + (struct re_registers *) 0); +} +#endif /* not emacs and not _POSIX_SOURCE */ + /* POSIX.2 functions. Don't define these for Emacs. */ #if !defined(emacs) @@ -7311,78 +7063,76 @@ re_exec (s) #ifdef __STDC__ -int -regcomp (regex_t * preg, __const__ char * pattern, int cflags) +int regcomp(regex_t * preg, __const__ char *pattern, int cflags) #else -int -regcomp (preg, pattern, cflags) - regex_t * preg; - __const__ char * pattern; - int cflags; -#endif -{ - reg_errcode_t ret; - unsigned syntax - = cflags & REG_EXTENDED ? RE_SYNTAX_POSIX_EXTENDED : RE_SYNTAX_POSIX_BASIC; - - /* regex_compile will allocate the space for the compiled pattern. */ - preg->buffer = 0; - preg->allocated = 0; - preg->fastmap = malloc (256); - if (!preg->fastmap) - return REG_ESPACE; - preg->fastmap_accurate = 0; - - if (cflags & REG_ICASE) - { - unsigned i; - - preg->translate = (unsigned char *) malloc (256); - if (!preg->translate) - return (int) REG_ESPACE; - - /* Map uppercase characters to corresponding lowercase ones. */ - for (i = 0; i < CHAR_SET_SIZE; i++) - preg->translate[i] = isupper (i) ? tolower (i) : i; - } - else - preg->translate = 0; - - /* If REG_NEWLINE is set, newlines are treated differently. */ - if (cflags & REG_NEWLINE) - { /* REG_NEWLINE implies neither . nor [^...] match newline. */ - syntax &= ~RE_DOT_NEWLINE; - syntax |= RE_HAT_LISTS_NOT_NEWLINE; - /* It also changes the matching behavior. */ - preg->newline_anchor = 1; - } - else - preg->newline_anchor = 0; - - preg->no_sub = !!(cflags & REG_NOSUB); - - /* POSIX says a null character in the pattern terminates it, so we - can use strlen here in compiling the pattern. */ - preg->re_nsub = 0; - preg->start = 0; - preg->se_params = 0; - preg->syntax_parens = 0; - preg->rx.nodec = 0; - preg->rx.epsnodec = 0; - preg->rx.instruction_table = 0; - preg->rx.nfa_states = 0; - preg->rx.local_cset_size = 256; - preg->rx.start = 0; - preg->rx.se_list_cmp = posix_se_list_order; - preg->rx.start_set = 0; - ret = rx_compile (pattern, strlen (pattern), syntax, preg); - alloca (0); +int regcomp(preg, pattern, cflags) +regex_t *preg; +__const__ char *pattern; +int cflags; +#endif +{ + reg_errcode_t ret; + unsigned syntax - /* POSIX doesn't distinguish between an unmatched open-group and an - unmatched close-group: both are REG_EPAREN. */ - if (ret == REG_ERPAREN) ret = REG_EPAREN; + = + cflags & REG_EXTENDED ? RE_SYNTAX_POSIX_EXTENDED : + RE_SYNTAX_POSIX_BASIC; - return (int) ret; + /* regex_compile will allocate the space for the compiled pattern. */ + preg->buffer = 0; + preg->allocated = 0; + preg->fastmap = malloc(256); + if (!preg->fastmap) + return REG_ESPACE; + preg->fastmap_accurate = 0; + + if (cflags & REG_ICASE) { + unsigned i; + + preg->translate = (unsigned char *) malloc(256); + if (!preg->translate) + return (int) REG_ESPACE; + + /* Map uppercase characters to corresponding lowercase ones. */ + for (i = 0; i < CHAR_SET_SIZE; i++) + preg->translate[i] = isupper(i) ? tolower(i) : i; + } else + preg->translate = 0; + + /* If REG_NEWLINE is set, newlines are treated differently. */ + if (cflags & REG_NEWLINE) { /* REG_NEWLINE implies neither . nor [^...] match newline. */ + syntax &= ~RE_DOT_NEWLINE; + syntax |= RE_HAT_LISTS_NOT_NEWLINE; + /* It also changes the matching behavior. */ + preg->newline_anchor = 1; + } else + preg->newline_anchor = 0; + + preg->no_sub = !!(cflags & REG_NOSUB); + + /* POSIX says a null character in the pattern terminates it, so we + can use strlen here in compiling the pattern. */ + preg->re_nsub = 0; + preg->start = 0; + preg->se_params = 0; + preg->syntax_parens = 0; + preg->rx.nodec = 0; + preg->rx.epsnodec = 0; + preg->rx.instruction_table = 0; + preg->rx.nfa_states = 0; + preg->rx.local_cset_size = 256; + preg->rx.start = 0; + preg->rx.se_list_cmp = posix_se_list_order; + preg->rx.start_set = 0; + ret = rx_compile(pattern, strlen(pattern), syntax, preg); + alloca(0); + + /* POSIX doesn't distinguish between an unmatched open-group and an + unmatched close-group: both are REG_EPAREN. */ + if (ret == REG_ERPAREN) + ret = REG_EPAREN; + + return (int) ret; } @@ -7402,73 +7152,66 @@ regcomp (preg, pattern, cflags) #ifdef __STDC__ int -regexec (__const__ regex_t *preg, __const__ char *string, - size_t nmatch, regmatch_t pmatch[], - int eflags) +regexec(__const__ regex_t * preg, __const__ char *string, + size_t nmatch, regmatch_t pmatch[], int eflags) #else -int -regexec (preg, string, nmatch, pmatch, eflags) - __const__ regex_t *preg; - __const__ char *string; - size_t nmatch; - regmatch_t pmatch[]; - int eflags; -#endif -{ - int ret; - struct re_registers regs; - regex_t private_preg; - int len = strlen (string); - boolean want_reg_info = !preg->no_sub && nmatch > 0; - - private_preg = *preg; - - private_preg.not_bol = !!(eflags & REG_NOTBOL); - private_preg.not_eol = !!(eflags & REG_NOTEOL); - - /* The user has told us exactly how many registers to return - * information about, via `nmatch'. We have to pass that on to the - * matching routines. - */ - private_preg.regs_allocated = REGS_FIXED; - - if (want_reg_info) - { - regs.num_regs = nmatch; - regs.start = (( regoff_t *) malloc ((nmatch) * sizeof ( regoff_t))); - regs.end = (( regoff_t *) malloc ((nmatch) * sizeof ( regoff_t))); - if (regs.start == 0 || regs.end == 0) - return (int) REG_NOMATCH; - } +int regexec(preg, string, nmatch, pmatch, eflags) +__const__ regex_t *preg; +__const__ char *string; +size_t nmatch; +regmatch_t pmatch[]; +int eflags; +#endif +{ + int ret; + struct re_registers regs; + regex_t private_preg; + int len = strlen(string); + boolean want_reg_info = !preg->no_sub && nmatch > 0; - /* Perform the searching operation. */ - ret = re_search (&private_preg, - string, len, - /* start: */ 0, - /* range: */ len, - want_reg_info ? ®s : (struct re_registers *) 0); - - /* Copy the register information to the POSIX structure. */ - if (want_reg_info) - { - if (ret >= 0) - { - unsigned r; - - for (r = 0; r < nmatch; r++) - { - pmatch[r].rm_so = regs.start[r]; - pmatch[r].rm_eo = regs.end[r]; - } - } - - /* If we needed the temporary register info, free the space now. */ - free (regs.start); - free (regs.end); - } + private_preg = *preg; + + private_preg.not_bol = !!(eflags & REG_NOTBOL); + private_preg.not_eol = !!(eflags & REG_NOTEOL); - /* We want zero return to mean success, unlike `re_search'. */ - return ret >= 0 ? (int) REG_NOERROR : (int) REG_NOMATCH; + /* The user has told us exactly how many registers to return + * information about, via `nmatch'. We have to pass that on to the + * matching routines. + */ + private_preg.regs_allocated = REGS_FIXED; + + if (want_reg_info) { + regs.num_regs = nmatch; + regs.start = ((regoff_t *) malloc((nmatch) * sizeof(regoff_t))); + regs.end = ((regoff_t *) malloc((nmatch) * sizeof(regoff_t))); + if (regs.start == 0 || regs.end == 0) + return (int) REG_NOMATCH; + } + + /* Perform the searching operation. */ + ret = re_search(&private_preg, string, len, + /* start: */ 0, + /* range: */ len, + want_reg_info ? ®s : (struct re_registers *) 0); + + /* Copy the register information to the POSIX structure. */ + if (want_reg_info) { + if (ret >= 0) { + unsigned r; + + for (r = 0; r < nmatch; r++) { + pmatch[r].rm_so = regs.start[r]; + pmatch[r].rm_eo = regs.end[r]; + } + } + + /* If we needed the temporary register info, free the space now. */ + free(regs.start); + free(regs.end); + } + + /* We want zero return to mean success, unlike `re_search'. */ + return ret >= 0 ? (int) REG_NOERROR : (int) REG_NOMATCH; } @@ -7477,60 +7220,54 @@ regexec (preg, string, nmatch, pmatch, eflags) #ifdef __STDC__ size_t -regerror (int errcode, __const__ regex_t *preg, - char *errbuf, size_t errbuf_size) -#else -size_t -regerror (errcode, preg, errbuf, errbuf_size) - int errcode; - __const__ regex_t *preg; - char *errbuf; - size_t errbuf_size; -#endif -{ - __const__ char *msg - = rx_error_msg[errcode] == 0 ? "Success" : rx_error_msg[errcode]; - size_t msg_size = strlen (msg) + 1; /* Includes the 0. */ - - if (errbuf_size != 0) - { - if (msg_size > errbuf_size) - { - strncpy (errbuf, msg, errbuf_size - 1); - errbuf[errbuf_size - 1] = 0; - } - else - strcpy (errbuf, msg); - } +regerror(int errcode, __const__ regex_t * preg, + char *errbuf, size_t errbuf_size) +#else +size_t regerror(errcode, preg, errbuf, errbuf_size) +int errcode; +__const__ regex_t *preg; +char *errbuf; +size_t errbuf_size; +#endif +{ + __const__ char *msg + = rx_error_msg[errcode] == 0 ? "Success" : rx_error_msg[errcode]; + size_t msg_size = strlen(msg) + 1; /* Includes the 0. */ + + if (errbuf_size != 0) { + if (msg_size > errbuf_size) { + strncpy(errbuf, msg, errbuf_size - 1); + errbuf[errbuf_size - 1] = 0; + } else + strcpy(errbuf, msg); + } - return msg_size; + return msg_size; } /* Free dynamically allocated space used by PREG. */ #ifdef __STDC__ -void -regfree (regex_t *preg) +void regfree(regex_t * preg) #else -void -regfree (preg) - regex_t *preg; +void regfree(preg) +regex_t *preg; #endif { - if (preg->buffer != 0) - free (preg->buffer); - preg->buffer = 0; - preg->allocated = 0; + if (preg->buffer != 0) + free(preg->buffer); + preg->buffer = 0; + preg->allocated = 0; - if (preg->fastmap != 0) - free (preg->fastmap); - preg->fastmap = 0; - preg->fastmap_accurate = 0; + if (preg->fastmap != 0) + free(preg->fastmap); + preg->fastmap = 0; + preg->fastmap_accurate = 0; - if (preg->translate != 0) - free (preg->translate); - preg->translate = 0; + if (preg->translate != 0) + free(preg->translate); + preg->translate = 0; } -#endif /* not emacs */ +#endif /* not emacs */ diff --git a/libc/misc/time/.indent.pro b/libc/misc/time/.indent.pro new file mode 100644 index 000000000..492ecf1c7 --- /dev/null +++ b/libc/misc/time/.indent.pro @@ -0,0 +1,33 @@ +--blank-lines-after-declarations +--blank-lines-after-procedures +--break-before-boolean-operator +--no-blank-lines-after-commas +--braces-on-if-line +--braces-on-struct-decl-line +--comment-indentation25 +--declaration-comment-column25 +--no-comment-delimiters-on-blank-lines +--cuddle-else +--continuation-indentation4 +--case-indentation0 +--else-endif-column33 +--space-after-cast +--line-comments-indentation0 +--declaration-indentation1 +--dont-format-first-column-comments +--dont-format-comments +--honour-newlines +--indent-level4 +/* changed from 0 to 4 */ +--parameter-indentation4 +--line-length78 /* changed from 75 */ +--continue-at-parentheses +--no-space-after-function-call-names +--dont-break-procedure-type +--dont-star-comments +--leave-optional-blank-lines +--dont-space-special-semicolon +--tab-size4 +/* additions by Mark */ +--case-brace-indentation0 +--leave-preprocessor-space diff --git a/libc/misc/time/Makefile b/libc/misc/time/Makefile index 14ccf5933..e3c2c26d6 100644 --- a/libc/misc/time/Makefile +++ b/libc/misc/time/Makefile @@ -27,13 +27,16 @@ LIBC=$(TOPDIR)libc.a CSRC=localtime.c gmtime.c asctime.c ctime.c asc_conv.c tm_conv.c mktime.c \ localtime_r.c gmtime_r.c asctime_r.c ctime_r.c COBJS=$(patsubst %.c,%.o, $(CSRC)) +OBJS=$(COBJS) -all: $(COBJS) $(LIBC) +all: $(OBJS) $(LIBC) -$(LIBC): $(COBJS) - $(AR) $(ARFLAGS) $(LIBC) $(COBJS) +$(LIBC): ar-target -$(COBJS): Makefile +ar-target: $(OBJS) + $(AR) $(ARFLAGS) $(LIBC) $(OBJS) + +$(OBJS): Makefile clean: rm -f *.[oa] *~ core diff --git a/libc/misc/time/asc_conv.c b/libc/misc/time/asc_conv.c index 78cbcdd8d..9f8c25844 100644 --- a/libc/misc/time/asc_conv.c +++ b/libc/misc/time/asc_conv.c @@ -6,44 +6,42 @@ */ -static void -hit(buf, val) -char * buf; +static void hit(buf, val) +char *buf; int val; { - *buf = '0' + val%10; + *buf = '0' + val % 10; } -void -__asctime(buffer, ptm) -register char * buffer; -struct tm * ptm; +void __asctime(buffer, ptm) +register char *buffer; +struct tm *ptm; { -static char days[] = "SunMonTueWedThuFriSat"; -static char mons[] = "JanFebMarAprMayJunJulAugSepOctNovDec"; - int year; - - /* 012345678901234567890123456 */ - strcpy(buffer, "Err Err .. ..:..:.. ....\n"); - if( (ptm->tm_wday >= 0) && (ptm->tm_wday <= 6) ) - memcpy(buffer, days+3*(ptm->tm_wday), 3); - - if( (ptm->tm_mon >= 0) && (ptm->tm_mon <= 11) ) - memcpy(buffer+4, mons+3*(ptm->tm_mon), 3); - - - hit(buffer+ 8, ptm->tm_mday/10); - hit(buffer+ 9, ptm->tm_mday ); - hit(buffer+11, ptm->tm_hour/10); - hit(buffer+12, ptm->tm_hour ); - hit(buffer+14, ptm->tm_min/10); - hit(buffer+15, ptm->tm_min ); - hit(buffer+17, ptm->tm_sec/10); - hit(buffer+18, ptm->tm_sec ); - - year = ptm->tm_year + 1900; - hit(buffer+20, year/1000); - hit(buffer+21, year/100); - hit(buffer+22, year/10); - hit(buffer+23, year); + static char days[] = "SunMonTueWedThuFriSat"; + static char mons[] = "JanFebMarAprMayJunJulAugSepOctNovDec"; + int year; + + /* 012345678901234567890123456 */ + strcpy(buffer, "Err Err .. ..:..:.. ....\n"); + if ((ptm->tm_wday >= 0) && (ptm->tm_wday <= 6)) + memcpy(buffer, days + 3 * (ptm->tm_wday), 3); + + if ((ptm->tm_mon >= 0) && (ptm->tm_mon <= 11)) + memcpy(buffer + 4, mons + 3 * (ptm->tm_mon), 3); + + + hit(buffer + 8, ptm->tm_mday / 10); + hit(buffer + 9, ptm->tm_mday); + hit(buffer + 11, ptm->tm_hour / 10); + hit(buffer + 12, ptm->tm_hour); + hit(buffer + 14, ptm->tm_min / 10); + hit(buffer + 15, ptm->tm_min); + hit(buffer + 17, ptm->tm_sec / 10); + hit(buffer + 18, ptm->tm_sec); + + year = ptm->tm_year + 1900; + hit(buffer + 20, year / 1000); + hit(buffer + 21, year / 100); + hit(buffer + 22, year / 10); + hit(buffer + 23, year); } diff --git a/libc/misc/time/asctime.c b/libc/misc/time/asctime.c index 08673cbcc..bfb1a13fc 100644 --- a/libc/misc/time/asctime.c +++ b/libc/misc/time/asctime.c @@ -3,13 +3,13 @@ extern void __asctime(); -char * -asctime(timeptr) -__const struct tm * timeptr; +char *asctime(timeptr) +__const struct tm *timeptr; { - static char timebuf[26]; + static char timebuf[26]; - if( timeptr == 0 ) return 0; - __asctime(timebuf, timeptr); - return timebuf; + if (timeptr == 0) + return 0; + __asctime(timebuf, timeptr); + return timebuf; } diff --git a/libc/misc/time/asctime_r.c b/libc/misc/time/asctime_r.c index ff2b6bfb3..823ab4f4d 100644 --- a/libc/misc/time/asctime_r.c +++ b/libc/misc/time/asctime_r.c @@ -3,13 +3,13 @@ extern void __asctime(); -char * -asctime_r(timeptr, buf) -__const struct tm * timeptr; -char * buf; +char *asctime_r(timeptr, buf) +__const struct tm *timeptr; +char *buf; { - if( timeptr == 0 ) return 0; - __asctime(buf, timeptr); - return buf; + if (timeptr == 0) + return 0; + __asctime(buf, timeptr); + return buf; } diff --git a/libc/misc/time/ctime.c b/libc/misc/time/ctime.c index 2f42cd3f3..ddcde6111 100644 --- a/libc/misc/time/ctime.c +++ b/libc/misc/time/ctime.c @@ -4,23 +4,22 @@ extern void __tm_conv(); extern void __asctime(); -char * -ctime(timep) -__const time_t * timep; +char *ctime(timep) +__const time_t *timep; { - static char cbuf[26]; - struct tm tmb; - struct timezone tz; - time_t offt; - - gettimeofday((void*)0, &tz); - - offt = -tz.tz_minuteswest*60L; - - /* tmb.tm_isdst = ? */ - __tm_conv(&tmb, timep, offt); - - __asctime(cbuf, &tmb); - - return cbuf; + static char cbuf[26]; + struct tm tmb; + struct timezone tz; + time_t offt; + + gettimeofday((void *) 0, &tz); + + offt = -tz.tz_minuteswest * 60L; + + /* tmb.tm_isdst = ? */ + __tm_conv(&tmb, timep, offt); + + __asctime(cbuf, &tmb); + + return cbuf; } diff --git a/libc/misc/time/ctime_r.c b/libc/misc/time/ctime_r.c index ac267b04e..85677b681 100644 --- a/libc/misc/time/ctime_r.c +++ b/libc/misc/time/ctime_r.c @@ -4,23 +4,22 @@ extern void __tm_conv(); extern void __asctime(); -char * -ctime_r(timep, buf) -__const time_t * timep; -char * buf; +char *ctime_r(timep, buf) +__const time_t *timep; +char *buf; { - struct tm tmb; - struct timezone tz; - time_t offt; - - gettimeofday((void*)0, &tz); - - offt = -tz.tz_minuteswest*60L; - - /* tmb.tm_isdst = ? */ - __tm_conv(&tmb, timep, offt); - - __asctime(buf, &tmb); - - return buf; + struct tm tmb; + struct timezone tz; + time_t offt; + + gettimeofday((void *) 0, &tz); + + offt = -tz.tz_minuteswest * 60L; + + /* tmb.tm_isdst = ? */ + __tm_conv(&tmb, timep, offt); + + __asctime(buf, &tmb); + + return buf; } diff --git a/libc/misc/time/gmtime.c b/libc/misc/time/gmtime.c index 69d539392..03cc9d56c 100644 --- a/libc/misc/time/gmtime.c +++ b/libc/misc/time/gmtime.c @@ -3,14 +3,12 @@ extern void __tm_conv(); -struct tm * -gmtime(timep) -__const time_t * timep; +struct tm *gmtime(timep) +__const time_t *timep; { - static struct tm tmb; + static struct tm tmb; - __tm_conv(&tmb, timep, 0L); + __tm_conv(&tmb, timep, 0L); - return &tmb; + return &tmb; } - diff --git a/libc/misc/time/gmtime_r.c b/libc/misc/time/gmtime_r.c index 00c41b349..a1502e275 100644 --- a/libc/misc/time/gmtime_r.c +++ b/libc/misc/time/gmtime_r.c @@ -3,12 +3,10 @@ extern void __tm_conv(); -struct tm * -gmtime_r(timep, tp) -__const time_t * timep; -struct tm * tp; +struct tm *gmtime_r(timep, tp) +__const time_t *timep; +struct tm *tp; { - __tm_conv(tp, timep, 0L); - return tp; + __tm_conv(tp, timep, 0L); + return tp; } - diff --git a/libc/misc/time/localtime.c b/libc/misc/time/localtime.c index 9d9b46d15..1f538ab7a 100644 --- a/libc/misc/time/localtime.c +++ b/libc/misc/time/localtime.c @@ -3,20 +3,19 @@ extern void __tm_conv(); -struct tm * -localtime(timep) -__const time_t * timep; +struct tm *localtime(timep) +__const time_t *timep; { - static struct tm tmb; - struct timezone tz; - time_t offt; + static struct tm tmb; + struct timezone tz; + time_t offt; - gettimeofday((void*)0, &tz); + gettimeofday((void *) 0, &tz); - offt = -tz.tz_minuteswest*60L; + offt = -tz.tz_minuteswest * 60L; - /* tmb.tm_isdst = ? */ - __tm_conv(&tmb, timep, offt); + /* tmb.tm_isdst = ? */ + __tm_conv(&tmb, timep, offt); - return &tmb; + return &tmb; } diff --git a/libc/misc/time/localtime_r.c b/libc/misc/time/localtime_r.c index fe024670c..d084f2b1e 100644 --- a/libc/misc/time/localtime_r.c +++ b/libc/misc/time/localtime_r.c @@ -3,20 +3,19 @@ extern void __tm_conv(); -struct tm * -localtime_r(timep, tp) -__const time_t * timep; -struct tm * tp; +struct tm *localtime_r(timep, tp) +__const time_t *timep; +struct tm *tp; { - struct timezone tz; - time_t offt; + struct timezone tz; + time_t offt; - gettimeofday((void*)0, &tz); + gettimeofday((void *) 0, &tz); - offt = -tz.tz_minuteswest*60L; + offt = -tz.tz_minuteswest * 60L; - /* tmb.tm_isdst = ? */ - __tm_conv(tp, timep, offt); + /* tmb.tm_isdst = ? */ + __tm_conv(tp, timep, offt); - return tp; + return tp; } diff --git a/libc/misc/time/mktime.c b/libc/misc/time/mktime.c index 7f7aabcd7..d6ee2ed42 100644 --- a/libc/misc/time/mktime.c +++ b/libc/misc/time/mktime.c @@ -10,7 +10,7 @@ #define LEAP_SECONDS_POSSIBLE 1 #endif -#include /* Some systems define `time_t' here. */ +#include /* Some systems define `time_t' here. */ #include #if __STDC__ || __GNU_LIBRARY__ || STDC_HEADERS @@ -24,15 +24,15 @@ #endif /* Make it work even if the system's libc has its own mktime routine. */ #define mktime my_mktime -#endif /* DEBUG */ +#endif /* DEBUG */ #ifndef __P #if defined (__GNUC__) || (defined (__STDC__) && __STDC__) #define __P(args) args #else #define __P(args) () -#endif /* GCC. */ -#endif /* Not __P. */ +#endif /* GCC. */ +#endif /* Not __P. */ #ifndef CHAR_BIT #define CHAR_BIT 8 @@ -64,47 +64,47 @@ #endif /* How many days come before each month (0-12). */ -const unsigned short int __mon_yday[2][13] = - { - /* Normal years. */ - { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }, - /* Leap years. */ - { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 } - }; - -static time_t ydhms_tm_diff __P ((int, int, int, int, int, const struct tm *)); -time_t __mktime_internal __P ((struct tm *, - struct tm *(*) (const time_t *, struct tm *), - time_t *)); +const unsigned short int __mon_yday[2][13] = { + /* Normal years. */ + {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365}, + /* Leap years. */ + {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366} +}; + +static time_t ydhms_tm_diff + +__P((int, int, int, int, int, const struct tm *)); +time_t __mktime_internal +__P((struct tm *, struct tm * (*)(const time_t *, struct tm *), time_t *)); /* Yield the difference between (YEAR-YDAY HOUR:MIN:SEC) and (*TP), measured in seconds, ignoring leap seconds. YEAR uses the same numbering as TM->tm_year. All values are in range, except possibly YEAR. If overflow occurs, yield the low order bits of the correct answer. */ -static time_t -ydhms_tm_diff (year, yday, hour, min, sec, tp) - int year, yday, hour, min, sec; - const struct tm *tp; +static time_t ydhms_tm_diff(year, yday, hour, min, sec, tp) +int year, yday, hour, min, sec; +const struct tm *tp; { - /* Compute intervening leap days correctly even if year is negative. - Take care to avoid int overflow. time_t overflow is OK, since - only the low order bits of the correct time_t answer are needed. - Don't convert to time_t until after all divisions are done, since - time_t might be unsigned. */ - int a4 = (year >> 2) + (TM_YEAR_BASE >> 2) - ! (year & 3); - int b4 = (tp->tm_year >> 2) + (TM_YEAR_BASE >> 2) - ! (tp->tm_year & 3); - int a100 = a4 / 25 - (a4 % 25 < 0); - int b100 = b4 / 25 - (b4 % 25 < 0); - int a400 = a100 >> 2; - int b400 = b100 >> 2; - int intervening_leap_days = (a4 - b4) - (a100 - b100) + (a400 - b400); - time_t years = year - (time_t) tp->tm_year; - time_t days = (365 * years + intervening_leap_days - + (yday - tp->tm_yday)); - return (60 * (60 * (24 * days + (hour - tp->tm_hour)) - + (min - tp->tm_min)) - + (sec - tp->tm_sec)); + /* Compute intervening leap days correctly even if year is negative. + Take care to avoid int overflow. time_t overflow is OK, since + only the low order bits of the correct time_t answer are needed. + Don't convert to time_t until after all divisions are done, since + time_t might be unsigned. */ + int a4 = (year >> 2) + (TM_YEAR_BASE >> 2) - !(year & 3); + int b4 = (tp->tm_year >> 2) + (TM_YEAR_BASE >> 2) - !(tp->tm_year & 3); + int a100 = a4 / 25 - (a4 % 25 < 0); + int b100 = b4 / 25 - (b4 % 25 < 0); + int a400 = a100 >> 2; + int b400 = b100 >> 2; + int intervening_leap_days = (a4 - b4) - (a100 - b100) + (a400 - b400); + time_t years = year - (time_t) tp->tm_year; + time_t days = (365 * years + intervening_leap_days + + + (yday - tp->tm_yday)); + return (60 * (60 * (24 * days + (hour - tp->tm_hour)) + + (min - tp->tm_min)) + + (sec - tp->tm_sec)); } @@ -112,18 +112,17 @@ static time_t localtime_offset; /* Convert *TP to a time_t value. */ -time_t -mktime (tp) - struct tm *tp; +time_t mktime(tp) +struct tm *tp; { #ifdef _LIBC - /* POSIX.1 8.1.1 requires that whenever mktime() is called, the - time zone names contained in the external variable `tzname' shall - be set as if the tzset() function had been called. */ - __tzset (); + /* POSIX.1 8.1.1 requires that whenever mktime() is called, the + time zone names contained in the external variable `tzname' shall + be set as if the tzset() function had been called. */ + __tzset(); #endif - return __mktime_internal (tp, localtime_r, &localtime_offset); + return __mktime_internal(tp, localtime_r, &localtime_offset); } /* Convert *TP to a time_t value, inverting @@ -131,129 +130,126 @@ mktime (tp) Use *OFFSET to keep track of a guess at the offset of the result, compared to what the result would be for UTC without leap seconds. If *OFFSET's guess is correct, only one CONVERT call is needed. */ -time_t -__mktime_internal (tp, convert, offset) - struct tm *tp; - struct tm *(*convert) __P ((const time_t *, struct tm *)); - time_t *offset; +time_t __mktime_internal(tp, convert, offset) +struct tm *tp; +struct tm *(*convert) __P((const time_t *, struct tm *)); +time_t *offset; { - time_t t, dt, t0; - struct tm tm; - - /* The maximum number of probes (calls to CONVERT) should be enough - to handle any combinations of time zone rule changes, solar time, - and leap seconds. Posix.1 prohibits leap seconds, but some hosts - have them anyway. */ - int remaining_probes = 4; - - /* Time requested. Copy it in case CONVERT modifies *TP; this can - occur if TP is localtime's returned value and CONVERT is localtime. */ - int sec = tp->tm_sec; - int min = tp->tm_min; - int hour = tp->tm_hour; - int mday = tp->tm_mday; - int mon = tp->tm_mon; - int year_requested = tp->tm_year; - int isdst = tp->tm_isdst; - - /* Ensure that mon is in range, and set year accordingly. */ - int mon_remainder = mon % 12; - int negative_mon_remainder = mon_remainder < 0; - int mon_years = mon / 12 - negative_mon_remainder; - int year = year_requested + mon_years; - - /* The other values need not be in range: - the remaining code handles minor overflows correctly, - assuming int and time_t arithmetic wraps around. - Major overflows are caught at the end. */ - - /* Calculate day of year from year, month, and day of month. - The result need not be in range. */ - int yday = ((__mon_yday[__isleap (year + TM_YEAR_BASE)] - [mon_remainder + 12 * negative_mon_remainder]) - + mday - 1); + time_t t, dt, t0; + struct tm tm; + + /* The maximum number of probes (calls to CONVERT) should be enough + to handle any combinations of time zone rule changes, solar time, + and leap seconds. Posix.1 prohibits leap seconds, but some hosts + have them anyway. */ + int remaining_probes = 4; + + /* Time requested. Copy it in case CONVERT modifies *TP; this can + occur if TP is localtime's returned value and CONVERT is localtime. */ + int sec = tp->tm_sec; + int min = tp->tm_min; + int hour = tp->tm_hour; + int mday = tp->tm_mday; + int mon = tp->tm_mon; + int year_requested = tp->tm_year; + int isdst = tp->tm_isdst; + + /* Ensure that mon is in range, and set year accordingly. */ + int mon_remainder = mon % 12; + int negative_mon_remainder = mon_remainder < 0; + int mon_years = mon / 12 - negative_mon_remainder; + int year = year_requested + mon_years; + + /* The other values need not be in range: + the remaining code handles minor overflows correctly, + assuming int and time_t arithmetic wraps around. + Major overflows are caught at the end. */ + + /* Calculate day of year from year, month, and day of month. + The result need not be in range. */ + int yday = ((__mon_yday[__isleap(year + TM_YEAR_BASE)] + [mon_remainder + 12 * negative_mon_remainder]) + + mday - 1); #if LEAP_SECONDS_POSSIBLE - /* Handle out-of-range seconds specially, - since ydhms_tm_diff assumes every minute has 60 seconds. */ - int sec_requested = sec; - if (sec < 0) - sec = 0; - if (59 < sec) - sec = 59; + /* Handle out-of-range seconds specially, + since ydhms_tm_diff assumes every minute has 60 seconds. */ + int sec_requested = sec; + + if (sec < 0) + sec = 0; + if (59 < sec) + sec = 59; #endif - /* Invert CONVERT by probing. First assume the same offset as last time. - Then repeatedly use the error to improve the guess. */ - - tm.tm_year = EPOCH_YEAR - TM_YEAR_BASE; - tm.tm_yday = tm.tm_hour = tm.tm_min = tm.tm_sec = 0; - t0 = ydhms_tm_diff (year, yday, hour, min, sec, &tm); - - for (t = t0 + *offset; - (dt = ydhms_tm_diff (year, yday, hour, min, sec, (*convert) (&t, &tm))); - t += dt) - if (--remaining_probes == 0) - return -1; - - /* Check whether tm.tm_isdst has the requested value, if any. */ - if (0 <= isdst && 0 <= tm.tm_isdst) - { - int dst_diff = (isdst != 0) - (tm.tm_isdst != 0); - if (dst_diff) - { - /* Move two hours in the direction indicated by the disagreement, - probe some more, and switch to a new time if found. - The largest known fallback due to daylight savings is two hours: - once, in Newfoundland, 1988-10-30 02:00 -> 00:00. */ - time_t ot = t - 2 * 60 * 60 * dst_diff; - while (--remaining_probes != 0) - { - struct tm otm; - if (! (dt = ydhms_tm_diff (year, yday, hour, min, sec, - (*convert) (&ot, &otm)))) - { - t = ot; - tm = otm; - break; - } - if ((ot += dt) == t) - break; /* Avoid a redundant probe. */ - } - } - } - - *offset = t - t0; + /* Invert CONVERT by probing. First assume the same offset as last time. + Then repeatedly use the error to improve the guess. */ + + tm.tm_year = EPOCH_YEAR - TM_YEAR_BASE; + tm.tm_yday = tm.tm_hour = tm.tm_min = tm.tm_sec = 0; + t0 = ydhms_tm_diff(year, yday, hour, min, sec, &tm); + + for (t = t0 + *offset; + (dt = + ydhms_tm_diff(year, yday, hour, min, sec, (*convert) (&t, &tm))); + t += dt) + if (--remaining_probes == 0) + return -1; + + /* Check whether tm.tm_isdst has the requested value, if any. */ + if (0 <= isdst && 0 <= tm.tm_isdst) { + int dst_diff = (isdst != 0) - (tm.tm_isdst != 0); + + if (dst_diff) { + /* Move two hours in the direction indicated by the disagreement, + probe some more, and switch to a new time if found. + The largest known fallback due to daylight savings is two hours: + once, in Newfoundland, 1988-10-30 02:00 -> 00:00. */ + time_t ot = t - 2 * 60 * 60 * dst_diff; + + while (--remaining_probes != 0) { + struct tm otm; + + if (!(dt = ydhms_tm_diff(year, yday, hour, min, sec, + (*convert) (&ot, &otm)))) { + t = ot; + tm = otm; + break; + } + if ((ot += dt) == t) + break; /* Avoid a redundant probe. */ + } + } + } + + *offset = t - t0; #if LEAP_SECONDS_POSSIBLE - if (sec_requested != tm.tm_sec) - { - /* Adjust time to reflect the tm_sec requested, not the normalized value. - Also, repair any damage from a false match due to a leap second. */ - t += sec_requested - sec + (sec == 0 && tm.tm_sec == 60); - (*convert) (&t, &tm); - } + if (sec_requested != tm.tm_sec) { + /* Adjust time to reflect the tm_sec requested, not the normalized value. + Also, repair any damage from a false match due to a leap second. */ + t += sec_requested - sec + (sec == 0 && tm.tm_sec == 60); + (*convert) (&t, &tm); + } #endif #if 0 - if (TIME_T_MAX / INT_MAX / 366 / 24 / 60 / 60 < 3) - { - /* time_t isn't large enough to rule out overflows in ydhms_tm_diff, - so check for major overflows. A gross check suffices, - since if t has overflowed, it is off by a multiple of - TIME_T_MAX - TIME_T_MIN + 1. So ignore any component of - the difference that is bounded by a small value. */ - - double dyear = (double) year_requested + mon_years - tm.tm_year; - double dday = 366 * dyear + mday; - double dsec = 60 * (60 * (24 * dday + hour) + min) + sec_requested; - - if (TIME_T_MAX / 3 - TIME_T_MIN / 3 < (dsec < 0 ? - dsec : dsec)) - return -1; - } + if (TIME_T_MAX / INT_MAX / 366 / 24 / 60 / 60 < 3) { + /* time_t isn't large enough to rule out overflows in ydhms_tm_diff, + so check for major overflows. A gross check suffices, + since if t has overflowed, it is off by a multiple of + TIME_T_MAX - TIME_T_MIN + 1. So ignore any component of + the difference that is bounded by a small value. */ + + double dyear = (double) year_requested + mon_years - tm.tm_year; + double dday = 366 * dyear + mday; + double dsec = 60 * (60 * (24 * dday + hour) + min) + sec_requested; + + if (TIME_T_MAX / 3 - TIME_T_MIN / 3 < (dsec < 0 ? -dsec : dsec)) + return -1; + } #endif - *tp = tm; - return t; + *tp = tm; + return t; } - diff --git a/libc/misc/time/tm_conv.c b/libc/misc/time/tm_conv.c index ec0073294..d345f7528 100644 --- a/libc/misc/time/tm_conv.c +++ b/libc/misc/time/tm_conv.c @@ -5,63 +5,64 @@ /* This is a translation from ALGOL in Collected Algorithms of CACM. */ /* Copied from Algorithm 199, Author: Robert G. Tantzen */ -void -__tm_conv(tmbuf, timep, offset) +void __tm_conv(tmbuf, timep, offset) struct tm *tmbuf; time_t *timep; time_t offset; { -static int moffset[] = - {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; - - long s; - long j, d, m, y; - - offset += *timep; - - tmbuf->tm_isdst = 0; /* Someone else can set this */ - - j = offset / 86400L + 719469; - s = offset % 86400L; - - if( s < 0 ) { s += 86400L; j--; } - - tmbuf->tm_sec = s % 60; - tmbuf->tm_min = (s / 60) % 60; - tmbuf->tm_hour = s / 3600; - - tmbuf->tm_wday = (j+2) % 7; - - /* - * Julian date converter. Takes a julian date (the number of days since - * some distant epoch or other), and fills tmbuf. - */ - - y = (4L * j - 1L) / 146097L; - j = 4L * j - 1L - 146097L * y; - d = j / 4L; - j = (4L * d + 3L) / 1461L; - d = 4L * d + 3L - 1461L * j; - d = (d + 4L) / 4L; - m = (5L * d - 3L) / 153L; - d = 5L * d - 3 - 153L * m; - d = (d + 5L) / 5L; - y = 100L * y + j; - if (m < 10) - m += 2; - else - { - m -= 10; - ++y; - } - - tmbuf->tm_year = y - 1900; - tmbuf->tm_mon = m; - tmbuf->tm_mday = d; - - tmbuf->tm_yday = d + moffset[m]; - if (m > 1 && ((y) % 4 == 0 && ((y) % 100 != 0 || (y) % 400 == 0))) - tmbuf->tm_yday++; + static int moffset[] = + { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 }; + + long s; + long j, d, m, y; + + offset += *timep; + + tmbuf->tm_isdst = 0; /* Someone else can set this */ + + j = offset / 86400L + 719469; + s = offset % 86400L; + + if (s < 0) { + s += 86400L; + j--; + } + + tmbuf->tm_sec = s % 60; + tmbuf->tm_min = (s / 60) % 60; + tmbuf->tm_hour = s / 3600; + + tmbuf->tm_wday = (j + 2) % 7; + + /* + * Julian date converter. Takes a julian date (the number of days since + * some distant epoch or other), and fills tmbuf. + */ + + y = (4L * j - 1L) / 146097L; + j = 4L * j - 1L - 146097L * y; + d = j / 4L; + j = (4L * d + 3L) / 1461L; + d = 4L * d + 3L - 1461L * j; + d = (d + 4L) / 4L; + m = (5L * d - 3L) / 153L; + d = 5L * d - 3 - 153L * m; + d = (d + 5L) / 5L; + y = 100L * y + j; + if (m < 10) + m += 2; + else { + m -= 10; + ++y; + } + + tmbuf->tm_year = y - 1900; + tmbuf->tm_mon = m; + tmbuf->tm_mday = d; + + tmbuf->tm_yday = d + moffset[m]; + if (m > 1 && ((y) % 4 == 0 && ((y) % 100 != 0 || (y) % 400 == 0))) + tmbuf->tm_yday++; } #else @@ -74,65 +75,59 @@ static int moffset[] = #include -static const unsigned short int __mon_lengths[2][12] = - { - /* Normal years. */ - { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, - /* Leap years. */ - { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } - }; +static const unsigned short int __mon_lengths[2][12] = { + /* Normal years. */ + {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, + /* Leap years. */ + {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} +}; -void -__tm_conv(tmbuf, t, offset) +void __tm_conv(tmbuf, t, offset) struct tm *tmbuf; time_t *t; time_t offset; { - long days, rem; - register int y; - register const unsigned short int *ip; - - days = *t / SECS_PER_DAY; - rem = *t % SECS_PER_DAY; - rem += offset; - while (rem < 0) - { - rem += SECS_PER_DAY; - --days; - } - while (rem >= SECS_PER_DAY) - { - rem -= SECS_PER_DAY; - ++days; - } - tmbuf->tm_hour = rem / SECS_PER_HOUR; - rem %= SECS_PER_HOUR; - tmbuf->tm_min = rem / 60; - tmbuf->tm_sec = rem % 60; - /* January 1, 1970 was a Thursday. */ - tmbuf->tm_wday = (4 + days) % 7; - if (tmbuf->tm_wday < 0) - tmbuf->tm_wday += 7; - y = 1970; - while (days >= (rem = __isleap(y) ? 366 : 365)) - { - ++y; - days -= rem; - } - while (days < 0) - { - --y; - days += __isleap(y) ? 366 : 365; - } - tmbuf->tm_year = y - 1900; - tmbuf->tm_yday = days; - ip = __mon_lengths[__isleap(y)]; - for (y = 0; days >= ip[y]; ++y) - days -= ip[y]; - tmbuf->tm_mon = y; - tmbuf->tm_mday = days + 1; - tmbuf->tm_isdst = -1; + long days, rem; + register int y; + register const unsigned short int *ip; + + days = *t / SECS_PER_DAY; + rem = *t % SECS_PER_DAY; + rem += offset; + while (rem < 0) { + rem += SECS_PER_DAY; + --days; + } + while (rem >= SECS_PER_DAY) { + rem -= SECS_PER_DAY; + ++days; + } + tmbuf->tm_hour = rem / SECS_PER_HOUR; + rem %= SECS_PER_HOUR; + tmbuf->tm_min = rem / 60; + tmbuf->tm_sec = rem % 60; + /* January 1, 1970 was a Thursday. */ + tmbuf->tm_wday = (4 + days) % 7; + if (tmbuf->tm_wday < 0) + tmbuf->tm_wday += 7; + y = 1970; + while (days >= (rem = __isleap(y) ? 366 : 365)) { + ++y; + days -= rem; + } + while (days < 0) { + --y; + days += __isleap(y) ? 366 : 365; + } + tmbuf->tm_year = y - 1900; + tmbuf->tm_yday = days; + ip = __mon_lengths[__isleap(y)]; + for (y = 0; days >= ip[y]; ++y) + days -= ip[y]; + tmbuf->tm_mon = y; + tmbuf->tm_mday = days + 1; + tmbuf->tm_isdst = -1; } #endif diff --git a/libc/pwd_grp/Makefile b/libc/pwd_grp/Makefile index e5977c5ea..c4875c208 100644 --- a/libc/pwd_grp/Makefile +++ b/libc/pwd_grp/Makefile @@ -28,14 +28,16 @@ CSRC=__getpwent.c pwent.c getpwnam.c getpwuid.c putpwent.c getpw.c fgetpwent.c \ __getgrent.c grent.c getgrnam.c getgrgid.c fgetgrent.c initgroups.c \ utent.c COBJS=$(patsubst %.c,%.o, $(CSRC)) +OBJS=$(COBJS) +all: $(OBJS) $(LIBC) -all: $(COBJS) $(LIBC) +$(LIBC): ar-target -$(LIBC): $(COBJS) - $(AR) $(ARFLAGS) $(LIBC) $(COBJS) +ar-target: $(OBJS) + $(AR) $(ARFLAGS) $(LIBC) $(OBJS) -$(COBJS): Makefile +$(OBJS): Makefile __getgrent.c: config.h initgroups.c: config.h diff --git a/libc/stdio/.indent.pro b/libc/stdio/.indent.pro new file mode 100644 index 000000000..492ecf1c7 --- /dev/null +++ b/libc/stdio/.indent.pro @@ -0,0 +1,33 @@ +--blank-lines-after-declarations +--blank-lines-after-procedures +--break-before-boolean-operator +--no-blank-lines-after-commas +--braces-on-if-line +--braces-on-struct-decl-line +--comment-indentation25 +--declaration-comment-column25 +--no-comment-delimiters-on-blank-lines +--cuddle-else +--continuation-indentation4 +--case-indentation0 +--else-endif-column33 +--space-after-cast +--line-comments-indentation0 +--declaration-indentation1 +--dont-format-first-column-comments +--dont-format-comments +--honour-newlines +--indent-level4 +/* changed from 0 to 4 */ +--parameter-indentation4 +--line-length78 /* changed from 75 */ +--continue-at-parentheses +--no-space-after-function-call-names +--dont-break-procedure-type +--dont-star-comments +--leave-optional-blank-lines +--dont-space-special-semicolon +--tab-size4 +/* additions by Mark */ +--case-brace-indentation0 +--leave-preprocessor-space diff --git a/libc/stdio/Makefile b/libc/stdio/Makefile index 048483d13..fbd0bbf15 100644 --- a/libc/stdio/Makefile +++ b/libc/stdio/Makefile @@ -38,12 +38,15 @@ MOBJ3=scanf.o sscanf.o fscanf.o vscanf.o vsscanf.o vfscanf.o CSRC=dputs.c COBJS=$(patsubst %.c,%.o, $(CSRC)) +OBJS=$(MOBJ) $(MOBJ2) $(MOBJ3) $(COBJS) -all: $(MOBJ) $(MOBJ2) $(MOBJ3) $(COBJS) $(LIBC) +all: $(OBJS) $(LIBC) -$(LIBC): $(MOBJ) $(MOBJ2) $(MOBJ3) $(COBJS) - $(AR) $(ARFLAGS) $(LIBC) $(MOBJ) $(MOBJ2) $(MOBJ3) $(COBJS) +$(LIBC): ar-target + +ar-target: $(OBJS) + $(AR) $(ARFLAGS) $(LIBC) $(OBJS) $(MOBJ): $(MSRC) $(CC) $(CFLAGS) -DL_$* $< -c -o $*.o @@ -54,6 +57,8 @@ $(MOBJ2): $(MSRC2) $(MOBJ3): $(MSRC3) $(CC) $(CFLAGS) -DL_$* $< -c -o $*.o +$(OBJS): Makefile + clean: rm -f *.[oa] *~ core diff --git a/libc/stdio/printf.c b/libc/stdio/printf.c index 7aba8c74d..d987966f6 100644 --- a/libc/stdio/printf.c +++ b/libc/stdio/printf.c @@ -34,115 +34,117 @@ -extern int vfnprintf(FILE *op, size_t max_size, register __const char *fmt, register va_list ap); +extern int vfnprintf(FILE * op, size_t max_size, + register __const char *fmt, register va_list ap); #ifdef L_printf -int printf(const char * fmt, ...) +int printf(const char *fmt, ...) { - va_list ptr; - int rv; + va_list ptr; + int rv; - va_strt(ptr, fmt); - rv = vfnprintf(stdout, -1, fmt, ptr); - va_end(ptr); - return rv; + va_strt(ptr, fmt); + rv = vfnprintf(stdout, -1, fmt, ptr); + va_end(ptr); + return rv; } #endif #ifdef L_sprintf -int sprintf(char * sp, const char * fmt, ...) +int sprintf(char *sp, const char *fmt, ...) { - static FILE string[1] = - { - {0, 0, (char*)(unsigned) -1, 0, (char*) (unsigned) -1, -1, - _IOFBF | __MODE_WRITE} - }; - - va_list ptr; - int rv; - va_strt(ptr, fmt); - string->bufpos = sp; - rv = vfnprintf(string, -1, fmt, ptr); - va_end(ptr); - *(string->bufpos) = 0; - return rv; + static FILE string[1] = { + {0, 0, (char *) (unsigned) -1, 0, (char *) (unsigned) -1, -1, + _IOFBF | __MODE_WRITE} + }; + + va_list ptr; + int rv; + + va_strt(ptr, fmt); + string->bufpos = sp; + rv = vfnprintf(string, -1, fmt, ptr); + va_end(ptr); + *(string->bufpos) = 0; + return rv; } #endif #ifdef L_snprintf -int snprintf(char * sp, size_t size, const char * fmt, ...) +int snprintf(char *sp, size_t size, const char *fmt, ...) { - static FILE string[1] = - { - {0, 0, (char*)(unsigned) -1, 0, (char*) (unsigned) -1, -1, - _IOFBF | __MODE_WRITE} - }; - - va_list ptr; - int rv; - va_strt(ptr, fmt); - string->bufpos = sp; - rv = vfnprintf(string, size, fmt, ptr); - va_end(ptr); - *(string->bufpos) = 0; - return rv; + static FILE string[1] = { + {0, 0, (char *) (unsigned) -1, 0, (char *) (unsigned) -1, -1, + _IOFBF | __MODE_WRITE} + }; + + va_list ptr; + int rv; + + va_strt(ptr, fmt); + string->bufpos = sp; + rv = vfnprintf(string, size, fmt, ptr); + va_end(ptr); + *(string->bufpos) = 0; + return rv; } #endif #ifdef L_fprintf -int fprintf(FILE * fp, const char * fmt, ...) +int fprintf(FILE * fp, const char *fmt, ...) { - va_list ptr; - int rv; - va_strt(ptr, fmt); - rv = vfnprintf(fp, -1, fmt, ptr); - va_end(ptr); - return rv; + va_list ptr; + int rv; + + va_strt(ptr, fmt); + rv = vfnprintf(fp, -1, fmt, ptr); + va_end(ptr); + return rv; } #endif #ifdef L_vprintf -int vprintf(const char * fmt, va_list ap) +int vprintf(const char *fmt, va_list ap) { - return vfprintf(stdout,fmt,ap); + return vfprintf(stdout, fmt, ap); } #endif #ifdef L_vsprintf -int vsprintf( char * sp, __const char *fmt, va_list ap) +int vsprintf(char *sp, __const char *fmt, va_list ap) { - static FILE string[1] = - { - {0, 0, (char*)(unsigned) -1, 0, (char*) (unsigned) -1, -1, - _IOFBF | __MODE_WRITE} - }; - - int rv; - string->bufpos = sp; - rv = vfnprintf(string, -1, fmt, ap); - *(string->bufpos) = 0; - return rv; + static FILE string[1] = { + {0, 0, (char *) (unsigned) -1, 0, (char *) (unsigned) -1, -1, + _IOFBF | __MODE_WRITE} + }; + + int rv; + + string->bufpos = sp; + rv = vfnprintf(string, -1, fmt, ap); + *(string->bufpos) = 0; + return rv; } #endif #ifdef L_vsprintf -int vsnprintf( char * sp, size_t size, __const char *fmt, va_list ap) +int vsnprintf(char *sp, size_t size, __const char *fmt, va_list ap) { - static FILE string[1] = - { - {0, 0, (char*)(unsigned) -1, 0, (char*) (unsigned) -1, -1, - _IOFBF | __MODE_WRITE} - }; - - int rv; - string->bufpos = sp; - rv = vfnprintf(string, size, fmt, ap); - *(string->bufpos) = 0; - return rv; + static FILE string[1] = { + {0, 0, (char *) (unsigned) -1, 0, (char *) (unsigned) -1, -1, + _IOFBF | __MODE_WRITE} + }; + + int rv; + + string->bufpos = sp; + rv = vfnprintf(string, size, fmt, ap); + *(string->bufpos) = 0; + return rv; } #endif @@ -155,261 +157,265 @@ int _vfprintf_fp_ref = 0; #endif static int -printfield(op, buf, ljustf, sign, pad, width, preci, buffer_mode, max_size, current_size) +printfield(op, buf, ljustf, sign, pad, width, preci, buffer_mode, max_size, + current_size) register FILE *op; register unsigned char *buf; -int ljustf; +int ljustf; register char sign; -char pad; +char pad; register int width; -int preci; -int buffer_mode; +int preci; +int buffer_mode; size_t max_size; size_t current_size; + /* * Output the given field in the manner specified by the arguments. Return * the number of characters output. */ { - register int cnt = 0, len; - register unsigned char ch; - - len = strlen(buf); - - if (*buf == '-') - sign = *buf++; - else if (sign) - len++; - - if ((preci != -1) && (len > preci)) /* limit max data width */ - len = preci; - - if (width < len) /* flexible field width or width overflow */ - width = len; - - /* - * at this point: width = total field width len = actual data width - * (including possible sign character) - */ - cnt = width; - width -= len; - - while (width || len) - { - if (!ljustf && width) /* left padding */ - { - if (len && sign && (pad == '0')) - goto showsign; - ch = pad; - --width; - } - else if (len) - { - if (sign) - { - showsign:ch = sign; /* sign */ - sign = '\0'; - } - else - ch = *buf++; /* main field */ - --len; - } - else - { - ch = pad; /* right padding */ - --width; - } - current_size++; - if (max_size>0 && current_size < max_size) - putc(ch, op); - if( ch == '\n' && buffer_mode == _IOLBF ) fflush(op); - } - - return (cnt); + register int cnt = 0, len; + register unsigned char ch; + + len = strlen(buf); + + if (*buf == '-') + sign = *buf++; + else if (sign) + len++; + + if ((preci != -1) && (len > preci)) /* limit max data width */ + len = preci; + + if (width < len) /* flexible field width or width overflow */ + width = len; + + /* + * at this point: width = total field width len = actual data width + * (including possible sign character) + */ + cnt = width; + width -= len; + + while (width || len) { + if (!ljustf && width) { /* left padding */ + if (len && sign && (pad == '0')) + goto showsign; + ch = pad; + --width; + } else if (len) { + if (sign) { + showsign:ch = sign; + /* sign */ + sign = '\0'; + } else + ch = *buf++; /* main field */ + --len; + } else { + ch = pad; /* right padding */ + --width; + } + current_size++; + if (max_size > 0 && current_size < max_size) + putc(ch, op); + if (ch == '\n' && buffer_mode == _IOLBF) + fflush(op); + } + + return (cnt); } -int vfnprintf(FILE *op, size_t max_size, register __const char *fmt, register va_list ap) +int vfnprintf(FILE * op, size_t max_size, register __const char *fmt, + register va_list ap) { - register int i, cnt = 0, ljustf, lval; - int preci, dpoint, width; - char pad, sign, radix, hash; - register char *ptmp; - char tmp[64], *ltostr(), *ultostr(); - int buffer_mode; - - /* This speeds things up a bit for unbuffered */ - buffer_mode = (op->mode&__MODE_BUF); - op->mode &= (~__MODE_BUF); - - while (*fmt) - { - if (*fmt == '%') - { - if( buffer_mode == _IONBF ) fflush(op); - ljustf = 0; /* left justify flag */ - sign = '\0'; /* sign char & status */ - pad = ' '; /* justification padding char */ - width = -1; /* min field width */ - dpoint = 0; /* found decimal point */ - preci = -1; /* max data width */ - radix = 10; /* number base */ - ptmp = tmp; /* pointer to area to print */ - hash = 0; - lval = (sizeof(int)==sizeof(long)); /* long value flaged */ - fmtnxt: - i = 0; - for(;;) - { - ++fmt; - if(*fmt < '0' || *fmt > '9' ) break; - i = (i * 10) + (*fmt - '0'); - if (dpoint) - preci = i; - else if (!i && (pad == ' ')) - { - pad = '0'; - goto fmtnxt; - } - else - width = i; - } - - switch (*fmt) - { - case '\0': /* early EOS */ - --fmt; - goto charout; - - case '-': /* left justification */ - ljustf = 1; - goto fmtnxt; - - case ' ': - case '+': /* leading sign flag */ - sign = *fmt; - goto fmtnxt; - - case '*': /* parameter width value */ - i = va_arg(ap, int); - if (dpoint) - preci = i; - else - width = i; - goto fmtnxt; - - case '.': /* secondary width field */ - dpoint = 1; - goto fmtnxt; - - case 'l': /* long data */ - lval = 1; - goto fmtnxt; - - case 'h': /* short data */ - lval = 0; - goto fmtnxt; - - case 'd': /* Signed decimal */ - case 'i': - ptmp = ltostr((long) ((lval) - ? va_arg(ap, long) - : va_arg(ap, short)), - 10, 0); - goto printit; - - case 'b': /* Unsigned binary */ - radix = 2; - goto usproc; - - case 'o': /* Unsigned octal */ - radix = 8; - goto usproc; - - case 'p': /* Pointer */ - lval = (sizeof(char*) == sizeof(long)); - pad = '0'; - width = 6; - preci = 8; - /* fall thru */ - - case 'x': /* Unsigned hexadecimal */ - case 'X': - radix = 16; - /* fall thru */ - - case 'u': /* Unsigned decimal */ - usproc: - ptmp = ultostr((unsigned long) ((lval) - ? va_arg(ap, unsigned long) - : va_arg(ap, unsigned short)), - radix, (*fmt == 'X') ? 1 : 0); - if( hash && radix == 8 ) { width = strlen(ptmp)+1; pad='0'; } - goto printit; - - case '#': - hash=1; - goto fmtnxt; - - case 'c': /* Character */ - ptmp[0] = va_arg(ap, int); - ptmp[1] = '\0'; - goto nopad; - - case 's': /* String */ - ptmp = va_arg(ap, char*); - nopad: - sign = '\0'; - pad = ' '; - printit: - cnt += printfield(op, ptmp, ljustf, sign, pad, width, - preci, buffer_mode, max_size, cnt); - break; + register int i, cnt = 0, ljustf, lval; + int preci, dpoint, width; + char pad, sign, radix, hash; + register char *ptmp; + char tmp[64], *ltostr(), *ultostr(); + int buffer_mode; + + /* This speeds things up a bit for unbuffered */ + buffer_mode = (op->mode & __MODE_BUF); + op->mode &= (~__MODE_BUF); + + while (*fmt) { + if (*fmt == '%') { + if (buffer_mode == _IONBF) + fflush(op); + ljustf = 0; /* left justify flag */ + sign = '\0'; /* sign char & status */ + pad = ' '; /* justification padding char */ + width = -1; /* min field width */ + dpoint = 0; /* found decimal point */ + preci = -1; /* max data width */ + radix = 10; /* number base */ + ptmp = tmp; /* pointer to area to print */ + hash = 0; + lval = (sizeof(int) == sizeof(long)); /* long value flaged */ + + fmtnxt: + i = 0; + for (;;) { + ++fmt; + if (*fmt < '0' || *fmt > '9') + break; + i = (i * 10) + (*fmt - '0'); + if (dpoint) + preci = i; + else if (!i && (pad == ' ')) { + pad = '0'; + goto fmtnxt; + } else + width = i; + } + + switch (*fmt) { + case '\0': /* early EOS */ + --fmt; + goto charout; + + case '-': /* left justification */ + ljustf = 1; + goto fmtnxt; + + case ' ': + case '+': /* leading sign flag */ + sign = *fmt; + goto fmtnxt; + + case '*': /* parameter width value */ + i = va_arg(ap, int); + + if (dpoint) + preci = i; + else + width = i; + goto fmtnxt; + + case '.': /* secondary width field */ + dpoint = 1; + goto fmtnxt; + + case 'l': /* long data */ + lval = 1; + goto fmtnxt; + + case 'h': /* short data */ + lval = 0; + goto fmtnxt; + + case 'd': /* Signed decimal */ + case 'i': + ptmp = ltostr((long) ((lval) + ? va_arg(ap, long) + : va_arg(ap, short)), 10, 0); + + goto printit; + + case 'b': /* Unsigned binary */ + radix = 2; + goto usproc; + + case 'o': /* Unsigned octal */ + radix = 8; + goto usproc; + + case 'p': /* Pointer */ + lval = (sizeof(char *) == sizeof(long)); + + pad = '0'; + width = 6; + preci = 8; + /* fall thru */ + + case 'x': /* Unsigned hexadecimal */ + case 'X': + radix = 16; + /* fall thru */ + + case 'u': /* Unsigned decimal */ + usproc: + ptmp = ultostr((unsigned long) ((lval) + ? va_arg(ap, unsigned long) + : va_arg(ap, + unsigned short)), + radix, (*fmt == 'X') ? 1 : 0); + + if (hash && radix == 8) { + width = strlen(ptmp) + 1; + pad = '0'; + } + goto printit; + + case '#': + hash = 1; + goto fmtnxt; + + case 'c': /* Character */ + ptmp[0] = va_arg(ap, int); + + ptmp[1] = '\0'; + goto nopad; + + case 's': /* String */ + ptmp = va_arg(ap, char *); + + nopad: + sign = '\0'; + pad = ' '; + printit: + cnt += printfield(op, ptmp, ljustf, sign, pad, width, + preci, buffer_mode, max_size, cnt); + break; #if FLOATS - case 'e': /* float */ - case 'f': - case 'g': - case 'E': - case 'G': - fprintf(stderr, "LIBM:PRINTF"); - gcvt(va_arg(ap, double), preci, ptmp); - preci = -1; - goto printit; + case 'e': /* float */ + case 'f': + case 'g': + case 'E': + case 'G': + fprintf(stderr, "LIBM:PRINTF"); + gcvt(va_arg(ap, double), preci, ptmp); + + preci = -1; + goto printit; #else - case 'e': /* float */ - case 'f': - case 'g': - case 'E': - case 'G': - fprintf(stderr, "LIBC:PRINTF"); - exit(-1); + case 'e': /* float */ + case 'f': + case 'g': + case 'E': + case 'G': + fprintf(stderr, "LIBC:PRINTF"); + exit(-1); #endif - default: /* unknown character */ - goto charout; - } - } - else - { - charout: - if (max_size>0 && ++cntmode |= buffer_mode; - if( buffer_mode == _IONBF ) fflush(op); - if( buffer_mode == _IOLBF ) op->bufwrite = op->bufstart; - return (cnt); + default: /* unknown character */ + goto charout; + } + } else { + charout: + if (max_size > 0 && ++cnt < max_size) + putc(*fmt, op); /* normal char out */ + if (*fmt == '\n' && buffer_mode == _IOLBF) + fflush(op); + } + ++fmt; + } + op->mode |= buffer_mode; + if (buffer_mode == _IONBF) + fflush(op); + if (buffer_mode == _IOLBF) + op->bufwrite = op->bufstart; + return (cnt); } -int vfprintf(FILE *op, register __const char *fmt, register va_list ap) +int vfprintf(FILE * op, register __const char *fmt, register va_list ap) { - return(vfnprintf(op, -1, fmt, ap)); + return (vfnprintf(op, -1, fmt, ap)); } #endif - diff --git a/libc/stdio/scanf.c b/libc/stdio/scanf.c index 593e01d25..8ce590684 100644 --- a/libc/stdio/scanf.c +++ b/libc/stdio/scanf.c @@ -14,64 +14,66 @@ #ifdef L_scanf #ifdef __STDC__ -int scanf(const char * fmt, ...) +int scanf(const char *fmt, ...) #else int scanf(fmt, va_alist) __const char *fmt; va_dcl #endif { - va_list ptr; - int rv; - va_strt(ptr, fmt); - rv = vfscanf(stdin,fmt,ptr); - va_end(ptr); - return rv; + va_list ptr; + int rv; + + va_strt(ptr, fmt); + rv = vfscanf(stdin, fmt, ptr); + va_end(ptr); + return rv; } #endif #ifdef L_sscanf #ifdef __STDC__ -int sscanf(const char * sp, const char * fmt, ...) +int sscanf(const char *sp, const char *fmt, ...) #else int sscanf(sp, fmt, va_alist) -__const char * sp; +__const char *sp; __const char *fmt; va_dcl #endif { - FILE string[1] = - { - {0, (char*)(unsigned) -1, 0, 0, (char*) (unsigned) -1, -1, - _IOFBF | __MODE_READ} - }; - - va_list ptr; - int rv; - va_strt(ptr, fmt); - string->bufpos = (unsigned char *)((void*)sp); - rv = vfscanf(string,fmt,ptr); - va_end(ptr); - return rv; + FILE string[1] = { + {0, (char *) (unsigned) -1, 0, 0, (char *) (unsigned) -1, -1, + _IOFBF | __MODE_READ} + }; + + va_list ptr; + int rv; + + va_strt(ptr, fmt); + string->bufpos = (unsigned char *) ((void *) sp); + rv = vfscanf(string, fmt, ptr); + va_end(ptr); + return rv; } #endif #ifdef L_fscanf #ifdef __STDC__ -int fscanf(FILE * fp, const char * fmt, ...) +int fscanf(FILE * fp, const char *fmt, ...) #else int fscanf(fp, fmt, va_alist) -FILE * fp; +FILE *fp; __const char *fmt; va_dcl #endif { - va_list ptr; - int rv; - va_strt(ptr, fmt); - rv = vfscanf(fp,fmt,ptr); - va_end(ptr); - return rv; + va_list ptr; + int rv; + + va_strt(ptr, fmt); + rv = vfscanf(fp, fmt, ptr); + va_end(ptr); + return rv; } #endif @@ -80,21 +82,20 @@ int vscanf(fmt, ap) __const char *fmt; va_list ap; { - return vfscanf(stdin,fmt,ap); + return vfscanf(stdin, fmt, ap); } #endif #ifdef L_vsscanf int vsscanf(__const char *sp, __const char *fmt, va_list ap) { - FILE string[1] = - { - {0, (char*)(unsigned) -1, 0, 0, (char*) (unsigned) -1, -1, - _IOFBF | __MODE_READ} - }; - - string->bufpos = (unsigned char *)((void*)sp); - return vfscanf(string,fmt,ap); + FILE string[1] = { + {0, (char *) (unsigned) -1, 0, 0, (char *) (unsigned) -1, -1, + _IOFBF | __MODE_READ} + }; + + string->bufpos = (unsigned char *) ((void *) sp); + return vfscanf(string, fmt, ap); } #endif @@ -112,23 +113,23 @@ int _vfscanf_fp_ref = 0; #if FLOATS /* fp scan actions */ -#define F_NADA 0 /* just change state */ -#define F_SIGN 1 /* set sign */ -#define F_ESIGN 2 /* set exponent's sign */ -#define F_INT 3 /* adjust integer part */ -#define F_FRAC 4 /* adjust fraction part */ -#define F_EXP 5 /* adjust exponent part */ +#define F_NADA 0 /* just change state */ +#define F_SIGN 1 /* set sign */ +#define F_ESIGN 2 /* set exponent's sign */ +#define F_INT 3 /* adjust integer part */ +#define F_FRAC 4 /* adjust fraction part */ +#define F_EXP 5 /* adjust exponent part */ #define F_QUIT 6 #define NSTATE 8 -#define FS_INIT 0 /* initial state */ -#define FS_SIGNED 1 /* saw sign */ -#define FS_DIGS 2 /* saw digits, no . */ -#define FS_DOT 3 /* saw ., no digits */ -#define FS_DD 4 /* saw digits and . */ -#define FS_E 5 /* saw 'e' */ -#define FS_ESIGN 6 /* saw exp's sign */ -#define FS_EDIGS 7 /* saw exp's digits */ +#define FS_INIT 0 /* initial state */ +#define FS_SIGNED 1 /* saw sign */ +#define FS_DIGS 2 /* saw digits, no . */ +#define FS_DOT 3 /* saw ., no digits */ +#define FS_DD 4 /* saw digits and . */ +#define FS_E 5 /* saw 'e' */ +#define FS_ESIGN 6 /* saw exp's sign */ +#define FS_EDIGS 7 /* saw exp's digits */ #define FC_DIG 0 #define FC_DOT 1 @@ -137,400 +138,376 @@ int _vfscanf_fp_ref = 0; /* given transition,state do what action? */ int fp_do[][NSTATE] = { - {F_INT,F_INT,F_INT, - F_FRAC,F_FRAC, - F_EXP,F_EXP,F_EXP}, /* see digit */ - {F_NADA,F_NADA,F_NADA, - F_QUIT,F_QUIT,F_QUIT,F_QUIT,F_QUIT}, /* see '.' */ - {F_QUIT,F_QUIT, - F_NADA,F_QUIT,F_NADA, - F_QUIT,F_QUIT,F_QUIT}, /* see e/E */ - {F_SIGN,F_QUIT,F_QUIT,F_QUIT,F_QUIT, - F_ESIGN,F_QUIT,F_QUIT}, /* see sign */ + {F_INT, F_INT, F_INT, + F_FRAC, F_FRAC, + F_EXP, F_EXP, F_EXP}, /* see digit */ + {F_NADA, F_NADA, F_NADA, + F_QUIT, F_QUIT, F_QUIT, F_QUIT, F_QUIT}, /* see '.' */ + {F_QUIT, F_QUIT, + F_NADA, F_QUIT, F_NADA, + F_QUIT, F_QUIT, F_QUIT}, /* see e/E */ + {F_SIGN, F_QUIT, F_QUIT, F_QUIT, F_QUIT, + F_ESIGN, F_QUIT, F_QUIT}, /* see sign */ }; + /* given transition,state what is new state? */ int fp_ns[][NSTATE] = { - {FS_DIGS,FS_DIGS,FS_DIGS, - FS_DD,FS_DD, - FS_EDIGS,FS_EDIGS,FS_EDIGS}, /* see digit */ - {FS_DOT,FS_DOT,FS_DD, - }, /* see '.' */ - {0,0, - FS_E,0,FS_E, - }, /* see e/E */ - {FS_SIGNED,0,0,0,0, - FS_ESIGN,0,0}, /* see sign */ + {FS_DIGS, FS_DIGS, FS_DIGS, + FS_DD, FS_DD, + FS_EDIGS, FS_EDIGS, FS_EDIGS}, /* see digit */ + {FS_DOT, FS_DOT, FS_DD, + }, /* see '.' */ + {0, 0, + FS_E, 0, FS_E, + }, /* see e/E */ + {FS_SIGNED, 0, 0, 0, 0, + FS_ESIGN, 0, 0}, /* see sign */ }; + /* which states are valid terminators? */ int fp_sval[NSTATE] = { - 0,0,1,0,1,0,0,1 + 0, 0, 1, 0, 1, 0, 0, 1 }; #endif -int -vfscanf(fp, fmt, ap) +int vfscanf(fp, fmt, ap) register FILE *fp; register const char *fmt; va_list ap; { - register long n; - register int c, width, lval, cnt = 0; - int store, neg, base, wide1, endnull, rngflag, c2; - register unsigned char *p; - unsigned char delim[128], digits[17], *q; + register long n; + register int c, width, lval, cnt = 0; + int store, neg, base, wide1, endnull, rngflag, c2; + register unsigned char *p; + unsigned char delim[128], digits[17], *q; + #if FLOATS - long frac, expo; - int eneg, fraclen, fstate, trans; - double fx, fp_scan(); + long frac, expo; + int eneg, fraclen, fstate, trans; + double fx, fp_scan(); #endif - if (!*fmt) - return (0); - - c = getc(fp); - while (c > 0) - { - store = 0; - if (*fmt == '%') - { - n = 0; - width = -1; - wide1 = 1; - base = 10; - lval = (sizeof(long) == sizeof(int)); - store = 1; - endnull = 1; - neg = -1; - - strcpy(delim, "\011\012\013\014\015 "); - strcpy(digits, "0123456789ABCDEF"); - - if (fmt[1] == '*') - { - endnull = store = 0; - ++fmt; - } - - while (isdigit(*++fmt))/* width digit(s) */ - { - if (width == -1) - width = 0; - wide1 = width = (width * 10) + (*fmt - '0'); - } - --fmt; - fmtnxt: - ++fmt; - switch (tolower(*fmt)) /* tolower() is a MACRO! */ - { - case '*': - endnull = store = 0; - goto fmtnxt; - - case 'l': /* long data */ - lval = 1; - goto fmtnxt; - case 'h': /* short data */ - lval = 0; - goto fmtnxt; - - case 'i': /* any-base numeric */ - base = 0; - goto numfmt; - - case 'b': /* unsigned binary */ - base = 2; - goto numfmt; - - case 'o': /* unsigned octal */ - base = 8; - goto numfmt; - - case 'x': /* unsigned hexadecimal */ - base = 16; - goto numfmt; - - case 'd': /* SIGNED decimal */ - neg = 0; - /* FALL-THRU */ - - case 'u': /* unsigned decimal */ - numfmt:skip(); - - if (isupper(*fmt)) - lval = 1; - - if (!base) - { - base = 10; - neg = 0; - if (c == '%') - { - base = 2; - goto skip1; - } - else if (c == '0') - { - c = getc(fp); - if (c < 1) - goto savnum; - if ((c != 'x') - && (c != 'X')) - { - base = 8; - digits[8] = '\0'; - goto zeroin; - } - base = 16; - goto skip1; - } - } - - if ((neg == 0) && (base == 10) - && ((neg = (c == '-')) || (c == '+'))) - { - skip1: - c = getc(fp); - if (c < 1) - goto done; - } - - digits[base] = '\0'; - p = ((unsigned char *) - strchr(digits, toupper(c))); - - if ((!c || !p) && width) - goto done; - - while (p && width-- && c) - { - n = (n * base) + (p - digits); - c = getc(fp); - zeroin: - p = ((unsigned char *) - strchr(digits, toupper(c))); - } - savnum: - if (store) - { - if (neg == 1) - n = -n; - if (lval) - *va_arg(ap, long*) = n; - else - *va_arg(ap, short*) = n; - ++cnt; - } - break; + if (!*fmt) + return (0); + + c = getc(fp); + while (c > 0) { + store = 0; + if (*fmt == '%') { + n = 0; + width = -1; + wide1 = 1; + base = 10; + lval = (sizeof(long) == sizeof(int)); + + store = 1; + endnull = 1; + neg = -1; + + strcpy(delim, "\011\012\013\014\015 "); + strcpy(digits, "0123456789ABCDEF"); + + if (fmt[1] == '*') { + endnull = store = 0; + ++fmt; + } + + while (isdigit(*++fmt)) { /* width digit(s) */ + if (width == -1) + width = 0; + wide1 = width = (width * 10) + (*fmt - '0'); + } + --fmt; + fmtnxt: + ++fmt; + switch (tolower(*fmt)) { /* tolower() is a MACRO! */ + case '*': + endnull = store = 0; + goto fmtnxt; + + case 'l': /* long data */ + lval = 1; + goto fmtnxt; + case 'h': /* short data */ + lval = 0; + goto fmtnxt; + + case 'i': /* any-base numeric */ + base = 0; + goto numfmt; + + case 'b': /* unsigned binary */ + base = 2; + goto numfmt; + + case 'o': /* unsigned octal */ + base = 8; + goto numfmt; + + case 'x': /* unsigned hexadecimal */ + base = 16; + goto numfmt; + + case 'd': /* SIGNED decimal */ + neg = 0; + /* FALL-THRU */ + + case 'u': /* unsigned decimal */ + numfmt:skip(); + + if (isupper(*fmt)) + lval = 1; + + if (!base) { + base = 10; + neg = 0; + if (c == '%') { + base = 2; + goto skip1; + } else if (c == '0') { + c = getc(fp); + if (c < 1) + goto savnum; + if ((c != 'x') + && (c != 'X')) { + base = 8; + digits[8] = '\0'; + goto zeroin; + } + base = 16; + goto skip1; + } + } + + if ((neg == 0) && (base == 10) + && ((neg = (c == '-')) || (c == '+'))) { + skip1: + c = getc(fp); + if (c < 1) + goto done; + } + + digits[base] = '\0'; + p = ((unsigned char *) + strchr(digits, toupper(c))); + + if ((!c || !p) && width) + goto done; + + while (p && width-- && c) { + n = (n * base) + (p - digits); + c = getc(fp); + zeroin: + p = ((unsigned char *) + strchr(digits, toupper(c))); + } + savnum: + if (store) { + if (neg == 1) + n = -n; + if (lval) + *va_arg(ap, long *) = n; + + else + *va_arg(ap, short *) = n; + + ++cnt; + } + break; #if FLOATS - case 'e': /* float */ - case 'f': - case 'g': - skip(); - fprintf(stderr,"LIBM:SCANF"); - - if (isupper(*fmt)) - lval = 1; - - fstate = FS_INIT; - neg = 0; - eneg = 0; - n = 0; - frac = 0; - expo = 0; - fraclen = 0; - - while (c && width--) - { - if (c >= '0' && c <= '9') - trans = FC_DIG; - else if (c == '.') - trans = FC_DOT; - else if (c == '+' || c == '-') - trans = FC_SIGN; - else if (tolower(c) == 'e') - trans = FC_E; - else - goto fdone; - - switch (fp_do[trans][fstate]) - { - case F_SIGN: - neg = (c == '-'); - break; - case F_ESIGN: - eneg = (c == '-'); - break; - case F_INT: - n = 10 * n + (c - '0'); - break; - case F_FRAC: - frac = 10 * frac + (c - '0'); - fraclen++; - break; - case F_EXP: - expo = 10 * expo + (c - '0'); - break; - case F_QUIT: - goto fdone; - } - fstate = fp_ns[trans][fstate]; - c = getc(fp); - } - - fdone: - if (!fp_sval[fstate]) - goto done; - if (store) - { - fx = fp_scan(neg, eneg, n, frac, expo, fraclen); - if (lval) - *va_arg(ap, double *) = fx; - else - *va_arg(ap, float *) = fx; - ++cnt; - } - break; + case 'e': /* float */ + case 'f': + case 'g': + skip(); + fprintf(stderr, "LIBM:SCANF"); + + if (isupper(*fmt)) + lval = 1; + + fstate = FS_INIT; + neg = 0; + eneg = 0; + n = 0; + frac = 0; + expo = 0; + fraclen = 0; + + while (c && width--) { + if (c >= '0' && c <= '9') + trans = FC_DIG; + else if (c == '.') + trans = FC_DOT; + else if (c == '+' || c == '-') + trans = FC_SIGN; + else if (tolower(c) == 'e') + trans = FC_E; + else + goto fdone; + + switch (fp_do[trans][fstate]) { + case F_SIGN: + neg = (c == '-'); + break; + case F_ESIGN: + eneg = (c == '-'); + break; + case F_INT: + n = 10 * n + (c - '0'); + break; + case F_FRAC: + frac = 10 * frac + (c - '0'); + fraclen++; + break; + case F_EXP: + expo = 10 * expo + (c - '0'); + break; + case F_QUIT: + goto fdone; + } + fstate = fp_ns[trans][fstate]; + c = getc(fp); + } + + fdone: + if (!fp_sval[fstate]) + goto done; + if (store) { + fx = fp_scan(neg, eneg, n, frac, expo, fraclen); + if (lval) + *va_arg(ap, double *) = fx; + + else + *va_arg(ap, float *) = fx; + + ++cnt; + } + break; #else - case 'e': /* float */ - case 'f': - case 'g': - fprintf(stderr, "LIBC:SCANF"); - exit(-1); + case 'e': /* float */ + case 'f': + case 'g': + fprintf(stderr, "LIBC:SCANF"); + exit(-1); #endif - case 'c': /* character data */ - width = wide1; - lval = endnull = 0; - delim[0] = '\0'; - goto strproc; - - case '[': /* string w/ delimiter set */ - - /* get delimiters */ - p = delim; - - if (*++fmt == '^') - { - fmt++; - lval = 0; - } - else - lval = 1; - - rngflag = 2; - if ((*fmt == ']') || (*fmt == '-')) - { - *p++ = *fmt++; - rngflag = 0; - } - - while (*fmt != ']') - { - if (*fmt == '\0') - goto done; - switch (rngflag) - { - case 1: - c2 = *(p - 2); - if (c2 <= *fmt) - { - p -= 2; - while (c2 < *fmt) - *p++ = c2++; - rngflag = 2; - break; - } - /* fall thru intentional */ - - case 0: - rngflag = (*fmt == '-'); - break; - - case 2: - rngflag = 0; - } - - *p++ = *fmt++; - } - - *p = '\0'; - goto strproc; - - case 's': /* string data */ - lval = 0; - skip(); - strproc: - /* process string */ - p = va_arg(ap, unsigned char *); - - /* if the 1st char fails, match fails */ - if (width) - { - q = ((unsigned char *) - strchr(delim, c)); - if ((c < 1) || lval == (q==0)) - { - if (endnull) - *p = '\0'; - goto done; - } - } - - for (;;) /* FOREVER */ - { - if (store) - *p++ = c; - if (((c = getc(fp)) < 1) || - (--width == 0)) - break; - - q = ((unsigned char *) - strchr(delim, c)); - if (lval == (q==0)) - break; - } - - if (store) - { - if (endnull) - *p = '\0'; - ++cnt; - } - break; - - case '\0': /* early EOS */ - --fmt; - /* FALL THRU */ - - default: - goto cmatch; - } - } - else if (isspace(*fmt)) /* skip whitespace */ - { - skip(); - } - else - { /* normal match char */ - cmatch: - if (c != *fmt) - break; - c = getc(fp); - } - - if (!*++fmt) - break; - } - - done: /* end of scan */ - if ((c == EOF) && (cnt == 0)) - return (EOF); - - if( c != EOF ) - ungetc(c, fp); - return (cnt); + case 'c': /* character data */ + width = wide1; + lval = endnull = 0; + delim[0] = '\0'; + goto strproc; + + case '[': /* string w/ delimiter set */ + + /* get delimiters */ + p = delim; + + if (*++fmt == '^') { + fmt++; + lval = 0; + } else + lval = 1; + + rngflag = 2; + if ((*fmt == ']') || (*fmt == '-')) { + *p++ = *fmt++; + rngflag = 0; + } + + while (*fmt != ']') { + if (*fmt == '\0') + goto done; + switch (rngflag) { + case 1: + c2 = *(p - 2); + if (c2 <= *fmt) { + p -= 2; + while (c2 < *fmt) + *p++ = c2++; + rngflag = 2; + break; + } + /* fall thru intentional */ + + case 0: + rngflag = (*fmt == '-'); + break; + + case 2: + rngflag = 0; + } + + *p++ = *fmt++; + } + + *p = '\0'; + goto strproc; + + case 's': /* string data */ + lval = 0; + skip(); + strproc: + /* process string */ + p = va_arg(ap, unsigned char *); + + /* if the 1st char fails, match fails */ + if (width) { + q = ((unsigned char *) + strchr(delim, c)); + if ((c < 1) || lval == (q == 0)) { + if (endnull) + *p = '\0'; + goto done; + } + } + + for (;;) { /* FOREVER */ + if (store) + *p++ = c; + if (((c = getc(fp)) < 1) || (--width == 0)) + break; + + q = ((unsigned char *) + strchr(delim, c)); + if (lval == (q == 0)) + break; + } + + if (store) { + if (endnull) + *p = '\0'; + ++cnt; + } + break; + + case '\0': /* early EOS */ + --fmt; + /* FALL THRU */ + + default: + goto cmatch; + } + } else if (isspace(*fmt)) { /* skip whitespace */ + skip(); + } else { /* normal match char */ + cmatch: + if (c != *fmt) + break; + c = getc(fp); + } + + if (!*++fmt) + break; + } + + done: /* end of scan */ + if ((c == EOF) && (cnt == 0)) + return (EOF); + + if (c != EOF) + ungetc(c, fp); + return (cnt); } #endif diff --git a/libc/stdio/stdio.c b/libc/stdio/stdio.c index 9043e6199..b34631154 100644 --- a/libc/stdio/stdio.c +++ b/libc/stdio/stdio.c @@ -19,7 +19,7 @@ void __io_init_vars(void); -extern FILE *__IO_list; /* For fflush at exit */ +extern FILE *__IO_list; /* For fflush at exit */ #define FIXED_BUFFERS 2 struct fixed_buffer { @@ -35,11 +35,12 @@ extern struct fixed_buffer _fixed_buffers[2]; #define buferr (stderr->unbuf) /* Stderr is unbuffered */ -FILE *__IO_list = 0; /* For fflush at exit */ +FILE *__IO_list = 0; /* For fflush at exit */ #if 0 static char bufin[BUFSIZ]; static char bufout[BUFSIZ]; + #ifndef buferr static char buferr[BUFSIZ]; #endif @@ -47,56 +48,54 @@ static char buferr[BUFSIZ]; #else static char *bufin; static char *bufout; + #ifndef buferr static char *buferr; #endif #endif -FILE stdin[1] = -{ +FILE stdin[1] = { #if 0 - {bufin, bufin, bufin, bufin, bufin + sizeof(bufin), + {bufin, bufin, bufin, bufin, bufin + sizeof(bufin), #else - {0, 0, 0, 0, 0, + {0, 0, 0, 0, 0, #endif - 0, _IOFBF | __MODE_READ | __MODE_IOTRAN} + 0, _IOFBF | __MODE_READ | __MODE_IOTRAN} }; -FILE stdout[1] = -{ +FILE stdout[1] = { #if 0 - {bufout, bufout, bufout, bufout, bufout + sizeof(bufout), + {bufout, bufout, bufout, bufout, bufout + sizeof(bufout), #else - {0, 0, 0, 0, 0, + {0, 0, 0, 0, 0, #endif - 1, _IOFBF | __MODE_WRITE | __MODE_IOTRAN} + 1, _IOFBF | __MODE_WRITE | __MODE_IOTRAN} }; -FILE stderr[1] = -{ +FILE stderr[1] = { #if 0 - {buferr, buferr, buferr, buferr, buferr + sizeof(buferr), + {buferr, buferr, buferr, buferr, buferr + sizeof(buferr), #else - {0, 0, 0, 0, 0, + {0, 0, 0, 0, 0, #endif - 2, _IONBF | __MODE_WRITE | __MODE_IOTRAN} + 2, _IONBF | __MODE_WRITE | __MODE_IOTRAN} }; /* Call the stdio initiliser; it's main job it to call atexit */ void __stdio_close_all(void) { - FILE *fp; - fflush(stdout); - fflush(stderr); - for (fp = __IO_list; fp; fp = fp->next) - { - fflush(fp); - close(fp->fd); - /* Note we're not de-allocating the memory */ - /* There doesn't seem to be much point :-) */ - fp->fd = -1; - } + FILE *fp; + + fflush(stdout); + fflush(stderr); + for (fp = __IO_list; fp; fp = fp->next) { + fflush(fp); + close(fp->fd); + /* Note we're not de-allocating the memory */ + /* There doesn't seem to be much point :-) */ + fp->fd = -1; + } } static int first_time = 0; @@ -106,295 +105,284 @@ struct fixed_buffer _fixed_buffers[2]; void __io_init_vars(void) { - if( first_time ) return; - first_time = 1; - - stdin->bufpos = bufin = _fixed_buffers[0].data; /*(char *)malloc(BUFSIZ)*/; - stdin->bufread = bufin; - stdin->bufwrite = bufin; - stdin->bufstart = bufin; - stdin->bufend = bufin + sizeof(bufin); - stdin->fd = 0; - stdin->mode = _IOFBF | __MODE_READ | __MODE_IOTRAN | __MODE_FREEBUF; - - _fixed_buffers[0].used = 1; - - stdout->bufpos = bufout = _fixed_buffers[1].data; /*(char *)malloc(BUFSIZ);*/ - stdout->bufread = bufout; - stdout->bufwrite = bufout; - stdout->bufstart = bufout; - stdout->bufend = bufout + sizeof(bufout); - stdout->fd = 1; - stdout->mode = _IOFBF | __MODE_WRITE | __MODE_IOTRAN | __MODE_FREEBUF; - - _fixed_buffers[1].used = 1; + if (first_time) + return; + first_time = 1; + + stdin->bufpos = bufin = _fixed_buffers[0].data; + /*(char *)malloc(BUFSIZ) */ ; + stdin->bufread = bufin; + stdin->bufwrite = bufin; + stdin->bufstart = bufin; + stdin->bufend = bufin + sizeof(bufin); + stdin->fd = 0; + stdin->mode = _IOFBF | __MODE_READ | __MODE_IOTRAN | __MODE_FREEBUF; + + _fixed_buffers[0].used = 1; + + stdout->bufpos = bufout = _fixed_buffers[1].data; /*(char *)malloc(BUFSIZ); */ + stdout->bufread = bufout; + stdout->bufwrite = bufout; + stdout->bufstart = bufout; + stdout->bufend = bufout + sizeof(bufout); + stdout->fd = 1; + stdout->mode = _IOFBF | __MODE_WRITE | __MODE_IOTRAN | __MODE_FREEBUF; + + _fixed_buffers[1].used = 1; #if 0 - stderr->bufpos = buferr = (char *)malloc(BUFSIZ); + stderr->bufpos = buferr = (char *) malloc(BUFSIZ); #else - stderr->bufpos = buferr; -#endif - stderr->bufread = buferr; - stderr->bufwrite = buferr; - stderr->bufstart = buferr; - stderr->bufend = buferr + sizeof(buferr); - stderr->fd = 2; - stderr->mode = _IONBF | __MODE_WRITE | __MODE_IOTRAN; - - if (isatty(1)) - stdout->mode |= _IOLBF; - atexit(__stdio_close_all); + stderr->bufpos = buferr; +#endif + stderr->bufread = buferr; + stderr->bufwrite = buferr; + stderr->bufstart = buferr; + stderr->bufend = buferr + sizeof(buferr); + stderr->fd = 2; + stderr->mode = _IONBF | __MODE_WRITE | __MODE_IOTRAN; + + if (isatty(1)) + stdout->mode |= _IOLBF; + atexit(__stdio_close_all); } #endif #ifdef L_fputc -int -fputc(ch, fp) -int ch; +int fputc(ch, fp) +int ch; FILE *fp; { - register int v; - Inline_init; + register int v; + + Inline_init; - v = fp->mode; - /* If last op was a read ... */ - if ((v & __MODE_READING) && fflush(fp)) - return EOF; + v = fp->mode; + /* If last op was a read ... */ + if ((v & __MODE_READING) && fflush(fp)) + return EOF; - /* Can't write or there's been an EOF or error then return EOF */ - if ((v & (__MODE_WRITE | __MODE_EOF | __MODE_ERR)) != __MODE_WRITE) - return EOF; + /* Can't write or there's been an EOF or error then return EOF */ + if ((v & (__MODE_WRITE | __MODE_EOF | __MODE_ERR)) != __MODE_WRITE) + return EOF; - /* In MSDOS translation mode */ + /* In MSDOS translation mode */ #if __MODE_IOTRAN - if (ch == '\n' && (v & __MODE_IOTRAN) && fputc('\r', fp) == EOF) - return EOF; + if (ch == '\n' && (v & __MODE_IOTRAN) && fputc('\r', fp) == EOF) + return EOF; #endif - /* Buffer is full */ - if (fp->bufpos >= fp->bufend && fflush(fp)) - return EOF; + /* Buffer is full */ + if (fp->bufpos >= fp->bufend && fflush(fp)) + return EOF; - /* Right! Do it! */ - *(fp->bufpos++) = ch; - fp->mode |= __MODE_WRITING; + /* Right! Do it! */ + *(fp->bufpos++) = ch; + fp->mode |= __MODE_WRITING; - /* Unbuffered or Line buffered and end of line */ - if (((ch == '\n' && (v & _IOLBF)) || (v & _IONBF)) - && fflush(fp)) - return EOF; + /* Unbuffered or Line buffered and end of line */ + if (((ch == '\n' && (v & _IOLBF)) || (v & _IONBF)) + && fflush(fp)) + return EOF; - /* Can the macro handle this by itself ? */ - if (v & (__MODE_IOTRAN | _IOLBF | _IONBF)) - fp->bufwrite = fp->bufstart; /* Nope */ - else - fp->bufwrite = fp->bufend; /* Yup */ + /* Can the macro handle this by itself ? */ + if (v & (__MODE_IOTRAN | _IOLBF | _IONBF)) + fp->bufwrite = fp->bufstart; /* Nope */ + else + fp->bufwrite = fp->bufend; /* Yup */ - /* Correct return val */ - return (unsigned char) ch; + /* Correct return val */ + return (unsigned char) ch; } #endif #ifdef L_fgetc -int -fgetc(fp) +int fgetc(fp) FILE *fp; { - int ch; - Inline_init; + int ch; - if (fp->mode & __MODE_WRITING) - fflush(fp); + Inline_init; + + if (fp->mode & __MODE_WRITING) + fflush(fp); #if __MODE_IOTRAN - try_again: -#endif - /* Can't read or there's been an EOF or error then return EOF */ - if ((fp->mode & (__MODE_READ | __MODE_EOF | __MODE_ERR)) != __MODE_READ) - return EOF; - - /* Nothing in the buffer - fill it up */ - if (fp->bufpos >= fp->bufread) - { - fp->bufpos = fp->bufread = fp->bufstart; - ch = fread(fp->bufpos, 1, fp->bufend - fp->bufstart, fp); - if (ch == 0) - return EOF; - fp->bufread += ch; - fp->mode |= __MODE_READING; - fp->mode &= ~__MODE_UNGOT; - } - ch = *(fp->bufpos++); + try_again: +#endif + /* Can't read or there's been an EOF or error then return EOF */ + if ((fp->mode & (__MODE_READ | __MODE_EOF | __MODE_ERR)) != + __MODE_READ) return EOF; + + /* Nothing in the buffer - fill it up */ + if (fp->bufpos >= fp->bufread) { + fp->bufpos = fp->bufread = fp->bufstart; + ch = fread(fp->bufpos, 1, fp->bufend - fp->bufstart, fp); + if (ch == 0) + return EOF; + fp->bufread += ch; + fp->mode |= __MODE_READING; + fp->mode &= ~__MODE_UNGOT; + } + ch = *(fp->bufpos++); #if __MODE_IOTRAN - /* In MSDOS translation mode; WARN: Doesn't work with UNIX macro */ - if (ch == '\r' && (fp->mode & __MODE_IOTRAN)) - goto try_again; + /* In MSDOS translation mode; WARN: Doesn't work with UNIX macro */ + if (ch == '\r' && (fp->mode & __MODE_IOTRAN)) + goto try_again; #endif - return ch; + return ch; } #endif #ifdef L_fflush -int -fflush(fp) +int fflush(fp) FILE *fp; { - int len, cc, rv=0; - char * bstart; - if (fp == NULL) /* On NULL flush the lot. */ - { - if (fflush(stdin)) - return EOF; - if (fflush(stdout)) - return EOF; - if (fflush(stderr)) - return EOF; - - for (fp = __IO_list; fp; fp = fp->next) - if (fflush(fp)) - return EOF; - - return 0; - } - - /* If there's output data pending */ - if (fp->mode & __MODE_WRITING) - { - len = fp->bufpos - fp->bufstart; - - if (len) - { - bstart = fp->bufstart; - /* - * The loop is so we don't get upset by signals or partial writes. - */ - do - { - cc = write(fp->fd, bstart, len); - if( cc > 0 ) - { - bstart+=cc; len-=cc; - } - } - while ( cc>0 || (cc == -1 && errno == EINTR)); - /* - * If we get here with len!=0 there was an error, exactly what to - * do about it is another matter ... - * - * I'll just clear the buffer. - */ - if (len) - { - fp->mode |= __MODE_ERR; - rv = EOF; - } - } - } - /* If there's data in the buffer sychronise the file positions */ - else if (fp->mode & __MODE_READING) - { - /* Humm, I think this means sync the file like fpurge() ... */ - /* Anyway the user isn't supposed to call this function when reading */ - - len = fp->bufread - fp->bufpos; /* Bytes buffered but unread */ - /* If it's a file, make it good */ - if (len > 0 && lseek(fp->fd, (long)-len, 1) < 0) - { - /* Hummm - Not certain here, I don't think this is reported */ - /* - * fp->mode |= __MODE_ERR; return EOF; - */ - } - } - - /* All done, no problem */ - fp->mode &= (~(__MODE_READING|__MODE_WRITING|__MODE_EOF|__MODE_UNGOT)); - fp->bufread = fp->bufwrite = fp->bufpos = fp->bufstart; - return rv; + int len, cc, rv = 0; + char *bstart; + + if (fp == NULL) { /* On NULL flush the lot. */ + if (fflush(stdin)) + return EOF; + if (fflush(stdout)) + return EOF; + if (fflush(stderr)) + return EOF; + + for (fp = __IO_list; fp; fp = fp->next) + if (fflush(fp)) + return EOF; + + return 0; + } + + /* If there's output data pending */ + if (fp->mode & __MODE_WRITING) { + len = fp->bufpos - fp->bufstart; + + if (len) { + bstart = fp->bufstart; + /* + * The loop is so we don't get upset by signals or partial writes. + */ + do { + cc = write(fp->fd, bstart, len); + if (cc > 0) { + bstart += cc; + len -= cc; + } + } + while (cc > 0 || (cc == -1 && errno == EINTR)); + /* + * If we get here with len!=0 there was an error, exactly what to + * do about it is another matter ... + * + * I'll just clear the buffer. + */ + if (len) { + fp->mode |= __MODE_ERR; + rv = EOF; + } + } + } + /* If there's data in the buffer sychronise the file positions */ + else if (fp->mode & __MODE_READING) { + /* Humm, I think this means sync the file like fpurge() ... */ + /* Anyway the user isn't supposed to call this function when reading */ + + len = fp->bufread - fp->bufpos; /* Bytes buffered but unread */ + /* If it's a file, make it good */ + if (len > 0 && lseek(fp->fd, (long) -len, 1) < 0) { + /* Hummm - Not certain here, I don't think this is reported */ + /* + * fp->mode |= __MODE_ERR; return EOF; + */ + } + } + + /* All done, no problem */ + fp->mode &= + (~(__MODE_READING | __MODE_WRITING | __MODE_EOF | __MODE_UNGOT)); + fp->bufread = fp->bufwrite = fp->bufpos = fp->bufstart; + return rv; } #endif #ifdef L_fgets /* Nothing special here ... */ -char * -fgets(s, count, f) +char *fgets(s, count, f) char *s; int count; FILE *f; { - char *ret; - register size_t i; - register int ch; - - ret = s; - for (i = count; i > 0; i--) - { - ch = getc(f); - if (ch == EOF) - { - if (s == ret) - return 0; - break; - } - *s++ = (char) ch; - if (ch == '\n') - break; - } - *s = 0; - - if (ferror(f)) - return 0; - return ret; + char *ret; + register size_t i; + register int ch; + + ret = s; + for (i = count; i > 0; i--) { + ch = getc(f); + if (ch == EOF) { + if (s == ret) + return 0; + break; + } + *s++ = (char) ch; + if (ch == '\n') + break; + } + *s = 0; + + if (ferror(f)) + return 0; + return ret; } #endif #ifdef L_gets -char * -gets(str) /* BAD function; DON'T use it! */ +char *gets(str) /* BAD function; DON'T use it! */ char *str; { - /* Auwlright it will work but of course _your_ program will crash */ - /* if it's given a too long line */ - register char *p = str; - register int c; - - while (((c = getc(stdin)) != EOF) && (c != '\n')) - *p++ = c; - *p = '\0'; - return (((c == EOF) && (p == str)) ? NULL : str); /* NULL == EOF */ + /* Auwlright it will work but of course _your_ program will crash */ + /* if it's given a too long line */ + register char *p = str; + register int c; + + while (((c = getc(stdin)) != EOF) && (c != '\n')) + *p++ = c; + *p = '\0'; + return (((c == EOF) && (p == str)) ? NULL : str); /* NULL == EOF */ } #endif #ifdef L_fputs -int -fputs(str, fp) +int fputs(str, fp) const char *str; FILE *fp; { - register int n = 0; - while (*str) - { - if (putc(*str++, fp) == EOF) - return (EOF); - ++n; - } - return (n); + register int n = 0; + + while (*str) { + if (putc(*str++, fp) == EOF) + return (EOF); + ++n; + } + return (n); } #endif #ifdef L_puts -int -puts(str) +int puts(str) const char *str; { - register int n; + register int n; - if (((n = fputs(str, stdout)) == EOF) - || (putc('\n', stdout) == EOF)) - return (EOF); - return (++n); + if (((n = fputs(str, stdout)) == EOF) + || (putc('\n', stdout) == EOF)) + return (EOF); + return (++n); } #endif @@ -407,56 +395,51 @@ const char *str; * This ignores __MODE__IOTRAN; probably exactly what you want. (It _is_ what * fgetc wants) */ -size_t -fread(buf, size, nelm, fp) -void *buf; -size_t size; -size_t nelm; -FILE *fp; +size_t fread(buf, size, nelm, fp) +void *buf; +size_t size; +size_t nelm; +FILE *fp; { - int len, v; - unsigned bytes, got = 0; - Inline_init; - - v = fp->mode; - - /* Want to do this to bring the file pointer up to date */ - if (v & __MODE_WRITING) - fflush(fp); - - /* Can't read or there's been an EOF or error then return zero */ - if ((v & (__MODE_READ | __MODE_EOF | __MODE_ERR)) != __MODE_READ) - return 0; - - /* This could be long, doesn't seem much point tho */ - bytes = size * nelm; - - len = fp->bufread - fp->bufpos; - if (len >= bytes) /* Enough buffered */ - { - memcpy(buf, fp->bufpos, (unsigned) bytes); - fp->bufpos += bytes; - return bytes; - } - else if (len > 0) /* Some buffered */ - { - memcpy(buf, fp->bufpos, len); - got = len; - } - - /* Need more; do it with a direct read */ - len = read(fp->fd, buf + got, (unsigned) (bytes - got)); - - /* Possibly for now _or_ later */ - if (len < 0) - { - fp->mode |= __MODE_ERR; - len = 0; - } - else if (len == 0) - fp->mode |= __MODE_EOF; - - return (got + len) / size; + int len, v; + unsigned bytes, got = 0; + + Inline_init; + + v = fp->mode; + + /* Want to do this to bring the file pointer up to date */ + if (v & __MODE_WRITING) + fflush(fp); + + /* Can't read or there's been an EOF or error then return zero */ + if ((v & (__MODE_READ | __MODE_EOF | __MODE_ERR)) != __MODE_READ) + return 0; + + /* This could be long, doesn't seem much point tho */ + bytes = size * nelm; + + len = fp->bufread - fp->bufpos; + if (len >= bytes) { /* Enough buffered */ + memcpy(buf, fp->bufpos, (unsigned) bytes); + fp->bufpos += bytes; + return bytes; + } else if (len > 0) { /* Some buffered */ + memcpy(buf, fp->bufpos, len); + got = len; + } + + /* Need more; do it with a direct read */ + len = read(fp->fd, buf + got, (unsigned) (bytes - got)); + + /* Possibly for now _or_ later */ + if (len < 0) { + fp->mode |= __MODE_ERR; + len = 0; + } else if (len == 0) + fp->mode |= __MODE_EOF; + + return (got + len) / size; } #endif @@ -469,22 +452,21 @@ FILE *fp; * * Again this ignores __MODE__IOTRAN. */ -size_t -fwrite(buf, size, nelm, fp) +size_t fwrite(buf, size, nelm, fp) const void *buf; -size_t size; -size_t nelm; -FILE *fp; +size_t size; +size_t nelm; +FILE *fp; { - register int v; - int len; - unsigned bytes, put; + register int v; + int len; + unsigned bytes, put; #ifdef STUB_FWRITE bytes = size * nelm; - while(bytes>0) { - len=write(fp->fd, buf, bytes); - if (len<=0) { + while (bytes > 0) { + len = write(fp->fd, buf, bytes); + if (len <= 0) { break; } bytes -= len; @@ -492,125 +474,119 @@ FILE *fp; } return nelm; #else - - v = fp->mode; - /* If last op was a read ... */ - if ((v & __MODE_READING) && fflush(fp)) - return 0; - - /* Can't write or there's been an EOF or error then return 0 */ - if ((v & (__MODE_WRITE | __MODE_EOF | __MODE_ERR)) != __MODE_WRITE) - return 0; - - /* This could be long, doesn't seem much point tho */ - bytes = size * nelm; - - len = fp->bufend - fp->bufpos; - - /* Flush the buffer if not enough room */ - if (bytes > len) - if (fflush(fp)) - return 0; - - len = fp->bufend - fp->bufpos; - if (bytes <= len) /* It'll fit in the buffer ? */ - { - fp->mode |= __MODE_WRITING; - memcpy(fp->bufpos, buf, bytes); - fp->bufpos += bytes; - - /* If we're not fully buffered */ - if (v & (_IOLBF | _IONBF)) - fflush(fp); - - return nelm; - } - else - /* Too big for the buffer */ - { - put = bytes; - do - { - len = write(fp->fd, buf, bytes); - if( len > 0 ) - { - buf+=len; bytes-=len; - } - } - while (len > 0 || (len == -1 && errno == EINTR)); - - if (len < 0) - fp->mode |= __MODE_ERR; - - put -= bytes; - } - - return put / size; + + v = fp->mode; + /* If last op was a read ... */ + if ((v & __MODE_READING) && fflush(fp)) + return 0; + + /* Can't write or there's been an EOF or error then return 0 */ + if ((v & (__MODE_WRITE | __MODE_EOF | __MODE_ERR)) != __MODE_WRITE) + return 0; + + /* This could be long, doesn't seem much point tho */ + bytes = size * nelm; + + len = fp->bufend - fp->bufpos; + + /* Flush the buffer if not enough room */ + if (bytes > len) + if (fflush(fp)) + return 0; + + len = fp->bufend - fp->bufpos; + if (bytes <= len) { /* It'll fit in the buffer ? */ + fp->mode |= __MODE_WRITING; + memcpy(fp->bufpos, buf, bytes); + fp->bufpos += bytes; + + /* If we're not fully buffered */ + if (v & (_IOLBF | _IONBF)) + fflush(fp); + + return nelm; + } else + /* Too big for the buffer */ + { + put = bytes; + do { + len = write(fp->fd, buf, bytes); + if (len > 0) { + buf += len; + bytes -= len; + } + } + while (len > 0 || (len == -1 && errno == EINTR)); + + if (len < 0) + fp->mode |= __MODE_ERR; + + put -= bytes; + } + + return put / size; #endif } #endif #ifdef L_rewind -void -rewind(fp) -FILE * fp; +void rewind(fp) +FILE *fp; { - fseek(fp, (long)0, 0); - clearerr(fp); + fseek(fp, (long) 0, 0); + clearerr(fp); } #endif #ifdef L_fseek -int -fseek(fp, offset, ref) +int fseek(fp, offset, ref) FILE *fp; -long offset; -int ref; +long offset; +int ref; { -#if 0 - /* FIXME: this is broken! BROKEN!!!! */ - /* if __MODE_READING and no ungetc ever done can just move pointer */ - /* This needs testing! */ - - if ( (fp->mode &(__MODE_READING | __MODE_UNGOT)) == __MODE_READING && - ( ref == SEEK_SET || ref == SEEK_CUR )) - { - long fpos = lseek(fp->fd, 0L, SEEK_CUR); - if( fpos == -1 ) return EOF; - - if( ref == SEEK_CUR ) - { - ref = SEEK_SET; - offset = fpos + offset + fp->bufpos - fp->bufread; - } - if( ref == SEEK_SET ) - { - if ( offset < fpos && offset >= fpos + fp->bufstart - fp->bufread ) - { - fp->bufpos = offset - fpos + fp->bufread; - return 0; - } - } - } -#endif - - /* Use fflush to sync the pointers */ - - if (fflush(fp) == EOF) - return EOF; - if (lseek(fp->fd, offset, ref) < 0) - return EOF; - return 0; +#if 0 + /* FIXME: this is broken! BROKEN!!!! */ + /* if __MODE_READING and no ungetc ever done can just move pointer */ + /* This needs testing! */ + + if ((fp->mode & (__MODE_READING | __MODE_UNGOT)) == __MODE_READING && + (ref == SEEK_SET || ref == SEEK_CUR)) { + long fpos = lseek(fp->fd, 0L, SEEK_CUR); + + if (fpos == -1) + return EOF; + + if (ref == SEEK_CUR) { + ref = SEEK_SET; + offset = fpos + offset + fp->bufpos - fp->bufread; + } + if (ref == SEEK_SET) { + if (offset < fpos + && offset >= fpos + fp->bufstart - fp->bufread) { + fp->bufpos = offset - fpos + fp->bufread; + return 0; + } + } + } +#endif + + /* Use fflush to sync the pointers */ + + if (fflush(fp) == EOF) + return EOF; + if (lseek(fp->fd, offset, ref) < 0) + return EOF; + return 0; } #endif #ifdef L_ftell long ftell(fp) -FILE * fp; +FILE *fp; { - if (fflush(fp) == EOF) - return EOF; - return lseek(fp->fd, 0L, SEEK_CUR); + if (fflush(fp) == EOF) + return EOF; + return lseek(fp->fd, 0L, SEEK_CUR); } #endif @@ -619,311 +595,299 @@ FILE * fp; * This Fopen is all three of fopen, fdopen and freopen. The macros in * stdio.h show the other names. */ -FILE * -__fopen(fname, fd, fp, mode) +FILE *__fopen(fname, fd, fp, mode) const char *fname; -int fd; +int fd; FILE *fp; const char *mode; { - int open_mode = 0; + int open_mode = 0; + #if __MODE_IOTRAN - int do_iosense = 1; -#endif - int fopen_mode = 0; - FILE *nfp = 0; - - /* If we've got an fp close the old one (freopen) */ - if (fp) - { - /* Careful, don't de-allocate it */ - fopen_mode |= (fp->mode & (__MODE_BUF | __MODE_FREEFIL | __MODE_FREEBUF)); - fp->mode &= ~(__MODE_FREEFIL | __MODE_FREEBUF); - fclose(fp); - } - - /* decode the new open mode */ - while (*mode) - switch (*mode++) - { - case 'r': - fopen_mode |= __MODE_READ; - break; - case 'w': - fopen_mode |= __MODE_WRITE; - open_mode = (O_CREAT | O_TRUNC); - break; - case 'a': - fopen_mode |= __MODE_WRITE; - open_mode = (O_CREAT | O_APPEND); - break; - case '+': - fopen_mode |= __MODE_RDWR; - break; + int do_iosense = 1; +#endif + int fopen_mode = 0; + FILE *nfp = 0; + + /* If we've got an fp close the old one (freopen) */ + if (fp) { + /* Careful, don't de-allocate it */ + fopen_mode |= + (fp->mode & (__MODE_BUF | __MODE_FREEFIL | __MODE_FREEBUF)); + fp->mode &= ~(__MODE_FREEFIL | __MODE_FREEBUF); + fclose(fp); + } + + /* decode the new open mode */ + while (*mode) + switch (*mode++) { + case 'r': + fopen_mode |= __MODE_READ; + break; + case 'w': + fopen_mode |= __MODE_WRITE; + open_mode = (O_CREAT | O_TRUNC); + break; + case 'a': + fopen_mode |= __MODE_WRITE; + open_mode = (O_CREAT | O_APPEND); + break; + case '+': + fopen_mode |= __MODE_RDWR; + break; #if __MODE_IOTRAN - case 'b': /* Binary */ - fopen_mode &= ~__MODE_IOTRAN; - do_iosense=0; - break; - case 't': /* Text */ - fopen_mode |= __MODE_IOTRAN; - do_iosense=0; - break; -#endif - } - - /* Add in the read/write options to mode for open() */ - switch (fopen_mode & (__MODE_READ | __MODE_WRITE)) - { - case 0: - return 0; - case __MODE_READ: - open_mode |= O_RDONLY; - break; - case __MODE_WRITE: - open_mode |= O_WRONLY; - break; - default: - open_mode |= O_RDWR; - break; - } - - /* Allocate the (FILE) before we do anything irreversable */ - if (fp == 0) - { - nfp = malloc(sizeof(FILE)); - if (nfp == 0) - return 0; - } - - /* Open the file itself */ - if (fname) - fd = open(fname, open_mode, 0666); - if (fd < 0) /* Grrrr */ - { - if (nfp) - free(nfp); - return 0; - } - - /* If this isn't freopen create a (FILE) and buffer for it */ - if (fp == 0) - { - int i; - fp = nfp; - fp->next = __IO_list; - __IO_list = fp; - - fp->mode = __MODE_FREEFIL; - if( isatty(fd) ) - { - fp->mode |= _IOLBF; + case 'b': /* Binary */ + fopen_mode &= ~__MODE_IOTRAN; + do_iosense = 0; + break; + case 't': /* Text */ + fopen_mode |= __MODE_IOTRAN; + do_iosense = 0; + break; +#endif + } + + /* Add in the read/write options to mode for open() */ + switch (fopen_mode & (__MODE_READ | __MODE_WRITE)) { + case 0: + return 0; + case __MODE_READ: + open_mode |= O_RDONLY; + break; + case __MODE_WRITE: + open_mode |= O_WRONLY; + break; + default: + open_mode |= O_RDWR; + break; + } + + /* Allocate the (FILE) before we do anything irreversable */ + if (fp == 0) { + nfp = malloc(sizeof(FILE)); + if (nfp == 0) + return 0; + } + + /* Open the file itself */ + if (fname) + fd = open(fname, open_mode, 0666); + if (fd < 0) { /* Grrrr */ + if (nfp) + free(nfp); + return 0; + } + + /* If this isn't freopen create a (FILE) and buffer for it */ + if (fp == 0) { + int i; + + fp = nfp; + fp->next = __IO_list; + __IO_list = fp; + + fp->mode = __MODE_FREEFIL; + if (isatty(fd)) { + fp->mode |= _IOLBF; #if __MODE_IOTRAN - if( do_iosense ) fopen_mode |= __MODE_IOTRAN; -#endif - } - else - fp->mode |= _IOFBF; - - for(i=0;ibufstart = _fixed_buffers[i].data; - _fixed_buffers[i].used = 1; - break; - } - - if (i == FIXED_BUFFERS) - fp->bufstart = malloc(BUFSIZ); - - if (fp->bufstart == 0) /* Oops, no mem */ - { /* Humm, full buffering with a two(!) byte - * buffer. */ - fp->bufstart = fp->unbuf; - fp->bufend = fp->unbuf + sizeof(fp->unbuf); - } - else - { - fp->bufend = fp->bufstart + BUFSIZ; - fp->mode |= __MODE_FREEBUF; - } - } - /* Ok, file's ready clear the buffer and save important bits */ - fp->bufpos = fp->bufread = fp->bufwrite = fp->bufstart; - fp->mode |= fopen_mode; - fp->fd = fd; - return fp; + if (do_iosense) + fopen_mode |= __MODE_IOTRAN; +#endif + } else + fp->mode |= _IOFBF; + + for (i = 0; i < FIXED_BUFFERS; i++) + if (!_fixed_buffers[i].used) { + fp->bufstart = _fixed_buffers[i].data; + _fixed_buffers[i].used = 1; + break; + } + + if (i == FIXED_BUFFERS) + fp->bufstart = malloc(BUFSIZ); + + if (fp->bufstart == 0) { /* Oops, no mem *//* Humm, full buffering with a two(!) byte + * buffer. */ + fp->bufstart = fp->unbuf; + fp->bufend = fp->unbuf + sizeof(fp->unbuf); + } else { + fp->bufend = fp->bufstart + BUFSIZ; + fp->mode |= __MODE_FREEBUF; + } + } + /* Ok, file's ready clear the buffer and save important bits */ + fp->bufpos = fp->bufread = fp->bufwrite = fp->bufstart; + fp->mode |= fopen_mode; + fp->fd = fd; + return fp; } #endif #ifdef L_fclose -int -fclose(fp) +int fclose(fp) FILE *fp; { - int rv = 0; - - if (fp == 0) - { - errno = EINVAL; - return EOF; - } - if (fflush(fp)) - return EOF; - - if (close(fp->fd)) - rv = EOF; - fp->fd = -1; - - if (fp->mode & __MODE_FREEBUF) - { - int i; - for(i=0;ibufstart == _fixed_buffers[i].data) { - _fixed_buffers[i].used = 0; - break; - } - if(i==FIXED_BUFFERS) - free(fp->bufstart); - fp->mode &= ~__MODE_FREEBUF; - fp->bufstart = fp->bufend = 0; - } - - if (fp->mode & __MODE_FREEFIL) - { - FILE *prev = 0, *ptr; - fp->mode = 0; - - for (ptr = __IO_list; ptr && ptr != fp; ptr = ptr->next) - ; - if (ptr == fp) - { - if (prev == 0) - __IO_list = fp->next; - else - prev->next = fp->next; - } - free(fp); - } - else - fp->mode = 0; - - return rv; + int rv = 0; + + if (fp == 0) { + errno = EINVAL; + return EOF; + } + if (fflush(fp)) + return EOF; + + if (close(fp->fd)) + rv = EOF; + fp->fd = -1; + + if (fp->mode & __MODE_FREEBUF) { + int i; + + for (i = 0; i < FIXED_BUFFERS; i++) + if (fp->bufstart == _fixed_buffers[i].data) { + _fixed_buffers[i].used = 0; + break; + } + if (i == FIXED_BUFFERS) + free(fp->bufstart); + fp->mode &= ~__MODE_FREEBUF; + fp->bufstart = fp->bufend = 0; + } + + if (fp->mode & __MODE_FREEFIL) { + FILE *prev = 0, *ptr; + + fp->mode = 0; + + for (ptr = __IO_list; ptr && ptr != fp; ptr = ptr->next); + if (ptr == fp) { + if (prev == 0) + __IO_list = fp->next; + else + prev->next = fp->next; + } + free(fp); + } else + fp->mode = 0; + + return rv; } #endif #ifdef L_setbuffer -void -setbuffer(fp, buf, size) -FILE * fp; -char * buf; +void setbuffer(fp, buf, size) +FILE *fp; +char *buf; int size; { - fflush(fp); - - if ((fp->bufstart == (unsigned char*)buf) && (fp->bufend == ((unsigned char*)buf + size))) - return; - - if( fp->mode & __MODE_FREEBUF ) { - int i; - for(i=0;ibufstart == _fixed_buffers[i].data) { - _fixed_buffers[i].used = 0; - break; - } - if(i==FIXED_BUFFERS) - free(fp->bufstart); - } - fp->mode &= ~(__MODE_FREEBUF|__MODE_BUF); - - if( buf == 0 ) - { - fp->bufstart = fp->unbuf; - fp->bufend = fp->unbuf + sizeof(fp->unbuf); - fp->mode |= _IONBF; - } - else - { - fp->bufstart = buf; - fp->bufend = buf+size; - fp->mode |= _IOFBF; - } - fp->bufpos = fp->bufread = fp->bufwrite = fp->bufstart; + fflush(fp); + + if ((fp->bufstart == (unsigned char *) buf) + && (fp->bufend == ((unsigned char *) buf + size))) + return; + + if (fp->mode & __MODE_FREEBUF) { + int i; + + for (i = 0; i < FIXED_BUFFERS; i++) + if (fp->bufstart == _fixed_buffers[i].data) { + _fixed_buffers[i].used = 0; + break; + } + if (i == FIXED_BUFFERS) + free(fp->bufstart); + } + fp->mode &= ~(__MODE_FREEBUF | __MODE_BUF); + + if (buf == 0) { + fp->bufstart = fp->unbuf; + fp->bufend = fp->unbuf + sizeof(fp->unbuf); + fp->mode |= _IONBF; + } else { + fp->bufstart = buf; + fp->bufend = buf + size; + fp->mode |= _IOFBF; + } + fp->bufpos = fp->bufread = fp->bufwrite = fp->bufstart; } #endif #ifdef L_setvbuf int setvbuf(fp, buf, mode, size) -FILE * fp; -char * buf; +FILE *fp; +char *buf; int mode; size_t size; { - fflush(fp); - if( fp->mode & __MODE_FREEBUF ) { - int i; - for(i=0;ibufstart == _fixed_buffers[i].data) { - _fixed_buffers[i].used = 0; - break; - } - if(i==FIXED_BUFFERS) - free(fp->bufstart); - } - fp->mode &= ~(__MODE_FREEBUF|__MODE_BUF); - fp->bufstart = fp->unbuf; - fp->bufend = fp->unbuf + sizeof(fp->unbuf); - fp->mode |= _IONBF; - - if( mode == _IOFBF || mode == _IOLBF ) - { - if( size <= 0 ) { size = BUFSIZ; } - if( buf == 0 ) { - if (size == BUFSIZ) { - int i; - for(i=0;ibufstart = buf; - fp->bufend = buf+size; - fp->mode |= mode; - } - fp->bufpos = fp->bufread = fp->bufwrite = fp->bufstart; - return 0; + fflush(fp); + if (fp->mode & __MODE_FREEBUF) { + int i; + + for (i = 0; i < FIXED_BUFFERS; i++) + if (fp->bufstart == _fixed_buffers[i].data) { + _fixed_buffers[i].used = 0; + break; + } + if (i == FIXED_BUFFERS) + free(fp->bufstart); + } + fp->mode &= ~(__MODE_FREEBUF | __MODE_BUF); + fp->bufstart = fp->unbuf; + fp->bufend = fp->unbuf + sizeof(fp->unbuf); + fp->mode |= _IONBF; + + if (mode == _IOFBF || mode == _IOLBF) { + if (size <= 0) { + size = BUFSIZ; + } + if (buf == 0) { + if (size == BUFSIZ) { + int i; + + for (i = 0; i < FIXED_BUFFERS; i++) + if (!_fixed_buffers[i].used) { + _fixed_buffers[i].used = 1; + buf = _fixed_buffers[i].data; + break; + } + if (i == FIXED_BUFFERS) + buf = malloc(size); + } else { + buf = malloc(size); + } + } + if (buf == 0) + return EOF; + + fp->bufstart = buf; + fp->bufend = buf + size; + fp->mode |= mode; + } + fp->bufpos = fp->bufread = fp->bufwrite = fp->bufstart; + return 0; } #endif #ifdef L_ungetc -int -ungetc(c, fp) +int ungetc(c, fp) int c; FILE *fp; { - if (fp->mode & __MODE_WRITING) - fflush(fp); - - /* Can't read or there's been an error then return EOF */ - if ((fp->mode & (__MODE_READ | __MODE_ERR)) != __MODE_READ) - return EOF; - - /* Can't do fast fseeks */ - fp->mode |= __MODE_UNGOT; - - if( fp->bufpos > fp->bufstart ) - return *--fp->bufpos = (unsigned char) c; - else if( fp->bufread == fp->bufstart ) - return *fp->bufread++ = (unsigned char) c; - else - return EOF; + if (fp->mode & __MODE_WRITING) + fflush(fp); + + /* Can't read or there's been an error then return EOF */ + if ((fp->mode & (__MODE_READ | __MODE_ERR)) != __MODE_READ) + return EOF; + + /* Can't do fast fseeks */ + fp->mode |= __MODE_UNGOT; + + if (fp->bufpos > fp->bufstart) + return *--fp->bufpos = (unsigned char) c; + else if (fp->bufread == fp->bufstart) + return *fp->bufread++ = (unsigned char) c; + else + return EOF; } #endif diff --git a/libc/stdlib/.indent.pro b/libc/stdlib/.indent.pro new file mode 100644 index 000000000..492ecf1c7 --- /dev/null +++ b/libc/stdlib/.indent.pro @@ -0,0 +1,33 @@ +--blank-lines-after-declarations +--blank-lines-after-procedures +--break-before-boolean-operator +--no-blank-lines-after-commas +--braces-on-if-line +--braces-on-struct-decl-line +--comment-indentation25 +--declaration-comment-column25 +--no-comment-delimiters-on-blank-lines +--cuddle-else +--continuation-indentation4 +--case-indentation0 +--else-endif-column33 +--space-after-cast +--line-comments-indentation0 +--declaration-indentation1 +--dont-format-first-column-comments +--dont-format-comments +--honour-newlines +--indent-level4 +/* changed from 0 to 4 */ +--parameter-indentation4 +--line-length78 /* changed from 75 */ +--continue-at-parentheses +--no-space-after-function-call-names +--dont-break-procedure-type +--dont-star-comments +--leave-optional-blank-lines +--dont-space-special-semicolon +--tab-size4 +/* additions by Mark */ +--case-brace-indentation0 +--leave-preprocessor-space diff --git a/libc/stdlib/Makefile b/libc/stdlib/Makefile index 14ebec492..da402be6c 100644 --- a/libc/stdlib/Makefile +++ b/libc/stdlib/Makefile @@ -35,12 +35,15 @@ CSRC=atoi.c atol.c ltoa.c ltostr.c ctype.c qsort.c bsearch.c rand.c lsearch.c \ mkstemp.c mktemp.c realpath.c getenv.c putenv.c popen.c system.c \ getcwd.c setenv.c execl.c execv.c execlp.c execvp.c execvep.c COBJS=$(patsubst %.c,%.o, $(CSRC)) +OBJS=$(MOBJ) $(MOBJ2) $(COBJS) -all: $(MOBJ) $(MOBJ2) $(COBJS) $(LIBC) +all: $(OBJS) $(LIBC) -$(LIBC): $(MOBJ) $(MOBJ2) $(COBJS) - $(AR) $(ARFLAGS) $(LIBC) $(MOBJ) $(MOBJ2) $(COBJS) +$(LIBC): ar-target + +ar-target: $(OBJS) + $(AR) $(ARFLAGS) $(LIBC) $(OBJS) $(MOBJ): $(MSRC) $(CC) $(CFLAGS) -DL_$* $< -c -o $*.o @@ -48,6 +51,8 @@ $(MOBJ): $(MSRC) $(MOBJ2): $(MSRC2) $(CC) $(CFLAGS) -DL_$* $< -c -o $*.o +$(OBJ): Makefile + clean: rm -f *.[oa] *~ core diff --git a/libc/stdlib/atexit.c b/libc/stdlib/atexit.c index 7c6a03af8..1c164ff86 100644 --- a/libc/stdlib/atexit.c +++ b/libc/stdlib/atexit.c @@ -14,20 +14,18 @@ #include /* ATEXIT.H */ -#define MAXONEXIT 20 /* AIUI Posix requires 10 */ +#define MAXONEXIT 20 /* AIUI Posix requires 10 */ typedef void (*vfuncp) (); extern vfuncp __cleanup; extern void __do_exit(); -extern void _exit __P ((int __status)) __attribute__ ((__noreturn__)); +extern void _exit __P((int __status)) __attribute__ ((__noreturn__)); -extern struct exit_table -{ - vfuncp called; - void *argument; -} -__on_exit_table[MAXONEXIT]; +extern struct exit_table { + vfuncp called; + void *argument; +} __on_exit_table[MAXONEXIT]; extern int __on_exit_count; @@ -36,83 +34,75 @@ extern int __on_exit_count; #ifdef L_atexit vfuncp __cleanup; -int -atexit(ptr) +int atexit(ptr) vfuncp ptr; { - if( __on_exit_count < 0 || __on_exit_count >= MAXONEXIT) - { - errno = ENOMEM; - return -1; - } - __cleanup = __do_exit; - if( ptr ) - { - __on_exit_table[__on_exit_count].called = ptr; - __on_exit_table[__on_exit_count].argument = 0; - __on_exit_count++; - } - return 0; + if (__on_exit_count < 0 || __on_exit_count >= MAXONEXIT) { + errno = ENOMEM; + return -1; + } + __cleanup = __do_exit; + if (ptr) { + __on_exit_table[__on_exit_count].called = ptr; + __on_exit_table[__on_exit_count].argument = 0; + __on_exit_count++; + } + return 0; } #endif #ifdef L_on_exit -int -on_exit(ptr, arg) +int on_exit(ptr, arg) vfuncp ptr; void *arg; { - if( __on_exit_count < 0 || __on_exit_count >= MAXONEXIT) - { - errno = ENOMEM; - return -1; - } - __cleanup = __do_exit; - if( ptr ) - { - __on_exit_table[__on_exit_count].called = ptr; - __on_exit_table[__on_exit_count].argument = arg; - __on_exit_count++; - } - return 0; + if (__on_exit_count < 0 || __on_exit_count >= MAXONEXIT) { + errno = ENOMEM; + return -1; + } + __cleanup = __do_exit; + if (ptr) { + __on_exit_table[__on_exit_count].called = ptr; + __on_exit_table[__on_exit_count].argument = arg; + __on_exit_count++; + } + return 0; } #endif #ifdef L___do_exit -int __on_exit_count = 0; +int __on_exit_count = 0; struct exit_table __on_exit_table[MAXONEXIT]; -void -__do_exit(rv) -int rv; +void __do_exit(rv) +int rv; { - register int count = __on_exit_count-1; - register vfuncp ptr; - __on_exit_count = -1; /* ensure no more will be added */ - __cleanup = 0; /* Calling exit won't re-do this */ - - /* In reverse order */ - for (; count >= 0; count--) - { - ptr = __on_exit_table[count].called; - (*ptr) (rv, __on_exit_table[count].argument); - } + register int count = __on_exit_count - 1; + register vfuncp ptr; + + __on_exit_count = -1; /* ensure no more will be added */ + __cleanup = 0; /* Calling exit won't re-do this */ + + /* In reverse order */ + for (; count >= 0; count--) { + ptr = __on_exit_table[count].called; + (*ptr) (rv, __on_exit_table[count].argument); + } } #endif #ifdef L_exit -void -exit(rv) -int rv; +void exit(rv) +int rv; { - if (__cleanup) - __cleanup(); - _exit(rv); + if (__cleanup) + __cleanup(); + _exit(rv); } #endif diff --git a/libc/stdlib/bsearch.c b/libc/stdlib/bsearch.c index 72ba2617a..04d5ab68b 100644 --- a/libc/stdlib/bsearch.c +++ b/libc/stdlib/bsearch.c @@ -10,37 +10,33 @@ */ #include -static int _bsearch; /* index of element found, or where to - * insert */ +static int _bsearch; /* index of element found, or where to -char * -bsearch(key, base, num, size, cmp) -register char *key; /* item to search for */ -register char *base; /* base address */ -int num; /* number of elements */ -register int size; /* element size in bytes */ -register int (*cmp) (); /* comparison function */ + * insert */ + +char *bsearch(key, base, num, size, cmp) +register char *key; /* item to search for */ +register char *base; /* base address */ +int num; /* number of elements */ +register int size; /* element size in bytes */ +register int (*cmp) (); /* comparison function */ { - register int a, b, c, dir; + register int a, b, c, dir; - a = 0; - b = num - 1; - while (a <= b) - { - c = (a + b) >> 1; /* == ((a + b) / 2) */ - if ((dir = (*cmp) ((base + (c * size)), key))) - { - if (dir > 0) - b = c - 1; - else /* (dir < 0) */ - a = c + 1; - } - else - { - _bsearch = c; - return (base + (c * size)); - } - } - _bsearch = b; - return (NULL); + a = 0; + b = num - 1; + while (a <= b) { + c = (a + b) >> 1; /* == ((a + b) / 2) */ + if ((dir = (*cmp) ((base + (c * size)), key))) { + if (dir > 0) + b = c - 1; + else /* (dir < 0) */ + a = c + 1; + } else { + _bsearch = c; + return (base + (c * size)); + } + } + _bsearch = b; + return (NULL); } diff --git a/libc/stdlib/getenv.c b/libc/stdlib/getenv.c index 1ed83a622..b5d4de9aa 100644 --- a/libc/stdlib/getenv.c +++ b/libc/stdlib/getenv.c @@ -6,26 +6,22 @@ #include #include -extern char ** environ; +extern char **environ; -char * -getenv(var) -const char * var; +char *getenv(var) +const char *var; { - char **p; - int len; + char **p; + int len; - len = strlen(var); - - if (!environ) - return 0; - - for(p=environ; *p; p++) - { - if( memcmp(var, *p, len) == 0 && (*p)[len] == '=' ) - return *p + len + 1; - } - return 0; -} + len = strlen(var); + if (!environ) + return 0; + for (p = environ; *p; p++) { + if (memcmp(var, *p, len) == 0 && (*p)[len] == '=') + return *p + len + 1; + } + return 0; +} diff --git a/libc/stdlib/malloc-simple/.indent.pro b/libc/stdlib/malloc-simple/.indent.pro new file mode 100644 index 000000000..492ecf1c7 --- /dev/null +++ b/libc/stdlib/malloc-simple/.indent.pro @@ -0,0 +1,33 @@ +--blank-lines-after-declarations +--blank-lines-after-procedures +--break-before-boolean-operator +--no-blank-lines-after-commas +--braces-on-if-line +--braces-on-struct-decl-line +--comment-indentation25 +--declaration-comment-column25 +--no-comment-delimiters-on-blank-lines +--cuddle-else +--continuation-indentation4 +--case-indentation0 +--else-endif-column33 +--space-after-cast +--line-comments-indentation0 +--declaration-indentation1 +--dont-format-first-column-comments +--dont-format-comments +--honour-newlines +--indent-level4 +/* changed from 0 to 4 */ +--parameter-indentation4 +--line-length78 /* changed from 75 */ +--continue-at-parentheses +--no-space-after-function-call-names +--dont-break-procedure-type +--dont-star-comments +--leave-optional-blank-lines +--dont-space-special-semicolon +--tab-size4 +/* additions by Mark */ +--case-brace-indentation0 +--leave-preprocessor-space diff --git a/libc/stdlib/malloc-simple/Makefile b/libc/stdlib/malloc-simple/Makefile new file mode 100644 index 000000000..c2f8827b5 --- /dev/null +++ b/libc/stdlib/malloc-simple/Makefile @@ -0,0 +1,46 @@ +# Makefile for uCLibc +# +# Copyright (C) 2000 by Lineo, inc. +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU Library General Public License as published by the Free +# Software Foundation; either version 2 of the License, or (at your option) any +# later version. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +# details. +# +# You should have received a copy of the GNU General Public License along with +# this program; if not, write to the Free Software Foundation, Inc., 59 Temple +# Place, Suite 330, Boston, MA 02111-1307 USA +# +# Derived in part from the Linux-8086 C library, the GNU C Library, and several +# other sundry sources. Files within this library are copyright by their +# respective copyright holders. + +TOPDIR=../ +include $(TOPDIR)Rules.make +LIBC=$(TOPDIR)libc.a + +MSRC=alloc.c +MOBJ=malloc.o realloc.o free.o calloc.o malloc_dbg.o free_dbg.o calloc_dbg.o +OBJS=$(MOBJ) + + +all: $(OBJS) $(LIBC) + +$(LIBC): ar-target + +ar-target: $(OBJS) + $(AR) $(ARFLAGS) $(LIBC) $(OBJS) + +$(MOBJ): $(MSRC) + $(CC) $(CFLAGS) -DL_$* $< -c -o $*.o + +$(OBJ): Makefile + +clean: + rm -f *.[oa] *~ core + diff --git a/libc/stdlib/malloc-simple/alloc.c b/libc/stdlib/malloc-simple/alloc.c new file mode 100644 index 000000000..cf4835c97 --- /dev/null +++ b/libc/stdlib/malloc-simple/alloc.c @@ -0,0 +1,106 @@ +#include +#include +#include +#include +#include +#include + + +#ifdef L_calloc_dbg + +void *calloc_dbg(size_t num, size_t size, char *function, char *file, + int line) +{ + void *ptr; + + fprintf(stderr, "calloc of %d bytes at %s @%s:%d = ", num * size, + function, file, line); + ptr = calloc(num, size); + fprintf(stderr, "%p\n", ptr); + return ptr; +} + +#endif + +#ifdef L_malloc_dbg + +void *malloc_dbg(size_t len, char *function, char *file, int line) +{ + void *result; + + fprintf(stderr, "malloc of %d bytes at %s @%s:%d = ", len, function, + file, line); + result = malloc(len); + fprintf(stderr, "%p\n", result); + return result; +} + +#endif + +#ifdef L_free_dbg + +void free_dbg(void *ptr, char *function, char *file, int line) +{ + fprintf(stderr, "free of %p at %s @%s:%d\n", ptr, function, file, + line); + free(ptr); +} + +#endif + + +#ifdef L_calloc + +void *calloc(size_t num, size_t size) +{ + void *ptr = malloc(num * size); + + if (ptr) + memset(ptr, 0, num * size); + return ptr; +} + +#endif + +#ifdef L_malloc + +void *malloc(size_t len) +{ + void *result = mmap((void *) 0, len, PROT_READ | PROT_WRITE, + //MAP_SHARED | MAP_ANONYMOUS, 0, 0); + MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); + + if (result == (void *) -1) + return 0; + + return result; +} + +#endif + +#ifdef L_free + +void free(void *ptr) +{ + munmap(ptr, 0); +} + +#endif + +#ifdef L_realloc + +void *realloc(void *ptr, size_t size) +{ + void *newptr = NULL; + + if (size > 0) { + newptr = malloc(size); + if (newptr && ptr) + memcpy(newptr, ptr, size); + } + if (ptr) + free(ptr); + return newptr; +} + +#endif diff --git a/libc/stdlib/malloc/Makefile b/libc/stdlib/malloc/Makefile index ba2567f12..fe3f8b424 100644 --- a/libc/stdlib/malloc/Makefile +++ b/libc/stdlib/malloc/Makefile @@ -27,13 +27,22 @@ LIBC=$(TOPDIR)libc.a CSRC=malloc.c COBJS=$(patsubst %.c,%.o, $(CSRC)) +MSRC=alloc.c +MOBJ=malloc_dbg.o free_dbg.o calloc_dbg.o +OBJS=$(COBJS) $(MOBJ) -all: $(COBJS) $(LIBC) +all: $(OBJS) $(LIBC) -$(LIBC): $(COBJS) - $(AR) $(ARFLAGS) $(LIBC) $(COBJS) +$(LIBC): ar-target -$(COBJS): Makefile +ar-target: $(OBJS) + $(AR) $(ARFLAGS) $(LIBC) $(OBJS) + +$(MOBJ): $(MSRC) + $(CC) $(CFLAGS) -DL_$* $< -c -o $*.o + +$(OBJ): Makefile clean: rm -f *.[oa] *~ core + diff --git a/libc/stdlib/malloc/alloc.c b/libc/stdlib/malloc/alloc.c new file mode 100644 index 000000000..b782f6dcf --- /dev/null +++ b/libc/stdlib/malloc/alloc.c @@ -0,0 +1,48 @@ +#include +#include +#include +#include +#include +#include + + +#ifdef L_calloc_dbg + +void * +calloc_dbg(size_t num, size_t size, char * function, char * file, int line) +{ + void * ptr; + fprintf(stderr, "calloc of %d bytes at %s @%s:%d = ", num*size, function, file, line); + ptr = calloc(num,size); + fprintf(stderr, "%p\n", ptr); + return ptr; +} + +#endif + +#ifdef L_malloc_dbg + +void * +malloc_dbg(size_t len, char * function, char * file, int line) +{ + void * result; + fprintf(stderr, "malloc of %d bytes at %s @%s:%d = ", len, function, file, line); + result = malloc(len); + fprintf(stderr, "%p\n", result); + return result; +} + +#endif + +#ifdef L_free_dbg + +void +free_dbg(void * ptr, char * function, char * file, int line) +{ + fprintf(stderr, "free of %p at %s @%s:%d\n", ptr, function, file, line); + free(ptr); +} + +#endif + + diff --git a/libc/stdlib/mkstemp.c b/libc/stdlib/mkstemp.c index de3c682b2..738952815 100644 --- a/libc/stdlib/mkstemp.c +++ b/libc/stdlib/mkstemp.c @@ -4,40 +4,40 @@ #include int mkstemp(template) -char * template; +char *template; { int i; - int num __attribute__ ((unused)); /* UNINITIALIZED */ + int num __attribute__ ((unused)); /* UNINITIALIZED */ int n2; int l = strlen(template); - - if (l<6) { + + if (l < 6) { errno = EINVAL; return -1; } - - for(i=l-6;i=l-6;i--) { + for (i = l - 1; i >= l - 6; i--) { template[i] = '0' + n2 % 10; n2 /= 10; } - - i = open(template, O_RDWR|O_EXCL|O_CREAT, 0666); - - if (i==-1) { + + i = open(template, O_RDWR | O_EXCL | O_CREAT, 0666); + + if (i == -1) { if (errno == EEXIST) { num++; goto again; } else return -1; } - + return i; } diff --git a/libc/stdlib/mktemp.c b/libc/stdlib/mktemp.c index bbe589efc..cfdcf0913 100644 --- a/libc/stdlib/mktemp.c +++ b/libc/stdlib/mktemp.c @@ -4,37 +4,37 @@ #include #include -char * mktemp(template) -char * template; +char *mktemp(template) +char *template; { int i; - int num __attribute__ ((unused)); /* UNINITIALIZED */ + int num __attribute__ ((unused)); /* UNINITIALIZED */ int n2; int l = strlen(template); struct stat stbuf; - - if (l<6) { + + if (l < 6) { errno = EINVAL; return 0; } - - for(i=l-6;i=l-6;i--) { + for (i = l - 1; i >= l - 6; i--) { template[i] = '0' + n2 % 10; n2 /= 10; } - + if (stat(template, &stbuf) == 0) { num++; goto again; } - + return template; } diff --git a/libc/stdlib/putenv.c b/libc/stdlib/putenv.c index 692aefb5e..5b2ebcb3c 100644 --- a/libc/stdlib/putenv.c +++ b/libc/stdlib/putenv.c @@ -6,57 +6,62 @@ #include #include -extern char ** environ; +extern char **environ; + #define ADD_NUM 4 -int -putenv(var) -const char * var; +int putenv(var) +const char *var; { -static char ** mall_env = 0; -static int extras = 0; - char **p, **d; - char * r; - int len; - - r = strchr(var, '='); - if( r == 0 ) len = strlen(var); - else len = r-var; - - if (!environ) { - environ = (char**)malloc(ADD_NUM * sizeof(char*)); - memset(environ, 0, sizeof(char*)*ADD_NUM); - extras = ADD_NUM; - } - - for(p=environ; *p; p++) - { - if( memcmp(var, *p, len) == 0 && (*p)[len] == '=' ) - { - while( (p[0] = p[1]) ) p++; - extras++; - break; - } - } - if( r == 0 ) return 0; - if( extras <= 0 ) /* Need more space */ - { - d = malloc((p-environ+1+ADD_NUM)*sizeof(char*)); - if( d == 0 ) return -1; - - memcpy((void*) d, (void*) environ, (p-environ+1)*sizeof(char*)); - p = d + (p-environ); - extras=ADD_NUM; - - if( mall_env ) free(mall_env); - environ = d; - mall_env = d; - } - *p++ = strdup((char*)var); - *p = '\0'; - extras--; - - return 0; -} + static char **mall_env = 0; + static int extras = 0; + char **p, **d; + char *r; + int len; + + r = strchr(var, '='); + if (r == 0) + len = strlen(var); + else + len = r - var; + + if (!environ) { + environ = (char **) malloc(ADD_NUM * sizeof(char *)); + memset(environ, 0, sizeof(char *) * ADD_NUM); + + extras = ADD_NUM; + } + for (p = environ; *p; p++) { + if (memcmp(var, *p, len) == 0 && (*p)[len] == '=') { + while ((p[0] = p[1])) + p++; + extras++; + break; + } + } + if (r == 0) + return 0; + if (extras <= 0) { /* Need more space */ + d = malloc((p - environ + 1 + ADD_NUM) * sizeof(char *)); + if (d == 0) + return -1; + + memcpy((void *) d, (void *) environ, + + (p - environ + 1) * sizeof(char *)); + p = d + (p - environ); + extras = ADD_NUM; + + if (mall_env) + free(mall_env); + environ = d; + mall_env = d; + } + *p++ = strdup((char *) var); + *p = '\0'; + extras--; + + return 0; +} diff --git a/libc/stdlib/qsort.c b/libc/stdlib/qsort.c index b45716c83..7cb1d8ab4 100644 --- a/libc/stdlib/qsort.c +++ b/libc/stdlib/qsort.c @@ -9,159 +9,140 @@ */ #include -char *_qbuf = 0; /* pointer to storage for qsort() */ +char *_qbuf = 0; /* pointer to storage for qsort() */ #define PIVOT ((i+j)>>1) #define moveitem(dst,src,size) if(dst != src) memcpy(dst, src, size) -static void -_wqsort(base, lo, hi, cmp) +static void _wqsort(base, lo, hi, cmp) register int *base; register int lo; register int hi; register int (*cmp) (); { - int k; - register int i, j, t; - register int *p = &k; + int k; + register int i, j, t; + register int *p = &k; - while (hi > lo) - { - i = lo; - j = hi; - t = PIVOT; - *p = base[t]; - base[t] = base[i]; - base[i] = *p; - while (i < j) - { - while (((*cmp) ((base + j), p)) > 0) - --j; - base[i] = base[j]; - while ((i < j) && (((*cmp) ((base + i), p)) <= 0)) - ++i; - base[j] = base[i]; - } - base[i] = *p; - if ((i - lo) < (hi - i)) - { - _wqsort(base, lo, (i - 1), cmp); - lo = i + 1; - } - else - { - _wqsort(base, (i + 1), hi, cmp); - hi = i - 1; - } - } + while (hi > lo) { + i = lo; + j = hi; + t = PIVOT; + *p = base[t]; + base[t] = base[i]; + base[i] = *p; + while (i < j) { + while (((*cmp) ((base + j), p)) > 0) + --j; + base[i] = base[j]; + while ((i < j) && (((*cmp) ((base + i), p)) <= 0)) + ++i; + base[j] = base[i]; + } + base[i] = *p; + if ((i - lo) < (hi - i)) { + _wqsort(base, lo, (i - 1), cmp); + lo = i + 1; + } else { + _wqsort(base, (i + 1), hi, cmp); + hi = i - 1; + } + } } -static void -_lqsort(base, lo, hi, cmp) +static void _lqsort(base, lo, hi, cmp) register long *base; register int lo; register int hi; register int (*cmp) (); { - long k; - register int i, j, t; - register long *p = &k; + long k; + register int i, j, t; + register long *p = &k; - while (hi > lo) - { - i = lo; - j = hi; - t = PIVOT; - *p = base[t]; - base[t] = base[i]; - base[i] = *p; - while (i < j) - { - while (((*cmp) ((base + j), p)) > 0) - --j; - base[i] = base[j]; - while ((i < j) && (((*cmp) ((base + i), p)) <= 0)) - ++i; - base[j] = base[i]; - } - base[i] = *p; - if ((i - lo) < (hi - i)) - { - _lqsort(base, lo, (i - 1), cmp); - lo = i + 1; - } - else - { - _lqsort(base, (i + 1), hi, cmp); - hi = i - 1; - } - } + while (hi > lo) { + i = lo; + j = hi; + t = PIVOT; + *p = base[t]; + base[t] = base[i]; + base[i] = *p; + while (i < j) { + while (((*cmp) ((base + j), p)) > 0) + --j; + base[i] = base[j]; + while ((i < j) && (((*cmp) ((base + i), p)) <= 0)) + ++i; + base[j] = base[i]; + } + base[i] = *p; + if ((i - lo) < (hi - i)) { + _lqsort(base, lo, (i - 1), cmp); + lo = i + 1; + } else { + _lqsort(base, (i + 1), hi, cmp); + hi = i - 1; + } + } } -static void -_nqsort(base, lo, hi, size, cmp) +static void _nqsort(base, lo, hi, size, cmp) register char *base; register int lo; register int hi; register int size; register int (*cmp) (); { - register int i, j; - register char *p = _qbuf; + register int i, j; + register char *p = _qbuf; - while (hi > lo) - { - i = lo; - j = hi; - p = (base + size * PIVOT); - moveitem(_qbuf, p, size); - moveitem(p, (base + size * i), size); - moveitem((base + size * i), _qbuf, size); - p = _qbuf; - while (i < j) - { - while (((*cmp) ((base + size * j), p)) > 0) - --j; - moveitem((base + size * i), (base + size * j), size); - while ((i < j) && (((*cmp) ((base + size * i), p)) <= 0)) - ++i; - moveitem((base + size * j), (base + size * i), size); - } - moveitem((base + size * i), p, size); - if ((i - lo) < (hi - i)) - { - _nqsort(base, lo, (i - 1), size, cmp); - lo = i + 1; - } - else - { - _nqsort(base, (i + 1), hi, size, cmp); - hi = i - 1; - } - } + while (hi > lo) { + i = lo; + j = hi; + p = (base + size * PIVOT); + moveitem(_qbuf, p, size); + moveitem(p, (base + size * i), size); + moveitem((base + size * i), _qbuf, size); + p = _qbuf; + while (i < j) { + while (((*cmp) ((base + size * j), p)) > 0) + --j; + moveitem((base + size * i), (base + size * j), size); + while ((i < j) && (((*cmp) ((base + size * i), p)) <= 0)) + ++i; + moveitem((base + size * j), (base + size * i), size); + } + moveitem((base + size * i), p, size); + if ((i - lo) < (hi - i)) { + _nqsort(base, lo, (i - 1), size, cmp); + lo = i + 1; + } else { + _nqsort(base, (i + 1), hi, size, cmp); + hi = i - 1; + } + } } extern int qsort(base, num, size, cmp) char *base; -int num; -int size; -int (*cmp) (); +int num; +int size; +int (*cmp) (); { - char _qtemp[128]; + char _qtemp[128]; - if (_qbuf == 0) - { - if (size > sizeof(_qtemp))/* records too large! */ - return 1; - _qbuf = _qtemp; - } - if (size == 2) - _wqsort(base, 0, num - 1, cmp); - else if (size == 4) - _lqsort(base, 0, num - 1, cmp); - else - _nqsort(base, 0, num - 1, size, cmp); - if (_qbuf == _qtemp) - _qbuf = 0; - return 0; + if (_qbuf == 0) { + if (size > sizeof(_qtemp)) /* records too large! */ + return 1; + _qbuf = _qtemp; + } + if (size == 2) + _wqsort(base, 0, num - 1, cmp); + else if (size == 4) + _lqsort(base, 0, num - 1, cmp); + else + _nqsort(base, 0, num - 1, size, cmp); + if (_qbuf == _qtemp) + _qbuf = 0; + return 0; } diff --git a/libc/stdlib/rand.c b/libc/stdlib/rand.c index 4bf98d5bc..b5c5cb764 100644 --- a/libc/stdlib/rand.c +++ b/libc/stdlib/rand.c @@ -12,13 +12,13 @@ static unsigned int sseed = 0; int rand() { - return ( sseed = (((sseed+1L)*75L)%65537L)-1 ) & MAXINT; + return (sseed = (((sseed + 1L) * 75L) % 65537L) - 1) & MAXINT; } void srand(seed) unsigned int seed; { - sseed=seed; + sseed = seed; } #else @@ -32,6 +32,7 @@ unsigned int seed; static int seed1 = 1; static int seed2 = 1; static int seed3 = 1; + #define MAXINT (((unsigned)-1)>>1) #define CRANK(a,b,c,m,s) \ @@ -41,21 +42,22 @@ static int seed3 = 1; int rand() { - register int q; - CRANK(206, 157, 31, 32363, seed1); - CRANK(217, 146, 45, 31727, seed2); - CRANK(222, 142, 133, 31657, seed3); + register int q; + + CRANK(206, 157, 31, 32363, seed1); + CRANK(217, 146, 45, 31727, seed2); + CRANK(222, 142, 133, 31657, seed3); - return seed1^seed2^seed3; + return seed1 ^ seed2 ^ seed3; } void srand(seed) unsigned int seed; { - seed &= MAXINT; - seed1= seed%32362 + 1; - seed2= seed%31726 + 1; - seed3= seed%31656 + 1; + seed &= MAXINT; + seed1 = seed % 32362 + 1; + seed2 = seed % 31726 + 1; + seed3 = seed % 31656 + 1; } #endif diff --git a/libc/stdlib/realpath.c b/libc/stdlib/realpath.c index d053cfcaf..73903371f 100644 --- a/libc/stdlib/realpath.c +++ b/libc/stdlib/realpath.c @@ -22,7 +22,7 @@ #include #include #include -#include /* for PATH_MAX */ +#include /* for PATH_MAX */ #include /* for MAXPATHLEN */ #include @@ -43,23 +43,23 @@ #define MAX_READLINKS 32 #ifdef __STDC__ -char *realpath(const char *path, char resolved_path []) +char *realpath(const char *path, char resolved_path[]) #else char *realpath(path, resolved_path) const char *path; -char resolved_path []; +char resolved_path[]; #endif { char copy_path[PATH_MAX]; char link_path[PATH_MAX]; - char got_path [PATH_MAX]; + char got_path[PATH_MAX]; char *new_path = got_path; char *max_path; int readlinks = 0; int n; /* Make a copy of the source path since we may need to modify it. */ - if (strlen(path)>=PATH_MAX-2) { + if (strlen(path) >= PATH_MAX - 2) { errno = ENAMETOOLONG; return NULL; } @@ -78,8 +78,7 @@ char resolved_path []; new_path += strlen(new_path); if (new_path[-1] != '/') *new_path++ = '/'; - } - else { + } else { *new_path++ = '/'; path++; } @@ -103,8 +102,7 @@ char resolved_path []; if (new_path == got_path + 1) continue; /* Handle ".." by backing up. */ - while ((--new_path)[-1] != '/') - ; + while ((--new_path)[-1] != '/'); continue; } } @@ -131,11 +129,10 @@ char resolved_path []; if (errno != EINVAL) { /* Make sure it's null terminated. */ *new_path = '\0'; - strcpy (resolved_path, got_path); + strcpy(resolved_path, got_path); return NULL; } - } - else { + } else { /* Note: readlink doesn't add the null byte. */ link_path[n] = '\0'; if (*link_path == '/') @@ -143,10 +140,9 @@ char resolved_path []; new_path = got_path; else /* Otherwise back up over this component. */ - while (*(--new_path) != '/') - ; + while (*(--new_path) != '/'); /* Safe sex check. */ - if (strlen(path) + n >= PATH_MAX-2) { + if (strlen(path) + n >= PATH_MAX - 2) { errno = ENAMETOOLONG; return NULL; } @@ -155,7 +151,7 @@ char resolved_path []; strcpy(copy_path, link_path); path = copy_path; } -#endif /* S_IFLNK */ +#endif /* S_IFLNK */ *new_path++ = '/'; } /* Delete trailing slash but don't whomp a lone slash. */ @@ -163,6 +159,6 @@ char resolved_path []; new_path--; /* Make sure it's null terminated. */ *new_path = '\0'; - strcpy (resolved_path, got_path); + strcpy(resolved_path, got_path); return resolved_path; } diff --git a/libc/stdlib/setenv.c b/libc/stdlib/setenv.c index afe5676d1..f7d597139 100644 --- a/libc/stdlib/setenv.c +++ b/libc/stdlib/setenv.c @@ -6,69 +6,71 @@ #include #include -extern char ** environ; +extern char **environ; + #define ADD_NUM 4 -int -setenv(var, value, overwrite) -const char * var; -const char * value; +int setenv(var, value, overwrite) +const char *var; +const char *value; int overwrite; { -static char ** mall_env = 0; -static int extras = 0; - char **p, **d; - char * t; - int len; - - len = strlen(var); - - if (!environ) { - environ = (char**)malloc(ADD_NUM * sizeof(char*)); - memset(environ, 0, sizeof(char*)*ADD_NUM); - extras = ADD_NUM; - } - - for(p=environ; *p; p++) - { - if( memcmp(var, *p, len) == 0 && (*p)[len] == '=' ) - { - if (!overwrite) - return -1; - /* Overwrite stuff */ - while( (p[0] = p[1]) ) p++; - extras++; - break; - } - } - - if( extras <= 0 ) /* Need more space */ - { - d = malloc((p-environ+1+ADD_NUM)*sizeof(char*)); - if( d == 0 ) return -1; - - memcpy((void*) d, (void*) environ, (p-environ+1)*sizeof(char*)); - p = d + (p-environ); - extras=ADD_NUM; - - if( mall_env ) free(mall_env); - environ = d; - mall_env = d; - } - - t = malloc(len + 1 + strlen(value) + 1); - if (!t) - return -1; - - strcpy(t, var); - strcat(t, "="); - strcat(t, value); - - *p++ = (char*)t; - *p = '\0'; - extras--; - - return 0; -} + static char **mall_env = 0; + static int extras = 0; + char **p, **d; + char *t; + int len; + + len = strlen(var); + + if (!environ) { + environ = (char **) malloc(ADD_NUM * sizeof(char *)); + memset(environ, 0, sizeof(char *) * ADD_NUM); + + extras = ADD_NUM; + } + + for (p = environ; *p; p++) { + if (memcmp(var, *p, len) == 0 && (*p)[len] == '=') { + if (!overwrite) + return -1; + /* Overwrite stuff */ + while ((p[0] = p[1])) + p++; + extras++; + break; + } + } + + if (extras <= 0) { /* Need more space */ + d = malloc((p - environ + 1 + ADD_NUM) * sizeof(char *)); + if (d == 0) + return -1; + memcpy((void *) d, (void *) environ, + + (p - environ + 1) * sizeof(char *)); + p = d + (p - environ); + extras = ADD_NUM; + + if (mall_env) + free(mall_env); + environ = d; + mall_env = d; + } + + t = malloc(len + 1 + strlen(value) + 1); + if (!t) + return -1; + + strcpy(t, var); + strcat(t, "="); + strcat(t, value); + + *p++ = (char *) t; + *p = '\0'; + extras--; + + return 0; +} diff --git a/libc/stdlib/strtod.c b/libc/stdlib/strtod.c index 0d3bb790a..de3aecbd3 100644 --- a/libc/stdlib/strtod.c +++ b/libc/stdlib/strtod.c @@ -20,77 +20,64 @@ #include #include -float -strtod(const char *nptr, char ** endptr) +float strtod(const char *nptr, char **endptr) { - unsigned short negative; - float number; - float fp_part; - int exponent; - unsigned short exp_negative; + unsigned short negative; + float number; + float fp_part; + int exponent; + unsigned short exp_negative; - /* advance beyond any leading whitespace */ - while (isspace(*nptr)) - nptr++; + /* advance beyond any leading whitespace */ + while (isspace(*nptr)) + nptr++; - /* check for optional '+' or '-' */ - negative=0; - if (*nptr=='-') - { - negative=1; - nptr++; - } - else - if (*nptr=='+') - nptr++; + /* check for optional '+' or '-' */ + negative = 0; + if (*nptr == '-') { + negative = 1; + nptr++; + } else if (*nptr == '+') + nptr++; - number=0; - while (isdigit(*nptr)) - { - number=number*10+(*nptr-'0'); - nptr++; - } - - if (*nptr=='.') - { - nptr++; - fp_part=0; - while (isdigit(*nptr)) - { - fp_part=fp_part/10.0 + (*nptr-'0')/10.0; - nptr++; + number = 0; + while (isdigit(*nptr)) { + number = number * 10 + (*nptr - '0'); + nptr++; } - number+=fp_part; - } - if (*nptr=='e' || *nptr=='E') - { - nptr++; - exp_negative=0; - if (*nptr=='-') - { - exp_negative=1; - nptr++; + if (*nptr == '.') { + nptr++; + fp_part = 0; + while (isdigit(*nptr)) { + fp_part = fp_part / 10.0 + (*nptr - '0') / 10.0; + nptr++; + } + number += fp_part; } - else - if (*nptr=='+') - nptr++; - exponent=0; - while (isdigit(*nptr)) - { - exponent=exponent*10+(*nptr-'0'); - exponent++; + if (*nptr == 'e' || *nptr == 'E') { + nptr++; + exp_negative = 0; + if (*nptr == '-') { + exp_negative = 1; + nptr++; + } else if (*nptr == '+') + nptr++; + + exponent = 0; + while (isdigit(*nptr)) { + exponent = exponent * 10 + (*nptr - '0'); + exponent++; + } } - } - while (exponent) - { - if (exp_negative) - number/=10; - else - number*=10; - exponent--; - } - return (negative ? -number:number); + while (exponent) { + if (exp_negative) + number /= 10; + else + number *= 10; + exponent--; + } + return (negative ? -number : number); } diff --git a/libc/stdlib/system.c b/libc/stdlib/system.c index 6c8a42dee..061dbc914 100644 --- a/libc/stdlib/system.c +++ b/libc/stdlib/system.c @@ -4,46 +4,44 @@ #include #include -int -system(command) -char * command; +int system(command) +char *command; { - int wait_val, pid; - __sighandler_t save_quit, save_int, save_chld; - - if( command == 0 ) return 1; - - save_quit = signal(SIGQUIT, SIG_IGN); - save_int = signal(SIGINT, SIG_IGN); - save_chld = signal(SIGCHLD, SIG_DFL); - - if( (pid=vfork()) < 0 ) - { - signal(SIGQUIT, save_quit); - signal(SIGINT, save_int); - signal(SIGCHLD, save_chld); - return -1; - } - if( pid == 0 ) - { - signal(SIGQUIT, SIG_DFL); - signal(SIGINT, SIG_DFL); - signal(SIGCHLD, SIG_DFL); - - execl("/bin/sh", "sh", "-c", command, (char*)0); - _exit(127); - } - /* Signals are not absolutly guarenteed with vfork */ - signal(SIGQUIT, SIG_IGN); - signal(SIGINT, SIG_IGN); - - printf("Waiting for child %d\n", pid); - - if (wait4(pid, &wait_val, 0, 0) == -1) - wait_val = -1; - - signal(SIGQUIT, save_quit); - signal(SIGINT, save_int); - signal(SIGCHLD, save_chld); - return wait_val; + int wait_val, pid; + __sighandler_t save_quit, save_int, save_chld; + + if (command == 0) + return 1; + + save_quit = signal(SIGQUIT, SIG_IGN); + save_int = signal(SIGINT, SIG_IGN); + save_chld = signal(SIGCHLD, SIG_DFL); + + if ((pid = vfork()) < 0) { + signal(SIGQUIT, save_quit); + signal(SIGINT, save_int); + signal(SIGCHLD, save_chld); + return -1; + } + if (pid == 0) { + signal(SIGQUIT, SIG_DFL); + signal(SIGINT, SIG_DFL); + signal(SIGCHLD, SIG_DFL); + + execl("/bin/sh", "sh", "-c", command, (char *) 0); + _exit(127); + } + /* Signals are not absolutly guarenteed with vfork */ + signal(SIGQUIT, SIG_IGN); + signal(SIGINT, SIG_IGN); + + printf("Waiting for child %d\n", pid); + + if (wait4(pid, &wait_val, 0, 0) == -1) + wait_val = -1; + + signal(SIGQUIT, save_quit); + signal(SIGINT, save_int); + signal(SIGCHLD, save_chld); + return wait_val; } diff --git a/libc/string/Makefile b/libc/string/Makefile index d8d65fa3d..171970c9a 100644 --- a/libc/string/Makefile +++ b/libc/string/Makefile @@ -32,15 +32,20 @@ MOBJ=strlen.o strcat.o strcpy.o strcmp.o strncat.o strncpy.o strncmp.o \ CSRC=strpbrk.c strsep.c strstr.c strtok.c strcspn.c \ config.c strspn.c strcasecmp.c strncasecmp.c COBJS=$(patsubst %.c,%.o, $(CSRC)) +OBJS=$(MOBJ) $(COBJS) -all: $(MOBJ) $(COBJS) $(LIBC) +all: $(OBJS) $(LIBC) -$(LIBC): $(MOBJ) $(COBJS) - $(AR) $(ARFLAGS) $(LIBC) $(MOBJ) $(COBJS) +$(LIBC): ar-target + +ar-target: $(OBJS) + $(AR) $(ARFLAGS) $(LIBC) $(OBJS) $(MOBJ): $(MSRC) $(CC) $(CFLAGS) -DL_$* $< -c -o $*.o +$(OBJS): Makefile + clean: rm -f *.[oa] *~ core diff --git a/libc/sysdeps/linux/common/Makefile b/libc/sysdeps/linux/common/Makefile index b4a922eb5..973531d98 100644 --- a/libc/sysdeps/linux/common/Makefile +++ b/libc/sysdeps/linux/common/Makefile @@ -25,13 +25,20 @@ TOPDIR=../../../ include $(TOPDIR)Rules.make LIBC=$(TOPDIR)libc.a +#The file makefile.objs defines $(COBJS) include makefile.objs +OBJ=$(COBJS) + all: $(OBJ) $(LIBC) -$(LIBC): $(OBJ) +$(LIBC): ar-target + +ar-target: $(OBJ) $(AR) $(ARFLAGS) $(LIBC) $(OBJ) +$(OBJ): Makefile + clean: rm -f *.[oa] *~ core diff --git a/libc/sysdeps/linux/i386/Makefile b/libc/sysdeps/linux/i386/Makefile index 3c32b6464..b91986b0b 100644 --- a/libc/sysdeps/linux/i386/Makefile +++ b/libc/sysdeps/linux/i386/Makefile @@ -25,28 +25,33 @@ include $(TOPDIR)Rules.make LIBC=$(TOPDIR)libc.a ASFLAGS=$(CFLAGS) -MSRC=syscalls.S +ASMSRC=syscalls.S +#The file makefile.objs defines $(ASMOBJS) include makefile.objs -SSRC=setjmp.S longjmp.S _start.S _exit.S #fork.S +SSRC=_start.S setjmp.S longjmp.S #fork.S #clone.S SOBJS=$(patsubst %.S,%.o, $(SSRC)) CSRC=readdir.c #select.c COBJS=$(patsubst %.c,%.o, $(CSRC)) +OBJS=$(SOBJS) $(ASMOBJS) $(COBJS) -all: $(MOBJ) $(SOBJS) $(COBJS) $(LIBC) +all: $(OBJS) $(LIBC) -$(LIBC): $(MOBJ) $(SOBJS) $(COBJS) - $(AR) $(ARFLAGS) $(LIBC) $(MOBJ) $(SOBJS) $(COBJS) +$(LIBC): ar-target -$(MOBJ): $(MSRC) +ar-target: $(OBJS) + $(AR) $(ARFLAGS) $(LIBC) $(OBJS) + +$(ASMOBJS): $(ASMSRC) $(CC) $(CFLAGS) -DL_$* $< -c -o $*.o $(SOBJS): $(SSRC) $(COBJS): $(CSRC) +$(OBJS): Makefile clean: rm -f *.[oa] *~ core diff --git a/libc/sysdeps/linux/i386/clone.S b/libc/sysdeps/linux/i386/clone.S index 7a2025b53..5db30b8e3 100644 --- a/libc/sysdeps/linux/i386/clone.S +++ b/libc/sysdeps/linux/i386/clone.S @@ -21,9 +21,8 @@ and invokes a function in the right context after its all over. */ #include -#define _ERRNO_H 1 -#include -#include +#include + /* int clone(int (*fn)(void *arg), void *child_stack, int flags, void *arg); */ @@ -59,7 +58,7 @@ ENTRY(__clone) /* Do the system call */ pushl %ebx movl 16(%esp),%ebx - movl $SYS_ify(clone),%eax + movl $__NR_clone,%eax int $0x80 popl %ebx @@ -80,8 +79,6 @@ L(here): addl $_GLOBAL_OFFSET_TABLE_+[.-L(here)], %ebx #endif pushl %eax - call JUMPTARGET (_exit) + call _exit -PSEUDO_END (__clone) -weak_alias (__clone, clone) diff --git a/libc/termios/.indent.pro b/libc/termios/.indent.pro new file mode 100644 index 000000000..492ecf1c7 --- /dev/null +++ b/libc/termios/.indent.pro @@ -0,0 +1,33 @@ +--blank-lines-after-declarations +--blank-lines-after-procedures +--break-before-boolean-operator +--no-blank-lines-after-commas +--braces-on-if-line +--braces-on-struct-decl-line +--comment-indentation25 +--declaration-comment-column25 +--no-comment-delimiters-on-blank-lines +--cuddle-else +--continuation-indentation4 +--case-indentation0 +--else-endif-column33 +--space-after-cast +--line-comments-indentation0 +--declaration-indentation1 +--dont-format-first-column-comments +--dont-format-comments +--honour-newlines +--indent-level4 +/* changed from 0 to 4 */ +--parameter-indentation4 +--line-length78 /* changed from 75 */ +--continue-at-parentheses +--no-space-after-function-call-names +--dont-break-procedure-type +--dont-star-comments +--leave-optional-blank-lines +--dont-space-special-semicolon +--tab-size4 +/* additions by Mark */ +--case-brace-indentation0 +--leave-preprocessor-space diff --git a/libc/termios/Makefile b/libc/termios/Makefile index 3a9ee7a13..7bcb35407 100644 --- a/libc/termios/Makefile +++ b/libc/termios/Makefile @@ -31,16 +31,21 @@ MOBJ=tcsetattr.o tcgetattr.o tcdrain.o tcflow.o tcflush.o tcsendbreak.o \ CSRC=ttyname.c COBJS=$(patsubst %.c,%.o, $(CSRC)) +OBJS=$(MOBJ) $(COBJS) -all: $(MOBJ) $(COBJS) $(LIBC) +all: $(OBJS) $(LIBC) -$(LIBC): $(MOBJ) $(COBJS) - $(AR) $(ARFLAGS) $(LIBC) $(MOBJ) $(COBJS) +$(LIBC): ar-target + +ar-target: $(OBJS) + $(AR) $(ARFLAGS) $(LIBC) $(OBJS) $(MOBJ): $(MSRC) $(CC) $(CFLAGS) -DL_$* $< -c -o $*.o +$(OBJS): Makefile + clean: rm -f *.[oa] *~ core diff --git a/libc/termios/termios.c b/libc/termios/termios.c index 7532259d4..6781f32fd 100644 --- a/libc/termios/termios.c +++ b/libc/termios/termios.c @@ -16,126 +16,119 @@ #ifdef L_isatty int isatty(int fd) { - struct termios term; - int rv, err = errno; - rv= (ioctl(fd, TCGETS, &term)==0); - if( rv==0 && errno == ENOSYS ) - rv = (fd<3); - errno = err; - return rv; + struct termios term; + int rv, err = errno; + + rv = (ioctl(fd, TCGETS, &term) == 0); + if (rv == 0 && errno == ENOSYS) + rv = (fd < 3); + errno = err; + return rv; } -#endif +#endif #ifdef L_tcgetattr -int -tcgetattr(fd, term) -int fd; +int tcgetattr(fd, term) +int fd; struct termios *term; { - return ioctl(fd, TCGETS, term); + return ioctl(fd, TCGETS, term); } #endif #ifdef L_tcsetattr -int -tcsetattr(fildes, optional_actions, termios_p) +int tcsetattr(fildes, optional_actions, termios_p) int fildes; int optional_actions; struct termios *termios_p; { - switch (optional_actions) - { - case TCSANOW: - return ioctl(fildes, TCSETS, termios_p); - case TCSADRAIN: - return ioctl(fildes, TCSETSW, termios_p); - case TCSAFLUSH: - return ioctl(fildes, TCSETSF, termios_p); - default: - errno = EINVAL; - return -1; - } + switch (optional_actions) { + case TCSANOW: + return ioctl(fildes, TCSETS, termios_p); + case TCSADRAIN: + return ioctl(fildes, TCSETSW, termios_p); + case TCSAFLUSH: + return ioctl(fildes, TCSETSF, termios_p); + default: + errno = EINVAL; + return -1; + } } #endif #ifdef L_tcdrain /* Wait for pending output to be written on FD. */ -int -tcdrain(fd) -int fd; +int tcdrain(fd) +int fd; { - /* With an argument of 1, TCSBRK just waits for output to drain. */ - return ioctl(fd, TCSBRK, 1); + /* With an argument of 1, TCSBRK just waits for output to drain. */ + return ioctl(fd, TCSBRK, 1); } #endif #ifdef L_tcflow -int -tcflow(fd, action) +int tcflow(fd, action) int fd; int action; { - return ioctl(fd, TCXONC, action); + return ioctl(fd, TCXONC, action); } #endif #ifdef L_tcflush /* Flush pending data on FD. */ -int -tcflush(fd, queue_selector) -int fd; -int queue_selector; +int tcflush(fd, queue_selector) +int fd; +int queue_selector; { - return ioctl(fd, TCFLSH, queue_selector); + return ioctl(fd, TCFLSH, queue_selector); } #endif #ifdef L_tcsendbreak /* Send zero bits on FD. */ -int -tcsendbreak(fd, duration) -int fd; -int duration; +int tcsendbreak(fd, duration) +int fd; +int duration; { - /* - * The break lasts 0.25 to 0.5 seconds if DURATION is zero, and an - * implementation-defined period if DURATION is nonzero. We define a - * positive DURATION to be number of milliseconds to break. - */ - if (duration <= 0) - return ioctl(fd, TCSBRK, 0); - - /* - * ioctl can't send a break of any other duration for us. This could be - * changed to use trickery (e.g. lower speed and send a '\0') to send - * the break, but for now just return an error. - */ - errno = EINVAL; - return -1; + /* + * The break lasts 0.25 to 0.5 seconds if DURATION is zero, and an + * implementation-defined period if DURATION is nonzero. We define a + * positive DURATION to be number of milliseconds to break. + */ + if (duration <= 0) + return ioctl(fd, TCSBRK, 0); + + /* + * ioctl can't send a break of any other duration for us. This could be + * changed to use trickery (e.g. lower speed and send a '\0') to send + * the break, but for now just return an error. + */ + errno = EINVAL; + return -1; } #endif #ifdef L_tcsetpgrp /* Set the foreground process group ID of FD set PGRP_ID. */ -int -tcsetpgrp(fd, pgrp_id) -int fd; +int tcsetpgrp(fd, pgrp_id) +int fd; pid_t pgrp_id; { - return ioctl(fd, TIOCSPGRP, &pgrp_id); + return ioctl(fd, TIOCSPGRP, &pgrp_id); } #endif #ifdef L_tcgetpgrp /* Return the foreground process group ID of FD. */ -pid_t -tcgetpgrp(fd) -int fd; +pid_t tcgetpgrp(fd) +int fd; { - int pgrp; - if (ioctl(fd, TIOCGPGRP, &pgrp) < 0) - return (pid_t) - 1; - return (pid_t) pgrp; + int pgrp; + + if (ioctl(fd, TIOCGPGRP, &pgrp) < 0) + return (pid_t) - 1; + return (pid_t) pgrp; } #endif @@ -143,7 +136,7 @@ int fd; speed_t cfgetospeed(tp) struct termios *tp; { - return (tp->c_cflag & CBAUD); + return (tp->c_cflag & CBAUD); } #endif @@ -151,34 +144,36 @@ struct termios *tp; speed_t cfgetispeed(tp) struct termios *tp; { - return (tp->c_cflag & CBAUD); + return (tp->c_cflag & CBAUD); } #endif #ifdef L_cfsetospeed int cfsetospeed(tp, speed) -struct termios *tp; speed_t speed; +struct termios *tp; +speed_t speed; { #ifdef CBAUDEX - if ((speed & ~CBAUD) || - ((speed & CBAUDEX) && (speed < B57600 || speed > B115200))) - return 0; + if ((speed & ~CBAUD) || + ((speed & CBAUDEX) && (speed < B57600 || speed > B115200))) + return 0; #else - if (speed & ~CBAUD) - return 0; + if (speed & ~CBAUD) + return 0; #endif - tp->c_cflag &= ~CBAUD; - tp->c_cflag |= speed; + tp->c_cflag &= ~CBAUD; + tp->c_cflag |= speed; - return 0; + return 0; } #endif #ifdef L_cfsetispeed int cfsetispeed(tp, speed) -struct termios *tp; speed_t speed; +struct termios *tp; +speed_t speed; { - return cfsetospeed(tp, speed); + return cfsetospeed(tp, speed); } #endif @@ -192,87 +187,110 @@ static struct { speed_t code; } tcspeeds[] = { #ifdef B50 - {50, B50}, + { + 50, B50}, #endif #ifdef B75 - {75, B75}, + { + 75, B75}, #endif #ifdef B110 - {110, B110}, + { + 110, B110}, #endif #ifdef B134 - {134, B134}, + { + 134, B134}, #endif #ifdef B150 - {150, B150}, + { + 150, B150}, #endif #ifdef B200 - {200, B200}, + { + 200, B200}, #endif #ifdef B300 - {300, B300}, + { + 300, B300}, #endif #ifdef B600 - {600, B600}, + { + 600, B600}, #endif #ifdef B1200 - {1200, B1200}, + { + 1200, B1200}, #endif #ifdef B1800 - {1800, B1800}, + { + 1800, B1800}, #endif #ifdef B2400 - {2400, B2400}, + { + 2400, B2400}, #endif #ifdef B4800 - {4800, B4800}, + { + 4800, B4800}, #endif #ifdef B9600 - {9600, B9600}, + { + 9600, B9600}, #endif #ifdef B19200 - {19200, B19200}, + { + 19200, B19200}, #endif #ifdef B38400 - {38400, B38400}, + { + 38400, B38400}, #endif #ifdef B57600 - {57600, B57600}, + { + 57600, B57600}, #endif #ifdef B115200 - {115200, B115200}, + { + 115200, B115200}, #endif #ifdef B230400 - {230400, B230400}, + { + 230400, B230400}, #endif #ifdef B460800 - {460800, B460800}, + { + 460800, B460800}, #endif #ifdef B0 - {0, B0}, + { + 0, B0}, #endif - {0, 0} + { + 0, 0} }; int tcspeed_to_number(code) speed_t code; { - int i; - code &= CBAUD; - for(i=0;tcspeeds[i].code;i++) - if (tcspeeds[i].code == code) - return tcspeeds[i].number; - return 0; + int i; + + code &= CBAUD; + for (i = 0; tcspeeds[i].code; i++) + if (tcspeeds[i].code == code) + return tcspeeds[i].number; + return 0; } speed_t tcspeed_from_number(number) int number; { - int i; - for(i=0;tcspeeds[i].code;i++) - if (tcspeeds[i].number == number) - return tcspeeds[i].code; - return B0; + int i; + + for (i = 0; tcspeeds[i].code; i++) + if (tcspeeds[i].number == number) + return tcspeeds[i].code; + return B0; } #endif @@ -280,7 +298,7 @@ int number; int cfgetospeedn(tp) struct termios *tp; { - return tcspeed_to_number(cfgetospeed(tp)); + return tcspeed_to_number(cfgetospeed(tp)); } #endif @@ -288,23 +306,25 @@ struct termios *tp; int cfgetispeedn(tp) struct termios *tp; { - return tcspeed_to_number(cfgetispeed(tp)); + return tcspeed_to_number(cfgetispeed(tp)); } #endif #ifdef L_cfsetospeedn int cfsetospeedn(tp, speed) -struct termios *tp; int speed; +struct termios *tp; +int speed; { - return cfsetospeed(tp, tcspeed_from_number(speed)); + return cfsetospeed(tp, tcspeed_from_number(speed)); } #endif #ifdef L_cfsetispeedn int cfsetispeedn(tp, speed) -struct termios *tp; int speed; +struct termios *tp; +int speed; { - return cfsetispeedn(tp, tcspeed_from_number(speed)); + return cfsetispeedn(tp, tcspeed_from_number(speed)); } #endif @@ -315,8 +335,7 @@ struct termios *tp; int speed; /* Copyright (C) 1992 Free Software Foundation, Inc. This file is part of the GNU C Library.*/ -void -cfmakeraw(t) +void cfmakeraw(t) struct termios *t; { /* I changed it to the current form according to the suggestions @@ -328,20 +347,23 @@ struct termios *t; * I took out the bits commented out by #if 1...#else - RHP */ - /* VMIN = 0 means non-blocking for Linux */ - t->c_cc[VMIN] = 1; t->c_cc[VTIME] = 1; - /* clear some bits with &= ~(bits), set others with |= */ - t->c_cflag &= ~(CSIZE|PARENB|CSTOPB); - t->c_cflag |= (CS8|HUPCL|CREAD); - t->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|INPCK|ISTRIP); - t->c_iflag &= ~(INLCR|IGNCR|ICRNL|IXON|IXOFF); - t->c_iflag |= (BRKINT|IGNPAR); - t->c_oflag &= ~(OPOST|OLCUC|OCRNL|ONOCR|ONLRET|OFILL|OFDEL); - t->c_oflag &= ~(NLDLY|CRDLY|TABDLY|BSDLY|VTDLY|FFDLY); - t->c_oflag |= (ONLCR|NL0|CR0|TAB3|BS0|VT0|FF0); - t->c_lflag &= ~(ISIG|ICANON|IEXTEN|ECHO|ECHOE|ECHOK|ECHONL); - t->c_lflag &= ~(NOFLSH|XCASE); - t->c_lflag &= ~(ECHOPRT|ECHOCTL|ECHOKE); + /* VMIN = 0 means non-blocking for Linux */ + t->c_cc[VMIN] = 1; + t->c_cc[VTIME] = 1; + /* clear some bits with &= ~(bits), set others with |= */ + t->c_cflag &= ~(CSIZE | PARENB | CSTOPB); + t->c_cflag |= (CS8 | HUPCL | CREAD); + t->c_iflag &= ~(IGNBRK | BRKINT | PARMRK | INPCK | ISTRIP); + t->c_iflag &= ~(INLCR | IGNCR | ICRNL | IXON | IXOFF); + t->c_iflag |= (BRKINT | IGNPAR); + t->c_oflag &= + ~(OPOST | OLCUC | OCRNL | ONOCR | ONLRET | OFILL | OFDEL); + t->c_oflag &= ~(NLDLY | CRDLY | TABDLY | BSDLY | VTDLY | FFDLY); + t->c_oflag |= (ONLCR | NL0 | CR0 | TAB3 | BS0 | VT0 | FF0); + t->c_lflag &= + ~(ISIG | ICANON | IEXTEN | ECHO | ECHOE | ECHOK | ECHONL); + t->c_lflag &= ~(NOFLSH | XCASE); + t->c_lflag &= ~(ECHOPRT | ECHOCTL | ECHOKE); } #endif diff --git a/libc/termios/ttyname.c b/libc/termios/ttyname.c index 9fc4f950c..ee78137c0 100644 --- a/libc/termios/ttyname.c +++ b/libc/termios/ttyname.c @@ -4,43 +4,39 @@ #include #include -char * -ttyname(fd) -int fd; +char *ttyname(fd) +int fd; { - static char dev[] = "/dev"; - struct stat st, dst; - DIR *fp; - struct dirent *d; - static char name[NAME_MAX]; - int noerr = errno; + static char dev[] = "/dev"; + struct stat st, dst; + DIR *fp; + struct dirent *d; + static char name[NAME_MAX]; + int noerr = errno; - if (fstat(fd, &st) < 0) - return 0; - if (!isatty(fd)) - { - errno = ENOTTY; - return 0; - } + if (fstat(fd, &st) < 0) + return 0; + if (!isatty(fd)) { + errno = ENOTTY; + return 0; + } - fp = opendir(dev); - if (fp == 0) - return 0; - strcpy(name, dev); - strcat(name, "/"); + fp = opendir(dev); + if (fp == 0) + return 0; + strcpy(name, dev); + strcat(name, "/"); - while ((d = readdir(fp)) != 0) - { - strcpy(name + sizeof(dev), d->d_name); - if (stat(name, &dst) == 0 - && st.st_dev == dst.st_dev && st.st_ino == dst.st_ino) - { - closedir(fp); - errno = noerr; - return name; - } - } - closedir(fp); - errno = noerr; - return 0; + while ((d = readdir(fp)) != 0) { + strcpy(name + sizeof(dev), d->d_name); + if (stat(name, &dst) == 0 + && st.st_dev == dst.st_dev && st.st_ino == dst.st_ino) { + closedir(fp); + errno = noerr; + return name; + } + } + closedir(fp); + errno = noerr; + return 0; } diff --git a/test/stdlib/.cvsignore b/test/stdlib/.cvsignore index 185db39b2..89fe8faa4 100644 --- a/test/stdlib/.cvsignore +++ b/test/stdlib/.cvsignore @@ -7,3 +7,5 @@ teststrtol_glibc teststrtol.o teststrtol.out teststrtol_glibc.out +mallocbug +mallocbug_glibc diff --git a/test/stdlib/Makefile b/test/stdlib/Makefile index e882d8f85..4cd6e5765 100644 --- a/test/stdlib/Makefile +++ b/test/stdlib/Makefile @@ -19,8 +19,9 @@ ifndef $(STRIPTOOL) endif STRIP = $(STRIPTOOL) --remove-section=.note --remove-section=.comment $@ -TARGETS=testmalloc_source testmalloc testmalloc_glibc -TARGETS+=teststrtol_source teststrtol teststrtol_glibc teststrtol_diff +TARGETS=testmalloc testmalloc_glibc +TARGETS=mallocbug mallocbug_glibc +TARGETS+=teststrtol teststrtol_glibc teststrtol_diff all: $(TARGETS) @@ -57,6 +58,32 @@ testmalloc_glibc: testmalloc.c Makefile $(TOPDIR)libc.a -./$@ -@ echo " " +mallocbug: mallocbug.c Makefile $(TOPDIR)libc.a + -@ echo "-------" + -@ echo " " + -@ echo "Compiling vs uC-Libc: " + -@ echo " " + $(CC) -g $(XCFLAGS) -c $< -o $@.o + $(CC) -g $(XLDFLAGS) $@.o -o $@ $(EXTRA_LIBS) + $(STRIP) + -ldd $@ + ls $(LSFLAGS) $@ + -./$@ + -@ echo " " + +mallocbug_glibc: mallocbug.c Makefile $(TOPDIR)libc.a + -@ echo "-------" + -@ echo " " + -@ echo "Compiling vs GNU libc: " + -@ echo " " + $(CC) $(YCFLAGS) -c $< -o $@.o + $(CC) $(YLDFLAGS) --static $@.o -o $@ + $(STRIP) + -ldd $@ + ls $(LSFLAGS) $@ + -./$@ + -@ echo " " + teststrtol_source: -@ echo "-------" -@ echo "teststrtol.c source: " diff --git a/test/stdlib/mallocbug.c b/test/stdlib/mallocbug.c new file mode 100644 index 000000000..84a638795 --- /dev/null +++ b/test/stdlib/mallocbug.c @@ -0,0 +1,67 @@ +/* Reproduce a GNU malloc bug. */ +#include +#include +#include + +#define size_t unsigned int + +int +main (int argc, char *argv[]) +{ + char *dummy0; + char *dummy1; + char *fill_info_table1; + char *over_top; + size_t over_top_size = 0x3000; + char *over_top_dup; + size_t over_top_dup_size = 0x7000; + char *x; + size_t i; + + /* Here's what memory is supposed to look like (hex): + size contents + 3000 original_info_table, later fill_info_table1 + 3fa000 dummy0 + 3fa000 dummy1 + 6000 info_table_2 + 3000 over_top + + */ + /* mem: original_info_table */ + dummy0 = malloc (0x3fa000); + /* mem: original_info_table, dummy0 */ + dummy1 = malloc (0x3fa000); + /* mem: free, dummy0, dummy1, info_table_2 */ + fill_info_table1 = malloc (0x3000); + /* mem: fill_info_table1, dummy0, dummy1, info_table_2 */ + + x = malloc (0x1000); + free (x); + /* mem: fill_info_table1, dummy0, dummy1, info_table_2, freexx */ + + /* This is what loses; info_table_2 and freexx get combined unbeknownst + to mmalloc, and mmalloc puts over_top in a section of memory which + is on the free list as part of another block (where info_table_2 had + been). */ + over_top = malloc (over_top_size); + over_top_dup = malloc (over_top_dup_size); + memset (over_top, 0, over_top_size); + memset (over_top_dup, 1, over_top_dup_size); + + for (i = 0; i < over_top_size; ++i) + if (over_top[i] != 0) + { + printf ("FAIL: malloc expands info table\n"); + return 0; + } + + for (i = 0; i < over_top_dup_size; ++i) + if (over_top_dup[i] != 1) + { + printf ("FAIL: malloc expands info table\n"); + return 0; + } + + printf ("PASS: malloc expands info table\n"); + return 0; +} -- cgit v1.2.3