Main Page | Class List | Directories | File List | Class Members | File Members

io.c File Reference

#include "awk.h"
#include <fcntl.h>

Go to the source code of this file.

Classes

struct  recmatch

Defines

#define O_ACCMODE   (O_RDONLY|O_WRONLY|O_RDWR)
#define ENFILE   EMFILE
#define at_eof(iop)   ((iop->flag & IOP_AT_EOF) != 0)
#define has_no_data(iop)   (iop->dataend == NULL)
#define no_data_left(iop)   (iop->off >= iop->dataend)
#define binmode(mode)   (mode)
#define getpgrp_arg()   getpid()
#define set_RT_to_null()
#define set_RT(str, len)
#define min(x, y)   (x < y ? x : y)

Typedefs

typedef enum recvalues RECVALUE
typedef enum scanstate SCANSTATE

Enumerations

enum  two_way_close_type { CLOSE_ALL, CLOSE_TO, CLOSE_FROM }
enum  recvalues { REC_OK, NOTERM, TERMATEND, TERMNEAREND }

Functions

static IOBUF *nextfile P ((int skipping))
void do_nextfile ()
static IOBUFnextfile (int skipping)
void set_FNR ()
void set_NR ()
static int inrec (IOBUF *iop)
static int iop_close (IOBUF *iop)
void do_input ()
const char * redflags2str (int flags)
redirectredirect (NODE *tree, int *errflg)
redirectgetredirect (const char *str, int len)
static void close_one ()
NODEdo_close (NODE *tree)
static int close_redir (register struct redirect *rp, int exitwarn, two_way_close_type how)
int flush_io ()
int close_io ()
static int str2mode (const char *mode)
int devopen (const char *name, const char *mode)
static void spec_setup (IOBUF *iop, int len, int allocate)
static int specfdopen (IOBUF *iop, const char *name, const char *mode)
static int pidopen (IOBUF *iop, const char *name, const char *mode ATTRIBUTE_UNUSED)
static int useropen (IOBUF *iop, const char *name ATTRIBUTE_UNUSED, const char *mode ATTRIBUTE_UNUSED)
static IOBUFiop_open (const char *name, const char *mode, IOBUF *iop)
static int two_way_open (const char *str, struct redirect *rp)
static int wait_any (int interesting)
static IOBUFgawk_popen (const char *cmd, struct redirect *rp)
static int gawk_pclose (struct redirect *rp)
NODEdo_getline (NODE *tree)
int pathopen (const char *file)
static int do_pathopen (const char *file)
static IOBUFiop_alloc (int fd, const char *name, IOBUF *iop)
static void grow_iop_buffer (IOBUF *iop)
static RECVALUE rs1scan (IOBUF *iop, struct recmatch *recm, SCANSTATE *state)
static RECVALUE rsrescan (IOBUF *iop, struct recmatch *recm, SCANSTATE *state)
static RECVALUE rsnullscan (IOBUF *iop, struct recmatch *recm, SCANSTATE *state)
int get_a_record (char **out, IOBUF *iop, int *errcode)
void set_RS ()
static int pty_vs_pipe (const char *command)
const char * iopflags2str (int flag)

Variables

int MRL
static struct redirectred_head = NULL
static NODERS
static RegexpRS_re_yes_case
static RegexpRS_re_no_case
static RegexpRS_regexp
int RS_is_null
int output_is_tty
NODEARGC_node
NODEARGV_node
NODEARGIND_node
NODEERRNO_node
NODE ** fields_arr
static jmp_buf filebuf


Define Documentation

#define at_eof iop   )     ((iop->flag & IOP_AT_EOF) != 0)
 

Definition at line 110 of file io.c.

Referenced by get_a_record(), inrec(), and nextfile().

#define binmode mode   )     (mode)
 

Definition at line 221 of file io.c.

Referenced by nextfile(), and redirect().

#define ENFILE   EMFILE
 

Definition at line 68 of file io.c.

Referenced by redirect().

 
#define getpgrp_arg  )     getpid()
 

Definition at line 1428 of file io.c.

Referenced by load_procinfo(), and pidopen().

#define has_no_data iop   )     (iop->dataend == NULL)
 

