[torquedev] new node attribute: "note"

Marcus R. Epperson mrepper at sandia.gov
Mon Mar 19 21:59:17 MDT 2007


Garrick Staples wrote:
> On Mon, Mar 19, 2007 at 02:02:21PM -0600, Marcus R. Epperson alleged:
>> Do you have any suggestions for how to do this argument parsing correctly?
> 
> Indeed, we definitely need to stay away from GNU extensions.
> 
> How about we use lowercase n for listing, and capital N for setting?

Ok, I think this is a lot cleaner.  Let me know what you think.

-Marcus
-------------- next part --------------
--- torque-2.1.8-orig/src/cmds/pbsnodes.c	2007-03-09 01:06:23.000000000 -0700
+++ torque-2.1.8/src/cmds/pbsnodes.c	2007-03-19 21:44:49.000000000 -0600
@@ -124,6 +124,8 @@
 #define DIAG    7
 #define NOTE    8
 
+enum note_flags {unused, set, list};
+
 int quiet = 0;
 
 
@@ -314,6 +316,26 @@ static int is_unknown(
 
 
 
+/* returns a pointer to the note if there is one, otherwise NULL */
+static char *get_note(
+
+  struct batch_status *pbs)  /* I */
+
+  {
+  struct attrl *pat;
+
+  for (pat = pbs->attribs;pat != NULL;pat = pat->next) 
+    {
+    if (strcmp(pat->name,ATTR_NODE_note) == 0)
+      {
+      return(pat->value);
+      }
+    }
+
+  return(NULL);
+  }
+
+
 
 static int marknode(
 
@@ -394,8 +416,7 @@ int main(
   struct batch_status *pbstat;
   int	flag = ALLI;
   char	*note = NULL;
-  int	note_flag = 0;
-  int	len;
+  enum note_flags note_flag = unused;
 
   /* get default server, may be changed by -s option */
 
@@ -404,7 +425,7 @@ int main(
   if (def_server == NULL)
     def_server = "";
 
-  while ((i = getopt(argc,argv,"acdlopqrs:x-:n:")) != EOF)
+  while ((i = getopt(argc,argv,"acdlopqrs:x-:N:n")) != EOF)
     {
     switch(i) 
       {
@@ -470,39 +491,45 @@ int main(
 
         break;
 
-      case 'n':
-
-        note_flag = 1;
+      case 'N':
 
         /* preserve any previous option other than the default,
-         * to allow -n to be combined with -o, -c, etc
+         * to allow -N to be combined with -o, -c, etc
          */
-        if ( flag == ALLI )
+        if (flag == ALLI)
           flag = NOTE;
 
         note = strdup(optarg);
 
-        if ( note != NULL )
+        if (note == NULL)
           {
-          /* -n n is the same as -n ""  -- it clears the note */
-          if ( !strcmp(note,"n") )
-            {
-            *note = '\0';
-            }
+          perror("Error: strdup() returned NULL");
+          }
+        else
+          {
+          note_flag = set;
 
-          len = strlen(note);
+          /* -N n is the same as -N ""  -- it clears the note */
+          if (!strcmp(note,"n"))
+            *note = '\0';
 
-          if ( len > MAX_NOTE )
+          if (strlen(note) > MAX_NOTE)
             fprintf(stderr,"Warning: note exceeds length limit (%d) - server may reject it...\n",
               MAX_NOTE);
 
-          if ( strchr(note,'\n') != NULL )
+          if (strchr(note,'\n') != NULL)
             fprintf(stderr,"Warning: note contains a newline - server may reject it...\n");
 
           }
 
         break;
 
+      case 'n':
+
+        note_flag = list;
+
+        break;
+
       case '-':
 
         if ((optarg != NULL) && !strcmp(optarg,"version"))
@@ -524,16 +551,22 @@ int main(
 
         break;
       }  /* END switch (i) */
-    }    /* END while (i == getopt()) */
+    }    /* END while (i = getopt()) */
+
+  if ((note_flag == list) && (flag != LIST))
+    {
+    fprintf(stderr,"Error: -n requires -l\n");
+    errflg = 1;
+    }
 
   if (errflg != 0) 
     {
     if (!quiet)
       {
-      fprintf(stderr,"usage:\t%s [-{c|d|l|o|p|r}][-s server] [-n \"note\"] [-q] node node ...\n",
+      fprintf(stderr,"usage:\t%s [-{c|d|l|o|p|r}] [-s server] [-n] [-N \"note\"] [-q] node ...\n",
         argv[0]);
 
-      fprintf(stderr,"\t%s -{a|x} [-s server] [-q] [node]\n",
+      fprintf(stderr,"\t%s [-{a|x}] [-s server] [-q] [node]\n",
         argv[0]);
       }
 
@@ -607,7 +640,7 @@ int main(
     }    /* END if ((flag == ALLI) || (flag == DOWN) || (flag == LIST) || (flag == DIAG)) */
 
 
-  if ( note_flag )
+  if (note_flag == set)
     {
     /* set the note attrib string on specified nodes */
     for (pa = argv + optind;*pa;pa++) 
@@ -783,9 +816,21 @@ int main(
         {
         if (is_down(pbstat) || is_offline(pbstat) || is_unknown(pbstat)) 
           {
-          printf("%-20.20s %s\n", 
-            pbstat->name,
-            get_nstate(pbstat));
+          char *n;
+
+          if ((note_flag == list) && (n = get_note(pbstat)))
+            {
+            printf("%-20.20s %-26.26s %s\n",
+              pbstat->name,
+              get_nstate(pbstat),
+              n);
+            }
+          else
+            {
+            printf("%-20.20s %s\n",
+              pbstat->name,
+              get_nstate(pbstat));
+            }
           }
         }
 


More information about the torquedev mailing list