Exemplo n.º 1
0
Alert get_alert(XmlElement xml_alert)
{
   Alert alert = malloc(sizeof(struct Alert));
   Queue alert_info = xml_element_children(xml_alert);

   while (!queue_empty(alert_info))
   {
      XmlElement el = queue_dequeue(alert_info);
      const char *element = xml_element_name(el);

      if (str_equals(element, "summary")) {
         alert->summary = strdup(xml_element_content(el));
      } else if (str_equals(element, "description")) {
         alert->description = strdup(xml_element_content(el));
      } else if (str_equals(element, "location")) {
         alert->location = strdup(xml_element_content(el));
      }

      free_xml_element(el);
   }

   destroy_queue(alert_info);

   return alert;
}// End of get_alert method
Exemplo n.º 2
0
/*
 * Retrieves a color (black or white) from user input.
 */
int stringToColor(char* str){
	if (str_equals(str, "black")){
		return BLACK;
	}
	if (str_equals(str, "white")){
		return WHITE;
	}
	return -1;
}
Exemplo n.º 3
0
/* Return a list of category names corresponding to a given list of
   category names and or numbers.  Doesn't allocate new names,
   just pointers to Strings in the CategoryMap object or the
   provided List */
List *cm_get_category_str_list(CategoryMap *cm, List *names, int ignore_missing) {
  int i, cat;
  List *retval = lst_new_ptr(lst_size(names));
  for (i = 0; i < lst_size(names); i++) {
    String *n = lst_get_ptr(names, i);
    if (str_as_int(n, &cat) == 0) {
      if (cm == NULL)
	die("ERROR: if categories are specified by number, a category map is required\n");
      if (cat < 0 || (cm != NULL && cat > cm->ncats)) 
        die("ERROR: category number %d is out of bounds.\n", cat);
      lst_push_ptr(retval, cm_get_feature(cm, cat));
    }
    else {
      if (cm != NULL) {
	cat = cm_get_category(cm, n);
	if (cat == 0 && !ignore_missing && !str_equals(n, cm_get_feature(cm, 0))) {
	  die("ERROR: illegal category name (\"%s\")\n", n->chars);
	}
	//return pointers to cm if possible
	lst_push_ptr(retval, cm_get_feature(cm, cat));
      }
      //otherwise return pointers to strings in list
      else lst_push_ptr(retval, n);
    }
  }
  return retval;
}
Exemplo n.º 4
0
PossibleMove* readMove(char* command, int* exitcode){
	char fromTile[6];
	char toTile[6];
	char promoteToAsString[10];
	char promoteTo = 0; //initializing promoteTo to avoid problems with promotion-less move commands
	strncpy(promoteToAsString, "undefined", 10);
	
	*exitcode = 0;
	if (sscanf(command, "move %5s to %5s %6s", fromTile, toTile, promoteToAsString) < 2){
		*exitcode = -1;
		return NULL;
	}
	int fromX, fromY, toX, toY;
	if (readTile(fromTile, &fromX, &fromY) == -1 
			|| readTile(toTile, &toX, &toY) == -1){
		*exitcode = -1;
		return NULL;
	}
	
	if (!Board_isInRange(fromX, fromY) || !Board_isInRange(toX, toY) ){
		*exitcode = -2;
		return NULL;
	}
	
	if (!str_equals(promoteToAsString, "undefined")){
		promoteTo = stringToPiece(promoteToAsString, turn);
		if (!promoteTo){
			*exitcode = -1; // the promotion was not input legally
			return NULL;
		}
		if (promoteTo && (!Board_isFurthestRowForPlayer(turn, toY) || !pieceIsPawn(fromX, fromY))){
			*exitcode = -6; // the promotion was input legally, but the move itself is illegal
			return NULL;
		}
	}
	
	if(pieceIsPawn(fromX, fromY) && Board_isFurthestRowForPlayer(turn, toY) && promoteTo == 0){
		promoteTo = (turn = WHITE)? 'q':'Q';        //default promotion
	}
	
	if (!Board_isInRange(fromX, fromY) 
			|| !Board_isInRange(toX, toY)){
		*exitcode = -2;
		return NULL;
	}
	if (Board_getColor(&board, fromX, fromY) != turn){
		*exitcode = -5;
		return NULL;
	}
	
	PossibleMove* move = PossibleMove_new(fromX, fromY, toX, toY, promoteTo, &board);
	if (!move){
		*exitcode = 1;
		return NULL;
	}
	return move;
}
Exemplo n.º 5
0
/*
 * Retrieves a piece type from user input.
 * 
 * @params: (str) - pointer to a user input string
 *			(color) - the relevant player's color
 */
