1: #ifndef lint 2: static char sccsid[] = "@(#)main.c 4.1 (Berkeley) 7/3/83"; 3: #endif 4: 5: #include "Courier.h" 6: #include <errno.h> 7: 8: char *input_file; 9: char hfile[MAXSTR], cfile1[MAXSTR], cfile2[MAXSTR]; 10: char ufile[MAXSTR], sfile[MAXSTR]; 11: FILE *hf, *cf1, *cf2, *uf, *sf; 12: list Values, Types; 13: int errs; 14: int explicit = 0; /* if true, generate stubs with explicit bindings */ 15: 16: /* 17: * Predefined types. 18: */ 19: struct object 20: *Boolean_type, 21: *Cardinal_type, *LongCardinal_type, 22: *Integer_type, *LongInteger_type, 23: *String_type, 24: *Unspecified_type, *LongUnspecified_type; 25: 26: struct object 27: *Undefined_constant; 28: 29: main(argc, argv) 30: int argc; 31: char **argv; 32: { 33: argc--; argv++; 34: if (argc == 2 && strcmp(*argv, "-x") == 0) { 35: explicit = 1; argc--; argv++; 36: } 37: if (argc != 1) { 38: fprintf(stderr, "Usage: courier [-x] input_file\n"); 39: exit(1); 40: } 41: input_file = *argv; 42: if (freopen(input_file, "r", stdin) == NULL) { 43: perror(input_file); exit(1); 44: } 45: tempname(hfile); tempname(ufile); tempname(sfile); 46: tempname(cfile1); tempname(cfile2); 47: if ((hf = fopen(hfile, "w")) == NULL) { 48: perror(hfile); goto bad; 49: } 50: if ((uf = fopen(ufile, "w")) == NULL) { 51: perror(ufile); goto bad; 52: } 53: if ((sf = fopen(sfile, "w")) == NULL) { 54: perror(sfile); goto bad; 55: } 56: if ((cf1 = fopen(cfile1, "w")) == NULL) { 57: perror(cfile1); goto bad; 58: } 59: if ((cf2 = fopen(cfile2, "w")) == NULL) { 60: perror(cfile2); goto bad; 61: } 62: setup_predefs(); 63: (void) yyparse(); 64: fclose(hf); fclose(uf); fclose(sf); 65: if (errs == 0) { 66: int c; 67: 68: freopen(cfile2, "r", cf2); 69: while ((c = getc(cf2)) != EOF) 70: putc(c, cf1); 71: fclose(cf1); fclose(cf2); 72: unlink(cfile2); 73: rename(cfile1, program_name, "_stubs.c", 0); 74: rename(ufile, program_name, "_client.c", 0); 75: rename(sfile, program_name, "_server.c", 0); 76: rename(hfile, program_name, ".h", 1); 77: exit(0); 78: } 79: fclose(cf1); fclose(cf2); 80: bad: 81: unlink(hfile); 82: unlink(ufile); 83: unlink(sfile); 84: unlink(cfile1); 85: unlink(cfile2); 86: exit(1); 87: } 88: 89: /* 90: * Rename the source file to be <dest>.<suffix> . 91: * If we're being paranoid, we prepend # to existing files that might 92: * get clobbered. 93: */ 94: rename(source, dest, suffix, paranoid) 95: char *source, *dest, *suffix; 96: int paranoid; 97: { 98: char newname[MAXSTR], backup[MAXSTR]; 99: extern int errno; 100: 101: sprintf(newname, "%s%s", dest, suffix); 102: for (;;) 103: if (link(source, newname) == 0) 104: if (unlink(source) != 0) { 105: perror(newname); 106: return (-1); 107: } else 108: return (0); 109: else if (errno != EEXIST || 110: (paranoid && rename(newname, "#", newname) != 0) || 111: (! paranoid && unlink(newname) != 0)) 112: break; 113: perror(newname); 114: return (-1); 115: } 116: 117: /* VARARGS1 */ 118: yyerror(s, args) 119: char *s; 120: { 121: extern int yylineno; 122: 123: errs = 1; 124: fprintf(stderr, "%d: ", yylineno); 125: _doprnt(s, &args, stderr); 126: putc('\n', stderr); 127: unlink(hfile); unlink(ufile); unlink(sfile); 128: unlink(cfile1); unlink(cfile2); 129: } 130: 131: tempname(buf) 132: char *buf; 133: { 134: static int n = 0; 135: 136: sprintf(buf, "tmp%d.%d", n, getpid()); 137: n++; 138: } 139: 140: /* 141: * This mess is needed because C doesn't handle initialization of unions. 142: */ 143: setup_predefs() 144: { 145: struct object *t; 146: 147: Boolean_type = make(O_SYMBOL, "Boolean"); 148: t = make(O_TYPE, C_PREDEF); 149: t->t_pfname = "PackBoolean"; 150: t->t_ufname = "UnpackBoolean"; 151: declare(&Values, Boolean_type, t); 152: 153: Cardinal_type = make(O_SYMBOL, "Cardinal"); 154: t = make(O_TYPE, C_PREDEF); 155: t->t_pfname = "PackCardinal"; 156: t->t_ufname = "UnpackCardinal"; 157: declare(&Values, Cardinal_type, t); 158: 159: LongCardinal_type = make(O_SYMBOL, "LongCardinal"); 160: t = make(O_TYPE, C_PREDEF); 161: t->t_pfname = "PackLongCardinal"; 162: t->t_ufname = "Unpack_LongCardinal"; 163: declare(&Values, LongCardinal_type, t); 164: 165: Integer_type = make(O_SYMBOL, "Integer"); 166: t = make(O_TYPE, C_PREDEF); 167: t->t_pfname = "PackInteger"; 168: t->t_ufname = "UnpackInteger"; 169: declare(&Values, Integer_type, t); 170: 171: LongInteger_type = make(O_SYMBOL, "LongInteger"); 172: t = make(O_TYPE, C_PREDEF); 173: t->t_pfname = "PackLongInteger"; 174: t->t_ufname = "UnpackLongInteger"; 175: declare(&Values, LongInteger_type, t); 176: 177: String_type = make(O_SYMBOL, "String"); 178: t = make(O_TYPE, C_PREDEF); 179: t->t_pfname = "PackString"; 180: t->t_ufname = "UnpackString"; 181: declare(&Values, String_type, t); 182: 183: Unspecified_type = make(O_SYMBOL, "Unspecified"); 184: t = make(O_TYPE, C_PREDEF); 185: t->t_pfname = "PackUnspecified"; 186: t->t_ufname = "UnpackUnspecified"; 187: declare(&Values, Unspecified_type, t); 188: 189: LongUnspecified_type = make(O_SYMBOL, "LongUnspecified"); 190: t = make(O_TYPE, C_PREDEF); 191: t->t_pfname = "PackLongUnspecified"; 192: t->t_ufname = "UnpackLongUnspecified"; 193: declare(&Values, LongUnspecified_type, t); 194: 195: Undefined_constant = make(O_SYMBOL, "?undefined?"); 196: declare(&Types, Undefined_constant, Unspecified_type); 197: declare(&Values, Undefined_constant, make(O_CONSTANT, 0)); 198: }