home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / PDP-11 / Distributions / ucb / spencer_2bsd.tar.gz / 2bsd.tar / src / ex / ex.c < prev    next >
C/C++ Source or Header  |  1980-02-17  |  9KB  |  427 lines

  1. /* Copyright (c) 1979 Regents of the University of California */
  2. #include "ex.h"
  3. #include "ex_argv.h"
  4. #include "ex_temp.h"
  5. #include "ex_tty.h"
  6.  
  7. #ifdef TRACE
  8. char    tttrace[]    = { '/','d','e','v','/','t','t','y','x','x',0 };
  9. #endif
  10.  
  11. /*
  12.  * The code for ex is divided as follows:
  13.  *
  14.  * ex.c            Entry point and routines handling interrupt, hangup
  15.  *            signals; initialization code.
  16.  *
  17.  * ex_addr.c        Address parsing routines for command mode decoding.
  18.  *            Routines to set and check address ranges on commands.
  19.  *
  20.  * ex_cmds.c        Command mode command decoding.
  21.  *
  22.  * ex_cmds2.c        Subroutines for command decoding and processing of
  23.  *            file names in the argument list.  Routines to print
  24.  *            messages and reset state when errors occur.
  25.  *
  26.  * ex_cmdsub.c        Subroutines which implement command mode functions
  27.  *            such as append, delete, join.
  28.  *
  29.  * ex_data.c        Initialization of options.
  30.  *
  31.  * ex_get.c        Command mode input routines.
  32.  *
  33.  * ex_io.c        General input/output processing: file i/o, unix
  34.  *            escapes, filtering, source commands, preserving
  35.  *            and recovering.
  36.  *
  37.  * ex_put.c        Terminal driving and optimizing routines for low-level
  38.  *            output (cursor-positioning); output line formatting
  39.  *            routines.
  40.  *
  41.  * ex_re.c        Global commands, substitute, regular expression
  42.  *            compilation and execution.
  43.  *
  44.  * ex_set.c        The set command.
  45.  *
  46.  * ex_subr.c        Loads of miscellaneous subroutines.
  47.  *
  48.  * ex_temp.c        Editor buffer routines for main buffer and also
  49.  *            for named buffers (Q registers if you will.)
  50.  *
  51.  * ex_tty.c        Terminal dependent initializations from termcap
  52.  *            data base, grabbing of tty modes (at beginning
  53.  *            and after escapes).
  54.  *
  55.  * ex_v*.c        Visual/open mode routines... see ex_v.c for a
  56.  *            guide to the overall organization.
  57.  */
  58.  
  59. /*
  60.  * Main procedure.  Process arguments and then
  61.  * transfer control to the main command processing loop
  62.  * in the routine commands.  We are entered as either "ex", "edit" or "vi"
  63.  * and the distinction is made here.  Actually, we are "vi" if
  64.  * there is a 'v' in our name, and "edit" if there is a 'd' in our
  65.  * name.  For edit we just diddle options; for vi we actually
  66.  * force an early visual command, setting the external initev so
  67.  * the q command in visual doesn't give command mode.
  68.  */
  69. main(ac, av)
  70.     register int ac;
  71.     register char *av[];
  72. {
  73.     char *erpath = EXSTRINGS;
  74.     register char *cp;
  75.     register int c;
  76.     bool recov = 0;
  77.     bool ivis = any('v', av[0]);
  78.     bool itag = 0;
  79.     bool fast = 0;
  80. #ifdef TRACE
  81.     register char *tracef;
  82. #endif
  83.  
  84.     /*
  85.      * Immediately grab the tty modes so that we wont
  86.      * get messed up if an interrupt comes in quickly.
  87.      */
  88.     gTTY(1);
  89.     normf = tty.sg_flags;
  90.  
  91.     /*
  92.      * For debugging take files out of . if name is a.out.
  93.      * If a 'd' in our name, then set options for edit.
  94.      */
  95.     if (av[0][0] == 'a')
  96.         erpath += 9;
  97.     if (ivis) {
  98.         options[MAGIC].odefault = value(MAGIC) = 0;
  99.         options[BEAUTIFY].odefault = value(BEAUTIFY) = 1;
  100.     } else if (any('d', av[0])) {
  101.         value(OPEN) = 0;
  102.         value(REPORT) = 1;
  103.         value(MAGIC) = 0;
  104.     }
  105.  
  106.     /*
  107.      * Open the error message file.
  108.      */
  109.     draino();
  110.     erfile = open(erpath, 0);
  111.     if (erfile < 0) {
  112.         flush();
  113.         exit(1);
  114.     }
  115.     pstop();
  116.  
  117.     /*
  118.      * Initialize interrupt handling.
  119.      */
  120.     oldhup = signal(SIGHUP, SIG_IGN);
  121.     if (oldhup == SIG_DFL)
  122.         signal(SIGHUP, onhup);
  123.     oldquit = signal(SIGQUIT, SIG_IGN);
  124.     ruptible = signal(SIGINT, SIG_IGN) == SIG_DFL;
  125.     if (signal(SIGTERM, SIG_IGN) == SIG_DFL)
  126.         signal(SIGTERM, onhup);
  127.  
  128.     /*
  129.      * Initialize end of core pointers.
  130.      * Normally we avoid breaking back to fendcore after each
  131.      * file since this can be expensive (much core-core copying).
  132.      * If your system can scatter load processes you could do
  133.      * this as ed does, saving a little core, but it will probably
  134.      * not often make much difference.
  135.      */
  136.     fendcore = (line *) sbrk(0);
  137.     endcore = fendcore - 2;
  138.  
  139.     /*
  140.      * Process flag arguments.
  141.      */
  142.     ac--, av++;
  143.     while (ac && av[0][0] == '-') {
  144.         c = av[0][1];
  145.         if (c == 0) {
  146.             hush = 1;
  147.             value(AUTOPRINT) = 0;
  148.             fast++;
  149.         } else switch (c) {
  150.  
  151. #ifdef TRACE
  152.         case 'T':
  153.             if (av[0][2] == 0)
  154.                 tracef = "trace";
  155.             else {
  156.                 tracef = tttrace;
  157.                 tracef[8] = av[0][2];
  158.                 if (tracef[8])
  159.                     tracef[9] = av[0][3];
  160.                 else
  161.                     tracef[9] = 0;
  162.             }
  163.             trace = fopen(tracef, "w");
  164.             if (trace == NULL)
  165.                 printf("Trace create error\n");
  166.             setbuf(trace, tracbuf);
  167.             break;
  168.  
  169. #endif
  170.  
  171. #ifdef LISP
  172.         case 'l':
  173.             value(LISP) = 1;
  174.             value(SHOWMATCH) = 1;
  175.             break;
  176. #endif
  177.  
  178.         case 'r':
  179.             recov++;
  180.             break;
  181.  
  182.         case 't':
  183.             if (ac > 1 && av[1][0] != '-') {
  184.                 ac--, av++;
  185.                 itag = 1;
  186.                 /* BUG: should check for too long tag. */
  187.                 CP(lasttag, av[0]);
  188.             }
  189.             break;
  190.  
  191.         case 'v':
  192.             globp = "";
  193.             ivis = 1;
  194.             break;
  195.  
  196.         default:
  197.             smerror("Unknown option %s\n", av[0]);
  198.             break;
  199.         }
  200.         ac--, av++;
  201.     }
  202.     if (ac && av[0][0] == '+') {
  203.         firstln = getn(av[0] + 1);
  204.         if (firstln == 0)
  205.             firstln = 20000;
  206.         ac--, av++;
  207.     }
  208.  
  209.     /*
  210.      * If we are doing a recover and no filename
  211.      * was given, then execute an exrecover command with
  212.      * the -r option to type out the list of saved file names.
  213.      * Otherwise set the remembered file name to the first argument
  214.      * file name so the "recover" initial command will find it.
  215.      */
  216.     if (recov) {
  217.         if (ac == 0) {
  218.             die++;
  219.             setrupt();
  220.             execl(EXRECOVER, "exrecover", "-r", 0);
  221.             filioerr(EXRECOVER);
  222.             exit(1);
  223.         }
  224.         CP(savedfile, *av++), ac--;
  225.     }
  226.  
  227.     /*
  228.      * Initialize the argument list.
  229.      */
  230.     argv0 = av;
  231.     argc0 = ac;
  232.     args0 = av[0];
  233.     erewind();
  234.  
  235.     /*
  236.      * Initialize a temporary file (buffer) and
  237.      * set up terminal environment.  Read user startup commands.
  238.      */
  239.     init();
  240.     if (setexit() == 0) {
  241.         setrupt();
  242.         intty = isatty(0);
  243.         if (fast || !intty)
  244.             setterm("dumb");
  245.         else {
  246.             gettmode();
  247.             if ((cp = getenv("TERM")) != 0)
  248.                 setterm(cp);
  249.             if ((cp = getenv("HOME")) != 0)
  250.                 source(strcat(strcpy(genbuf, cp), "/.exrc"), 1);
  251.         }
  252.     }
  253.  
  254.     /*
  255.      * Initial processing.  Handle tag, recover, and file argument
  256.      * implied next commands.  If going in as 'vi', then don't do
  257.      * anything, just set initev so we will do it later (from within
  258.      * visual).
  259.      */
  260.     if (setexit() == 0) {
  261.         if (recov)
  262.             globp = "recover";
  263.         else if (itag)
  264.             globp = ivis ? "tag" : "tag|p";
  265.         else if (argc)
  266.             globp = "next";
  267.         if (ivis)
  268.             initev = globp;
  269.         else if (globp) {
  270.             inglobal = 1;
  271.             commands(1, 1);
  272.             inglobal = 0;
  273.         }
  274.     }
  275.  
  276.     /*
  277.      * Vi command... go into visual.
  278.      * Strange... everything in vi usually happens
  279.      * before we ever "start".
  280.      */
  281.     if (ivis) {
  282.         /*
  283.          * Don't have to be upward compatible with stupidity
  284.          * of starting editing at line $.
  285.          */
  286.         if (dol > zero)
  287.             dot = one;
  288.         globp = "visual";
  289.         if (setexit() == 0)
  290.             commands(1, 1);
  291.     }
  292.  
  293.     /*
  294.      * Clear out trash in state accumulated by startup,
  295.      * and then do the main command loop for a normal edit.
  296.      * If you quit out of a 'vi' command by doing Q or ^\,
  297.      * you also fall through to here.
  298.      */
  299.     ungetchar(0);
  300.     globp = 0;
  301.     initev = 0;
  302.     setlastchar('\n');
  303.     setexit();
  304.     commands(0, 0);
  305.     cleanup(1);
  306.     exit(0);
  307. }
  308.  
  309. /*
  310.  * Initialization, before editing a new file.
  311.  * Main thing here is to get a new buffer (in fileinit),
  312.  * rest is peripheral state resetting.
  313.  */
  314. init()
  315. {
  316.     register int i;
  317.  
  318.     fileinit();
  319.     dot = zero = truedol = unddol = dol = fendcore;
  320.     one = zero+1;
  321.     undkind = UNDNONE;
  322.     chng = 0;
  323.     edited = 0;
  324.     for (i = 0; i <= 'z'-'a'+1; i++)
  325.         names[i] = 1;
  326.     anymarks = 0;
  327. }
  328.  
  329. /*
  330.  * When a hangup occurs our actions are similar to a preserve
  331.  * command.  If the buffer has not been [Modified], then we do
  332.  * nothing but remove the temporary files and exit.
  333.  * Otherwise, we sync the temp file and then attempt a preserve.
  334.  * If the preserve succeeds, we unlink our temp files.
  335.  * If the preserve fails, we leave the temp files as they are
  336.  * as they are a backup even without preservation if they
  337.  * are not removed.
  338.  */
  339. onhup()
  340. {
  341.  
  342.     if (chng == 0) {
  343.         cleanup(1);
  344.         exit(0);
  345.     }
  346.     if (setexit() == 0) {
  347.         if (preserve()) {
  348.             cleanup(1);
  349.             exit(0);
  350.         }
  351.     }
  352.     exit(1);
  353. }
  354.  
  355. /*
  356.  * An interrupt occurred.  Drain any output which
  357.  * is still in the output buffering pipeline.
  358.  * Catch interrupts again.  Unless we are in visual
  359.  * reset the output state (out of -nl mode, e.g).
  360.  * Then like a normal error (with the \n before Interrupt
  361.  * suppressed in visual mode).
  362.  */
  363. onintr()
  364. {
  365.  
  366. #ifdef V6
  367.     signal(SIGINT, onintr);
  368. #else
  369.     signal(SIGINT, inopen ? vintr : onintr);
  370. #endif
  371.     draino();
  372.     if (!inopen) {
  373.         pstop();
  374.         setlastchar('\n');
  375. #ifndef V6
  376.     }
  377. #else
  378.     } else
  379.         vraw();
  380. #endif
  381.     error("\nInterrupt" + inopen);
  382. }
  383.  
  384. /*
  385.  * If we are interruptible, enable interrupts again.
  386.  * In some critical sections we turn interrupts off,
  387.  * but not very often.
  388.  */
  389. setrupt()
  390. {
  391.  
  392.     if (ruptible)
  393. #ifdef V6
  394.         signal(SIGINT, onintr);
  395. #else
  396.         signal(SIGINT, inopen ? vintr : onintr);
  397. #endif
  398. }
  399.  
  400. preserve()
  401. {
  402.  
  403.     synctmp();
  404.     pid = fork();
  405.     if (pid < 0)
  406.         return (0);
  407.     if (pid == 0) {
  408.         close(0);
  409.         dup(tfile);
  410.         execl(EXPRESERVE, "expreserve", (char *) 0);
  411.         exit(1);
  412.     }
  413.     waitfor();
  414.     if (rpid == pid && status == 0)
  415.         return (1);
  416.     return (0);
  417. }
  418.  
  419. #ifndef V6
  420. exit(i)
  421.     int i;
  422. {
  423.  
  424.     _exit(i);
  425. }
  426. #endif
  427.