1 /*
2  *  powwow-movieplay.c  --  replay powwow movies or convert them into ASCII
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  */
10 
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <sys/time.h>
15 #include <sys/types.h>
16 #include <unistd.h>
17 
millisec_sleep(msec)18 void millisec_sleep(msec)
19 long msec;
20 {
21     struct timeval t;
22     t.tv_sec = msec / 1000;
23     t.tv_usec = (msec % 1000) * 1000;
24     select(0, NULL, NULL, NULL, &t);
25 }
26 
main(argc,argv)27 int main(argc, argv)
28 int argc; char *argv[];
29 {
30     FILE *infile, *outfile;
31     char buf[4096];
32     int i, play = 0;
33 
34     if (strstr(argv[0], "powwow-movieplay"))
35 	play = 1;
36     else if (!strstr(argv[0], "powwow-movie2ascii")) {
37 	fprintf(stderr, "Please run this program as \"powwow-movieplay\" or \"powwow-movie2ascii\"\n");
38 	return 1;
39     }
40 
41     if (play) {
42 	if (argc == 2) {
43 	    infile = fopen(argv[1], "rb");
44 	    outfile = stdout;
45 	    if (infile == NULL) {
46 		fprintf(stderr, "Error opening input file \"%s\"\n", argv[1]);
47 		return 1;
48 	    }
49 	} else {
50 	    infile = stdin;
51 	    outfile = stdout;
52 	}
53     } else {
54 	if (argc == 3) {
55 	    infile = fopen(argv[1], "rb");
56 	    outfile = fopen(argv[2], "wb");
57 	    if (infile == NULL) {
58 		fprintf(stderr, "Error opening input file \"%s\"\n", argv[1]);
59 		return 1;
60 	    }
61 	    if (outfile == NULL) {
62 		fprintf(stderr, "Error opening output file \"%s\"\n", argv[2]);
63 		return 1;
64 	    }
65 	} else {
66 	    fprintf(stderr, "Usage: %s [infile [outfile]]\n", argv[0]);
67 	    return 1;
68 	}
69     }
70 
71     while (fgets(buf, 4096, infile) != NULL) {
72 	i = strlen(buf);
73 	if (i > 0 && buf[i-1] == '\n')
74 	    buf[i-1] = '\0';
75 	if (!strncmp(buf, "sleep ", 6)) {
76 	    if (play)
77 		millisec_sleep(atoi(buf + 6));
78 	}
79 	else if (!strncmp(buf, "line ", 5))
80 	    fprintf(outfile, "%s\n", buf + 5);
81 	else if (!strncmp(buf, "prompt ", 7))
82 	    fprintf(outfile, "%s", buf + 7);
83 	else {
84 	    fprintf(stderr, "Syntax error in line:\n%s\n", buf);
85 	    return 1;
86 	}
87 	fflush(outfile);
88     }
89     if (feof(infile)) {
90 	fprintf(outfile, "\n");
91 	return 0;
92     } else {
93 	fprintf(stderr, "Error reading file\n");
94 	return 1;
95     }
96 }
97 
98