home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 1 / ARM_CLUB_CD.iso / contents / apps / program / d / elvis / Source / c / arc next >
Encoding:
Text File  |  1990-04-15  |  6.9 KB  |  469 lines

  1. /*
  2.  *    Archimedes specific file - M. Andreasen 1990
  3.  */
  4.  
  5. #include "vi.h"
  6. #include <string.h>
  7. #include <kernel.h>
  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10.  
  11. /*    this file is needed since the arc does not have a escape sequence
  12.  *    type of screen handler. 
  13.  */
  14.  
  15. static char str[256],gotos[20],_ESC[10],work[80];
  16. static int _ESC_SET=0;
  17. static char col_f=-1,col_b=-1;
  18.  
  19. /* find the cursor postion - needed for inserts and delete where a window is
  20.  * defined and then scrolled in the required direction
  21.  */
  22.         
  23. static void convert(path)
  24. char *path;
  25. {
  26.     char    s[256];
  27.     int    len,i;
  28.  
  29.     len=strlen(path);
  30.     if (path[len-2]!='.' || len<3) return;
  31.     i=len-3;
  32.     path[len-2]=NULL;
  33.     while (path[i]!='.' && i>=0) --i;
  34.     if (i<0) {
  35.         sprintf(s,"%c.%s",path[len-1],path);
  36.     } else {
  37.         path[i]=NULL;
  38.         sprintf(s,"%s.%c.%s",path,path[len-1],path+i+1);
  39.     }
  40.     strcpy(path,s);
  41. }
  42.  
  43. void get_pos(x,y)
  44. int *x,*y;
  45. {
  46.       int r;
  47.  
  48.       r=_kernel_osbyte(134,0,0);
  49.       *x=r&0xff;
  50.       *y=(r>>8)&0xff;
  51. }
  52.  
  53. /* used for creating unique filenames. multi user systems ? */
  54.  
  55. int rnd()
  56. {
  57.     return(rand()&0xfff);
  58. }
  59.  
  60. /* Arch version of an escape sequence */
  61.  
  62. void vdu(char *seq)
  63. {
  64.     char *p;
  65.     int x;
  66.  
  67.     p=strtok(seq,",");
  68.     while (p!=NULL) {
  69.         x=atoi(p);
  70.         _kernel_oswrch(x);
  71.         p=strtok(NULL,",");
  72.     }
  73. }
  74.  
  75. /* insert character */
  76.  
  77. void A_IC()
  78. {
  79.       int x,y;
  80.  
  81.       get_pos(&x,&y);
  82.       sprintf(str,"28,%d,%d,%d,%d,23,7,0,0,0,0,0,0,0,0,26,31,%d,%d",x,y,79,y,x,y);
  83.     vdu(str);
  84.     _ESC_SET=0;
  85. }
  86.  
  87. /* delete character */
  88.  
  89. void A_DC()
  90. {
  91.       int x,y;
  92.  
  93.       get_pos(&x,&y);
  94.       sprintf(str,"28,%d,%d,%d,%d,23,7,0,1,0,0,0,0,0,0,26,31,%d,%d",x,y,79,y,x,y);
  95.     vdu(str);
  96.     _ESC_SET=0;
  97. }
  98.  
  99. /* insert line */
  100.  
  101. void A_IL()
  102. {
  103.       int x,y;
  104.  
  105.       get_pos(&x,&y);
  106.       sprintf(str,"28,%d,%d,%d,%d,23,7,0,2,0,0,0,0,0,0,26,31,%d,%d",0,31,79,y,x,y);
  107.     vdu(str);
  108.     _ESC_SET=0;
  109. }
  110.  
  111. /* delete line */
  112.  
  113. void A_DL()
  114. {
  115.       int x,y;
  116.  
  117.       get_pos(&x,&y);
  118.       sprintf(str,"28,%d,%d,%d,%d,23,7,0,3,0,0,0,0,0,0,26,31,%d,%d",0,31,79,y,x,y);
  119.     vdu(str);
  120.     _ESC_SET=0;
  121. }
  122.  
  123. /* clear to end of line */
  124.  
  125. void A_CE()
  126. {
  127.     sprintf(str,"23,8,5,6,0,0,0,0,0,0");
  128.     vdu(str);
  129.     _ESC_SET=0;
  130. }
  131.  
  132. /* scroll reverse */
  133.  
  134. void A_SR()
  135. {
  136.     sprintf(str,"23,7,1,2,0,0,0,0,0,0");
  137.     vdu(str);
  138.     _ESC_SET=0;
  139. }
  140.  
  141. /* clear screen and home */
  142.  
  143. void A_CL()
  144. {
  145.     sprintf(str,"12,30");
  146.     vdu(str);
  147.     _ESC_SET=0;
  148. }
  149.  
  150. /* visual bell - not very visable ? */
  151.  
  152. void A_VB()
  153. {
  154.     _kernel_oswrch(7);
  155.     _ESC_SET=0;
  156. }
  157.  
  158. /* you guessed it - up ! */
  159.  
  160. void A_UP()
  161. {
  162.     _kernel_oswrch(11);
  163.     _ESC_SET=0;
  164. }
  165.  
  166. /* process a two character escape code */
  167.  
  168. void check_esc1()
  169. {
  170.     if (strcmp("VB",_ESC)==0) A_VB();
  171.     if (strcmp("UP",_ESC)==0) A_UP();
  172.     if (strcmp("CE",_ESC)==0) A_CE();
  173.     if (strcmp("CL",_ESC)==0) A_CL();
  174.     if (strcmp("DL",_ESC)==0) A_DL();
  175.     if (strcmp("SR",_ESC)==0) A_SR();
  176.     if (strcmp("IL",_ESC)==0) A_IL();
  177.     if (strcmp("IC",_ESC)==0) A_IC();
  178.     if (strcmp("DC",_ESC)==0) A_DC();
  179. }
  180.  
  181. /* a cursor movement sequence or an invalid sequence */
  182.  
  183. void check_esc2()
  184. {
  185.     int x,y;
  186.  
  187.     if (strncmp("CM",_ESC,2)!=0) {
  188.         printf("BAD escape sequence (%s)\n",_ESC);
  189.         exit(1);
  190.     } else {
  191.         x=_ESC[2]-' ';
  192.         y=_ESC[3]-' ';
  193.         _kernel_oswrch(31);
  194.         _kernel_oswrch(x);
  195.         _kernel_oswrch(y);
  196.         _ESC_SET=0;
  197.     }
  198. }
  199.  
  200. /* output a character or build an escape sequence */
  201.  
  202. void outchar(c)
  203. char c;
  204. {
  205.     int l;
  206.  
  207.     if (o_colourf[0]!=col_f) {
  208.         col_f=o_colourf[0];
  209.         _kernel_oswrch(17);
  210.         _kernel_oswrch(col_f);
  211.     }
  212.     if (o_colourb[0]!=col_b) {
  213.         col_b=o_colourb[0];
  214.         _kernel_oswrch(17);
  215.         _kernel_oswrch(col_b+128);
  216.     }
  217.     if (c==27) {
  218.         _ESC[0]=NULL;
  219.         _ESC_SET=1;
  220.     } else {
  221.         if (_ESC_SET) {
  222.             l=strlen(_ESC);
  223.             _ESC[l]=c;
  224.             _ESC[l+1]=NULL;
  225.             if (l==1) check_esc1();
  226.             if (l==3) check_esc2();
  227.         } else {
  228.             _kernel_oswrch(c);
  229.         }
  230.     }
  231. }
  232.  
  233.  
  234. /* this is the UNIX hook to the termial infomation required in the 
  235.  * curses library. this is hardwired for the Arc. */
  236.  
  237. char *tputs(s,lines,func)
  238. void func(char);
  239. int lines;
  240. char *s;
  241. {
  242.       register int x=0;
  243.  
  244.     while (s[x]!=NULL) {
  245.         func(s[x]);
  246.         x++;
  247.     }
  248.     return(s);
  249. }
  250.  
  251. char *tgoto(a,x,y)
  252. char *a;
  253. int x,y;
  254. {
  255.       sprintf(gotos,"\033CM%c%c",' '+x,' '+y);
  256.       return(gotos);
  257. }
  258.  
  259. void ARC_keyword(word)
  260. char *word;
  261. {
  262.     sprintf(work,"%s %s",o_keywordprg,word);
  263.     system(work);
  264. }
  265.  
  266. /*     the following are functions that are called from various parts of
  267.  *    vi. I don't know what they are do, but most of them appear to
  268.  *     be in connection with system() and piping between programs - not
  269.  *     on the arc
  270.  */
  271.  
  272.  /* this should really be implemented !! */
  273.  
  274. int dup()
  275. {
  276.     return(-1);
  277. }
  278.  
  279. link()
  280. {
  281.     printf("Abort - link called\n");
  282.     exit(1);
  283. }
  284.  
  285. execl()
  286. {
  287.     printf("Abort - execl called\n");
  288.     exit(1);
  289. }
  290.  
  291. fstat()
  292. {
  293.     printf("Abort - fstat called\n");
  294.     exit(1);
  295. }
  296.  
  297. alarm()
  298. {
  299.     printf("Abort - alarm called\n");
  300.     exit(1);
  301. }
  302.  
  303. pipe()
  304. {
  305.     printf("Abort - pipe called\n");
  306.     exit(1);
  307. }
  308.  
  309. wait()
  310. {
  311.     printf("Abort - wait called\n");
  312.     exit(1);
  313. }
  314.  
  315. execle()
  316. {
  317.     printf("Abort - execle called\n");
  318.     exit(1);
  319. }
  320.  
  321. fork()
  322. {
  323.     printf("Abort - fork called\n");
  324.     exit(1);
  325. }
  326.  
  327. environ()
  328. {
  329.     printf("Abort - environ called\n");
  330.     exit(1);
  331. }
  332.  
  333. int getpid()
  334. {
  335.     return(rnd());
  336. }
  337.  
  338. int geteuid()
  339. {
  340.     return(rnd());
  341. }
  342.  
  343. /* I don't know what ctype ascii is but this seems to work */
  344.  
  345. int isascii(c)
  346. int c;
  347. {
  348.      return(isprint(c));
  349. }
  350.  
  351. /*    the following functions are low level UNIX functions for
  352.  *    creating, opening, reading, writing and seeking.
  353.  *    handle number lower that 3 appear to be screen and keyboard
  354.  *    based so thats what I have made the following do
  355.  */
  356.  
  357. /* seek to a postion in a file */
  358.  
  359. int lseek(fd,off,mode)
  360. int fd,off,mode;
  361. {
  362.      int l;
  363.  
  364.     /* 0 - seek from start of file
  365.        2 - seek from end of file */
  366.  
  367.       switch (mode) {
  368.            case 0:
  369.                 return(_kernel_osargs(1,fd,off));
  370.            case 2:
  371.                  l=_kernel_osargs(2,fd,0);
  372.                  return(_kernel_osargs(1,fd,l-off));
  373.            default:
  374.         /* did not occur while testing ? */
  375.  
  376.                 printf("lseek: mode=%d, offset=%d\n",mode,off);
  377.         exit(1);
  378.     }
  379.     return(0);
  380. }
  381.  
  382. int open(file,mode)
  383. char *file;
  384. int mode;
  385. {
  386.     int     handle;
  387.  
  388.     convert(file);
  389.     handle = _kernel_osfind(195,file);
  390.     if (handle==0) {
  391.         errno=2;
  392.         return(-1);
  393.     }
  394.     errno=0;
  395.     return(handle);
  396. }
  397.  
  398. int creat(file,mode)
  399. char *file;
  400. int mode;
  401. {
  402.     int         handle,type=0;
  403.     struct stat    st;
  404.  
  405.     convert(file);
  406.     if (stat(file,&st)>=0) type=st.st_type;
  407.     handle = _kernel_osfind(131,file);
  408.     if (handle>0) {
  409.         stat(file,&st);
  410.         if (type==0) st.st_type=4095;
  411.         set_stat(file,&st);
  412.     }
  413.     return(handle);
  414. }
  415.  
  416. int close(handle)
  417. int handle;
  418. {
  419.     if (handle>2)
  420.         _kernel_osfind(0, (char *)handle);
  421.     return(0);
  422. }
  423.  
  424. /* delete a file */
  425.  
  426. unlink(file)
  427. char *file;
  428. {
  429.       remove(file);
  430. }
  431.  
  432. /* UNIX low level write */
  433.  
  434. int write (h,b,l)
  435. char *b;
  436. int l,h;
  437. {
  438.     register int    i;
  439.  
  440.     if (h<3 && h>=0) for (i=0; i<l; i++) outchar(b[i]);
  441.     else for (i=0; i<l; i++) {
  442.         if (_kernel_osbput(b[i],h) < 0) return(-1);
  443.     }
  444.     return(l);
  445. }
  446.  
  447. /* UNIX low level read */
  448.  
  449. int read (h,b,l)
  450. char *b;
  451. int l,h;
  452. {
  453.     register int     i;
  454.     int    c;
  455.  
  456.     if (h<3 && h>=0) {
  457.         b[0]=_kernel_osrdch();
  458.         return(1);
  459.     } else {
  460.         for (i=0; i<l; i++) {
  461.             c=_kernel_osbget(h);
  462.             if (c<0) return(i);
  463.             b[i]=c;
  464.         }
  465.     }
  466.     return(l);
  467. }
  468.  
  469.