int main( int argc, char** argv ) { char* varlist[255]; char* command[BUFFER_LENGTH]; int argCount = 1; signal( SIGSEGV, FaultHandler ); signal( SIGBUS, FaultHandler ); signal( SIGALRM, FaultHandler ); signal( SIGILL, FaultHandler ); alarm( loopTimer ); EXIT_STATUS = 0; if ( argc > 1 ){ DEBUG_LEVEL = atoi( argv[1] ); } else { DEBUG_LEVEL = 0; } if ( setjmp( buf )) { resets++; if (resets > 2 ){ ExitShell(-1); } printf("\tWARNING: recovered from an exception\n"); } /* set default values */ InitShell( command ); while ( EXIT_STATUS != 1 ){ /* prompt only if input is from STDIN */ if( isatty( STDIN_FILENO )){ /* display prompt */ printf("%s", PROMPT); } /* get input and parse command */ argCount = InputPrompt( command ); /* make sure returned command is not empty and command is not internally recognized */ if ( argCount ){ if ( !ExecuteInternal( command, varlist )) { ExecuteExternal( command, argCount ); } } } ExitShell( EXIT_STATUS ); }
int main(int argc,char *argv[]) { int i; unsigned int borderwidth ; char *display_name = NULL; char *wname = "wmload"; XGCValues gcv; unsigned long gcm; XEvent Event; XTextProperty name; XClassHint classHint; Pixmap pixmask; Atom _XA_WM_DELETE_WINDOW = None; Geometry = ""; mywmhints.initial_state = NormalState; /* Parse command line options */ ProgName = argv[0]; for(i=1;i<argc;i++) { char *arg= argv[i]; if (arg[0] == '-') { switch(arg[1]) { case 'u': if(++i >=argc) usage(); sscanf(argv[i], "%d", &updatespeed); continue; case 'e': if(++i >=argc) usage(); strcpy(&Execute[0], argv[i]); strcat(&Execute[0], " &"); continue; case 's': ONLYSHAPE=1; continue; case 'p': if(++i >=argc) usage(); Geometry = argv[i]; continue; case 'i': mywmhints.initial_state = IconicState; continue; case 'w': mywmhints.initial_state = WithdrawnState; continue; case 'l': if(++i >=argc) usage(); LedColor = argv[i]; continue; case 'v': fprintf(stdout, "\nwmload version: %i.%i.%i\n", major_VER, minor_VER, patch_VER); if(argc == 2) exit(0); continue; default: usage(); } } else { fprintf(stderr, "\nInvalid argument: %s\n", arg); usage(); } } /* Open the display */ if (!(dpy = XOpenDisplay(display_name))) { fprintf(stderr,"wmload: can't open display %s\n", XDisplayName(display_name)); exit (1); } screen= DefaultScreen(dpy); Root = RootWindow(dpy, screen); d_depth = DefaultDepth(dpy, screen); x_fd = XConnectionNumber(dpy); _XA_WM_DELETE_WINDOW = XInternAtom (dpy, "WM_DELETE_WINDOW", False); /* Convert XPM Data to XImage */ GetXPM(); /* Create a window to hold the banner */ mysizehints.flags= USSize|USPosition; mysizehints.x = 0; mysizehints.y = 0; back_pix = GetColor("white"); fore_pix = GetColor("black"); XWMGeometry(dpy, screen, Geometry, NULL, (borderwidth =1), &mysizehints, &mysizehints.x,&mysizehints.y,&mysizehints.width,&mysizehints.height, &i); mysizehints.width = wmload.attributes.width; mysizehints.height= wmload.attributes.height; win = XCreateSimpleWindow(dpy,Root,mysizehints.x,mysizehints.y, mysizehints.width,mysizehints.height, borderwidth,fore_pix,back_pix); iconwin = XCreateSimpleWindow(dpy,win,mysizehints.x,mysizehints.y, mysizehints.width,mysizehints.height, borderwidth,fore_pix,back_pix); /* activate hints */ XSetWMNormalHints(dpy, win, &mysizehints); classHint.res_name = "wmload"; classHint.res_class = "WMLoad"; XSetClassHint(dpy, win, &classHint); XSelectInput(dpy,win,MW_EVENTS); XSelectInput(dpy,iconwin,MW_EVENTS); XSetCommand(dpy,win,argv,argc); if (XStringListToTextProperty(&wname, 1, &name) ==0) { fprintf(stderr, "wmload: can't allocate window name\n"); exit(-1); } XSetWMName(dpy, win, &name); /* Create a GC for drawing */ gcm = GCForeground|GCBackground|GCGraphicsExposures; gcv.foreground = fore_pix; gcv.background = back_pix; gcv.graphics_exposures = FALSE; NormalGC = XCreateGC(dpy, Root, gcm, &gcv); if (ONLYSHAPE) { /* try to make shaped window here */ pixmask = XCreateBitmapFromData(dpy, win, (char *)mask2_bits, mask2_width, mask2_height); XShapeCombineMask(dpy, win, ShapeBounding, 0, 0, pixmask, ShapeSet); XShapeCombineMask(dpy, iconwin, ShapeBounding, 0, 0, pixmask, ShapeSet); } mywmhints.icon_window = iconwin; mywmhints.icon_x = mysizehints.x; mywmhints.icon_y = mysizehints.y; mywmhints.window_group = win; mywmhints.flags = StateHint | IconWindowHint | IconPositionHint | WindowGroupHint; XSetWMHints(dpy, win, &mywmhints); XSetWMProtocols (dpy, win, &_XA_WM_DELETE_WINDOW, 1); XMapWindow(dpy,win); InitLoad(); InsertLoad(); RedrawWindow(&visible); while(1) { if (actualtime != time(0)) { actualtime = time(0); if(actualtime % updatespeed == 0) InsertLoad(); RedrawWindow(&visible); } /* read a packet */ while (XPending(dpy)) { XNextEvent(dpy,&Event); switch(Event.type) { case Expose: if(Event.xexpose.count == 0 ) RedrawWindow(&visible); break; case ButtonPress: ExecuteExternal(); break; case ClientMessage: if ((Event.xclient.format != 32) || ((Atom)Event.xclient.data.l[0] != _XA_WM_DELETE_WINDOW)) break; case DestroyNotify: XFreeGC(dpy, NormalGC); XDestroyWindow(dpy, iconwin); XDestroyWindow(dpy, win); XCloseDisplay(dpy); exit(0); break ; default: break; } } XFlush(dpy); #ifdef SYSV poll((struct poll *) 0, (size_t) 0, 50); #else { struct timespec ts; ts.tv_sec = 0; ts.tv_nsec = 50000000L; /* 5/100 sec */ nanosleep(&ts, NULL); } #endif } return 0; }
void RunShell() { int running = 1; char* str; char** argv; InitBackgroundProcessing(); while (running) { CheckProcessQueue(); PrintPrompt(); str = ReadInput(); argv = ParseInput(str); if (argv[0] == NULL) { // do nothing if empty arguments } else if (CheckForIOandPipeErrors(argv)) { // do nothing if IO or pipe error } else if (CheckForBackgroundErrors(argv)) { // do nothing if background processing format error } else if (strcmp(argv[0], "exit") == 0) { BigFree(argv); printf("Exiting Shell...\n"); OnExitProcessQueueWait(); exit(0); } else if (strcmp(argv[0], "cd") == 0) { if (ArraySize(argv) <= 2) { if (ArraySize(argv) == 2) ChangeDirectory(argv[1]); else ChangeDirectory(getenv("HOME")); } else { printf("Too many arguments...\n"); } } else if (strcmp(argv[0], "limits") == 0) { if (ArraySize(argv) > 1) { argv = ArrayRemoveElement(argv, 0); Limits(argv); } } else if (strcmp(argv[0], "etime") == 0) { if (ArraySize(argv) > 1) { argv = ArrayRemoveElement(argv, 0); ETime(argv); } } else if (IsExecutable(argv[0])) { int background = VecContainsStr(argv, "&"); int I_loc = VecContainsStr(argv, "<"); int O_loc = VecContainsStr(argv, ">"); int pipe_count = ArgvCountSymbol(argv, "|"); if (I_loc != -1) { argv = ExecuteExternalWithInput(argv, I_loc, background); } else if (O_loc != -1) { argv = ExecuteExternalWithOutput(argv, O_loc, background); } else if (pipe_count > 0) { argv = ExecuteExternalWithPipe(argv, pipe_count, background); } else { char* cmd = ArgvToString(argv); if (background != -1) { argv = ArrayRemoveElement(argv, background); } ExecuteExternal(argv, background, cmd); free(cmd); } } BigFree(argv); } }