Browse Source

Start adding the dc

signals
Gavin Howard 4 years ago
parent
commit
4ffe5a9252
Signed by untrusted user who does not match committer: gavin
GPG Key ID: C08038BDF280D33E
  1. 3
      .gitignore
  2. 62
      Makefile
  3. 28
      gen/dc_help.txt
  4. 8
      include/bc.h
  5. 34
      include/dc.h
  6. 3
      include/vm.h
  7. 80
      install.sh
  8. 69
      safe-install.sh
  9. 2
      src/bc/bc.c
  10. 3
      src/data.c
  11. 94
      src/dc/dc.c
  12. 24
      src/main.c

3
.gitignore vendored

@ -5,6 +5,8 @@
*.creator.user
bc
!bc/
dc
!dc/
*.o
*.a
.log_*.txt
@ -15,6 +17,7 @@ bc
strgen
lib.c
bc_help.c
dc_help.c
config.mak
timeconst.bc

62
Makefile

@ -13,9 +13,15 @@
# PERFORMANCE OF THIS SOFTWARE.
#
BC_SRC = $(sort $(wildcard src/*.c))
SRC = $(sort $(wildcard src/*.c))
OBJ = $(SRC:.c=.o)
BC_SRC = $(sort $(wildcard src/bc/*.c))
BC_OBJ = $(BC_SRC:.c=.o)
DC_SRC = $(sort $(wildcard src/dc/*.c))
DC_OBJ = $(DC_SRC:.c=.o)
GEN_DIR = gen
GEN_EXEC = strgen
@ -27,7 +33,12 @@ BC_HELP = $(GEN_DIR)/bc_help.txt
BC_HELP_C = $(GEN_DIR)/bc_help.c
BC_HELP_O = $(GEN_DIR)/bc_help.o
DC_HELP = $(GEN_DIR)/dc_help.txt
DC_HELP_C = $(GEN_DIR)/dc_help.c
DC_HELP_O = $(GEN_DIR)/dc_help.o
BC_EXEC = bc
DC_EXEC = dc
PREFIX ?= /usr/local
@ -42,7 +53,34 @@ LDLIBS += -lm
HOSTCC ?= $(CC)
all: $(BC_EXEC)
all: CPPFLAGS += -DDC_CONFIG -DBC_CONFIG
all: clean $(DC_HELP_O) $(BC_HELP_O) $(BC_LIB_O) $(BC_OBJ) $(DC_OBJ) $(OBJ)
$(CC) $(CFLAGS) $(OBJ) $(DC_OBJ) $(BC_OBJ) $(BC_LIB_O) $(BC_HELP_O) $(DC_HELP_O) \
$(LDLIBS) $(LDFLAGS) -o $(BC_EXEC)
ln -s ./$(BC_EXEC) $(DC_EXEC)
$(GEN_EXEC):
$(HOSTCC) -o $(GEN_EXEC) $(GEN_DIR)/$(GEN_EXEC).c
$(BC_LIB_C): $(GEN_EXEC)
$(GEN_EMU) ./$(GEN_EXEC) $(BC_LIB) $(BC_LIB_C) bc_lib bc_lib_name
$(BC_HELP_C): $(GEN_EXEC)
$(GEN_EMU) ./$(GEN_EXEC) $(BC_HELP) $(BC_HELP_C) bc_help
$(DC_HELP_C): $(GEN_EXEC)
$(GEN_EMU) ./$(GEN_EXEC) $(DC_HELP) $(DC_HELP_C) dc_help
$(DC_EXEC): CPPFLAGS += -DDC_CONFIG
$(DC_EXEC): clean $(DC_OBJ) $(DC_HELP_O) $(OBJ)
$(CC) $(CFLAGS) $(OBJ) $(DC_OBJ) $(DC_HELP_O) $(LDLIBS) $(LDFLAGS) -o $(DC_EXEC)
$(BC_EXEC): CPPFLAGS += -DBC_CONFIG
$(BC_EXEC): clean $(BC_OBJ) $(BC_LIB_O) $(BC_HELP_O) $(OBJ)
$(CC) $(CFLAGS) $(OBJ) $(BC_OBJ) $(BC_LIB_O) $(BC_HELP_O) $(LDLIBS) $(LDFLAGS) -o $(BC_EXEC)
main: clean $(BC_OBJ) $(BC_LIB_O) $(BC_HELP_O)
$(CC) $(CFLAGS) $(BC_OBJ) $(BC_LIB_O) $(BC_HELP_O) $(LDLIBS) $(LDFLAGS)
help:
@echo "available targets:"
@ -66,18 +104,6 @@ help:
@echo " if PREFIX is \"/usr\", $(BC_EXEC) will be installed to \"/usr/bin\""
@echo " GEN_EMU Emulator to run $(GEN_EXEC) under (leave empty if not necessary)"
$(GEN_EXEC):
$(HOSTCC) -o $(GEN_EXEC) $(GEN_DIR)/$(GEN_EXEC).c
$(BC_LIB_C): $(GEN_EXEC)
$(GEN_EMU) ./$(GEN_EXEC) $(BC_LIB) $(BC_LIB_C) bc_lib bc_lib_name
$(BC_HELP_C): $(GEN_EXEC)
$(GEN_EMU) ./$(GEN_EXEC) $(BC_HELP) $(BC_HELP_C) bc_help
$(BC_EXEC): $(BC_OBJ) $(BC_LIB_O) $(BC_HELP_O)
$(CC) $(CFLAGS) -o $(BC_EXEC) $(BC_OBJ) $(BC_LIB_O) $(BC_HELP_O) $(LDLIBS) $(LDFLAGS)
test:
tests/all.sh
@ -88,8 +114,11 @@ timeconst:
tests/timeconst.sh
clean:
$(RM) -f $(OBJ)
$(RM) -f $(BC_OBJ)
$(RM) -f $(DC_OBJ)
$(RM) -f $(BC_EXEC)
$(RM) -f $(DC_EXEC)
$(RM) -f $(GEN_EXEC)
$(RM) -f $(BC_LIB_C)
$(RM) -f $(BC_LIB_O)
@ -104,10 +133,11 @@ clean_tests: clean
$(RM) -f .math.txt .results.txt .ops.txt
$(RM) -f .test.txt
install: $(BC_EXEC)
$(INSTALL) -Dm 755 $(BC_EXEC) $(DESTDIR)$(PREFIX)/bin/$(BC_EXEC)
install:
$(INSTALL) $(DESTDIR)$(PREFIX)/bin $(BC_EXEC) $(DC_EXEC)
uninstall:
$(RM) -f $(DESTDIR)$(PREFIX)/bin/$(BC_EXEC)
$(RM) -f $(DESTDIR)$(PREFIX)/bin/$(DC_EXEC)
.PHONY: all help clean clean_tests install uninstall test

28
gen/dc_help.txt

@ -0,0 +1,28 @@
/*
* *****************************************************************************
*
* Copyright 2018 Gavin D. Howard
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*
* *****************************************************************************
*
* The dc help text.
*
*/
usage: %s [options] [file...]
dc is a command-line, reverse Polish notation arbitrary-precision calculator.
See the GNU dc manual (https://www.gnu.org/software/bc/manual/dc-1.05/) for
details.

8
include/bc.h

@ -16,12 +16,12 @@
*
* *****************************************************************************
*
* Definitions for all of bc.
* Definitions for bc.
*
*/
#ifndef BC_H
#define BC_H
#ifndef BC_BC_H
#define BC_BC_H
#include <status.h>
@ -31,4 +31,4 @@ BcStatus bc_main(int argc, char *argv[]);
extern const char bc_help[];
#endif // BC_H
#endif // BC_BC_H

34
include/dc.h

@ -0,0 +1,34 @@
/*
* *****************************************************************************
*
* Copyright 2018 Gavin D. Howard
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*
* *****************************************************************************
*
* Definitions for bc.
*
*/
#ifndef BC_DC_H
#define BC_DC_H
#include <status.h>
// ** Exclude start. **
BcStatus dc_main(int argc, char *argv[]);
// ** Exclude end. **
extern const char dc_help[];
#endif // BC_DC_H

3
include/vm.h

@ -86,6 +86,9 @@ BcStatus bc_vm_exec(unsigned int flags, BcVec *files);
extern BcGlobals bcg;
extern const char bc_name[];
extern const char dc_name[];
extern const char bc_header[];
extern const char bc_lib[];

80
install.sh

@ -1,67 +1,47 @@
#!/bin/sh
#! /bin/sh
#
# Written by Rich Felker, originally as part of musl libc.
# Multi-licensed under MIT, 0BSD, and CC0.
# Copyright 2018 Gavin D. Howard
#
# This is an actually-safe install command which installs the new
# file atomically in the new location, rather than overwriting
# existing files.
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.
#
usage() {
printf "usage: %s [-D] [-l] [-m mode] src dest\n" "$0" 1>&2
exit 1
printf "usage: %s install_dir executable [symlinks...]\n" "$0" 1>&2
exit 1
}
mkdirp=
symlink=
mode=755
script=$(realpath "$0")
scriptdir=$(dirname "$script")
while getopts Dlm: name ; do
case "$name" in
D) mkdirp=yes ;;
l) symlink=yes ;;
m) mode=$OPTARG ;;
?) usage ;;
esac
done
shift $(($OPTIND - 1))
INSTALL="$scriptdir/safe-install.sh"
test "$#" -eq 2 || usage
src=$1
dst=$2
tmp="$dst.tmp.$$"
test "$#" -gt 1 || usage
case "$dst" in
*/) printf "%s: %s ends in /\n", "$0" "$dst" 1>&2 ; exit 1 ;;
esac
installdir="$1"
shift
set -C
set -e
exe="$1"
shift
if test "$mkdirp" ; then
umask 022
case "$2" in
*/*) mkdir -p "${dst%/*}" ;;
esac
fi
"$INSTALL" -Dm 755 "$exe" "$installdir/$exe"
trap 'rm -f "$tmp"' EXIT INT QUIT TERM HUP
while [ $# -gt 0 ]; do
umask 077
link="$1"
shift
if test "$symlink" ; then
ln -s "$1" "$tmp"
else
cat < "$1" > "$tmp"
chmod "$mode" "$tmp"
fi
base=$(basename "$link")
mv -f "$tmp" "$2"
test -d "$2" && {
rm -f "$2/$tmp"
printf "%s: %s is a directory\n" "$0" "$dst" 1>&2
exit 1
}
rm -f "$installdir/$link"
ln -s "$installdir/$exe" "$installdir/$link"
exit 0
done

69
safe-install.sh

@ -0,0 +1,69 @@
#!/bin/sh
#
# Written by Rich Felker, originally as part of musl libc.
# Multi-licensed under MIT, 0BSD, and CC0.
#
# This is an actually-safe install command which installs the new
# file atomically in the new location, rather than overwriting
# existing files.
#
usage() {
printf "usage: %s [-D] [-l] [-m mode] src dest\n" "$0" 1>&2
exit 1
}
mkdirp=
symlink=
mode=755
while getopts Dlm: name ; do
case "$name" in
D) mkdirp=yes ;;
l) symlink=yes ;;
m) mode=$OPTARG ;;
?) usage ;;
esac
done
shift $(($OPTIND - 1))
test "$#" -eq 2 || usage
src=$1
dst=$2
tmp="$dst.tmp.$$"
[ -f "$src" ] || exit 0
case "$dst" in
*/) printf "%s: %s ends in /\n", "$0" "$dst" 1>&2 ; exit 1 ;;
esac
set -C
set -e
if test "$mkdirp" ; then
umask 022
case "$2" in
*/*) mkdir -p "${dst%/*}" ;;
esac
fi
trap 'rm -f "$tmp"' EXIT INT QUIT TERM HUP
umask 077
if test "$symlink" ; then
ln -s "$1" "$tmp"
else
cat < "$1" > "$tmp"
chmod "$mode" "$tmp"
fi
mv -f "$tmp" "$2"
test -d "$2" && {
rm -f "$2/$tmp"
printf "%s: %s is a directory\n" "$0" "$dst" 1>&2
exit 1
}
exit 0

2
src/bc.c → src/bc/bc.c

@ -16,7 +16,7 @@
*
* *****************************************************************************
*
* Code common to all of bc.
* The main procedure of bc.
*
*/