Definition at line 111 of file io.c.

Referenced by get_a_record().

#define min x,
 )     (x < y ? x : y)
 

Referenced by __mktime_internal(), and get_a_record().

#define no_data_left iop   )     (iop->off >= iop->dataend)
 

Definition at line 112 of file io.c.

Referenced by get_a_record(), and inrec().

#define O_ACCMODE   (O_RDONLY|O_WRONLY|O_RDWR)
 

Definition at line 37 of file io.c.

Referenced by devopen().

#define set_RT str,
len   ) 
 

Value:

(void)(! do_traditional && (unref(RT_node->var_value), \
                           RT_node->var_value = make_string(str, len)))

Definition at line 2440 of file io.c.

Referenced by get_a_record().

 
#define set_RT_to_null  ) 
 

Value:

(void)(! do_traditional && (unref(RT_node->var_value), \
                           RT_node->var_value = Nnull_string))

Definition at line 2436 of file io.c.

Referenced by get_a_record().


Typedef Documentation

typedef enum recvalues RECVALUE
 

typedef enum scanstate SCANSTATE
 


Enumeration Type Documentation

enum recvalues
 

Enumerator:
REC_OK 
NOTERM 
TERMATEND 
TERMNEAREND 

Definition at line 121 of file io.c.

00121                        {
00122         REC_OK,         /* record and terminator found, recmatch struct filled in */
00123         NOTERM,         /* no terminator found, give me more input data */
00124         TERMATEND,      /* found terminator at end of buffer */
00125         TERMNEAREND,    /* found terminator close to end of buffer, for RE might be bigger */
00126 } RECVALUE;

enum two_way_close_type
 

Enumerator:
CLOSE_ALL 
CLOSE_TO 
CLOSE_FROM 

Definition at line 104 of file io.c.


Function Documentation

int close_io  ) 
 

Definition at line 1015 of file io.c.

References _, CLOSE_ALL, close_redir(), do_lint, errno, redirect::next, NULL, red_head, redirect::status, strerror, and warning().

Referenced by main().

01016 {
01017         register struct redirect *rp;
01018         register struct redirect *next;
01019         int status = 0;
01020 
01021         errno = 0;
01022         for (rp = red_head; rp != NULL; rp = next) {
01023                 next = rp->next;
01024                 /*
01025                  * close_redir() will print a message if needed
01026                  * if do_lint, warn about lack of explicit close
01027                  */
01028                 if (close_redir(rp, do_lint, CLOSE_ALL))
01029                         status++;
01030                 rp = NULL;
01031         }
01032         /*
01033          * Some of the non-Unix os's have problems doing an fclose
01034          * on stdout and stderr.  Since we don't really need to close
01035          * them, we just flush them, and do that across the board.
01036          */
01037         if (fflush(stdout)) {
01038                 warning(_("error writing standard output (%s)"), strerror(errno));
01039                 status++;
01040         }
01041         if (fflush(stderr)) {
01042                 warning(_("error writing standard error (%s)"), strerror(errno));
01043                 status++;
01044         }
01045         return status;
01046 }

static void close_one  )  [static]
 

Definition at line 756 of file io.c.

References _, do_lint, errno, FALSE, fatal, fclose, redirect::flag, redirect::fp, lintwarn, redirect::next, NULL, redirect::prev, RED_FILE, red_head, RED_USED, strerror, TRUE, redirect::value, and warning().

Referenced by redirect().

00757 {
00758         register struct redirect *rp;
00759         register struct redirect *rplast = NULL;
00760 
00761         static short warned = FALSE;
00762 
00763         if (do_lint && ! warned) {
00764                 warned = TRUE;
00765                 lintwarn(_("reached system limit for open files: starting to multiplex file descriptors"));
00766         }
00767 
00768         /* go to end of list first, to pick up least recently used entry */
00769         for (rp = red_head; rp != NULL; rp = rp->next)
00770                 rplast = rp;
00771         /* now work back up through the list */
00772         for (rp = rplast; rp != NULL; rp = rp->prev)
00773                 if (rp->fp != NULL && (rp->flag & RED_FILE) != 0) {
00774                         rp->flag |= RED_USED;
00775                         errno = 0;
00776                         if (/* do_lint && */ fclose(rp->fp) != 0)
00777                                 warning(_("close of `%s' failed (%s)."),
00778                                         rp->value, strerror(errno));
00779                         rp->fp = NULL;
00780                         break;
00781                 }
00782         if (rp == NULL)
00783                 /* surely this is the only reason ??? */
00784                 fatal(_("too many pipes or input files open")); 
00785 }

