示例#1
0
static bool
sendMailSMTP(const struct MACCOUNT *account, const char *reply,
	     const char **recipients, const char *message)
{
	CURLcode res = CURLE_OK;
	bool smtp_success = false;
	char *smtp_url = buildSMTPURL(account);
	struct curl_slist *recipient_slist = buildRecipientSList(recipients);
	struct smtp_upload upload = {
		.data = message,.length = strlen(message),.pos = 0
	};
	CURL *handle =
	    newSendmailHandle(account, smtp_url, reply, recipient_slist);

	if (!handle)
		goto smtp_cleanup;

	curl_easy_setopt(handle, CURLOPT_READFUNCTION, smtp_upload_callback);
	curl_easy_setopt(handle, CURLOPT_READDATA, &upload);
	curl_easy_setopt(handle, CURLOPT_UPLOAD, 1L);

	res = curl_easy_perform(handle);
	if (res == CURLE_OK)
		smtp_success = true;

smtp_cleanup:
	if (res != CURLE_OK)
		ebcurl_setError(res, smtp_url);
	if (handle)
		curl_easy_cleanup(handle);
	curl_slist_free_all(recipient_slist);
	nzFree(smtp_url);
	return smtp_success;
}				/* sendMailSMTP */

/* Send mail to the smtp server. */
bool
sendMail(int account, const char **recipients, const char *body,
	 int subjat, const char **attachments, const char *refline,
	 int nalt, bool dosig)
{
	char *from, *fromiso, *reply, *login, *smlogin, *pass;
	const struct MACCOUNT *a, *ao, *localMail;
	const char *s, *boundary;
	char reccc[MAXRECAT];
	char *t;
	int nat, cx, i, j;
	char *out = 0;
	bool sendmail_success = false;
	bool mustmime = false;
	bool firstgreet = true;
	bool firstrec;
	const char *ct, *ce;
	char *encoded = 0;

	if (!validAccount(account))
		return false;
	mailAccount = account;
	localMail = accounts + localAccount - 1;

	a = accounts + account - 1;
	from = a->from;
	reply = a->reply;
	ao = a->outssl ? a : localMail;
	doSignature = dosig;

	nat = 0;		/* number of attachments */
	if (attachments) {
		while (attachments[nat])
			++nat;
	}
	if (nat)
		mustmime = true;
	if (nalt && nalt < nat) {
		setError(MSG_AttAlternate);
		return false;
	}

	if (!loadAddressBook())
		return false;

/* set copy flags */
	for (j = 0; s = recipients[j]; ++j) {
		char cc = 0;
		if (*s == '^' || *s == '?')
			cc = *s++;
		if (j == MAXRECAT) {
			setError(MSG_RecipMany, MAXRECAT);
			return false;
		}
		recipients[j] = s;
		reccc[j] = cc;
	}

/* Look up aliases in the address book */
	for (j = 0; s = recipients[j]; ++j) {
		if (strchr(s, '@'))
			continue;
		t = 0;
		for (i = 0; i < nads; ++i) {
			const char *a = addressList[i].name;
			if (*a == '-' || *a == '!')
				++a;
			if (!stringEqual(s, a))
				continue;
			t = addressList[i].email;
			debugPrint(3, " %s becomes %s", s, t);
			break;
		}
		if (t) {
			recipients[j] = t;
			continue;
		}
		if (!addressFile) {
			setError(MSG_ABMissing);
			return false;
		}
		setError(MSG_ABNoAlias2, s);
		return false;
	}			/* recipients */

	if (!j) {
		setError(MSG_RecipNone);
		return false;
	}

/* verify attachments are readable */
	for (j = 0; s = attachments[j]; ++j) {
		if (!ismc && (cx = stringIsNum(s)) >= 0) {
			if (!cxCompare(cx) || !cxActive(cx))
				return false;
			if (!sessionList[cx].lw->dol) {
				setError(MSG_AttSessionEmpty, cx);
				return false;
			}
		} else {
			char ftype = fileTypeByName(s, false);
			if (!ftype) {
				setError(MSG_AttAccess, s);
				return false;
			}
			if (ftype != 'f') {
				setError(MSG_AttRegular, s);
				return false;
			}
			if (!fileSizeByName(s)) {
				setError(MSG_AttEmpty2, s);
				return false;
			}
		}
	}			/* loop over attachments */

	if (!encodeAttachment(body, subjat, false, &ct, &ce, &encoded))
		return false;
	if (ce[0] == 'q')
		mustmime = true;

	boundary = makeBoundary();

/* Build the outgoing mail, as one string. */
	out = initString(&j);

	firstrec = true;
	for (i = 0; s = recipients[i]; ++i) {
		if (reccc[i])
			continue;
		stringAndString(&out, &j, firstrec ? "To:" : ",\r\n  ");
		stringAndString(&out, &j, s);
		firstrec = false;
	}
	if (!firstrec)
		stringAndString(&out, &j, eol);

	firstrec = true;
	for (i = 0; s = recipients[i]; ++i) {
		if (reccc[i] != '^')
			continue;
		stringAndString(&out, &j, firstrec ? "CC:" : ",\r\n  ");
		stringAndString(&out, &j, s);
		firstrec = false;
	}
	if (!firstrec)
		stringAndString(&out, &j, eol);

	firstrec = true;
	for (i = 0; s = recipients[i]; ++i) {
		if (reccc[i] != '?')
			continue;
		stringAndString(&out, &j, firstrec ? "BCC:" : ",\r\n  ");
		stringAndString(&out, &j, s);
		firstrec = false;
	}
	if (!firstrec)
		stringAndString(&out, &j, eol);

	fromiso = isoEncode(from, from + strlen(from));
	if (!fromiso)
		fromiso = from;
	sprintf(serverLine, "From: %s <%s>%s", fromiso, reply, eol);
	stringAndString(&out, &j, serverLine);
	sprintf(serverLine, "Reply-to: %s <%s>%s", fromiso, reply, eol);
	stringAndString(&out, &j, serverLine);
	if (fromiso != from)
		nzFree(fromiso);
	if (refline) {
		s = strchr(refline, '\n');
		if (!s)		/* should never happen */
			s = refline + strlen(refline);
		stringAndBytes(&out, &j, refline, s - refline);
		stringAndString(&out, &j, eol);
	}
	sprintf(serverLine, "User-Agent: %s%s", currentAgent, eol);
	stringAndString(&out, &j, serverLine);
	if (subjectLine[0]) {
		sprintf(serverLine, "Subject: %s%s", subjectLine, eol);
		stringAndString(&out, &j, serverLine);
	}
	sprintf(serverLine,
		"Date: %s%sMessage-ID: <%s.%s>%sMime-Version: 1.0%s",
		mailTimeString(), eol, messageTimeID(), reply, eol, eol);
	stringAndString(&out, &j, serverLine);

	if (!mustmime) {
/* no mime components required, we can just send the mail. */
		sprintf(serverLine,
			"Content-Type: %s%s%sContent-Transfer-Encoding: %s%s%s",
			ct, charsetString(ct, ce), eol, ce, eol, eol);
		stringAndString(&out, &j, serverLine);
	} else {
		sprintf(serverLine,
			"Content-Type: multipart/%s; boundary=%s%sContent-Transfer-Encoding: 7bit%s%s",
			nalt ? "alternative" : "mixed", boundary, eol, eol,
			eol);
		stringAndString(&out, &j, serverLine);
		stringAndString(&out, &j,
				"This message is in MIME format. Since your mail reader does not understand\r\n\
this format, some or all of this message may not be legible.\r\n\r\n--");
		stringAndString(&out, &j, boundary);
		sprintf(serverLine,
			"%sContent-Type: %s%s%sContent-Transfer-Encoding: %s%s%s",
			eol, ct, charsetString(ct, ce), eol, ce, eol, eol);
		stringAndString(&out, &j, serverLine);
	}

/* Now send the body, line by line. */
	appendAttachment(encoded, &out, &j);
	nzFree(encoded);
	encoded = 0;

	if (mustmime) {
		for (i = 0; s = attachments[i]; ++i) {
			if (!encodeAttachment
			    (s, 0, false, &ct, &ce, &encoded))
				return false;
			sprintf(serverLine, "%s--%s%sContent-Type: %s%s", eol,
				boundary, eol, ct, charsetString(ct, ce));
			stringAndString(&out, &j, serverLine);
/* If the filename has a quote in it, forget it. */
/* Also, suppress filename if this is an alternate presentation. */
			if (!nalt && !strchr(s, '"')
			    && (ismc || stringIsNum(s) < 0)) {
				sprintf(serverLine, "; name=\"%s\"", s);
				stringAndString(&out, &j, serverLine);
			}
			sprintf(serverLine,
				"%sContent-Transfer-Encoding: %s%s%s", eol, ce,
				eol, eol);
			stringAndString(&out, &j, serverLine);
			appendAttachment(encoded, &out, &j);
			nzFree(encoded);
			encoded = 0;
		}		/* loop over attachments */

/* The last boundary */
		sprintf(serverLine, "%s--%s--%s", eol, boundary, eol);
		stringAndString(&out, &j, serverLine);
	}

	/* mime format */

	sendmail_success = sendMailSMTP(ao, reply, recipients, out);
	nzFree(out);
	return sendmail_success;
}				/* sendMail */
示例#2
0
/* Return 0 for ok, 1 to delete the mail, -1 to stop.
 * stop is only meaningful for imap. */
