run.c (221381) | run.c (224731) |
---|---|
1/**************************************************************** 2Copyright (C) Lucent Technologies 1997 3All Rights Reserved 4 5Permission to use, copy, modify, and distribute this software and 6its documentation for any purpose and without fee is hereby 7granted, provided that the above copyright notice appear in all 8copies and that both that the copyright notice and this --- 9 unchanged lines hidden (view full) --- 18SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 20IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 21ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 22THIS SOFTWARE. 23****************************************************************/ 24 25#include <sys/cdefs.h> | 1/**************************************************************** 2Copyright (C) Lucent Technologies 1997 3All Rights Reserved 4 5Permission to use, copy, modify, and distribute this software and 6its documentation for any purpose and without fee is hereby 7granted, provided that the above copyright notice appear in all 8copies and that both that the copyright notice and this --- 9 unchanged lines hidden (view full) --- 18SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 20IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 21ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 22THIS SOFTWARE. 23****************************************************************/ 24 25#include <sys/cdefs.h> |
26__FBSDID("$FreeBSD: head/contrib/one-true-awk/run.c 221381 2011-05-03 11:47:19Z ru $"); | 26__FBSDID("$FreeBSD: head/contrib/one-true-awk/run.c 224731 2011-08-09 12:54:43Z ru $"); |
27 28#define DEBUG 29#include <stdio.h> 30#include <ctype.h> 31#include <setjmp.h> 32#include <limits.h> 33#include <math.h> 34#include <string.h> --- 1199 unchanged lines hidden (view full) --- 1234 ap = execute(a[1]); /* array name */ 1235 freesymtab(ap); 1236 dprintf( ("split: s=|%s|, a=%s, sep=|%s|\n", s, NN(ap->nval), fs) ); 1237 ap->tval &= ~STR; 1238 ap->tval |= ARR; 1239 ap->sval = (char *) makesymtab(NSYMTAB); 1240 1241 n = 0; | 27 28#define DEBUG 29#include <stdio.h> 30#include <ctype.h> 31#include <setjmp.h> 32#include <limits.h> 33#include <math.h> 34#include <string.h> --- 1199 unchanged lines hidden (view full) --- 1234 ap = execute(a[1]); /* array name */ 1235 freesymtab(ap); 1236 dprintf( ("split: s=|%s|, a=%s, sep=|%s|\n", s, NN(ap->nval), fs) ); 1237 ap->tval &= ~STR; 1238 ap->tval |= ARR; 1239 ap->sval = (char *) makesymtab(NSYMTAB); 1240 1241 n = 0; |
1242 if (arg3type == REGEXPR && strlen((char*)((fa*)a[2])->restr) == 0) { 1243 /* split(s, a, //); have to arrange that it looks like empty sep */ 1244 arg3type = 0; 1245 fs = ""; 1246 sep = 0; 1247 } |
|
1242 if (*s != '\0' && (strlen(fs) > 1 || arg3type == REGEXPR)) { /* reg expr */ 1243 fa *pfa; 1244 if (arg3type == REGEXPR) { /* it's ready already */ 1245 pfa = (fa *) a[2]; 1246 } else { 1247 pfa = makedfa(fs, 1); 1248 } 1249 if (nematch(pfa,s)) { --- 366 unchanged lines hidden (view full) --- 1616 tempfree(x); 1617 return fp; 1618} 1619 1620struct files { 1621 FILE *fp; 1622 const char *fname; 1623 int mode; /* '|', 'a', 'w' => LE/LT, GT */ | 1248 if (*s != '\0' && (strlen(fs) > 1 || arg3type == REGEXPR)) { /* reg expr */ 1249 fa *pfa; 1250 if (arg3type == REGEXPR) { /* it's ready already */ 1251 pfa = (fa *) a[2]; 1252 } else { 1253 pfa = makedfa(fs, 1); 1254 } 1255 if (nematch(pfa,s)) { --- 366 unchanged lines hidden (view full) --- 1622 tempfree(x); 1623 return fp; 1624} 1625 1626struct files { 1627 FILE *fp; 1628 const char *fname; 1629 int mode; /* '|', 'a', 'w' => LE/LT, GT */ |
1624} files[FOPEN_MAX] ={ 1625 { NULL, "/dev/stdin", LT }, /* watch out: don't free this! */ 1626 { NULL, "/dev/stdout", GT }, 1627 { NULL, "/dev/stderr", GT } 1628}; | 1630} *files; |
1629 | 1631 |
1632int nfiles; 1633 |
|
1630void stdinit(void) /* in case stdin, etc., are not constants */ 1631{ | 1634void stdinit(void) /* in case stdin, etc., are not constants */ 1635{ |
1632 files[0].fp = stdin; 1633 files[1].fp = stdout; 1634 files[2].fp = stderr; | 1636 nfiles = FOPEN_MAX; 1637 files = calloc(nfiles, sizeof(*files)); 1638 if (files == NULL) 1639 FATAL("can't allocate file memory for %u files", nfiles); 1640 files[0].fp = stdin; 1641 files[0].fname = "/dev/stdin"; 1642 files[0].mode = LT; 1643 files[1].fp = stdout; 1644 files[1].fname = "/dev/stdout"; 1645 files[1].mode = GT; 1646 files[2].fp = stderr; 1647 files[2].fname = "/dev/stderr"; 1648 files[2].mode = GT; |
1635} 1636 1637FILE *openfile(int a, const char *us) 1638{ 1639 const char *s = us; 1640 int i, m; 1641 FILE *fp = 0; 1642 1643 if (*s == '\0') 1644 FATAL("null file name in print or getline"); | 1649} 1650 1651FILE *openfile(int a, const char *us) 1652{ 1653 const char *s = us; 1654 int i, m; 1655 FILE *fp = 0; 1656 1657 if (*s == '\0') 1658 FATAL("null file name in print or getline"); |
1645 for (i=0; i < FOPEN_MAX; i++) | 1659 for (i=0; i < nfiles; i++) |
1646 if (files[i].fname && strcmp(s, files[i].fname) == 0) { 1647 if (a == files[i].mode || (a==APPEND && files[i].mode==GT)) 1648 return files[i].fp; 1649 if (a == FFLUSH) 1650 return files[i].fp; 1651 } 1652 if (a == FFLUSH) /* didn't find it, so don't create it! */ 1653 return NULL; 1654 | 1660 if (files[i].fname && strcmp(s, files[i].fname) == 0) { 1661 if (a == files[i].mode || (a==APPEND && files[i].mode==GT)) 1662 return files[i].fp; 1663 if (a == FFLUSH) 1664 return files[i].fp; 1665 } 1666 if (a == FFLUSH) /* didn't find it, so don't create it! */ 1667 return NULL; 1668 |
1655 for (i=0; i < FOPEN_MAX; i++) | 1669 for (i=0; i < nfiles; i++) |
1656 if (files[i].fp == 0) 1657 break; | 1670 if (files[i].fp == 0) 1671 break; |
1658 if (i >= FOPEN_MAX) 1659 FATAL("%s makes too many open files", s); | 1672 if (i >= nfiles) { 1673 struct files *nf; 1674 int nnf = nfiles + FOPEN_MAX; 1675 nf = realloc(files, nnf * sizeof(*nf)); 1676 if (nf == NULL) 1677 FATAL("cannot grow files for %s and %d files", s, nnf); 1678 memset(&nf[nfiles], 0, FOPEN_MAX * sizeof(*nf)); 1679 nfiles = nnf; 1680 files = nf; 1681 } |
1660 fflush(stdout); /* force a semblance of order */ 1661 m = a; 1662 if (a == GT) { 1663 fp = fopen(s, "w"); 1664 } else if (a == APPEND) { 1665 fp = fopen(s, "a"); 1666 m = GT; /* so can mix > and >> */ 1667 } else if (a == '|') { /* output pipe */ --- 11 unchanged lines hidden (view full) --- 1679 } 1680 return fp; 1681} 1682 1683const char *filename(FILE *fp) 1684{ 1685 int i; 1686 | 1682 fflush(stdout); /* force a semblance of order */ 1683 m = a; 1684 if (a == GT) { 1685 fp = fopen(s, "w"); 1686 } else if (a == APPEND) { 1687 fp = fopen(s, "a"); 1688 m = GT; /* so can mix > and >> */ 1689 } else if (a == '|') { /* output pipe */ --- 11 unchanged lines hidden (view full) --- 1701 } 1702 return fp; 1703} 1704 1705const char *filename(FILE *fp) 1706{ 1707 int i; 1708 |
1687 for (i = 0; i < FOPEN_MAX; i++) | 1709 for (i = 0; i < nfiles; i++) |
1688 if (fp == files[i].fp) 1689 return files[i].fname; 1690 return "???"; 1691} 1692 1693Cell *closefile(Node **a, int n) 1694{ 1695 Cell *x; 1696 int i, stat; 1697 1698 n = n; 1699 x = execute(a[0]); 1700 getsval(x); 1701 stat = -1; | 1710 if (fp == files[i].fp) 1711 return files[i].fname; 1712 return "???"; 1713} 1714 1715Cell *closefile(Node **a, int n) 1716{ 1717 Cell *x; 1718 int i, stat; 1719 1720 n = n; 1721 x = execute(a[0]); 1722 getsval(x); 1723 stat = -1; |
1702 for (i = 0; i < FOPEN_MAX; i++) { | 1724 for (i = 0; i < nfiles; i++) { |
1703 if (files[i].fname && strcmp(x->sval, files[i].fname) == 0) { 1704 if (ferror(files[i].fp)) 1705 WARNING( "i/o error occurred on %s", files[i].fname ); 1706 if (files[i].mode == '|' || files[i].mode == LE) 1707 stat = pclose(files[i].fp); 1708 else 1709 stat = fclose(files[i].fp); 1710 if (stat == EOF) --- 27 unchanged lines hidden (view full) --- 1738 } 1739 } 1740} 1741 1742void flush_all(void) 1743{ 1744 int i; 1745 | 1725 if (files[i].fname && strcmp(x->sval, files[i].fname) == 0) { 1726 if (ferror(files[i].fp)) 1727 WARNING( "i/o error occurred on %s", files[i].fname ); 1728 if (files[i].mode == '|' || files[i].mode == LE) 1729 stat = pclose(files[i].fp); 1730 else 1731 stat = fclose(files[i].fp); 1732 if (stat == EOF) --- 27 unchanged lines hidden (view full) --- 1760 } 1761 } 1762} 1763 1764void flush_all(void) 1765{ 1766 int i; 1767 |
1746 for (i = 0; i < FOPEN_MAX; i++) | 1768 for (i = 0; i < nfiles; i++) |
1747 if (files[i].fp) 1748 fflush(files[i].fp); 1749} 1750 1751void backsub(char **pb_ptr, char **sptr_ptr); 1752 1753Cell *sub(Node **a, int nnn) /* substitute command */ 1754{ --- 184 unchanged lines hidden --- | 1769 if (files[i].fp) 1770 fflush(files[i].fp); 1771} 1772 1773void backsub(char **pb_ptr, char **sptr_ptr); 1774 1775Cell *sub(Node **a, int nnn) /* substitute command */ 1776{ --- 184 unchanged lines hidden --- |