Esempio n. 1
0
/* for now this function can only unexpand strings with tables that contain only
single character strings like "\n", "\t" etc. */
gchar *unexpand_string(const gchar *original, const char specialchar, Tconvert_table *table) {
	gchar *tmp, *tosearchfor, *retval, *prev, *dest, *orig;
	Tconvert_table *tmpentry;
	
	orig = g_strdup(original);
	DEBUG_MSG("original='%s', strlen()=%d\n",original,strlen(original));
	tosearchfor = g_malloc(tablesize(table)+1);
	DEBUG_MSG("tablesize(table)=%d, alloc'ed %d bytes for tosearchfor\n",tablesize(table), tablesize(table)+1);
	tmp = tosearchfor;
	tmpentry = table;
	while(tmpentry->my_char != NULL) {
		*tmp = tmpentry->my_char[0]; /* we fill the search string with the first character */
		tmpentry++;
		tmp++;
	}
	*tmp = '\0';
	DEBUG_MSG("unexpand_string, tosearchfor='%s'\n",tosearchfor);
	DEBUG_MSG("alloc'ing %d bytes\n", (countchars(original, tosearchfor) + strlen(original) + 1));
	retval = g_malloc((countchars(original, tosearchfor) + strlen(original) + 1) * sizeof(gchar));
	dest = retval;
	prev = orig;
	/* now we go trough the original till we hit specialchar */
	tmp = strpbrk(prev, tosearchfor);
	while (tmp) {
		gint len = tmp - prev;
		gint mychar = table_convert_char2int(table, tmp, tcc2i_firstchar);
		DEBUG_MSG("unexpand_string, tmp='%s', prev='%s'\n",tmp, prev);
		if (mychar == -1) mychar = *tmp;
		DEBUG_MSG("unexpand_string, copy %d bytes and advancing dest\n",len);
		memcpy(dest, prev, len);
		dest += len;
		*dest = specialchar;
		dest++;
		*dest = mychar;
		dest++;
		prev=tmp+1;
		DEBUG_MSG("prev now is '%s'\n",prev);
		tmp = strpbrk(prev, tosearchfor);
	}
	DEBUG_MSG("unexpand_string, copy the rest (%s) to dest\n",prev);
	memcpy(dest,prev,strlen(prev)+1); /* this will also make sure there is a \0 at the end */
	DEBUG_MSG("unexpand_string, retval='%s'\n",retval);
	g_free(orig);
	g_free(tosearchfor);
	return retval;
}
Esempio n. 2
0
int main(int argc, char **argv)
{
	int opt, numcolon;
	int totsec, seconds, minutes, hours;
	fdata sfd;
	time_t now;
	char *user, *start, *end, *prog2run, *mov2show, *timespec;
	char userbuf[FILENAME_MAX];
	char *fmt = "/home/%s/.config/alarm/alarm.txt";
	int cookingmode;
	int ampm = 0;
	// defaults
	cookingmode = 1;
	while((opt = getopt(argc, argv, ":ha")) != -1) {
		switch(opt) {
		case 'h':
			dohelp(0);
		break;
		case 'a':
			// absolute time of day ie alarm clock mode;
			cookingmode = 0;
		break;
		case ':':
			fprintf(stderr, "Option %c requires an argument\n",optopt);
			dohelp(1);
		break;
		case '?':
			fprintf(stderr, "Unknown option: %c\n",optopt);
			dohelp(1);
		break;
		} //switch()
	}//while()
	// now process the non-option arguments

	now = time(NULL);

	// 1.Check that argv[???] exists.
	if (!(argv[optind])) {
		fprintf(stderr, "No time specifier provided.\n");
		dohelp(1);
	}
	timespec = argv[optind];
	optind++;
	// go look for am or pm
	if (argv[optind]) {
		char *cp = argv[optind];
		if (*cp == 'P' || *cp == 'p') ampm = 1;
		// and it just doesn't matter if there is a following M|m or not
	}
	// Read my config file
	user = getenv("USER");
	sprintf(userbuf,  fmt, user);
	sfd = readfile(userbuf, 0, 0);
	if(sfd.from) {
		start = sfd.from;
		end = sfd.to;
	} else {
		firstrun("alarm", "alarm.txt");
		user = getenv("HOME");
		char advice[NAME_MAX];
		sprintf(advice, "Installed alarm.txt at %s.config/alarm/\n",
				user);
		fputs(advice, stderr);
		fputs("Please edit that file to suit your needs.\n", stderr);
		exit(EXIT_SUCCESS);
	}

	prog2run = getactdata(start, end, "program");
	mov2show = getactdata(start, end, "data");

	// now deal with the users timespec
	numcolon = countchars(timespec, ':');
	hours = minutes = seconds = 0;
	char *wstr = strdup(timespec);
	char *tofree = wstr;	// wstr gets altered.
	switch(numcolon) {
		char *cp;
		case 0:
		// the number is minutes;
		if (strlen(wstr)) minutes = strtol(wstr, NULL, 10);
		break;
		case 1:
		// have mm:ss or :ss or mm:
		cp = strchr(wstr, ':');
		*cp = '\0';
		if (strlen(wstr)) minutes = strtol(wstr, NULL, 10);
		cp++; // look at the second half
		wstr = cp;
		if (strlen(wstr)) seconds = strtol(wstr, NULL, 10);
		break;
		case 2:
		// have hh:mm:ss or hh:mm:
		cp = strchr(wstr, ':');
		*cp = '\0';
		if (strlen(wstr)) hours = strtol(wstr, NULL, 10);
		cp++; // next part
		wstr = cp;
		cp = strchr(wstr, ':');
		*cp = '\0';
		if (strlen(wstr)) minutes = strtol(wstr, NULL, 10);
		cp++;
		wstr = cp;
		if (strlen(wstr)) seconds = strtol(wstr, NULL, 10);
		break;
		default:
		fprintf(stderr,
				"Badly formed time specification: %s\n", timespec);
		exit(EXIT_FAILURE);
		break;
	}
	free(tofree);
	// total seconds to elapse before alarm
	if (ampm) {
		if (!(cookingmode)) {
			if (hours < 12) hours += 12;
		}
	}
	if(!(cookingmode)) { // used as an alarm clock, not cooking timer
		totsec = addclocktime(now, hours, minutes, seconds);
	} else {
		totsec = seconds + 60 * minutes + 3600 * hours;
	}
	printf("Total seconds: %d\n", totsec);
	// report every 60 seconds
	int aminute = 60;	// seconds
	while (aminute < totsec) {
		sleep(aminute);
		totsec -= aminute;
		if (cookingmode) {
			fprintf(stdout, "Remaining: %d seconds.\n", totsec);
		}
	}
	sleep(totsec);
	runit(prog2run, mov2show);
	return 0;
}//main()