home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 018.lha / sources / lexcmd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-10-19  |  4.9 KB  |  229 lines

  1. /*
  2.     Little Smalltalk
  3.         misc lexer related routines
  4.         timothy a. budd 12/84
  5. */
  6. /*
  7.     The source code for the Little Smalltalk System may be freely
  8.     copied provided that the source of all files is acknowledged
  9.     and that this condition is copied with each file.
  10.  
  11.     The Little Smalltalk System is distributed without responsibility
  12.     for the performance of the program and without any guarantee of
  13.     maintenance.
  14.  
  15.     All questions concerning Little Smalltalk should be addressed to:
  16.  
  17.         Professor Tim Budd
  18.         Department of Computer Science
  19.         The University of Arizona
  20.         Tucson, Arizona
  21.         85721
  22.         USA
  23. */
  24. # include <stdio.h>
  25. # include "env.h"
  26. # include <ctype.h>
  27.  
  28. extern char toktext[];
  29.  
  30. /* dolexcommand - read a ) type directive, and process it */
  31. dolexcommand(p)
  32. char *p;
  33. {       char *q, buffer[100];
  34.  
  35.     /* replace trailing newline with end of string */
  36.     for (q = p; *q && *q != '\n'; q++);
  37.     if (*q == '\n') *q = '\0';
  38.  
  39.         switch( *++p) {
  40.            case '!': 
  41. # ifndef NOSYSTEM
  42.         system(++p); 
  43. # endif
  44.         break;
  45.  
  46.            case 'e': for (++p; isspace(*p); p++);
  47.              if (! lexedit(p)) lexinclude(p);
  48.                      break;
  49.  
  50.        case 'g': for (++p; isspace(*p); p++);
  51.              sprintf(buffer,"%s/%s", LIBLOC, p);
  52.              lexread(buffer);
  53.              break;
  54.  
  55.            case 'i': for (++p; isspace(*p); p++);
  56.                      lexinclude(p);
  57.                      break;
  58.  
  59.            case 'r': for (++p; isspace(*p); p++);
  60.                      lexread(p);
  61.                      break;
  62.  
  63.        case 's': for(++p; isspace(*p); p++);
  64.              dosave(p);
  65.              break;
  66.  
  67.        case 'l': for(++p; isspace(*p); p++);
  68.              doload(p);
  69.              break;
  70.  
  71.            default:  lexerr("unknown command %s", toktext);
  72.            }
  73. }
  74.  
  75. /* doload/dosave routines written by nick buchholz */
  76. /*
  77.     doload and dosave routines make the following assumptions
  78.     1. version is the first global variable declared in main.
  79.     2. main is the first procedure seen by the loader
  80.     3. the loader allocates memory in the order it sees the procedures
  81.     4. memory is laid out as on the vax 780 under 4.2
  82.  
  83.     on other machines any or all of these might be false and the
  84.     doload/dosave routines will not work
  85. */
  86. extern int version;
  87.  
  88. dosave(p) char *p;{
  89.     int fd; 
  90.     char *start, *end, *sbrk(); 
  91.     unsigned int length, len;
  92.     int dlen;
  93.  
  94. # ifdef OPEN3ARG
  95.     if ((fd = open(p, O_WRONLY|O_CREAT|O_TRUNC, 0666)) == -1)
  96. # endif
  97. # ifndef OPEN3ARG
  98.     if ((fd = creat(p, 0666)) == -1)
  99. # endif
  100.        fprintf(stderr,"can't open: %s\n",p);
  101.  
  102.     start = (char *) &version;
  103.     end = sbrk(0);
  104.     length = end - start;
  105.  
  106.     write(fd, &version, sizeof(int));
  107.     write(fd, &start, sizeof(char *));
  108.     write(fd, &length, sizeof(unsigned int));
  109.  
  110.     for (len = 0; len < length; len += dlen) {
  111.     dlen = ((length - len) > 512) ? 512 : (length - len);
  112.     if (dlen != write(fd, start + len, dlen)) {
  113.         cant_happen(23);
  114.         }
  115.     }
  116.  
  117.     fprintf(stderr,"%u bytes written\n",len);
  118.  
  119.     close(fd);
  120. }
  121.  
  122. # ifdef ENVSAVE
  123. extern char **environ;
  124. # endif
  125.  
  126. doload(p) char *p;{
  127.     int fd; 
  128.     char *start, *end, *brk(); 
  129.     unsigned int length, len; 
  130.     int dlen;
  131.     int test;
  132. # ifdef ENVSAVE
  133.     char **evsave;
  134. # endif
  135.  
  136. # ifdef OPEN3ARG
  137.     if ((fd = open(p, O_RDONLY, 0)) == -1)
  138. # endif
  139. # ifndef OPEN3ARG
  140.     if ((fd = open(p, 0 )) == -1)
  141. # endif
  142.     fprintf(stderr,"no such context as: %s\n", p);
  143.  
  144.     else {
  145.     read(fd, &test, sizeof(int));
  146.     read(fd, &start, sizeof(char *));
  147.     read(fd, &length, sizeof(unsigned int));
  148.  
  149.     if ((test != version) || (start != (char *) &version))
  150.         fprintf(stderr,"%s: not a valid context file for version %d\n", 
  151.                 p, version);
  152.     else {
  153.         start = (char *) &version;
  154.         end = brk(start + length + 1);
  155. # ifdef ENVSAVE
  156.         evsave = environ;
  157. # endif
  158.  
  159.             for (len = 0; len < length; len += dlen) {
  160.         dlen = ((length - len) > 512) ? 512 : (length - len);
  161.         if (dlen != read(fd, start + len, dlen)) {
  162.             cant_happen(23);
  163.             }
  164.         }
  165. # ifdef ENVSAVE
  166.        environ = evsave;
  167. # endif
  168.         fprintf(stderr,"%u bytes read\n",len);
  169.     }
  170.     close(fd);
  171.     }
  172. }
  173.  
  174. /* lexread - read commands from a file */
  175. lexread(name)
  176. char *name;
  177. {    FILE *fd;
  178.  
  179.     fd = fopen(name, "r");
  180.     if (fd == NULL) {
  181.         fprintf(stderr,"can't open %s\n", name);
  182.         }
  183.     else {
  184.         set_file(fd);
  185.         }
  186. }
  187.  
  188. /* lexinclude - parse a class and include the class description */
  189. lexinclude(name)
  190. char *name;
  191. {  char template[60], cmdbuf[120];
  192.    int  i;
  193.  
  194. # ifndef NOSYSTEM
  195.    gettemp(template);
  196.    sprintf(cmdbuf,"%s %s %s", PARSER, name, template); /* WMK removed > */
  197.    i = system(cmdbuf);
  198. /* printf("%s = %d\n", cmdbuf, i); */
  199.    if (i == 0)
  200.        lexread(template);
  201. # endif
  202. # ifdef NOSYSTEM
  203.    fprintf(stderr,")i does not work on this system\n");
  204. # endif
  205. }
  206.  
  207. /* lexedit - edit a class description */
  208. int lexedit(name)
  209. char *name;
  210. {    char *e, buffer[100], *getenv();
  211.  
  212. # ifndef NOSYSTEM
  213.     e = getenv("EDITOR");
  214.     if (!e) e = "ed";
  215.     sprintf(buffer,"%s %s", e, name);
  216.     return(system(buffer));
  217. # endif
  218. # ifdef NOSYSTEM
  219.     fprintf(stderr,")e does not work on this system\n");
  220.     return(1);
  221. # endif
  222. }
  223.  
  224. char *brk(addr) {return (addr);}
  225.  
  226. void *malloc();
  227.  
  228. char *sbrk(incr) {return (malloc(incr));}
  229.