This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Document BEGIN, END, etc. in perlsyn
[perl5.git] / x2p / util.c
CommitLineData
8665f9e4 1/* util.c
a687059c 2 *
4bb101f2 3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1999,
8665f9e4 4 * 2000, 2001, 2005 by Larry Wall and others
a687059c 5 *
d48672a2
LW
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
8d063cd8
LW
8 */
9
8d063cd8
LW
10#include "EXTERN.h"
11#include "a2p.h"
12#include "INTERN.h"
13#include "util.h"
14
17c3b450 15#include <stdarg.h>
8d063cd8 16#define FLUSH
8d063cd8 17
8665f9e4 18static const char nomem[] = "Out of memory!\n";
8d063cd8
LW
19
20/* paranoid version of malloc */
21
8d063cd8 22
75f92628 23Malloc_t
e9cb6d14 24safemalloc(MEM_SIZE size)
8d063cd8 25{
6d82b384 26 Malloc_t ptr;
8d063cd8 27
6d82b384
CS
28 /* malloc(0) is NASTY on some systems */
29 ptr = malloc(size ? size : 1);
8d063cd8
LW
30#ifdef DEBUGGING
31 if (debug & 128)
fb73857a 32 fprintf(stderr,"0x%lx: (%05d) malloc %ld bytes\n",(unsigned long)ptr,
33 an++,(long)size);
8d063cd8 34#endif
d34ed59f 35 if (ptr != NULL)
8d063cd8
LW
36 return ptr;
37 else {
38 fputs(nomem,stdout) FLUSH;
39 exit(1);
40 }
41 /*NOTREACHED*/
2c5424a7 42 return 0;
8d063cd8
LW
43}
44
45/* paranoid version of realloc */
46
75f92628 47Malloc_t
e9cb6d14 48saferealloc(Malloc_t where, MEM_SIZE size)
8d063cd8 49{
6d82b384 50 Malloc_t ptr;
8d063cd8 51
6d82b384
CS
52 /* realloc(0) is NASTY on some systems */
53 ptr = realloc(where, size ? size : 1);
8d063cd8
LW
54#ifdef DEBUGGING
55 if (debug & 128) {
68dc0745 56 fprintf(stderr,"0x%lx: (%05d) rfree\n",(unsigned long)where,an++);
fb73857a 57 fprintf(stderr,"0x%lx: (%05d) realloc %ld bytes\n",(unsigned long)ptr,an++,(long)size);
8d063cd8
LW
58 }
59#endif
d34ed59f 60 if (ptr != NULL)
8d063cd8
LW
61 return ptr;
62 else {
63 fputs(nomem,stdout) FLUSH;
64 exit(1);
65 }
66 /*NOTREACHED*/
2c5424a7 67 return 0;
8d063cd8
LW
68}
69
70/* safe version of free */
71
6d82b384 72Free_t
e9cb6d14 73safefree(Malloc_t where)
8d063cd8
LW
74{
75#ifdef DEBUGGING
76 if (debug & 128)
68dc0745 77 fprintf(stderr,"0x%lx: (%05d) free\n",(unsigned long)where,an++);
8d063cd8
LW
78#endif
79 free(where);
80}
81
8d063cd8
LW
82/* copy a string up to some (non-backslashed) delimiter, if any */
83
84char *
f0f333f4 85cpytill(register char *to, register char *from, register int delim)
8d063cd8
LW
86{
87 for (; *from; from++,to++) {
378cc40b
LW
88 if (*from == '\\') {
89 if (from[1] == delim)
90 from++;
91 else if (from[1] == '\\')
92 *to++ = *from++;
93 }
8d063cd8
LW
94 else if (*from == delim)
95 break;
96 *to = *from;
97 }
98 *to = '\0';
99 return from;
100}
101
378cc40b 102
8d063cd8 103char *
f0f333f4 104cpy2(register char *to, register char *from, register int delim)
8d063cd8
LW
105{
106 for (; *from; from++,to++) {
378cc40b 107 if (*from == '\\')
8d063cd8
LW
108 *to++ = *from++;
109 else if (*from == '$')
110 *to++ = '\\';
111 else if (*from == delim)
112 break;
113 *to = *from;
114 }
115 *to = '\0';
116 return from;
117}
118
119/* return ptr to little string in big string, NULL if not found */
120
121char *
aab39148 122instr(char *big, const char *little)
8d063cd8 123{
aab39148
RB
124 register char *t, *x;
125 register const char *s;
8d063cd8
LW
126
127 for (t = big; *t; t++) {
128 for (x=t,s=little; *s; x++,s++) {
129 if (!*x)
d34ed59f 130 return NULL;
8d063cd8
LW
131 if (*s != *x)
132 break;
133 }
134 if (!*s)
135 return t;
136 }
d34ed59f 137 return NULL;
8d063cd8
LW
138}
139
140/* copy a string to a safe spot */
141
142char *
aab39148 143savestr(const char *str)
8d063cd8 144{
8665f9e4 145 register char * const newaddr = (char *) safemalloc((MEM_SIZE)(strlen(str)+1));
8d063cd8
LW
146
147 (void)strcpy(newaddr,str);
148 return newaddr;
149}
150
151/* grow a static string to at least a certain length */
152
153void
f0f333f4 154growstr(char **strptr, int *curlen, int newlen)
8d063cd8
LW
155{
156 if (newlen > *curlen) { /* need more room? */
157 if (*curlen)
f0f333f4 158 *strptr = (char *) saferealloc(*strptr,(MEM_SIZE)newlen);
8d063cd8 159 else
f0f333f4 160 *strptr = (char *) safemalloc((MEM_SIZE)newlen);
8d063cd8
LW
161 *curlen = newlen;
162 }
163}
164
e50aee73 165void
b7787f18 166fatal(const char *pat,...)
8d063cd8 167{
17c3b450 168#if defined(HAS_VPRINTF)
55497cff 169 va_list args;
170
171 va_start(args, pat);
172 vfprintf(stderr,pat,args);
304b2fa9 173 va_end(args);
55497cff 174#else
8d063cd8 175 fprintf(stderr,pat,a1,a2,a3,a4);
55497cff 176#endif
8d063cd8
LW
177 exit(1);
178}
179
f72d1791 180#if defined(DARWIN)
8f1f23e8
W
181__private_extern__ /* warn() conflicts with libc */
182#endif
9c8d0b29 183void
b7787f18 184warn(const char *pat,...)
a687059c 185{
17c3b450 186#if defined(HAS_VPRINTF)
55497cff 187 va_list args;
188
189 va_start(args, pat);
190 vfprintf(stderr,pat,args);
304b2fa9 191 va_end(args);
55497cff 192#else
a687059c 193 fprintf(stderr,pat,a1,a2,a3,a4);
55497cff 194#endif
a687059c
LW
195}
196