sigap.c (2027B)
1 /* Copyright 2021 Jacob R. Edwards 2 * 3 * This program is free software: you can redistribute it and/or modify 4 * it under the terms of the GNU General Public License as published by 5 * the Free Software Foundation, either version 3 of the License, or 6 * (at your option) any later version. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <https://www.gnu.org/licenses/>. 15 */ 16 17 #include <ap.h> 18 19 #include <fcntl.h> 20 #include <signal.h> 21 #include <stdio.h> 22 #include <unistd.h> 23 24 static int info; 25 static int loop; 26 static int next; 27 static int persist; 28 static int quit; 29 30 void 31 handle(int sig) 32 { 33 switch (sig) { 34 case SIGINFO: 35 info = 1; 36 break; 37 case SIGINT: 38 next = 1; 39 break; 40 case SIGQUIT: 41 quit = 1; 42 break; 43 } 44 } 45 46 int 47 main(int argc, char *argv[]) 48 { 49 int c; 50 int fd; 51 int i; 52 sample buf[4096]; 53 ssize_t wrote; 54 struct ap *ap; 55 56 while ((c = getopt(argc, argv, "lp")) != -1) { 57 switch (c) { 58 case 'l': 59 loop = 1; 60 break; 61 case 'p': 62 persist = 1; 63 break; 64 default: 65 fprintf(stderr, "usage: %s [-lp] file ...\n", 66 *argv); 67 return 1; 68 } 69 } 70 argc -= optind; 71 argv += optind; 72 73 signal(SIGINFO, handle); 74 signal(SIGINT, handle); 75 signal(SIGQUIT, handle); 76 77 ao_initialize(); 78 79 for (i = 0; i < argc; ++i) { 80 ap = apopen((fd = open(argv[i], O_RDONLY))); 81 if (ap == NULL) { 82 aperror: 83 perror(argv[i]); 84 if (persist) 85 continue; 86 exit(1); 87 } 88 89 info = 1; 90 while (!next && 91 (wrote = applay(ap, buf, sizeof(buf) / sizeof(*buf))) > 0) { 92 if (quit) { 93 apclose(ap); 94 return 0; 95 } 96 if (info) { 97 info = 0; 98 puts(argv[i]); 99 } 100 } 101 102 apclose(ap); 103 if (loop && i + 1 == argc) 104 i = 0; 105 if (next) 106 next = 0; 107 if (wrote < 0) 108 goto aperror; 109 } 110 111 ao_shutdown(); 112 return 0; 113 }