static int close_redir register struct redirect rp,
int  exitwarn,
two_way_close_type  how
[static]
 

Definition at line 852 of file io.c.

References _, BINMODE, CLOSE_ALL, CLOSE_FROM, CLOSE_TO, do_lint, do_traditional, errno, fclose, free(), fwrite, gawk_pclose(), iop_close(), lintwarn, redirect::next, NULL, O_BINARY, os_setbinmode(), pclose, red_head, RED_PIPE, RED_PTY, RED_SOCKET, RED_TCP, RED_TWOWAY, RED_WRITE, redirect::status, strerror, update_ERRNO(), and warning().

Referenced by close_io(), and do_close().

00853 {
00854         int status = 0;
00855 
00856         if (rp == NULL)
00857                 return 0;
00858         if (rp->fp == stdout || rp->fp == stderr)
00859                 return 0;
00860 
00861         if (do_lint && (rp->flag & RED_TWOWAY) == 0 && how != CLOSE_ALL)
00862                 lintwarn(_("close: redirection `%s' not opened with `|&', second argument ignored"),
00863                                 rp->value);
00864 
00865         errno = 0;
00866         if ((rp->flag & RED_TWOWAY) != 0) {     /* two-way pipe */
00867                 /* write end: */
00868                 if ((how == CLOSE_ALL || how == CLOSE_TO) && rp->fp != NULL) {
00869 #ifdef HAVE_SOCKETS
00870                         if ((rp->flag & RED_TCP) != 0)
00871                                 (void) shutdown(fileno(rp->fp), SHUT_WR);
00872 #endif /* HAVE_SOCKETS */
00873 
00874                         if ((rp->flag & RED_PTY) != 0) {
00875                                 fwrite("\004\n", sizeof("\004\n") - 1, 1, rp->fp);
00876                                 fflush(rp->fp);
00877                         }
00878                         status = fclose(rp->fp);
00879                         rp->fp = NULL;
00880                 }
00881 
00882                 /* read end: */
00883                 if (how == CLOSE_ALL || how == CLOSE_FROM) {
00884                         if ((rp->flag & RED_SOCKET) != 0 && rp->iop != NULL) {
00885 #ifdef HAVE_SOCKETS
00886                                 if ((rp->flag & RED_TCP) != 0)
00887                                         (void) shutdown(rp->iop->fd, SHUT_RD);
00888 #endif /* HAVE_SOCKETS */
00889                                 (void) iop_close(rp->iop);
00890                         } else
00891                                 status = gawk_pclose(rp);
00892 
00893                         rp->iop = NULL;
00894                 }
00895         } else if ((rp->flag & (RED_PIPE|RED_WRITE)) == (RED_PIPE|RED_WRITE)) { /* write to pipe */
00896                 status = pclose(rp->fp);
00897                 if ((BINMODE & 1) != 0)
00898                         os_setbinmode(fileno(stdin), O_BINARY);
00899 
00900                 rp->fp = NULL;
00901         } else if (rp->fp != NULL) {    /* write to file */
00902                 status = fclose(rp->fp);
00903                 rp->fp = NULL;
00904         } else if (rp->iop != NULL) {   /* read from pipe/file */
00905                 if ((rp->flag & RED_PIPE) != 0)         /* read from pipe */
00906                         status = gawk_pclose(rp);
00907                         /* gawk_pclose sets rp->iop to null */
00908                 else {                                  /* read from file */
00909                         status = iop_close(rp->iop);
00910                         rp->iop = NULL;
00911                 }
00912         }
00913 
00914         /* SVR4 awk checks and warns about status of close */
00915         if (status != 0) {
00916                 char *s = strerror(errno);
00917 
00918                 /*
00919                  * Too many people have complained about this.
00920                  * As of 2.15.6, it is now under lint control.
00921                  */
00922                 if (do_lint) {
00923                         if ((rp->flag & RED_PIPE) != 0)
00924                                 lintwarn(_("failure status (%d) on pipe close of `%s' (%s)"),
00925                                          status, rp->value, s);
00926                         else
00927                                 lintwarn(_("failure status (%d) on file close of `%s' (%s)"),
00928                                          status, rp->value, s);
00929                 }
00930 
00931                 if (! do_traditional) {
00932                         /* set ERRNO too so that program can get at it */
00933                         update_ERRNO();
00934                 }
00935         }
00936 
00937         if (exitwarn) {
00938                 /*
00939                  * Don't use lintwarn() here.  If lint warnings are fatal,
00940                  * doing so prevents us from closing other open redirections.
00941                  *
00942                  * Using multiple full messages instead of string parameters
00943                  * for the types makes message translation easier.
00944                  */
00945                 if ((rp->flag & RED_SOCKET) != 0)
00946                         warning(_("no explicit close of socket `%s' provided"),
00947                                 rp->value);
00948                 else if ((rp->flag & RED_TWOWAY) != 0)
00949                         warning(_("no explicit close of co-process `%s' provided"),
00950                                 rp->value);
00951                 else if ((rp->flag & RED_PIPE) != 0)
00952                         warning(_("no explicit close of pipe `%s' provided"),
00953                                 rp->value);
00954                 else
00955                         warning(_("no explicit close of file `%s' provided"),
00956                                 rp->value);
00957         }
00958 
00959         /* remove it from the list if closing both or both ends have been closed */
00960         if (how == CLOSE_ALL || (rp->iop == NULL && rp->fp == NULL)) {
00961                 if (rp->next != NULL)
00962                         rp->next->prev = rp->prev;
00963                 if (rp->prev != NULL)
00964                         rp->prev->next = rp->next;
00965                 else
00966                         red_head = rp->next;
00967                 free(rp->value);
00968                 free((char *) rp);
00969         }
00970 
00971         return status;
00972 }

