1 /* $Header: str.c,v 3.0.1.1 89/10/26 23:23:41 lwall Locked $
3 * Copyright (c) 1989, Larry Wall
5 * You may distribute under the terms of the GNU General Public License
6 * as specified in the README file that comes with the perl 3.0 kit.
9 * Revision 3.0.1.1 89/10/26 23:23:41 lwall
10 * patch1: string ordering tests were wrong
11 * patch1: $/ now works even when STDSTDIO undefined
13 * Revision 3.0 89/10/18 15:23:38 lwall
22 extern char **environ;
30 tainted |= str->str_tainted;
32 return str->str_pok ? str->str_ptr : str_2ptr(str);
36 /* dlb ... guess we have a "crippled cc".
37 * dlb the following functions are usually macros.
44 if (*Str->str_ptr > '0' ||
46 (Str->str_cur && *Str->str_ptr != '0'))
51 return (Str->str_u.str_nval != 0.0);
61 tainted |= Str->str_tainted;
64 return Str->str_u.str_nval;
68 /* dlb ... end of crutch */
75 register char *s = str->str_ptr;
77 if (str->str_state == SS_INCR) { /* data before str_ptr? */
78 str->str_len += str->str_u.str_useful;
79 str->str_ptr -= str->str_u.str_useful;
80 str->str_u.str_useful = 0L;
81 bcopy(s, str->str_ptr, str->str_cur+1);
83 str->str_state = SS_NORM; /* normal again */
84 if (newlen > str->str_len)
85 newlen += 10 * (newlen - str->str_cur); /* avoid copy each time */
87 if (newlen > str->str_len) { /* need more room? */
91 New(703,s,newlen,char);
93 str->str_len = newlen;
102 str->str_u.str_nval = num;
103 str->str_state = SS_NORM;
104 str->str_pok = 0; /* invalidate pointer */
105 str->str_nok = 1; /* validate number */
107 str->str_tainted = tainted;
125 olderrno = errno; /* some Xenix systems wipe out errno here */
126 #if defined(scs) && defined(ns32000)
127 gcvt(str->str_u.str_nval,20,s);
130 if (str->str_u.str_nval == 0.0)
134 (void)sprintf(s,"%.20g",str->str_u.str_nval);
140 if (str == &str_undef)
143 warn("Use of uninitialized variable");
148 str->str_cur = s - str->str_ptr;
152 fprintf(stderr,"0x%lx ptr(%s)\n",str,str->str_ptr);
163 str->str_state = SS_NORM;
164 if (str->str_len && str->str_pok)
165 str->str_u.str_nval = atof(str->str_ptr);
167 if (str == &str_undef)
170 warn("Use of uninitialized variable");
171 str->str_u.str_nval = 0.0;
176 fprintf(stderr,"0x%lx num(%g)\n",str,str->str_u.str_nval);
178 return str->str_u.str_nval;
186 tainted |= sstr->str_tainted;
189 dstr->str_pok = dstr->str_nok = 0;
190 else if (sstr->str_pok) {
191 str_nset(dstr,sstr->str_ptr,sstr->str_cur);
193 dstr->str_u.str_nval = sstr->str_u.str_nval;
195 dstr->str_state = SS_NORM;
197 else if (sstr->str_cur == sizeof(STBP)) {
198 char *tmps = sstr->str_ptr;
200 if (*tmps == 'S' && bcmp(tmps,"Stab",4) == 0) {
201 dstr->str_magic = str_smake(sstr->str_magic);
202 dstr->str_magic->str_rare = 'X';
206 else if (sstr->str_nok)
207 str_numset(dstr,sstr->str_u.str_nval);
209 dstr->str_pok = dstr->str_nok = 0;
212 str_nset(str,ptr,len)
217 STR_GROW(str, len + 1);
218 (void)bcopy(ptr,str->str_ptr,len);
220 *(str->str_ptr+str->str_cur) = '\0';
221 str->str_nok = 0; /* invalidate number */
222 str->str_pok = 1; /* validate pointer */
224 str->str_tainted = tainted;
237 STR_GROW(str, len + 1);
238 (void)bcopy(ptr,str->str_ptr,len+1);
240 str->str_nok = 0; /* invalidate number */
241 str->str_pok = 1; /* validate pointer */
243 str->str_tainted = tainted;
247 str_chop(str,ptr) /* like set but assuming ptr is in str */
254 fatal("str_chop: internal inconsistency");
255 delta = ptr - str->str_ptr;
256 str->str_len -= delta;
257 str->str_cur -= delta;
258 str->str_ptr += delta;
259 if (str->str_state == SS_INCR)
260 str->str_u.str_useful += delta;
262 str->str_u.str_useful = delta;
263 str->str_state = SS_INCR;
265 str->str_nok = 0; /* invalidate number */
266 str->str_pok = 1; /* validate pointer (and unstudy str) */
269 str_ncat(str,ptr,len)
276 STR_GROW(str, str->str_cur + len + 1);
277 (void)bcopy(ptr,str->str_ptr+str->str_cur,len);
279 *(str->str_ptr+str->str_cur) = '\0';
280 str->str_nok = 0; /* invalidate number */
281 str->str_pok = 1; /* validate pointer */
283 str->str_tainted |= tainted;
292 tainted |= sstr->str_tainted;
296 if (!(sstr->str_pok))
297 (void)str_2ptr(sstr);
299 str_ncat(dstr,sstr->str_ptr,sstr->str_cur);
313 STR_GROW(str, str->str_cur + len + 1);
314 (void)bcopy(ptr,str->str_ptr+str->str_cur,len+1);
316 str->str_nok = 0; /* invalidate number */
317 str->str_pok = 1; /* validate pointer */
319 str->str_tainted |= tainted;
324 str_append_till(str,from,fromend,delim,keeplist)
327 register char *fromend;
336 len = fromend - from;
337 STR_GROW(str, str->str_cur + len + 1);
338 str->str_nok = 0; /* invalidate number */
339 str->str_pok = 1; /* validate pointer */
340 to = str->str_ptr+str->str_cur;
341 for (; from < fromend; from++,to++) {
342 if (*from == '\\' && from+1 < fromend && delim != '\\') {
344 if (from[1] == delim || from[1] == '\\')
349 else if (from[1] && index(keeplist,from[1]))
354 else if (*from == delim)
359 str->str_cur = to - str->str_ptr;
376 freestrroot = str->str_magic;
377 str->str_magic = Nullstr;
378 str->str_state = SS_NORM;
381 Newz(700+x,str,1,STR);
384 STR_GROW(str, len + 1);
389 str_magic(str, stab, how, name, namlen)
398 str->str_magic = Str_new(75,namlen);
399 str = str->str_magic;
400 str->str_u.str_stab = stab;
403 str_nset(str,name,namlen);
407 str_insert(bigstr,offset,len,little,littlelen)
416 register char *midend;
417 register char *bigend;
421 if (i > 0) { /* string might grow */
422 STR_GROW(bigstr, bigstr->str_cur + i + 1);
423 big = bigstr->str_ptr;
424 mid = big + offset + len;
425 midend = bigend = big + bigstr->str_cur;
428 while (midend > mid) /* shove everything down */
429 *--bigend = *--midend;
430 (void)bcopy(little,big+offset,littlelen);
431 bigstr->str_cur += i;
435 (void)bcopy(little,bigstr->str_ptr+offset,len);
439 big = bigstr->str_ptr;
442 bigend = big + bigstr->str_cur;
445 fatal("panic: str_insert");
447 bigstr->str_pok = SP_VALID; /* disable possible screamer */
449 if (mid - big > bigend - midend) { /* faster to shorten from end */
451 (void)bcopy(little, mid, littlelen);
456 (void)bcopy(midend, mid, i);
460 bigstr->str_cur = mid - big;
462 else if (i = mid - big) { /* faster from front */
465 str_chop(bigstr,midend-i);
470 (void)bcopy(little, mid, littlelen);
472 else if (littlelen) {
474 str_chop(bigstr,midend);
475 (void)bcopy(little,midend,littlelen);
478 str_chop(bigstr,midend);
483 /* make str point to what nstr did */
486 str_replace(str,nstr)
490 if (str->str_state == SS_INCR)
491 str_grow(str,0); /* just force copy down */
492 if (nstr->str_state == SS_INCR)
495 Safefree(str->str_ptr);
496 str->str_ptr = nstr->str_ptr;
497 str->str_len = nstr->str_len;
498 str->str_cur = nstr->str_cur;
499 str->str_pok = nstr->str_pok;
500 str->str_nok = nstr->str_nok;
502 str->str_u = nstr->str_u;
504 str->str_u.str_nval = nstr->str_u.str_nval;
507 str->str_tainted = nstr->str_tainted;
518 if (str->str_state) {
519 if (str->str_state == SS_FREE) /* already freed */
521 if (str->str_state == SS_INCR && !(str->str_pok & 2)) {
522 str->str_ptr -= str->str_u.str_useful;
523 str->str_len += str->str_u.str_useful;
527 str_free(str->str_magic);
530 Safefree(str->str_ptr);
531 if ((str->str_pok & SP_INTRP) && str->str_u.str_args)
532 arg_free(str->str_u.str_args);
536 if (str->str_len > 127) { /* next user not likely to want more */
537 Safefree(str->str_ptr); /* so give it back to malloc */
538 str->str_ptr = Nullch;
542 str->str_ptr[0] = '\0';
544 if ((str->str_pok & SP_INTRP) && str->str_u.str_args)
545 arg_free(str->str_u.str_args);
549 str->str_state = SS_FREE;
551 str->str_tainted = 0;
553 str->str_magic = freestrroot;
555 #endif /* LEAKTEST */
576 return str2 == Nullstr;
581 (void)str_2ptr(str1);
583 (void)str_2ptr(str2);
585 if (str1->str_cur != str2->str_cur)
588 return !bcmp(str1->str_ptr, str2->str_ptr, str1->str_cur);
598 return str2 == Nullstr;
603 (void)str_2ptr(str1);
605 (void)str_2ptr(str2);
607 if (str1->str_cur < str2->str_cur) {
608 if (retval = memcmp(str1->str_ptr, str2->str_ptr, str1->str_cur))
613 else if (retval = memcmp(str1->str_ptr, str2->str_ptr, str2->str_cur))
615 else if (str1->str_cur == str2->str_cur)
622 str_gets(str,fp,append)
627 register char *bp; /* we're going to steal some values */
628 register int cnt; /* from the stdio struct and put EVERYTHING */
629 register STDCHAR *ptr; /* in the innermost loop into registers */
630 register char newline = record_separator;/* (assuming >= 6 registers) */
634 register int get_paragraph;
635 register char *oldbp;
637 if (get_paragraph = !rslen) { /* yes, that's an assignment */
639 oldbp = Nullch; /* remember last \n position (none) */
641 #ifdef STDSTDIO /* Here is some breathtakingly efficient cheating */
643 cnt = fp->_cnt; /* get count into register */
644 str->str_nok = 0; /* invalidate number */
645 str->str_pok = 1; /* validate pointer */
646 if (str->str_len <= cnt + 1) /* make sure we have the room */
647 STR_GROW(str, append+cnt+2); /* (remembering cnt can be -1) */
648 bp = str->str_ptr + append; /* move these two too to registers */
652 while (--cnt >= 0) { /* this */ /* eat */
653 if ((*bp++ = *ptr++) == newline) /* really */ /* dust */
654 goto thats_all_folks; /* screams */ /* sed :-) */
657 fp->_cnt = cnt; /* deregisterize cnt and ptr */
659 i = _filbuf(fp); /* get more characters */
661 ptr = fp->_ptr; /* reregisterize cnt and ptr */
663 bpx = bp - str->str_ptr; /* prepare for possible relocation */
664 if (get_paragraph && oldbp)
665 obpx = oldbp - str->str_ptr;
666 STR_GROW(str, bpx + cnt + 2);
667 bp = str->str_ptr + bpx; /* reconstitute our pointer */
668 if (get_paragraph && oldbp)
669 oldbp = str->str_ptr + obpx;
671 if (i == newline) { /* all done for now? */
673 goto thats_all_folks;
675 else if (i == EOF) /* all done for ever? */
676 goto thats_really_all_folks;
677 *bp++ = i; /* now go back to screaming loop */
681 if (get_paragraph && bp - 1 != oldbp) {
682 oldbp = bp; /* remember where this newline was */
683 goto screamer; /* and go back to the fray */
685 thats_really_all_folks:
686 fp->_cnt = cnt; /* put these back or we're in trouble */
689 str->str_cur = bp - str->str_ptr; /* set length */
691 #else /* !STDSTDIO */ /* The big, slow, and stupid way */
694 static char buf[8192];
695 char * bpe = buf + sizeof(buf) - 3;
700 while ((i = getc(fp)) != EOF && (*bp++ = i) != newline && bp < bpe);
701 if (i == newline && get_paragraph &&
702 (i = getc(fp)) != EOF && (*bp++ = i) != newline && bp < bpe)
710 if (i != newline && i != EOF) {
716 #endif /* STDSTDIO */
718 return str->str_cur - append ? str->str_ptr : Nullch;
727 line_t oldline = line;
730 str_sset(linestr,str);
732 oldoldbufptr = oldbufptr = bufptr = str_get(linestr);
733 bufend = bufptr + linestr->str_cur;
734 if (setjmp(eval_env)) {
736 fatal("%s\n",stab_val(stabent("@",TRUE))->str_ptr);
741 if (retval || error_count)
742 fatal("Invalid component in string or format");
745 if (cmd->c_type != C_EXPR || cmd->c_next || arg->arg_type != O_LIST)
746 fatal("panic: error in parselist %d %x %d", cmd->c_type,
747 cmd->c_next, arg ? arg->arg_type : -1);
757 register char *s = str_get(src);
758 register char *send = s + src->str_cur;
763 register int brackets;
768 toparse = Str_new(76,0);
772 str_nset(toparse,"",0);
775 if (*s == '\\' && s[1] && index("$@[{\\]}",s[1])) {
776 str_ncat(str, t, s - t);
778 if (*nointrp && s+1 < send)
779 if (*s != '@' && (*s != '$' || index(nointrp,s[1])))
781 str_ncat(str, "$b", 2);
786 else if ((*s == '@' || (*s == '$' && !index(nointrp,s[1]))) &&
790 if (*s == '$' && s[1] == '#' && isalpha(s[2]) || s[2] == '_')
792 s = scanreg(s,send,tokenbuf);
794 (!(stab = stabent(tokenbuf,FALSE)) || !stab_xarray(stab)) ) {
797 continue; /* grandfather @ from old scripts */
799 str_ncat(str,"$a",2);
800 str_ncat(toparse,",",1);
801 if (t[1] != '{' && (*s == '[' || *s == '{' /* }} */ ) &&
802 (stab = stabent(tokenbuf,FALSE)) &&
803 ((*s == '[') ? (stab_xarray(stab) != 0) : (stab_xhash(stab) != 0)) ) {
817 s = cpytill(tokenbuf,s+1,send,*s,&len);
819 fatal("Unterminated string");
824 } while (brackets > 0 && s < send);
826 fatal("Unmatched brackets in string");
827 if (*nointrp) { /* we're in a regular expression */
829 if (*d == '{' && s[-1] == '}') { /* maybe {n,m} */
831 if (isdigit(*d)) { /* matches /^{\d,?\d*}$/ */
837 s = checkpoint; /* Is {n,m}! Backoff! */
840 else if (*d == '[' && s[-1] == ']') { /* char class? */
841 int weight = 2; /* let's weigh the evidence */
843 unsigned char uchar = 0, lastuchar;
849 else if (d[1] == '$')
853 if (isdigit(d[2]) && !d[3])
859 for (d++; d < s; d++) {
861 uchar = (unsigned char)*d;
865 weight -= seen[uchar] * 10;
866 if (isalpha(d[1]) || isdigit(d[1]) ||
868 d = scanreg(d,s,tokenbuf);
869 if (stabent(tokenbuf,FALSE))
874 else if (*d == '$' && d[1] &&
875 index("[#!%*<>()-=",d[1])) {
876 if (!d[2] || /*{*/ index("])} =",d[2]))
885 if (index("wds",d[1]))
887 else if (seen['\''] || seen['"'])
889 else if (index("rnftb",d[1]))
891 else if (isdigit(d[1])) {
893 while (d[1] && isdigit(d[1]))
901 if (lastuchar < d[1] || d[1] == '\\') {
902 if (index("aA01! ",lastuchar))
904 if (index("zZ79~",d[1]))
910 if (isalpha(*d) && d[1] && isalpha(d[1])) {
916 if (uchar == lastuchar + 1)
918 weight -= seen[uchar];
925 fprintf(stderr,"[%s] weight %d\n",
926 checkpoint+1,weight);
929 if (weight >= 0) /* probably a character class */
935 str_ncat(toparse, "join($\",", 8);
936 if (t[1] == '{' && s[-1] == '}') {
937 str_ncat(toparse, t, 1);
938 str_ncat(toparse, t+2, s - t - 3);
941 str_ncat(toparse, t, s - t);
943 str_ncat(toparse, ")", 1);
950 if (toparse->str_ptr && *toparse->str_ptr == ',') {
951 *toparse->str_ptr = '(';
952 str_ncat(toparse,",$$);",5);
953 str->str_u.str_args = parselist(toparse);
954 str->str_u.str_args->arg_len--; /* ignore $$ reference */
957 str->str_u.str_args = Nullarg;
959 str->str_pok |= SP_INTRP;
961 str_replace(src,str);
975 if (!(src->str_pok & SP_INTRP)) {
976 int oldsave = savestack->ary_fill;
978 (void)savehptr(&curstash);
979 curstash = src->str_u.str_hash; /* so stabent knows right package */
981 restorelist(oldsave);
983 s = src->str_ptr; /* assumed valid since str_pok set */
985 send = s + src->str_cur;
987 if (src->str_u.str_args) {
988 (void)eval(src->str_u.str_args,G_ARRAY,sp);
989 /* Assuming we have correct # of args */
990 elem = stack->ary_array + sp;
995 if (*s == '$' && s+1 < send) {
999 str_scat(str,*++elem);
1002 str_ncat(str,++s,1);
1010 str_ncat(str,t,s-t);
1023 str->str_u.str_nval += 1.0;
1027 if (!str->str_pok || !*str->str_ptr) {
1028 str->str_u.str_nval = 1.0;
1034 while (isalpha(*d)) d++;
1035 while (isdigit(*d)) d++;
1037 str_numset(str,atof(str->str_ptr) + 1.0); /* punt */
1041 while (d >= str->str_ptr) {
1051 *(d--) -= 'z' - 'a' + 1;
1054 /* oh,oh, the number grew */
1055 STR_GROW(str, str->str_cur + 2);
1057 for (d = str->str_ptr + str->str_cur; d > str->str_ptr; d--)
1072 str->str_u.str_nval -= 1.0;
1076 if (!str->str_pok) {
1077 str->str_u.str_nval = -1.0;
1081 str_numset(str,atof(str->str_ptr) - 1.0);
1084 /* Make a string that will exist for the duration of the expression
1085 * evaluation. Actually, it may have to last longer than that, but
1086 * hopefully cmd_exec won't free it until it has been assigned to a
1087 * permanent location. */
1089 static long tmps_size = -1;
1095 register STR *str = Str_new(78,0);
1097 str_sset(str,oldstr);
1098 if (++tmps_max > tmps_size) {
1099 tmps_size = tmps_max;
1100 if (!(tmps_size & 127)) {
1102 Renew(tmps_list, tmps_size + 128, STR*);
1104 New(702,tmps_list, 128, STR*);
1107 tmps_list[tmps_max] = str;
1111 /* same thing without the copying */
1117 if (++tmps_max > tmps_size) {
1118 tmps_size = tmps_max;
1119 if (!(tmps_size & 127)) {
1121 Renew(tmps_list, tmps_size + 128, STR*);
1123 New(704,tmps_list, 128, STR*);
1126 tmps_list[tmps_max] = str;
1135 register STR *str = Str_new(79,0);
1139 str_nset(str,s,len);
1147 register STR *str = Str_new(80,0);
1153 /* make an exact duplicate of old */
1159 register STR *new = Str_new(81,0);
1163 if (old->str_state == SS_FREE) {
1164 warn("semi-panic: attempt to dup freed string");
1167 if (old->str_state == SS_INCR && !(old->str_pok & 2))
1170 Safefree(new->str_ptr);
1171 Copy(old,new,1,STR);
1173 new->str_ptr = nsavestr(old->str_ptr,old->str_len);
1181 register HENT *entry;
1182 register STAB *stab;
1185 register SPAT *spat;
1188 if (!*s) { /* reset ?? searches */
1189 for (spat = stash->tbl_spatroot;
1191 spat = spat->spat_next) {
1192 spat->spat_flags &= ~SPAT_USED;
1197 /* reset variables */
1205 for ( ; i <= max; i++) {
1206 for (entry = stash->tbl_array[i];
1208 entry = entry->hent_next) {
1209 stab = (STAB*)entry->hent_val;
1210 str = stab_val(stab);
1214 str->str_tainted = tainted;
1216 if (str->str_ptr != Nullch)
1217 str->str_ptr[0] = '\0';
1218 if (stab_xarray(stab)) {
1219 aclear(stab_xarray(stab));
1221 if (stab_xhash(stab)) {
1222 hclear(stab_xhash(stab));
1223 if (stab == envstab)
1224 environ[0] = Nullch;
1237 fprintf(stderr,"%s %d %d %d\n",s,tainted,uid, euid);
1239 if (tainted && (!euid || euid != uid)) {
1249 register STR *envstr;
1251 envstr = hfetch(stab_hash(envstab),"PATH",4,FALSE);
1252 if (!envstr || envstr->str_tainted) {
1254 taintproper("Insecure PATH");
1256 envstr = hfetch(stab_hash(envstab),"IFS",3,FALSE);
1257 if (envstr && envstr->str_tainted) {
1259 taintproper("Insecure IFS");