From 05ac506098525a3634de497af4a535e77006b655 Mon Sep 17 00:00:00 2001 From: koen_1711 Date: Fri, 26 Apr 2024 21:41:35 +0200 Subject: [PATCH 1/5] chore: readd some old dirs --- src/build-helpers/versioninfo.m4 | 73 ++++ src/build-helpers/versioninfo.md | 190 +++++++++++ src/build-helpers/versioninfo.mk | 31 ++ src/build-helpers/versioninfo.sh | 118 +++++++ src/msvc/getopt.c | 563 +++++++++++++++++++++++++++++++ src/msvc/getopt.h | 95 ++++++ src/msvc/gettimeofday.c | 77 +++++ src/msvc/msvc_compat.h | 58 ++++ src/msvc/readline.cpp | 95 ++++++ src/msvc/readline/history.h | 17 + src/msvc/readline/readline.h | 24 ++ src/msvc/sys/time.h | 43 +++ src/msvc/unistd.h | 38 +++ src/msvc/usleep.cpp | 93 +++++ 14 files changed, 1515 insertions(+) create mode 100644 src/build-helpers/versioninfo.m4 create mode 100644 src/build-helpers/versioninfo.md create mode 100644 src/build-helpers/versioninfo.mk create mode 100755 src/build-helpers/versioninfo.sh create mode 100644 src/msvc/getopt.c create mode 100644 src/msvc/getopt.h create mode 100644 src/msvc/gettimeofday.c create mode 100644 src/msvc/msvc_compat.h create mode 100644 src/msvc/readline.cpp create mode 100644 src/msvc/readline/history.h create mode 100644 src/msvc/readline/readline.h create mode 100644 src/msvc/sys/time.h create mode 100644 src/msvc/unistd.h create mode 100644 src/msvc/usleep.cpp diff --git a/src/build-helpers/versioninfo.m4 b/src/build-helpers/versioninfo.m4 new file mode 100644 index 000000000..30e36d6e9 --- /dev/null +++ b/src/build-helpers/versioninfo.m4 @@ -0,0 +1,73 @@ +# versioninfo.m4 - find avrdude version information for msg -*- Autoconf -*- +# serial 1 +dnl | Increment the above serial number every time you edit this file. +dnl | When it finds multiple m4 files with the same name, +dnl | aclocal will use the one with the highest serial. +dnl +dnl The sequence of version info items to store in the version-stamp file. +dnl This must be the same sequence as the versioninfo script writes. +m4_pattern_forbid([versioninfo_items]) +m4_define([versioninfo_items], [ + [CMAKE_PROJECT_VERSION], + [GIT_COMMIT_DATE], + [GIT_COMMIT_HASH], + [GIT_TAG_HASH] +]) +dnl +dnl Initialize version info from the script command given as macro argument. +m4_pattern_forbid([versioninfo_init]) +m4_define([versioninfo_init], [ + m4_pushdef([versioninfo_split], m4_split(m4_esyscmd($1),m4_newline)) + m4_case(m4_sysval, [0], [ + m4_for([N], [1], m4_count(versioninfo_split), [1], [ + m4_define([versioninfo_]m4_normalize(m4_argn(N, versioninfo_items)), + m4_normalize(m4_argn(N, versioninfo_split))) + ]) + ], [ + m4_fatal([versioninfo script returned non-0]) + ]) +]) +dnl +m4_define([versioninfo_stampfile], [versioninfo-stamp]) +dnl +versioninfo_init([./build-helpers/versioninfo.sh . ]m4_defn([versioninfo_stampfile])) +dnl +dnl +dnl +dnl ======================================================================== +dnl VERSIONINFO_SETUP() +dnl ======================================================================== +dnl +AC_DEFUN([VERSIONINFO_SETUP], [dnl +m4_foreach([ITEM], [versioninfo_items], [dnl + AC_SUBST(m4_normalize(ITEM), [m4_defn([versioninfo_]m4_normalize(ITEM))]) + AC_MSG_CHECKING([versioninfo item ]m4_normalize(ITEM)) + AC_MSG_RESULT([$]m4_normalize(ITEM)) +]) +dnl +dnl Define helper substitutions containing shell code for use inside +dnl make recipes in build-helpers/versioninfo.mk and $(shell ...) +dnl calls in GNUmakefile.in dealing with version info. +dnl +AC_SUBST([VERSIONINFO_STAMPFILE], [m4_defn([versioninfo_stampfile])]) +AM_SUBST_NOTMAKE([VERSIONINFO_WRITE]) +dnl +AC_SUBST([VERSIONINFO_READ], + ['m4_foreach([ITEM], [versioninfo_items], [read m4_normalize(ITEM); ]):']) +AM_SUBST_NOTMAKE([VERSIONINFO_READ]) +dnl +AC_SUBST([VERSIONINFO_WRITE], + ['printf "%s\n"m4_foreach([ITEM], [versioninfo_items], [ "$(m4_normalize(ITEM))"])']) +AM_SUBST_NOTMAKE([VERSIONINFO_WRITE]) +dnl +AC_SUBST([VERSIONINFO_IS_UNCHANGED], + ['false; then :; m4_foreach([ITEM], [versioninfo_items], [elif test "x$(m4_normalize(ITEM))" != "x$$m4_normalize(ITEM)"; then printf "%s has changed from %s to %s\n" "m4_normalize(ITEM)" "$(m4_normalize(ITEM))" "$$m4_normalize(ITEM)" >&2; ]) else ']) +AM_SUBST_NOTMAKE([VERSIONINFO_IS_UNCHANGED]) +dnl +])dnl +dnl +dnl #################################################################### +dnl +dnl Local Variables: +dnl mode: autoconf +dnl End: diff --git a/src/build-helpers/versioninfo.md b/src/build-helpers/versioninfo.md new file mode 100644 index 000000000..2e3ae9f71 --- /dev/null +++ b/src/build-helpers/versioninfo.md @@ -0,0 +1,190 @@ +The versioninfo mechanism +========================= + +This describes the versioninfo mechanism which automatically generates +the version information for use in `configure.ac` and then the rest of +the autotools build system. + + +The basic idea +-------------- + +The main goals are + + * Avoid the need for avrdude maintainers to edit the avrdude version + in the autotools buildsystem's `configure.ac` in addition to the + cmake buildsystem's main `CMakeLists.txt` file. + + * Have the autotools buildsystem generate the same version message as + the cmake buildsystem in both the `avrdude.conf` file and the + `avrdude -?` output version message. + +To achieve this, we need to replicate some of the logic from the +top-level `CMakeLists.txt` which creates the `AVRDUDE_FULL_VERSION` +variable. + +We run this logic mostly at bootstrap/autoreconf time, with the +`configure` propagating the information, so that some `make` time +checks can determine whether the bootstrap code needs to be re-run, +and re-run it automatically if possible. + + +All the different build types +----------------------------- + +There are many ways to build avrdude. + +There are three types of source trees: + + * a git clone's working directory with a `.git/` directory + + * an extracted GitHub tarball/zipfile (contains the same as a git + clone's working directory, but no `.git/` directory with its + associated git commit information) + + * from a dist tarball as generated by the autotools buildsystem's + `make dist` or `make distcheck` commands (contains the required + git commit information inside the versioninfo-stamp file) + +In any of these cases, this can be an avrdude release or non-release, +determined by whether the latest commit's hash is equal to the latest +tag's commit's hash (`GIT_COMMIT_HASH == GIT_TAG_HASH`). + +In principle, any of these source types could be built with either the +cmake buildsystem or the autotools buildsystem. With `configure.ac` +being rooted in the `src/` subdirectory though, the dist tarball +generated by `make dist` cannot contain files outside of `src/` which +are required for a cmake build. + +In all types of builds, we want the same version message when building +using the autotools buildsystem as the cmake buildsystem creates: + + * `7.3` for a release + * `7.3-20240213 (9634fd15)` with commit date and commit hash for a + non-release. + +If we abbreviate those types of version messages with `R` for release +and `H` for hash, we can put all cases into a table. `BS` stands for +buildsystem, obviously. + +``` + Version_Message +SOURCE TYPE BS Release? Wanted Actual +git clone wd cm yes R R +git clone wd cm no H H +github tarball cm yes R R +github tarball cm no H R (wrong/missing info) +dist tarball cm yes R cmake build N/A at this time +dist tarball cm no H cmake build N/A at this time + +git clone wd am yes R R +git clone wd am no H H +github tarball am yes R R +github tarball am no H R (wrong/missing info/like cmake) +dist tarball am yes R R +dist tarball am no H H +``` + +As the github tarball (like the github zipfile) does not contain any +commit/tag information, it is impossible for any buildsystem to +determine whether building a release or non-release. + +The dist tarball generated by the autotools buildsystem's `make dist` +does contain the commit/tag information, so builds from such a dist +tarball can correctly determine whether they are building a release or +a non-release source tree. However, only builds using the autotools +buildsystem are possible as long as the `configure.ac` file is located +inside the `src/` subdirectory instead of the top-level directory. + + +The `build-helpers/versioninfo.sh` script file +---------------------------------------------- + +The `versioninfo.sh` script tries to determine the version and git +information from the raw sources: + + * The version number (`7.3`) is determined from the top-level + `CMakeLists.txt` file's `project(... VERSION ...)` line. + + * The information on the latest git commit date and hash and git tag + hash is determined from the `.git/` subdirectory corresponding to + the top-level `CMakeLIsts.txt` file. + +The `versioninfo.sh` script then prints these items, one per line. + + +The `build-helpers/versioninfo.m4` m4 include file +-------------------------------------------------- + +This defines the two parts of the m4 code: + + * The m4 code which calls `versioninfo.sh`, parses its output, and + defines some m4 macros before `AC_INIT`: + + * The `versioninfo_items` macro describes the sequence of + versioninfo items printed in lines by the `versioninfo.sh` + script + + * The `versioninfo_init` macro runs the `versioninfo.sh` script + and parses its output, `m4_define`ing a `versioninfo_` + macro for each `ITEM` from `versioninfo_items` + + * The `versioninfo_stampfile` macro contains the name of the + stamp file which caches the versioninfo items for use in + tarballs. + + * The `versioninfo_init` macro is evaluated once, so that + `versioninfo.sh` is only run once as well. + + The m4 macros with the parsed results can then be evaluated using + `m4_defn(...)` both in the `AC_INIT(...)` invocation for the + `PACKAGE_VERSION`, and also later for defining + `AVRDUDE_FULL_VERSION` and git commit information. + + * The m4 code `AC_DEFUN`s the `VERSIONINFO_SETUP` macro, which in + turn defines `AC_SUBST` substitution and make variables for each + of the versioninfo items, and also defines a few + stubstitution-only (not make) variables for use in make rules in + `versioninfo.mk` and `GNUmakefile.in`, which write, read, parse, + and compare the versioninfo items as defined in + `versioninfo_items`. + + +The `build-helpers/versioninfo.mk` Makefile include file +-------------------------------------------------------- + +To be `include`d into the top-level `Makefile.am`, this does two things: + + * Hooks into the `dist` and `distcheck` targets to check the + recorded version information is the same one `versioninfo.sh` + would detect right now, and prevents builds with out of date + versioninfo items. + + * Writes the stamp file with the versioninfo data when creating a + dist tarball. + + +The `configure.ac` file +----------------------- + +Before `AC_INIT`, uses m4 logic to evaluate what the `PACKAGE_VERSION` +should be. + +Expands the `VERSIONINFO_SETUP` macro both to run its content and to +force `aclocal` to pull in `versioninfo.m4` into `aclocal.m4` so that +the m4 code from `versioninfo.m4` is evaluated before `AC_INIT`. + +Evaluates the `AVRDUDE_FULL_VERSION` message and adds it as both a C +preprocessor macro and a autoconf substitution and make variable. + + +The `GNUmakefile.in` and `GNUmakefile` files +-------------------------------------------- + +If you are running GNU make, the `GNUmakefile` file generated from the +`GNUmakefile.in` file will update the versioninfo automatically before +executing any possible make recipes. + +If you are not running GNU make, you will need to run the command to +update the versioninfo update command manually. You will be shown that +command if necessary. diff --git a/src/build-helpers/versioninfo.mk b/src/build-helpers/versioninfo.mk new file mode 100644 index 000000000..7978bc2ec --- /dev/null +++ b/src/build-helpers/versioninfo.mk @@ -0,0 +1,31 @@ +# -*- makefile-automake -*- + +EXTRA_DIST += build-helpers/versioninfo.m4 +EXTRA_DIST += build-helpers/versioninfo.md +EXTRA_DIST += build-helpers/versioninfo.sh + +# Before creating dist tarballs, check that autom4te version matches +# versioninfo script version. +dist-hook: versioninfo-check versioninfo-stamp +distcheck-hook: versioninfo-check + +# Note: We cannot run autoreconf from this make recipe, because we would +# need some way to restart the whole dist process from the start +# and there is none. +versioninfo-check: + @:; \ + $(top_srcdir)/build-helpers/versioninfo.sh "$(top_srcdir)" "@VERSIONINFO_STAMPFILE@" \ + | ( @VERSIONINFO_READ@; \ + if @VERSIONINFO_IS_UNCHANGED@ \ + exit 0; \ + fi; \ + rm -rf "$(top_srcdir)/autom4te.cache"; \ + echo "Update the recorded version information by re-running bootstrap/autoreconf(1)."; \ + exit 1; ) + +# Version stamp files can only exist in tarball source trees. +# +# So there is no need to generate them anywhere else or to clean them +# up anywhere. +versioninfo-stamp: + @VERSIONINFO_WRITE@ > "$(distdir)/versioninfo-stamp" diff --git a/src/build-helpers/versioninfo.sh b/src/build-helpers/versioninfo.sh new file mode 100755 index 000000000..58ebc9c6d --- /dev/null +++ b/src/build-helpers/versioninfo.sh @@ -0,0 +1,118 @@ +#!/bin/sh +# Syntax: +# $0 +# +# may be relative +# is relative to the dist top_srcdir +# +# Test this script by running something like +# rm -rf autom4te.cache/ && autoreconf -vis . && sed -n "/^# Generated by GNU/p" configure +# +# On error handling +# ----------------- +# +# If we run into an error, we write an error message to stderr and exit +# with a non-0 code. The caller can then check the exit code, and the +# user can read the message from stderr. + + +# Parse the command line arguments +prog="$(basename "$0")" +top_srcdir="${1-.}" +versioninfo_stamp="${2-versioninfo-stamp}" + + +# global settings +verbose=false + + +# Usage example: +# ret_error "Something is wrong: answer != 42" +ret_error() { + printf "$prog: error: %s\n" "$1" >&2 + exit 1 +} + + +# Usage examples: +# ret_ok "7.3" +# ret_ok "7.3" "20231231" +# ret_ok "7.3" "20231231" "0123abcd" +# ret_ok "7.3" "20231231" "0123abcd" "4567cdef" +ret_ok() { + if $verbose; then + printf "$prog: version info from cmake/git: %s\n" "$*" >&2 + fi + printf "%s\n" "$@" + exit 0 +} + + +# Usage example: +# ret_ok_file "$top_srcdir/$versioninfo_stamp" +ret_ok_file() { + if $verbose; then + printf "$prog: version information from file: %s\n" "$1" >&2 + fi + cat "$1" + exit 0 +} + + +# Uncomment the following line to help debug the error case +# ret_error "Some error happened for testing purposes" + + +test -d "$top_srcdir" || \ + ret_error "Could not change to top_srcdir '$1'" + + +# If this is a "make dist" source tree, use info from versioninfo_stamp file. +if test -f "$top_srcdir/$versioninfo_stamp"; then + ret_ok_file "$top_srcdir/$versioninfo_stamp" +fi + + +# This must be part of a git checkout or an expanded github snapshot tarball. +test -f "$top_srcdir/../CMakeLists.txt" || \ + ret_error "top-level avrdude CMakeLists.txt file not found" + + +# Find and parse "project(...)" line in top-level CmakeLists.txt file +if PROJECT_VERSION="$(${SED-sed} -n 's/project(avrdude[[:space:]]\{1,\}VERSION[[:space:]]\{1,\}\([0-9\.]\{1,\}\)[[:space:]]\{1,\}.*/\1/p' "$top_srcdir/../CMakeLists.txt")"; then + : +else + ret_error "Error parsing top-level avrdude 'CMakeLists.txt'." +fi + +test -n "$PROJECT_VERSION" || \ + ret_error "Cannot find project(...) in top-level avrdude 'CMakeLists.txt'" + + +# If GIT_DIR is set, use it. If not, try "$top_srcdir/../.git". +test -n "$GIT_DIR" || { \ + GIT_DIR="$top_srcdir/../.git"; \ + export GIT_DIR; \ +} + + +# If working with a git source tree, determine git information +if test -d "$GIT_DIR" && ${GIT-git} --version > /dev/null 2>&1; then + GIT_COMMIT_HASH="$(${GIT-git} log -1 --format=%h)" || \ + ret_error "$prog: Cannot run 'git log' for commit hash" + GIT_COMMIT_DATE="$(${GIT-git} log -1 --format=%cd --date=format:%Y%m%d)" || \ + ret_error "$prog: Error: Cannot run 'git log' for commit date" + GIT_TAG_HASH="$(${GIT-git} log -1 --tags --format=%h)" || \ + ret_error "$prog: Cannot run 'git log' for tag hash" + # This must be the same sequence as versioninfo_items in configure.ac + ret_ok "${PROJECT_VERSION}" \ + "${GIT_COMMIT_DATE}" "${GIT_COMMIT_HASH}" "${GIT_TAG_HASH}" +else # This is a github release tarball or github snapshot tarball + # Presume this is a release version, because who would build a + # non-release version from a snapshot tarball? + ret_ok "${PROJECT_VERSION}" +fi + + +# This code should never be reached. +ret_error "$prog: This code should never be reached." diff --git a/src/msvc/getopt.c b/src/msvc/getopt.c new file mode 100644 index 000000000..9a0347d49 --- /dev/null +++ b/src/msvc/getopt.c @@ -0,0 +1,563 @@ +/* $OpenBSD: getopt_long.c,v 1.23 2007/10/31 12:34:57 chl Exp $ */ +/* $NetBSD: getopt_long.c,v 1.15 2002/01/31 22:43:40 tv Exp $ */ + +/* + * Copyright (c) 2002 Todd C. Miller + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * 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. + * + * Sponsored in part by the Defense Advanced Research Projects + * Agency (DARPA) and Air Force Research Laboratory, Air Force + * Materiel Command, USAF, under agreement number F39502-99-1-0512. + */ +/*- + * Copyright (c) 2000 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Dieter Baron and Thomas Klausner. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#define WIN32_LEAN_AND_MEAN +#include +#include +#include +#include +#include +#include +#include + +#define REPLACE_GETOPT /* use this getopt as the system getopt(3) */ + +#ifdef REPLACE_GETOPT +int opterr = 1; /* if error message should be printed */ +int optind = 1; /* index into parent argv vector */ +int optopt = '?'; /* character checked for validity */ +#undef optreset /* see getopt.h */ +#define optreset __mingw_optreset +int optreset; /* reset getopt */ +char *optarg; /* argument associated with option */ +#endif + +#define PRINT_ERROR ((opterr) && (*options != ':')) + +#define FLAG_PERMUTE 0x01 /* permute non-options to the end of argv */ +#define FLAG_ALLARGS 0x02 /* treat non-options as args to option "-1" */ +#define FLAG_LONGONLY 0x04 /* operate as getopt_long_only */ + +/* return values */ +#define BADCH (int)'?' +#define BADARG ((*options == ':') ? (int)':' : (int)'?') +#define INORDER (int)1 + +#ifndef __CYGWIN__ +#define __progname __argv[0] +#else +extern char __declspec(dllimport) *__progname; +#endif + +#ifdef __CYGWIN__ +static char EMSG[] = ""; +#else +#define EMSG "" +#endif + +static int getopt_internal(int, char * const *, const char *, + const struct option *, int *, int); +static int parse_long_options(char * const *, const char *, + const struct option *, int *, int); +static int gcd(int, int); +static void permute_args(int, int, int, char * const *); + +static char *place = EMSG; /* option letter processing */ + +/* XXX: set optreset to 1 rather than these two */ +static int nonopt_start = -1; /* first non option argument (for permute) */ +static int nonopt_end = -1; /* first option after non options (for permute) */ + +/* Error messages */ +static const char recargchar[] = "option requires an argument -- %c"; +static const char recargstring[] = "option requires an argument -- %s"; +static const char ambig[] = "ambiguous option -- %.*s"; +static const char noarg[] = "option doesn't take an argument -- %.*s"; +static const char illoptchar[] = "unknown option -- %c"; +static const char illoptstring[] = "unknown option -- %s"; + +static void +_vwarnx(const char *fmt,va_list ap) +{ + (void)fprintf(stderr,"%s: ",__progname); + if (fmt != NULL) + (void)vfprintf(stderr,fmt,ap); + (void)fprintf(stderr,"\n"); +} + +static void +warnx(const char *fmt,...) +{ + va_list ap; + va_start(ap,fmt); + _vwarnx(fmt,ap); + va_end(ap); +} + +/* + * Compute the greatest common divisor of a and b. + */ +static int +gcd(int a, int b) +{ + int c; + + c = a % b; + while (c != 0) { + a = b; + b = c; + c = a % b; + } + + return (b); +} + +/* + * Exchange the block from nonopt_start to nonopt_end with the block + * from nonopt_end to opt_end (keeping the same order of arguments + * in each block). + */ +static void +permute_args(int panonopt_start, int panonopt_end, int opt_end, + char * const *nargv) +{ + int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos; + char *swap; + + /* + * compute lengths of blocks and number and size of cycles + */ + nnonopts = panonopt_end - panonopt_start; + nopts = opt_end - panonopt_end; + ncycle = gcd(nnonopts, nopts); + cyclelen = (opt_end - panonopt_start) / ncycle; + + for (i = 0; i < ncycle; i++) { + cstart = panonopt_end+i; + pos = cstart; + for (j = 0; j < cyclelen; j++) { + if (pos >= panonopt_end) + pos -= nnonopts; + else + pos += nopts; + swap = nargv[pos]; + /* LINTED const cast */ + ((char **) nargv)[pos] = nargv[cstart]; + /* LINTED const cast */ + ((char **)nargv)[cstart] = swap; + } + } +} + +/* + * parse_long_options -- + * Parse long options in argc/argv argument vector. + * Returns -1 if short_too is set and the option does not match long_options. + */ +static int +parse_long_options(char * const *nargv, const char *options, + const struct option *long_options, int *idx, int short_too) +{ + char *current_argv, *has_equal; + size_t current_argv_len; + int i, ambiguous, match; + +#define IDENTICAL_INTERPRETATION(_x, _y) \ + (long_options[(_x)].has_arg == long_options[(_y)].has_arg && \ + long_options[(_x)].flag == long_options[(_y)].flag && \ + long_options[(_x)].val == long_options[(_y)].val) + + current_argv = place; + match = -1; + ambiguous = 0; + + optind++; + + if ((has_equal = strchr(current_argv, '=')) != NULL) { + /* argument found (--option=arg) */ + current_argv_len = has_equal - current_argv; + has_equal++; + } else + current_argv_len = strlen(current_argv); + + for (i = 0; long_options[i].name; i++) { + /* find matching long option */ + if (strncmp(current_argv, long_options[i].name, + current_argv_len)) + continue; + + if (strlen(long_options[i].name) == current_argv_len) { + /* exact match */ + match = i; + ambiguous = 0; + break; + } + /* + * If this is a known short option, don't allow + * a partial match of a single character. + */ + if (short_too && current_argv_len == 1) + continue; + + if (match == -1) /* partial match */ + match = i; + else if (!IDENTICAL_INTERPRETATION(i, match)) + ambiguous = 1; + } + if (ambiguous) { + /* ambiguous abbreviation */ + if (PRINT_ERROR) + warnx(ambig, (int)current_argv_len, + current_argv); + optopt = 0; + return (BADCH); + } + if (match != -1) { /* option found */ + if (long_options[match].has_arg == no_argument + && has_equal) { + if (PRINT_ERROR) + warnx(noarg, (int)current_argv_len, + current_argv); + /* + * XXX: GNU sets optopt to val regardless of flag + */ + if (long_options[match].flag == NULL) + optopt = long_options[match].val; + else + optopt = 0; + return (BADARG); + } + if (long_options[match].has_arg == required_argument || + long_options[match].has_arg == optional_argument) { + if (has_equal) + optarg = has_equal; + else if (long_options[match].has_arg == + required_argument) { + /* + * optional argument doesn't use next nargv + */ + optarg = nargv[optind++]; + } + } + if ((long_options[match].has_arg == required_argument) + && (optarg == NULL)) { + /* + * Missing argument; leading ':' indicates no error + * should be generated. + */ + if (PRINT_ERROR) + warnx(recargstring, + current_argv); + /* + * XXX: GNU sets optopt to val regardless of flag + */ + if (long_options[match].flag == NULL) + optopt = long_options[match].val; + else + optopt = 0; + --optind; + return (BADARG); + } + } else { /* unknown option */ + if (short_too) { + --optind; + return (-1); + } + if (PRINT_ERROR) + warnx(illoptstring, current_argv); + optopt = 0; + return (BADCH); + } + if (idx) + *idx = match; + if (long_options[match].flag) { + *long_options[match].flag = long_options[match].val; + return (0); + } else + return (long_options[match].val); +#undef IDENTICAL_INTERPRETATION +} + +/* + * getopt_internal -- + * Parse argc/argv argument vector. Called by user level routines. + */ +static int +getopt_internal(int nargc, char * const *nargv, const char *options, + const struct option *long_options, int *idx, int flags) +{ + char *oli; /* option letter list index */ + int optchar, short_too; + static int posixly_correct = -1; + + if (options == NULL) + return (-1); + + /* + * XXX Some GNU programs (like cvs) set optind to 0 instead of + * XXX using optreset. Work around this braindamage. + */ + if (optind == 0) + optind = optreset = 1; + + /* + * Disable GNU extensions if POSIXLY_CORRECT is set or options + * string begins with a '+'. + * + * CV, 2009-12-14: Check POSIXLY_CORRECT anew if optind == 0 or + * optreset != 0 for GNU compatibility. + */ + if (posixly_correct == -1 || optreset != 0) + posixly_correct = (getenv("POSIXLY_CORRECT") != NULL); + if (*options == '-') + flags |= FLAG_ALLARGS; + else if (posixly_correct || *options == '+') + flags &= ~FLAG_PERMUTE; + if (*options == '+' || *options == '-') + options++; + + optarg = NULL; + if (optreset) + nonopt_start = nonopt_end = -1; +start: + if (optreset || !*place) { /* update scanning pointer */ + optreset = 0; + if (optind >= nargc) { /* end of argument vector */ + place = EMSG; + if (nonopt_end != -1) { + /* do permutation, if we have to */ + permute_args(nonopt_start, nonopt_end, + optind, nargv); + optind -= nonopt_end - nonopt_start; + } + else if (nonopt_start != -1) { + /* + * If we skipped non-options, set optind + * to the first of them. + */ + optind = nonopt_start; + } + nonopt_start = nonopt_end = -1; + return (-1); + } + if (*(place = nargv[optind]) != '-' || + (place[1] == '\0' && strchr(options, '-') == NULL)) { + place = EMSG; /* found non-option */ + if (flags & FLAG_ALLARGS) { + /* + * GNU extension: + * return non-option as argument to option 1 + */ + optarg = nargv[optind++]; + return (INORDER); + } + if (!(flags & FLAG_PERMUTE)) { + /* + * If no permutation wanted, stop parsing + * at first non-option. + */ + return (-1); + } + /* do permutation */ + if (nonopt_start == -1) + nonopt_start = optind; + else if (nonopt_end != -1) { + permute_args(nonopt_start, nonopt_end, + optind, nargv); + nonopt_start = optind - + (nonopt_end - nonopt_start); + nonopt_end = -1; + } + optind++; + /* process next argument */ + goto start; + } + if (nonopt_start != -1 && nonopt_end == -1) + nonopt_end = optind; + + /* + * If we have "-" do nothing, if "--" we are done. + */ + if (place[1] != '\0' && *++place == '-' && place[1] == '\0') { + optind++; + place = EMSG; + /* + * We found an option (--), so if we skipped + * non-options, we have to permute. + */ + if (nonopt_end != -1) { + permute_args(nonopt_start, nonopt_end, + optind, nargv); + optind -= nonopt_end - nonopt_start; + } + nonopt_start = nonopt_end = -1; + return (-1); + } + } + + /* + * Check long options if: + * 1) we were passed some + * 2) the arg is not just "-" + * 3) either the arg starts with -- we are getopt_long_only() + */ + if (long_options != NULL && place != nargv[optind] && + (*place == '-' || (flags & FLAG_LONGONLY))) { + short_too = 0; + if (*place == '-') + place++; /* --foo long option */ + else if (*place != ':' && strchr(options, *place) != NULL) + short_too = 1; /* could be short option too */ + + optchar = parse_long_options(nargv, options, long_options, + idx, short_too); + if (optchar != -1) { + place = EMSG; + return (optchar); + } + } + + if ((optchar = (int)*place++) == (int)':' || + (optchar == (int)'-' && *place != '\0') || + (oli = strchr(options, optchar)) == NULL) { + /* + * If the user specified "-" and '-' isn't listed in + * options, return -1 (non-option) as per POSIX. + * Otherwise, it is an unknown option character (or ':'). + */ + if (optchar == (int)'-' && *place == '\0') + return (-1); + if (!*place) + ++optind; + if (PRINT_ERROR) + warnx(illoptchar, optchar); + optopt = optchar; + return (BADCH); + } + if (long_options != NULL && optchar == 'W' && oli[1] == ';') { + /* -W long-option */ + if (*place) /* no space */ + /* NOTHING */; + else if (++optind >= nargc) { /* no arg */ + place = EMSG; + if (PRINT_ERROR) + warnx(recargchar, optchar); + optopt = optchar; + return (BADARG); + } else /* white space */ + place = nargv[optind]; + optchar = parse_long_options(nargv, options, long_options, + idx, 0); + place = EMSG; + return (optchar); + } + if (*++oli != ':') { /* doesn't take argument */ + if (!*place) + ++optind; + } else { /* takes (optional) argument */ + optarg = NULL; + if (*place) /* no white space */ + optarg = place; + else if (oli[1] != ':') { /* arg not optional */ + if (++optind >= nargc) { /* no arg */ + place = EMSG; + if (PRINT_ERROR) + warnx(recargchar, optchar); + optopt = optchar; + return (BADARG); + } else + optarg = nargv[optind]; + } + place = EMSG; + ++optind; + } + /* dump back option letter */ + return (optchar); +} + +#ifdef REPLACE_GETOPT +/* + * getopt -- + * Parse argc/argv argument vector. + * + * [eventually this will replace the BSD getopt] + */ +int +getopt(int nargc, char * const *nargv, const char *options) +{ + + /* + * We don't pass FLAG_PERMUTE to getopt_internal() since + * the BSD getopt(3) (unlike GNU) has never done this. + * + * Furthermore, since many privileged programs call getopt() + * before dropping privileges it makes sense to keep things + * as simple (and bug-free) as possible. + */ + return (getopt_internal(nargc, nargv, options, NULL, NULL, 0)); +} +#endif /* REPLACE_GETOPT */ + +/* + * getopt_long -- + * Parse argc/argv argument vector. + */ +int +getopt_long(int nargc, char * const *nargv, const char *options, + const struct option *long_options, int *idx) +{ + + return (getopt_internal(nargc, nargv, options, long_options, idx, + FLAG_PERMUTE)); +} + +/* + * getopt_long_only -- + * Parse argc/argv argument vector. + */ +int +getopt_long_only(int nargc, char * const *nargv, const char *options, + const struct option *long_options, int *idx) +{ + + return (getopt_internal(nargc, nargv, options, long_options, idx, + FLAG_PERMUTE|FLAG_LONGONLY)); +} diff --git a/src/msvc/getopt.h b/src/msvc/getopt.h new file mode 100644 index 000000000..1922a0efb --- /dev/null +++ b/src/msvc/getopt.h @@ -0,0 +1,95 @@ +#ifndef __GETOPT_H__ +/** + * DISCLAIMER + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the mingw-w64 runtime package. + * + * The mingw-w64 runtime package and its code is distributed in the hope that it + * will be useful but WITHOUT ANY WARRANTY. ALL WARRANTIES, EXPRESSED OR + * IMPLIED ARE HEREBY DISCLAIMED. This includes but is not limited to + * warranties of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + */ + +#define __GETOPT_H__ + +/* All the headers include this file. */ +#include + +#ifdef __cplusplus +extern "C" { +#endif + +extern int optind; /* index of first non-option in argv */ +extern int optopt; /* single option character, as parsed */ +extern int opterr; /* flag to enable built-in diagnostics... */ + /* (user may set to zero, to suppress) */ + +extern char *optarg; /* pointer to argument of current option */ + +extern int getopt(int nargc, char * const *nargv, const char *options); + +#ifdef _BSD_SOURCE +/* + * BSD adds the non-standard `optreset' feature, for reinitialisation + * of `getopt' parsing. We support this feature, for applications which + * proclaim their BSD heritage, before including this header; however, + * to maintain portability, developers are advised to avoid it. + */ +# define optreset __mingw_optreset +extern int optreset; +#endif +#ifdef __cplusplus +} +#endif +/* + * POSIX requires the `getopt' API to be specified in `unistd.h'; + * thus, `unistd.h' includes this header. However, we do not want + * to expose the `getopt_long' or `getopt_long_only' APIs, when + * included in this manner. Thus, close the standard __GETOPT_H__ + * declarations block, and open an additional __GETOPT_LONG_H__ + * specific block, only when *not* __UNISTD_H_SOURCED__, in which + * to declare the extended API. + */ +#endif /* !defined(__GETOPT_H__) */ + +#if !defined(__UNISTD_H_SOURCED__) && !defined(__GETOPT_LONG_H__) +#define __GETOPT_LONG_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +struct option /* specification for a long form option... */ +{ + const char *name; /* option name, without leading hyphens */ + int has_arg; /* does it take an argument? */ + int *flag; /* where to save its status, or NULL */ + int val; /* its associated status value */ +}; + +enum /* permitted values for its `has_arg' field... */ +{ + no_argument = 0, /* option never takes an argument */ + required_argument, /* option always requires an argument */ + optional_argument /* option may take an argument */ +}; + +extern int getopt_long(int nargc, char * const *nargv, const char *options, + const struct option *long_options, int *idx); +extern int getopt_long_only(int nargc, char * const *nargv, const char *options, + const struct option *long_options, int *idx); +/* + * Previous MinGW implementation had... + */ +#ifndef HAVE_DECL_GETOPT +/* + * ...for the long form API only; keep this for compatibility. + */ +# define HAVE_DECL_GETOPT 1 +#endif + +#ifdef __cplusplus +} +#endif + +#endif /* !defined(__UNISTD_H_SOURCED__) && !defined(__GETOPT_LONG_H__) */ diff --git a/src/msvc/gettimeofday.c b/src/msvc/gettimeofday.c new file mode 100644 index 000000000..eb62d0c77 --- /dev/null +++ b/src/msvc/gettimeofday.c @@ -0,0 +1,77 @@ +/** +* This file has no copyright assigned and is placed in the Public Domain. +* This file is part of the mingw-w64 runtime package. +* No warranty is given; refer to the file DISCLAIMER.PD within this package. +*/ +#define WIN32_LEAN_AND_MEAN +#include +#include +#include +#include +#include + +#define FILETIME_1970 116444736000000000ull /* seconds between 1/1/1601 and 1/1/1970 */ +#define HECTONANOSEC_PER_SEC 10000000ull + +int getntptimeofday(struct timespec *, struct timezone *); + +int getntptimeofday(struct timespec *tp, struct timezone *z) +{ + int res = 0; + union + { + unsigned long long ns100; /*time since 1 Jan 1601 in 100ns units */ + FILETIME ft; + } _now; + TIME_ZONE_INFORMATION TimeZoneInformation; + DWORD tzi; + + if (z != NULL) + { + if ((tzi = GetTimeZoneInformation(&TimeZoneInformation)) != TIME_ZONE_ID_INVALID) + { + z->tz_minuteswest = TimeZoneInformation.Bias; + if (tzi == TIME_ZONE_ID_DAYLIGHT) + z->tz_dsttime = 1; + else + z->tz_dsttime = 0; + } + else + { + z->tz_minuteswest = 0; + z->tz_dsttime = 0; + } + } + + if (tp != NULL) + { + GetSystemTimeAsFileTime(&_now.ft); /* 100-nanoseconds since 1-1-1601 */ + /* The actual accuracy on XP seems to be 125,000 nanoseconds = 125 microseconds = 0.125 milliseconds */ + _now.ns100 -= FILETIME_1970; /* 100 nano-seconds since 1-1-1970 */ + tp->tv_sec = _now.ns100 / HECTONANOSEC_PER_SEC; /* seconds since 1-1-1970 */ + tp->tv_nsec = (long)(_now.ns100 % HECTONANOSEC_PER_SEC) * 100; /* nanoseconds */ + } + return res; +} + +int __cdecl gettimeofday(struct timeval *p, void *z) +{ + struct timespec tp; + + if (getntptimeofday(&tp, (struct timezone *) z)) + return -1; + p->tv_sec = (long)tp.tv_sec; + p->tv_usec = (tp.tv_nsec / 1000); + return 0; +} + +int __cdecl mingw_gettimeofday(struct timeval *p, struct timezone *z) +{ + struct timespec tp; + + if (getntptimeofday(&tp, z)) + return -1; + p->tv_sec = (long)tp.tv_sec; + p->tv_usec = (tp.tv_nsec / 1000); + return 0; +} diff --git a/src/msvc/msvc_compat.h b/src/msvc/msvc_compat.h new file mode 100644 index 000000000..9ccddaafc --- /dev/null +++ b/src/msvc/msvc_compat.h @@ -0,0 +1,58 @@ +/* + * avrdude - A Downloader/Uploader for AVR device programmers + * Copyright (C) 2018 Marius Greuel + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once +#include +#include +#include +#include +#include + +#define strerror_r(errno,buf,len) strerror_s(buf,len,errno) + +#define R_OK 4 +#define W_OK 2 +#define X_OK 1 +#define F_OK 0 + +#define PATH_MAX _MAX_PATH + +#define setvbuf msvc_setvbuf + +static inline int msvc_setvbuf( + FILE* const public_stream, + char* const buffer, + int const type, + size_t const buffer_size_in_bytes +) +{ + // Just ignore calls to setvbuf with invalid buffer size. + // Purpose of setvbuf calls unknown, probably in an attempt to fix broken + // programs that capture stdout and stderr using separate stream handles? + return 0; +} + +static inline int strcasecmp(const char* s1, const char* s2) +{ + return _stricmp(s1, s2); +} + +static inline int strncasecmp(const char* s1, const char* s2, size_t n) +{ + return _strnicmp(s1, s2, n); +} diff --git a/src/msvc/readline.cpp b/src/msvc/readline.cpp new file mode 100644 index 000000000..4fed1b1e5 --- /dev/null +++ b/src/msvc/readline.cpp @@ -0,0 +1,95 @@ +// +// readline.cpp +// Copyright (C) 2022 Marius Greuel +// SPDX-License-Identifier: GPL-2.0-or-later +// + +#include +#include +#include +#include +#include +#include +#include "readline/readline.h" +#include "readline/history.h" + +int rl_readline_version = 0x0502; + +static rl_vcpfunc_t* rl_handler; +static std::unique_ptr rl_thread; +static std::mutex rl_mutex; +static std::string rl_line; +static bool rl_has_line = false; + +static void get_line_thread() +{ + std::string line; + std::getline(std::cin, line); + + const std::lock_guard lock(rl_mutex); + rl_line = line; + rl_has_line = true; +} + +static void call_handler(const char* string) +{ + if (rl_thread) + { + rl_thread->join(); + rl_thread = nullptr; + } + + if (rl_handler != nullptr) + { + if (string == nullptr) + { + rl_handler(nullptr); + } + else + { + rl_handler(_strdup(string)); + } + } +} + +int rl_input_available(void) +{ + return 1; +} + +void rl_callback_read_char(void) +{ + if (std::cin.eof()) + { + call_handler(nullptr); + } + else if (!rl_thread) + { + rl_thread = std::make_unique(get_line_thread); + } + else + { + const std::lock_guard lock(rl_mutex); + if (rl_has_line) + { + rl_has_line = false; + call_handler(rl_line.c_str()); + } + } +} + +void rl_callback_handler_install(char* prompt, rl_vcpfunc_t* handler) +{ + rl_handler = handler; + + std::cout << prompt; +} + +void rl_callback_handler_remove(void) +{ + rl_handler = nullptr; +} + +void add_history(const char*) +{ +} diff --git a/src/msvc/readline/history.h b/src/msvc/readline/history.h new file mode 100644 index 000000000..86a7b0a8c --- /dev/null +++ b/src/msvc/readline/history.h @@ -0,0 +1,17 @@ +// +// history.h +// Copyright (C) 2022 Marius Greuel +// SPDX-License-Identifier: GPL-2.0-or-later +// + +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +void add_history(const char* string); + +#ifdef __cplusplus +} +#endif diff --git a/src/msvc/readline/readline.h b/src/msvc/readline/readline.h new file mode 100644 index 000000000..6a7d7a555 --- /dev/null +++ b/src/msvc/readline/readline.h @@ -0,0 +1,24 @@ +// +// readline.h +// Copyright (C) 2022 Marius Greuel +// SPDX-License-Identifier: GPL-2.0-or-later +// + +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +typedef void (rl_vcpfunc_t)(char* line); + +extern int rl_readline_version; + +int rl_input_available(void); +void rl_callback_read_char(void); +void rl_callback_handler_install(char* prompt, rl_vcpfunc_t* handler); +void rl_callback_handler_remove(void); + +#ifdef __cplusplus +} +#endif diff --git a/src/msvc/sys/time.h b/src/msvc/sys/time.h new file mode 100644 index 000000000..d5c3a8c4b --- /dev/null +++ b/src/msvc/sys/time.h @@ -0,0 +1,43 @@ +/* + * avrdude - A Downloader/Uploader for AVR device programmers + * Copyright (C) 2018 Marius Greuel + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef _TIME_H_ +#define _TIME_H_ + +// If you need both and , +// make sure you include first. +#ifndef _WINSOCKAPI_ +#ifndef _TIMEVAL_DEFINED +#define _TIMEVAL_DEFINED +struct timeval +{ + long tv_sec; + long tv_usec; +}; +#endif /* _TIMEVAL_DEFINED */ +#endif /* _WINSOCKAPI_ */ + +struct timezone +{ + int tz_minuteswest; + int tz_dsttime; +}; + +int __cdecl gettimeofday(struct timeval* p, void* z); + +#endif diff --git a/src/msvc/unistd.h b/src/msvc/unistd.h new file mode 100644 index 000000000..49d5114e7 --- /dev/null +++ b/src/msvc/unistd.h @@ -0,0 +1,38 @@ +/* + * avrdude - A Downloader/Uploader for AVR device programmers + * Copyright (C) 2018 Marius Greuel + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef _UNISTD_H +#define _UNISTD_H + +#define STDIN_FILENO _fileno(stdin) +#define STDERR_FILENO _fileno(stderr) + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +typedef unsigned int useconds_t; +int usleep(unsigned int us); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/msvc/usleep.cpp b/src/msvc/usleep.cpp new file mode 100644 index 000000000..6759b7218 --- /dev/null +++ b/src/msvc/usleep.cpp @@ -0,0 +1,93 @@ +/* + * avrdude - A Downloader/Uploader for AVR device programmers + * Copyright (C) 2018 Marius Greuel + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "unistd.h" + +#define WIN32_LEAN_AND_MEAN +#include +#include +#pragma comment(lib, "winmm.lib") + +class MicroSleep +{ +public: + MicroSleep() + { + if (::timeBeginPeriod(timerPeriod) == TIMERR_NOERROR) + { + m_resetTimerPeriod = true; + } + } + + ~MicroSleep() + { + if (m_resetTimerPeriod) + { + ::timeEndPeriod(timerPeriod); + } + } + + int Sleep(DWORD us) + { + if (us == 0) + { + return 0; + } + + LARGE_INTEGER frequency{}; + if (QueryPerformanceFrequency(&frequency)) + { + LARGE_INTEGER start{}; + QueryPerformanceCounter(&start); + + if (us > 10000) + { + ::Sleep((us - 5000) / 1000); + } + + LARGE_INTEGER end{}; + end.QuadPart = start.QuadPart + (frequency.QuadPart * us / 1000000); + + while (true) + { + LARGE_INTEGER current; + QueryPerformanceCounter(¤t); + if (current.QuadPart >= end.QuadPart) + { + break; + } + } + } + else + { + ::Sleep((us / 1000) + 1); + } + + return 0; + } + +private: + static const UINT timerPeriod = 1; // 1ms + bool m_resetTimerPeriod = false; +}; + +int usleep(unsigned int us) +{ + static MicroSleep microSleep; + return microSleep.Sleep(us); +} From 2da2e88693a4a0439a0f3a2aa941dd2f0b82ba0f Mon Sep 17 00:00:00 2001 From: koen_1711 Date: Fri, 26 Apr 2024 21:44:25 +0200 Subject: [PATCH 2/5] fix: also readd src/doc --- src/doc/.gitignore | 26 +++++ src/doc/CMakeLists.txt | 179 +++++++++++++++++++++++++++++++++ src/doc/Makefile.am | 95 +++++++++++++++++ src/doc/avrdude.css | 20 ++++ src/doc/parts.cmake | 37 +++++++ src/doc/parts_comments.txt | 5 + src/doc/programmer_types.cmake | 28 ++++++ src/doc/programmers.cmake | 28 ++++++ 8 files changed, 418 insertions(+) create mode 100644 src/doc/.gitignore create mode 100644 src/doc/CMakeLists.txt create mode 100644 src/doc/Makefile.am create mode 100644 src/doc/avrdude.css create mode 100644 src/doc/parts.cmake create mode 100644 src/doc/parts_comments.txt create mode 100644 src/doc/programmer_types.cmake create mode 100644 src/doc/programmers.cmake diff --git a/src/doc/.gitignore b/src/doc/.gitignore new file mode 100644 index 000000000..c0147403e --- /dev/null +++ b/src/doc/.gitignore @@ -0,0 +1,26 @@ +avrdude.t2d/ +avrdude.t2p/ +Makefile +Makefile.in +avrdude-html +avrdude.aux +avrdude.cp +avrdude.cps +avrdude.dvi +avrdude.fn +avrdude.info +avrdude.ky +avrdude.log +avrdude.pdf +avrdude.pg +avrdude.ps +avrdude.toc +avrdude.tp +avrdude.vr +mdate-sh +stamp-vti +texinfo.tex +version.texi +programmer_types.texi +parts.texi +programmers.texi diff --git a/src/doc/CMakeLists.txt b/src/doc/CMakeLists.txt new file mode 100644 index 000000000..9c8030c3d --- /dev/null +++ b/src/doc/CMakeLists.txt @@ -0,0 +1,179 @@ +# +# CMakeLists.txt - CMake project for AVRDUDE documentation +# Copyright (C) 2022 Marius Greuel +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# + +set(AVRDUDE_CONF "${PROJECT_BINARY_DIR}/src/avrdude.conf") + +set(TEXINFOS "${CMAKE_CURRENT_SOURCE_DIR}/avrdude.texi") +set(GENERATED_TEXINFOS + programmers.texi + programmer_types.texi + parts.texi + version.texi + ) + +string(TIMESTAMP TODAY "%d %B %Y") +set(DOCS_VERSION ${PROJECT_VERSION}) +set(DOCS_UPDATED ${TODAY}) + +find_program(MAKEINFO_EXECUTABLE NAMES makeinfo) +find_program(TEXI2HTML_EXECUTABLE NAMES texi2html) + +# ===================================== +# Custom rules for auto-generated texi +# ===================================== + +add_custom_target(avrdude_binaries DEPENDS avrdude conf) + +add_custom_command( + OUTPUT programmers.txt + DEPENDS avrdude_binaries + COMMAND $ -C ${AVRDUDE_CONF} -c ? 2>&1 | more > programmers.txt + VERBATIM + ) + +add_custom_command( + OUTPUT programmer_types.txt + DEPENDS avrdude_binaries + COMMAND $ -C ${AVRDUDE_CONF} -c ?type 2>&1 | more > programmer_types.txt + VERBATIM + ) + +add_custom_command( + OUTPUT parts.txt + DEPENDS avrdude_binaries + COMMAND $ -C ${AVRDUDE_CONF} -p ? 2>&1 | more > parts.txt + VERBATIM + ) + +add_custom_command( + OUTPUT programmers.texi + DEPENDS programmers.txt + COMMAND ${CMAKE_COMMAND} + -D TXT_FILE=programmers.txt + -D TEXI_FILE=programmers.texi + -P "${CMAKE_CURRENT_SOURCE_DIR}/programmers.cmake" + VERBATIM + ) + +add_custom_command( + OUTPUT programmer_types.texi + DEPENDS programmer_types.txt + COMMAND ${CMAKE_COMMAND} + -D TXT_FILE=programmer_types.txt + -D TEXI_FILE=programmer_types.texi + -P "${CMAKE_CURRENT_SOURCE_DIR}/programmer_types.cmake" + VERBATIM + ) + +add_custom_command( + OUTPUT parts.texi + DEPENDS parts.txt + COMMAND ${CMAKE_COMMAND} + -D TXT_FILE=parts.txt + -D TEXI_FILE=parts.texi + -D COMMENTS_FILE=${CMAKE_CURRENT_SOURCE_DIR}/parts_comments.txt + -P "${CMAKE_CURRENT_SOURCE_DIR}/parts.cmake" + VERBATIM + ) + +add_custom_command( + OUTPUT version.texi + COMMAND ${CMAKE_COMMAND} -E echo "@set EDITION ${DOCS_VERSION}" > version.texi + COMMAND ${CMAKE_COMMAND} -E echo "@set VERSION ${DOCS_VERSION}" >> version.texi + COMMAND ${CMAKE_COMMAND} -E echo "@set UPDATED ${DOCS_UPDATED}" >> version.texi + VERBATIM + ) + +# ===================================== +# Custom rules for output files +# ===================================== + +add_custom_command( + OUTPUT avrdude.info + COMMAND ${MAKEINFO_EXECUTABLE} -o avrdude.info ${TEXINFOS} + DEPENDS ${TEXINFOS} ${GENERATED_TEXINFOS} + VERBATIM + ) + +add_custom_command( + OUTPUT avrdude.dvi + COMMAND ${MAKEINFO_EXECUTABLE} + --dvi + --Xopt=--quiet + --Xopt=--build-dir=dvi + -o avrdude.dvi + ${TEXINFOS} + DEPENDS ${TEXINFOS} ${GENERATED_TEXINFOS} + VERBATIM + ) + +add_custom_command( + OUTPUT avrdude.pdf + COMMAND ${MAKEINFO_EXECUTABLE} + --pdf + --Xopt=--quiet + --Xopt=--build-dir=pdf + -o avrdude.pdf + ${TEXINFOS} + DEPENDS ${TEXINFOS} ${GENERATED_TEXINFOS} + VERBATIM + ) + +add_custom_command( + OUTPUT avrdude.ps + COMMAND ${MAKEINFO_EXECUTABLE} + --ps + --Xopt=--quiet + --Xopt=--build-dir=ps + -o avrdude.ps + ${TEXINFOS} + DEPENDS ${TEXINFOS} ${GENERATED_TEXINFOS} + VERBATIM + ) + +add_custom_command( + OUTPUT avrdude-html/avrdude.html + COMMAND ${TEXI2HTML_EXECUTABLE} + --split=node + --css-include=avrdude.css + --output=avrdude-html + -I ${CMAKE_CURRENT_BINARY_DIR} + ${TEXINFOS} + DEPENDS ${TEXINFOS} ${GENERATED_TEXINFOS} avrdude.css + VERBATIM + ) + +# ===================================== +# Custom targets for output files +# ===================================== + +add_custom_target(info ALL DEPENDS avrdude.info) +add_custom_target(dvi ALL DEPENDS avrdude.dvi) +add_custom_target(pdf ALL DEPENDS avrdude.pdf) +add_custom_target(ps ALL DEPENDS avrdude.ps) +add_custom_target(html ALL DEPENDS avrdude-html/avrdude.html) + +# ===================================== +# Install +# ===================================== + +install(FILES "${CMAKE_CURRENT_BINARY_DIR}/avrdude.info" DESTINATION ${CMAKE_INSTALL_INFODIR}) +install(FILES "${CMAKE_CURRENT_BINARY_DIR}/avrdude.dvi" DESTINATION ${CMAKE_INSTALL_DOCDIR}) +install(FILES "${CMAKE_CURRENT_BINARY_DIR}/avrdude.pdf" DESTINATION ${CMAKE_INSTALL_DOCDIR}) +install(FILES "${CMAKE_CURRENT_BINARY_DIR}/avrdude.ps" DESTINATION ${CMAKE_INSTALL_DOCDIR}) +install(DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/avrdude-html" DESTINATION ${CMAKE_INSTALL_DOCDIR}) diff --git a/src/doc/Makefile.am b/src/doc/Makefile.am new file mode 100644 index 000000000..e0b19d12d --- /dev/null +++ b/src/doc/Makefile.am @@ -0,0 +1,95 @@ +# +# avrdude - A Downloader/Uploader for AVR device programmers +# Copyright (C) 2003 Theodore A. Roth +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# + +# +# $Id$ +# + +GENERATED_TEXINFOS = \ + $(builddir)/programmers.texi \ + $(builddir)/parts.texi \ + $(builddir)/programmer_types.texi \ + $(builddir)/version.texi + +CLEANFILES = \ + $(GENERATED_TEXINFOS) \ + $(builddir)/stamp-vti + +info_TEXINFOS = avrdude.texi + +EXTRA_DIST = \ + parts_comments.txt + +all-local: info html ps pdf + +html: avrdude-html/avrdude.html + +avrdude-html/avrdude.html: $(srcdir)/$(info_TEXINFOS) $(GENERATED_TEXINFOS) + texi2html --split=node --css-include=$(srcdir)/avrdude.css $(srcdir)/$(info_TEXINFOS) + if [ -e ./avrdude.html -o -e ./avrdude_1.html ]; then \ + mkdir -p avrdude-html ; \ + mv -f *.html avrdude-html ; \ + else \ + rm -rf avrdude-html; \ + mv -f avrdude avrdude-html; \ + fi; + +$(builddir)/avrdude.info: $(GENERATED_TEXINFOS) +$(builddir)/avrdude.dvi: $(GENERATED_TEXINFOS) +$(builddir)/avrdude.pdf: $(GENERATED_TEXINFOS) + +# if it does not exist make this first +../avrdude$(EXEEXT): + $(MAKE) -C .. avrdude$(EXEEXT) + +$(builddir)/programmers.texi: ../avrdude$(EXEEXT) ../avrdude.conf Makefile + ../avrdude$(EXEEXT) -C ../avrdude.conf -c \? 2>&1 \ + | $(AWK) '$$2 ~ /^=$$/ {printf("@item @code{%s} @tab %s\n",$$1,gensub("[^=]+=[ \t]*","",1))}' \ + | sed "s# *,\? *<\?\(http://[^ \t>]*\)>\?#,@*\n@url{\1}#g" \ + >programmers.texi + +$(builddir)/programmer_types.texi: ../avrdude$(EXEEXT) ../avrdude.conf Makefile + ../avrdude$(EXEEXT) -C ../avrdude.conf -c \?type 2>&1 \ + | $(AWK) '$$2 ~ /^=$$/ {printf("@item @code{%s} @tab %s\n",$$1,gensub("[^=]+=[ \t]*","",1))}' \ + | sed "s#<\?\(http://[^ \t,>]*\)>\?#@url{\1}#g" \ + >programmer_types.texi + +$(builddir)/parts.texi: ../avrdude$(EXEEXT) ../avrdude.conf parts_comments.txt Makefile + ../avrdude$(EXEEXT) -C ../avrdude.conf -p \? 2>&1 \ + | $(AWK) '$$2 ~ /^=$$/ {printf("@item @code{%s} @tab %s\n",$$1,$$3)}' \ + | sed -e "`sed 's:\([^ \t]*\)[ \t]*\(.*\):s/\1$$/\1 \2/g:g' parts.texi + +clean-local: + rm -rf avrdude-html *.info + +install-data-local: install-docs + +install-docs: html ps pdf + $(mkinstalldirs) $(DOC_INST_DIR) + $(INSTALL_DATA) avrdude.ps $(DOC_INST_DIR)/avrdude.ps + $(INSTALL_DATA) avrdude.pdf $(DOC_INST_DIR)/avrdude.pdf + $(mkinstalldirs) $(DOC_INST_DIR)/avrdude-html + @list=`echo avrdude-html/*.html`; \ + for file in $$list; \ + do \ + $(INSTALL_DATA) $$file $(DOC_INST_DIR)/$$file; \ + done + +uninstall-local: + rm -rf $(DOC_INST_DIR) diff --git a/src/doc/avrdude.css b/src/doc/avrdude.css new file mode 100644 index 000000000..c5cba5ba1 --- /dev/null +++ b/src/doc/avrdude.css @@ -0,0 +1,20 @@ +body { background-color: #ffd; } +h1 { text-shadow: .05em .05em #ccc; } +table { + border: 3px solid #ccf; + background-color: white; +} +div.smallexample { + background-color: #dfd; + border: 3px solid #cfc; +} +div.example { + background-color: #dfd; + border: 3px solid #cfc; +} +samp { + color: blue; +} +code { + color: green; +} diff --git a/src/doc/parts.cmake b/src/doc/parts.cmake new file mode 100644 index 000000000..c305743db --- /dev/null +++ b/src/doc/parts.cmake @@ -0,0 +1,37 @@ +# +# programmers.cmake - create parts.texi from parts.txt +# Copyright (C) 2022 Marius Greuel +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# + +file(STRINGS ${COMMENTS_FILE} COMMENTS_CONTENTS) + +file(STRINGS ${TXT_FILE} TXT_CONTENTS REGEX "=") + +set(TEXI_CONTENTS "") +foreach(TXT_LINE IN LISTS TXT_CONTENTS) + string(REGEX REPLACE "^[ \t]*([^ \t]+)[ \t]*=[ \t]*(.*)$" "@item @code{\\1} @tab \\2" TEXI_LINE "${TXT_LINE}") + + foreach(COMMENTS_LINE IN LISTS COMMENTS_CONTENTS) + string(REGEX MATCH "^([^ \t]*)(.*)$" DUMMY "${COMMENTS_LINE}") + set(PART_REGEX "${CMAKE_MATCH_1}") + set(COMMENT "${CMAKE_MATCH_2}") + string(REGEX REPLACE "(${PART_REGEX})" "\\1${COMMENT}" TEXI_LINE "${TEXI_LINE}") + endforeach() + + set(TEXI_CONTENTS "${TEXI_CONTENTS}${TEXI_LINE}\n") +endforeach() + +file(WRITE ${TEXI_FILE} "${TEXI_CONTENTS}") diff --git a/src/doc/parts_comments.txt b/src/doc/parts_comments.txt new file mode 100644 index 000000000..d83bb4312 --- /dev/null +++ b/src/doc/parts_comments.txt @@ -0,0 +1,5 @@ +AT90S1200 (****) +AT90S2343 (*) +ATmega2560 (**) +ATmega2561 (**) +ATtiny11 (***) diff --git a/src/doc/programmer_types.cmake b/src/doc/programmer_types.cmake new file mode 100644 index 000000000..7e62f6117 --- /dev/null +++ b/src/doc/programmer_types.cmake @@ -0,0 +1,28 @@ +# +# programmer_types.cmake - create programmer_types.texi from programmer_types.txt +# Copyright (C) 2022 Marius Greuel +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# + +file(STRINGS ${TXT_FILE} TXT_CONTENTS REGEX "=") + +SET(TEXI_CONTENTS "") +foreach(TXT_LINE IN LISTS TXT_CONTENTS) + string(REGEX REPLACE "^[ \t]*([^ \t]+)[ \t]*=[ \t]*(.*)$" "@item @code{\\1} @tab \\2" TEXI_LINE "${TXT_LINE}") + string(REGEX REPLACE "]+)>?" "@url{\\1}" TEXI_LINE "${TEXI_LINE}") + set(TEXI_CONTENTS "${TEXI_CONTENTS}${TEXI_LINE}\n") +endforeach() + +file(WRITE ${TEXI_FILE} "${TEXI_CONTENTS}") diff --git a/src/doc/programmers.cmake b/src/doc/programmers.cmake new file mode 100644 index 000000000..b8611c0f8 --- /dev/null +++ b/src/doc/programmers.cmake @@ -0,0 +1,28 @@ +# +# programmers.cmake - create programmers.texi from programmers.txt +# Copyright (C) 2022 Marius Greuel +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# + +file(STRINGS ${TXT_FILE} TXT_CONTENTS REGEX "=") + +SET(TEXI_CONTENTS "") +foreach(TXT_LINE IN LISTS TXT_CONTENTS) + string(REGEX REPLACE "^[ \t]*([^ \t]+)[ \t]*=[ \t]*(.*)$" "@item @code{\\1} @tab \\2" TEXI_LINE "${TXT_LINE}") + string(REGEX REPLACE "[ \t>]*,?[ \t>]*]+)>?" ",@*\n@url{\\1}" TEXI_LINE "${TEXI_LINE}") + set(TEXI_CONTENTS "${TEXI_CONTENTS}${TEXI_LINE}\n") +endforeach() + +file(WRITE ${TEXI_FILE} "${TEXI_CONTENTS}") From df7df2df924e6805543b48f36acb8f1ab374db9e Mon Sep 17 00:00:00 2001 From: koen_1711 Date: Fri, 26 Apr 2024 21:47:32 +0200 Subject: [PATCH 3/5] chore: sync avr910.c --- src/avr910.c | 355 ++++++++++++++++++++++++--------------------------- 1 file changed, 164 insertions(+), 191 deletions(-) diff --git a/src/avr910.c b/src/avr910.c index d6fe5d94b..06c664f91 100644 --- a/src/avr910.c +++ b/src/avr910.c @@ -88,7 +88,7 @@ static void avr910_teardown(PROGRAMMER * pgm) { static int avr910_send(const PROGRAMMER *pgm, char *buf, size_t len) { - return serial_send(&pgm->fd, (unsigned char *)buf, len); + return serial_send(&pgm->fd, (unsigned char *)buf, len); } @@ -98,12 +98,12 @@ static int avr910_recv(const PROGRAMMER *pgm, char *buf, size_t len) { static int avr910_drain(const PROGRAMMER *pgm, int display) { - return serial_drain(&pgm->fd, display); + return serial_drain(&pgm->fd, display); } static int avr910_vfy_cmd_sent(const PROGRAMMER *pgm, char *errmsg) { - char c; + char c; EI(avr910_recv(pgm, &c, 1)); if (c != '\r') { @@ -123,12 +123,12 @@ static int avr910_chip_erase(const PROGRAMMER *pgm, const AVRPART *p) { if (avr910_vfy_cmd_sent(pgm, "chip erase") < 0) return -1; - /* - * avr910 firmware may not delay long enough - */ - usleep (p->chip_erase_delay); + /* + * avr910 firmware may not delay long enough + */ + usleep (p->chip_erase_delay); - return 0; + return 0; } @@ -153,22 +153,22 @@ static int avr910_program_enable(const PROGRAMMER *pgm, const AVRPART *p) { * initialize the AVR device and prepare it to accept commands */ static int avr910_initialize(const PROGRAMMER *pgm, const AVRPART *p) { - char id[8]; - char sw[2]; - char hw[2]; - char buf[10]; - char type; - char c; - AVRPART * part; + char id[8]; + char sw[2]; + char hw[2]; + char buf[10]; + char type; + char c; + AVRPART * part; - /* Get the programmer identifier. Programmer returns exactly 7 chars - _without_ the null.*/ + /* Get the programmer identifier. Programmer returns exactly 7 chars + _without_ the null.*/ EI(avr910_send(pgm, "S", 1)); memset (id, 0, sizeof(id)); EI(avr910_recv(pgm, id, sizeof(id)-1)); - /* Get the HW and SW versions to see if the programmer is present. */ + /* Get the HW and SW versions to see if the programmer is present. */ EI(avr910_send(pgm, "V", 1)); EI(avr910_recv(pgm, sw, sizeof(sw))); @@ -176,23 +176,23 @@ static int avr910_initialize(const PROGRAMMER *pgm, const AVRPART *p) { EI(avr910_send(pgm, "v", 1)); EI(avr910_recv(pgm, hw, sizeof(hw))); - /* Get the programmer type (serial or parallel). Expect serial. */ + /* Get the programmer type (serial or parallel). Expect serial. */ EI(avr910_send(pgm, "p", 1)); EI(avr910_recv(pgm, &type, 1)); - msg_notice("Programmer id = %s; type = %c\n", id, type); - msg_notice("Software version = %c.%c; ", sw[0], sw[1]); - msg_notice("Hardware version = %c.%c\n", hw[0], hw[1]); + msg_notice("Programmer id = %s; type = %c\n", id, type); + msg_notice("Software version = %c.%c; ", sw[0], sw[1]); + msg_notice("Hardware version = %c.%c\n", hw[0], hw[1]); - /* See if programmer supports autoincrement of address. */ + /* See if programmer supports autoincrement of address. */ EI(avr910_send(pgm, "a", 1)); EI(avr910_recv(pgm, &PDATA(pgm)->has_auto_incr_addr, 1)); if (PDATA(pgm)->has_auto_incr_addr == 'Y') msg_notice("programmer supports auto addr increment\n"); - /* Check support for buffered memory access, ignore if not available */ + /* Check support for buffered memory access, ignore if not available */ if (PDATA(pgm)->test_blockmode == 1) { EI(avr910_send(pgm, "b", 1)); @@ -207,14 +207,17 @@ static int avr910_initialize(const PROGRAMMER *pgm, const AVRPART *p) { PDATA(pgm)->buffersize); PDATA(pgm)->use_blockmode = 1; } else { - PDATA(pgm)->use_blockmode = 0; + PDATA(pgm)->use_blockmode = 0; } + } else { + PDATA(pgm)->use_blockmode = 0; + } - if (PDATA(pgm)->devcode == 0) { - char devtype_1st; - int dev_supported = 0; + if (PDATA(pgm)->devcode == 0) { + char devtype_1st; + int dev_supported = 0; - /* Get list of devices that the programmer supports. */ + /* Get list of devices that the programmer supports. */ EI(avr910_send(pgm, "t", 1)); msg_notice2("\nProgrammer supports the following devices:\n"); @@ -227,40 +230,40 @@ static int avr910_initialize(const PROGRAMMER *pgm, const AVRPART *p) { break; part = locate_part_by_avr910_devcode(part_list, c); - msg_notice2(" Device code: 0x%02x = %s\n", c & 0xff, part? part->desc: "(unknown)"); - - /* FIXME: Need to lookup devcode and report the device. */ - - if (p->avr910_devcode == c) - dev_supported = 1; - }; - msg_notice2("\n"); - - if (!dev_supported) { - if(ovsigck) - pmsg_warning("selected device %s is not supported by programmer %s\n", p->desc, pgmid); - else { - pmsg_error("selected device %s is not supported by programmer %s\n", p->desc, pgmid); - return -1; - } - } - /* If the user forced the selection, use the first device - type that is supported by the programmer. */ - buf[1] = ovsigck? devtype_1st: p->avr910_devcode; - } else { - /* devcode overridden by -x devcode= option */ - buf[1] = (char)(PDATA(pgm)->devcode); + msg_notice2(" Device code: 0x%02x = %s\n", c & 0xff, part? part->desc: "(unknown)"); + + /* FIXME: Need to lookup devcode and report the device. */ + + if (p->avr910_devcode == c) + dev_supported = 1; + }; + msg_notice2("\n"); + + if (!dev_supported) { + if(ovsigck) + pmsg_warning("selected device %s is not supported by programmer %s\n", p->desc, pgmid); + else { + pmsg_error("selected device %s is not supported by programmer %s\n", p->desc, pgmid); + return -1; + } } + /* If the user forced the selection, use the first device + type that is supported by the programmer. */ + buf[1] = ovsigck? devtype_1st: p->avr910_devcode; + } else { + /* devcode overridden by -x devcode= option */ + buf[1] = (char)(PDATA(pgm)->devcode); + } - /* Tell the programmer which part we selected. */ - buf[0] = 'T'; - /* buf[1] has been set up above */ + /* Tell the programmer which part we selected. */ + buf[0] = 'T'; + /* buf[1] has been set up above */ EI(avr910_send(pgm, buf, 2)); if(avr910_vfy_cmd_sent(pgm, "select device") < 0) return -1; - pmsg_notice("avr910_devcode selected: 0x%02x\n", (unsigned) buf[1]); + pmsg_notice("avr910_devcode selected: 0x%02x\n", (unsigned) buf[1]); return pgm->program_enable(pgm, p); } @@ -282,64 +285,41 @@ static void avr910_enable(PROGRAMMER *pgm, const AVRPART *p) { static int avr910_cmd(const PROGRAMMER *pgm, const unsigned char *cmd, unsigned char *res) { - char buf[5]; + char buf[5]; - /* FIXME: Insert version check here */ + /* FIXME: Insert version check here */ - buf[0] = '.'; /* New Universal Command */ - buf[1] = cmd[0]; - buf[2] = cmd[1]; - buf[3] = cmd[2]; - buf[4] = cmd[3]; + buf[0] = '.'; /* New Universal Command */ + buf[1] = cmd[0]; + buf[2] = cmd[1]; + buf[3] = cmd[2]; + buf[4] = cmd[3]; EI(avr910_send(pgm, buf, 5)); EI(avr910_recv(pgm, buf, 2)); - res[0] = 0x00; /* Dummy value */ - res[1] = cmd[0]; - res[2] = cmd[1]; - res[3] = buf[0]; + res[0] = 0x00; /* Dummy value */ + res[1] = cmd[0]; + res[2] = cmd[1]; + res[3] = buf[0]; - return 0; + return 0; } static int avr910_parseextparms(const PROGRAMMER *pgm, const LISTID extparms) { - LNODEID ln; - const char *extended_param; - int rv = 0; - - for (ln = lfirst(extparms); ln; ln = lnext(ln)) { - extended_param = ldata(ln); - - if (str_starts(extended_param, "devcode=")) { - int devcode; - if (sscanf(extended_param, "devcode=%i", &devcode) != 1 || - devcode <= 0 || devcode > 255) { - pmsg_error("invalid devcode '%s'\n", extended_param); - rv = -1; - continue; - } - pmsg_notice2("avr910_parseextparms(): devcode overwritten as 0x%02x\n", devcode); - PDATA(pgm)->devcode = devcode; - - continue; - } - if (str_eq(extended_param, "no_blockmode")) { - pmsg_notice2("avr910_parseextparms(-x): no testing for Blockmode\n"); - PDATA(pgm)->test_blockmode = 0; - - continue; - } - if (str_eq(extended_param, "help")) { - msg_error("%s -c %s extended options:\n", progname, pgmid); - msg_error(" -xdevcode= Override device code\n"); - msg_error(" -xno_blockmode Disable default checking for block transfer capability\n"); - msg_error(" -xhelp Show this help menu and exit\n"); - exit(0); - } - - pmsg_error("invalid extended parameter '%s'\n", extended_param); + LNODEID ln; + const char *extended_param; + int rv = 0; + + for (ln = lfirst(extparms); ln; ln = lnext(ln)) { + extended_param = ldata(ln); + + if (str_starts(extended_param, "devcode=")) { + int devcode; + if (sscanf(extended_param, "devcode=%i", &devcode) != 1 || + devcode <= 0 || devcode > 255) { + pmsg_error("invalid devcode '%s'\n", extended_param); rv = -1; continue; } @@ -362,7 +342,11 @@ static int avr910_parseextparms(const PROGRAMMER *pgm, const LISTID extparms) { return LIBAVRDUDE_EXIT; } - return rv; + pmsg_error("invalid extended parameter '%s'\n", extended_param); + rv = -1; + } + + return rv; } @@ -390,12 +374,12 @@ static void avr910_close(PROGRAMMER *pgm) { static void avr910_display(const PROGRAMMER *pgm, const char *p) { - return; + return; } static void avr910_set_addr(const PROGRAMMER *pgm, unsigned long addr) { - char cmd[3]; + char cmd[3]; cmd[0] = 'A'; cmd[1] = (addr >> 8) & 0xff; @@ -409,7 +393,7 @@ static void avr910_set_addr(const PROGRAMMER *pgm, unsigned long addr) { static int avr910_write_byte(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM *m, unsigned long addr, unsigned char value) { - char cmd[2]; + char cmd[2]; if (mem_is_flash(m)) { if (addr & 0x01) { @@ -426,10 +410,9 @@ static int avr910_write_byte(const PROGRAMMER *pgm, const AVRPART *p, const AVRM return avr_write_byte_default(pgm, p, m, addr, value); } - avr910_set_addr(pgm, addr); + cmd[1] = value; - avr910_send(pgm, cmd, sizeof(cmd)); - avr910_vfy_cmd_sent(pgm, "write byte"); + avr910_set_addr(pgm, addr); EI(avr910_send(pgm, cmd, sizeof(cmd))); return avr910_vfy_cmd_sent(pgm, "write byte"); @@ -439,7 +422,7 @@ static int avr910_write_byte(const PROGRAMMER *pgm, const AVRPART *p, const AVRM static int avr910_read_byte_flash(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM *m, unsigned long addr, unsigned char * value) { - char buf[2]; + char buf[2]; if(PDATA(pgm)->ctype == 'F' && PDATA(pgm)->caddr == addr) { *value = PDATA(pgm)->cvalue; @@ -456,7 +439,7 @@ static int avr910_read_byte_flash(const PROGRAMMER *pgm, const AVRPART *p, const PDATA(pgm)->cvalue = buf[addr & 1]; PDATA(pgm)->caddr = addr ^ 1; - return 0; + return 0; } @@ -467,22 +450,22 @@ static int avr910_read_byte_eeprom(const PROGRAMMER *pgm, const AVRPART *p, cons EI(avr910_send(pgm, "d", 1)); EI(avr910_recv(pgm, (char *) value, 1)); - return 0; + return 0; } static int avr910_read_byte(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM *m, unsigned long addr, unsigned char * value) { - if (mem_is_flash(m)) { - return avr910_read_byte_flash(pgm, p, m, addr, value); - } + if (mem_is_flash(m)) { + return avr910_read_byte_flash(pgm, p, m, addr, value); + } - if (mem_is_eeprom(m)) { - return avr910_read_byte_eeprom(pgm, p, m, addr, value); - } + if (mem_is_eeprom(m)) { + return avr910_read_byte_eeprom(pgm, p, m, addr, value); + } - return avr_read_byte_default(pgm, p, m, addr, value); + return avr_read_byte_default(pgm, p, m, addr, value); } @@ -490,12 +473,12 @@ static int avr910_paged_write_flash(const PROGRAMMER *pgm, const AVRPART *p, con unsigned int page_size, unsigned int addr, unsigned int n_bytes) { - unsigned char cmd[] = {'c', 'C'}; - char buf[2]; - unsigned int max_addr = addr + n_bytes; - unsigned int page_addr; - int page_bytes = page_size; - int page_wr_cmd_pending = 0; + unsigned char cmd[] = {'c', 'C'}; + char buf[2]; + unsigned int max_addr = addr + n_bytes; + unsigned int page_addr; + int page_bytes = page_size; + int page_wr_cmd_pending = 0; PDATA(pgm)->ctype = 0; // Invalidate read cache @@ -510,40 +493,30 @@ static int avr910_paged_write_flash(const PROGRAMMER *pgm, const AVRPART *p, con if(avr910_vfy_cmd_sent(pgm, "write byte") < 0) return -1; - addr++; - page_bytes--; + addr++; + page_bytes--; - if (m->paged && (page_bytes == 0)) { - /* Send the "Issue Page Write" if we have sent a whole page. */ + if (m->paged && (page_bytes == 0)) { + /* Send the "Issue Page Write" if we have sent a whole page. */ avr910_set_addr(pgm, page_addr>>1); EI(avr910_send(pgm, "m", 1)); if(avr910_vfy_cmd_sent(pgm, "flush page") < 0) return -1; - page_wr_cmd_pending = 0; - usleep(m->max_write_delay); - avr910_set_addr(pgm, addr>>1); + page_wr_cmd_pending = 0; + usleep(m->max_write_delay); + avr910_set_addr(pgm, addr>>1); - /* Set page address for next page. */ + /* Set page address for next page. */ - page_addr = addr; - page_bytes = page_size; - } - else if ((PDATA(pgm)->has_auto_incr_addr != 'Y') && ((addr & 0x01) == 0)) { - avr910_set_addr(pgm, addr>>1); - } + page_addr = addr; + page_bytes = page_size; } - - /* If we didn't send the page wr cmd after the last byte written in the - loop, send it now. */ - - if (page_wr_cmd_pending) { - avr910_set_addr(pgm, page_addr>>1); - avr910_send(pgm, "m", 1); - avr910_vfy_cmd_sent(pgm, "flush final page"); - usleep(m->max_write_delay); + else if ((PDATA(pgm)->has_auto_incr_addr != 'Y') && ((addr & 0x01) == 0)) { + avr910_set_addr(pgm, addr>>1); } + } /* If we didn't send the page wr cmd after the last byte written in the loop, send it now. */ @@ -565,12 +538,12 @@ static int avr910_paged_write_eeprom(const PROGRAMMER *pgm, const AVRPART *p, unsigned int page_size, unsigned int addr, unsigned int n_bytes) { - char cmd[2]; - unsigned int max_addr = addr + n_bytes; + char cmd[2]; + unsigned int max_addr = addr + n_bytes; - avr910_set_addr(pgm, addr); + avr910_set_addr(pgm, addr); - cmd[0] = 'D'; + cmd[0] = 'D'; while (addr < max_addr) { cmd[1] = m->buf[addr]; @@ -579,7 +552,7 @@ static int avr910_paged_write_eeprom(const PROGRAMMER *pgm, const AVRPART *p, return -1; usleep(m->max_write_delay); - addr++; + addr++; if (PDATA(pgm)->has_auto_incr_addr != 'Y') avr910_set_addr(pgm, addr); @@ -655,7 +628,7 @@ static int avr910_paged_load(const PROGRAMMER *pgm, const AVRPART *p, const AVRM char buf[2]; int isee = mem_is_eeprom(m); - max_addr = addr + n_bytes; + max_addr = addr + n_bytes; if(mem_is_flash(m)) cmd[0] = 'R'; @@ -709,12 +682,12 @@ static int avr910_paged_load(const PROGRAMMER *pgm, const AVRPART *p, const AVRM /* Signature byte reads are always 3 bytes. */ static int avr910_read_sig_bytes(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM *m) { - unsigned char tmp; + unsigned char tmp; - if (m->size < 3) { - pmsg_error("memsize too small for sig byte read"); - return -1; - } + if (m->size < 3) { + pmsg_error("memsize too small for sig byte read"); + return -1; + } EI(avr910_send(pgm, "s", 1)); EI(avr910_recv(pgm, (char *) m->buf, 3)); @@ -723,40 +696,40 @@ static int avr910_read_sig_bytes(const PROGRAMMER *pgm, const AVRPART *p, const m->buf[2] = m->buf[0]; m->buf[0] = tmp; - return 3; + return 3; } const char avr910_desc[] = "Serial programmers using protocol described in application note AVR910"; void avr910_initpgm(PROGRAMMER *pgm) { - strcpy(pgm->type, "avr910"); - - /* - * mandatory functions - */ - pgm->initialize = avr910_initialize; - pgm->display = avr910_display; - pgm->enable = avr910_enable; - pgm->disable = avr910_disable; - pgm->program_enable = avr910_program_enable; - pgm->chip_erase = avr910_chip_erase; - pgm->cmd = avr910_cmd; - pgm->open = avr910_open; - pgm->close = avr910_close; - - /* - * optional functions - */ - - pgm->write_byte = avr910_write_byte; - pgm->read_byte = avr910_read_byte; - - pgm->paged_write = avr910_paged_write; - pgm->paged_load = avr910_paged_load; - - pgm->read_sig_bytes = avr910_read_sig_bytes; - - pgm->parseextparams = avr910_parseextparms; - pgm->setup = avr910_setup; - pgm->teardown = avr910_teardown; + strcpy(pgm->type, "avr910"); + + /* + * mandatory functions + */ + pgm->initialize = avr910_initialize; + pgm->display = avr910_display; + pgm->enable = avr910_enable; + pgm->disable = avr910_disable; + pgm->program_enable = avr910_program_enable; + pgm->chip_erase = avr910_chip_erase; + pgm->cmd = avr910_cmd; + pgm->open = avr910_open; + pgm->close = avr910_close; + + /* + * optional functions + */ + + pgm->write_byte = avr910_write_byte; + pgm->read_byte = avr910_read_byte; + + pgm->paged_write = avr910_paged_write; + pgm->paged_load = avr910_paged_load; + + pgm->read_sig_bytes = avr910_read_sig_bytes; + + pgm->parseextparams = avr910_parseextparms; + pgm->setup = avr910_setup; + pgm->teardown = avr910_teardown; } \ No newline at end of file From 9d21ba719c15a556625280212a9d847017ffd608 Mon Sep 17 00:00:00 2001 From: koen_1711 Date: Fri, 26 Apr 2024 21:49:27 +0200 Subject: [PATCH 4/5] chore: sync linuxgpio.c --- src/linuxgpio.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/linuxgpio.c b/src/linuxgpio.c index 7b54f6fed..5ebe7f6c5 100644 --- a/src/linuxgpio.c +++ b/src/linuxgpio.c @@ -724,4 +724,4 @@ void linuxgpio_initpgm(PROGRAMMER *pgm) { const char linuxgpio_desc[] = "GPIO bitbanging using the Linux libgpiod or sysfs interface (not available)"; -#endif /* HAVE_LINUXGPIO */ +#endif /* HAVE_LINUXGPIO */ \ No newline at end of file From 7afc2c410d647bcacb2e3a3cad0081ba72c867f4 Mon Sep 17 00:00:00 2001 From: koen_1711 Date: Fri, 26 Apr 2024 21:58:42 +0200 Subject: [PATCH 5/5] chore: bump version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 3d45a15fc..b8cf686ce 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@leaphy-robotics/avrdude-webassembly", - "version": "1.6.1", + "version": "1.6.2", "description": "An port of avrdude to the browser using WebAssembly", "type": "module", "scripts": {