int devopen const char *  name,
const char *  mode
 

Definition at line 1227 of file io.c.

References _, do_traditional, fatal, INVALID_HANDLE, NULL, O_ACCMODE, O_RDONLY, O_WRONLY, open, os_close_on_exec(), os_devopen(), os_isdir(), str2mode(), STREQ, and STREQN.

Referenced by do_pathopen(), init_fds(), redirect(), specfdopen(), and two_way_open().

01228 {
01229         int openfd;
01230         char *cp;
01231         char *ptr;
01232         int flag = 0;
01233         extern double strtod();
01234 
01235         flag = str2mode(mode);
01236 
01237         if (STREQ(name, "-"))
01238                 return fileno(stdin);
01239 
01240         openfd = INVALID_HANDLE;
01241 
01242         if (do_traditional)
01243                 goto strictopen;
01244 
01245         if ((openfd = os_devopen(name, flag)) != INVALID_HANDLE) {
01246                 os_close_on_exec(openfd, name, "file", "");
01247                 return openfd;
01248         }
01249 
01250         if (STREQN(name, "/dev/", 5)) {
01251                 cp = (char *) name + 5;
01252 
01253                 if (STREQ(cp, "stdin") && (flag & O_ACCMODE) == O_RDONLY)
01254                         openfd = fileno(stdin);
01255                 else if (STREQ(cp, "stdout") && (flag & O_ACCMODE) == O_WRONLY)
01256                         openfd = fileno(stdout);
01257                 else if (STREQ(cp, "stderr") && (flag & O_ACCMODE) == O_WRONLY)
01258                         openfd = fileno(stderr);
01259                 else if (STREQN(cp, "fd/", 3)) {
01260                         cp += 3;
01261                         openfd = (int) strtod(cp, &ptr);
01262                         if (openfd <= INVALID_HANDLE || ptr == cp)
01263                                 openfd = INVALID_HANDLE;
01264                 }
01265                 /* do not set close-on-exec for inherited fd's */
01266                 if (openfd != INVALID_HANDLE)
01267                         return openfd;
01268         } else if (STREQN(name, "/inet/", 6)) {
01269 #ifdef HAVE_SOCKETS
01270                 /* /inet/protocol/localport/hostname/remoteport */
01271                 enum inet_prot protocol = INET_NONE;
01272                 int localport, remoteport;
01273                 char *hostname;
01274                 char *hostnameslastcharp;
01275                 char *localpname;
01276                 char proto[4];
01277                 struct servent *service;
01278 
01279                 cp = (char *) name + 6;
01280                 /* which protocol? */
01281                 if (STREQN(cp, "tcp/", 4))
01282                         protocol = INET_TCP;
01283                 else if (STREQN(cp, "udp/", 4))
01284                         protocol = INET_UDP;
01285                 else if (STREQN(cp, "raw/", 4))
01286                         protocol = INET_RAW;
01287                 else
01288                         fatal(_("no (known) protocol supplied in special filename `%s'"),
01289                                 name);
01290 
01291                 proto[0] = cp[0];
01292                 proto[1] = cp[1];   
01293                 proto[2] = cp[2];   
01294                 proto[3] =  '\0';
01295                 cp += 4;
01296 
01297                 /* which localport? */
01298                 localpname = cp;
01299                 while (*cp != '/' && *cp != '\0')
01300                         cp++;
01301                 /*                    
01302                  * Require a port, let them explicitly put 0 if
01303                  * they don't care.  
01304                  */
01305                 if (*cp != '/' || cp == localpname)
01306                         fatal(_("special file name `%s' is incomplete"), name);
01307                 /* We change the special file name temporarily because we
01308                  * need a 0-terminated string here for conversion with atoi().
01309                  * By using atoi() the use of decimal numbers is enforced.
01310                  */
01311                 *cp = '\0';
01312 
01313                 localport = atoi(localpname);
01314                 if (strcmp(localpname, "0") != 0
01315                     && (localport <= 0 || localport > 65535)) {
01316                         service = getservbyname(localpname, proto);
01317                         if (service == NULL)
01318                                 fatal(_("local port invalid in `%s'"), name);
01319                         else
01320                                 localport = ntohs(service->s_port);
01321                 }
01322                 *cp = '/';
01323 
01324                 /* which hostname? */
01325                 cp++;
01326                 hostname = cp;
01327                 while (*cp != '/' && *cp != '\0')
01328                         cp++; 
01329                 if (*cp != '/' || cp == hostname)
01330                         fatal(_("must supply a remote hostname to `/inet'"));
01331                 *cp = '\0';
01332                 hostnameslastcharp = cp;
01333 
01334                 /* which remoteport? */
01335                 cp++;
01336                 /*
01337                  * The remote port ends the special file name.
01338                  * This means there already is a 0 at the end of the string.
01339                  * Therefore no need to patch any string ending.
01340                  *
01341                  * Here too, require a port, let them explicitly put 0 if
01342                  * they don't care.
01343                  */
01344                 if (*cp == '\0')
01345                         fatal(_("must supply a remote port to `/inet'"));
01346                 remoteport = atoi(cp);
01347                 if (strcmp(cp, "0") != 0
01348                     && (remoteport <= 0 || remoteport > 65535)) {
01349                         service = getservbyname(cp, proto);
01350                         if (service == NULL)
01351                                  fatal(_("remote port invalid in `%s'"), name);
01352                         else
01353                                 remoteport = ntohs(service->s_port);
01354                 }
01355 
01356                 /* Open Sesame! */
01357                 openfd = socketopen(protocol, localport, remoteport, hostname);
01358                 *hostnameslastcharp = '/';
01359 
01360 #else /* ! HAVE_SOCKETS */
01361                 fatal(_("TCP/IP communications are not supported"));
01362 #endif /* HAVE_SOCKETS */
01363         }
01364 
01365 strictopen:
01366         if (openfd == INVALID_HANDLE)
01367                 openfd = open(name, flag, 0666);
01368         if (openfd != INVALID_HANDLE) {
01369                 if (os_isdir(openfd))
01370                         fatal(_("file `%s' is a directory"), name);
01371 
01372                 os_close_on_exec(openfd, name, "file", "");
01373         }
01374         return openfd;
01375 }

