home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / os2 / stevi0s2 / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-06-09  |  6.9 KB  |  307 lines

  1. /*
  2.  * STEVIE - ST Editor for VI Enthusiasts   ...Tim Thompson...twitch!tjt...
  3.  *
  4.  * Extensive modifications by:  Tony Andrews       onecom!wldrdg!tony
  5.  *
  6.  */
  7.  
  8. #include "stevie.h"
  9.  
  10. int Rows;        /* Number of Rows and Columns */
  11. int Columns;        /* in the current window. */
  12.  
  13. char *Realscreen = NULL;    /* What's currently on the screen, a single */
  14.                 /* array of size Rows*Columns. */
  15. char *Nextscreen = NULL;    /* What's to be put on the screen. */
  16.  
  17. char *Filename = NULL;    /* Current file name */
  18.  
  19. LPTR *Filemem;        /* The contents of the file, as a single array. */
  20.  
  21. LPTR *Fileend;        /* Pointer to the end of the file in Filemem. */
  22.             /* (It points to the byte AFTER the last byte.) */
  23.  
  24. LPTR *Topchar;        /* Pointer to the byte in Filemem which is */
  25.             /* in the upper left corner of the screen. */
  26.  
  27. LPTR *Botchar;        /* Pointer to the byte in Filemem which is */
  28.             /* just off the bottom of the screen. */
  29.  
  30. LPTR *Curschar;        /* Pointer to byte in Filemem at which the */
  31.             /* cursor is currently placed. */
  32.  
  33. int Cursrow, Curscol;    /* Current position of cursor */
  34.  
  35. int Cursvcol;        /* Current virtual column, the column number of */
  36.             /* the file's actual line, as opposed to the */
  37.             /* column number we're at on the screen.  This */
  38.             /* makes a difference on lines that span more */
  39.             /* than one screen line. */
  40.  
  41. int Curswant = 0;    /* The column we'd like to be at. This is used */
  42.             /* try to stay in the same column through up/down */
  43.             /* cursor motions. */
  44.  
  45. bool_t set_want_col;    /* If set, then update Curswant the next time */
  46.             /* through cursupdate() to the current virtual */
  47.             /* column. */
  48.  
  49. int State = NORMAL;    /* This is the current state of the command */
  50.             /* interpreter. */
  51.  
  52. int Prenum = 0;        /* The (optional) number before a command. */
  53.  
  54. LPTR *Insstart;        /* This is where the latest insert/append */
  55.             /* mode started. */
  56.  
  57. bool_t Changed = 0;    /* Set to 1 if something in the file has been */
  58.             /* changed and not written out. */
  59.  
  60. bool_t Debug = 0;
  61.  
  62. char Redobuff[1024];    /* Each command should stuff characters into this */
  63.             /* buffer that will re-execute itself. */
  64.  
  65. char Undobuff[1024];    /* Each command should stuff characters into this */
  66.             /* buffer that will undo its effects. */
  67.  
  68. char Insbuff[1024];    /* Each insertion gets stuffed into this buffer. */
  69.  
  70. LPTR *Uncurschar;    /* Curschar is restored to this before undoing. */
  71.  
  72. int Ninsert = 0;    /* Number of characters in the current insertion. */
  73. int Undelchars = 0;    /* Number of characters to delete, when undoing. */
  74. char *Insptr = NULL;
  75.  
  76. char **files;        /* list of input files */
  77. int  numfiles;        /* number of input files */
  78. int  curfile;        /* number of the current file */
  79.  
  80. static void
  81. usage()
  82. {
  83.     fprintf(stderr, "usage: stevie [file ...]\n");
  84.     fprintf(stderr, "       stevie -t tag\n");
  85.     fprintf(stderr, "       stevie +[num] file\n");
  86.     fprintf(stderr, "       stevie +/pat  file\n");
  87.     exit(1);
  88. }
  89.  
  90. main(argc,argv)
  91. int    argc;
  92. char    *argv[];
  93. {
  94.     char    *initstr, *getenv();    /* init string from the environment */
  95.     char    *tag = NULL;        /* tag from command line */
  96.     char    *pat = NULL;        /* pattern from command line */
  97.     int    line = -1;        /* line number from command line */
  98.  
  99.     /*
  100.      * Process the command line arguments.
  101.      */
  102.     if (argc > 1) {
  103.         switch (argv[1][0]) {
  104.         
  105.         case '-':            /* -t tag */
  106.             if (argv[1][1] != 't')
  107.                 usage();
  108.  
  109.             if (argv[2] == NULL)
  110.                 usage();
  111.  
  112.             Filename = NULL;
  113.             tag = argv[2];
  114.             numfiles = 1;
  115.             break;
  116.  
  117.         case '+':            /* +n or +/pat */
  118.             if (argv[1][1] == '/') {
  119.                 if (argv[2] == NULL)
  120.                     usage();
  121.                 Filename = strsave(argv[2]);
  122.                 pat = &(argv[1][1]);
  123.                 numfiles = 1;
  124.  
  125.             } else if (isdigit(argv[1][1]) || argv[1][1] == NUL) {
  126.                 if (argv[2] == NULL)
  127.                     usage();
  128.                 Filename = strsave(argv[2]);
  129.                 numfiles = 1;
  130.  
  131.                 line = (isdigit(argv[1][1])) ?
  132.                     atoi(&(argv[1][1])) : 0;
  133.             } else
  134.                 usage();
  135.  
  136.             break;
  137.  
  138.         default:            /* must be a file name */
  139.             Filename = strsave(argv[1]);
  140.             files = &(argv[1]);
  141.             numfiles = argc - 1;
  142.             break;
  143.         }
  144.     } else {
  145.         Filename = NULL;
  146.         numfiles = 1;
  147.     }
  148.     curfile = 0;
  149.  
  150.     windinit();
  151.  
  152.     /*
  153.      * Allocate LPTR structures for all the various position pointers
  154.      */
  155.     if ((Filemem = (LPTR *) malloc(sizeof(LPTR))) == NULL) {
  156.         fprintf(stderr, "Can't allocate data structures\n");
  157.         windexit(0);
  158.     }
  159.     if ((Fileend = (LPTR *) malloc(sizeof(LPTR))) == NULL) {
  160.         fprintf(stderr, "Can't allocate data structures\n");
  161.         windexit(0);
  162.     }
  163.     if ((Topchar = (LPTR *) malloc(sizeof(LPTR))) == NULL) {
  164.         fprintf(stderr, "Can't allocate data structures\n");
  165.         windexit(0);
  166.     }
  167.     if ((Botchar = (LPTR *) malloc(sizeof(LPTR))) == NULL) {
  168.         fprintf(stderr, "Can't allocate data structures\n");
  169.         windexit(0);
  170.     }
  171.     if ((Curschar = (LPTR *) malloc(sizeof(LPTR))) == NULL) {
  172.         fprintf(stderr, "Can't allocate data structures\n");
  173.         windexit(0);
  174.     }
  175.     if ((Insstart = (LPTR *) malloc(sizeof(LPTR))) == NULL) {
  176.         fprintf(stderr, "Can't allocate data structures\n");
  177.         windexit(0);
  178.     }
  179.     if ((Uncurschar = (LPTR *) malloc(sizeof(LPTR))) == NULL) {
  180.         fprintf(stderr, "Can't allocate data structures\n");
  181.         windexit(0);
  182.     }
  183.  
  184.     screenalloc();
  185.     filealloc();        /* Initialize Filemem & Fileend */
  186.  
  187.     screenclear();
  188.  
  189.     if ((initstr = getenv("EXINIT")) != NULL) {
  190.         char *lp, buf[128];
  191.  
  192.         if ((lp = getenv("LINES")) != NULL) {
  193.             sprintf(buf, "%s lines=%s", initstr, lp);
  194.             readcmdline(':', buf);
  195.         } else
  196.             readcmdline(':', initstr);
  197.     }
  198.  
  199.     if (Filename != NULL) {
  200.         if (readfile(Filename, Filemem, FALSE))
  201.             filemess("[New File]");
  202.     } else
  203.         msg("Empty Buffer");
  204.  
  205.     setpcmark();
  206.  
  207.     updatescreen();
  208.     
  209.     if (tag) {
  210.         stuffin(":ta ");
  211.         stuffin(tag);
  212.         stuffin("\n");
  213.  
  214.     } else if (pat) {
  215.         stuffin(pat);
  216.         stuffin("\n");
  217.  
  218.     } else if (line >= 0) {
  219.         if (line > 0)
  220.             stuffnum(line);
  221.         stuffin("G");
  222.     }
  223.  
  224.     edit();
  225.  
  226.     windexit(0);
  227. }
  228.  
  229. #define    RBSIZE    1280        /* should be a little bigger than YBSIZE */
  230. static char getcbuff[RBSIZE];
  231. static char *getcnext = NULL;
  232.  
  233. void
  234. stuffin(s)
  235. char *s;
  236. {
  237.     if ( getcnext == NULL ) {
  238.         strcpy(getcbuff,s);
  239.         getcnext = getcbuff;
  240.     } else
  241.         strcat(getcbuff,s);
  242. }
  243.  
  244. void
  245. stuffnum(n)
  246. int    n;
  247. {
  248.     char    buf[32];
  249.  
  250.     sprintf(buf, "%d", n);
  251.     stuffin(buf);
  252. }
  253.  
  254. void
  255. addtobuff(s,c1,c2,c3,c4,c5,c6)
  256. char *s;
  257. char c1, c2, c3, c4, c5, c6;
  258. {
  259.     char *p = s;
  260.     if ( (*p++ = c1) == NUL )
  261.         return;
  262.     if ( (*p++ = c2) == NUL )
  263.         return;
  264.     if ( (*p++ = c3) == NUL )
  265.         return;
  266.     if ( (*p++ = c4) == NUL )
  267.         return;
  268.     if ( (*p++ = c5) == NUL )
  269.         return;
  270.     if ( (*p++ = c6) == NUL )
  271.         return;
  272. }
  273.  
  274. int
  275. vgetc()
  276. {
  277.     if ( getcnext != NULL ) {
  278.         int nextc = *getcnext++;
  279.         if ( *getcnext == NUL ) {
  280.             *getcbuff = NUL;
  281.             getcnext = NULL;
  282.         }
  283.         return(nextc);
  284.     }
  285.     return(inchar());
  286. }
  287.  
  288. int
  289. vpeekc()
  290. {
  291.     if ( getcnext != NULL )
  292.         return(*getcnext);
  293.     return(-1);
  294. }
  295.  
  296. /*
  297.  * anyinput
  298.  *
  299.  * Return non-zero if input is pending.
  300.  */
  301.  
  302. bool_t
  303. anyinput()
  304. {
  305.     return (getcnext != NULL);
  306. }
  307.