home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume38 / menushell / part03 / functions1.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-30  |  4.8 KB  |  226 lines

  1. #include "mshell.h"
  2.  
  3. extern char     G_homevar      [];
  4. extern char     G_uservar      [];
  5. extern char     G_termvar      [];
  6. extern char     G_mailfile     [WORDLEN];
  7. extern char     G_mail_message [WORDLEN];
  8. extern int      G_mailsize; 
  9. extern struct   stat G_st;
  10.  
  11. helpfile_display (filename)
  12. char * filename;
  13.  
  14. {
  15.     FILE * fp;
  16.     char record [DESCLEN];
  17.     if (( fp = fopen (filename, "r")) == NULL ) {
  18.         printf ("\tNo such helpfile as %s\!\!\n", filename);
  19.         return;
  20.     }
  21.  
  22.     while ( fgets (record, sizeof(record), fp) )
  23.         fputs (record, stdout);
  24.     printf ("\n");
  25.     fclose (fp);
  26. }
  27.  
  28. /* ================================ */
  29. char *
  30. prompt(p)
  31. /* ================================ */
  32. char *p;
  33. {
  34.     static char ans[WORDLEN];
  35.     extern int G_limited;
  36.  
  37.     printf ("%s : ", ufix(p));
  38.     if (!G_limited) {
  39.         read_input_line (ans);
  40.         replace_string (ans, HOME_CHAR, getenv(&G_homevar[1])) ;
  41.     }
  42.     else {
  43.         strcpy(ans, "XXX");
  44.         printf("(no input for unvalidated users)");
  45.     }
  46.     printf ("\n");
  47.     return(ans);
  48. }
  49.  
  50. /* ================================================ */
  51. extract_action_word ( str, sub_str, dest_str, flag)
  52. /* ================================================ */
  53. char *str, *sub_str, *dest_str;
  54. int  flag;
  55.  
  56. {
  57.     char *position, *target;
  58.     int startpos = 0, string_length = 0, i = 0;
  59.     unsigned  no_of_chars = 0;
  60.  
  61.     strcpy (dest_str, NULLSTR);        /* initialize dest_str */
  62.  
  63.     startpos = strsearch (str, sub_str);
  64.                         /* find position of sub-string 
  65.                            in main string             */
  66.  
  67.     if ( startpos != -1 )                /* if it exists */
  68.         target = &str[startpos] + strlen (sub_str);    
  69.                             /* assign target */
  70.     else
  71.         return;                /* no such string found */
  72.  
  73.     for (position = target;         /* actual value of parameter */
  74.         (*position != BLANK) && (*position != EOS);
  75.          ++position, ++no_of_chars);
  76.  
  77.     strncpy (dest_str, target, no_of_chars);  /* copy the required value */
  78.     dest_str[no_of_chars] = EOS;
  79.  
  80.     string_length = strlen(sub_str) + no_of_chars;
  81.  
  82.     if ( *position != EOS )
  83.         ++string_length ;
  84.  
  85.     remove_string (str, startpos, string_length) ;
  86.  
  87.     if ( flag ) 
  88.         for (i = 0; dest_str[i] != EOS ; ++i)
  89.             if ( dest_str[i] == VISIBLE_SPACE ) 
  90.                 dest_str[i] = BLANK;
  91. }
  92.  
  93. /* ===================== */
  94. display_menu (menu_name, menu_array, menu_flag, idx)
  95. /* ===================== */
  96. char * menu_name;
  97. char * menu_array [];
  98. int  * menu_flag;
  99. int  * idx;
  100.  
  101. {
  102.     FILE * fp;
  103.     char junk[COLUMNS] , keyword[DESCLEN], description[DESCLEN],
  104.         action_description[DESCLEN], *strsave(), *fgets(), *index();
  105.     register int i;
  106.     int delay;
  107.  
  108.     if (!moreinput())
  109.         clear_screen();
  110.  
  111.     check_for_new_mail (G_mail_message);
  112.  
  113.     if ( *menu_flag == FALSE ) {
  114.         *menu_flag = TRUE;
  115.         if (index(menu_name, '/'))
  116.             strcpy(junk, menu_name);
  117.         else
  118.             sprintf(junk, "%s/%s%s", MENUDIR, menu_name, SUFFIX);
  119.         if (( fp = fopen (junk, "r")) == NULL ) {
  120.             printf ("\tNo such menu as %s!!\n", junk);
  121.             return(0);
  122.         }
  123.  
  124.         for (
  125.               i = *idx = 0;
  126.               i < LINES &&
  127.             (menu_array[i] = strsave(fgets(junk, COLUMNS, fp)));
  128.               i++
  129.             )
  130.             if ( strcmp (menu_array[i], DELIM_LINE) == 0 )
  131.                 *idx = i;
  132.         
  133.         fclose (fp);
  134.  
  135.         for ( ; i < LINES; i++)
  136.             menu_array[i] = strsave("");
  137.     }
  138.  
  139.     if (strcmp(G_mail_message, "") != 0) {
  140.         junk[0] = EOS;
  141.         sscanf(menu_array[0], "%[^\n]", junk);
  142.         printf("%s%s\n", junk, G_mail_message);
  143.         i = 1;
  144.     }
  145.     else
  146.         i = 0;
  147.  
  148.     if (!moreinput())
  149.         for (; i < *idx; i++)
  150.             fputs(menu_array[i], stdout);
  151.  
  152.     printf ("\n");
  153.     return(1);
  154. }
  155.  
  156. /* find the home directory of the person invoking M_Shell */
  157. /* ====================================================== */
  158.  
  159. find_user_details (home_dir, user_name)
  160. char * home_dir;
  161. char * user_name;
  162. {
  163.     struct passwd *pw, *getpwuid();
  164.  
  165.     pw = getpwuid (getuid ());
  166.  
  167.      strcpy (home_dir, pw->pw_dir);
  168.     strcpy (user_name, pw->pw_name);
  169. }
  170.  
  171. /* ====================================================== */
  172. search_menu_array (menu_array, ind, option, string, invalid)
  173. /* ====================================================== */
  174. char * menu_array [];
  175. int    ind;
  176. char * option, * string;
  177. int  * invalid;
  178.  
  179. {
  180.     int i;
  181.     char keyword [WORDLEN];
  182.     char action_description[DESCLEN], junk[DESCLEN];
  183.     char opt1[OPTLEN], opt2[OPTLEN];
  184.  
  185.     for ( i = ++ind; i < LINES ; ++i ) {
  186.         sscanf (menu_array[i], "%s    %[^\n]", keyword, action_description);
  187.         if ( index (option, BLANK) != NULL ) 
  188.             sscanf (option, "%s %[^\n]", opt1, opt2);
  189.         else
  190.             strcpy (opt1, option);
  191.  
  192.         if ( strcmp (opt1, keyword) == 0 ) {
  193.             strcpy (string, action_description);
  194.             *invalid = FALSE;
  195.             return;
  196.         }
  197.     }
  198.  
  199.     if ( all_blanks(option)            || strcmp(option, QUIT) == 0 ||
  200.              strncmp(option, HELP, 4) == 0 || strcmp(option, BYE)  == 0 ||
  201.          strcmp(option, "T") == 0      || strcmp(option, "top") == 0)
  202.         *invalid = FALSE;
  203.     else
  204.         *invalid = TRUE;
  205.  
  206.     strcpy (string, NULLSTR);
  207. }
  208.  
  209. wait_for_user ()
  210. {
  211.     printf("\nHit ENTER to continue ... ");
  212.     read_input_line (NULL);
  213. }
  214.  
  215. clear_screen()
  216. {
  217.     static int doneinit = 0;
  218.  
  219.     if (!doneinit) {
  220.         initscr();
  221.         doneinit++;
  222.     }
  223.     clear();
  224.     refresh();
  225. }
  226.