NODE* do_close NODE tree  ) 
 

Definition at line 790 of file io.c.

References _, AWKNUM, CLOSE_ALL, CLOSE_FROM, close_redir(), CLOSE_TO, do_lint, do_posix, ERRNO_node, FALSE, fatal, force_string, free_temp, lintwarn, make_string, redirect::next, NULL, red_head, strcasecmp, STREQN, tmp_number, tree_eval, unref(), and redirect::value.

Referenced by snode().

00791 {
00792         NODE *tmp, *tmp2;
00793         register struct redirect *rp;
00794         two_way_close_type how = CLOSE_ALL;     /* default */
00795 
00796         tmp = force_string(tree_eval(tree->lnode));     /* 1st arg: redir to close */
00797 
00798         if (tree->rnode != NULL) {
00799                 /* 2nd arg if present: "to" or "from" for two-way pipe */
00800                 /* DO NOT use _() on the strings here! */
00801                 tmp2 = force_string(tree->rnode->lnode);
00802                 if (strcasecmp(tmp2->stptr, "to") == 0)
00803                         how = CLOSE_TO;
00804                 else if (strcasecmp(tmp2->stptr, "from") == 0)
00805                         how = CLOSE_FROM;
00806                 else
00807                         fatal(_("close: second argument must be `to' or `from'"));
00808                 free_temp(tmp2);
00809         }
00810 
00811         for (rp = red_head; rp != NULL; rp = rp->next) {
00812                 if (strlen(rp->value) == tmp->stlen
00813                     && STREQN(rp->value, tmp->stptr, tmp->stlen))
00814                         break;
00815         }
00816 
00817         if (rp == NULL) {       /* no match, return -1 */
00818                 char *cp;
00819 
00820                 if (do_lint)
00821                         lintwarn(_("close: `%.*s' is not an open file, pipe or co-process"),
00822                                 tmp->stlen, tmp->stptr);
00823 
00824                 /* update ERRNO manually, using errno = ENOENT is a stretch. */
00825                 cp = _("close of redirection that was never opened");
00826                 unref(ERRNO_node->var_value);
00827                 ERRNO_node->var_value = make_string(cp, strlen(cp));
00828 
00829                 free_temp(tmp);
00830                 return tmp_number((AWKNUM) -1.0);
00831         }
00832         free_temp(tmp);
00833         fflush(stdout); /* synchronize regular output */
00834         tmp = tmp_number((AWKNUM) close_redir(rp, FALSE, how));
00835         rp = NULL;
00836         /*
00837          * POSIX says close() returns 0 on success, non-zero otherwise.
00838          * For POSIX, at this point we just return 0.  Otherwise we
00839          * return the exit status of the process or of pclose(), depending.
00840          * This whole business is a mess.
00841          */
00842         if (do_posix) {
00843                 free_temp(tmp);
00844                 return tmp_number((AWKNUM) 0);
00845         }
00846         return tmp;
00847 }

