Windows batch file needed

PaulStoffregen

Well-known member
Ok, I need help on a Windows batch file. Hoping some of the Windows experts here can comment...

The goal is a batch file similar to Frank's suggestion on this thread, but without the command embedded, and with the ability to pass an arbitrary number of extra args.

This is required because Arduino's platform.txt doesn't provide a way to redirect stdout to a file, and the objdump command doesn't have an option to specify an output file (presumably because all shells let you redirect stdout to a file).

Here's the Unix version for Linux and Macintosh:

Code:
#! /bin/sh
output=$1
shift
$@ > $output

It takes the first arg as the output filename to store the text, then the 2nd and all other args are the command to run.

This runs as:

stdout_redirect.sh filename command arg1 arg2 arg3 argN

Can anyone who knows Windows well suggest how to do this with a batch file? If so, we can have nice things.... :)
 
something like this? where foo.bat == "%2 %3 %4 %5 > %1"
Code:
@rem -------- test
@echo You ran: %0
@echo out file param 1: %1
@echo CMD param 2: %2
@echo param 3: %3
@echo param 4: %4
@echo param 5: %5
@rem more as needed ?
@rem -------- end test

@rem CODE BELOW.
@rem example:: foo.txt dir c:\*.bat /a /s

%2 %3 %4 %5 > %1
 
Last edited:
I ended up writing this. A shell script and batch file would be so much simpler, but then there's also the issue of needing different file names on each operating system, which Arduino's platform.txt doesn't offer. Well, I guess the file could end in .bat on Linux and Mac....

Ended up with this...

Code:
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

/*
#! /bin/sh
output=$1
shift
$@ > $output
*/

void usage(void);
void die(const char *format, ...) __attribute__ ((format (printf, 1, 2)));

int main(int argc, char **argv)
{
        int i, r, fout;
        FILE *fp;
        char **args;

        if (argc < 3) usage();
        args = (char **)malloc((argc - 1) * sizeof(char *));
        if (!(void *)args) die("unable to allocate memory");
        for (i=0; i < argc-2; i++) {
#if defined(OS_WINDOWS)
                r = strlen(argv[i + 2]) + 4;
                args[i] = (char *)malloc(r);
                if (args[i] == NULL) die("unable to allocate memory, %d bytes", r);
                snprintf(args[i], r, "\"%s\"", argv[i + 2]);
#else
                args[i] = argv[i + 2];
#endif
        }
        args[i] = NULL;
        //fout = open(argv[1], O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
        //if (fout < 0) die("unable to write \"%s\"", argv[1]);
        fp = fopen(argv[1], "w+b");
        if (fp == NULL) die("unable to write \"%s\"", argv[1]);
        fout = fileno(fp);
        dup2(fout, 1); // send stdout to file
        close(fout);
        r = execv(argv[2], args);
        if (r < 0) die("unable to run: %s", args[0]);
        die("internal error: not supposed to get here!");

        return 0;
}



void usage(void)
{
        printf("usage: stdout_redirect <file> <cmd> [args1] [arg2] [argN]\n");
        exit(0);
}

void die(const char *format, ...)
{
        va_list args;
        va_start(args, format);
        fprintf(stderr, "stdout_redirect: ");
        vfprintf(stderr, format, args);
        fprintf(stderr, "\n");
        fflush(stderr);
        exit(1);
}
 
In the next beta, a disassembly file and symbol table file will be written into the temporary folder where the code is compiled. Unfortunately on Mac and Windows these are hidden locations, but anyone who's *really* interested can find the folder and get these files without having the pain of having the run objdump.

These are the changed platform.txt lines:

Code:
## Post Build - inform Teensy Loader of new file
recipe.hooks.postbuild.1.pattern="{compiler.path}stdout_redirect" "{build.path}/{build.project_name}.lst" "{compiler.path}{build.toolchain}{build.command.objdump}" -d -S -C "{build.path}/{build.project_name}.elf"
recipe.hooks.postbuild.2.pattern="{compiler.path}stdout_redirect" "{build.path}/{build.project_name}.sym" "{compiler.path}{build.toolchain}{build.command.objdump}" -t -C "{build.path}/{build.project_name}.elf"
recipe.hooks.postbuild.3.pattern="{compiler.path}teensy_post_compile" "-file={build.project_name}" "-path={build.path}" "-tools={compiler.path}" "-board={build.board}"
 
I ended up writing this. A shell script and batch file would be so much simpler, but then there's also the issue of needing different file names on each operating system, which Arduino's platform.txt doesn't offer. Well, I guess the file could end in .bat on Linux and Mac....

I saw this on another board for OS specific actions: /master/platform.txt#L120

Is that section unique to upload or is it a usable feature in PostBuild too?
Code:
# Uploader tools

# --------------
# DFU
tools.stm32l4_dfu.cmd=stm32l4-upload
tools.stm32l4_dfu.cmd.[B]windows[/B]=stm32l4-upload.bat
tools.stm32l4_dfu.path={runtime.platform.path}/tools/windows
tools.stm32l4_dfu.path.[B]macosx[/B]={runtime.platform.path}/tools/macosx
tools.stm32l4_dfu.path.[B]linux[/B]={runtime.platform.path}/tools/linux
 
Good Morning :)

indeed, os-dependend settings are possible. See for example the "defs.h" thing:
Code:
[..]
> ## defs.h patch
> recipe.hooks.sketch.prebuild.1.pattern[COLOR=#ff0000].[B]windows[/B][/COLOR]=cmd /c if not exist "{build.path}\sketch\defs.h" "echo.>{build.path}\sketch\defs.h"
> recipe.hooks.sketch.prebuild.1.pattern.[COLOR=#ff0000][B]linux[/B][/COLOR]=bash -c "[ -f {build.path}/sketch/defs.h ] || touch {build.path}/sketch/defs.h"
> #recipe.hooks.sketch.prebuild.1.pattern.[COLOR=#ff0000][B]macosx[/B][/COLOR]=bash -c "[ -f {build.path}/sketch/defs.h ] || touch {build.path}/sketch/defs.h"
[..]
Full code is here :
https://forum.pjrc.com/threads/3853...e-F_CPU-USB-Keyboard-layout)?highlight=defs.h

MacOSX never tested.

I still use it, and it works like a charm. I use windows-ports of the linux patch/diff utility and a batch that patches my arduino installation in seconds when a new Teensyduino beta is out.
 
Last edited:
Back
Top