BOOL GetParams(Params *p, int argc, TCHAR* argv[]) { int i; BOOL hive = FALSE, cert = FALSE; if (argc < 3) return FALSE; p->crl = NULL; p->store = TEXT("ROOT"); for (i = 1; i < argc; ++i) { if (!IS_CMD(argv[i])) { if (!hive) { p->hive = argv[i]; hive = TRUE; } else if (!cert) { p->cert = argv[i]; cert = TRUE; } else { return FALSE; } // too many params } else if (CHK_CMD(argv[i], TEXT("CRL")) && i < argc-1) { p->crl = argv[++i]; } else if (CHK_CMD(argv[i], TEXT("Store")) && i < argc-1) { p->store = argv[++i]; } else { return FALSE; } } return hive && cert; }
int main(int argc, char** argv) { if(argc < 2) { print_help(); return 1; } const char* const command = argv[1]; const char** const cargv = const_quirk(argv); if(IS_CMD("decode-all", 1)) { return cmd_get_all_and_decode(argv + 2, argc - 2); } else if(IS_CMD("decode", 2)) { return cmd_decode(argv[2], cargv + 3, argc - 3); } else if(IS_CMD("append-all", 2)) { return cmd_append_all(argv[2], argv[3]); } else if(IS_CMD("recode", 3)) { return cmd_recode(argv[2], argv[3], cargv + 4, argc - 4); } else if(IS_CMD("info", 1)) { return cmd_info(argv[2]); } else if(IS_CMD("dump", 1)) { return cmd_dump(argv[2]); } else { print_help(); return 1; } return 0; }
// Admin commands bool ExecuteCommandString_Callback(CCmds* cmds, const wstring &wscCmd) { returncode = DEFAULT_RETURNCODE; if (IS_CMD("showmarket")) { returncode = SKIPPLUGINS_NOFUNCTIONCALL; //AdminCmd_GenerateID(cmds, cmds->ArgStrToEnd(1)); return true; } return false; }
static void * handle_requests (struct daemon *daemon) { /* The message typed by the user */ char *message = NULL; struct daemon_request *r; struct pool *pool; void* (*handler) (void *); for (;;) { message = socket_getline (daemon->socket); if (!message) break; /* Only request we're allowed to treat no matter how many requests are * currently being treated */ if (strncmp (message, "quit", 4) == 0) break; sem_wait (&daemon->req_lock); if (daemon->nb_requests == prefs->max_requests_per_daemon) { sem_post (&daemon->req_lock); /* FIXME: this is copy-paste from client, should be different */ daemon_send (daemon, " < Too many requests, mister, plz calm down\n"); continue; } sem_post (&daemon->req_lock); /* Treating all the common requests */ /* FIXME : use the IS_CMD macro */ pool = fast_pool; #if 0 if (strncmp (message, "list", 4) == 0) { pool = slow_pool; handler = daemon_request_list; } else if (strncmp (message, "get", 3) == 0) handler = daemon_request_get; else if (strncmp (message, "file", 4) == 0) handler = daemon_request_file; else if (strncmp (message, "neighbourhood", 13) == 0) handler = daemon_request_neighbourhood; else if (strncmp (message, "neighbour", 9) == 0) handler = daemon_request_neighbour; else if (strncmp (message, "ready", 5) == 0) handler = daemon_request_ready; #endif if (IS_CMD (message, "list")) { pool = slow_pool; handler = &daemon_request_list; } else if (IS_CMD (message, "get")) handler = &daemon_request_get; else if (IS_CMD (message, "file")) handler = &daemon_request_file; else if (IS_CMD (message, "neighbourhood")) handler = &daemon_request_neighbourhood; else if (IS_CMD (message, "neighbour")) handler = &daemon_request_neighbour; else if (IS_CMD (message, "ready")) handler = &daemon_request_ready; else handler = daemon_request_unknown; r = daemon_request_new (message, daemon, pool, handler); if (!r) { daemon_send (daemon, " < Failed to create a new request\n"); continue; } sem_wait (&daemon->req_lock); daemon->requests = daemon_request_add (daemon->requests, r); if (!daemon->requests) { daemon_request_free (r); break; } sem_post (&daemon->req_lock); pool_queue (r->pool, daemon_request_handler, r); } if (message) free (message); return NULL; }
void user_interface (pid_t pid) { /* Number of chars written in cmd */ int ptr = 0; char buffer[BUFFSIZE]; int n_received; printf ("Launching user interface\n"); father_pid = pid; if (signal (SIGINT, quit) == SIG_ERR) { perror ("signal"); quit (); } /* BUFFSIZE + 1, so the terminating null byte ('\0') can be stored */ if ((cmd = calloc (BUFFSIZE + 1, sizeof (char))) == NULL) { fprintf (stderr, "Allocating memory for user command"); quit (); } /* Displays a prompt-like thing */ printf("\n$> "); if (fflush(stdout) == EOF) { perror ("fflush"); quit (); } while ((n_received = read (STDIN_FILENO, buffer, BUFFSIZE)) != 0) { if (n_received < 0) { perror ("read"); quit (); } /* * Seems necessary, unless we can guarantee that we will only receive * well-formed strings... */ buffer[n_received] = '\0'; strcpy (cmd + ptr, buffer); cmd = realloc (cmd, ptr + 2*BUFFSIZE + 1); ptr += BUFFSIZE; cmd[ptr] = '\0'; /* * Upon receiving end of transmission, treating data */ if (strstr (buffer, "\n") != NULL) { if (IS_CMD(cmd, "set")) { callback = &do_set; } else if (IS_CMD (cmd, "help")) { callback = &do_help; } else if (IS_CMD (cmd, "list")) { callback = &do_list; } else if (IS_CMD (cmd, "get")) { callback = &do_get; } else if (IS_CMD (cmd, "info")) { callback = &do_info; } else if (IS_CMD (cmd, "download")) { callback = &do_download; } else if (IS_CMD (cmd, "upload")) { callback = &do_upload; } else if (IS_CMD (cmd, "connect")) { callback = &do_connect; } else if (IS_CMD (cmd, "raw")) { callback = &do_raw; } else if (IS_CMD (cmd, "quit")) { printf ("Goodbye\n"); break; // not quit () unless you free cmd } else { printf("Unknown command. Type help for the command list.\n"); callback = NULL; } string_remove_trailer (cmd); if (callback) { switch (fork ()) { case -1: // break; case 0: (*callback) (cmd); exit (EXIT_SUCCESS); break; default: break; } } if ((cmd = realloc (cmd, BUFFSIZE+1)) == NULL) { fprintf (stderr, "Allocating memory for user command"); quit (); } ptr = 0; printf ("\n$> "); if (fflush (stdout) == EOF) { perror ("fflush"); quit (); } } } quit (); }
// выполнение команды с параметрами void my_exec(char *cmd) { char *params[256]; //параметры команды разделенные пробелами char *token; int np; token = strtok(cmd, " "); np = 0; while( token && np < 255 ) { params[np++] = token; token = strtok(NULL, " "); } params[np] = NULL; // выполнение встроенных команд if( IS_CMD(pwd) ) SHCMD_EXEC(pwd); else if ( IS_CMD(rm) ) SHCMD_EXEC(rm); else if ( IS_CMD(mkdir) ) SHCMD_EXEC(mkdir); else if ( IS_CMD(rmdir) ) SHCMD_EXEC(rmdir); else if ( IS_CMD(free) ) SHCMD_EXEC(free); else if ( IS_CMD(tail) ) SHCMD_EXEC(tail); else if ( IS_CMD(cat) ) SHCMD_EXEC(cat); else if ( IS_CMD(ls) ) SHCMD_EXEC(ls); else if ( IS_CMD(nl) ) SHCMD_EXEC(nl); else if ( IS_CMD(link) ) SHCMD_EXEC(link); else if( IS_CMD(exit) ) SHCMD_EXEC(exit); else { // иначе вызов внешней команды execvp(params[0], params); perror("exec"); // если возникла ошибка при запуске } }
void do_get (char *cmd) { /* The message typed by the user might be longer than BUFFSIZE */ char *message; /* Number of chars written in message */ int ptr = 0; char buffer[BUFFSIZE]; int n_received; char filekey[KEYSIZE + 1]; // + 1 for '\0' long beginning; long end; int sock; struct sockaddr_in addr; if (sscanf (cmd, "get %s %ld %ld", filekey, &beginning, &end) == EOF) { perror ("sscanf"); printf ("Error scanning your request. See stderr for details.\n"); return; } // FIXME: for now, filekey is filename, must use a key instead // FIXME: for now, beginning and end are unused, must be implemented // FIXME: for now, server is statically located at 127.0.0.1:1338 addr.sin_family = AF_INET; addr.sin_port = htons (1338); addr.sin_addr.s_addr = inet_addr ("127.0.0.1"); sock = socket (AF_INET, SOCK_STREAM, 0); if (sock < 0) { perror ("socket"); printf ("Unable to open a new socket. Request cancelled.\n"); return; } if (connect (sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) { perror ("connect"); printf ("Unable to connect to 127.0.0.1:1338. Request cancelled.\n"); return; } socket_write (sock, "get "); socket_write (sock, filekey); socket_write (sock, "\n\0"); #if 1 if ((message = calloc (BUFFSIZE + 1, sizeof (char))) == NULL) { perror ("Allocating memory for message"); // TODO: warn someone exit (EXIT_FAILURE); } do { n_received = recv (sock, buffer, BUFFSIZE, 0); if (n_received < 0) { perror ("recv"); // TODO: warn someone exit (EXIT_FAILURE); } if (n_received != 0) { /* * Seems necessary, unless we can guarantee that we will only * receive well-formed strings... */ buffer[n_received] = '\0'; strcpy (message + ptr, buffer); if((message = realloc (message, ptr + 2*BUFFSIZE + 1)) == NULL) { fprintf (stderr, "Error reallocating memory for message\n"); // TODO: warn someone exit (EXIT_FAILURE); } ptr += n_received; message[ptr] = '\0'; } } while (strstr (buffer, "\n") == NULL); if (IS_CMD (message, "ready")) { // TODO: outsource this fragment of code int port; int data_sock; struct sockaddr_in data_addr; int file; char data[BUFFSIZE]; int nb_recv; int nb_written; int nb_written_sum; // Retrieve the data connection port string_remove_trailer(message); if (sscanf (message, "ready %*s %d", &port) == EOF) { perror ("sscanf"); printf ("Unable to read IP and port from peer response.\n"); return; } data_addr.sin_family = AF_INET; data_addr.sin_port = htons (port); data_addr.sin_addr.s_addr = addr.sin_addr.s_addr; data_sock = socket (AF_INET, SOCK_STREAM, 0); if (sock < 0) { perror ("socket"); printf ("Unable to open a new socket for data.\n"); return; } if (connect (data_sock, (struct sockaddr *)&data_addr, sizeof(data_addr)) < 0) { perror ("connect"); printf ("Unable to connect to %s:%d.\n", inet_ntoa (data_addr.sin_addr), port); return; } // FIXME: Now where are we downloading this file to? file = open ("download", O_WRONLY|O_CREAT|O_TRUNC, (mode_t)0644); if (file < 0) { perror ("open"); // FIXME: what file? printf ("Unable to open file ...\n"); return; } do { nb_recv = recv (data_sock, data, BUFFSIZE, 0); if (nb_recv < 0) { perror ("recv"); return; } if (nb_recv > 0) { nb_written_sum = 0; do { nb_written = write (file, data, nb_recv - nb_written_sum); if (nb_written < 0) { perror ("write"); return; } nb_written_sum += nb_written; } while (nb_written_sum < nb_recv); } } while (nb_recv > 0); // TODO: name this file! printf ("Successfully received file into local file 'download'.\n"); close (data_sock); close (file); } else if (IS_CMD (message, "error")) { printf ("%s", message); // TODO } else { printf ("Received unknown response from client: %s", message); } free (message); free (cmd); #endif }
void processCommand(const char *cmd) { int tmpx, tmpy, btn; float tmpInterval; UInt32 tmpkc; char str[CMD_STRING_MAXLEN]; bzero(str, CMD_STRING_MAXLEN); if (IS_CMD(cmd, "mouselocation")) { CGPoint cgLoc = mouseLoc(); printf("%.f %.f\n", cgLoc.x, cgLoc.y); } else if (IS_CMD(cmd, "mousewarp ")) { print_msg("Warping mouse to location."); sscanf(cmd, "mousewarp %d %d", &tmpx, &tmpy); mouseMove(tmpx, tmpy); } else if (IS_CMD(cmd, "mousemove ")) { print_msg("Moving mouse."); sscanf(cmd, "mousemove %d %d", &tmpx, &tmpy); mouseMoveTo(tmpx, tmpy, 0.7); } else if (IS_CMD(cmd, "mousedown")) { print_msg("Pressing mouse button."); sscanf(cmd, "mousedown %d", &btn); mousePress(btn, SINGLE_CLICK); } else if (IS_CMD(cmd, "mouseup")) { print_msg("Releasing mouse button."); sscanf(cmd, "mouseup %d", &btn); mouseRelease(btn, SINGLE_CLICK); } else if (IS_CMD(cmd, "mouseclick")) { print_msg("Clicking mouse."); sscanf(cmd, "mouseclick %d", &btn); mouseClick(btn, SINGLE_CLICK); } else if (IS_CMD(cmd, "mousedoubleclick")) { print_msg("Double-clicking mouse."); sscanf(cmd, "mousedoubleclick %d", &btn); mouseClick(btn, DOUBLE_CLICK); } else if (IS_CMD(cmd, "mousetripleclick")) { print_msg("Triple-clicking mouse."); sscanf(cmd, "mousetripleclick %d", &btn); mouseClick(btn, TRIPLE_CLICK); } else if (IS_CMD(cmd, "mousedrag ")) { print_msg("Dragging mouse."); sscanf(cmd, "mousedrag %d %d", &tmpx, &tmpy); mouseDrag(LEFT_MOUSE, tmpx, tmpy); } else if (IS_CMD(cmd, "press ")) { print_msg("Pressing key."); sscanf(cmd, "press %x", &tmpkc); keyPress((CGKeyCode)tmpkc, NULL); } else if (IS_CMD(cmd, "release ")) { print_msg("Releasing key."); sscanf(cmd, "release %x", &tmpkc); keyRelease((CGKeyCode)tmpkc, NULL); } else if (IS_CMD(cmd, "hit ")) { print_msg("Hitting key."); sscanf(cmd, "hit %x", &tmpkc); keyHit((CGKeyCode)tmpkc, NULL); } else if (IS_CMD(cmd, "type ")) { print_msg("Typing."); strncpy(str, &cmd[5], CMD_STRING_MAXLEN); typeString(str); } else if (IS_CMD(cmd, "wait")) { print_msg("Waiting."); sscanf(cmd, "wait %f", &tmpInterval); usleep(1000000 * tmpInterval); } else { print_msg("I don't know what you want to do."); } }