Example #1
0
File: cli.c Project: Godzil/sd2snes
void cmd_sreset(void) {
  if(*curchar != 0) {
    int32_t resetstate;
    resetstate = parse_unsigned(0,1,10);
    snes_reset(resetstate);
  } else {
    snes_reset(1);
    delay_ms(20);
    snes_reset(0);
  }
}
bool
snp_type_info::
get_format_unsigned(const char* const* word,
                    const char* key,
                    unsigned& val) {

    const char* str(get_format_string_nocopy(word,key));
    if (NULL==str) return false;
    val=parse_unsigned(str);
    return true;
}
Example #3
0
    void edituser_page(Database &database, Request &request,
            std::ostream &fcout, std::istream &fcin)
    {
        if (request.type() == RequestType::Post)
        {
            std::map<std::string, std::string> data;
            parse_pairs(fcin, '&', data);
            auto pass = data.find("pass");
            auto pass_again = data.find("again");
            std::string userid_str = request.post("userid");
            uint64_t userid;
            if (userid_str.empty() || !parse_unsigned(userid_str, userid))
                throw std::logic_error("Invalid or missing userid!");

            if (pass != data.end())
            {
                if (pass_again == data.end() || pass_again->second == pass->second)
                {
                    User user = User::find(database, userid);
                    if (!user.valid())
                    {
                        LOG_MESSAGE_DEBUG("No such user");
                        throw std::logic_error("No such user!");
                    }
                    //user.name(name->second);
                    user.reset_password(pass->second);
                    user.save();
                    fcout << "Location: ?q=user\r\n";
                    fcout << "\r\n\r\n";
                } else
                {
                    LOG_MESSAGE_DEBUG("Password different");
                    throw std::logic_error("Password different");
                }
            } else
            {
                LOG_MESSAGE_DEBUG("Missing password");
                throw std::logic_error("Missing password");
            }
        } else
        {
            fcout << "<form name=\"edituser_form\" method=\"post\">";
            fcout << "<input type=\"hidden\" name=\"userid\" value=\"" << request.get("userid") << "\">";
            //fcout << "Username: <input name=\"name\">";
            fcout << "Password: <input name=\"pass\">";
            fcout << "Password again: <input name=\"pass_again\">";
            fcout << "<input type=\"submit\" value=\"Edit\">";
            fcout << "</form>";
        }
    }
