home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
gdead.berkeley.edu
/
gdead.berkeley.edu.tar
/
gdead.berkeley.edu
/
pub
/
cad-tools
/
ciftomann.tar
/
cmd_dir
/
ciftomann.c
next >
Wrap
C/C++ Source or Header
|
1988-01-28
|
8KB
|
385 lines
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <signal.h>
#include "cmd.h"
#include "pg_desc.h"
#include "fd.h"
#define NIL(type) ( (type *) 0)
typedef struct cmd_tag {
char *inlayer;
int invert_flag;
int mod_factor;
char *outlayer;
struct cmd_tag *next;
} cmd_type;
extern char *bin_lib_name;
cmd_type *cmd_list;
char *tmp_dir = TMP_DIR;
char *cif_file_name;
char *cmd_file_name;
FILE *cmd_file,*cif_file;
extern FILE *yyin; /* the input file used by the command parser */
FILE *popen(),*fopen();
char *user;
char *getlogin();
char driver[101];
char basename[11];
char cif_tmp_name[101];
char out_tmp_name[101];
char *user_log_file = ".";
char *user_out_file = (char *) 0;
int dont_ask = 0;
int silent = 0;
int already_flat = 0;
int pg_modified = 0;
float scale = 1;
int num_masks = 0;
/*
* ciftomann reads and parses the command file,
* copies the cif file to a temporary directory
* and starts off driver, the routine that actually
* starts the processing
*/
main(argc,argv)
int argc;
char **argv;
{
char cmd[101];
FILE *d_file;
init_pg();
get_args(argc,argv);
yyparse();
sprintf(driver,"%s/driver",bin_lib_name);
if (!already_flat) {
sprintf(basename,"%4s%05d","mann",getpid());
}
if (!silent) {
pretty_print_cmds(stdout);
}
user = getlogin();
if ( !dont_ask ) {
char c;
printf("Do you still want to do this? (y or n) : ");
scanf("%1s",&c);
if ( c != 'y' ) {
printf("Job deleted\n");
exit(1);
}
}
build_name(cif_tmp_name,"cif");
build_name(out_tmp_name,"out");
catch_signals();
fflush(stdout);
if ( strcmp(user_log_file,".") ) {
/* the user requested a log file */
set_up_log_file();
}
if (!already_flat) {
if ( !silent ) {
fprintf(stderr,"copying your cif file...");
}
sprintf(cmd,"cp %s %s",cif_file_name,cif_tmp_name);
if ( system(cmd) != 0) {
fprintf(stderr,"Panic : cannot copy %s to %s\n",cif_file_name,
cif_tmp_name);
terminate();
}
if ( !silent ) {
fprintf(stderr,"done.\n");
}
}
sprintf(cmd,"%s -%c%c %s %s ",driver, (silent ? 's' : 'n'),
(already_flat ? 'a' : 'f'), user, cif_tmp_name);
if ( (d_file = popen(cmd,"w")) == NULL) {
fprintf(stderr,"Panic : can't find %s\n",driver);
exit(1);
}
/* pass the commands to the driver */
write_cmds(d_file);
#ifdef DEBUG
write_cmds(stderr);
fflush(stderr);
#endif
/* wait for the process to finish */
pclose(d_file);
clean_up();
exit(0);
}
terminate()
{
clean_up();
exit(1);
}
/* clean up all the temporary files and directories */
clean_up()
{
char cmd_string[101];
struct stat buf;
int out_size;
if ( stat(out_tmp_name,&buf) == 0 && (out_size=buf.st_size) != 0 ) {
if ( user_out_file != NULL ) {
sprintf(cmd_string,"cp %s %s",out_tmp_name,user_out_file);
if ( system(cmd_string) != 0 ||
stat(user_out_file,&buf) != 0 ||
buf.st_size != out_size) {
fprintf(stderr,"Panic : cannot properly copy output file into your file %s\n",
user_out_file);
fprintf(stderr,"The temporary copy is in %s, pick it up soon\n",
out_tmp_name);
fclose(stderr);
} else {
unlink(out_tmp_name);
}
} else {
unlink(out_tmp_name);
}
} else {
fclose(stderr);
unlink(out_tmp_name);
}
unlink(cif_tmp_name);
}
/* output the commands in suitable format to the driver */
write_cmds(outfile)
FILE *outfile;
{
cmd_type *ptr;
fprintf(outfile,"%s %s %d %d %d %d %d %e\n",tmp_dir,basename,
pg_desc.stage_min,pg_desc.stage_max,pg_desc.aperture_min,
pg_desc.aperture_max, pg_desc.grid_size,
scale/pg_desc.convert_factor);
fprintf(outfile,"%d\n",num_masks);
for ( ptr = cmd_list; ptr != NIL(cmd_type); ptr = ptr->next) {
fprintf(outfile,"%s %d %d %s\n",ptr->inlayer,ptr->mod_factor,
ptr->invert_flag,ptr->outlayer);
}
}
build_name(array,tail)
char *array,*tail;
{
sprintf(array,"%s/%s_%s",tmp_dir,basename,tail);
}
set_up_log_file()
{
FILE *log_file;
if ( !access(user_log_file,0) ) {
/* to keep from over-writing something important,
* the log file must not previously exist
*/
fprintf(stderr,"Error : log file '%s' already exists\n",
user_log_file);
terminate();
}
if ( (log_file = fopen(user_log_file,"w")) == NULL) {
fprintf(stderr,"Cannot open your log file '%s', I quit\n",
user_log_file);
terminate();
}
pretty_print_cmds(log_file); /* an extra copy */
dup2(fileno(log_file), STDERR); /* connect the log file to stderr */
fclose(log_file);
}
/*
* Print the commands out in a user-readable fashion so that
* he may confirm the processing
*/
pretty_print_cmds(outfile)
FILE *outfile;
{
cmd_type *ptr;
fprintf(outfile,"\nGenerating pg code for ");
if (pg_modified) {
fprintf(outfile, "**MODIFIED** ");
}
fprintf(outfile, "%s :\n",pg_desc.name);
fprintf(outfile,"\tMinimum and maximum stage positions are (%d,%d) - (%d,%d)\n",
pg_desc.stage_min,pg_desc.stage_min,pg_desc.stage_max,
pg_desc.stage_max);
fprintf(outfile,"\tMinimum and maximum aperture positions are (%d,%d) - (%d,%d)\n",
pg_desc.aperture_min,pg_desc.aperture_min,pg_desc.aperture_max,
pg_desc.aperture_max);
fprintf(outfile,"\tin units of %g microns\n",
pg_desc.convert_factor/100.);
fprintf(outfile,"\nAll coordinates will be multiplied by %g",scale);
fprintf(outfile," and then rounded to the\n");
fprintf(outfile,"nearest multiple of %g microns\n",
pg_desc.grid_size*pg_desc.convert_factor/100.);
fprintf(outfile,"\nThe layers will be processed as follows :\n\n");
for(ptr = cmd_list; ptr != NIL(cmd_type); ptr = ptr->next) {
fprintf(outfile,"\tLayer %s",ptr->inlayer);
if (ptr->mod_factor > 0) {
fprintf(outfile," grown by %d",ptr->mod_factor);
} else if (ptr->mod_factor < 0) {
fprintf(outfile," shrunk by %d",-ptr->mod_factor);
} else {
fprintf(outfile," with no grow/shrink");
}
if (ptr->invert_flag) {
fprintf(outfile," and inverted");
}
if ( ptr->inlayer != ptr->outlayer ) {
fprintf(outfile,", outputing it as layer %s", ptr->outlayer);
}
putc('\n',outfile);
}
putc('\n',outfile);
if (already_flat) {
fprintf(outfile, "\nUsing intermediate files in %s/%s*\n",
tmp_dir, basename);
}
fflush(outfile);
}
make_cmd(inlayer,delta,invert_flag,outlayer)
char *inlayer,*outlayer;
int delta,invert_flag;
{
static cmd_type *last_cmd = NIL(cmd_type);
if (last_cmd == NIL(cmd_type)) {
last_cmd = cmd_list = (cmd_type *) malloc(sizeof(cmd_type));
} else {
last_cmd->next = (cmd_type *) malloc(sizeof(cmd_type));
last_cmd = last_cmd->next;
}
last_cmd->inlayer = inlayer;
last_cmd->mod_factor = delta;
last_cmd->invert_flag = invert_flag;
last_cmd->outlayer = outlayer;
num_masks++;
}
extern int line_number;
error(str)
char *str;
{
fprintf(stderr, "error in %s around line %d : %s\n", cmd_file_name,
line_number, str);
exit(1);
}
yyerror(str)
char *str;
{
error(str);
}
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);
}
/* check to see if a file exists and is non-empty */
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);
}