2 * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
3 * See the copyright notice in the ACK home directory, in the file "Copyright".
7 /**********************************************************************/
9 /* Several utility routines used throughout ack */
10 /* error handling, string handling and such. */
12 /**********************************************************************/
19 static char rcs_id[] = "$Id: util.c,v 2.5 1994/06/24 10:13:21 ceriel Exp $" ;
22 extern char *progname ;
26 extern char *calloc();
27 extern char *realloc();
30 # define STDOUT stdout
32 # define STDOUT stderr
35 char *basename(string) char *string ; {
36 static char retval[256] ;
37 char *last_dot, *last_start ;
39 register char *fetch ;
44 for ( fetch=string ; ; fetch++ ) {
45 switch ( ctoken= *fetch&0377 ) {
46 case SUFCHAR : last_dot=fetch ; break ;
47 case '/' : last_start=fetch+1 ; break ;
50 if ( !isascii(ctoken) || !isprint(ctoken) ) {
51 werror("non-ascii characters in argument %s",string) ;
55 if ( ! *last_start ) fuerror("empty filename \"%s\"",string) ;
56 for ( fetch= last_start, store=retval ;
57 *fetch && fetch!=last_dot && store< &retval[sizeof retval-1] ;
65 clr_noscan(str) char *str ; {
67 for ( ptr=str ; *ptr ; ptr++ ) {
72 char *skipblank(str) char *str ; {
75 for ( ptr=str ; *ptr==SPACE || *ptr==TAB ; ptr++ ) ;
79 char *firstblank(str) char *str ; {
82 for ( ptr=str ; *ptr && *ptr!=SPACE && *ptr!=TAB ; ptr++ ) ;
90 void fatal(const char *fmt, ...) {
93 /* Fatal internal error */
94 fprintf(STDOUT,"%s: fatal internal error, ",progname) ;
96 vfprintf(STDOUT,fmt,ap);
98 fprintf(STDOUT,"\n") ;
104 void vprint(const char *fmt, ...) {
107 /* Diagnostic print, no auto NL */
109 vfprintf(STDOUT,fmt,ap);
114 prns(s) register char *s ; {
116 putc((*s&0377)&~NO_SCAN,STDOUT) ;
123 void fuerror(const char *fmt, ...) {
126 /* Fatal user error */
127 fprintf(STDOUT,"%s: ",progname) ;
129 vfprintf(STDOUT,fmt,ap);
131 fprintf(STDOUT,"\n") ;
136 void werror(const char *fmt, ...) {
139 /* Warning user error, w_flag */
140 if ( w_flag ) return ;
141 fprintf(STDOUT,"%s: warning, ",progname) ;
143 vfprintf(STDOUT,fmt,ap);
145 fprintf(STDOUT,"\n") ;
149 void error(const char *fmt, ...) {
152 /* User error, it is the callers responsibility to quit */
153 fprintf(STDOUT,"%s: ",progname) ;
155 vfprintf(STDOUT,fmt,ap);
157 fprintf(STDOUT,"\n") ;
163 fatal(fmt,p1,p2,p3,p4,p5,p6,p7) char *fmt ; {
164 /* Fatal internal error */
165 fprintf(STDOUT,"%s: fatal internal error, ",progname) ;
166 fprintf(STDOUT,fmt,p1,p2,p3,p4,p5,p6,p7);
167 fprintf(STDOUT,"\n") ;
173 vprint(fmt,p1,p2,p3,p4,p5,p6,p7) char *fmt ; {
174 /* Diagnostic print, no auto NL */
175 fprintf(STDOUT,fmt,p1,p2,p3,p4,p5,p6,p7);
179 prns(s) register char *s ; {
181 putc((*s&0377)&~NO_SCAN,STDOUT) ;
188 fuerror(fmt,p1,p2,p3,p4,p5,p6,p7) char *fmt ; {
189 /* Fatal user error */
190 fprintf(STDOUT,"%s: ",progname) ;
191 fprintf(STDOUT,fmt,p1,p2,p3,p4,p5,p6,p7);
192 fprintf(STDOUT,"\n") ;
197 werror(fmt,p1,p2,p3,p4,p5,p6,p7) char *fmt ; {
198 /* Warning user error, w_flag */
199 if ( w_flag ) return ;
200 fprintf(STDOUT,"%s: warning, ",progname) ;
201 fprintf(STDOUT,fmt,p1,p2,p3,p4,p5,p6,p7);
202 fprintf(STDOUT,"\n") ;
206 error(fmt,p1,p2,p3,p4,p5,p6,p7) char *fmt ; {
207 /* User error, it is the callers responsibility to quit */
208 fprintf(STDOUT,"%s: ",progname) ;
209 fprintf(STDOUT,fmt,p1,p2,p3,p4,p5,p6,p7);
210 fprintf(STDOUT,"\n") ;
231 Keep the string in stable storage.
233 Remove the string stored by keep from stable storage.
236 char *keeps(str) char *str ; {
237 register char *result ;
238 result= getcore( (unsigned)(strlen(str)+1) ) ;
239 if ( !result ) fatal("Out of core") ;
240 return strcpy(result,str) ;
243 throws(str) char *str ; {
247 char *getcore(size) unsigned size ; {
248 register char *retptr ;
250 retptr= calloc(1,size) ;
251 if ( !retptr ) fatal("Out of memory") ;
255 char *changecore(ptr,size) char *ptr ; unsigned size ; {
256 register char *retptr ;
258 retptr= realloc(ptr,size) ;
259 if ( !retptr ) fatal("Out of memory") ;