input-scrub.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /* input_scrub.c - layer between app and the rest of the world
  2. Copyright (C) 1987 Free Software Foundation, Inc.
  3. This file is part of GAS, the GNU Assembler.
  4. GAS is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 1, or (at your option)
  7. any later version.
  8. GAS is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GAS; see the file COPYING. If not, write to
  14. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  15. #include "as.h"
  16. #include "read.h"
  17. #include "input-file.h"
  18. #ifdef VMS
  19. #include <errno.h> /* Need this to make errno declaration right */
  20. #include <perror.h> /* Need this to make sys_errlist/sys_nerr right */
  21. #endif /* VMS */
  22. /*
  23. * O/S independent module to supply buffers of sanitised source code
  24. * to rest of assembler. We get raw input data of some length.
  25. * Also looks after line numbers, for e.g. error messages.
  26. * This module used to do the sanitising, but now a pre-processor program
  27. * (app) does that job so this module is degenerate.
  28. * Now input is pre-sanitised, so we only worry about finding the
  29. * last partial line. A buffer of full lines is returned to caller.
  30. * The last partial line begins the next buffer we build and return to caller.
  31. * The buffer returned to caller is preceeded by BEFORE_STRING and followed
  32. * by AFTER_STRING. The last character before AFTER_STRING is a newline.
  33. */
  34. /*
  35. * We expect the following sanitation has already been done.
  36. *
  37. * No comments, reduce a comment to a space.
  38. * Reduce a tab to a space unless it is 1st char of line.
  39. * All multiple tabs and spaces collapsed into 1 char. Tab only
  40. * legal if 1st char of line.
  41. * # line file statements converted to .line x;.file y; statements.
  42. * Escaped newlines at end of line: remove them but add as many newlines
  43. * to end of statement as you removed in the middle, to synch line numbers.
  44. */
  45. #define BEFORE_STRING ("\n")
  46. #define AFTER_STRING (" ") /* bcopy of 0 chars might choke. */
  47. #define BEFORE_SIZE (1)
  48. #define AFTER_SIZE (1)
  49. static char * buffer_start; /* -> 1st char of full buffer area. */
  50. static char * partial_where; /* -> after last full line in buffer. */
  51. static int partial_size; /* >=0. Number of chars in partial line in buffer. */
  52. static char save_source [AFTER_SIZE];
  53. /* Because we need AFTER_STRING just after last */
  54. /* full line, it clobbers 1st part of partial */
  55. /* line. So we preserve 1st part of partial */
  56. /* line here. */
  57. static int buffer_length; /* What is the largest size buffer that */
  58. /* input_file_give_next_buffer() could */
  59. /* return to us? */
  60. static void as_1_char ();
  61. /*
  62. We never have more than one source file open at once.
  63. We may, however, read more than 1 source file in an assembly.
  64. NULL means we have no file open right now.
  65. */
  66. /*
  67. We must track the physical file and line number for error messages.
  68. We also track a "logical" file and line number corresponding to (C?)
  69. compiler source line numbers.
  70. Whenever we open a file we must fill in physical_input_file. So if it is NULL
  71. we have not opened any files yet.
  72. */
  73. static
  74. char * physical_input_file,
  75. * logical_input_file;
  76. typedef unsigned int line_numberT; /* 1-origin line number in a source file. */
  77. /* A line ends in '\n' or eof. */
  78. static
  79. line_numberT physical_input_line,
  80. logical_input_line;
  81. void
  82. input_scrub_begin ()
  83. {
  84. know( strlen(BEFORE_STRING) == BEFORE_SIZE );
  85. know( strlen( AFTER_STRING) == AFTER_SIZE );
  86. input_file_begin ();
  87. buffer_length = input_file_buffer_size ();
  88. buffer_start = xmalloc ((long)(BEFORE_SIZE + buffer_length + buffer_length + AFTER_SIZE));
  89. bcopy (BEFORE_STRING, buffer_start, (int)BEFORE_SIZE);
  90. /* Line number things. */
  91. logical_input_line = 0;
  92. logical_input_file = (char *)NULL;
  93. physical_input_file = NULL; /* No file read yet. */
  94. do_scrub_begin();
  95. }
  96. void
  97. input_scrub_end ()
  98. {
  99. input_file_end ();
  100. }
  101. char * /* Return start of caller's part of buffer. */
  102. input_scrub_new_file (filename)
  103. char * filename;
  104. {
  105. input_file_open (filename, !flagseen['f']);
  106. physical_input_file = filename[0] ? filename : "{standard input}";
  107. physical_input_line = 0;
  108. partial_size = 0;
  109. return (buffer_start + BEFORE_SIZE);
  110. }
  111. char *
  112. input_scrub_next_buffer (bufp)
  113. char **bufp;
  114. {
  115. register char * limit; /* -> just after last char of buffer. */
  116. #ifdef DONTDEF
  117. if(preprocess) {
  118. if(save_buffer) {
  119. *bufp = save_buffer;
  120. save_buffer = 0;
  121. }
  122. limit = input_file_give_next_buffer(buffer_start+BEFORE_SIZE);
  123. if (!limit) {
  124. partial_where = 0;
  125. if(partial_size)
  126. as_warn("Partial line at end of file ignored");
  127. return partial_where;
  128. }
  129. if(partial_size)
  130. bcopy(save_source, partial_where,(int)AFTER_SIZE);
  131. do_scrub(partial_where,partial_size,buffer_start+BEFORE_SIZE,limit-(buffer_start+BEFORE_SIZE),&out_string,&out_length);
  132. limit=out_string + out_length;
  133. for(p=limit;*--p!='\n';)
  134. ;
  135. p++;
  136. if(p<=buffer_start+BEFORE_SIZE)
  137. as_fatal("Source line too long. Please change file '%s' and re-make the assembler.",__FILE__);
  138. partial_where = p;
  139. partial_size = limit-p;
  140. bcopy(partial_where, save_source,(int)AFTER_SIZE);
  141. bcopy(AFTER_STRING, partial_where, (int)AFTER_SIZE);
  142. save_buffer = *bufp;
  143. *bufp = out_string;
  144. return partial_where;
  145. }
  146. /* We're not preprocessing. Do the right thing */
  147. #endif
  148. if (partial_size)
  149. {
  150. bcopy (partial_where, buffer_start + BEFORE_SIZE, (int)partial_size);
  151. bcopy (save_source, buffer_start + BEFORE_SIZE, (int)AFTER_SIZE);
  152. }
  153. limit = input_file_give_next_buffer (buffer_start + BEFORE_SIZE + partial_size);
  154. if (limit)
  155. {
  156. register char * p; /* Find last newline. */
  157. for (p = limit; * -- p != '\n'; )
  158. {
  159. }
  160. ++ p;
  161. if (p <= buffer_start + BEFORE_SIZE)
  162. {
  163. as_fatal ("Source line too long. Please change file %s then rebuild assembler.", __FILE__);
  164. }
  165. partial_where = p;
  166. partial_size = limit - p;
  167. bcopy (partial_where, save_source, (int)AFTER_SIZE);
  168. bcopy (AFTER_STRING, partial_where, (int)AFTER_SIZE);
  169. }
  170. else
  171. {
  172. partial_where = 0;
  173. if (partial_size > 0)
  174. {
  175. as_warn( "Partial line at end of file ignored" );
  176. }
  177. }
  178. return (partial_where);
  179. }
  180. /*
  181. * The remaining part of this file deals with line numbers, error
  182. * messages and so on.
  183. */
  184. int
  185. seen_at_least_1_file () /* TRUE if we opened any file. */
  186. {
  187. return (physical_input_file != NULL);
  188. }
  189. void
  190. bump_line_counters ()
  191. {
  192. ++ physical_input_line;
  193. ++ logical_input_line;
  194. }
  195. /*
  196. * new_logical_line()
  197. *
  198. * Tells us what the new logical line number and file are.
  199. * If the line_number is <0, we don't change the current logical line number.
  200. * If the fname is NULL, we don't change the current logical file name.
  201. */
  202. void
  203. new_logical_line (fname, line_number)
  204. char * fname; /* DON'T destroy it! We point to it! */
  205. int line_number;
  206. {
  207. if ( fname )
  208. {
  209. logical_input_file = fname;
  210. }
  211. if ( line_number >= 0 )
  212. {
  213. logical_input_line = line_number;
  214. }
  215. }
  216. /*
  217. * a s _ w h e r e ( )
  218. *
  219. * Write a line to stderr locating where we are in reading
  220. * input source files.
  221. * As a sop to the debugger of AS, pretty-print the offending line.
  222. */
  223. void
  224. as_where()
  225. {
  226. char *p;
  227. line_numberT line;
  228. if (physical_input_file)
  229. { /* we tried to read SOME source */
  230. if (input_file_is_open())
  231. { /* we can still read lines from source */
  232. #ifdef DONTDEF
  233. fprintf (stderr," @ physical line %ld., file \"%s\"",
  234. (long) physical_input_line, physical_input_file);
  235. fprintf (stderr," @ logical line %ld., file \"%s\"\n",
  236. (long) logical_input_line, logical_input_file);
  237. (void)putc(' ', stderr);
  238. as_howmuch (stderr);
  239. (void)putc('\n', stderr);
  240. #else
  241. p = logical_input_file ? logical_input_file : physical_input_file;
  242. line = logical_input_line ? logical_input_line : physical_input_line;
  243. fprintf(stderr,"%s:%u:", p, line);
  244. #endif
  245. }
  246. else
  247. {
  248. #ifdef DONTDEF
  249. fprintf (stderr," After reading source.\n");
  250. #else
  251. p = logical_input_file ? logical_input_file : physical_input_file;
  252. line = logical_input_line ? logical_input_line : physical_input_line;
  253. fprintf (stderr,"%s:unknown:", p);
  254. #endif
  255. }
  256. }
  257. else
  258. {
  259. #ifdef DONTDEF
  260. fprintf (stderr," Before reading source.\n");
  261. #else
  262. #endif
  263. }
  264. }
  265. /*
  266. * a s _ p e r r o r
  267. *
  268. * Like perror(3), but with more info.
  269. */
  270. void
  271. as_perror(gripe, filename)
  272. char * gripe; /* Unpunctuated error theme. */
  273. char * filename;
  274. {
  275. extern int errno; /* See perror(3) for details. */
  276. extern int sys_nerr;
  277. extern char * sys_errlist[];
  278. fprintf (stderr,"as:file(%s) %s! ",
  279. filename, gripe
  280. );
  281. if (errno > sys_nerr)
  282. {
  283. fprintf (stderr, "Unknown error #%d.", errno);
  284. }
  285. else
  286. {
  287. fprintf (stderr, "%s.", sys_errlist [errno]);
  288. }
  289. (void)putc('\n', stderr);
  290. errno = 0; /* After reporting, clear it. */
  291. if (input_file_is_open()) /* RMS says don't mention line # if not needed. */
  292. {
  293. as_where();
  294. }
  295. }
  296. /*
  297. * a s _ h o w m u c h ( )
  298. *
  299. * Output to given stream how much of line we have scanned so far.
  300. * Assumes we have scanned up to and including input_line_pointer.
  301. * No free '\n' at end of line.
  302. */
  303. void
  304. as_howmuch (stream)
  305. FILE * stream; /* Opened for write please. */
  306. {
  307. register char * p; /* Scan input line. */
  308. /* register char c; JF unused */
  309. for (p = input_line_pointer - 1; * p != '\n'; --p)
  310. {
  311. }
  312. ++ p; /* p -> 1st char of line. */
  313. for (; p <= input_line_pointer; p++)
  314. {
  315. /* Assume ASCII. EBCDIC & other micro-computer char sets ignored. */
  316. /* c = *p & 0xFF; JF unused */
  317. as_1_char (*p, stream);
  318. }
  319. }
  320. static void
  321. as_1_char (c,stream)
  322. unsigned char c;
  323. FILE * stream;
  324. {
  325. if ( c > 127 )
  326. {
  327. (void)putc( '%', stream);
  328. c -= 128;
  329. }
  330. if ( c < 32 )
  331. {
  332. (void)putc( '^', stream);
  333. c += '@';
  334. }
  335. (void)putc( c, stream);
  336. }
  337. /* end: input_scrub.c */