NODE* do_getline NODE tree  ) 
 

Definition at line 2220 of file io.c.

References AWKNUM, do_traditional, FALSE, redirect::flag, FNR, get_a_record(), get_lhs, redirect::iop, iop_close(), make_string, MAYBE_NUM, nextfile(), NR, NULL, RED_EOF, RED_PIPE, RED_TWOWAY, redirect(), set_record(), tmp_number, unref(), and update_ERRNO().

Referenced by r_tree_eval().

02221 {
02222         struct redirect *rp = NULL;
02223         IOBUF *iop;
02224         int cnt = EOF;
02225         char *s = NULL;
02226         int errcode;
02227 
02228         while (cnt == EOF) {
02229                 if (tree->rnode == NULL) {       /* no redirection */
02230                         iop = nextfile(FALSE);
02231                         if (iop == NULL)                /* end of input */
02232                                 return tmp_number((AWKNUM) 0.0);
02233                 } else {
02234                         int redir_error = 0;
02235 
02236                         rp = redirect(tree->rnode, &redir_error);
02237                         if (rp == NULL && redir_error) { /* failed redirect */
02238                                 if (! do_traditional)
02239                                         update_ERRNO();
02240 
02241                                 return tmp_number((AWKNUM) -1.0);
02242                         }
02243                         iop = rp->iop;
02244                         if (iop == NULL)                /* end of input */
02245                                 return tmp_number((AWKNUM) 0.0);
02246                 }
02247                 errcode = 0;
02248                 cnt = get_a_record(&s, iop, &errcode);
02249                 if (errcode != 0) {
02250                         if (! do_traditional)
02251                                 update_ERRNO();
02252 
02253                         return tmp_number((AWKNUM) -1.0);
02254                 }
02255                 if (cnt == EOF) {
02256                         if (rp != NULL) {
02257                                 /*
02258                                  * Don't do iop_close() here if we are
02259                                  * reading from a pipe; otherwise
02260                                  * gawk_pclose will not be called.
02261                                  */
02262                                 if ((rp->flag & (RED_PIPE|RED_TWOWAY)) == 0) {
02263                                         (void) iop_close(iop);
02264                                         rp->iop = NULL;
02265                                 }
02266                                 rp->flag |= RED_EOF;    /* sticky EOF */
02267                                 return tmp_number((AWKNUM) 0.0);
02268                         } else
02269                                 continue;       /* try another file */
02270                 }
02271                 if (rp == NULL) {
02272                         NR++;
02273                         FNR++;
02274                 }
02275                 if (tree->lnode == NULL)        /* no optional var. */
02276                         set_record(s, cnt);
02277                 else {                  /* assignment to variable */
02278                         Func_ptr after_assign = NULL;
02279                         NODE **lhs;
02280 
02281                         lhs = get_lhs(tree->lnode, &after_assign, FALSE);
02282                         unref(*lhs);
02283                         *lhs = make_string(s, cnt);
02284                         (*lhs)->flags |= MAYBE_NUM;
02285                         /* we may have to regenerate $0 here! */
02286                         if (after_assign != NULL)
02287                                 (*after_assign)();
02288                 }
02289         }
02290         return tmp_number((AWKNUM) 1.0);
02291 }