3
src/data.c

@ -23,6 +23,9 @@
#include <lex.h>
#include <parse.h>
const char bc_name[] = "bc";
const char dc_name[] = "dc";
const char bc_header[] =
"bc 1.0\n"
"Copyright (c) 2018 Gavin D. Howard and contributors\n"

94
src/dc/dc.c

@ -0,0 +1,94 @@
/*
* *****************************************************************************
*
* Copyright 2018 Gavin D. Howard
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*
* *****************************************************************************
*
* The main procedure of dc.
*
*/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <status.h>
#include <vector.h>
#include <bc.h>
#include <vm.h>
#include <args.h>
BcStatus dc_main(int argc, char *argv[]) {
BcStatus s;
BcVec files, args;
unsigned int flags;
char *env_args, *buffer, *buf;
flags = 0;
buffer = NULL;
if ((s = bc_vec_init(&files, sizeof(char*), NULL))) return (int) s;
if ((env_args = getenv(bc_args_env_name))) {
if ((s = bc_vec_init(&args, sizeof(char*), NULL))) goto err;
if ((s = bc_vec_push(&args, 1, &bc_args_env_name))) goto args_err;
if (!(buffer = strdup(env_args))) {
s = BC_STATUS_ALLOC_ERR;
goto args_err;
}
buf = buffer;
while (*buf) {
if (!isspace(*buf)) {
if ((s = bc_vec_push(&args, 1, &buf))) goto buf_err;
while (*buf && !isspace(*buf)) ++buf;
if (*buf) (*(buf++)) = '\0';
}
else ++buf;
}
s = bc_args((int) args.len, (char**) args.vec, &flags, &files);
if(s) goto buf_err;
}
if((s = bc_args(argc, argv, &flags, &files))) goto buf_err;
flags |= BC_FLAG_S * (getenv("POSIXLY_CORRECT") != NULL);
s = bc_vm_exec(flags, &files);
buf_err:
if (env_args) free(buffer);
args_err:
if (env_args) bc_vec_free(&args);
err:
bc_vec_free(&files);
return (int) s;
}

24
src/main.c

@ -20,13 +20,20 @@
*
*/
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#include <libgen.h>
#include <status.h>
#include <vm.h>
#include <bc.h>
#if !defined(BC_CONFIG) && !defined(DC_CONFIG)
#error Must define BC_CONFIG, DC_CONFIG, or both
#endif
BcGlobals bcg;
int main(int argc, char *argv[]) {
@ -34,5 +41,22 @@ int main(int argc, char *argv[]) {
setlocale(LC_ALL, "");
memset(&bcg, 0, sizeof(BcGlobals));
#if !defined(DC_CONFIG)
return (int) bc_main(argc, argv);
#elif !defined(BC_CONFIG)
return (int) dc_main(argc, argv);
#else
BcStatus result;
char* name, *base;
if (!(name = strdup(argv[0]))) return (int) BC_STATUS_ALLOC_ERR;
base = basename(name);
if (!strcmp(base, dc_name)) result = dc_main(argc, argv);
else result = bc_main(argc, argv);
free(name);
return (int) result;
#endif
}

Loading…
Cancel
Save