home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / mm / mm-ccmd-0.91-20031009.tar.gz / mm-ccmd-0.91-20031009.tar / work / ccmd / cmmisc.c < prev    next >
C/C++ Source or Header  |  2002-02-19  |  8KB  |  312 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. /*
  11.  * cmmisc
  12.  *
  13.  * miscellaneous parsing routines.
  14.  */
  15.  
  16. #include "ccmdlib.h"
  17. #include "cmfncs.h"
  18.  
  19. static pval parseval;
  20. static fdb *used;
  21.  
  22. /*
  23.  * simple confirm routine
  24.  */
  25.  
  26. confirm()
  27. {
  28.   static fdb cfmfdb = { _CMCFM, 0, NULL, NULL, NULL, NULL, NULL };
  29.   
  30.   parse(&cfmfdb,&parseval,&used);
  31. }
  32.  
  33. /*
  34.  * simple noise routine
  35.  */
  36. noise(nw)
  37. char *nw;
  38. {
  39.   static fdb noifdb = { _CMNOI, 0, NULL, NULL, NULL, NULL, NULL };
  40.   
  41.   noifdb._cmdat = (pdat) nw;
  42.   parse(&noifdb,&parseval,&used);
  43.   cmxflsh();
  44. }
  45.  
  46.  
  47. /*
  48.  * general take command
  49.  * accepts routine to recur on.
  50.  * resets cmcsb descriptors, continues parsing, and then 
  51.  * restores descriptors.
  52.  * expects to be returned to on eof.
  53.  * sets eof on plain take command, if in inferior take.
  54.  */
  55.  
  56. cmtake(proutine) int (*proutine)(); {
  57.   FILE *in,*out,*err,*oldin,*oldout,*olderr; /* input and output streams */
  58.   char ifname[200],ofname[200],efname[200];
  59.   static int level = 0;
  60.   static fdb cmdfdb = { _CMFIL, FIL_NODIR, NULL, NULL, NULL, NULL, NULL};
  61.   static fdb cfm1fdb = { _CMCFM, CM_SDH, &cmdfdb, NULL,
  62.              "Confirm to end current command level", NULL, NULL};
  63.   static fdb logfdb = { _CMFIL, FIL_PO, NULL, NULL, NULL, NULL, NULL };
  64.   static fdb cfmfdb = { _CMCFM, 0, &logfdb, NULL, NULL, NULL, NULL};
  65.   int useij=0,useoj=0,useej=0;
  66.   int r;
  67.   int history;
  68.  
  69.   noise("commands from");        /* give noise */
  70.   parse(&cfm1fdb,&parseval,&used);    /* parse for filename or confirm */
  71.   if (used == &cfm1fdb) {        /* if a confirm */
  72.     if (level != 0)            /* if not top level parse, */
  73.       cmcsb._cmerr = CMxEOF;        /* set eof to pop back */
  74.     else 
  75.       cmcsb._cmerr = CMxOK;        /* otherwise, no error */
  76.     longjmp(cmerjb,1);            /* and then jump back to error catch */
  77.   }
  78.   strcpy(ifname,*parseval._pvfil);    /* a filename...copy it */
  79.   noise("logging output on");        /* try to parse a log file */
  80.   parse(&cfmfdb, &parseval, &used);
  81.   if (used == &cfmfdb) {        /* just a confirm? */
  82.     useoj = useej = 0;
  83.   }
  84.   else {
  85.     useoj = 1;
  86.     strcpy(ofname,cmcsb._cmabp);    /* copy output filename */
  87.     noise("and errors on");        /* try to parse an error file */
  88.     parse(&cfmfdb, &parseval, &used);
  89.     if (used == &cfmfdb) {        /* just a confirm? */
  90.       useej = 0;
  91.     }
  92.     else {
  93.       useej = 1;
  94.       strcpy(efname,cmcsb._cmabp);    /* copy output filename */
  95.       confirm();
  96.     }
  97.   }
  98.   in = fopen(ifname,"r");        /* try to open input file */
  99.   if (in == NULL) {
  100.     cmxputs("?Unable to open input file\n");
  101.     return;
  102.   }
  103.   if (useoj) {
  104.     out = fopen(ofname,"a");        /* try to open output file */
  105.     if (out == NULL) {
  106.       cmxerr("?Unable to open logfile\n");
  107.       fclose(in);            /* clean up */
  108.       return;
  109.     }
  110.   }
  111.   else 
  112.     out = NULL;
  113.   if (useej) {
  114.     err = fopen(efname,"a");
  115.     if (err == NULL) {
  116.       cmxerr("?Unable to open error file\n");
  117.       fclose(in);
  118.       fclose(out);
  119.       return;
  120.     }
  121.   }
  122.   else 
  123.     err = cmcsb._cmej;
  124.   oldin = cmcsb._cmij;            /* save old Files */
  125.   oldout = cmcsb._cmoj;
  126.   olderr = cmcsb._cmej;
  127.   if (useej) {
  128.     cmseti(in,out,err);
  129.   }
  130.   else
  131.     cmseti(in,out,cmcsb._cmej);
  132.   level++;                /* note the recursion */
  133.   if (cmcsb._cmhist) {
  134.       history = cmcsb._cmhist->enabled;
  135.       cmhst_disable();
  136.   }
  137.   r = (*proutine)();                /* call the parser */
  138.   if (cmcsb._cmhist) {
  139.       cmcsb._cmhist->enabled = history;
  140.   }
  141.   level--;                /* back from the recursion */
  142.   fclose(in);                /* close up private files */
  143.   cmxflsh();
  144.   if (out != NULL)
  145.     fclose(out);
  146.   cmcsb._cmerr = CMxOK;
  147.   if (useej)
  148.     if (err != NULL)
  149.       fclose(err);
  150.   cmseti(oldin,oldout,olderr);        /* restore old FILES */
  151.   return(r);
  152. }
  153.  
  154.  
  155. /*
  156.  * simple command line arg handler.
  157.  * just make cmd line appear as imput.
  158.  * returns TRUE if there were cmd line args.
  159.  * returns FALSE on any but the first call.
  160.  */
  161. int
  162. cmargs(argc,argv)
  163. int argc;
  164. char **argv;
  165. {
  166.   static int first = TRUE;
  167.   
  168.   if (argc <= 1)
  169.     return(FALSE);            /* no args to stuff */
  170.   if (first) {
  171.     FILE *oin,*oout,*oerr;
  172.     oin = cmcsb._cmij;
  173.     oout = cmcsb._cmoj;
  174.     oerr = cmcsb._cmej;
  175.     cmseti(NULL,NULL,NULL);
  176.     prompt("");                /* issue an empty prompt */
  177.     argc--;
  178.     while (argc-- > 0) {
  179.       cmsti(*(++argv),0);        /* stuff each arg */
  180.       if (argc > 0) {
  181.         cmsti1(SPACE,0);        /* separated by spaces */
  182.       }
  183.     }
  184.     cmsti1(NEWLINE,0);            /* and finished with a newline */
  185.     cmcsb._cmflg |= CM_CFM;        /* mark buffer as confirmed */
  186.     cmcsb._cmbkc = NEWLINE;        /* and this is the confirm char */
  187.     first = FALSE;
  188.     cmseti(oin,oout,oerr);
  189.     return(TRUE);
  190.   }
  191.   else
  192.     return(FALSE);
  193. }
  194.  
  195.  
  196. /*
  197.  * make sure hstact() won't do anything next time.
  198.  * for use after parsing a password, etc.
  199.  */
  200. cmnohist() {
  201.   cmcsb._cmcnt += cmcsb._cmptr - cmcsb._cmbfp;
  202.   cmcsb._cmhst = cmcsb._cmptr = cmcsb._cmbfp ;
  203. /*  cmcsb._cmflg2 |= CM_CRT; */  /* XXX */
  204. }
  205.  
  206. /*
  207.  * set up break mask with * in it.
  208.  */
  209. static brktab passbrk = {
  210.   {
  211.     0xff, 0xff, 0xff, 0xff, 0xff, 0xdc, 0x00, 0x3f,
  212.     0x80, 0x00, 0x00, 0x17, 0x80, 0x00, 0x00, 0x1f  
  213.   },
  214.   {
  215.     0xff, 0xff, 0xff, 0xff, 0xff, 0xdc, 0x00, 0x3f,
  216.     0x80, 0x00, 0x00, 0x17, 0x80, 0x00, 0x00, 0x1f  
  217.   }
  218. };
  219. static fdb passfdb = { _CMFLD, CM_SDH, NULL, NULL, "Password", NULL, &passbrk};
  220. static fdb qpassfdb = { _CMCFM, CM_SDH, &passfdb, NULL, NULL, NULL, NULL };
  221.  
  222. char *
  223. cmpasswd(prmpt) char * prmpt; {
  224.   static char password[50];
  225.   extern cmerjnp();
  226.   prompt(prmpt);            /* prompt */
  227.   setjmp(cmerjb); cmcsb._cmerh = cmerjnp; /* error handler */
  228.   if (cmcsb._cmerr != CMxOK) {
  229.     cmxputc('\n');
  230.     cmecho(TRUE);
  231.     cmnohist();                /* clean up the buffers */
  232.     return(NULL);
  233.   }
  234.   cmsetrp();                /* set reparse handler */
  235.   cmecho(FALSE);            /* turn off echoing */
  236.   parse(&qpassfdb,&parseval,&used);    /* parse a field */
  237.   if (used == &passfdb) {
  238.     strcpy(password,cmcsb._cmabp);    /* copy it into a local buffer */
  239.     confirm();                /* confirm */
  240.   }
  241.   else password[0] = '\0';
  242.   cmxputc('\n');            /* and display the confirm*/
  243.   cmecho(TRUE);                /* turn back on echoing */
  244.   cmnohist();                /* clean up the buffers */
  245.   return(password);
  246. }
  247.  
  248. cmsystem(cmd) char *cmd; {
  249.   int r;
  250.   cmtend();
  251.   r = system(cmd);
  252.   cmtset();
  253.   return(r);
  254. }
  255.  
  256. char *
  257. cmini() {
  258.   static int cmdbuf[200];
  259.   static char atmbuf[200];
  260.   static char wrkbuf[200];
  261.  
  262.   cmbufs(cmdbuf,200,atmbuf,200,wrkbuf,200); /* set up our buffers */
  263.   cmseti(stdin, stdout, stderr);
  264.   return(atmbuf);
  265. }
  266.  
  267. /*
  268.  * convert a datime struct into a long time(2) value.
  269.  */
  270. long
  271. datime_to_time(dt) datime *dt; {
  272.   long t;
  273.   int i,y;
  274.   static int months[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
  275.   int isaleap;
  276.  
  277. #if 0 /* pre-Y2K fix */
  278.   t = dt->_dtyr - 2000;            /* num years */
  279.   isaleap = ((!(t%4) && t%100) || !(t%400));
  280.   t = t/4 + t*365 - t/100 + t/400;    /* num days */
  281.   t += 30*365 + 30/4;            /* subtract num of days in 1900-1970 */
  282. #else /* Y2K fix */
  283.   y = dt->_dtyr - 2000;            /* num years */
  284.   isaleap = ((!(y%4) && y%100) || !(y%400));
  285.   t = 30*365 + 30/4;            /* start w/num of days in 1970-1999 */
  286.   t += y*365;                /* add/subtr num days since/til 2000 */
  287.   if (y>0) {                /* count leap day in 2000 */
  288.     y+=3;                /* weird round-off stuff */
  289.   }
  290.   t += y/4 - y/100 + y/400;        /* add/subtr leap days since/til 2000*/
  291. #endif
  292.  
  293.   for ( i = 0; i < dt->_dtmon; i++)
  294.     t += months[i];
  295.   t += dt->_dtday;
  296.   if (dt->_dtmon >= 2 && isaleap) t += 1; /* add feb 29 (leap day) */
  297.   t *= 24;                /* to hours */
  298.   t += dt->_dthr;
  299.   t *= 60;                /* to minutes */
  300.   t += dt->_dtmin + dt->_dttz + dt->_dtdst;;
  301.   t *= 60;
  302.   t += dt->_dtsec;                   /* to seconds. */
  303.   return(t);
  304. }
  305.  
  306. cm_set_ind(flag) int flag; {
  307.     if (!flag) 
  308.     cmcsb._cmflg2 |= CM_NIN;
  309.     else
  310.     cmcsb._cmflg2 &= ~CM_NIN;
  311. }
  312.