]> andersk Git - moira.git/blobdiff - clients/moira/utils.c
Diane Delgado's changes for a fixed table-locking order
[moira.git] / clients / moira / utils.c
index bad1587eeb0a45d7d16fa142cd0c28ffa024b272..570a30dabab78075682ce69e60530a58bee90dea 100644 (file)
@@ -2,8 +2,8 @@
   static char rcsid_module_c[] = "$Header$";
 #endif lint
 
-/*     This is the file utils.c for the SMS Client, which allows a nieve
- *      user to quickly and easily maintain most parts of the SMS database.
+/*     This is the file utils.c for the MOIRA Client, which allows a nieve
+ *      user to quickly and easily maintain most parts of the MOIRA database.
  *     It Contains:  Many useful utility functions.
  *     
  *     Created:        4/25/88
@@ -20,9 +20,9 @@
  */
 
 #include <stdio.h>
-#include <strings.h>
-#include <sms.h>
-#include <sms_app.h>
+#include <string.h>
+#include <moira.h>
+#include <moira_site.h>
 #include <menu.h>
 #include <ctype.h>
 
 #include "defs.h"
 #include "f_defs.h"
 #include "globals.h"
-
 #include <netdb.h>             /* for gethostbyname. */
+#include <sys/types.h>
+#include <netinet/in.h>
+
 
 /*     Function Name: AddQueue
  *     Description: Adds an element to a queue
@@ -69,6 +71,27 @@ struct qelem *elem;
        (elem->q_back)->q_forw = elem->q_forw;
 }
 
+
+/* CopyInfo: allocates memory for a copy of a NULL terminated array of 
+ * strings <and returns a pointer to the copy.
+ */
+
+char **CopyInfo(info)
+char **info;
+{
+    char **ret;
+    int i;
+
+    ret = (char **) malloc(sizeof(char *) * (CountArgs(info) + 1));
+    if (ret == NULL)
+      return(ret);
+    for (i = 0; info[i]; i++)
+      ret[i] = Strsave(info[i]);
+    ret[i] = NULL;
+    return(ret);
+}
+
+
 /*     Function Name: FreeInfo
  *     Description: Frees all elements of a NULL terminated arrary of char*'s
  *     Arguments: info - array who's elements we are to free.
@@ -179,7 +202,7 @@ struct qelem * elem;
 }
 
 /*     Function Name: StoreInfo
- *     Description: Stores information from an sms query into a queue.
+ *     Description: Stores information from an moira query into a queue.
  *     Arguments: argc, argv, - information returned from the query returned
  *                               in argv.
  *                 data - the previous element on the queue, this data will be
@@ -187,7 +210,7 @@ struct qelem * elem;
  *                        If NULL then a new queue will be created.  This value
  *                        is updated to the current element at the end off the
  *                        call.
- *     Returns: SMS_CONT, or SMS_ABORT if it has problems.
+ *     Returns: MR_CONT, or MR_ABORT if it has problems.
  */
 
 int
@@ -205,7 +228,7 @@ char * data;
        Put_message("Could Not allocate more memory.");
        FreeQueue(*old_elem);
        *old_elem = (struct qelem *) NULL;
-       return(SMS_ABORT);
+       return(MR_ABORT);
     }
 
     for (count = 0; count < argc; count++)
@@ -216,7 +239,7 @@ char * data;
     AddQueue(new_elem, *old_elem);
 
     *old_elem = new_elem;
-    return(SMS_CONT);
+    return(MR_CONT);
 }
 
 /*     Function Name: CountArgs
@@ -251,9 +274,11 @@ int
 Scream()
 {
     com_err(program_name, 0,
-           "\nAn SMS update returned a value -- programmer botch\n");
-    sms_disconnect();
+           "\nA Moira update returned a value -- programmer botch\n");
+    mr_disconnect();
     exit(1);
+    /*NOTREACHED*/
+    return(-1);
 }
 
 /*     Function Name: PromptWithDefault
@@ -262,7 +287,7 @@ Scream()
  *     Arguments: prompt - the prompt string.
  *                 buf, buflen - buffer to be returned and its MAX size?
  *                 default value for the answer.
- *     Returns: the value returned by prompt_input.
+ *     Returns: zero on failure
  */
 
 int
