home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 200-299 / ff256.lzh / Stevie / main.c < prev    next >
C/C++ Source or Header  |  1989-10-19  |  9KB  |  364 lines

  1. /*
  2.  * STEVIE - Simply Try this Editor for VI Enthusiasts
  3.  *
  4.  * Code Contributions By : Tim Thompson           twitch!tjt
  5.  *                         Tony Andrews           onecom!wldrdg!tony 
  6.  *                         G. R. (Fred) Walter    watmath!grwalter 
  7.  */
  8.  
  9. #ifdef AMIGA
  10. # include <proto/exec.h>
  11. #endif
  12.  
  13. #include "stevie.h"
  14.  
  15. #ifdef AMIGA
  16. int             Aux_Device = FALSE;
  17. #endif
  18.  
  19. int             Rows;        /* Number of Rows and Columns */
  20. int             Columns;    /* in the current window. */
  21.  
  22. int             CheckTopcharAndBotchar = FALSE;
  23. int             MustUpdateBotchar = FALSE;
  24. int             ValidToCurschar = FALSE;
  25. int             LineNotValid = FALSE;
  26.  
  27. int             NumLineSizes = -1;    /* # of active LineSizes */
  28. LINE          **LinePointers = NULL;    /* Pointer to the line for LineSizes */
  29. char           *LineSizes = NULL;    /* Size of a line (pline output) */
  30.  
  31. char           *Filename = NULL;/* Current file name */
  32.  
  33. LPtr           *Filemem;    /* The contents of the file, as a single
  34.                  * array. */
  35. LPtr           *Filetop;    /* Line 'above' the start of the file */
  36.  
  37. LPtr           *Fileend;    /* Pointer to the end of the file in Filemem.
  38.                  * (It points to the byte AFTER the last
  39.                  * byte.) */
  40.  
  41. LPtr           *Topchar;    /* Pointer to the byte in Filemem which is in
  42.                  * the upper left corner of the screen. */
  43.  
  44. LPtr           *Botchar;    /* Pointer to the byte in Filemem which is
  45.                  * just off the bottom of the screen. */
  46.  
  47. LPtr           *Curschar;    /* Pointer to byte in Filemem at which the
  48.                  * cursor is currently placed. */
  49.  
  50. int             Curscol;    /* Current position of cursor (column) */
  51. int             Cursrow;    /* Current position of cursor (row) */
  52.  
  53. int             Cursvcol;    /* Current virtual column, the column number
  54.                  * of the file's actual line, as opposed to
  55.                  * the column number we're at on the screen.
  56.                  * This makes a difference on lines that span
  57.                  * more than one screen line. */
  58.  
  59. int             Curswant = 0;    /* The column we'd like to be at. This is
  60.                  * used try to stay in the same column
  61.                  * through up/down cursor motions. */
  62.  
  63. bool_t          set_want_col;    /* If set, then update Curswant the next time
  64.                  * through cursupdate() to the current
  65.                  * virtual column. */
  66.  
  67. int             State = NORMAL;    /* This is the current state of the command
  68.                  * interpreter. */
  69.  
  70. int             Prenum = 0;    /* The (optional) number before a command. */
  71.  
  72. LPtr           *Insstart;    /* This is where the latest insert/append
  73.                  * mode started. */
  74.  
  75. bool_t          Changed = FALSE;/* Set to TRUE if something in the file has
  76.                  * been changed and not written out. */
  77.  
  78. char           *IObuff;        /* file reads are done, one line at a time,
  79.                  * into this buffer; as well as sprintf's */
  80.  
  81. char           *Insbuffptr = NULL;
  82. char           *Insbuff;    /* Each insertion gets stuffed into this
  83.                  * buffer. */
  84.  
  85. char           *Readbuffptr = NULL;
  86. char           *Readbuff;    /* Having this buffer allows STEVIE to easily
  87.                  * make itself do commands */
  88.  
  89. char           *Redobuffptr = NULL;
  90. char           *Redobuff;    /* Each command should stuff characters into
  91.                  * this buffer that will re-execute itself. */
  92.  
  93. bool_t          UndoInProgress = FALSE;    /* Set to TRUE if undo'ing */
  94. char           *Undobuffptr = NULL;
  95. char           *Undobuff;    /* Each command should stuff characters into
  96.                  * this buffer that will undo its effects. */
  97.  
  98. char           *UndoUndobuffptr = NULL;
  99. char           *UndoUndobuff;    /* Each command should stuff characters into
  100.                  * this buffer that will undo its undo. */
  101.  
  102. char           *Yankbuffptr = NULL;
  103. char           *Yankbuff;    /* Yank buffer */
  104.  
  105. char            last_command = NUL;    /* last command */
  106. char            last_command_char = NUL;    /* character needed to undo
  107.                          * last command */
  108.  
  109. bool_t          RedrawingDisabled = FALSE;    /* Set to TRUE if undo'ing or
  110.                          * put'ing */
  111.  
  112. char          **files = NULL;    /* list of input files */
  113. int             numfiles = 0;    /* number of input files */
  114. int             curfile;    /* number of the current file */
  115.  
  116. static void
  117. usage()
  118. {
  119.     fprintf(stderr, "usage: stevie [file ...]\n");
  120.     fprintf(stderr, "       stevie -t tag\n");
  121.     fprintf(stderr, "       stevie +[num] file\n");
  122.     fprintf(stderr, "       stevie +/pat  file\n");
  123.     exit(1);
  124. }
  125.  
  126. #ifdef AMIGA
  127. void
  128. #else
  129. int
  130. #endif
  131. main(argc, argv)
  132.     int             argc;
  133.     char          **argv;
  134. {
  135.     char           *initstr;    /* init string from the environment */
  136.     char           *tag = NULL;    /* tag from command line */
  137.     char           *pat = NULL;    /* pattern from command line */
  138.     int             line = -1;    /* line number from command line */
  139.  
  140.     int             atoi();
  141.     char           *getenv();
  142.  
  143. #ifdef AMIGA
  144.     {
  145.     struct Library *DosBase;/* Used for checking version */
  146.  
  147.     DosBase = OpenLibrary("dos.library", 33);
  148.     if (!DosBase) {
  149.         fprintf(stderr,
  150.          "\nSTEVIE requires Version 33 or later of dos.library.\n");
  151.         exit(2);
  152.     } else {
  153.         CloseLibrary(DosBase);
  154.     }
  155.  
  156. /*
  157.  * I don't think STEVIE should be exited with a break.
  158.  */
  159.     (void) signal(SIGINT, SIG_IGN);
  160.     }
  161. #endif
  162.  
  163.     /*
  164.      * Process the command line arguments. 
  165.      */
  166.     if (argc > 1) {
  167.     switch (argv[1][0]) {
  168.  
  169.       case '-':        /* -t tag */
  170.         if (argv[1][1] != 't')
  171.         usage();
  172.  
  173.         if (argv[2] == NULL)
  174.         usage();
  175.  
  176.         tag = argv[2];
  177.         numfiles = 1;
  178.         break;
  179.  
  180.       case '+':        /* +n or +/pat */
  181.         if (argv[1][1] == '/') {
  182.         if (argv[2] == NULL)
  183.             usage();
  184.         Filename = strsave(argv[2]);
  185.         pat = &(argv[1][1]);
  186.         numfiles = 1;
  187.  
  188.         } else if (isdigit(argv[1][1]) || argv[1][1] == NUL) {
  189.         if (argv[2] == NULL)
  190.             usage();
  191.         Filename = strsave(argv[2]);
  192.         numfiles = 1;
  193.  
  194.         line = (isdigit(argv[1][1])) ?
  195.             atoi(&(argv[1][1])) : 0;
  196.         } else
  197.         usage();
  198.  
  199.         break;
  200.  
  201.       default:        /* must be a file name */
  202. #ifdef WILD_CARDS
  203.         ExpandWildCards(argc - 1, &(argv[1]), &numfiles, &files);
  204.         if (numfiles == 0)
  205.         numfiles = 1;
  206.         else
  207.         Filename = strsave(files[0]);
  208. #else
  209.         Filename = strsave(argv[1]);
  210.         files = &(argv[1]);
  211.         numfiles = argc - 1;
  212. #endif
  213.         if (numfiles > 1)
  214.         printf("%d files to edit\n", numfiles);
  215.         break;
  216.     }
  217.     } else {
  218.     numfiles = 1;
  219.     }
  220.     curfile = 0;
  221.  
  222.     windinit();
  223.  
  224.     /*
  225.      * Allocate LPtr structures for all the various position pointers and for
  226.      * the many buffers. 
  227.      */
  228.     Filetop = (LPtr *) alloc((unsigned) sizeof(LPtr));
  229.     Filemem = (LPtr *) alloc((unsigned) sizeof(LPtr));
  230.     Fileend = (LPtr *) alloc((unsigned) sizeof(LPtr));
  231.     Topchar = (LPtr *) alloc((unsigned) sizeof(LPtr));
  232.     Curschar = (LPtr *) alloc((unsigned) sizeof(LPtr));
  233.     Botchar = (LPtr *) alloc((unsigned) sizeof(LPtr));
  234.     Insstart = (LPtr *) alloc((unsigned) sizeof(LPtr));
  235.     IObuff = alloc(IOSIZE);
  236.     Insbuff = alloc(INSERT_SIZE);
  237.     Readbuff = alloc(READSIZE);
  238.     Redobuff = alloc(REDO_UNDO_SIZE);
  239.     Undobuff = alloc(REDO_UNDO_SIZE);
  240.     UndoUndobuff = alloc(REDO_UNDO_SIZE);
  241.     Yankbuff = alloc(YANKSIZE);
  242.     if (Filetop == NULL ||
  243.     Filemem == NULL ||
  244.     Fileend == NULL ||
  245.     Topchar == NULL ||
  246.     Curschar == NULL ||
  247.     Botchar == NULL ||
  248.     Insstart == NULL ||
  249.     IObuff == NULL ||
  250.     Insbuff == NULL ||
  251.     Readbuff == NULL ||
  252.     Redobuff == NULL ||
  253.     Undobuff == NULL ||
  254.     UndoUndobuff == NULL ||
  255.     Yankbuff == NULL) {
  256.     fprintf(stderr, "Can't allocate data structures\n");
  257.     windexit(0);
  258.     }
  259.     screenalloc();
  260.     filealloc();        /* Initialize Filemem, Filetop & Fileend */
  261.  
  262.     s_clear();
  263.  
  264.     initstr = getenv("EXINIT");
  265.     if (initstr != NULL) {
  266.     char           *lp, buf[128];
  267.  
  268.     lp = getenv("LINES");
  269.     if (lp != NULL) {
  270.         sprintf(buf, "%s lines=%s", initstr, lp);
  271.         readcmdline(':', buf);
  272.     } else
  273.         readcmdline(':', initstr);
  274.     }
  275.     if (Filename != NULL) {
  276.     if (readfile(Filename, Filemem, FALSE))
  277.         filemess("[New File]");
  278.     } else {
  279.     s_refresh(NOT_VALID);
  280.     msg("Empty Buffer");
  281.     }
  282.  
  283.     setpcmark();
  284.  
  285.     if (tag) {
  286.     stuffReadbuff(":ta ");
  287.     stuffReadbuff(tag);
  288.     stuffReadbuff("\n");
  289.     } else if (pat) {
  290.     stuffReadbuff(pat);
  291.     stuffReadbuff("\n");
  292.     } else if (line >= 0) {
  293.     if (line > 0)
  294.         stuffnumReadbuff(line);
  295.     stuffReadbuff("G");
  296.     }
  297.     edit();
  298.     /* NOTREACHED */
  299.     /* windexit(0); */
  300. }
  301.  
  302. void
  303. stuffReadbuff(s)
  304.     char           *s;
  305. {
  306.     if (Readbuffptr == NULL) {
  307.     if ((strlen(s) + 1) < READSIZE) {
  308.         strcpy(Readbuff, s);
  309.         Readbuffptr = Readbuff;
  310.         return;
  311.     }
  312.     } else if ((strlen(Readbuff) + (strlen(s) + 1)) < READSIZE) {
  313.     strcat(Readbuff, s);
  314.     return;
  315.     }
  316.     emsg("Couldn't stuffReadbuff() - clearing Readbuff\n");
  317.     *Readbuff = NUL;
  318.     Readbuffptr = NULL;
  319. }
  320.  
  321. void
  322. stuffnumReadbuff(n)
  323.     int             n;
  324. {
  325.     char            buf[32];
  326.  
  327.     sprintf(buf, "%d", n);
  328.     stuffReadbuff(buf);
  329. }
  330.  
  331. /* OPTRESULT */
  332. char
  333. vgetc()
  334. {
  335.     int             c;
  336.  
  337.     /*
  338.      * inchar() may map special keys by using stuffReadbuff(). If it does so,
  339.      * it returns -1 so we know to loop here to get a real char. 
  340.      */
  341.     do {
  342.     if (Readbuffptr != NULL) {
  343.         char            nextc = *Readbuffptr++;
  344.  
  345.         if (*Readbuffptr == NUL) {
  346.         *Readbuff = NUL;
  347.         Readbuffptr = NULL;
  348.         }
  349.         return (nextc);
  350.     }
  351.     c = inchar();
  352.     } while (c == -1);
  353.  
  354.     return (char) c;
  355. }
  356.  
  357. char
  358. vpeekc()
  359. {
  360.     if (Readbuffptr != NULL)
  361.     return (*Readbuffptr);
  362.     return (NUL);
  363. }
  364.