From owner-svn-src-head@FreeBSD.ORG Mon Apr 14 14:05:00 2014 Return-Path: Delivered-To: svn-src-head@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id A211FBB1; Mon, 14 Apr 2014 14:05:00 +0000 (UTC) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:1900:2254:2068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 8E3481B21; Mon, 14 Apr 2014 14:05:00 +0000 (UTC) Received: from svn.freebsd.org ([127.0.1.70]) by svn.freebsd.org (8.14.8/8.14.8) with ESMTP id s3EE5080063387; Mon, 14 Apr 2014 14:05:00 GMT (envelope-from eadler@svn.freebsd.org) Received: (from eadler@localhost) by svn.freebsd.org (8.14.8/8.14.8/Submit) id s3EE50U3063384; Mon, 14 Apr 2014 14:05:00 GMT (envelope-from eadler@svn.freebsd.org) Message-Id: <201404141405.s3EE50U3063384@svn.freebsd.org> From: Eitan Adler Date: Mon, 14 Apr 2014 14:05:00 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r264458 - head/usr.bin/units X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.17 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 14 Apr 2014 14:05:00 -0000 Author: eadler Date: Mon Apr 14 14:04:59 2014 New Revision: 264458 URL: http://svnweb.freebsd.org/changeset/base/264458 Log: units(1): add libedit support Add line editing and history support to units(1) via libedit. Reviewed by: bdrewery Modified: head/usr.bin/units/Makefile head/usr.bin/units/units.c Modified: head/usr.bin/units/Makefile ============================================================================== --- head/usr.bin/units/Makefile Mon Apr 14 13:30:08 2014 (r264457) +++ head/usr.bin/units/Makefile Mon Apr 14 14:04:59 2014 (r264458) @@ -4,4 +4,7 @@ PROG= units FILES= units.lib FILESDIR= ${SHAREDIR}/misc +LDADD+=-ledit -ltermcap +DPADD+=${LIBEDIT} ${LIBTERMCAP} + .include Modified: head/usr.bin/units/units.c ============================================================================== --- head/usr.bin/units/units.c Mon Apr 14 13:30:08 2014 (r264457) +++ head/usr.bin/units/units.c Mon Apr 14 14:04:59 2014 (r264458) @@ -23,6 +23,8 @@ static const char rcsid[] = #include #include #include +#include +#include #include #include #include @@ -81,7 +83,7 @@ void initializeunit(struct unittype * t int addsubunit(char *product[], char *toadd); void showunit(struct unittype * theunit); void zeroerror(void); -int addunit(struct unittype *theunit, char *toadd, int flip, int quantity); +int addunit(struct unittype *theunit, const char *toadd, int flip, int quantity); int compare(const void *item1, const void *item2); void sortunit(struct unittype * theunit); void cancelunit(struct unittype * theunit); @@ -94,6 +96,12 @@ int completereduce(struct unittype * un void showanswer(struct unittype * have, struct unittype * want); void usage(void); +static const char* promptstr = ""; + +static const char * prompt(EditLine *e __unused) { + return promptstr; +} + char * dupstr(const char *str) { @@ -304,7 +312,7 @@ zeroerror(void) */ int -addunit(struct unittype * theunit, char *toadd, int flip, int quantity) +addunit(struct unittype * theunit, const char *toadd, int flip, int quantity) { char *scratch, *savescr; char *item; @@ -682,18 +690,25 @@ main(int argc, char **argv) { struct unittype have, want; - char havestr[81], wantstr[81]; + const char * havestr; + const char * wantstr; int optchar; - char *userfile = 0; - int quiet = 0; + char *userfile; + bool quiet; + History *inhistory; + EditLine *el; + HistEvent ev; + int inputsz; + userfile = NULL; + quiet = false; while ((optchar = getopt(argc, argv, "Vqf:")) != -1) { switch (optchar) { case 'f': userfile = optarg; break; case 'q': - quiet = 1; + quiet = true; break; case 'V': fprintf(stderr, "FreeBSD units\n"); @@ -707,11 +722,22 @@ main(int argc, char **argv) if (optind != argc - 2 && optind != argc) usage(); + inhistory = history_init(); + el = el_init(argv[0], stdin, stdout, stderr); + el_source(el, NULL); + el_set(el, EL_PROMPT, &prompt); + el_set(el, EL_EDITOR, "emacs"); + el_set(el, EL_SIGNAL, 1); + el_set(el, EL_HIST, history, inhistory); + history(inhistory, &ev, H_SETSIZE, 800); + if (inhistory == 0) + err(1, "Could not initalize history"); + readunits(userfile); if (optind == argc - 2) { - strlcpy(havestr, argv[optind], sizeof(havestr)); - strlcpy(wantstr, argv[optind + 1], sizeof(wantstr)); + havestr = argv[optind]; + wantstr = argv[optind + 1]; initializeunit(&have); addunit(&have, havestr, 0, 1); completereduce(&have); @@ -728,28 +754,31 @@ main(int argc, char **argv) do { initializeunit(&have); if (!quiet) - printf("You have: "); - if (!fgets(havestr, sizeof(havestr), stdin)) { - if (!quiet) - putchar('\n'); + promptstr = "You have: "; + havestr = el_gets(el, &inputsz); + if (havestr == NULL) exit(0); - } + if (inputsz > 0) + history(inhistory, &ev, H_ENTER, + havestr); } while (addunit(&have, havestr, 0, 1) || completereduce(&have)); do { initializeunit(&want); if (!quiet) - printf("You want: "); - if (!fgets(wantstr, sizeof(wantstr), stdin)) { - if (!quiet) - putchar('\n'); + promptstr = "You want: "; + wantstr = el_gets(el, &inputsz); + if (wantstr == NULL) exit(0); - } + if (inputsz > 0) + history(inhistory, &ev, H_ENTER, + wantstr); } while (addunit(&want, wantstr, 0, 1) || completereduce(&want)); showanswer(&have, &want); } } + history_end(inhistory); return(0); }