@@ -274,16 +299,17 @@ char *def;
     char tmp[BUFSIZ];
     int ans;
 
+    if (parsed_argc > 0) {
+       parsed_argc--;
+       strncpy(buf, parsed_argv[0], buflen);
+       sprintf(tmp, "%s: %s", prompt, buf);
+       Put_message(tmp);
+       parsed_argv++;
+       return(1);
+    }
+
     (void) sprintf(tmp, "%s [%s]: ", prompt, def ? def : "");
     ans = Prompt_input(tmp, buf, buflen);
-    if (ans == 0) {
-       if (YesNoQuestion("Are you sure you want to exit", 1)) {
-           Cleanup_menu();
-           exit(0);
-       }
-       Put_message("Continuing input...");
-       return(PromptWithDefault(prompt, buf, buflen, def));
-    }
     if (IS_EMPTY(buf))
        (void) strcpy(buf, def);
     else if (!strcmp(buf, "\"\""))
@@ -385,9 +411,9 @@ char *s;
 {
     if (IS_EMPTY(s))
        Put_message("Please use a non-empty name.");
-    else if (index(s, ' '))
+    else if (strchr(s, ' '))
        Put_message("You cannot use space (' ') in this name.");
-    else if (index(s, WILDCARD))
+    else if (strchr(s, '*') || strchr(s, '?') || strchr(s, '['))
        Put_message("Wildcards not accepted here.");
     else
        return TRUE;
@@ -417,13 +443,13 @@ ToggleVerboseMode()
 /*     Function Name: NullFunc
  *     Description:  dummy callback routine 
  *     Arguments: none
- *     Returns: SMS_CONT
+ *     Returns: MR_CONT
  */
 
 int
 NullFunc()
 {
-    return(SMS_CONT);
+    return(MR_CONT);
 }
 
 /*     Function Name: SlipInNewName
@@ -444,8 +470,8 @@ char * name;
     register int i;
 
     /* This also pushes the NULL down. */
-    for (i = CountArgs(info); i > 1; i--) { 
-       info[i] = info[i-1];
+    for (i = CountArgs(info); i > 0; i--) { 
+       info[i+1] = info[i];
     }
     info[1] = name;    /* now slip in the name. */
 }
@@ -465,7 +491,7 @@ char * prompt, ** pointer;
 {
     char buf[BUFSIZ];
 
-    if (PromptWithDefault(prompt, buf, BUFSIZ, *pointer) == -1)
+    if (PromptWithDefault(prompt, buf, BUFSIZ, *pointer) == 0)
        return(SUB_ERROR);
 
 /* 
@@ -474,10 +500,11 @@ char * prompt, ** pointer;
  * a difference that makes no difference, IS no difference.
  */
 
-    if (strcmp(buf, *pointer) != 0) { 
-       if (*pointer != NULL)
+    if (*pointer != NULL) {
+       if (strcmp(buf, *pointer) != 0) { 
            free(*pointer);
-       *pointer = Strsave(buf);
+           *pointer = Strsave(buf);
+       }
     }
     return(SUB_NORMAL);
 }
@@ -558,8 +585,9 @@ int mask, current, *new;
  */
 
 int
-GetFSTypes(current)
+GetFSTypes(current, options)
 char **  current;
+int options;
 {
     int c_value, new_val = 0;  /* current value of filesys type (int). */
     char ret_value[BUFSIZ];
@@ -569,14 +597,19 @@ char **  current;
     else 
        c_value = atoi(*current);
 
-    if (GetFSVal("student", SMS_FS_STUDENT, c_value, &new_val) == FALSE)
+    if (GetFSVal("student", MR_FS_STUDENT, c_value, &new_val) == FALSE)
        return(SUB_ERROR);
-    if (GetFSVal("faculty", SMS_FS_FACULTY, c_value, &new_val) == FALSE)
+    if (GetFSVal("faculty", MR_FS_FACULTY, c_value, &new_val) == FALSE)
        return(SUB_ERROR);
-    if (GetFSVal("staff", SMS_FS_STAFF, c_value, &new_val) == FALSE)
+    if (GetFSVal("staff", MR_FS_STAFF, c_value, &new_val) == FALSE)
        return(SUB_ERROR);
-    if (GetFSVal("miscellaneous", SMS_FS_MISC, c_value, &new_val) == FALSE)
+    if (GetFSVal("miscellaneous", MR_FS_MISC, c_value, &new_val) == FALSE)
        return(SUB_ERROR);
+    if (options) {
+       if (GetFSVal("Group Quotas", MR_FS_GROUPQUOTA, c_value, &new_val) ==
+           FALSE)
+         return(SUB_ERROR);
+    }
 
     FreeAndClear(current, TRUE);
     sprintf(ret_value, "%d", new_val);
@@ -602,11 +635,27 @@ char *str;
        return (strcpy(newstr, str));
 }
 
+
+/* atot: convert ASCII integer unix time into human readable date string */
+
+char *atot(itime)
+char *itime;
+{
+    int time;
+    char *ct, *ctime();
+
+    time = atoi(itime);
+    ct = ctime(&time);
+    ct[24] = 0;
+    return(&ct[4]);
+}
+
+
 /*     Function Name: Print
  *     Description: prints out all the arguments on a single line.
- *     Arguments: argc, argv - the standard SMS arguments.
+ *     Arguments: argc, argv - the standard MR arguments.
  *                 callback - the callback function - NOT USED.
- *     Returns: SMS_CONT
+ *     Returns: MR_CONT
  */
 
 /* ARGSUSED */
@@ -624,16 +673,16 @@ char **argv, *callback;
        (void) sprintf(buf,"%s %s",buf,argv[i]);
     (void) Put_message(buf);
 
-    return (SMS_CONT);
+    return (MR_CONT);
 }
 
 /*     Function Name: PrintByType
  *     Description: This function prints all members of the type specified
  *                   by the callback arg, unless the callback is NULL, in which
  *                   case it prints all members.
- *     Arguments: argc, argc - normal arguments for sms_callback function. 
+ *     Arguments: argc, argc - normal arguments for mr_callback function. 
  *                 callback - either a type of member or NULL.
- *     Returns: SMS_CONT or SMS_QUIT.
+ *     Returns: MR_CONT or MR_QUIT.
  */
 
 /*ARGSUSED*/
@@ -646,7 +695,7 @@ char **argv, *callback;
        return( Print(argc, argv, callback) );
     if (strcmp(argv[0], callback) == 0) 
        return( Print(argc, argv, callback) );
-    return(SMS_CONT);
+    return(MR_CONT);
 }
 
 /*     Function Name: PrintHelp
@@ -790,7 +839,7 @@ char *tname;
     argv[1] = "TYPE";
     argv[2] = "*";
     elem = NULL;
-    if (stat = do_sms_query("get_alias", 3, argv, StoreInfo, (char *)&elem)) {
+    if (stat = do_mr_query("get_alias", 3, argv, StoreInfo, (char *)&elem)) {
        com_err(program_name, stat, " in GetTypeValues");
        return(NULL);
     }
@@ -803,7 +852,7 @@ char *tname;
     }
     elem = (struct qelem *) malloc(sizeof(struct qelem));
     ce = (struct cache_elem *) malloc(sizeof(struct cache_elem));
-    ce->cache_name = tname;
+    ce->cache_name = strsave(tname);
     ce->cache_data = QueueTop(oelem);
     elem->q_data = (char  *)ce;
     AddQueue(elem, cache);
@@ -815,10 +864,10 @@ char *tname;
 /*     Function Name: GetTypeFromUser
  *     Description: gets a typed value from the user
  *     Arguments: prompt string, type name, buffer pointer
- *     Returns: 
+ *     Returns: SUB_ERROR if ^C, SUB_NORMAL otherwise
  */
 
