home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume2 / sbs < prev    next >
Internet Message Format  |  1991-08-07  |  7KB

  1. From: nelson@MCNC.ORG (jim nelson)
  2. Newsgroups: comp.sources.misc
  3. Subject: v02i006: Side-by-side file paginator
  4. Message-ID: <7080@ncoast.UUCP>
  5. Date: 15 Jan 88 00:42:01 GMT
  6. Approved: allbery@ncoast.UUCP
  7.  
  8. Comp.Sources.Misc: Volume 2, Issue 6
  9. Submitted-By: jim nelson <nelson%mcnc.org@ecsvax.UUCP>
  10. Archive-Name: sbs
  11.  
  12. [Whatever happened to "pr -m -w80 file1 file2 | more"?  ++bsa]
  13.  
  14. This is a little thingie I (thought) I had to write myself, because
  15. it warnt available.  Presumably I re-invented some wheel; but I
  16. like it.  Pub Dom.  Take it.  If it belongs in comp.sources.misc,
  17. I'll send it over there... it's short.
  18. #  --- cut here ---
  19. #/bin/sh
  20. #This is a shar file.  To use:
  21. #  1. Remove everything before the /bin/sh line
  22. #  2. Execute with /bin/sh (not csh) to extract the files:
  23. #         Makefile
  24. #         sbs.c
  25. file="${0}"
  26. echo extracting Makefile 1>&2
  27. sed -e 's/^X//'  >Makefile << 'EnD of Makefile'
  28. XCFLAGS=
  29. XLDFLAGS=-lcurses #-ltermlib
  30. Xsbs: sbs.o
  31. X    cc sbs.o -o sbs $(LDFLAGS)
  32. EnD of Makefile
  33. echo extracting sbs.c 1>&2
  34. sed -e 's/^X//'  >sbs.c << 'EnD of sbs.c'
  35. X/*puts two files side-by-side on terminal */
  36. X/*for poor folks who don't have windows and suns and all that
  37. Xother stuff*/
  38. X/*no man page: it's not worth it; but i did make a Makefile
  39. Xof sorts*/
  40. X/*this compiles and works perfectly with no alterations on both
  41. XSysVr2(3b2/300) and BSD4.1-derived-GENIX on a nsc16032(lmc)*/
  42. X/*other systems at own risk ...*/
  43. X/*'taint so hot at 1200 baud, but presumably i'm the only
  44. Xperson on earth who still has to put up with measly 1200 baud*/
  45. X/*no copyright, public domain, not worth the effort*/
  46. X/*author: jim nelson, unc-wilmington, wilmington nc 28403;
  47. X919-395-3300*/
  48. X#include <curses.h>
  49. X#define UNIX
  50. X#include <stdio.h>
  51. X#define MAXLINE 82
  52. X#ifdef UNIX
  53. X#include <ctype.h>
  54. X#endif
  55. X/*global variables: */
  56. Xchar left[20][MAXLINE],rite[20][MAXLINE],lft[MAXLINE],rte[MAXLINE];
  57. Xint efl,efr;
  58. Xchar leader[MAXLINE];
  59. Xchar *fnl,*fnr;
  60. X/*end of global variables */
  61. X
  62. Xmain(argc,argv)
  63. Xint argc;
  64. Xchar *argv[];
  65. X
  66. X{
  67. X    FILE *fp1,*fp2;
  68. X    int jhn,prefix,zork,c,i,k,lp,rp;
  69. X    efl=efr=0;
  70. X    fnl=argv[1]; 
  71. X    fnr=argv[2];
  72. X
  73. X    if(argc!=3){
  74. X        printf("usage: sbs file1 file2\n"); 
  75. X        exit (1); 
  76. X    }
  77. X
  78. X
  79. X    if(  (fp2=fopen(argv[2],"r"))  == NULL)
  80. X    {
  81. X        printf("not found:%s\n",argv[2]);
  82. X        exit (1);
  83. X    }
  84. X
  85. X    if(  (fp1=fopen(argv[1],"r"))  == NULL)
  86. X    {
  87. X        printf("not found:%s\n",argv[1]);
  88. X        exit (1);
  89. X    }
  90. X
  91. X    initscr();
  92. X    clear();
  93. X    nocrmode();
  94. X    noecho();
  95. X    raw();
  96. X    lp = rp = (-1);
  97. X    for (k=0;fnl[k];){
  98. X        leader[k]=fnl[k];
  99. X        k++;
  100. X    }
  101. X    while(k<39)leader[k++]='-';
  102. X    leader[k++]=' ';
  103. X    leader[k++]='|';
  104. X    leader[k++]=' ';
  105. X    for(i=0;fnr[i];)leader[k++]=fnr[i++];
  106. X    while(k<75)leader[k++]='-';
  107. X    leader[k]='\0';
  108. X    for(i=0;i<20;i++){
  109. X
  110. X        if(!efl){
  111. X            lp = (lp+1)%20;
  112. X            if(!efl)if(fgets(left[lp],MAXLINE,fp1)==NULL)efl=1;
  113. X            if(efl)strcpy(left[lp],"[EOF]\n");
  114. X        }
  115. X
  116. X        if(!efr){
  117. X            rp = (rp+1)%20;
  118. X            if(!efr)if(fgets(rite[rp],MAXLINE,fp2)==NULL)efr=1;
  119. X            if(efr)strcpy(rite[rp],"[EOF]\n");
  120. X        }
  121. X    }
  122. X    display(lp,rp,' '); /* display is supposed to be fixed up so that
  123. X                the lpth line from left and the rpth line from rite
  124. X                will be the last ones displayed */
  125. X    while(1)
  126. X    {
  127. X
  128. X        prefix=0;
  129. X        lbl: c=getch()&0177;
  130. X        if(c>='0' && c<='9'){prefix=prefix*10+c-'0';goto lbl;}    
  131. X        if(prefix==0)prefix=1;
  132. X
  133. X        if(c==('c'&037)||c==('y'&037)){
  134. X            endwin();
  135. X            exit();
  136. X        }
  137. X        if(isupper(c))c=tolower(c);
  138. X        if(c=='t')c='y';
  139. X        if(c=='j')c='f';
  140. X
  141. X        if(c==';'||c=='f')
  142. X        for(jhn=0;jhn<prefix;jhn++)
  143. X        {
  144. X            rp = (rp+1) % 20;
  145. X            if(!efr)if(fgets(rite[rp],MAXLINE,fp2)==NULL)efr=1;
  146. X            if(efr)strcpy(rite[rp],"[EOF]\n");
  147. X        }
  148. X
  149. X        if (c=='a'||c=='f')
  150. X        for(jhn=0;jhn<prefix;jhn++)
  151. X        {
  152. X            lp = (lp+1) % 20;
  153. X            if(!efl)if(fgets(left[lp],MAXLINE,fp1)==NULL)efl=1;
  154. X            if(efl)strcpy(left[lp],"[EOF]\n");
  155. X        }
  156. X        if (c=='q'||c=='y'||c=='d')
  157. X        for(jhn=0;jhn<prefix;jhn++)
  158. X        {
  159. X            for(i=0;i<20;i++){
  160. X                lp = (lp+1)%20;
  161. X                if(!efl)if(fgets(left[lp],MAXLINE,fp1)==NULL)efl=1;
  162. X                if(efl)strcpy(left[lp],"[EOF]\n");
  163. X            }
  164. X        }
  165. X
  166. X        if (c=='p'||c=='y'||c=='d')
  167. X        for(jhn=0;jhn<prefix;jhn++)
  168. X        {
  169. X            for(i=0;i<20;i++){
  170. X                rp = (rp+1)%20;
  171. X                if(!efr)if(fgets(rite[rp],MAXLINE,fp2)==NULL)efr=1;
  172. X                if(efr)strcpy(rite[rp],"[EOF]\n");
  173. X            }
  174. X        }
  175. X
  176. X        if (c=='d')
  177. X        for(jhn=0;jhn<prefix;jhn++)
  178. X        { 
  179. X            zork=0;
  180. X
  181. X            for(i=0;i<20;i++){
  182. X                lp = (lp+1)%20;
  183. X                rp = (rp+1)%20;
  184. X                if(!zork)if(jstrcmp(left[lp],rite[rp])!=0)zork=1;
  185. X            }
  186. X            while( !zork ) {
  187. X                if(!efr){
  188. X                    rp = (rp+1)%20;
  189. X                    if(!efr)
  190. X                        if(fgets(rite[rp],MAXLINE,fp2)==NULL)efr=1;
  191. X                    if(efr)strcpy(rite[rp],"[EOF]\n");
  192. X                }
  193. X                if(!efl){
  194. X                    lp = (lp+1)%20;
  195. X                    if(!efl)
  196. X                        if(fgets(left[lp],MAXLINE,fp1)==NULL)efl=1;
  197. X                    if(efl)strcpy(left[lp],"[EOF]\n");
  198. X                }
  199. X                if(efl && efr)break;
  200. X                zork=   jstrcmp(left[lp],rite[rp])   !=0 ;
  201. X            }
  202. X        }
  203. X        display(lp,rp,c);
  204. X    }
  205. X}
  206. X
  207. Xfixline(p) char *p;
  208. X{
  209. X    int j;
  210. X    char *q;
  211. X    q=p;
  212. X    for(j=0;j<37;j++){
  213. X        if(*p=='\n')break; 
  214. X        if(*p=='\t') *p=' '; 
  215. X        p++;
  216. X    }
  217. X    if(q==lft)while(j<37){
  218. X        *p++ = ' ';
  219. X        j++;
  220. X    }
  221. X    else q[37]='\0';
  222. X    *p='\0';
  223. X}
  224. X
  225. Xdisplay(p,q,c) int p,q; 
  226. Xchar c;
  227. X{
  228. X    int i,zork,cnt;
  229. X
  230. X    move(0,0);
  231. X    for(i=0;i<20;i++)
  232. X    {
  233. X
  234. X        p = (p+1) % 20; 
  235. X        q = (q+1) % 20;
  236. X        zork=jstrcmp(left[p],rite[q]);
  237. X        if(c!='-' && c!='='){
  238. X            strcpy(lft,left[p],38);
  239. X            strcpy(rte,rite[q],38);
  240. X        }
  241. X        else {  /*fgets allows the \n in the string, and
  242. X                            appends a \0 after it.  strlen therefore will
  243. X                            count the \n in a string that has it.*/
  244. X            if(c=='-')cnt=37;
  245. X            if(c=='=')cnt=74;
  246. X            if(strlen(left[p])>cnt)
  247. X                strcpy(lft,&(left[p][cnt]),38);
  248. X            else
  249. X                strcpy(lft,"\n",2);
  250. X            if(strlen(rite[q])>cnt)
  251. X                strcpy(rte,&(rite[q][cnt]),38);
  252. X            else
  253. X                strcpy(rte,"\n",2);
  254. X        }/*endelse*/
  255. X
  256. X        fixline(lft); 
  257. X        fixline(rte);
  258. X        printw("%c %s | %s\n",zork?'+':' ',lft,rte);
  259. X
  260. X    }
  261. X
  262. X
  263. X    printw("%s\n",leader);
  264. X    printw(
  265. X    "a:upl, ;:upr, q:pgl, p:pgr, t|y:pgboth, f|j:upboth, d:gotodif, -|=:horiz\n");
  266. X    printw(
  267. X    "---------------------------------------------------------------------------\n"
  268. X        );
  269. X    refresh();
  270. X
  271. X
  272. X}
  273. X#define white(c) ((c)==' ' || (c)=='\t')
  274. Xjstrcmp(s1,s2)
  275. Xchar *s1,*s2;
  276. X{
  277. X    while(*s1&&*s2)
  278. X    {    
  279. X        if(white(*s1)&&white(*s2)){
  280. X            while(white(*s1))s1++;    
  281. X            while(white(*s2))s2++;
  282. X            continue;
  283. X        }
  284. X        if(*s1==*s2){s1++;s2++;continue;}
  285. X        return(*s1 - *s2);
  286. X    }
  287. X    if(*s1=='\0' && *s2!='\0')return(-1);
  288. X    if(*s1!='\0' && *s2=='\0')return(1);
  289. X    if(*s1=='\0' && *s2=='\0')return(0);
  290. X    exit(1);/*disaster*/
  291. X}
  292. EnD of sbs.c
  293.