char stringToPiece(char* str, int color){
	char piece = 0;
	if (str_equals(str, "pawn")){
		piece = Board_WHITE_PAWN;
	}
	if (str_equals(str, "bishop")){
		piece = Board_WHITE_BISHOP;
	}
	if (str_equals(str, "rook")){
		piece = Board_WHITE_ROOK;
	}
	if (str_equals(str, "knight")){
		piece = Board_WHITE_KNIGHT;
	}
	if (str_equals(str, "queen")){
		piece = Board_WHITE_QUEEN;
	}
	if (str_equals(str, "king")){
		piece = Board_WHITE_KING;
	}
	if (color == BLACK){
		piece = toBlack(piece);
	}
	return piece;
}
Exemplo n.º 6
0
void FMS::connect()
{
	if(isConnected())
		return;
	
	client = Wifi::cc3000.connectTCP(Wifi::cc3000.IP2U32(10, 0, 1, 49), 4159); // Using port 4159 cause why not
	if(!client.connected())
	{
		return;
	}
	
	Serial.print(F("Connecting to FMS... "));
	
	char* in;
	
	while(str_equals((in = readLine()), ""));
	
	if(str_startsWith(in, "I_G"))
	{
		Wifi::cc3000.disconnect();
		return;
	}
	
	client.fastrprint(F("I_"));
	char ca[1] = {(char)(((int)'0') + TEAM_NUMBER)};
	client.fastrprintln(ca); //It's janky but whatever it will do
	
	while(str_equals((in = readLine()), ""));
	if(str_startsWith(in, "I_ACK"))
	{
		Wifi::cc3000.disconnect();
		return;
	}
	
	connectedToFMS = true;
	
	Serial.println(F("done"));
	
	fmsLastSeen = millis();
}
Exemplo n.º 7
0
/*
 * Main function for handling the "difficulty" command for setting the difficulty level during the settings stage. 
 *
 * @return: -1 if the input was not formatted legally or if the current game mode is not player vs. AI
 *			-4 if an illegal number was input as the specified minimax depth
 *			 0 otherwise
 */
int setDifficulty(char* command){
	if (gameMode != 2){
		return -1;
	}
	
	char diff[6];
	int depth = -1; //initializing to non-relevant value to avoid problems with illegal inputs
	if(sscanf(command, "difficulty %5s %d", diff, &depth) < 1){
		return -1;
	}
	if (str_equals(diff, "best")){
		maxRecursionDepth = BEST;
		return 0;
	}
	if (!str_equals(diff, "depth")){
		return -1;
	}
	
	if (depth < 1 || depth > 4){
		return -4;
	}
	maxRecursionDepth = depth;
	return 0;
}
Exemplo n.º 8
0
/* Return the 'base' category for a given feature type (first
    category in range).  Returns 0 (background) if no match. */
