home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume26 / pico / part01 / main.c < prev    next >
C/C++ Source or Header  |  1991-12-14  |  2KB  |  93 lines

  1. #include <setjmp.h>
  2. #include <string.h>
  3. #include "pico.h"
  4.  
  5. int debug, saw_eof;
  6. Tree *tree;
  7.  
  8. static void run(Tree *t);
  9.  
  10. static disp = 1;
  11. static jmp_buf jb;
  12.  
  13. extern int main(int argc, char *argv[]) {
  14.     extern int optind;
  15.     int c, err = 0;
  16.  
  17.     while ((c = getopt(argc, argv, "dn")) != -1)
  18.         if (c == 'd')
  19.             debug = 1;
  20.         else if (c == 'n')
  21.             disp = 0;
  22.         else
  23.             err = 1;
  24.     if (err || argc - optind != 0) {
  25.         fprintf(stderr, "usage: pico [-nd]");
  26.         exit(1);
  27.     }
  28.     buf[0].data = ealloc(DEF_X * DEF_Y);
  29.     buf[1].data = ealloc(DEF_X * DEF_Y);
  30.     setjmp(jb);
  31.     while (1) {
  32.         fprintf(stderr, "> ");
  33.         fflush(stderr);
  34.         yyparse();
  35.         if (saw_eof)
  36.             return 0;
  37.         if (tree != NULL)
  38.             run(tree);
  39.         afree();
  40.     }
  41. }
  42.  
  43. #define swap(x,y) {\
  44.     unsigned char *tmp = x;\
  45.     x = y;\
  46.     y = tmp;\
  47. }
  48.  
  49. static void run(Tree *t) {
  50.     int f;
  51.     switch (t->t) {
  52.     case Quit:
  53.         exit(0);
  54.     case Undo:
  55.         swap(buf[0].data, buf[1].data);
  56.         if (disp)
  57.             display(buf[0].data);
  58.         break;
  59.     case Read:
  60.         if ((f = readfile(t->kids[0])) > 0) {
  61.             swap(buf[0].data, buf[1].data);
  62.             memcpy(buf[0].data, buf[f].data, DEF_X * DEF_Y);
  63.             if (disp)
  64.                 display(buf[0].data);
  65.         }
  66.         break;
  67.     case Write:
  68.         writefile(t->kids[0]);
  69.         break;
  70.     case Files:
  71.         showfiles();
  72.         break;
  73.     default:
  74.         swap(buf[0].data, buf[1].data);
  75.         compile(t);
  76. #ifdef sgi
  77.         if (debug)
  78.             system("dis pico.out");
  79. #else
  80.         if (debug)
  81.             system("echo ',-1?' | adb " DUMPFILE);
  82. #endif
  83.         if (disp)
  84.             display(buf[0].data);
  85.         break;
  86.     }
  87. }
  88.  
  89. extern void error(char *s) {
  90.     fprintf(stderr, "%s\n", s);
  91.     longjmp(jb,1);
  92. }
  93.