void do_input  ) 
 

Definition at line 431 of file io.c.

References exiting, expression_value, FALSE, filebuf, inrec(), interpret(), redirect::iop, nextfile(), and NULL.

Referenced by main().

00432 {
00433         IOBUF *iop;
00434         extern int exiting;
00435         int rval1, rval2, rval3;
00436 
00437         (void) setjmp(filebuf); /* for `nextfile' */
00438 
00439         while ((iop = nextfile(FALSE)) != NULL) {
00440                 /*
00441                  * This was:
00442                 if (inrec(iop) == 0)
00443                         while (interpret(expression_value) && inrec(iop) == 0)
00444                                 continue;
00445                  * Now expand it out for ease of debugging.
00446                  */
00447                 rval1 = inrec(iop);
00448                 if (rval1 == 0) {
00449                         for (;;) {
00450                                 rval2 = rval3 = -1;     /* for debugging */
00451                                 rval2 = interpret(expression_value);
00452                                 if (rval2 != 0)
00453                                         rval3 = inrec(iop);
00454                                 if (rval2 == 0 || rval3 != 0)
00455                                         break;
00456                         }
00457                 }
00458                 if (exiting)
00459                         break;
00460         }
00461 }

void do_nextfile  ) 
 

Definition at line 236 of file io.c.

References filebuf, nextfile(), and TRUE.

Referenced by interpret().

00237 {
00238         (void) nextfile(TRUE);
00239         longjmp(filebuf, 1);
00240 }

static int do_pathopen const char *  file  )  [static]
 

Definition at line 2329 of file io.c.

References defpath, devopen(), do_traditional, emalloc, envsep, FALSE, free(), getenv, INVALID_HANDLE, isdirpunct(), ispath(), len, NULL, STREQ, and TRUE.

Referenced by pathopen().

