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 >
C/C++ Source or Header  |  1988-01-28  |  8KB  |  385 lines

  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <signal.h>
  5.  
  6. #include "cmd.h"
  7. #include "pg_desc.h"
  8. #include "fd.h"
  9.  
  10. #define NIL(type) ( (type *) 0)
  11.  
  12. typedef struct cmd_tag {
  13.     char *inlayer;
  14.     int invert_flag;
  15.     int mod_factor;
  16.     char *outlayer;
  17.     struct cmd_tag *next;
  18. } cmd_type;
  19.  
  20. extern char *bin_lib_name;
  21.  
  22. cmd_type *cmd_list;
  23.  
  24. char *tmp_dir = TMP_DIR;
  25.  
  26. char *cif_file_name;
  27. char *cmd_file_name;
  28. FILE *cmd_file,*cif_file;
  29. extern FILE *yyin;    /* the input file used by the command parser */
  30. FILE *popen(),*fopen();
  31.  
  32. char *user;
  33. char *getlogin();
  34.  
  35. char driver[101];
  36.  
  37. char basename[11];
  38.  
  39. char cif_tmp_name[101];
  40. char out_tmp_name[101];
  41.  
  42. char *user_log_file = ".";
  43. char *user_out_file = (char *) 0;
  44.  
  45. int dont_ask = 0;
  46. int silent = 0;
  47. int already_flat = 0;
  48. int pg_modified = 0;
  49.  
  50. float scale = 1;
  51.  
  52. int num_masks = 0;
  53.  
  54.     /*
  55.      * ciftomann reads and  parses the command file, 
  56.      * copies the cif file to a temporary directory
  57.      * and starts off driver, the routine that actually
  58.      * starts the processing
  59.      */
  60.  
  61. main(argc,argv)
  62. int argc;
  63. char **argv;
  64. {
  65.     
  66.     char cmd[101];
  67.     FILE *d_file;
  68.  
  69.     init_pg();
  70.     get_args(argc,argv);
  71.  
  72.     yyparse();
  73.  
  74.     sprintf(driver,"%s/driver",bin_lib_name);
  75.  
  76.     if (!already_flat) {
  77.     sprintf(basename,"%4s%05d","mann",getpid());
  78.     }
  79.  
  80.     if (!silent) {
  81.     pretty_print_cmds(stdout);
  82.     }
  83.  
  84.     user = getlogin();
  85.  
  86.     if ( !dont_ask ) {
  87.     char c;
  88.  
  89.     printf("Do you still want to do this? (y or n) : ");
  90.     scanf("%1s",&c);
  91.     if ( c != 'y' ) {
  92.         printf("Job deleted\n");
  93.         exit(1);
  94.     }
  95.     }
  96.  
  97.  
  98.     build_name(cif_tmp_name,"cif");
  99.     build_name(out_tmp_name,"out");
  100.  
  101.     catch_signals();
  102.  
  103.     fflush(stdout);
  104.  
  105.     if ( strcmp(user_log_file,".") ) { 
  106.     
  107.     /* the user requested a log file */
  108.  
  109.     set_up_log_file();
  110.     }
  111.  
  112.     if (!already_flat) {
  113.     if ( !silent ) {
  114.         fprintf(stderr,"copying your cif file...");
  115.     }
  116.  
  117.     sprintf(cmd,"cp %s %s",cif_file_name,cif_tmp_name);
  118.  
  119.     if ( system(cmd) != 0) {
  120.         fprintf(stderr,"Panic : cannot copy %s to %s\n",cif_file_name,
  121.             cif_tmp_name);
  122.         terminate();
  123.     }
  124.  
  125.     if ( !silent ) {
  126.         fprintf(stderr,"done.\n");
  127.     }
  128.     }
  129.  
  130.     sprintf(cmd,"%s -%c%c  %s %s ",driver, (silent ? 's' : 'n'),
  131.         (already_flat ? 'a' : 'f'), user, cif_tmp_name);
  132.  
  133.     if ( (d_file = popen(cmd,"w")) == NULL) {
  134.     fprintf(stderr,"Panic : can't find %s\n",driver);
  135.     exit(1);
  136.     }
  137.  
  138.     /* pass the commands to the driver */
  139.  
  140.     write_cmds(d_file);
  141.  
  142. #ifdef DEBUG
  143.     write_cmds(stderr);
  144.     fflush(stderr);
  145. #endif
  146.  
  147.     /* wait for the process to finish */
  148.     pclose(d_file);
  149.     clean_up();
  150.     exit(0);
  151.  
  152. }
  153.  
  154. terminate()
  155. {
  156.     clean_up();
  157.     exit(1);
  158. }
  159.  
  160.     /* clean up all the temporary files and directories */
  161.  
  162. clean_up()
  163. {
  164.  
  165.     char cmd_string[101];
  166.     struct stat buf;
  167.     int out_size;
  168.  
  169.  
  170.     if ( stat(out_tmp_name,&buf) == 0 && (out_size=buf.st_size) != 0 ) {
  171.  
  172.     if ( user_out_file != NULL ) {
  173.         sprintf(cmd_string,"cp %s %s",out_tmp_name,user_out_file);
  174.  
  175.         if ( system(cmd_string) != 0 ||
  176.             stat(user_out_file,&buf) != 0 || 
  177.             buf.st_size != out_size) {
  178.  
  179.         fprintf(stderr,"Panic : cannot properly copy output file into your file %s\n",
  180.             user_out_file);
  181.         fprintf(stderr,"The temporary copy is in %s, pick it up soon\n",
  182.             out_tmp_name);
  183.         fclose(stderr);
  184.         } else {
  185.         unlink(out_tmp_name);
  186.         }
  187.     } else {
  188.         unlink(out_tmp_name);
  189.     }
  190.  
  191.     } else {
  192.     fclose(stderr);
  193.     unlink(out_tmp_name);
  194.     }
  195.  
  196.     unlink(cif_tmp_name);
  197. }
  198.  
  199.     /* output the commands in suitable format to the driver */
  200.  
  201. write_cmds(outfile)
  202. FILE *outfile;
  203. {
  204.     cmd_type *ptr;
  205.  
  206.     fprintf(outfile,"%s %s %d %d %d %d %d %e\n",tmp_dir,basename,
  207.         pg_desc.stage_min,pg_desc.stage_max,pg_desc.aperture_min,
  208.         pg_desc.aperture_max, pg_desc.grid_size,
  209.         scale/pg_desc.convert_factor);
  210.     fprintf(outfile,"%d\n",num_masks);
  211.  
  212.     for ( ptr = cmd_list; ptr != NIL(cmd_type); ptr = ptr->next) {
  213.     fprintf(outfile,"%s %d %d %s\n",ptr->inlayer,ptr->mod_factor,
  214.         ptr->invert_flag,ptr->outlayer);
  215.     }
  216.  
  217. }
  218.  
  219. build_name(array,tail)
  220. char *array,*tail;
  221. {
  222.     
  223.     sprintf(array,"%s/%s_%s",tmp_dir,basename,tail);
  224.  
  225. }
  226.  
  227. set_up_log_file()
  228. {
  229.  
  230.     FILE *log_file;
  231.  
  232.     if ( !access(user_log_file,0) ) {
  233.  
  234.         /* to keep from over-writing something important,
  235.          * the log file must not previously exist
  236.          */
  237.  
  238.     fprintf(stderr,"Error : log file '%s' already exists\n",
  239.         user_log_file);
  240.     terminate();
  241.     }
  242.  
  243.     if ( (log_file = fopen(user_log_file,"w")) == NULL) {
  244.     fprintf(stderr,"Cannot open your log file '%s', I quit\n",
  245.         user_log_file);
  246.     terminate();
  247.     }
  248.  
  249.     pretty_print_cmds(log_file);    /* an extra copy */
  250.  
  251.     dup2(fileno(log_file), STDERR);    /* connect the log file to stderr */
  252.     fclose(log_file);
  253. }
  254.  
  255.     /* 
  256.      *    Print the commands out in a user-readable fashion so that
  257.      *    he may confirm the processing
  258.      */
  259.     
  260. pretty_print_cmds(outfile)
  261. FILE *outfile;
  262. {
  263.     cmd_type *ptr;
  264.  
  265.     fprintf(outfile,"\nGenerating pg code for ");
  266.  
  267.     if (pg_modified) {
  268.     fprintf(outfile, "**MODIFIED** ");
  269.     }
  270.  
  271.     fprintf(outfile, "%s :\n",pg_desc.name);
  272.  
  273.     fprintf(outfile,"\tMinimum and maximum stage positions are (%d,%d) - (%d,%d)\n",
  274.         pg_desc.stage_min,pg_desc.stage_min,pg_desc.stage_max,
  275.         pg_desc.stage_max);
  276.     fprintf(outfile,"\tMinimum and maximum aperture positions are (%d,%d) - (%d,%d)\n",
  277.         pg_desc.aperture_min,pg_desc.aperture_min,pg_desc.aperture_max,
  278.         pg_desc.aperture_max);
  279.     fprintf(outfile,"\tin units of %g microns\n",
  280.         pg_desc.convert_factor/100.);
  281.  
  282.     fprintf(outfile,"\nAll coordinates will be multiplied by %g",scale);
  283.     fprintf(outfile," and then rounded to the\n");
  284.     fprintf(outfile,"nearest multiple of %g microns\n",
  285.         pg_desc.grid_size*pg_desc.convert_factor/100.);
  286.     fprintf(outfile,"\nThe layers will be processed as follows :\n\n");
  287.  
  288.     for(ptr = cmd_list; ptr != NIL(cmd_type); ptr = ptr->next) {
  289.     fprintf(outfile,"\tLayer %s",ptr->inlayer);
  290.     if (ptr->mod_factor > 0) {
  291.         fprintf(outfile," grown by %d",ptr->mod_factor);
  292.     } else if (ptr->mod_factor < 0) {
  293.         fprintf(outfile," shrunk by %d",-ptr->mod_factor);
  294.     } else {
  295.         fprintf(outfile," with no grow/shrink");
  296.     }
  297.     if (ptr->invert_flag) {
  298.         fprintf(outfile," and inverted");
  299.     }
  300.     if ( ptr->inlayer != ptr->outlayer ) {
  301.         fprintf(outfile,", outputing it as layer %s", ptr->outlayer);
  302.     }
  303.     putc('\n',outfile);
  304.     }
  305.     putc('\n',outfile);
  306.  
  307.     if (already_flat) {
  308.     fprintf(outfile, "\nUsing intermediate files in %s/%s*\n",
  309.             tmp_dir, basename);
  310.     }
  311.  
  312.     fflush(outfile);
  313. }
  314.         
  315. make_cmd(inlayer,delta,invert_flag,outlayer)
  316. char *inlayer,*outlayer;
  317. int delta,invert_flag;
  318. {
  319.     static cmd_type *last_cmd = NIL(cmd_type);
  320.  
  321.     if (last_cmd == NIL(cmd_type)) {
  322.     last_cmd = cmd_list = (cmd_type *) malloc(sizeof(cmd_type));
  323.     } else {
  324.     last_cmd->next = (cmd_type *) malloc(sizeof(cmd_type));
  325.     last_cmd = last_cmd->next;
  326.     }
  327.  
  328.     last_cmd->inlayer = inlayer;
  329.     last_cmd->mod_factor = delta;
  330.     last_cmd->invert_flag = invert_flag;
  331.     last_cmd->outlayer = outlayer;
  332.  
  333.     num_masks++;
  334. }
  335.  
  336. extern int line_number;
  337.  
  338. error(str)
  339. char *str;
  340. {
  341.     fprintf(stderr, "error in %s around line %d : %s\n", cmd_file_name,
  342.         line_number, str);
  343.     exit(1);
  344. }
  345.  
  346. yyerror(str)
  347. char *str;
  348. {
  349.     error(str);
  350. }
  351.  
  352. catch_signals()
  353. {
  354.     if (signal(SIGHUP, SIG_IGN) != SIG_IGN) {
  355.     signal(SIGHUP, terminate);
  356.     }
  357.  
  358.     if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
  359.     signal(SIGINT, terminate);
  360.     }
  361.  
  362.     signal(SIGPIPE, terminate);
  363.  
  364.     signal(SIGTERM, terminate);
  365. }
  366.  
  367.     /* check to see if a file exists and is non-empty */
  368.  
  369. non_empty(file_name)
  370. char *file_name;
  371. {
  372.     
  373.     struct stat buf;
  374.  
  375.     if ( stat(file_name,&buf) != 0) {
  376.     return(0);
  377.     }
  378.  
  379.     if (buf.st_size <= 0) {
  380.     return(0);
  381.     }
  382.  
  383.     return(1);
  384. }
  385.