home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Interactive Guide
/
c-cplusplus-interactive-guide.iso
/
c_ref
/
csource5
/
334_01
/
command.c
< prev
next >
Wrap
Text File
|
1991-02-06
|
40KB
|
1,484 lines
/* GNUPLOT - command.c */
/*
* Copyright (C) 1986, 1987, 1990 Thomas Williams, Colin Kelley
*
* Permission to use, copy, and distribute this software and its
* documentation for any purpose with or without fee is hereby granted,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation.
*
* Permission to modify the software is granted, but not the right to
* distribute the modified code. Modifications are to be distributed
* as patches to released version.
*
* This software is provided "as is" without express or implied warranty.
*
*
* AUTHORS
*
* Original Software:
* Thomas Williams, Colin Kelley.
*
* Gnuplot 2.0 additions:
* Russell Lang, Dave Kotz, John Campbell.
*
* send your comments or suggestions to (pixar!info-gnuplot@sun.com).
*
*/
#include <stdio.h>
#include <math.h>
#ifdef MSDOS
#include <process.h>
#ifdef __ZTC__
#define P_WAIT 0
#include <time.h> /* usleep() */
#else
#ifdef __TURBOC__
#include <dos.h> /* sleep() */
#include <conio.h>
extern unsigned _stklen = 8192; /* increase stack size */
#else /* must be MSC */
#include <time.h> /* kludge to provide sleep() */
void sleep(); /* defined later */
#endif /* TURBOC */
#endif /* ZTC */
#endif /* MSDOS */
#include "plot.h"
#include "setshow.h"
#include "help.h"
#ifndef STDOUT
#define STDOUT 1
#endif
#ifndef HELPFILE
#define HELPFILE "docs/gnuplot.gih" /* changed by makefile */
#endif
#define inrange(z,min,max) ((min<max) ? ((z>=min)&&(z<=max)) : ((z>=max)&&(z<=min)) )
/*
* instead of <strings.h>
*/
extern char *gets(),*getenv();
extern char *strcpy(),*strncpy(),*strcat();
extern int strlen(), strcmp();
#ifdef unix
#ifdef GETCWD
extern char *getcwd(); /* some Unix's use getcwd */
#else
extern char *getwd(); /* most Unix's use getwd */
#endif
#else
extern char *getcwd(); /* Turbo C, MSC and VMS use getcwd */
#endif
extern int chdir();
extern double magnitude(),angle(),real(),imag();
extern struct value *const_express(), *pop(), *complex();
extern struct at_type *temp_at(), *perm_at();
extern struct udft_entry *add_udf();
extern struct udvt_entry *add_udv();
extern void squash_spaces();
extern void lower_case();
extern BOOLEAN interactive; /* from plot.c */
/* input data, parsing variables */
struct lexical_unit token[MAX_TOKENS];
char input_line[MAX_LINE_LEN+1] = "";
int num_tokens, c_token;
int inline_num = 0; /* input line number */
char c_dummy_var[MAX_ID_LEN+1]; /* current dummy var */
/* the curves of the plot */
struct curve_points *first_plot = NULL;
static struct udft_entry plot_func;
struct udft_entry *dummy_func;
/* support for replot command */
char replot_line[MAX_LINE_LEN+1] = "";
static int plot_token; /* start of 'plot' command */
com_line()
{
read_line(PROMPT);
/* So we can flag any new output: if false at time of error, */
/* we reprint the command line before printing caret. */
/* TRUE for interactive terminals, since the command line is typed. */
/* FALSE for non-terminal stdin, so command line is printed anyway. */
/* (DFK 11/89) */
screen_ok = interactive;
do_line();
}
do_line() /* also used in load_file */
{
if (is_system(input_line[0])) {
do_system();
(void) fputs("!\n",stderr);
return;
}
num_tokens = scanner(input_line);
c_token = 0;
while(c_token < num_tokens) {
command();
if (c_token < num_tokens) /* something after command */
if (equals(c_token,";"))
c_token++;
else
int_error("';' expected",c_token);
}
}
command()
{
char sv_file[MAX_LINE_LEN+1];
/* string holding name of save or load file */
c_dummy_var[0] = '\0'; /* no dummy variable */
if (is_definition(c_token))
define();
else if (equals(c_token,"help") || equals(c_token,"?")) {
c_token++;
do_help();
}
else if (almost_equals(c_token,"test")) {
c_token++;
test_term();
}
else if (almost_equals(c_token,"pa$use")) {
struct value a;
int stime, text=0;
char buf[MAX_LINE_LEN+1];
c_token++;
stime = (int )real(const_express(&a));
if (!(END_OF_COMMAND)) {
if (!isstring(c_token))
int_error("expecting string",c_token);
else {
quotel_str(buf,c_token);
(void) fprintf (stderr, "%s",buf);
text = 1;
}
}
if (stime < 0) (void) fgets (buf,MAX_LINE_LEN,stdin);
/* Hold until CR hit. */
#ifdef __ZTC__
if (stime > 0) usleep((unsigned long) stime);
#else
if (stime > 0) sleep((unsigned int) stime);
#endif
if (text != 0 && stime >= 0) (void) fprintf (stderr,"\n");
c_token++;
screen_ok = FALSE;
}
else if (almost_equals(c_token,"pr$int")) {
struct value a;
c_token++;
(void) const_express(&a);
(void) putc('\t',stderr);
disp_value(stderr,&a);
(void) putc('\n',stderr);
screen_ok = FALSE;
}
else if (almost_equals(c_token,"p$lot")) {
plot_token = c_token++;
plotrequest();
}
else if (almost_equals(c_token,"rep$lot")) {
if (replot_line[0] == '\0')
int_error("no previous plot",c_token);
c_token++;
replotrequest();
}
else if (almost_equals(c_token,"se$t"))
set_command();
else if (almost_equals(c_token,"sh$ow"))
show_command();
else if (almost_equals(c_token,"cl$ear")) {
if (!term_init) {
(*term_tbl[term].init)();
term_init = TRUE;
}
(*term_tbl[term].graphics)();
(*term_tbl[term].text)();
(void) fflush(outfile);
screen_ok = FALSE;
c_token++;
}
else if (almost_equals(c_token,"she$ll")) {
do_shell();
screen_ok = FALSE;
c_token++;
}
else if (almost_equals(c_token,"sa$ve")) {
if (almost_equals(++c_token,"f$unctions")) {
if (!isstring(++c_token))
int_error("expecting filename",c_token);
else {
quote_str(sv_file,c_token);
save_functions(fopen(sv_file,"w"));
}
}
else if (almost_equals(c_token,"v$ariables")) {
if (!isstring(++c_token))
int_error("expecting filename",c_token);
else {
quote_str(sv_file,c_token);
save_variables(fopen(sv_file,"w"));
}
}
else if (almost_equals(c_token,"s$et")) {
if (!isstring(++c_token))
int_error("expecting filename",c_token);
else {
quote_str(sv_file,c_token);
save_set(fopen(sv_file,"w"));
}
}
else if (isstring(c_token)) {
quote_str(sv_file,c_token);
save_all(fopen(sv_file,"w"));
}
else {
int_error(
"filename or keyword 'functions', 'variables', or 'set' expected",
c_token);
}
c_token++;
}
else if (almost_equals(c_token,"l$oad")) {
if (!isstring(++c_token))
int_error("expecting filename",c_token);
else {
quote_str(sv_file,c_token);
load_file(fopen(sv_file,"r"), sv_file);
/* input_line[] and token[] now destroyed! */
c_token = num_tokens = 0;
}
}
else if (almost_equals(c_token,"cd")) {
if (!isstring(++c_token))
int_error("expecting directory name",c_token);
else {
quotel_str(sv_file,c_token);
if (chdir(sv_file)) {
int_error("Can't change to this directory",c_token);
}
c_token++;
}
}
else if (almost_equals(c_token,"pwd")) {
#ifdef unix
#ifdef GETCWD
(void) getcwd(sv_file,MAX_ID_LEN); /* some Unix's use getcwd */
#else
(void) getwd(sv_file); /* most Unix's use getwd */
#endif
#else
/* Turbo C and VMS have getcwd() */
(void) getcwd(sv_file,MAX_ID_LEN);
#endif
fprintf(stderr,"%s\n", sv_file);
c_token++;
}
else if (almost_equals(c_token,"ex$it") ||
almost_equals(c_token,"q$uit")) {
done(IO_SUCCESS);
}
else if (!equals(c_token,";")) { /* null statement */
int_error("invalid command",c_token);
}
}
replotrequest()
{
char str[MAX_LINE_LEN+1];
if(equals(c_token,"["))
int_error("cannot set range with replot",c_token);
if (!END_OF_COMMAND) {
capture(str,c_token,num_tokens-1);
if ( (strlen(str) + strlen(replot_line)) <= MAX_LINE_LEN-1) {
(void) strcat(replot_line,",");
(void) strcat(replot_line,str);
}