Example #4
0
File: cli.c Project: Godzil/sd2snes
static void cmd_vmode(void) {
  int32_t hz;
  if(get_cic_state() != CIC_PAIR) {
    printf("not in pair mode\n");
  } else {
    hz = parse_unsigned(50,60,10);
    if(hz==50) {
      cic_videomode(CIC_PAL);
    } else {
      cic_videomode(CIC_NTSC);
    }
    printf("ok\n");
  }
}
Example #5
0
VcfRecord::
VcfRecord(const istream_line_splitter& vparse)
{
    const unsigned ws(vparse.n_word());
    if (static_cast<int>(ws) <= VCFID::INFO) {
        std::ostringstream oss;
        oss << "Too few fields (" << ws << ") in vcf record input:\n";
        vparse.dump(oss);
        throw blt_exception(oss.str().c_str());
    }
    
    _chrom = vparse.word[VCFID::CHROM];
    
    const char* pos_ptr(vparse.word[VCFID::POS]);
    _pos = parse_unsigned(pos_ptr);

    _id = vparse.word[VCFID::ID];

    _ref = vparse.word[VCFID::REF];
    assert(_ref.size() > 0);

    Splitter(vparse.word[VCFID::ALT],',',_alt);

    for(unsigned i(0);i<_alt.size();++i) {
        assert(_alt[i].size() > 0);
    }

    _qual = vparse.word[VCFID::QUAL];

    Splitter(vparse.word[VCFID::FILT],';',_filt);

    Splitter(vparse.word[VCFID::INFO],';',_info);
    
    if(ws > VCFID::FORMAT) {
        Splitter(vparse.word[VCFID::FORMAT],':',_format);
    }
    
    if(ws > VCFID::SAMPLE) {
        Splitter(vparse.word[VCFID::SAMPLE],':',_sample);
    }
    
    if(_format.size() != _sample.size()) {
        std::ostringstream oss;
        oss << "FORMAT and SAMPLE fields do not agree for vcf record:\n";
        vparse.dump(oss);
        throw blt_exception(oss.str().c_str());
    }
}
// extract unsigned value for key from the vcf info field
bool
snp_type_info::
get_info_unsigned(const char* info,
                  const char* key,
                  unsigned& val) {

    const char* tmp(NULL);
    do {
        if (NULL != tmp) info=tmp+1;
        if (0!=strncmp(info,key,strlen(key))) continue;
        if (NULL==(info=strchr(info,'='))) return false;
        const char* s(info+1);
        val=parse_unsigned(s);
        return true;
    } while (NULL != (tmp=strchr(info,';')));
    return false;
}
Example #7
0
static bool
parse_bool (char *token, bool min, bool max)
{
  while (*token && ISSPACE(*token))
    ++token;

  if (ISDIGIT (*token))
    return parse_unsigned (token, min, max);

  /* FIXME: handle min and max.  */
  if (!strcasecmp (token, "true")) {
    return true;
  } else if (!strcasecmp (token, "false")) {
    return false;
  } else {
    emsg ("%s:%d: %s should be either false or true",
          preferences_file (), firstline, token);
    return false; /* Never reached.  */
  }
}
Example #8
0
/* Play a list of audio files. */
int
main(int argc, char **argv) {
	int		errorStatus = 0;
	int		i;
	int		c;
	int		cnt;
	int		file_type;
	int		rem;
	int		outsiz;
	int		tsize;
	int		len;
	int		err;
	int		ifd;
	int		stdinseen;
	int		regular;
	int		swapBytes;
	int		frame;
	char		*outbuf;
	caddr_t		mapaddr;
	struct stat	st;
	char		*cp;
	char		ctldev[MAXPATHLEN];

	(void) setlocale(LC_ALL, "");
	(void) textdomain(TEXT_DOMAIN);

	/* Get the program name */
	prog = strrchr(argv[0], '/');
	if (prog == NULL)
		prog = argv[0];
	else
		prog++;
	Stdin = MGET("(stdin)");

	/* Check AUDIODEV environment for audio device name */
	if (cp = getenv("AUDIODEV")) {
		Audio_dev = cp;
	}

	/* Parse the command line arguments */
	err = 0;
	while ((i = getopt(argc, argv, prog_opts)) != EOF) {
		switch (i) {
			case 'v':
				if (parse_unsigned(optarg, &Volume, "-v")) {
					err++;
				} else if (Volume > MAX_GAIN) {
					Error(stderr, MGET("%s: invalid value "
					    "for -v\n"), prog);
					err++;
				}
				break;
			case 'd':
				Audio_dev = optarg;
				break;
			case 'V':
				Verbose = TRUE;
				break;
			case 'E':
				Errdetect = TRUE;
				break;
			case 'i':
				Immediate = TRUE;
				break;
			case '?':
				usage();
		/*NOTREACHED*/
		}
	}
	if (err > 0)
		exit(1);

	argc -= optind;		/* update arg pointers */
	argv += optind;

	/* Validate and open the audio device */
	err = stat(Audio_dev, &st);
	if (err < 0) {
		Error(stderr, MGET("%s: cannot stat "), prog);
		perror(Audio_dev);
		exit(1);
	}
	if (!S_ISCHR(st.st_mode)) {
		Error(stderr, MGET("%s: %s is not an audio device\n"), prog,
		    Audio_dev);
		exit(1);
	}

	/* This should probably use audio_cntl instead of open_audio */
	if ((argc <= 0) && isatty(fileno(stdin))) {
		Error(stderr, MGET("%s: No files and stdin is a tty.\n"), prog);
		exit(1);
	}

	/* Check on the -i status now. */
	Audio_fd = open(Audio_dev, O_WRONLY | O_NONBLOCK);
	if ((Audio_fd < 0) && (errno == EBUSY)) {
		if (Immediate) {
			Error(stderr, MGET("%s: %s is busy\n"), prog,
			    Audio_dev);
			exit(1);
		}
	}
	(void) close(Audio_fd);
	Audio_fd = -1;

	/* Try to open the control device and save the current format */
	(void) snprintf(ctldev, sizeof (ctldev), "%sctl", Audio_dev);
	Audio_ctlfd = open(ctldev, O_RDWR);
	if (Audio_ctlfd >= 0) {
		/*
		 * wait for the device to become available then get the
		 * controls. We want to save the format that is left when the
		 * device is in a quiescent state. So wait until then.
		 */
		Audio_fd = open(Audio_dev, O_WRONLY);
		(void) close(Audio_fd);
		Audio_fd = -1;
		if (audio_get_play_config(Audio_ctlfd, &Save_hdr)
		    != AUDIO_SUCCESS) {
			(void) close(Audio_ctlfd);
			Audio_ctlfd = -1;
		}
	}

	/* store AUDIOPATH so we don't keep doing getenv() */
	Audio_path = getenv("AUDIOPATH");

	/* Set up SIGINT handler to flush output */
	(void) signal(SIGINT, sigint);

	/* Set the endian nature of the machine. */
	if ((ulong_t)1 != htonl((ulong_t)1)) {
		NetEndian = FALSE;
	}

	/* If no filenames, read stdin */
	stdinseen = FALSE;
	if (argc <= 0) {
		Ifile = Stdin;
	} else {
		Ifile = *argv++;
		argc--;
	}

	/* Loop through all filenames */
	do {
		/* Interpret "-" filename to mean stdin */
		if (strcmp(Ifile, "-") == 0)
			Ifile = Stdin;

		if (Ifile == Stdin) {
			if (stdinseen) {
				Error(stderr,
				    MGET("%s: stdin already processed\n"),
				    prog);
				goto nextfile;
			}
			stdinseen = TRUE;
			ifd = fileno(stdin);
		} else {
			if ((ifd = path_open(Ifile, O_RDONLY, 0, Audio_path))
			    < 0) {
				Error(stderr, MGET("%s: cannot open "), prog);
				perror(Ifile);
				errorStatus++;
				goto nextfile;
			}
		}

		/* Check to make sure this is an audio file */
		err = audio_read_filehdr(ifd, &File_hdr, &file_type,
		    (char *)NULL, 0);
		if (err != AUDIO_SUCCESS) {
			Error(stderr,
			    MGET("%s: %s is not a valid audio file\n"),
			    prog, Ifile);
			errorStatus++;
			goto closeinput;
		}

		/* If G.72X adpcm, set flags for conversion */
		if ((File_hdr.encoding == AUDIO_ENCODING_G721) &&
		    (File_hdr.samples_per_unit == 2) &&
		    (File_hdr.bytes_per_unit == 1)) {
			Decode = AUDIO_ENCODING_G721;
			File_hdr.encoding = AUDIO_ENCODING_ULAW;
			File_hdr.samples_per_unit = 1;
			File_hdr.bytes_per_unit = 1;
			adpcm_state = (struct audio_g72x_state *)malloc
			    (sizeof (*adpcm_state) * File_hdr.channels);
			for (i = 0; i < File_hdr.channels; i++) {
				g721_init_state(&adpcm_state[i]);
			}
		} else if ((File_hdr.encoding == AUDIO_ENCODING_G723) &&
		    (File_hdr.samples_per_unit == 8) &&
		    (File_hdr.bytes_per_unit == 3)) {
			Decode = AUDIO_ENCODING_G723;
			File_hdr.encoding = AUDIO_ENCODING_ULAW;
			File_hdr.samples_per_unit = 1;
			File_hdr.bytes_per_unit = 1;
			adpcm_state = (struct audio_g72x_state *)malloc
			    (sizeof (*adpcm_state) * File_hdr.channels);
			for (i = 0; i < File_hdr.channels; i++) {
				g723_init_state(&adpcm_state[i]);
			}
		} else {
			Decode = AUDIO_ENCODING_NONE;
		}

		/* Check the device configuration */
		open_audio();
		if (audio_cmp_hdr(&Dev_hdr, &File_hdr) != 0) {
			/*
			 * The device does not match the input file.
			 * Wait for any old output to drain, then attempt
			 * to reconfigure the audio device to match the
			 * input data.
			 */
			if (audio_drain(Audio_fd, FALSE) != AUDIO_SUCCESS) {
				/* Flush any remaining audio */
				(void) ioctl(Audio_fd, I_FLUSH, FLUSHW);

				Error(stderr, MGET("%s: "), prog);
				perror(MGET("AUDIO_DRAIN error"));
				exit(1);
			}

			/* Flush any remaining audio */
			(void) ioctl(Audio_fd, I_FLUSH, FLUSHW);

			if (!reconfig()) {
				errorStatus++;
				goto closeinput;
			}
		}


		/* try to do the mmaping - for regular files only ... */
		err = fstat(ifd, &st);
		if (err < 0) {
			Error(stderr, MGET("%s: cannot stat "), prog);
			perror(Ifile);
			exit(1);
		}
		regular = (S_ISREG(st.st_mode));


		/* If regular file, map it.  Else, allocate a buffer */
		mapaddr = 0;

		/*
		 * This should compare to MAP_FAILED not -1, can't
		 * find MAP_FAILED
		 */
		if (regular && ((mapaddr = mmap(0, st.st_size, PROT_READ,
		    MAP_SHARED, ifd, 0)) != MAP_FAILED)) {

			(void) madvise(mapaddr, st.st_size, MADV_SEQUENTIAL);

			/* Skip the file header and set the proper size */
			cnt = lseek(ifd, 0, SEEK_CUR);
			if (cnt < 0) {
			    perror("lseek");
			    exit(1);
			}
			inbuf = (unsigned char *) mapaddr + cnt;
			len = cnt = st.st_size - cnt;
		} else {		/* Not a regular file, or map failed */

			/* mark is so. */
			mapaddr = 0;

			/* Allocate buffer to hold 10 seconds of data */
			cnt = BUFFER_LEN * File_hdr.sample_rate *
			    File_hdr.bytes_per_unit * File_hdr.channels;
			if (bufsiz != cnt) {
				if (buf != NULL) {
					(void) free(buf);
				}
				buf = (unsigned char *) malloc(cnt);
				if (buf == NULL) {
					Error(stderr,
					    MGET("%s: couldn't allocate %dK "
					    "buf\n"), prog, bufsiz / 1000);
					exit(1);
				}
				inbuf = buf;
				bufsiz = cnt;
			}
		}

		/* Set buffer sizes and pointers for conversion, if any */
		switch (Decode) {
		default:
		case AUDIO_ENCODING_NONE:
			insiz = bufsiz;
			outbuf = (char *)buf;
			break;
		case AUDIO_ENCODING_G721:
			insiz = ADPCM_SIZE / 2;
			outbuf = (char *)adpcm_buf;
			initmux(1, 2);
			break;
		case AUDIO_ENCODING_G723:
			insiz = (ADPCM_SIZE * 3) / 8;
			outbuf = (char *)adpcm_buf;
			initmux(3, 8);
			break;
		}

		/*
		 * 8-bit audio isn't a problem, however 16-bit audio is.
		 * If the file is an endian that is different from the machine
		 * then the bytes will need to be swapped.
		 *
		 * Note: Because the G.72X conversions produce 8bit output,
		 * they don't require a byte swap before display and so
		 * this scheme works just fine. If a conversion is added
		 * that produces a 16 bit result and therefore requires
		 * byte swapping before output, then a mechanism
		 * for chaining the two conversions will have to be built.
		 *
		 * Note: The following if() could be simplified, but then
		 * it gets to be very hard to read. So it's left as is.
		 */

		if (File_hdr.bytes_per_unit == 2 &&
		    ((!NetEndian && file_type == FILE_AIFF) ||
		    (!NetEndian && file_type == FILE_AU) ||
		    (NetEndian && file_type == FILE_WAV))) {
			swapBytes = TRUE;
		} else {
			swapBytes = FALSE;
		}

		if (swapBytes) {
			/* Read in interal number of sample frames. */
			frame = File_hdr.bytes_per_unit * File_hdr.channels;
			insiz = (SWAP_SIZE / frame) * frame;
			/* make the output buffer  the swap buffer. */
			outbuf = (char *)swap_buf;
		}

		/*
		 * At this point, we're all ready to copy the data.
		 */
		if (mapaddr == 0) { /* Not mmapped, do it a buffer at a time. */
			inbuf = buf;
			frame = File_hdr.bytes_per_unit * File_hdr.channels;
			rem = 0;
			while ((cnt = read(ifd, inbuf+rem, insiz-rem)) >= 0) {
				/*
				 * We need to ensure only an integral number of
				 * samples is ever written to the audio device.
				 */
				cnt = cnt + rem;
				rem = cnt % frame;
				cnt = cnt - rem;

				/*
				 * If decoding adpcm, or swapping bytes do it
				 * now.
				 *
				 * We treat the swapping like a separate
				 * encoding here because the G.72X encodings
				 * decode to single byte output samples. If
				 * another encoding is added and it produces
				 * multi-byte output samples this will have to
				 * be changed.
				 */
				if (Decode == AUDIO_ENCODING_G721) {
				    outsiz = 0;
				    demux(1, cnt / File_hdr.channels);
				    for (c = 0; c < File_hdr.channels; c++) {
					err = g721_decode(in_ch_data[c],
					    cnt / File_hdr.channels,
					    &File_hdr,
					    (void*)out_ch_data[c],
					    &tsize,
					    &adpcm_state[c]);
					outsiz = outsiz + tsize;
					if (err != AUDIO_SUCCESS) {
					    Error(stderr, MGET(
						"%s: error decoding g721\n"),
						prog);
						errorStatus++;
						break;
					}
				    }
				    mux(outbuf);
				    cnt = outsiz;
				} else if (Decode == AUDIO_ENCODING_G723) {
				    outsiz = 0;
				    demux(3, cnt / File_hdr.channels);
				    for (c = 0; c < File_hdr.channels; c++) {
					err = g723_decode(in_ch_data[c],
					    cnt / File_hdr.channels,
					    &File_hdr,
					    (void*)out_ch_data[c],
					    &tsize,
					    &adpcm_state[c]);
					outsiz = outsiz + tsize;
					if (err != AUDIO_SUCCESS) {
					    Error(stderr, MGET(
						"%s: error decoding g723\n"),
						prog);
					    errorStatus++;
					    break;
					}
				    }
				    mux(outbuf);
				    cnt = outsiz;
				} else if (swapBytes) {
					swab((char *)inbuf, outbuf, cnt);
				}

				/* If input EOF, write an eof marker */
				err = write(Audio_fd, outbuf, cnt);

				if (err < 0) {
					perror("write");
					errorStatus++;
					break;
				} else if (err != cnt) {
					Error(stderr,
					    MGET("%s: output error: "), prog);
					perror("");
					errorStatus++;
					break;
				}
				if (cnt == 0) {
					break;
				}
				/* Move remainder to the front of the buffer */
				if (rem != 0) {
					(void *)memcpy(inbuf, inbuf + cnt, rem);
				}

			}
			if (cnt < 0) {
				Error(stderr, MGET("%s: error reading "), prog);
				perror(Ifile);
				errorStatus++;
			}
		} else {	/* We're mmaped */
			if ((Decode != AUDIO_ENCODING_NONE) || swapBytes) {

				/* Transform data if we have to. */
				for (i = 0; i <= len; i += cnt) {
					cnt = insiz;
					if ((i + cnt) > len) {
						cnt = len - i;
					}
					if (Decode == AUDIO_ENCODING_G721) {
					    outsiz = 0;
					    demux(1, cnt / File_hdr.channels);
					    for (c = 0; c < File_hdr.channels;
						c++) {
						err = g721_decode(
						    in_ch_data[c],
						    cnt / File_hdr.channels,
						    &File_hdr,
						    (void*)out_ch_data[c],
						    &tsize,
						    &adpcm_state[c]);
						outsiz = outsiz + tsize;
						if (err != AUDIO_SUCCESS) {
						    Error(stderr, MGET(
							"%s: error decoding "
							"g721\n"), prog);
						    errorStatus++;
						    break;
						}
					    }
					    mux(outbuf);
					} else if
					    (Decode == AUDIO_ENCODING_G723) {
						outsiz = 0;
						demux(3,
						    cnt / File_hdr.channels);
						for (c = 0;
							c < File_hdr.channels;
							c++) {
						    err = g723_decode(
							in_ch_data[c],
							cnt /
							    File_hdr.channels,
							&File_hdr,
							(void*)out_ch_data[c],
							&tsize,
							&adpcm_state[c]);
						    outsiz = outsiz + tsize;
						    if (err != AUDIO_SUCCESS) {
							Error(stderr, MGET(
							    "%s: error "
							    "decoding g723\n"),
							    prog);
							errorStatus++;
							break;
						    }
						}
						mux(outbuf);
					} else if (swapBytes) {
						swab((char *)inbuf, outbuf,
						    cnt);
						outsiz = cnt;
					}
					inbuf += cnt;

					/* If input EOF, write an eof marker */
					err = write(Audio_fd, (char *)outbuf,
					    outsiz);
					if (err < 0) {
						perror("write");
						errorStatus++;
					} else if (outsiz == 0) {
						break;
					}

				}
			} else {
				/* write the whole thing at once!  */
				err = write(Audio_fd, inbuf, len);
				if (err < 0) {
					perror("write");
					errorStatus++;
				}
				if (err != len) {
					Error(stderr,
					    MGET("%s: output error: "), prog);
					perror("");
					errorStatus++;
				}
				err = write(Audio_fd, inbuf, 0);
				if (err < 0) {
					perror("write");
					errorStatus++;
				}
			}
		}

		/* Free memory if decoding ADPCM */
		switch (Decode) {
		case AUDIO_ENCODING_G721:
		case AUDIO_ENCODING_G723:
			freemux();
			break;
		default:
			break;
		}

closeinput:;
		if (mapaddr != 0)
			(void) munmap(mapaddr, st.st_size);
		(void) close(ifd);		/* close input file */
		if (Errdetect) {
			cnt = 0;
			audio_set_play_error(Audio_fd, (unsigned int *)&cnt);
			if (cnt) {
				Error(stderr,
				    MGET("%s: output underflow in %s\n"),
				    Ifile, prog);
				errorStatus++;
			}
		}
nextfile:;
	} while ((argc > 0) && (argc--, (Ifile = *argv++) != NULL));

	/*
	 * Though drain is implicit on close(), it's performed here
	 * to ensure that the volume is reset after all output is complete.
	 */
	(void) audio_drain(Audio_fd, FALSE);

	/* Flush any remaining audio */
	(void) ioctl(Audio_fd, I_FLUSH, FLUSHW);

	if (Volume != INT_MAX)
		(void) audio_set_play_gain(Audio_fd, &Savevol);
	if ((Audio_ctlfd >= 0) && (audio_cmp_hdr(&Save_hdr, &Dev_hdr) != 0)) {
		(void) audio_set_play_config(Audio_fd, &Save_hdr);
	}
	(void) close(Audio_fd);			/* close output */
	return (errorStatus);
}
Example #9
0
File: cli.c Project: Godzil/sd2snes
void cmd_w16(void) {
  uint32_t offset = parse_unsigned(0, 16777215, 16);
  uint16_t val = parse_unsigned(0, 65535, 16);
  sram_writeshort(val, offset);
}
Example #10
0
bool
load_preferences (void)
{
  FILE* fs;
  int endline = 0;
  char* buf = 0;
  size_t bufsize = 0;

  firstline = 0;

  reinit_preferences ();

  dmsg (D_FILE, "reading option from %s", preferences_file ());
  fs = fopen (preferences_file (), "rb");

  if (fs == NULL) {
    /* Don't warn, the file doesn't exists the first time.  */
    dmsg (D_FILE, "cannot open preferences file %s", preferences_file ());
    dperror ("fopen");
    return true;
  }

  XFREE0 (ignored_lines);

  while (getshline_numbered
	 (&firstline, &endline, &buf, &bufsize, fs) != -1) {
    char* token;
    char* line = xstrdup (buf);

    token = strtok (buf, " \t.:");
    if (!strcasecmp ("heroes", token)) {
    token = strtok (0, " \t.:");
    if (!strcasecmp ("screen", token)) {
    token = strtok (0, " \t.:");
    if (!strcasecmp ("display_radar", token)) {
    token = strtok (0, "\n");
    opt.radar_map = parse_bool (token, false, true);
    } else 
    if (!strcasecmp ("display_infos", token)) {
    token = strtok (0, "\n");
    opt.display_infos = parse_bool (token, false, true);
    } else 
    if (!strcasecmp ("gamma", token)) {
    token = strtok (0, "\n");
    opt.luminance = parse_unsigned (token, 0, 12);
    } else 
    if (!strcasecmp ("inertia", token)) {
    token = strtok (0, "\n");
    opt.inertia = parse_bool (token, false, true);
    } else { append_ignored (line); }
    } else 
    if (!strcasecmp ("sound", token)) {
    token = strtok (0, " \t.:");
    if (!strcasecmp ("music_enable", token)) {
    token = strtok (0, "\n");
    opt.music = parse_bool (token, false, true);
    } else 
    if (!strcasecmp ("music_volume", token)) {
    token = strtok (0, "\n");
    opt.music_volume = parse_unsigned (token, 0, 12);
    } else 
    if (!strcasecmp ("sfx_enable", token)) {
    token = strtok (0, "\n");
    opt.sfx = parse_bool (token, false, true);
    } else 
    if (!strcasecmp ("sfx_volume", token)) {
    token = strtok (0, "\n");
    opt.sfx_volume = parse_unsigned (token, 0, 12);
    } else { append_ignored (line); }
    } else 
    if (!strcasecmp ("control", token)) {
    token = strtok (0, " \t.:");
    if (!strcasecmp ("player1", token)) {
    token = strtok (0, "\n");
    opt.ctrl_one = parse_unsigned (token, 0, 1);
    } else 
    if (!strcasecmp ("autopilot1", token)) {
    token = strtok (0, "\n");
    opt.autopilot_one = parse_bool (token, false, true);
    } else 
    if (!strcasecmp ("player2", token)) {
    token = strtok (0, "\n");
    opt.ctrl_two = parse_unsigned (token, 0, 1);
    } else 
    if (!strcasecmp ("autopilot2", token)) {
    token = strtok (0, "\n");
    opt.autopilot_two = parse_bool (token, false, true);
    } else { append_ignored (line); }
    } else 
    if (!strcasecmp ("game", token)) {
    token = strtok (0, " \t.:");
    if (!strcasecmp ("speed", token)) {
    token = strtok (0, "\n");
    opt.speed = parse_unsigned (token, 0, 2);
    } else 
    if (!strcasecmp ("gamerounds", token)) {
    token = strtok (0, "\n");
    opt.gamerounds = parse_unsigned (token, 0, 15);
    } else 
    if (!strcasecmp ("player_colors", token)) {
    token = strtok (0, " \t");
    opt.player_color[0] = parse_unsigned (token, 0, 4);
    token = strtok (0, " \t");
    opt.player_color[1] = parse_unsigned (token, 0, 4);
    token = strtok (0, " \t");
    opt.player_color[2] = parse_unsigned (token, 0, 4);
    token = strtok (0, " \t");
    opt.player_color[3] = parse_unsigned (token, 0, 4);
    } else { append_ignored (line); }
    } else 
    if (!strcasecmp ("extras", token)) {
    token = strtok (0, " \t.:");
    if (!strcasecmp ("mode", token)) {
    token = strtok (0, "\n");
    opt.extras = parse_unsigned (token, 0, 2);
    } else { append_ignored (line); }
    } else 
    if (!strcasecmp (keys_pref_group (), token)) {
    token = strtok (0, " \t.:");
    if (!strcasecmp ("player1_keys", token)) {
    token = strtok (0, " \t");
    opt.player_keys[0][0] = parse_unsigned (token, 0, HKEYCODE_MAX);
    token = strtok (0, " \t");
    opt.player_keys[0][1] = parse_unsigned (token, 0, HKEYCODE_MAX);
    token = strtok (0, " \t");
    opt.player_keys[0][2] = parse_unsigned (token, 0, HKEYCODE_MAX);
    token = strtok (0, " \t");
    opt.player_keys[0][3] = parse_unsigned (token, 0, HKEYCODE_MAX);
    token = strtok (0, " \t");
    opt.player_keys[0][4] = parse_unsigned (token, 0, HKEYCODE_MAX);
    token = strtok (0, " \t");
    opt.player_keys[0][5] = parse_unsigned (token, 0, HKEYCODE_MAX);
    } else 
    if (!strcasecmp ("player2_keys", token)) {
    token = strtok (0, " \t");
    opt.player_keys[1][0] = parse_unsigned (token, 0, HKEYCODE_MAX);
    token = strtok (0, " \t");
    opt.player_keys[1][1] = parse_unsigned (token, 0, HKEYCODE_MAX);
    token = strtok (0, " \t");
    opt.player_keys[1][2] = parse_unsigned (token, 0, HKEYCODE_MAX);
    token = strtok (0, " \t");
    opt.player_keys[1][3] = parse_unsigned (token, 0, HKEYCODE_MAX);
    token = strtok (0, " \t");
    opt.player_keys[1][4] = parse_unsigned (token, 0, HKEYCODE_MAX);
    token = strtok (0, " \t");
    opt.player_keys[1][5] = parse_unsigned (token, 0, HKEYCODE_MAX);
    } else { append_ignored (line); }
    } else { append_ignored (line); }
    } else { append_ignored (line); }
    free (line);
  }

  free (buf);
  fclose (fs);
  return false;
}
Example #11
0
File: cli.c Project: Godzil/sd2snes
void cmd_hexdump(void) {
  uint32_t offset = parse_unsigned(0, 16777215, 16);
  uint32_t len = parse_unsigned(0, 16777216, 16);
  sram_hexdump(offset, len);
}
Example #12
0
File: cli.c Project: Godzil/sd2snes
void cmd_setfeature(void) {
  uint8_t feat = parse_unsigned(0, 255, 16);
  fpga_set_features(feat);
}
Example #13
0
File: cli.c Project: Godzil/sd2snes
void cmd_mapper(void) {
  int32_t mapper;
  mapper = parse_unsigned(0,7,10);
  set_mapper((uint8_t)mapper & 0x7);
  printf("mapper set to %ld\n", mapper);
}
Example #14
0
File: cli.c Project: Godzil/sd2snes
static void cmd_saveraw(void) {
  uint32_t address = parse_unsigned(0,16777216,16);
  uint32_t length = parse_unsigned(0,16777216,16);
  save_sram((uint8_t*)curchar, length, address);
}
Example #15
0
File: cli.c Project: Godzil/sd2snes
static void cmd_loadraw(void) {
  uint32_t address = parse_unsigned(0,16777216,16);
  load_sram((uint8_t*)curchar, address);
}
Example #16
0
static int load_config_file(DaemonConfig *c) {
    int r = -1;
    AvahiIniFile *f;
    AvahiIniFileGroup *g;

    assert(c);

    if (!(f = avahi_ini_file_load(c->config_file ? c->config_file : AVAHI_CONFIG_FILE)))
        goto finish;

    for (g = f->groups; g; g = g->groups_next) {

        if (strcasecmp(g->name, "server") == 0) {
            AvahiIniFilePair *p;

            for (p = g->pairs; p; p = p->pairs_next) {

                if (strcasecmp(p->key, "host-name") == 0) {
                    avahi_free(c->server_config.host_name);
                    c->server_config.host_name = avahi_strdup(p->value);
                } else if (strcasecmp(p->key, "domain-name") == 0) {
                    avahi_free(c->server_config.domain_name);
                    c->server_config.domain_name = avahi_strdup(p->value);
                } else if (strcasecmp(p->key, "browse-domains") == 0) {
                    char **e, **t;

                    e = avahi_split_csv(p->value);

                    for (t = e; *t; t++) {
                        char cleaned[AVAHI_DOMAIN_NAME_MAX];

                        if (!avahi_normalize_name(*t, cleaned, sizeof(cleaned))) {
                            avahi_log_error("Invalid domain name \"%s\" for key \"%s\" in group \"%s\"\n", *t, p->key, g->name);
                            avahi_strfreev(e);
                            goto finish;
                        }

                        c->server_config.browse_domains = avahi_string_list_add(c->server_config.browse_domains, cleaned);
                    }

                    avahi_strfreev(e);

                    c->server_config.browse_domains = filter_duplicate_domains(c->server_config.browse_domains);
                } else if (strcasecmp(p->key, "use-ipv4") == 0)
                    c->server_config.use_ipv4 = is_yes(p->value);
                else if (strcasecmp(p->key, "use-ipv6") == 0)
                    c->server_config.use_ipv6 = is_yes(p->value);
                else if (strcasecmp(p->key, "check-response-ttl") == 0)
                    c->server_config.check_response_ttl = is_yes(p->value);
                else if (strcasecmp(p->key, "allow-point-to-point") == 0)
                    c->server_config.allow_point_to_point = is_yes(p->value);
                else if (strcasecmp(p->key, "use-iff-running") == 0)
                    c->server_config.use_iff_running = is_yes(p->value);
                else if (strcasecmp(p->key, "disallow-other-stacks") == 0)
                    c->server_config.disallow_other_stacks = is_yes(p->value);
                else if (strcasecmp(p->key, "host-name-from-machine-id") == 0) {
                    if (*(p->value) == 'y' || *(p->value) == 'Y') {
                        char *machine_id = get_machine_id();
                        if (machine_id != NULL) {
                            avahi_free(c->server_config.host_name);
                            c->server_config.host_name = machine_id;
                        }
                    }
                }
#ifdef HAVE_DBUS
                else if (strcasecmp(p->key, "enable-dbus") == 0) {

                    if (*(p->value) == 'w' || *(p->value) == 'W') {
                        c->fail_on_missing_dbus = 0;
                        c->enable_dbus = 1;
                    } else if (*(p->value) == 'y' || *(p->value) == 'Y') {
                        c->fail_on_missing_dbus = 1;
                        c->enable_dbus = 1;
                    } else {
                        c->enable_dbus = 0;
                    }
                }
#endif
                else if (strcasecmp(p->key, "allow-interfaces") == 0) {
                    char **e, **t;

                    avahi_string_list_free(c->server_config.allow_interfaces);
                    c->server_config.allow_interfaces = NULL;
                    e = avahi_split_csv(p->value);

                    for (t = e; *t; t++)
                        c->server_config.allow_interfaces = avahi_string_list_add(c->server_config.allow_interfaces, *t);

                    avahi_strfreev(e);
                } else if (strcasecmp(p->key, "deny-interfaces") == 0) {
                    char **e, **t;

                    avahi_string_list_free(c->server_config.deny_interfaces);
                    c->server_config.deny_interfaces = NULL;
                    e = avahi_split_csv(p->value);

                    for (t = e; *t; t++)
                        c->server_config.deny_interfaces = avahi_string_list_add(c->server_config.deny_interfaces, *t);

                    avahi_strfreev(e);
                } else if (strcasecmp(p->key, "ratelimit-interval-usec") == 0) {
                    AvahiUsec k;

                    if (parse_usec(p->value, &k) < 0) {
                        avahi_log_error("Invalid ratelimit-interval-usec setting %s", p->value);
                        goto finish;
                    }

                    c->server_config.ratelimit_interval = k;

                } else if (strcasecmp(p->key, "ratelimit-burst") == 0) {
                    unsigned k;

                    if (parse_unsigned(p->value, &k) < 0) {
                        avahi_log_error("Invalid ratelimit-burst setting %s", p->value);
                        goto finish;
                    }

                    c->server_config.ratelimit_burst = k;

                } else if (strcasecmp(p->key, "cache-entries-max") == 0) {
                    unsigned k;

                    if (parse_unsigned(p->value, &k) < 0) {
                        avahi_log_error("Invalid cache-entries-max setting %s", p->value);
                        goto finish;
                    }

                    c->server_config.n_cache_entries_max = k;
#ifdef HAVE_DBUS
                } else if (strcasecmp(p->key, "clients-max") == 0) {
                    unsigned k;

                    if (parse_unsigned(p->value, &k) < 0) {
                        avahi_log_error("Invalid clients-max setting %s", p->value);
                        goto finish;
                    }

                    c->n_clients_max = k;
                } else if (strcasecmp(p->key, "objects-per-client-max") == 0) {
                    unsigned k;

                    if (parse_unsigned(p->value, &k) < 0) {
                        avahi_log_error("Invalid objects-per-client-max setting %s", p->value);
                        goto finish;
                    }

                    c->n_objects_per_client_max = k;
                } else if (strcasecmp(p->key, "entries-per-entry-group-max") == 0) {
                    unsigned k;

                    if (parse_unsigned(p->value, &k) < 0) {
                        avahi_log_error("Invalid entries-per-entry-group-max setting %s", p->value);
                        goto finish;
                    }

                    c->n_entries_per_entry_group_max = k;
#endif
                } else {
                    avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
                    goto finish;
                }
            }

        } else if (strcasecmp(g->name, "publish") == 0) {
            AvahiIniFilePair *p;

            for (p = g->pairs; p; p = p->pairs_next) {

                if (strcasecmp(p->key, "publish-addresses") == 0)
                    c->server_config.publish_addresses = is_yes(p->value);
                else if (strcasecmp(p->key, "publish-hinfo") == 0)
                    c->server_config.publish_hinfo = is_yes(p->value);
                else if (strcasecmp(p->key, "publish-workstation") == 0)
                    c->server_config.publish_workstation = is_yes(p->value);
                else if (strcasecmp(p->key, "publish-domain") == 0)
                    c->server_config.publish_domain = is_yes(p->value);
                else if (strcasecmp(p->key, "publish-resolv-conf-dns-servers") == 0)
                    c->publish_resolv_conf = is_yes(p->value);
                else if (strcasecmp(p->key, "disable-publishing") == 0)
                    c->server_config.disable_publishing = is_yes(p->value);
                else if (strcasecmp(p->key, "disable-user-service-publishing") == 0)
                    c->disable_user_service_publishing = is_yes(p->value);
                else if (strcasecmp(p->key, "add-service-cookie") == 0)
                    c->server_config.add_service_cookie = is_yes(p->value);
                else if (strcasecmp(p->key, "publish-dns-servers") == 0) {
                    avahi_strfreev(c->publish_dns_servers);
                    c->publish_dns_servers = avahi_split_csv(p->value);
                } else if (strcasecmp(p->key, "publish-a-on-ipv6") == 0)
                    c->server_config.publish_a_on_ipv6 = is_yes(p->value);
                else if (strcasecmp(p->key, "publish-aaaa-on-ipv4") == 0)
                    c->server_config.publish_aaaa_on_ipv4 = is_yes(p->value);
                else {
                    avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
                    goto finish;
                }
            }

        } else if (strcasecmp(g->name, "wide-area") == 0) {
            AvahiIniFilePair *p;

            for (p = g->pairs; p; p = p->pairs_next) {

                if (strcasecmp(p->key, "enable-wide-area") == 0)
                    c->server_config.enable_wide_area = is_yes(p->value);
                else {
                    avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
                    goto finish;
                }
            }

        } else if (strcasecmp(g->name, "reflector") == 0) {
            AvahiIniFilePair *p;

            for (p = g->pairs; p; p = p->pairs_next) {

                if (strcasecmp(p->key, "enable-reflector") == 0)
                    c->server_config.enable_reflector = is_yes(p->value);
                else if (strcasecmp(p->key, "reflect-ipv") == 0)
                    c->server_config.reflect_ipv = is_yes(p->value);
                else {
                    avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
                    goto finish;
                }
            }

        } else if (strcasecmp(g->name, "rlimits") == 0) {
            AvahiIniFilePair *p;

            for (p = g->pairs; p; p = p->pairs_next) {

                if (strcasecmp(p->key, "rlimit-as") == 0) {
                    c->rlimit_as_set = 1;
                    c->rlimit_as = atoi(p->value);
                } else if (strcasecmp(p->key, "rlimit-core") == 0) {
                    c->rlimit_core_set = 1;
                    c->rlimit_core = atoi(p->value);
                } else if (strcasecmp(p->key, "rlimit-data") == 0) {
                    c->rlimit_data_set = 1;
                    c->rlimit_data = atoi(p->value);
                } else if (strcasecmp(p->key, "rlimit-fsize") == 0) {
                    c->rlimit_fsize_set = 1;
                    c->rlimit_fsize = atoi(p->value);
                } else if (strcasecmp(p->key, "rlimit-nofile") == 0) {
                    c->rlimit_nofile_set = 1;
                    c->rlimit_nofile = atoi(p->value);
                } else if (strcasecmp(p->key, "rlimit-stack") == 0) {
                    c->rlimit_stack_set = 1;
                    c->rlimit_stack = atoi(p->value);
                } else if (strcasecmp(p->key, "rlimit-nproc") == 0) {
#ifdef RLIMIT_NPROC
                    c->rlimit_nproc_set = 1;
                    c->rlimit_nproc = atoi(p->value);
#else
                    avahi_log_error("Ignoring configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
#endif
                } else {
                    avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
                    goto finish;
                }

            }

        } else {
            avahi_log_error("Invalid configuration file group \"%s\".\n", g->name);
            goto finish;
        }
    }

    r = 0;

finish:

    if (f)
        avahi_ini_file_free(f);

    return r;
}
Example #17
0
/*
 * Record from the audio device to a file.
 */
int
main(int argc, char **argv)
{
	int		i;
	int		cnt;
	int		err;
	int		file_type;
	int		ofd;
	int 		swapBytes = FALSE;
	double		vol;
	struct stat	st;
	struct pollfd	pfd;
	char		*cp;

	(void) setlocale(LC_ALL, "");
	(void) textdomain(TEXT_DOMAIN);

	/* Get the program name */
	prog = strrchr(argv[0], '/');
	if (prog == NULL)
		prog = argv[0];
	else
		prog++;
	Stdout = MGET("(stdout)");

	/* first check AUDIODEV environment for audio device name */
	if (cp = getenv("AUDIODEV")) {
		Audio_dev = cp;
	}

	/* Set the endian nature of the machine */
	if ((ulong_t)1 != htonl((ulong_t)1)) {
		NetEndian = FALSE;
	}

	err = 0;
	while ((i = getopt(argc, argv, prog_opts)) != EOF) {
		switch (i) {
		case 'v':
			if (parse_unsigned(optarg, &Volume, "-v")) {
				err++;
			} else if (Volume > MAX_GAIN) {
				Error(stderr, MGET("%s: invalid value for "
				"-v\n"), prog);
				err++;
			}
			break;
		case 't':
			Time = audio_str_to_secs(optarg);
			if ((Time == HUGE_VAL) || (Time < 0.)) {
				Error(stderr, MGET("%s: invalid value for "
				"-t\n"), prog);
				err++;
			}
			break;
		case 'd':
			Audio_dev = optarg;
			break;
		case 'f':
			Force = TRUE;
			break;
		case 'a':
			Append = TRUE;
			break;
		case 'i':
			Info = optarg;		/* set information string */
			Ilen = strlen(Info);
			break;
		case 's':
			if (parse_sample_rate(optarg, &Sample_rate)) {
				err++;
			}
			break;
		case 'c':
			if (strncmp(optarg, "mono", strlen(optarg)) == 0) {
				Channels = 1;
			} else if (strncmp(optarg, "stereo",
			    strlen(optarg)) == 0) {
				Channels = 2;
			} else if (parse_unsigned(optarg, &Channels, "-c")) {
				err++;
			} else if ((Channels != 1) && (Channels != 2)) {
				Error(stderr, "%s: invalid value for -c\n",
				    prog);
				err++;
			}
			break;
		case 'e':
			if (strncmp(optarg, "ulinear", strlen(optarg)) == 0) {
				Encoding = AUDIO_ENCODING_LINEAR8;
				Precision = 8;
			} else if (strncmp(optarg, "linear8",
			    strlen("linear8")) == 0) {
				Encoding = AUDIO_ENCODING_LINEAR;
				Precision = 8;
			} else if (strncmp(optarg, "ulaw",
			    strlen(optarg)) == 0) {
				Encoding = AUDIO_ENCODING_ULAW;
				Precision = 8;
			} else if (strncmp(optarg, "alaw",
			    strlen(optarg)) == 0) {
				Encoding = AUDIO_ENCODING_ALAW;
				Precision = 8;
			} else if ((strncmp(optarg, "linear",
			    strlen(optarg)) == 0) || (strncmp(optarg, "pcm",
			    strlen(optarg)) == 0)) {
				Encoding = AUDIO_ENCODING_LINEAR;
				Precision = 16;
			} else {
				Error(stderr, MGET("%s: invalid value for "
				    "-e\n"), prog);
				err++;
			}
			break;
		case 'T':
			if (strncmp(optarg, "au", strlen(optarg)) == 0) {
				File_type = FILE_AU;
			} else if (strncmp(optarg, "wav",
			    strlen(optarg)) == 0) {
				File_type = FILE_WAV;
			} else if (strncmp(optarg, "aif",
			    strlen(optarg)) == 0) {
				File_type = FILE_AIFF;
			} else if (strncmp(optarg, "aiff",
			    strlen(optarg)) == 0) {
				File_type = FILE_AIFF;
			} else {
				Error(stderr, MGET("%s: invalid value for "
				    "-T\n"), prog);
				err++;
			}
			File_type_set = TRUE;
			break;
		case '?':
			usage();
	/*NOTREACHED*/
		}
	}
	if (Append && (Info != NULL)) {
		Error(stderr, MGET("%s: cannot specify -a and -i\n"), prog);
		err++;
	}
	if (err > 0)
		exit(1);

	argc -= optind;		/* update arg pointers */
	argv += optind;

	/* Open the output file */
	if (argc <= 0) {
		Ofile = Stdout;
	} else {
		Ofile = *argv++;
		argc--;

		/* Interpret "-" filename to mean stdout */
		if (strcmp(Ofile, "-") == 0)
			Ofile = Stdout;

		/* if -T not set then we use the file suffix */
		if (File_type_set == FALSE) {
			char	*file_name;
			char	*start;

			/* get the file name without the path */
			file_name = basename(Ofile);

			/* get the true suffix */
			start = strrchr(file_name, '.');

			/* if no '.' then there's no suffix */
			if (start) {
				/* is this a .au file? */
				if (strcasecmp(start, ".au") == 0) {
					File_type = FILE_AU;
				} else if (strcasecmp(start, ".wav") == 0) {
					File_type = FILE_WAV;
				} else if (strcasecmp(start, ".aif") == 0) {
					File_type = FILE_AIFF;
				} else if (strcasecmp(start, ".aiff") == 0) {
					File_type = FILE_AIFF;
				} else {
					/* the default is .au */
					File_type = FILE_AU;
				}
			} else {
				/* no suffix, so default to .au */
				File_type = FILE_AU;
			}
		}
	}

	if (Ofile == Stdout) {
		ofd = fileno(stdout);
		Append = FALSE;
	} else {
		ofd = open(Ofile,
		    (O_RDWR | O_CREAT | (Append ? 0 : O_TRUNC)), 0666);
		if (ofd < 0) {
			Error(stderr, MGET("%s: cannot open "), prog);
			perror(Ofile);
			exit(1);
		}
		if (Append) {
			/*
			 * Check to make sure we're appending to an audio file.
			 * It must be a regular file (if zero-length, simply
			 * write it from scratch).  Also, its file header
			 * must match the input device configuration.
			 */
			if ((fstat(ofd, &st) < 0) || (!S_ISREG(st.st_mode))) {
				Error(stderr,
				    MGET("%s: %s is not a regular file\n"),
				    prog, Ofile);
				exit(1);
			}
			if (st.st_size == 0) {
				Append = FALSE;
				goto openinput;
			}

			err = audio_read_filehdr(ofd, &File_hdr, &file_type,
			    (char *)NULL, 0);

			if (err != AUDIO_SUCCESS) {
				Error(stderr,
				    MGET("%s: %s is not a valid audio file\n"),
				    prog, Ofile);
				exit(1);
			}

			/* we need to make sure file types match */
			if (File_type_set == TRUE) {
				/* specified by the command line, must match */
				if (File_type != file_type) {
					Error(stderr,
					    MGET("%s: file types must match\n"),
					    prog);
					exit(1);
				}
			} else {
				/* not specified, so force */
				File_type = file_type;
			}

			/*
			 * Set the format state to the format
			 * in the file header.
			 */
			Sample_rate = File_hdr.sample_rate;
			Channels = File_hdr.channels;
			Encoding = File_hdr.encoding;
			Precision = File_hdr.bytes_per_unit * 8;

			/* make sure we support the encoding method */
			switch (Encoding) {
				case AUDIO_ENCODING_LINEAR8:
				case AUDIO_ENCODING_ULAW:
				case AUDIO_ENCODING_ALAW:
				case AUDIO_ENCODING_LINEAR:
					break;
				default: {
					char	msg[AUDIO_MAX_ENCODE_INFO];
					(void) audio_enc_to_str(&File_hdr, msg);
					Error(stderr,
					    MGET("%s: Append is not supported "
					    "for "), prog);
					Error(stderr,
					    MGET("this file encoding:\n\t"
					    "[%s]\n"), msg);
					exit(1);
					}
			}

			/* Get the current size, if possible */
			Oldsize = File_hdr.data_size;
			if ((Oldsize == AUDIO_UNKNOWN_SIZE) &&
			    ((err = (int)lseek(ofd, 0L, SEEK_CUR)) >= 0)) {
				if (err < 0) {
					Error(stderr,
					    MGET("%s: %s is not a valid audio "
					    "file\n"), prog, Ofile);
					exit(1);
				}
				Oldsize = st.st_size - err;
			}
			/* Seek to end to start append */
			if ((int)lseek(ofd, st.st_size, SEEK_SET) < 0) {
				Error(stderr,
				    MGET("%s: cannot find end of %s\n"),
				    prog, Ofile);
				exit(1);
			}
		}
	}
openinput:
	/* Validate and open the audio device */
	err = stat(Audio_dev, &st);
	if (err < 0) {
		Error(stderr, MGET("%s: cannot open "), prog);
		perror(Audio_dev);
		exit(1);
	}
	if (!S_ISCHR(st.st_mode)) {
		Error(stderr, MGET("%s: %s is not an audio device\n"), prog,
		    Audio_dev);
		exit(1);
	}

	/*
	 * For the mixer environment we need to open the audio device before
	 * the control device. If successful we pause right away to keep
	 * from queueing up a bunch of useless data.
	 */
	Audio_fd = open(Audio_dev, O_RDONLY | O_NONBLOCK);
	if (Audio_fd < 0) {
		if (errno == EBUSY) {
			Error(stderr, MGET("%s: %s is busy\n"),
			    prog, Audio_dev);
		} else {
			Error(stderr, MGET("%s: error opening "), prog);
			perror(Audio_dev);
		}
		exit(1);
	}
	if (audio_pause_record(Audio_fd) != AUDIO_SUCCESS) {
		Error(stderr, MGET("%s: not able to pause recording\n"), prog);
		exit(1);
	}

	/* get the current settings */
	if (audio_get_record_config(Audio_fd, &Save_hdr) != AUDIO_SUCCESS) {
		(void) close(Audio_fd);
		Error(stderr, MGET("%s: %s is not an audio device\n"),
		    prog, Audio_dev);
		exit(1);
	}
	/* make a copy into the working data structure */
	bcopy(&Save_hdr, &Dev_hdr, sizeof (Save_hdr));

	/* flush any queued audio data */
	if (audio_flush_record(Audio_fd) != AUDIO_SUCCESS) {
		Error(stderr, MGET("%s: not able to flush recording\n"), prog);
		exit(1);
	}

	if (Sample_rate != 0) {
		Dev_hdr.sample_rate = Sample_rate;
	}
	if (Channels != 0) {
		Dev_hdr.channels = Channels;
	}
	if (Precision != 0) {
		Dev_hdr.bytes_per_unit = Precision / 8;
	}
	if (Encoding != 0) {
		Dev_hdr.encoding = Encoding;
	}

	/*
	 * For .wav we always record 8-bit linear as unsigned. Thus we
	 * force unsigned linear to make life a lot easier on the user.
	 *
	 * For .aiff we set the default to 8-bit signed linear, not
	 * u-law, if Encoding isn't already set.
	 */
	if (File_type == FILE_WAV &&
	    Dev_hdr.encoding == AUDIO_ENCODING_LINEAR &&
	    Dev_hdr.bytes_per_unit == 1) {
		/* force to unsigned */
		Dev_hdr.encoding = AUDIO_ENCODING_LINEAR8;
	} else if (File_type == FILE_AIFF && Encoding == 0) {
		Dev_hdr.encoding = AUDIO_ENCODING_LINEAR;
		if (Precision == 0) {
			Dev_hdr.bytes_per_unit = AUDIO_PRECISION_8 / 8;
		}
	}

	if (audio_set_record_config(Audio_fd, &Dev_hdr) != AUDIO_SUCCESS) {
		Error(stderr, MGET(
		    "%s: Audio format not supported by the audio device\n"),
		    prog);
		exit(1);
	}

	if (audio_resume_record(Audio_fd) != AUDIO_SUCCESS) {
		Error(stderr, MGET("%s: not able to resume recording\n"), prog);
		exit(1);
	}

	/* If appending to an existing file, check the configuration */
	if (Append) {
		char	msg[AUDIO_MAX_ENCODE_INFO];

		switch (audio_cmp_hdr(&Dev_hdr, &File_hdr)) {
		case 0:			/* configuration matches */
			break;
		case 1:			/* all but sample rate matches */
			if (Force) {
				Error(stderr, MGET("%s: WARNING: appending "
				    "%.3fkHz data to %s (%.3fkHz)\n"), prog,
				    ((double)Dev_hdr.sample_rate / 1000.),
				    Ofile,
				    ((double)File_hdr.sample_rate / 1000.));
				break;
			}		/* if not -f, fall through */

		default:		/* encoding mismatch */
			(void) audio_enc_to_str(&Dev_hdr, msg);
			Error(stderr,
			    MGET("%s: device encoding [%s]\n"), prog, msg);
			(void) audio_enc_to_str(&File_hdr, msg);
			Error(stderr,
			    MGET("\tdoes not match file encoding [%s]\n"), msg);
			exit(1);
		}
	} else if (!isatty(ofd)) {
		if (audio_write_filehdr(ofd, &Dev_hdr, File_type, Info,
		    Ilen) != AUDIO_SUCCESS) {
			Error(stderr,
			    MGET("%s: error writing header for %s\n"), prog,
			    Ofile);
			exit(1);
		}
	}

	/*
	 * 8-bit audio isn't a problem, however 16-bit audio is. If the file
	 * is an endian that is different from the machine then the bytes
	 * will need to be swapped.
	 *
	 * Note: The following if() could be simplified, but then it gets
	 * to be very hard to read. So it's left as is.
	 */
	if (Dev_hdr.bytes_per_unit == 2 &&
	    ((!NetEndian && File_type == FILE_AIFF) ||
	    (!NetEndian && File_type == FILE_AU) ||
	    (NetEndian && File_type == FILE_WAV))) {
		swapBytes = TRUE;
	}

	/* If -v flag, set the record volume now */
	if (Volume != INT_MAX) {
		vol = (double)Volume / (double)MAX_GAIN;
		(void) audio_get_record_gain(Audio_fd, &Savevol);
		err = audio_set_record_gain(Audio_fd, &vol);
		if (err != AUDIO_SUCCESS) {
			Error(stderr,
			    MGET("%s: could not set record volume for %s\n"),
			    prog, Audio_dev);
			exit(1);
		}
	}

	if (isatty(ofd)) {
		Error(stderr, MGET("%s: No files and stdout is a tty\n"),
		    prog);
		exit(1);
	}

	/* Set up SIGINT handler so that final buffers may be flushed */
	(void) signal(SIGINT, sigint);

	/*
	 * At this point, we're (finally) ready to copy the data.
	 * Init a poll() structure, to use when there's nothing to read.
	 */
	if (Time > 0)
		Limit = audio_secs_to_bytes(&Dev_hdr, Time);
	pfd.fd = Audio_fd;
	pfd.events = POLLIN;
	while ((Limit == AUDIO_UNKNOWN_SIZE) || (Limit != 0)) {
		/* Fill the buffer or read to the time limit */
		cnt = read(Audio_fd, (char *)buf,
		    ((Limit != AUDIO_UNKNOWN_SIZE) && (Limit < sizeof (buf)) ?
		    (int)Limit : sizeof (buf)));

		if (cnt == 0)		/* normally, eof can't happen */
			break;

		/* If error, probably have to wait for input */
		if (cnt < 0) {
			if (Cleanup)
				break;		/* done if ^C seen */
			switch (errno) {
			case EAGAIN:
				(void) poll(&pfd, 1L, -1);
				break;
			case EOVERFLOW:  /* Possibly a Large File */
				Error(stderr, MGET("%s: error reading"), prog);
				perror("Large File");
				exit(1);
			default:
				Error(stderr, MGET("%s: error reading"), prog);
				perror(Audio_dev);
				exit(1);
			}
			continue;
		}

		/* Swab the output if required. */
		if (swapBytes) {
			swab((char *)buf, swapBuf, cnt);
			err = write(ofd, swapBuf, cnt);
		} else {
			err = write(ofd, (char *)buf, cnt);
		}
		if (err < 0) {
			Error(stderr, MGET("%s: error writing "), prog);
			perror(Ofile);
			exit(1);
		}
		if (err != cnt) {
			Error(stderr, MGET("%s: error writing "), prog);
			perror(Ofile);
			break;
		}
		Size += cnt;
		if (Limit != AUDIO_UNKNOWN_SIZE)
			Limit -= cnt;
	}

	/* Attempt to rewrite the data_size field of the file header */
	if (!Append || (Oldsize != AUDIO_UNKNOWN_SIZE)) {
		if (Append)
			Size += Oldsize;
		(void) audio_rewrite_filesize(ofd, File_type, Size,
		    Dev_hdr.channels, Dev_hdr.bytes_per_unit);
	}

	(void) close(ofd);			/* close input file */


	/* Check for error during record */
	if (audio_get_record_error(Audio_fd, (unsigned *)&err) != AUDIO_SUCCESS)
		Error(stderr, MGET("%s: error reading device status\n"), prog);
	else if (err)
		Error(stderr, MGET("%s: WARNING: Data overflow occurred\n"),
		    prog);

	/* Reset record volume, encoding */
	if (Volume != INT_MAX)
		(void) audio_set_record_gain(Audio_fd, &Savevol);
	if (audio_cmp_hdr(&Save_hdr, &Dev_hdr) != 0) {
		(void) audio_set_record_config(Audio_fd, &Save_hdr);
	}
	(void) close(Audio_fd);
	return (0);
}
Example #18
0
File: cli.c Project: Godzil/sd2snes
void cmd_w8(void) {
  uint32_t offset = parse_unsigned(0, 16777215, 16);
  uint8_t val = parse_unsigned(0, 255, 16);
  sram_writebyte(val, offset);
}