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 >
Text File  |  1991-02-06  |  40KB  |  1,484 lines

  1. /* GNUPLOT - command.c */
  2. /*
  3.  * Copyright (C) 1986, 1987, 1990   Thomas Williams, Colin Kelley
  4.  *
  5.  * Permission to use, copy, and distribute this software and its
  6.  * documentation for any purpose with or without fee is hereby granted, 
  7.  * provided that the above copyright notice appear in all copies and 
  8.  * that both that copyright notice and this permission notice appear 
  9.  * in supporting documentation.
  10.  *
  11.  * Permission to modify the software is granted, but not the right to
  12.  * distribute the modified code.  Modifications are to be distributed 
  13.  * as patches to released version.
  14.  *  
  15.  * This software  is provided "as is" without express or implied warranty.
  16.  * 
  17.  *
  18.  * AUTHORS
  19.  * 
  20.  *   Original Software:
  21.  *     Thomas Williams,  Colin Kelley.
  22.  * 
  23.  *   Gnuplot 2.0 additions:
  24.  *       Russell Lang, Dave Kotz, John Campbell.
  25.  * 
  26.  * send your comments or suggestions to (pixar!info-gnuplot@sun.com).
  27.  * 
  28.  */
  29.  
  30. #include <stdio.h>
  31. #include <math.h>
  32.  
  33. #ifdef MSDOS
  34. #include <process.h>
  35.  
  36. #ifdef __ZTC__
  37. #define P_WAIT 0
  38. #include <time.h>    /* usleep() */
  39. #else
  40.  
  41. #ifdef __TURBOC__
  42. #include <dos.h>    /* sleep() */
  43. #include <conio.h>
  44. extern unsigned _stklen = 8192;    /* increase stack size */
  45.  
  46. #else    /* must be MSC */
  47. #include <time.h>    /* kludge to provide sleep() */
  48. void sleep();        /* defined later */
  49. #endif /* TURBOC */
  50. #endif /* ZTC */
  51.  
  52. #endif /* MSDOS */
  53.  
  54.  
  55. #include "plot.h"
  56. #include "setshow.h"
  57. #include "help.h"
  58.  
  59. #ifndef STDOUT
  60. #define STDOUT 1
  61. #endif
  62.  
  63. #ifndef HELPFILE
  64. #define HELPFILE "docs/gnuplot.gih" /* changed by makefile */
  65. #endif
  66.  
  67. #define inrange(z,min,max) ((min<max) ? ((z>=min)&&(z<=max)) : ((z>=max)&&(z<=min)) )
  68.  
  69. /*
  70.  * instead of <strings.h>
  71.  */
  72.  
  73. extern char *gets(),*getenv();
  74. extern char *strcpy(),*strncpy(),*strcat();
  75. extern int strlen(), strcmp();
  76.  
  77. #ifdef unix
  78. #ifdef GETCWD
  79. extern char *getcwd();    /* some Unix's use getcwd */
  80. #else
  81. extern char *getwd();    /* most Unix's use getwd */
  82. #endif
  83. #else
  84. extern char *getcwd();    /* Turbo C, MSC and VMS use getcwd */
  85. #endif
  86. extern int chdir();
  87.  
  88. extern double magnitude(),angle(),real(),imag();
  89. extern struct value *const_express(), *pop(), *complex();
  90. extern struct at_type *temp_at(), *perm_at();
  91. extern struct udft_entry *add_udf();
  92. extern struct udvt_entry *add_udv();
  93. extern void squash_spaces();
  94. extern void lower_case();
  95.  
  96. extern BOOLEAN interactive;    /* from plot.c */
  97.  
  98. /* input data, parsing variables */
  99. struct lexical_unit token[MAX_TOKENS];
  100. char input_line[MAX_LINE_LEN+1] = "";
  101. int num_tokens, c_token;
  102. int inline_num = 0;            /* input line number */
  103.  
  104. char c_dummy_var[MAX_ID_LEN+1];         /* current dummy var */
  105.  
  106. /* the curves of the plot */
  107. struct curve_points *first_plot = NULL;
  108. static struct udft_entry plot_func;
  109. struct udft_entry *dummy_func;
  110.  
  111. /* support for replot command */
  112. char replot_line[MAX_LINE_LEN+1] = "";
  113. static int plot_token;                    /* start of 'plot' command */
  114.  
  115. com_line()
  116. {
  117.     read_line(PROMPT);
  118.  
  119.     /* So we can flag any new output: if false at time of error, */
  120.     /* we reprint the command line before printing caret. */
  121.     /* TRUE for interactive terminals, since the command line is typed. */
  122.     /* FALSE for non-terminal stdin, so command line is printed anyway. */
  123.     /* (DFK 11/89) */
  124.     screen_ok = interactive; 
  125.  
  126.     do_line();
  127. }
  128.  
  129.  
  130. do_line()      /* also used in load_file */
  131. {
  132.     if (is_system(input_line[0])) {
  133.         do_system();
  134.         (void) fputs("!\n",stderr);
  135.         return;
  136.     }
  137.     num_tokens = scanner(input_line);
  138.     c_token = 0;
  139.     while(c_token < num_tokens) {
  140.         command();
  141.         if (c_token < num_tokens)    /* something after command */
  142.             if (equals(c_token,";"))
  143.                 c_token++;
  144.             else
  145.                     int_error("';' expected",c_token);
  146.     }
  147. }
  148.  
  149.  
  150.  
  151. command()
  152. {
  153.     char sv_file[MAX_LINE_LEN+1];
  154.     /* string holding name of save or load file */
  155.  
  156.     c_dummy_var[0] = '\0';        /* no dummy variable */
  157.  
  158.     if (is_definition(c_token))
  159.         define();
  160.     else if (equals(c_token,"help") || equals(c_token,"?")) {
  161.         c_token++;
  162.         do_help();
  163.     }
  164.     else if (almost_equals(c_token,"test")) {
  165.         c_token++;
  166.         test_term();
  167.     }
  168.     else if (almost_equals(c_token,"pa$use")) {
  169.         struct value a;
  170.         int stime, text=0;
  171.         char buf[MAX_LINE_LEN+1];
  172.  
  173.         c_token++;
  174.         stime = (int )real(const_express(&a));
  175.         if (!(END_OF_COMMAND)) {
  176.             if (!isstring(c_token))
  177.                 int_error("expecting string",c_token);
  178.             else {
  179.                 quotel_str(buf,c_token);
  180.                 (void) fprintf (stderr, "%s",buf);
  181.                 text = 1;
  182.             }
  183.         }
  184.         if (stime < 0) (void) fgets (buf,MAX_LINE_LEN,stdin);  
  185.                         /* Hold until CR hit. */
  186. #ifdef __ZTC__
  187.         if (stime > 0) usleep((unsigned long) stime);
  188. #else
  189.         if (stime > 0) sleep((unsigned int) stime);
  190. #endif
  191.         if (text != 0 && stime >= 0) (void) fprintf (stderr,"\n");
  192.         c_token++;
  193.         screen_ok = FALSE;
  194.     }
  195.     else if (almost_equals(c_token,"pr$int")) {
  196.         struct value a;
  197.  
  198.         c_token++;
  199.         (void) const_express(&a);
  200.         (void) putc('\t',stderr);
  201.         disp_value(stderr,&a);
  202.         (void) putc('\n',stderr);
  203.         screen_ok = FALSE;
  204.     }
  205.     else if (almost_equals(c_token,"p$lot")) {
  206.         plot_token = c_token++;
  207.         plotrequest();
  208.     }
  209.     else if (almost_equals(c_token,"rep$lot")) {
  210.         if (replot_line[0] == '\0') 
  211.             int_error("no previous plot",c_token);
  212.         c_token++;
  213.         replotrequest();
  214.     }
  215.     else if (almost_equals(c_token,"se$t"))
  216.         set_command();
  217.     else if (almost_equals(c_token,"sh$ow"))
  218.         show_command();
  219.     else if (almost_equals(c_token,"cl$ear")) {
  220.         if (!term_init) {
  221.             (*term_tbl[term].init)();
  222.             term_init = TRUE;
  223.         }
  224.         (*term_tbl[term].graphics)();
  225.         (*term_tbl[term].text)();
  226.         (void) fflush(outfile);
  227.         screen_ok = FALSE;
  228.         c_token++;
  229.     }
  230.     else if (almost_equals(c_token,"she$ll")) {
  231.         do_shell();
  232.         screen_ok = FALSE;
  233.         c_token++;
  234.     }
  235.     else if (almost_equals(c_token,"sa$ve")) {
  236.         if (almost_equals(++c_token,"f$unctions")) {
  237.             if (!isstring(++c_token))
  238.                 int_error("expecting filename",c_token);
  239.             else {
  240.                 quote_str(sv_file,c_token);
  241.                 save_functions(fopen(sv_file,"w"));
  242.             }
  243.         }
  244.         else if (almost_equals(c_token,"v$ariables")) {
  245.             if (!isstring(++c_token))
  246.                 int_error("expecting filename",c_token);
  247.             else {
  248.                 quote_str(sv_file,c_token);
  249.                 save_variables(fopen(sv_file,"w"));
  250.             }
  251.         }
  252.         else if (almost_equals(c_token,"s$et")) {
  253.             if (!isstring(++c_token))
  254.                 int_error("expecting filename",c_token);
  255.             else {
  256.                 quote_str(sv_file,c_token);
  257.                 save_set(fopen(sv_file,"w"));
  258.             }
  259.         }
  260.         else if (isstring(c_token)) {
  261.             quote_str(sv_file,c_token);
  262.             save_all(fopen(sv_file,"w"));
  263.         }
  264.         else {
  265.             int_error(
  266.         "filename or keyword 'functions', 'variables', or 'set' expected",
  267.                     c_token);
  268.         }
  269.         c_token++;
  270.     }
  271.     else if (almost_equals(c_token,"l$oad")) {
  272.         if (!isstring(++c_token))
  273.             int_error("expecting filename",c_token);
  274.         else {
  275.             quote_str(sv_file,c_token);
  276.             load_file(fopen(sv_file,"r"), sv_file);    
  277.         /* input_line[] and token[] now destroyed! */
  278.             c_token = num_tokens = 0;
  279.         }
  280.     }
  281.     else if (almost_equals(c_token,"cd")) {
  282.         if (!isstring(++c_token))
  283.             int_error("expecting directory name",c_token);
  284.         else {
  285.             quotel_str(sv_file,c_token);
  286.             if (chdir(sv_file)) {
  287.               int_error("Can't change to this directory",c_token);
  288.             }
  289.         c_token++;
  290.         }
  291.     }
  292.     else if (almost_equals(c_token,"pwd")) {
  293. #ifdef unix
  294. #ifdef GETCWD
  295.       (void) getcwd(sv_file,MAX_ID_LEN); /* some Unix's use getcwd */
  296. #else
  297.       (void) getwd(sv_file); /* most Unix's use getwd */
  298. #endif
  299. #else
  300. /* Turbo C and VMS have getcwd() */
  301.       (void) getcwd(sv_file,MAX_ID_LEN);
  302. #endif
  303.       fprintf(stderr,"%s\n", sv_file);
  304.       c_token++;
  305.     }
  306.     else if (almost_equals(c_token,"ex$it") ||
  307.             almost_equals(c_token,"q$uit")) {
  308.         done(IO_SUCCESS);
  309.     }
  310.     else if (!equals(c_token,";")) {        /* null statement */
  311.         int_error("invalid command",c_token);
  312.     }
  313. }
  314.  
  315. replotrequest()
  316. {
  317. char str[MAX_LINE_LEN+1];
  318.         if(equals(c_token,"["))
  319.             int_error("cannot set range with replot",c_token);
  320.         if (!END_OF_COMMAND) {
  321.             capture(str,c_token,num_tokens-1);
  322.             if ( (strlen(str) + strlen(replot_line)) <= MAX_LINE_LEN-1) {
  323.                 (void) strcat(replot_line,",");
  324.                 (void) strcat(replot_line,str); 
  325.             }