/*{{{ main*/ int main(int argc, char *argv[]) { /*{{{ variables*/ enum { NOTHING, LOCAL, REMOTE } in=NOTHING; int c; int x,y; int mouse_x,mouse_y; int image_width,image_height; int image_xoffset=0,image_yoffset=0; int my_width,my_height; int err=0,usage=0; BITMAP *bp; FILE *input; static struct menu_entry menu[] = { { "quit","q" } }; char file[_POSIX_PATH_MAX]; /*}}} */ /*{{{ parse arguments*/ while ((c=getopt(argc,argv,"l:r:"))!=EOF) { switch (c) { /*{{{ l file*/ case 'l': { if ((input=fopen(optarg,"r"))==(FILE*)0) { fprintf(stderr,"%s: Can't open %s\r\n",argv[0],optarg); err=1; } else in=LOCAL; break; } /*}}} */ /*{{{ r file*/ case 'r': { char *cwd; in=REMOTE; if (*optarg!='/' && *optarg!='.') { if ((cwd=getcwd((char*)0,(size_t)0))!=(char*)0) { strcpy(file,cwd); strcat(file,"/"); strcat(file,optarg); } else { fprintf(stderr,"%s: Can't get current directory\r\n",argv[0]); err=1; } } else strcpy(file,optarg); break; } /*}}} */ /*{{{ default*/ default: { usage=1; break; } /*}}} */ } } if (err) exit(err); if (usage || optind!=argc) { fprintf(stderr,"Usage: mgrview [-l file | -r file]\n"); exit(1); } if (in==NOTHING) { in=LOCAL; input=stdin; } /*}}} */ /*{{{ setup*/ ckmgrterm(argv[0]); m_setup(M_MODEOK); signal(SIGINT,clean); signal(SIGTERM,clean); signal(SIGPIPE,clean); m_ttyset(); m_push(P_MENU|P_EVENT|P_FLAGS); m_setmode(M_ABS); m_setcursor(CS_INVIS); menu_load(1,1,menu); m_setevent(REDRAW, "R"); m_setevent(RESHAPE, "R"); m_setevent(BUTTON_1,"[%p]"); m_setevent(BUTTON_2,"m"); m_flush(); /*}}} */ if (in==LOCAL) { /*{{{ load bitmap to client space*/ if ((bp=bitmapread(input))==(BITMAP*)0) { fprintf(stderr,"%s: No MGR bitmap.\r\n",argv[0]); clean(1); } image_width=BIT_WIDE(bp); image_height=BIT_HIGH(bp); /*}}} */ /*{{{ transfer bitmap to server space*/ m_func(BIT_SRC); m_bitcreate(IMAGE_BITMAP,image_width,image_height); m_bitldto(image_width,image_height,0,0,IMAGE_BITMAP,bit_size(image_width,image_height,BIT_DEPTH(bp))); fwrite(BIT_DATA(bp),bit_size(image_width,image_height,BIT_DEPTH(bp)),1,m_termout); m_flush(); bit_destroy(bp); /*}}} */ } else if (in==REMOTE) { /*{{{ transfer bitmap from server fs to server space*/ m_bitfromfile(IMAGE_BITMAP,file); m_get(); if (sscanf(m_linebuf,"%d %d",&image_width,&image_height)<2) { fprintf(stderr,"%s: No MGR bitmap.\r\n",argv[0]); clean(1); } /*}}} */ } /*{{{ user interaction*/ get_size((int*)0,(int*)0,&my_width,&my_height); display(image_xoffset,image_yoffset,my_width,my_height,image_width,image_height); m_flush(); while ((c=getc(m_termin))!='q') switch (c) { /*{{{ m -- left button displays menu*/ case 'm': { m_selectmenu(1); m_flush(); break; } /*}}} */ /*{{{ [%d %d] -- right button*/ case '[': { fscanf(m_termin,"%d %d]",&mouse_x,&mouse_y); /*{{{ compute new x start*/ if (my_width>image_width) image_xoffset=0; else if (mouse_x<=0) image_xoffset=0; else if (mouse_x>=my_width) image_xoffset=my_width-image_width; else { /*{{{ move x start by difference from mouse and middle*/ image_xoffset=image_xoffset-(mouse_x-my_width/2); /*}}} */ /*{{{ check and corrent range of x start*/ if (image_xoffset<my_width-image_width) image_xoffset=my_width-image_width; else if (image_xoffset>0) image_xoffset=0; /*}}} */ } /*}}} */ /*{{{ compute new y start*/ if (my_height>image_height) image_yoffset=0; else if (mouse_y<=0) image_yoffset=0; else if (mouse_y>=my_height) image_yoffset=my_height-image_height; else { /*{{{ move y start by difference from mouse and middle*/ image_yoffset=image_yoffset-(mouse_y-my_height/2); /*}}} */ /*{{{ check and corrent range of y start*/ if (image_yoffset<my_height-image_height) image_yoffset=my_height-image_height; else if (image_yoffset>0) image_yoffset=0; /*}}} */ } /*}}} */ display(image_xoffset,image_yoffset,my_width,my_height,image_width,image_height); m_flush(); break; } /*}}} */ /*{{{ R -- redraw*/ case 'R': { get_size((int*)0,(int*)0,&my_width,&my_height); /*{{{ compute new x offset*/ if (my_width<image_width) { if (image_xoffset<my_width-image_width) image_xoffset=my_width-image_width; } else image_xoffset=0; /*}}} */ /*{{{ compute new y offset*/ if (my_height<image_height) { if (image_yoffset<my_height-image_height) image_yoffset=my_height-image_height; } else image_yoffset=0; /*}}} */ m_func(BIT_CLR); m_bitwrite(0,0,my_width,my_height); m_func(BIT_SRC); m_bitcopyto(image_xoffset,image_yoffset,image_width,image_height,0,0,WINDOW_BITMAP,IMAGE_BITMAP); m_flush(); break; } /*}}} */ } /*}}} */ /*{{{ exit*/ m_bitdestroy(IMAGE_BITMAP); get_colrow(&x,&y); m_move(0,y-1); clean(0); /* NOTREACHED */ return 255; /*}}} */ }
/*{{{ main*/ int main(int argc, char *argv[]) { /*{{{ variables*/ int x, login_y, password_y; char loginstr[9], passwordstr[9], ret; char ttystr[_POSIX_PATH_MAX]; char *background = (char *)0; char *fontname = (char *)0; /*}}} */ /*{{{ parse arguments*/ { int c; while ((c = getopt(argc, argv, "b:f:")) != EOF) switch (c) { case 'b': background = optarg; break; case 'f': fontname = optarg; break; } /*{{{ parse tty*/ { int tty; if (optind + 1 > argc) { fprintf(stderr, "Usage: %s tty\n", argv[0]); exit(1); } else { strcpy(ttystr, "/dev/"); strcat(ttystr, argv[optind++]); } close(0); close(1); close(2); setsid(); if ((tty = open(ttystr, O_RDWR)) != 0) { fprintf(stderr, "%s: Can't open controlling terminal on fd 0.\n", argv[0]); exit(1); } fchmod(tty, 0600); fchown(tty, getuid(), getgid()); open(argv[optind], O_RDWR); open(argv[optind], O_RDWR); } /*}}} */ } /*}}} */ /*{{{ get into grafics mode*/ signal(SIGTERM, quit); signal(SIGHUP, quit); set_tty(0); if ((screen = bit_open(SCREEN_DEV)) == (BITMAP *)0) { reset_tty(0); exit(EX_NOPERM); } bit_grafscreen(); /*}}} */ /*{{{ load font*/ if (fontname) { char fontpath[_POSIX_PATH_MAX]; if (*fontname == '/' || *fontname == '.') strcpy(fontpath, fontname); else { strcpy(fontpath, ICONDIR); strcat(fontpath, "/"); strcat(fontpath, fontname); } if ((font = open_font(fontname)) == (struct font *)0) font = open_font((char *)0); } else font = open_font((char *)0); /*}}} */ /*{{{ draw background*/ bit_blit(screen, 0, 0, screen->wide, screen->high, BIT_CLR, (BITMAP *)0, 0, 0); if (background) { BITMAP *bp; FILE *fp; char backgroundpath[_POSIX_PATH_MAX]; if (*background == '/' || *background == '.') strcpy(backgroundpath, background); else { strcpy(backgroundpath, ICONDIR); strcat(backgroundpath, "/"); strcat(backgroundpath, background); } if ((fp = fopen(backgroundpath, "r")) != (FILE *)0 && (bp = bitmapread(fp)) != (BITMAP *)0) { int x, y; for (x = 0; x < screen->wide; x += bp->wide) bit_blit( screen, x, 0, screen->wide - x < bp->wide ? screen->wide - x : bp->wide, bp->high, BIT_SRC, bp, 0, 0); for (y = 0; y < screen->high; y += bp->high) bit_blit( screen, 0, y, screen->wide, screen->high - y < bp->high ? screen->high - y : bp->high, BIT_SRC, screen, 0, 0); } } /*}}} */ /*{{{ draw hostname*/ { int bx, bw, by, bh; char hostname[_POSIX_PATH_MAX]; struct hostent *h; gethostname(hostname, sizeof(hostname)); if ((h = gethostbyname(hostname)) != (struct hostent *)0) strcpy(hostname, h->h_name); bw = font->head.wide * (strlen(hostname) + 2); bh = 2 * font->head.high; bx = (screen->wide - bw) / 2; by = screen->high / 6 - bh / 2; cutebox(bx, by, bw, bh); printstr(bx + font->head.wide, by + bh - font->head.high / 2, hostname); } /*}}} */ /*{{{ draw login box*/ { int bx, bw, by, bh; bx = (screen->wide - font->head.wide * 40) / 2; by = (screen->high - font->head.high * 8) / 2; bw = font->head.wide * 40; bh = font->head.high * 8; cutebox(bx, by, bw, bh); } /*}}} */ /*{{{ draw login box contents*/ x = (screen->wide - font->head.wide * 18) / 2; login_y = screen->high / 2 - font->head.wide / 6; password_y = screen->high / 2 + font->head.high / 6 + font->head.high; printstr(x, password_y, "Password: "******"Press ESC for terminal login"); /*}}} */ while (1) { /*{{{ get login and password or escape*/ printstr(x, login_y, "Login: "******"mgr", (char *)0 }; int i; sprintf(env_user, "USER=%s", pw->pw_name); sprintf(env_logname, "LOGNAME=%s", pw->pw_name); sprintf(env_home, "HOME=%s", pw->pw_dir); sprintf(env_shell, "SHELL=%s", pw->pw_shell == (char *)0 || pw->pw_shell[0] == '\0' ? "/bin/sh" : pw->pw_shell); sprintf(env_path, "PATH=%s", PATH); sprintf(env_mail, "MAIL=%s/%s", MAILDIR, pw->pw_name); if (chdir(pw->pw_dir) != 0) chdir("/"); if (ttyname(0)) { chown(ttyname(0), pw->pw_uid, pw->pw_gid); chmod(ttyname(0), 0600); } for (i = 1; i <= _NSIG; i++) signal(i, SIG_DFL); bit_destroy(screen); reset_tty(0); initgroups(pw->pw_name, pw->pw_gid); setgid(pw->pw_gid); setuid(pw->pw_uid); sprintf(mgrlogin, "%s/.mgrlogin", pw->pw_dir); execve(mgrlogin, login_argv, login_env); execve(MGR_BINARY, login_argv, login_env); exit(EX_OSFILE); } /*}}} */ else /*{{{ incorrect login*/ { printstr((screen->wide - font->head.wide * 16) / 2, login_y + 3 * font->head.high, "Login incorrect"); } /*}}} */ } /*}}} */ } }