prout

print out papers using libcups
git clone git://z3bra.org/prout
Log | Files | Refs | README | LICENSE

commit f851bb0901d8084ae7eb2ef43af64b93d633c17c
parent 29e9c5ae17148c2569f3a5c7119157df2819a17d
Author: z3bra <willy@mailoo.org>
Date:   Mon Feb 10 10:08:19 2014

More sane Makefile

Diffstat:
 Makefile       |  72 +++++------------------
 man/prout.1.gz | Bin 1029 -> 0 bytes
 prout.1.gz     | Bin 0 -> 1029 bytes
 prout.c        | 183 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 src/prout.c    | 183 +----------------------------------------------------------
 5 files changed, 201 insertions(+), 237 deletions(-)

diff --git a/Makefile b/Makefile @@ -9,71 +9,35 @@ LDFLAGS+=`cups-config --libs` # Command paths RM=/bin/rm -# Directories -SRCDIR=src -INCDIR=inc -OBJDIR=obj -BINDIR=bin -ETCDIR=etc -QOTDIR=dat -MANDIR=man PREFIX:=/usr -MANPREFIX:=$(PREFIX)/share/man +MANPREFIX:=${PREFIX}/share/man -# Filetype paths -vpath %.c $(SRCDIR) -vpath %.h $(INCDIR) +.SUFFIXES: +.SUFFIXES: .c .h .o +.PHONY : all clean init install uninstall -# Files -SRC:=$(wildcard $(SRCDIR)/*.c) -OBJ:=$(patsubst $(SRCDIR)/%.c,$(OBJDIR)/%.o,$(SRC)) -INC:=$(wildcard $(INCDIR)/*.h) -QOT:=$(wildcard $(QOTDIR)/*.qot) -ETC:=$(ETCDIR)/$(PROG).conf -BIN:=$(BINDIR)/$(PROG) - -.PHONY : all echo mrproper clean init install uninstall - -$(BIN) : $(OBJ) - @if [[ ! -d $(BINDIR) ]]; then mkdir $(BINDIR); fi - @echo -e "LD $(PROG)" - @$(CC) $^ -o $@ $(LDFLAGS) - -$(OBJDIR)/%.o : $(SRCDIR)/%.c - @if [[ ! -d $(OBJDIR) ]]; then mkdir $(OBJDIR); fi +.c.o: @echo -e "CC $<" - @$(CC) -c $(CFLAGS) $< -o $@ + @${CC} -c ${CFLAGS} $< -o $@ -all : init $(BIN) $(INC) +prout: prout.o + @echo -e "LD prout" + @${CC} $^ -o $@ ${LDFLAGS} -list : - @echo "SOURCES : $(SRC)" - @echo "OBJECTS : $(OBJ)" - @echo "INCLUDE : $(INC)" - @echo "BINARY : $(BIN)" - -mrproper : clean - $(RM) $(BIN) +all : prout clean : - $(RM) -f $(OBJDIR)/*.o - $(RM) -f *~ - -init : - @echo "CC = $(CC)" - @echo "CFLAGS = $(CFLAGS)" - @echo "LDFLAGS = $(LDFLAGS)" - @echo + ${RM} -f prout + ${RM} -f *.o + ${RM} -f *~ -install: $(BIN) - install -d $(DESTDIR)$(PREFIX)/bin $(DESTDIR)$(PREFIX)/share/$(PROG) $(DESTDIR)$(MANPREFIX)/man1 - install -m 755 bin/$(PROG) $(DESTDIR)$(PREFIX)/bin/$(PROG) - install -m 644 $(MANDIR)/$(PROG).1.gz $(DESTDIR)$(MANPREFIX)/man1/$(PROG).1.gz +install: ${BIN} + install -D -m 0755 bin/prout ${DESTDIR}${PREFIX}/bin/prout + install -D -m 0644 ${MANDIR}/prout.1.gz ${DESTDIR}${MANPREFIX}/man1/prout.1.gz uninstall: - $(RM) $(DESTDIR)$(PREFIX)/bin/$(PROG) - $(RM) -r $(DESTDIR)$(PREFIX)/share/$(PROG) - $(RM) $(DESTDIR)$(MANPREFIX)/man1/$(PROG).1.gz + ${RM} ${DESTDIR}${PREFIX}/bin/prout + ${RM} ${DESTDIR}${MANPREFIX}/man1/prout.1.gz ## EOF diff --git a/man/prout.1.gz b/man/prout.1.gz Binary files differ diff --git a/prout.1.gz b/prout.1.gz Binary files differ diff --git a/prout.c b/prout.c @@ -0,0 +1,183 @@ +/* + * DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE + * Version 2, December 2004 + * + * Copyright (C) 2004 Sam Hocevar <sam@hocevar.net> + * + * Everyone is permitted to copy and distribute verbatim or modified + * copies of this license document, and changing it is allowed as long + * as the name is changed. + * + * DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE + * TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + * + * 0. You just DO WHAT THE FUCK YOU WANT TO. + * + */ + +#include <stdio.h> +#include <stdbool.h> +#include <cups/cups.h> + +/* Usage function */ +static void usage() +{ + fputs("usage: prout [-h] <file>\n", stdout); +} + +/* Return the file name if the file is opennable. exit instead */ +static char* check_file(char *file) +{ + + FILE *f; + + /* Try to open the given file */ + f = fopen(file, "r"); + + if (!f) { + fputs("Cannot open the given file\n", stderr); + exit (1); + } + + /* Don't let the file close longer */ + fclose(f); + + return file; +} + +/* Return a pointer to the destion if reachable. exit instead */ +static cups_dest_t* check_dest() +{ + int num_dests; + cups_dest_t *dests, *dest; + + /* Get the number of destionation configured */ + num_dests = cupsGetDests(&dests); + + /* Leave if there is no default printer configured */ + if (num_dests < 1) { + fputs("No default printer configured\n", stderr); + exit (1); + } + + /* Get the default destination from all dests */ + dest = cupsGetDest(NULL, NULL, 1, dests); + + /* No need to go on if we can't reach the printer */ + if (!dest) { + fputs("Cannot get a pointer to the default destination", stderr); + exit (1); + } + + return (dest); +} + +/* Return the job id of the printed file */ +static int print(const char* file, cups_dest_t *dest) +{ + int job_id = -1; + + /* Create a new job to print the document */ + job_id = cupsPrintFile (dest->name, file, "pp file", 0, NULL); + + fprintf(stdout, "printing %s (%d) to %s.\n", file, job_id, dest->name); + + return job_id; +} + +/* Blocking function. Wait util the job is completed, or an error is thrown */ +static void check_job (cups_dest_t *dest, int job_id) +{ + int i, num_jobs; + + cups_job_t *jobs; + + ipp_jstate_t state = IPP_JOB_PENDING; + + while (state < IPP_JOB_STOPPED) { + /* GET ALL THE JOBS "\_(°D°)/ */ + num_jobs = cupsGetJobs(&jobs, dest->name, 1, CUPS_WHICHJOBS_ALL); + + /* Find MY job id */ + for (i = 0; i < num_jobs; i++) { + if (jobs[i].id == job_id) { + state = jobs[i].state; + break; + } + } + + /* Free the job array */ + cupsFreeJobs(num_jobs, jobs); + + switch (state) { + case IPP_JOB_PENDING : + fprintf(stdout, "\rjob %d pending.", job_id); + break; + case IPP_JOB_HELD : + fprintf(stdout, "\rjob %d held.", job_id); + break; + case IPP_JOB_PROCESSING : + fprintf(stdout, "\rjob %d processing.", job_id); + break; + case IPP_JOB_STOPPED : + fprintf(stdout, "\rjob %d stopped.", job_id); + break; + case IPP_JOB_CANCELED : + fprintf(stdout, "\rjob %d canceled.", job_id); + break; + case IPP_JOB_ABORTED : + fprintf(stdout, "\rjob %d aborted.", job_id); + break; + case IPP_JOB_COMPLETED : + fprintf(stdout, "\rjob %d completed.\n", job_id); + break; + } + + /* Actually print text to stdout */ + fflush(stdout); + + /* Wait just a second if the job is not finished */ + if (state < IPP_JOB_STOPPED) { + sleep(1); + } + } + + return; +} + +int main(int argc, char **argv) +{ + int i = 0; + int job_id = -1; + + char *file; + + cups_dest_t *dest; + + /* check the given arguments */ + if (argc < 2) { + usage(); + exit (1); + } + + for (i=1; (i < argc) && (argv[i][0] == '-'); i++) { + switch (argv[i][1]) { + case 'h': usage(); exit(0); + default: usage(); exit(1); + } + } + + /* check if the file is correct */ + file = check_file(argv[1]); + + /* Is the default printer set up ? Can we use it ? */ + dest = check_dest(); + + /* Well, let's print the file then */ + job_id = print(file, dest); + + /* How is the printing going ? */ + check_job(dest, job_id); + + return (0); +} diff --git a/src/prout.c b/src/prout.c @@ -1,183 +0,0 @@ -/* - * DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE - * Version 2, December 2004 - * - * Copyright (C) 2004 Sam Hocevar <sam@hocevar.net> - * - * Everyone is permitted to copy and distribute verbatim or modified - * copies of this license document, and changing it is allowed as long - * as the name is changed. - * - * DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE - * TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION - * - * 0. You just DO WHAT THE FUCK YOU WANT TO. - * - */ - -#include <stdio.h> -#include <stdbool.h> -#include <cups/cups.h> - -/* Usage function */ -static void usage() -{ - fputs("usage: prout [-h] <file>\n", stdout); -} - -/* Return the file name if the file is opennable. exit instead */ -static char* check_file(char *file) -{ - - FILE *f; - - /* Try to open the given file */ - f = fopen(file, "r"); - - if (!f) { - fputs("Cannot open the given file\n", stderr); - exit (1); - } - - /* Don't let the file close longer */ - fclose(f); - - return file; -} - -/* Return a pointer to the destion if reachable. exit instead */ -static cups_dest_t* check_dest() -{ - int num_dests; - cups_dest_t *dests, *dest; - - /* Get the number of destionation configured */ - num_dests = cupsGetDests(&dests); - - /* Leave if there is no default printer configured */ - if (num_dests < 1) { - fputs("No default printer configured\n", stderr); - exit (1); - } - - /* Get the default destination from all dests */ - dest = cupsGetDest(NULL, NULL, 1, dests); - - /* No need to go on if we can't reach the printer */ - if (!dest) { - fputs("Cannot get a pointer to the default destination", stderr); - exit (1); - } - - return (dest); -} - -/* Return the job id of the printed file */ -static int print(const char* file, cups_dest_t *dest) -{ - int job_id = -1; - - /* Create a new job to print the document */ - job_id = cupsPrintFile (dest->name, file, "pp file", 0, NULL); - - fprintf(stdout, "printing %s (%d) to %s.\n", file, job_id, dest->name); - - return job_id; -} - -/* Blocking function. Wait util the job is completed, or an error is thrown */ -static void check_job (cups_dest_t *dest, int job_id) -{ - int i, num_jobs; - - cups_job_t *jobs; - - ipp_jstate_t state = IPP_JOB_PENDING; - - while (state < IPP_JOB_STOPPED) { - /* GET ALL THE JOBS "\_(°D°)/ */ - num_jobs = cupsGetJobs(&jobs, dest->name, 1, CUPS_WHICHJOBS_ALL); - - /* Find MY job id */ - for (i = 0; i < num_jobs; i++) { - if (jobs[i].id == job_id) { - state = jobs[i].state; - break; - } - } - - /* Free the job array */ - cupsFreeJobs(num_jobs, jobs); - - switch (state) { - case IPP_JOB_PENDING : - fprintf(stdout, "\rjob %d pending.", job_id); - break; - case IPP_JOB_HELD : - fprintf(stdout, "\rjob %d held.", job_id); - break; - case IPP_JOB_PROCESSING : - fprintf(stdout, "\rjob %d processing.", job_id); - break; - case IPP_JOB_STOPPED : - fprintf(stdout, "\rjob %d stopped.", job_id); - break; - case IPP_JOB_CANCELED : - fprintf(stdout, "\rjob %d canceled.", job_id); - break; - case IPP_JOB_ABORTED : - fprintf(stdout, "\rjob %d aborted.", job_id); - break; - case IPP_JOB_COMPLETED : - fprintf(stdout, "\rjob %d completed.\n", job_id); - break; - } - - /* Actually print text to stdout */ - fflush(stdout); - - /* Wait just a second if the job is not finished */ - if (state < IPP_JOB_STOPPED) { - sleep(1); - } - } - - return; -} - -int main(int argc, char **argv) -{ - int i = 0; - int job_id = -1; - - char *file; - - cups_dest_t *dest; - - /* check the given arguments */ - if (argc < 2) { - usage(); - exit (1); - } - - for (i=1; (i < argc) && (argv[i][0] == '-'); i++) { - switch (argv[i][1]) { - case 'h': usage(); exit(0); - default: usage(); exit(1); - } - } - - /* check if the file is correct */ - file = check_file(argv[1]); - - /* Is the default printer set up ? Can we use it ? */ - dest = check_dest(); - - /* Well, let's print the file then */ - job_id = print(file, dest); - - /* How is the printing going ? */ - check_job(dest, job_id); - - return (0); -}