em.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  3. * See the copyright notice in the ACK home directory, in the file "Copyright".
  4. *
  5. */
  6. #include <stdio.h>
  7. #include <em_path.h>
  8. #include <local.h>
  9. char rcs_id[] = "$Id$" ;
  10. #define MAGIC 07255
  11. struct header {
  12. short h_magic; /* Magic number */
  13. short h_flags; /* See below for defines */
  14. short h_unresolved; /* Cannot run if nonzero */
  15. short h_version; /* Check for VERSION */
  16. short h_wsize;
  17. short h_psize;
  18. short h_unused[2]; /* left over */
  19. } header;
  20. #define TEST 001
  21. #define PROFILE 002
  22. #define FLOW 004
  23. #define COUNT 010
  24. char em_dir[64] = EM_DIR; /* easier to patch */
  25. char *defargv[] = {
  26. "em",
  27. "e.out",
  28. 0
  29. };
  30. char interpret[BUFSIZ];
  31. char flags[5];
  32. char tflg,fflg,cflg,pflg;
  33. main(argc,argv) char **argv; {
  34. char *file;
  35. int fildes;
  36. while (argc>1 && (argv[1][0]=='-' || argv[1][0]=='+')) {
  37. switch(argv[1][1]) {
  38. case 't': tflg=argv[1][0]; break;
  39. case 'c': cflg=argv[1][0]; break;
  40. case 'f': fflg=argv[1][0]; break;
  41. case 'p': pflg=argv[1][0]; break;
  42. default:
  43. fprintf(stderr,"Bad flag %c\n",argv[1][1]);
  44. exit(-1);
  45. }
  46. argv[1] = argv[0];
  47. argc--;argv++;
  48. }
  49. if (argc==1)
  50. argv= defargv;
  51. file=argv[1];
  52. if ((fildes=open(file,0))<0) {
  53. perror(file);
  54. exit(8) ;
  55. }
  56. header.h_magic= r2b(fildes,file) ;
  57. header.h_flags= r2b(fildes,file) ;
  58. header.h_unresolved= r2b(fildes,file) ;
  59. header.h_version= r2b(fildes,file) ;
  60. header.h_wsize= r2b(fildes,file) ;
  61. header.h_psize= r2b(fildes,file) ;
  62. header.h_unused[0]= r2b(fildes,file) ;
  63. header.h_unused[1]= r2b(fildes,file) ;
  64. if (header.h_magic != MAGIC) {
  65. fprintf(stderr,"%s not in correct format\n",file);
  66. exit(-1);
  67. }
  68. if (header.h_version != VERSION) {
  69. fprintf(stderr,"%s obsolete, recompile\n",file);
  70. exit(-1);
  71. }
  72. if (header.h_unresolved != 0) {
  73. fprintf(stderr,
  74. "%s has unresolved references, cannot run it\n",file);
  75. exit(-1);
  76. }
  77. if (tflg)
  78. flags[0] = tflg=='+' ? 't' : '-';
  79. else
  80. flags[0]= header.h_flags&TEST ? 't' : '-';
  81. if (fflg)
  82. flags[1] = fflg=='+' ? 'f' : '-';
  83. else
  84. flags[1]= header.h_flags&FLOW ? 'f' : '-';
  85. if (cflg)
  86. flags[2] = cflg=='+' ? 'c' : '-';
  87. else
  88. flags[2]= header.h_flags&COUNT ? 'c' : '-';
  89. if (pflg)
  90. flags[3] = pflg=='+' ? 'p' : '-';
  91. else
  92. flags[3]= header.h_flags&PROFILE ? 'p' : '-';
  93. sprintf(interpret,"%s/lib.bin/em%d%d/em_%s",
  94. em_dir,header.h_wsize,header.h_psize,flags);
  95. execv(interpret,argv);
  96. fprintf(stderr,"Interpreter %s not available\n",interpret);
  97. }
  98. r2b(fildes,file) char *file ; {
  99. char rd2[2] ;
  100. if ( read(fildes,rd2,sizeof rd2)!=sizeof rd2) {
  101. fprintf(stderr,"%s too short\n",file);
  102. exit(-1);
  103. }
  104. return (rd2[0]&0xFF) | ( (rd2[1]&0xFF)<<8 ) ;
  105. }