home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume14 / mush6.0 / part04 / init.c < prev    next >
C/C++ Source or Header  |  1988-04-12  |  10KB  |  369 lines

  1. /* init.c    (c) copyright 1986 (Dan Heller) */
  2.  
  3. /* init.c -- functions and whatnot that initialize everything */
  4. #include "mush.h"
  5. #include <pwd.h>
  6.  
  7. #ifdef SUNTOOL
  8. /* mouse symbols */
  9. short dat_mouse_left[] = {
  10. #include <images/confirm_left.pr> 
  11. };
  12.  
  13. short dat_mouse_middle[] = {
  14. #include <images/confirm_middle.pr> 
  15. };
  16.  
  17. short dat_mouse_right[] = {
  18. #include <images/confirm_right.pr> 
  19. };
  20.  
  21. short dat_mail_icon_1[] = {
  22. #include "mail.icon.1"
  23. };
  24.  
  25. short dat_mail_icon_2[] = {
  26. #include "mail.icon.2"
  27. };
  28.  
  29. short dat_coffee_cup[] = {
  30. #include "coffee.cup.pr"
  31. };
  32.  
  33. short dat_read_cursor[] = {
  34. #include "glasses.pr"
  35. };
  36.  
  37. short dat_write_cursor[] = {
  38. #include "write.pr"
  39. };
  40.  
  41. short dat_up_arrow[] = {
  42. #include "up.arrow.pr"
  43. };
  44.  
  45. short dat_dn_arrow[] = {
  46. #include "dn.arrow.pr"
  47. };
  48.  
  49. short dat_envelope[] = {
  50. #include "envelope.pr"
  51. };
  52.  
  53. short dat_cycle_cursor[] = {
  54. #include "cycle.pr"
  55. };
  56.  
  57. short dat_check_cursor[] = {
  58. #include "check.pr"
  59. };
  60.  
  61. mpr_static(mail_icon_image1, 64, 64, 1, dat_mail_icon_1);
  62. mpr_static(mail_icon_image2, 64, 64, 1, dat_mail_icon_2);
  63.  
  64. mpr_static(mouse_left,      16, 16, 1, dat_mouse_left);
  65. mpr_static(mouse_middle,    16, 16, 1, dat_mouse_middle);
  66. mpr_static(mouse_right,     16, 16, 1, dat_mouse_right);
  67. mpr_static(coffee_cup,      16, 16, 1, dat_coffee_cup);
  68. mpr_static(glasses_cursor,  16, 16, 1, dat_read_cursor);
  69. mpr_static(pencil_cursor,   16, 16, 1, dat_write_cursor);
  70. mpr_static(up_arrow,        16, 16, 1, dat_up_arrow);
  71. mpr_static(dn_arrow,        16, 16, 1, dat_dn_arrow);
  72. mpr_static(envelope_cursor, 16, 16, 1, dat_envelope);
  73. mpr_static(cycle,           16, 16, 1, dat_cycle_cursor);
  74. mpr_static(check_cursor,    16, 16, 1, dat_check_cursor);
  75.  
  76. struct cursor l_cursor     = { 3, 3, PIX_SRC,         &mouse_left      };
  77. struct cursor m_cursor     = { 3, 3, PIX_SRC,         &mouse_middle    };
  78. struct cursor r_cursor     = { 3, 3, PIX_SRC,         &mouse_right     };
  79. struct cursor coffee       = { 8, 8, PIX_SRC,         &coffee_cup      };
  80. struct cursor read_cursor  = { 8, 8, PIX_SRC|PIX_DST, &glasses_cursor  };
  81. struct cursor write_cursor = { 8, 8, PIX_SRC|PIX_DST, &pencil_cursor   };
  82. struct cursor main_cursor  = { 8, 8, PIX_SRC,         &envelope_cursor };
  83. struct cursor checkmark = { 8, 8, PIX_SRC|PIX_DST, &check_cursor };
  84.  
  85. /* text and font will be set in mail_status() */
  86. struct icon mail_icon = { 64,  64, (struct pixrect *)NULL,
  87.             { 0, 0, 64, 64 }, &mail_icon_image1,
  88.             { 5, 5, 26, 12 }, NULL, (struct pixfont *)NULL, 0 };
  89.  
  90. char *font_files[] = {
  91.     "serif.r.14", "sail.r.6", "serif.r.16",
  92. };
  93.  
  94. char *alt_fonts[] = {
  95.     "gacha.r.8", "sail.r.6", "screen.r.14",
  96. };
  97.  
  98. #endif /* SUNTOOL */
  99.  
  100. #ifdef BSD
  101. #include <netdb.h>
  102. #endif /* BSD */
  103.  
  104. void
  105. init()
  106. {
  107.     char         *home;
  108.     extern char        *getlogin();
  109. #ifdef SYSV
  110.     extern struct passwd *getpwuid();  /* sys-v forgot this in pwd.h! */
  111. #else
  112.     char ourhost[128];
  113. #endif /* SYSV */
  114.     register char     *p;
  115.     struct passwd     *entry;
  116.     int            cnt = 0;
  117. #ifdef BSD
  118.     struct hostent     *hp;
  119. #endif /* BSD */
  120.  
  121.     home = getenv ("HOME");
  122.  
  123.     if (!(entry = getpwuid(getuid())))
  124.     if (p = getlogin())
  125.         strdup(login, p);
  126.     else {
  127.         login = "unknown";
  128.         print("I don't know you, but that's ok.\n");
  129.     }
  130.     else {
  131.     strdup(login, entry->pw_name);
  132.     if (!home || !*home)
  133.         home = entry->pw_dir;
  134.     endpwent();
  135.     }
  136.     if (!home || !*home || Access(home, W_OK)) {
  137.     if (home && *home)
  138.         error(home);
  139.     else
  140.         print("No home!? ");
  141.     print_more("Using \"%s\" as home.\n", ALTERNATE_HOME);
  142.     } else {
  143.     char buf[MAXPATHLEN];
  144.     cmd_line(sprintf(buf, "set home=\"%s\"", home), msg_list);
  145.     }
  146.     crt = 25;
  147.     screen = 18;
  148.     escape = DEF_ESCAPE;
  149.     prompt = DEF_PROMPT;
  150.  
  151. #ifndef SYSV
  152.     (void) gethostname(ourhost, sizeof ourhost);
  153.     if (!(hp = gethostbyname(ourhost)))
  154.     error("gethostbyname: %s", ourhost);
  155.     else for (p = hp->h_name; p && *p && cnt < MAX_HOST_NAMES;
  156.           p = hp->h_aliases[cnt++])
  157.     ourname[cnt] = savestr(p);
  158.     endhostent();
  159. #endif /* SYSV */
  160.  
  161. #ifdef CURSES
  162.     init_bindings();
  163. #endif /* CURSES */
  164. }
  165.  
  166. /*
  167.  * Source a file, or just the default file.  Since sourcing files
  168.  * means reading possible aliases, don't expand the ! as history
  169.  * by setting the IGN_BANG flag.  Since a command in the sourced file
  170.  * may call source on another file, this routine may be called from
  171.  * within itself.  Continue to ignore ! chars by setting save_bang (local).
  172.  *
  173.  * Try opening the file passed to us.  If not given, check for the correct
  174.  * .rc file which is found in the user's home dir.
  175.  */
  176. source(argc, argv)
  177. char **argv;
  178. {
  179.     register char *p, *p2, **newargv;
  180.     int       line_no = 0, if_else = 0, parsing = 1, cont_line = 0;
  181.     FILE      *fp;
  182.     char       file[128], line[BUFSIZ];
  183.     u_long      save_bang = ison(glob_flags, IGN_BANG);
  184.  
  185.     if (argc && *++argv && !strcmp(*argv, "-?"))
  186.     return help(0, "source_help", cmd_help);
  187.     if (argc && *argv)
  188.     (void) strcpy(file, *argv);
  189.     else if (p = getenv("MAILRC"))
  190.     (void) strcpy(file, p);
  191.     else {
  192.     char *home = do_set(set_options, "home");
  193.     if (!home || !*home)
  194.         home = ALTERNATE_HOME;
  195.     if (Access(sprintf(file, "%s/%s", home, MAILRC), R_OK)
  196.           && Access(sprintf(file, "%s/%s", home, ALTERNATE_RC), R_OK))
  197.     (void) strcpy(file, DEFAULT_RC);
  198.     }
  199.  
  200.     argc = 0; /* don't ignore ENOENT */
  201.     p = getpath(file, &argc);
  202.     if (argc) {
  203.     if (strcmp(file, DEFAULT_RC))
  204.         if (argc == -1)
  205.         print("%s: %s\n", file, p);
  206.         else
  207.         print("%s is a directory.\n", file);
  208.     return -1;
  209.     }
  210.     if (!(fp = fopen(p, "r"))) {
  211.     if (errno != ENOENT)
  212.         error("Can't open %s", p);
  213.     return -1;
  214.     }
  215.     (void) strcpy(file, p);
  216.     turnon(glob_flags, IGN_BANG); /* ignore ! when reading record files */
  217.     while (p = fgets(&line[cont_line], BUFSIZ - cont_line, fp)) {
  218.     line_no++;
  219.     if (*(p2 = no_newln(p)) == '\\') {
  220.         cont_line = p2 - line;
  221.         continue;
  222.     } else
  223.         cont_line = 0;
  224.     /* don't consider comments (#) in lines. check if # is within quotes */
  225.         if (p = any(line, "\"'#")) {
  226.         register int balanced = 1;
  227.         while (p && (*p == '\'' || *p == '"') && balanced) {
  228.         /* first find matching quote */
  229.         register char *quote = index(p+1, *p);
  230.         if (!quote) {
  231.             print("%s: line %d: unbalanced %c.\n", file, line_no, *p);
  232.             balanced = 0;
  233.         } else
  234.             p = any(quote+1, "'\"#");
  235.         }
  236.         if (!balanced)
  237.         continue;
  238.         if (p && *p == '#')
  239.         *p = 0; /* found a Comment: null terminate line at comment */
  240.     }
  241.     if (!*line || !(newargv = make_command(line, TRPL_NULL, &argc))) {
  242.         if (!strncmp(line, "if", 2))
  243.         if_else++, parsing = FALSE;
  244.         continue;
  245.     }
  246.     if (!strcmp(newargv[0], "endif")) {
  247.         if (!if_else)
  248.         print("%s: line %d: endif with no \"if\".\n", file, line_no);
  249.         else
  250.         if_else = 0, parsing = 1;
  251.         goto bad;
  252.     } else if (!strcmp(newargv[0], "else")) {
  253.         if (!if_else)
  254.         print("%s: line %d: if-less \"else\".\n", file, line_no);
  255.         else
  256.         parsing = !parsing;
  257.         goto bad;
  258.     } else if (parsing && !strcmp(newargv[0], "if")) {
  259.         /* if statements are of the form:
  260.          *     if expr
  261.          *     if !expr  or  if ! expr
  262.          *     if expr == expr   or   if expr != expr
  263.          */
  264.         int equals = TRUE;
  265.         register char *lhs = newargv[1], *rhs = NULL;
  266.  
  267.         if (if_else)
  268.         print("%s: line %d: no nested if statements!\n", file, line_no);
  269.         else
  270.         if_else = 1;
  271.         parsing = 0;
  272.         if (!lhs || !*lhs) {
  273.         print("%s: line %d: if what?\n", file, line_no);
  274.         goto bad;
  275.         }
  276.         /* "lhs" is the left hand side of the equation
  277.          * In this instance, we're doing case 2 above.
  278.          */
  279.         if (*lhs == '!') {
  280.         int tmp = argc;
  281.         equals = FALSE;
  282.         if (!*++lhs)
  283.             if (!(lhs = newargv[2])) {
  284.             print("%s: %d: syntax error: \"if ! <what?>\"\n",
  285.                 file, line_no);
  286.             goto bad;
  287.             } else
  288.             tmp--;
  289.         if (tmp > 2) {
  290.             print("%s: %d: syntax error: \"if !<expr> <more junk>\"\n",
  291.             file, line_no);
  292.             goto bad;
  293.         }
  294.         } else if (argc > 2) {
  295.         if (argc != 4) {
  296.             print("%s: %d: argument count error: line has %d args.\n",
  297.             file, line_no, argc);
  298.             goto bad;
  299.         }
  300.         /* now check newargv[1] for == or != */
  301.         if (!strcmp(newargv[2], "!="))
  302.             equals = FALSE;
  303.         else if (strcmp(newargv[2], "==")) {
  304.             print("%s: %d: use `==' or `!=' only.\n", file, line_no);
  305.             goto bad;
  306.         }
  307.         rhs = newargv[3];
  308.         }
  309.         if (!strcmp(lhs, "redirect") &&
  310.               (ison(glob_flags, REDIRECT) && equals ||
  311.                isoff(glob_flags, REDIRECT) && !equals)
  312.         || !strcmp(lhs, "is_sending") &&
  313.               (ison(glob_flags, IS_SENDING) && equals ||
  314.                isoff(glob_flags, IS_SENDING) && !equals)
  315.         || !strcmp(lhs, "hdrs_only") &&
  316.               (hdrs_only && equals || !hdrs_only && !equals)
  317.         || !strcmp(lhs, "istool") &&
  318.               (istool && equals || !istool && !equals)
  319.         || !strcmp(lhs, "iscurses") &&
  320.               ((iscurses || ison(glob_flags, PRE_CURSES)) && equals ||
  321.               (isoff(glob_flags, PRE_CURSES) && !iscurses && !equals)))
  322.             parsing = 1;
  323.         else if (rhs)
  324.         if (strcmp(lhs, rhs) && !equals || !strcmp(lhs, rhs) && equals)
  325.             parsing = 1;
  326. bad:
  327.         free_vec(newargv);
  328.         continue;
  329.     }
  330.     if (parsing && argc > 0)
  331.         if (!strcmp(newargv[0], "exit")) {
  332.         if_else = 0;
  333.         break;
  334.         } else
  335.         (void) do_command(argc, newargv, msg_list);
  336.     else
  337.         free_vec(newargv);
  338.     }
  339.     if (if_else)
  340.     print("%s: missing endif\n", file);
  341.     fclose(fp);
  342.     /* if we entered the routine ignoring !, leave it that way. */
  343.     if (!save_bang)
  344.     turnoff(glob_flags, IGN_BANG);
  345.     return -1;
  346. }
  347.  
  348. #ifdef SUNTOOL
  349. /* open all fonts and place in fonts array. */
  350. getfonts()
  351. {
  352.     char tmp[80];
  353.     register int offset = strlen(FONTDIR) + 1;
  354.     struct pixfont *pf_open();
  355.  
  356.     (void) sprintf(tmp, "%s/", FONTDIR);
  357.     for (total_fonts = 0; total_fonts < MAX_FONTS; total_fonts++) {
  358.     (void) strcpy(&tmp[offset], font_files[total_fonts]);
  359.     if (!(fonts[total_fonts] = pf_open(tmp))) {
  360.         (void) strcpy(&tmp[offset], alt_fonts[total_fonts]);
  361.         if (!(fonts[total_fonts] = pf_open(tmp))) {
  362.         print("couldn't open font \"%s\"\n", tmp);
  363.         fonts[total_fonts] = pf_default();
  364.         }
  365.     }
  366.     }
  367. }
  368. #endif /* SUNTOOL */
  369.