Beispiel #1
0
/*
 * docmd:
 *	take a passed string as a command line and translate
 * 	it to be executed as a command. This function will be
 *	used by execute-command-line and by all source and
 *	startup files. Lastflag/thisflag is also updated.
 *
 *	format of the command line is:
 *
 *		{# arg} <command-name> {<argument string(s)>}
 *
 * char *cline;		command line to execute
 */
int docmd(char *cline)
{
	int f;		/* default argument flag */
	int n;		/* numeric repeat value */
	fn_t fnc;		/* function to execute */
	int status;		/* return status of function */
	int oldcle;		/* old contents of clexec flag */
	char *oldestr;		/* original exec string */
	char *tkn;	/* next token off of command line */

        tkn = alloca(NSTRING * sizeof(char));
	/* if we are scanning and not executing..go back here */
	if (execlevel)
		return TRUE;

	oldestr = execstr;	/* save last ptr to string to execute */
	execstr = cline;	/* and set this one as current */

	/* first set up the default command values */
	f = FALSE;
	n = 1;
	lastflag = thisflag;
	thisflag = 0;

	if ((status = macarg(tkn)) != TRUE) {	/* and grab the first token */
		execstr = oldestr;
		return status;
	}

	/* process leadin argument */
	if (gettyp(tkn) != TKCMD) {
		f = TRUE;
		strcpy(tkn, getval(tkn));
		n = atoi(tkn);

		/* and now get the command to execute */
		if ((status = macarg(tkn)) != TRUE) {
			execstr = oldestr;
			return status;
		}
	}

	/* and match the token to see if it exists */
	if ((fnc = fncmatch(tkn)) == NULL) {
		mlwrite("(No such Function)");
		execstr = oldestr;
		return FALSE;
	}

	/* save the arguments and go execute the command */
	oldcle = clexec;	/* save old clexec flag */
	clexec = TRUE;		/* in cline execution */
	status = (*fnc) (f, n);	/* call the function */
	cmdstatus = status;	/* save the status */
	clexec = oldcle;	/* restore clexec flag */
	execstr = oldestr;
	return status;
}
Beispiel #2
0
void graph(FILE * fp)
{
   static char buf[BUFLNG], arg[BUFLNG / 2], xtype[16], ytype[16];
   static double xa, ya, xap, yap, xmin, xmax, ymin, ymax;
   static double xs = -NSCALE, ys = -NSCALE;
   int n, c;
   char *s, *p;
   double x, y, lpt, th, dt, lscale, rad;
   int is_grid, old_lbl = 0;
   char xory;

   h *= fct;
   w *= fct;

   for (n = 0; (s = fgets(buf, BUFLNG, fp));) {
      s = getarg(s, arg);
      if (s == NULL || *arg == '#');    /* comment line */
      else if ((!is_t && strcmp(arg, "x") == 0)
               || (is_t && strcmp(arg, "y") == 0)) {
         s = gettyp(s, xtype);
         if (sscanf(s, "%lf %lf %lf", &xmin, &xmax, &xa) != 3)
            xa = xmin;
         if (strncmp(xtype, "log", 3) == 0) {
            xmin = log10(xmin);
            xmax = log10(xmax);
            xa = log10(xa);
            is_xlog = (xtype[3] == '*') ? -1 : 1;
         }
         xfct = xl / (xmax - xmin);
         xap = (xa - xmin) * xfct;
         x00 = -xmin * xfct;
      } else if ((!is_t && strcmp(arg, "y") == 0)
                 || (is_t && strcmp(arg, "x") == 0)) {
         s = gettyp(s, ytype);
         if (sscanf(s, "%lf %lf %lf", &ymin, &ymax, &ya) != 3)
            ya = ymin;
         if (strncmp(ytype, "log", 3) == 0) {
            ymin = log10(ymin);
            ymax = log10(ymax);
            ya = log10(ya);
            is_ylog = (ytype[3] == '*') ? -1 : 1;
         }
         yfct = (yl) ? yl / (ymax - ymin) : 0;
         yap = (ya - ymin) * yfct;
         y00 = -ymin * yfct;
      } else if ((!is_t && strncmp(arg, "xscale", 6) == 0)
                 || (is_t && strncmp(arg, "yscale", 6) == 0)) {
         is_grid = *(arg + 6);
         if (type < 0 || (ya != ymin && ya != ymax)) {
            plot(0.0, yap, 3);
            plot(xl, yap, 2);
         }
         ys = yap - h - MSCALE;
         while ((s = getarg(s, p = arg)) != NULL) {
            if (*p != '"') {
               x = atof((is_number(*p)) ? p : p + 1);
               if (strncmp(xtype, "mel", 3) == 0)
                  x = argapf(x / nz(xmax, xmin),
                             atof(xtype + 3)) * nz(xmax, xmin);
               else if (is_xlog)
                  x = log10(x);
               x = (x - xmin) * xfct;
               lscale = (*p == 's') ? LSCALE / 2 : LSCALE;
               if (*p != '\\' && *p != '@') {
                  plot(x, yap, 3);
                  plot(x, yap + lscale, 2);
                  if (type > 0 && !is_grid && yap == 0) {
                     plot(x, yl, 3);
                     plot(x, yl - lscale, 2);
                  }
               } else if (*p == '\\')
                  ++p;
            }
            if (is_number(*p) || *p++ == '"')
               _symbol(x - sleng(p, h, w) / 2, ys - ysadj(), p, h, w, 0.0);
         }
      } else if ((!is_t && strncmp(arg, "yscale", 6) == 0)
                 || (is_t && strncmp(arg, "xscale", 6) == 0)) {
         is_grid = *(arg + 6);
         if (type < 0 || (xa != xmin && xa != xmax)) {
            plot(xap, 0.0, 3);
            plot(xap, yl, 2);
         }
         while ((s = getarg(s, p = arg)) != NULL) {
            if (*p != '"') {
               y = atof((is_number(*p)) ? p : p + 1);
               if (strncmp(ytype, "mel", 3) == 0)
                  y = argapf(y / nz(ymax, ymin),
                             atof(ytype + 3)) * nz(ymax, ymin);
               else if (is_ylog)
                  y = log10(y);
               y = (y - ymin) * yfct;
               lscale = (*p == 's') ? LSCALE / 2 : LSCALE;
               if (*p != '\\' && *p != '@') {
                  plot(xap, y, 3);
                  plot(xap + lscale, y, 2);
                  if (type > 0 && !is_grid && xap == 0) {
                     plot(xl, y, 3);
                     plot(xl - lscale, y, 2);
                  }
               } else if (*p == '\\')
                  ++p;
            }
            if (is_number(*p) || *p++ == '"') {
               x = xap - sleng(p, h, w) - MSCALE;
               if (x < xs)
                  xs = x;
               _symbol(x, y - h * 0.5, p, h, w, 0.0);
            }
         }
      } else if (strcmp(arg + 1, "grid") == 0) {
         draw_fig0(xbuf, ybuf, n, wf, hf, fct);
         if ((!is_t && (*arg == 'x')) || (is_t && (*arg == 'y'))) {
            ybuf[0] = 0;
            ybuf[1] = yl;
            while ((s = getarg(s, arg)) != NULL) {
               x = atof(arg);
               if (is_xlog)
                  x = log10(x);
               xbuf[0] = xbuf[1]
                   = (x - xmin) * xfct;
               draw_fig0(xbuf, ybuf, 2, wf, hf, fct);
            }
         } else {
            xbuf[0] = 0;
            xbuf[1] = xl;
            while ((s = getarg(s, arg)) != NULL) {
               y = atof(arg);
               if (is_ylog)
                  y = log10(y);
               ybuf[0] = ybuf[1]
                   = (y - ymin) * yfct;
               draw_fig0(xbuf, ybuf, 2, wf, hf, fct);
            }
         }
         n = 0;
      } else if (strcmp(arg + 1, "circle") == 0) {
         xory = *arg;
         s = getarg(s, arg);
         x = xt(atof(arg));
         s = getarg(s, arg);
         y = yt(atof(arg));
         swap(&x, &y);
         x = xfct * x + x00;
         y = yfct * y + y00;
         while ((s = getarg(s, arg)) != NULL) {
            if ((!is_t && xory == 'x') || (is_t && xory == 'y'))
               rad = xt(atof(arg)) * xfct;
            else
               rad = yt(atof(arg)) * yfct;
            pntstyl(ptyp);
            circle(x, y, rad, rad, 0., 360.);
         }
      } else if (strcmp(arg, "circle") == 0) {
         s = getarg(s, arg);
         x = xt(atof(arg));
         s = getarg(s, arg);
         y = yt(atof(arg));
         swap(&x, &y);
         x = xfct * x + x00;
         y = yfct * y + y00;
         while ((s = getarg(s, arg)) != NULL) {
            rad = atof(arg);
            pntstyl(ptyp);
            circle(x, y, rad, rad, 0., 360.);
         }
      } else if (strcmp(arg + 1, "name") == 0) {
         s = getname(s, p = arg + 1);
         if ((!is_t && *arg == 'x') || (is_t && *arg == 'y'))
            _symbol((xl - sleng(s, h, w)) / 2,
                    (*p) ? -atof(p) - h : ys - h - NSCALE, s, h, w, 0.0);
         else
            _symbol((*p) ? -atof(p) : xs - MSCALE,
                    (yl - sleng(s, h, w)) / 2, s, h, w, 90.0);
      } else if (strncmp(arg, "title", 5) == 0 || strncmp(arg, "print", 5) == 0) {
         sscanf(s, "%lf %lf", &x, &y);
         swap(&x, &y);
         if (*arg == 'p') {
            x = xfct * xt(x) + x00;
            y = yfct * yt(y) + y00;
         }
         s = gettxt_fig(s);
         th = getarg(s + strlen(s) + 1, arg) ? atof(arg) : 0;
         if (*(arg + 5)) {
            x -= rx(LADJ * h / 2, h / 2, th);
            y -= ry(LADJ * h / 2, h / 2, th);
         }
         _symbol(x, y, s, h, w, th);
      } else if (strcmp(arg, "eod") == 0 || strcmp(arg, "EOD") == 0) {
         draw_fig0(xbuf, ybuf, n, wf, hf, fct);
         n = 0;
      } else if (strcmp(arg, "pen") == 0) {
         n = flush(xbuf, ybuf, n, wf, hf, fct);
         pen(atoi(s));
      } else if (strcmp(arg, "join") == 0) {
         n = flush(xbuf, ybuf, n, wf, hf, fct);
         join(atoi(s));
      } else if (strcmp(arg, "csize") == 0) {
         if (sscanf(s, "%lf %lf", &h, &w) != 2)
            w = h;
      } else if (strcmp(arg, "hight") == 0) {
         if (sscanf(s, "%lf %lf", &mh, &mw) != 2)
            mw = mh;
      } else if (strcmp(arg, "line") == 0) {
         n = flush(xbuf, ybuf, n, wf, hf, fct);
         if (sscanf(s, "%d %lf", &ltype, &lpt) != 2) {
            if (ltype > 0)
               lpt = lpit[ltype - 1];
         }
         if (--ltype >= 0)
            mode(lmod[ltype], lpt);
      } else if (strcmp(arg, "italic") == 0)
         italic(atof(s));
      else if (strcmp(arg, "mark") == 0) {
         while (*s == ' ' || *s == '\t')
            ++s;
         if (*s == '\\' && *(s + 1) == '0')
            *label = '\0';
         else
            strcpy(label, s);
      } else if (strcmp(arg, "paint") == 0) {
         sscanf(s, "%d %lf %lf", &ptyp, &dhat, &that);
      } else if (strcmp(arg, "clip") == 0) {
         draw_fig0(xbuf, ybuf, n, wf, hf, fct);
         for (n = 0; (s = getarg(s, arg)) != NULL; ++n) {
            x = xt(atof(arg));
            if ((s = getarg(s, arg)) == NULL)
               break;
            y = yt(atof(arg));
            swap(&x, &y);
            xbuf[n] = xfct * x + x00;
            ybuf[n] = yfct * y + y00;
         }
         if (n == 0) {
            xclip0 = yclip0 = 0;
            xclip1 = xl;
            yclip1 = yl;
            swap(&xclip1, &yclip1);
         } else if (n == 2) {
            xclip0 = xbuf[0];
            yclip0 = ybuf[0];
            xclip1 = xbuf[1];
            yclip1 = ybuf[1];
         }
         n = 0;
      } else if (strcmp(arg, "box") == 0) {
         draw_fig0(xbuf, ybuf, n, wf, hf, fct);
         for (n = 0; (s = getarg(s, arg)) != NULL; ++n) {
            x = xt(atof(arg));
            if ((s = getarg(s, arg)) == NULL)
               break;
            y = yt(atof(arg));
            swap(&x, &y);
            xbuf[n] = xfct * x + x00;
            ybuf[n] = yfct * y + y00;
         }
         if (n == 2) {
            xbuf[2] = xbuf[1];
            ybuf[3] = ybuf[2] = ybuf[1];
            ybuf[1] = ybuf[0];
            xbuf[3] = xbuf[0];
            n = 4;
         }
         polyg(xbuf, ybuf, n, wf, hf, fct);
         n = 0;
      } else {
         x = xt(atof(arg));
         s = getarg(s, arg);
         y = yt(atof(arg));
         swap(&x, &y);
         xbuf[n] = x = xfct * x + x00;
         ybuf[n] = y = yfct * y + y00;
         if (is_in(x, y) && ((s = getarg(s, arg))
                             || *label || old_lbl > 0)) {
            c = 0;
            if (s || *label) {
               if (s == NULL)
                  s = getarg(label, arg);
               if (*arg == '\\' && (abs(c = atoi(arg + 1))) < 16)
                  mark(abs(c), &x, &y, 1, mh, 1);
               else if (abs(c) == 16) {
                  pntstyl(ptyp);
                  circle(x, y, mh / 2, mh / 2, 0., 360.);
               } else {
                  if (c) {
                     *arg = c;
                     *(arg + 1) = '\0';
                  }
                  _symbol(x - LADJ * h / 2, y - w / 2, arg, h, w, atof(s));
               }
            }
            if (c > 0)
               n = flush(xbuf, ybuf, n, wf, hf, fct);
            if ((c > 0 || old_lbl > 0) && n) {
               dt = atan2(y - ybuf[0], x - xbuf[0]);
               if (old_lbl > 0) {
                  xbuf[0] += MADJ * mh * cos(dt);
                  ybuf[0] += MADJ * mh * sin(dt);
               }
               if (c > 0) {
                  xbuf[1] -= MADJ * mh * cos(dt);
                  ybuf[1] -= MADJ * mh * sin(dt);
               }
               draw_fig0(xbuf, ybuf, 2, wf, hf, fct);
               xbuf[0] = x;
               ybuf[0] = y;
               n = 0;
            }
            old_lbl = c;
         }
         if (++n >= BUFLNG)
            n = flush(xbuf, ybuf, n, wf, hf, fct);
      }
   }
   draw_fig0(xbuf, ybuf, n, wf, hf, fct);
}
Beispiel #3
0
/*
 * find the value of a token
 *
 * char *token;		token to evaluate
 */
