Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 7 of 7

Thread: Windows batch file needed

  1. #1
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    17,625

    Windows batch file needed

    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....

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    17,625
    Hmmm... maybe easiest to just build this "cross platform" with a small C program?

  3. #3
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    6,444
    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 by defragster; 08-27-2017 at 01:39 AM. Reason: forgot to not echo the kraap - or @echo off

  4. #4
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    17,625
    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);
    }

  5. #5
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    17,625
    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}"

  6. #6
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    6,444
    Quote Originally Posted by PaulStoffregen View Post
    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.windows=stm32l4-upload.bat
    tools.stm32l4_dfu.path={runtime.platform.path}/tools/windows
    tools.stm32l4_dfu.path.macosx={runtime.platform.path}/tools/macosx
    tools.stm32l4_dfu.path.linux={runtime.platform.path}/tools/linux

  7. #7
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany NRW
    Posts
    4,438
    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.windows=cmd /c if not exist "{build.path}\sketch\defs.h" "echo.>{build.path}\sketch\defs.h"
    > recipe.hooks.sketch.prebuild.1.pattern.linux=bash -c "[ -f {build.path}/sketch/defs.h ] || touch {build.path}/sketch/defs.h"
    > #recipe.hooks.sketch.prebuild.1.pattern.macosx=bash -c "[ -f {build.path}/sketch/defs.h ] || touch {build.path}/sketch/defs.h"
    [..]
    Full code is here :
    https://forum.pjrc.com/threads/38533...ghlight=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 by Frank B; 08-27-2017 at 07:41 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •