Пример #1
0
std::vector<PetModel*> getKnownApps() {
    std::vector<PetModel*> apps;
    FILE* appFile = _wfopen((Constants::polarisExecutablesFolderPath() 
        + L"\\knownapps.ini").c_str(), L"r");

    PetModel *nextModel = NULL;
    std::vector<std::wstring> nextPair = getKeyVal(appFile);
    while (nextPair[0].length() > 0) {
        std::wstring key = nextPair[0];
        std::wstring val = nextPair[1];
        if (key == L"nickname") {
            if (nextModel != NULL) {
                apps.push_back(nextModel);
            }
            nextModel = new PetModel();
            nextModel->petname = val;
        } else if (key == L"path") {
            nextModel->executables.push_back(val);
        } else if (key == L"readlinks") {
            nextModel->readOnlyWithLinks = val == L"true";
        } else if (key == L"suffix") {
            nextModel->fileExtensions.push_back(val);
        } else if (key == L"isdefaultbrowserpet") {
            nextModel->isDefaultBrowserPet = val == L"true";
		} else if (key == L"commandargs") {
			nextModel->commandLineArgs = val;
		}
        nextPair = getKeyVal(appFile);
        
    }
    apps.push_back(nextModel);
    return apps;
}
Пример #2
0
int loadKeyMap(char* appname, char* keymapfile)
{
	int length;
	char buffer[MAX_STR_LEN];
	char* nextKey;
	char iniBuff[MAX_INI];
	int retHandle;
	KEYVALNODE *binKey;
	LOGKEYNODE *headKey = NULL;
	KEYDATA *keyData;

	if (nextEntry == -1) return 0;   //max'ed out the keymap list - error out

	buffer[0]='\0';

	length = GetPrivateProfileString(appname, (char *)NULL, (char*)NULL, buffer, MAX_STR_LEN, keymapfile);
	if (length==0) return 0;

	//for each logical key get the value - which is the actual key for the TAP
	nextKey = buffer;  // final dir is marked with double null
	while(strlen(nextKey)>0)
	{

		//keyData = (KEYDATA *) malloc(sizeof(KEYDATA));
		//keyData->_keyList = NULL;
		//keyData->_len = 0;
		//keyData->_next = 0;
		//strcpy(keyData->_logKeyName, nextKey);

		//addKeyNode(&headKey, *keyData);
		addKeyNode(&headKey, nextKey);

		if (GetPrivateProfileString(appname, nextKey, "", iniBuff, MAX_INI, keymapfile) > 0)
		{
			binKey = (KEYVALNODE *) malloc(sizeof(KEYVALNODE));
			binKey->_next = NULL;
			//look for iniBUFF in our KEYNAMES and save the equivalent KEYVALUE
			binKey->_keyVal = getKeyVal(iniBuff);
			//check for commas in next version
			//attach to key structure
			headKey->_keydata._len = 1;
			headKey->_keydata._next = 1;
			headKey->_keydata._keyList = binKey;
		}
		nextKey += strlen(nextKey) + 1;
	}
	keyMap[nextEntry] = headKey;
	retHandle = nextEntry;

	//Max keymaps = 512 - in future, re-use released keymaps
	if (++nextEntry > MAX_KEYMAPS - 1) nextEntry = -1;

	return retHandle+1;  //ie to cater for 0'th index not being an error
}
int main_loop(){
	int retval = getKeyVal();
	return retval;
}
//______________________________________________________________________________
bool scopeConfigReader::readConfig( scopeConfigReader & obj, const std::string fn, aKeyValMemFn f1, aKeyValMemFn f2, aKeyValMemFn f3 )
{
    debugMessage( "\n", "**** Attempting to Read ", fn, " ****" );

    std::string line, trimmedLine;
    bool success = false;

    std::ifstream inputFile;

    inputFile.open( fn.c_str(), std::ios::in );
    if( inputFile )
    {
        bool readingData       = false;
        bool readingObjs       = false;
        bool readingCommandPVs = false;
        bool readingMonitorPVs = false;

        debugMessage( "File Opened from ", fn );
        while( std::getline( inputFile, line ) ) /// Go through, reading file line by line
        {
            trimmedLine = trimAllWhiteSpace( trimToDelimiter( line, UTL::END_OF_LINE ) );
            if( trimmedLine.size() > 0 )
            {
                if( stringIsSubString( line, UTL::END_OF_DATA ) )
                {
                    debugMessage( "Found END_OF_DATA" );
                    readingData = false;
                    readingObjs = false;
                    readingCommandPVs  = false;
                    readingMonitorPVs  = false;
                    break;
                }
                if( readingData )
                {
                    if( stringIsSubString( trimmedLine, UTL::VERSION ) )
                        getVersion( trimmedLine );
                    else if( stringIsSubString( trimmedLine, UTL::NUMBER_OF_OBJECTS ) )
                        getNumObjs( trimmedLine );
                    else if( stringIsSubString( trimmedLine, UTL::NUMBER_OF_ILOCKS ) )
                        getNumIlocks( trimmedLine );
                    else
                    {
                        switch( configVersion )
                        {
                            case 1:
                                if( trimmedLine.find_first_of( UTL::EQUALS_SIGN ) != std::string::npos )
                                {
                                    std::vector<std::string> keyVal = getKeyVal( trimmedLine );

                                    if( readingObjs )
                                    {
                                        CALL_MEMBER_FN(obj, f1)( keyVal ) ;
                                    }

                                    else if ( readingCommandPVs  )
                                    {
                                        CALL_MEMBER_FN(obj, f2)( keyVal ) ;
                                    }

                                    else if ( readingMonitorPVs )
                                    {
                                        CALL_MEMBER_FN(obj, f3)( keyVal ) ;
                                    }
                                }
                                break;
                            default:
                                message( "!!!!!WARNING DID NOT FIND CONFIG FILE VERSION NUMBER!!!!!!"  );
                        }
                    }
                }
                if( stringIsSubString( line, UTL::START_OF_DATA ) )
                {
                    readingData = true;
                    debugMessage( "Found START_OF_DATA" );
                }
                if( stringIsSubString( line, UTL::PV_COMMANDS_START ) )
                {
                    readingCommandPVs  = true;
                    readingObjs = false;
                    readingMonitorPVs = false;
                    debugMessage( "Found PV_COMMANDS_START" );
                }
                if( stringIsSubString( line, UTL::PV_MONITORS_START ) )
                {
                    readingCommandPVs = false;
                    readingObjs       = false;
                    readingMonitorPVs = true;
                    debugMessage( "Found PV_MONITORS_START" );
                }
                if( stringIsSubString( line, UTL::OBJECTS_START ) )
                {
                    readingObjs        = true;
                    readingCommandPVs  = false;
                    readingMonitorPVs  = false;
                    debugMessage( "Found OBJECTS_START" );
                }
            }
        }
        inputFile.close( );
        debugMessage( "File Closed" );
        success = true;
    }
    else{
        message( "!!!! Error Can't Open Shutter Config File after searching for:  ", fn, " !!!!"  );
    }
    return success;
}
Пример #5
0
int main(int nargs, char *args[])
{
  char *line, *words[MAX_WORDS];
  char string[1024],combined[1024],line_s[MAX_LINE];
  char c, *dir=NULL,*cwd, pwd[PATH_MAX];
  char username[128],password[128],viFile[256],viTemp[256];
  char comname[128],authkey[80],authkeyword[64],key[64];
  int  gotauthkey=0,gotusername=0,gotvi=0,gotnew=0;
  int  res, i, n, status, nwords=0;
  FILE *auth_fp = (FILE *)0;
  FILE *key_fp = (FILE *)0;
  pid_t wpid, pid;
  char mode[] = "0755";
  struct stat stbuf;

  // get server login user and password
  while ((c = getopt(nargs, args, "dhu:l:")) != -1)
    switch (c) {

    case 'h':             // help
      printf("Usage: %s {-d} {-h} {-u <username>}\n\n",args[0]);
      printf("-d : don't delete /tmp files\n");
      printf("-h : help\n");
      printf("-u : commands.com username\n");
      exit(0);

    case 'u':             // username
      strcpy(username,optarg);
      gotusername = 1;
      strcpy(password,getpass("Password: "******"Usage: %s {-d} {-h} {-u <username>}\n\n",args[0]);
      printf("-d : don't delete /tmp files\n");
      printf("-h : help\n");
      printf("-u : commands.com username\n");
      exit(0);
      break;
    }

  // if we have both the username and password, login and
  // get the authkey

  // get the authorization key for the username
  if (gotusername) {
    if ((res = getAuthKey(username, password, authkey, key)) != 0) {
      printf("Unable to get authkey from server (%d)\n",res);
      exit(1);
    }
  } else {
    getKeyVal(key);
  }

  // create the authkey.json file only if we have -u
  sprintf (comname,"/tmp/.%s.commands.com",getenv("USER"));
  if (gotusername) {
    sscanf (authkey,"{\"%[^\"]\":\"%[^\"]",authkeyword,authKeyVal);
    if (!strcmp(authkeyword,"error")) {
      printf("Invalid Login.. Exiting.\n");
      exit(1);
      // do not save error as authkey
    } else {
      auth_fp = fopen(comname,"w");
      if (auth_fp == NULL) {
        printf("unable to create %s\n",comname);
        exit(1);
      }
      fputs(authkey,auth_fp);
      fputs("\n",auth_fp);
      fclose(auth_fp);
    }
  } else {
    auth_fp = fopen(comname, "r");
    if (auth_fp != NULL) {
      if(fgets(authkey, 64, auth_fp) == NULL) {
        printf("Unable to read authkey from %s\n",comname);
        exit(1);
      }
      // remove trailing \n
      n = strlen(authkey);
      if (authkey[n-1] == '\n') authkey[n-1] = '\0';
      gotauthkey = 1;
    }
  }

  // create the key.json file
  key_fp = fopen(fName(KEY_NAME),"w");
  if (key_fp == NULL) {
    printf("unable to create %s\n",fName(KEY_NAME));
    exit(1);
  }
  fputs(key,key_fp);
  fputs("\n",key_fp);
  fclose(key_fp);

  // save off the key url to display to the user when we are done
  sscanf (key,"{\"key\":\"%[^\"]",keyVal);
  sprintf (displayUrl,"%s/%s",gotoKeyUrl,keyVal);

  // save off the authkey value
  if (gotusername || gotauthkey) {
    sscanf (authkey,"{\"%[^\"]\":\"%[^\"]",authkeyword,authKeyVal);
    if (!strcmp(authkeyword,"error")) {
      printf("\nError: %s\n\n",authKeyVal);
      exit(1);
    } else {
      printf("\nSuccessfully logged in...");
      if (gotusername) {
        printf("\nAuthKey saved to %s.  Delete file to return to Anonymous posting.\n",comname);
      } else {
        printf("\nAuthKey retrieved from %s.  Delete file to return to Anonymous posting.\n",comname);
      }
    }
  } else {
    strcpy(authKeyVal,"");
  }


  // set up termination function
  atexit((void *)terminate);

  // catch abort signals
  signal(SIGINT,(void *)terminate2);	// trap ctl-c
  signal(SIGTERM,(void *)terminate2);	// trap kill -15

  // create the post.txt file
  post_fp = fopen(fName(POST_NAME),"w");
  if (post_fp == NULL) {
    printf("unable to create post file %s\n",fName(POST_NAME));
    exit(1);
  }

  // record all input from the user
  while(1)
    {
      if (!gotdebug)
        unlink(fName(SHELL_NAME));

	line = rl_gets();
        if (line == NULL) {		// trap ctl-d
          exit(1);
        } else {
	  strcpy(line_s, line);
        }
      line = rl_gets();
      strcpy(line_s, line);
      if (line == NULL) {		// trap ctl-d
        exit(1);
      }

      // break the line up into words
      tokenize(line, words, &nwords);

      // just a blank line?
      if (words[0] == NULL) {
        continue;
      }

      // are we done ?
      if (!strcasecmp(words[0], "exit")) {
        exit(1);
      }

      fputs("monitor$ ",post_fp);
      fflush(post_fp);
      fputs(line_s,post_fp);
      fflush(post_fp);
      fputs("\n",post_fp);
      fflush(post_fp);



      // toss out any commands that cannot be handled such
      // as those that use libcurses.so

      if (!strcasecmp(words[0], "top")) {
        printf("Unable to capture output from %s\n",words[0]);
        sprintf(string,"Unable to capture output from %s\n",words[0]);
        fputs(string,post_fp);
        fflush(post_fp);
        continue;
      }


      // builtin command
      if (!strcasecmp(words[0], "cd")) {
        if (nwords == 1)                     dir = getenv("HOME");
        if (nwords == 2 && *words[1] == '~') dir = getenv("HOME");
        if (nwords == 2 && *words[1] != '~') dir = words[1];
        if (chdir(dir) == -1) {
          perror("chdir");
          fputs(strerror(errno),post_fp);
          fflush(post_fp);
          fputs("\n",post_fp);
          fflush(post_fp);
          continue;
        }
        continue;
      }

      // builtin command
      if (!strcasecmp(words[0], "pwd")) {
        if(NULL == (cwd = getcwd(pwd, PATH_MAX))) {
          strcpy(pwd,"Unable to get current working directory\n");
        }
        printf("%s\n",pwd);
        fputs(pwd,post_fp);
        fflush(post_fp);
        fputs("\n",post_fp);
        fflush(post_fp);
        continue;
      }

      // builtin command
      if (!strcasecmp(words[0], "export")) {
        if (nwords > 1) {
          putenv(words[1]);
          continue;
        }
      }

      // look for "ls" by itself and add -C to make it tabbed format
      // because when piped through tee, it thinks it is not connected
      // to a terminal.
      if (!strcasecmp(words[0], "ls")) {
        if (nwords == 1) {
          words[1] = "-C";
          nwords = 2;
        }
      }

      // look for "man" and add | col -b
      if (!strcasecmp(words[0], "man")) {
        if (nwords == 2) {
          words[2] = "|";
          words[3] = "col";
          words[4] = "-b";
          nwords = 5;
        }
        // for when there is a "man 3 foo"
        if (nwords == 3) {
          words[3] = "|";
          words[4] = "col";
          words[5] = "-b";
          nwords = 6;
        }
      }

      // process special "vi" command
      if (!strcasecmp(words[0], "vi")) {
        if (nwords == 1) {
          printf("Please specify the new file you wish to create.\n");
          printf("It is required to correctly log the new file.\n");
          continue;
        }
        gotvi = 1;
        if (stat(words[1],&stbuf) != 0) {
          // new file
          gotnew=1;
          strcpy(viFile,words[1]);
        } else {
          // make copy of existing file
          strcpy(viFile,words[1]);
          strcpy(viTemp,fName(TEMP_NAME));
          my_cp(viFile,viTemp);
          gotnew=0;
        }
      } else {
        gotvi = 0;
      }

      // close the post.txt before forking
      fclose(post_fp);

      // OK, lets process the external command using fork/execvp
      if ((pid = fork ()) < 0) {
        perror ("fork");
        exit(0);
      }

      // this will split output between the terminal and post.txt
      //  <command> 2>&1 | tee -ai <file>
      if (pid == 0) {		// if child then exec the command
        if (!gotvi) {
          // create the bash script
          tmp_fp = fopen(fName(SHELL_NAME),"w");
          if (tmp_fp == NULL) {
            printf("unable to create %s file\n",fName(SHELL_NAME));
            exit(1);
          }
          // set file permission to 755
          i = strtol(mode, 0, 8);
          chmod (fName(SHELL_NAME),i);
          memset(combined,0,sizeof(combined));
          for(i=0;i<nwords;i++) {
            strcat(combined,words[i]);
            strcat(combined," ");
          }

          sprintf(string,"#!/bin/bash -l\n%s 2>&1 | tee -ai %s\n",combined,fName(POST_NAME));
          fputs(string,tmp_fp);
          fclose(tmp_fp);

          execlp ("/bin/bash","bash","-c",fName(SHELL_NAME),(char *)0);
          perror ("execlp");
          exit(0);
        } else {

          execlp ("vi","vi",viFile,(char *)0);
          perror ("execlp");
          exit(0);
        }
      }

      if (pid > 0)            // parent waits for child process to terminate
        {
          do {
            wpid = waitpid(pid, &status, WUNTRACED);
            if (wpid == -1) {
              perror("waitpid");
              return(0);
            }


            if (WIFEXITED(status)) {
              //printf("child exited, status=%d\n", WEXITSTATUS(status));


            } else if (WIFSIGNALED(status)) {
              printf("process killed (signal %d)\n", WTERMSIG(status));


            } else if (WIFSTOPPED(status)) {
              printf("process stopped (signal %d)\n", WSTOPSIG(status));


            } else {    // Non-standard case -- may never happen
              printf("Unexpected status (0x%x)\n", status);
            }
          } while (!WIFEXITED(status) && !WIFSIGNALED(status));

          // existing file
          if (gotvi && !gotnew) {
            my_diff(viTemp,viFile,fName(POST_NAME));
            unlink(viTemp);
          }

          // new file
          if (gotvi && gotnew) {
            my_append(viFile,fName(POST_NAME));
          }

          // re-open the post.txt file
          post_fp = fopen(fName(POST_NAME),"a");
          if (post_fp == NULL) {
            printf("unable to re-open post file %s\n",fName(POST_NAME));
            exit(2);
          }

        }



    }

  exit(0);
}