source: vis_dev/vis-2.3/src/vm/vmMain.c @ 104

Last change on this file since 104 was 14, checked in by cecile, 13 years ago

vis2.3

File size: 11.6 KB
Line 
1/**CFile***********************************************************************
2
3  FileName    [vmMain.c]
4
5  PackageName [vm]
6
7  Synopsis    [Main VIS routine. Parses command line at invocation of VIS.]
8
9  Author      [Originated from SIS]
10
11  Copyright   [Copyright (c) 1994-1996 The Regents of the Univ. of California.
12  All rights reserved.
13
14  Permission is hereby granted, without written agreement and without license
15  or royalty fees, to use, copy, modify, and distribute this software and its
16  documentation for any purpose, provided that the above copyright notice and
17  the following two paragraphs appear in all copies of this software.
18
19  IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
20  DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
21  OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
22  CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23
24  THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
25  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
26  FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS ON AN
27  "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE
28  MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.]
29
30******************************************************************************/
31
32#include "vmInt.h"
33
34static char rcsid[] UNUSED = "$Id: vmMain.c,v 1.6 2004/07/16 00:09:59 wangc Exp $";
35
36/*---------------------------------------------------------------------------*/
37/* Variable declarations                                                     */
38/*---------------------------------------------------------------------------*/
39FILE *vis_stderr;
40FILE *vis_stdout;
41FILE *vis_historyFile;
42FILE *vis_stdpipe;
43array_t *vm_commandHistoryArray;
44char *vm_programName;
45
46
47/**AutomaticStart*************************************************************/
48
49/*---------------------------------------------------------------------------*/
50/* Static function prototypes                                                */
51/*---------------------------------------------------------------------------*/
52
53static int VisrcSource(Hrc_Manager_t ** hmgr);
54static void UsagePrint(char * program);
55static int TypeCheck(char * s);
56
57/**AutomaticEnd***************************************************************/
58
59
60/*---------------------------------------------------------------------------*/
61/* Definition of exported functions                                          */
62/*---------------------------------------------------------------------------*/
63
64/**Function********************************************************************
65
66  Synopsis    [required]
67
68  Description [optional]
69
70  SideEffects [Sets vm_programName to the name of the excutable.]
71
72  SeeAlso     [optional]
73
74******************************************************************************/
75int
76main(
77  int  argc,
78  char ** argv)
79{
80  int c,
81    status,
82    batch,          /* 0 iff we have an interactive run (-c or -f) */
83    initial_source, /* 1 iff we have to read .visrc                */
84    initial_read,
85    final_write;
86  int quit_flag;
87  char readcmd[20],
88    writecmd[20];
89  char *dummy,
90    *cmdline,
91    *cmdline1,
92    *infile,        /* where to read input */
93    *outfile;       /* where to write output */
94  Hrc_Manager_t *globalHmgr;
95 
96  vm_programName = argv[0];
97  quit_flag = -1;     /* Quick quit */
98
99  VmInit();
100  globalHmgr = Hrc_ManagerAlloc();
101
102  cmdline = util_strsav("");
103  (void) strcpy(readcmd, "read_blif_mv");
104  (void) strcpy(writecmd, "write_blif_mv");
105  infile = "-";
106  outfile = "-";
107  vm_commandHistoryArray = array_alloc(char *, 0);
108  initial_source = 1;
109  initial_read = 1;
110  final_write = 1;
111  batch = 0;
112  util_getopt_reset();
113  while ((c = util_getopt(argc, argv, "c:hf:o:st:T:x")) != EOF) {
114    switch(c) {
115        case 'c':
116          FREE(cmdline);
117          cmdline = util_strsav(util_optarg);
118          batch = 1;
119          break;
120
121        case 'f':
122          FREE(cmdline);
123          cmdline = ALLOC(char, strlen(util_optarg) + 20);
124          (void) sprintf(cmdline, "source %s", util_optarg);
125          batch = 1;
126          break;
127         
128        case 'h':
129            UsagePrint(argv[0]);
130            break;
131             
132        case 'o':
133          outfile = util_optarg;
134          break;
135
136        case 's':
137          initial_source = 0;
138          break;
139
140        case 't':
141          if (TypeCheck(util_optarg)) {
142            if (strcmp(util_optarg, "none") == 0) {
143              initial_read = 0;
144            }
145            else {
146              (void) sprintf(readcmd, "read_%s", util_optarg);
147            }
148          }
149          else {
150            UsagePrint(argv[0]);
151          }
152          batch = 1;
153          break;
154
155        case 'T':
156          if (TypeCheck(util_optarg)) {
157            if (strcmp(util_optarg, "none") == 0) {
158              final_write = 0;
159            }
160            else {
161              (void) sprintf(writecmd, "write_%s", util_optarg);
162            }
163          }
164          else {
165            UsagePrint(argv[0]);
166          }
167          batch = 1;
168          break;
169
170        case 'x':
171          final_write = 0;
172          initial_read = 0;
173          batch = 1;
174          break;
175
176        default:
177          UsagePrint(argv[0]);
178    }
179  }
180
181  if (! batch) {
182    /* interactive use ... */
183    if (argc - util_optind != 0) {
184      (void) fprintf(vis_stderr, "warning -- trailing arguments ignored\n");
185    }
186
187    (void) fprintf(vis_stdout, "%s\n", Vm_VisReadVersion());
188    if (initial_source) {
189      (void) VisrcSource(&globalHmgr);
190    }
191    while ((quit_flag = Cmd_CommandExecute(&globalHmgr, "source -ip -")) >= 0)
192      ;
193    status = 0;
194
195  } else {
196
197    if (argc - util_optind == 0) {
198      infile = "-";
199    }
200    else if (argc - util_optind == 1) {
201      infile = argv[util_optind];
202    }
203    else {
204      UsagePrint(argv[0]);
205    }
206
207    if (initial_source) {
208      (void) VisrcSource(&globalHmgr);
209    }
210
211    status = 0;
212    if (initial_read) {
213      cmdline1 = ALLOC(char, strlen(infile) + 20);
214      (void) sprintf(cmdline1, "%s %s", readcmd, infile);
215      status = Cmd_CommandExecute(&globalHmgr, cmdline1);
216      FREE(cmdline1);
217    }
218
219    if (status == 0) {
220      /* cmd line contains `source <file>' */
221      status = Cmd_CommandExecute(&globalHmgr, cmdline);
222      if ((status == 0 || status == -1) && final_write) {
223        cmdline1 = ALLOC(char, strlen(outfile) + 20);
224        (void) sprintf(cmdline1, "%s %s", writecmd, outfile);
225        status = Cmd_CommandExecute(&globalHmgr, cmdline1);
226        FREE(cmdline1);
227      }
228    }
229   
230    /* FIX: RB, allows a clean quit from a script */
231    quit_flag = status;
232   
233
234  }
235
236  FREE(cmdline);
237  for (c = array_n(vm_commandHistoryArray); c-- > 0; ){
238    dummy = array_fetch(char *, vm_commandHistoryArray, c);
239    FREE(dummy);
240  }
241
242  array_free(vm_commandHistoryArray);
243  /* Value of "quit_flag" is determined by the "quit" command */
244  if (quit_flag == -1 || quit_flag == -2) {
245    status = 0;
246  }
247  if (quit_flag == -2) {
248    Hrc_ManagerFree(globalHmgr);
249    VmEnd();
250  }
251  return(status);
252}
253
254
255/*---------------------------------------------------------------------------*/
256/* Definition of internal functions                                          */
257/*---------------------------------------------------------------------------*/
258
259
260/*---------------------------------------------------------------------------*/
261/* Definition of static functions                                            */
262/*---------------------------------------------------------------------------*/
263
264
265/**Function********************************************************************
266
267  Synopsis    [Sources the .visrc file.]
268
269  Description [Sources the .visrc file.  Always sources the .visrc from
270  library.  Then source the .visrc from the home directory.  If there is none
271  in the home directory, then execute the one in the current directory if one
272  is present.  Returns 1 if scripts were successfully executed, else return 0.]
273
274  SideEffects []
275
276  SeeAlso     [optional]
277
278******************************************************************************/
279static int
280VisrcSource(
281  Hrc_Manager_t ** hmgr)
282{
283  char *commandLine;
284  char *libraryName;
285  char *homeFile;
286  struct stat home;
287  struct stat cur;
288  int s1;
289  int s2;                       /* flags for checking the stat() call */
290  int status0;
291  int status1 = TRUE;
292  int status2 = TRUE;
293
294  /*
295   * First execute the standard .visrc.
296   */
297  libraryName = Vm_VisObtainLibrary();
298  commandLine = ALLOC(char, strlen(libraryName) + 25);
299  (void) sprintf(commandLine, "source -s %s/master.visrc", libraryName);
300  FREE(libraryName);
301  status0 = Cmd_CommandExecute(hmgr, commandLine);
302  FREE(commandLine);
303
304  /*
305   * Look in home directory and current directory for .visrc.
306   */
307  homeFile = util_tilde_expand("~/.visrc");
308  s1 = stat(homeFile, &home);
309  FREE(homeFile);
310  s2 = stat(".visrc", &cur);
311
312  /*
313   * If .visrc is present in both the home and current directories, then read
314   * it from the home directory.  Otherwise, read it from wherever it's
315   * located.
316   */
317  if ((s1 == 0) && (s2 == 0) && (home.st_ino == cur.st_ino)){
318    /* ~/.visrc == .visrc : Source the file only once */
319    status1 = Cmd_CommandExecute(hmgr, "source -s ~/.visrc");
320  }
321  else {
322    if (s1 == 0) {
323      status1 = Cmd_CommandExecute(hmgr, "source -s ~/.visrc");
324    }
325    if (s2 == 0) {
326      status2 = Cmd_CommandExecute(hmgr, "source -s .visrc");
327    }
328  }
329
330  return (status0 && status1 && status2);
331}
332
333
334/**Function********************************************************************
335
336  Synopsis    [Prints the usage of the VIS shell interface.]
337
338  SideEffects []
339
340  SeeAlso     [optional]
341
342******************************************************************************/
343static void
344UsagePrint(
345  char * program)
346{
347  char *libraryName;
348 
349  (void) fprintf(vis_stderr,
350                 "%s\n", Vm_VisReadVersion());
351  (void) fprintf(vis_stderr,
352                 "usage: %s [-c cmd] [-f script] [-h] [-o file] [-s] [-t type] [-T type] [-x] [file]\n", 
353                 program);
354  (void) fprintf(vis_stderr,
355                 "    -c cmd\texecute VIS commands `cmd'\n");
356  (void) fprintf(vis_stderr,
357                 "    -f file\texecute VIS commands from a file\n");
358  (void) fprintf(vis_stderr,
359                 "    -h\t\tprint the command usage\n");
360  (void) fprintf(vis_stderr,
361                 "    -o file\tspecify output filename (default is -)\n");
362  (void) fprintf(vis_stderr,
363                 "    -s\t\tdo not read any initialization file\n");
364  libraryName = Vm_VisObtainLibrary();
365  (void) fprintf(vis_stderr,
366                 "      \t\t(%s/master.visrc, ~/.visrc)\n", libraryName);
367  FREE(libraryName);
368  (void) fprintf(vis_stderr,
369                 "    -t type\tspecify input type (blif_mv (default), blif, or none)\n");
370  (void) fprintf(vis_stderr,
371                 "    -T type\tspecify output type (blif_mv (default), blif, smv, or none)\n");
372  (void) fprintf(vis_stderr,
373                 "    -x\t\tequivalent to '-t none -T none'\n");
374
375  exit(2);
376}
377
378
379/**Function********************************************************************
380
381  Synopsis    [Returns 1 if s is a file type recognized by VIS, else returns 0.]
382
383  Description [Returns 1 if s is a file type recognized by VIS, else returns
384  0. Recognized types are "blif", "blif_mv", and "none".]
385
386  SideEffects []
387
388******************************************************************************/
389static int
390TypeCheck(
391  char * s)
392{
393  if (strcmp(s, "blif") == 0) {
394    return 1;
395  }
396  else if (strcmp(s, "blif_mv") == 0) {
397    return 1;
398  }
399  else if (strcmp(s, "smv") == 0) {
400    return 1;
401  }
402  else if (strcmp(s, "none") == 0) {
403    return 1;
404  }
405  else {
406    (void) fprintf(vis_stderr, "unknown type %s\n", s);
407    return 0;
408  }
409}
Note: See TracBrowser for help on using the repository browser.