home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
gdead.berkeley.edu
/
gdead.berkeley.edu.tar
/
gdead.berkeley.edu
/
pub
/
cad-tools
/
ciftomann.tar
/
driver_dir
/
driver.c
< prev
next >
Wrap
C/C++ Source or Header
|
1988-01-28
|
8KB
|
398 lines
#include "fd.h"
#include <stdio.h>
#include <signal.h>
#define FILE_SIZE 15
#define PATH_SIZE 101
#define NUM_SIZE 20
#define FLAG_SIZE 3
#define BB_SIZE 4*NUM_SIZE
#define PG_SIZE 6*NUM_SIZE
#define NIL(type) ( (type *) 0)
typedef struct cmd_tag {
char inlayer[5];
int invert_flag;
int mod_factor;
char *outlayer[5];
struct cmd_tag *next;
} cmd_type;
/* the path names of all the sub processes */
extern char flatten[];
extern char edger[];
extern char sort[];
extern char merge[];
extern char resort[];
extern char boxer[];
extern char smash[];
cmd_type *get_cmd();
char bb_tmp_name[FILE_SIZE],
out_tmp_name[FILE_SIZE],
in_tmp_name[FILE_SIZE],
log_tmp_name[FILE_SIZE];
char cif_file_name[PATH_SIZE];
/* the elements of the per-layer pipelines */
char *layer_cmdv[] = { merge, resort, boxer, smash, NIL(char) };
/* the elements of the initial flatten and edging pipeline */
char *flat_cmdv[] = { flatten, edger, NIL(char) };
char tmp_dir[101];
char basename[FILE_SIZE];
char mod_string[NUM_SIZE], invert_string[FLAG_SIZE],
bb_string[BB_SIZE], pg_string[PG_SIZE], round_string[2*NUM_SIZE];
/* the individual arguments for the per-layer processes */
char *merge_argv[] = { "merge", invert_string, bb_string, mod_string,
NIL(char) },
*resort_argv[] = { "resort", mod_string, NIL(char) },
*boxer_argv[] = { "boxer", NIL(char) },
*smash_argv[] = { "smash", pg_string, NIL(char) };
char **layer_argvv[] = {
merge_argv,
resort_argv,
boxer_argv,
smash_argv,
NIL(char *)
};
/* the individual arguements for the flatten/edger pipeline */
char *flatten_argv[] = { "flatten", cif_file_name, NIL(char) },
*edger_argv[] = { "edger", basename, round_string, bb_tmp_name, tmp_dir, NIL(char) };
char **flat_argvv[] = {
flatten_argv,
edger_argv,
NIL(char *)
};
char *user;
FILE *fopen();
/*
* Driver constructs the pipeline elements and their arguments
* from the command file and starts the pipelines going.
*/
main(argc,argv)
int argc;
char **argv;
{
FILE *log_file,*bb_file,*out_file,*in_file;
int lower_left_x, lower_left_y, upper_right_x, upper_right_y;
int stage_min, stage_max, aperture_min, aperture_max, grid_size;
int num_masks, mask;
int already_flat = 0;
int silent = 0;
int status;
int mod_factor, invert_flag;
char inlayer[5], outlayer[5];
float scale_factor;
cmd_type *cmd_list, *last;
set_names();
catch_signals();
if ( argv[1][1] == 's' ) {
silent = 1;
}
if (argv[1][2] == 'a') {
already_flat = 1;
}
user = argv[2];
strcpy(cif_file_name,argv[3]);
/*
* read the various parameters for the job
*/
if ( (scanf("%s %s %d %d %d %d %d %f",tmp_dir,basename,&stage_min,
&stage_max,&aperture_min, &aperture_max, &grid_size,
&scale_factor) != 8)
|| (scanf("%d ",&num_masks) != 1) ) {
fprintf(stderr,"Panic : internal commands to the driver have been hashed\n");
exit(1);
}
/* build the pattern generator descriptor */
sprintf(pg_string,"%d %d %d %d %d %f", stage_min, stage_max,
aperture_min, aperture_max, grid_size, scale_factor);
sprintf(round_string,"%g %d", scale_factor, grid_size);
/* build a linked list of the commands */
last = cmd_list = get_cmd();
for ( mask = 2; mask <= num_masks; mask++) {
last->next = get_cmd();
last = last->next;
}
/*
* Change to the temporary directory and start up the various
* files needed
*/
chdir(tmp_dir);
sprintf(log_tmp_name,"%s_log",basename);
sprintf(out_tmp_name,"%s_out",basename);
sprintf(bb_tmp_name,"%s_bb",basename);
if ( (out_file = fopen(out_tmp_name,"w")) == NULL) {
fprintf(stderr,"Could not open the output file %s .\n",
out_tmp_name);
fprintf(stderr,"Job was not started\n");
terminate();
}
dup2(fileno(out_file),STDOUT); /* connect out_file to stdout */
fclose(out_file);
/*
* Flatten the cif and then edge and sort the flattened cif
* producing a seperate "basename.layername" file for each
* layer. ( edger popens the sorter itself, since it does
* the layer file manipulation )
*/
if ( !silent ) {
fprintf(stderr,"\nFlattening and translating the cif.\n");
}
#ifdef DEBUG
print_pipe(flat_cmdv,flat_argvv);
#endif
if (!already_flat) {
status = pipeline(stdin,stderr,flat_cmdv,flat_argvv,stdout);
}
if ( status != 0 ) {
fprintf(stderr,"Job terminated\n");
terminate();
}
/*
* Read the bounding box information produced by flatten
* in "basename_bb" file
*/
if ( (bb_file = fopen(bb_tmp_name,"r")) == NULL) {
fprintf(stderr,"Panic : could not open the temp file %s .\n",
bb_tmp_name);
fprintf(stderr,"Job terminated\n");
terminate();
}
if ( fscanf(bb_file,"%d %d %d %d", &lower_left_x, &lower_left_y,
&upper_right_x, &upper_right_y) != 4 ) {
fprintf(stderr,"Panic : temp file %s has been trashed\n",
bb_tmp_name);
fprintf(stderr,"Job terminated\n");
terminate();
}
fclose(bb_file);
sprintf(bb_string,"%d %d %d %d",lower_left_x,lower_left_y,
upper_right_x, upper_right_y);
/*
* The main loop : Process each layer according to the
* information in the command file and append the result
* to the output file
*/
for ( mask = 1; mask <= num_masks; mask++) {
sprintf(in_tmp_name,"%s.%s",basename,cmd_list->inlayer);
if ( (in_file = fopen(in_tmp_name,"r")) == NULL) {
fprintf(stderr,"Panic : either temp layer file %s has been trashed\n",
in_tmp_name);
fprintf(stderr,"or layer %s was never used in your chip\n",
cmd_list->inlayer);
fprintf(stderr,"Job terminated\n");
terminate();
}
printf("L %s;\n",cmd_list->outlayer);
fflush(stdout);
if ( !silent ) {
fprintf(stderr,"Processing layer %s.\n",cmd_list->outlayer);
}
sprintf(mod_string,"%d",cmd_list->mod_factor);
sprintf(invert_string,"-%c",(cmd_list->invert_flag ? 'i' : 'n'));
#ifdef DEBUG
print_pipe(layer_cmdv,layer_argvv);
#endif
/* start the pipeline */
status = pipeline(in_file,stderr,layer_cmdv,layer_argvv,stdout);
if ( status != 0) {
fprintf(stderr,"Job terminated\n");
terminate();
}
fclose(in_file);
cmd_list = cmd_list->next;
}
printf("E\n");
if ( !silent ) {
fprintf(stderr,"\nTranslation was successful.\n");
}
clean_up();
exit(0);
}
clean_up()
{
char cmd_string[101];
int cmd_stat;
fclose(stdout);
unlink(bb_tmp_name);
delete_layer_temps(basename);
}
terminate()
{
kill_pipe();
clean_up();
exit(1);
}
delete_layer_temps(basename)
char *basename;
{
char string[101];
sprintf(string,"rm %s.*",basename);
if ( vfork() == 0) {
execl("/bin/csh","csh","-f","-c",string,0);
}
}
#include <sys/types.h>
#include <sys/stat.h>
exists(file_name)
char *file_name;
{
struct stat buf;
if ( stat(file_name,&buf) != 0) {
return(0);
}
return(1);
}
non_empty(file_name)
char *file_name;
{
struct stat buf;
if ( stat(file_name,&buf) != 0) {
return(0);
}
if (buf.st_size <= 0) {
return(0);
}
return(1);
}
#ifdef DEBUG
print_pipe(cmdv,argvv)
char **cmdv;
char ***argvv;
{
char *cmd,**argv,*arg;
while ( (cmd = *(cmdv++)) != NIL(char) ) {
argv = *(argvv++);
fprintf(stderr,"%s ",cmd);
while ( (arg = *(argv++)) != NIL(char) ) {
fprintf(stderr,"%s ",arg);
}
putc('\n',stderr);
}
}
#endif
/*
* read the commands being sent by ciftomann
*/
cmd_type *get_cmd()
{
cmd_type *new;
new = (cmd_type *) malloc( sizeof(cmd_type));
if (scanf("%s %d %d %s", new->inlayer, &new->mod_factor,
&new->invert_flag, new->outlayer) != 4) {
fprintf(stderr,"Panic : internal commands have been trashed\n");
fprintf(stderr,"Job terminated\n");
terminate();
}
return(new);
}
catch_signals()
{
if (signal(SIGHUP, SIG_IGN) != SIG_IGN) {
signal(SIGHUP, terminate);
}
if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
signal(SIGINT, terminate);
}
signal(SIGPIPE, terminate);
signal(SIGTERM, terminate);
}