Esempio n. 1
0
int
run_access (newts_nfref *ref)
{
  List access_list;
  short changed = FALSE;
  short redraw = TRUE;
  int c;
  int first = 0;
  int entries;

  entries = get_access_list (ref, &access_list);

  while (1)
    {
      if (redraw)
        display_access (&access_list, first, entries);
      redraw = FALSE;

      mvprintw (LINES - 2, 0, _("Option: "));
      refresh ();

      c = getch ();

      switch (c)
        {
        case '?':
          access_help ();
          /* Fall through. */

        case 'r':  /* Redraw the screen. */
        case '\f':
          redraw = TRUE;
          break;

        case '!':
          redraw = TRUE;
          spawn_subshell ();
          break;

        case 's':
          move (LINES - 2, 0);
          clrtoeol ();
          printw (_("Sorting..."));
          refresh ();
          list_merge_sort (&access_list);
          redraw = TRUE;
          break;

        case 'Q':  /* Quit without saving. */
        case 'K':
          list_destroy (&access_list);
          return 0;

        case 'q':
        case 'k':
          if (changed)
            {
              list_merge_sort (&access_list);
              write_access_list (ref, &access_list);
            }
          list_destroy (&access_list);
          return 0;

        case '-':
        case KEY_UP:
        case KEY_PPAGE:
          first -= c == KEY_UP ? 1 : (LINES - 6) / 2 - 1;
          if (first < 0)
            first = 0;
          redraw = TRUE;
          break;

        case '+':
        case KEY_DOWN:
        case KEY_NPAGE:
          first += c == KEY_DOWN ? 1 : (LINES - 6) / 2 - 1;
          if (first >= entries - (LINES - 6) / 2)
            {
              first = entries - (LINES - 6) - 3;
              if (first < 0)
                first = 0;
            }
          redraw = TRUE;
          break;

        case 'i':  /* Insert new entries. */
          {
            char *persistent_error = NULL;
            short stop = FALSE;

            while (entries < NPERMS && !stop)
              {
                ListNode *node;
                struct access *access_entry;
                char *temp, *name, *prompt;
                int key, i;
                int y, x;
                enum newts_access_scopes scope;
                int scope_is_set = FALSE;
                int mode;
                short restart = FALSE;
                short advice_displayed = FALSE;

                if (traditional)
                  mvprintw (LINES - 5, 39, _("Entry type: "));
                else
                  {
                    clear ();
                    display_access (&access_list, first, entries);
                    if (persistent_error != NULL)
                      {
                        move (LINES - 3, 0);
                        clrtoeol ();
                        printw ("%s", persistent_error);
                        persistent_error = NULL;
                      }
                    move (LINES - 2, 0);
                    clrtoeol ();
                    printw (_("Entry type: "));
                  }
                refresh ();
                if (!traditional)
                  advice_displayed = FALSE;
                getyx (stdscr, y, x);
                while (scope_is_set == FALSE)
                  {
                    key = getch ();
                    if (key == '\n' || key == '\r' || key == KEY_ENTER ||
                        key == 'q' || key == 'k')
                      {
                        if (traditional && (key == 'k' || key == 'q'))
                          echochar (key);
                        stop = TRUE;
                        break;
                      }

                    switch (key)
                      {
                      case 'u':
                        if (traditional)
                          {
                            echochar (key);
                            move (y, x);
                          }
                        scope = SCOPE_USER;
                        scope_is_set = TRUE;
                        break;

                      case 'g':
                        if (traditional)
                          {
                            echochar (key);
                            move (y, x);
                          }
                        scope = SCOPE_GROUP;
                        scope_is_set = TRUE;
                        break;

                      case 's':
                        if (traditional)
                          {
                            echochar (key);
                            move (y, x);
                          }
                        scope = SCOPE_SYSTEM;
                        scope_is_set = TRUE;
                        break;

                      case KEY_RESIZE:
                        break;

                      case EOF:
                        clear ();
                        display_access (&access_list, first, entries);
                        if (traditional)
                          {
                            if (advice_displayed)
                              mvprintw (LINES - 5, 54, "(u,g,s, q,k,<cr>)");
                            move (LINES - 5, 51);
                          }
                        else
                          {
                            if (advice_displayed)
                              mvprintw (LINES - 3, 0,
                                        _("Please enter 'u', 'g', or 's'; or "
                                          "'q', 'k', or <RET> to exit."));
                            mvprintw (LINES - 2, 0, _("Entry type: "));
                          }
                        refresh ();
                        break;

                      default:
                        advice_displayed = TRUE;
                        if (traditional)
                          {
                            mvprintw (LINES - 5, 54, "(u,g,s, q,k,<cr>)");
                            move (LINES - 5, 51);
                          }
                        else
                          {
                            move (LINES - 3, 0);
                            clrtoeol ();
                            printw (_("Please enter 'u', 'g', or 's'; or 'q', "
                                      "'k', or <RET> to exit."));
                            move (LINES - 2, 0);
                            clrtoeol ();
                            printw (_("Entry type: "));
                          }
                        refresh ();
                        break;
                      }
                  }

                if (stop) continue;

                if (traditional)
                  {
                    prompt = newts_nmalloc (strlen (_("Name: ")) + 40,
                                            sizeof (char));
                    strcpy (prompt, "                                       ");
                    strncat (prompt, _("Name: "),
                             strlen (_("Name: ")) + 1);
                    move (LINES - 4, 0);
                    clrtoeol ();
                  }
                else
                  {
                    move (LINES - 3, 0);
                    clrtoeol ();
                    prompt = newts_strdup (scope == SCOPE_SYSTEM ? _("System name: ") :
                                           scope == SCOPE_GROUP ? _("Group name: ") :
                                           _("User name: "));
                    move (LINES - 2, 0);
                    clrtoeol ();
                  }
                refresh ();
                temp = gl_getline (prompt);
                temp[strlen (temp) - 1] = '\0';
                newts_free (prompt);

                if (strlen (temp) == 0)
                  continue;
                name = newts_strdup (temp);
                gl_histadd (name);

                if (scope == SCOPE_USER)
                  {
                    if (strcasecmp (name, "other") != 0)
                      {
                        struct passwd *pw = getpwnam (name);

                        if (pw == NULL)
                          {
                            if (traditional)
                              {
                                move (LINES - 3, 0);
                                clrtoeol ();
                                mvprintw (LINES - 3, 39, _("--No such user--"));
                              }
                            else
                              persistent_error = _("No such user.");
                            continue;
                          }

                        endpwent ();
                      }
                  }

                if (scope == SCOPE_GROUP)
                  {
                    if (strcasecmp (name, "other") != 0)
                      {
                        struct group *gp = getgrnam (name);

                        if (gp == NULL)
                          {
                            if (traditional)
                              {
                                move (LINES - 3, 0);
                                clrtoeol ();
                                mvprintw (LINES - 3, 39, _("--No such group--"));
                              }
                            else
                              persistent_error = _("No such group.");
                            continue;
                          }

                        endgrent ();
                      }
                  }

                node = list_head (&access_list);
                for (i=0; i<entries; i++)
                  {
                    access_entry = (struct access *) list_data (node);
                    if (access_scope (access_entry) == scope &&
                        strcmp (access_name (access_entry), name) == 0)
                      {
                        if (traditional)
                          {
                            move (LINES - 3, 0);
                            clrtoeol ();
                            mvprintw (LINES - 3, 39, _("%s entry exists"), name);
                          }
                        else
                          persistent_error =
                            scope == SCOPE_USER ? _("User already exists in "
                                             "permission table.") :
                            (scope == SCOPE_GROUP ? _("Group already exists in "
                                               "permission table.") :
                             _("System already exists in permission table."));
                        restart = TRUE;
                        continue;
                      }
                    node = list_next (node);
                    if (node == NULL)
                      continue;
                  }

                if (restart)
                  continue;

                {
                  struct access *new_access = access_alloc ();

                  access_set_permissions (new_access, READ | WRITE | REPLY);
                  access_set_scope (new_access, scope);
                  access_set_name (new_access, name);

                  get_mode (new_access, &access_list, first, entries);

                  list_insert_sorted (&access_list, (void *) new_access);
                }

                newts_free (name);

                entries++;
                redraw = TRUE;
                changed = TRUE;
                clear ();
                display_access (&access_list, first, entries);
              }

            if (!traditional)
              redraw = TRUE;
            break;
          }

        case 'd':  /* Delete existing entries. */
          {
            ListNode *node, *prev;
            struct access *data;
            int key, number, i;

            move (LINES - 2, 0);
            clrtoeol ();
            if (traditional)
              printw ("%s", _("Delete entry #: "));
            else
              printw ("%s", _("Delete entry number: "));

            key = getch ();
            while (key != '\n' && key != '\r' && key != KEY_ENTER &&
                   (key < '1' || key > '9'))
              key = getch ();

            if (key == '\n' || key == '\r' || key == KEY_ENTER)
              {
                redraw = TRUE;
                break;
              }

            number = get_number (key, entries);
            if (number < 0)
              {
                redraw = TRUE;
                break;
              }
            if (number > entries || key < '0' || key > '9' || number == 0)
              {
                clear ();
                display_access (&access_list, first, entries);
                if (traditional)
                  {
                    move (LINES - 1, 0);
                    clrtoeol ();
                    printw ("%s", _("Bad entry"));
                  }
                else
                  {
                    move (LINES - 3, 0);
                    clrtoeol ();
                    printw ("%s", _("Invalid entry."));
                  }
                break;
              }

            number--;  /* Adjust to base zero. */

            prev = NULL;
            node = list_head (&access_list);
            for (i=0; i<number; i++)
              {
                prev = node;
                node = list_next (prev);
              }

            data = (struct access *) list_data (node);
            if (data->scope == SCOPE_USER && strcmp (data->name, username) == 0)
              {
                clear ();
                display_access (&access_list, first, entries);
                if (traditional)
                  {
                    move (LINES - 1, 0);
                    clrtoeol ();
                    printw ("%s", _(" Can't Delete self"));
                  }
                else
                  {
                    move (LINES - 3, 0);
                    clrtoeol ();
                    printw ("%s", _("Can't delete own entry."));
                  }
                break;
              }

            list_remove_next (&access_list, prev, NULL);

            entries--;
            changed = TRUE;
            redraw = TRUE;
            break;
          }

        case 'm':  /* Modify existing entries. */
          {
            ListNode *node;
            struct access *existing_entry;
            int key, number, i;

            move (LINES - 2, 0);
            clrtoeol ();
            if (traditional)
              printw ("%s", _("Modify entry #: "));
            else
              printw ("%s", _("Modify entry number: "));

            key = getch ();
            while (key != '\n' && key != '\r' && key != KEY_ENTER &&
                   (key < '1' || key > '9'))
              key = getch ();

            if (key == '\n' || key == '\r' || key == KEY_ENTER)
              {
                redraw = TRUE;
                break;
              }

            number = get_number (key, entries);
            if (number < 0)
              {
                redraw = TRUE;
                break;
              }
            if (number > entries || key < '0' || key > '9' || number == 0)
              {
                clear ();
                display_access (&access_list, first, entries);
                if (traditional)
                  {
                    move (LINES - 1, 0);
                    clrtoeol ();
                    printw ("%s", _("Bad entry"));
                  }
                else
                  {
                    move (LINES - 3, 0);
                    clrtoeol ();
                    printw ("%s", _("Invalid entry."));
                  }
                break;
              }

            number--;  /* Adjust to base zero. */

            node = list_head (&access_list);
            for (i=0; i<number; i++)
              {
                node = list_next (node);
              }

            existing_entry = (struct access *) list_data (node);
            get_mode (existing_entry, &access_list, first, entries);
            changed = TRUE;
            redraw = TRUE;
            break;
          }

        case '\004':
          list_destroy (&access_list);
          return QUITNOSEQ;

        case 'z':
          list_destroy (&access_list);
          return QUITSEQ;

        default:
          beep ();
          break;
        }
    }

  return 0;
}
Esempio n. 2
0
void
AddHistory(char *line)
{
    gl_histadd(line);
}	/* AddHistory */
Esempio n. 3
0
void exread( char *prompt, char *input, FTNINT *iostat,
	     long int PromptLength, long int InputLength )
