home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / mm / ccmd / skel.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-18  |  6.2 KB  |  247 lines

  1. /*
  2.  Copyright (c) 1986, 1990 by The Trustees of Columbia University in
  3.  the City of New York.  Permission is granted to any individual or
  4.  institution to use, copy, or redistribute this software so long as it
  5.  is not sold for profit, provided this copyright notice is retained.
  6.  
  7.  Author: Howie Kaye
  8. */
  9.  
  10. static char *Version = "Skel Version 1.0 of Sun Apr  5 02:37:41 1987";
  11.  
  12. #include "ccmd.h"            /* get ccmd symbols */
  13.  
  14. /*
  15.  * forward declare parse routines.
  16.  */
  17. int c_exit(), c_take(), c_version(), c_help(), c_bug_report();
  18.  
  19. char *malloc();
  20.  
  21. static char *atmbuf;
  22. /*
  23.  * set up the top level keyword table
  24.  * This will be used by the help command also.
  25.  */
  26.  
  27. static keywrd cmds[] = {
  28.   { "bug-report", KEY_INV, (keyval) c_bug_report },
  29.   { "exit",    0,    (keyval) c_exit },
  30.   { "help",    0,    (keyval) c_help },
  31.   { "quit",    KEY_INV,(keyval) c_exit },
  32.   { "take",    0,    (keyval) c_take },
  33.   { "version",    KEY_INV,(keyval) c_version },
  34. };
  35.  
  36. /*
  37.  * the actual keyword table type has a length in it.
  38.  */
  39. static keytab cmdtab = { (sizeof(cmds)/sizeof(keywrd)), cmds };
  40.  
  41. int argc;
  42. char *argv;
  43. main(Argc,Argv) int Argc; char *Argv;
  44. {
  45.   char *cmini();
  46.   argc = Argc;
  47.   argv = Argv;
  48.  
  49.   atmbuf = cmini();            /* initialize ccmd */
  50.   cmxprintf("Skeleton program Version 1.0\n"); /* display version info */
  51.   cmxprintf("%s\n",cm_version());    /* and CCMD version as well. */
  52.   toplevel();                /* top level cmd parser. */
  53.   cmdone();                /* restore terminal, etc */
  54.   return(0);
  55. }
  56.  
  57. static int done = FALSE;        /* exit flag */
  58. toplevel() {
  59.   pval parseval;
  60.   fdb *used;
  61.                     /* FDB for top level commands */
  62.   static fdb cmdfdb = { _CMKEY, 0, NULL, (pdat) &(cmdtab), "Command, ", 
  63.               NULL, NULL };
  64.   while (!done) {
  65.     cmseter();                /* set error trap */
  66.                     /* control will come here in the */
  67.                     /* case of a parse error. */
  68.     if (cmcsb._cmerr == CMxEOF)        /* exit on EOF -- this is useful */
  69.       break;                /* for take's */
  70.     if (cmargs(argc,argv))        /* check for command line args */
  71.       done = TRUE;
  72.     else
  73.       prompt("Skel> ");            /* prompt */
  74.     cmsetrp();                /* set reparse trap */
  75.                     /* control will come here in the */
  76.                     /* case of a reparse. */
  77.     parse(&cmdfdb,&parseval,&used);    /* parse a command */
  78.     execute(parseval._pvint);        /* execute it. */
  79.   }
  80. }
  81.  
  82. /*
  83.  * execute a command.
  84.  * call the function, with the help flag off.
  85.  */
  86. execute(f) int (*f)(); {
  87.   (*f)(FALSE);                /* call function with help flag off */
  88. }
  89.  
  90. /*
  91.  * call a command function with the help flag on
  92.  */
  93. dohelp(f) int (*f)(); {
  94.   (*f)(TRUE);                /* call function with help flag on */
  95. }
  96.  
  97.  
  98.  
  99. /*
  100.  * top level commands.
  101.  * all of these take the form 
  102.  * command(helpflag) {
  103.  *   if (helpflag) print a help string
  104.  *   else { parse and execute }
  105.  *
  106.  * this style is encouraged, as the documentation for each command
  107.  * resides inside the command itself.  This leads to self documenting
  108.  * parses, and documentation which is consistant with the code.
  109.  */
  110.  
  111. /*
  112.  * the exit command
  113.  */
  114. c_exit(helpflg) int helpflg; {
  115.   if (helpflg) {
  116.     cmxprintf("\
  117. Exit from this program.  The invisible quit command is a synonym for it.\n");
  118.   }
  119.   else {
  120.     noise("this program");
  121.     confirm();
  122.     done = TRUE;            /* just set the global exitflg */
  123.   }
  124. }
  125.  
  126. /*
  127.  * give help.
  128.  */
  129. c_help(helpflg) {
  130.   static fdb cmdfdb = { _CMKEY, 0, NULL, (pdat) &(cmdtab), "Command, ", 
  131.               NULL, NULL };
  132.   static fdb hlpfdb = { _CMCFM, 0, &cmdfdb, NULL, NULL, NULL, NULL };
  133.   pval parseval;
  134.   fdb *used;
  135.   
  136.   if (helpflg) {
  137.     cmxprintf("The help command gives help.\n");
  138.   }
  139.   else {
  140.     noise("me with");
  141.     parse(&hlpfdb,&parseval,&used);    /* parse a command */
  142.     if (used == &hlpfdb) {
  143.       cmxprintf("This is a skeleton CCMD program.\n");
  144.     }
  145.     else {
  146.       int which = parseval._pvint;
  147.       confirm();
  148.       dohelp(which);            /* help on a subject. */
  149.     }
  150.   }
  151. }
  152.  
  153. /*
  154.  * the take command
  155.  * takes commands from another file.
  156.  */
  157.  
  158. c_take(helpflg) int helpflg; {
  159.   if (helpflg) {
  160.     cmxprintf("Take commands from another file.\n");
  161.   }
  162.   else {
  163.     cmtake(toplevel);
  164.   }
  165. }
  166.  
  167.  
  168. c_version(helpflg) int helpflg; {
  169.   if (helpflg) {
  170.     cmxprintf("Display the current version of ccmd\n");
  171.   }
  172.   else {
  173.     noise("of this program");
  174.     confirm();
  175.     cmxprintf("%s\n%s\n", Version,cm_version());
  176.   }
  177. }
  178.  
  179. c_bug_report(helpflg) int helpflg; {
  180.   static char *bug_address = "SY.Howie@CU20B.Columbia.EDU";
  181.   static char *mail_program = "/usr/ucb/mail";
  182.   static char *mail_args = "-s 'CCMD bug'";
  183.   if (helpflg) {
  184.     cmxprintf("Report a CCMD bug.  This will execute the command:\n\
  185.    %s %s %s\n\
  186. to send out a bug report.  If this is not valid for your site, get the\n\
  187. person locally responsible for CCMD to change the file: skel.c to have\n\
  188. the appropriate program and/or address.\n",
  189.           mail_program, mail_args, bug_address);
  190.  
  191.   }
  192.   else {
  193.     static char *msg = NULL;
  194.     static char *cmd = NULL;
  195.     static para_data pd = { NULL, NULL };
  196.     static fdb parafdb = { _CMPARA, 0, NULL, NULL, NULL, NULL, NULL };
  197.     FILE *mypipe, *popen();
  198.     pval parseval;
  199.     fdb *used;
  200.  
  201.     noise("on CCMD to send off");
  202.     parafdb._cmdat = (pdat) &pd;
  203.     confirm();
  204.     cmcls();
  205.     cmxputs(" Message (CTRL/D to send,\n\
  206.   Use CTRL/B to insert a file, CTRL/E to enter editor, CTRL/K to redisplay\n\
  207.   message, CTRL/L to clear screen and redisplay, CTRL/N to abort.):\n");
  208.     parse(¶fdb,&parseval,&used);
  209.     msg = parseval._pvpara;
  210.     if (msg == NULL) {
  211.       cmxprintf("Aborted!\n");
  212.     }
  213.     else {
  214.       cmd = malloc(strlen(bug_address)+sizeof(" ")+
  215.            strlen(mail_args)+sizeof(" ")+
  216.            strlen(mail_program)+1);
  217.       sprintf(cmd,"%s %s %s",mail_program,mail_args,bug_address);
  218. #ifndef MSDOS
  219.       if ((mypipe = popen(cmd,"w")) == NULL) {
  220.     cmxeprintf("?Could not run %s",mail_program);
  221.     perror("");
  222.       }
  223.       else {
  224.     fprintf(mypipe,"Bug report in %s\n\n",cm_version());
  225.     fprintf(mypipe,"%s",msg);
  226.     pclose(mypipe);
  227.     cmxprintf("Sent to %s\n", bug_address);
  228.       }
  229. #else
  230.       cmxprintf("Sorry, but i don't know how to send mail from an MSDOS\n\
  231. machine.  I will save your message in the file CCMD.BUG  -- please try to\n\
  232. find another way to send it out.\n");
  233.       if ((mypipe = fopen("CCMD.BUG", "w")) == NULL) {
  234.     cmxeprintf("Could not open CCMD.BUG");
  235.     perror("");
  236.       }
  237.       else {
  238.     fprintf(mypipe, "%s\n\n",cmd);
  239.     fprintf(mypipe,"Bug report in %s\n\n",cm_version());
  240.     fprintf(mypipe,"%s",msg);
  241.     fclose(mypipe);
  242.       }
  243. #endif
  244.     }
  245.   }
  246. }
  247.