home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 15 / 15.iso / s / s300 / 1.ddi / CHAP2 / MAIN2.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-05-02  |  9.9 KB  |  313 lines

  1. /***********************************************************************
  2.  
  3. FILE
  4.     main2.c  -  d.c. motor speed control example, main program module 2
  5.  
  6. ROUTINES
  7.     main    -  main program
  8.     cmdint  -  command processor
  9.     fstr    -  extract command word
  10.     help    -  print list of commands
  11.  
  12. REMARKS
  13.     This module is similar to main1.c but implements a better command
  14.     interpreter.
  15.  
  16.     To generate the simulation program under Microsoft C version 4.00
  17.     and above, type: cl /DSIMRT main2.c cntrl1.c rtsim1.c
  18.  
  19. LAST UPDATE
  20.     18 January 1988
  21.         minor tidying-up
  22.  
  23.     Copyright (c) 1984-1988  D.M. Auslander and C.H. Tham
  24.  
  25. ***********************************************************************/
  26.  
  27. /***********************************************************************
  28.                             I M P O R T S
  29. ***********************************************************************/
  30.  
  31. #include <stdio.h>
  32.  
  33. extern void exit();         /* this is needed for the command table */
  34.  
  35. #include "envir.h"          /* program environment declarations */
  36. #include "cntrl1.h"         /* extern declarations for cntrl1.c */
  37.  
  38.  
  39. /***********************************************************************
  40.              F O R W A R D    D E C L A R A T I O N S
  41. ***********************************************************************/
  42.  
  43. #ifdef ANSI
  44.  
  45. void cmdint(char *);
  46. char *getcmd(char *, char *);
  47. void menu(void);
  48.  
  49. #else
  50.  
  51. void cmdint();
  52. char getcmd();
  53. void menu();
  54.  
  55. #endif
  56.  
  57.  
  58. /***********************************************************************
  59.     P R I V A T E    D A T A     A N D    D E C L A R A T I O N S
  60. ***********************************************************************/
  61.  
  62. #define PROMPT  "READY "    /* defines the command processor prompt */
  63. #define BUFSIZE 100         /* command input buffer size */
  64.  
  65. /*-----------------------------------------------------------------
  66.     The following declares a data structure for associating a
  67.     command string with the routine that acts on the contents
  68.     of that command string.  This is done using a C construct
  69.     known as a "struct" (record to you Pascal enthusiasts).
  70.  
  71.     The structure has 2 fields:
  72.  
  73.         1) the command name in the form of a NUL terminated string
  74.         2) a pointer to the associated function
  75. -----------------------------------------------------------------*/
  76.  
  77. #define CMDLEN  12      /* max length of command name */
  78.  
  79. struct command {
  80.  
  81.     char cmd[CMDLEN+1]; /* command string (+1 for NUL terminator) */
  82.  
  83.     void (*cmdfunc)();  /* pointer to command function.  Note the  */
  84.                         /*   parentheses in the declaration - they */
  85.                         /*   are very important and must be placed */
  86.                         /*   exactly right! (See K & R, page 209). */
  87. };
  88.  
  89.  
  90. /*----------------------------------------------------------------------
  91.     This is the command interpreter table, an array of commands and
  92.     their associated function pointers.  Note that commands have a
  93.     long and a short form.
  94.  
  95.     The interpreter table is an array of structures.  Each structure
  96.     in the array is initialized with a command name and an associated
  97.     function pointer (function name used without brackets).
  98.  
  99.     The function pointers must be declared ahead so that the compiler
  100.     knows that the names refer to functions and not data variables,
  101.     and assigns the proper function addresses (entry points).
  102. ----------------------------------------------------------------------*/
  103.  
  104. static struct command cmdtable[] = {
  105.  
  106.     {"gains",       gain},          /* specify controller gains */
  107.     {"k",           gain}, 
  108.  
  109.     {"setpoint",    setv},          /* specify setpoint */
  110.     {"s",           setv}, 
  111.  
  112.     {"initialize",  init},          /* initialize */
  113.     {"i",           init}, 
  114.  
  115.     {"sample-time", smpset},        /* specify sample time */
  116.     {"t",           smpset}, 
  117.  
  118.     {"help",        menu},          /* print help menu */
  119.     {"h",           menu},
  120.  
  121.     {"go",          control},       /* start controller */
  122.     {"g",           control}, 
  123.  
  124.     {"exit",        exit},          /* program exit */
  125.     {"e",           exit}, 
  126. };
  127.  
  128.  
  129. static int ncmds;       /* number of entries in command table */
  130.  
  131.  
  132. /***********************************************************************
  133.                     M A I N    P R O G R A M
  134. ***********************************************************************/
  135.  
  136. main()
  137. {
  138.     char cbuf[BUFSIZE];     /* space for the user-typed command line */
  139.  
  140.  
  141.     ncmds = sizeof(cmdtable) / sizeof(struct command);
  142.  
  143.     menu();                 /* print command list to start. */
  144.  
  145.     for(;;)                 /* an endless loop. */
  146.     {
  147.         fputs(PROMPT, stdout);  /* print a prompt so that user will */
  148.                                 /*   know that the program expects  */
  149.                                 /*   an input.                      */
  150.                                     
  151.         fgets(cbuf, BUFSIZE, stdin);    /* read a NULL terminated string */
  152.                                         /*   from the "standard input"   */
  153.                                         /*   (normally the user console) */
  154.  
  155.         cmdint(cbuf);           /* call the routine that interprets  */
  156.                                 /*   the command line.               */
  157.     }
  158.  
  159. }
  160.  
  161.  
  162. /***********************************************************************
  163.                     P R I V A T E    R O U T I N E S 
  164. ***********************************************************************/
  165.  
  166. /*----------------------------------------------------------------------
  167. PROCEDURE
  168.     CMDINT  -  command interpreter
  169.  
  170. SYNOPSIS
  171.     static void cmdint(cline)
  172.     char cline[];
  173.  
  174. PARAMETERS
  175.     cline  -  command line
  176.  
  177. DESCRIPTION
  178.     The command word is isolated and matched against the list of valid
  179.     commands in the command table.  If a match is found, the routine
  180.     associated with that command is executed.
  181.  
  182. LAST UPDATE
  183.     1 December 1987
  184. ----------------------------------------------------------------------*/
  185.  
  186. static void cmdint(cline)
  187. char cline[];
  188. {
  189.     char cword[CMDLEN+1];   /* storage to hold the command word. */
  190.     char *prest;            /* ptr to rest of line after command word */
  191.     struct command *pcom;   /* ptr to the command structure list. */
  192.     int i;                  /* iteration counter */
  193.  
  194.  
  195.     prest = getcmd(cline, cword);        /* isolate the command word */
  196.  
  197.     /*-----------------------------------------------------------------
  198.         Search the command list for a match on the command word.
  199.     -----------------------------------------------------------------*/
  200.  
  201.     pcom = cmdtable;    /* initialize the pointer to the command list */
  202.  
  203.     for (i = 0; i < ncmds; i++)
  204.     {
  205.         /*  note: strcmp() returns zero if strings match  */
  206.  
  207.         if (strcmp(pcom->cmd, cword) == 0)
  208.         {
  209.             /*-----------------------------------------------------
  210.                 Command word matches a name entry in the command
  211.                 interpreter table.  The associated function is
  212.                 executed  --  again, watch the parentheses!
  213.             ------------------------------------------------------*/
  214.  
  215.             (*pcom->cmdfunc)(prest);    /* execute assocaited func */
  216.  
  217.             break;                      /* break out of "for" loop */
  218.         }
  219.  
  220.         pcom++;                         /* not found, next item. */
  221.     }
  222.  
  223.     if (i >= ncmds)     /* did not match any of the valid commands */
  224.     {
  225.         printf("command not found : %s\n", cline);
  226.     }
  227.  
  228. }
  229.  
  230.  
  231.  
  232. /*----------------------------------------------------------------------
  233. FUNCTION
  234.     GETCMD  -  isolate command word from input string
  235.  
  236. SYNOPSIS
  237.     static char *getcmd(line, word)
  238.     char line[], word[];
  239.  
  240. PARAMETERS
  241.     line  -  command line
  242.     word  -  where to put the command word
  243.  
  244. RETURNS
  245.     pointer to rest of line (after the command word)
  246.  
  247. DESCRIPTION
  248.     Command words are assumed to be delimited by blanks.  Thus this
  249.     routine finds the first blank-terminated word in the command line
  250.     and copies it into the 'word' argument.
  251.  
  252.     The command line is a NUL terminated character array which is
  253.     passed to this routine in the form of a NUL terminated character 
  254.     array.  Note that the command line CANNOT begin with a blank.
  255.  
  256.     getcmd() returns NULL if the input command line begins with a blank
  257.     or carriage-return.
  258.  
  259. LAST UPDATE
  260.     18 October 1984
  261. ----------------------------------------------------------------------*/
  262.  
  263. static char *getcmd(line, word)
  264. char line[], word[];
  265. {
  266.     int i;      /* loop index */
  267.  
  268.  
  269.     for (i = 0; i < CMDLEN; i++)
  270.     {
  271.         if ((line[i] == ' ') || (line[i] == '\n') || (line[i] == '\0'))
  272.             break;      /* end of word found, get out of loop */
  273.     
  274.         word[i] = line[i];      /* Copy the character to w. */
  275.     }
  276.  
  277.     word[i] = 0;        /* terminate output word with NUL character */
  278.  
  279.     return(&line[i]);   /* return a pointer to the remainder of line. */
  280.  
  281. }
  282.  
  283.  
  284.  
  285. /*---------------------------------------------------------------------
  286. PROCEDURE
  287.     MENU  -  print a list of commands
  288.  
  289. SYNOPSIS
  290.     static void menu()
  291.  
  292. LAST UPDATE
  293.     22 September 1984
  294. ----------------------------------------------------------------------*/
  295.  
  296. static void menu()
  297. {
  298.  
  299.     fputs("\n                COMMAND LIST\n\n", stdout);
  300.     fputs("full command (or single-letter equivalent) arguments\n\n", stdout);
  301.  
  302.     fputs("initialize    (i)   delta_t\n", stdout);
  303.     fputs("sample-time   (t)   tsamp\n", stdout);
  304.     fputs("gains         (k)   kp ki const\n", stdout);
  305.     fputs("setpoint      (s)   setpoint_value\n", stdout);
  306.     fputs("go            (g)   number_of_iterations print(0/1)\n", stdout);
  307.     fputs("exit          (e) \n", stdout);
  308.     fputs("help          (h) \n\n", stdout);
  309.  
  310. }
  311.  
  312.  
  313.