1: /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1984. */ 2: static char rcsid[] = "$Header: main.c,v 2.5 85/08/22 16:05:00 timo Exp $"; 3: 4: /* 5: * B editor -- Main program (init/exit processing), error handling. 6: */ 7: 8: /* 9: * The B editor is a structured editor for a programming language 10: * for beginners and non-professional computer users. 11: * [L.G.L.T. Meertens: Draft Proposal for the B programming language, 12: * Mathematical Centre, Amsterdam, 1982, ISBN 90 6169 238 2.] 13: * Note that `B' is only a provisional name for the language. 14: * The editor uses a subset of the run-time system for the B 15: * interpreter, so that they may be linked together in a later stage. 16: * Also the sharing strategy of the B run-time routines makes a very 17: * elegant and powerful UNDO-mechanism possible. 18: */ 19: 20: #include "b.h" /* Contains definitions like string, etc. */ 21: #include "feat.h" 22: #include "bobj.h" 23: 24: 25: #ifdef SAVEPOS 26: #define SAVEPOSFILE ".Bed_pos" /* Last focus position database */ 27: #define MAXSAVE 50 /* Maximum number of entries kept in SAVEPOSFILE */ 28: #endif SAVEPOS 29: 30: 31: /* Command line flags */ 32: 33: bool dflag; /* -d: debugging output wanted */ 34: 35: bool slowterminal; 36: /* -s: the terminal is so slow that long messages are annoying */ 37: 38: bool hushbaby; /* -h: no bells are to be heard */ 39: 40: #ifdef COMMENTED_OUT /* Lower levels don't respond to this */ 41: bool nostandout; /* -n: inhibit use of standout */ 42: #endif COMMENTED_OUT 43: 44: 45: /* 46: * Main program -- call module initializations, do some work, 47: * call module shut-off code, exit. 48: */ 49: 50: Visible Procedure 51: main(argc, argv) 52: int argc; 53: string *argv; 54: { 55: bool initdone = No; 56: bool status = Yes; 57: int lineno = 0; 58: string arg0 = argv[0]; 59: string cp; 60: string filename; 61: extern string malloc(); 62: 63: cp = rindex(arg0, '/'); 64: if (cp) 65: arg0 = cp+1; 66: 67: /* Process UNIX command line options */ 68: for (; argc > 1 && argv[1][0] == '-'; --argc, ++argv) { 69: switch (argv[1][1]) { 70: 71: #ifndef NDEBUG 72: case 'd': 73: dflag = Yes; 74: break; 75: #endif NDEBUG 76: 77: case 'h': 78: hushbaby = Yes; 79: break; 80: 81: #ifdef COMMENTED_OUT /* Lower levels don't respond to this */ 82: case 'n': 83: nostandout = Yes; 84: break; 85: #endif COMMENTED_OUT 86: 87: case 's': 88: slowterminal = Yes; 89: break; 90: 91: default: 92: fprintf(stderr, 93: "*** Usage: %s [-h] [-s] %s\n", 94: arg0, 95: #ifdef FILEARGS 96: "[ [+lineno] file ] ..."); 97: #else !FILEARGS 98: ""); 99: #endif !FILEARGS 100: exit(1); 101: 102: } 103: } 104: 105: /* Setbuf must be called before any output is produced! */ 106: setbuf(stdout, malloc((unsigned)BUFSIZ)); 107: 108: #ifdef FILEARGS 109: for (; status && argc > 1; --argc, ++argv) { 110: if (argv[1][0] == '+') { /* +lineno option */ 111: lineno = atoi(argv[1] + 1); 112: } 113: else { 114: filename = argv[1]; 115: if (!initdone) { 116: initall(); 117: initdone = Yes; 118: } 119: status = demo(filename, lineno); 120: lineno = 0; 121: } 122: } 123: #endif FILEARGS 124: if (!initdone) { 125: #ifdef BTOP 126: initall(); 127: mainloop(); 128: #else BTOP 129: #ifndef FILEARGS 130: Deliberate error. You should define at least one of BTOP and FILEARGS; 131: #endif !FILEARGS 132: fprintf(stderr, "*** No file edited\n"); 133: exit(0); 134: #endif BTOP 135: } 136: endall(); 137: objstats(); 138: if (status) 139: objcheck(); 140: else 141: objdump(); 142: return !status; 143: } 144: 145: 146: /* 147: * Module initializations -- for each module xxxx that needs dynamic 148: * initialization, call a routine named initxxxx. 149: * The order is determined by the inter-module dependencies. 150: * Also note that all terminal- and screen-related initializations are called 151: * indirectly by initterm(). 152: */ 153: 154: Hidden Procedure 155: initall() 156: { 157: #ifndef NDEBUG 158: if (dflag) 159: fprintf(stderr, "*** initall();\n\r"); 160: #endif NDEBUG 161: initfile(); 162: initkeys(); 163: initgram(); 164: #ifdef USERSUGG 165: initsugg(); 166: #endif USERSUGG 167: initunix(); 168: initterm(); 169: } 170: 171: 172: /* 173: * Module shut-off code -- for each module xxxx that needs dynamic 174: * shut-off code (what is the inverse of `initialization'?), 175: * call a routine named endxxxx. 176: * Endall is also called (from module "unix") when a signal or interrupt 177: * causes termination. 178: */ 179: 180: Visible Procedure 181: endall() 182: { 183: if (dflag) 184: fprintf(stderr, "*** endall();\n\r"); 185: endterm(); 186: enddemo(); 187: endunix(); 188: enderro(); 189: #ifdef USERSUGG 190: endsugg(); 191: #endif USERSUGG 192: } 193: 194: 195: 196: /* 197: * System error -- abort the editor with a short error message. 198: * Should only be called for catastrophic, unrecoverable errors 199: * or those that `cannot happen'. 200: */ 201: 202: /* VARARGS 1 */ 203: Visible Procedure 204: syserr(fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) 205: string fmt; 206: { 207: #ifdef BTOP 208: termchild(); 209: #endif BTOP 210: endall(); 211: fprintf(stderr, "*** System error: "); 212: fprintf(stderr, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10); 213: fprintf(stderr, "\n"); 214: #ifndef NDEBUG 215: fprintf(stderr, "*** Core dump for B guru: "); 216: fflush(stderr); 217: abort(); 218: #else 219: fflush(stderr); 220: _exit(1); 221: #endif 222: /* NOTREACHED */ 223: } 224: 225: 226: /* 227: * Assertion error. 228: * Call syserr with information about where something was wrong. 229: * (Sorry, WHAT was wrong must be dug out of the core dump.) 230: */ 231: 232: Visible Procedure 233: asserr(file, line) 234: string file; 235: int line; 236: { 237: syserr("Assertion failed: file %s, line %d", file, line); 238: /* NOTREACHED */ 239: }