PIPS
print.c
Go to the documentation of this file.
1 /*
2 
3  $Id: print.c 23065 2016-03-02 09:05:50Z coelho $
4 
5  Copyright 1989-2016 MINES ParisTech
6 
7  This file is part of PIPS.
8 
9  PIPS is free software: you can redistribute it and/or modify it
10  under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  any later version.
13 
14  PIPS is distributed in the hope that it will be useful, but WITHOUT ANY
15  WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  FITNESS FOR A PARTICULAR PURPOSE.
17 
18  See the GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with PIPS. If not, see <http://www.gnu.org/licenses/>.
22 
23 */
24 #ifdef HAVE_CONFIG_H
25  #include "pips_config.h"
26 #endif
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <ctype.h>
30 #include <string.h>
31 
32 #include "genC.h"
33 #include "text.h"
34 
35 #include "misc.h"
36 
37 #include "arithmetique.h"
38 
39 /* FI: just to make sure that text.h is built; pips-makemake -l does not
40  tale into account a library whose modules do not use the library header */
41 #include "text-util.h"
42 
43 /* print_sentence:
44  *
45  * FI: I had to change this module to handle string longer than the space available
46  * on one line; I tried to preserve as much as I could of the previous behavior to
47  * avoid pseudo-hyphenation at the wrong place and to avoid extensicve problems
48  * with validate; the resulting code is lousy, of course; FI, 15 March 1993
49  *
50  * RK: the print_sentence could print lower case letter according to
51  * a property... 17/12/1993.
52  */
53 void print_sentence(FILE * fd, sentence s)
54 {
55  if (sentence_formatted_p(s)) {
56  string ps = sentence_formatted(s);
57  while (*ps) {
58  char c = *ps++;
59  /* FI/FC: Why on earth?!?
60  (void) putc((islower((int) c) ? (char) toupper((int) c) : c), fd);
61  */
62  (void) putc( c, fd);
63  }
64  }
65  else {
67  int col;
68  int i;
69  int line_num = 1;
70  string label = unformatted_label(u);
71  int em = unformatted_extra_margin(u);
72  int n = unformatted_number(u);
73  cons *lw = unformatted_words(u);
74 
75  if (label != (char *) NULL) {
76  fprintf(fd, "%-5s ", label);
77  }
79  fputs(" ", fd);
80  }
81 
82 
83  for (i = 0; i < em; i++)
84  putc(' ', fd);
85  col = 7+em;
86 
87  pips_assert("print_sentence", col <= MAX_LINE_LENGTH);
88 
89  while (lw) {
90  string w = STRING(CAR(lw));
91 
92  STRING(CAR(lw)) = NULL;
93  lw = CDR(lw);
94 
95  /* if the string fits on the current line: no problem */
96  if (col + strlen(w) <= 70) {
97  (void) fprintf(fd, "%s", w);
98  col += strlen(w);
99  }
100  /* if the string fits on one line:
101  * use the 88 algorithm to break as few
102  * syntactic constructs as possible */
103  else if(strlen(w) < 70-7-em) {
104  if (col + strlen(w) > 70) {
105  /* complete current line */
106  if (n > 0) {
107  for (i = col; i <= MAX_LINE_LENGTH; i++) putc(' ', fd);
108  fprintf(fd, "%04d", n);
109  }
110 
111  /* start a new line with its prefix */
112  putc('\n', fd);
113 
114  if(label != (char *) NULL
115  && (strcmp(label,"CDIR$")==0
116  || strcmp(label,"CDIR@")==0
117  || strcmp(label,"CMIC$")==0)) {
118  /* Special label for Cray directives */
119  fputs(label, fd);
120  fprintf(fd, "%d", (++line_num)%10);
121  }
122  else
123  fputs(" &", fd);
124 
125  for (i = 0; i < em; i++)
126  putc(' ', fd);
127 
128  col = 7+em;
129  }
130  (void) fprintf(fd, "%s", w);
131  col += strlen(w);
132  }
133  /* if the string has to be broken in at least two lines:
134  * new algorithmic part
135  * to avoid line overflow (FI, March 1993) */
136  else {
137  char * line = w;
138  int ncar;
139 
140  /* complete the current line */
141  ncar = MAX_LINE_LENGTH - col + 1;
142  fprintf(fd,"%.*s", ncar, line);
143  line += ncar;
144  col = 73;
145 
146  /*
147  if (n > 0) {
148  for (i = col; i <= 72; i++) putc(' ', fd);
149  fprintf(fd, "%04d", n);
150  }
151  */
152 
153  while(strlen(line)!=0) {
154  ncar = MIN(MAX_LINE_LENGTH - 7 +1, strlen(line));
155 
156  /* start a new line with its prefix but no indentation
157  * since string constants may be broken onto two lines */
158  putc('\n', fd);
159 
160  if(label != (char *) NULL
161  && (strcmp(label,"CDIR$")==0
162  || strcmp(label,"CDIR@")==0
163  || strcmp(label,"CMIC$")==0)) {
164  /* Special label for Cray directives */
165  fputs(label, fd);
166  (void) fprintf(fd, "%d", (++line_num)%10);
167  }
168  else
169  fputs(" &", fd);
170 
171  col = 7 ;
172  (void) fprintf(fd,"%.*s", ncar, line);
173  line += ncar;
174  col += ncar;
175  }
176  }
177  free(w);
178  }
179 
180  pips_assert("print_sentence", col <= MAX_LINE_LENGTH);
181 
182  if (n > 0) {
183  for (i = col; i <= MAX_LINE_LENGTH; i++) putc(' ', fd);
184  fprintf(fd, "%04d", n);
185  }
186  putc('\n', fd);
187  }
188 }
189 
191 {
192  print_sentence(stderr, s);
193 }
194 ␌
195 void print_text(fd, t)
196 FILE *fd;
197 text t;
198 {
200  print_sentence(fd, s);
201 }
202 
203 /* FI: print_text() should be fprint_text() and dump_text(), print_text() */
204 
205 void dump_text(t)
206 text t;
207 {
208  print_text(stderr, t);
209 }
210 
211 string words_to_string(lw)
212 cons *lw;
213 {
214  static char buffer[1024];
215 
216  buffer[0] = '\0';
217  MAPL(pw, {
218  string w = STRING(CAR(pw));
219  if (strlen(buffer)+strlen(w) > 1023) {
220  fprintf(stderr, "[words_to_string] buffer too small\n");
221  exit(1);
222  }
223  (void) strcat(buffer, w);
224  }, lw);
225 
226  return(strdup(buffer));
227 }
228 
229 /* SG: moved here from icfdg */
231 {
232  if (!sentence_formatted_p(sen))
234  else
235  return sentence_formatted(sen);
236 }
237 
238 /* SG: moved here from ricedg */
240 {
241  string str = strdup("");
242  string str_new;
243  MAP(SENTENCE, sen, {
244  str_new = strdup(concatenate(str, sentence_to_string(sen), NULL));
245  free(str);
246  str = str_new;
247  }, text_sentences(t));
248  return(str);
249 }
250 
251 void dump_words(list lw)
252 {
253  print_words(stderr, lw);
254 }
255 
256 /* print a list of strings */
258 {
259  dump_words(sl);
260 }
261 
262 ␌
263 void print_words(fd, lw)
264 FILE *fd;
265 cons *lw;
266 {
267  string s = words_to_string(lw);
268  fputs(s, fd);
269  free(s);
270 }
#define MIN(x, y)
minimum and maximum if they are defined somewhere else, they are very likely to be defined the same w...
#define STRING(x)
Definition: genC.h:87
void free(void *)
#define CAR(pcons)
Get the value of the first element of a list.
Definition: newgen_list.h:92
#define FOREACH(_fe_CASTER, _fe_item, _fe_list)
Apply/map an instruction block on all the elements of a list.
Definition: newgen_list.h:179
#define CDR(pcons)
Get the list less its first element.
Definition: newgen_list.h:111
#define MAPL(_map_list_cp, _code, _l)
Apply some code on the addresses of all the elements of a list.
Definition: newgen_list.h:203
#define MAP(_map_CASTER, _map_item, _map_code, _map_list)
Apply/map an instruction block on all the elements of a list (old fashioned)
Definition: newgen_list.h:226
bool prettyprint_language_is_fortran_p()
Definition: language.c:75
#define pips_assert(what, predicate)
common macros, two flavors depending on NDEBUG
Definition: misc-local.h:172
#define exit(code)
Definition: misc-local.h:54
string concatenate(const char *,...)
Return the concatenation of the given strings.
Definition: string.c:183
int fprintf()
test sc_min : ce test s'appelle par : programme fichier1.data fichier2.data ...
char * strdup()
static int line
FLEX_SCANNER.
Definition: scanner.c:852
static string buffer
Definition: string.c:113
The structure used to build lists in NewGen.
Definition: newgen_list.h:41
#define MAX_LINE_LENGTH
maximum length of a line when prettyprinting...
void dump_sentence(sentence s)
Definition: print.c:190
void print_sentence(FILE *fd, sentence s)
FI: just to make sure that text.h is built; pips-makemake -l does not tale into account a library who...
Definition: print.c:53
string sentence_to_string(sentence sen)
SG: moved here from icfdg.
Definition: print.c:230
void print_text(FILE *fd, text t)
Definition: print.c:195
string text_to_string(text t)
SG: moved here from ricedg.
Definition: print.c:239
void dump_words(list lw)
Definition: print.c:251
string words_to_string(cons *lw)
Definition: print.c:211
void dump_text(text t)
FI: print_text() should be fprint_text() and dump_text(), print_text()
Definition: print.c:205
void print_words(FILE *fd, cons *lw)
Definition: print.c:263
void dump_strings(list sl)
print a list of strings
Definition: print.c:257
#define unformatted_number(x)
Definition: text.h:151
#define SENTENCE(x)
newgen_unformatted_domain_defined
Definition: text.h:36
#define unformatted_extra_margin(x)
Definition: text.h:153
#define sentence_unformatted(x)
Definition: text.h:81
#define sentence_formatted(x)
Definition: text.h:78
#define text_sentences(x)
Definition: text.h:113
#define unformatted_words(x)
Definition: text.h:155
#define unformatted_label(x)
Definition: text.h:149
#define sentence_formatted_p(x)
Definition: text.h:76