int cm_get_category(CategoryMap *cm, String *type) {
  int i, j;
  if (cm->ncats == 0) return 0;
  for (i = 0; i <= cm->ncats; i++) {
    CategoryRange *cr = cm->ranges[i];
    if (cr == NULL) return 0;   /* possible if called in cm_read
                                   before object is complete */
    for (j = 0; j < lst_size(cr->feature_types); j++) 
      if (str_equals(type, (String*)lst_get_ptr(cr->feature_types, j)))
        return i;
    i = cr->end_cat_no;         /* avoid looking multiple times at the
                                   same range */
  }
  return 0;
}
Exemplo n.º 9
0
int main(int argc, char* argv[]){
	displayMode = CONSOLE;
	if(argc>1){
		if (str_equals(argv[1], "gui")){
			displayMode = GUI;
		}
	}
	
	int initializationError = initialize();
	if (initializationError == 1){ // allocation error occured
		allocationFailed();
	}
	else if (initializationError == 2){ // SDL failed to initialize
		exit(0);
	}
	
	display();

	while (1){
		if (isEndGame()){
			if (displayMode == CONSOLE){
				break;
			}
			else{
				gameEnded = 1;
			}
		}
		if (turn != player1 && gameMode == SINGLE_PLAYER_MODE && !gameEnded){
			if (computerTurn()){
				allocationFailed();
			}
		}
		else{
			short error = humanTurn(turn);  
			if (error != 0 && error != 2){ //error occured or "start" command entered
				return error;
			}
		}	
	}

	if (displayMode == CONSOLE){
		printEndGameResults();
	}
	return 0;
}
Exemplo n.º 10
0
static char *
path_from_uri(WebdavBackendFs *pbctx, const char *real_uri) {
  char *toret = NULL;

  assert(str_startswith(real_uri, "/"));

  toret = davfuse_util_strdup(pbctx->base_path);
  if (!toret) goto err;

  if (str_equals(real_uri, "/")) return toret;

  const char *start_of_dirname = real_uri;
  while (*start_of_dirname) {
    start_of_dirname += 1;

    const char *next = strchr(start_of_dirname, '/');
    if (!next) next = start_of_dirname + strlen(start_of_dirname);

    char *path_comp = malloc(next - start_of_dirname + 1);
    if (!path_comp) goto err;

    memcpy(path_comp, start_of_dirname, next - start_of_dirname);
    path_comp[next - start_of_dirname] = '\0';

    char *newtoret = util_fs_path_join(pbctx->fs, toret, path_comp);
    free(path_comp);

    if (!newtoret) goto err;
    free(toret);
    toret = newtoret;

    start_of_dirname = next;
  }

  if (false) {
  err:
    free(toret);
    toret = NULL;
  }

  return toret;
}
Exemplo n.º 11
0
file_io_t *Open(const char *pathname, char *mode) {
    file_io_t *fd = Malloc(sizeof(file_io_t));

#ifdef FILE_API
    fd->fd = NULL;
    if (str_equals(mode, "w")) {
        fd->fd = fopen(pathname, "wb");
    } else if (str_equals(mode, "a")) {
        fd->fd = fopen(pathname, "ab");
    } else if (str_equals(mode, "r")) {
        fd->fd = fopen(pathname, "rb");
    }

    if (fd->fd == NULL) {
        logprintf(LOG_ERR, "Cant open the file %s. Exiting now.", pathname);
        free(fd);
        exitPgm(EXIT_FAILURE);
    }

    fd->fileName = pathname;
    fd->mode = mode;

    return fd;
#else
    fd->fd = -1;

    if (str_equals(mode, "w")) {
        fd->fd = open(pathname, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
    } else if (str_equals(mode, "a")) {
        fd->fd = open(pathname, O_WRONLY | O_APPEND, 0 /*ignored*/);
    } else if (str_equals(mode, "r")) {
        fd->fd = open(pathname, O_RDONLY, 0 /*ignored*/);
    }

    if (fd->fd == -1) {
        logprintf(LOG_ERR, "Cant open the file %s. Exiting now.", pathname);
        free(fd);
        exitPgm(EXIT_FAILURE);
    }

    fd->fileName = pathname;
    fd->mode = mode;

    return fd;
#endif
}
Exemplo n.º 12
0
/* Return a list of category numbers corresponding to a given list of
   category names and or numbers.  */
List *cm_get_category_list(CategoryMap *cm,  List *names, int ignore_missing) {
  int i, j, cat;
  List *retval = lst_new_int(lst_size(names));
  for (i = 0; i < lst_size(names); i++) {
    String *n = lst_get_ptr(names, i);
    if (str_as_int(n, &cat) == 0) {
      if (cat < 0 || (cm != NULL && cat > cm->ncats)) 
        die("ERROR: category number %d is out of bounds.\n", cat);
      lst_push_int(retval, cat);
    }
    else {
      if (cm == NULL) 
        die("ERROR: if categories are specified by name, a category map is required.\n");
      cat = cm_get_category(cm, n);
      if (cat == 0 && !ignore_missing && !str_equals(n, cm_get_feature(cm, 0))) 
        die("ERROR: illegal category name (\"%s\")\n", n->chars);
      for (j = cm->ranges[cat]->start_cat_no; 
           j <= cm->ranges[cat]->end_cat_no; j++)
        lst_push_int(retval, j);
   }
  }
  return retval;
}
Exemplo n.º 13
0
/* Parse the options */
static int parse_options(int argc, char *argv[], char **pp_memo_name,
			 char **pp_outfile_name)
{
  int last_opt=1;
  int i=0;

  /* Assign an option argument. */
#define assign_optarg(opt, pp) do                                                      \
                                 {                                                     \
                                   if (i+1 >= argc || argv[i+1][0] == '-')             \
                                     {                                                 \
			 	       fprintf(stderr, opt" requires an argument!\n"); \
			 	       return -1;                                      \
				     }                                                 \
                                   else                                                \
                                     {                                                 \
                                       *(pp) = argv[++i];                              \
                                       last_opt++;                                     \
                                     }                                                 \
                                 } while(0)

#define str_equals(str) (strcmp((str), argv[i]) == 0)
  while (i < argc)
    {
      if (argv[i][0] == '-')
	last_opt++;

      if (str_equals("-n") || str_equals("--name")) /* Memo-name */
	assign_optarg("-n", pp_memo_name);
      else if (str_equals("-o") || str_equals("--outfile"))
	assign_optarg("-i", pp_outfile_name);
      else if (str_equals("-h") || str_equals("--help"))
	{
	  print_help();
	  exit(0);
	}
      i++;
    }
#undef str_equals
#undef assign_optarg

  return last_opt;
}
Exemplo n.º 14
0
void FMS::periodic()
{
	if(isConnected())
	{
		//If it's time then send a heartbeat request
		if(millis() - fmsLastSeen > FMS_HEARTBEAT_DELAY) //Add stuff to prevent dropped packets maybe idc
		{
			disableWatchdogs(); // Make sure watchdogs don't die when checking on heartbeat
			//Serial.println(F("Checking on heartbeat")); //TODO: dleete this
			
			sendPacket(F("HB_LUB"));
			long hbStarted = millis();
			
			while(str_equals(readLine(), ""))
			{
				if(millis() - hbStarted > FMS_HEARTBEAT_TIMEOUT)
				{
					connectedToFMS = false;
					client.close();
					Serial.println(F("FMS connection dropped."));
					setState(DISABLED);
					enableWatchdogs();
					return;
				}
			}
			
			fmsLastSeen = millis();
			enableWatchdogs();
		}
		
		//handle packets from fms
		char* input = readLine();
		
		if(!str_equals(input, ""))
		{
			if(str_startsWith(input, "S_S_"))
			{
				str_replace(input, "S_S_", "");
				if(str_startsWith(input, "A"))
				{
					setState(AUTONOMOUS);
				} else
				if(str_startsWith(input, "D"))
				{
					setState(DISABLED);
				} else
				if(str_startsWith(input, "T"))
				{
					setState(TELEOP);
				}
				sendPacket(F("S_ACK"));
			}
			else
				Serial.println(input); //Something wrong happened so lets output it for debug
			
			fmsLastSeen = millis();
		}
	} else {
		disableWatchdogs();
		connect();
		enableWatchdogs();
	}
}
Exemplo n.º 15
0
int main (int argc, char *argv[])
{
  char *dir = NULL;
  char *classpath = NULL;
  char *cmd = NULL;
  int task = EXECUTE;

  // Process command line parameters:
  while ( TRUE )
    {
      // Proceed to next parameter:
      if ( --argc > 0 )
        argv++;
      else
        argv = NULL;

      // Exit loop if command was found or no more parameters exist:
      if ( cmd != NULL || argc == 0 )
        break;

      // Next parameter:
      char *arg = argv[0];

      // --dir, -d:
      if ( str_starts_with(arg, "--dir=") )
        dir = arg+strlen("--dir=");
      else if ( str_equals(arg, "--dir") )
        {
          if ( --argc == 0 || str_is_opt((++argv)[0]) )
            error("Missing value after --dir");
          dir = argv[0];
        }
      else if ( str_starts_with(arg, "-d=") )
        dir = arg+strlen("-d=");
      else if ( str_equals(arg, "-d") )
        {
          if ( --argc == 0 || str_is_opt((++argv)[0]) )
            error("Missing value after -d");
          dir = argv[0];
        }

      // --dir-file:
      else if ( str_starts_with(arg, "--dir-file=") )
        {
          dir = read_file(arg+strlen("--dir-file="));
        }
      else if ( str_equals(arg, "--dir-file") )
        {
          if ( --argc == 0 || str_is_opt((++argv)[0]) )
            error("Missing value after --dir-file");
          dir = read_file(argv[0]);
        }

      // --classpath:
      else if ( str_starts_with(arg, "--classpath=") )
        classpath = arg+strlen("--classpath=");
      else if ( str_equals(arg, "--classpath") )
        {
          if ( --argc == 0 || str_is_opt((++argv)[0]) )
            error("Missing value after --classpath");
          classpath = argv[0];
        }

      // --stop-server:
      else if ( str_equals(arg, "--stop-server") )
        task = STOP_SERVER;

      // --stop-server-force:
      else if ( str_equals(arg, "--stop-server-force") )
        task = STOP_SERVER_FORCE;

      // --ping:
      else if ( str_equals(arg, "--ping") )
        task = PING;

      // --help:
      else if ( str_equals(arg, "--help") || str_equals(arg, "-h") )
        task = SHOW_HELP;

      // --version:
      else if ( str_equals(arg, "--version") || str_equals(arg, "-v") )
        task = SHOW_VERSION;

      // Unknown option:
      else if ( str_is_opt(arg) )
        error("Unknown option: %s", arg);

      // Command:
      else
        cmd = arg;
    }

  // Set dir to default if necessary:
  if ( dir == NULL )
    dir = getcwd (NULL, 0); // GNU specifiec

  switch ( task )
    {
    case EXECUTE:
      execute(cmd, argc, argv, dir, classpath);
      break;
    case STOP_SERVER:
      stop_server();
      break;
    case STOP_SERVER_FORCE:
      stop_server_force();
      break;
    case PING:
      ping();
      break;
    case SHOW_HELP:
      show_help();
      break;
    case SHOW_VERSION:
      show_version();
      break;
    }

  return(0);
}
Exemplo n.º 16
0
/*
 * Executes a command given by the user
 *
 * @params: (command) - the command given by the user
 * @return: relevant exitcode
 */
int executeCommand(char* command){
	char str[64];
	sscanf(command, "%s", str);
	if (str_equals(str, "quit")){
		exit(0);
	}	
	if (state == SETTINGS){
		if (str_equals(str, "game_mode")){
			return setGameMode(command);
		}
		if (str_equals(str, "difficulty")){
			return setDifficulty(command);
		}
		if (str_equals(str, "user_color")){
			return setUserColor(command);
		}
		if (str_equals(str, "load")){
			return loadGameByCommand(command);
		}
		if (str_equals(str, "clear")){
			Board_clear(&board);
			PieceCounter_reset(counter);
			return 0;
		}
		if (str_equals(str, "next_player")){
			return setFirstPlayer(command);
		}
		if (str_equals(str, "rm")){
			return removePiece(command);
		}
		if (str_equals(str, "set")){
			return setPiece(command);
		}
		if (str_equals(str, "print")){
			display();
			return 0;
		}
		if (str_equals(str, "start")){
			if(PieceCounter_kingIsMissing(counter)){
				return -7;
			}	
			turn = first;
			state = GAME;
			return 2; //special value to break the humanTurn loop so the initial board will always be checked for immediate loss or tie conditions
		}
	}
	else{
		if (str_equals(str, "get_moves")){
			return printMovesOfPiece(command);
		}
		if (str_equals(str, "move")){
			return movePiece(command);
		}
		if (str_equals(str, "castle")){
			return castleRook(command);
		}
		if (str_equals(str, "get_best_moves")){
			return printBestMoves(command);
		}
		if (str_equals(str, "get_score")){
			return printMoveValue(command);
		}	
		if (str_equals(str,"save")){
			return saveGameByCommand(command);
		}
	}
	return -1;
}
Exemplo n.º 17
0
json_t *make_replicate(const char *resource, const char *location,
                       const char *checksum, const char *replicate,
                       const char *status, baton_error_t *error) {
    json_t *result   = NULL;
    json_t *is_valid = NULL; // Ref to be stolen by result
    json_t *ck_value = NULL; // Ref to be stolen by result

    init_baton_error(error);

    int base = 10;
    char *endptr;
    int repl = strtoul(replicate, &endptr, base);
    if (*endptr) {
        set_baton_error(error, -1,
                        "Failed to parse replicate number from string '%s'",
                        replicate);
        goto error;
    }

    if (str_equals(status, INVALID_REPLICATE, 1)) {
        is_valid = json_false();
    }
    else if (str_equals(status, VALID_REPLICATE, 1)) {
        is_valid = json_true();
    }
    else {
        set_baton_error(error, CAT_INVALID_ARGUMENT,
                        "Invalid replicate status '%s'", status);
        goto error;
    }

    if (checksum) {
        ck_value = json_string(checksum);
    }
    else {
        ck_value = json_null();
    }

    result = json_pack("{s:s, s:s, s:o, s:i, s:o}",
                       JSON_RESOURCE_KEY,         resource,
                       JSON_LOCATION_KEY,         location,
                       JSON_CHECKSUM_KEY,         ck_value,
                       JSON_REPLICATE_NUMBER_KEY, repl,
                       JSON_REPLICATE_STATUS_KEY, is_valid);

    if (!result) {
        set_baton_error(error, -1, "Failed to pack replicate object");
        goto error;
    }

    return result;

error:
    if (result)   {
        json_decref(result);
    }
    else {
        // Only decref these if they were not stolen by a succesfully
        // created result
        if (is_valid) json_decref(is_valid);
        if (ck_value) json_decref(ck_value);
    }

    return NULL;
}
Exemplo n.º 18
0
SEXP rph_gff_one_attribute(SEXP gffP, SEXP tagP) {
  GFF_Set *gff = (GFF_Set*)EXTPTR_PTR(gffP);
  GFF_Feature *f;
  ListOfLists *lol;
  List *l1, *l2;
  int numtag, numval, i, j, k, resultLen, maxResultLen=10;
  String *currStr, *tag, *currTag;
  char **result;
  SEXP rv;
  SEXP rph_listOfLists_to_SEXP(ListOfLists *lol);


  if (lst_size(gff->features) == 0) return R_NilValue;
  gff_register_protect(gff);
  result = smalloc(maxResultLen*sizeof(char*));
  tag = str_new_charstr(CHARACTER_VALUE(tagP));
  str_double_trim(tag);
  lol = lol_new(lst_size(gff->features));
  l1 = lst_new_ptr(10);
  l2 = lst_new_ptr(10);
  for (i=0; i < lst_size(gff->features); i++) {
    checkInterruptN(i, 1000);
    resultLen=0;
    f = (GFF_Feature*) lst_get_ptr(gff->features, i);
    numtag = str_split_with_quotes(f->attribute, ";", l1);  //split tags
    for (j=0; j < numtag; j++) {
      currStr = (String*)lst_get_ptr(l1, j);
      str_double_trim(currStr);

      //first try gff version 3, see if we have tag=val format
      numval = str_split_with_quotes(currStr, "=", l2);
      if (numval == 2) {
	currTag = (String*)lst_get_ptr(l2, 0);
	str_double_trim(currTag);
	if (str_equals(tag, currTag)) {  // tag matches target, add all values to list
	  currStr = str_new_charstr(((String*)lst_get_ptr(l2, 1))->chars);
	  lst_free_strings(l2);
	  numval = str_split_with_quotes(currStr, ",", l2);
	  str_free(currStr);
	  for (k=0; k < numval; k++) {
	    currStr = lst_get_ptr(l2, k);
	    str_double_trim(currStr);
	    str_remove_quotes(currStr);
	    if (resultLen > maxResultLen) {
	      maxResultLen += 100;
	      result = srealloc(result, maxResultLen*sizeof(char*));
	    }
	    result[resultLen++] = copy_charstr(currStr->chars);
	  }
	}
      } else {
	lst_free_strings(l2);

	//gff version 2
	//split into tag val val ... by whitespace unless enclosed in quotes
	numval =  str_split_with_quotes(currStr, NULL, l2);
	if (numval > 1) {
	  currStr = (String*)lst_get_ptr(l2, 0);
	  str_double_trim(currStr);
	  if (str_equals(tag, currStr)) {  //tag matches target, add all values to list
	    for (k=1; k < numval; k++) {
	      currStr = (String*)lst_get_ptr(l2, k);
	      str_double_trim(currStr);
	      str_remove_quotes(currStr);
	      if (resultLen > maxResultLen) {
		maxResultLen += 100;
		result = srealloc(result, maxResultLen*sizeof(char*));
	      }
	      result[resultLen++] = copy_charstr(currStr->chars);
	    }
	  }
	}
	lst_free_strings(l2);
      }
    }
    if (resultLen == 0)
      result[resultLen++] = copy_charstr("");  //empty string will be converted to NA later
    lol_push_charvec(lol, result, resultLen, NULL);
    for (j=0; j < resultLen; j++) sfree(result[j]);
  }
  PROTECT(rv = rph_listOfLists_to_SEXP(lol));
  UNPROTECT(1);
  return rv;
}
Exemplo n.º 19
0
int main(int argc, char *argv[]){
  jlist = create_job_list();

  char *tok;
  TOKENIZER *tokenizer;
  int ispipe, redirect_out_flag, redirect_in_flag, not_exec_cmd, is_bg_flag;

  //read-in buffer
  char data[BUFFER_MAX]; 
  char extra[1]; 

  //linked list to add argument arrays to execvp: 1 for process 1, 2 for piped process
  list_elt *argv2_elt = NULL; 
  list_elt *argv1_elt = NULL;
  message_q = NULL;
  int argv1size = 0;
  int argv2size = 0;
  int status;

  //set shell pgid
  sh_pgid = getpgid(0);

  //register sigaction for SIGCHLD, SIGINT, SIGTSTP
  struct sigaction handler_action;
  sigaction(SIGCHLD, NULL, &handler_action);  //save previous action for SIGCHLD to handler_action

  //change what handler_action does
  handler_action.sa_flags |= SA_SIGINFO;
  handler_action.sa_flags |= SA_RESTART;

  handler_action.sa_sigaction = child_signal_handler;
  sigfillset(&handler_action.sa_mask);

  // sigaction setup
  sigaction(SIGCHLD, &handler_action, NULL);
  sigaction(SIGTSTP, &handler_action, NULL);
  sigaction(SIGINT, &handler_action, NULL);

  // igmore the sigint and sigtstp sigmals
  signal(SIGINT, SIG_IGN);
  //signal(SIGTSTP, SIG_IGN);

  while(1){
    signal(SIGTTOU, SIG_IGN); //igmore sigttou for tcsetpgrp

    if (-1 == tcsetpgrp(STDIN_FILENO, sh_pgid)){
      perror("tcsetpgrp read error");
    }

    if(-1 == tcsetpgrp(STDOUT_FILENO, sh_pgid)){
      perror("tcsetpgrp read error 2");
    }

    signal(SIGTTOU, SIG_DFL); // un-ignmore sigttou

    print_list(message_q); // print the message_q

    while(message_q){
      message_q =  delete(message_q, message_q); //empty the message_q
    }
    write(STDOUT_FILENO, "kinda-sh# ", 10); //prompt

    //initialize all the flags before tokenizing
    argv1size = 0;
    argv2size = 0;
    is_bg_flag = 0;
    ispipe = 0;
    redirect_out_flag = 0;
    redirect_in_flag = 0;  
    int new_std_in=0; // <
    int new_std_out = 0; // >
    char* fname_in;
    char* fname_out;
    int error_flag=0;
    not_exec_cmd = 0;
    fg_pid=0;

    int read_val;
    data[0] = '\0'; // null terminate data array

    read_val = read(0, data, BUFFER_MAX); // read into the data 


    if(data[0] != '\n'){
      //check if there is buffer overflow
      if((read_val == BUFFER_MAX) && (data[BUFFER_MAX - 1] != '\n')){ 

        while(1){
          argv2_elt = NULL;
          argv1_elt = NULL;
          read(0, extra, 1); // read 1 char at a time from the input stream until its empty (new line)
          if(extra[0] == '\n'){ //read in until the "return(\n) key is hit (similar to flush the std_in)
            break;
          }
        }
      }
      //error handling for inappropriate read size
      if(read_val < 0){ 
        perror("Read"); //check for read error
      } 
      if(read_val == 0){
        kill(0, SIGKILL);
      }

      data[read_val-1] = '\0'; //null terminate dat
      tokenizer = init_tokenizer(data); //create tokenizer on data

      // start tokenizer loop
      while((tok = get_next_token(tokenizer)) != NULL){

        if(is_bg_flag){
          printf("& should be at the end of the command \n");
          error_flag=1;
          break;
        }

        if( (*tok != '|') && (*tok != '&') && (*tok != '<') &&(*tok !=  '>') && (*tok != '\0')){ //check for token

          if(!ispipe){

            if(redirect_out_flag || redirect_in_flag){
              printf("invalid argument between redirection and pipe\n"); //cat < infile something | wc
              error_flag=1;
              break;
            }
            argv1_elt = add(argv1_elt, tok); // add tok to  arv1
            argv1size++;
          }
          else{ // after pipe - second process
            argv2size++;
            argv2_elt = add(argv2_elt, tok);
          }
          //if jobs command called
          if(str_equals(tok, "jobs")){
            print_job_list(jlist);
            not_exec_cmd = 1;
          }

          // if fg command called
          if(str_equals(tok, "fg")){
            job* new_bg_jb;
            char* num;
            num = get_next_token(tokenizer);
            int num_int = -1;
            not_exec_cmd = 1;
            int list_len;
            //if number argument is not specified, take the most recent background job
            if(num == NULL){
              new_bg_jb = get_ith_job(jlist, 1);
              if(new_bg_jb == NULL){
                printf("fg error: no job in job queue\n");
                break;
              }
            }
            //take the num'th background job
            else{
              num_int= my_atoi(num); // run atoi on input number (ie: fg 2)
              //reverse the number into the correct job order
              list_len = listlength(jlist);
              num_int = num_int - list_len -1;

              if(num_int < 0){
                num_int = num_int * (-1);
              }
              new_bg_jb = get_ith_job(jlist, num_int); //get num_int job from the job list

              if(new_bg_jb == NULL){
                printf("fg error: no [%s] job\n", num);
                free(num);
                break;
              } 
            }
            //take the foreground job pid as the chosen bg pid
            fg_pid= new_bg_jb->pgid;
            message_q = add(message_q, "\n"); // add restarting prompt to message_q
            message_q = add(message_q, new_bg_jb->command);
            message_q = add(message_q, "Restarting: ");

            print_list(message_q);
            while(message_q){
              message_q =  delete(message_q, message_q); //empty the message_q
            }

            if(tcsetpgrp(STDIN_FILENO, fg_pid) == -1){
              perror("tcsetpgrp error");
            }

            //relay SIGCONT to the job, and mask it from all signals but SIGCHLD
            killpg(fg_pid, SIGCONT);

            sigset_t cont_mask;
            sigfillset(&cont_mask);
            sigdelset(&cont_mask, SIGCHLD);

            //now the shell should wait for the new foregrounded job to be finished
            while(fg_pid){
              sigsuspend(&cont_mask);
            }
          }
          //take the specific background job to restart. If already running, do nothing.
          if(str_equals(tok, "bg")){
            job* new_bg_jb;
            not_exec_cmd = 1;
            char* num;
            int num_int=-1;
            num = get_next_token(tokenizer);

            // if num is not specified, get the most recent background job (to restart it)
            if(num == NULL){
              new_bg_jb = get_ith_job(jlist, 1);
              if(new_bg_jb == NULL){
                printf("bg error: no job in job queue\n");
                break;
              }
            }
            else{
              num_int= my_atoi(num); // run atoi for bg 
              //set up num_int to pass
              int list_len;
              list_len = listlength(jlist);
              num_int = num_int - list_len -1;
              if(num_int < 0){
                num_int = num_int * (-1);
              }
              new_bg_jb = get_ith_job(jlist, num_int); // get ith job from job_list

              if(new_bg_jb == NULL){
                printf("bg error: no [%s] job\n", num);
                free(num);
                break;
              }
            }

            if(!job_stopped(new_bg_jb)){
              //   tcsetpgrp(STDIN_FILENO, 0);
              printf("bg error: job is already running in the background\n");
              break;
            }

            killpg(new_bg_jb->pgid, SIGCONT);
            tcsetpgrp(STDIN_FILENO, 0);
          }
        }
        else if(*tok=='>'){ // if redirect out token

          if(redirect_out_flag){
            error_flag=1;
            printf("multiple stdout redirection is invalid\n"); //printf?
            break;
          }
          else{
            fname_out = get_next_token(tokenizer); // get hte next token
            new_std_out = open(fname_out, O_WRONLY| O_TRUNC | O_CREAT, 0644); // open file
            if(new_std_out == -1){
              perror("stdout Redir Error");
            }
            redirect_out_flag=1;
          }
        }
        else if(*tok=='<'){ //if redirect in token

          if(ispipe){
            printf("invalid stdin redirection after pipe\n");
            error_flag=1;
            break;
          }
          else if(redirect_in_flag){
            printf("multiple stdin redirection is invalid\n");
            error_flag=1;
            break;
          }            
          else{
            fname_in = get_next_token(tokenizer); // get next token
            new_std_in = open(fname_in, O_RDONLY); // open the file 
            if(new_std_in == -1){
              perror("stdin Redir Error");
            }
          }
        }
        else if(*tok=='|'){ //if pipe token

          if(ispipe){ // cant have more than 1 pipe (didnt do the extra credit)
            printf("invalid multiple pipes\n");
            error_flag=1;
            break;
          }
          else if(redirect_out_flag){
            printf("invalid pipe after stdout redirection\n");
            error_flag=1;
            break;
          }
          ispipe=1; //set a pipe flag
        }
        else if(*tok == '&'){ // if background command
          is_bg_flag = 1;
        }
      }

      if(is_bg_flag){
        data[read_val-2]= '\0'; // delete the '&' from the data array
      }

      argv1size++;
      argv2size++;

      char *argv1[argv1size];
      char *argv2[argv2size];

      if(error_flag || not_exec_cmd){ //if not elecutable command (jobs, fg, bg...) or error flag (bad command)
        while( argv1_elt ){
          argv1_elt = delete(argv1_elt, argv1_elt); //empty the linked list argv1_elt
        }
        while( argv2_elt ){
          argv2_elt = delete(argv2_elt, argv2_elt); //empty the linked list argv2_elt
        }
        continue;
      }

      //set up argv1 array
      list_elt *cursor = argv1_elt;
      list_elt *last;
      int tempIndex=0;
      //last pointer to the last element of argv1_elt linked list
      while(cursor != NULL){
        last = cursor;
        cursor = cursor->next;
      }
      //move all values from argv1_elt into argv1 array.
      while(last != NULL){ 
        argv1[tempIndex]=last->item;
        last = last->prev;
        tempIndex++;
      }
      argv1[argv1size-1] = NULL;

      //set up argv2 array if there's a pipe
      if(ispipe){
        cursor = argv2_elt;
        tempIndex=0;

        //last pointer to the last element of argv2_elt linked list
        while(cursor != NULL){
          last = cursor;
          cursor = cursor->next;
        }
        //move all values from argv2_elt into argv2 array.
        while(last != NULL){ 
          argv2[tempIndex]=last->item; //put input after the pipe into an array argv2
          last = last->prev;
          tempIndex++;
        }
        argv2[argv2size-1] = NULL;
      }
      free_tokenizer( tokenizer ); 

      // tokenizer update done.

      if((pid=fork()) < 0){
        perror("fork1");
      }

      //process & job stuff
      job* jb;
      jb = create_job(); // create corresponding job
      subjob* sj;
      sj = create_subjob(); // create corresponding subjob
      set_command(data, jb);

      if(pid==0){

        if(setpgid(0,0)==-1){
          perror("setpgid error");
        }
        sj->pid = getpid(); // get subjobs pid
        jb->pgid = getpgid(pid);//pid=0
      }
      else{

        if(setpgid(pid, pid)==-1){
          perror("setpgid error");
        }
        // set correct job and subjob pid vals
        sj->pid = pid;
        jb->pgid = pid;
      }

      set_first_subjob(sj, jb); // link subjob to job
      add_new_job(jb, jlist); // add the job to the job_list jlist

      if(pid==0){
        signal(SIGINT, SIG_DFL);
        signal(SIGTSTP, SIG_DFL);

        if(setpgid(0,0)==-1){
          perror("setpgid error");
        }

        if(new_std_out != 0){
          // if redirect out command, dup accordingly
          if(dup2(new_std_out, STDOUT_FILENO) == -1){ //dup2 for > (out)
            perror("stdout dup2");
            _exit(0);
          }
          free(fname_out);
        }

        if(new_std_in != 0){
          // if redirect in command, dup accordingly
          if(dup2(new_std_in, STDIN_FILENO) == -1){ //dup2 for < (in)
            perror("stdin dup2");
            _exit(0);
          }
          free(fname_in);
        }

        if(ispipe){ // if pipe command called
          int filedes[2];

          if(pipe(filedes)){//pipe
            perror("pipe error");
          }
          scnd_pid = fork();

          if(scnd_pid < 0){ //print error if the fork failed
            perror("Fork");
            exit(-1);
          }
          //create a subjob, set its pid for both child and parent
          subjob* sj2;
          sj2 = create_subjob();

          if(scnd_pid==0){
            sj2->pid = getpid(); 
          }
          else{
            sj2->pid = scnd_pid;
          }

          jb->pgid = getpgid(scnd_pid);

          //add to the job group as the first (most recent) process.
          set_first_subjob(sj2, jb);
          set_next_subjob(sj2, sj);
          pipe_pid=sj2->pid;

          if(scnd_pid==0){ // process that writes to pipe (program 1) grand child

            if(close(filedes[0]) == -1){ // close STDIN part of pipe
              perror("close");
            }

            if(dup2(filedes[1], STDOUT_FILENO) == -1){ // dip for STDOUT
              perror("pipe dup2 #1");
            }
            status =  execvp(argv1[0], argv1); // execute

            if(status == -1){  
              perror("execvp program1");
              exit(-1);
            }
            killpg(0, SIGKILL);
            _exit(0); // exit the child
          }
          else{ //program 2; (first fork) process that reads from pipe
            sigset_t sigmask;
            sigfillset(&sigmask);
            sigdelset(&sigmask, SIGCHLD);

            while(pipe_pid){
              sigsuspend(&sigmask);
            }

            if(close(filedes[1]) == -1){ // close STDOUT part of pipe
              perror("close");
            }

            if(dup2(filedes[0], STDIN_FILENO) == -1){ // dup for the STDIN
              perror("dup2 (pipe #2)");
            }

            status = execvp(argv2[0], argv2); // execute the second part of pipe

            if(status == -1){  
              perror("execvp program2");
            }
            killpg(0, SIGKILL);
            _exit(0);
          }
        }
        else{ // if not pipe, pid.
          status = execvp(argv1[0], argv1); 

          if(status == -1){  
            perror("execvp");
          }
          _exit(0);
        }
      } 
      else{ //kinda-sh
        if(setpgid(pid, pid) ==-1){
          perror("setpgid pid");
        }

        if(is_bg_flag){ //if background command, prompt with running
          printf("Running: %s\n", data);
        }

        if(!is_bg_flag){
          fg_pid= pid; // if not bacground command, set the fg_pid to be the first fork pid val
          tcsetpgrp(STDIN_FILENO, pid);
        }

        sigset_t sigmask0;
        sigfillset(&sigmask0);
        sigdelset(&sigmask0, SIGCHLD);

        //suspend only if it's not bg process  
        while(fg_pid!=0){
          sigsuspend(&sigmask0);
        }
        if(new_std_in != 0){
          close(new_std_in); //close stdin if used
        }
        if(new_std_out != 0){
          close(new_std_out); //close stdout if used
        }

      }
      while(argv1_elt){
        argv1_elt = delete(argv1_elt, argv1_elt); //empty the linked list argv1_elt
      }
      while(argv2_elt){
        argv2_elt = delete(argv2_elt, argv2_elt); //empty the linked list argv2_elt
      }
    } //if data[0]!=\n
  }//while end

  return 0;
}
Exemplo n.º 20
0
#include <glib.h>
#include <stdlib.h>
#include <string.h>

#include "str/str.h"
#include "adt/adt.h"

static void test_str_equals_1() {
    g_assert_cmpint(str_equals((char *) "", (char *) ""), ==, 1);
}

static void test_str_equals_2() {
    g_assert_cmpint(str_equals((char *) "Sport Club Corinthians Paulista",
                               (char *) "Sport Club Corinthians Paulista"), ==, 1);
}

static void test_str_equals_3() {
    g_assert_cmpint(str_equals((char *) "Sport Club Corinthians Paulista",
                               (char *) "Republica Oriental del Uruguay"), ==, 0);
}

static void test_str_dup_1() {
    char *dup = str_dup((char *) "");
    g_assert_cmpstr(dup, ==, "");
    free(dup);
}

static void test_str_dup_2() {
    char *dup = str_dup((char *) "Sport Club Corinthians Paulista");
    g_assert_cmpstr(dup, ==, (char *) "Sport Club Corinthians Paulista");
    free(dup);