cae-tools/spool/spool.c

86 lines
1.5 KiB
C

/*
* spool.c - Send a file to a Roland MDX series CNC mill
*
* Written 2009, 2010, 2012 by Werner Almesberger
* Copyright 2009, 2010, 2012 Werner Almesberger
*
* 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.
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include "serial.h"
#define DEFAULT_PORT "/dev/ttyS0"
#define BUF_SIZE 8192
static void spool_file(FILE *file)
{
char buf[BUF_SIZE+1];
size_t n;
while (1) {
n = fread(buf, 1, BUF_SIZE, file);
if (!n)
break;
buf[n] = 0;
serial_printf("%s", buf);
}
}
static void usage(const char *name)
{
fprintf(stderr, "usage: %s [file ...]\n", name);
exit(1);
}
int main(int argc, const char **argv)
{
FILE **files;
char *port;
int n_files, i;
/*
* Open all files before starting to spool, so that we don't risk
* failing in the middle of the job.
*/
n_files = argc == 1 ? 1 : argc-1;
files = malloc(sizeof(FILE *));
if (!files) {
perror("malloc");
exit(1);
}
if (argc == 1) {
*files = stdin;
} else {
if (*argv[1] == '-')
usage(*argv);
for (i = 0; i != n_files; i++) {
files[i] = fopen(argv[i+1], "r");
if (!files[i]) {
perror(argv[i+1]);
exit(1);
}
}
}
port = getenv("PORT");
serial_open(port ? port : DEFAULT_PORT);
for (i = 0; i != n_files; i++)
spool_file(files[i]);
sleep(3600);
serial_close();
return 0;
}