home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume9 / teco / part01 / te_main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-03-11  |  5.8 KB  |  202 lines

  1. /* TECO for Ultrix   Copyright 1986 Matt Fichtenbaum                        */
  2. /* This program and its components belong to GenRad Inc, Concord MA 01742    */
  3. /* They may be copied if this copyright notice is included                    */
  4.  
  5. /* te_main.c  main program  1/14/87 */
  6.  
  7. /*
  8. * This is TECO for Ultrix on a Vax.  It is mostly compatible with DEC TECO
  9. * as explained in the DEC TECO manual.  It was written from a manual for
  10. * TECO-11 version 34, and so adheres most closely to that version.
  11. *
  12. * This program consists of several source files, as follows:
  13. *
  14. * te_main.c (this file)        Main program - initialize, read command line and
  15. *                            startup file, handle errors, high-level read and
  16. *                            execute command strings.
  17. *
  18. * te_defs.h                    Definitions file, to be #included with other files
  19. *
  20. * te_data.c                    Global variables
  21. *
  22. * te_rdcmd.c                Read in a command string
  23. *
  24. * te_exec0.c                First-level command execution - numbers, values,
  25. *                            assemble expressions
  26. *
  27. * te_exec1.c                Most commands
  28. *
  29. * te_exec2.c                "E" and "F" commands, and file I/O
  30. *
  31. * te_srch.c                    routines associated with "search" commands
  32. *
  33. * te_subs.c                    higher-level subroutines
  34. *
  35. * te_utils.c                lower-level subroutines
  36. *
  37. * te_chario.c                keyboard (stdin), typeout (stdout), suspend
  38. *
  39. * te_window.c                display window and display special functions
  40. *
  41. * These routines should be compiled and linked to form the TECO executable.
  42. */
  43.  
  44. #include "te_defs.h"
  45.  
  46. main(argc, argv)
  47.     int argc;            /* arg count */
  48.     char *argv[];        /* array of string pointers */
  49.     {
  50.     int i;
  51.  
  52.     save_args(argc, argv, &qreg[36]);    /* copy command line to Qz */
  53.     read_startup();                        /* read startup file */
  54.     setup_tty(TTY_ON);        /* set tty to CBREAK, no echo, asynch mode */
  55.     window(WIN_INIT);        /* initialize screen-image buffer */
  56.     get_term_par();            /* set terminal screen-size parameters */
  57.  
  58. /* set up error restart */
  59.     if (err = setjmp(xxx))
  60.         {
  61.         if (err == E_EFI) goto quit;        /* EOF from standard input - clean up and exit */
  62.         printf("\015\012?  %s", errors[err-1]);
  63.         if (err == E_SRH) print_string(SERBUF);            /* print unfulfilled search string */
  64.         else if ((err == E_FNF) || (err == E_COF) || (err == E_AMB)) print_string(FILBUF);    /* or file string */
  65.         crlf();
  66.         eisw = 0;                /* stop indirect command execution */
  67.         et_val &= ~(ET_CTRLC | ET_NOWAIT | ET_CTRLO | ET_NOECHO);    /* reset ^C trap, read w/o wait, ^O (unused), no echo */
  68.         if (et_val & ET_QUIT)    /* if ET has "quit on error" set, exit (phone home) */
  69.             {
  70.             cleanup();            /* reset screen, keyboard, output files */
  71.             exit(1);            /* and exit */
  72.             }
  73.         }
  74.  
  75. /* forever: read and execute command strings */
  76.  
  77.     for (exitflag = 1; exitflag >= 0; )        /* "exit" sets exitflag to -1; ^C to -2; "hangup" to -3 */
  78.         {
  79.         window(WIN_REFR);                /* display the buffer */
  80.         free_blist(insert_p);            /* free any storage from failed insert */
  81.         free_blist(dly_freebuff);        /* return any delayed cells */
  82.         insert_p = dly_freebuff = NULL;
  83.         et_val &= ~ET_QUIT;                /* clear "abort on error" */
  84.         if (read_cmdstr()) goto quit;
  85.         exitflag = 0;                    /* enable ^C detector */
  86.         if (!WN_scroll) window(WIN_REDRAW);        /* if not in scroll mode, force full redraw on first ^W or nW */
  87.         exec_cmdstr();
  88.         }
  89.  
  90.     if (exitflag == -2) ERROR(E_XAB);        /* ^C detected during execution */
  91.     else if (exitflag == -3) panic();        /* hangup during execution: save buffer and close files */
  92.  
  93. /* exit from program */
  94.  
  95.   quit:
  96.     ev_val = es_val = 0;    /* no last one-line window */
  97.     window(WIN_REFR);        /* last display */
  98.     cleanup();                /* reset screen, terminal, output files */
  99.     exit(0);                /* and quit */
  100.     }
  101. /* reset screen state, keyboard state; remove open output files */
  102.  
  103. cleanup()
  104.     {
  105.     window(WIN_OFF);            /* restore screen */
  106.     setup_tty(TTY_OFF);            /* restore terminal */
  107.     kill_output(&po_file);        /* kill any open primary output file */
  108.     kill_output(&so_file);        /* and secondary file */
  109.     }
  110.  
  111.  
  112. /* print string for error message */
  113. /* argument is subscript of a qreg qh, prints text from that buffer */
  114.  
  115. print_string(arg)
  116.     int arg;
  117.     {
  118.     int i, c;
  119.     struct buffcell *p;
  120.  
  121.     type_char('"');
  122.     for (p = qreg[arg].f, c = 0, i = 0; i < qreg[arg].z; i++)
  123.         {
  124.         if (!p->ch[c]) break;
  125.         type_char(p->ch[c]);
  126.         if (++c > CELLSIZE-1)
  127.             {
  128.             p = p->f;
  129.             c = 0;
  130.             }
  131.         }
  132.     type_char('"');
  133.     }
  134. /* copy invocation command line to a text buffer */
  135.  
  136. save_args(argc, argv, q)
  137.     int argc;
  138.     char *argv[];
  139.     struct qh *q;
  140.     {
  141.     char c;
  142.     struct qp ptr;
  143.  
  144.     make_buffer(q);                /* attach a text buffer */
  145.     ptr.p = q->f;                /* initialize pointer to output string */
  146.     ptr.c = q->z = 0;            /* and output char count */
  147.     for (; argc > 0; argv++, argc--)    /* for each arg */
  148.         {
  149.         while ( ((c = *((*argv)++)) != '\0') && (q->z < CELLSIZE-1) )
  150.             {
  151.             ptr.p->ch[ptr.c] = c;        /* copy char to q-reg */
  152.             fwdcx(&ptr);
  153.             ++q->z;                        /* count characters */
  154.             }
  155.         if (argc > 1)                    /* if not last argument... */
  156.             {
  157.             ptr.p->ch[ptr.c] = ' ';        /* space to separate arguments */
  158.             fwdcx(&ptr);
  159.             }
  160.         }
  161.     }
  162.  
  163.  
  164.  
  165. /* routine to read startup file */
  166.  
  167. char startup_name[] = "/.tecorc";        /* name of startup file */
  168.  
  169. read_startup()
  170.     {
  171.     char *hp, *getenv();
  172.     char fn[CELLSIZE];        /* filename storage */
  173.     int i;
  174.  
  175. /* look for ".tecorc" in current directory first */
  176.  
  177.     if (!(eisw = fopen(&startup_name[1], "r")))
  178.         {
  179.         if (hp = getenv("HOME"))    /* if not found, look in home directory */
  180.             {
  181.             for (i = 0; i < CELLSIZE; i++) if (!(fn[i] = *(hp++))) break;    /* copy until trailing null */
  182.             for (hp = &startup_name[0]; i < CELLSIZE; i++) if (!(fn[i] = *(hp++))) break;
  183.             eisw = fopen(fn, "r");        /* set eisw if file found, or not if not */
  184.             }
  185.         }
  186.     }
  187. /* routine to get terminal height and width from termcap */
  188.  
  189. get_term_par()
  190.     {
  191.     char buff[1024];    /* termcap buffer */
  192.     char *pname;        /* pointer to name of terminal */
  193.     extern char *getenv();
  194.  
  195.     if (pname = getenv("TERM"))        /* read terminal name */
  196.         {
  197.         tgetent(buff, pname);        /* get entry */
  198.         set_term_par(tgetnum("li"), tgetnum("co"));    /* get #lines and #columns and set params */
  199.         }
  200.     }
  201.  
  202.