#endif
     
/*
************************************************************************
C
C     DESCRIPTION:
C     This routine prompts, reads, and echos from the standard input
C     device. For an interactive job, this would prompt for input from
C     the terminal and read (with echo) from the keyboard. For a batch
C     job, this would read from the main input file and echo to the
C     log file with the prompt string as a prefix. This routine should
C     assume the burden of assuring that the standard input and output
C     devices are properly openned.
C
C     FORMAL PARAMETERS:
C     PROMPT    CHARACTER       Prompt String
C     INPUT     CHARACTER       Input String
C     IOSTAT    INTEGER         I/O Status ( -1 = EOF, 0 = normal )
C
************************************************************************
*/
{
  static int debug = 0;
  if (debug == 1 || isatty(0) == 0 || isatty(1) == 0) {
    int icnt;
    
    (void)write( 1, prompt, PromptLength );
    icnt = my_getline( input, InputLength );
    
    /* Next evaluate the error status. */
    /* For icnt <= 0 indicate an error condition. */
    *iostat = ( icnt > 0 ) ? 0 : -1;
  } else {
    static char internal_prompt[128];
    char *p = NULL;

    /* Fill line with blanks... */
    int dlen = InputLength;
    char *ds = input;
    while( dlen-- > 0 )		/* Blank out the entire string. */
      *ds++ = ' ';
    
    strncpy(internal_prompt, prompt, PromptLength);
    internal_prompt[PromptLength-1] = ' ';
    internal_prompt[PromptLength]   = '\0';

    p = getline_int(internal_prompt);

    if (p) {
      gl_histadd(p);
      int i = 0;
      /* Strip the trailing \n */
      p[strlen(p)-1] = '\0';
      
      while (i < strlen(p) && i < InputLength) {
	input[i] = p[i];
	++i;
      }
      *iostat = 0;
    } else {
      *iostat = -1;
    }
  }
}