02330 {
02331         static const char *savepath = NULL;
02332         static int first = TRUE;
02333         const char *awkpath;
02334         char *cp, *trypath;
02335         int fd;
02336         int len;
02337 
02338         if (STREQ(file, "-"))
02339                 return (0);
02340 
02341         if (do_traditional)
02342                 return (devopen(file, "r"));
02343 
02344         if (first) {
02345                 first = FALSE;
02346                 if ((awkpath = getenv("AWKPATH")) != NULL && *awkpath)
02347                         savepath = awkpath;     /* used for restarting */
02348                 else
02349                         savepath = defpath;
02350         }
02351         awkpath = savepath;
02352 
02353         /* some kind of path name, no search */
02354         if (ispath(file))
02355                 return (devopen(file, "r"));
02356 
02357         /* no arbitrary limits: */
02358         len = strlen(awkpath) + strlen(file) + 2;
02359         emalloc(trypath, char *, len, "do_pathopen");
02360 
02361         do {
02362                 trypath[0] = '\0';
02363 
02364                 for (cp = trypath; *awkpath && *awkpath != envsep; )
02365                         *cp++ = *awkpath++;
02366 
02367                 if (cp != trypath) {    /* nun-null element in path */
02368                         /* add directory punctuation only if needed */
02369                         if (! isdirpunct(*(cp-1)))
02370                                 *cp++ = '/';
02371                         /* append filename */
02372                         strcpy(cp, file);
02373                 } else
02374                         strcpy(trypath, file);
02375                 if ((fd = devopen(trypath, "r")) > INVALID_HANDLE) {
02376                         free(trypath);
02377                         return (fd);
02378                 }
02379 
02380                 /* no luck, keep going */
02381                 if(*awkpath == envsep && awkpath[1] != '\0')
02382                         awkpath++;      /* skip colon */
02383         } while (*awkpath != '\0');
02384         free(trypath);
02385 
02386         /*
02387          * You might have one of the awk paths defined, WITHOUT the current
02388          * working directory in it. Therefore try to open the file in the
02389          * current directory.
02390          */
02391         return (devopen(file, "r"));
02392 }

int flush_io  ) 
 

Definition at line 977 of file io.c.

References _, errno, redirect::flag, redirect::fp, redirect::next, NULL, red_head, RED_PIPE, RED_TWOWAY, RED_WRITE, redirect::status, strerror, redirect::value, and warning().

Referenced by do_fflush(), do_system(), and redirect().

00978 {
00979         register struct redirect *rp;
00980         int status = 0;
00981 
00982         errno = 0;
00983         if (fflush(stdout)) {
00984                 warning(_("error writing standard output (%s)"), strerror(errno));
00985                 status++;
00986         }
00987         if (fflush(stderr)) {
00988                 warning(_("error writing standard error (%s)"), strerror(errno));
00989                 status++;
00990         }
00991         for (rp = red_head; rp != NULL; rp = rp->next)
00992                 /* flush both files and pipes, what the heck */
00993                 if ((rp->flag & RED_WRITE) && rp->fp != NULL) {
00994                         if (fflush(rp->fp)) {
00995                                 if (rp->flag  & RED_PIPE)
00996                                         warning(_("pipe flush of `%s' failed (%s)."),
00997                                                 rp->value, strerror(errno));
00998                                 else if (rp->flag & RED_TWOWAY)
00999                                         warning(_("co-process flush of pipe to `%s' failed (%s)."),
01000                                                 rp->value, strerror(errno));
01001                                 else
01002                                         warning(_("file flush of `%s' failed (%s)."),
01003                                                 rp->value, strerror(errno));
01004                                 status++;
01005                         }
01006                 }
01007         if (status != 0)
01008                 status = -1;    /* canonicalize it */
01009         return status;
01010 }

static int gawk_pclose struct redirect rp  )  [static]
 

Definition at line 2097 of file io.c.

References redirect::iop, iop_close(), NULL, redirect::pid, redirect::status, and wait_any().

Referenced by close_redir().

02098 {
02099         if (rp->iop != NULL)
02100                 (void) iop_close(rp->iop);
02101         rp->iop = NULL;
02102 
02103         /* process previously found, return stored status */
02104         if (rp->pid == -1)
02105                 return rp->status;
02106         rp->status = wait_any(rp->pid);
02107         rp->pid = -1;
02108         return rp->status;
02109 }

static IOBUF* gawk_popen const char *  cmd,
struct redirect rp
[static]