static int presentMail(void)
{
	int j, k;
	const char *redirect = NULL;	/* send mail elsewhere */
	char key = 0;
	const char *atname = NULL;	/* name of file or attachment */
	bool delflag = false;	/* delete this mail */
	bool scanat = false;	/* scan for attachments */
	int displine;
	int stashNumber = -1;

/* clear things out from the last message */
	if (lastMailInfo)
		freeMailInfo(lastMailInfo);
	lastMailInfo = 0;

	if (sessionList[1].lw)
		cxQuit(1, 2);
	cs = 0;
	cxSwitch(1, false);

	iuReformat(mailstring, mailstring_l, &mailu8, &mailu8_l);
	if (mailu8) {
		if (!addTextToBuffer((pst) mailu8, mailu8_l, 0, false))
			showErrorAbort();
	} else {
		if (!addTextToBuffer((pst) mailstring, mailstring_l, 0, false))
			showErrorAbort();
	}

	browseCurrentBuffer();

	if (!passMail) {
		redirect = mailRedirect(lastMailInfo->to,
					lastMailInfo->from,
					lastMailInfo->reply,
					lastMailInfo->subject);
	}

	if (redirect) {
		if (!isimap) {
			delflag = true;
			key = 'w';
			if (*redirect == '-')
				++redirect, key = 'u';
			if (stringEqual(redirect, "x"))
				i_puts(MSG_Junk);
			else
				printf("> %s\n", redirect);
		} else {
			if (*redirect == '-')
				++redirect;
			if (stringEqual(redirect, "x"))
				redirect = NULL;
		}
	}

/* display the next page of mail and get a command from the keyboard */
	displine = 1;
paging:
	if (!delflag) {		/* show next page */
		if (displine <= cw->dol) {
			for (j = 0; j < 20 && displine <= cw->dol;
			     ++j, ++displine) {
				char *showline = (char *)fetchLine(displine, 1);
				k = pstLength((pst) showline);
				showline[--k] = 0;
				printf("%s\n", showline);
				nzFree(showline);
			}
		}
	}

/* get key command from user */
key_command:
	if (delflag)
		goto writeMail;

/* interactive prompt depends on whether there is more text or not */
	printf("%c ", displine > cw->dol ? '?' : '*');
	fflush(stdout);
	key = getLetter((isimap ? "q? nwWuUasd" : "q? nwud"));
	printf("\b\b\b");
	fflush(stdout);

	switch (key) {
	case 'q':
		i_puts(MSG_Quit);
		exit(0);

	case 'n':
		i_puts(MSG_Next);
		goto afterinput;

	case 's':
		i_puts(MSG_Stop);
		goto afterinput;

	case 'd':
		i_puts(MSG_Delete);
		delflag = true;
		goto afterinput;

	case ' ':
		if (displine > cw->dol)
			i_puts(MSG_EndMessage);
		goto paging;

	case '?':
		i_puts(isimap ? MSG_ImapReadHelp : MSG_MailHelp);
		goto key_command;

	case 'a':
		key = 'w';	/* this will scan attachments */
		scanat = true;

	case 'w':
	case 'W':
	case 'u':
	case 'U':
		break;

	default:
		i_puts(MSG_NYI);
		goto key_command;
	}			/* switch */

/* At this point we're saving the mail somewhere. */
writeMail:
	if (!isimap || isupper(key))
		delflag = true;
	atname = 0;
	if (!isimap)
		atname = redirect;

	if (scanat)
		goto attachOnly;

saveMail:
	if (!atname)
		atname = getFileName(MSG_FileName, redirect, false, false);
	if (stringEqual(atname, "x"))
		goto afterinput;

	char exists = fileTypeByName(atname, false);
	int fsize;		/* file size */
	int fh = open(atname, O_WRONLY | O_TEXT | O_CREAT | O_APPEND, 0666);
	if (fh < 0) {
		i_printf(MSG_NoCreate, atname);
		goto saveMail;
	}
	if (exists)
		write(fh,
		      "======================================================================\n",
		      71);
	if (key == 'u') {
		if (write(fh, mailstring, mailstring_l) < mailstring_l) {
badsave:
			i_printf(MSG_NoWrite, atname);
			close(fh);
			goto saveMail;
		}
		close(fh);
		fsize = mailstring_l;
	} else {

/* key = w, write the file - if pop then save the original unformatted */
		if (!isimap && mailStash) {
			char *rmf;	/* raw mail file */
			int rmfh;	/* file handle to same */
/* I want a fairly easy filename, in case I want to go look at the original.
* Not a 30 character message ID that I am forced to cut&paste.
* 4 or 5 digits would be nice.
* So the filename looks like /home/foo/.Trash/rawmail/36921
* I pick the digits randomly.
* Please don't accumulate 100,000 emails before you empty your trash.
* It's good to have a cron job empty the trash early Sunday morning.
*/

			k = strlen(mailStash);
			rmf = allocMem(k + 12);
/* Try 20 times, then give up. */
			for (j = 0; j < 20; ++j) {
				int rn = rand() % 100000;	/* random number */
				sprintf(rmf, "%s/%05d", mailStash, rn);
				if (fileTypeByName(rmf, false))
					continue;
/* dump the original mail into the file */
				rmfh =
				    open(rmf,
					 O_WRONLY | O_TEXT | O_CREAT | O_APPEND,
					 0666);
				if (rmfh < 0)
					break;
				if (write(rmfh, mailstring, mailstring_l) <
				    mailstring_l) {
					close(rmfh);
					unlink(rmf);
					break;
				}
				close(rmfh);
/* written successfully, remember the stash number */
				stashNumber = rn;
				break;
			}
		}

		fsize = 0;
		for (j = 1; j <= cw->dol; ++j) {
			char *showline = (char *)fetchLine(j,
							   1);
			int len = pstLength((pst)
					    showline);
			if (write(fh, showline, len) < len)
				goto badsave;
			nzFree(showline);
			fsize += len;
		}		/* loop over lines */

		if (stashNumber >= 0) {
			char addstash[60];
			sprintf(addstash, "\nUnformatted %05d\n", stashNumber);
			k = strlen(addstash);
			if (write(fh, addstash, k) < k)
				goto badsave;
			fsize += k;
		}

		close(fh);

attachOnly:

		if (nattach)
			writeAttachments(lastMailInfo);
		else if (scanat)
			i_puts(MSG_NoAttachments);
	}			/* unformat or format */

	if (scanat)
		goto afterinput;
/* print "mail saved" message */
	i_printf(MSG_MailSaved, fsize);
	if (exists)
		i_printf(MSG_Appended);
	nl();

afterinput:
	nzFree(mailstring);
	mailstring = 0;
	nzFree(mailu8);
	mailu8 = 0;

	if (delflag)
		return 1;
	if (key == 's')
		return -1;
	return 0;
}				/* presentMail */
示例#3
0
void auto_mount::run()
{
	/*
	 * Not exactly sure what i am doing here but this kind of thing seem to be necessary to prevent
	 * an occassional crash on exit with an error that reads something like "object deleted while thread is still running"
	 */
	m_mtoto = static_cast< QThread * >( this ) ;
	connect( m_mtoto,SIGNAL( terminated() ),m_main,SLOT( threadStopped() ) ) ;
	connect( m_mtoto,SIGNAL( terminated() ),m_mtoto,SLOT( deleteLater() ) ) ;
	connect( m_mtoto,SIGNAL( terminated() ),this,SLOT( deleteLater() ) ) ;

	#define BUFF_SIZE 4096
	char buffer[ BUFF_SIZE ];

	m_fdDir = inotify_init() ;
	if( m_fdDir == -1 ){
		qDebug() << "inotify_init() failed to start,automounting is turned off";
		m_threadIsRunning = false ;
		return ;
	}else{
		m_threadIsRunning = true ;
	}

	int dev    = inotify_add_watch( m_fdDir,"/dev",IN_CREATE|IN_DELETE ) ;
	int mapper = inotify_add_watch( m_fdDir,"/dev/mapper",IN_CREATE|IN_DELETE ) ;
	int md = -1 ;

	QDir d( QString( "/dev/dm" ) ) ;
	if( d.exists() ){
		md = inotify_add_watch( m_fdDir,"/dev/md",IN_DELETE ) ;
	}

	struct inotify_event * pevent ;
	QString device ;

	const char * f ;
	const char * z ;
	int data_read ;
	int baseSize = sizeof( struct inotify_event ) ;

	while( 1 ) {

		data_read = read( m_fdDir,buffer,BUFF_SIZE ) ;

		z = buffer + data_read ;

		for( f = buffer ; f < z ; f = f + baseSize + pevent->len ){

			pevent = ( struct inotify_event * )f;

			m_device = f + baseSize ;

			#define stringPrefixMatch( x,y,z ) strncmp( x,y,z ) == 0
			#define stringEqual( x,y ) strcmp( x,y ) == 0
			#define stringHasComponent( x,y ) strstr( x,y ) != NULL

			if(     stringPrefixMatch( m_device,"sg",2 ) ||
				stringPrefixMatch( m_device,"dm-",3 ) ||
				stringHasComponent( m_device,".dev/tmp" ) ||
				stringHasComponent( m_device,".tmp.md." ) ||
				stringHasComponent( m_device,"md/md-device-map" ) ){
				/*
				 * dont care about these devices.
				 * /dev/sgX seem to be created when a usb device is plugged in
				 * /dev/dm-X are dm devices we dont care about since we will be dealing with them differently
				 */
				 ;
			}else{
				if( pevent->wd == dev && pevent->mask & IN_CREATE ){
					/*
					 * /dev/md path seem to be deleted when the last entry in it is removed and
					 * created before the first entry is added.To account for this,monitor for the
					 * folder created to start monitoring its contents if it get created after we have started
					 */
					if( stringEqual( "md",m_device ) ){
						md = inotify_add_watch( m_fdDir,"/dev/md",IN_DELETE ) ;
						continue ;
					}
				}

				if( pevent->wd == dev && pevent->mask & IN_DELETE ){
					if( stringEqual( "md",m_device ) ){
						inotify_rm_watch( md,dev );
						continue ;
					}
				}

				device = QString( m_device );
				m_thread_helper = new auto_mount_helper() ;

				connect( m_thread_helper,SIGNAL( getVolumeSystemInfo( QStringList ) ),
					 m_babu,SLOT( autoMountVolumeSystemInfo( QStringList ) ) ) ;
				connect( m_thread_helper,SIGNAL( getVolumeInfo( QStringList ) ),
					 m_babu,SLOT( autoMountVolumeInfo( QStringList ) ) ) ;
				connect( m_thread_helper,SIGNAL( deviceRemoved( QString ) ),
					 m_babu,SLOT( deviceRemoved( QString ) ) ) ;

				if( pevent->wd == dev ){
					m_thread_helper->start( device,auto_mount_helper::dev,pevent->mask ) ;
				}else if( pevent->wd == mapper ){
					m_thread_helper->start( device,auto_mount_helper::dev_mapper,pevent->mask ) ;
				}else if( pevent->wd == md ){
					m_thread_helper->start( device,auto_mount_helper::dev_md,pevent->mask ) ;
				}else{
					;
				}
			}
		}
	}
}
示例#4
0
int stringNotEq(struct string *s, struct string *t)
{
    return !stringEqual(s, t);
}
示例#5
0
static void rebuildSelector(struct htmlTag *sel, jsobjtype oa, int len2)
{
	int i1, i2, len1;
	bool check2;
	char *s;
	const char *selname;
	bool changed = false;
	struct htmlTag *t;
	jsobjtype oo;		/* option object */

	len1 = cw->numTags;
	i1 = i2 = 0;
	selname = sel->name;
	if (!selname)
		selname = "?";
	debugPrint(4, "testing selector %s %d %d", selname, len1, len2);

	sel->lic = (sel->multiple ? 0 : -1);

	while (i1 < len1 && i2 < len2) {
/* there is more to both lists */
		t = tagList[i1++];
		if (t->action != TAGACT_OPTION)
			continue;
		if (t->controller != sel)
			continue;

/* find the corresponding option object */
		if ((oo = get_array_element_object(oa, i2)) == NULL) {
/* Wow this shouldn't happen. */
/* Guess I'll just pretend the array stops here. */
			len2 = i2;
			--i1;
			break;
		}

		t->jv = oo;	/* should already equal oo */
		t->rchecked = get_property_bool(oo, "defaultSelected");
		check2 = get_property_bool(oo, "selected");
		if (check2) {
			if (sel->multiple)
				++sel->lic;
			else
				sel->lic = i2;
		}
		++i2;
		if (t->checked != check2)
			changed = true;
		t->checked = check2;
		s = get_property_string(oo, "text");
		if (s && !t->textval || !stringEqual(t->textval, s)) {
			nzFree(t->textval);
			t->textval = s;
			changed = true;
		} else
			nzFree(s);
		s = get_property_string(oo, "value");
		if (s && !t->value || !stringEqual(t->value, s)) {
			nzFree(t->value);
			t->value = s;
		} else
			nzFree(s);
	}

/* one list or the other or both has run to the end */
	if (i2 == len2) {
		for (; i1 < len1; ++i1) {
			t = tagList[i1];
			if (t->action != TAGACT_OPTION)
				continue;
			if (t->controller != sel)
				continue;
/* option is gone in js, disconnect this option tag from its select */
			t->jv = 0;
			t->controller = 0;
			t->action = TAGACT_NOP;
			changed = true;
		}
	} else if (i1 == len1) {
		for (; i2 < len2; ++i2) {
			if ((oo = get_array_element_object(oa, i2)) == NULL)
				break;
			t = newTag("option");
			t->lic = i2;
			t->controller = sel;
			t->jv = oo;
			t->step = 2;	// already decorated
			t->textval = get_property_string(oo, "text");
			t->value = get_property_string(oo, "value");
			t->checked = get_property_bool(oo, "selected");
			if (t->checked) {
				if (sel->multiple)
					++sel->lic;
				else
					sel->lic = i2;
			}
			t->rchecked = get_property_bool(oo, "defaultSelected");
			changed = true;
		}
	}

	if (!changed)
		return;
	debugPrint(4, "selector %s has changed", selname);

/* If js change the menu, it should have also changed select.value
 * according to the checked options, but did it?
 * Don't know, so I'm going to do it here. */
	s = displayOptions(sel);
	if (!s)
		s = emptyString;
	set_property_string(sel->jv, "value", s);
	javaSetsTagVar(sel->jv, s);
	nzFree(s);

	if (!sel->multiple)
		set_property_number(sel->jv, "selectedIndex", sel->lic);
}				/* rebuildSelector */
示例#6
0
文件: cli.c 项目: opensesame11/BuenOS
void commandLine(){
	inputKey = 0;
	exit = 0;
	temp = 0;
	counter = 0;

	vgaSetup(3);
	vgaSetPos(0,0);
	vgaPrintString( getOSVersion() );

	while( 1 ){
		exit = 0;
		counter = 0;
		vgaPrintString( "BuenOS >" );//prompt input

		while( ( exit == 0 && inputKey == 0 ) || exit == 0 ){//fill input buffer
			inputKey = getKey();

			if( isInput( inputKey ) ){
				if( inputKey == 27 ){//esc
					cursorPos = vgaGetPos();
					vgaSetPos( ( cursorPos->x - counter ), cursorPos->y );
					for( temp = 0; temp < counter; temp++){
						vgaPrint( ' ' );
					}
					vgaSetPos( ( cursorPos->x - counter ), cursorPos->y );
					counter = 0;
					inputBuffer[counter] = 0;
				}
				else if( inputKey == 13 ){//enter
					exit = 1;//finish buffer loop
					inputBuffer[counter] = 0;//null terminate string
					while( counter < sizeOfBuffer ){//null out remaining buffer
						inputBuffer[counter] = 0;
						counter++;
					}
					vgaPrintString( "\r\n" );
				}

				else if( inputKey == 8 ){//backspace
					if( counter != 0 ){
						vgaPrintString( "\b \b" );
						counter--;
						inputBuffer[counter] = 0;
					}
				}
				else if( counter != sizeOfBuffer ){
					vgaPrint( inputKey );
					inputBuffer[counter] = inputKey;
					counter++;
				}
			}
		}
		stringCopy( inputBuffer, tempBuffer );
		stringParseInfo = stringParse( tempBuffer );
		stringParseInfo->argc++;
		stringUppercase( stringParseInfo->argv[0] );

		if( stringLength( stringParseInfo->argv[0] ) != 0 ){
			if( stringEqual( stringParseInfo->argv[0], "HELP" ) )  vgaPrintString( helpString );
			else if( stringEqual( stringParseInfo->argv[0], "CLEAR" ) )  vgaSetup( 3 );
			else if( stringEqual( stringParseInfo->argv[0], "VERSION" ) )  vgaPrintString( getOSVersion() );
			else if( stringEqual( stringParseInfo->argv[0], "ECHO" ) ) {
				stringStrip( stringParseInfo->argv[1], '"' );
				vgaPrintString( stringParseInfo->argv[1] );
				vgaPrintString( "\r\n" );
			}
			else if( stringEqual( stringParseInfo->argv[0], "DIR" ) ){
				getFileList( tempBuffer );
				stringFindAndReplace( tempBuffer, ',', ' ' );
				stringParseInfo = stringParse( tempBuffer );
				temp = 0;
				while( temp <= stringParseInfo->argc ){
					vgaPrintString( stringParseInfo->argv[temp] );
					cursorPos = vgaGetPos();
					vgaSetPos( 12, cursorPos->y );
					vgaPrintString( intToString( getFileSize( stringParseInfo->argv[temp] ) ) );
					vgaPrintString( " bytes\r\n" );
					temp++;
				}
			}
			else if( stringEqual( stringParseInfo->argv[0], "SIZEOF" ) ){
				vgaPrintString( intToString( getFileSize( stringParseInfo->argv[1] ) ) );
				vgaPrintString( " bytes\r\n" );
			}
			else if( stringEqual( stringParseInfo->argv[0], "RENAME" ) ){
				stringUppercase( stringParseInfo->argv[1] );
				stringUppercase( stringParseInfo->argv[2] );
				if( fileExists( stringParseInfo->argv[1] ) && stringLength( stringParseInfo->argv[1] ) != 0 && stringLength( stringParseInfo->argv[2] ) != 0 ){
					if( stringEqual( stringParseInfo->argv[1], "KERNEL.BIN" ) ) vgaPrintString( "Nice try.\r\n" );
					else if( fileExists( stringParseInfo->argv[2] ) || renameFile( stringParseInfo->argv[1], stringParseInfo->argv[2] ) ){
						vgaPrintString( "File " );
						vgaPrintString( stringParseInfo->argv[1] );
						vgaPrintString( " could not be renamed to " );
						vgaPrintString( stringParseInfo->argv[2] );
						vgaPrintString( "\r\n" );
					}
					else{
						vgaPrintString( "File " );
						vgaPrintString( stringParseInfo->argv[1] );
						vgaPrintString( " successfully renamed to " );
						vgaPrintString( stringParseInfo->argv[2] );
						vgaPrintString( "\r\n" );
					}
				}
				else{
					vgaPrintString( "File " );
					vgaPrintString( stringParseInfo->argv[1] );
					vgaPrintString( " cannot be found.\r\n" );
				}
			}
			else if( stringEqual( stringParseInfo->argv[0], "DELETE" ) ){
				stringUppercase( stringParseInfo->argv[1] );
				if( fileExists( stringParseInfo->argv[1] ) && stringLength( stringParseInfo->argv[1] ) != 0 ){
					if( stringEqual( stringParseInfo->argv[1], "KERNEL.BIN" ) != 1 ){
						if( removeFile( stringParseInfo->argv[1] ) ){
							vgaPrintString( "File " );
							vgaPrintString( stringParseInfo->argv[1] );
							vgaPrintString( " could not be deleted\r\n" );
						}
						else{
							vgaPrintString( "File " );
							vgaPrintString( stringParseInfo->argv[1] );
							vgaPrintString( " successfully deleted\r\n" );
						}
					}
					else vgaPrintString( "OMG just stop it. I NEED THAT!\r\n\nIT'S NOT FUNNY! ARGH!\r\n\n\n" );
				}
			}
			else if( stringEqual( stringParseInfo->argv[0], "SHUTDOWN" ) )  shutdown();
			else if( stringEqual( stringParseInfo->argv[0], "RESTART" ) )  restart();
			else runApplication( stringParseInfo );
		}
	}
}
示例#7
0
void
sql_connect(const char *db, const char *login, const char *pw)
{
    short waste;
    char constring[200];
    char outstring[200];
    char drivername[40];
    char *s;

    if(isnullstring(db))
	errorPrint
	   ("2sql_connect receives no data source, check your edbrowse config file");
    if(debugLevel >= 1)
	i_printf(MSG_DBConnecting, db);

    /* first disconnect the old one */
    if(disconnect())
	return;

    /* initial call to sql_connect sets up ODBC */
    if(henv == SQL_NULL_HENV) {
	char verstring[6];

	/* Allocate environment and connection handles */
	/* these two handles are never freed */
	rc = SQLAllocEnv(&henv);
	if(rc)
	    errorPrint("@could not alloc ODBC environment handle");
	rc = SQLAllocConnect(henv, &hdbc);
	if(rc)
	    errorPrint("@could not alloc ODBC connection handle");

	/* Establish the ODBC major version number.
	 * Course the call to make this determination doesn't exist
	 * prior to version 2.0. */
	odbc_version = 1;
	rc = SQLGetInfo(hdbc, SQL_DRIVER_ODBC_VER, verstring, 6, &waste);
	if(!rc) {
	    verstring[2] = 0;
	    odbc_version = atoi(verstring);
	}
    }

    /* connect to the database */
    sprintf(constring, "DSN=%s", db);
    if(login) {
	s = constring + strlen(constring);
	sprintf(s, ";UID=%s", login);
    }
    if(pw) {
	s = constring + strlen(constring);
	sprintf(s, ";PWD=%s", pw);
    }

    stmt_text = constring;
    debugStatement();
    rc = SQLDriverConnect(hdbc, NULL,
       constring, SQL_NTS,
       outstring, sizeof (outstring), &waste, SQL_DRIVER_NOPROMPT);
    if(errorTrap(0))
	return;
    sql_database = db;
    exclist = 0;

    /* Set the persistent connect/statement options.
     * Note that some of these merely reassert the default,
     * but it's good documentation to spell it out here. */
    stmt_text = "noscan on";
    rc = SQLSetConnectOption(hdbc, SQL_NOSCAN, SQL_NOSCAN_ON);
    stmt_text = "repeatable read";
    rc = SQLSetConnectOption(hdbc, SQL_TXN_ISOLATION, SQL_TXN_REPEATABLE_READ);	/* fail */
    stmt_text = "rowset size";
    rc = SQLSetConnectOption(hdbc, SQL_ROWSET_SIZE, 1);
    stmt_text = "login timeout";
    rc = SQLSetConnectOption(hdbc, SQL_LOGIN_TIMEOUT, 15);	/* fail */
    stmt_text = "query timeout";
    rc = SQLSetConnectOption(hdbc, SQL_QUERY_TIMEOUT, 0);	/* fail */
    stmt_text = "async disable";
    rc = SQLSetConnectOption(hdbc, SQL_ASYNC_ENABLE, SQL_ASYNC_ENABLE_OFF);	/* fail */
    stmt_text = "autocommit";
    rc = SQLSetConnectOption(hdbc, SQL_AUTOCOMMIT, SQL_AUTOCOMMIT_ON);
    stmt_text = "cursor forward";
    rc = SQLSetConnectOption(hdbc, SQL_CURSOR_TYPE, SQL_CURSOR_FORWARD_ONLY);
    stmt_text = "concurrent reads";
    rc = SQLSetConnectOption(hdbc, SQL_CONCURRENCY, SQL_CONCUR_READ_ONLY);
    stmt_text = "use driver";
    rc = SQLSetConnectOption(hdbc, SQL_ODBC_CURSORS, SQL_CUR_USE_DRIVER);	/* fail */
    stmt_text = "no bookmarks";
    rc = SQLSetConnectOption(hdbc, SQL_USE_BOOKMARKS, SQL_UB_OFF);	/* fail */

    /* this call is only necessary if SQL_NULL_HSTMT != 0 */
    clearAllCursors();

    /* set defaults, in case the GetInfo command fails */
    cursors_under_commit = cursors_under_rollback = SQL_CB_DELETE;
    SQLGetInfo(hdbc, SQL_CURSOR_COMMIT_BEHAVIOR, &cursors_under_commit, 4,
       &waste);
    SQLGetInfo(hdbc, SQL_CURSOR_ROLLBACK_BEHAVIOR, &cursors_under_rollback, 4,
       &waste);
    getdata_opts = 0;
    SQLGetInfo(hdbc, SQL_GETDATA_EXTENSIONS, &getdata_opts, 4, &waste);
    bookmarkBits = false;
    SQLGetInfo(hdbc, SQL_BOOKMARK_PERSISTENCE, &bookmarkBits, 4, &waste);

    exclist = 0;

/* Time to find out what the driver is, so we can have driver specific tweaks. */
    SQLGetInfo(hdbc, SQL_DRIVER_NAME, drivername, sizeof (drivername), &waste);
    current_driver = DRIVER_NONE;
    if(stringEqual(drivername, "libsqliteodbc.so") ||
       stringEqual(drivername, "sqlite3odbc.so"))
	current_driver = DRIVER_SQLITE;
    if(stringEqual(drivername, "libmyodbc.so"))
	current_driver = DRIVER_MYSQL;
    if(stringEqual(drivername, "libodbcpsql.so"))
	current_driver = DRIVER_POSTGRESQL;
    if(stringEqual(drivername, "iclis09b.so"))
	current_driver = DRIVER_INFORMIX;
    if(stringEqual(drivername, "libtdsodbc.so")) {
	current_driver = DRIVER_TDS;
	openfirst = true;
    }

    if(sql_debug) {
	if(current_driver)
	    appendFile(sql_debuglog, "driver is %d", current_driver);
	else
	    appendFile(sql_debuglog, "driver string is %s", drivername);
    }

    exclist = 0;
}				/* sql_connect */
示例#8
0
static bool
errorTrap(const char *cxerr)
{
    short i, waste;
    char errcodes[6];
    bool firstError, errorFound;

    /* innocent until proven guilty */
    rv_lastStatus = 0;
    rv_vendorStatus = 0;
    rv_stmtOffset = 0;
    rv_badToken = 0;
    if(!rc)
	return false;		/* no problem */

    /* log the SQL statement that elicitted the error */
    showStatement();

    if(rc == SQL_INVALID_HANDLE)
	errorPrint
	   ("@ODBC fails to recognize one of the handles (env, connect, stmt)");

    /* get error info from ODBC */
    firstError = true;
    errorFound = false;

    while(true) {
	rc = SQLError(henv, hdbc, hstmt,
	   errcodes, &rv_vendorStatus, errorText, sizeof (errorText), &waste);
	if(rc == SQL_NO_DATA) {
	    if(firstError) {
		printf
		   ("ODBC command failed, but SQLError() provided no additional information\n");
		return true;
	    }
	    return errorFound;
	}

	/* Skip past the ERROR-IN-ROW errors. */
	if(stringEqual(errcodes, "01S01"))
	    continue;

	firstError = false;
	if(cxerr && stringEqual(cxerr, errcodes))
	    continue;

	if(errorFound)
	    continue;
	errorFound = true;
	rv_lastStatus = errTranslate(errcodes);

	/* Don't know how to get statement ofset or invalid token from ODBC.
	   /* I can get them from Informix; see dbinfx.ec */

	/* if the application didn't trap for this exception, blow up! */
	if(exclist) {
	    for(i = 0; exclist[i]; ++i)
		if(exclist[i] == rv_lastStatus)
		    break;
	    if(exclist[i]) {
		exclist = 0;	/* we've spent that exception */
		continue;
	    }
	}

	printf("ODBC error %s, %s, driver %s\n",
	   errcodes, sqlErrorList[rv_lastStatus], errorText);
	setError(MSG_DBUnexpected, rv_vendorStatus);
    }
}				/* errorTrap */