-GetTypeFromUser(prompt, tname, pointer)
+int GetTypeFromUser(prompt, tname, pointer)
 char *prompt;
 char *tname;
 char  **pointer;
@@ -838,7 +887,8 @@ char  **pointer;
     strcat(buffer, ")");
     if (strlen(buffer) > 64)
        sprintf(buffer, "%s (? for help)", prompt);
-    GetValueFromUser(buffer, pointer);
+    if (GetValueFromUser(buffer, pointer) == SUB_ERROR)
+      return(SUB_ERROR);
     if (**pointer == '?') {
        sprintf(buffer, "Type %s is one of:", tname);
        Put_message(buffer);
@@ -849,8 +899,10 @@ char  **pointer;
        return(GetTypeFromUser(prompt, tname, pointer));
     }
     for (elem = GetTypeValues(tname); elem; elem = elem->q_forw) {
-       if (!cistrcmp(elem->q_data, *pointer))
+       if (!strcasecmp(elem->q_data, *pointer)) {
+           strcpy(*pointer, elem->q_data);
            return(SUB_NORMAL);
+       }
     }
     sprintf(buffer, "\"%s\" is not a legal value for %s.  Use one of:",
            *pointer, tname);
@@ -860,15 +912,17 @@ char  **pointer;
     }
     sprintf(buffer, "Are you sure you want \"%s\" to be a legal %s",
            *pointer, tname);
