VIS

src/cmd/cmdMisc.c

Go to the documentation of this file.
00001 
00033 #include "cmdInt.h"
00034 #include <errno.h>
00035 
00036 static char rcsid[] UNUSED = "$Id: cmdMisc.c,v 1.59 2009/04/11 18:25:50 fabio Exp $";
00037 
00038 /*---------------------------------------------------------------------------*/
00039 /* Constant declarations                                                     */
00040 /*---------------------------------------------------------------------------*/
00041 /* The maximum length of an input line */
00042 #define MAX_STR         32768
00043 
00044 
00045 /*---------------------------------------------------------------------------*/
00046 /* Variable declarations                                                     */
00047 /*---------------------------------------------------------------------------*/
00048 avl_tree *cmdAliasTable;
00049 avl_tree *cmdFlagTable;
00050 
00051 static boolean fileCreated;
00052 
00053 
00056 /*---------------------------------------------------------------------------*/
00057 /* Static function prototypes                                                */
00058 /*---------------------------------------------------------------------------*/
00059 
00060 static int CommandTime(Hrc_Manager_t ** hmgr, int argc, char ** argv);
00061 static int CommandEcho(Hrc_Manager_t ** hmgr, int argc, char ** argv);
00062 static int CommandSetBddParameters(Hrc_Manager_t ** hmgr, int argc, char ** argv);
00063 static int CommandMemoryProfile(Hrc_Manager_t ** hmgr, int argc, char ** argv);
00064 static int CommandQuit(Hrc_Manager_t ** hmgr, int argc, char ** argv);
00065 static int CommandUsage(Hrc_Manager_t ** hmgr, int argc, char ** argv);
00066 static int CommandWhich(Hrc_Manager_t ** hmgr, int argc, char ** argv);
00067 static int CommandHistory(Hrc_Manager_t ** hmgr, int argc, char ** argv);
00068 static int CommandAlias(Hrc_Manager_t ** hmgr, int argc, char ** argv);
00069 static int CommandUnalias(Hrc_Manager_t ** hmgr, int argc, char ** argv);
00070 static int CommandSetVariable(Hrc_Manager_t ** hmgr, int argc, char ** argv);
00071 static int CommandUnsetVariable(Hrc_Manager_t ** hmgr, int argc, char ** argv);
00072 static int CommandHelp(Hrc_Manager_t ** hmgr, int argc, char ** argv);
00073 static int CommandSource(Hrc_Manager_t ** hmgr, int argc, char ** argv);
00074 static void print_alias(char * value);
00075 static char * command_alias_help(char * command);
00076 static void FlushBuffers(int sigtype);
00077 
00081 /*---------------------------------------------------------------------------*/
00082 /* Definition of exported functions                                          */
00083 /*---------------------------------------------------------------------------*/
00084 
00085 
00098 char *
00099 Cmd_FlagReadByName(
00100   char * flag)
00101 {
00102   char *value;
00103 
00104   if (avl_lookup(cmdFlagTable, flag, &value)) {
00105     return value;
00106   }
00107   else {
00108     return NIL(char);
00109   }
00110 }
00111 
00112 
00122 void
00123 Cmd_FlagUpdateValue(
00124   char * key, char * value)
00125 {
00126   char *oldValue, *newValue;
00127 
00128   if (!key)
00129     return;
00130   if (value)
00131     newValue = util_strsav(value);
00132   else
00133     newValue = util_strsav("");
00134 
00135   if (avl_delete(cmdFlagTable, &key, &oldValue))
00136     FREE(oldValue);
00137 
00138   (void) avl_insert(cmdFlagTable, key, newValue);
00139 }
00140 
00141 
00151 void
00152 Cmd_FlagDeleteByName(
00153   char * key)
00154 {
00155   char *value;
00156 
00157   if (!key)
00158     return;
00159 
00160   if (avl_delete(cmdFlagTable, &key, &value)) {
00161     FREE(key);
00162     FREE(value);
00163   }
00164 }
00165 
00166 
00176 void
00177 Cmd_Init(void)
00178 {
00179   char *path;
00180   char *lib_name;
00181 
00182 
00183   cmdCommandTable = avl_init_table((int(*)(const void *, const void *))strcmp);
00184   cmdFlagTable = avl_init_table((int(*)(const void *, const void *))strcmp);
00185   cmdAliasTable = avl_init_table((int(*)(const void *, const void *))strcmp);
00186 
00187   Cmd_CommandAdd("alias", CommandAlias, 0);
00188   Cmd_CommandAdd("echo", CommandEcho, 0);
00189   Cmd_CommandAdd("help", CommandHelp, 0);
00190   Cmd_CommandAdd("quit", CommandQuit, 0);
00191   Cmd_CommandAdd("source", CommandSource, 0);
00192 /*  Cmd_CommandAdd("undo", CommandUndo, 0); */
00193   Cmd_CommandAdd("set", CommandSetVariable, 0);
00194   Cmd_CommandAdd("unalias", CommandUnalias, 0);
00195   Cmd_CommandAdd("unset", CommandUnsetVariable, 0);
00196   Cmd_CommandAdd("time", CommandTime, 0);
00197   Cmd_CommandAdd("usage", CommandUsage, 0);
00198   Cmd_CommandAdd("history", CommandHistory, 0);
00199   Cmd_CommandAdd("which", CommandWhich, 0);
00200   Cmd_CommandAdd("set_bdd_parameters" , CommandSetBddParameters, 0);
00201   Cmd_CommandAdd("_memory_profile", CommandMemoryProfile, 0);
00202   fileCreated = FALSE;
00203 
00204   /* Program the signal of type USR1 to flush vis_stdout and vis_stderr */
00205 #ifdef SIGUSR1
00206   (void) signal(SIGUSR1, FlushBuffers);
00207 #endif
00208 
00209   /* set the default open_path */
00210   lib_name = Vm_VisObtainLibrary();
00211   path = ALLOC(char, strlen(lib_name) + 20);
00212   sprintf(path, "set open_path .:%s", lib_name);
00213   Cmd_CommandExecute(NULL, path);
00214   FREE(lib_name);
00215   FREE(path);
00216 }
00217 
00218 
00231 void
00232 Cmd_End(void)
00233 {
00234   avl_free_table(cmdFlagTable, (void (*)(char *))free, (void (*)(char *))free);
00235   avl_free_table(cmdCommandTable, (void (*)(char *)) 0, CmdCommandFree);
00236   avl_free_table(cmdAliasTable, (void (*)(char *)) 0, CmdAliasFree);
00237   if (cmdBackupHmgr != NIL(Hrc_Manager_t)) {
00238     Hrc_ManagerFree(cmdBackupHmgr);
00239   }
00240   error_cleanup();
00241 
00242   if (fileCreated == TRUE) {
00243     (void) fprintf(vis_stdout, "Purify has created a temporary file. The file");
00244     (void) fprintf(vis_stdout, " must be deleted.\n");
00245   }
00246 }
00247 
00248 
00259 int
00260 Cmd_StringCheckIsInteger(
00261   char *string,
00262   int *value)
00263 {
00264   char *ptr;
00265   long l;
00266 
00267   errno = 0 ;
00268   l = strtol (string, &ptr, 0) ;
00269   if(*ptr != '\0')
00270     return 0;
00271   if (errno != 0)
00272     return 1;
00273   if ((l > MAXINT) || (l < -1 - MAXINT))
00274     return 1 ;
00275   *value = (int) l;
00276   return 2 ;
00277 }
00278 
00279 
00280 /*---------------------------------------------------------------------------*/
00281 /* Definition of internal functions                                          */
00282 /*---------------------------------------------------------------------------*/
00283 
00284 
00296 void
00297 CmdFreeArgv(int  argc,  char ** argv)
00298 {
00299   int i;
00300 
00301   for(i = 0; i < argc; i++) {
00302     FREE(argv[i]);
00303   }
00304   FREE(argv);
00305 }
00306 
00307 
00319 void
00320 CmdAliasFree(
00321   char * value)
00322 {
00323   CmdAliasDescr_t *alias = (CmdAliasDescr_t *) value;
00324 
00325   CmdFreeArgv(alias->argc, alias->argv);
00326   FREE(alias->name);            /* same as key */
00327   FREE(alias);
00328 }
00329 
00330 
00331 
00332 /*---------------------------------------------------------------------------*/
00333 /* Definition of static functions                                            */
00334 /*---------------------------------------------------------------------------*/
00335 
00359 static int
00360 CommandTime(
00361   Hrc_Manager_t ** hmgr,
00362   int  argc,
00363   char ** argv)
00364 {
00365   static long last_time_u = 0;
00366   static long last_time_c = 0;
00367   long time;
00368   int c;
00369   boolean excludeChildren = FALSE;
00370 
00371   util_getopt_reset();
00372   while ((c = util_getopt(argc,argv,"hu")) != EOF){
00373     switch(c){
00374     case 'h':
00375       goto usage;
00376     case 'u':
00377       excludeChildren = TRUE;
00378       break;
00379     default:
00380       goto usage;
00381     }
00382   }
00383 
00384   if (argc != util_optind) {
00385     goto usage;
00386   }
00387 
00388   if (excludeChildren) {
00389     time = util_cpu_time();
00390     (void) fprintf(vis_stdout,
00391                    "elapse: %2.1f seconds, total: %2.1f seconds\n",
00392                    (time - last_time_u) / 1000.0, time / 1000.0);
00393     last_time_u = time;
00394   } else {
00395     time = util_cpu_ctime();
00396     (void) fprintf(vis_stdout,
00397                    "elapse: %2.1f seconds, total: %2.1f seconds\n",
00398                    (time - last_time_c) / 1000.0, time / 1000.0);
00399     last_time_c = time;
00400   }
00401   return 0;
00402 
00403 usage:
00404   (void) fprintf(vis_stderr, "usage: time [-h][-u]\n");
00405   (void) fprintf(vis_stderr, "   -h \t\tprint the command usage\n");
00406   (void) fprintf(vis_stderr, "   -u \t\texclude child process time\n");
00407   return 1;
00408 }
00409 
00426 static int
00427 CommandEcho(
00428   Hrc_Manager_t ** hmgr,
00429   int  argc,
00430   char ** argv)
00431 {
00432   int i;
00433   int c;
00434 
00435   util_getopt_reset();
00436   while ((c = util_getopt(argc, argv, "h")) != EOF) {
00437     switch(c) {
00438       case 'h':
00439         goto usage;
00440       default:
00441         goto usage;
00442     }
00443   }
00444 
00445   for(i = 1; i < argc; i++) {
00446     (void) fprintf(vis_stdout, "%s ", argv[i]);
00447   }
00448   (void) fprintf(vis_stdout, "\n");
00449   return 0;
00450 
00451   usage:
00452   (void) fprintf(vis_stderr, "usage: echo [-h] string \n");
00453   (void) fprintf(vis_stderr, "   -h \t\tprint the command usage\n");
00454   return (1);
00455 }
00456 
00498 static int
00499 CommandSetBddParameters(
00500   Hrc_Manager_t ** hmgr,
00501   int  argc,
00502   char ** argv)
00503 {
00504   Ntk_Network_t *network;
00505   boolean  showAfter;
00506   int      c;
00507 
00508   showAfter = FALSE;
00509   network = Ntk_HrcManagerReadCurrentNetwork(*hmgr);
00510 
00511   /*
00512    * Parse the command line.
00513    */
00514   util_getopt_reset();
00515   while ((c = util_getopt(argc, argv, "hs")) != EOF) {
00516     switch (c) {
00517       case 'h':
00518         goto usage;
00519       case 's':
00520         showAfter = TRUE;
00521         break;
00522       default:
00523         goto usage;
00524     }
00525   }
00526 
00527   /* flatten_hierarchy and static_order must have been invoked already. */
00528   if (network == NIL(Ntk_Network_t)) {
00529     return 1;
00530   }
00531   if (!Ntk_NetworkReadMddManager(network)) {
00532     (void) fprintf(vis_stderr, "The MDD variables have not been ordered. ");
00533     (void) fprintf(vis_stderr, "Use static_order.\n");
00534     return 1;
00535   }
00536 
00537   /* Create the table of variable->value */
00538   bdd_set_parameters(Ntk_NetworkReadMddManager(network), cmdFlagTable,
00539                      vis_stdout);
00540 
00541   if (showAfter) {
00542     bdd_print_stats(Ntk_NetworkReadMddManager(network), vis_stdout);
00543   }
00544 
00545   return 0;  /* Everything okay */
00546 
00547 usage:
00548   (void) fprintf(vis_stderr, "usage: set_bdd_parameters [-h] [-s]\n");
00549   (void) fprintf(vis_stderr, "   -h  print the command usage\n");
00550   (void) fprintf(vis_stderr, "   -s  print also the bdd statistics\n");
00551 
00552   return 1;
00553 }
00554 
00619 static int
00620 CommandMemoryProfile(
00621   Hrc_Manager_t ** hmgr,
00622   int  argc,
00623   char ** argv)
00624 {
00625 
00626   int   c;
00627   char  options[128];
00628 #ifdef PURIFY
00629   char  tmpFileName[128];
00630   FILE  *fp;
00631   char  command[256];
00632   char  *visDirectoryName;
00633   int   systemStatus;
00634 #endif
00635 
00636   /*
00637    * Parse command line options.
00638    */
00639   options[0] = 0;
00640   util_getopt_reset();
00641   while ((c = util_getopt(argc, argv, "f:hpu:")) != EOF) {
00642     switch(c) {
00643       case 'f':
00644         strcat(options, " -f ");
00645         strcat(options, util_optarg);
00646         break;
00647       case 'h':
00648         goto usage;
00649       case 'p':
00650         strcat(options, " -p ");
00651         break;
00652       case 'u':
00653         strcat(options, " -u ");
00654         strcat(options, util_optarg);
00655         break;
00656       default:
00657         goto usage;
00658     }
00659   }
00660 
00661 
00662 #ifdef PURIFY
00663   /* Flag to remember that a file has been created by purify */
00664   fileCreated = TRUE;
00665 
00666   /* Obtain the name of a temporary file */
00667   tmpnam(tmpFileName);
00668 
00669   /* Kick purify to dump the data in the file */
00670   purify_all_inuse();
00671 
00672   /* Obtain the path to the perl script */
00673   visDirectoryName = Vm_VisObtainLibrary();
00674 
00675   /* Prepare the string to be sent to a shell */
00676   (void)sprintf(command, "%s/memoryaccount %s %s/.fmap ./.fmap >%s",
00677                 visDirectoryName, options, visDirectoryName,
00678                 tmpFileName);
00679 
00680   /* Effectively execute the perlscript */
00681   systemStatus = system(command);
00682   if (systemStatus != 0) {
00683     return 1;
00684   }
00685 
00686   fp = Cmd_FileOpen(tmpFileName, "r", NIL(char *), 1);
00687 
00688   /* Check if the open has been successful */
00689   if (fp == NIL(FILE)) {
00690     (void) fprintf(vis_stderr, "** cmd error: File %s was not found\n", tmpFileName);
00691     return 1;
00692   }
00693 
00694   /* Dump the contents of the result file in vis_stdout */
00695   while(fgets(command, 128, fp) != NIL(char)) {
00696     (void) fprintf(vis_stdout, "%s", command);
00697   }
00698   fclose(fp);
00699 
00700   /* Remove the temporary file */
00701 #if HAVE_UNLINK
00702   unlink(tmpFileName);
00703 #endif
00704 #else
00705   (void) fprintf(vis_stderr, "** cmd error: Command not available: Vis has not been ");
00706   (void) fprintf(vis_stderr, "compiled with purify.\n");
00707 #endif
00708 
00709   return 0;             /* normal exit */
00710 
00711   usage:
00712   (void) fprintf(vis_stderr, "usage: _memory_profile [-f <filename>] [-h] ");
00713   (void) fprintf(vis_stderr, "[-p] [-u <units>] <filenames>\n");
00714   (void) fprintf(vis_stderr, "   -f <file>\tFile to read the purify dump");
00715   (void) fprintf(vis_stderr, " from. The default is purify.log\n");
00716   (void) fprintf(vis_stderr, "   -h\t\tprint the command usage\n");
00717   (void) fprintf(vis_stderr, "   -p\t\tPrint also the packages that do not ");
00718   (void) fprintf(vis_stderr, " allocate any memory\n");
00719   (void) fprintf(vis_stderr, "   -u <units>\tUnits to print the memory usage");
00720   (void) fprintf(vis_stderr, " in. It may be b for bytes\n");
00721   (void) fprintf(vis_stderr, "     \t\tk for kilobytes, m for megabytes and ");
00722   (void) fprintf(vis_stderr, "g for gigabutes.\n");
00723   return 1;             /* error exit */
00724 }
00725 
00726 
00752 static int
00753 CommandQuit(
00754   Hrc_Manager_t ** hmgr,
00755   int  argc,
00756   char ** argv)
00757 {
00758   int c;
00759 
00760   util_getopt_reset();
00761   while ((c = util_getopt(argc,argv,"hs")) != EOF){
00762     switch(c){
00763       case 'h':
00764         goto usage;
00765       case 's':
00766         return -2;
00767       default:
00768         goto usage;
00769     }
00770   }
00771 
00772   if ( argc != util_optind){
00773     goto usage;
00774   }
00775   return -1;
00776 
00777   usage:
00778     (void)fprintf(vis_stderr, "usage: quit [-h] [-s]\n");
00779     (void)fprintf(vis_stderr, "   -h  print the command usage\n");
00780     (void)fprintf(vis_stderr, "   -s  frees all the memory before quitting\n");
00781     return 1;
00782 }
00783 
00802 static int
00803 CommandUsage(
00804   Hrc_Manager_t ** hmgr,
00805   int  argc,
00806   char ** argv)
00807 {
00808   int c;
00809 
00810   util_getopt_reset();
00811   while ((c = util_getopt(argc,argv,"h")) != EOF){
00812     switch(c){
00813       case 'h':
00814         goto usage;
00815       default:
00816         goto usage;
00817     }
00818   }
00819 
00820   if (argc != util_optind){
00821     goto usage;
00822   }
00823   util_print_cpu_stats(vis_stdout);
00824   return 0;
00825 
00826   usage:
00827     (void) fprintf(vis_stderr, "usage: usage [-h]\n");
00828     (void) fprintf(vis_stderr, "   -h \t\tprint the command usage\n");
00829     return 1;
00830 }
00831 
00856 static int
00857 CommandWhich(
00858   Hrc_Manager_t ** hmgr,
00859   int  argc,
00860   char ** argv)
00861 {
00862   FILE *fp;
00863   char *filename;
00864   int c;
00865 
00866   util_getopt_reset();
00867   while ((c = util_getopt(argc,argv,"h")) != EOF){
00868     switch(c){
00869       case 'h':
00870         goto usage;
00871       default:
00872         goto usage;
00873     }
00874   }
00875 
00876   if (argc-1 != util_optind){
00877     goto usage;
00878   }
00879 
00880   fp = Cmd_FileOpen(argv[1], "r", &filename, 0);
00881   if (fp != 0) {
00882     (void) fprintf(vis_stdout, "%s\n", filename);
00883     (void) fclose(fp);
00884   }
00885   FREE(filename);
00886   return 0;
00887 
00888   usage:
00889     (void)fprintf(vis_stderr,"usage: which [-h] file_name\n");
00890     (void) fprintf(vis_stderr, "   -h \t\tprint the command usage\n");
00891     return 1;
00892 }
00893 
00894 
00957 static int
00958 CommandHistory(
00959   Hrc_Manager_t ** hmgr,
00960   int  argc,
00961   char ** argv)
00962 {
00963   int i, num, lineno;
00964   int size;
00965   int c;
00966 
00967   util_getopt_reset();
00968   while ((c = util_getopt(argc, argv, "h")) != EOF) {
00969     switch(c) {
00970       case 'h':
00971         goto usage;
00972       default:
00973         goto usage;
00974     }
00975   }
00976 
00977   if (argc > 3) {
00978     goto usage;
00979   }
00980   num = 30;
00981   lineno = 1;
00982   for (i = 1; i < argc; i++) {
00983     if (argv[i][0] == '-') {
00984       if (argv[i][1] == 'h') {
00985         lineno = 0;
00986       }
00987       else {
00988         goto usage;
00989       }
00990     }
00991     else {
00992       num = atoi(argv[i]);
00993       if (num <= 0) {
00994         goto usage;
00995       }
00996     }
00997   }
00998   size = array_n(vm_commandHistoryArray);
00999   num = (num < size) ? num : size;
01000   for (i = size - num; i < size; i++) {
01001     if (lineno != 0) {
01002       (void) fprintf(vis_stdout, "%d\t", i + 1);
01003     }
01004     (void) fprintf(vis_stdout, "%s\n", array_fetch(char *, vm_commandHistoryArray, i));
01005   }
01006   return(0);
01007 
01008 usage:
01009   (void) fprintf(vis_stderr, "usage: history [-h] [num]\n");
01010   (void) fprintf(vis_stderr, "   -h \t\tprint the command usage\n");
01011   (void) fprintf(vis_stderr, "   num \t\tprint the last num commands\n");
01012   return(1);
01013 }
01014 
01015 
01016 
01017 
01089 static int
01090 CommandAlias(
01091   Hrc_Manager_t ** hmgr,
01092   int  argc,
01093   char ** argv)
01094 {
01095   int i;
01096   char *key, *value;
01097   CmdAliasDescr_t *alias;
01098   avl_generator *gen;
01099   int status;
01100   int c;
01101 
01102   util_getopt_reset();
01103   while ((c = util_getopt(argc, argv, "h")) != EOF) {
01104     switch(c) {
01105       case 'h':
01106         goto usage;
01107       default:
01108         goto usage;
01109     }
01110   }
01111 
01112 
01113   if (argc == 1) {
01114     avl_foreach_item(cmdAliasTable, gen, AVL_FORWARD, &key, &value) {
01115       print_alias(value);
01116     }
01117     return 0;
01118 
01119   }
01120   else if (argc == 2) {
01121     if (avl_lookup(cmdAliasTable, argv[1], &value)) {
01122       print_alias(value);
01123     }
01124     return 0;
01125   }
01126 
01127   /* delete any existing alias */
01128   key = argv[1];
01129   if (avl_delete(cmdAliasTable, &key, &value)) {
01130     CmdAliasFree(value);
01131   }
01132 
01133   alias = ALLOC(CmdAliasDescr_t, 1);
01134   alias->name = util_strsav(argv[1]);
01135   alias->argc = argc - 2;
01136   alias->argv = ALLOC(char *, alias->argc);
01137   for(i = 2; i < argc; i++) {
01138     alias->argv[i-2] = util_strsav(argv[i]);
01139   }
01140   status = avl_insert(cmdAliasTable, alias->name, (char *) alias);
01141   assert(!status);  /* error here in SIS version, TRS, 8/4/95 */
01142   return 0;
01143 
01144   usage:
01145     (void) fprintf(vis_stderr, "usage: alias [-h] [command [string]]\n");
01146     (void) fprintf(vis_stderr, "   -h \t\tprint the command usage\n");
01147     return (1);
01148 }
01149 
01150 
01172 static int
01173 CommandUnalias(
01174   Hrc_Manager_t ** hmgr,
01175   int  argc,
01176   char ** argv)
01177 {
01178   int i;
01179   char *key, *value;
01180   int c;
01181 
01182   util_getopt_reset();
01183   while ((c = util_getopt(argc, argv, "h")) != EOF) {
01184     switch(c) {
01185       case 'h':
01186         goto usage;
01187       default:
01188         goto usage;
01189     }
01190   }
01191 
01192   if (argc < 2) {
01193     goto usage;
01194   }
01195 
01196   for(i = 1; i < argc; i++) {
01197     key = argv[i];
01198     if (avl_delete(cmdAliasTable, &key, &value)) {
01199       CmdAliasFree(value);
01200     }
01201   }
01202   return 0;
01203 
01204   usage:
01205     (void) fprintf(vis_stderr, "usage: unalias [-h] alias_names\n");
01206     (void) fprintf(vis_stderr, "   -h \t\tprint the command usage\n");
01207     return 1;
01208 }
01209 
01210 
01452 static int
01453 CommandSetVariable(
01454   Hrc_Manager_t ** hmgr,
01455   int  argc,
01456   char ** argv)
01457 {
01458   char *flag_value, *key, *value;
01459   avl_generator *gen;
01460   int c;
01461 
01462   util_getopt_reset();
01463   while ((c = util_getopt(argc, argv, "h")) != EOF) {
01464     switch(c) {
01465       case 'h':
01466         goto usage;
01467       default:
01468         goto usage;
01469     }
01470   }
01471   if (argc == 0 || argc > 3) {
01472     goto usage ;
01473   }
01474   else if (argc == 1) {
01475     avl_foreach_item(cmdFlagTable, gen, AVL_FORWARD, &key, &value) {
01476       (void) fprintf(vis_stdout, "%s\t%s\n", key, value);
01477     }
01478     return 0;
01479   }
01480   else {
01481     key = argv[1];
01482     if (avl_delete(cmdFlagTable, &key, &value)) {
01483       FREE(key);
01484       FREE(value);
01485     }
01486 
01487     flag_value = argc == 2 ? util_strsav("") : util_strsav(argv[2]);
01488 
01489     (void) avl_insert(cmdFlagTable, util_strsav(argv[1]), flag_value);
01490 
01491     if (strcmp(argv[1], "vis_stdout") == 0) {
01492       if (vis_stdout != stdout) {
01493         (void) fclose(vis_stdout);
01494       }
01495       if (strcmp(flag_value, "") == 0) {
01496         flag_value = "-";
01497       }
01498       vis_stdout = Cmd_FileOpen(flag_value, "w", NIL(char *), 0);
01499       if (vis_stdout == NULL) {
01500         vis_stdout = stdout;
01501       }
01502 #if HAVE_SETVBUF
01503       setvbuf(vis_stdout, (char *)NULL, _IOLBF, 0);
01504 #endif
01505     }
01506     if (strcmp(argv[1], "vis_stderr") == 0) {
01507       if (vis_stderr != stderr) {
01508         (void) fclose(vis_stderr);
01509       }
01510       if (strcmp(flag_value, "") == 0) {
01511         flag_value = "-";
01512       }
01513       vis_stderr = Cmd_FileOpen(flag_value, "w", NIL(char *), 0);
01514       if (vis_stderr == NULL) {
01515         vis_stderr = stderr;
01516       }
01517 #if HAVE_SETVBUF
01518       setvbuf(vis_stderr, (char *)NULL, _IOLBF, 0);
01519 #endif
01520     }
01521     if (strcmp(argv[1], "history") == 0) {
01522       if (vis_historyFile != NIL(FILE)) {
01523         (void) fclose(vis_historyFile);
01524       }
01525       if (strcmp(flag_value, "") == 0) {
01526         vis_historyFile = NIL(FILE);
01527       }
01528       else {
01529         vis_historyFile = Cmd_FileOpen(flag_value, "w", NIL(char *), 0);
01530         if (vis_historyFile == NULL) {
01531           vis_historyFile = NIL(FILE);
01532         }
01533       }
01534     }
01535     return 0;
01536   }
01537 
01538   usage:
01539       (void) printf("usage: set [-h] [name] [value]\n");
01540       (void) fprintf(vis_stderr, "   -h \t\tprint the command usage\n");
01541       return 1;
01542 
01543 }
01544 
01545 
01575 static int
01576 CommandUnsetVariable(
01577   Hrc_Manager_t ** hmgr,
01578   int  argc,
01579   char ** argv)
01580 {
01581   int i;
01582   char *key, *value;
01583   int c;
01584 
01585   util_getopt_reset();
01586   while ((c = util_getopt(argc, argv, "h")) != EOF) {
01587     switch(c) {
01588       case 'h':
01589         goto usage;
01590       default:
01591         goto usage;
01592     }
01593   }
01594 
01595   if (argc < 2) {
01596     goto usage;
01597   }
01598 
01599   for(i = 1; i < argc; i++) {
01600     key = argv[i];
01601     if (avl_delete(cmdFlagTable, &key, &value)) {
01602       FREE(key);
01603       FREE(value);
01604     }
01605   }
01606   return 0;
01607 
01608 
01609   usage:
01610     (void) fprintf(vis_stderr, "usage: unset [-h] variables \n");
01611     (void) fprintf(vis_stderr, "   -h \t\tprint the command usage\n");
01612     return 1;
01613 }
01614 
01641 static int
01642 CommandHelp(
01643   Hrc_Manager_t ** hmgr,
01644   int  argc,
01645   char ** argv)
01646 {
01647   int c, i, all;
01648   char *key;
01649   avl_generator *gen;
01650   char buffer[1024];
01651   char *command;
01652   char *lib_name;
01653 #if HAVE_GETENV
01654   char *pager;
01655 #endif
01656 
01657 
01658   util_getopt_reset();
01659   all = 0;
01660   while ((c = util_getopt(argc, argv, "ah")) != EOF) {
01661     switch(c) {
01662       case 'a':
01663         all = 1;
01664         break;
01665       case 'h':
01666         goto usage;
01667       default:
01668         goto usage;
01669     }
01670   }
01671 
01672   if (argc - util_optind == 0) {
01673     fprintf(vis_stderr,"List of commands:\n");
01674     i = 0;
01675     avl_foreach_item(cmdCommandTable, gen, AVL_FORWARD, &key, NIL(char *)) {
01676       if ((key[0] == '_') == all) {
01677         (void) fprintf(vis_stdout, "%-26s", key);
01678         if ((++i%3) == 0) {
01679           (void) fprintf(vis_stdout, "\n");
01680         }
01681       }
01682     }
01683     if ((i%3) != 0) {
01684       (void) fprintf(vis_stdout, "\n");
01685     }
01686   }
01687   else if (argc - util_optind == 1) {
01688     command = command_alias_help(argv[util_optind]);
01689     lib_name = Vm_VisObtainLibrary();
01690 #if HAVE_GETENV
01691     pager = getenv("PAGER");
01692     if (pager != NULL) {
01693       (void) sprintf(buffer, "%s %s/help/%sCmd.txt", pager, lib_name, command);
01694     } else {
01695       (void) sprintf(buffer, "more %s/help/%sCmd.txt", lib_name, command);
01696     }
01697 #else
01698     (void) sprintf(buffer, "more %s/help/%sCmd.txt", lib_name, command);
01699 #endif
01700     (void) system(buffer);
01701     FREE(lib_name);
01702   }
01703   else {
01704     goto usage;
01705   }
01706 
01707   return 0;
01708 
01709 usage:
01710   (void) fprintf(vis_stderr, "usage: help [-a] [-h] [command]\n");
01711   (void) fprintf(vis_stderr, "   -a \t\tprint help for all commands\n");
01712   (void) fprintf(vis_stderr, "   -h \t\tprint the command usage\n");
01713 
01714   return 1;
01715 }
01716 
01717 #if 0
01718 Change "/*!*Function" to "/**Function" when reactivate command
01719 /*!*Function********************************************************************
01720 
01721   Synopsis          [Implements the undo command.]
01722 
01723   CommandName       [undo]
01724 
01725   CommandSynopsis   [undo the result of the last command which changed the network]
01726 
01727   CommandDescription [ A simple 1-level undo is supported.
01728   It reverts the network to its state before the last command which
01729   changed the network.  Note that interrupting a command (with ^C)
01730   which changes the network uses up the one level of undo.<p> ]
01731 
01732   SideEffects        []
01733 
01734 ******************************************************************************/
01735 static int
01736 CommandUndo(
01737   Hrc_Manager_t ** hmgr,
01738   int  argc,
01739   char ** argv)
01740 {
01741   if (argc != 1) {
01742     (void) fprintf(vis_stderr, "usage: undo\n");
01743     return 1;
01744   }
01745 
01746   (void) fprintf(vis_stderr, "** cmd error: undo: not yet implemented\n");
01747   return 1;
01748 
01749 #if 0
01750 
01751   /*
01752    * FIX (Tom): can't enable "undo" until network_dup and hmgr_dup exist.
01753    */
01754   if (cmdBackupHmgr == NIL(Hrc_Manager_t)) {
01755     (void) fprintf(vis_stderr, "undo: no hmgr currently saved\n");
01756     return 1;
01757   }
01758   else {
01759 
01760     /* Swap the current and backup. */
01761     Hrc_Manager_t *temp = *hmgr;
01762     *hmgr = cmdBackupHmgr;
01763     cmdBackupHmgr = temp;
01764     return 0;
01765   }
01766 #endif
01767 }
01768 #endif
01769 
01770 
01771 
01859 static int
01860 CommandSource(
01861   Hrc_Manager_t ** hmgr,
01862   int  argc,
01863   char ** argv)
01864 {
01865   int c, echo, prompt, silent, interactive, quit_count, lp_count;
01866   int status = 0; /* initialize so that lint doesn't complain */
01867   int lp_file_index, did_subst;
01868   char *prompt_string, *real_filename, line[MAX_STR], *command;
01869   FILE *fp;
01870 
01871   interactive = silent = prompt = echo = 0;
01872 
01873   util_getopt_reset();
01874   while ((c = util_getopt(argc, argv, "hipsx")) != EOF) {
01875     switch(c) {
01876         case 'h':
01877           goto usage ;
01878         case 'i':               /* a hack to distinguish EOF from stdin */
01879           interactive = 1;
01880           break;
01881         case 'p':
01882           prompt = 1;
01883           break;
01884         case 's':
01885           silent = 1;
01886           break;
01887         case 'x':
01888           echo = 1;
01889           break;
01890       default:
01891           goto usage;
01892     }
01893   }
01894 
01895   /* added to avoid core-dumping when no script file is specified */
01896   if (argc == util_optind){
01897     goto usage;
01898   }
01899 
01900   lp_file_index = util_optind;
01901   lp_count = 0;
01902 
01903   /*
01904    * FIX (Tom, 5/7/95):  I'm not sure what the purpose of this outer do loop
01905    * is. In particular, lp_file_index is never modified in the loop, so it
01906    * looks it would just read the same file over again.  Also, SIS had
01907    * lp_count initialized to -1, and hence, any file sourced by SIS (if -l or
01908    * -t options on "source" were used in SIS) would actually be executed
01909    * twice.
01910    */
01911   do {
01912     lp_count ++; /* increment the loop counter */
01913 
01914     fp = Cmd_FileOpen(argv[lp_file_index], "r", &real_filename, silent);
01915     if (fp == NULL) {
01916       FREE(real_filename);
01917       return ! silent;  /* error return if not silent */
01918     }
01919 
01920     quit_count = 0;
01921     do {
01922       if (prompt) {
01923         prompt_string = Cmd_FlagReadByName("prompt");
01924         if (prompt_string == NIL(char)) {
01925           prompt_string = "vis> ";
01926         }
01927 
01928       }
01929       else {
01930         prompt_string = NIL(char);
01931       }
01932 
01933       /* clear errors -- e.g., EOF reached from stdin */
01934       clearerr(fp);
01935 
01936       /* read another command line */
01937       if (CmdFgetsFilec(line, MAX_STR, fp, prompt_string) == NULL) {
01938         if (interactive) {
01939           if (quit_count++ < 5) {
01940             (void) fprintf(vis_stderr, "\nUse \"quit\" to leave VIS.\n");
01941             continue;
01942           }
01943           status = -1;          /* fake a 'quit' */
01944         }
01945         else {
01946           status = 0;           /* successful end of 'source' ; loop? */
01947         }
01948         break;
01949       }
01950       quit_count = 0;
01951 
01952       if (echo) {
01953         (void) fprintf(vis_stdout, "%s", line);
01954       }
01955       command = CmdHistorySubstitution(line, &did_subst);
01956       if (command == NIL(char)) {
01957         status = 1;
01958         break;
01959       }
01960       if (did_subst) {
01961         if (interactive) {
01962           (void) fprintf(stdout, "%s\n", command);
01963         }
01964       }
01965       if (command != line) {
01966         (void) strcpy(line, command);
01967       }
01968       if (interactive && *line != '\0') {
01969         array_insert_last(char *, vm_commandHistoryArray, util_strsav(line));
01970         if (vis_historyFile != NIL(FILE)) {
01971           (void) fprintf(vis_historyFile, "%s\n", line);
01972           (void) fflush(vis_historyFile);
01973         }
01974       }
01975 
01976       status = Cmd_CommandExecute(hmgr, line);
01977     } while (status == 0);
01978 
01979     if (fp != stdin) {
01980       if (status > 0) {
01981         (void) fprintf(vis_stderr, "** cmd error: aborting 'source %s'\n", real_filename);
01982       }
01983       (void) fclose(fp);
01984     }
01985     FREE(real_filename);
01986 
01987   } while ((status == 0) && (lp_count <= 0));
01988 
01989   return status;
01990 
01991 usage:
01992   (void) fprintf(vis_stderr, "source [-h] [-p] [-s] [-x] file_name\n");
01993   (void) fprintf(vis_stderr, "\t-h print the command usage\n");
01994   (void) fprintf(vis_stderr, "\t-p supply prompt before reading each line\n");
01995   (void) fprintf(vis_stderr, "\t-s silently ignore nonexistent file\n");
01996   (void) fprintf(vis_stderr, "\t-x echo each line as it is executed\n");
01997   return 1;
01998 }
01999 
02011 static void
02012 print_alias(
02013   char * value)
02014 {
02015   int i;
02016   CmdAliasDescr_t *alias;
02017 
02018   alias = (CmdAliasDescr_t *) value;
02019   (void) fprintf(vis_stdout, "%s\t", alias->name);
02020   for(i = 0; i < alias->argc; i++) {
02021     (void) fprintf(vis_stdout, " %s", alias->argv[i]);
02022   }
02023   (void) fprintf(vis_stdout, "\n");
02024 }
02025 
02026 
02038 static char *
02039 command_alias_help(
02040   char * command)
02041 {
02042   char *value;
02043   CmdAliasDescr_t *alias;
02044 
02045   if (!avl_lookup(cmdAliasTable, command, &value)) {
02046     return command;
02047   }
02048   alias = (CmdAliasDescr_t *) value;
02049   return alias->argv[0];
02050 }
02051 
02065 static void
02066 FlushBuffers(
02067   int sigtype)
02068 {
02069   fflush(vis_stdout);
02070   fflush(vis_stderr);
02071 
02072   /* Reprogram again the handler */
02073 #ifdef SIGUSR1
02074   (void) signal(SIGUSR1, FlushBuffers);
02075 #endif
02076 } /* End of FlushBuffers */