VIS

src/vm/vmMain.c

Go to the documentation of this file.
00001 
00032 #include "vmInt.h"
00033 
00034 static char rcsid[] UNUSED = "$Id: vmMain.c,v 1.6 2004/07/16 00:09:59 wangc Exp $";
00035 
00036 /*---------------------------------------------------------------------------*/
00037 /* Variable declarations                                                     */
00038 /*---------------------------------------------------------------------------*/
00039 FILE *vis_stderr;
00040 FILE *vis_stdout;
00041 FILE *vis_historyFile;
00042 FILE *vis_stdpipe;
00043 array_t *vm_commandHistoryArray;
00044 char *vm_programName;
00045 
00046 
00049 /*---------------------------------------------------------------------------*/
00050 /* Static function prototypes                                                */
00051 /*---------------------------------------------------------------------------*/
00052 
00053 static int VisrcSource(Hrc_Manager_t ** hmgr);
00054 static void UsagePrint(char * program);
00055 static int TypeCheck(char * s);
00056 
00060 /*---------------------------------------------------------------------------*/
00061 /* Definition of exported functions                                          */
00062 /*---------------------------------------------------------------------------*/
00063 
00075 int
00076 main(
00077   int  argc,
00078   char ** argv)
00079 {
00080   int c,
00081     status,
00082     batch,          /* 0 iff we have an interactive run (-c or -f) */
00083     initial_source, /* 1 iff we have to read .visrc                */
00084     initial_read,
00085     final_write;
00086   int quit_flag;
00087   char readcmd[20],
00088     writecmd[20];
00089   char *dummy,
00090     *cmdline,
00091     *cmdline1,
00092     *infile,        /* where to read input */
00093     *outfile;       /* where to write output */
00094   Hrc_Manager_t *globalHmgr;
00095   
00096   vm_programName = argv[0];
00097   quit_flag = -1;     /* Quick quit */
00098 
00099   VmInit();
00100   globalHmgr = Hrc_ManagerAlloc();
00101 
00102   cmdline = util_strsav("");
00103   (void) strcpy(readcmd, "read_blif_mv");
00104   (void) strcpy(writecmd, "write_blif_mv");
00105   infile = "-";
00106   outfile = "-";
00107   vm_commandHistoryArray = array_alloc(char *, 0);
00108   initial_source = 1;
00109   initial_read = 1;
00110   final_write = 1;
00111   batch = 0;
00112   util_getopt_reset();
00113   while ((c = util_getopt(argc, argv, "c:hf:o:st:T:x")) != EOF) {
00114     switch(c) {
00115         case 'c':
00116           FREE(cmdline);
00117           cmdline = util_strsav(util_optarg);
00118           batch = 1;
00119           break;
00120 
00121         case 'f':
00122           FREE(cmdline);
00123           cmdline = ALLOC(char, strlen(util_optarg) + 20);
00124           (void) sprintf(cmdline, "source %s", util_optarg);
00125           batch = 1;
00126           break;
00127           
00128         case 'h':
00129             UsagePrint(argv[0]);
00130             break;
00131              
00132         case 'o':
00133           outfile = util_optarg;
00134           break;
00135 
00136         case 's':
00137           initial_source = 0;
00138           break;
00139 
00140         case 't':
00141           if (TypeCheck(util_optarg)) {
00142             if (strcmp(util_optarg, "none") == 0) {
00143               initial_read = 0;
00144             }
00145             else {
00146               (void) sprintf(readcmd, "read_%s", util_optarg);
00147             }
00148           }
00149           else {
00150             UsagePrint(argv[0]);
00151           }
00152           batch = 1;
00153           break;
00154 
00155         case 'T':
00156           if (TypeCheck(util_optarg)) {
00157             if (strcmp(util_optarg, "none") == 0) {
00158               final_write = 0;
00159             }
00160             else {
00161               (void) sprintf(writecmd, "write_%s", util_optarg);
00162             }
00163           }
00164           else {
00165             UsagePrint(argv[0]);
00166           }
00167           batch = 1;
00168           break;
00169 
00170         case 'x':
00171           final_write = 0;
00172           initial_read = 0;
00173           batch = 1;
00174           break;
00175 
00176         default:
00177           UsagePrint(argv[0]);
00178     }
00179   }
00180 
00181   if (! batch) {
00182     /* interactive use ... */
00183     if (argc - util_optind != 0) {
00184       (void) fprintf(vis_stderr, "warning -- trailing arguments ignored\n");
00185     }
00186 
00187     (void) fprintf(vis_stdout, "%s\n", Vm_VisReadVersion());
00188     if (initial_source) {
00189       (void) VisrcSource(&globalHmgr);
00190     }
00191     while ((quit_flag = Cmd_CommandExecute(&globalHmgr, "source -ip -")) >= 0)
00192       ;
00193     status = 0;
00194 
00195   } else {
00196 
00197     if (argc - util_optind == 0) {
00198       infile = "-";
00199     }
00200     else if (argc - util_optind == 1) {
00201       infile = argv[util_optind];
00202     }
00203     else {
00204       UsagePrint(argv[0]);
00205     }
00206 
00207     if (initial_source) {
00208       (void) VisrcSource(&globalHmgr);
00209     }
00210 
00211     status = 0;
00212     if (initial_read) {
00213       cmdline1 = ALLOC(char, strlen(infile) + 20);
00214       (void) sprintf(cmdline1, "%s %s", readcmd, infile);
00215       status = Cmd_CommandExecute(&globalHmgr, cmdline1);
00216       FREE(cmdline1);
00217     }
00218 
00219     if (status == 0) {
00220       /* cmd line contains `source <file>' */
00221       status = Cmd_CommandExecute(&globalHmgr, cmdline);
00222       if ((status == 0 || status == -1) && final_write) {
00223         cmdline1 = ALLOC(char, strlen(outfile) + 20);
00224         (void) sprintf(cmdline1, "%s %s", writecmd, outfile);
00225         status = Cmd_CommandExecute(&globalHmgr, cmdline1);
00226         FREE(cmdline1);
00227       }
00228     }
00229     
00230     /* FIX: RB, allows a clean quit from a script */
00231     quit_flag = status;
00232     
00233 
00234   }
00235 
00236   FREE(cmdline);
00237   for (c = array_n(vm_commandHistoryArray); c-- > 0; ){
00238     dummy = array_fetch(char *, vm_commandHistoryArray, c);
00239     FREE(dummy);
00240   }
00241 
00242   array_free(vm_commandHistoryArray);
00243   /* Value of "quit_flag" is determined by the "quit" command */
00244   if (quit_flag == -1 || quit_flag == -2) {
00245     status = 0;
00246   }
00247   if (quit_flag == -2) {
00248     Hrc_ManagerFree(globalHmgr);
00249     VmEnd();
00250   }
00251   return(status);
00252 }
00253 
00254 
00255 /*---------------------------------------------------------------------------*/
00256 /* Definition of internal functions                                          */
00257 /*---------------------------------------------------------------------------*/
00258 
00259 
00260 /*---------------------------------------------------------------------------*/
00261 /* Definition of static functions                                            */
00262 /*---------------------------------------------------------------------------*/
00263 
00264 
00279 static int
00280 VisrcSource(
00281   Hrc_Manager_t ** hmgr)
00282 {
00283   char *commandLine;
00284   char *libraryName;
00285   char *homeFile;
00286   struct stat home;
00287   struct stat cur;
00288   int s1;
00289   int s2;                       /* flags for checking the stat() call */
00290   int status0;
00291   int status1 = TRUE;
00292   int status2 = TRUE;
00293 
00294   /*
00295    * First execute the standard .visrc.
00296    */
00297   libraryName = Vm_VisObtainLibrary();
00298   commandLine = ALLOC(char, strlen(libraryName) + 25);
00299   (void) sprintf(commandLine, "source -s %s/master.visrc", libraryName);
00300   FREE(libraryName);
00301   status0 = Cmd_CommandExecute(hmgr, commandLine);
00302   FREE(commandLine);
00303 
00304   /*
00305    * Look in home directory and current directory for .visrc.
00306    */
00307   homeFile = util_tilde_expand("~/.visrc");
00308   s1 = stat(homeFile, &home);
00309   FREE(homeFile);
00310   s2 = stat(".visrc", &cur);
00311 
00312   /*
00313    * If .visrc is present in both the home and current directories, then read
00314    * it from the home directory.  Otherwise, read it from wherever it's
00315    * located.
00316    */
00317   if ((s1 == 0) && (s2 == 0) && (home.st_ino == cur.st_ino)){
00318     /* ~/.visrc == .visrc : Source the file only once */
00319     status1 = Cmd_CommandExecute(hmgr, "source -s ~/.visrc");
00320   }
00321   else {
00322     if (s1 == 0) {
00323       status1 = Cmd_CommandExecute(hmgr, "source -s ~/.visrc");
00324     }
00325     if (s2 == 0) {
00326       status2 = Cmd_CommandExecute(hmgr, "source -s .visrc");
00327     }
00328   }
00329 
00330   return (status0 && status1 && status2);
00331 }
00332 
00333 
00343 static void
00344 UsagePrint(
00345   char * program)
00346 {
00347   char *libraryName;
00348   
00349   (void) fprintf(vis_stderr,
00350                  "%s\n", Vm_VisReadVersion());
00351   (void) fprintf(vis_stderr,
00352                  "usage: %s [-c cmd] [-f script] [-h] [-o file] [-s] [-t type] [-T type] [-x] [file]\n", 
00353                  program);
00354   (void) fprintf(vis_stderr,
00355                  "    -c cmd\texecute VIS commands `cmd'\n");
00356   (void) fprintf(vis_stderr,
00357                  "    -f file\texecute VIS commands from a file\n");
00358   (void) fprintf(vis_stderr,
00359                  "    -h\t\tprint the command usage\n");
00360   (void) fprintf(vis_stderr,
00361                  "    -o file\tspecify output filename (default is -)\n");
00362   (void) fprintf(vis_stderr,
00363                  "    -s\t\tdo not read any initialization file\n");
00364   libraryName = Vm_VisObtainLibrary();
00365   (void) fprintf(vis_stderr,
00366                  "      \t\t(%s/master.visrc, ~/.visrc)\n", libraryName);
00367   FREE(libraryName);
00368   (void) fprintf(vis_stderr,
00369                  "    -t type\tspecify input type (blif_mv (default), blif, or none)\n");
00370   (void) fprintf(vis_stderr,
00371                  "    -T type\tspecify output type (blif_mv (default), blif, smv, or none)\n");
00372   (void) fprintf(vis_stderr,
00373                  "    -x\t\tequivalent to '-t none -T none'\n");
00374 
00375   exit(2);
00376 }
00377 
00378 
00389 static int
00390 TypeCheck(
00391   char * s)
00392 {
00393   if (strcmp(s, "blif") == 0) {
00394     return 1;
00395   }
00396   else if (strcmp(s, "blif_mv") == 0) {
00397     return 1;
00398   }
00399   else if (strcmp(s, "smv") == 0) {
00400     return 1;
00401   }
00402   else if (strcmp(s, "none") == 0) {
00403     return 1;
00404   }
00405   else {
00406     (void) fprintf(vis_stderr, "unknown type %s\n", s);
00407     return 0;
00408   }
00409 }