Root/spool/spool.c

1/*
2 * spool.c - Send a file to a Roland MDX series CNC mill
3 *
4 * Written 2009, 2010, 2012 by Werner Almesberger
5 * Copyright 2009, 2010, 2012 Werner Almesberger
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13
14#include <stdlib.h>
15#include <stdio.h>
16#include <unistd.h>
17
18#include "serial.h"
19
20
21#define DEFAULT_PORT "/dev/ttyS0"
22#define BUF_SIZE 8192
23
24
25static void spool_file(FILE *file)
26{
27    char buf[BUF_SIZE+1];
28    size_t n;
29
30    while (1) {
31        n = fread(buf, 1, BUF_SIZE, file);
32        if (!n)
33            break;
34        buf[n] = 0;
35        serial_printf("%s", buf);
36    }
37}
38
39
40static void usage(const char *name)
41{
42    fprintf(stderr, "usage: %s [file ...]\n", name);
43    exit(1);
44}
45
46
47int main(int argc, const char **argv)
48{
49    FILE **files;
50    char *port;
51    int n_files, i;
52
53    /*
54     * Open all files before starting to spool, so that we don't risk
55     * failing in the middle of the job.
56     */
57    n_files = argc == 1 ? 1 : argc-1;
58    files = malloc(sizeof(FILE *));
59    if (!files) {
60        perror("malloc");
61        exit(1);
62    }
63
64    if (argc == 1) {
65        *files = stdin;
66    } else {
67        if (*argv[1] == '-')
68            usage(*argv);
69        for (i = 0; i != n_files; i++) {
70            files[i] = fopen(argv[i+1], "r");
71            if (!files[i]) {
72                perror(argv[i+1]);
73                exit(1);
74            }
75        }
76    }
77
78    port = getenv("PORT");
79    serial_open(port ? port : DEFAULT_PORT);
80    for (i = 0; i != n_files; i++)
81        spool_file(files[i]);
82sleep(3600);
83    serial_close();
84    return 0;
85}
86

Archive Download this file

Branches:
master



interactive