scr2str.c 2.54 KB
Newer Older
bp's avatar
bp committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
/*
 * Screen to stream converter and vice versa
 * used to filter screen files for CVS
 * written in C, so that I can run it on systems without installing Forth
 */

#include <stdio.h>
#include <string.h>
#include <errno.h>

#define B_BLK 0x400
#define C_L 0x40

int backscan(char * block, int n, char c)
{
  for(n--; n>=0 && (block[n] == c); n--);
  return n < 0 ? 0 : n + (block[n] != c);
}

int ctrlmask(char * p, char * o, int n)
{
  int i, k=0;

  for(i=0; i<n; i++) {
    switch(p[i]) {
    case '\0' ... ' '-1:
      *o++ = '^';
      *o++ = p[i] + '@';
      k+=2;
      break;
    case '^':
      *o++ = '^';
      *o++ = '~';
      k+=2;
      break;
    default:
      *o++ = p[i];
      k++;
      break;
    }
  }
  return k;
}

int ctrlunmask(char * p, char * o, int n)
{
  int i, k=0;

  for(i=0; i<n; i++) {
    switch(p[i]) {
    case '^':
      if(p[++i] == '~') {
	*o++ = '^';
      } else {
	*o++ = p[i] - '@';
      }
      k++;
      break;
    default:
      *o++ = p[i];
      k++;
      break;
    }
  }
  return k;
}

void scr2str(FILE * in, FILE * out)
{
  char block[B_BLK];
  char line[C_L*2];
  int n, i, k;

  do {
    memset(block, ' ', B_BLK);
    n = fread(block, 1, B_BLK, in);
    if(n == 0) break;
    n = backscan(block, n, ' ');
    for(i=0; i < n; i+= C_L) {
      k = backscan(block+i, C_L, ' ');
      k = ctrlmask(block+i, line, k);
      fwrite(line, 1, k, out);
      fwrite("\n", 1, 1, out);
    }
    fwrite("\f\n", 2, 1, out);
  } while(1);
}

void str2scr(FILE * in, FILE * out)
{
  char block[B_BLK];
  char line[2*C_L+3];
  int n, i=0, k=0;

  memset(block, ' ', B_BLK);

  while(!feof(in)) {
    fgets(line, 2*C_L+2, in);
    if(feof(in)) break;
    if(!strcmp(line, "\f\n")) {
      fwrite(block, 1, B_BLK, out);
      memset(block, ' ', B_BLK);
      k = 0;
    } else {
      n = strlen(line);
      if(n)
	n -= (line[n-1]=='\n');
      n = ctrlunmask(line, line, n);
      memcpy(block+k, line, n);
      k += C_L;
    }
  }
}

int main(int argc, char ** argv, char ** env)
{
  int arglen = strlen(argv[0])-strlen("scr2str");
  int what = !strcmp(argv[0]+arglen, "scr2str");

  FILE * in = stdin;
  FILE * out = stdout;

  if(argc > 1) {
    if(strcmp(argv[1], "-")) {
      if(!(in = fopen(argv[1], "rb"))) {
	fprintf(stderr, "Error opening input file: %s\n", strerror(errno));
	return -1;
      }
    }
  }
  if(argc > 2) {
    if(!(out = fopen(argv[2], "wb"))) {
      fprintf(stderr, "Error opening output file: %s\n", strerror(errno));
      return -1;
    }
  }
  if(what)
    scr2str(in, out);
  else
    str2scr(in, out);

  return 0;
}