home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume11 / mush5.7 / part12 / init.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-09-19  |  10.7 KB  |  399 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         buf[BUFSIZ], cwd[128], *home;
  108.     extern char        *getwd(), *getlogin();
  109. #ifdef SYSV
  110.     char *getcwd();
  111.     extern struct passwd *getpwuid();  /* sys-v forgot this in pwd.h! */
  112. #else
  113.     char ourhost[128];
  114. #endif SYSV
  115.     register char     *p, *p2 = buf, **argv;
  116.     struct passwd     *entry;
  117.     int            cnt = 0;
  118. #ifdef BSD
  119.     struct hostent     *hp;
  120. #endif BSD
  121.  
  122.     home = getenv ("HOME");
  123.  
  124.     if (!(entry = getpwuid(getuid())))
  125.     if (p = getlogin())
  126.         strdup(login, p);
  127.     else {
  128.         login = "unknown";
  129.         print("I don't know you, but that's ok.\n");
  130.     }
  131.     else {
  132.     strdup(login, entry->pw_name);
  133.     if (!home || !*home)
  134.         home = entry->pw_dir;
  135.     endpwent();
  136.     }
  137.     if (!home || !*home || access(home, 2)) {
  138.     error(home);
  139.     home = ALTERNATE_HOME;
  140.     print_more("Using \"%s\" as home.\n", home);
  141.     }
  142.  
  143. #ifndef SYSV
  144.     (void) gethostname(ourhost, sizeof ourhost);
  145.     if (!(hp = gethostbyname(ourhost)))
  146.     error("gethostbyname: %s", ourhost);
  147.     else for (p = hp->h_name; p && *p && cnt < MAX_HOST_NAMES;
  148.           p = hp->h_aliases[cnt++])
  149.     ourname[cnt] = savestr(p);
  150.     endhostent();
  151. #endif SYSV
  152.  
  153. #ifndef SYSV
  154.     if (getwd(cwd) == NULL)
  155. #else
  156.     if (getcwd(cwd, 128) == NULL)
  157. #endif SYSV
  158.     error("getcwd: %s", cwd), *cwd = 0;
  159.  
  160.     p2 += strlen(strcpy(p2, "set "));
  161.     p2 += strlen(sprintf(p2, "cwd=\"%s\" ", cwd));
  162.     p2 += strlen(sprintf(p2, "home=\"%s\" ", home));
  163.     p2 += strlen(sprintf(p2, "prompt=\"%s\" ", DEF_PROMPT));
  164.     p2 += strlen(sprintf(p2, "mbox=\"%s\" ",   DEF_MBOX));
  165.     p2 += strlen(sprintf(p2, "folder=\"%s\" ", DEF_FOLDER));
  166.     p2 += strlen(sprintf(p2, "escape=\"%s\" ", DEF_ESCAPE));
  167.  
  168.     p = getenv("SHELL");
  169.     p2 += strlen(sprintf(p2, "shell=\"%s\" ", (p)? p: DEF_SHELL));
  170.  
  171.     p = getenv("EDITOR");
  172.     p2 += strlen(sprintf(p2, "editor=\"%s\" ", (p)? p: DEF_EDITOR));
  173.  
  174.     p = getenv("VISUAL");
  175.     p2 += strlen(sprintf(p2, "visual=\"%s\" ", (p)? p: DEF_EDITOR));
  176.  
  177.     p = getenv("PAGER");
  178.     p2 += strlen(sprintf(p2, "pager=\"%s\" ", (p)? p: DEF_PAGER));
  179.  
  180.     p = getenv("PRINTER");
  181.     p2 += strlen(sprintf(p2, "printer=\"%s\" ", (p)? p: DEF_PRINTER));
  182.  
  183.     crt = 25;
  184.     /* p2 += strlen(strcat(p2, "crt=\"25\" ")); */
  185.     screen = 18;
  186.     /* p2 += strlen(strcat(p2, "screen=\"18\" ")); */
  187.  
  188.     if (!(argv = make_command(buf, TRPL_NULL, &cnt)))
  189.     print("error initializing variables.\n");
  190.     else {
  191.     (void) set(cnt, argv);
  192.     free_vec(argv);
  193.     }
  194. #ifdef CURSES
  195.     init_bindings();
  196. #endif CURSES
  197. }
  198.  
  199. /*
  200.  * Source a file, or just the default file.  Since sourcing files
  201.  * means reading possible aliases, don't expand the ! as history
  202.  * by setting the IGN_BANG flag.  Since a command in the sourced file
  203.  * may call source on another file, this routine may be called from
  204.  * within itself.  Continue to ignore ! chars by setting save_bang (local).
  205.  *
  206.  * Try opening the file passed to us.  If not given, check for the correct
  207.  * .rc file which is found in the user's home dir.
  208.  */
  209. source(argc, argv)
  210. char **argv;
  211. {
  212.     register char *p, *p2, **newargv;
  213.     int       line_no = 0, if_else = 0, parsing = 1, cont_line = 0;
  214.     FILE      *fp;
  215.     char       file[128], line[BUFSIZ];
  216.     long      save_bang = ison(glob_flags, IGN_BANG);
  217.  
  218.     if (argc && *++argv && !strcmp(*argv, "-?"))
  219.     return help(0, "source_help", cmd_help);
  220.     if (argc && *argv)
  221.     (void) strcpy(file, *argv);
  222.     else if (p = getenv("MAILRC"))
  223.     (void) strcpy(file, p);
  224.     else {
  225.     char *home = do_set(set_options, "home");
  226.     if (!home || !*home)
  227.         home = ALTERNATE_HOME;
  228.     if (access(sprintf(file, "%s/%s", home, MAILRC), R_OK)
  229.           && access(sprintf(file, "%s/%s", home, ALTERNATE_RC), R_OK))
  230.     (void) strcpy(file, DEFAULT_RC);
  231.     }
  232.  
  233.     argc = 0; /* don't ignore ENOENT */
  234.     p = getpath(file, &argc);
  235.     if (argc) {
  236.     if (strcmp(file, DEFAULT_RC))
  237.         if (argc == -1)
  238.         print("%s: %s\n", file, p);
  239.         else
  240.         print("%s is a directory.\n", file);
  241.     return -1;
  242.     }
  243.     if (!(fp = fopen(p, "r"))) {
  244.     if (errno != ENOENT)
  245.         error("Can't open %s", p);
  246.     return -1;
  247.     }
  248.     (void) strcpy(file, p);
  249.     turnon(glob_flags, IGN_BANG); /* ignore ! when reading record files */
  250.     while (p = fgets(&line[cont_line], BUFSIZ - cont_line, fp)) {
  251.     line_no++;
  252.     if (*(p2 = no_newln(line + cont_line)) == '\\') {
  253.         cont_line = p2 - line;
  254.         continue;
  255.     } else
  256.         cont_line = 0;
  257.     /* don't consider comments (#) in lines. check if # is within quotes */
  258.         if (p = any(p, "\"'#")) {
  259.         register int balanced = 1;
  260.         while (p && (*p == '\'' || *p == '"') && balanced) {
  261.         /* first find matching quote */
  262.         register char *quote = index(p+1, *p);
  263.         if (!quote) {
  264.             print("%s: line %d: unbalanced %c.\n", file, line_no, *p);
  265.             balanced = 0;
  266.         } else
  267.             p = any(quote+1, "'\"#");
  268.         }
  269.         if (!balanced)
  270.         continue;
  271.         if (p && *p == '#')
  272.         *p = 0; /* found a Comment: null terminate line at comment */
  273.     }
  274.     if (!*line || !(newargv = make_command(line, TRPL_NULL, &argc))) {
  275.         if (!strncmp(line, "if", 2))
  276.         if_else++, parsing = FALSE;
  277.         continue;
  278.     }
  279.     if (!strcmp(newargv[0], "endif")) {
  280.         if (!if_else)
  281.         print("%s: line %d: endif with no \"if\".\n", file, line_no);
  282.         else
  283.         if_else = 0, parsing = 1;
  284.         goto bad;
  285.     } else if (!strcmp(newargv[0], "else")) {
  286.         if (!if_else)
  287.         print("%s: line %d: if-less \"else\".\n", file, line_no);
  288.         else
  289.         parsing = !parsing;
  290.         goto bad;
  291.     } else if (parsing && !strcmp(newargv[0], "if")) {
  292.         /* if statements are of the form:
  293.          *     if expr
  294.          *     if !expr  or  if ! expr
  295.          *     if expr == expr   or   if expr != expr
  296.          */
  297.         int equals = TRUE;
  298.         register char *lhs = newargv[1], *rhs = NULL;
  299.  
  300.         if (if_else)
  301.         print("%s: line %d: no nested if statements!\n", file, line_no);
  302.         else
  303.         if_else = 1;
  304.         parsing = 0;
  305.         if (!lhs || !*lhs) {
  306.         print("%s: line %d: if what?\n", file, line_no);
  307.         goto bad;
  308.         }
  309.         /* "lhs" is the left hand side of the equation
  310.          * In this instance, we're doing case 2 above.
  311.          */
  312.         if (*lhs == '!') {
  313.         int tmp = argc;
  314.         equals = FALSE;
  315.         if (!*++lhs)
  316.             if (!(lhs = newargv[2])) {
  317.             print("%s: %d: syntax error: \"if ! <what?>\"\n",
  318.                 file, line_no);
  319.             goto bad;
  320.             } else
  321.             tmp--;
  322.         if (tmp > 2) {
  323.             print("%s: %d: syntax error: \"if !<expr> <more junk>\"\n",
  324.             file, line_no);
  325.             goto bad;
  326.         }
  327.         } else if (argc > 2) {
  328.         if (argc != 4) {
  329.             print("%s: %d: argument count error: line has %d args.\n",
  330.             file, line_no, argc);
  331.             goto bad;
  332.         }
  333.         /* now check newargv[1] for == or != */
  334.         if (!strcmp(newargv[2], "!="))
  335.             equals = FALSE;
  336.         else if (strcmp(newargv[2], "==")) {
  337.             print("%s: %d: use `==' or `!=' only.\n", file, line_no);
  338.             goto bad;
  339.         }
  340.         rhs = newargv[3];
  341.         }
  342.         if (!strcmp(lhs, "redirect") &&
  343.               (ison(glob_flags, REDIRECT) && equals ||
  344.                isoff(glob_flags, REDIRECT) && !equals)
  345.         || !strcmp(lhs, "istool") &&
  346.               (istool && equals || !istool && !equals)
  347.         || !strcmp(lhs, "hdrs_only") &&
  348.               (hdrs_only && equals || !hdrs_only && !equals)
  349.         || !strcmp(lhs, "iscurses") &&
  350.               ((iscurses || ison(glob_flags, PRE_CURSES)) && equals ||
  351.               (isoff(glob_flags, PRE_CURSES) && !iscurses && !equals)))
  352.             parsing = 1;
  353.         else if (rhs)
  354.         if (strcmp(lhs, rhs) && !equals || !strcmp(lhs, rhs) && equals)
  355.             parsing = 1;
  356. bad:
  357.         free_vec(newargv);
  358.         continue;
  359.     }
  360.     if (parsing && argc > 0)
  361.         if (!strcmp(newargv[0], "exit")) {
  362.         if_else = 0;
  363.         break;
  364.         } else
  365.         (void) do_command(argc, newargv, msg_list);
  366.     else
  367.         free_vec(newargv);
  368.     }
  369.     if (if_else)
  370.     print("%s: missing endif\n", file);
  371.     fclose(fp);
  372.     /* if we entered the routine ignoring !, leave it that way. */
  373.     if (!save_bang)
  374.     turnoff(glob_flags, IGN_BANG);
  375.     return -1;
  376. }
  377.  
  378. #ifdef SUNTOOL
  379. /* open all fonts and place in fonts array. */
  380. getfonts()
  381. {
  382.     char tmp[80];
  383.     register int offset = strlen(FONTDIR) + 1;
  384.     struct pixfont *pf_open();
  385.  
  386.     (void) sprintf(tmp, "%s/", FONTDIR);
  387.     for (total_fonts = 0; total_fonts < MAX_FONTS; total_fonts++) {
  388.     (void) strcpy(&tmp[offset], font_files[total_fonts]);
  389.     if (!(fonts[total_fonts] = pf_open(tmp))) {
  390.         (void) strcpy(&tmp[offset], alt_fonts[total_fonts]);
  391.         if (!(fonts[total_fonts] = pf_open(tmp))) {
  392.         print("couldn't open font \"%s\"\n", tmp);
  393.         fonts[total_fonts] = pf_default();
  394.         }
  395.     }
  396.     }
  397. }
  398. #endif SUNTOOL
  399.