Another "trylock" test.
[lcr.git] / genrc.c
1 /*****************************************************************************\
2 **                                                                           **
3 ** LCR                                                                       **
4 **                                                                           **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg                                              **
7 **                                                                           **
8 ** generate start/stop script                                                **
9 **                                                                           **
10 \*****************************************************************************/ 
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <stdarg.h>
16 #include "macro.h"
17
18 int type[256];
19 int coredebug=0, carddebug=0, dspdebug=0;
20 int lawopt=0;
21
22 struct cards {
23         const char *name;
24         const char *module;
25 } cards[] = {
26 //      { "AVM Fritz PCI (PNP)", "avmfritz"},
27         { "HFC PCI (Cologne Chip)", "hfcpci"},
28         { "HFC-4S / HFC-8S / HFC-E1 (Cologne Chip)", "hfcmulti"},
29         { "HFC-S USB (Cologne Chip)", "hfcsusb"},
30 //      { "HFC-S MINI (Cologne Chip)", "hfcsmini"},
31 //      { "XHFC (Cologne Chip)", "xhfc"},
32 //      { "Sedlbaur FAX", "sedlfax"},
33 //      { "Winbond 6692 PCI", "w6692pci"},
34         {NULL, NULL}
35 };
36
37 int main(void)
38 {
39         FILE *fp;
40         int i = 0, j, jj, n;
41         char input[256], file[256];
42         int ret;
43
44         printf("\n\nThis program generates a script, which is used to start/stop/restart mISDN\n");
45         printf("driver. Please select card only once. Mode and options are given by LCR.\n");
46
47         while(1) {
48                 printf("\nSelect %sdriver for cards:\n\n", i?"another ":"");
49                 jj = 0;
50                 while(cards[jj].name) {
51                         printf(" (%d) %s\n", jj+1, cards[jj].name);
52                         jj++;
53                 }
54                 do {
55                         printf("\nSelect driver number[1-n] (or enter 'done'): "); fflush(stdout);
56                         ret = scanf("%s", input);
57                 } while (atoi(input) <= 0 && !!strcmp(input, "done"));
58                 type[i] = atoi(input);
59                 i++;
60                 if (!strcmp(input, "done"))
61                         break;
62         }
63
64         if (!i) {
65                 printf("\nNo cards defined!\n");
66                 return(-1);
67         }
68
69         printf("\nEnter LAW audio mode. For a-LAW (default), just enter 0. For u-LAW enter 1.\n[0..n | 0xn]: "); fflush(stdout);
70         ret = scanf("%s", input);
71         lawopt = strtoul(input, NULL, 0);
72         printf("\nEnter debugging flags of mISDN core. For no debug, just enter 0.\n[0..n | 0xn]: "); fflush(stdout);
73         ret = scanf("%s", input);
74         coredebug = strtoul(input, NULL, 0);
75         printf("\nEnter debugging flags of cards. For no debug, just enter 0.\n[0..n | 0xn]: "); fflush(stdout);
76         ret = scanf("%s", input);
77         carddebug = strtoul(input, NULL, 0);
78         printf("\nEnter dsp debugging flags of driver. For no debug, just enter 0.\n[0..n | 0xn]: "); fflush(stdout);
79         ret = scanf("%s", input);
80         dspdebug = strtoul(input, NULL, 0);
81
82         n = i;
83
84         printf("\nWhere do you like to load the modules from, enter 0 for default, 1 for\n'/usr/local/lcr/modules/' or the full path.\n[0 | 1 | <path>]: "); fflush(stdout);
85         ret = scanf("%s", input);
86         if (!strcmp(input, "0"))
87                 SCPY(input, "");
88         if (!strcmp(input, "1"))
89                 SCPY(input, "/usr/local/lcr/modules");
90         if (input[0]) if (input[strlen(input)-1] != '/')
91                 SCAT(input, "/");
92
93         printf("\n\nFinally tell me where to write the mISDN rc file.\nEnter the name 'mISDN' for current directory.\nYou may want to say '/usr/local/lcr/mISDN' or '/etc/rc.d/mISDN'\n: "); fflush(stdout);
94         ret = scanf("%s", file);
95         if (!(fp=fopen(file, "w"))) {
96                 fprintf(stderr, "\nError: Failed to open '%s', try again.\n", file);
97                 exit(EXIT_FAILURE);
98         }
99         fprintf(fp, "# rc script for mISDN driver\n\n");
100         fprintf(fp, "case \"$1\" in\n");
101         fprintf(fp, "\tstart|--start)\n");
102         fprintf(fp, "\t\t%s %smISDN_core%s debug=0x%x\n", input[0]?"insmod -f":"modprobe --ignore-install", input, input[0]?".ko":"", coredebug);
103         fprintf(fp, "\t\t%s %smISDN_dsp%s debug=0x%x options=0x%x\n", input[0]?"insmod -f":"modprobe --ignore-install", input, input[0]?".ko":"", dspdebug, lawopt);
104         j = 0;
105         while(cards[j].name) {
106                 jj = 0;
107                 while (jj < n) {
108                         if (type[jj] == j+1)
109                                 fprintf(fp, "\t\t%s %s%s%s debug=0x%x\n", input[0]?"insmod -f":"modprobe --ignore-install", input, cards[j].module, input[0]?".ko":"", carddebug);
110                         jj++;
111                 }
112                 j++;
113         }
114         fprintf(fp, "\t\tsleep 1\n");
115         fprintf(fp, "\t\t;;\n\n");
116         fprintf(fp, "\tstop|--stop)\n");
117         while(j) {
118                 j--;
119                 jj = 0;
120                 while (jj < n) {
121                         if (type[jj] == j+1)
122                                 fprintf(fp, "\t\trmmod %s\n", cards[j].module);
123                         jj++;
124                 }
125         }
126         fprintf(fp, "\t\trmmod mISDN_dsp\n");
127         fprintf(fp, "\t\trmmod mISDN_core\n");
128         fprintf(fp, "\t\t;;\n\n");
129         fprintf(fp, "\trestart|--restart)\n");
130         fprintf(fp, "\t\tsh $0 stop\n");
131         fprintf(fp, "\t\tsleep 2 # some phones will release tei when layer 1 is down\n");
132         fprintf(fp, "\t\tsh $0 start\n");
133         fprintf(fp, "\t\t;;\n\n");
134         fprintf(fp, "\thelp|--help)\n");
135         fprintf(fp, "\t\techo \"Usage: $0 {start|stop|restart|help}\"\n");
136         fprintf(fp, "\t\texit 0\n");
137         fprintf(fp, "\t\t;;\n\n");
138         fprintf(fp, "\t*)\n");
139         fprintf(fp, "\t\techo \"Usage: $0 {start|stop|restart|help}\"\n");
140         fprintf(fp, "\t\texit 2\n");
141         fprintf(fp, "\t\t;;\n\n");
142         fprintf(fp, "esac\n");
143         fclose(fp);
144
145         printf("\nFile '%s' is written to the current directory.\n", file);
146 }
147
148