bool KPrinterImpl::printFiles(KPrinter *p, const TQStringList& f, bool flag) { TQString cmd; if (p->option("kde-isspecial") == "1") { if (p->option("kde-special-command").isEmpty() && p->outputToFile()) { KURL url( p->outputFileName() ); if ( !url.isLocalFile() ) { cmd = ( flag ? "mv" : "cp" ) + ( " %in $out{" + p->outputFileName() + "}" ); } else { if (f.count() > 1) { p->setErrorMessage(i18n("Cannot copy multiple files into one file.")); return false; } else { TDEProcess proc; proc << (flag?"mv":"cp") << f[0] << p->outputFileName(); if (!proc.start(TDEProcess::Block) || !proc.normalExit() || proc.exitStatus() != 0) { p->setErrorMessage(i18n("Cannot save print file to %1. Check that you have write access to it.").arg(p->outputFileName())); return false; } } return true; } } else if (!setupSpecialCommand(cmd,p,f)) return false; } else if (!setupCommand(cmd,p)) return false; return startPrinting(cmd,p,f,flag); }
int vrpn_ImmersionBox::reset(void) { //----------------------------------------------------------------------- // Set the values back to zero for all buttons, analogs and encoders clear_values(); //----------------------------------------------------------------------- // sending an end at this time will force the ibox into the reset mode, if it // was not already. if the ibox is in the power up mode, nothing will happen because // it 'should' be waiting to sync up the baudrate // try to synchronize for 2 seconds if (syncBaudrate (10.0)) { printf("vrpn_ImmersionBox found\n"); } else { return -1; } if (0 == sendIboxCommand((char) CMD_GETNAME, (char *) &iname, .1)) { fprintf(stderr,"problems with ibox command CMD_GETNAME\n"); return -1; } if (0 == sendIboxCommand((char) CMD_GETPRID, (char *) &id, .1)) { fprintf(stderr,"problems with ibox command CMD_GETPRID\n"); return -1; } if (0 == sendIboxCommand((char) CMD_GETMODL, (char *) &model, .1)){ fprintf(stderr,"problems with ibox command CMD_GETMODL\n"); return -1; } if (0 == sendIboxCommand((char) CMD_GETSERN, (char *) &serial, .1)){ fprintf(stderr,"problems with ibox command CMD_GETSERN\n"); return -1; } if (0 == sendIboxCommand((char) CMD_GETCOMM, (char *) &comment, .1)){ fprintf(stderr,"problems with ibox command CMD_GETCOMM\n"); return -1; } if (0 == sendIboxCommand((char) CMD_GETPERF, (char *) &parmf, .1)){ fprintf(stderr,"problems with ibox command CMD_GETPERF\n"); return -1; } if (0 == sendIboxCommand((char) CMD_GETVERS, (char *) &vers, .1)){ fprintf(stderr,"problems with ibox command CMD_GETVERS\n"); return -1; } #ifdef VERBOSE printf("%s\n%s\n%s\n%s\n%s\n%s\n", iname, id, serial, comment, parmf, vers); #endif //----------------------------------------------------------------------- // Compute the proper string to initialize the device based on how many // of each type of input/output that is selected. setupCommand (0, _numchannels, _numencoders); unsigned char command[26] = ""; command[0] = 0xcf; command[1] = 0x0; command[2] = (unsigned char) 10; // milliseconds between reports command[3] = commandByte; for (int i = 4; i < 25;i++) command[i] = 0x0; //----------------------------------------------------------------------- int result = vrpn_write_characters(serial_fd, (const unsigned char *) command, 25); // Ask the box to send a report, ensure echo received if (result < 25) { fprintf(stderr,"vrpnImmersionBox::reset: could not write command\n"); return -1; } pause (.1); // look for command echo result = vrpn_read_available_characters(serial_fd, (unsigned char *) command, 1); if (result <= 0 || command[0] != 0xcf) { fprintf(stderr,"vrpnImmersionBox::reset: no command echo\n"); return -1; } // flush the input buffer vrpn_flush_input_buffer(serial_fd); printf("ImmersionBox reset complete.\n"); status = STATUS_SYNCING; vrpn_gettimeofday(×tamp, NULL); // Set watchdog now return 0; }
int main(void) { system("clear"); char in_buffer[MAX_LINE]; /* to store the input commands*/ int background; /*is set if '&' is given at end of command */ char *args[MAX_LINE/+1];/* to have 40 arguments */ char checkCommand[10]; /* To track the input like !! and !N*/ pid_t pid; int i; int shouldrun = 1; while (shouldrun) //while runs always, unless user enter exit { background = 0; printf(" COMMAND->\n"); setupCommand(in_buffer,args,&background); /* To get next command */ if(strcmp(args[0],"exit")==0) //if user enters exit, it exits out { printf("exiting\n"); shouldrun=0; exit(0); } int k,j,status_flag; k = 0; j=0; int histCounter =0; /* This is loop counter to traverse through history array*/ int lengthArgs = strlen(args[0]); status_flag=0; /* This is to track whether command entered will be executed or not by execvp*/ int no_commands=0; int hist_flag = 0; /* This flag is to test whether !! or !N has been given as input or not*/ int maxIndex = count; int minIndex =0; if(count>9) minIndex = count - 9; char* histHold[20]; strcpy(checkCommand,args[0]); int position_value = 0; /* This will hold what position will fetched from history */ if(lengthArgs<=2) position_value = (int)checkCommand[1] - '0'; /* Convert the character digit to numeric value*/ /** *This else loop will work in this way: if !10 is given then position to be fetched from is 10 * It will extract the value given after '!' and will store in variable position_value */ else { int power = lengthArgs -2; int value = 0; int loop=0; int hold; /** * This for loop takes value N from !N * It then parse each digit from N and forms the numeric value * If input is given as !10 then it will take out 12 * It will parse each digit (1 and 0) * It will form the numeric value as 1*(10 power of 1) + 2 * (10 power of 0) */ for(loop=1;loop<lengthArgs;loop++) { hold = (int)checkCommand[loop] -'0'; value = value + hold * pow(10,power); power--; } position_value = value; } /* This block will fetch the command from history only when the input pattern will be like !! or !N*/ if(checkCommand[0] == '!' ) { if((checkCommand[1] == '!' && count>0) || (position_value >= minIndex && position_value <= maxIndex)) { int position = 0; /* This is initialized to 0 to get the latest command from history*/ /*Check for the pattern !N*/ if(checkCommand[1]!='!') { position = maxIndex - position_value; } memset(&hist_array[0], 0, sizeof(hist_array)); /* Array initialization*/ /** * This while loop will take each character (except null and enter) from history array * It will put the character in a temporary 1D array */ while (history[position][j] != '\n' && history[position][j] != '\0') { hist_array[k] = history[position][j]; j++; k++; } /** *This is to make an entry in the history array * with the command corresponding to the position !! * or !N */ for (histCounter = 9;histCounter>0; histCounter--) strcpy(history[histCounter], history[histCounter-1]); strcpy(history[0],hist_array); count++; printf("Command fetched : %s\n",hist_array); /** * This block will get the command from the history for the given position. * Commands are spliited by space if it contains more than one word * It will store the command in a pointer of character array * The pointer character array is passed to execvp to execute */ int hist_array_length = strlen(hist_array); /* Get the length of the command fetched from history*/ int arrayCounter = 0; char holdCharacter; /* This will hold the each character of the command while parsing the command*/ int histHoldCounter = 0; int blankPossible = 0; /* This is handle multiple blanks between words in a command*/ histHold[histHoldCounter] = (char*) calloc(1, sizeof(char)); /* Dynamic Initialization*/ /** * This for loop will parse the temporary 1D array * characters are concatenated and put in the current index of array of character pointers * If the character is space then next index of pointer array is initialized */ for(arrayCounter =0 ;arrayCounter<hist_array_length;arrayCounter++) { holdCharacter = hist_array[arrayCounter]; if(holdCharacter != ' ') { histHold[histHoldCounter] = strcat(histHold[histHoldCounter],&holdCharacter); blankPossible = 1; } if(holdCharacter == ' ') { /*handle multiple blank*/ if(blankPossible == 1 ) { histHoldCounter++; histHold[histHoldCounter] = (char*) calloc(1, sizeof(char)); blankPossible = 0; } } } /* Set the immediate next index as zero after populating it with the parsed command words*/ histHold[histHoldCounter + 1 ] = NULL; status_flag = 1; hist_flag = 1; /* Block ends here */ } else { if(count == 0) printf("No commands in history\n"); else { printf("No such command in histoy\n"); no_commands = 1; //flag used to stop creating processes if there are no such commands to execute } status_flag = 1; //flag used to stop executing execvp if there are no commands to execute } } /* This block is to print the history of commands when user give input command as 'history'*/ if(strcmp("history",args[0])==0) { if(count>0) { printHistory(); status_flag = 1; }else { printf("No commands in history\n"); status_flag = 1; } } if((strcmp("history",args[0])!=0) && (no_commands == 0)) //conditions to check when to create processes pid = fork(); int pid_child = 0; if (pid < 0) { printf("Fork failed.\n"); exit (1); } /*the child process will invoke execvp()*/ else if (pid == 0) { pid_child = getpid(); //getting the pid of child process if(hist_flag == 1) { status_flag = 1; execvp (histHold[0], histHold); //executing of command fetched from history } if(status_flag == 0) { if (execvp (args[0], args) == -1) //checking whether command has executed properly or not. If it cannot execute, displaying error executing command { printf("Error executing command\n"); kill(pid_child,SIGKILL);//if command gives an error, the child process has to be killed explicitly. If not, program won't exit when exit command is given. } } } /** *if background == 0, the parent will wait, * else returns to the setupCommand() function. */ else { if (background == 0) waitpid(pid,NULL,0); } } }