PIPS
unsplit.c
Go to the documentation of this file.
1 /*
2 
3  $Id: unsplit.c 23182 2016-09-18 09:38:57Z 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 /*
28  * regenerate user files. the modules are put in a file which has the
29  * same name as the original user file. All regenerated files are stored
30  * in the subdirectory Src of the database. If some includes are
31  * generated, they should also be stored there.
32  *
33  */
34 
35 #include <stdio.h>
36 #include <string.h>
37 #include "genC.h"
38 #include "misc.h"
39 #include "resources.h"
40 #include "linear.h"
41 #include "ri.h"
42 #include "ri-util.h"
43 #include "pipsdbm.h"
44 #include "preprocessor.h"
45 
46 /* initial user file -> generated user file
47  */
49 
50 /* returns the new user file where to store user_file
51  */
52 static string
53 get_new_user_file(string dir_name, string preprocessed_user_file)
54 {
55  /* C or Fortran preprocessing may have or have not occured */
56  string user_file = preprocessed_to_user_file(preprocessed_user_file);
57  string s = hash_get(user_files, user_file);
58 
59  if (s==HASH_UNDEFINED_VALUE) {
60  FILE * tmp;
61  string name = pips_basename(user_file, NULL);
62 
63  pips_debug(1,
64  "It does not exist a file \"%p\"\n for user_file \"%s\"\n"
65  " and for preprocessed_user_file \"%s\".\n"
66  "in table %p\n",
67  s, user_file, preprocessed_user_file, user_files);
68  s = strdup(concatenate(dir_name, "/", name, NULL));
70  /* could check that the file does not exist...
71  * there could be homonymes...
72  */
73  if((tmp=fopen(s, "r"))!=NULL) {
74  pips_internal_error("Rewriting existing file \"%s\" for user_file \"%s\""
75  " and for preprocessed_user_file \"%s\".\n",
76  s, user_file, preprocessed_user_file);
77  fclose(tmp);
78  }
79  tmp = safe_fopen(s, "w");
82  fprintf(tmp, "!!\n!! file for %s\n!!\n", name);
83  }
84  else if(dot_c_file_p(user_file)) {
85  fprintf(tmp, "/*\n * file for %s\n */\n", name);
86  }
87  else {
88  pips_internal_error("unexpected user file suffix: \"%s\"", user_file);
89  }
90  safe_fclose(tmp, s);
91  free(name);
92  }
93  else {
94  pips_debug(1,
95  "It does exist a file \"%s\"\n for user_file \"%s\"\n"
96  " and for preprocessed_user_file \"%s\".\n"
97  "in table %p\n",
98  s, user_file, preprocessed_user_file, user_files);
99  }
100  free(user_file);
101  return s;
102 }
103 
104 static bool unsplit_internal(const string name, const string dbr)
105 {
107  int n = gen_array_nitems(modules), i;
109  string summary_name = db_build_file_resource_name
110  (DBR_USER_FILE, PROGRAM_RESOURCE_OWNER, ".txt");
111  string summary_full_name;
112  string dir_name;
113  FILE * summary;
114  int nfiles = 0; // Number of preexisting files in src_dir
115 
116  pips_assert("unused argument", name==name);
117 
118  debug_on("UNSPLIT_DEBUG_LEVEL");
119 
121 
123  summary_full_name = strdup(concatenate(dir_name, "/", summary_name, NULL));
124 
125  // Get rid of previous unsplitted files
126  if ((nfiles = safe_system_no_abort_no_warning(
127  concatenate("i=`ls ", src_dir, " | wc -l`; export i; exit $i ",
128  NULL)))>0) {
129  int failure = 0;
131  concatenate("/bin/rm ", src_dir, "/*", NULL))))
132  pips_user_warning("exit code for /bin/rm is %d\n", failure);
133  }
134 
135  summary = safe_fopen(summary_full_name, "w");
136  fprintf(summary, "! module / file\n");
137 
138  // each module PRINTED_FILE is appended to a new user file
139  // depending on its initial user file.
140  for (i=0; i<n; i++)
141  {
142  string module, user_file, new_user_file, printed_file, full;
143  FILE * out, * in;
144 
145  module = gen_array_item(modules, i);
146  user_file = db_get_memory_resource(DBR_USER_FILE, module, true);
147  new_user_file = get_new_user_file(src_dir, user_file);
148  printed_file = db_get_memory_resource(dbr, module, true);
149  full = strdup(concatenate(dir_name, "/", printed_file, NULL));
150  pips_debug(1, "Module: \"%s\", user_file: \"%s\", new_user_file: \"%s\","
151  "full: \"%s\"\n",
152  module, user_file, new_user_file, full);
153 
154  out = safe_fopen(new_user_file, "a");
155  in = safe_fopen(full, "r");
156 
157  safe_cat(out, in);
158  safe_fclose(out, new_user_file);
159  safe_fclose(in, full);
160 
161  fprintf(summary, "%s: %s\n", module, new_user_file);
162  free(full);
163  }
164 
165  // cleanup
166  safe_fclose(summary, summary_full_name);
167  free(summary_full_name), free(src_dir), free(dir_name);
168  gen_array_full_free(modules);
169  HASH_MAP(k, v, free(v), user_files);
172 
173  debug_off();
174 
175  // kind of a pseudo resource...
176  DB_PUT_FILE_RESOURCE(DBR_USER_FILE, PROGRAM_RESOURCE_OWNER, summary_name);
177 
178  return true;
179 }
180 
181 /* unsplit > PROGRAM.user_file
182  * < ALL.user_file
183  * < ALL.printed_file
184  */
185 bool unsplit(const string name)
186 {
187  return unsplit_internal(name, DBR_PRINTED_FILE);
188 }
189 
190 bool unsplit_parsed(const string name)
191 {
192  return unsplit_internal(name, DBR_PARSED_PRINTED_FILE);
193 }
gen_array_t db_get_module_list_initial_order(void)
Definition: database.c:1151
static FILE * out
Definition: alias_check.c:128
size_t gen_array_nitems(const gen_array_t a)
Definition: array.c:131
void gen_array_full_free(gen_array_t a)
Definition: array.c:77
void * gen_array_item(const gen_array_t a, size_t i)
Definition: array.c:143
FILE * safe_fopen(const char *filename, const char *what)
Definition: file.c:67
int safe_fclose(FILE *stream, const char *filename)
Definition: file.c:77
char * pips_basename(char *fullpath, char *suffix)
Definition: file.c:822
void safe_cat(FILE *out, FILE *in)
Definition: file.c:669
static FILE * user_file
These functions implements the writing of objects.
Definition: genClib.c:1485
void free(void *)
string db_get_memory_resource(const char *rname, const char *oname, bool pure)
Return the pointer to the resource, whatever it is.
Definition: database.c:755
#define DB_PUT_FILE_RESOURCE
Put a file resource into the current workspace database.
Definition: pipsdbm-local.h:85
hash_table hash_table_make(hash_key_type key_type, size_t size)
Definition: hash.c:294
void * hash_get(const hash_table htp, const void *key)
this function retrieves in the hash table pointed to by htp the couple whose key is equal to key.
Definition: hash.c:449
void hash_put(hash_table htp, const void *key, const void *val)
This functions stores a couple (key,val) in the hash table pointed to by htp.
Definition: hash.c:364
void hash_table_free(hash_table htp)
this function deletes a hash table that is no longer useful.
Definition: hash.c:327
static int failure(Pproblem XX, Pproblem UU, Pproblem VV, struct rproblem *RR)
Definition: isolve.c:1964
string db_build_file_resource_name(const char *rname, const char *oname, const char *suffix)
returns an allocated file name for a file resource.
Definition: lowlevel.c:169
string db_get_directory_name_for_module(const char *name)
returns the allocated and mkdir'ed directory for module name
Definition: lowlevel.c:150
#define debug_on(env)
Definition: misc-local.h:157
#define pips_debug
these macros use the GNU extensions that allow variadic macros, including with an empty list.
Definition: misc-local.h:145
#define pips_user_warning
Definition: misc-local.h:146
#define pips_assert(what, predicate)
common macros, two flavors depending on NDEBUG
Definition: misc-local.h:172
#define pips_internal_error
Definition: misc-local.h:149
#define debug_off()
Definition: misc-local.h:160
int safe_system_no_abort_no_warning(string)
the command to be executed
Definition: system.c:63
int safe_system_no_abort(string)
the command to be executed
Definition: system.c:47
string concatenate(const char *,...)
Return the concatenation of the given strings.
Definition: string.c:183
#define HASH_MAP(k, v, code, ht)
Definition: newgen_hash.h:60
@ hash_string
Definition: newgen_hash.h:32
#define HASH_UNDEFINED_VALUE
value returned by hash_get() when the key is not found; could also be called HASH_KEY_NOT_FOUND,...
Definition: newgen_hash.h:56
#define hash_table_undefined
Value of an undefined hash_table.
Definition: newgen_hash.h:49
static char * module
Definition: pips.c:74
#define WORKSPACE_SRC_SPACE
Definition: pipsdbm-local.h:32
#define PROGRAM_RESOURCE_OWNER
Definition: pipsdbm-local.h:29
string db_get_current_workspace_directory(void)
Definition: workspace.c:96
bool dot_c_file_p(string)
Test if a name ends with .c.
Definition: source_file.c:661
bool dot_f95_file_p(string)
Test if a name ends with .f95.
Definition: source_file.c:655
string preprocessed_to_user_file(string)
Allocate a new string containing the user file name, before preprocessing.
Definition: source_file.c:617
bool dot_f90_file_p(string)
Test if a name ends with .f90.
Definition: source_file.c:649
bool dot_f_file_p(string)
Test if a name ends with .f.
Definition: source_file.c:643
int fprintf()
test sc_min : ce test s'appelle par : programme fichier1.data fichier2.data ...
char * strdup()
@ full
Definition: union-local.h:65
static bool unsplit_internal(const string name, const string dbr)
Definition: unsplit.c:104
bool unsplit(const string name)
unsplit > PROGRAM.user_file < ALL.user_file < ALL.printed_file
Definition: unsplit.c:185
static string get_new_user_file(string dir_name, string preprocessed_user_file)
returns the new user file where to store user_file
Definition: unsplit.c:53
bool unsplit_parsed(const string name)
Definition: unsplit.c:190
static hash_table user_files
initial user file -> generated user file
Definition: unsplit.c:48