|
|
1.1 ! root 1: /* monitor.c - ! 2: * Routines for handling a monitor file -- a places to stash input to be ! 3: * processed later. ! 4: * This package supports only a single monitor file. ! 5: * Monitor files are unlinked immdiately after they are opened, so they ! 6: * will just go away if a program halts for any reason. ! 7: */ ! 8: ! 9: #include <stdio.h> ! 10: #include "checkperms.h" ! 11: #include "monitor.h" ! 12: ! 13: static FILE *monitor_fp = (FILE *)NULL; ! 14: ! 15: void open_monitor_file() /* Create a new monitor file. */ ! 16: { ! 17: FILE *tmp_monitor; ! 18: static char tmp_file[]="/tmp/monXXXXXX"; ! 19: ! 20: /* Try to open a tmp file. */ ! 21: strcpy(tmp_file, tempnam(NULL, "mon")); ! 22: ! 23: if((tmp_monitor = fopen(tmp_file, "w+")) == NULL) { ! 24: /* Do something useful in the way of error handling. */ ! 25: } /* if we can open the tmp file. */ ! 26: ! 27: /* Now unlink the tmp file so that we can just ! 28: * close it to make it go away. ! 29: */ ! 30: unlink(tmp_file); ! 31: ! 32: /* CHECKPOINT - we've got an open file pointer on a nameless ! 33: * file. */ ! 34: monitor_fp = tmp_monitor; ! 35: /* Save current line for later reference. */ ! 36: oldlineno = lineno; ! 37: } /* open_monitor_file() */ ! 38: ! 39: void close_monitor_file() /* Close the monitor file. */ ! 40: { ! 41: /* This makes the file go away, since it has already been unlinked. */ ! 42: fclose(monitor_fp); ! 43: } /* close_monitor_file() */ ! 44: ! 45: void putchar_monitor_file(c) /* Write to the monitor file. */ ! 46: char c; ! 47: { ! 48: putc(c, monitor_fp); ! 49: } /* write_monitor_file() */ ! 50: ! 51: int getchar_monitor_file() /* Read from the monitor file. */ ! 52: { ! 53: return(getc(monitor_fp)); ! 54: } /* read_monitor_file() */ ! 55: ! 56: int fseek_monitor_file(where, how) /* Seek to an arbitrary position in the ! 57: * monitor file. ! 58: */ ! 59: long where; ! 60: int how; ! 61: { ! 62: return(fseek(monitor_fp, where, how)); ! 63: lineno=-1000; /* Set the line number to something meaningless. */ ! 64: } /* fseek_monitor_file() */ ! 65: ! 66: void ungetchar_monitor_file(c) ! 67: char c; ! 68: { ! 69: ungetc(c, monitor_fp); ! 70: } /* ungetchar_monitor_file */ ! 71: ! 72: static boolean hava_char = FALSE; ! 73: static int ungot_char; ! 74: ! 75: void rewind_monitor_file() /* Rewind the monitor file. */ ! 76: { ! 77: rewind(monitor_fp); ! 78: hava_char = FALSE; /* Throw away any ungetc()d character. */ ! 79: hava_delay = FALSE; /* Throw away the delayed character. */ ! 80: lineno = oldlineno; ! 81: } /* rewind_monitor_file() */ ! 82: ! 83: int my_getchar() ! 84: { ! 85: int c; ! 86: ! 87: if(hava_char){ ! 88: c = ungot_char; ! 89: hava_char = FALSE; ! 90: } else { /* Don't have an ungot character. */ ! 91: if(reading_from_monitor_file){ ! 92: c = getchar_monitor_file(); ! 93: } else { ! 94: c = getchar(); ! 95: } ! 96: ! 97: if(saving_to_monitor_file) { ! 98: /* We want to save all but the last character ! 99: * because the lexer has to read one character too ! 100: * far for us to find an unindent. ! 101: */ ! 102: if (hava_delay) { ! 103: putchar_monitor_file(delayed_char); ! 104: } else { ! 105: hava_delay = TRUE; ! 106: } /* if hava_delay */ ! 107: delayed_char = c; ! 108: } /* if saving to monitor file */ ! 109: ! 110: } /* if have an ungot char */ ! 111: ! 112: return(c); ! 113: } /* my_getchar() */ ! 114: ! 115: int my_ungetchar(c) ! 116: int c; ! 117: { ! 118: /* Do not use ungetch() at all because that will cause every ! 119: * ungetch()d character to appear (at least) twice in the monitor file. ! 120: */ ! 121: ! 122: if (hava_char) { /* Can not unget more than one character. */ ! 123: return(EOF); ! 124: } else { ! 125: if (c != EOF) { ! 126: hava_char = TRUE; ! 127: ungot_char = (int) c; ! 128: return(ungot_char); ! 129: } else { ! 130: return(EOF); ! 131: } ! 132: } /* if hava_char already */ ! 133: ! 134: ! 135: } /* my_ungetchar() */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.