-    if (YesNoQuestion("Do you want this to be a new legal value", 0) &&
-       YesNoQuestion(buffer, 0)) {
+    if (YesNoQuestion("Do you want this to be a new legal value", 0) == TRUE &&
+       YesNoQuestion(buffer, 0) == TRUE) {
        argv[0] = tname;
        argv[1] = "TYPE";
        argv[2] = *pointer;
-       for (p = argv[2]; *p; p++)
+       /* don't uppercase access flags.  Do uppercase everything else */
+       if (strncmp(tname, "fs_access", 9))
+         for (p = argv[2]; *p; p++)
            if (islower(*p))
-               *p = toupper(*p);
-       if (stat = do_sms_query("add_alias", 3, argv, Scream, NULL)) {
+             *p = toupper(*p);
+       if (stat = do_mr_query("add_alias", 3, argv, Scream, NULL)) {
            com_err(program_name, stat, " in add_alias");
        } else {
            elem = (struct qelem *) malloc(sizeof(struct qelem));
@@ -882,7 +936,34 @@ char  **pointer;
 }
 
 
-do_sms_query(name, argc, argv, proc, hint)
+/*     Function Name: GetAddressFromUser
+ *     Description: gets an IP address from the user
+ *     Arguments: prompt string, buffer pointer
+ *             buffer contains default value as long int
+ *     Returns: SUB_ERROR if ^C, SUB_NORMAL otherwise
+ */
+
+int GetAddressFromUser(prompt, pointer)
+char *prompt;
+char  **pointer;
+{
+    char *value, buf[256];
+    struct in_addr addr;
+    int ret;
+
+    addr.s_addr = htonl(atoi(*pointer));
+    value = strsave(inet_ntoa(addr));
+    ret = GetValueFromUser(prompt, &value);
+    if (ret == SUB_ERROR) return(SUB_ERROR);
+    addr.s_addr = inet_addr(value);
+    free(pointer);
+    sprintf(buf, "%d", ntohl(addr.s_addr));
+    *pointer = strsave(buf);
+    return(SUB_NORMAL);
+}
+
+
+do_mr_query(name, argc, argv, proc, hint)
 char *name;
 int argc;
 char **argv;
@@ -890,23 +971,26 @@ int (*proc)();
 char *hint;
 {
     int status;
-    extern char *whoami;
+    extern char *whoami, *moira_server;
 
-    status = sms_query(name, argc, argv, proc, hint);
-    if (status != SMS_ABORTED && status != SMS_NOT_CONNECTED)
+    refresh_screen();
+    status = mr_query(name, argc, argv, proc, hint);
+    if (status != MR_ABORTED && status != MR_NOT_CONNECTED)
       return(status);
-    status = sms_connect(SMS_SERVER);
+    status = mr_connect(moira_server);
     if (status) {
-       com_err(whoami, status, " while re-connecting to server");
-       return(SMS_ABORTED);
+       com_err(whoami, status, " while re-connecting to server %s",
+               moira_server);
+       return(MR_ABORTED);
     }
-    status = sms_auth(whoami);
+    status = mr_auth(whoami);
     if (status) {
-       com_err(whoami, status, " while re-authenticating to server");
-       sms_disconnect();
-       return(SMS_ABORTED);
+       com_err(whoami, status, " while re-authenticating to server %s",
+               moira_server);
+       mr_disconnect();
+       return(MR_ABORTED);
     }
-    status = sms_query(name, argc, argv, proc, hint);
+    status = mr_query(name, argc, argv, proc, hint);
     return(status);
 }
 
This page took 0.056649 seconds and 4 git commands to generate.