home *** CD-ROM | disk | FTP | other *** search
/ Really Useful CD 1 / ReallyUsefulCD1.iso / extras / languages / smalltalk / _smalltalk / sources / c / lexcmd < prev    next >
Encoding:
Text File  |  1989-11-20  |  6.8 KB  |  284 lines

  1. /*
  2.      Little Smalltalk
  3.           misc lexer related routines
  4.           timothy a. budd 12/84
  5. */
  6.  
  7. /*
  8.      The source code for the Little Smalltalk System may be freely
  9.      copied provided that the source of all files is acknowledged
  10.      and that this condition is copied with each file.
  11.  
  12.      The Little Smalltalk System is distributed without responsibility
  13.      for the performance of the program and without any guarantee of
  14.      maintenance.
  15.  
  16.      All questions concerning Little Smalltalk should be addressed to:
  17.  
  18.           Professor Tim Budd
  19.           Department of Computer Science
  20.           Oregon State University
  21.           Corvallis, Oregon
  22.           97331
  23.           US
  24. */
  25. # include <stdio.h>
  26. # include "env.h"
  27. # include <ctype.h>
  28.  
  29. extern char toktext[];
  30.  
  31. /* dolexcommand - read a ) type directive, and process it */
  32. dolexcommand(p)
  33. char *p;
  34. {       char *q, buffer[100];
  35.  
  36.      /* replace trailing newline with end of string */
  37.      for (q = p; *q && *q != '\n'; q++);
  38.      if (*q == '\n') *q = '\0';
  39.  
  40.         switch( *++p) {
  41.            case '!': 
  42. # ifndef NOSYSTEM
  43. # ifdef ARM
  44.         ssystem(++p);
  45. # else
  46.           system(++p); 
  47. # endif
  48. # endif
  49.           break;
  50.  
  51.            case 'e': for (++p; isspace(*p); p++);
  52. # ifdef ARM  /* return codes not used yet */
  53.                      lexedit(p);
  54.                      lexinclude(p);
  55.                      break;
  56. #else
  57.  
  58.                if (! lexedit(p)) lexinclude(p);
  59.                      break;
  60. # endif
  61.         case 'g': for (++p; isspace(*p); p++);
  62. #ifdef ARM   /* ARM uses '.' instead of '/' */
  63.                      sprintf(buffer,"%s.%s", LIBLOC, p);
  64. #else
  65.                      sprintf(buffer,"%s/%s", LIBLOC, p);
  66. #endif
  67.                lexread(buffer);
  68.               break;
  69.  
  70.            case 'i': for (++p; isspace(*p); p++);
  71.                      lexinclude(p);
  72.                      break;
  73.  
  74.            case 'r': for (++p; isspace(*p); p++);
  75.                      lexread(p);
  76.                      break;
  77.  
  78.         case 's': for(++p; isspace(*p); p++);
  79.                dosave(p);
  80.                break;
  81.  
  82.         case 'l': for(++p; isspace(*p); p++);
  83.                doload(p);
  84.                break;
  85.  
  86.            default:  lexerr("unknown command %s", toktext);
  87.            }
  88. }
  89.  
  90. /* doload/dosave routines written by nick buchholz */
  91. /*
  92.      doload and dosave routines make the following assumptions
  93.      1. version is the first global variable declared in main.
  94.      2. main is the first procedure seen by the loader
  95.      3. the loader allocates memory in the order it sees the procedures
  96.      4. memory is laid out as on the vax 780 under 4.2
  97.  
  98.      on other machines any or all of these might be false and the
  99.      doload/dosave routines will not work
  100. */
  101. extern int version;
  102. #ifdef ARM
  103. /* replacement versions to warn of missing facility TPR */
  104. dosave(p)
  105. char *p;
  106. {
  107. printf("Sorry -- no can do at dosave\n");
  108. }
  109.  
  110. doload(p)
  111. char *p;
  112. {
  113. printf("Sorry -- no can do at doload\n");
  114. }
  115. #else
  116.  
  117. dosave(p) char *p;{
  118.     int fd; 
  119.     char *start, *end, *sbrk(); 
  120.     unsigned int length, len;
  121.     int dlen;
  122.  
  123. # ifdef OPEN3ARG
  124.     if ((fd = open(p, O_WRONLY|O_CREAT|O_TRUNC, 0666)) == -1)
  125. # endif
  126. # ifndef OPEN3ARG
  127.     if ((fd = creat(p, 0666)) == -1)
  128. # endif
  129.  
  130.     ARMX(tprintf(,"can't open: %s\n",p), fprintf(stderr,"can't open: %s\n",p));
  131.  
  132.     start = (char *) &version;
  133.     end = sbrk(0);
  134.     length = end - start;
  135.  
  136.     write(fd, &version, sizeof(int));
  137.     write(fd, &start, sizeof(char *));
  138.     write(fd, &length, sizeof(unsigned int));
  139.  
  140.     for (len = 0; len < length; len += dlen) {
  141.      dlen = ((length - len) > 512) ? 512 : (length - len);
  142.      if (dlen != write(fd, start + len, dlen)) {
  143.           cant_happen(23);
  144.           }
  145.      }
  146.  
  147.     ARMX(tprintf("%u bytes written\n",len), fprintf(stderr,"%u bytes written\n",len));
  148.  
  149.     close(fd);
  150. }
  151.  
  152. # ifdef ENVSAVE
  153. extern char **environ;
  154. # endif
  155.  
  156. doload(p) char *p;{
  157.     int fd; 
  158.     char *start, *end, *brk(); 
  159.     unsigned int length, len; 
  160.     int dlen;
  161.     int test;
  162. # ifdef ENVSAVE
  163.     char **evsave;
  164. # endif
  165.  
  166. # ifdef OPEN3ARG
  167.     if ((fd = open(p, O_RDONLY, 0)) == -1)
  168. # endif
  169. # ifndef OPEN3ARG
  170.     if ((fd = open(p, 0 )) == -1)
  171. # endif
  172.      ARMX(tprintf("no such context as: %s\n", p), fprintf(stderr,"no such context as: %s\n", p));
  173.  
  174.     else {
  175.      read(fd, &test, sizeof(int));
  176.      read(fd, &start, sizeof(char *));
  177.      read(fd, &length, sizeof(unsigned int));
  178.  
  179.      if ((test != version) || (start != (char *) &version))
  180.          ARMX(tprintf("%s: not a valid context file for version %d\n", p, version),
  181.               fprintf(stderr,"%s: not a valid context file for version %d\n", p, version));
  182.      else {
  183.          start = (char *) &version;
  184.          end = brk(start + length + 1);
  185. # ifdef ENVSAVE
  186.          evsave = environ;
  187. # endif
  188.  
  189.          for (len = 0; len < length; len += dlen) {
  190.           dlen = ((length - len) > 512) ? 512 : (length - len);
  191.           if (dlen != read(fd, start + len, dlen)) {
  192.                cant_happen(23);
  193.                }
  194.           }
  195. # ifdef ENVSAVE
  196.         environ = evsave;
  197. # endif
  198.          ARMX(tprintf("%u bytes read\n",len), fprintf(stderr,"%u bytes read\n",len));
  199.      }
  200.      close(fd);
  201.     }
  202. }
  203. # endif
  204. /* lexread - read commands from a file */
  205. lexread(name)
  206. char *name;
  207. {    FILE *fd;
  208.  
  209.      fd = fopen(name, "r");
  210.      if (fd == NULL) {
  211.           ARMX(tprintf("can't open %s\n", name), fprintf(stderr,"can't open %s\n", name));
  212.           }
  213.      else {
  214.           set_file(fd);
  215.           }
  216. }
  217.  
  218. /* lexinclude - parse a class and include the class description */
  219. lexinclude(name)
  220. char *name;
  221. {  char template[60], cmdbuf[120];
  222.    int  i;
  223.  
  224. # ifndef NOSYSTEM
  225.    gettemp(template);
  226. #ifdef ARM  
  227. /* TPRs modified parser takes 2 files as args, since redirection is dodgy still */
  228.    sprintf(cmdbuf, "*remove %s", template);
  229.    ssystem(cmdbuf);
  230.    sprintf(cmdbuf,"%s %s %s", PARSER, name, template);
  231.    ssystem(cmdbuf);
  232.    lexread(template); /* no return code at the mo */
  233. #else
  234.    sprintf(cmdbuf,"%s %s >%s", PARSER, name, template);
  235.    i = system(cmdbuf);
  236.    if (i == 0)
  237.      lexread(template);
  238. # endif
  239. # endif
  240. # ifdef NOSYSTEM
  241.    fprintf(stderr,")i does not work on this system\n");
  242. # endif
  243. }
  244.  
  245. /* lexedit - edit a class description */
  246. int lexedit(name)
  247. char *name;
  248. {    char *e, buffer[100], *getenv();
  249.  
  250. #ifdef ARM
  251.        /* sprintf(buffer, "tw %s",name);  ---- Changed by SLD to... */
  252.        sprintf(buffer, "StEd %s", name);  /* Alias StEd must exist! */
  253.        return(ssystem(buffer));
  254. #else
  255. # ifndef NOSYSTEM
  256.      e = getenv("EDITOR");
  257. # ifdef IBMPC
  258.      if (!e) e = "edlin";
  259. #else
  260.      if (!e) e = "ed";
  261. #endif
  262.      sprintf(buffer,"%s %s", e, name);
  263.      return(system(buffer));
  264. # endif
  265. # endif
  266. # ifdef NOSYSTEM
  267.      fprintf(stderr,")e does not work on this system\n");
  268.      return(1);
  269. # endif
  270. }
  271. /* TPR ssystem -- divert all system calls to oursystemcall */
  272.  
  273. ssystem(s)
  274. char *s;
  275. {
  276. /* oursystemcall(s); */
  277. #ifndef ARM
  278. system(s);
  279. #else
  280. /* RiscOS system call added by MA  - uses Wimp_StartTask */
  281. os_swi1(0x400DE, s);
  282. #endif 
  283. }
  284.