char *getval(char *token)
{
	int status;	/* error return */
	struct buffer *bp;	/* temp buffer pointer */
	int blen;	/* length of buffer argument */
	int distmp;	/* temporary discmd flag */
	static char buf[NSTRING];	/* string buffer for some returns */

	switch (gettyp(token)) {
	case TKNUL:
		return "";

	case TKARG:		/* interactive argument */
		strcpy(token, getval(&token[1]));
		distmp = discmd;	/* echo it always! */
		discmd = TRUE;
		status = getstring(token, buf, NSTRING, ctoec('\n'));
		discmd = distmp;
		if (status == ABORT)
			return errorm;
		return buf;

	case TKBUF:		/* buffer contents fetch */

		/* grab the right buffer */
		strcpy(token, getval(&token[1]));
		bp = bfind(token, FALSE, 0);
		if (bp == NULL)
			return errorm;

		/* if the buffer is displayed, get the window
		   vars instead of the buffer vars */
		if (bp->b_nwnd > 0) {
			curbp->b_dotp = curwp->w_dotp;
			curbp->b_doto = curwp->w_doto;
		}

		/* make sure we are not at the end */
		if (bp->b_linep == bp->b_dotp)
			return errorm;

		/* grab the line as an argument */
		blen = bp->b_dotp->l_used - bp->b_doto;
		if (blen > NSTRING)
			blen = NSTRING;
		strncpy(buf, bp->b_dotp->l_text + bp->b_doto, blen);
		buf[blen] = 0;

		/* and step the buffer's line ptr ahead a line */
		bp->b_dotp = bp->b_dotp->l_fp;
		bp->b_doto = 0;

		/* if displayed buffer, reset window ptr vars */
		if (bp->b_nwnd > 0) {
			curwp->w_dotp = curbp->b_dotp;
			curwp->w_doto = 0;
			curwp->w_flag |= WFMOVE;
		}

		/* and return the spoils */
		return buf;

	case TKVAR:
		return gtusr(token + 1);
	case TKENV:
		return gtenv(token + 1);
	case TKFUN:
		return gtfun(token + 1);
	case TKDIR:
		return errorm;
	case TKLBL:
		return errorm;
	case TKLIT:
		return token;
	case TKSTR:
		return token + 1;
	case TKCMD:
		return token;
	}
	return errorm;
}