home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / games / volume8 / unidraw / part01 / command.c next >
C/C++ Source or Header  |  1989-12-19  |  4KB  |  149 lines

  1. #ifndef LINT
  2. static char COPYRIGHT[]="\
  3.   COPYRIGHT --- COPYRIGHT --- COPYRIGHT --- COPYRIGHT \n\
  4.   This program is copyright 1989 Nils McCarthy. This \
  5.   program may be distributed if it is impossible for \
  6.   the distributor to get a more up-to-date-version of \
  7.   it. \n\
  8.   COPYRIGHT --- COPYRIGHT --- COPYRIGHT --- COPYRIGHT \n\
  9.   ";
  10. static char AUTHOR[]="Copyright 1989 Nils McCarthy";
  11. #endif /* LINT */
  12.  
  13. #include "unidraw.h"
  14.  
  15. char *WinGet(output,mlen,chrset) /* Get a string from a window */
  16.  char *output;
  17.  int mlen;
  18.  char *chrset;
  19. {
  20.    char ch;
  21.    output[0]=NULL;
  22.    raw();
  23.    
  24.    while(strlen(output)<(mlen+1)) { /* so if there is too many chars, it
  25.                        just dumps them out. */
  26.       ch=getchar();
  27.       if(ch==8 || ch==10) /* Backspace */
  28.      if(strlen(output)>0) {
  29.         output[strlen(output)-1]=NULL;
  30.         printw("\b \b");
  31.         refresh();
  32.      } else {} /* Needed so the following else will take the
  33.               right if */
  34.       else
  35.      if(ch==13) { /* enter key */
  36.         return(output);
  37.      } else
  38.         if(chrset==NULL || index(chrset,ch)!=NULL) {
  39.            int l; /* Add on a character */
  40.            l=strlen(output);
  41.            output[l]=ch;
  42.            output[l+1]=NULL;
  43.            addch(ch);
  44.            refresh();
  45.         }
  46.    }
  47.    return(output);
  48.    printw("\n");
  49.    resetty();
  50. }
  51.  
  52. command(comm) /* execute a special command */
  53. char *comm;
  54. {
  55.    if(!strncmp(comm,"write ",6)) { /* Write to a file */
  56.       char **writebox;
  57.       FILE *fptr;
  58.       char *file;
  59.       char *linespace;
  60.       char *line;
  61.       int i,j;
  62.  
  63.       writebox=(char **) malloc(PADLINES*sizeof(*writebox));
  64.       for(i=0;i<PADCOLS;i++)
  65.      writebox[i]=malloc(PADCOLS*sizeof(**writebox));
  66.       linespace=malloc(PADCOLS*sizeof(*linespace));
  67.       line=linespace+1;
  68.       linespace[0]=NULL;
  69.       for(i=0;i<PADLINES;i++)
  70.      for(j=0;j<PADLINES;j++)
  71.         (writebox[i])[j]=pad(i,j);
  72.       file=index(comm,' ');
  73.       file+=1;
  74.       if(*file==NULL) return;
  75.       move(3,0);
  76.       printw("Writing file... ");
  77.       refresh();
  78.       fptr=fopen(file,"w");
  79.       if(fptr==NULL) {
  80.      move(3,0);
  81.      printw("ERROR: FILE NOT WRITABLE!!! ");
  82.      refresh();
  83.      return;
  84.       }
  85.       line[PADCOLS]=NULL;
  86.       for(i=0;i<PADLINES;i++) {
  87.      for(j=0;j<PADCOLS;j++) /* Take of ending spaces*/
  88.         line[j]=pad(i,j);
  89.      while(line[strlen(line)-1]==' ' && strlen(line)>0)
  90.         line[strlen(line)-1]=NULL;
  91.      strcpy(writebox[i],line);
  92.      strcat(writebox[i],"\n");
  93.       }
  94.       for(i=PADLINES-1;(writebox[i])[0]=='\n' && i>=0;i--)
  95.      (writebox[i])[0]=NULL; /* take of ending blank lines */
  96.       for(i=0;i<PADLINES;i++)
  97.      fprintf(fptr,writebox[i]);
  98.       fclose(fptr);
  99.    }
  100.    if(!strncmp(comm,"read ",5)) { /* READ FILE */
  101.       FILE *fptr;
  102.       char *file;
  103.       int i,j;
  104.       char ch;
  105.       
  106.       move(3,0);
  107.       file=index(comm,' ')+1;
  108.       if(*file==NULL) return;
  109.       move(3,0);
  110.       if((fptr=fopen(file,"r"))==NULL) {
  111.      printw("CANNOT OPEN FILE");
  112.      refresh();
  113.      return;
  114.       }
  115.       printw("Reading...");
  116.       refresh();
  117.       for(i=0;i<PADLINES;i++)
  118.      for(j=0;j<PADCOLS;j++)
  119.         pad(i,j)=' ';
  120.       i=0;j=0;
  121.       while(j<PADLINES) {
  122.      if((ch=fgetc(fptr))==EOF)
  123.         return;
  124.      if(ch=='\n' || i>PADCOLS) { /* next line */
  125.         j++;
  126.         i=0;
  127.      } else {
  128.         pad(j,i)=ch;
  129.         i++;
  130.      }
  131.       }
  132.       redraw();
  133.    }
  134.    if(!strcmp(comm,"quit")) /* put the break key in the buffer */
  135.       ungetc(brk,stdin);
  136.    if(!strcmp(comm,"suspend")) /* put the suspend key in the buffer */
  137.       ungetc(suspend,stdin);
  138.    if(!strcmp(comm,"fill")) /* fill area */
  139.       fill(padx,pady);
  140.    if(!strcmp(comm,"clear")) { /* clear area */
  141.       int i,j;
  142.       for(i=0;i<PADCOLS;i++)
  143.      for(j=0;j<PADLINES;j++)
  144.         pad(j,i)=' ';
  145.    }
  146.    if(!strcmp(comm,"copy")) /* copyright notice */
  147.       printw(COPYRIGHT);
  148. }
  149.