home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / pc / memacs / ue311c.arc / MAIN.C < prev    next >
C/C++ Source or Header  |  1991-03-12  |  24KB  |  1,014 lines

  1. /*
  2.  *    MicroEMACS 3.11
  3.  *        written by Daniel M. Lawrence
  4.  *        based on code by Dave G. Conroy.
  5.  *
  6.  *    (C)Copyright 1988,1989,1990,1991 by Daniel M. Lawrence
  7.  *    MicroEMACS 3.11 can be copied and distributed freely for any
  8.  *    non-commercial purposes. MicroEMACS 3.11 can only be incorporated
  9.  *    into commercial software with the permission of the current author.
  10.  *
  11.  * This file contains the main driving routine, and some keyboard processing
  12.  * code, for the MicroEMACS screen editor.
  13.  *
  14.  */
  15.  
  16. #include    <stdio.h>
  17.  
  18. /* make global definitions not external */
  19. #define maindef
  20.  
  21. #include    "estruct.h"    /* global structures and defines */
  22. #include    "eproto.h"    /* variable prototype definitions */
  23. #include    "efunc.h"    /* function declarations and name table */
  24. #include    "edef.h"    /* global definitions */
  25. #include    "elang.h"    /* human language definitions */
  26. #include    "ebind.h"    /* default key bindings */
  27.  
  28. /* for many different systems, increase the default stack space */
  29.  
  30. #if    MSDOS & LATTICE
  31. unsigned _stack = 20000;
  32. #endif
  33.  
  34. #if    MSDOS & DTL
  35. int    _okbigbuf = 0;        /* Only allocate memory when needed.*/
  36. int    _stack = 20000;     /* Reset the ol' stack size.*/
  37. #endif
  38.  
  39. #if    ATARI & MWC
  40. long _stksize = 20000L;     /* reset stack size (must be even) */
  41. #endif
  42.  
  43. #if    MSDOS & AZTEC
  44. int _STKSIZ = 20000/16;     /* stack size in paragraphs */
  45. int _STKRED = 1024;        /* stack checking limit */
  46. int _HEAPSIZ = 4096/16;     /* (in paragraphs) */
  47. /*int _STKLOW = 0;        default is stack above heap (small only) */
  48. #endif
  49.  
  50. #if    MSDOS & TURBO
  51. extern unsigned int _stklen = 10000;
  52. #endif
  53.  
  54. /*    make VMS happy...    */
  55.  
  56. #if    VMS
  57. #include    <ssdef.h>
  58. #define GOOD    (SS$_NORMAL)
  59. #endif
  60.  
  61. #ifndef GOOD
  62. #define GOOD    0
  63. #endif
  64.  
  65. /*
  66.     This is the primary entry point that is used by command line
  67.     invocation, and by applications that link with microemacs in
  68.     such a way that each invocation of Emacs is a fresh environment.
  69.  
  70.     There is another entry point in VMS.C that is used when
  71.     microemacs is "linked" (In quotes, because it is a run-time link
  72.     rather than a link-time link.) with applications that wish Emacs
  73.     to preserve it's context across invocations.  (For example,
  74.     EMACS.RC is only executed once per invocation of the
  75.     application, instead of once per invocation of Emacs.)
  76.  
  77.     Note that re-entering an Emacs that is saved in a kept
  78.     subprocess would require a similar entrypoint.
  79. */
  80.  
  81. #if    CALLED
  82. emacs(argc, argv)
  83. #else
  84. #if    VMS
  85. me$edit(argc, argv)
  86. MAIN_PROGRAM
  87. #else
  88. main(argc, argv)
  89. #endif
  90. #endif
  91.  
  92. int argc;    /* # of arguments */
  93. char *argv[];    /* argument strings */
  94.  
  95. {
  96.     register int status;
  97.  
  98.     /* Initialize the editor */
  99.     eexitflag = FALSE;
  100.     vtinit();        /* Terminal */
  101.  
  102.     if (eexitflag)
  103.         goto abortrun;
  104.     edinit("main");     /* Buffers, windows, screens */
  105.  
  106.     varinit();        /* user variables */
  107.     initchars();        /* character set definitions */
  108.  
  109.     /* Process the command line and let the user edit */
  110.     dcline(argc, argv, TRUE);
  111.     status = editloop();
  112. abortrun:
  113.     vttidy();
  114. #if    CLEAN
  115.     clean();
  116. #endif
  117. #if    CALLED
  118.     return(status);
  119. #else
  120.     exit(status);
  121. #endif
  122. }
  123.  
  124. #if    CLEAN
  125. /*
  126.     On some primitive operation systems, and when emacs is used as
  127.     a subprogram to a larger project, emacs needs to de-alloc its
  128.     own used memory, otherwise we just exit.
  129. */
  130.  
  131. PASCAL NEAR clean()
  132. {
  133.     register BUFFER *bp;    /* buffer list pointer */
  134.     register SCREEN *scrp;    /* ptr to screen to dump */
  135.  
  136.     /* first clean up the screens */
  137.     scrp = first_screen;
  138.     while (wp) {
  139.         first_screen = sp->s_next_screen;
  140.         free_screen(scrp);
  141.         sp = first_screen;
  142.     }
  143.     cur_screen->s_first_window = cur_screen->w_cur_window = wheadp = NULL;
  144.  
  145.     /* then the buffers */
  146.     bp = bheadp;
  147.     while (bp) {
  148.         bp->b_nwnd = 0;
  149.         bp->b_flag = 0; /* don't say anything about a changed buffer! */
  150.         zotbuf(bp);
  151.         bp = bheadp;
  152.     }
  153.  
  154.     /* the screens */
  155.     /***************/
  156.  
  157.     /* and the kill buffer */
  158.     kdelete();
  159.  
  160.     /* clear some search variables */
  161. #if    MAGIC
  162.     mcclear();
  163.     rmcclear();
  164. #endif
  165.     if (patmatch != NULL) {
  166.         free(patmatch);
  167.         patmatch = NULL;
  168.     }
  169.  
  170.     /* dealloc the user variables */
  171.     varclean();
  172.  
  173.     /* and the video buffers */
  174.     vtfree();
  175. }
  176. #endif
  177.  
  178. /*    Process a command line.   May be called any time.    */
  179.  
  180. PASCAL NEAR dcline(argc, argv, firstflag)
  181.  
  182. int argc;
  183. char *argv[];
  184. int firstflag;    /* is this the first time in? */
  185.  
  186. {
  187.     register BUFFER *bp;        /* temp buffer pointer */
  188.     register int    firstfile;    /* first file flag */
  189.     register int    carg;        /* current arg to scan */
  190.     register int    startflag;    /* startup executed flag */
  191.     BUFFER *firstbp = NULL;     /* ptr to first buffer in cmd line */
  192.     int viewflag;            /* are we starting in view mode? */
  193.     int gotoflag;            /* do we need to goto a line at start? */
  194.     int gline;            /* if so, what line? */
  195.     int searchflag;         /* Do we need to search at start? */
  196.     int errflag;            /* C error processing? */
  197.     VDESC vd;            /* variable num/type */
  198.     char bname[NBUFN];        /* buffer name of file to read */
  199.  
  200. #if    CRYPT
  201.     int cryptflag;            /* encrypting on the way in? */
  202.     char ekey[NPAT];        /* startup encryption key */
  203. #endif
  204.     CONST NOSHARE extern *pathname[]; /* startup file path/name array */
  205.  
  206.     viewflag = FALSE;    /* view mode defaults off in command line */
  207.     gotoflag = FALSE;    /* set to off to begin with */
  208.     searchflag = FALSE;    /* set to off to begin with */
  209.     firstfile = TRUE;    /* no file to edit yet */
  210.     startflag = FALSE;    /* startup file not executed yet */
  211.     errflag = FALSE;    /* not doing C error parsing */
  212. #if    CRYPT
  213.     cryptflag = FALSE;    /* no encryption by default */
  214. #endif
  215.     disphigh = FALSE;    /* don't escape high bit characters */
  216.     lterm[0] = 0;        /* standard line terminators */
  217.  
  218.     /* Parse a command line */
  219.     for (carg = 1; carg < argc; ++carg) {
  220.  
  221.         /* Process Switches */
  222. #if WMCS
  223.         if (argv[carg][0] == ':') {
  224. #else
  225.         if (argv[carg][0] == '-') {
  226. #endif
  227.             switch (argv[carg][1]) {
  228.                 /* Process Startup macroes */
  229.                 case 'c':    /* -c for changable file */
  230.                 case 'C':
  231.                     viewflag = FALSE;
  232.                     break;
  233.                 case 'e':    /* -e process error file */
  234.                 case 'E':
  235.                     errflag = TRUE;
  236.                     break;
  237.                 case 'g':    /* -g for initial goto */
  238.                 case 'G':
  239.                     gotoflag = TRUE;
  240.                     gline = asc_int(&argv[carg][2]);
  241.                     break;
  242.                 case 'i':    /* -i<var> <value> set an initial */
  243.                 case 'I':    /* value for a variable */
  244.                     bytecopy(bname, &argv[carg][2], NVSIZE);
  245.                     findvar(bname, &vd, NVSIZE + 1);
  246.                     if (vd.v_type == -1) {
  247.                         mlwrite(TEXT52, bname);
  248. /*                            "%%No such variable as '%s'" */
  249.                         break;
  250.                     }
  251.                     svar(&vd, argv[++carg]);
  252.                     break;
  253. #if    CRYPT
  254.                 case 'k':    /* -k<key> for code key */
  255.                 case 'K':
  256.                     cryptflag = TRUE;
  257.                     strcpy(ekey, &argv[carg][2]);
  258.                     break;
  259. #endif
  260.                 case 'r':    /* -r restrictive use */
  261.                 case 'R':
  262.                     restflag = TRUE;
  263.                     break;
  264.                 case 's':    /* -s for initial search string */
  265.                 case 'S':
  266.                     searchflag = TRUE;
  267.                     bytecopy(pat,&argv[carg][2],NPAT);
  268.                     setjtable();
  269.                     break;
  270.                 case 'v':    /* -v for View File */
  271.                 case 'V':
  272.                     viewflag = TRUE;
  273.                     break;
  274.                 default:    /* unknown switch */
  275.                     /* ignore this for now */
  276.                     break;
  277.             }
  278.  
  279.         } else if (argv[carg][0]== '@') {
  280.  
  281.             /* Process Startup macroes */
  282.             if (startup(&argv[carg][1]) == TRUE)
  283.                 /* don't execute emacs.rc */
  284.                 startflag = TRUE;
  285.  
  286.         } else {
  287.  
  288.             /* Process an input file */
  289.  
  290.             /* set up a buffer for this file */
  291.             makename(bname, argv[carg]);
  292.             unqname(bname);
  293.  
  294.             /* set this to inactive */
  295.             bp = bfind(bname, TRUE, 0);
  296.             strcpy(bp->b_fname, argv[carg]);
  297.             bp->b_active = FALSE;
  298.             if (firstfile) {
  299.                 firstbp = bp;
  300.                 firstfile = FALSE;
  301.             }
  302.  
  303.             /* set the modes appropriatly */
  304.             if (viewflag)
  305.                 bp->b_mode |= MDVIEW;
  306. #if    CRYPT
  307.             if (cryptflag) {
  308.                 bp->b_mode |= MDCRYPT;
  309.                 crypt((char *)NULL, 0);
  310.                 crypt(ekey, strlen(ekey));
  311.                 bytecopy(bp->b_key, ekey, NPAT);
  312.             }
  313. #endif
  314.         }
  315.     }
  316.  
  317.     /* if we are C error parsing... run it! */
  318.     if (errflag) {
  319.         if (startup("error.cmd") == TRUE)
  320.             startflag = TRUE;
  321.     }
  322.  
  323.     /* if invoked with no other startup files,
  324.        run the system startup file here */
  325.     if (firstflag && startflag == FALSE)
  326.         startup("");
  327.  
  328.     /* if there are any files to read, read the first one! */
  329.     if (firstflag) {
  330.         bp = bfind("main", FALSE, 0);
  331.         if (firstfile == FALSE && (gflags & GFREAD)) {
  332.             swbuffer(firstbp);
  333.             curbp->b_mode |= gmode;
  334.             update(TRUE);
  335.             mlwrite(lastmesg);
  336.             zotbuf(bp);
  337.         } else
  338.             bp->b_mode |= gmode;
  339.     } else {
  340.         swbuffer(firstbp);
  341.         curbp->b_mode |= gmode;
  342.         update(TRUE);
  343.         mlwrite(lastmesg);
  344.     }
  345.  
  346.  
  347.     /* Deal with startup gotos and searches */
  348.     if (gotoflag && searchflag) {
  349.         update(FALSE);
  350.         mlwrite(TEXT101);
  351. /*            "[Can not search and goto at the same time!]" */
  352.     }
  353.     else if (gotoflag) {
  354.         if (gotoline(TRUE,gline) == FALSE) {
  355.             update(FALSE);
  356.             mlwrite(TEXT102);
  357. /*                "[Bogus goto argument]" */
  358.         }
  359.     } else if (searchflag) {
  360.         if (forwhunt(FALSE, 0) == FALSE)
  361.             update(FALSE);
  362.     }
  363.  
  364. }
  365.  
  366. /*
  367.     This is called to let the user edit something.    Note that if you
  368.     arrange to be able to call this from a macro, you will have
  369.     invented the "recursive-edit" function.
  370. */
  371.  
  372. PASCAL NEAR editloop()
  373.  
  374. {
  375.     register int c;         /* command character */
  376.     register int f;     /* default flag */
  377.     register int n;     /* numeric repeat count */
  378.     register int mflag;    /* negative flag on repeat */
  379.     register int basec;    /* c stripped of meta character */
  380.     register int oldflag;    /* old last flag value */
  381.  
  382.     /* setup to process commands */
  383.     lastflag = 0;        /* Fake last flags.    */
  384.  
  385. loop:
  386.     /* if we were called as a subroutine and want to leave, do so */
  387.     if (eexitflag)
  388.         return(eexitval);
  389.  
  390.     /* execute the "command" macro...normally null */
  391.     oldflag = lastflag;    /* preserve lastflag through this */
  392.     execkey(&cmdhook, FALSE, 1);
  393.     lastflag = oldflag;
  394.  
  395.     /* Fix up the screen    */
  396.     update(FALSE);
  397.  
  398.     /* get the next command from the keyboard */
  399.     c = getkey();
  400.  
  401.     /* if there is something on the command line, clear it */
  402.     if (mpresf != FALSE) {
  403.         mlerase();
  404.         update(FALSE);
  405.     }
  406.  
  407.     /* override the arguments if prefixed */
  408.     if (prefix) {
  409.         if (islower(c & 255))
  410.             c = (c & ~255) | upperc(c & 255);
  411.         c |= prefix;
  412.         f = predef;
  413.         n = prenum;
  414.         prefix = 0;
  415.     } else {
  416.         f = FALSE;
  417.         n = 1;
  418.     }
  419.  
  420.     /* do META-# processing if needed */
  421.  
  422.     basec = c & ~META;        /* strip meta char off if there */
  423.     if ((c & META) && ((basec >= '0' && basec <= '9') || basec == '-') &&
  424.         (getbind(c) == NULL)) {
  425.         f = TRUE;        /* there is a # arg */
  426.         n = 0;            /* start with a zero default */
  427.         mflag = 1;        /* current minus flag */
  428.         c = basec;        /* strip the META */
  429.         while ((c >= '0' && c <= '9') || (c == '-')) {
  430.             if (c == '-') {
  431.                 /* already hit a minus or digit? */
  432.                 if ((mflag == -1) || (n != 0))
  433.                     break;
  434.                 mflag = -1;
  435.             } else {
  436.                 n = n * 10 + (c - '0');
  437.             }
  438.             if ((n == 0) && (mflag == -1))    /* lonely - */
  439.                 mlwrite("Arg:");
  440.             else
  441.                 mlwrite("Arg: %d",n * mflag);
  442.  
  443.             c = getkey();    /* get the next key */
  444.         }
  445.         n = n * mflag;    /* figure in the sign */
  446.     }
  447.  
  448.     /* do ^U repeat argument processing */
  449.  
  450.     if (c == reptc) {           /* ^U, start argument   */
  451.         f = TRUE;
  452.         n = 4;                /* with argument of 4 */
  453.         mflag = 0;            /* that can be discarded. */
  454.         mlwrite("Arg: 4");
  455.         while ((c=getkey()) >='0' && c<='9' || c==reptc || c=='-') {
  456.             if (c == reptc)
  457.                 if ((n > 0) == ((n*4) > 0))
  458.                     n = n*4;
  459.                 else
  460.                     n = 1;
  461.             /*
  462.              * If dash, and start of argument string, set arg.
  463.              * to -1.  Otherwise, insert it.
  464.              */
  465.             else if (c == '-') {
  466.                 if (mflag)
  467.                     break;
  468.                 n = 0;
  469.                 mflag = -1;
  470.             }
  471.             /*
  472.              * If first digit entered, replace previous argument
  473.              * with digit and set sign.  Otherwise, append to arg.
  474.              */
  475.             else {
  476.                 if (!mflag) {
  477.                     n = 0;
  478.                     mflag = 1;
  479.                 }
  480.                 n = 10*n + c - '0';
  481.             }
  482.             mlwrite("Arg: %d", (mflag >=0) ? n : (n ? -n : -1));
  483.         }
  484.         /*
  485.          * Make arguments preceded by a minus sign negative and change
  486.          * the special argument "^U -" to an effective "^U -1".
  487.          */
  488.         if (mflag == -1) {
  489.             if (n == 0)
  490.                 n++;
  491.             n = -n;
  492.         }
  493.     }
  494.  
  495.     /* and execute the command */
  496.     execute(c, f, n);
  497.     goto loop;
  498. }
  499.  
  500. /*
  501.  * Initialize all of the buffers, windows and screens. The buffer name is
  502.  * passed down as an argument, because the main routine may have been told
  503.  * to read in a file by default, and we want the buffer name to be right.
  504.  */
  505.  
  506. PASCAL NEAR edinit(bname)
  507.  
  508. char bname[];    /* name of buffer to initialize */
  509.  
  510. {
  511.     register BUFFER *bp;
  512.  
  513.     /* initialize some important globals */
  514.  
  515.     readhook.k_ptr.fp = nullproc;    /* set internal hooks to OFF */
  516.     readhook.k_type = BINDFNC;
  517.     wraphook.k_ptr.fp = wrapword;
  518.     wraphook.k_type = BINDFNC;
  519.     cmdhook.k_ptr.fp = nullproc;
  520.     cmdhook.k_type = BINDFNC;
  521.     writehook.k_ptr.fp = nullproc;
  522.     writehook.k_type = BINDFNC;
  523.     bufhook.k_ptr.fp = nullproc;
  524.     bufhook.k_type = BINDFNC;
  525.     exbhook.k_ptr.fp = nullproc;
  526.     exbhook.k_type = BINDFNC;
  527.  
  528.     /* allocate the first buffer */
  529.     bp = bfind(bname, TRUE, 0);        /* First buffer     */
  530.     blistp = bfind("[List]", TRUE, BFINVS); /* Buffer list buffer    */
  531.     slistp = bfind("[Screens]", TRUE, BFINVS); /* Buffer list buffer    */
  532.     if (bp==NULL || blistp==NULL)
  533.         meexit(1);
  534.  
  535.     /* and allocate the default screen */
  536.     first_screen = init_screen("MAIN", bp);
  537.     if (first_screen == (SCREEN *)NULL)
  538.         meexit(1);
  539.  
  540.     /* set the current default screen/buffer/window */
  541.     cur_screen = first_screen;
  542.     scr_num = 1;
  543.     curbp = bp;
  544.     curwp = wheadp = cur_screen->s_cur_window = cur_screen->s_first_window;
  545. }
  546.  
  547. /*
  548.  * This is the general command execution routine. It handles the fake binding
  549.  * of all the keys to "self-insert". It also clears out the "thisflag" word,
  550.  * and arranges to move it to the "lastflag", so that the next command can
  551.  * look at it. Return the status of command.
  552.  */
  553. PASCAL NEAR execute(c, f, n)
  554.  
  555. int c;        /* key to execute */
  556. int f;        /* prefix argument flag */
  557. int n;        /* prefix value */
  558.  
  559. {
  560.     register int status;
  561.     KEYTAB *key;            /* key entry to execute */
  562. #if    DBCS
  563.     int schar;        /* second key in 2 byte sequence */
  564. #endif
  565.  
  566.     /* if the keystroke is a bound function...do it */
  567.     key = getbind(c);
  568.     if (key != NULL) {
  569.         thisflag = 0;
  570.         status = execkey(key, f, n);
  571.         lastflag = thisflag;
  572.         return(status);
  573.     }
  574.  
  575.     /*
  576.      * If a space was typed, fill column is defined, the argument is non-
  577.      * negative, wrap mode is enabled, and we are now past fill column,
  578.      * and we are not read-only, perform word wrap.
  579.      */
  580.     if (c == ' ' && (curwp->w_bufp->b_mode & MDWRAP) && fillcol > 0 &&
  581.         n >= 0 && getccol(FALSE) > fillcol &&
  582.         (curwp->w_bufp->b_mode & MDVIEW) == FALSE)
  583.         execkey(&wraphook, FALSE, 1);
  584.  
  585.     if ((c>=0x20 && c<=0xFF)) {    /* Self inserting.    */
  586.         if (n <= 0) {            /* Fenceposts.        */
  587.             lastflag = 0;
  588.             return(n<0 ? FALSE : TRUE);
  589.         }
  590.         thisflag = 0;            /* For the future.    */
  591.  
  592.         /* replace or overwrite mode, not at the end of a string */
  593.         if (curwp->w_bufp->b_mode & (MDREPL | MDOVER) &&
  594.             curwp->w_doto < curwp->w_dotp->l_used) {
  595.  
  596.             /* if we are in replace mode, or
  597.                (next char is not a tab or we are at a tab stop) */
  598.             if (curwp->w_bufp->b_mode & MDREPL ||
  599.                 (lgetc(curwp->w_dotp, curwp->w_doto) != '\t' ||
  600.                 (curwp->w_doto) % tabsize == (tabsize - 1)))
  601.                         ldelete(1L, FALSE);
  602.         }
  603.  
  604.         /* do the appropriate insertion */
  605.         if (c == '}' && (curbp->b_mode & MDCMOD) != 0)
  606.             status = insbrace(n, c);
  607.         else if (c == '#' && (curbp->b_mode & MDCMOD) != 0)
  608.             status = inspound();
  609. #if    DBCS
  610.         else if (is2char(c)) {
  611.             schar = getkey();
  612.             status = TRUE;
  613.             while (n--) {
  614.                 if (linsert(1, c) == FALSE)
  615.                     status = FALSE;
  616.                 if (linsert(1, schar) == FALSE)
  617.                     status = FALSE;
  618.             }
  619.         }
  620. #endif
  621.  
  622.         else
  623.             status = linsert(n, c);
  624.  
  625.         /* check for CMODE fence matching */
  626.         if ((c == '}' || c == ')' || c == ']') &&
  627.                 (curbp->b_mode & MDCMOD) != 0)
  628.             fmatch(c);
  629.  
  630.         /* check auto-save mode */
  631.         if (curbp->b_mode & MDASAVE)
  632.             if (--gacount == 0) {
  633.                 /* and save the file if needed */
  634.                 upscreen(FALSE, 0);
  635.                 filesave(FALSE, 0);
  636.                 gacount = gasave;
  637.             }
  638.  
  639.         lastflag = thisflag;
  640.         return(status);
  641.     }
  642.     TTbeep();
  643.     mlwrite(TEXT19);        /* complain        */
  644. /*        "[Key not bound]" */
  645.     lastflag = 0;                /* Fake last flags.    */
  646.     return(FALSE);
  647. }
  648.  
  649. /*
  650.     Fancy quit command, as implemented by Norm. If the any buffer
  651. has changed do a write on that buffer and exit emacs, otherwise simply
  652. exit.
  653. */
  654.  
  655. PASCAL NEAR quickexit(f, n)
  656.  
  657. int f,n;    /* prefix flag and argument */
  658.  
  659. {
  660.     register BUFFER *bp;    /* scanning pointer to buffers */
  661.     register BUFFER *oldcb; /* original current buffer */
  662.     register int status;
  663.  
  664.     oldcb = curbp;                /* save in case we fail */
  665.  
  666.     bp = bheadp;
  667.     while (bp != NULL) {
  668.         if ((bp->b_flag&BFCHG) != 0    /* Changed.        */
  669.         && (bp->b_flag&BFINVS) == 0) {    /* Real.        */
  670.             curbp = bp;        /* make that buffer cur */
  671.             mlwrite(TEXT103,bp->b_fname);
  672. /*                "[Saving %s]" */
  673.             mlwrite("\n");
  674.             if ((status = filesave(f, n)) != TRUE) {
  675.                 curbp = oldcb;    /* restore curbp */
  676.                 return(status);
  677.             }
  678.         }
  679.     bp = bp->b_bufp;            /* on to the next buffer */
  680.     }
  681.     quit(f, n);                /* conditionally quit    */
  682.     return(TRUE);
  683. }
  684.  
  685. /*
  686.  * Quit command. If an argument, always quit. Otherwise confirm if a buffer
  687.  * has been changed and not written out. Normally bound to "C-X C-C".
  688.  */
  689.  
  690. PASCAL NEAR quit(f, n)
  691.  
  692. int f,n;    /* prefix flag and argument */
  693.  
  694. {
  695.     register int status;    /* return status */
  696.  
  697.     if (f != FALSE        /* Argument forces it.    */
  698.     || anycb() == FALSE    /* All buffers clean or user says it's OK. */
  699.     || (status = mlyesno(TEXT104)) == TRUE) {
  700. /*                 "Modified buffers exist. Leave anyway" */
  701. #if    FILOCK
  702.         if (lockrel() != TRUE) {
  703.             TTputc('\n');
  704.             TTputc('\r');
  705.             TTclose();
  706.             TTkclose();
  707.             status = meexit(1);
  708.         }
  709. #endif
  710.         if (f)
  711.             status = meexit(n);
  712.         else
  713.             status = meexit(GOOD);
  714.     }
  715.     mlerase();
  716.     return(status);
  717. }
  718.  
  719. PASCAL NEAR meexit(status)
  720. int status;    /* return status of emacs */
  721. {
  722.     eexitflag = TRUE;    /* flag a program exit */
  723.     eexitval = status;
  724.  
  725.     /* and now.. we leave and let the main loop kill us */
  726.     return(TRUE);
  727. }
  728.  
  729. /*
  730.  * Begin a keyboard macro.
  731.  * Error if not at the top level in keyboard processing. Set up variables and
  732.  * return.
  733.  */
  734.  
  735. PASCAL NEAR ctlxlp(f, n)
  736.  
  737. int f,n;    /* prefix flag and argument */
  738.  
  739. {
  740.     if (kbdmode != STOP) {
  741.         mlwrite(TEXT105);
  742. /*            "%%Macro already active" */
  743.         return(FALSE);
  744.     }
  745.     mlwrite(TEXT106);
  746. /*        "[Start macro]" */
  747.     kbdptr = &kbdm[0];
  748.     kbdend = kbdptr;
  749.     kbdmode = RECORD;
  750.     return(TRUE);
  751. }
  752.  
  753. /*
  754.  * End keyboard macro. Check for the same limit conditions as the above
  755.  * routine. Set up the variables and return to the caller.
  756.  */
  757.  
  758. PASCAL NEAR ctlxrp(f, n)
  759.  
  760. int f,n;    /* prefix flag and argument */
  761.  
  762. {
  763.     if (kbdmode == STOP) {
  764.         mlwrite(TEXT107);
  765. /*            "%%Macro not active" */
  766.         return(FALSE);
  767.     }
  768.     if (kbdmode == RECORD) {
  769.         mlwrite(TEXT108);
  770. /*            "[End macro]" */
  771.         kbdmode = STOP;
  772.     }
  773.     return(TRUE);
  774. }
  775.  
  776. /*
  777.  * Execute a macro.
  778.  * The command argument is the number of times to loop. Quit as soon as a
  779.  * command gets an error. Return TRUE if all ok, else FALSE.
  780.  */
  781.  
  782. PASCAL NEAR ctlxe(f, n)
  783.  
  784. int f,n;    /* prefix flag and argument */
  785.  
  786. {
  787.     if (kbdmode != STOP) {
  788.         mlwrite(TEXT105);
  789. /*            "%%Macro already active" */
  790.         return(FALSE);
  791.     }
  792.     if (n <= 0)
  793.         return(TRUE);
  794.     kbdrep = n;        /* remember how many times to execute */
  795.     kbdmode = PLAY;     /* start us in play mode */
  796.     kbdptr = &kbdm[0];    /*    at the beginning */
  797.     return(TRUE);
  798. }
  799.  
  800. /*
  801.  * Abort.
  802.  * Beep the beeper. Kill off any keyboard macro, etc., that is in progress.
  803.  * Sometimes called as a routine, to do general aborting of stuff.
  804.  */
  805.  
  806. PASCAL NEAR ctrlg(f, n)
  807.  
  808. int f,n;    /* prefix flag and argument */
  809.  
  810. {
  811.     TTbeep();
  812.     kbdmode = STOP;
  813.     mlwrite(TEXT8);
  814. /*        "[Aborted]" */
  815.     return(ABORT);
  816. }
  817.  
  818. /* tell the user that this command is illegal while we are in
  819.    VIEW (read-only) mode                */
  820.  
  821. PASCAL NEAR rdonly()
  822.  
  823. {
  824.     TTbeep();
  825.     mlwrite(TEXT109);
  826. /*        "[Key illegal in VIEW mode]" */
  827.     return(FALSE);
  828. }
  829.  
  830. PASCAL NEAR resterr()
  831.  
  832. {
  833.     TTbeep();
  834.     mlwrite(TEXT110);
  835. /*        "[That command is RESTRICTED]" */
  836.     return(FALSE);
  837. }
  838.  
  839. PASCAL NEAR nullproc(f, n)    /* user function that does NOTHING */
  840.  
  841. int n, f;    /* yes, these are default and never used.. but MUST be here */
  842.  
  843. {
  844. }
  845.  
  846. PASCAL NEAR meta(f, n)    /* set META prefixing pending */
  847.  
  848. int f, n;    /* prefix flag and argument */
  849.  
  850. {
  851.     prefix |= META;
  852.     prenum = n;
  853.     predef = f;
  854.     return(TRUE);
  855. }
  856.  
  857. PASCAL NEAR cex(f, n)    /* set ^X prefixing pending */
  858.  
  859. int f, n;    /* prefix flag and argument */
  860.  
  861. {
  862.     prefix |= CTLX;
  863.     prenum = n;
  864.     predef = f;
  865.     return(TRUE);
  866. }
  867.  
  868. PASCAL NEAR unarg()    /* dummy function for binding to universal-argument */
  869. {
  870. }
  871.  
  872. /*    bytecopy:    copy a string...with length restrictions
  873.             ALWAYS null terminate
  874. */
  875.  
  876. char *PASCAL NEAR bytecopy(dst, src, maxlen)
  877.  
  878. char *dst;    /* destination of copied string */
  879. char *src;    /* source */
  880. int maxlen;    /* maximum length */
  881.  
  882. {
  883.     char *dptr;    /* ptr into dst */
  884.  
  885.     dptr = dst;
  886.     while (*src && (maxlen-- > 0))
  887.         *dptr++ = *src++;
  888.     *dptr = 0;
  889.     return(dst);
  890. }
  891.  
  892. /*    copystr:    make another copy of the argument
  893.  
  894. */
  895.  
  896. char *PASCAL NEAR copystr(sp)
  897.  
  898. char *sp;    /* string to copy */
  899.  
  900. {
  901.     char *dp;    /* copy of string */
  902.  
  903.     /* make room! */
  904.     dp = malloc(strlen(sp)+1);
  905.     if (dp == NULL)
  906.         return(NULL);
  907.     strcpy(dp, sp);
  908.     return(dp);
  909. }
  910.  
  911. /*****        Compiler specific Library functions    ****/
  912.  
  913. #if    RAMSIZE
  914. /*    These routines will allow me to track memory usage by placing
  915.     a layer on top of the standard system malloc() and free() calls.
  916.     with this code defined, the environment variable, $RAM, will
  917.     report on the number of bytes allocated via malloc.
  918.  
  919.     with SHOWRAM defined, the number is also posted on the
  920.     end of the bottom mode line and is updated whenever it is changed.
  921. */
  922.  
  923. #undef    malloc
  924. #undef    free
  925.  
  926. #if     VMS & OPTMEM        /* these routines are faster! */
  927. #define    malloc    VAXC$MALLOC_OPT
  928. #define free    VAXC$FREE_OPT
  929. #endif
  930.  
  931. char *allocate(nbytes)    /* allocate nbytes and track */
  932.  
  933. unsigned nbytes;    /* # of bytes to allocate */
  934.  
  935. {
  936.     char *mp;    /* ptr returned from malloc */
  937.     char *malloc();
  938.     FILE *track;    /* malloc track file */
  939.  
  940.     mp = malloc(nbytes);
  941.  
  942. #if    RAMTRCK
  943.     track = fopen("malloc.dat", "a");
  944.     fprintf(track, "Allocating %u bytes at %u:%u\n", nbytes,
  945.             FP_SEG(mp), FP_OFF(mp));
  946.     fclose(track);
  947. #endif
  948.  
  949.     if (mp) {
  950. #if    0
  951.         envram += nbytes;
  952. #else
  953.         envram += 1;
  954. #endif
  955. #if    RAMSHOW
  956.         dspram();
  957. #endif
  958.     }
  959.  
  960.     return(mp);
  961. }
  962.  
  963. release(mp)    /* release malloced memory and track */
  964.  
  965. char *mp;    /* chunk of RAM to release */
  966.  
  967. {
  968.     unsigned *lp;    /* ptr to the long containing the block size */
  969. #if    RAMTRCK
  970.     FILE *track;    /* malloc track file */
  971.  
  972.     track = fopen("malloc.dat", "a");
  973.     fprintf(track, "Freeing %u:%u\n",
  974.             FP_SEG(mp), FP_OFF(mp));
  975.     fclose(track);
  976. #endif
  977.  
  978.     if (mp) {
  979.         /* update amount of ram currently malloced */
  980. #if    0
  981.         lp = ((unsigned *)mp) - 1;
  982.         envram -= (long)*lp - 2;
  983. #else
  984.         envram -= 1;
  985. #endif
  986.         free(mp);
  987. #if    RAMSHOW
  988.         dspram();
  989. #endif
  990.     }
  991. }
  992.  
  993. #if    RAMSHOW
  994. dspram()    /* display the amount of RAM currently malloced */
  995.  
  996. {
  997.     char mbuf[20];
  998.     char *sp;
  999.  
  1000.     TTmove(term.t_nrow - 1, 70);
  1001. #if    COLOR
  1002.     TTforg(7);
  1003.     TTbacg(0);
  1004. #endif
  1005.     sprintf(mbuf, "[%lu]", envram);
  1006.     sp = &mbuf[0];
  1007.     while (*sp)
  1008.         TTputc(*sp++);
  1009.     TTmove(term.t_nrow, 0);
  1010.     movecursor(term.t_nrow, 0);
  1011. }
  1012. #endif
  1013. #endif
  1014.