Ejemplo n.º 1
0
void initgetrec(void)
{
	int i;
	char *p;

	for (i = 1; i < *ARGC; i++) {
		if (!isclvar(p = getargv(i))) {	/* find 1st real filename */
			setsval(lookup("FILENAME", symtab), getargv(i));
			return;
		}
		setclvar(p);	/* a commandline assignment before filename */
		argno++;
	}
	infile = stdin;		/* no filenames, so use stdin */
}
Ejemplo n.º 2
0
getrec(uchar *buf)	/* get next input record from whatever source */
{   /* note: tests whether buf == record */
    int c;
    static int firsttime = 1;

    if (firsttime) {
        firsttime = 0;
        initgetrec();
    }
    dprintf( ("RS=<%s>, FS=<%s>, ARGC=%g, FILENAME=%s\n",
              *RS, *FS, *ARGC, *FILENAME) );
    donefld = 0;
    donerec = 1;
    buf[0] = 0;
    while (argno < *ARGC || infile == stdin) {
        dprintf( ("argno=%d, file=|%s|\n", argno, file) );
        if (infile == NULL) {	/* have to open a new file */
            file = getargv(argno);
            if (*file == '\0') {	/* it's been zapped */
                argno++;
                continue;
            }
            if (isclvar(file)) {	/* a var=value arg */
                setclvar(file);
                argno++;
                continue;
            }
            *FILENAME = file;
            dprintf( ("opening file %s\n", file) );
            if (*file == '-' && *(file+1) == '\0')
                infile = stdin;
            else if ((infile = fopen((char *)file, "r")) == NULL)
                ERROR "can't open file %s", file FATAL;
            setfval(fnrloc, 0.0);
        }
        c = readrec(buf, recsize, infile);
        if (c != 0 || buf[0] != '\0') {	/* normal record */
            if (buf == record) {
                if (!(recloc->tval & DONTFREE))
                    xfree(recloc->sval);
                recloc->sval = record;
                recloc->tval = REC | STR | DONTFREE;
                if (isnumber(recloc->sval)) {
                    recloc->fval = atof(recloc->sval);
                    recloc->tval |= NUM;
                }
            }
            setfval(nrloc, nrloc->fval+1);
            setfval(fnrloc, fnrloc->fval+1);
            return 1;
        }
        /* EOF arrived on this file; set up next */
        if (infile != stdin)
            fclose(infile);
        infile = NULL;
        argno++;
    }
    return 0;	/* true end of file */
}
Ejemplo n.º 3
0
void CmdManager::run(int argc, char** argv)
{
	bool isint = argc <= 1;
	
	for(int i = 1; i<argc; ++i)
	{ 
		std::string a = getargv(i, argc, argv);
		
		if(a == "-i")
			isint = true;
		else if(a == "-e")
			execfile(getargv(++i, argc, argv));
		else if(a == "-p")
			execute(getargv(++i, argc, argv));
	}
	
	interact();
}
Ejemplo n.º 4
0
void
lt_XMLTags::ParseValues(char const *t, GMap<GUTF8String,GUTF8String> &args,bool downcase)
{
  GUTF8String argn;
  char const *tt;
  while((argn=getargn(t,tt)).length())
  {
    if(downcase)
      argn=argn.downcase();
    args[argn]=getargv(tt,t).fromEscaped();
  }
}
Ejemplo n.º 5
0
void initgetrec(void)
{
	int i;
	uchar *p;

	for (i = 1; i < *ARGC; i++) {
		if (!isclvar(p = getargv(i)))	/* find 1st real filename */
			return;
		setclvar(p);	/* a commandline assignment before filename */
		argno++;
	}
	infile = stdin;		/* no filenames, so use stdin */
	/* *FILENAME = file = (uchar*) "-"; */
}
Ejemplo n.º 6
0
struct process *
fillprocess(pid_t pid)
{
	struct process *pr;
	char *argv;
	
	pr = (struct process *) malloc(sizeof(struct process));
	if (!pr) {
		perror("fillprocess(linux):malloc");
		return NULL;
	}
	argv = getargv(pid,1024);
	strlcpy(pr->pargv, argv, 1024); 
	
	/* now get the process size from /proc/%d/status */
	pr->procsize = getpidsize(pid);
	
	return pr;
}
Ejemplo n.º 7
0
void initgetrec(void)
{
	extern unsigned char **start_delayed, **after_delayed;
	unsigned char **pp;
	int i;
	unsigned char *p;

	/* first handle delayed name=val arguments */
	for (pp = start_delayed; pp != after_delayed; pp++)
		setclvar(*pp);
	for (i = 1; i < *ARGC; i++) {
		if (!isclvar(p = getargv(i)))	/* find 1st real filename */
			return;
		setclvar(p);	/* a commandline assignment before filename */
		argno++;
	}
	infile = stdin;		/* no filenames, so use stdin */
	/* *FILENAME = file = (unsigned char*) "-"; */
}
Ejemplo n.º 8
0
void initgetrec(void)
{
	int i;
	char *p;

	for (i = 1; i < *ARGC; i++) {
		p = getargv(i); /* find 1st real filename */
		if (p == NULL || *p == '\0') {  /* deleted or zapped */
			argno++;
			continue;
		}
		if (!isclvar(p)) {
			setsval(lookup("FILENAME", symtab), p);
			return;
		}
		setclvar(p);	/* a commandline assignment before filename */
		argno++;
	}
	infile = stdin;		/* no filenames, so use stdin */
}
Ejemplo n.º 9
0
int getrec(char **pbuf, int *pbufsize, int isrecord)	/* get next input record */
{			/* note: cares whether buf == record */
	int c;
	static int firsttime = 1;
	char *buf = *pbuf;
	int bufsize = *pbufsize;

	if (firsttime) {
		firsttime = 0;
		initgetrec();
	}
	   dprintf( ("RS=<%s>, FS=<%s>, ARGC=%g, FILENAME=%s\n",
		*RS, *FS, *ARGC, *FILENAME) );
	if (isrecord) {
		donefld = 0;
		donerec = 1;
	}
	buf[0] = 0;
	while (argno < *ARGC || infile == stdin) {
		   dprintf( ("argno=%d, file=|%s|\n", argno, file) );
		if (infile == NULL) {	/* have to open a new file */
			file = getargv(argno);
			if (*file == '\0') {	/* it's been zapped */
				argno++;
				continue;
			}
			if (isclvar(file)) {	/* a var=value arg */
				setclvar(file);
				argno++;
				continue;
			}
			*FILENAME = file;
			   dprintf( ("opening file %s\n", file) );
			if (*file == '-' && *(file+1) == '\0')
				infile = stdin;
			else if ((infile = fopen(file, "r")) == NULL)
				FATAL("can't open file %s", file);
			setfval(fnrloc, 0.0);
		}
		c = readrec(&buf, &bufsize, infile);
		if (c != 0 || buf[0] != '\0') {	/* normal record */
			if (isrecord) {
				if (freeable(fldtab[0]))
					xfree(fldtab[0]->sval);
				fldtab[0]->sval = buf;	/* buf == record */
				fldtab[0]->tval = REC | STR | DONTFREE;
				if (is_number(fldtab[0]->sval)) {
					fldtab[0]->fval = atof(fldtab[0]->sval);
					fldtab[0]->tval |= NUM;
				}
			}
			setfval(nrloc, nrloc->fval+1);
			setfval(fnrloc, fnrloc->fval+1);
			*pbuf = buf;
			*pbufsize = bufsize;
			return 1;
		}
		/* EOF arrived on this file; set up next */
		if (infile != stdin)
			fclose(infile);
		infile = NULL;
		argno++;
	}
	*pbuf = buf;
	*pbufsize = bufsize;
	return 0;	/* true end of file */
}
Ejemplo n.º 10
0
Archivo: cat.c Proyecto: alhazred/onarm
int
main(int argc, char **argv)
{
	FILE *fi;
	int c;
	extern	int optind;
	int	errflg = 0;
	int	stdinflg = 0;
	int	status = 0;
	int	estatus = 0;
	struct stat source, target;

	(void) setlocale(LC_ALL, "");
#if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
#define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
#endif
	(void) textdomain(TEXT_DOMAIN);

#ifdef STANDALONE
	/*
	 * If the first argument is NULL,
	 * discard arguments until we find cat.
	 */
	if (argv[0][0] == '\0')
		argc = getargv("cat", &argv, 0);
#endif

	/*
	 * Process the options for cat.
	 */

	while ((c = getopt(argc, argv, "usvtebn")) != EOF) {
		switch (c) {

		case 'u':

			/*
			 * If not standalone, set stdout to
			 * completely unbuffered I/O when
			 * the 'u' option is used.
			 */

#ifndef	STANDALONE
			setbuf(stdout, (char *)NULL);
#endif
			continue;

		case 's':

			/*
			 * The 's' option requests silent mode
			 * where no messages are written.
			 */

			silent++;
			continue;

		case 'v':

			/*
			 * The 'v' option requests that non-printing
			 * characters (with the exception of newlines,
			 * form-feeds, and tabs) be displayed visibly.
			 *
			 * Control characters are printed as "^x".
			 * DEL characters are printed as "^?".
			 * Non-printable  and non-contrlol characters with the
			 * 8th bit set are printed as "M-x".
			 */

			visi_mode++;
			continue;

		case 't':

			/*
			 * When in visi_mode, this option causes tabs
			 * to be displayed as "^I".
			 */

			visi_tab++;
			continue;

		case 'e':

			/*
			 * When in visi_mode, this option causes newlines
			 * and form-feeds to be displayed as "$" at the end
			 * of the line prior to the newline.
			 */

			visi_newline++;
			continue;

		case 'b':

			/*
			 * Precede each line output with its line number,
			 * but omit the line numbers from blank lines.
			 */

			bflg++;
			nflg++;
			continue;

		case 'n':

			/*
			 * Precede each line output with its line number.
			 */

			nflg++;
			continue;

		case '?':
			errflg++;
			break;
		}
		break;
	}

	if (errflg) {
		if (!silent)
			(void) fprintf(stderr,
			    gettext("usage: cat [ -usvtebn ] [-|file] ...\n"));
		exit(2);
	}

	/*
	 * Stat stdout to be sure it is defined.
	 */

	if (fstat(fileno(stdout), &target) < 0) {
		if (!silent)
			(void) fprintf(stderr,
			    gettext("cat: Cannot stat stdout\n"));
		exit(2);
	}
	obsize = target.st_blksize;

	/*
	 * If no arguments given, then use stdin for input.
	 */

	if (optind == argc) {
		argc++;
		stdinflg++;
	}

	/*
	 * Process each remaining argument,
	 * unless there is an error with stdout.
	 */


	for (argv = &argv[optind];
	    optind < argc && !ferror(stdout); optind++, argv++) {

		/*
		 * If the argument was '-' or there were no files
		 * specified, take the input from stdin.
		 */

		if (stdinflg ||
		    ((*argv)[0] == '-' && (*argv)[1] == '\0'))
			fi = stdin;
		else {
			/*
			 * Attempt to open each specified file.
			 */

			if ((fi = fopen(*argv, "r")) == NULL) {
				if (!silent)
				    (void) fprintf(stderr,
				    gettext("cat: cannot open %s: %s\n"),
					    *argv, strerror(errno));
				status = 2;
				continue;
			}
		}

		/*
		 * Stat source to make sure it is defined.
		 */

		if (fstat(fileno(fi), &source) < 0) {
			if (!silent)
				(void) fprintf(stderr,
				    gettext("cat: cannot stat %s: %s\n"),
				    (stdinflg) ? "-" : *argv, strerror(errno));
			status = 2;
			continue;
		}


		/*
		 * If the source is not a character special file, socket or a
		 * block special file, make sure it is not identical
		 * to the target.
		 */

		if (!S_ISCHR(target.st_mode) &&
		    !S_ISBLK(target.st_mode) &&
		    !S_ISSOCK(target.st_mode) &&
		    IDENTICAL(target, source)) {
			if (!silent)
			    (void) fprintf(stderr,
			    gettext("cat: input/output files '%s' identical\n"),
				stdinflg?"-": *argv);
			if (fclose(fi) != 0)
				(void) fprintf(stderr,
				    gettext("cat: close error: %s\n"),
				    strerror(errno));
			status = 2;
			continue;
		}
		ibsize = source.st_blksize;

		/*
		 * If in visible mode and/or nflg, use vncat;
		 * otherwise, use cat.
		 */

		if (visi_mode || nflg)
			estatus = vncat(fi);
		else
			estatus = cat(fi, &source, &target,
			    fi != stdin ? *argv : "standard input");

		if (estatus)
			status = estatus;

		/*
		 * If the input is not stdin, close the source file.
		 */

		if (fi != stdin) {
			if (fclose(fi) != 0)
				if (!silent)
					(void) fprintf(stderr,
					    gettext("cat: close error: %s\n"),
					    strerror(errno));
		}
	}

	/*
	 * Display any error with stdout operations.
	 */

	if (fclose(stdout) != 0) {
		if (!silent)
			perror(gettext("cat: close error"));
		status = 2;
	}
	return (status);
}
Ejemplo n.º 11
0
Archivo: idfio.c Proyecto: jobovy/nemo
nemo_main()
{
  stream istr;
  float fval;
  double dval;
  int i, k, l, n, n2, iw, i1, i2, lineno, ival;
  string *sp, *idf0, *pars;
  char *cp, *cp1, line[MAX_LINELEN];
  bool Qline = getbparam("lineno");
  bool Qshow_idf;
  bool Qtype = getbparam("checktype");
  bool Qreport = getbparam("report");
  int argc, nidf, nidf2, nw, nopen, nrow;
  IDF *idf;
  string *argv, *av, *w;
  cstring *csp, *csn;
  
  if (hasvalue("idf")) {
    n = nemo_file_lines(getparam("idf"),0);
    dprintf(1,"%s : nlines=%d\n",getparam("idf"),n);
    idf0 = (string *) allocate((n+1)*sizeof(string));
    istr = stropen(getparam("idf"),"r");
    for (i=0; i<n; i++) {
      if (fgets(line,MAX_LINELEN,istr) == NULL) error("short file");
      strip_newline(line);
      idf0[i] = strdup(line);
    }
    idf0[n] = 0;
    strclose(istr);
  } else {
    warning("Testing IDF");
    idf0 = testidf;
  }

  /* report, and pre-parse and count the true IDF's */

  dprintf(1,"report IDF\n");
  Qshow_idf = !hasvalue("out");
  nidf = 0;
  for (sp = idf0, lineno=0; *sp; sp++) {
    if (*sp[0] == '#') continue;
    lineno++;
    if (Qshow_idf) {
      if (Qline)
	printf("%d: %s\n",lineno,*sp);	
      else
	printf("%s\n",*sp);	
    }
    w = burststring(*sp," \t");
    nw = xstrlen(w,sizeof(string))-1;
    for (i=0; i<nw; i++) {
      if (*w[i] == '#') break;
      cp = strchr(w[i],':');
      if (cp==NULL) error("Missing : on %s",w[i]);
      nidf++;
    }
  } 
  dprintf(0,"Found %d IDF_parameters in %d lines in idf file\n",nidf,lineno);

  /* now fully parse IDF */

  idf = (IDF *) allocate(nidf*sizeof(IDF));
  nidf2 = 0;
  nopen = 0;
  nrow = 0;
  for (sp = idf0; *sp; sp++) {
    if (*sp[0] == '#') continue;
    nrow++;
    if (nopen) error("Cannot handle any parameters after an open ended array");
    w = burststring(*sp," \t");
    nw = xstrlen(w,sizeof(string))-1;
    for (i=0; i<nw; i++) {
      if (*w[i] == '#') break;
      cp = strchr(w[i],':');
      if (cp==NULL) error("Missing : on %s",w[i]);
      *cp++ = 0;
      /* now w[i] points to type; cp to keyword, possibly with a [] */
      idf[nidf2].type = strdup(w[i]);
      idf[nidf2].key  = strdup(cp);
      idf[nidf2].row  = nrow;
      idf[nidf2].col  = i+1;
      cp1 = strchr(cp,'[');
      if (cp1) {
	*cp1++ = 0;
	idf[nidf2].key  = strdup(cp);
	if (*cp1 == ']') {
	  if (nopen) error("Cannot handle more than one open ended array");
	  idf[nidf2].nvals = 0;
	  nopen++;
	} else
	  error("fixed dimensioned arrays not allowed yet : %s",cp);
      } else {
	idf[nidf2].key   = strdup(cp);
	idf[nidf2].nvals = -1;
      }
      nidf2++;
    }
  }
  if (nidf2 != nidf) error("nidf2=%d != nidf=%d\n",nidf2,nidf);


  /* report the full IDF */

  if (Q) {
      for (i=0; i<nidf; i++) {
      dprintf(1,"###: [%d,%d] %s %s\n", idf[i].row, idf[i].col, idf[i].type, idf[i].key);
    }
  }

  if (hasvalue("par")) {
    pars = line_open_file(getparam("par"));
    n2 = xstrlen(pars,sizeof(string))-1;
    dprintf(0,"Found %d lines in par file\n",n2);
    if (Q)
      if (n2 != lineno) warning("par file not same as idf");

    for (l=0, i=0; l<n2; l++) {    /* loop over all lines :   l counts lines, i counts idf's */
      /*  idf[i] is the current IDF */
      /*  pars[l] is the current line */
      /*  idf[i].row should match the current line */


      /* a comment spans (by definition) the whole line */
      if (i<nidf && streq(idf[i].type,IDF_COMMENT)) {
	  idf[i].out = strdup(pars[l]);
	  idf[i].cout.val = strdup(pars[l]);
	  idf[i].cout.nxt = 0;
	  i++;
	  continue;
      } 
      /* for now any types are not comments, and so treated same way */
      w = burststring(pars[l]," \t");
      nw = xstrlen(w,sizeof(string))-1;

#if 1
      /* just do it, no checking */
      for (iw=0; iw<nw; iw++) {
	if (*w[iw] == '#') break;
	if (i < nidf) {
	  idf[i].out = strdup(w[iw]);
	  idf[i].cout.val = strdup(w[iw]);
	  idf[i].cout.nxt = 0;
	} else {
	  if (nopen==0) error("Too many values, and no open ended array");

	  csn = (cstring *) allocate(sizeof(cstring));
	  csn->val = strdup(w[iw]);
	  csn->nxt = 0;

	  csp = &idf[nidf-1].cout;
	  while (csp->nxt)
	    csp = csp->nxt;
	  csp->nxt = csn;
	}
	i++;
      }
      if (Q) {
	if (i==nidf) {
	  warning("end of idf %d %d",l,n2);
	  if (l<n2-1) warning("not exhausting lines in par file");
	}
      }
#else
      /* messy double checking */
      for (i1=i; i<nidf && idf[i1].row==idf[i].row; i1++) {
	dprintf(0,"i=%d i1=%d row=%d row1=%d\n",i,i1,idf[i].row,idf[i1].row);
      }
      n2 = i1-i;
      if (nw != n2) error("nw=%d != n2=%d i=%d l=%d (%s)",nw,n2,i,l,pars[l]);
      for (i1=i, iw=0; i<nidf && idf[i1].row==idf[i].row; i1++, iw++) {
	idf[i1].out = strdup(w[iw]);
      }
#endif
    }

    if (Q) {
      for (i=0; i<nidf; i++) {
	printf("###: [%d,%d] %-3s %-10s = %s\n", idf[i].row, idf[i].col, idf[i].type, idf[i].key, idf[i].out);
      }
    }
  }

  /* report the command line tail after the -- */

  dprintf(1,"getargv\n");
  argv = getargv(&argc);
  dprintf(1,"  argc=%d\n",argc);
  if (argc>0) {
    argv++; /* skip the -- */
    if (Qtype) warning("Type checking not implemented yet");
    for (av = argv; *av; av++) {
      if (Q) printf("arg: %s\n",*av);
      cp = strchr(*av,'=');
      if (cp) {
	*cp++ = 0;
	for (i=0; i<nidf; i++) {
	  if (streq(*av,idf[i].key)) {
	    dprintf(1,"Patching key=%s with val=%s\n",*av,cp);
	    if (idf[i].nvals < 0) {
	      idf[i].out      = strdup(cp);
	      idf[i].cout.val = strdup(cp);
	      idf[i].cout.nxt = 0;
	    } else {
	      w = burststring(cp," ,");
	      nw = xstrlen(w,sizeof(string))-1;
	      idf[i].out      = strdup(w[0]);
	      idf[i].cout.val = strdup(cp);
	      idf[i].cout.nxt = 0;
	      csn = &idf[i].cout;
	      for (iw=0; iw<nw; iw++) {
		csn->val = strdup(w[iw]);
		if (iw<nw-1) {
  		  csn->nxt = (cstring *) allocate(sizeof(cstring));
		  csn = csn->nxt;
		} else
		  csn->nxt = 0;
	      }
	    }
	    break;
	  }
	}
	if (i==nidf) error("%d: key=%s not registered in IDF",i,*av);
      } else {
	error("cannot handle arugments yet that are not key=val");
      }
    }
  }

  if (Q) {
    for (i=0; i<nidf; i++) {
      printf(">>>: [%d,%d] %-3s %-10s = %s\n", idf[i].row, idf[i].col, idf[i].type, idf[i].key, idf[i].out);
    }
  }
  
  if (hasvalue("out")) {
    istr = stropen(getparam("out"),"w");
    for (i=0; i<nidf; i++) {
      if (i>0 && idf[i].row != idf[i-1].row) 
	fprintf(istr,"\n");
      if (idf[i].col > 1) fprintf(istr," ");
      if (streq(idf[i].type,"qs") && idf[i].out[0] != '\'')
	fprintf(istr,"'%s'",idf[i].out);
      else
	fprintf(istr,"%s",idf[i].out);

    }
    fprintf(istr,"\n");
    if (nopen) {
      csp = idf[nidf-1].cout.nxt;
      while (csp) {
	fprintf(istr,"%s\n",csp->val);	
	csp = csp->nxt;
      }
    }
    strclose(istr);
  }

  if (Qreport) {
    for (i=0; i<nidf; i++) {
      printf("%s=%s\n",idf[i].key,idf[i].out);
    }
  }

}
Ejemplo n.º 12
0
int getrec(unsigned char **buf, int *bufsize)
{
	int c, saved;
	static int firsttime = 1;

	if (firsttime) {
		firsttime = 0;
		initgetrec();
	}
	dprintf( ("RS=<%s>, FS=<%s>, ARGC=%d, FILENAME=%s\n",
		*RS ? *RS : tostring(""),
		*FS ? *FS : tostring(""),
		(int) *ARGC,
		*FILENAME ? *FILENAME : tostring("")) );
	donefld = 0;
	donerec = 1;
	if (*bufsize == 0) {
		if ((*buf = malloc(*bufsize = CHUNK)) == NULL)
			error(MM_ERROR, outofspace, "getrec");
		**buf = '\0';
	}
	saved = (*buf)[0];
	(*buf)[0] = 0;
	while (argno < *ARGC || infile == stdin) {
		dprintf( ("argno=%d, file=|%s|\n", argno, file) )
		;
		if (infile == NULL) {	/* have to open a new file */
			file = getargv(argno);
			if (*file == '\0') {	/* it's been zapped */
				argno++;
				continue;
			}
			if (isclvar(file)) {	/* a var=value arg */
				setclvar(file);
				argno++;
				continue;
			}
			*FILENAME = file;
			dprintf( ("opening file %s\n", file) );
			if (*file == '-' && *(file+1) == '\0')
				infile = stdin;
			else if ((infile = fopen((char *)file, "r")) == NULL)
				error(MM_ERROR, badopen, file, strerror(errno));
			setfval(fnrloc, 0.0);
		}
		c = readrec(buf, bufsize, infile);
		if (c != 0 || (*buf)[0] != '\0') {	/* normal record */
			if (*buf == record) {
				if (!(recloc->tval & DONTFREE))
					xfree(recloc->sval);
				recloc->sval = record;
				recloc->tval = REC | STR | DONTFREE;
				(void)is2number(0, recloc);
			}
			setfval(nrloc, nrloc->fval+1);
			setfval(fnrloc, fnrloc->fval+1);
			return 1;
		}
		/* EOF arrived on this file; set up next */
		if (infile != stdin)
			fclose(infile);
		infile = NULL;
		argno++;
	}
	/*
	* POSIX.2 requires that NF stick with its last value
	* at the start of the END code.  The most straightforward
	* way to do this is to restore the contents of record
	* [==buf when called from program()] so that getnf() will
	* recompute the same NF value unless something strange
	* occurs.  This has the side effect of $0...$NF *also*
	* having sticky values into END, but that seems to match
	* the spirit of POSIX.2's rule for NF.
	*/
	if (posix)
		(*buf)[0] = saved;
	return 0;	/* true end of file */
}